Skip to content
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

cp: truncate destination when --reflink is set #3759

Merged
merged 4 commits into from
Aug 4, 2022
Merged
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
2 changes: 1 addition & 1 deletion src/uu/cp/src/cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1535,7 +1535,7 @@ fn copy_on_write_linux(
let src_file = File::open(source).context(context)?;
let dst_file = OpenOptions::new()
.write(true)
.truncate(false)
.truncate(true)
.create(true)
.open(dest)
.context(context)?;
Expand Down
80 changes: 79 additions & 1 deletion tests/by-util/test_cp.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// spell-checker:ignore (flags) reflink (fs) tmpfs (linux) rlimit Rlim NOFILE clob
// spell-checker:ignore (flags) reflink (fs) tmpfs (linux) rlimit Rlim NOFILE clob btrfs ROOTDIR USERDIR

use crate::common::util::*;
#[cfg(not(windows))]
Expand Down Expand Up @@ -1388,6 +1388,84 @@ fn test_closes_file_descriptors() {
.succeeds();
}

#[cfg(any(target_os = "linux", target_os = "android"))]
#[test]
fn test_cp_reflink_always_override() {
let scene = TestScenario::new(util_name!());

const DISK: &str = "disk.img";
const ROOTDIR: &str = "disk_root/";
const USERDIR: &str = "dir/";
const MOUNTPOINT: &str = "mountpoint/";

let src1_path: &str = &vec![MOUNTPOINT, USERDIR, "src1"].concat();
let src2_path: &str = &vec![MOUNTPOINT, USERDIR, "src2"].concat();
let dst_path: &str = &vec![MOUNTPOINT, USERDIR, "dst"].concat();

scene.fixtures.mkdir(ROOTDIR);
scene.fixtures.mkdir(&vec![ROOTDIR, USERDIR].concat());

// Setup:
// Because neither `mkfs.btrfs` not btrfs `mount` options allow us to have a mountpoint owned
// by a non-root user, we want the following directory structure:
//
// uid | path
// ---------------------------
// user | .
// root | └── mountpoint
// user | └── dir
// user | ├── src1
// user | └── src2

scene
.ccmd("truncate")
.args(&["-s", "128M", DISK])
.succeeds();

if !scene
.cmd_keepenv("env")
.args(&["mkfs.btrfs", "--rootdir", ROOTDIR, DISK])
.run()
.succeeded()
{
print!("Test skipped; couldn't make btrfs disk image");
return;
}

scene.fixtures.mkdir(MOUNTPOINT);

let mount = scene
.cmd_keepenv("sudo")
.args(&["-E", "--non-interactive", "mount", DISK, MOUNTPOINT])
.run();

if !mount.succeeded() {
print!("Test skipped; requires root user");
return;
}

scene.fixtures.make_file(src1_path);
scene.fixtures.write_bytes(src1_path, &[0x64; 8192]);

scene.fixtures.make_file(src2_path);
scene.fixtures.write(src2_path, "other data");

scene
.ucmd()
.args(&["--reflink=always", src1_path, dst_path])
.succeeds();

scene
.ucmd()
.args(&["--reflink=always", src2_path, dst_path])
.succeeds();

scene
.cmd_keepenv("sudo")
.args(&["-E", "--non-interactive", "umount", MOUNTPOINT])
.succeeds();
}

#[test]
fn test_copy_dir_symlink() {
let (at, mut ucmd) = at_and_ucmd!();
Expand Down