Commit graph

66 commits

Author SHA1 Message Date
Terts Diepraam
e3aab307fe
Merge pull request #4189 from jfinkels/dd-stdin-from-file-descriptor
dd: open stdin from file descriptor when possible
2023-03-18 01:30:31 +01:00
Miles Liu
1435d3573a
dd: fix unused imports on FreeBSD 2023-03-15 18:08:32 +08:00
Jeffrey Finkelstein
9cb6b4a3c0 dd: open stdin from file descriptor when possible
Open stdin using its file descriptor so that a `dd skip=N` command in
a subshell does not consume all bytes from stdin.

For example, before this commit, multiple instances of `dd` reading
from stdin and appearing in a single command line would incorrectly
result in an empty stdin for each instance of `dd` after the first:

    $ printf "abcdef\n" | (dd bs=1 skip=3 count=0 && dd)  2> /dev/null
    # incorrectly results in no output

After this commit, the `dd skip=3` process reads three bytes from the
file descriptor referring to stdin without draining the remaining
three bytes when it terminates:

    $ printf "abcdef\n" | (dd bs=1 skip=3 count=0 && dd)  2> /dev/null
    def
2023-03-11 20:56:37 +00:00
Jeffrey Finkelstein
c52647a632 dd: add support for skipping in input FIFO
For example, `dd skip=1 if=fifo` will now work.
2023-02-18 21:24:16 +01:00
Jeffrey Finkelstein
9c3f810f97 dd: add support for seeking in output FIFOs
For example, `dd seek=1 of=fifo` will now work.
2023-02-18 21:24:16 +01:00
Daniel Hofstetter
f6b646e4e5 clippy: fix warnings introduced with Rust 1.67.0 2023-01-27 17:37:56 +01:00
Joining7943
1fadeb43b2 tests/util: Do not trim stderr in CmdResult::stderr_is. Add method stderr_trimmed_is.
Fix tests assert whitespace instead of trimming it. Disable some tests in `test_tr` because `tr`
produces too many newlines.
2023-01-22 14:56:19 +01:00
Roy Ivy III
d78e1e7399 tests: revise/standardize usage error testing (for dd, install, mktemp, rm, seq, and touch) 2023-01-13 22:14:48 -06:00
Terts Diepraam
44ea43f058 tests: remove status_code, which is the same as code_is 2023-01-01 17:25:59 +01:00
Jeffrey Finkelstein
3a5b31a30f dd: correct rendering of SI and IEC byte counts
Adjust the rendering of the concise byte counts in both SI and IEC
units to better match the behavior of GNU dd.

Before this commit,

    $ head -c 1024 /dev/zero | dd > /dev/null
    2+0 records in
    2+0 records out
    1024 bytes (1 KB, 1024 B) copied, 0.0 s, 1.0 MB/s

After this commit,

    $ head -c 1024 /dev/zero | dd > /dev/null
    2+0 records in
    2+0 records out
    1024 bytes (1.0 kB, 1.0 KiB) copied, 0.0 s, 1.0 MB/s

For comparison, GNU dd produces the following:

    $ head -c 1024 /dev/zero | dd > /dev/null
    2+0 records in
    2+0 records out
    1024 bytes (1.0 kB, 1.0 KiB) copied, 0.000332864 s, 3.1 MB/s
2022-12-03 10:14:00 -05:00
Joining7943
982fb682e9 tests: Use UChild in tests. Rename run_no_wait_child to run_no_wait and return UChild
tests/tail:
* test_stdin_redirect_file:. Test fails now when assert_alive()!
The follow test `tail -f < file` where file's content is `foo` fails with:
    Assertion failed. Expected 'tail' to be running but exited with status=exit status: 0

I also tried on the command line and can confirm that tail isn't runnning when following by
descriptor. The test is deactivated until the implementation is fixed.

* test_follow_stdin_descriptor
* test_follow_stdin_explicit_indefinitely.
* test_follow_single
* test_follow_non_utf8_bytes
* test_follow_multiple
* test_follow_name_multiple
* test_follow_invalid_pid
* test_single_big_args
* test_retry3
* test_retry4
* test_retry5
* test_retry7
* test_retry8
* test_retry9
* test_follow_descriptor_vs_rename1
* test_follow_descriptor_vs_rename2
* test_follow_name_retry_headers
* test_follow_name_remove
* test_follow_name_truncate1
* test_follow_name_truncate2
* test_follow_name_truncate3
* test_follow_name_truncate4
* test_follow_truncate_fast
* test_follow_name_move_create1
* test_follow_name_move_create2
* test_follow_name_move1
* test_follow_name_move2
* test_follow_name_move_retry1
* test_follow_name_move_retry2
* test_follow_inotify_only_regular
* test_fifo
* test_illegal_seek

tests/cat:
* test_dev_full
* test_dev_full_show_all
* test_dev_random
* test_fifo_symlink

tests/dd:
* test_random_73k_test_lazy_fullblock
* test_sync_delayed_reader

tests/factor:
* test_parallel

tests/rm:
* test_rm_force_prompts_order
* test_rm_descend_directory
* test_rm_prompts

tests/seq:
* the helper run method

tests/sort:
* test_sigpipe_panic

tests/tee:
* the helper run_tee method

tests/tty:
* test_tty module

tests/yes:
* the helper run method
2022-12-02 08:06:45 +01:00
Jeffrey Finkelstein
9632db4f86 dd: correctly set file size when conv=sparse
Before this commit, if `sparsefile` were a regular file of non-zero
size whose contents are all null bytes, then

    dd if=sparsefile of=outfile conv=notrunc

would have resulted in `outfile` having zero size as reported by
`stat`. After this commit, `outfile` will have the same size as
`sparsefile` (even if the contents are represented sparsely by the
filesystem).
2022-11-29 22:53:18 +01:00
Jeffrey Finkelstein
903ad1656d dd: move tests of slow reader to test_dd.rs
Move some tests that simulate a slow reader from `dd.rs` to
`tests/by-util/test_dd.rs`, and employ a FIFO and `sleep()` to
simulate the slow reader instead of a custom struct that implements
`Read`. This change restricts the type of `Input`s the
`Output::dd_out()` function can accept, facilitating a future change
to make `Input` an enum.
2022-11-19 09:27:23 -05:00
Jeffrey Finkelstein
701550d76b dd: allow B as a suffix for count, seek, and skip
Allow uppercase "B" on its own as a unit specifier for the `count`,
`seek`, and `skip` arguments to `dd`.

For example,

    $ printf "abcdef" | dd count=3B status=none
    abc
2022-11-18 20:27:42 -05:00
Terts Diepraam
e377e4f046 dd: custom positional argument parsing 2022-09-21 18:41:12 +02:00
Terts Diepraam
9177cb7b24 all: add tests for usage error exit code 2022-09-10 20:59:42 +02:00
Owen Anderson
08d3da8f6c
Fix a TODO by making conv_block_unblock_helper consume the input. (#3787)
* Fix a TODO by making `conv_block_unblock_helper` consume the input.
2022-08-28 10:07:04 +02:00
Przemysław Fuchs
3acbd1c048
dd: Error message of invalid args is matched with GNU (#3831) 2022-08-17 11:40:42 +02:00
Niyaz Nigmatullin
161fc6b642 test/dd: add ignore_stdin_write_error, because count=0 and dd doesn't
read anything
2022-08-08 22:44:59 +03:00
Jeffrey Finkelstein
a375644c50 dd: only print concise byte counts if large enough
Update `dd` to only print a concise form of the number of bytes with
an SI prefix (like "1 MB" or "2 GB") if the number is at least
1000. Similarly, only print the concise form with an IEC prefix (like
"1 MiB" or "2 GiB") if the number is at least 1024. For example,

    $ head -c 999 /dev/zero | dd > /dev/null
    1+1 records in
    1+1 records out
    999 bytes copied, 0.0 s, 999.0 KB/s

    $ head -c 1000 /dev/zero | dd > /dev/null
    1+1 records in
    1+1 records out
    1000 bytes (1000 B) copied, 0.0 s, 1000.0 KB/s

    $ head -c 1024 /dev/zero | dd > /dev/null
    2+0 records in
    2+0 records out
    1024 bytes (1 KB, 1024 B) copied, 0.0 s, 1.0 MB/s
2022-06-11 23:16:16 -04:00
Justin Tracey
2a0d58d060 get android builds to compile and pass tests 2022-04-20 08:44:49 +02:00
Terts Diepraam
18369dc0be all: use array intoiterator 2022-04-05 10:39:31 +02:00
chordtoll
72ce815d87 Add correct set of tests, fix typo in flags to test, write test to confirm additive flags 2022-03-18 20:45:04 +01:00
chordtoll
b77b3cba55 dd: implement iseek + oseek flags
These are the first half of changes needed to pass the dd/bytes.sh tests:
- Add iseek and oseek options (additive with skip and seek options)
- Implement tests for the new flags, matching those from dd/bytes.sh
2022-03-18 20:45:04 +01:00
Sylvestre Ledru
66e9956595
Merge pull request #3156 from jfinkels/dd-cbs-blocks
dd: pad partial record with spaces in some cases
2022-03-03 22:35:58 +01:00
Omer Tuchfeld
0ce22f3a08 Improve coverage / error messages from parse_size PR
https://github.com/uutils/coreutils/pull/3084 (2a333ab391) had some
missing coverage and was merged before I had a chance to fix it.

This PR adds some coverage / improved error messages that were missing
from that previous PR.
2022-02-22 22:09:45 +01:00
Jeffrey Finkelstein
9f367b72e6 dd: pad partial record with spaces in some cases
If `conv=block,sync` command-line arguments are given and there is at
least one partial record read from the input (for example, if the
length of the input is not divisible by the value of the `ibs`
argument), then output an extra block of `cbs` spaces.

For example, no extra spaces are printed in this example because the
input is of length 10, a multiple of `ibs`:

    $ printf "012\nabcde\n" \
    > | dd ibs=5 cbs=5 conv=block,sync status=noxfer \
    > && echo $
    012  abcde$
    2+0 records in
    0+1 records out

But in this example, 5 extra spaces are printed because the length of
the input is not a multiple of `ibs`:

    $ printf "012\nabcdefg\n" \
    > | dd ibs=5 cbs=5 conv=block,sync status=noxfer \
    > && echo $
    012  abcde     $
    2+1 records in
    0+1 records out
    1 truncated record

The number of spaces printed is the size of the conversion block,
given by `cbs`.
2022-02-21 13:00:11 -05:00
Jeffrey Finkelstein
6900638ac6 dd: don't error when outfile is /dev/null
Prevent `dd` from terminating with an error when given the
command-line argument `of=/dev/null`. This commit allows the call to
`File::set_len()` to result in an error without causing the process to
terminate prematurely.
2022-02-19 10:29:09 -05:00
Jeffrey Finkelstein
766c85fb5e dd: correct order and phrasing of truncated record
Place the "truncated records" line below the "records out" line in the
status report produced by `dd` and properly handle the singularization
of the word "record" in the case of 1 truncated record. This matches
the behavior of GNU `dd`.

For example

    $ printf "ab" | dd cbs=1 conv=block status=noxfer > /dev/null
    0+1 records in
    0+1 records out
    1 truncated record

    $ printf "ab\ncd\n" | dd cbs=1 conv=block status=noxfer > /dev/null
    0+1 records in
    0+1 records out
    2 truncated records
2022-02-18 23:49:33 -05:00
Jeffrey Finkelstein
89f428b44f dd: remove spurious zero multiplier warning
Fix a bug in which `dd` was inappropriately showing a warning about a
"0x" multiplier when there was no "x" character in the argument.
2022-02-17 19:06:05 -05:00
Jeffrey Finkelstein
ba1ce7179b dd: move unit tests into dd.rs and test_dd.rs
Clean up unit tests in the `dd` crate to make them easier to
manage. This commit does a few things.

* move test cases that test the complete functionality of the `dd`
  program from the `dd_unit_tests` module up to the
  `tests/by-util/test_dd.rs` module so that they can take advantage of
  the testing framework and common testing tools provided by uutils,
* move test cases that test internal functions of the `dd`
  implementation into the `tests` module within `dd.rs` so that they
  live closer to the code they are testing,
* replace test cases defined by macros with test cases defined by
  plain old functions to make the test cases easier to read at a
  glance.
2022-02-15 21:50:48 -05:00
Jeffrey Finkelstein
3ada6af19d dd: correctly account for partial record written
Correct the accounting for partial records written by `dd` to the
output file. After this commit, if fewer than `obs` bytes are written,
then that is counted as a partial record. For example,

    $ printf 'abc' | dd bs=2 status=noxfer > /dev/null
    1+1 records in
    1+1 records out

That is, one complete record and one partial record are read from the
input, one complete record and one partial record are written to the
output. Previously, `dd` reported two complete records and zero
partial records written to the output in this case.
2022-02-12 22:38:25 -05:00
Sylvestre Ledru
daaae90113
Merge branch 'main' into dd-skip-beyond-file 2022-02-08 20:46:07 +01:00
Sylvestre Ledru
9c813be5f1
Merge pull request #3076 from jfinkels/dd-seek-truncate
dd: truncate to specified seek length
2022-02-08 11:16:15 +01:00
Jeffrey Finkelstein
9f8ec676c5 dd: show warning if skipping past end of input
Show a warning if the `skip=N` command-line argument would cause `dd`
to skip past the end of the input. For example:

    $ printf "abcd" | dd bs=1 skip=5 count=0 status=noxfer
    'standard input': cannot skip to specified offset
    0+0 records in
    0+0 records out
2022-02-06 20:10:42 -05:00
Jeffrey Finkelstein
fec662a623 dd: show warning when using 0x size multiplier
Show a warning when a block size includes "0x" since this is
ambiguous: the user may have meant "multiply the next number by zero"
or they may have meant "the following characters should be interpreted
as a hexadecimal number".
2022-02-06 17:13:15 -05:00
Jeffrey Finkelstein
1af709f642 dd: truncate to specified seek length
When specifying `seek=N` and *not* specifying `conv=notrunc`, truncate
the output file to `N` blocks instead of truncating it to zero before
starting to write output. For example

    $ printf "abc" > outfile
    $ printf "123" | dd bs=1 skip=1 seek=1 count=1 status=noxfer of=outfile
    1+0 records in
    1+0 records out
    $ cat outfile
    a2

Fixes #3068.
2022-02-05 16:07:44 -05:00
Jeffrey Finkelstein
3fbaa79359 dd: add support for 'b' and 'x' multipliers
Support the suffix 'b' (multiply by 512) and 'x' (multiply by an
arbitrary amount) when specifying numeric arguments to dd.
2022-02-03 21:56:13 -05:00
Sylvestre Ledru
24744ca42b
Merge pull request #2911 from jfinkels/dd-seek-stdout
dd: support seek=N when destination is stdout
2022-01-29 10:47:17 +01:00
Sylvestre Ledru
5c45911c13
Merge pull request #2915 from jfinkels/dd-status-noxfer
dd: correct behavior when status=noxfer
2022-01-25 23:19:53 +01:00
Jeffrey Finkelstein
e8df666c2e dd: support seek=N when destination is stdout
Add support for the `seek=N` argument when the destination is stdout
and not a file. Previously, the argument was ignored when writing to
stdout.
2022-01-25 12:34:13 -05:00
Jeffrey Finkelstein
80ac2619e4 dd: correct behavior when status=noxfer
Correct the behavior of `dd` with the `status=noxfer` option. Before
this commit, the status output was entirely suppressed (as happens
with `status=none`). This was incorrect behavior. After this commit,
the input/output counts are printed to stderr as expected.

For example,

    $ printf "" | dd status=noxfer
    0+0 records in
    0+0 records out

This commit also updates a unit test that was enforcing the wrong
behavior.
2022-01-23 17:36:14 -05:00
Jeffrey Finkelstein
1c8df122d7 dd: block/unblock on ebcdic/ascii conversions
Update `dd` so that the conversion `conv=ascii` implies `conv=unblock`
and, symmetrically, the conversion `conv=ebcdic` implies `conv=block`.
2022-01-23 09:57:37 -05:00
jfinkels
3f18b98c9d
dd: return UResult from uumain() function (#2792)
* dd: return UResult from uumain() function

* fixup! dd: return UResult from uumain() function
2021-12-29 15:13:52 +01:00
Roy Ivy III
f07a1749a1 fix spell-check errors 2021-11-19 17:55:02 -06:00
Sylvestre Ledru
00de952592 Fix various 'if_then_panic' clippy warnings 2021-10-10 09:57:39 +02:00
Tyler
0864234e3d Removes unused macros from windows & macos builds 2021-08-04 12:47:03 -07:00
Tyler
d1907edd65 Adds additional words to exclude list. Improves documentation in dd.rs. 2021-07-24 16:38:08 -07:00
Tyler
076ff32e85 Removes project-specific cspell files. 2021-07-23 14:53:24 -07:00
Tyler
885a875552 Addresses build errors
- Adds words to cspell exceptions
- Converts test macros to use Default trait.
- Converts parser to use Default trait.
- Adds Windows-friendly test files for block/unblock when nl is present
  in test/spec file.
2021-07-22 16:04:35 -07:00