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
use alloc::ffi::CString;
use core::{ffi::CStr, str::FromStr};
use sys::fs::*;
pub fn file_exists(path: &str) -> bool {
let path = CString::from_str(path);
match path {
Ok(p) => unsafe { forge_fs_fileExists(p.as_ptr()) },
Err(_) => false,
}
}
pub mod romfs {
use super::*;
/// Returns the mount point of the RomFS filesystem. Usually `app`.
pub fn mount_point() -> &'static str {
let ptr = unsafe { forge_fs_getRomFsMountPoint() };
unsafe { CStr::from_ptr::<'static>(ptr) }
.to_str()
.expect("Invalid UTF-8 in mount point")
}
}
pub mod savedata {
use super::*;
/// Returns the mount point of the SaveData filesystem. Usually `savedata`.
pub fn mount_point() -> &'static str {
let ptr = unsafe { forge_fs_getSaveDataMountPoint() };
unsafe { CStr::from_ptr::<'static>(ptr) }
.to_str()
.expect("Invalid UTF-8 in mount point")
}
/// Mounts the SaveData filesystem. Returns `true` if successful, `false` otherwise.
///
/// # Safety
/// You must check the return value of this function before attempting to access the SaveData filesystem.
pub fn mount() -> bool {
unsafe { forge_fs_mountSaveData() }
}
/// Unmounts the SaveData filesystem.
///
/// # Safety
/// Do not call this function if any of the following conditions are true:
/// - You have any open file handles to the SaveData filesystem.
/// - The last call to `mount()` returned `false`.
/// - You have not called `mount()` at least once before calling this function.
pub fn unmount() {
unsafe { forge_fs_unmountSaveData() }
}
pub struct ScopedMount;
impl ScopedMount {
pub fn new() -> Self {
if !savedata::mount() {
panic!("Failed to mount savedata");
}
Self
}
}
impl Drop for ScopedMount {
fn drop(&mut self) {
savedata::unmount();
}
}
}