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
|
use core::marker::PhantomData;
use core::ptr;
use core::slice;
use core::mem;
use alloc::arc::Arc;
use collections::Vec;
use path::{Path, PathBuf};
use ffi::OsString;
use libctru::services::fs::*;
#[derive(Copy, Clone, Debug)]
pub enum PathType {
Invalid,
Empty,
Binary,
ASCII,
UTF16,
}
#[derive(Copy, Clone, Debug)]
pub enum ArchiveID {
RomFS,
Savedata,
Extdata,
SharedExtdata,
SystemSavedata,
Sdmc,
SdmcWriteOnly,
BossExtdata,
CardSpiFS,
ExtDataAndBossExtdata,
SystemSaveData2,
NandRW,
NandRO,
NandROWriteAccess,
SaveDataAndContent,
SaveDataAndContent2,
NandCtrFS,
TwlPhoto,
NandTwlFS,
GameCardSavedata,
UserSavedata,
DemoSavedata,
}
pub struct Fs {
pd: PhantomData<i32>,
}
pub struct Archive {
id: ArchiveID,
handle: u64,
}
pub struct File {
handle: u32,
offset: u64,
}
pub struct Metadata {
attributes: u32,
size: u64,
}
#[derive(Clone)]
pub struct OpenOptions {
read: bool,
write: bool,
create: bool,
arch_handle: u64,
}
pub struct ReadDir {
handle: Dir,
root: Arc<PathBuf>,
}
pub struct DirEntry {
entry: FS_DirectoryEntry,
root: Arc<PathBuf>,
}
struct Dir(u32);
unsafe impl Send for Dir {}
unsafe impl Sync for Dir {}
impl Fs {
pub fn init() -> Result<Fs, i32> {
unsafe {
let r = fsInit();
if r < 0 {
Err(r)
} else {
Ok(Fs { pd: PhantomData })
}
}
}
pub fn sdmc(&self) -> Result<Archive, i32> {
unsafe {
let mut handle = 0;
let id = ArchiveID::Sdmc;
let path = fsMakePath(PathType::Empty.into(), ptr::null() as _);
let r = FSUSER_OpenArchive(&mut handle, id.into(), path);
if r < 0 {
Err(r)
} else {
Ok(Archive {
handle: handle,
id: id,
})
}
}
}
}
impl Archive {
pub fn get_id(&self) -> ArchiveID {
self.id
}
}
impl File {
pub fn open<P: AsRef<Path>>(arch: &Archive, path: P) -> Result<File, i32> {
OpenOptions::new().read(true).archive(arch).open(path.as_ref())
}
pub fn create<P: AsRef<Path>>(arch: &Archive, path: P) -> Result<File, i32> {
OpenOptions::new().write(true).create(true).archive(arch).open(path.as_ref())
}
pub fn set_len(&mut self, len: u64) -> Result<(), i32> {
unsafe {
let r = FSFILE_SetSize(self.handle, len);
if r < 0 {
Err(r)
} else {
Ok(())
}
}
}
// Right now the only file metadata we really have is file size
// This will probably expand later on
pub fn metadata(&self) -> Result<Metadata, i32> {
unsafe {
let mut size = 0;
let r = FSFILE_GetSize(self.handle, &mut size);
if r < 0 {
Err(r)
} else {
Ok(Metadata { attributes: 0, size: size })
}
}
}
pub fn read(&mut self, buf: &mut [u8]) -> Result<usize, i32> {
unsafe {
let mut n_read = 0;
let r = FSFILE_Read(
self.handle,
&mut n_read,
self.offset,
buf.as_mut_ptr() as _,
buf.len() as u32
);
self.offset += n_read as u64;
if r < 0 {
Err(r)
} else {
Ok(n_read as usize)
}
}
}
pub fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, i32> {
unsafe {
read_to_end_uninitialized(self, buf)
}
}
pub fn write(&mut self, buf: &[u8]) -> Result<usize, i32> {
unsafe {
let mut n_written = 0;
let r = FSFILE_Write(
self.handle,
&mut n_written,
self.offset,
buf.as_ptr() as _,
buf.len() as u32,
FS_WRITE_UPDATE_TIME
);
self.offset += n_written as u64;
if r < 0 {
Err(r)
} else {
Ok(n_written as usize)
}
}
}
}
impl Metadata {
pub fn is_dir(&self) -> bool {
self.attributes == self.attributes | FS_ATTRIBUTE_DIRECTORY
}
pub fn is_file(&self) -> bool {
!self.is_dir()
}
pub fn len(&self) -> u64 {
self.size
}
}
impl OpenOptions {
pub fn new() -> OpenOptions {
OpenOptions {
read: false,
write: false,
create: false,
arch_handle: 0,
}
}
pub fn read(&mut self, read: bool) -> &mut OpenOptions {
self.read = read;
self
}
pub fn write(&mut self, write: bool) -> &mut OpenOptions {
self.write = write;
self
}
pub fn create(&mut self, create: bool) -> &mut OpenOptions {
self.create = create;
self
}
pub fn archive(&mut self, archive: &Archive) -> &mut OpenOptions {
self.arch_handle = archive.handle;
self
}
pub fn open<P: AsRef<Path>>(&self, path: P) -> Result<File, i32> {
self._open(path.as_ref(), self.get_open_flags())
}
fn _open(&self, path: &Path, flags: u32) -> Result<File, i32> {
unsafe {
let mut file_handle = 0;
let path = to_utf16(path);
let fs_path = fsMakePath(PathType::UTF16.into(), path.as_ptr() as _);
let r = FSUSER_OpenFile(&mut file_handle, self.arch_handle, fs_path, flags, 0);
if r < 0 {
Err(r)
} else {
Ok(File {
handle: file_handle,
offset: 0,
})
}
}
}
fn get_open_flags(&self) -> u32 {
match (self.read, self.write, self.create) {
(true, false, false) => FS_OPEN_READ,
(false, true, false) => FS_OPEN_WRITE,
(false, true, true) => FS_OPEN_WRITE | FS_OPEN_CREATE,
(true, false, true) => FS_OPEN_READ | FS_OPEN_CREATE,
(true, true, false) => FS_OPEN_READ | FS_OPEN_WRITE,
(true, true, true) => FS_OPEN_READ | FS_OPEN_WRITE | FS_OPEN_CREATE,
_ => 0, //failure case
}
}
}
impl Iterator for ReadDir {
type Item = Result<DirEntry, i32>;
fn next(&mut self) -> Option<Result<DirEntry, i32>> {
unsafe {
let mut ret = DirEntry {
entry: mem::zeroed(),
root: self.root.clone(),
};
let mut entries_read = 0;
let entry_count = 1;
let r = FSDIR_Read(self.handle.0, &mut entries_read, entry_count, &mut ret.entry);
if r < 0 {
return Some(Err(r))
}
if entries_read != entry_count {
return None
}
Some(Ok(ret))
}
}
}
impl DirEntry {
pub fn path(&self) -> PathBuf {
self.root.join(&self.file_name())
}
// Requiring the user to explicitly pass in the Archive here is pretty ugly,
// But I'm not sure of how else to do it right now.
pub fn metadata(&self, arch: &Archive) -> Result<Metadata, i32> {
metadata(&arch, self.path())
}
pub fn file_name(&self) -> OsString {
let filename = truncate_utf16_at_nul(&self.entry.name);
OsString::from_wide(filename)
}
}
pub fn create_dir<P: AsRef<Path>>(arch: &Archive, path: P) -> Result<(), i32> {
unsafe {
let path = to_utf16(path.as_ref());
let fs_path = fsMakePath(PathType::UTF16.into(), path.as_ptr() as _);
let r = FSUSER_CreateDirectory(arch.handle, fs_path, FS_ATTRIBUTE_DIRECTORY);
if r < 0 {
Err(r)
} else {
Ok(())
}
}
}
pub fn metadata<P: AsRef<Path>>(arch: &Archive, path: P) -> Result<Metadata, i32> {
let maybe_file = File::open(&arch, path.as_ref());
let maybe_dir = read_dir(&arch, path.as_ref());
match (maybe_file, maybe_dir) {
(Ok(file), _) => file.metadata(),
(_, Ok(_dir)) => Ok(Metadata { attributes: FS_ATTRIBUTE_DIRECTORY, size: 0 }),
(Err(r), _) => Err(r),
}
}
pub fn remove_dir<P: AsRef<Path>>(arch: &Archive, path: P) -> Result<(), i32> {
unsafe {
let path = to_utf16(path.as_ref());
let fs_path = fsMakePath(PathType::UTF16.into(), path.as_ptr() as _);
let r = FSUSER_DeleteDirectory(arch.handle, fs_path);
if r < 0 {
Err(r)
} else {
Ok(())
}
}
}
pub fn remove_dir_all<P: AsRef<Path>>(arch: &Archive, path: P) -> Result<(), i32> {
unsafe {
let path = to_utf16(path.as_ref());
let fs_path = fsMakePath(PathType::UTF16.into(), path.as_ptr() as _);
let r = FSUSER_DeleteDirectoryRecursively(arch.handle, fs_path);
if r < 0 {
Err(r)
} else {
Ok(())
}
}
}
pub fn read_dir<P: AsRef<Path>>(arch: &Archive, path: P) -> Result<ReadDir, i32> {
readdir(&arch, path.as_ref())
}
pub fn remove_file<P: AsRef<Path>>(arch: &Archive, path: P) -> Result<(), i32> {
unsafe {
let path = to_utf16(path.as_ref());
let fs_path = fsMakePath(PathType::UTF16.into(), path.as_ptr() as _);
let r = FSUSER_DeleteFile(arch.handle, fs_path);
if r < 0 {
Err(r)
} else {
Ok(())
}
}
}
pub fn rename<P, Q>(arch: &Archive, from: P, to: Q) -> Result<(), i32>
where P: AsRef<Path>,
Q: AsRef<Path> {
unsafe {
let from = to_utf16(from.as_ref());
let to = to_utf16(to.as_ref());
let fs_from = fsMakePath(PathType::UTF16.into(), from.as_ptr() as _);
let fs_to = fsMakePath(PathType::UTF16.into(), to.as_ptr() as _);
let r = FSUSER_RenameFile(arch.handle, fs_from, arch.handle, fs_to);
if r == 0 {
return Ok(())
}
let r = FSUSER_RenameDirectory(arch.handle, fs_from, arch.handle, fs_to);
if r == 0 {
return Ok(())
}
Err((r))
}
}
fn readdir(arch: &Archive, p: &Path) -> Result<ReadDir, i32> {
unsafe {
let mut handle = 0;
let root = Arc::new(p.to_path_buf());
let path = to_utf16(p);
let fs_path = fsMakePath(PathType::UTF16.into(), path.as_ptr() as _);
let r = FSUSER_OpenDirectory(&mut handle, arch.handle, fs_path);
if r < 0 {
Err(r)
} else {
Ok(ReadDir { handle: Dir(handle), root: root })
}
}
}
// TODO: Determine if we should check UTF-16 paths for interior NULs
fn to_utf16(path: &Path) -> Vec<u16> {
path.as_os_str().encode_wide().collect::<Vec<_>>()
}
// Adapted from sys/windows/fs.rs in libstd
fn truncate_utf16_at_nul<'a>(v: &'a [u16]) -> &'a [u16] {
match v.iter().position(|c| *c == 0) {
// don't include the 0
Some(i) => &v[..i],
None => v
}
}
// Adapted from sys/common/io.rs in libstd
unsafe fn read_to_end_uninitialized(f: &mut File, buf: &mut Vec<u8>) -> Result<usize, i32> {
let start_len = buf.len();
buf.reserve(16);
// Always try to read into the empty space of the vector (from the length to the capacity).
// If the vector ever fills up then we reserve an extra byte which should trigger the normal
// reallocation routines for the vector, which will likely double the size.
//
// This function is similar to the read_to_end function in std::io, but the logic about
// reservations and slicing is different enough that this is duplicated here.
loop {
if buf.len() == buf.capacity() {
buf.reserve(1);
}
let buf_slice = slice::from_raw_parts_mut(buf.as_mut_ptr().offset(buf.len() as isize),
buf.capacity() - buf.len());
match f.read(buf_slice) {
Ok(0) => { return Ok(buf.len() - start_len); }
Ok(n) => { let len = buf.len() + n; buf.set_len(len); },
Err(e) => { return Err(e); }
}
}
}
impl Drop for Fs {
fn drop(&mut self) {
unsafe {
fsExit();
}
}
}
impl Drop for Archive {
fn drop(&mut self) {
unsafe {
FSUSER_CloseArchive(self.handle);
}
}
}
impl Drop for File {
fn drop(&mut self) {
unsafe {
FSFILE_Close(self.handle);
}
}
}
impl Drop for Dir {
fn drop(&mut self) {
unsafe {
FSDIR_Close(self.0);
}
}
}
impl From<PathType> for FS_PathType {
fn from(p: PathType) -> Self {
use self::PathType::*;
use libctru::services::fs::FS_PathType::*;
match p {
Invalid => PATH_INVALID,
Empty => PATH_EMPTY,
Binary => PATH_BINARY,
ASCII => PATH_ASCII,
UTF16 => PATH_UTF16,
}
}
}
impl From<ArchiveID> for FS_ArchiveID {
fn from(a: ArchiveID) -> Self {
use self::ArchiveID::*;
use libctru::services::fs::FS_ArchiveID::*;
match a {
RomFS => ARCHIVE_ROMFS,
Savedata => ARCHIVE_SAVEDATA,
Extdata => ARCHIVE_EXTDATA,
SharedExtdata => ARCHIVE_SHARED_EXTDATA,
SystemSavedata => ARCHIVE_SYSTEM_SAVEDATA,
Sdmc => ARCHIVE_SDMC,
SdmcWriteOnly => ARCHIVE_SDMC_WRITE_ONLY,
BossExtdata => ARCHIVE_BOSS_EXTDATA,
CardSpiFS => ARCHIVE_CARD_SPIFS,
ExtDataAndBossExtdata => ARCHIVE_EXTDATA_AND_BOSS_EXTDATA,
SystemSaveData2 => ARCHIVE_SYSTEM_SAVEDATA2,
NandRW => ARCHIVE_NAND_RW,
NandRO => ARCHIVE_NAND_RO,
NandROWriteAccess => ARCHIVE_NAND_RO_WRITE_ACCESS,
SaveDataAndContent => ARCHIVE_SAVEDATA_AND_CONTENT,
SaveDataAndContent2 => ARCHIVE_SAVEDATA_AND_CONTENT2,
NandCtrFS => ARCHIVE_NAND_CTR_FS,
TwlPhoto => ARCHIVE_TWL_PHOTO,
NandTwlFS => ARCHIVE_NAND_TWL_FS,
GameCardSavedata => ARCHIVE_GAMECARD_SAVEDATA,
UserSavedata => ARCHIVE_USER_SAVEDATA,
DemoSavedata => ARCHIVE_DEMO_SAVEDATA,
}
}
}
|