@@ -155,10 +155,7 @@ impl DirEntry {
155155 }
156156
157157 pub fn file_name ( & self ) -> OsString {
158- self . path
159- . file_name ( )
160- . unwrap_or ( crate :: ffi:: OsStr :: new ( "" ) )
161- . to_os_string ( )
158+ self . path . file_name ( ) . unwrap_or ( crate :: ffi:: OsStr :: new ( "" ) ) . to_os_string ( )
162159 }
163160
164161 pub fn metadata ( & self ) -> io:: Result < FileAttr > {
@@ -433,16 +430,21 @@ impl Drop for File {
433430}
434431
435432pub fn readdir ( p : & Path ) -> io:: Result < ReadDir > {
433+ // getting directory entries does not work with trailing slashes
434+ let p = Path :: new (
435+ p. to_str ( )
436+ . ok_or ( io:: Error :: new (
437+ io:: ErrorKind :: InvalidInput ,
438+ "Path contained invalid characters" ,
439+ ) ) ?
440+ . trim_end_matches ( "/" ) ,
441+ ) ;
442+
436443 if !stat ( p) ?. file_type ( ) . is_dir ( ) {
437444 return Err ( io:: Error :: new ( io:: ErrorKind :: InvalidInput , "Given directory was not a path" ) ) ;
438445 }
439446
440- // getting directory entries does not work with trailing slashes
441- let path = p
442- . to_str ( )
443- . ok_or ( io:: Error :: new ( io:: ErrorKind :: InvalidInput , "Path contained invalid characters" ) ) ?
444- . trim_end_matches ( "/" ) ;
445- let path = CString :: new ( path. as_bytes ( ) )
447+ let path = CString :: new ( p. as_os_str ( ) . as_encoded_bytes ( ) )
446448 . map_err ( |_| io:: Error :: new ( io:: ErrorKind :: InvalidInput , "Path contained a null byte" ) ) ?;
447449
448450 //TODO: Figure out if there is any way to check the number of entries in a directory/the needed length
@@ -458,15 +460,19 @@ pub fn readdir(p: &Path) -> io::Result<ReadDir> {
458460 // stop at null-terminator
459461 let filenames = match filenames_buffer. split ( |& e| e == 0 ) . next ( ) {
460462 Some ( filenames) => filenames,
461- None => & filenames_buffer
463+ None => & filenames_buffer,
462464 } ;
463- let filenames = String :: from_utf8 ( filenames. to_vec ( ) ) . map_err ( |_| io:: Error :: new ( io:: ErrorKind :: InvalidData , "Path contained a null byte" ) ) ?;
464- let paths = filenames. split ( '\n' ) . map ( |filename| {
465- let mut path = PathBuf :: new ( ) ;
466- path. push ( p) ;
467- path. push ( filename) ;
468- DirEntry { path }
469- } ) . collect :: < Vec < _ > > ( ) ;
465+ let filenames = String :: from_utf8 ( filenames. to_vec ( ) )
466+ . map_err ( |_| io:: Error :: new ( io:: ErrorKind :: InvalidData , "Path contained a null byte" ) ) ?;
467+ let paths = filenames
468+ . split ( '\n' )
469+ . map ( |filename| {
470+ let mut path = PathBuf :: new ( ) ;
471+ path. push ( p) ;
472+ path. push ( filename) ;
473+ DirEntry { path }
474+ } )
475+ . collect :: < Vec < _ > > ( ) ;
470476
471477 Ok ( ReadDir { entries : paths } )
472478}
0 commit comments