Skip to content

Commit a019495

Browse files
Gavin-Niedermanmax-niederman
authored andcommitted
feat: file writes vectored writes and vectored reads
1 parent eb07255 commit a019495

File tree

1 file changed

+27
-5
lines changed
  • library/std/src/sys/pal/vexos

1 file changed

+27
-5
lines changed

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

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl FileAttr {
4545
}
4646

4747
pub fn perm(&self) -> FilePermissions {
48-
todo!()
48+
FilePermissions
4949
}
5050

5151
pub fn file_type(&self) -> FileType {
@@ -149,6 +149,13 @@ impl OpenOptions {
149149

150150
impl File {
151151
pub fn open(path: &Path, opts: &OpenOptions) -> io::Result<File> {
152+
let fs_status = unsafe { vex_sdk::vexFileMountSD() };
153+
match fs_status {
154+
vex_sdk::FRESULT::FR_OK => (),
155+
//TODO: cover more results
156+
_ => return Err(io::Error::new(io::ErrorKind::NotFound, "SD card cannot be written or read from")),
157+
}
158+
152159
let path = CString::new(path.as_os_str().as_encoded_bytes()).map_err(|_| {
153160
io::Error::new(io::ErrorKind::InvalidData, "Path contained a null byte")
154161
})?;
@@ -205,7 +212,7 @@ impl File {
205212
}
206213

207214
pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
208-
crate::io::default_read_vectored(|buf| self.read(buf), bufs)
215+
crate::io::default_read_vectored(|b| self.read(b), bufs)
209216
}
210217

211218
#[inline]
@@ -217,12 +224,22 @@ impl File {
217224
crate::io::default_read_buf(|b| self.read(b), cursor)
218225
}
219226

220-
pub fn write(&self, _buf: &[u8]) -> io::Result<usize> {
221-
todo!()
227+
pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
228+
let len = buf.len();
229+
let buf_ptr = buf.as_ptr();
230+
let written = unsafe { vex_sdk::vexFileWrite(buf_ptr.cast_mut().cast(), 1, len as _, self.0.0) };
231+
if written < 0 {
232+
Err(io::Error::new(
233+
io::ErrorKind::Other,
234+
"Could not write to file",
235+
))
236+
} else {
237+
Ok(written as usize)
238+
}
222239
}
223240

224241
pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
225-
crate::io::default_write_vectored(|buf| self.write(buf), bufs)
242+
crate::io::default_write_vectored(|b| self.write(b), bufs)
226243
}
227244

228245
#[inline]
@@ -269,6 +286,11 @@ impl fmt::Debug for File {
269286
f.debug_struct("File").finish_non_exhaustive()
270287
}
271288
}
289+
impl Drop for File {
290+
fn drop(&mut self) {
291+
unsafe { vex_sdk::vexFileClose(self.0.0) };
292+
}
293+
}
272294

273295
pub fn readdir(_p: &Path) -> io::Result<ReadDir> {
274296
unsupported()

0 commit comments

Comments
 (0)