Skip to content

Commit

Permalink
dd: add count_bytes, skip_bytes and seek_bytes flags
Browse files Browse the repository at this point in the history
dd now accepts the count_bytes and skip_bytes input flag and the
seek_bytes output flag, to more easily allow processing portions of a
file.

* src/dd.c (scanargs): Compute skip_records and skip_bytes when
'skip_bytes' iflag is used. Compute max_records and max_bytes when
'count_bytes' iflag is used. Compute seek_records and seek_bytes
when 'seek_bytes' oflag is used.
(skip_via_lseek): Use new 'bytes' parameter and handle potential
'records' equals to zero. Update the bytes parameter when called with
'fdesc' equal to STDOUT_FILENO. Update the header comments.
(dd_copy): Skip accordingly to skip_records AND skip_bytes. Count
accordingly to max_records AND max_bytes. Seek on output accordingly
to seek_records AND seek_bytes.
* NEWS (New features): Mention it.
* doc/coreutils.texi (dd invocation): Detail new flags and behaviors.
* tests/dd/bytes: New file. Tests for these new flags.
* tests/Makefile.am (TESTS): Add it.
  • Loading branch information
jeremy-compostella authored and pixelb committed Feb 12, 2012
1 parent da2b32e commit 140eca1
Show file tree
Hide file tree
Showing 5 changed files with 253 additions and 64 deletions.
5 changes: 5 additions & 0 deletions NEWS
Expand Up @@ -2,6 +2,11 @@ GNU coreutils NEWS -*- outline -*-

* Noteworthy changes in release ?.? (????-??-??) [?]

** New features

dd now accepts the count_bytes, skip_bytes iflags and the count_bytes
oflag, to more easily allow processing portions of a file.

** Bug fixes

mv now lets you move a symlink onto a same-inode destination file that
Expand Down
50 changes: 40 additions & 10 deletions doc/coreutils.texi
Expand Up @@ -8057,18 +8057,24 @@ When converting variable-length records to fixed-length ones
(@option{conv=block}) or the reverse (@option{conv=unblock}),
use @var{bytes} as the fixed record length.

@item skip=@var{blocks}
@item skip=@var{n}
@opindex skip
Skip @var{blocks} @samp{ibs}-byte blocks in the input file before copying.
Skip @var{n} @samp{ibs}-byte blocks in the input file before copying.
If @samp{iflag=skip_bytes} is specified, @var{n} is interpreted
as a byte count rather than a block count.

@item seek=@var{blocks}
@item seek=@var{n}
@opindex seek
Skip @var{blocks} @samp{obs}-byte blocks in the output file before copying.
Skip @var{n} @samp{obs}-byte blocks in the output file before copying.
if @samp{oflag=seek_bytes} is specified, @var{n} is interpreted
as a byte count rather than a block count.

@item count=@var{blocks}
@item count=@var{n}
@opindex count
Copy @var{blocks} @samp{ibs}-byte blocks from the input file, instead
Copy @var{n} @samp{ibs}-byte blocks from the input file, instead
of everything until the end of the file.
if @samp{iflag=count_bytes} is specified, @var{n} is interpreted
as a byte count rather than a block count.

@item status=noxfer
@opindex status
Expand Down Expand Up @@ -8321,6 +8327,27 @@ When that happens, continue calling @code{read} to fill the remainder
of the block.
This flag can be used only with @code{iflag}.

@item count_bytes
@opindex count_bytes
Interpret the @samp{count=} operand as a byte count,
rather than a block count, which allows specifying
a length that is not a multiple of the I/O block size.
This flag can be used only with @code{iflag}.

@item skip_bytes
@opindex skip_bytes
Interpret the @samp{skip=} operand as a byte count,
rather than a block count, which allows specifying
an offset that is not a multiple of the I/O block size.
This flag can be used only with @code{iflag}.

@item seek_bytes
@opindex seek_bytes
Interpret the @samp{seek=} operand as a byte count,
rather than a block count, which allows specifying
an offset that is not a multiple of the I/O block size.
This flag can be used only with @code{oflag}.

@end table

These flags are not supported on all systems, and @samp{dd} rejects
Expand All @@ -8343,10 +8370,13 @@ should not be too large---values larger than a few megabytes
are generally wasteful or (as in the gigabyte..exabyte case) downright
counterproductive or error-inducing.

Use different @command{dd} invocations to use different block sizes for
skipping and I/O@. For example, the following shell commands copy data
in 512 KiB blocks between a disk and a tape, but do not save or restore a
4 KiB label at the start of the disk:
To process data that is at an offset or size that is not a
multiple of the I/O@ block size, you can use the @samp{skip_bytes},
@samp{seek_bytes} and @samp{count_bytes} flags. Alternatively
the traditional method of separate @command{dd} invocations can be used.
For example, the following shell commands copy data
in 512 KiB blocks between a disk and a tape, but do not save
or restore a 4 KiB label at the start of the disk:

@example
disk=/dev/rdsk/c0t1d0s2
Expand Down

0 comments on commit 140eca1

Please sign in to comment.