cat-dev 0.0.13

A library for interacting with the CAT-DEV hardware units distributed by Nintendo (i.e. a type of Wii-U DevKits).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
//! Definitions for the `GetQueryByInfo` packet type, and it's response types.
//!
//! Although this name implies there's some sort of querying, or something
//! going on there's actually not. You simply give us a file path, and we
//! give you either file information, the count of files in a directory, or
//! the size of a particular file. Wow.

use crate::{
	errors::NetworkParseError,
	fsemul::{
		HostFilesystem,
		pcfs::errors::{PcfsApiError, SataProtocolError},
	},
};
use bytes::{Buf, BufMut, Bytes, BytesMut};
use std::{
	ffi::CStr,
	fs::Metadata,
	path::PathBuf,
	sync::LazyLock,
	time::{Duration, SystemTime},
};
use valuable::{Fields, NamedField, NamedValues, StructDef, Structable, Valuable, Value, Visit};

/// Timestamps are "FAT" timestamps which start in 1980.
static FAT_TIMESTAMP_START: LazyLock<SystemTime> = LazyLock::new(|| {
	SystemTime::UNIX_EPOCH
		.checked_add(Duration::from_secs(315_540_000))
		.expect("Failed to get timestamp for 1980! required!")
});

/// A packet to get information about a particular directory path.
///
/// This can do everything from "get the free space of the disk this path
/// is on", to "get my some metadata about this very specific path."
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SataGetInfoByQueryPacketBody {
	/// The path to query, note that this is not the 'resolved' path which is
	/// the path to actual read from.
	///
	/// Interpolation has a few known ways of being replaced:
	///
	/// - `%MLC_EMU_DIR`: `<cafe_sdk>/data/mlc/`
	/// - `%SLC_EMU_DIR`: `<cafe_sdk>/data/slc/`
	/// - `%DISC_EMU_DIR`: `<cafe_sdk>/data/disc/`
	/// - `%SAVE_EMU_DIR`: `<cafe_sdk>/data/save/`
	/// - `%NETWORK`: <mounted network share path>
	path: String,
	/// The type of information we're looking for.
	typ: SataQueryType,
}

impl SataGetInfoByQueryPacketBody {
	/// Attempt to construct a new packet to get info about some path.
	///
	/// ## Errors
	///
	/// If the path is longer than 511 bytes. Normally the max path is 512 bytes,
	/// but because we need to encode our data as a C-String with a NUL
	/// terminator we cannot be longer than 511 bytes.
	///
	/// Consider using relative/mapped paths if possible when dealing with long
	/// paths.
	pub fn new(path: String, query_type: SataQueryType) -> Result<Self, PcfsApiError> {
		if path.len() > 511 {
			return Err(PcfsApiError::PathTooLong(path));
		}

		Ok(Self {
			path,
			typ: query_type,
		})
	}

	#[must_use]
	pub const fn query_type(&self) -> SataQueryType {
		self.typ
	}

	pub const fn set_query_type(&mut self, new_type: SataQueryType) {
		self.typ = new_type;
	}

	#[must_use]
	pub fn path(&self) -> &str {
		self.path.as_str()
	}

	/// Update the path to send in this particular get info by query packet.
	///
	/// ## Errors
	///
	/// If the path is longer than 511 bytes. Normally the max path is 512 bytes,
	/// but because we need to encode our data as a C-String with a NUL
	/// terminator we cannot be longer than 511 bytes.
	///
	/// Consider using relative/mapped paths if possible when dealing with long
	/// paths.
	pub fn set_path(&mut self, new_path: String) -> Result<(), PcfsApiError> {
		if new_path.len() > 511 {
			return Err(PcfsApiError::PathTooLong(new_path));
		}

		self.path = new_path;
		Ok(())
	}
}

impl From<&SataGetInfoByQueryPacketBody> for Bytes {
	fn from(value: &SataGetInfoByQueryPacketBody) -> Self {
		let mut result = BytesMut::with_capacity(0x204);
		result.extend_from_slice(value.path.as_bytes());
		// These are C Strings so we need a NUL terminator.
		// Pad with `0`, til we get a full path with a nul terminator.
		result.extend(BytesMut::zeroed(0x200 - result.len()));
		result.put_u32(u32::from(value.typ));
		result.freeze()
	}
}

impl From<SataGetInfoByQueryPacketBody> for Bytes {
	fn from(value: SataGetInfoByQueryPacketBody) -> Self {
		Self::from(&value)
	}
}

impl TryFrom<Bytes> for SataGetInfoByQueryPacketBody {
	type Error = NetworkParseError;

	fn try_from(value: Bytes) -> Result<Self, Self::Error> {
		if value.len() < 0x204 {
			return Err(NetworkParseError::FieldNotLongEnough(
				"SataGetInfoByQuery",
				"Body",
				0x204,
				value.len(),
				value,
			));
		}
		if value.len() > 0x204 {
			return Err(NetworkParseError::UnexpectedTrailer(
				"SataGetInfoByQueryBody",
				value.slice(0x204..),
			));
		}

		let (path_bytes, num) = value.split_at(0x200);
		let path_c_str =
			CStr::from_bytes_until_nul(path_bytes).map_err(NetworkParseError::BadCString)?;
		let query_type = u32::from_be_bytes([num[0], num[1], num[2], num[3]]);
		let final_path = path_c_str.to_str()?.to_owned();

		Ok(Self {
			path: final_path,
			typ: SataQueryType::try_from(query_type)?,
		})
	}
}

const SATA_GET_INFO_BY_QUERY_PACKET_BODY_FIELDS: &[NamedField<'static>] =
	&[NamedField::new("path"), NamedField::new("type")];

impl Structable for SataGetInfoByQueryPacketBody {
	fn definition(&self) -> StructDef<'_> {
		StructDef::new_static(
			"SataGetInfoByQueryPacketBody",
			Fields::Named(SATA_GET_INFO_BY_QUERY_PACKET_BODY_FIELDS),
		)
	}
}

impl Valuable for SataGetInfoByQueryPacketBody {
	fn as_value(&self) -> Value<'_> {
		Value::Structable(self)
	}

	fn visit(&self, visitor: &mut dyn Visit) {
		visitor.visit_named_fields(&NamedValues::new(
			SATA_GET_INFO_BY_QUERY_PACKET_BODY_FIELDS,
			&[
				Valuable::as_value(&self.path),
				Valuable::as_value(&self.typ),
			],
		));
	}
}

/// The type of information we're looking for from our request.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Valuable)]
pub enum SataQueryType {
	/// Get the amount of free disk space available to the calling application.
	FreeDiskSpace,
	/// Get the size of files in a directory, recursively.
	SizeOfFolder,
	/// Get the number of files in a directory, recursively.
	FileCount,
	/// Get the information around a particular file or folder.
	FileDetails,
}

impl From<SataQueryType> for u32 {
	fn from(value: SataQueryType) -> Self {
		match value {
			SataQueryType::FreeDiskSpace => 0,
			SataQueryType::SizeOfFolder => 1,
			SataQueryType::FileCount => 2,
			SataQueryType::FileDetails => 5,
		}
	}
}

impl TryFrom<u32> for SataQueryType {
	type Error = SataProtocolError;

	fn try_from(value: u32) -> Result<Self, Self::Error> {
		match value {
			0 => Ok(Self::FreeDiskSpace),
			1 => Ok(Self::SizeOfFolder),
			2 => Ok(Self::FileCount),
			5 => Ok(Self::FileDetails),
			val => Err(SataProtocolError::UnknownGetInfoQueryType(val)),
		}
	}
}

/// A response that came from a particular query response.
///
/// These depends on the query type that was actively passed in. Most paths
/// just return a very basic "size" (e.g. file count, or file length, etc.).
/// However the file stat query type actually returns all the information about
/// a particular path.
#[derive(Clone, Debug, Valuable, PartialEq, Eq)]
pub enum SataQueryResponse {
	/// An error has occured, and we are returning an error code.
	ErrorCode(u32),
	/// A size response that is guaranteed to fit within a u32.
	///
	/// Query types that return this:
	///
	/// - [`SataQueryType::FileCount`]
	SmallSize(u32),
	/// A size response that is guaranteed to fit within a u64.
	///
	/// Query types that return this:
	///
	/// - [`SataQueryType::SizeOfFolder`]
	/// - [`SataQueryType::FreeDiskSpace`]
	LargeSize(u64),
	/// All the info about a particular file path.
	///
	/// Query types that return this:
	///
	/// - [`SataQueryType::FileDetails`]
	FDInfo(SataFDInfo),
}

impl SataQueryResponse {
	/// Try to read a [`SataQueryResponse::SmallSize`] from a full response
	/// body.
	///
	/// ## Errors
	///
	/// If the response had an error code, or we could not get all of the [`u32`]
	/// value of the body.
	pub fn try_from_small(mut value: Bytes) -> Result<Self, NetworkParseError> {
		let rc = value.get_u32();
		if rc != 0 {
			return Err(NetworkParseError::ErrorCode(rc));
		}

		let smol = value.get_u32();

		Ok(Self::SmallSize(smol))
	}

	/// Try to read a [`SataQueryResponse::LargeSize`] from a full response
	/// body.
	///
	/// ## Errors
	///
	/// If the response had an error code, or we could not get all of the [`u64`]
	/// value of the body.
	pub fn try_from_large(mut value: Bytes) -> Result<Self, NetworkParseError> {
		let rc = value.get_u32();
		if rc != 0 {
			return Err(NetworkParseError::ErrorCode(rc));
		}

		let larg = value.get_u64();

		Ok(Self::LargeSize(larg))
	}

	/// Try to read a [`SataQueryResponse::FDInfo`] from a full response
	/// body.
	///
	/// ## Errors
	///
	/// If the response had an error code, or we could not get the file info from
	/// the file.
	pub fn try_from_fd_info(mut value: Bytes) -> Result<Self, NetworkParseError> {
		let rc = value.get_u32();
		if rc != 0 {
			return Err(NetworkParseError::ErrorCode(rc));
		}

		let fd_info = SataFDInfo::try_from(value)?;

		Ok(Self::FDInfo(fd_info))
	}
}

impl From<&SataQueryResponse> for Bytes {
	fn from(value: &SataQueryResponse) -> Self {
		match value {
			SataQueryResponse::FDInfo(fd_info) => {
				let mut buff = BytesMut::with_capacity(88);
				buff.put_u32(0);
				buff.extend(Bytes::from(fd_info));
				buff.freeze()
			}
			SataQueryResponse::LargeSize(lorg) => {
				let mut buff = BytesMut::with_capacity(88);
				buff.put_u32(0);
				buff.put_u64(*lorg);
				buff.extend([0; 76]);
				buff.freeze()
			}
			SataQueryResponse::SmallSize(smol) => {
				let mut buff = BytesMut::with_capacity(88);
				buff.put_u32(0);
				buff.put_u32(*smol);
				buff.extend([0; 80]);
				buff.freeze()
			}
			SataQueryResponse::ErrorCode(ec) => {
				let mut buff = BytesMut::with_capacity(88);
				buff.put_u32(*ec);
				buff.extend_from_slice(&[0; 84]);
				buff.freeze()
			}
		}
	}
}

impl From<SataQueryResponse> for Bytes {
	fn from(value: SataQueryResponse) -> Self {
		Self::from(&value)
	}
}

#[derive(Clone, Debug, PartialEq, Eq, Valuable)]
/// The info related to the file/directory of the path queried.
pub struct SataFDInfo {
	/// The raw underlying flags for the file/directory at the path queried.
	file_or_folder_flags: u32,
	/// The permissions bits for the file/directory at the path queried.
	perms: u32,
	/// The length of the file if this is an actual file, otherwise it _should_
	/// be set to 0.
	file_length: u32,
	/// A FAT-TS like timestamp for when this path was created.
	created_timestamp: u64,
	/// A FAT-TS like timestamp for when this path was last updated.
	last_updated_timestamp: u64,
}

impl SataFDInfo {
	/// File information for a particular file descriptor/path.
	#[must_use]
	pub async fn get_info(
		host_filesystem: &HostFilesystem,
		metadata: &Metadata,
		path: &PathBuf,
	) -> Self {
		let is_read_only = if metadata.is_dir() {
			host_filesystem.folder_is_read_only(path).await
		} else {
			metadata.permissions().readonly()
		};

		let file_or_folder_flags = if metadata.is_file() {
			0x2C00_0000
		} else {
			0xAC00_0000
		};
		let perms = if is_read_only { 0x444 } else { 0x666 };
		let file_length = if metadata.is_dir() {
			0
		} else {
			u32::try_from(metadata.len()).unwrap_or(u32::MAX)
		};
		let created_timestamp = u64::try_from(
			metadata
				.created()
				.unwrap_or(SystemTime::now())
				.duration_since(*FAT_TIMESTAMP_START)
				.unwrap_or(Duration::from_secs(0))
				.as_millis(),
		)
		.unwrap_or(u64::MAX);
		let updated_timestamp = u64::try_from(
			metadata
				.modified()
				.unwrap_or(SystemTime::now())
				.duration_since(*FAT_TIMESTAMP_START)
				.unwrap_or(Duration::from_secs(0))
				.as_millis(),
		)
		.unwrap_or(u64::MAX);

		Self {
			file_or_folder_flags,
			perms,
			file_length,
			created_timestamp,
			last_updated_timestamp: updated_timestamp,
		}
	}

	/// Create a fake fd info from totally controlled values.
	#[must_use]
	pub fn create_fake_info(
		file_or_folder_flags: u32,
		perms: u32,
		file_length: u32,
		created_timestamp: u64,
		updated_timestamp: u64,
	) -> Self {
		Self {
			file_or_folder_flags,
			perms,
			file_length,
			created_timestamp,
			last_updated_timestamp: updated_timestamp,
		}
	}

	/// Get the raw file or folder type flags for a particular path.
	#[must_use]
	pub const fn flags(&self) -> u32 {
		self.file_or_folder_flags
	}

	/// Check if this path actually exists on disk.
	#[must_use]
	pub const fn exists(&self) -> bool {
		(self.file_or_folder_flags & 0x2000_0000) != 0
	}

	/// Check if this path was interpreted as a file.
	#[must_use]
	pub const fn is_file(&self) -> bool {
		(self.file_or_folder_flags & 0x8000_0000) == 0
	}

	/// Check if this path was interpreted as a directory.
	#[must_use]
	pub const fn is_directory(&self) -> bool {
		!self.is_file()
	}

	/// Get the unix permissions that exists on this file.
	///
	/// Given this is based originally on a windows filesystem, which really only
	/// has a natural equivalent for read only flags. You will either get
	/// `0x666`, or `0x444`.
	#[must_use]
	pub const fn permissions(&self) -> u32 {
		self.perms
	}

	/// The size of a file, if we are actually pointing at a file.
	#[must_use]
	pub const fn file_size(&self) -> Option<u32> {
		if self.is_file() {
			Some(self.file_length)
		} else {
			None
		}
	}

	/// A FAT-like timestamp that may be wrapped around.
	///
	/// Access the raw underlying value.
	#[must_use]
	pub const fn raw_created_timestamp(&self) -> u64 {
		self.created_timestamp
	}

	/// A FAT-like timestamp that may be wrapped around.
	///
	/// Access the raw underlying value.
	#[must_use]
	pub const fn raw_last_updated_timestamp(&self) -> u64 {
		self.last_updated_timestamp
	}
}

impl From<&SataFDInfo> for Bytes {
	fn from(value: &SataFDInfo) -> Self {
		let mut buff = BytesMut::with_capacity(84);
		buff.put_u32(value.file_or_folder_flags);
		buff.put_u32(value.perms);
		buff.put_u32(1);
		buff.put_u32(1);
		buff.put_u32(value.file_length);
		buff.put_u32(0);
		buff.put_u32(0xE8);
		buff.put_u32(0xDA6F_F000);
		buff.put_u32(0);
		buff.put_u64(value.created_timestamp);
		buff.put_u64(value.last_updated_timestamp);
		buff.extend_from_slice(&[0; 32]);
		buff.freeze()
	}
}

impl From<SataFDInfo> for Bytes {
	fn from(value: SataFDInfo) -> Self {
		Self::from(&value)
	}
}

impl TryFrom<Bytes> for SataFDInfo {
	type Error = NetworkParseError;

	fn try_from(mut value: Bytes) -> Result<Self, Self::Error> {
		if value.len() < 84 {
			return Err(NetworkParseError::FieldNotLongEnough(
				"SataFDInfo",
				"Body",
				84,
				value.len(),
				value,
			));
		}
		if value.len() > 84 {
			return Err(NetworkParseError::UnexpectedTrailer(
				"SataFDInfoBody",
				value.slice(84..),
			));
		}

		let fd_flags = value.get_u32();
		let unix_perms = value.get_u32();
		// skip two u32 that should always be 1
		_ = value.get_u32();
		_ = value.get_u32();
		let file_size = value.get_u32();
		// skip 4 u32's that should be various values
		_ = value.get_u32();
		_ = value.get_u32();
		_ = value.get_u32();
		_ = value.get_u32();
		// Get the timestamps.
		let created_ts = value.get_u64();
		let updated_ts = value.get_u64();
		// 32 0's

		Ok(Self {
			file_or_folder_flags: fd_flags,
			perms: unix_perms,
			file_length: file_size,
			created_timestamp: created_ts,
			last_updated_timestamp: updated_ts,
		})
	}
}

#[cfg(test)]
mod unit_tests {
	use super::*;

	#[test]
	pub fn query_types_to_and_fro() {
		for qt in vec![
			SataQueryType::FreeDiskSpace,
			SataQueryType::SizeOfFolder,
			SataQueryType::FileCount,
			SataQueryType::FileDetails,
		] {
			assert_eq!(Ok(qt), SataQueryType::try_from(u32::from(qt)));
		}
	}
}