install: fix -D data-loss when destination parent is the root directory#13259
Open
koopatroopa787 wants to merge 1 commit into
Open
install: fix -D data-loss when destination parent is the root directory#13259koopatroopa787 wants to merge 1 commit into
koopatroopa787 wants to merge 1 commit into
Conversation
`install -D src /file` strips trailing slashes from the destination's parent directory before calling `create_dir_all_safe`. For a parent of `"/"` the loop removes ALL bytes and produces `""`, which `find_existing_ancestor` (inside `create_dir_all_safe`) maps to `"."` — the current directory. The resulting `DirFd` points at CWD, so the subsequent `unlink_at(cwd, "file")` deletes the source file, and the copy fails leaving the user with no file at all. Fix: when the post-stripping result would be empty (i.e. the original path was entirely slashes), keep the path unchanged so the root directory is opened as the parent fd instead of the current directory. Adds a regression test that verifies the source file is preserved when `install -D src /single-component-dest` fails (as a non-root user). Fixes uutils#13232
Merging this PR will improve performance by 4.84%
Performance Changes
Tip Curious why this is faster? Comment Comparing Footnotes
|
|
GNU testsuite comparison: |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
install -D src /filesilently deletes the source file and thenfails, leaving the user with neither the source nor the destination.
Root cause
When the target is a single-component absolute path like
/file,target.parent()returns"/". Before callingcreate_dir_all_safe,the code strips trailing slashes from that parent:
For
"/"this strips ALL bytes and produces"".find_existing_ancestor("")maps an empty path to"."(currentdirectory), so
create_dir_all_safereturns aDirFdpointing at CWDinstead of
/.The subsequent
unlink_at(cwd_fd, "file")then removes./file— thesource — rather than any existing destination. The copy step fails
because the source is gone, and the user ends up with nothing.
Fix
Skip the trailing-slash strip when the result would be empty (i.e. the
path was entirely slashes). The root directory
"/"is left as-is, socreate_dir_all_safeopens/as the parent fd and laterunlink_at(root_fd, "file")targets the intended destination.Changes
src/uu/install/src/install.rs: guard the slash-strip so anall-slash path (
"/") is kept intact.tests/by-util/test_install.rs: regression test verifying the sourcefile survives when
install -D src /destfails (non-root execution).Fixes #13232