-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
fix(realpath): To error out in long paths #13223
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
HackingRepo
wants to merge
12
commits into
uutils:main
Choose a base branch
from
HackingRepo:patch-3
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+71
−10
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8fbf5c4
Enhance error handling for file name length, to match gnu
HackingRepo 6aa2e66
Refactor code
HackingRepo 365129e
Fix caused unused variable warning
HackingRepo def010c
Use MAX_PATH constant instead of 255
HackingRepo ef238d0
Add tests for long path component handling
HackingRepo 2b5cb41
Fix cargo fmt issues
HackingRepo d4a1692
Restore removed accidentaly tests
HackingRepo 1ebc41a
fix failing tests
HackingRepo 49d30ec
Fix testing issues
HackingRepo 74de757
Remove 'Fix:' from comments
HackingRepo 82d8d64
rsplit to ignored by spell check
HackingRepo e172782
Add translate! to the error message
HackingRepo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,15 +3,15 @@ | |
| // For the full copyright and license information, please view the LICENSE | ||
| // file that was distributed with this source code. | ||
|
|
||
| // spell-checker:ignore (ToDO) retcode | ||
| // spell-checker:ignore (ToDO) retcode rsplit | ||
|
|
||
| use clap::{ | ||
| Arg, ArgAction, ArgMatches, Command, | ||
| builder::{TypedValueParser, ValueParserFactory}, | ||
| }; | ||
| use std::{ | ||
| ffi::{OsStr, OsString}, | ||
| io::{Write, stdout}, | ||
| io::{Error, ErrorKind, Write, stdout}, | ||
| path::{Path, PathBuf}, | ||
| }; | ||
| use uucore::fs::make_path_relative_to; | ||
|
|
@@ -35,7 +35,7 @@ const OPT_CANONICALIZE: &str = "canonicalize"; | |
| const OPT_CANONICALIZE_EXISTING: &str = "canonicalize-existing"; | ||
| const OPT_RELATIVE_TO: &str = "relative-to"; | ||
| const OPT_RELATIVE_BASE: &str = "relative-base"; | ||
|
|
||
| const MAX_PATH: usize = 255; | ||
| const ARG_FILES: &str = "files"; | ||
|
|
||
| /// Custom parser that validates `OsString` is not empty | ||
|
|
@@ -75,7 +75,7 @@ impl ValueParserFactory for NonEmptyOsStringParser { | |
| pub fn uumain(args: impl uucore::Args) -> UResult<()> { | ||
| let matches = uucore::clap_localization::handle_clap_result(uu_app(), args)?; | ||
|
|
||
| /* the list of files */ | ||
| /* the list of files */ | ||
|
|
||
| let paths: Vec<PathBuf> = matches | ||
| .get_many::<OsString>(ARG_FILES) | ||
|
|
@@ -294,14 +294,46 @@ fn resolve_path( | |
| relative_to: Option<&Path>, | ||
| relative_base: Option<&Path>, | ||
| ) -> std::io::Result<()> { | ||
| let abs = canonicalize(p, can_mode, resolve)?; | ||
| if p.components().any(|c| { | ||
| c.as_os_str() | ||
| .to_str() | ||
| .is_some_and(|name| name.len() > MAX_PATH) | ||
| }) { | ||
| return Err(Error::new(ErrorKind::InvalidInput, translate!("File name too long"))); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it isn't the way translate works |
||
| } | ||
|
|
||
| // GNU realpath compatibility: If a path explicitly references a directory modifier | ||
| // via a trailing slash or directory shortcuts like `/.` or `/..`, the parent must exist. | ||
| if can_mode == MissingHandling::Normal { | ||
| let path_str = p.to_string_lossy(); | ||
| if path_str.ends_with("/.") || path_str.ends_with("/./") { | ||
| abs.metadata()?; // raise no such file or directory error | ||
| let p_str = p.to_string_lossy(); | ||
| let has_trailing_dot = | ||
| p_str.ends_with("/.") || p_str.ends_with("/..") || p_str == "." || p_str == ".."; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we don't have a function already doing that in the code base ? |
||
| let has_trailing_slash = p_str.ends_with('/'); | ||
|
|
||
| if has_trailing_dot || has_trailing_slash { | ||
| // Reconstruct the expected parent path by stripping the trailing segment manually | ||
| // to bypass rust path normalization flaws on non-existent targets. | ||
| let parent_path = if has_trailing_slash { | ||
| p_str.trim_end_matches('/').to_string() | ||
| } else { | ||
| p_str | ||
| .rsplit_once('/') | ||
| .map_or("", |(before, _)| before) | ||
| .to_string() | ||
| }; | ||
|
|
||
| if !parent_path.is_empty() { | ||
| let parent = Path::new(&parent_path); | ||
| // Check symlink metadata. If it doesn't exist AND isn't a broken symlink, fail early. | ||
| if !parent.exists() && parent.symlink_metadata().is_err() { | ||
| return Err(Error::new(ErrorKind::NotFound, "No such file or directory")); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| let abs = canonicalize(p, can_mode, resolve)?; | ||
|
|
||
| let abs = process_relative(abs, relative_base, relative_to); | ||
|
|
||
| print_verbatim(abs)?; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems low, no ?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no