Skip to content

Commit f4a3291

Browse files
authored
Merge pull request #5 from firefly-zero/load_buf-option
Make load_buf return Option
2 parents add7bff + 82f57a3 commit f4a3291

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/fs.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ pub mod rom {
100100
/// Read the whole file with the given name from ROM.
101101
///
102102
/// If you have a pre-allocated buffer of the right size, use [load] instead.
103+
///
104+
/// If the file does not exist, the returned buffer will be empty.
105+
/// This, however, should not happen in normal operation because
106+
/// the contents of the ROM directory are statically known.
103107
#[cfg(feature = "alloc")]
104108
#[must_use]
105109
pub fn load_buf(name: &str) -> FileBuf {
@@ -111,6 +115,12 @@ pub mod rom {
111115
}
112116

113117
/// Functions for accessing files in the app data dir.
118+
///
119+
/// Each app has an its own data dir. That directory is empty by default,
120+
/// writable by the app, and not accessible by other apps.
121+
/// Typically, it is used to store game save data.
122+
///
123+
/// The device owner may empty this dir if they wish to remove the app data.
114124
pub mod data {
115125
use super::*;
116126
use crate::bindings as b;
@@ -146,13 +156,18 @@ pub mod data {
146156
/// Read the whole file with the given name from the data dir.
147157
///
148158
/// If you have a pre-allocated buffer of the right size, use [load] instead.
159+
///
160+
/// `None` is returned if the file does not exist.
149161
#[cfg(feature = "alloc")]
150162
#[must_use]
151-
pub fn load_buf(name: &str) -> FileBuf {
163+
pub fn load_buf(name: &str) -> Option<FileBuf> {
152164
let size = data::get_size(name);
165+
if size == 0 {
166+
return None;
167+
}
153168
let mut buf = vec![0; size];
154169
data::load(name, &mut buf);
155-
FileBuf { raw: buf }
170+
Some(FileBuf { raw: buf })
156171
}
157172

158173
/// Write the buffer into the given file in the data dir.

src/sudo.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ pub fn get_file_size(path: &str) -> usize {
104104
size as usize
105105
}
106106

107+
/// Read a file from the filesystem into the buffer.
108+
///
109+
/// The path must be relative to the root of the FS.
110+
/// For example: `sys/launcher`.
107111
pub fn load_file<'a>(path: &str, buf: &'a mut [u8]) -> File<'a> {
108112
let path_ptr = path.as_ptr() as u32;
109113
let path_len = path.len() as u32;
@@ -115,10 +119,16 @@ pub fn load_file<'a>(path: &str, buf: &'a mut [u8]) -> File<'a> {
115119
File { raw: buf }
116120
}
117121

122+
/// Like [`load_file`] but takes care of the buffer allocation and ownership.
123+
///
124+
/// `None` is returned if the file does not exist.
118125
#[cfg(feature = "alloc")]
119126
#[must_use]
120-
pub fn load_file_buf(path: &str) -> FileBuf {
127+
pub fn load_file_buf(path: &str) -> Option<FileBuf> {
121128
let size = get_file_size(path);
129+
if size == 0 {
130+
return None;
131+
}
122132
let mut buf = vec![0; size];
123133
let path_ptr = path.as_ptr() as u32;
124134
let path_len = path.len() as u32;
@@ -127,7 +137,7 @@ pub fn load_file_buf(path: &str) -> FileBuf {
127137
unsafe {
128138
b::load_file(path_ptr, path_len, buf_ptr, buf_len);
129139
}
130-
FileBuf { raw: buf }
140+
Some(FileBuf { raw: buf })
131141
}
132142

133143
/// Low-level bindings for host-defined "sudo" module.

0 commit comments

Comments
 (0)