Commit graph

121 commits

Author SHA1 Message Date
Terts Diepraam
44ea43f058 tests: remove status_code, which is the same as code_is 2023-01-01 17:25:59 +01:00
Sylvestre Ledru
8489e9b69c Ignore some cp tests on android
Closes: #4233
2022-12-17 16:54:23 +01:00
Jeffrey Finkelstein
1a839fb2c4 cp: correct --verbose --parents output for dirs
This commit corrects the behavior of `cp -r --parents --verbose` when
the source path is a directory, so that it prints the copied ancestor
directories. For example,

    $ mkdir -p a/b/c d
    $ cp -r --verbose --parents a/b/c d
    a -> d/a
    a/b -> d/a/b
    'a/b/c' -> 'd/a/b/c'
2022-12-05 21:37:13 -05:00
Jeffrey Finkelstein
bd665ea44a cp: correct --verbose --parents output for files
This commit corrects the behavior of `cp --parents --verbose` when the
source path is a file so that it prints the copied ancestor
directories. For example,

    $ mkdir -p a/b d
    $ touch a/b/c
    $ cp --verbose --parents a/b/c d
    a -> d/a
    a/b -> d/a/b
    'a/b/c' -> 'd/a/b/c'

Fixes #3332.
2022-12-05 21:36:56 -05:00
Jeffrey Finkelstein
682667fd1d cp: force link between two files that exist
Allow `cp` to link files that exist. For example,

    $ touch src dest
    $ cp -f --link src dest

Before this commit, an error was raised.
2022-12-04 19:36:05 -05:00
Jeffrey Finkelstein
bf54fce055 tests: skip inode check on BSD 2022-12-03 23:50:39 -05:00
Sylvestre Ledru
75502aba93
Merge pull request #4136 from Joining7943/tail-test-runner
`tests/common/util`: Implement UChild, an abstraction for std::process::Child, to return UChild from run_no_wait instead
2022-12-03 10:18:25 +01:00
Jeffrey Finkelstein
66ee373373 cp: preserve hard links when target already exists
Prevent a panic in `cp -a` when the target of a hard link already
exists in the target directory structure.

For example,

    $ mkdir -p src dest/src
    $ touch src/f dest/src/f
    $ ln src/f src/link
    $ cp -a src dest

The `cp` command now succeeds without error.
2022-12-02 23:50:18 -05:00
Joining7943
4a2ced5940 tests: Adjust some tests to use improved UChild. test_tac: Ignore pipe tests on windows.
Summary:
* Disable test_retry6 on android because of intermittent failures
* Use wait() instead of wait_with_output in test_cat, test_cp, test_sort
* tests/sort: Simplify usage of test_sigpipe_panic
* Fix tests in test_tee.

tests/tac:
There was a change in the `tests/common/util.rs` test api concerning piped input which may have
revealed a bug in the implementation of tac. Please see also
https://github.com/uutils/coreutils/pull/4136
2022-12-02 13:13:10 +01:00
Miles Liu
3896aa31be
cp: fix cp --force --backup f f fails on macOS 2022-11-20 18:14:42 +08:00
Miles Liu
d748e3900b
test_cp: ignore failing tests for FreeBSD 2022-11-19 00:05:37 +08:00
Terts Diepraam
ed34264b95 cp: use uucore prompt_yes instead of custom 2022-11-17 15:09:34 +01:00
Sylvestre Ledru
c00fc0a8c4
Merge pull request #4146 from niyaznigmatullin/clippy_fix
Run `cargo +nightly clippy --fix`
2022-11-17 10:33:40 +01:00
Niyaz Nigmatullin
fdd6a05259 chore: run cargo +nightly clippy --fix 2022-11-16 11:09:44 +02:00
Emil Suleymanov
a6c48b084c test_cp: verify metadata for preserve no args case 2022-11-16 01:39:25 +01:00
Niyaz Nigmatullin
f73f686ca3 test_cp: temporarily disable non-working test on Android 2022-11-15 10:00:59 +02:00
Jeffrey Finkelstein
416a742e31 cp: use sparse copy on --sparse=always
Correct the behavior of `cp --reflink=never --sparse=always` so that
it performs a sparse copy. Before this commit, it was incorrectly
performing a dense copy.
2022-11-10 21:43:19 -05:00
Emil Suleymanov
1172a7e781
cp: remove interactive mode message on 'no' (#4069) 2022-11-10 12:45:18 +01:00
Sylvestre Ledru
8e5c259e4b
Merge pull request #3966 from jfinkels/cp-backup-force
cp: force copying file to itself with --backup
2022-11-06 08:43:52 +01:00
Sylvestre Ledru
3ea7a061d7
Merge pull request #4097 from devnexen/fbsd_test_fix
fix cp tests build since they re supposed to be ignored on freebsd an…
2022-11-06 07:41:16 +01:00
Sylvestre Ledru
446df9da5a
Merge branch 'main' into cp-backup-force 2022-11-06 07:24:48 +01:00
Sylvestre Ledru
47e3149e85
Merge pull request #3978 from jfinkels/cp-copy-contents-fifo
cp: implement --copy-contents option for fifos
2022-11-06 07:23:25 +01:00
Jeffrey Finkelstein
5b100fef62 cp: implement --copy-contents option for fifos
Implement the `--copy-contents` option when the source is a FIFO, so
that the contents of the FIFO are copied (when the bytes become
available for reading) instead of the FIFO object itself. For example,

    $ mkfifo fifo
    $ cp --copy-contents fifo outfile &
    [1] 1614080
    $ echo foo > fifo
    $ cat outfile
    foo
    [1]+  Done                    cp --copy-contents fifo outfile
2022-11-05 19:25:37 -04:00
Jeffrey Finkelstein
fbed01dd54 cp: force copying file to itself with --backup
Fix the behavior of `cp` when both `--backup` and `--force` are
specified and the source and destination are the same file.  Before
this commit, `cp` terminated without copying and without making a
backup. After this commit, the copy is made and the backup file is
made. For example,

    $ touch f
    $ cp --force --backup f f

results in a backup file `f~` being created.
2022-11-05 19:23:50 -04:00
David CARLIER
40b1ce7151 disable cp tests on FreeBSD where they are supposed to be ignored. 2022-11-03 08:25:40 +01:00
Sylvestre Ledru
b3af47a390
Merge branch 'main' into cp-force-dangling-symlink 2022-10-28 21:24:42 +02:00
Sylvestre Ledru
6e8f8034ba {cp, mv} -i --update source existing should not do anything and exit 0
Should fix tests/mv/update.sh
2022-10-24 21:03:30 +02:00
Jeffrey Finkelstein
d48a650966 cp: restrict copy through dangling symlink with -f
Change `cp` to terminate with an error when attempting to copy through
a dangling symbolic link with the `--force` option. Before this
commit,

    touch src
    ln -s no-such-file dest
    cp -f src dest

would incorrectly replace `dest` with the contents of `src`. After
this commit, it correctly fails with the error message

    cp: not writing through dangling symlink 'dest'
2022-10-23 20:17:20 -04:00
Jeffrey Finkelstein
ac3fcca6c0 cp: correctly copy ancestor dirs in --parents mode
Fix a bug where `cp` failed to copy ancestor directories when using
the `--parents` option. For example, before this commit:

    $ mkdir -p a/b/c d
    $ cp --parents a/b/c d
    $ find d
    d
    d/c

After this commit

    $ mkdir -p a/b/c d
    $ cp --parents a/b/c d
    $ find d
    d
    d/a
    d/a/b
    d/a/b/c

This commit also adds the correct messages for `--verbose` mode:

    $ cp -r --parents --verbose a/b/c d
    a -> d/a
    a/b -> d/a/b
    'a/b/c' -> 'd/a/b/c'

Fixes #3332.
2022-10-23 00:06:56 -04:00
Jeffrey Finkelstein
cee6c25cc4 cp: make test for interactive mode more specific 2022-10-23 00:03:26 -04:00
Niyaz Nigmatullin
277e3d17a1 tests: change error messages for wrong arguments
Clap used `"` for argument values, now uses `'`.
2022-10-16 19:28:04 +03:00
Terts Diepraam
042bb3c4e3 cp: update to clap 4 2022-10-13 17:50:26 +02:00
Sylvestre Ledru
ad0ca2f042
Merge pull request #3973 from jfinkels/cp-directory-preserve-permissions
cp: preserve permissions when copying directory and don't terminate early on inaccessible file
2022-10-10 08:16:27 +02:00
Jeffrey Finkelstein
b89e8e54c4 cp: continue directory walk if file inaccessible
Stop `cp` from terminating prematurely if a file in a directory is
inaccesible due to insufficient permissions.
2022-10-09 17:31:41 -04:00
Jeffrey Finkelstein
2450493302 cp: preserve permissions when copying directory
Make cp preserve the permissions of a directory when copying
it. Before this commit,

    cp -pR src/ dest/

failed to copy the permissions of `src/` to `dest/`. After this
commit, the permissions are correctly copied.
2022-10-09 17:29:29 -04:00
Jeffrey Finkelstein
fba8d495d2 tests: create assert_metadata_eq!() macro
Create `assert_metadata_eq!()` macro to contain a series of assertions
that check whether two `Metadata` objects are equal.
2022-10-09 17:28:43 -04:00
Sylvestre Ledru
88fb43c8e4
Merge branch 'main' into cp-symbolic-link-loop 2022-10-06 09:53:02 +02:00
Jeffrey Finkelstein
8bfd96fb59 cp: correct error message on copying dir to itself
Correct the error message produced when attempting to copy a directory
into itself with `cp`. Before this commit, the error message was

    $ cp -R d d
    cp: cannot copy a directory, 'd', into itself, 'd'

After this commit, the error message is

    $ cp -R d d
    cp: cannot copy a directory, 'd', into itself, 'd/d'
2022-10-05 21:37:30 -04:00
Jeffrey Finkelstein
24630db45e cp: allow removing symbolic link loop destination
Allow `cp --remove-destination` to remove a symbolic link loop (or a
symbolic link that initiates a chain of too many symbolic
links). Before this commit, if `loop` were a symbolic link to itself,
then

    cp --remove-destination file loop

would fail with an error message. After this commit, it succeeds. This
matches the behaviotr of GNU cp.
2022-10-05 13:32:21 +02:00
Emil Suleymanov
a7b637b1eb
cp: assume --reflink=always on no value (#3992)
* Set reflink to auto by default
2022-10-03 20:20:55 +02:00
Niyaz Nigmatullin
84a741fe91 cp: add -H option, add tests, fix test 2022-09-23 20:31:24 +03:00
Niyaz Nigmatullin
2ce999c959 cp: fix cp-i GNU test
`cp` in interactive mode used to write to stdout asking for
overwrite. GNU version writes to stderr.

Changed: write to stderr to make compatible with GNU.
2022-09-17 19:15:52 +03:00
Niyaz Nigmatullin
0d6383dd19 test_cp: comment out bad syscall failing piece of code for Android 2022-09-09 16:12:05 +03:00
Sylvestre Ledru
a8d1d73465 fix a test on cp counting the number of fd:
The testsuite runs in parallel, we have pipe, sockets
opened by other tests.
So, we take in account the various fd to increase the limit
2022-09-07 21:26:03 +02:00
Jeffrey Finkelstein
8458bf1387 Clippy fixes in multiple crates 2022-08-23 18:30:43 -04:00
Jan Scheer
391709c911
tests: ignore failing tests for FreeBSD (#3778) 2022-08-13 10:09:05 +02:00
Niyaz Nigmatullin
fee851dc1c
test/cp: fix test_copy_through_dangling_symlink_no_dereference_permissions (#3789)
* test/cp: -p changes ctime and added sleep for better timestamp testing

* test/cp: add nanoseconds checking for copied timestamps

* test/cp: made compilable on other OSes

* test/cp: added error messages to assert_eq calls
2022-08-08 11:11:17 +02:00
Pierre Marsais
e1991525af
cp: Implement --sparse flag (#3766)
* cp: Refactor `reflink`/`sparse` handling to enable `--sparse` flag

`--sparse` and `--reflink` options have a lot of similarities:
 - They have similar options (`always`, `never`, `auto`)
 - Both need OS specific handling
 - They can be mutually exclusive

Prior to this change, `sparse` was defined as `CopyMode`, but `reflink`
wasn't. Given the similarities, it makes sense to handle them similarly.

The idea behind this change is to move all OS specific file copy
handling in the `copy_on_write_*` functions. Those function then
dispatch to the correct logic depending on the arguments (at the moment,
the tuple `(reflink, sparse)`).

Also, move the handling of `--reflink=never` from `copy_file` to the
`copy_on_write_*` functions, at the cost of a bit of code duplication,
to allow `copy_on_write_*` to handle all cases (and later handle
`--reflink=never` with `--sparse`).

* cp: Implement `--sparse` flag

This begins to address #3362

At the moment, only the `--sparse=always` logic matches the requirement
form GNU cp info page, i.e. always make holes in destination when
possible.

Sparse copy is done by copying the source to the destination block by
block (blocks being of the destination's fs block size). If the block
only holds NUL bytes, we don't write to the destination.

About `--sparse=auto`: according to GNU cp info page, the destination
file will be made sparse if the source file is sparse as well. The next
step are likely to use `lseek` with `SEEK_HOLE` detect if the source
file has holes. Currently, this has the same behaviour as
`--sparse=never`. This `SEEK_HOLE` logic can also be applied to
`--sparse=always` to improve performance when copying sparse files.

About `--sparse=never`: from my understanding, it is not guaranteed that
Rust's `fs::copy` will always produce a file with no holes, as
["platform-specific behavior may change in the
future"](https://doc.rust-lang.org/std/fs/fn.copy.html#platform-specific-behavior)

About other platforms:
 - `macos`: The solution may be to use `fcntl` command `F_PUNCHHOLE`.
 - `windows`: I only see `FSCTL_SET_SPARSE`.

This should pass the following GNU tests:
 - `tests/cp/sparse.sh`
 - `tests/cp/sparse-2.sh`
 - `tests/cp/sparse-extents.sh`
 - `tests/cp/sparse-extents-2.sh`

`sparse-perf.sh` needs `--sparse=auto`, and in particular a way to skip
holes in the source file.

Co-authored-by: Sylvestre Ledru <sylvestre@debian.org>
2022-08-04 13:22:59 +02:00
Pierre Marsais
90a9829287
cp: truncate destination when --reflink is set (#3759)
* cp: truncate destination when `--reflink` is set

This is needed in order to allow overriding an existing file.

```
$ truncate -s 512M /tmp/disk.img
$ mkfs.btrfs /tmp/disk.img
[...]
$ mkdir /tmp/disk
$ sudo mount /tmp/disk.img /tmp/disk
$ sudo chown $(id -u):$(id -g) -R /tmp/disk
$ for i in $(seq 0 8192); do echo -ne 'a' >>/tmp/disk/src1; done
$ echo "success" >/tmp/disk/src2
$
$ # GNU ls supports overriding files created with `--reflink`
$ cp --reflink=always /tmp/disk/src1 /tmp/disk/dst1
$ cp --reflink=always /tmp/disk/src2 /tmp/disk/dst1
$ cat /tmp/disk/dst1
success
$
$ # Now testing with uutils
$ cargo run cp --reflink=always /tmp/disk/src1 /tmp/disk/dst2
    Finished dev [unoptimized + debuginfo] target(s) in 0.25s
     Running `target/debug/coreutils cp --reflink=always /tmp/disk/src1 /tmp/disk/dst2`
$ cargo run cp --reflink=always /tmp/disk/src2 /tmp/disk/dst2
    Finished dev [unoptimized + debuginfo] target(s) in 0.26s
     Running `target/debug/coreutils cp --reflink=always /tmp/disk/src2 /tmp/disk/dst2`
cp: failed to clone "/tmp/disk/src2" from "/tmp/disk/dst2": Invalid argument (os error 22)
$ cat /tmp/disk/dst2
[lots of 'a']
$
$ # With truncate(true)
$ cargo run cp --reflink=always /tmp/disk/src1 /tmp/disk/dst3
    Finished dev [unoptimized + debuginfo] target(s) in 7.98s
     Running `target/debug/coreutils cp --reflink=always /tmp/disk/src1 /tmp/disk/dst3`
$ cargo run cp --reflink=always /tmp/disk/src2 /tmp/disk/dst3
    Finished dev [unoptimized + debuginfo] target(s) in 0.27s
     Running `target/debug/coreutils cp --reflink=always /tmp/disk/src2 /tmp/disk/dst3`
$ cat /tmp/disk/dst3
success
```
2022-08-04 08:50:19 +02:00
Niyaz Nigmatullin
147f83f68a tests/cp: add test for cp-parents.sh 2022-07-17 23:04:09 +03:00