Repository navigation
cp: preserve file contents with -u --attributes-only #16781
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
limadog9
wants to merge
1
commit into
uutils:main
Choose a base branch
from
limadog9:cp-attributes-update-16780
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.
+116
−41
Open
Changes from all commits
Commits
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 |
|---|---|---|
|
|
@@ -2603,7 +2603,7 @@ fn handle_copy_mode( | |
| ) | ||
| })?; | ||
| } | ||
| CopyMode::Copy => { | ||
| CopyMode::Copy | CopyMode::AttrOnly => { | ||
| copy_helper( | ||
| source, | ||
| dest, | ||
|
|
@@ -2687,42 +2687,6 @@ fn handle_copy_mode( | |
| )?; | ||
| } | ||
| } | ||
| CopyMode::AttrOnly => { | ||
| // The destination must have the source's type. Creating a regular | ||
| // file for a FIFO or a device both gets the type wrong and, for a | ||
| // FIFO, makes the later xattr copy open it and block forever with | ||
| // no writer. | ||
| #[cfg(unix)] | ||
| { | ||
| let ft = source_metadata.file_type(); | ||
| if ft.is_fifo() { | ||
| return copy_fifo(dest, options.overwrite, options.debug) | ||
| .map(|()| PerformedAction::Copied); | ||
| } | ||
| if ft.is_socket() { | ||
| return copy_socket(dest, options.overwrite, options.debug) | ||
| .map(|()| PerformedAction::Copied); | ||
| } | ||
| if ft.is_char_device() || ft.is_block_device() { | ||
| return copy_node(dest, source_metadata, options.overwrite, options.debug) | ||
| .map(|()| PerformedAction::Copied); | ||
| } | ||
| } | ||
| OpenOptions::new() | ||
| .write(true) | ||
| .truncate(false) | ||
| .create(true) | ||
| .open(dest) | ||
| .map_err(|e| { | ||
| CpError::IoErrContext( | ||
| e, | ||
| translate!( | ||
| "cp-error-cannot-create-regular-file", | ||
| "path" => dest.quote() | ||
| ), | ||
| ) | ||
| })?; | ||
| } | ||
| } | ||
|
|
||
| Ok(PerformedAction::Copied) | ||
|
|
@@ -3141,8 +3105,8 @@ fn handle_no_preserve_mode(options: &Options, org_mode: u32) -> u32 { | |
| org_mode | ||
| } | ||
|
|
||
| /// Copy the file from `source` to `dest` either using the normal `fs::copy` or a | ||
| /// copy-on-write scheme if --reflink is specified and the filesystem supports it. | ||
| /// Create the destination according to the source file type and copy its data | ||
| /// unless only attributes were requested. | ||
| #[allow(clippy::too_many_arguments)] | ||
| fn copy_helper( | ||
| source: &Path, | ||
|
|
@@ -3165,8 +3129,11 @@ fn copy_helper( | |
| return Err(CpError::NotADirectory(dest.to_path_buf())); | ||
| } | ||
|
|
||
| // Library callers can select AttrOnly without setting the CLI flag. | ||
| // Conversely, -u --attributes-only selects Update and sets the flag. | ||
| let attributes_only = options.attributes_only || options.copy_mode == CopyMode::AttrOnly; | ||
| #[cfg(unix)] | ||
| if options.recursive && !options.copy_contents { | ||
| if attributes_only || (options.recursive && !options.copy_contents) { | ||
| let ft = source_metadata.file_type(); | ||
| if ft.is_socket() { | ||
| return copy_socket(dest, options.overwrite, options.debug); | ||
|
|
@@ -3181,6 +3148,21 @@ fn copy_helper( | |
|
|
||
| if source_metadata.is_symlink() { | ||
| copy_link(source, dest, symlinked_files, options)?; | ||
| } else if attributes_only { | ||
|
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. this also changes |
||
| OpenOptions::new() | ||
| .write(true) | ||
| .truncate(false) | ||
| .create(true) | ||
| .open(dest) | ||
| .map_err(|e| { | ||
| CpError::IoErrContext( | ||
| e, | ||
| translate!( | ||
| "cp-error-cannot-create-regular-file", | ||
| "path" => dest.quote() | ||
| ), | ||
| ) | ||
| })?; | ||
| } else { | ||
| // Use O_NOFOLLOW on the source open iff cp is in no-dereference mode. | ||
| // In that case source_metadata was obtained via lstat, so a path swap | ||
|
|
@@ -3202,7 +3184,7 @@ fn copy_helper( | |
| nofollow, | ||
| )?; | ||
|
|
||
| if !options.attributes_only && options.debug { | ||
| if options.debug { | ||
| show_debug(©_debug) | ||
| .map_err(|e| CpError::IoErrContext(e, translate!("cp-error-write")))?; | ||
| } | ||
|
|
||
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.
CopyMode::AttrOnlyalready impliesoptions.attributes_only, isn't the second check redundant?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.
It’s redundant for CLI calls, but Rust callers can set CopyMode::AttrOnly while leaving attributes_only at its default of false. I kept the check and added a test for that case. I also added the --remove-destination --attributes-only regression test