Skip to content

Commit 3e24ad7

Browse files
author
Joseph Rafael Ferrer
committed
Fix clippy errors
1 parent 306af46 commit 3e24ad7

File tree

8 files changed

+98
-98
lines changed

8 files changed

+98
-98
lines changed

ftw/src/dir.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,17 @@ impl OwnedDir {
8080
dir_file_descriptor: &FileDescriptor,
8181
filename: *const libc::c_char,
8282
) -> Result<Self, Error> {
83-
let file_descriptor =
84-
FileDescriptor::open_at(dir_file_descriptor, filename, libc::O_RDONLY)
85-
.map_err(|e| Error::new(e, ErrorKind::Open))?;
83+
let file_descriptor = FileDescriptor::open_at(
84+
dir_file_descriptor,
85+
unsafe { CStr::from_ptr(filename) },
86+
libc::O_RDONLY,
87+
)
88+
.map_err(|e| Error::new(e, ErrorKind::Open))?;
8689
let dir = OwnedDir::new(file_descriptor).map_err(|e| Error::new(e, ErrorKind::OpenDir))?;
8790
Ok(dir)
8891
}
8992

90-
pub fn iter<'a>(&'a self) -> OwnedDirIterator<'a> {
93+
pub fn iter(&self) -> OwnedDirIterator {
9194
OwnedDirIterator {
9295
dirp: self.dirp,
9396
phantom: PhantomData,
@@ -150,7 +153,7 @@ impl DeferredDir {
150153
}
151154
}
152155

153-
pub fn iter<'a>(&'a self) -> DeferredDirIterator<'a> {
156+
pub fn iter(&self) -> DeferredDirIterator {
154157
let file_descriptor = self.open_file_descriptor();
155158
let dir = OwnedDir::new(file_descriptor).unwrap();
156159
let dirp = dir.dirp;
@@ -177,7 +180,7 @@ impl DeferredDir {
177180

178181
let filename_cstr = CString::new(components.as_path().as_os_str().as_bytes()).unwrap();
179182

180-
FileDescriptor::open_at(&starting_dir, filename_cstr.as_ptr(), libc::O_RDONLY).unwrap()
183+
FileDescriptor::open_at(&starting_dir, &filename_cstr, libc::O_RDONLY).unwrap()
181184
}
182185

183186
pub fn parent(&self) -> &Rc<(FileDescriptor, PathBuf)> {

ftw/src/lib.rs

Lines changed: 44 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ impl FileDescriptor {
8181
/// Create a `FileDescriptor` with arguments similar to `libc::openat`.
8282
pub fn open_at(
8383
dir_file_descriptor: &FileDescriptor,
84-
filename: *const libc::c_char,
84+
file_name: &CStr,
8585
flags: i32,
8686
) -> io::Result<Self> {
8787
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);
8989
if fd == -1 {
9090
Err(io::Error::last_os_error())
9191
} else {
@@ -124,7 +124,7 @@ impl Metadata {
124124
/// process' current working directory.
125125
pub fn new(
126126
dirfd: libc::c_int,
127-
filename: *const libc::c_char,
127+
file_name: &CStr,
128128
follow_symlinks: bool,
129129
) -> io::Result<Metadata> {
130130
let mut statbuf = MaybeUninit::uninit();
@@ -133,7 +133,7 @@ impl Metadata {
133133
} else {
134134
libc::AT_SYMLINK_NOFOLLOW
135135
};
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) };
137137
if ret != 0 {
138138
return Err(io::Error::last_os_error());
139139
}
@@ -400,11 +400,8 @@ impl<'a> Entry<'a> {
400400

401401
/// Check if this `Entry` is an empty directory.
402402
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)?;
408405
match OwnedDir::new(file_descriptor) {
409406
Ok(dir) => {
410407
let mut num_entries = 0;
@@ -474,6 +471,8 @@ impl Deref for DisplayablePath {
474471
}
475472
}
476473

474+
// Ignore clippy warnings, this is only used in `process_file` below
475+
#[allow(clippy::large_enum_variant)]
477476
enum ProcessFileResult<'a> {
478477
ProcessedDirectory(Entry<'a>),
479478
ProcessedFile,
@@ -495,11 +494,15 @@ where
495494
H: FnMut(Entry<'_>, Error),
496495
{
497496
// 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+
) {
499502
Ok(md) => md,
500503
Err(e) => {
501504
err_reporter(
502-
Entry::new(dir_fd, &path_stack, entry_filename, None),
505+
Entry::new(dir_fd, path_stack, entry_filename, None),
503506
Error::new(e, ErrorKind::Stat),
504507
);
505508
return ProcessFileResult::NotProcessed;
@@ -521,7 +524,11 @@ where
521524
}
522525
};
523526

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+
) {
525532
Ok(md) => (Some(read_link), md),
526533
Err(e) => {
527534
if e.kind() == io::ErrorKind::NotFound {
@@ -558,25 +565,6 @@ where
558565
// Is the directory searchable?
559566
if entry_metadata.is_executable() {
560567
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-
// }
580568
} else {
581569
// "Permission denied" error. `io::ErrorKind::PermissionDenied` uses
582570
// lowercase for "permission" in the error message so don't use that here.
@@ -642,19 +630,22 @@ where
642630
let filename_cstr = CString::new(component.as_os_str().as_bytes()).unwrap();
643631
let filename = cstring_to_rc(&filename_cstr);
644632

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+
);
656645
}
657-
};
646+
return None;
647+
}
648+
};
658649

659650
if let Some(path_stack) = &mut path_stack {
660651
path_stack.push(filename);
@@ -685,12 +676,12 @@ pub struct TraverseDirectoryOpts {
685676
///
686677
/// # Arguments
687678
/// * `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.
690681
///
691682
/// * `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.
694685
///
695686
/// * `postprocess_dir` - Called when `traverse_directory` is exiting a directory.
696687
///
@@ -801,12 +792,7 @@ where
801792
let fd_threshold: usize = fd_rlim_cur as usize - 7;
802793

803794
// 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() {
810796
let dir = &current.dir;
811797
let dir_fd = match dir {
812798
HybridDir::Owned(dir) => dir.file_descriptor(),
@@ -958,7 +944,7 @@ where
958944
subdirs.sort_by(|a, b| {
959945
let filename_a = unsafe { CStr::from_ptr(a.filename.as_ptr()) };
960946
let filename_b = unsafe { CStr::from_ptr(b.filename.as_ptr()) };
961-
filename_a.cmp(&filename_b)
947+
filename_a.cmp(filename_b)
962948
});
963949

964950
// Add in reverse order because `stack` is a LIFO
@@ -979,12 +965,14 @@ where
979965
},
980966
None => &starting_dir,
981967
};
982-
if let Err(_) = postprocess_dir(Entry::new(
968+
if postprocess_dir(Entry::new(
983969
prev_dir,
984970
&path_stack,
985971
current.filename.clone(),
986972
Some(current.metadata.clone()),
987-
)) {
973+
))
974+
.is_err()
975+
{
988976
success = false;
989977
// Don't `continue` here, falldown below
990978
}

ftw/tests/integration.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ fn test_ftw_deep() {
144144
panic!("{}", io::Error::last_os_error());
145145
}
146146

147-
fd = ftw::FileDescriptor::open_at(&fd, filename.as_ptr(), libc::O_RDONLY).unwrap();
147+
fd = ftw::FileDescriptor::open_at(&fd, filename, libc::O_RDONLY).unwrap();
148148
}
149149

150150
let mut count = 0;
@@ -322,7 +322,7 @@ fn test_ftw_long_filename() {
322322
panic!("{}", io::Error::last_os_error());
323323
}
324324

325-
fd = ftw::FileDescriptor::open_at(&fd, filename.as_ptr(), libc::O_RDONLY).unwrap();
325+
fd = ftw::FileDescriptor::open_at(&fd, filename, libc::O_RDONLY).unwrap();
326326

327327
// If at the last index, add dummy directories
328328
if i == DIR_HIERARCHY_DEPTH - 1 {

ftw/tests/integration2.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ fn test_ftw_too_many_open_files() {
9191
panic!("{}", io::Error::last_os_error());
9292
}
9393

94-
fd = ftw::FileDescriptor::open_at(&fd, filename.as_ptr(), libc::O_RDONLY).unwrap();
94+
fd = ftw::FileDescriptor::open_at(&fd, filename, libc::O_RDONLY).unwrap();
9595
}
9696
}
9797

@@ -125,7 +125,7 @@ fn test_ftw_too_many_open_files() {
125125
// Open the penultimate directory
126126
for i in 0..DIR_HIERARCHY_DEPTH - 1 {
127127
let filename = if i == 0 { &test_dir_name } else { &dir_name };
128-
fd = ftw::FileDescriptor::open_at(&fd, filename.as_ptr(), libc::O_RDONLY).unwrap();
128+
fd = ftw::FileDescriptor::open_at(&fd, filename, libc::O_RDONLY).unwrap();
129129
}
130130

131131
// Remove the directories starting from the deepest
@@ -142,7 +142,7 @@ fn test_ftw_too_many_open_files() {
142142
}
143143

144144
// Go up a level in the tree
145-
fd = ftw::FileDescriptor::open_at(&fd, c"..".as_ptr(), libc::O_RDONLY).unwrap();
145+
fd = ftw::FileDescriptor::open_at(&fd, c"..", libc::O_RDONLY).unwrap();
146146
}
147147

148148
assert!(!Path::new(test_dir).exists());

tree/common/mod.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,18 @@ where
104104
return Ok(CopyResult::CopiedFile);
105105
}
106106

107-
let source_deref_md = ftw::Metadata::new(source.dir_fd(), source.file_name().as_ptr(), true);
107+
let source_deref_md = unsafe { ftw::Metadata::new(source.dir_fd(), source.file_name(), true) };
108108

109-
let target_symlink_md = ftw::Metadata::new(target_dirfd, target_filename, false);
110-
let target_deref_md = ftw::Metadata::new(target_dirfd, target_filename, true);
109+
let target_symlink_md = ftw::Metadata::new(
110+
target_dirfd,
111+
unsafe { CStr::from_ptr(target_filename) },
112+
false,
113+
);
114+
let target_deref_md = ftw::Metadata::new(
115+
target_dirfd,
116+
unsafe { CStr::from_ptr(target_filename) },
117+
true,
118+
);
111119
let target_is_dangling_symlink = target_symlink_md.is_ok() && target_deref_md.is_err();
112120

113121
let target_symlink_md = match target_symlink_md {
@@ -527,11 +535,13 @@ where
527535
// not allow atomically creating a directory then opening it:
528536
//
529537
// https://stackoverflow.com/questions/45818628/whats-the-expected-behavior-of-openname-o-creato-directory-mode/48693137#48693137
530-
let new_target_dirfd = match ftw::FileDescriptor::open_at(
531-
target_dirfd,
532-
target_filename_cstr.as_ptr(),
533-
libc::O_RDONLY,
534-
) {
538+
let new_target_dirfd = match unsafe {
539+
ftw::FileDescriptor::open_at(
540+
target_dirfd,
541+
&target_filename_cstr,
542+
libc::O_RDONLY,
543+
)
544+
} {
535545
Ok(fd) => fd,
536546
Err(e) => {
537547
let err_str = gettext!(
@@ -773,7 +783,7 @@ fn copy_characteristics(
773783
// `io::copy`).
774784
// Should fix sporadic errors on `test_cp_preserve_slink_time` where `dangle` has a later
775785
// access time than `d2`.
776-
let source_md = ftw::Metadata::new(source.dir_fd(), source.file_name().as_ptr(), false)?;
786+
let source_md = unsafe { ftw::Metadata::new(source.dir_fd(), source.file_name(), false) }?;
777787

778788
// [last_access_time, last_modified_time]
779789
let times = [
@@ -829,7 +839,8 @@ fn copy_characteristics(
829839

830840
// Symbolic link permissions are ignored on Linux
831841
#[cfg(target_os = "linux")]
832-
if let Ok(md) = ftw::Metadata::new(target_dirfd, target_filename, false) {
842+
if let Ok(md) = ftw::Metadata::new(target_dirfd, CStr::from_ptr(target_filename), false)
843+
{
833844
if md.file_type() == ftw::FileType::SymbolicLink {
834845
if let Some(errno) = fchmodat_error.raw_os_error() {
835846
if errno == libc::EOPNOTSUPP {

tree/ls.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,7 @@ fn ls(paths: Vec<PathBuf>, config: &Config) -> io::Result<u8> {
927927
let mut file_entries = Vec::new();
928928
for path in files {
929929
let path_cstr = CString::new(path.as_os_str().as_bytes()).unwrap();
930-
let metadata = match ftw::Metadata::new(libc::AT_FDCWD, path_cstr.as_ptr(), false) {
930+
let metadata = match ftw::Metadata::new(libc::AT_FDCWD, &path_cstr, false) {
931931
Ok(m) => m,
932932
Err(e) => {
933933
eprintln!("ls: {e}");
@@ -946,7 +946,7 @@ fn ls(paths: Vec<PathBuf>, config: &Config) -> io::Result<u8> {
946946
// If -H or -L are enabled, the metadata to be reported is from the file
947947
// that the symbolic link points to.
948948
let metadata = if metadata.is_symlink() && dereference_symlink {
949-
match ftw::Metadata::new(libc::AT_FDCWD, path_cstr.as_ptr(), true) {
949+
match ftw::Metadata::new(libc::AT_FDCWD, &path_cstr, true) {
950950
Ok(m) => m,
951951
Err(e) => {
952952
eprintln!("ls: {e}");
@@ -1114,20 +1114,18 @@ fn process_single_dir(
11141114

11151115
// Get the metadata of the file, equivalent to `std::fs::symlink_metadata`
11161116
let marker = {
1117-
let metadata = match ftw::Metadata::new(
1118-
dir_entry.dir_fd(),
1119-
dir_entry.file_name().as_ptr(),
1120-
false,
1121-
) {
1122-
Ok(md) => md,
1123-
Err(e) => {
1124-
let path_str =
1125-
ls_from_utf8_lossy(dir_entry.path().as_inner().as_os_str().as_bytes());
1126-
let err_str = gettext!("cannot access '{}': {}", path_str, e);
1127-
errors.push(io::Error::other(err_str));
1128-
return Ok(false);
1129-
}
1130-
};
1117+
let metadata =
1118+
match ftw::Metadata::new(dir_entry.dir_fd(), dir_entry.file_name(), false) {
1119+
Ok(md) => md,
1120+
Err(e) => {
1121+
let path_str = ls_from_utf8_lossy(
1122+
dir_entry.path().as_inner().as_os_str().as_bytes(),
1123+
);
1124+
let err_str = gettext!("cannot access '{}': {}", path_str, e);
1125+
errors.push(io::Error::other(err_str));
1126+
return Ok(false);
1127+
}
1128+
};
11311129
(metadata.dev(), metadata.ino())
11321130
};
11331131

0 commit comments

Comments
 (0)