@@ -34,6 +34,17 @@ use libc::c_char;
34
34
use libc:: dirfd;
35
35
#[ cfg( any( target_os = "linux" , target_os = "emscripten" ) ) ]
36
36
use libc:: fstatat64;
37
+ #[ cfg( any(
38
+ target_os = "solaris" ,
39
+ target_os = "fuchsia" ,
40
+ target_os = "redox" ,
41
+ target_os = "illumos"
42
+ ) ) ]
43
+ use libc:: readdir as readdir64;
44
+ #[ cfg( target_os = "linux" ) ]
45
+ use libc:: readdir64;
46
+ #[ cfg( any( target_os = "emscripten" , target_os = "l4re" ) ) ]
47
+ use libc:: readdir64_r;
37
48
#[ cfg( not( any(
38
49
target_os = "linux" ,
39
50
target_os = "emscripten" ,
@@ -60,9 +71,7 @@ use libc::{
60
71
lstat as lstat64, off_t as off64_t, open as open64, stat as stat64,
61
72
} ;
62
73
#[ cfg( any( target_os = "linux" , target_os = "emscripten" , target_os = "l4re" ) ) ]
63
- use libc:: {
64
- dirent64, fstat64, ftruncate64, lseek64, lstat64, off64_t, open64, readdir64_r, stat64,
65
- } ;
74
+ use libc:: { dirent64, fstat64, ftruncate64, lseek64, lstat64, off64_t, open64, stat64} ;
66
75
67
76
pub use crate :: sys_common:: fs:: { remove_dir_all, try_exists} ;
68
77
@@ -202,6 +211,7 @@ struct InnerReadDir {
202
211
pub struct ReadDir {
203
212
inner : Arc < InnerReadDir > ,
204
213
#[ cfg( not( any(
214
+ target_os = "linux" ,
205
215
target_os = "solaris" ,
206
216
target_os = "illumos" ,
207
217
target_os = "fuchsia" ,
@@ -223,12 +233,13 @@ pub struct DirEntry {
223
233
// array to store the name, b) its lifetime between readdir
224
234
// calls is not guaranteed.
225
235
#[ cfg( any(
236
+ target_os = "linux" ,
226
237
target_os = "solaris" ,
227
238
target_os = "illumos" ,
228
239
target_os = "fuchsia" ,
229
240
target_os = "redox"
230
241
) ) ]
231
- name : Box < [ u8 ] > ,
242
+ name : Box < CStr > ,
232
243
}
233
244
234
245
#[ derive( Clone , Debug ) ]
@@ -449,22 +460,21 @@ impl Iterator for ReadDir {
449
460
type Item = io:: Result < DirEntry > ;
450
461
451
462
#[ cfg( any(
463
+ target_os = "linux" ,
452
464
target_os = "solaris" ,
453
465
target_os = "fuchsia" ,
454
466
target_os = "redox" ,
455
467
target_os = "illumos"
456
468
) ) ]
457
469
fn next ( & mut self ) -> Option < io:: Result < DirEntry > > {
458
- use crate :: slice;
459
-
460
470
unsafe {
461
471
loop {
462
472
// Although readdir_r(3) would be a correct function to use here because
463
473
// of the thread safety, on Illumos and Fuchsia the readdir(3C) function
464
474
// is safe to use in threaded applications and it is generally preferred
465
475
// over the readdir_r(3C) function.
466
476
super :: os:: set_errno ( 0 ) ;
467
- let entry_ptr = libc :: readdir ( self . inner . dirp . 0 ) ;
477
+ let entry_ptr = readdir64 ( self . inner . dirp . 0 ) ;
468
478
if entry_ptr. is_null ( ) {
469
479
// null can mean either the end is reached or an error occurred.
470
480
// So we had to clear errno beforehand to check for an error now.
@@ -475,14 +485,11 @@ impl Iterator for ReadDir {
475
485
}
476
486
477
487
let name = ( * entry_ptr) . d_name . as_ptr ( ) ;
478
- let namelen = libc:: strlen ( name) as usize ;
479
488
480
489
let ret = DirEntry {
481
490
entry : * entry_ptr,
482
- name : slice:: from_raw_parts ( name as * const u8 , namelen as usize )
483
- . to_owned ( )
484
- . into_boxed_slice ( ) ,
485
491
dir : Arc :: clone ( & self . inner ) ,
492
+ name : CStr :: from_ptr ( name) . into ( ) ,
486
493
} ;
487
494
if ret. name_bytes ( ) != b"." && ret. name_bytes ( ) != b".." {
488
495
return Some ( Ok ( ret) ) ;
@@ -492,6 +499,7 @@ impl Iterator for ReadDir {
492
499
}
493
500
494
501
#[ cfg( not( any(
502
+ target_os = "linux" ,
495
503
target_os = "solaris" ,
496
504
target_os = "fuchsia" ,
497
505
target_os = "redox" ,
@@ -547,7 +555,7 @@ impl DirEntry {
547
555
#[ cfg( any( target_os = "linux" , target_os = "emscripten" , target_os = "android" ) ) ]
548
556
pub fn metadata ( & self ) -> io:: Result < FileAttr > {
549
557
let fd = cvt ( unsafe { dirfd ( self . dir . dirp . 0 ) } ) ?;
550
- let name = self . entry . d_name . as_ptr ( ) ;
558
+ let name = self . name . as_ptr ( ) ;
551
559
552
560
cfg_has_statx ! {
553
561
if let Some ( ret) = unsafe { try_statx(
@@ -647,7 +655,6 @@ impl DirEntry {
647
655
}
648
656
#[ cfg( any(
649
657
target_os = "android" ,
650
- target_os = "linux" ,
651
658
target_os = "emscripten" ,
652
659
target_os = "l4re" ,
653
660
target_os = "haiku" ,
@@ -658,13 +665,14 @@ impl DirEntry {
658
665
unsafe { CStr :: from_ptr ( self . entry . d_name . as_ptr ( ) ) . to_bytes ( ) }
659
666
}
660
667
#[ cfg( any(
668
+ target_os = "linux" ,
661
669
target_os = "solaris" ,
662
670
target_os = "illumos" ,
663
671
target_os = "fuchsia" ,
664
672
target_os = "redox"
665
673
) ) ]
666
674
fn name_bytes ( & self ) -> & [ u8 ] {
667
- & * self . name
675
+ self . name . to_bytes ( )
668
676
}
669
677
670
678
pub fn file_name_os_str ( & self ) -> & OsStr {
@@ -1068,6 +1076,7 @@ pub fn readdir(p: &Path) -> io::Result<ReadDir> {
1068
1076
Ok ( ReadDir {
1069
1077
inner : Arc :: new ( inner) ,
1070
1078
#[ cfg( not( any(
1079
+ target_os = "linux" ,
1071
1080
target_os = "solaris" ,
1072
1081
target_os = "illumos" ,
1073
1082
target_os = "fuchsia" ,
0 commit comments