@@ -72,36 +72,6 @@ pub struct FileType {
7272}
7373
7474impl FileAttr {
75- /// Creates a FileAttr by getting data from an opened file.
76- fn from_fd ( fd : * mut vex_sdk:: FIL ) -> io:: Result < Self > {
77- // `vexFileSize` returns -1 upon error, so u64::try_from will fail on error.
78- if let Ok ( size) = u64:: try_from ( unsafe { vex_sdk:: vexFileSize ( fd) } ) {
79- Ok ( Self :: File { size } )
80- } else {
81- Err ( io:: const_error!( io:: ErrorKind :: InvalidData , "Failed to get file size" ) )
82- }
83- }
84-
85- fn from_path ( path : & Path ) -> io:: Result < Self > {
86- // vexFileStatus returns 3 if the given path is a directory.
87- const FILE_STATUS_DIR : u32 = 3 ;
88-
89- run_path_with_cstr ( path, & |c_path| {
90- let file_type = unsafe { vex_sdk:: vexFileStatus ( c_path. as_ptr ( ) ) } ;
91-
92- // We can't get the size if its a directory because we cant open it as a file
93- if file_type == FILE_STATUS_DIR {
94- Ok ( Self :: Dir )
95- } else {
96- let mut opts = OpenOptions :: new ( ) ;
97- opts. read ( true ) ;
98- let file = File :: open ( path, & opts) ?;
99-
100- Self :: from_fd ( file. fd . 0 )
101- }
102- } )
103- }
104-
10575 pub fn size ( & self ) -> u64 {
10676 match self {
10777 Self :: File { size } => * size,
@@ -302,7 +272,12 @@ impl File {
302272 }
303273
304274 pub fn file_attr ( & self ) -> io:: Result < FileAttr > {
305- FileAttr :: from_fd ( self . fd . 0 )
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 } )
278+ } else {
279+ Err ( io:: const_error!( io:: ErrorKind :: InvalidData , "Failed to get file size" ) )
280+ }
306281 }
307282
308283 pub fn fsync ( & self ) -> io:: Result < ( ) > {
@@ -519,7 +494,20 @@ pub fn exists(path: &Path) -> io::Result<bool> {
519494}
520495
521496pub fn stat ( p : & Path ) -> io:: Result < FileAttr > {
522- FileAttr :: from_path ( p)
497+ // `vexFileStatus` returns 3 if the given path is a directory.
498+ const FILE_STATUS_DIR : u32 = 3 ;
499+
500+ run_path_with_cstr ( path, & |c_path| {
501+ let file_type = unsafe { vex_sdk:: vexFileStatus ( c_path. as_ptr ( ) ) } ;
502+
503+ // We can't get the size if its a directory because we cant open it as a file
504+ if file_type == FILE_STATUS_DIR {
505+ Ok ( Self :: Dir )
506+ } else {
507+ let file = File :: open ( path, & OpenOptions :: new ( ) . read ( true ) ) ?;
508+ file. file_Attr ( )
509+ }
510+ } )
523511}
524512
525513pub fn lstat ( p : & Path ) -> io:: Result < FileAttr > {
0 commit comments