2000-02-09 01:28:47 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
1999-10-05 21:54:54 +05:30
|
|
|
/*
|
1999-10-21 03:38:37 +05:30
|
|
|
* Mini dd implementation for busybox
|
|
|
|
*
|
2001-12-07 21:57:37 +05:30
|
|
|
* Copyright (C) 2000,2001 Matt Kraai
|
1999-10-21 03:38:37 +05:30
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
1999-10-05 21:54:54 +05:30
|
|
|
*/
|
2015-02-07 06:11:22 +05:30
|
|
|
//config:config DD
|
2018-12-28 07:50:17 +05:30
|
|
|
//config: bool "dd (7.5 kb)"
|
2015-02-07 06:11:22 +05:30
|
|
|
//config: default y
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: dd copies a file (from standard input to standard output,
|
|
|
|
//config: by default) using specific input and output blocksizes,
|
|
|
|
//config: while optionally performing conversions on it.
|
2015-02-07 06:11:22 +05:30
|
|
|
//config:
|
|
|
|
//config:config FEATURE_DD_SIGNAL_HANDLING
|
|
|
|
//config: bool "Enable signal handling for status reporting"
|
|
|
|
//config: default y
|
|
|
|
//config: depends on DD
|
|
|
|
//config: help
|
2017-08-02 17:42:48 +05:30
|
|
|
//config: Sending a SIGUSR1 signal to a running 'dd' process makes it
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: print to standard error the number of records read and written
|
|
|
|
//config: so far, then to resume copying.
|
2015-02-07 06:11:22 +05:30
|
|
|
//config:
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: $ dd if=/dev/zero of=/dev/null &
|
|
|
|
//config: $ pid=$!; kill -USR1 $pid; sleep 1; kill $pid
|
|
|
|
//config: 10899206+0 records in
|
|
|
|
//config: 10899206+0 records out
|
2015-02-07 06:11:22 +05:30
|
|
|
//config:
|
|
|
|
//config:config FEATURE_DD_THIRD_STATUS_LINE
|
|
|
|
//config: bool "Enable the third status line upon signal"
|
|
|
|
//config: default y
|
|
|
|
//config: depends on DD && FEATURE_DD_SIGNAL_HANDLING
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: Displays a coreutils-like third status line with transferred bytes,
|
|
|
|
//config: elapsed time and speed.
|
2015-02-07 06:11:22 +05:30
|
|
|
//config:
|
|
|
|
//config:config FEATURE_DD_IBS_OBS
|
2018-10-15 15:56:15 +05:30
|
|
|
//config: bool "Enable ibs, obs, iflag, oflag and conv options"
|
2015-02-07 06:11:22 +05:30
|
|
|
//config: default y
|
|
|
|
//config: depends on DD
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: Enable support for writing a certain number of bytes in and out,
|
|
|
|
//config: at a time, and performing conversions on the data stream.
|
2015-02-07 06:11:22 +05:30
|
|
|
//config:
|
|
|
|
//config:config FEATURE_DD_STATUS
|
|
|
|
//config: bool "Enable status display options"
|
|
|
|
//config: default y
|
|
|
|
//config: depends on DD
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: Enable support for status=noxfer/none option.
|
2015-02-07 06:11:22 +05:30
|
|
|
|
2016-11-23 19:16:56 +05:30
|
|
|
//applet:IF_DD(APPLET_NOEXEC(dd, dd, BB_DIR_BIN, BB_SUID_DROP, dd))
|
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_DD) += dd.o
|
|
|
|
|
2011-03-31 18:13:25 +05:30
|
|
|
//usage:#define dd_trivial_usage
|
2018-10-30 21:34:52 +05:30
|
|
|
//usage: "[if=FILE] [of=FILE] [" IF_FEATURE_DD_IBS_OBS("ibs=N obs=N/") "bs=N] [count=N] [skip=N] [seek=N]\n"
|
|
|
|
//usage: IF_FEATURE_DD_IBS_OBS(
|
|
|
|
//usage: " [conv=notrunc|noerror|sync|fsync]\n"
|
2019-02-06 17:27:15 +05:30
|
|
|
//usage: " [iflag=skip_bytes|fullblock] [oflag=seek_bytes|append]"
|
2018-10-30 21:34:52 +05:30
|
|
|
//usage: )
|
2011-03-31 18:13:25 +05:30
|
|
|
//usage:#define dd_full_usage "\n\n"
|
|
|
|
//usage: "Copy a file with converting and formatting\n"
|
|
|
|
//usage: "\n if=FILE Read from FILE instead of stdin"
|
|
|
|
//usage: "\n of=FILE Write to FILE instead of stdout"
|
|
|
|
//usage: "\n bs=N Read and write N bytes at a time"
|
|
|
|
//usage: IF_FEATURE_DD_IBS_OBS(
|
|
|
|
//usage: "\n ibs=N Read N bytes at a time"
|
|
|
|
//usage: )
|
|
|
|
//usage: IF_FEATURE_DD_IBS_OBS(
|
|
|
|
//usage: "\n obs=N Write N bytes at a time"
|
|
|
|
//usage: )
|
|
|
|
//usage: "\n count=N Copy only N input blocks"
|
|
|
|
//usage: "\n skip=N Skip N input blocks"
|
|
|
|
//usage: "\n seek=N Skip N output blocks"
|
|
|
|
//usage: IF_FEATURE_DD_IBS_OBS(
|
|
|
|
//usage: "\n conv=notrunc Don't truncate output file"
|
|
|
|
//usage: "\n conv=noerror Continue after read errors"
|
|
|
|
//usage: "\n conv=sync Pad blocks with zeros"
|
|
|
|
//usage: "\n conv=fsync Physically write data out before finishing"
|
2013-08-19 12:31:39 +05:30
|
|
|
//usage: "\n conv=swab Swap every pair of bytes"
|
2016-02-01 06:47:28 +05:30
|
|
|
//usage: "\n iflag=skip_bytes skip=N is in bytes"
|
2018-01-25 23:30:19 +05:30
|
|
|
//usage: "\n iflag=fullblock Read full blocks"
|
2018-10-15 15:56:15 +05:30
|
|
|
//usage: "\n oflag=seek_bytes seek=N is in bytes"
|
2019-02-06 17:27:15 +05:30
|
|
|
//usage: "\n oflag=append Open output file in append mode"
|
2011-03-31 18:13:25 +05:30
|
|
|
//usage: )
|
2015-02-07 06:11:22 +05:30
|
|
|
//usage: IF_FEATURE_DD_STATUS(
|
|
|
|
//usage: "\n status=noxfer Suppress rate output"
|
|
|
|
//usage: "\n status=none Suppress all output"
|
|
|
|
//usage: )
|
2011-03-31 18:13:25 +05:30
|
|
|
//usage: "\n"
|
2015-02-07 06:11:22 +05:30
|
|
|
//usage: "\nN may be suffixed by c (1), w (2), b (512), kB (1000), k (1024), MB, M, GB, G"
|
2011-03-31 18:13:25 +05:30
|
|
|
//usage:
|
|
|
|
//usage:#define dd_example_usage
|
|
|
|
//usage: "$ dd if=/dev/zero of=/dev/ram1 bs=1M count=4\n"
|
|
|
|
//usage: "4+0 records in\n"
|
|
|
|
//usage: "4+0 records out\n"
|
|
|
|
|
2007-05-27 00:30:18 +05:30
|
|
|
#include "libbb.h"
|
2016-04-21 19:56:30 +05:30
|
|
|
#include "common_bufsiz.h"
|
2007-04-10 21:13:37 +05:30
|
|
|
|
|
|
|
/* This is a NOEXEC applet. Be very careful! */
|
|
|
|
|
2001-02-20 11:44:08 +05:30
|
|
|
|
2007-12-02 07:14:42 +05:30
|
|
|
enum {
|
|
|
|
ifd = STDIN_FILENO,
|
|
|
|
ofd = STDOUT_FILENO,
|
|
|
|
};
|
|
|
|
|
2007-04-04 19:31:23 +05:30
|
|
|
struct globals {
|
|
|
|
off_t out_full, out_part, in_full, in_part;
|
2009-10-14 03:59:28 +05:30
|
|
|
#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
|
|
|
|
unsigned long long total_bytes;
|
|
|
|
unsigned long long begin_time_us;
|
|
|
|
#endif
|
2015-02-07 06:11:22 +05:30
|
|
|
int flags;
|
2010-02-04 19:30:15 +05:30
|
|
|
} FIX_ALIASING;
|
2016-04-21 19:56:30 +05:30
|
|
|
#define G (*(struct globals*)bb_common_bufsiz1)
|
2009-09-30 06:09:57 +05:30
|
|
|
#define INIT_G() do { \
|
2016-04-21 21:48:48 +05:30
|
|
|
setup_common_bufsiz(); \
|
2009-09-30 06:09:57 +05:30
|
|
|
/* we have to zero it out because of NOEXEC */ \
|
|
|
|
memset(&G, 0, sizeof(G)); \
|
|
|
|
} while (0)
|
2007-07-27 20:32:00 +05:30
|
|
|
|
2015-02-07 06:11:22 +05:30
|
|
|
enum {
|
|
|
|
/* Must be in the same order as OP_conv_XXX! */
|
|
|
|
/* (see "flags |= (1 << what)" below) */
|
|
|
|
FLAG_NOTRUNC = (1 << 0) * ENABLE_FEATURE_DD_IBS_OBS,
|
|
|
|
FLAG_SYNC = (1 << 1) * ENABLE_FEATURE_DD_IBS_OBS,
|
|
|
|
FLAG_NOERROR = (1 << 2) * ENABLE_FEATURE_DD_IBS_OBS,
|
|
|
|
FLAG_FSYNC = (1 << 3) * ENABLE_FEATURE_DD_IBS_OBS,
|
|
|
|
FLAG_SWAB = (1 << 4) * ENABLE_FEATURE_DD_IBS_OBS,
|
|
|
|
/* end of conv flags */
|
2016-02-01 06:47:28 +05:30
|
|
|
/* start of input flags */
|
|
|
|
FLAG_IFLAG_SHIFT = 5,
|
|
|
|
FLAG_SKIP_BYTES = (1 << 5) * ENABLE_FEATURE_DD_IBS_OBS,
|
2018-01-25 23:30:19 +05:30
|
|
|
FLAG_FULLBLOCK = (1 << 6) * ENABLE_FEATURE_DD_IBS_OBS,
|
2016-02-01 06:47:28 +05:30
|
|
|
/* end of input flags */
|
2018-10-15 15:56:15 +05:30
|
|
|
/* start of output flags */
|
|
|
|
FLAG_OFLAG_SHIFT = 7,
|
|
|
|
FLAG_SEEK_BYTES = (1 << 7) * ENABLE_FEATURE_DD_IBS_OBS,
|
2019-02-06 17:27:15 +05:30
|
|
|
FLAG_APPEND = (1 << 8) * ENABLE_FEATURE_DD_IBS_OBS,
|
2018-10-15 15:56:15 +05:30
|
|
|
/* end of output flags */
|
2019-02-06 17:27:15 +05:30
|
|
|
FLAG_TWOBUFS = (1 << 9) * ENABLE_FEATURE_DD_IBS_OBS,
|
|
|
|
FLAG_COUNT = 1 << 10,
|
|
|
|
FLAG_STATUS_NONE = 1 << 11,
|
|
|
|
FLAG_STATUS_NOXFER = 1 << 12,
|
2015-02-07 06:11:22 +05:30
|
|
|
};
|
2006-05-03 04:14:04 +05:30
|
|
|
|
2008-07-05 14:48:54 +05:30
|
|
|
static void dd_output_status(int UNUSED_PARAM cur_signal)
|
2006-05-03 04:14:04 +05:30
|
|
|
{
|
2009-10-14 03:59:28 +05:30
|
|
|
#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
|
2009-10-14 21:13:41 +05:30
|
|
|
double seconds;
|
|
|
|
unsigned long long bytes_sec;
|
|
|
|
unsigned long long now_us = monotonic_us(); /* before fprintf */
|
2009-10-14 03:59:28 +05:30
|
|
|
#endif
|
|
|
|
|
2007-07-30 16:28:09 +05:30
|
|
|
/* Deliberately using %u, not %d */
|
|
|
|
fprintf(stderr, "%"OFF_FMT"u+%"OFF_FMT"u records in\n"
|
|
|
|
"%"OFF_FMT"u+%"OFF_FMT"u records out\n",
|
2007-04-04 19:31:23 +05:30
|
|
|
G.in_full, G.in_part,
|
|
|
|
G.out_full, G.out_part);
|
2009-10-14 03:59:28 +05:30
|
|
|
|
|
|
|
#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
|
2015-02-07 06:11:22 +05:30
|
|
|
# if ENABLE_FEATURE_DD_STATUS
|
|
|
|
if (G.flags & FLAG_STATUS_NOXFER) /* status=noxfer active? */
|
|
|
|
return;
|
|
|
|
//TODO: should status=none make dd stop reacting to USR1 entirely?
|
|
|
|
//So far we react to it (we print the stats),
|
|
|
|
//status=none only suppresses final, non-USR1 generated status message.
|
|
|
|
# endif
|
2009-10-14 03:59:28 +05:30
|
|
|
fprintf(stderr, "%llu bytes (%sB) copied, ",
|
|
|
|
G.total_bytes,
|
|
|
|
/* show fractional digit, use suffixes */
|
|
|
|
make_human_readable_str(G.total_bytes, 1, 0)
|
|
|
|
);
|
|
|
|
/* Corner cases:
|
|
|
|
* ./busybox dd </dev/null >/dev/null
|
|
|
|
* ./busybox dd bs=1M count=2000 </dev/zero >/dev/null
|
|
|
|
* (echo DONE) | ./busybox dd >/dev/null
|
|
|
|
* (sleep 1; echo DONE) | ./busybox dd >/dev/null
|
|
|
|
*/
|
2009-10-14 21:13:41 +05:30
|
|
|
seconds = (now_us - G.begin_time_us) / 1000000.0;
|
|
|
|
bytes_sec = G.total_bytes / seconds;
|
2009-10-14 03:59:28 +05:30
|
|
|
fprintf(stderr, "%f seconds, %sB/s\n",
|
2009-10-14 21:13:41 +05:30
|
|
|
seconds,
|
2009-10-14 03:59:28 +05:30
|
|
|
/* show fractional digit, use suffixes */
|
2009-10-14 21:13:41 +05:30
|
|
|
make_human_readable_str(bytes_sec, 1, 0)
|
2009-10-14 03:59:28 +05:30
|
|
|
);
|
|
|
|
#endif
|
2006-05-03 04:14:04 +05:30
|
|
|
}
|
|
|
|
|
2007-12-02 07:14:42 +05:30
|
|
|
static bool write_and_stats(const void *buf, size_t len, size_t obs,
|
2007-07-24 21:24:42 +05:30
|
|
|
const char *filename)
|
2007-04-04 19:31:23 +05:30
|
|
|
{
|
2019-05-14 21:16:18 +05:30
|
|
|
ssize_t n;
|
|
|
|
|
|
|
|
n = full_write(ofd, buf, len);
|
2009-10-14 03:59:28 +05:30
|
|
|
#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
|
2019-05-14 21:16:18 +05:30
|
|
|
if (n > 0)
|
|
|
|
G.total_bytes += n;
|
2009-10-14 03:59:28 +05:30
|
|
|
#endif
|
2018-01-10 15:28:55 +05:30
|
|
|
if ((size_t)n == obs) {
|
|
|
|
G.out_full++;
|
|
|
|
return 0;
|
|
|
|
}
|
2018-01-10 15:34:09 +05:30
|
|
|
if ((size_t)n == len) {
|
2018-01-10 15:28:55 +05:30
|
|
|
G.out_part++;
|
2018-01-10 15:34:09 +05:30
|
|
|
return 0;
|
|
|
|
}
|
2019-05-14 21:16:18 +05:30
|
|
|
/* n is < len (and possibly is -1).
|
|
|
|
* Even if n >= 0, errno is usually set correctly.
|
|
|
|
* For example, if writing to block device and getting ENOSPC,
|
|
|
|
* full_write() first sees a short write, then tries to write
|
|
|
|
* the remainder and gets errno set to ENOSPC.
|
|
|
|
* It returns n > 0 (the amount which it did write).
|
|
|
|
*/
|
|
|
|
bb_perror_msg("error writing '%s'", filename);
|
2018-01-10 15:28:55 +05:30
|
|
|
return 1;
|
2007-04-04 19:31:23 +05:30
|
|
|
}
|
|
|
|
|
2006-11-25 20:16:21 +05:30
|
|
|
#if ENABLE_LFS
|
2009-10-14 03:59:28 +05:30
|
|
|
# define XATOU_SFX xatoull_sfx
|
2006-11-25 20:16:21 +05:30
|
|
|
#else
|
2009-10-14 03:59:28 +05:30
|
|
|
# define XATOU_SFX xatoul_sfx
|
2006-11-25 20:16:21 +05:30
|
|
|
#endif
|
|
|
|
|
2016-02-01 06:47:28 +05:30
|
|
|
#if ENABLE_FEATURE_DD_IBS_OBS
|
|
|
|
static int parse_comma_flags(char *val, const char *words, const char *error_in)
|
|
|
|
{
|
|
|
|
int flags = 0;
|
|
|
|
while (1) {
|
|
|
|
int n;
|
|
|
|
char *arg;
|
|
|
|
/* find ',', replace them with NUL so we can use val for
|
|
|
|
* index_in_strings() without copying.
|
|
|
|
* We rely on val being non-null, else strchr would fault.
|
|
|
|
*/
|
|
|
|
arg = strchr(val, ',');
|
|
|
|
if (arg)
|
|
|
|
*arg = '\0';
|
|
|
|
n = index_in_strings(words, val);
|
|
|
|
if (n < 0)
|
|
|
|
bb_error_msg_and_die(bb_msg_invalid_arg_to, val, error_in);
|
|
|
|
flags |= (1 << n);
|
|
|
|
if (!arg) /* no ',' left, so this was the last specifier */
|
|
|
|
break;
|
|
|
|
*arg = ','; /* to preserve ps listing */
|
|
|
|
val = arg + 1; /* skip this keyword and ',' */
|
|
|
|
}
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
int dd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 14:48:54 +05:30
|
|
|
int dd_main(int argc UNUSED_PARAM, char **argv)
|
1999-10-13 03:56:06 +05:30
|
|
|
{
|
2007-08-13 02:28:27 +05:30
|
|
|
static const char keywords[] ALIGN1 =
|
2015-02-07 06:11:22 +05:30
|
|
|
"bs\0""count\0""seek\0""skip\0""if\0""of\0"IF_FEATURE_DD_STATUS("status\0")
|
2007-04-04 19:31:23 +05:30
|
|
|
#if ENABLE_FEATURE_DD_IBS_OBS
|
2018-10-15 15:56:15 +05:30
|
|
|
"ibs\0""obs\0""conv\0""iflag\0""oflag\0"
|
2007-04-04 19:31:23 +05:30
|
|
|
#endif
|
2007-07-24 21:24:42 +05:30
|
|
|
;
|
2008-04-03 02:50:35 +05:30
|
|
|
#if ENABLE_FEATURE_DD_IBS_OBS
|
|
|
|
static const char conv_words[] ALIGN1 =
|
2013-08-19 12:31:39 +05:30
|
|
|
"notrunc\0""sync\0""noerror\0""fsync\0""swab\0";
|
2016-02-01 06:47:28 +05:30
|
|
|
static const char iflag_words[] ALIGN1 =
|
2018-01-25 23:30:19 +05:30
|
|
|
"skip_bytes\0""fullblock\0";
|
2018-10-15 15:56:15 +05:30
|
|
|
static const char oflag_words[] ALIGN1 =
|
2019-02-06 17:27:15 +05:30
|
|
|
"seek_bytes\0append\0";
|
2015-02-07 06:11:22 +05:30
|
|
|
#endif
|
|
|
|
#if ENABLE_FEATURE_DD_STATUS
|
|
|
|
static const char status_words[] ALIGN1 =
|
|
|
|
"none\0""noxfer\0";
|
2008-04-03 02:50:35 +05:30
|
|
|
#endif
|
2007-04-11 01:00:50 +05:30
|
|
|
enum {
|
2008-04-03 02:50:35 +05:30
|
|
|
OP_bs = 0,
|
2007-04-11 01:00:50 +05:30
|
|
|
OP_count,
|
|
|
|
OP_seek,
|
|
|
|
OP_skip,
|
|
|
|
OP_if,
|
|
|
|
OP_of,
|
2015-02-07 06:11:22 +05:30
|
|
|
IF_FEATURE_DD_STATUS(OP_status,)
|
2007-04-16 17:51:05 +05:30
|
|
|
#if ENABLE_FEATURE_DD_IBS_OBS
|
2007-04-11 01:00:50 +05:30
|
|
|
OP_ibs,
|
|
|
|
OP_obs,
|
|
|
|
OP_conv,
|
2016-02-01 06:47:28 +05:30
|
|
|
OP_iflag,
|
2018-10-15 15:56:15 +05:30
|
|
|
OP_oflag,
|
2008-04-03 02:50:35 +05:30
|
|
|
/* Must be in the same order as FLAG_XXX! */
|
|
|
|
OP_conv_notrunc = 0,
|
2007-04-11 01:00:50 +05:30
|
|
|
OP_conv_sync,
|
2007-04-20 01:46:14 +05:30
|
|
|
OP_conv_noerror,
|
2008-04-03 02:50:35 +05:30
|
|
|
OP_conv_fsync,
|
2013-08-19 12:31:39 +05:30
|
|
|
OP_conv_swab,
|
2008-04-03 02:50:35 +05:30
|
|
|
/* Unimplemented conv=XXX: */
|
|
|
|
//nocreat do not create the output file
|
|
|
|
//excl fail if the output file already exists
|
|
|
|
//fdatasync physically write output file data before finishing
|
|
|
|
//lcase change upper case to lower case
|
|
|
|
//ucase change lower case to upper case
|
|
|
|
//block pad newline-terminated records with spaces to cbs-size
|
|
|
|
//unblock replace trailing spaces in cbs-size records with newline
|
|
|
|
//ascii from EBCDIC to ASCII
|
|
|
|
//ebcdic from ASCII to EBCDIC
|
|
|
|
//ibm from ASCII to alternate EBCDIC
|
2013-08-19 12:31:39 +05:30
|
|
|
/* Partially implemented: */
|
|
|
|
//swab swap every pair of input bytes: will abort on non-even reads
|
2016-02-01 06:47:28 +05:30
|
|
|
OP_iflag_skip_bytes,
|
2018-01-25 23:30:19 +05:30
|
|
|
OP_iflag_fullblock,
|
2018-10-15 15:56:15 +05:30
|
|
|
OP_oflag_seek_bytes,
|
2007-04-16 17:51:05 +05:30
|
|
|
#endif
|
2007-04-11 01:00:50 +05:30
|
|
|
};
|
2013-08-20 06:20:49 +05:30
|
|
|
smallint exitcode = EXIT_FAILURE;
|
2013-08-19 12:30:08 +05:30
|
|
|
int i;
|
2013-08-20 15:38:46 +05:30
|
|
|
size_t ibs = 512;
|
|
|
|
char *ibuf;
|
|
|
|
#if ENABLE_FEATURE_DD_IBS_OBS
|
|
|
|
size_t obs = 512;
|
|
|
|
char *obuf;
|
|
|
|
#else
|
|
|
|
# define obs ibs
|
|
|
|
# define obuf ibuf
|
|
|
|
#endif
|
|
|
|
/* These are all zeroed at once! */
|
2007-07-27 20:32:00 +05:30
|
|
|
struct {
|
2018-10-30 21:34:52 +05:30
|
|
|
IF_FEATURE_DD_IBS_OBS(size_t ocount;)
|
2013-08-20 06:20:49 +05:30
|
|
|
ssize_t prev_read_size; /* for detecting swab failure */
|
2007-07-27 20:32:00 +05:30
|
|
|
off_t count;
|
|
|
|
off_t seek, skip;
|
|
|
|
const char *infile, *outfile;
|
|
|
|
} Z;
|
2018-10-30 21:34:52 +05:30
|
|
|
#define ocount (Z.ocount )
|
2013-08-20 06:20:49 +05:30
|
|
|
#define prev_read_size (Z.prev_read_size)
|
2007-07-27 20:32:00 +05:30
|
|
|
#define count (Z.count )
|
|
|
|
#define seek (Z.seek )
|
|
|
|
#define skip (Z.skip )
|
|
|
|
#define infile (Z.infile )
|
|
|
|
#define outfile (Z.outfile)
|
2000-12-19 03:08:57 +05:30
|
|
|
|
2007-07-27 20:32:00 +05:30
|
|
|
memset(&Z, 0, sizeof(Z));
|
|
|
|
INIT_G();
|
2009-11-02 18:49:51 +05:30
|
|
|
//fflush_all(); - is this needed because of NOEXEC?
|
2006-05-03 04:14:04 +05:30
|
|
|
|
2013-08-19 12:30:08 +05:30
|
|
|
for (i = 1; argv[i]; i++) {
|
2008-04-03 02:50:35 +05:30
|
|
|
int what;
|
|
|
|
char *val;
|
2013-08-19 12:30:08 +05:30
|
|
|
char *arg = argv[i];
|
2007-04-04 19:31:23 +05:30
|
|
|
|
2008-04-03 02:50:35 +05:30
|
|
|
#if ENABLE_DESKTOP
|
|
|
|
/* "dd --". NB: coreutils 6.9 will complain if they see
|
|
|
|
* more than one of them. We wouldn't. */
|
|
|
|
if (arg[0] == '-' && arg[1] == '-' && arg[2] == '\0')
|
|
|
|
continue;
|
|
|
|
#endif
|
|
|
|
val = strchr(arg, '=');
|
|
|
|
if (val == NULL)
|
2007-04-04 19:31:23 +05:30
|
|
|
bb_show_usage();
|
2008-04-03 02:50:35 +05:30
|
|
|
*val = '\0';
|
|
|
|
what = index_in_strings(keywords, arg);
|
|
|
|
if (what < 0)
|
2007-04-04 19:31:23 +05:30
|
|
|
bb_show_usage();
|
2008-04-03 02:50:35 +05:30
|
|
|
/* *val = '='; - to preserve ps listing? */
|
|
|
|
val++;
|
2007-04-16 17:51:05 +05:30
|
|
|
#if ENABLE_FEATURE_DD_IBS_OBS
|
2008-02-24 19:06:01 +05:30
|
|
|
if (what == OP_ibs) {
|
|
|
|
/* Must fit into positive ssize_t */
|
2015-03-04 22:16:48 +05:30
|
|
|
ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, cwbkMG_suffixes);
|
2008-04-03 02:50:35 +05:30
|
|
|
/*continue;*/
|
2008-02-24 19:06:01 +05:30
|
|
|
}
|
|
|
|
if (what == OP_obs) {
|
2015-03-04 22:16:48 +05:30
|
|
|
obs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, cwbkMG_suffixes);
|
2008-04-03 02:50:35 +05:30
|
|
|
/*continue;*/
|
2008-02-24 19:06:01 +05:30
|
|
|
}
|
|
|
|
if (what == OP_conv) {
|
2016-02-01 06:47:28 +05:30
|
|
|
G.flags |= parse_comma_flags(val, conv_words, "conv");
|
|
|
|
/*continue;*/
|
|
|
|
}
|
|
|
|
if (what == OP_iflag) {
|
|
|
|
G.flags |= parse_comma_flags(val, iflag_words, "iflag") << FLAG_IFLAG_SHIFT;
|
2013-08-20 15:38:46 +05:30
|
|
|
/*continue;*/
|
2008-02-24 19:06:01 +05:30
|
|
|
}
|
2018-10-15 15:56:15 +05:30
|
|
|
if (what == OP_oflag) {
|
|
|
|
G.flags |= parse_comma_flags(val, oflag_words, "oflag") << FLAG_OFLAG_SHIFT;
|
|
|
|
/*continue;*/
|
|
|
|
}
|
2007-04-16 17:51:05 +05:30
|
|
|
#endif
|
2007-04-04 19:31:23 +05:30
|
|
|
if (what == OP_bs) {
|
2015-03-04 22:16:48 +05:30
|
|
|
ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, cwbkMG_suffixes);
|
2013-08-20 15:38:46 +05:30
|
|
|
obs = ibs;
|
2008-04-03 02:50:35 +05:30
|
|
|
/*continue;*/
|
2007-04-04 19:31:23 +05:30
|
|
|
}
|
|
|
|
/* These can be large: */
|
|
|
|
if (what == OP_count) {
|
2015-02-07 06:11:22 +05:30
|
|
|
G.flags |= FLAG_COUNT;
|
2015-03-04 22:16:48 +05:30
|
|
|
count = XATOU_SFX(val, cwbkMG_suffixes);
|
2008-04-03 02:50:35 +05:30
|
|
|
/*continue;*/
|
2007-04-04 19:31:23 +05:30
|
|
|
}
|
|
|
|
if (what == OP_seek) {
|
2015-03-04 22:16:48 +05:30
|
|
|
seek = XATOU_SFX(val, cwbkMG_suffixes);
|
2008-04-03 02:50:35 +05:30
|
|
|
/*continue;*/
|
2007-04-04 19:31:23 +05:30
|
|
|
}
|
2007-04-20 01:38:19 +05:30
|
|
|
if (what == OP_skip) {
|
2015-03-04 22:16:48 +05:30
|
|
|
skip = XATOU_SFX(val, cwbkMG_suffixes);
|
2008-04-03 02:50:35 +05:30
|
|
|
/*continue;*/
|
2007-04-04 19:31:23 +05:30
|
|
|
}
|
|
|
|
if (what == OP_if) {
|
2008-04-03 02:50:35 +05:30
|
|
|
infile = val;
|
|
|
|
/*continue;*/
|
2007-04-04 19:31:23 +05:30
|
|
|
}
|
2008-04-03 02:50:35 +05:30
|
|
|
if (what == OP_of) {
|
|
|
|
outfile = val;
|
|
|
|
/*continue;*/
|
|
|
|
}
|
2015-02-07 06:11:22 +05:30
|
|
|
#if ENABLE_FEATURE_DD_STATUS
|
|
|
|
if (what == OP_status) {
|
|
|
|
int n;
|
|
|
|
n = index_in_strings(status_words, val);
|
|
|
|
if (n < 0)
|
2015-10-14 16:51:01 +05:30
|
|
|
bb_error_msg_and_die(bb_msg_invalid_arg_to, val, "status");
|
2017-07-14 17:14:30 +05:30
|
|
|
G.flags |= FLAG_STATUS_NONE << n;
|
2015-02-07 06:11:22 +05:30
|
|
|
/*continue;*/
|
|
|
|
}
|
|
|
|
#endif
|
2013-08-19 12:30:08 +05:30
|
|
|
} /* end of "for (argv[i])" */
|
2008-04-03 02:50:35 +05:30
|
|
|
|
2007-04-04 19:31:23 +05:30
|
|
|
//XXX:FIXME for huge ibs or obs, malloc'ing them isn't the brightest idea ever
|
2013-08-20 15:38:46 +05:30
|
|
|
ibuf = xmalloc(ibs);
|
|
|
|
obuf = ibuf;
|
|
|
|
#if ENABLE_FEATURE_DD_IBS_OBS
|
2006-10-31 21:25:56 +05:30
|
|
|
if (ibs != obs) {
|
2015-02-07 06:11:22 +05:30
|
|
|
G.flags |= FLAG_TWOBUFS;
|
2006-08-29 05:01:54 +05:30
|
|
|
obuf = xmalloc(obs);
|
2006-10-31 21:25:56 +05:30
|
|
|
}
|
2013-08-20 15:38:46 +05:30
|
|
|
#endif
|
2009-10-14 03:59:28 +05:30
|
|
|
|
|
|
|
#if ENABLE_FEATURE_DD_SIGNAL_HANDLING
|
|
|
|
signal_SA_RESTART_empty_mask(SIGUSR1, dd_output_status);
|
|
|
|
#endif
|
|
|
|
#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
|
|
|
|
G.begin_time_us = monotonic_us();
|
|
|
|
#endif
|
|
|
|
|
2013-08-20 15:38:46 +05:30
|
|
|
if (infile) {
|
2007-12-02 07:14:42 +05:30
|
|
|
xmove_fd(xopen(infile, O_RDONLY), ifd);
|
2013-08-20 15:38:46 +05:30
|
|
|
} else {
|
2003-03-19 14:43:01 +05:30
|
|
|
infile = bb_msg_standard_input;
|
1999-10-13 06:23:55 +05:30
|
|
|
}
|
2013-08-20 15:38:46 +05:30
|
|
|
if (outfile) {
|
2007-04-04 19:31:23 +05:30
|
|
|
int oflag = O_WRONLY | O_CREAT;
|
2001-01-17 05:51:05 +05:30
|
|
|
|
2015-02-07 06:11:22 +05:30
|
|
|
if (!seek && !(G.flags & FLAG_NOTRUNC))
|
2001-01-17 05:51:05 +05:30
|
|
|
oflag |= O_TRUNC;
|
2019-02-06 17:27:15 +05:30
|
|
|
if (G.flags & FLAG_APPEND)
|
|
|
|
oflag |= O_APPEND;
|
2001-01-17 05:51:05 +05:30
|
|
|
|
2007-12-02 07:14:42 +05:30
|
|
|
xmove_fd(xopen(outfile, oflag), ofd);
|
2001-01-17 05:51:05 +05:30
|
|
|
|
2015-02-07 06:11:22 +05:30
|
|
|
if (seek && !(G.flags & FLAG_NOTRUNC)) {
|
2018-10-15 15:56:15 +05:30
|
|
|
size_t blocksz = (G.flags & FLAG_SEEK_BYTES) ? 1 : obs;
|
|
|
|
if (ftruncate(ofd, seek * blocksz) < 0) {
|
2002-02-05 20:58:54 +05:30
|
|
|
struct stat st;
|
|
|
|
|
2010-01-28 06:54:24 +05:30
|
|
|
if (fstat(ofd, &st) < 0
|
|
|
|
|| S_ISREG(st.st_mode)
|
|
|
|
|| S_ISDIR(st.st_mode)
|
|
|
|
) {
|
2006-08-29 05:01:54 +05:30
|
|
|
goto die_outfile;
|
2010-01-28 06:54:24 +05:30
|
|
|
}
|
2002-02-05 20:58:54 +05:30
|
|
|
}
|
2001-01-17 05:51:05 +05:30
|
|
|
}
|
2000-12-19 03:08:57 +05:30
|
|
|
} else {
|
2003-03-19 14:43:01 +05:30
|
|
|
outfile = bb_msg_standard_output;
|
2000-12-19 03:08:57 +05:30
|
|
|
}
|
|
|
|
if (skip) {
|
2016-02-01 06:47:28 +05:30
|
|
|
size_t blocksz = (G.flags & FLAG_SKIP_BYTES) ? 1 : ibs;
|
|
|
|
if (lseek(ifd, skip * blocksz, SEEK_CUR) < 0) {
|
2013-08-20 16:15:05 +05:30
|
|
|
do {
|
2018-01-25 23:30:19 +05:30
|
|
|
ssize_t n;
|
|
|
|
#if ENABLE_FEATURE_DD_IBS_OBS
|
|
|
|
if (G.flags & FLAG_FULLBLOCK)
|
|
|
|
n = full_read(ifd, ibuf, blocksz);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
n = safe_read(ifd, ibuf, blocksz);
|
2006-05-16 22:22:12 +05:30
|
|
|
if (n < 0)
|
2007-04-04 19:31:23 +05:30
|
|
|
goto die_infile;
|
2006-05-16 22:22:12 +05:30
|
|
|
if (n == 0)
|
|
|
|
break;
|
2013-08-20 16:15:05 +05:30
|
|
|
} while (--skip != 0);
|
2002-11-28 16:35:28 +05:30
|
|
|
}
|
1999-10-13 03:56:06 +05:30
|
|
|
}
|
2000-12-19 03:08:57 +05:30
|
|
|
if (seek) {
|
2018-10-15 15:56:15 +05:30
|
|
|
size_t blocksz = (G.flags & FLAG_SEEK_BYTES) ? 1 : obs;
|
|
|
|
if (lseek(ofd, seek * blocksz, SEEK_CUR) < 0)
|
2006-08-29 05:01:54 +05:30
|
|
|
goto die_outfile;
|
2000-12-19 03:08:57 +05:30
|
|
|
}
|
2000-11-30 04:03:02 +05:30
|
|
|
|
2015-02-07 06:11:22 +05:30
|
|
|
while (!(G.flags & FLAG_COUNT) || (G.in_full + G.in_part != count)) {
|
2013-08-19 12:30:08 +05:30
|
|
|
ssize_t n;
|
2018-01-25 23:30:19 +05:30
|
|
|
#if ENABLE_FEATURE_DD_IBS_OBS
|
|
|
|
if (G.flags & FLAG_FULLBLOCK)
|
|
|
|
n = full_read(ifd, ibuf, ibs);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
n = safe_read(ifd, ibuf, ibs);
|
2006-08-29 05:01:54 +05:30
|
|
|
if (n == 0)
|
2006-05-16 22:22:12 +05:30
|
|
|
break;
|
2002-04-27 07:01:43 +05:30
|
|
|
if (n < 0) {
|
2009-09-19 00:19:36 +05:30
|
|
|
/* "Bad block" */
|
2015-02-07 06:11:22 +05:30
|
|
|
if (!(G.flags & FLAG_NOERROR))
|
2007-04-04 19:31:23 +05:30
|
|
|
goto die_infile;
|
2007-12-02 07:14:42 +05:30
|
|
|
bb_simple_perror_msg(infile);
|
2009-09-19 00:19:36 +05:30
|
|
|
/* GNU dd with conv=noerror skips over bad blocks */
|
2009-08-06 02:46:27 +05:30
|
|
|
xlseek(ifd, ibs, SEEK_CUR);
|
2009-09-19 00:19:36 +05:30
|
|
|
/* conv=noerror,sync writes NULs,
|
|
|
|
* conv=noerror just ignores input bad blocks */
|
|
|
|
n = 0;
|
2002-04-27 07:01:43 +05:30
|
|
|
}
|
2015-02-07 06:11:22 +05:30
|
|
|
if (G.flags & FLAG_SWAB) {
|
2013-08-20 16:19:28 +05:30
|
|
|
uint16_t *p16;
|
|
|
|
ssize_t n2;
|
2013-08-20 06:20:49 +05:30
|
|
|
|
|
|
|
/* Our code allows only last read to be odd-sized */
|
|
|
|
if (prev_read_size & 1)
|
|
|
|
bb_error_msg_and_die("can't swab %lu byte buffer",
|
|
|
|
(unsigned long)prev_read_size);
|
|
|
|
prev_read_size = n;
|
|
|
|
|
2013-08-20 15:57:19 +05:30
|
|
|
/* If n is odd, last byte is not swapped:
|
2013-08-19 14:00:55 +05:30
|
|
|
* echo -n "qwe" | dd conv=swab
|
2013-08-19 12:31:39 +05:30
|
|
|
* prints "wqe".
|
|
|
|
*/
|
2013-08-20 06:20:49 +05:30
|
|
|
p16 = (void*) ibuf;
|
2013-08-20 16:19:28 +05:30
|
|
|
n2 = (n >> 1);
|
|
|
|
while (--n2 >= 0) {
|
2013-08-19 12:31:39 +05:30
|
|
|
*p16 = bswap_16(*p16);
|
|
|
|
p16++;
|
|
|
|
}
|
|
|
|
}
|
2006-08-29 05:01:54 +05:30
|
|
|
if ((size_t)n == ibs)
|
2007-04-04 19:31:23 +05:30
|
|
|
G.in_full++;
|
2006-08-29 05:01:54 +05:30
|
|
|
else {
|
2007-04-04 19:31:23 +05:30
|
|
|
G.in_part++;
|
2015-02-07 06:11:22 +05:30
|
|
|
if (G.flags & FLAG_SYNC) {
|
2009-09-19 00:19:36 +05:30
|
|
|
memset(ibuf + n, 0, ibs - n);
|
2006-05-16 22:22:12 +05:30
|
|
|
n = ibs;
|
|
|
|
}
|
2002-11-28 16:35:28 +05:30
|
|
|
}
|
2018-10-30 21:34:52 +05:30
|
|
|
#if ENABLE_FEATURE_DD_IBS_OBS
|
2015-02-07 06:11:22 +05:30
|
|
|
if (G.flags & FLAG_TWOBUFS) {
|
2006-05-16 22:22:12 +05:30
|
|
|
char *tmp = ibuf;
|
|
|
|
while (n) {
|
2018-10-30 21:34:52 +05:30
|
|
|
size_t d = obs - ocount;
|
2008-05-13 07:57:31 +05:30
|
|
|
if (d > (size_t)n)
|
2006-08-29 05:01:54 +05:30
|
|
|
d = n;
|
2018-10-30 21:34:52 +05:30
|
|
|
memcpy(obuf + ocount, tmp, d);
|
2006-05-16 22:22:12 +05:30
|
|
|
n -= d;
|
|
|
|
tmp += d;
|
2018-10-30 21:34:52 +05:30
|
|
|
ocount += d;
|
|
|
|
if (ocount == obs) {
|
2007-12-02 07:14:42 +05:30
|
|
|
if (write_and_stats(obuf, obs, obs, outfile))
|
2007-04-04 19:31:23 +05:30
|
|
|
goto out_status;
|
2018-10-30 21:34:52 +05:30
|
|
|
ocount = 0;
|
2006-05-16 22:22:12 +05:30
|
|
|
}
|
|
|
|
}
|
2018-10-30 21:34:52 +05:30
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2013-08-20 16:10:29 +05:30
|
|
|
if (write_and_stats(ibuf, n, obs, outfile))
|
|
|
|
goto out_status;
|
|
|
|
}
|
2017-03-17 01:24:35 +05:30
|
|
|
}
|
2008-04-03 02:50:35 +05:30
|
|
|
|
2017-03-17 01:24:35 +05:30
|
|
|
if (G.flags & FLAG_FSYNC) {
|
|
|
|
if (fsync(ofd) < 0)
|
|
|
|
goto die_outfile;
|
2006-05-16 22:22:12 +05:30
|
|
|
}
|
2006-09-17 21:58:10 +05:30
|
|
|
|
2018-10-30 21:34:52 +05:30
|
|
|
#if ENABLE_FEATURE_DD_IBS_OBS
|
|
|
|
if (ocount != 0) {
|
|
|
|
if (write_and_stats(obuf, ocount, obs, outfile))
|
2013-08-20 16:10:29 +05:30
|
|
|
goto out_status;
|
2000-09-11 06:02:13 +05:30
|
|
|
}
|
2018-10-30 21:34:52 +05:30
|
|
|
#endif
|
2006-10-31 21:25:56 +05:30
|
|
|
if (close(ifd) < 0) {
|
2007-07-24 21:24:42 +05:30
|
|
|
die_infile:
|
2007-10-01 17:28:38 +05:30
|
|
|
bb_simple_perror_msg_and_die(infile);
|
2002-11-28 16:35:28 +05:30
|
|
|
}
|
2001-12-07 21:57:37 +05:30
|
|
|
|
2006-10-31 21:25:56 +05:30
|
|
|
if (close(ofd) < 0) {
|
2007-07-24 21:24:42 +05:30
|
|
|
die_outfile:
|
2007-10-01 17:28:38 +05:30
|
|
|
bb_simple_perror_msg_and_die(outfile);
|
2002-11-28 16:35:28 +05:30
|
|
|
}
|
2007-12-02 07:14:42 +05:30
|
|
|
|
|
|
|
exitcode = EXIT_SUCCESS;
|
2007-07-24 21:24:42 +05:30
|
|
|
out_status:
|
2015-02-07 06:11:22 +05:30
|
|
|
if (!ENABLE_FEATURE_DD_STATUS || !(G.flags & FLAG_STATUS_NONE))
|
|
|
|
dd_output_status(0);
|
1999-10-05 21:54:54 +05:30
|
|
|
|
2011-03-16 13:09:46 +05:30
|
|
|
if (ENABLE_FEATURE_CLEAN_UP) {
|
|
|
|
free(obuf);
|
2015-02-07 06:11:22 +05:30
|
|
|
if (G.flags & FLAG_TWOBUFS)
|
2011-03-16 13:09:46 +05:30
|
|
|
free(ibuf);
|
|
|
|
}
|
|
|
|
|
2007-12-02 07:14:42 +05:30
|
|
|
return exitcode;
|
1999-10-05 21:54:54 +05:30
|
|
|
}
|