@@ -45,7 +45,7 @@ impl FileAttr {
45
45
}
46
46
47
47
pub fn perm ( & self ) -> FilePermissions {
48
- todo ! ( )
48
+ FilePermissions
49
49
}
50
50
51
51
pub fn file_type ( & self ) -> FileType {
@@ -149,6 +149,13 @@ impl OpenOptions {
149
149
150
150
impl File {
151
151
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
+
152
159
let path = CString :: new ( path. as_os_str ( ) . as_encoded_bytes ( ) ) . map_err ( |_| {
153
160
io:: Error :: new ( io:: ErrorKind :: InvalidData , "Path contained a null byte" )
154
161
} ) ?;
@@ -205,7 +212,7 @@ impl File {
205
212
}
206
213
207
214
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)
209
216
}
210
217
211
218
#[ inline]
@@ -217,12 +224,22 @@ impl File {
217
224
crate :: io:: default_read_buf ( |b| self . read ( b) , cursor)
218
225
}
219
226
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
+ }
222
239
}
223
240
224
241
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)
226
243
}
227
244
228
245
#[ inline]
@@ -269,6 +286,11 @@ impl fmt::Debug for File {
269
286
f. debug_struct ( "File" ) . finish_non_exhaustive ( )
270
287
}
271
288
}
289
+ impl Drop for File {
290
+ fn drop ( & mut self ) {
291
+ unsafe { vex_sdk:: vexFileClose ( self . 0 . 0 ) } ;
292
+ }
293
+ }
272
294
273
295
pub fn readdir ( _p : & Path ) -> io:: Result < ReadDir > {
274
296
unsupported ( )
0 commit comments