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