Skip to content

cp: preserve file contents with -u --attributes-only - #16781

Open
limadog9 wants to merge 1 commit into
uutils:mainfrom
limadog9:cp-attributes-update-16780
Open

limadog9 wants to merge 1 commit into
uutils:mainfrom
limadog9:cp-attributes-update-16780

Conversation

@limadog9

Copy link
Copy Markdown
Contributor

When a source was newer, cp -u --attributes-only copied its data into the destination. An empty template could erase a populated file.

Honor --attributes-only in the copy helper used after the update check. Existing regular files keep their contents, missing files are created empty, and requested attributes are applied in both cases.

Closes #16780

Comment thread src/uu/cp/src/cp.rs
return Err(CpError::NotADirectory(dest.to_path_buf()));
}

let attributes_only = options.attributes_only || options.copy_mode == CopyMode::AttrOnly;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CopyMode::AttrOnly already implies options.attributes_only, isn't the second check redundant?

Copy link
Copy Markdown
Contributor Author

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

Comment thread src/uu/cp/src/cp.rs

if source_metadata.is_symlink() {
copy_link(source, dest, symlinked_files, options)?;
} else if attributes_only {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this also changes --remove-destination --attributes-only on a regular file (it now gives an empty dest instead of the data), could you please add a test for it?

@limadog9
limadog9 force-pushed the cp-attributes-update-16780 branch 2 times, most recently from a4f09ef to ecb4e23 Compare November 23, 2026 13:07
When a source is newer, apply the requested attributes without copying
its data. Keep the existing destination contents, or create an empty
file if the destination is missing or --remove-destination removed it.

The CLI records --attributes-only separately from update mode. Library
callers can also select CopyMode::AttrOnly with the default flag value.
Test both entry points and the GNU 9.12 regular-file replacement case.

Closes uutils#16780
@limadog9
limadog9 force-pushed the cp-attributes-update-16780 branch from ecb4e23 to bbd4f06 Compare December 1, 2026 17:25

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cp -u --attributes-only silently overwrites existing file contents when the source is newer

2 participants