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