Skip to content

Commit b0c536b

Browse files
Gavin-Niedermanmax-niederman
authored andcommitted
fix: support File::file_attr and dissalow read and write mode at the same time
1 parent 7e3109f commit b0c536b

File tree

1 file changed

+37
-23
lines changed
  • library/std/src/sys/pal/vexos

1 file changed

+37
-23
lines changed

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

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub struct OpenOptions {
2828
read: bool,
2929
write: bool,
3030
append: bool,
31+
truncate: bool,
3132
create_new: bool,
3233
}
3334

@@ -46,6 +47,19 @@ pub struct FileType {
4647
pub struct DirBuilder {}
4748

4849
impl FileAttr {
50+
/// Creates a FileAttr by getting data from an opened file.
51+
fn from_fd(fd: *mut vex_sdk::FIL) -> io::Result<Self> {
52+
let size = unsafe {
53+
vex_sdk::vexFileSize(fd)
54+
};
55+
56+
if size >= 0 {
57+
Ok(Self { size: size as u64 })
58+
} else {
59+
Err(io::Error::new(io::ErrorKind::NotSeekable, "Failed to seek file"))
60+
}
61+
}
62+
4963
pub fn size(&self) -> u64 {
5064
self.size
5165
}
@@ -134,7 +148,7 @@ impl DirEntry {
134148

135149
impl OpenOptions {
136150
pub fn new() -> OpenOptions {
137-
OpenOptions { read: false, write: false, append: false, create_new: false }
151+
OpenOptions { read: false, write: false, append: false, truncate: false, create_new: false }
138152
}
139153

140154
pub fn read(&mut self, read: bool) {
@@ -146,7 +160,9 @@ impl OpenOptions {
146160
pub fn append(&mut self, append: bool) {
147161
self.append = append;
148162
}
149-
pub fn truncate(&mut self, _truncate: bool) {}
163+
pub fn truncate(&mut self, truncate: bool) {
164+
self.truncate = truncate;
165+
}
150166
pub fn create(&mut self, create: bool) {
151167
self.write = create;
152168
}
@@ -164,6 +180,12 @@ impl File {
164180
io::Error::new(io::ErrorKind::InvalidData, "Path contained a null byte")
165181
})?;
166182

183+
if opts.write && opts.read {
184+
return Err(io::Error::new(
185+
io::ErrorKind::InvalidInput,
186+
"Files cannot be opened with read and write access",
187+
));
188+
}
167189
if opts.create_new {
168190
let file_exists = unsafe { vex_sdk::vexFileStatus(path.as_ptr()) };
169191
if file_exists != 0 {
@@ -178,9 +200,19 @@ impl File {
178200
} else if opts.write && opts.append {
179201
// Open in read/write and append mode
180202
unsafe { vex_sdk::vexFileOpenWrite(path.as_ptr()) }
181-
} else if opts.write {
203+
} else if opts.write && opts.truncate {
182204
// Open in read/write mode
183205
unsafe { vex_sdk::vexFileOpenCreate(path.as_ptr()) }
206+
} else if opts.write {
207+
// Open in read/write and overwrite mode
208+
unsafe {
209+
// Open in read/write and append mode
210+
let fd = vex_sdk::vexFileOpenWrite(path.as_ptr());
211+
// Seek to beginning of the file
212+
vex_sdk::vexFileSeek(fd, 0, 0);
213+
214+
fd
215+
}
184216
} else {
185217
return Err(io::Error::new(
186218
io::ErrorKind::InvalidInput,
@@ -196,7 +228,7 @@ impl File {
196228
}
197229

198230
pub fn file_attr(&self) -> io::Result<FileAttr> {
199-
todo!()
231+
FileAttr::from_fd(self.fd.0)
200232
}
201233

202234
pub fn fsync(&self) -> io::Result<()> {
@@ -351,25 +383,7 @@ pub fn stat(p: &Path) -> io::Result<FileAttr> {
351383
let file = File::open(p, &opts)?;
352384
let fd = file.fd.0;
353385

354-
const SEEK_END: i32 = 2;
355-
const SEEK_SET: i32 = 0;
356-
357-
let end = unsafe {
358-
let cur = vex_sdk::vexFileTell(fd);
359-
if cur < 0 {
360-
return Err(io::Error::new(io::ErrorKind::NotSeekable, "Failed to seek file"));
361-
}
362-
map_fresult(vex_sdk::vexFileSeek(fd, 0, SEEK_END))?;
363-
let end = vex_sdk::vexFileTell(fd);
364-
map_fresult(vex_sdk::vexFileSeek(fd, cur as _, SEEK_SET))?;
365-
end
366-
};
367-
368-
if end >= 0 {
369-
Ok(FileAttr { size: end as u64 })
370-
} else {
371-
Err(io::Error::new(io::ErrorKind::NotSeekable, "Failed to seek file"))
372-
}
386+
FileAttr::from_fd(fd)
373387
}
374388

375389
pub fn lstat(p: &Path) -> io::Result<FileAttr> {

0 commit comments

Comments
 (0)