30249707421f1268148b3503f777ef2be18f88c1
[oweals/busybox.git] / coreutils / dd.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini dd implementation for busybox
4  *
5  *
6  * Copyright (C) 2000,2001  Matt Kraai
7  *
8  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
9  */
10
11 //config:config DD
12 //config:       bool "dd"
13 //config:       default y
14 //config:       help
15 //config:         dd copies a file (from standard input to standard output,
16 //config:         by default) using specific input and output blocksizes,
17 //config:         while optionally performing conversions on it.
18 //config:
19 //config:config FEATURE_DD_SIGNAL_HANDLING
20 //config:       bool "Enable signal handling for status reporting"
21 //config:       default y
22 //config:       depends on DD
23 //config:       help
24 //config:         Sending a SIGUSR1 signal to a running `dd' process makes it
25 //config:         print to standard error the number of records read and written
26 //config:         so far, then to resume copying.
27 //config:
28 //config:         $ dd if=/dev/zero of=/dev/null &
29 //config:         $ pid=$!; kill -USR1 $pid; sleep 1; kill $pid
30 //config:         10899206+0 records in
31 //config:         10899206+0 records out
32 //config:
33 //config:config FEATURE_DD_THIRD_STATUS_LINE
34 //config:       bool "Enable the third status line upon signal"
35 //config:       default y
36 //config:       depends on DD && FEATURE_DD_SIGNAL_HANDLING
37 //config:       help
38 //config:         Displays a coreutils-like third status line with transferred bytes,
39 //config:         elapsed time and speed.
40 //config:
41 //config:config FEATURE_DD_IBS_OBS
42 //config:       bool "Enable ibs, obs and conv options"
43 //config:       default y
44 //config:       depends on DD
45 //config:       help
46 //config:         Enables support for writing a certain number of bytes in and out,
47 //config:         at a time, and performing conversions on the data stream.
48 //config:
49 //config:config FEATURE_DD_STATUS
50 //config:       bool "Enable status display options"
51 //config:       default y
52 //config:       depends on DD
53 //config:       help
54 //config:         Enables support for status=noxfer/none option.
55
56 //usage:#define dd_trivial_usage
57 //usage:       "[if=FILE] [of=FILE] " IF_FEATURE_DD_IBS_OBS("[ibs=N] [obs=N] ") "[bs=N] [count=N] [skip=N]\n"
58 //usage:       "        [seek=N]" IF_FEATURE_DD_IBS_OBS(" [conv=notrunc|noerror|sync|fsync]")
59 //usage:#define dd_full_usage "\n\n"
60 //usage:       "Copy a file with converting and formatting\n"
61 //usage:     "\n        if=FILE         Read from FILE instead of stdin"
62 //usage:     "\n        of=FILE         Write to FILE instead of stdout"
63 //usage:     "\n        bs=N            Read and write N bytes at a time"
64 //usage:        IF_FEATURE_DD_IBS_OBS(
65 //usage:     "\n        ibs=N           Read N bytes at a time"
66 //usage:        )
67 //usage:        IF_FEATURE_DD_IBS_OBS(
68 //usage:     "\n        obs=N           Write N bytes at a time"
69 //usage:        )
70 //usage:     "\n        count=N         Copy only N input blocks"
71 //usage:     "\n        skip=N          Skip N input blocks"
72 //usage:     "\n        seek=N          Skip N output blocks"
73 //usage:        IF_FEATURE_DD_IBS_OBS(
74 //usage:     "\n        conv=notrunc    Don't truncate output file"
75 //usage:     "\n        conv=noerror    Continue after read errors"
76 //usage:     "\n        conv=sync       Pad blocks with zeros"
77 //usage:     "\n        conv=fsync      Physically write data out before finishing"
78 //usage:     "\n        conv=swab       Swap every pair of bytes"
79 //usage:        )
80 //usage:        IF_FEATURE_DD_STATUS(
81 //usage:     "\n        status=noxfer   Suppress rate output"
82 //usage:     "\n        status=none     Suppress all output"
83 //usage:        )
84 //usage:     "\n"
85 //usage:     "\nN may be suffixed by c (1), w (2), b (512), kB (1000), k (1024), MB, M, GB, G"
86 //usage:
87 //usage:#define dd_example_usage
88 //usage:       "$ dd if=/dev/zero of=/dev/ram1 bs=1M count=4\n"
89 //usage:       "4+0 records in\n"
90 //usage:       "4+0 records out\n"
91
92 #include "libbb.h"
93
94 /* This is a NOEXEC applet. Be very careful! */
95
96
97 enum {
98         ifd = STDIN_FILENO,
99         ofd = STDOUT_FILENO,
100 };
101
102 static const struct suffix_mult dd_suffixes[] = {
103         { "c", 1 },
104         { "w", 2 },
105         { "b", 512 },
106         { "kB", 1000 },
107         { "kD", 1000 },
108         { "k", 1024 },
109         { "K", 1024 },  /* compat with coreutils dd (it also accepts KB and KD, TODO?) */
110         { "MB", 1000000 },
111         { "MD", 1000000 },
112         { "M", 1024*1024 },
113         { "GB", 1000000000 },
114         { "GD", 1000000000 },
115         { "G", 1024*1024*1024 },
116         /* "D" suffix for decimal is not in coreutils manpage, looks like it's deprecated */
117         /* coreutils also understands TPEZY suffixes for tera- and so on, with B suffix for decimal */
118         { "", 0 }
119 };
120
121 struct globals {
122         off_t out_full, out_part, in_full, in_part;
123 #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
124         unsigned long long total_bytes;
125         unsigned long long begin_time_us;
126 #endif
127         int flags;
128 } FIX_ALIASING;
129 #define G (*(struct globals*)&bb_common_bufsiz1)
130 #define INIT_G() do { \
131         /* we have to zero it out because of NOEXEC */ \
132         memset(&G, 0, sizeof(G)); \
133 } while (0)
134
135 enum {
136         /* Must be in the same order as OP_conv_XXX! */
137         /* (see "flags |= (1 << what)" below) */
138         FLAG_NOTRUNC = (1 << 0) * ENABLE_FEATURE_DD_IBS_OBS,
139         FLAG_SYNC    = (1 << 1) * ENABLE_FEATURE_DD_IBS_OBS,
140         FLAG_NOERROR = (1 << 2) * ENABLE_FEATURE_DD_IBS_OBS,
141         FLAG_FSYNC   = (1 << 3) * ENABLE_FEATURE_DD_IBS_OBS,
142         FLAG_SWAB    = (1 << 4) * ENABLE_FEATURE_DD_IBS_OBS,
143         /* end of conv flags */
144         FLAG_TWOBUFS = (1 << 5) * ENABLE_FEATURE_DD_IBS_OBS,
145         FLAG_COUNT   = 1 << 6,
146         FLAG_STATUS  = 1 << 7,
147         FLAG_STATUS_NONE = 1 << 7,
148         FLAG_STATUS_NOXFER = 1 << 8,
149 };
150
151 static void dd_output_status(int UNUSED_PARAM cur_signal)
152 {
153 #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
154         double seconds;
155         unsigned long long bytes_sec;
156         unsigned long long now_us = monotonic_us(); /* before fprintf */
157 #endif
158
159         /* Deliberately using %u, not %d */
160         fprintf(stderr, "%"OFF_FMT"u+%"OFF_FMT"u records in\n"
161                         "%"OFF_FMT"u+%"OFF_FMT"u records out\n",
162                         G.in_full, G.in_part,
163                         G.out_full, G.out_part);
164
165 #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
166 # if ENABLE_FEATURE_DD_STATUS
167         if (G.flags & FLAG_STATUS_NOXFER) /* status=noxfer active? */
168                 return;
169         //TODO: should status=none make dd stop reacting to USR1 entirely?
170         //So far we react to it (we print the stats),
171         //status=none only suppresses final, non-USR1 generated status message.
172 # endif
173         fprintf(stderr, "%llu bytes (%sB) copied, ",
174                         G.total_bytes,
175                         /* show fractional digit, use suffixes */
176                         make_human_readable_str(G.total_bytes, 1, 0)
177         );
178         /* Corner cases:
179          * ./busybox dd </dev/null >/dev/null
180          * ./busybox dd bs=1M count=2000 </dev/zero >/dev/null
181          * (echo DONE) | ./busybox dd >/dev/null
182          * (sleep 1; echo DONE) | ./busybox dd >/dev/null
183          */
184         seconds = (now_us - G.begin_time_us) / 1000000.0;
185         bytes_sec = G.total_bytes / seconds;
186         fprintf(stderr, "%f seconds, %sB/s\n",
187                         seconds,
188                         /* show fractional digit, use suffixes */
189                         make_human_readable_str(bytes_sec, 1, 0)
190         );
191 #endif
192 }
193
194 static ssize_t full_write_or_warn(const void *buf, size_t len,
195         const char *const filename)
196 {
197         ssize_t n = full_write(ofd, buf, len);
198         if (n < 0)
199                 bb_perror_msg("writing '%s'", filename);
200         return n;
201 }
202
203 static bool write_and_stats(const void *buf, size_t len, size_t obs,
204         const char *filename)
205 {
206         ssize_t n = full_write_or_warn(buf, len, filename);
207         if (n < 0)
208                 return 1;
209         if ((size_t)n == obs)
210                 G.out_full++;
211         else if (n) /* > 0 */
212                 G.out_part++;
213 #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
214         G.total_bytes += n;
215 #endif
216         return 0;
217 }
218
219 #if ENABLE_LFS
220 # define XATOU_SFX xatoull_sfx
221 #else
222 # define XATOU_SFX xatoul_sfx
223 #endif
224
225 int dd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
226 int dd_main(int argc UNUSED_PARAM, char **argv)
227 {
228         static const char keywords[] ALIGN1 =
229                 "bs\0""count\0""seek\0""skip\0""if\0""of\0"IF_FEATURE_DD_STATUS("status\0")
230 #if ENABLE_FEATURE_DD_IBS_OBS
231                 "ibs\0""obs\0""conv\0"
232 #endif
233                 ;
234 #if ENABLE_FEATURE_DD_IBS_OBS
235         static const char conv_words[] ALIGN1 =
236                 "notrunc\0""sync\0""noerror\0""fsync\0""swab\0";
237 #endif
238 #if ENABLE_FEATURE_DD_STATUS
239         static const char status_words[] ALIGN1 =
240                 "none\0""noxfer\0";
241 #endif
242         enum {
243                 OP_bs = 0,
244                 OP_count,
245                 OP_seek,
246                 OP_skip,
247                 OP_if,
248                 OP_of,
249                 IF_FEATURE_DD_STATUS(OP_status,)
250 #if ENABLE_FEATURE_DD_IBS_OBS
251                 OP_ibs,
252                 OP_obs,
253                 OP_conv,
254                 /* Must be in the same order as FLAG_XXX! */
255                 OP_conv_notrunc = 0,
256                 OP_conv_sync,
257                 OP_conv_noerror,
258                 OP_conv_fsync,
259                 OP_conv_swab,
260         /* Unimplemented conv=XXX: */
261         //nocreat       do not create the output file
262         //excl          fail if the output file already exists
263         //fdatasync     physically write output file data before finishing
264         //lcase         change upper case to lower case
265         //ucase         change lower case to upper case
266         //block         pad newline-terminated records with spaces to cbs-size
267         //unblock       replace trailing spaces in cbs-size records with newline
268         //ascii         from EBCDIC to ASCII
269         //ebcdic        from ASCII to EBCDIC
270         //ibm           from ASCII to alternate EBCDIC
271         /* Partially implemented: */
272         //swab          swap every pair of input bytes: will abort on non-even reads
273 #endif
274         };
275         smallint exitcode = EXIT_FAILURE;
276         int i;
277         size_t ibs = 512;
278         char *ibuf;
279 #if ENABLE_FEATURE_DD_IBS_OBS
280         size_t obs = 512;
281         char *obuf;
282 #else
283 # define obs  ibs
284 # define obuf ibuf
285 #endif
286         /* These are all zeroed at once! */
287         struct {
288                 size_t oc;
289                 ssize_t prev_read_size; /* for detecting swab failure */
290                 off_t count;
291                 off_t seek, skip;
292                 const char *infile, *outfile;
293         } Z;
294 #define oc      (Z.oc     )
295 #define prev_read_size (Z.prev_read_size)
296 #define count   (Z.count  )
297 #define seek    (Z.seek   )
298 #define skip    (Z.skip   )
299 #define infile  (Z.infile )
300 #define outfile (Z.outfile)
301
302         memset(&Z, 0, sizeof(Z));
303         INIT_G();
304         //fflush_all(); - is this needed because of NOEXEC?
305
306         for (i = 1; argv[i]; i++) {
307                 int what;
308                 char *val;
309                 char *arg = argv[i];
310
311 #if ENABLE_DESKTOP
312                 /* "dd --". NB: coreutils 6.9 will complain if they see
313                  * more than one of them. We wouldn't. */
314                 if (arg[0] == '-' && arg[1] == '-' && arg[2] == '\0')
315                         continue;
316 #endif
317                 val = strchr(arg, '=');
318                 if (val == NULL)
319                         bb_show_usage();
320                 *val = '\0';
321                 what = index_in_strings(keywords, arg);
322                 if (what < 0)
323                         bb_show_usage();
324                 /* *val = '='; - to preserve ps listing? */
325                 val++;
326 #if ENABLE_FEATURE_DD_IBS_OBS
327                 if (what == OP_ibs) {
328                         /* Must fit into positive ssize_t */
329                         ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes);
330                         /*continue;*/
331                 }
332                 if (what == OP_obs) {
333                         obs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes);
334                         /*continue;*/
335                 }
336                 if (what == OP_conv) {
337                         while (1) {
338                                 int n;
339                                 /* find ',', replace them with NUL so we can use val for
340                                  * index_in_strings() without copying.
341                                  * We rely on val being non-null, else strchr would fault.
342                                  */
343                                 arg = strchr(val, ',');
344                                 if (arg)
345                                         *arg = '\0';
346                                 n = index_in_strings(conv_words, val);
347                                 if (n < 0)
348                                         bb_error_msg_and_die(bb_msg_invalid_arg, val, "conv");
349                                 G.flags |= (1 << n);
350                                 if (!arg) /* no ',' left, so this was the last specifier */
351                                         break;
352                                 /* *arg = ','; - to preserve ps listing? */
353                                 val = arg + 1; /* skip this keyword and ',' */
354                         }
355                         /*continue;*/
356                 }
357 #endif
358                 if (what == OP_bs) {
359                         ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes);
360                         obs = ibs;
361                         /*continue;*/
362                 }
363                 /* These can be large: */
364                 if (what == OP_count) {
365                         G.flags |= FLAG_COUNT;
366                         count = XATOU_SFX(val, dd_suffixes);
367                         /*continue;*/
368                 }
369                 if (what == OP_seek) {
370                         seek = XATOU_SFX(val, dd_suffixes);
371                         /*continue;*/
372                 }
373                 if (what == OP_skip) {
374                         skip = XATOU_SFX(val, dd_suffixes);
375                         /*continue;*/
376                 }
377                 if (what == OP_if) {
378                         infile = val;
379                         /*continue;*/
380                 }
381                 if (what == OP_of) {
382                         outfile = val;
383                         /*continue;*/
384                 }
385 #if ENABLE_FEATURE_DD_STATUS
386                 if (what == OP_status) {
387                         int n;
388                         n = index_in_strings(status_words, val);
389                         if (n < 0)
390                                 bb_error_msg_and_die(bb_msg_invalid_arg, val, "status");
391                         G.flags |= FLAG_STATUS << n;
392                         /*continue;*/
393                 }
394 #endif
395         } /* end of "for (argv[i])" */
396
397 //XXX:FIXME for huge ibs or obs, malloc'ing them isn't the brightest idea ever
398         ibuf = xmalloc(ibs);
399         obuf = ibuf;
400 #if ENABLE_FEATURE_DD_IBS_OBS
401         if (ibs != obs) {
402                 G.flags |= FLAG_TWOBUFS;
403                 obuf = xmalloc(obs);
404         }
405 #endif
406
407 #if ENABLE_FEATURE_DD_SIGNAL_HANDLING
408         signal_SA_RESTART_empty_mask(SIGUSR1, dd_output_status);
409 #endif
410 #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
411         G.begin_time_us = monotonic_us();
412 #endif
413
414         if (infile) {
415                 xmove_fd(xopen(infile, O_RDONLY), ifd);
416         } else {
417                 infile = bb_msg_standard_input;
418         }
419         if (outfile) {
420                 int oflag = O_WRONLY | O_CREAT;
421
422                 if (!seek && !(G.flags & FLAG_NOTRUNC))
423                         oflag |= O_TRUNC;
424
425                 xmove_fd(xopen(outfile, oflag), ofd);
426
427                 if (seek && !(G.flags & FLAG_NOTRUNC)) {
428                         if (ftruncate(ofd, seek * obs) < 0) {
429                                 struct stat st;
430
431                                 if (fstat(ofd, &st) < 0
432                                  || S_ISREG(st.st_mode)
433                                  || S_ISDIR(st.st_mode)
434                                 ) {
435                                         goto die_outfile;
436                                 }
437                         }
438                 }
439         } else {
440                 outfile = bb_msg_standard_output;
441         }
442         if (skip) {
443                 if (lseek(ifd, skip * ibs, SEEK_CUR) < 0) {
444                         do {
445                                 ssize_t n = safe_read(ifd, ibuf, ibs);
446                                 if (n < 0)
447                                         goto die_infile;
448                                 if (n == 0)
449                                         break;
450                         } while (--skip != 0);
451                 }
452         }
453         if (seek) {
454                 if (lseek(ofd, seek * obs, SEEK_CUR) < 0)
455                         goto die_outfile;
456         }
457
458         while (!(G.flags & FLAG_COUNT) || (G.in_full + G.in_part != count)) {
459                 ssize_t n;
460
461                 n = safe_read(ifd, ibuf, ibs);
462                 if (n == 0)
463                         break;
464                 if (n < 0) {
465                         /* "Bad block" */
466                         if (!(G.flags & FLAG_NOERROR))
467                                 goto die_infile;
468                         bb_simple_perror_msg(infile);
469                         /* GNU dd with conv=noerror skips over bad blocks */
470                         xlseek(ifd, ibs, SEEK_CUR);
471                         /* conv=noerror,sync writes NULs,
472                          * conv=noerror just ignores input bad blocks */
473                         n = 0;
474                 }
475                 if (G.flags & FLAG_SWAB) {
476                         uint16_t *p16;
477                         ssize_t n2;
478
479                         /* Our code allows only last read to be odd-sized */
480                         if (prev_read_size & 1)
481                                 bb_error_msg_and_die("can't swab %lu byte buffer",
482                                                 (unsigned long)prev_read_size);
483                         prev_read_size = n;
484
485                         /* If n is odd, last byte is not swapped:
486                          *  echo -n "qwe" | dd conv=swab
487                          * prints "wqe".
488                          */
489                         p16 = (void*) ibuf;
490                         n2 = (n >> 1);
491                         while (--n2 >= 0) {
492                                 *p16 = bswap_16(*p16);
493                                 p16++;
494                         }
495                 }
496                 if ((size_t)n == ibs)
497                         G.in_full++;
498                 else {
499                         G.in_part++;
500                         if (G.flags & FLAG_SYNC) {
501                                 memset(ibuf + n, 0, ibs - n);
502                                 n = ibs;
503                         }
504                 }
505                 if (G.flags & FLAG_TWOBUFS) {
506                         char *tmp = ibuf;
507                         while (n) {
508                                 size_t d = obs - oc;
509
510                                 if (d > (size_t)n)
511                                         d = n;
512                                 memcpy(obuf + oc, tmp, d);
513                                 n -= d;
514                                 tmp += d;
515                                 oc += d;
516                                 if (oc == obs) {
517                                         if (write_and_stats(obuf, obs, obs, outfile))
518                                                 goto out_status;
519                                         oc = 0;
520                                 }
521                         }
522                 } else {
523                         if (write_and_stats(ibuf, n, obs, outfile))
524                                 goto out_status;
525                 }
526
527                 if (G.flags & FLAG_FSYNC) {
528                         if (fsync(ofd) < 0)
529                                 goto die_outfile;
530                 }
531         }
532
533         if (ENABLE_FEATURE_DD_IBS_OBS && oc) {
534                 if (write_and_stats(obuf, oc, obs, outfile))
535                         goto out_status;
536         }
537         if (close(ifd) < 0) {
538  die_infile:
539                 bb_simple_perror_msg_and_die(infile);
540         }
541
542         if (close(ofd) < 0) {
543  die_outfile:
544                 bb_simple_perror_msg_and_die(outfile);
545         }
546
547         exitcode = EXIT_SUCCESS;
548  out_status:
549         if (!ENABLE_FEATURE_DD_STATUS || !(G.flags & FLAG_STATUS_NONE))
550                 dd_output_status(0);
551
552         if (ENABLE_FEATURE_CLEAN_UP) {
553                 free(obuf);
554                 if (G.flags & FLAG_TWOBUFS)
555                         free(ibuf);
556         }
557
558         return exitcode;
559 }