@@ -155,10 +155,7 @@ impl DirEntry {
155
155
}
156
156
157
157
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 ( )
162
159
}
163
160
164
161
pub fn metadata ( & self ) -> io:: Result < FileAttr > {
@@ -433,16 +430,21 @@ impl Drop for File {
433
430
}
434
431
435
432
pub 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
+
436
443
if !stat ( p) ?. file_type ( ) . is_dir ( ) {
437
444
return Err ( io:: Error :: new ( io:: ErrorKind :: InvalidInput , "Given directory was not a path" ) ) ;
438
445
}
439
446
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 ( ) )
446
448
. map_err ( |_| io:: Error :: new ( io:: ErrorKind :: InvalidInput , "Path contained a null byte" ) ) ?;
447
449
448
450
//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> {
458
460
// stop at null-terminator
459
461
let filenames = match filenames_buffer. split ( |& e| e == 0 ) . next ( ) {
460
462
Some ( filenames) => filenames,
461
- None => & filenames_buffer
463
+ None => & filenames_buffer,
462
464
} ;
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 < _ > > ( ) ;
470
476
471
477
Ok ( ReadDir { entries : paths } )
472
478
}
0 commit comments