Skip to content

Commit eec6756

Browse files
committed
vexos: resolve errors from reorganizing fs methods
1 parent 96cee7f commit eec6756

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

library/std/src/sys/fs/vexos.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ impl File {
198198
// the requirements that `create_new` can't have an existing file and `!create`
199199
// doesn't create a file ourselves.
200200
if !opts.read && (opts.write || opts.append) && (opts.create_new || !opts.create) {
201-
let status = vex_sdk::vexFileStatus(path.as_ptr());
201+
let status = unsafe { vex_sdk::vexFileStatus(path.as_ptr()) };
202202

203203
if opts.create_new && status != 0 {
204204
return Err(io::const_error!(io::ErrorKind::AlreadyExists, "File exists",));
@@ -273,8 +273,11 @@ impl File {
273273

274274
pub fn file_attr(&self) -> io::Result<FileAttr> {
275275
// `vexFileSize` returns -1 upon error, so u64::try_from will fail on error.
276-
if let Ok(size) = u64::try_from(unsafe { vex_sdk::vexFileSize(fd) }) {
277-
Ok(Self::File { size })
276+
if let Ok(size) = u64::try_from(unsafe {
277+
// SAFETY: `self.fd` contains a valid pointer to `FIL` for this struct's lifetime.
278+
vex_sdk::vexFileSize(self.fd.0)
279+
}) {
280+
Ok(FileAttr::File { size })
278281
} else {
279282
Err(io::const_error!(io::ErrorKind::InvalidData, "Failed to get file size"))
280283
}
@@ -497,15 +500,17 @@ pub fn stat(p: &Path) -> io::Result<FileAttr> {
497500
// `vexFileStatus` returns 3 if the given path is a directory.
498501
const FILE_STATUS_DIR: u32 = 3;
499502

500-
run_path_with_cstr(path, &|c_path| {
503+
run_path_with_cstr(p, &|c_path| {
501504
let file_type = unsafe { vex_sdk::vexFileStatus(c_path.as_ptr()) };
502505

503506
// We can't get the size if its a directory because we cant open it as a file
504507
if file_type == FILE_STATUS_DIR {
505-
Ok(Self::Dir)
508+
Ok(FileAttr::Dir)
506509
} else {
507-
let file = File::open(path, &OpenOptions::new().read(true))?;
508-
file.file_Attr()
510+
let mut opts = OpenOptions::new();
511+
opts.read(true);
512+
let file = File::open(p, &opts)?;
513+
file.file_attr()
509514
}
510515
})
511516
}

0 commit comments

Comments
 (0)