1 /* vi: set sw=4 ts=4: */
3 * Mini dd implementation for busybox
5 * Copyright (C) 2000,2001 Matt Kraai
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
10 //config: bool "dd (7.5 kb)"
13 //config: dd copies a file (from standard input to standard output,
14 //config: by default) using specific input and output blocksizes,
15 //config: while optionally performing conversions on it.
17 //config:config FEATURE_DD_SIGNAL_HANDLING
18 //config: bool "Enable signal handling for status reporting"
20 //config: depends on DD
22 //config: Sending a SIGUSR1 signal to a running 'dd' process makes it
23 //config: print to standard error the number of records read and written
24 //config: so far, then to resume copying.
26 //config: $ dd if=/dev/zero of=/dev/null &
27 //config: $ pid=$!; kill -USR1 $pid; sleep 1; kill $pid
28 //config: 10899206+0 records in
29 //config: 10899206+0 records out
31 //config:config FEATURE_DD_THIRD_STATUS_LINE
32 //config: bool "Enable the third status line upon signal"
34 //config: depends on DD && FEATURE_DD_SIGNAL_HANDLING
36 //config: Displays a coreutils-like third status line with transferred bytes,
37 //config: elapsed time and speed.
39 //config:config FEATURE_DD_IBS_OBS
40 //config: bool "Enable ibs, obs, iflag, oflag and conv options"
42 //config: depends on DD
44 //config: Enable support for writing a certain number of bytes in and out,
45 //config: at a time, and performing conversions on the data stream.
47 //config:config FEATURE_DD_STATUS
48 //config: bool "Enable status display options"
50 //config: depends on DD
52 //config: Enable support for status=noxfer/none option.
54 //applet:IF_DD(APPLET_NOEXEC(dd, dd, BB_DIR_BIN, BB_SUID_DROP, dd))
56 //kbuild:lib-$(CONFIG_DD) += dd.o
58 //usage:#define dd_trivial_usage
59 //usage: "[if=FILE] [of=FILE] [" IF_FEATURE_DD_IBS_OBS("ibs=N obs=N/") "bs=N] [count=N] [skip=N] [seek=N]\n"
60 //usage: IF_FEATURE_DD_IBS_OBS(
61 //usage: " [conv=notrunc|noerror|sync|fsync]\n"
62 //usage: " [iflag=skip_bytes|fullblock] [oflag=seek_bytes|append]"
64 //usage:#define dd_full_usage "\n\n"
65 //usage: "Copy a file with converting and formatting\n"
66 //usage: "\n if=FILE Read from FILE instead of stdin"
67 //usage: "\n of=FILE Write to FILE instead of stdout"
68 //usage: "\n bs=N Read and write N bytes at a time"
69 //usage: IF_FEATURE_DD_IBS_OBS(
70 //usage: "\n ibs=N Read N bytes at a time"
72 //usage: IF_FEATURE_DD_IBS_OBS(
73 //usage: "\n obs=N Write N bytes at a time"
75 //usage: "\n count=N Copy only N input blocks"
76 //usage: "\n skip=N Skip N input blocks"
77 //usage: "\n seek=N Skip N output blocks"
78 //usage: IF_FEATURE_DD_IBS_OBS(
79 //usage: "\n conv=notrunc Don't truncate output file"
80 //usage: "\n conv=noerror Continue after read errors"
81 //usage: "\n conv=sync Pad blocks with zeros"
82 //usage: "\n conv=fsync Physically write data out before finishing"
83 //usage: "\n conv=swab Swap every pair of bytes"
84 //usage: "\n iflag=skip_bytes skip=N is in bytes"
85 //usage: "\n iflag=fullblock Read full blocks"
86 //usage: "\n oflag=seek_bytes seek=N is in bytes"
87 //usage: "\n oflag=append Open output file in append mode"
89 //usage: IF_FEATURE_DD_STATUS(
90 //usage: "\n status=noxfer Suppress rate output"
91 //usage: "\n status=none Suppress all output"
94 //usage: "\nN may be suffixed by c (1), w (2), b (512), kB (1000), k (1024), MB, M, GB, G"
96 //usage:#define dd_example_usage
97 //usage: "$ dd if=/dev/zero of=/dev/ram1 bs=1M count=4\n"
98 //usage: "4+0 records in\n"
99 //usage: "4+0 records out\n"
102 #include "common_bufsiz.h"
104 /* This is a NOEXEC applet. Be very careful! */
113 off_t out_full, out_part, in_full, in_part;
114 #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
115 unsigned long long total_bytes;
116 unsigned long long begin_time_us;
120 #define G (*(struct globals*)bb_common_bufsiz1)
121 #define INIT_G() do { \
122 setup_common_bufsiz(); \
123 /* we have to zero it out because of NOEXEC */ \
124 memset(&G, 0, sizeof(G)); \
128 /* Must be in the same order as OP_conv_XXX! */
129 /* (see "flags |= (1 << what)" below) */
130 FLAG_NOTRUNC = (1 << 0) * ENABLE_FEATURE_DD_IBS_OBS,
131 FLAG_SYNC = (1 << 1) * ENABLE_FEATURE_DD_IBS_OBS,
132 FLAG_NOERROR = (1 << 2) * ENABLE_FEATURE_DD_IBS_OBS,
133 FLAG_FSYNC = (1 << 3) * ENABLE_FEATURE_DD_IBS_OBS,
134 FLAG_SWAB = (1 << 4) * ENABLE_FEATURE_DD_IBS_OBS,
135 /* end of conv flags */
136 /* start of input flags */
137 FLAG_IFLAG_SHIFT = 5,
138 FLAG_SKIP_BYTES = (1 << 5) * ENABLE_FEATURE_DD_IBS_OBS,
139 FLAG_FULLBLOCK = (1 << 6) * ENABLE_FEATURE_DD_IBS_OBS,
140 /* end of input flags */
141 /* start of output flags */
142 FLAG_OFLAG_SHIFT = 7,
143 FLAG_SEEK_BYTES = (1 << 7) * ENABLE_FEATURE_DD_IBS_OBS,
144 FLAG_APPEND = (1 << 8) * ENABLE_FEATURE_DD_IBS_OBS,
145 /* end of output flags */
146 FLAG_TWOBUFS = (1 << 9) * ENABLE_FEATURE_DD_IBS_OBS,
147 FLAG_COUNT = 1 << 10,
148 FLAG_STATUS_NONE = 1 << 11,
149 FLAG_STATUS_NOXFER = 1 << 12,
152 static void dd_output_status(int UNUSED_PARAM cur_signal)
154 #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
156 unsigned long long bytes_sec;
157 unsigned long long now_us = monotonic_us(); /* before fprintf */
160 /* Deliberately using %u, not %d */
161 fprintf(stderr, "%"OFF_FMT"u+%"OFF_FMT"u records in\n"
162 "%"OFF_FMT"u+%"OFF_FMT"u records out\n",
163 G.in_full, G.in_part,
164 G.out_full, G.out_part);
166 #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
167 # if ENABLE_FEATURE_DD_STATUS
168 if (G.flags & FLAG_STATUS_NOXFER) /* status=noxfer active? */
170 //TODO: should status=none make dd stop reacting to USR1 entirely?
171 //So far we react to it (we print the stats),
172 //status=none only suppresses final, non-USR1 generated status message.
174 fprintf(stderr, "%llu bytes (%sB) copied, ",
176 /* show fractional digit, use suffixes */
177 make_human_readable_str(G.total_bytes, 1, 0)
180 * ./busybox dd </dev/null >/dev/null
181 * ./busybox dd bs=1M count=2000 </dev/zero >/dev/null
182 * (echo DONE) | ./busybox dd >/dev/null
183 * (sleep 1; echo DONE) | ./busybox dd >/dev/null
185 seconds = (now_us - G.begin_time_us) / 1000000.0;
186 bytes_sec = G.total_bytes / seconds;
187 fprintf(stderr, "%f seconds, %sB/s\n",
189 /* show fractional digit, use suffixes */
190 make_human_readable_str(bytes_sec, 1, 0)
195 static ssize_t full_write_or_warn(const void *buf, size_t len,
196 const char *const filename)
198 ssize_t n = full_write(ofd, buf, len);
200 bb_perror_msg("writing '%s'", filename);
204 static bool write_and_stats(const void *buf, size_t len, size_t obs,
205 const char *filename)
207 ssize_t n = full_write_or_warn(buf, len, filename);
210 #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
213 if ((size_t)n == obs) {
217 if ((size_t)n == len) {
225 # define XATOU_SFX xatoull_sfx
227 # define XATOU_SFX xatoul_sfx
230 #if ENABLE_FEATURE_DD_IBS_OBS
231 static int parse_comma_flags(char *val, const char *words, const char *error_in)
237 /* find ',', replace them with NUL so we can use val for
238 * index_in_strings() without copying.
239 * We rely on val being non-null, else strchr would fault.
241 arg = strchr(val, ',');
244 n = index_in_strings(words, val);
246 bb_error_msg_and_die(bb_msg_invalid_arg_to, val, error_in);
248 if (!arg) /* no ',' left, so this was the last specifier */
250 *arg = ','; /* to preserve ps listing */
251 val = arg + 1; /* skip this keyword and ',' */
257 int dd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
258 int dd_main(int argc UNUSED_PARAM, char **argv)
260 static const char keywords[] ALIGN1 =
261 "bs\0""count\0""seek\0""skip\0""if\0""of\0"IF_FEATURE_DD_STATUS("status\0")
262 #if ENABLE_FEATURE_DD_IBS_OBS
263 "ibs\0""obs\0""conv\0""iflag\0""oflag\0"
266 #if ENABLE_FEATURE_DD_IBS_OBS
267 static const char conv_words[] ALIGN1 =
268 "notrunc\0""sync\0""noerror\0""fsync\0""swab\0";
269 static const char iflag_words[] ALIGN1 =
270 "skip_bytes\0""fullblock\0";
271 static const char oflag_words[] ALIGN1 =
272 "seek_bytes\0append\0";
274 #if ENABLE_FEATURE_DD_STATUS
275 static const char status_words[] ALIGN1 =
285 IF_FEATURE_DD_STATUS(OP_status,)
286 #if ENABLE_FEATURE_DD_IBS_OBS
292 /* Must be in the same order as FLAG_XXX! */
298 /* Unimplemented conv=XXX: */
299 //nocreat do not create the output file
300 //excl fail if the output file already exists
301 //fdatasync physically write output file data before finishing
302 //lcase change upper case to lower case
303 //ucase change lower case to upper case
304 //block pad newline-terminated records with spaces to cbs-size
305 //unblock replace trailing spaces in cbs-size records with newline
306 //ascii from EBCDIC to ASCII
307 //ebcdic from ASCII to EBCDIC
308 //ibm from ASCII to alternate EBCDIC
309 /* Partially implemented: */
310 //swab swap every pair of input bytes: will abort on non-even reads
316 smallint exitcode = EXIT_FAILURE;
320 #if ENABLE_FEATURE_DD_IBS_OBS
327 /* These are all zeroed at once! */
329 IF_FEATURE_DD_IBS_OBS(size_t ocount;)
330 ssize_t prev_read_size; /* for detecting swab failure */
333 const char *infile, *outfile;
335 #define ocount (Z.ocount )
336 #define prev_read_size (Z.prev_read_size)
337 #define count (Z.count )
338 #define seek (Z.seek )
339 #define skip (Z.skip )
340 #define infile (Z.infile )
341 #define outfile (Z.outfile)
343 memset(&Z, 0, sizeof(Z));
345 //fflush_all(); - is this needed because of NOEXEC?
347 for (i = 1; argv[i]; i++) {
353 /* "dd --". NB: coreutils 6.9 will complain if they see
354 * more than one of them. We wouldn't. */
355 if (arg[0] == '-' && arg[1] == '-' && arg[2] == '\0')
358 val = strchr(arg, '=');
362 what = index_in_strings(keywords, arg);
365 /* *val = '='; - to preserve ps listing? */
367 #if ENABLE_FEATURE_DD_IBS_OBS
368 if (what == OP_ibs) {
369 /* Must fit into positive ssize_t */
370 ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, cwbkMG_suffixes);
373 if (what == OP_obs) {
374 obs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, cwbkMG_suffixes);
377 if (what == OP_conv) {
378 G.flags |= parse_comma_flags(val, conv_words, "conv");
381 if (what == OP_iflag) {
382 G.flags |= parse_comma_flags(val, iflag_words, "iflag") << FLAG_IFLAG_SHIFT;
385 if (what == OP_oflag) {
386 G.flags |= parse_comma_flags(val, oflag_words, "oflag") << FLAG_OFLAG_SHIFT;
391 ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, cwbkMG_suffixes);
395 /* These can be large: */
396 if (what == OP_count) {
397 G.flags |= FLAG_COUNT;
398 count = XATOU_SFX(val, cwbkMG_suffixes);
401 if (what == OP_seek) {
402 seek = XATOU_SFX(val, cwbkMG_suffixes);
405 if (what == OP_skip) {
406 skip = XATOU_SFX(val, cwbkMG_suffixes);
417 #if ENABLE_FEATURE_DD_STATUS
418 if (what == OP_status) {
420 n = index_in_strings(status_words, val);
422 bb_error_msg_and_die(bb_msg_invalid_arg_to, val, "status");
423 G.flags |= FLAG_STATUS_NONE << n;
427 } /* end of "for (argv[i])" */
429 //XXX:FIXME for huge ibs or obs, malloc'ing them isn't the brightest idea ever
432 #if ENABLE_FEATURE_DD_IBS_OBS
434 G.flags |= FLAG_TWOBUFS;
439 #if ENABLE_FEATURE_DD_SIGNAL_HANDLING
440 signal_SA_RESTART_empty_mask(SIGUSR1, dd_output_status);
442 #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
443 G.begin_time_us = monotonic_us();
447 xmove_fd(xopen(infile, O_RDONLY), ifd);
449 infile = bb_msg_standard_input;
452 int oflag = O_WRONLY | O_CREAT;
454 if (!seek && !(G.flags & FLAG_NOTRUNC))
456 if (G.flags & FLAG_APPEND)
459 xmove_fd(xopen(outfile, oflag), ofd);
461 if (seek && !(G.flags & FLAG_NOTRUNC)) {
462 size_t blocksz = (G.flags & FLAG_SEEK_BYTES) ? 1 : obs;
463 if (ftruncate(ofd, seek * blocksz) < 0) {
466 if (fstat(ofd, &st) < 0
467 || S_ISREG(st.st_mode)
468 || S_ISDIR(st.st_mode)
475 outfile = bb_msg_standard_output;
478 size_t blocksz = (G.flags & FLAG_SKIP_BYTES) ? 1 : ibs;
479 if (lseek(ifd, skip * blocksz, SEEK_CUR) < 0) {
482 #if ENABLE_FEATURE_DD_IBS_OBS
483 if (G.flags & FLAG_FULLBLOCK)
484 n = full_read(ifd, ibuf, blocksz);
487 n = safe_read(ifd, ibuf, blocksz);
492 } while (--skip != 0);
496 size_t blocksz = (G.flags & FLAG_SEEK_BYTES) ? 1 : obs;
497 if (lseek(ofd, seek * blocksz, SEEK_CUR) < 0)
501 while (!(G.flags & FLAG_COUNT) || (G.in_full + G.in_part != count)) {
503 #if ENABLE_FEATURE_DD_IBS_OBS
504 if (G.flags & FLAG_FULLBLOCK)
505 n = full_read(ifd, ibuf, ibs);
508 n = safe_read(ifd, ibuf, ibs);
513 if (!(G.flags & FLAG_NOERROR))
515 bb_simple_perror_msg(infile);
516 /* GNU dd with conv=noerror skips over bad blocks */
517 xlseek(ifd, ibs, SEEK_CUR);
518 /* conv=noerror,sync writes NULs,
519 * conv=noerror just ignores input bad blocks */
522 if (G.flags & FLAG_SWAB) {
526 /* Our code allows only last read to be odd-sized */
527 if (prev_read_size & 1)
528 bb_error_msg_and_die("can't swab %lu byte buffer",
529 (unsigned long)prev_read_size);
532 /* If n is odd, last byte is not swapped:
533 * echo -n "qwe" | dd conv=swab
539 *p16 = bswap_16(*p16);
543 if ((size_t)n == ibs)
547 if (G.flags & FLAG_SYNC) {
548 memset(ibuf + n, 0, ibs - n);
552 #if ENABLE_FEATURE_DD_IBS_OBS
553 if (G.flags & FLAG_TWOBUFS) {
556 size_t d = obs - ocount;
559 memcpy(obuf + ocount, tmp, d);
564 if (write_and_stats(obuf, obs, obs, outfile))
572 if (write_and_stats(ibuf, n, obs, outfile))
577 if (G.flags & FLAG_FSYNC) {
582 #if ENABLE_FEATURE_DD_IBS_OBS
584 if (write_and_stats(obuf, ocount, obs, outfile))
588 if (close(ifd) < 0) {
590 bb_simple_perror_msg_and_die(infile);
593 if (close(ofd) < 0) {
595 bb_simple_perror_msg_and_die(outfile);
598 exitcode = EXIT_SUCCESS;
600 if (!ENABLE_FEATURE_DD_STATUS || !(G.flags & FLAG_STATUS_NONE))
603 if (ENABLE_FEATURE_CLEAN_UP) {
605 if (G.flags & FLAG_TWOBUFS)