@@ -81,11 +81,11 @@ impl FileDescriptor {
81
81
/// Create a `FileDescriptor` with arguments similar to `libc::openat`.
82
82
pub fn open_at (
83
83
dir_file_descriptor : & FileDescriptor ,
84
- filename : * const libc :: c_char ,
84
+ file_name : & CStr ,
85
85
flags : i32 ,
86
86
) -> io:: Result < Self > {
87
87
unsafe {
88
- let fd = libc:: openat ( dir_file_descriptor. fd , filename , flags) ;
88
+ let fd = libc:: openat ( dir_file_descriptor. fd , file_name . as_ptr ( ) , flags) ;
89
89
if fd == -1 {
90
90
Err ( io:: Error :: last_os_error ( ) )
91
91
} else {
@@ -124,7 +124,7 @@ impl Metadata {
124
124
/// process' current working directory.
125
125
pub fn new (
126
126
dirfd : libc:: c_int ,
127
- filename : * const libc :: c_char ,
127
+ file_name : & CStr ,
128
128
follow_symlinks : bool ,
129
129
) -> io:: Result < Metadata > {
130
130
let mut statbuf = MaybeUninit :: uninit ( ) ;
@@ -133,7 +133,7 @@ impl Metadata {
133
133
} else {
134
134
libc:: AT_SYMLINK_NOFOLLOW
135
135
} ;
136
- let ret = unsafe { libc:: fstatat ( dirfd, filename , statbuf. as_mut_ptr ( ) , flags) } ;
136
+ let ret = unsafe { libc:: fstatat ( dirfd, file_name . as_ptr ( ) , statbuf. as_mut_ptr ( ) , flags) } ;
137
137
if ret != 0 {
138
138
return Err ( io:: Error :: last_os_error ( ) ) ;
139
139
}
@@ -400,11 +400,8 @@ impl<'a> Entry<'a> {
400
400
401
401
/// Check if this `Entry` is an empty directory.
402
402
pub fn is_empty_dir ( & self ) -> io:: Result < bool > {
403
- let file_descriptor = FileDescriptor :: open_at (
404
- self . dir_file_descriptor ,
405
- self . file_name ( ) . as_ptr ( ) ,
406
- libc:: O_RDONLY ,
407
- ) ?;
403
+ let file_descriptor =
404
+ FileDescriptor :: open_at ( self . dir_file_descriptor , self . file_name ( ) , libc:: O_RDONLY ) ?;
408
405
match OwnedDir :: new ( file_descriptor) {
409
406
Ok ( dir) => {
410
407
let mut num_entries = 0 ;
@@ -474,6 +471,8 @@ impl Deref for DisplayablePath {
474
471
}
475
472
}
476
473
474
+ // Ignore clippy warnings, this is only used in `process_file` below
475
+ #[ allow( clippy:: large_enum_variant) ]
477
476
enum ProcessFileResult < ' a > {
478
477
ProcessedDirectory ( Entry < ' a > ) ,
479
478
ProcessedFile ,
@@ -495,11 +494,15 @@ where
495
494
H : FnMut ( Entry < ' _ > , Error ) ,
496
495
{
497
496
// Get the metadata for the file without following symlinks.
498
- let entry_symlink_metadata = match Metadata :: new ( dir_fd. fd , entry_filename. as_ptr ( ) , false ) {
497
+ let entry_symlink_metadata = match Metadata :: new (
498
+ dir_fd. fd ,
499
+ unsafe { CStr :: from_ptr ( entry_filename. as_ptr ( ) ) } ,
500
+ false ,
501
+ ) {
499
502
Ok ( md) => md,
500
503
Err ( e) => {
501
504
err_reporter (
502
- Entry :: new ( dir_fd, & path_stack, entry_filename, None ) ,
505
+ Entry :: new ( dir_fd, path_stack, entry_filename, None ) ,
503
506
Error :: new ( e, ErrorKind :: Stat ) ,
504
507
) ;
505
508
return ProcessFileResult :: NotProcessed ;
@@ -521,7 +524,11 @@ where
521
524
}
522
525
} ;
523
526
524
- match Metadata :: new ( dir_fd. fd , entry_filename. as_ptr ( ) , true ) {
527
+ match Metadata :: new (
528
+ dir_fd. fd ,
529
+ unsafe { CStr :: from_ptr ( entry_filename. as_ptr ( ) ) } ,
530
+ true ,
531
+ ) {
525
532
Ok ( md) => ( Some ( read_link) , md) ,
526
533
Err ( e) => {
527
534
if e. kind ( ) == io:: ErrorKind :: NotFound {
@@ -558,25 +565,6 @@ where
558
565
// Is the directory searchable?
559
566
if entry_metadata. is_executable ( ) {
560
567
ProcessFileResult :: ProcessedDirectory ( entry)
561
- // if conserve_fds {
562
- // ProcessFileResult::ProcessedDirectory(NodeOrMetadata::Metadata(
563
- // entry_metadata,
564
- // ))
565
- // } else {
566
- // match OwnedDir::open_at(dir_fd, entry_filename.as_ptr()) {
567
- // Ok(new_dir) => ProcessFileResult::ProcessedDirectory(
568
- // NodeOrMetadata::TreeNode(TreeNode {
569
- // dir: HybridDir::Owned(new_dir),
570
- // filename: entry_filename,
571
- // metadata: entry_metadata,
572
- // }),
573
- // ),
574
- // Err(error) => {
575
- // err_reporter(entry, error);
576
- // ProcessFileResult::NotProcessed
577
- // }
578
- // }
579
- // }
580
568
} else {
581
569
// "Permission denied" error. `io::ErrorKind::PermissionDenied` uses
582
570
// lowercase for "permission" in the error message so don't use that here.
@@ -642,19 +630,22 @@ where
642
630
let filename_cstr = CString :: new ( component. as_os_str ( ) . as_bytes ( ) ) . unwrap ( ) ;
643
631
let filename = cstring_to_rc ( & filename_cstr) ;
644
632
645
- starting_dir =
646
- match FileDescriptor :: open_at ( & starting_dir, filename. as_ptr ( ) , libc:: O_RDONLY ) {
647
- Ok ( fd) => fd,
648
- Err ( e) => {
649
- if let Some ( path_stack) = & path_stack {
650
- err_reporter (
651
- Entry :: new ( & starting_dir, path_stack, filename, None ) ,
652
- Error :: new ( e, ErrorKind :: Open ) ,
653
- ) ;
654
- }
655
- return None ;
633
+ starting_dir = match FileDescriptor :: open_at (
634
+ & starting_dir,
635
+ unsafe { CStr :: from_ptr ( filename. as_ptr ( ) ) } ,
636
+ libc:: O_RDONLY ,
637
+ ) {
638
+ Ok ( fd) => fd,
639
+ Err ( e) => {
640
+ if let Some ( path_stack) = & path_stack {
641
+ err_reporter (
642
+ Entry :: new ( & starting_dir, path_stack, filename, None ) ,
643
+ Error :: new ( e, ErrorKind :: Open ) ,
644
+ ) ;
656
645
}
657
- } ;
646
+ return None ;
647
+ }
648
+ } ;
658
649
659
650
if let Some ( path_stack) = & mut path_stack {
660
651
path_stack. push ( filename) ;
@@ -685,12 +676,12 @@ pub struct TraverseDirectoryOpts {
685
676
///
686
677
/// # Arguments
687
678
/// * `path` - Pathname of the directory. Passing a file to this argument will cause the function to
688
- /// return `false` but will otherwise allow processing the file inside `file_handler` like a normal
689
- /// entry.
679
+ /// return `false` but will otherwise allow processing the file inside `file_handler` like a
680
+ /// normal entry.
690
681
///
691
682
/// * `file_handler` - Called for each entry in the tree. If the current entry is a directory, its
692
- /// contents will be skipped if `file_handler` returns `false`. The return value of `file_handler`
693
- /// is ignored when the entry is a file.
683
+ /// contents will be skipped if `file_handler` returns `false`. The return value of
684
+ /// `file_handler` is ignored when the entry is a file.
694
685
///
695
686
/// * `postprocess_dir` - Called when `traverse_directory` is exiting a directory.
696
687
///
@@ -801,12 +792,7 @@ where
801
792
let fd_threshold: usize = fd_rlim_cur as usize - 7 ;
802
793
803
794
// Depth first traversal main loop
804
- ' outer: loop {
805
- let current = match stack. last ( ) {
806
- Some ( last) => last,
807
- None => break , // Done, stack is empty
808
- } ;
809
-
795
+ ' outer: while let Some ( current) = stack. last ( ) {
810
796
let dir = & current. dir ;
811
797
let dir_fd = match dir {
812
798
HybridDir :: Owned ( dir) => dir. file_descriptor ( ) ,
@@ -958,7 +944,7 @@ where
958
944
subdirs. sort_by ( |a, b| {
959
945
let filename_a = unsafe { CStr :: from_ptr ( a. filename . as_ptr ( ) ) } ;
960
946
let filename_b = unsafe { CStr :: from_ptr ( b. filename . as_ptr ( ) ) } ;
961
- filename_a. cmp ( & filename_b)
947
+ filename_a. cmp ( filename_b)
962
948
} ) ;
963
949
964
950
// Add in reverse order because `stack` is a LIFO
@@ -979,12 +965,14 @@ where
979
965
} ,
980
966
None => & starting_dir,
981
967
} ;
982
- if let Err ( _ ) = postprocess_dir ( Entry :: new (
968
+ if postprocess_dir ( Entry :: new (
983
969
prev_dir,
984
970
& path_stack,
985
971
current. filename . clone ( ) ,
986
972
Some ( current. metadata . clone ( ) ) ,
987
- ) ) {
973
+ ) )
974
+ . is_err ( )
975
+ {
988
976
success = false ;
989
977
// Don't `continue` here, falldown below
990
978
}
0 commit comments