• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* dd.c - program to convert and copy a file.
2  *
3  * Copyright 2013 Ashwini Kumar <ak.ashwini@gmail.com>
4  * Copyright 2013 Kyungwan Han <asura321@gmail.com>
5  *
6  * See  http://opengroup.org/onlinepubs/9699919799/utilities/dd.html
7 
8 USE_DD(NEWTOY(dd, 0, TOYFLAG_USR|TOYFLAG_BIN))
9 
10 config DD
11   bool "dd"
12   default n
13   help
14     usage: dd [if=FILE] [of=FILE] [ibs=N] [obs=N] [iflag=FLAGS] [oflag=FLAGS]
15             [bs=N] [count=N] [seek=N] [skip=N]
16             [conv=notrunc|noerror|sync|fsync] [status=noxfer|none]
17 
18     Copy/convert files.
19 
20     if=FILE		Read from FILE instead of stdin
21     of=FILE		Write to FILE instead of stdout
22     bs=N		Read and write N bytes at a time
23     ibs=N		Input block size
24     obs=N		Output block size
25     count=N		Copy only N input blocks
26     skip=N		Skip N input blocks
27     seek=N		Skip N output blocks
28     iflag=FLAGS	Set input flags
29     oflag=FLAGS	Set output flags
30     conv=notrunc	Don't truncate output file
31     conv=noerror	Continue after read errors
32     conv=sync	Pad blocks with zeros
33     conv=fsync	Physically write data out before finishing
34     status=noxfer	Don't show transfer rate
35     status=none	Don't show transfer rate or records in/out
36 
37     FLAGS is a comma-separated list of:
38 
39     count_bytes	(iflag) interpret count=N in bytes, not blocks
40     seek_bytes	(oflag) interpret seek=N in bytes, not blocks
41     skip_bytes	(iflag) interpret skip=N in bytes, not blocks
42 
43     Numbers may be suffixed by c (*1), w (*2), b (*512), kD (*1000), k (*1024),
44     MD (*1000*1000), M (*1024*1024), GD (*1000*1000*1000) or G (*1024*1024*1024).
45 */
46 
47 #define FOR_dd
48 #include "toys.h"
49 
50 GLOBALS(
51   int show_xfer, show_records;
52   unsigned long long bytes, c_count, in_full, in_part, out_full, out_part;
53   struct timeval start;
54   struct {
55     char *name;
56     int fd;
57     unsigned char *buff, *bp;
58     long sz, count;
59     unsigned long long offset;
60   } in, out;
61   unsigned conv, iflag, oflag;
62 );
63 
64 struct dd_flag {
65   char *name;
66 };
67 
68 static const struct dd_flag dd_conv[] = TAGGED_ARRAY(DD_conv,
69   {"fsync"}, {"noerror"}, {"notrunc"}, {"sync"},
70 );
71 
72 static const struct dd_flag dd_iflag[] = TAGGED_ARRAY(DD_iflag,
73   {"count_bytes"}, {"skip_bytes"},
74 );
75 
76 static const struct dd_flag dd_oflag[] = TAGGED_ARRAY(DD_oflag,
77   {"seek_bytes"},
78 );
79 
status()80 static void status()
81 {
82   double seconds;
83   struct timeval now;
84 
85   gettimeofday(&now, NULL);
86   seconds = ((now.tv_sec * 1000000 + now.tv_usec) -
87       (TT.start.tv_sec * 1000000 + TT.start.tv_usec))/1000000.0;
88 
89   if (TT.show_records)
90     fprintf(stderr, "%llu+%llu records in\n%llu+%llu records out\n",
91             TT.in_full, TT.in_part, TT.out_full, TT.out_part);
92 
93   if (TT.show_xfer) {
94     human_readable(toybuf, TT.bytes, HR_SPACE|HR_B);
95     fprintf(stderr, "%llu bytes (%s) copied, ", TT.bytes, toybuf);
96     human_readable(toybuf, TT.bytes/seconds, HR_SPACE|HR_B);
97     fprintf(stderr, "%f s, %s/s\n", seconds, toybuf);
98   }
99 }
100 
dd_sigint(int sig)101 static void dd_sigint(int sig) {
102   status();
103   toys.exitval = sig|128;
104   xexit();
105 }
106 
write_out(int all)107 static void write_out(int all)
108 {
109   TT.out.bp = TT.out.buff;
110   while (TT.out.count) {
111     ssize_t nw = writeall(TT.out.fd, TT.out.bp, ((all)? TT.out.count : TT.out.sz));
112 
113     all = 0; //further writes will be on obs
114     if (nw <= 0) perror_exit("%s: write error", TT.out.name);
115     if (nw == TT.out.sz) TT.out_full++;
116     else TT.out_part++;
117     TT.out.count -= nw;
118     TT.out.bp += nw;
119     TT.bytes += nw;
120     if (TT.out.count < TT.out.sz) break;
121   }
122   if (TT.out.count) memmove(TT.out.buff, TT.out.bp, TT.out.count); //move remainder to front
123 }
124 
parse_flags(char * what,char * arg,const struct dd_flag * flags,int flag_count,unsigned * result)125 static void parse_flags(char *what, char *arg,
126     const struct dd_flag* flags, int flag_count, unsigned *result)
127 {
128   char *pre = xstrdup(arg);
129   int i;
130 
131   for (i=0; i<flag_count; ++i) {
132     while (comma_remove(pre, flags[i].name)) *result |= 1<<i;
133   }
134   if (*pre) error_exit("bad %s=%s", what, pre);
135   free(pre);
136 }
137 
dd_main()138 void dd_main()
139 {
140   char **args;
141   unsigned long long bs = 0;
142   int trunc = O_TRUNC;
143 
144   TT.show_xfer = TT.show_records = 1;
145   TT.c_count = ULLONG_MAX;
146 
147   TT.in.sz = TT.out.sz = 512; //default io block size
148   for (args = toys.optargs; *args; args++) {
149     char *arg = *args;
150 
151     if (strstart(&arg, "bs=")) bs = atolx_range(arg, 1, LONG_MAX);
152     else if (strstart(&arg, "ibs=")) TT.in.sz = atolx_range(arg, 1, LONG_MAX);
153     else if (strstart(&arg, "obs=")) TT.out.sz = atolx_range(arg, 1, LONG_MAX);
154     else if (strstart(&arg, "count="))
155       TT.c_count = atolx_range(arg, 0, LLONG_MAX);
156     else if (strstart(&arg, "if=")) TT.in.name = arg;
157     else if (strstart(&arg, "of=")) TT.out.name = arg;
158     else if (strstart(&arg, "seek="))
159       TT.out.offset = atolx_range(arg, 0, LLONG_MAX);
160     else if (strstart(&arg, "skip="))
161       TT.in.offset = atolx_range(arg, 0, LLONG_MAX);
162     else if (strstart(&arg, "status=")) {
163       if (!strcmp(arg, "noxfer")) TT.show_xfer = 0;
164       else if (!strcmp(arg, "none")) TT.show_xfer = TT.show_records = 0;
165       else error_exit("unknown status '%s'", arg);
166     } else if (strstart(&arg, "conv=")) {
167       parse_flags("conv", arg, dd_conv, ARRAY_LEN(dd_conv), &TT.conv);
168       fprintf(stderr, "conv=%x\n", TT.conv);
169     } else if (strstart(&arg, "iflag="))
170       parse_flags("iflag", arg, dd_iflag, ARRAY_LEN(dd_iflag), &TT.iflag);
171     else if (strstart(&arg, "oflag="))
172       parse_flags("oflag", arg, dd_oflag, ARRAY_LEN(dd_oflag), &TT.oflag);
173     else error_exit("bad arg %s", arg);
174   }
175   if (bs) TT.in.sz = TT.out.sz = bs;
176 
177   signal(SIGINT, dd_sigint);
178   signal(SIGUSR1, generic_signal);
179   gettimeofday(&TT.start, NULL);
180 
181   // For bs=, in/out is done as it is. so only in.sz is enough.
182   // With Single buffer there will be overflow in a read following partial read.
183   TT.in.buff = TT.out.buff = xmalloc(TT.in.sz + (bs ? 0 : TT.out.sz));
184   TT.in.bp = TT.out.bp = TT.in.buff;
185 
186   if (!TT.in.name) TT.in.name = "stdin";
187   else TT.in.fd = xopenro(TT.in.name);
188 
189   if (TT.conv & _DD_conv_notrunc) trunc = 0;
190 
191   if (!TT.out.name) {
192     TT.out.name = "stdout";
193     TT.out.fd = 1;
194   } else TT.out.fd = xcreate(TT.out.name,
195     O_WRONLY|O_CREAT|(trunc*!TT.out.offset), 0666);
196 
197   // Implement skip=
198   if (TT.in.offset) {
199     off_t off = TT.in.offset;
200 
201     if (!(TT.iflag & _DD_iflag_skip_bytes)) off *= TT.in.sz;
202     if (lseek(TT.in.fd, off, SEEK_CUR) < 0) {
203       while (off > 0) {
204         int chunk = off < TT.in.sz ? off : TT.in.sz;
205         ssize_t n = read(TT.in.fd, TT.in.bp, chunk);
206 
207         if (n < 0) {
208           perror_msg("%s", TT.in.name);
209           if (TT.conv & _DD_conv_noerror) status();
210           else return;
211         } else if (!n) {
212           xprintf("%s: Can't skip\n", TT.in.name);
213           return;
214         }
215         off -= n;
216       }
217     }
218   }
219 
220   // Implement seek= and truncate as necessary. We handled position zero
221   // truncate with O_TRUNC on open, so output to /dev/null and such doesn't
222   // error.
223   bs = TT.out.offset;
224   if (!(TT.oflag & _DD_oflag_seek_bytes)) bs *= TT.out.sz;
225   if (bs) {
226     xlseek(TT.out.fd, bs, SEEK_CUR);
227     if (trunc && ftruncate(TT.out.fd, bs)) perror_exit("ftruncate");
228   }
229 
230   unsigned long long bytes_left = TT.c_count;
231   if (TT.c_count != ULLONG_MAX && !(TT.iflag & _DD_iflag_count_bytes)) {
232     bytes_left *= TT.in.sz;
233   }
234   while (bytes_left) {
235     int chunk = bytes_left < TT.in.sz ? bytes_left : TT.in.sz;
236     ssize_t n;
237 
238     // Show progress and exit on SIGINT or just continue on SIGUSR1.
239     if (toys.signal) {
240       status();
241       if (toys.signal==SIGINT) exit_signal(toys.signal);
242       toys.signal = 0;
243     }
244 
245     TT.in.bp = TT.in.buff + TT.in.count;
246     if (TT.conv & _DD_conv_sync) memset(TT.in.bp, 0, TT.in.sz);
247     if (!(n = read(TT.in.fd, TT.in.bp, chunk))) break;
248     if (n < 0) {
249       if (errno == EINTR) continue;
250       //read error case.
251       perror_msg("%s: read error", TT.in.name);
252       if (!(TT.conv & _DD_conv_noerror)) exit(1);
253       status();
254       xlseek(TT.in.fd, TT.in.sz, SEEK_CUR);
255       if (!(TT.conv & _DD_conv_sync)) continue;
256       // if SYNC, then treat as full block of nuls
257       n = TT.in.sz;
258     }
259     if (n == TT.in.sz) {
260       TT.in_full++;
261       TT.in.count += n;
262     } else {
263       TT.in_part++;
264       if (TT.conv & _DD_conv_sync) TT.in.count += TT.in.sz;
265       else TT.in.count += n;
266     }
267     bytes_left -= n;
268 
269     TT.out.count = TT.in.count;
270     if (bs) {
271       write_out(1);
272       TT.in.count = 0;
273       continue;
274     }
275 
276     if (TT.in.count >= TT.out.sz) {
277       write_out(0);
278       TT.in.count = TT.out.count;
279     }
280   }
281   if (TT.out.count) write_out(1); //write any remaining input blocks
282   if ((TT.conv & _DD_conv_fsync) && fsync(TT.out.fd)<0)
283     perror_exit("%s: fsync", TT.out.name);
284 
285   close(TT.in.fd);
286   close(TT.out.fd);
287   if (TT.in.buff) free(TT.in.buff);
288 
289   status();
290 }
291