+
Skip to content

find: Fix -printf interactions with -L #531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/find/matchers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@
Roots,
/// Always follow symlinks (-L).
Always,
/// Follow all links, treating broken links as errors (-printf %Y).
Force,
}

impl Follow {
Expand All @@ -93,14 +95,20 @@
Self::Never => false,
Self::Roots => depth == 0,
Self::Always => true,
Self::Force => true,
}
}

/// Get metadata for a [WalkEntry].
pub fn metadata(self, entry: &WalkEntry) -> Result<Metadata, WalkError> {
if self.follow_at_depth(entry.depth()) == entry.follow() {
// Same follow flag, re-use cached metadata
entry.metadata().cloned()
if self == Self::Force && entry.file_type().is_symlink() {
// Broken link, return an error
self.metadata_at_depth(entry.path(), entry.depth())

Check warning on line 107 in src/find/matchers/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/find/matchers/mod.rs#L107

Added line #L107 was not covered by tests
} else {
// Same follow flag, re-use cached metadata
entry.metadata().cloned()
}
} else if !entry.follow() && !entry.file_type().is_symlink() {
// Not a symlink, re-use cached metadata
entry.metadata().cloned()
Expand All @@ -126,7 +134,11 @@
let path = path.as_ref();

if self.follow_at_depth(depth) {
match path.metadata().map_err(WalkError::from) {
let meta = path.metadata().map_err(WalkError::from);
if self == Self::Force {
return meta;
}
match meta {
Ok(meta) => return Ok(meta),
Err(e) if !e.is_not_found() => return Err(e),
_ => {}
Expand Down
65 changes: 37 additions & 28 deletions src/find/matchers/printf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

use chrono::{format::StrftimeItems, DateTime, Local};

use super::{FileType, Matcher, MatcherIO, WalkEntry, WalkError};
use super::{FileType, Follow, Matcher, MatcherIO, WalkEntry};

#[cfg(unix)]
use std::os::unix::prelude::MetadataExt;
Expand Down Expand Up @@ -359,18 +359,6 @@
.unwrap()
}

fn format_non_link_file_type(file_type: FileType) -> char {
match file_type {
FileType::Regular => 'f',
FileType::Directory => 'd',
FileType::BlockDevice => 'b',
FileType::CharDevice => 'c',
FileType::Fifo => 'p',
FileType::Socket => 's',
_ => 'U',
}
}

fn format_directive<'entry>(
file_info: &'entry WalkEntry,
directive: &FormatDirective,
Expand Down Expand Up @@ -525,7 +513,7 @@
FormatDirective::StartingPoint => get_starting_point(file_info).to_string_lossy(),

FormatDirective::SymlinkTarget => {
if file_info.path_is_symlink() {
if file_info.file_type().is_symlink() {
fs::read_link(file_info.path())?
.to_string_lossy()
.into_owned()
Expand All @@ -535,22 +523,43 @@
}
}

FormatDirective::Type { follow_links } => if file_info.path_is_symlink() {
if *follow_links {
match file_info.path().metadata().map_err(WalkError::from) {
Ok(meta) => format_non_link_file_type(meta.file_type().into()),
Err(e) if e.is_not_found() => 'N',
Err(e) if e.is_loop() => 'L',
Err(_) => '?',
}
FormatDirective::Type { follow_links } => {
let follow = if *follow_links {
Follow::Force
} else if file_info.follow() {
Follow::Always

Check warning on line 530 in src/find/matchers/printf.rs

View check run for this annotation

Codecov / codecov/patch

src/find/matchers/printf.rs#L530

Added line #L530 was not covered by tests
} else {
'l'
}
} else {
format_non_link_file_type(file_info.file_type())
Follow::Never
};
let meta = follow.metadata(file_info);
let ftype = meta
.as_ref()
.map(|m| m.file_type().into())
.unwrap_or(FileType::Unknown);

let ret = match ftype {
FileType::Regular => "f",
FileType::Directory => "d",
FileType::BlockDevice => "b",
FileType::CharDevice => "c",

Check warning on line 544 in src/find/matchers/printf.rs

View check run for this annotation

Codecov / codecov/patch

src/find/matchers/printf.rs#L543-L544

Added lines #L543 - L544 were not covered by tests
FileType::Fifo => "p",
FileType::Socket => "s",
FileType::Symlink => "l",
FileType::Unknown if *follow_links => {
match meta {
Err(e) if e.is_not_found() => "N",
Err(e) if e.is_loop() => "L",
Err(_) => {
// TODO: matcher_io.set_exit_code(1);
"?"

Check warning on line 554 in src/find/matchers/printf.rs

View check run for this annotation

Codecov / codecov/patch

src/find/matchers/printf.rs#L554

Added line #L554 was not covered by tests
}
_ => "U",

Check warning on line 556 in src/find/matchers/printf.rs

View check run for this annotation

Codecov / codecov/patch

src/find/matchers/printf.rs#L556

Added line #L556 was not covered by tests
}
}
FileType::Unknown => "U",

Check warning on line 559 in src/find/matchers/printf.rs

View check run for this annotation

Codecov / codecov/patch

src/find/matchers/printf.rs#L559

Added line #L559 was not covered by tests
};
ret.into()
}
.to_string()
.into(),

#[cfg(not(unix))]
FormatDirective::User { .. } => "0".into(),
Expand Down
Loading
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载