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