@@ -198,7 +198,7 @@ impl File {
198
198
// the requirements that `create_new` can't have an existing file and `!create`
199
199
// doesn't create a file ourselves.
200
200
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 ( ) ) } ;
202
202
203
203
if opts. create_new && status != 0 {
204
204
return Err ( io:: const_error!( io:: ErrorKind :: AlreadyExists , "File exists" , ) ) ;
@@ -273,8 +273,11 @@ impl File {
273
273
274
274
pub fn file_attr ( & self ) -> io:: Result < FileAttr > {
275
275
// `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 } )
278
281
} else {
279
282
Err ( io:: const_error!( io:: ErrorKind :: InvalidData , "Failed to get file size" ) )
280
283
}
@@ -497,15 +500,17 @@ pub fn stat(p: &Path) -> io::Result<FileAttr> {
497
500
// `vexFileStatus` returns 3 if the given path is a directory.
498
501
const FILE_STATUS_DIR : u32 = 3 ;
499
502
500
- run_path_with_cstr ( path , & |c_path| {
503
+ run_path_with_cstr ( p , & |c_path| {
501
504
let file_type = unsafe { vex_sdk:: vexFileStatus ( c_path. as_ptr ( ) ) } ;
502
505
503
506
// We can't get the size if its a directory because we cant open it as a file
504
507
if file_type == FILE_STATUS_DIR {
505
- Ok ( Self :: Dir )
508
+ Ok ( FileAttr :: Dir )
506
509
} 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 ( )
509
514
}
510
515
} )
511
516
}
0 commit comments