@@ -4,6 +4,7 @@ use crate::fs::TryLockError;
44use crate :: hash:: Hash ;
55use crate :: io:: { self , BorrowedCursor , IoSlice , IoSliceMut , SeekFrom } ;
66use crate :: path:: { Path , PathBuf } ;
7+ use crate :: sys:: common:: small_c_string:: run_path_with_cstr;
78use crate :: sys:: time:: SystemTime ;
89use crate :: sys:: { unsupported, unsupported_err} ;
910
@@ -63,23 +64,21 @@ impl FileAttr {
6364 }
6465
6566 fn from_path ( path : & Path ) -> io:: Result < Self > {
66- let c_path = CString :: new ( path. as_os_str ( ) . as_encoded_bytes ( ) ) . map_err ( |_| {
67- io:: Error :: new ( io:: ErrorKind :: InvalidData , "Path contained a null byte" )
68- } ) ?;
69-
70- let file_type = unsafe { vex_sdk:: vexFileStatus ( c_path. as_ptr ( ) ) } ;
71- let is_dir = file_type == 3 ;
72-
73- // We can't get the size if its a directory because we cant open it as a file
74- if is_dir {
75- Ok ( Self { size : 0 , is_dir : true } )
76- } else {
77- let mut opts = OpenOptions :: new ( ) ;
78- opts. read ( true ) ;
79- let file = File :: open ( path, & opts) ?;
80-
81- Self :: from_fd ( file. fd . 0 )
82- }
67+ run_path_with_cstr ( path, & |c_path| {
68+ let file_type = unsafe { vex_sdk:: vexFileStatus ( c_path. as_ptr ( ) ) } ;
69+ let is_dir = file_type == 3 ;
70+
71+ // We can't get the size if its a directory because we cant open it as a file
72+ if is_dir {
73+ Ok ( Self { size : 0 , is_dir : true } )
74+ } else {
75+ let mut opts = OpenOptions :: new ( ) ;
76+ opts. read ( true ) ;
77+ let file = File :: open ( path, & opts) ?;
78+
79+ Self :: from_fd ( file. fd . 0 )
80+ }
81+ } )
8382 }
8483
8584 pub fn size ( & self ) -> u64 {
@@ -203,13 +202,15 @@ impl OpenOptions {
203202
204203impl File {
205204 pub fn open ( path : & Path , opts : & OpenOptions ) -> io:: Result < File > {
206- let path = CString :: new ( path. as_os_str ( ) . as_encoded_bytes ( ) ) . map_err ( |_| {
207- io:: Error :: new ( io:: ErrorKind :: InvalidData , "Path contained a null byte" )
208- } ) ?;
209-
210- let file =
211- match ( opts. read , opts. write , opts. append , opts. truncate , opts. create , opts. create_new )
212- {
205+ run_path_with_cstr ( path, & |path| {
206+ let file = match (
207+ opts. read ,
208+ opts. write ,
209+ opts. append ,
210+ opts. truncate ,
211+ opts. create ,
212+ opts. create_new ,
213+ ) {
213214 // read + write - unsupported
214215 ( true , true , _, _, _, _) => {
215216 return Err ( io:: Error :: new (
@@ -277,11 +278,12 @@ impl File {
277278 }
278279 } ;
279280
280- if file. is_null ( ) {
281- Err ( io:: Error :: new ( io:: ErrorKind :: NotFound , "Could not open file" ) )
282- } else {
283- Ok ( Self { fd : FileDesc ( file) } )
284- }
281+ if file. is_null ( ) {
282+ Err ( io:: Error :: new ( io:: ErrorKind :: NotFound , "Could not open file" ) )
283+ } else {
284+ Ok ( Self { fd : FileDesc ( file) } )
285+ }
286+ } )
285287 }
286288
287289 pub fn file_attr ( & self ) -> io:: Result < FileAttr > {
@@ -509,10 +511,7 @@ pub fn remove_dir_all(_path: &Path) -> io::Result<()> {
509511}
510512
511513pub fn exists ( path : & Path ) -> io:: Result < bool > {
512- let path = CString :: new ( path. as_os_str ( ) . as_encoded_bytes ( ) )
513- . map_err ( |_| io:: Error :: new ( io:: ErrorKind :: InvalidData , "Path contained a null byte" ) ) ?;
514-
515- Ok ( unsafe { vex_sdk:: vexFileStatus ( path. as_ptr ( ) ) } != 0 )
514+ run_path_with_cstr ( path, & |path| Ok ( unsafe { vex_sdk:: vexFileStatus ( path. as_ptr ( ) ) } != 0 ) )
516515}
517516
518517pub fn readlink ( _p : & Path ) -> io:: Result < PathBuf > {
0 commit comments