Merge pull request #3938 from tertsdiepraam/dd-positional-args

`dd`: parse operands manually via positional args
This commit is contained in:
Sylvestre Ledru 2022-09-21 22:39:13 +02:00 committed by GitHub
commit 5a746a778f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 1012 additions and 1636 deletions

122
src/uu/dd/dd.md Normal file
View file

@ -0,0 +1,122 @@
<!-- spell-checker:ignore convs iseek oseek -->
# dd
## About
Copy, and optionally convert, a file system resource
## After Help
OPERANDS:
bs=BYTES read and write up to BYTES bytes at a time (default: 512);
overwrites ibs and obs.
cbs=BYTES the 'conversion block size' in bytes. Applies to
the conv=block, and conv=unblock operations.
conv=CONVS a comma-separated list of conversion options or
(for legacy reasons) file flags.
count=N stop reading input after N ibs-sized read operations rather
than proceeding until EOF. See iflag=count_bytes if stopping
after N bytes is preferred
ibs=N the size of buffer used for reads (default: 512)
if=FILE the file used for input. When not specified, stdin is used instead
iflag=FLAGS a comma-separated list of input flags which specify how the input
source is treated. FLAGS may be any of the input-flags or
general-flags specified below.
skip=N (or iseek=N) skip N ibs-sized records into input before beginning
copy/convert operations. See iflag=seek_bytes if seeking N bytes
is preferred.
obs=N the size of buffer used for writes (default: 512)
of=FILE the file used for output. When not specified, stdout is used
instead
oflag=FLAGS comma separated list of output flags which specify how the output
source is treated. FLAGS may be any of the output flags or
general flags specified below
seek=N (or oseek=N) seeks N obs-sized records into output before
beginning copy/convert operations. See oflag=seek_bytes if
seeking N bytes is preferred
status=LEVEL controls whether volume and performance stats are written to
stderr.
When unspecified, dd will print stats upon completion. An example is below.
6+0 records in
16+0 records out
8192 bytes (8.2 kB, 8.0 KiB) copied, 0.00057009 s, 14.4 MB/s
The first two lines are the 'volume' stats and the final line is
the 'performance' stats.
The volume stats indicate the number of complete and partial
ibs-sized reads, or obs-sized writes that took place during the
copy. The format of the volume stats is
<complete>+<partial>. If records have been truncated (see
conv=block), the volume stats will contain the number of
truncated records.
Possible LEVEL values are:
progress: Print periodic performance stats as the copy
proceeds.
noxfer: Print final volume stats, but not performance stats.
none: Do not print any stats.
Printing performance stats is also triggered by the INFO signal
(where supported), or the USR1 signal. Setting the
POSIXLY_CORRECT environment variable to any value (including an
empty value) will cause the USR1 signal to be ignored.
CONVERSION OPTIONS:
ascii convert from EBCDIC to ASCII. This is the inverse of the 'ebcdic'
option. Implies conv=unblock.
ebcdic convert from ASCII to EBCDIC. This is the inverse of the 'ascii'
option. Implies conv=block.
ibm convert from ASCII to EBCDIC, applying the conventions for '[', ']'
and '~' specified in POSIX. Implies conv=block.
ucase convert from lower-case to upper-case
lcase converts from upper-case to lower-case.
block for each newline less than the size indicated by cbs=BYTES, remove
the newline and pad with spaces up to cbs. Lines longer than cbs are
truncated.
unblock for each block of input of the size indicated by cbs=BYTES, remove
right-trailing spaces and replace with a newline character.
sparse attempts to seek the output when an obs-sized block consists of only
zeros.
swab swaps each adjacent pair of bytes. If an odd number of bytes is
present, the final byte is omitted.
sync pad each ibs-sided block with zeros. If 'block' or 'unblock' is
specified, pad with spaces instead.
excl the output file must be created. Fail if the output file is already
present.
nocreat the output file will not be created. Fail if the output file in not
already present.
notrunc the output file will not be truncated. If this option is not
present, output will be truncated when opened.
noerror all read errors will be ignored. If this option is not present, dd
will only ignore Error::Interrupted.
fdatasync data will be written before finishing.
fsync data and metadata will be written before finishing.
INPUT FLAGS:
count_bytes a value to count=N will be interpreted as bytes.
skip_bytes a value to skip=N will be interpreted as bytes.
fullblock wait for ibs bytes from each read. zero-length reads are still
considered EOF.
OUTPUT FLAGS:
append open file in append mode. Consider setting conv=notrunc as well.
seek_bytes a value to seek=N will be interpreted as bytes.
GENERAL FLAGS:
direct use direct I/O for data.
directory fail unless the given input (if used as an iflag) or output (if used
as an oflag) is a directory.
dsync use synchronized I/O for data.
sync use synchronized I/O for data and metadata.
nonblock use non-blocking I/O.
noatime do not update access time.
nocache request that OS drop cache.
noctty do not assign a controlling tty.
nofollow do not follow system links.

View file

@ -17,20 +17,20 @@ type Cbs = usize;
/// certain order. The variants of this enumeration give the different
/// ways of combining those three operations.
#[derive(Debug, PartialEq)]
pub(crate) enum ConversionMode<'a> {
ConvertOnly(&'a ConversionTable),
pub(crate) enum ConversionMode {
ConvertOnly(&'static ConversionTable),
BlockOnly(Cbs, bool),
UnblockOnly(Cbs),
BlockThenConvert(&'a ConversionTable, Cbs, bool),
ConvertThenBlock(&'a ConversionTable, Cbs, bool),
UnblockThenConvert(&'a ConversionTable, Cbs),
ConvertThenUnblock(&'a ConversionTable, Cbs),
BlockThenConvert(&'static ConversionTable, Cbs, bool),
ConvertThenBlock(&'static ConversionTable, Cbs, bool),
UnblockThenConvert(&'static ConversionTable, Cbs),
ConvertThenUnblock(&'static ConversionTable, Cbs),
}
/// Stores all Conv Flags that apply to the input
#[derive(Debug, Default, PartialEq)]
pub(crate) struct IConvFlags {
pub mode: Option<ConversionMode<'static>>,
pub mode: Option<ConversionMode>,
pub swab: bool,
pub sync: Option<u8>,
pub noerror: bool,
@ -88,30 +88,6 @@ pub struct OFlags {
pub seek_bytes: bool,
}
/// The value of count=N
/// Defaults to Reads(N)
/// if iflag=count_bytes
/// then becomes Bytes(N)
#[derive(Debug, PartialEq, Eq)]
pub enum CountType {
Reads(u64),
Bytes(u64),
}
pub mod options {
pub const INFILE: &str = "if";
pub const OUTFILE: &str = "of";
pub const IBS: &str = "ibs";
pub const OBS: &str = "obs";
pub const BS: &str = "bs";
pub const CBS: &str = "cbs";
pub const COUNT: &str = "count";
pub const SKIP: &str = "skip";
pub const SEEK: &str = "seek";
pub const ISEEK: &str = "iseek";
pub const OSEEK: &str = "oseek";
pub const STATUS: &str = "status";
pub const CONV: &str = "conv";
pub const IFLAG: &str = "iflag";
pub const OFLAG: &str = "oflag";
pub const OPERANDS: &str = "operands";
}

View file

@ -5,15 +5,15 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
// spell-checker:ignore fname, tname, fpath, specfile, testfile, unspec, ifile, ofile, outfile, fullblock, urand, fileio, atoe, atoibm, behaviour, bmax, bremain, cflags, creat, ctable, ctty, datastructures, doesnt, etoa, fileout, fname, gnudd, iconvflags, iseek, nocache, noctty, noerror, nofollow, nolinks, nonblock, oconvflags, oseek, outfile, parseargs, rlen, rmax, rremain, rsofar, rstat, sigusr, wlen, wstat seekable canonicalized Canonicalized icflags ocflags ifname ofname
// spell-checker:ignore fname, tname, fpath, specfile, testfile, unspec, ifile, ofile, outfile, fullblock, urand, fileio, atoe, atoibm, behaviour, bmax, bremain, cflags, creat, ctable, ctty, datastructures, doesnt, etoa, fileout, fname, gnudd, iconvflags, iseek, nocache, noctty, noerror, nofollow, nolinks, nonblock, oconvflags, oseek, outfile, parseargs, rlen, rmax, rremain, rsofar, rstat, sigusr, wlen, wstat seekable oconv canonicalized
mod datastructures;
use datastructures::*;
mod parseargs;
use parseargs::Parser;
mod conversion_tables;
use conversion_tables::*;
mod progress;
use progress::{gen_prog_updater, ProgUpdate, ReadStat, StatusLevel, WriteStat};
@ -33,44 +33,75 @@ use std::sync::mpsc;
use std::thread;
use std::time;
use clap::{crate_version, Arg, ArgMatches, Command};
use clap::{crate_version, Arg, Command};
use gcd::Gcd;
use uucore::display::Quotable;
use uucore::error::{FromIo, UResult};
use uucore::help_section;
use uucore::show_error;
const ABOUT: &str = "copy, and optionally convert, a file system resource";
const ABOUT: &str = help_section!("about", "dd.md");
const AFTER_HELP: &str = help_section!("after help", "dd.md");
const BUF_INIT_BYTE: u8 = 0xDD;
struct Input<R: Read> {
src: R,
/// Final settings after parsing
#[derive(Default)]
struct Settings {
infile: Option<String>,
outfile: Option<String>,
ibs: usize,
print_level: Option<StatusLevel>,
count: Option<CountType>,
cflags: IConvFlags,
obs: usize,
skip: u64,
seek: u64,
count: Option<Num>,
iconv: IConvFlags,
iflags: IFlags,
oconv: OConvFlags,
oflags: OFlags,
status: Option<StatusLevel>,
}
impl Input<io::Stdin> {
fn new(
ibs: usize,
print_level: Option<StatusLevel>,
count: Option<CountType>,
cflags: IConvFlags,
iflags: IFlags,
skip_amount: u64,
) -> UResult<Self> {
let mut i = Self {
/// A number in blocks or bytes
///
/// Some values (seek, skip, iseek, oseek) can have values either in blocks or in bytes.
/// We need to remember this because the size of the blocks (ibs) is only known after parsing
/// all the arguments.
#[derive(Clone, Copy, Debug, PartialEq)]
enum Num {
Blocks(u64),
Bytes(u64),
}
impl Num {
fn force_bytes_if(self, force: bool) -> Self {
match self {
Self::Blocks(n) if force => Self::Bytes(n),
count => count,
}
}
fn to_bytes(self, block_size: u64) -> u64 {
match self {
Self::Blocks(n) => n * block_size,
Self::Bytes(n) => n,
}
}
}
struct Input<'a, R: Read> {
src: R,
settings: &'a Settings,
}
impl<'a> Input<'a, io::Stdin> {
fn new(settings: &'a Settings) -> UResult<Self> {
let mut input = Self {
src: io::stdin(),
ibs,
print_level,
count,
cflags,
iflags,
settings,
};
if skip_amount > 0 {
if let Err(e) = i.read_skip(skip_amount) {
if settings.skip > 0 {
if let Err(e) = input.read_skip(settings.skip) {
if let io::ErrorKind::UnexpectedEof = e.kind() {
show_error!("'standard input': cannot skip to specified offset");
} else {
@ -80,7 +111,7 @@ impl Input<io::Stdin> {
}
}
Ok(i)
Ok(input)
}
}
@ -120,53 +151,38 @@ fn make_linux_iflags(iflags: &IFlags) -> Option<libc::c_int> {
}
}
impl Input<File> {
fn new(
ibs: usize,
print_level: Option<StatusLevel>,
count: Option<CountType>,
cflags: IConvFlags,
iflags: IFlags,
fname: &str,
skip_amount: u64,
) -> UResult<Self> {
impl<'a> Input<'a, File> {
fn new(filename: &Path, settings: &'a Settings) -> UResult<Self> {
let mut src = {
let mut opts = OpenOptions::new();
opts.read(true);
#[cfg(any(target_os = "linux", target_os = "android"))]
if let Some(libc_flags) = make_linux_iflags(&iflags) {
if let Some(libc_flags) = make_linux_iflags(&settings.iflags) {
opts.custom_flags(libc_flags);
}
opts.open(fname)
.map_err_context(|| format!("failed to open {}", fname.quote()))?
opts.open(filename)
.map_err_context(|| format!("failed to open {}", filename.quote()))?
};
if skip_amount > 0 {
src.seek(io::SeekFrom::Start(skip_amount))
if settings.skip > 0 {
src.seek(io::SeekFrom::Start(settings.skip))
.map_err_context(|| "failed to seek in input file".to_string())?;
}
Ok(Self {
src,
ibs,
print_level,
count,
cflags,
iflags,
})
Ok(Self { src, settings })
}
}
impl<R: Read> Read for Input<R> {
impl<'a, R: Read> Read for Input<'a, R> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let mut base_idx = 0;
let target_len = buf.len();
loop {
match self.src.read(&mut buf[base_idx..]) {
Ok(0) => return Ok(base_idx),
Ok(rlen) if self.iflags.fullblock => {
Ok(rlen) if self.settings.iflags.fullblock => {
base_idx += rlen;
if base_idx >= target_len {
@ -175,14 +191,14 @@ impl<R: Read> Read for Input<R> {
}
Ok(len) => return Ok(len),
Err(e) if e.kind() == io::ErrorKind::Interrupted => continue,
Err(_) if self.cflags.noerror => return Ok(base_idx),
Err(_) if self.settings.iconv.noerror => return Ok(base_idx),
Err(e) => return Err(e),
}
}
}
}
impl<R: Read> Input<R> {
impl<'a, R: Read> Input<'a, R> {
/// Fills a given buffer.
/// Reads in increments of 'self.ibs'.
/// The start of each ibs-sized read follows the previous one.
@ -191,9 +207,9 @@ impl<R: Read> Input<R> {
let mut reads_partial = 0;
let mut bytes_total = 0;
for chunk in buf.chunks_mut(self.ibs) {
for chunk in buf.chunks_mut(self.settings.ibs) {
match self.read(chunk)? {
rlen if rlen == self.ibs => {
rlen if rlen == self.settings.ibs => {
bytes_total += rlen;
reads_complete += 1;
}
@ -223,7 +239,7 @@ impl<R: Read> Input<R> {
let mut base_idx = 0;
while base_idx < buf.len() {
let next_blk = cmp::min(base_idx + self.ibs, buf.len());
let next_blk = cmp::min(base_idx + self.settings.ibs, buf.len());
let target_len = next_blk - base_idx;
match self.read(&mut buf[base_idx..next_blk])? {
@ -238,7 +254,7 @@ impl<R: Read> Input<R> {
}
}
base_idx += self.ibs;
base_idx += self.settings.ibs;
}
buf.truncate(base_idx);
@ -265,42 +281,30 @@ impl<R: Read> Input<R> {
}
trait OutputTrait: Sized + Write {
fn new(
obs: usize,
oflags: OFlags,
cflags: OConvFlags,
seek_amount: u64,
fname: &str,
) -> UResult<Self>;
fn fsync(&mut self) -> io::Result<()>;
fn fdatasync(&mut self) -> io::Result<()>;
}
struct Output<W: Write> {
struct Output<'a, W: Write> {
dst: W,
obs: usize,
cflags: OConvFlags,
settings: &'a Settings,
}
impl OutputTrait for Output<io::Stdout> {
fn new(
obs: usize,
_oflags: OFlags,
cflags: OConvFlags,
seek_amount: u64,
_fname: &str,
) -> UResult<Self> {
impl<'a> Output<'a, io::Stdout> {
fn new(settings: &'a Settings) -> UResult<Self> {
let mut dst = io::stdout();
// stdout is not seekable, so we just write null bytes.
if seek_amount > 0 {
io::copy(&mut io::repeat(0u8).take(seek_amount), &mut dst)
if settings.seek > 0 {
io::copy(&mut io::repeat(0u8).take(settings.seek as u64), &mut dst)
.map_err_context(|| String::from("write error"))?;
}
Ok(Self { dst, obs, cflags })
Ok(Self { dst, settings })
}
}
impl<'a> OutputTrait for Output<'a, io::Stdout> {
fn fsync(&mut self) -> io::Result<()> {
self.dst.flush()
}
@ -310,7 +314,7 @@ impl OutputTrait for Output<io::Stdout> {
}
}
impl<W: Write> Output<W>
impl<'a, W: Write> Output<'a, W>
where
Self: OutputTrait,
{
@ -319,9 +323,9 @@ where
let mut writes_partial = 0;
let mut bytes_total = 0;
for chunk in buf.chunks(self.obs) {
for chunk in buf.chunks(self.settings.obs) {
let wlen = self.write(chunk)?;
if wlen < self.obs {
if wlen < self.settings.obs {
writes_partial += 1;
} else {
writes_complete += 1;
@ -338,9 +342,9 @@ where
/// Flush the output to disk, if configured to do so.
fn sync(&mut self) -> std::io::Result<()> {
if self.cflags.fsync {
if self.settings.oconv.fsync {
self.fsync()
} else if self.cflags.fdatasync {
} else if self.settings.oconv.fdatasync {
self.fdatasync()
} else {
// Intentionally do nothing in this case.
@ -381,7 +385,7 @@ where
//
// This is an educated guess about a good buffer size based on
// the input and output block sizes.
let bsize = calc_bsize(i.ibs, self.obs);
let bsize = calc_bsize(i.settings.ibs, self.settings.obs);
// Start a thread that reports transfer progress.
//
@ -394,7 +398,7 @@ where
// to the receives `rx`, and the receiver prints the transfer
// information.
let (prog_tx, rx) = mpsc::channel();
let output_thread = thread::spawn(gen_prog_updater(rx, i.print_level));
let output_thread = thread::spawn(gen_prog_updater(rx, i.settings.status));
let mut progress_as_secs = 0;
// Create a common buffer with a capacity of the block size.
@ -407,13 +411,14 @@ where
// blocks to this output. Read/write statistics are updated on
// each iteration and cumulative statistics are reported to
// the progress reporting thread.
while below_count_limit(&i.count, &rstat, &wstat) {
while below_count_limit(&i.settings.count, &rstat, &wstat) {
// Read a block from the input then write the block to the output.
//
// As an optimization, make an educated guess about the
// best buffer size for reading based on the number of
// blocks already read and the number of blocks remaining.
let loop_bsize = calc_loop_bsize(&i.count, &rstat, &wstat, i.ibs, bsize);
let loop_bsize =
calc_loop_bsize(&i.settings.count, &rstat, &wstat, i.settings.ibs, bsize);
let rstat_update = read_helper(&mut i, &mut buf, loop_bsize)?;
if rstat_update.is_empty() {
break;
@ -489,14 +494,8 @@ fn make_linux_oflags(oflags: &OFlags) -> Option<libc::c_int> {
}
}
impl OutputTrait for Output<File> {
fn new(
obs: usize,
oflags: OFlags,
cflags: OConvFlags,
seek_amount: u64,
fname: &str,
) -> UResult<Self> {
impl<'a> Output<'a, File> {
fn new(filename: &Path, settings: &'a Settings) -> UResult<Self> {
fn open_dst(path: &Path, cflags: &OConvFlags, oflags: &OFlags) -> Result<File, io::Error> {
let mut opts = OpenOptions::new();
opts.write(true)
@ -512,8 +511,8 @@ impl OutputTrait for Output<File> {
opts.open(path)
}
let mut dst = open_dst(Path::new(&fname), &cflags, &oflags)
.map_err_context(|| format!("failed to open {}", fname.quote()))?;
let mut dst = open_dst(filename, &settings.oconv, &settings.oflags)
.map_err_context(|| format!("failed to open {}", filename.quote()))?;
// Seek to the index in the output file, truncating if requested.
//
@ -524,15 +523,17 @@ impl OutputTrait for Output<File> {
// the behavior of GNU `dd` when given the command-line
// argument `of=/dev/null`.
if !cflags.notrunc {
dst.set_len(seek_amount).ok();
if !settings.oconv.notrunc {
dst.set_len(settings.seek).ok();
}
dst.seek(io::SeekFrom::Start(seek_amount))
dst.seek(io::SeekFrom::Start(settings.seek))
.map_err_context(|| "failed to seek in output file".to_string())?;
Ok(Self { dst, obs, cflags })
Ok(Self { dst, settings })
}
}
impl<'a> OutputTrait for Output<'a, File> {
fn fsync(&mut self) -> io::Result<()> {
self.dst.flush()?;
self.dst.sync_all()
@ -544,19 +545,19 @@ impl OutputTrait for Output<File> {
}
}
impl Seek for Output<File> {
impl<'a> Seek for Output<'a, File> {
fn seek(&mut self, pos: io::SeekFrom) -> io::Result<u64> {
self.dst.seek(pos)
}
}
impl Write for Output<File> {
impl<'a> Write for Output<'a, File> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
fn is_sparse(buf: &[u8]) -> bool {
buf.iter().all(|&e| e == 0u8)
}
// -----------------------------
if self.cflags.sparse && is_sparse(buf) {
if self.settings.oconv.sparse && is_sparse(buf) {
let seek_amt: i64 = buf
.len()
.try_into()
@ -573,7 +574,7 @@ impl Write for Output<File> {
}
}
impl Write for Output<io::Stdout> {
impl<'a> Write for Output<'a, io::Stdout> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.dst.write(buf)
}
@ -600,7 +601,7 @@ fn read_helper<R: Read>(
// Resize the buffer to the bsize. Any garbage data in the buffer is overwritten or truncated, so there is no need to fill with BUF_INIT_BYTE first.
buf.resize(bsize, BUF_INIT_BYTE);
let mut rstat = match i.cflags.sync {
let mut rstat = match i.settings.iconv.sync {
Some(ch) => i.fill_blocks(buf, ch)?,
_ => i.fill_consecutive(buf)?,
};
@ -610,11 +611,11 @@ fn read_helper<R: Read>(
}
// Perform any conv=x[,x...] options
if i.cflags.swab {
if i.settings.iconv.swab {
perform_swab(buf);
}
match i.cflags.mode {
match i.settings.iconv.mode {
Some(ref mode) => {
*buf = conv_block_unblock_helper(buf.clone(), mode, &mut rstat);
Ok(rstat)
@ -638,19 +639,19 @@ fn calc_bsize(ibs: usize, obs: usize) -> usize {
// Calculate the buffer size appropriate for this loop iteration, respecting
// a count=N if present.
fn calc_loop_bsize(
count: &Option<CountType>,
count: &Option<Num>,
rstat: &ReadStat,
wstat: &WriteStat,
ibs: usize,
ideal_bsize: usize,
) -> usize {
match count {
Some(CountType::Reads(rmax)) => {
Some(Num::Blocks(rmax)) => {
let rsofar = rstat.reads_complete + rstat.reads_partial;
let rremain = rmax - rsofar;
cmp::min(ideal_bsize as u64, rremain * ibs as u64) as usize
}
Some(CountType::Bytes(bmax)) => {
Some(Num::Bytes(bmax)) => {
let bmax: u128 = (*bmax).try_into().unwrap();
let bremain: u128 = bmax - wstat.bytes_total;
cmp::min(ideal_bsize as u128, bremain as u128) as usize
@ -661,13 +662,13 @@ fn calc_loop_bsize(
// Decide if the current progress is below a count=N limit or return
// true if no such limit is set.
fn below_count_limit(count: &Option<CountType>, rstat: &ReadStat, wstat: &WriteStat) -> bool {
fn below_count_limit(count: &Option<Num>, rstat: &ReadStat, wstat: &WriteStat) -> bool {
match count {
Some(CountType::Reads(n)) => {
Some(Num::Blocks(n)) => {
let n = *n;
rstat.reads_complete + rstat.reads_partial <= n
}
Some(CountType::Bytes(n)) => {
Some(Num::Bytes(n)) => {
let n = (*n).try_into().unwrap();
wstat.bytes_total <= n
}
@ -675,14 +676,6 @@ fn below_count_limit(count: &Option<CountType>, rstat: &ReadStat, wstat: &WriteS
}
}
fn append_dashes_if_not_present(mut acc: Vec<String>, mut s: String) -> Vec<String> {
if !s.starts_with("--") && !s.starts_with('-') {
s.insert_str(0, "--");
}
acc.push(s);
acc
}
/// Canonicalized file name of `/dev/stdout`.
///
/// For example, if this process were invoked from the command line as
@ -728,82 +721,48 @@ fn is_stdout_redirected_to_seekable_file() -> bool {
#[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let dashed_args = args
.collect_ignore()
.into_iter()
.fold(Vec::new(), append_dashes_if_not_present);
let args = args.collect_ignore();
let matches = uu_app()
//.after_help(TODO: Add note about multiplier strings here.)
.try_get_matches_from(dashed_args)?;
let matches = uu_app().try_get_matches_from(args)?;
// Parse options for reading from input.
let ibs = parseargs::parse_ibs(&matches)?;
let print_level = parseargs::parse_status_level(&matches)?;
let icflags = parseargs::parse_conv_flag_input(&matches)?;
let iflags = parseargs::parse_iflags(&matches)?;
let skip = parseargs::parse_seek_skip_amt(&ibs, iflags.skip_bytes, &matches, options::SKIP)?;
let iseek = parseargs::parse_seek_skip_amt(&ibs, iflags.skip_bytes, &matches, options::ISEEK)?;
let count = parseargs::parse_count(&iflags, &matches)?;
// The --skip and --iseek flags are additive. On a file, they seek.
let skip_amount = skip.unwrap_or(0) + iseek.unwrap_or(0);
let settings: Settings = Parser::new().parse(
&matches
.get_many::<String>(options::OPERANDS)
.unwrap_or_default()
.map(|s| s.as_ref())
.collect::<Vec<_>>()[..],
)?;
// Parse options for writing to the output.
let obs = parseargs::parse_obs(&matches)?;
let ocflags = parseargs::parse_conv_flag_output(&matches)?;
let oflags = parseargs::parse_oflags(&matches)?;
let seek = parseargs::parse_seek_skip_amt(&obs, oflags.seek_bytes, &matches, options::SEEK)?;
let oseek = parseargs::parse_seek_skip_amt(&obs, oflags.seek_bytes, &matches, options::OSEEK)?;
// The --seek and --oseek flags are additive.
let seek_amount = seek.unwrap_or(0) + oseek.unwrap_or(0);
match (
matches.contains_id(options::INFILE),
matches.contains_id(options::OUTFILE),
) {
(true, true) => {
let ifname = matches.value_of(options::INFILE).unwrap();
let ofname = matches.value_of(options::OUTFILE).unwrap();
let i = Input::<File>::new(
ibs,
print_level,
count,
icflags,
iflags,
ifname,
skip_amount,
)?;
let o = Output::<File>::new(obs, oflags, ocflags, seek_amount, ofname)?;
match (&settings.infile, &settings.outfile) {
(Some(infile), Some(outfile)) => {
let i = Input::<File>::new(Path::new(&infile), &settings)?;
let o = Output::<File>::new(Path::new(&outfile), &settings)?;
o.dd_out(i).map_err_context(|| "IO error".to_string())
}
(false, true) => {
let ofname = matches.value_of(options::OUTFILE).unwrap();
let i = Input::<io::Stdin>::new(ibs, print_level, count, icflags, iflags, skip_amount)?;
let o = Output::<File>::new(obs, oflags, ocflags, seek_amount, ofname)?;
(None, Some(outfile)) => {
let i = Input::<io::Stdin>::new(&settings)?;
let o = Output::<File>::new(Path::new(&outfile), &settings)?;
o.dd_out(i).map_err_context(|| "IO error".to_string())
}
(true, false) => {
let fname = matches.value_of(options::INFILE).unwrap();
let i =
Input::<File>::new(ibs, print_level, count, icflags, iflags, fname, skip_amount)?;
(Some(infile), None) => {
let i = Input::<File>::new(Path::new(&infile), &settings)?;
if is_stdout_redirected_to_seekable_file() {
let ofname = stdout_canonicalized().into_string().unwrap();
let o = Output::<File>::new(obs, oflags, ocflags, seek_amount, &ofname)?;
let filename = stdout_canonicalized();
let o = Output::<File>::new(Path::new(&filename), &settings)?;
o.dd_out(i).map_err_context(|| "IO error".to_string())
} else {
let o = Output::<io::Stdout>::new(obs, oflags, ocflags, seek_amount, "-")?;
let o = Output::<io::Stdout>::new(&settings)?;
o.dd_out(i).map_err_context(|| "IO error".to_string())
}
}
(false, false) => {
let i = Input::<io::Stdin>::new(ibs, print_level, count, icflags, iflags, skip_amount)?;
(None, None) => {
let i = Input::<io::Stdin>::new(&settings)?;
if is_stdout_redirected_to_seekable_file() {
let ofname = stdout_canonicalized().into_string().unwrap();
let o = Output::<File>::new(obs, oflags, ocflags, seek_amount, &ofname)?;
let filename = stdout_canonicalized();
let o = Output::<File>::new(Path::new(&filename), &settings)?;
o.dd_out(i).map_err_context(|| "IO error".to_string())
} else {
let o = Output::<io::Stdout>::new(obs, oflags, ocflags, seek_amount, "-")?;
let o = Output::<io::Stdout>::new(&settings)?;
o.dd_out(i).map_err_context(|| "IO error".to_string())
}
}
@ -814,246 +773,22 @@ pub fn uu_app<'a>() -> Command<'a> {
Command::new(uucore::util_name())
.version(crate_version!())
.about(ABOUT)
.after_help(AFTER_HELP)
.infer_long_args(true)
.arg(
Arg::new(options::INFILE)
.long(options::INFILE)
.overrides_with(options::INFILE)
.takes_value(true)
.value_hint(clap::ValueHint::FilePath)
.require_equals(true)
.value_name("FILE")
.help("(alternatively if=FILE) specifies the file used for input. When not specified, stdin is used instead")
)
.arg(
Arg::new(options::OUTFILE)
.long(options::OUTFILE)
.overrides_with(options::OUTFILE)
.takes_value(true)
.value_hint(clap::ValueHint::FilePath)
.require_equals(true)
.value_name("FILE")
.help("(alternatively of=FILE) specifies the file used for output. When not specified, stdout is used instead")
)
.arg(
Arg::new(options::IBS)
.long(options::IBS)
.overrides_with(options::IBS)
.takes_value(true)
.require_equals(true)
.value_name("N")
.help("(alternatively ibs=N) specifies the size of buffer used for reads (default: 512). Multiplier strings permitted.")
)
.arg(
Arg::new(options::OBS)
.long(options::OBS)
.overrides_with(options::OBS)
.takes_value(true)
.require_equals(true)
.value_name("N")
.help("(alternatively obs=N) specifies the size of buffer used for writes (default: 512). Multiplier strings permitted.")
)
.arg(
Arg::new(options::BS)
.long(options::BS)
.overrides_with(options::BS)
.takes_value(true)
.require_equals(true)
.value_name("N")
.help("(alternatively bs=N) specifies ibs=N and obs=N (default: 512). If ibs or obs are also specified, bs=N takes precedence. Multiplier strings permitted.")
)
.arg(
Arg::new(options::CBS)
.long(options::CBS)
.overrides_with(options::CBS)
.takes_value(true)
.require_equals(true)
.value_name("N")
.help("(alternatively cbs=BYTES) specifies the 'conversion block size' in bytes. Applies to the conv=block, and conv=unblock operations. Multiplier strings permitted.")
)
.arg(
Arg::new(options::SKIP)
.long(options::SKIP)
.overrides_with(options::SKIP)
.takes_value(true)
.require_equals(true)
.value_name("N")
.help("(alternatively skip=N) causes N ibs-sized records of input to be skipped before beginning copy/convert operations. See iflag=count_bytes if skipping N bytes is preferred. Multiplier strings permitted.")
)
.arg(
Arg::new(options::SEEK)
.long(options::SEEK)
.overrides_with(options::SEEK)
.takes_value(true)
.require_equals(true)
.value_name("N")
.help("(alternatively seek=N) seeks N obs-sized records into output before beginning copy/convert operations. See oflag=seek_bytes if seeking N bytes is preferred. Multiplier strings permitted.")
)
.arg(
Arg::new(options::ISEEK)
.long(options::ISEEK)
.overrides_with(options::ISEEK)
.takes_value(true)
.require_equals(true)
.value_name("N")
.help("(alternatively iseek=N) seeks N obs-sized records into input before beginning copy/convert operations. See iflag=seek_bytes if seeking N bytes is preferred. Multiplier strings permitted.")
)
.arg(
Arg::new(options::OSEEK)
.long(options::OSEEK)
.overrides_with(options::OSEEK)
.takes_value(true)
.require_equals(true)
.value_name("N")
.help("(alternatively oseek=N) seeks N obs-sized records into output before beginning copy/convert operations. See oflag=seek_bytes if seeking N bytes is preferred. Multiplier strings permitted.")
)
.arg(
Arg::new(options::COUNT)
.long(options::COUNT)
.overrides_with(options::COUNT)
.takes_value(true)
.require_equals(true)
.value_name("N")
.help("(alternatively count=N) stop reading input after N ibs-sized read operations rather than proceeding until EOF. See iflag=count_bytes if stopping after N bytes is preferred. Multiplier strings permitted.")
)
.arg(
Arg::new(options::STATUS)
.long(options::STATUS)
.overrides_with(options::STATUS)
.takes_value(true)
.require_equals(true)
.value_name("LEVEL")
.help("(alternatively status=LEVEL) controls whether volume and performance stats are written to stderr.
When unspecified, dd will print stats upon completion. An example is below.
\t6+0 records in
\t16+0 records out
\t8192 bytes (8.2 kB, 8.0 KiB) copied, 0.00057009 s, 14.4 MB/s
The first two lines are the 'volume' stats and the final line is the 'performance' stats.
The volume stats indicate the number of complete and partial ibs-sized reads, or obs-sized writes that took place during the copy. The format of the volume stats is <complete>+<partial>. If records have been truncated (see conv=block), the volume stats will contain the number of truncated records.
Permissible LEVEL values are:
\t progress: Print periodic performance stats as the copy proceeds.
\t noxfer: Print final volume stats, but not performance stats.
\t none: Do not print any stats.
Printing performance stats is also triggered by the INFO signal (where supported), or the USR1 signal. Setting the POSIXLY_CORRECT environment variable to any value (including an empty value) will cause the USR1 signal to be ignored.
")
)
.arg(
Arg::new(options::CONV)
.long(options::CONV)
.takes_value(true)
.multiple_occurrences(true)
.use_value_delimiter(true)
.require_value_delimiter(true)
.multiple_values(true)
.require_equals(true)
.value_name("CONV")
.help("(alternatively conv=CONV[,CONV]) specifies a comma-separated list of conversion options or (for legacy reasons) file flags. Conversion options and file flags may be intermixed.
Conversion options:
\t One of {ascii, ebcdic, ibm} will perform an encoding conversion.
\t\t 'ascii' converts from EBCDIC to ASCII. This is the inverse of the 'ebcdic' option.
\t\t 'ebcdic' converts from ASCII to EBCDIC. This is the inverse of the 'ascii' option.
\t\t 'ibm' converts from ASCII to EBCDIC, applying the conventions for '[', ']' and '~' specified in POSIX.
\t One of {ucase, lcase} will perform a case conversion. Works in conjunction with option {ascii, ebcdic, ibm} to infer input encoding. If no other conversion option is specified, input is assumed to be ascii.
\t\t 'ucase' converts from lower-case to upper-case
\t\t 'lcase' converts from upper-case to lower-case.
\t One of {block, unblock}. Convert between lines terminated by newline characters, and fixed-width lines padded by spaces (without any newlines). Both the 'block' and 'unblock' options require cbs=BYTES be specified.
\t\t 'block' for each newline less than the size indicated by cbs=BYTES, remove the newline and pad with spaces up to cbs. Lines longer than cbs are truncated.
\t\t 'unblock' for each block of input of the size indicated by cbs=BYTES, remove right-trailing spaces and replace with a newline character.
\t 'sparse' attempts to seek the output when an obs-sized block consists of only zeros.
\t 'swab' swaps each adjacent pair of bytes. If an odd number of bytes is present, the final byte is omitted.
\t 'sync' pad each ibs-sided block with zeros. If 'block' or 'unblock' is specified, pad with spaces instead.
Conversion Flags:
\t One of {excl, nocreat}
\t\t 'excl' the output file must be created. Fail if the output file is already present.
\t\t 'nocreat' the output file will not be created. Fail if the output file in not already present.
\t 'notrunc' the output file will not be truncated. If this option is not present, output will be truncated when opened.
\t 'noerror' all read errors will be ignored. If this option is not present, dd will only ignore Error::Interrupted.
\t 'fdatasync' data will be written before finishing.
\t 'fsync' data and metadata will be written before finishing.
")
)
.arg(
Arg::new(options::IFLAG)
.long(options::IFLAG)
.takes_value(true)
.multiple_occurrences(true)
.use_value_delimiter(true)
.require_value_delimiter(true)
.multiple_values(true)
.require_equals(true)
.value_name("FLAG")
.help("(alternatively iflag=FLAG[,FLAG]) a comma separated list of input flags which specify how the input source is treated. FLAG may be any of the input-flags or general-flags specified below.
Input-Flags
\t 'count_bytes' a value to count=N will be interpreted as bytes.
\t 'skip_bytes' a value to skip=N will be interpreted as bytes.
\t 'fullblock' wait for ibs bytes from each read. zero-length reads are still considered EOF.
General-Flags
\t 'direct' use direct I/O for data.
\t 'directory' fail unless the given input (if used as an iflag) or output (if used as an oflag) is a directory.
\t 'dsync' use synchronized I/O for data.
\t 'sync' use synchronized I/O for data and metadata.
\t 'nonblock' use non-blocking I/O.
\t 'noatime' do not update access time.
\t 'nocache' request that OS drop cache.
\t 'noctty' do not assign a controlling tty.
\t 'nofollow' do not follow system links.
")
)
.arg(
Arg::new(options::OFLAG)
.long(options::OFLAG)
.takes_value(true)
.multiple_occurrences(true)
.use_value_delimiter(true)
.require_value_delimiter(true)
.multiple_values(true)
.require_equals(true)
.value_name("FLAG")
.help("(alternatively oflag=FLAG[,FLAG]) a comma separated list of output flags which specify how the output source is treated. FLAG may be any of the output-flags or general-flags specified below.
Output-Flags
\t 'append' open file in append mode. Consider setting conv=notrunc as well.
\t 'seek_bytes' a value to seek=N will be interpreted as bytes.
General-Flags
\t 'direct' use direct I/O for data.
\t 'directory' fail unless the given input (if used as an iflag) or output (if used as an oflag) is a directory.
\t 'dsync' use synchronized I/O for data.
\t 'sync' use synchronized I/O for data and metadata.
\t 'nonblock' use non-blocking I/O.
\t 'noatime' do not update access time.
\t 'nocache' request that OS drop cache.
\t 'noctty' do not assign a controlling tty.
\t 'nofollow' do not follow system links.
")
)
.arg(Arg::new(options::OPERANDS).multiple_values(true))
}
#[cfg(test)]
mod tests {
use crate::datastructures::{IConvFlags, IFlags, OConvFlags, OFlags};
use crate::{calc_bsize, Input, Output, OutputTrait};
use crate::datastructures::{IConvFlags, IFlags};
use crate::{calc_bsize, Input, Output, Parser, Settings};
use std::cmp;
use std::fs;
use std::fs::File;
use std::io;
use std::io::{BufReader, Read};
use std::path::Path;
struct LazyReader<R: Read> {
src: R,
@ -1137,40 +872,36 @@ mod tests {
}
#[test]
#[should_panic]
fn test_nocreat_causes_failure_when_ofile_doesnt_exist() {
let obs = 1;
let oflags = OFlags::default();
let ocflags = OConvFlags {
nocreat: true,
..Default::default()
};
let seek_amount = 0;
let ofname = "not-a-real.file";
let _ = Output::<File>::new(obs, oflags, ocflags, seek_amount, ofname).unwrap();
let args = &["conv=nocreat", "of=not-a-real.file"];
let settings = Parser::new().parse(args).unwrap();
assert!(
Output::<File>::new(Path::new(settings.outfile.as_ref().unwrap()), &settings).is_err()
);
}
#[test]
fn test_deadbeef_16_delayed() {
let settings = Settings {
ibs: 16,
obs: 32,
count: None,
iconv: IConvFlags {
sync: Some(0),
..Default::default()
},
..Default::default()
};
let input = Input {
src: LazyReader {
src: File::open("./test-resources/deadbeef-16.test").unwrap(),
},
ibs: 16,
print_level: None,
count: None,
cflags: IConvFlags {
sync: Some(0),
..IConvFlags::default()
},
iflags: IFlags::default(),
settings: &settings,
};
let output = Output {
dst: File::create("./test-resources/FAILED-deadbeef-16-delayed.test").unwrap(),
obs: 32,
cflags: OConvFlags::default(),
settings: &settings,
};
output.dd_out(input).unwrap();
@ -1198,26 +929,28 @@ mod tests {
#[test]
fn test_random_73k_test_lazy_fullblock() {
let settings = Settings {
ibs: 521,
obs: 1031,
count: None,
iflags: IFlags {
fullblock: true,
..IFlags::default()
},
..Default::default()
};
let input = Input {
src: LazyReader {
src: File::open("./test-resources/random-5828891cb1230748e146f34223bbd3b5.test")
.unwrap(),
},
ibs: 521,
print_level: None,
count: None,
cflags: IConvFlags::default(),
iflags: IFlags {
fullblock: true,
..IFlags::default()
},
settings: &settings,
};
let output = Output {
dst: File::create("./test-resources/FAILED-random_73k_test_lazy_fullblock.test")
.unwrap(),
obs: 1031,
cflags: OConvFlags::default(),
settings: &settings,
};
output.dd_out(input).unwrap();

File diff suppressed because it is too large Load diff

View file

@ -1,7 +1,11 @@
// spell-checker:ignore fname, tname, fpath, specfile, testfile, unspec, ifile, ofile, outfile, fullblock, urand, fileio, atoe, atoibm, behaviour, bmax, bremain, btotal, cflags, creat, ctable, ctty, datastructures, doesnt, etoa, fileout, fname, gnudd, iconvflags, iseek, nocache, noctty, noerror, nofollow, nolinks, nonblock, oconvflags, oseek, outfile, parseargs, rlen, rmax, rposition, rremain, rsofar, rstat, sigusr, sigval, wlen, wstat
// spell-checker:ignore fname, tname, fpath, specfile, testfile, unspec, ifile, ofile, outfile, fullblock, urand, fileio, atoe, atoibm, behaviour, bmax, bremain, btotal, cflags, creat, ctable, ctty, datastructures, doesnt, etoa, fileout, fname, gnudd, iconvflags, iseek, nocache, noctty, noerror, nofollow, nolinks, nonblock, oconvflags, oseek, outfile, parseargs, rlen, rmax, rposition, rremain, rsofar, rstat, sigusr, sigval, wlen, wstat, oconv
use super::*;
use crate::conversion_tables::{
ASCII_TO_EBCDIC_UCASE_TO_LCASE, ASCII_TO_IBM, EBCDIC_TO_ASCII_LCASE_TO_UCASE,
};
use crate::parseargs::Parser;
use crate::StatusLevel;
#[cfg(not(any(target_os = "linux", target_os = "android")))]
@ -20,18 +24,22 @@ fn unimplemented_flags_should_error_non_linux() {
"noctty",
"nofollow",
] {
let args = vec![
String::from("dd"),
format!("--iflag={}", flag),
format!("--oflag={}", flag),
];
let matches = uu_app().try_get_matches_from(args).unwrap();
let args = vec![format!("iflag={}", flag)];
if parse_iflags(&matches).is_ok() {
if Parser::new()
.parse(&args.iter().map(AsRef::as_ref).collect::<Vec<_>>()[..])
.is_ok()
{
succeeded.push(format!("iflag={}", flag));
}
if parse_oflags(&matches).is_ok() {
succeeded.push(format!("oflag={}", flag));
let args = vec![format!("oflag={}", flag)];
if Parser::new()
.parse(&args.iter().map(AsRef::as_ref).collect::<Vec<_>>()[..])
.is_ok()
{
succeeded.push(format!("iflag={}", flag));
}
}
@ -48,18 +56,22 @@ fn unimplemented_flags_should_error() {
// The following flags are not implemented
for flag in ["cio", "nocache", "nolinks", "text", "binary"] {
let args = vec![
String::from("dd"),
format!("--iflag={}", flag),
format!("--oflag={}", flag),
];
let matches = uu_app().try_get_matches_from(args).unwrap();
let args = vec![format!("iflag={}", flag)];
if parse_iflags(&matches).is_ok() {
if Parser::new()
.parse(&args.iter().map(AsRef::as_ref).collect::<Vec<_>>()[..])
.is_ok()
{
succeeded.push(format!("iflag={}", flag));
}
if parse_oflags(&matches).is_ok() {
succeeded.push(format!("oflag={}", flag));
let args = vec![format!("oflag={}", flag)];
if Parser::new()
.parse(&args.iter().map(AsRef::as_ref).collect::<Vec<_>>()[..])
.is_ok()
{
succeeded.push(format!("iflag={}", flag));
}
}
@ -72,103 +84,62 @@ fn unimplemented_flags_should_error() {
#[test]
fn test_status_level_absent() {
let args = vec![
String::from("dd"),
String::from("--if=foo.file"),
String::from("--of=bar.file"),
];
let args = &["if=foo.file", "of=bar.file"];
let matches = uu_app().try_get_matches_from(args).unwrap();
let st = parse_status_level(&matches).unwrap();
assert_eq!(st, None);
assert_eq!(Parser::new().parse(args).unwrap().status, None)
}
#[test]
fn test_status_level_none() {
let args = vec![
String::from("dd"),
String::from("--status=none"),
String::from("--if=foo.file"),
String::from("--of=bar.file"),
];
let args = &["status=none", "if=foo.file", "of=bar.file"];
let matches = uu_app().try_get_matches_from(args).unwrap();
let st = parse_status_level(&matches).unwrap().unwrap();
assert_eq!(st, StatusLevel::None);
assert_eq!(
Parser::new().parse(args).unwrap().status,
Some(StatusLevel::None)
)
}
#[test]
fn test_all_top_level_args_no_leading_dashes() {
let args = vec![
String::from("dd"),
String::from("if=foo.file"),
String::from("of=bar.file"),
String::from("ibs=10"),
String::from("obs=10"),
String::from("cbs=1"),
String::from("bs=100"),
String::from("count=2"),
String::from("skip=2"),
String::from("seek=2"),
String::from("iseek=2"),
String::from("oseek=2"),
String::from("status=progress"),
String::from("conv=ascii,ucase"),
String::from("iflag=count_bytes,skip_bytes"),
String::from("oflag=append,seek_bytes"),
let args = &[
"if=foo.file",
"of=bar.file",
"ibs=10",
"obs=10",
"cbs=1",
"bs=100",
"count=2",
"skip=2",
"seek=2",
"iseek=2",
"oseek=2",
"status=progress",
"conv=ascii,ucase",
"iflag=count_bytes,skip_bytes",
"oflag=append,seek_bytes",
];
let args = args
.into_iter()
.fold(Vec::new(), append_dashes_if_not_present);
let matches = uu_app().try_get_matches_from(args).unwrap();
let settings = Parser::new().parse(args).unwrap();
assert_eq!(100, parse_ibs(&matches).unwrap());
assert_eq!(100, parse_obs(&matches).unwrap());
assert_eq!(1, parse_cbs(&matches).unwrap().unwrap());
assert_eq!(
CountType::Bytes(2),
parse_count(
&IFlags {
count_bytes: true,
..IFlags::default()
},
&matches
)
.unwrap()
.unwrap()
);
assert_eq!(
200,
parse_seek_skip_amt(&100, IFlags::default().skip_bytes, &matches, options::SKIP)
.unwrap()
.unwrap()
);
assert_eq!(
200,
parse_seek_skip_amt(&100, OFlags::default().seek_bytes, &matches, options::SEEK)
.unwrap()
.unwrap()
);
assert_eq!(
200,
parse_seek_skip_amt(&100, IFlags::default().skip_bytes, &matches, options::ISEEK)
.unwrap()
.unwrap()
);
assert_eq!(
200,
parse_seek_skip_amt(&100, OFlags::default().seek_bytes, &matches, options::OSEEK)
.unwrap()
.unwrap()
);
assert_eq!(
StatusLevel::Progress,
parse_status_level(&matches).unwrap().unwrap()
);
// ibs=10 and obs=10 are overwritten by bs=100
assert_eq!(settings.ibs, 100);
assert_eq!(settings.obs, 100);
// count=2 iflag=count_bytes
assert_eq!(settings.count, Some(Num::Bytes(2)));
// seek=2 oflag=seek_bytes
assert_eq!(settings.seek, 2);
// skip=2 iflag=skip_bytes
assert_eq!(settings.skip, 2);
// status=progress
assert_eq!(settings.status, Some(StatusLevel::Progress));
// conv=ascii,ucase
assert_eq!(
settings.iconv,
IConvFlags {
// ascii implies unblock
mode: Some(ConversionMode::ConvertThenUnblock(
@ -177,500 +148,294 @@ fn test_all_top_level_args_no_leading_dashes() {
)),
..IConvFlags::default()
},
parse_conv_flag_input(&matches).unwrap()
);
assert_eq!(
OConvFlags::default(),
parse_conv_flag_output(&matches).unwrap()
);
// no conv flags apply to output
assert_eq!(settings.oconv, OConvFlags::default(),);
// iconv=count_bytes,skip_bytes
assert_eq!(
settings.iflags,
IFlags {
count_bytes: true,
skip_bytes: true,
..IFlags::default()
},
parse_iflags(&matches).unwrap()
);
// oconv=append,seek_bytes
assert_eq!(
settings.oflags,
OFlags {
append: true,
seek_bytes: true,
..OFlags::default()
},
parse_oflags(&matches).unwrap()
);
}
#[test]
fn test_all_top_level_args_with_leading_dashes() {
let args = vec![
String::from("dd"),
String::from("--if=foo.file"),
String::from("--of=bar.file"),
String::from("--ibs=10"),
String::from("--obs=10"),
String::from("--cbs=1"),
String::from("--bs=100"),
String::from("--count=2"),
String::from("--skip=2"),
String::from("--seek=2"),
String::from("--iseek=2"),
String::from("--oseek=2"),
String::from("--status=progress"),
String::from("--conv=ascii,ucase"),
String::from("--iflag=count_bytes,skip_bytes"),
String::from("--oflag=append,seek_bytes"),
];
let args = args
.into_iter()
.fold(Vec::new(), append_dashes_if_not_present);
let matches = uu_app().try_get_matches_from(args).unwrap();
assert_eq!(100, parse_ibs(&matches).unwrap());
assert_eq!(100, parse_obs(&matches).unwrap());
assert_eq!(1, parse_cbs(&matches).unwrap().unwrap());
assert_eq!(
CountType::Bytes(2),
parse_count(
&IFlags {
count_bytes: true,
..IFlags::default()
},
&matches
)
.unwrap()
.unwrap()
);
assert_eq!(
200,
parse_seek_skip_amt(&100, IFlags::default().skip_bytes, &matches, options::SKIP)
.unwrap()
.unwrap()
);
assert_eq!(
200,
parse_seek_skip_amt(&100, OFlags::default().seek_bytes, &matches, options::SEEK)
.unwrap()
.unwrap()
);
assert_eq!(
200,
parse_seek_skip_amt(&100, IFlags::default().skip_bytes, &matches, options::ISEEK)
.unwrap()
.unwrap()
);
assert_eq!(
200,
parse_seek_skip_amt(&100, OFlags::default().seek_bytes, &matches, options::OSEEK)
.unwrap()
.unwrap()
);
assert_eq!(
StatusLevel::Progress,
parse_status_level(&matches).unwrap().unwrap()
);
assert_eq!(
IConvFlags {
// ascii implies unblock
mode: Some(ConversionMode::ConvertThenUnblock(
&EBCDIC_TO_ASCII_LCASE_TO_UCASE,
1
)),
..IConvFlags::default()
},
parse_conv_flag_input(&matches).unwrap()
);
assert_eq!(
OConvFlags::default(),
parse_conv_flag_output(&matches).unwrap()
);
assert_eq!(
IFlags {
count_bytes: true,
skip_bytes: true,
..IFlags::default()
},
parse_iflags(&matches).unwrap()
);
assert_eq!(
OFlags {
append: true,
seek_bytes: true,
..OFlags::default()
},
parse_oflags(&matches).unwrap()
);
}
#[test]
fn test_status_level_progress() {
let args = vec![
String::from("dd"),
String::from("--if=foo.file"),
String::from("--of=bar.file"),
String::from("--status=progress"),
];
let args = &["if=foo.file", "of=bar.file", "status=progress"];
let matches = uu_app().try_get_matches_from(args).unwrap();
let st = parse_status_level(&matches).unwrap().unwrap();
let settings = Parser::new().parse(args).unwrap();
assert_eq!(st, StatusLevel::Progress);
assert_eq!(settings.status, Some(StatusLevel::Progress));
}
#[test]
fn test_status_level_noxfer() {
let args = vec![
String::from("dd"),
String::from("--if=foo.file"),
String::from("--status=noxfer"),
String::from("--of=bar.file"),
];
let args = &["if=foo.file", "status=noxfer", "of=bar.file"];
let matches = uu_app().try_get_matches_from(args).unwrap();
let st = parse_status_level(&matches).unwrap().unwrap();
let settings = Parser::new().parse(args).unwrap();
assert_eq!(st, StatusLevel::Noxfer);
assert_eq!(settings.status, Some(StatusLevel::Noxfer));
}
#[test]
fn test_multiple_flags_options() {
let args = vec![
String::from("dd"),
String::from("--iflag=fullblock,count_bytes"),
String::from("--iflag=skip_bytes"),
String::from("--oflag=append"),
String::from("--oflag=seek_bytes"),
String::from("--conv=ascii,ucase"),
String::from("--conv=unblock"),
let args = &[
"iflag=fullblock,count_bytes",
"iflag=skip_bytes",
"oflag=append",
"oflag=seek_bytes",
"conv=ascii,ucase",
"conv=unblock",
"cbs=512",
];
let matches = uu_app().try_get_matches_from(args).unwrap();
let settings = Parser::new().parse(args).unwrap();
// iflag
let iflags = parse_flag_list::<Flag>(options::IFLAG, &matches).unwrap();
assert_eq!(
vec![Flag::FullBlock, Flag::CountBytes, Flag::SkipBytes],
iflags
settings.iflags,
IFlags {
fullblock: true,
count_bytes: true,
skip_bytes: true,
..Default::default()
}
);
// oflag
let oflags = parse_flag_list::<Flag>(options::OFLAG, &matches).unwrap();
assert_eq!(vec![Flag::Append, Flag::SeekBytes], oflags);
assert_eq!(
settings.oflags,
OFlags {
append: true,
seek_bytes: true,
..Default::default()
}
);
// conv
let conv = parse_flag_list::<ConvFlag>(options::CONV, &matches).unwrap();
assert_eq!(
vec![ConvFlag::FmtEtoA, ConvFlag::UCase, ConvFlag::Unblock],
conv
settings.iconv,
IConvFlags {
mode: Some(ConversionMode::ConvertThenUnblock(
&EBCDIC_TO_ASCII_LCASE_TO_UCASE,
512
)),
..Default::default()
}
);
}
#[test]
fn test_override_multiple_options() {
let args = vec![
String::from("dd"),
String::from("--if=foo.file"),
String::from("--if=correct.file"),
String::from("--of=bar.file"),
String::from("--of=correct.file"),
String::from("--ibs=256"),
String::from("--ibs=1024"),
String::from("--obs=256"),
String::from("--obs=1024"),
String::from("--cbs=1"),
String::from("--cbs=2"),
String::from("--skip=0"),
String::from("--skip=2"),
String::from("--seek=0"),
String::from("--seek=2"),
String::from("--iseek=0"),
String::from("--iseek=2"),
String::from("--oseek=0"),
String::from("--oseek=2"),
String::from("--status=none"),
String::from("--status=noxfer"),
String::from("--count=512"),
String::from("--count=1024"),
let args = &[
"if=foo.file",
"if=correct.file",
"of=bar.file",
"of=correct.file",
"ibs=256",
"ibs=1024",
"obs=256",
"obs=1024",
"cbs=1",
"cbs=2",
"skip=0",
"skip=2",
"seek=0",
"seek=2",
"iseek=0",
"iseek=2",
"oseek=0",
"oseek=2",
"status=none",
"status=noxfer",
"count=512",
"count=1024",
"iflag=count_bytes",
];
let matches = uu_app().try_get_matches_from(args).unwrap();
let settings = Parser::new().parse(args).unwrap();
// if
assert_eq!("correct.file", matches.value_of(options::INFILE).unwrap());
// of
assert_eq!("correct.file", matches.value_of(options::OUTFILE).unwrap());
// ibs
assert_eq!(1024, parse_ibs(&matches).unwrap());
// obs
assert_eq!(1024, parse_obs(&matches).unwrap());
// cbs
assert_eq!(2, parse_cbs(&matches).unwrap().unwrap());
// status
assert_eq!(
StatusLevel::Noxfer,
parse_status_level(&matches).unwrap().unwrap()
);
// skip
assert_eq!(
200,
parse_seek_skip_amt(&100, IFlags::default().skip_bytes, &matches, options::SKIP)
.unwrap()
.unwrap()
);
// seek
assert_eq!(
200,
parse_seek_skip_amt(&100, OFlags::default().seek_bytes, &matches, options::SEEK)
.unwrap()
.unwrap()
);
// iseek
assert_eq!(
200,
parse_seek_skip_amt(&100, IFlags::default().skip_bytes, &matches, options::ISEEK)
.unwrap()
.unwrap()
);
// oseek
assert_eq!(
200,
parse_seek_skip_amt(&100, OFlags::default().seek_bytes, &matches, options::OSEEK)
.unwrap()
.unwrap()
);
// count
assert_eq!(
CountType::Bytes(1024),
parse_count(
&IFlags {
count_bytes: true,
..IFlags::default()
},
&matches
)
.unwrap()
.unwrap()
);
assert_eq!(settings.infile, Some("correct.file".into()));
assert_eq!(settings.outfile, Some("correct.file".into()));
assert_eq!(settings.ibs, 1024);
assert_eq!(settings.obs, 1024);
assert_eq!(settings.status, Some(StatusLevel::Noxfer));
assert_eq!(settings.skip, 2048);
assert_eq!(settings.seek, 2048);
assert_eq!(settings.count, Some(Num::Bytes(1024)));
}
// ----- IConvFlags/Output -----
// // ----- IConvFlags/Output -----
#[test]
#[should_panic]
fn icf_ctable_error() {
let args = vec![String::from("dd"), String::from("--conv=ascii,ebcdic,ibm")];
let matches = uu_app().try_get_matches_from(args).unwrap();
let _ = parse_conv_flag_input(&matches).unwrap();
let args = &["conv=ascii,ebcdic,ibm"];
assert!(Parser::new().parse(args).is_err());
}
#[test]
#[should_panic]
fn icf_case_error() {
let args = vec![String::from("dd"), String::from("--conv=ucase,lcase")];
let matches = uu_app().try_get_matches_from(args).unwrap();
let _ = parse_conv_flag_input(&matches).unwrap();
let args = &["conv=ucase,lcase"];
assert!(Parser::new().parse(args).is_err());
}
#[test]
#[should_panic]
fn icf_block_error() {
let args = vec![String::from("dd"), String::from("--conv=block,unblock")];
let matches = uu_app().try_get_matches_from(args).unwrap();
let _ = parse_conv_flag_input(&matches).unwrap();
let args = &["conv=block,unblock"];
assert!(Parser::new().parse(args).is_err());
}
#[test]
#[should_panic]
fn icf_creat_error() {
let args = vec![String::from("dd"), String::from("--conv=excl,nocreat")];
let matches = uu_app().try_get_matches_from(args).unwrap();
let _ = parse_conv_flag_output(&matches).unwrap();
let args = &["conv=excl,nocreat"];
assert!(Parser::new().parse(args).is_err());
}
#[test]
fn parse_icf_token_ibm() {
let exp = vec![ConvFlag::FmtAtoI];
let args = &["conv=ibm"];
let settings = Parser::new().parse(args).unwrap();
let args = vec![String::from("dd"), String::from("--conv=ibm")];
let matches = uu_app().try_get_matches_from(args).unwrap();
let act = parse_flag_list::<ConvFlag>("conv", &matches).unwrap();
assert_eq!(exp.len(), act.len());
for cf in &exp {
assert!(exp.contains(cf));
}
assert_eq!(
settings.iconv,
IConvFlags {
mode: Some(ConversionMode::ConvertOnly(&ASCII_TO_IBM)),
..Default::default()
}
);
}
#[test]
fn parse_icf_tokens_elu() {
let exp = vec![ConvFlag::FmtEtoA, ConvFlag::LCase, ConvFlag::Unblock];
let args = &["conv=ebcdic,lcase"];
let settings = Parser::new().parse(args).unwrap();
let args = vec![
String::from("dd"),
String::from("--conv=ebcdic,lcase,unblock"),
];
let matches = uu_app().try_get_matches_from(args).unwrap();
let act = parse_flag_list::<ConvFlag>("conv", &matches).unwrap();
assert_eq!(exp.len(), act.len());
for cf in &exp {
assert!(exp.contains(cf));
}
assert_eq!(
settings.iconv,
IConvFlags {
mode: Some(ConversionMode::ConvertOnly(&ASCII_TO_EBCDIC_UCASE_TO_LCASE)),
..Default::default()
}
);
}
#[test]
fn parse_icf_tokens_remaining() {
let exp = vec![
ConvFlag::FmtAtoE,
ConvFlag::UCase,
ConvFlag::Block,
ConvFlag::Sparse,
ConvFlag::Swab,
ConvFlag::Sync,
ConvFlag::NoError,
ConvFlag::Excl,
ConvFlag::NoCreat,
ConvFlag::NoTrunc,
ConvFlag::NoError,
ConvFlag::FDataSync,
ConvFlag::FSync,
];
let args = vec![
String::from("dd"),
String::from("--conv=ascii,ucase,block,sparse,swab,sync,noerror,excl,nocreat,notrunc,noerror,fdatasync,fsync"),
];
let matches = uu_app().try_get_matches_from(args).unwrap();
let act = parse_flag_list::<ConvFlag>("conv", &matches).unwrap();
assert_eq!(exp.len(), act.len());
for cf in &exp {
assert!(exp.contains(cf));
}
let args = &["conv=ascii,ucase,block,sparse,swab,sync,noerror,excl,nocreat,notrunc,noerror,fdatasync,fsync"];
assert_eq!(
Parser::new().read(args),
Ok(Parser {
conv: ConvFlags {
ascii: true,
ucase: true,
block: true,
sparse: true,
swab: true,
sync: true,
noerror: true,
excl: true,
nocreat: true,
notrunc: true,
fdatasync: true,
fsync: true,
..Default::default()
},
..Default::default()
})
);
}
#[test]
fn parse_iflag_tokens() {
let exp = vec![
Flag::FullBlock,
Flag::CountBytes,
Flag::SkipBytes,
Flag::Append,
Flag::SeekBytes,
];
let args = vec![
String::from("dd"),
String::from("--iflag=fullblock,count_bytes,skip_bytes,append,seek_bytes"),
];
let matches = uu_app().try_get_matches_from(args).unwrap();
let act = parse_flag_list::<Flag>("iflag", &matches).unwrap();
assert_eq!(exp.len(), act.len());
for cf in &exp {
assert!(exp.contains(cf));
}
let args = &["iflag=fullblock,count_bytes,skip_bytes"];
assert_eq!(
Parser::new().read(args),
Ok(Parser {
iflag: IFlags {
fullblock: true,
count_bytes: true,
skip_bytes: true,
..Default::default()
},
..Default::default()
})
);
}
#[test]
fn parse_oflag_tokens() {
let exp = vec![
Flag::FullBlock,
Flag::CountBytes,
Flag::SkipBytes,
Flag::Append,
Flag::SeekBytes,
];
let args = vec![
String::from("dd"),
String::from("--oflag=fullblock,count_bytes,skip_bytes,append,seek_bytes"),
];
let matches = uu_app().try_get_matches_from(args).unwrap();
let act = parse_flag_list::<Flag>("oflag", &matches).unwrap();
assert_eq!(exp.len(), act.len());
for cf in &exp {
assert!(exp.contains(cf));
}
let args = &["oflag=append,seek_bytes"];
assert_eq!(
Parser::new().read(args),
Ok(Parser {
oflag: OFlags {
append: true,
seek_bytes: true,
..Default::default()
},
..Default::default()
})
);
}
#[cfg(any(target_os = "linux", target_os = "android"))]
#[test]
fn parse_iflag_tokens_linux() {
let exp = vec![
Flag::Direct,
Flag::Directory,
Flag::Dsync,
Flag::Sync,
Flag::NonBlock,
Flag::NoATime,
Flag::NoCtty,
Flag::NoFollow,
];
let args = vec![
String::from("dd"),
String::from("--iflag=direct,directory,dsync,sync,nonblock,noatime,noctty,nofollow"),
];
let matches = uu_app().try_get_matches_from(args).unwrap();
let act = parse_flag_list::<Flag>("iflag", &matches).unwrap();
assert_eq!(exp.len(), act.len());
for cf in &exp {
assert!(exp.contains(cf));
}
let args = &["iflag=direct,directory,dsync,sync,nonblock,noatime,noctty,nofollow"];
assert_eq!(
Parser::new().read(args),
Ok(Parser {
iflag: IFlags {
direct: true,
directory: true,
dsync: true,
sync: true,
nonblock: true,
noatime: true,
noctty: true,
nofollow: true,
..Default::default()
},
..Default::default()
})
);
}
#[cfg(any(target_os = "linux", target_os = "android"))]
#[test]
fn parse_oflag_tokens_linux() {
let exp = vec![
Flag::Direct,
Flag::Directory,
Flag::Dsync,
Flag::Sync,
Flag::NonBlock,
Flag::NoATime,
Flag::NoCtty,
Flag::NoFollow,
];
let args = vec![
String::from("dd"),
String::from("--oflag=direct,directory,dsync,sync,nonblock,noatime,noctty,nofollow"),
];
let matches = uu_app().try_get_matches_from(args).unwrap();
let act = parse_flag_list::<Flag>("oflag", &matches).unwrap();
assert_eq!(exp.len(), act.len());
for cf in &exp {
assert!(exp.contains(cf));
}
let args = &["oflag=direct,directory,dsync,sync,nonblock,noatime,noctty,nofollow"];
assert_eq!(
Parser::new().read(args),
Ok(Parser {
oflag: OFlags {
direct: true,
directory: true,
dsync: true,
sync: true,
nonblock: true,
noatime: true,
noctty: true,
nofollow: true,
..Default::default()
},
..Default::default()
})
);
}
// ----- Multiplier Strings etc. -----

View file

@ -1165,12 +1165,12 @@ fn test_bytes_iseek_bytes_iflag() {
}
#[test]
fn test_bytes_iseek_skip_additive() {
fn test_bytes_iseek_skip_not_additive() {
new_ucmd!()
.args(&["iseek=5", "skip=5", "iflag=skip_bytes", "bs=2"])
.args(&["iseek=4", "skip=4", "iflag=skip_bytes", "bs=2"])
.pipe_in("0123456789abcdefghijklm")
.succeeds()
.stdout_is("abcdefghijklm");
.stdout_is("456789abcdefghijklm");
}
#[test]
@ -1193,9 +1193,9 @@ fn test_bytes_oseek_bytes_trunc_oflag() {
}
#[test]
fn test_bytes_oseek_seek_additive() {
fn test_bytes_oseek_seek_not_additive() {
new_ucmd!()
.args(&["oseek=4", "seek=4", "oflag=seek_bytes", "bs=2"])
.args(&["oseek=8", "seek=8", "oflag=seek_bytes", "bs=2"])
.pipe_in("abcdefghijklm")
.succeeds()
.stdout_is_fixture_bytes("dd-bytes-alphabet-null.spec");
@ -1278,3 +1278,20 @@ fn test_invalid_file_arg_gnu_compatibility() {
.pipe_in("")
.succeeds();
}
#[test]
fn test_ucase_lcase() {
new_ucmd!()
.arg("conv=ucase,lcase")
.fails()
.stderr_contains("lcase")
.stderr_contains("ucase");
}
#[test]
fn test_big_multiplication() {
new_ucmd!()
.arg("ibs=10x10x10x10x10x10x10x10x10x10x10x10x10x10x10x10x10x10x10x10x10x10x10")
.fails()
.stderr_contains("invalid number");
}

View file

@ -90,16 +90,16 @@ fn test_ls_allocation_size() {
// fill empty file with zeros
scene
.ccmd("dd")
.arg("--if=/dev/zero")
.arg("--of=some-dir1/zero-file")
.arg("if=/dev/zero")
.arg("of=some-dir1/zero-file")
.arg("bs=1024")
.arg("count=4096")
.succeeds();
scene
.ccmd("dd")
.arg("--if=/dev/zero")
.arg("--of=irregular-file")
.arg("if=/dev/zero")
.arg("of=irregular-file")
.arg("bs=1")
.arg("count=777")
.succeeds();