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 #define BS_MAX_SIZE INT32_MAX
50 
51 GLOBALS(
52   int show_xfer, show_records;
53   unsigned long long bytes, c_count, in_full, in_part, out_full, out_part;
54   struct timeval start;
55   struct {
56     char *name;
57     int fd;
58     unsigned char *buff, *bp;
59     long sz, count;
60     unsigned long long offset;
61   } in, out;
62   unsigned conv, iflag, oflag;
63 );
64 
65 struct dd_flag {
66   char *name;
67 };
68 
69 static const struct dd_flag dd_conv[] = TAGGED_ARRAY(DD_conv,
70   {"fsync"}, {"noerror"}, {"notrunc"}, {"sync"},
71 );
72 
73 static const struct dd_flag dd_iflag[] = TAGGED_ARRAY(DD_iflag,
74   {"count_bytes"}, {"skip_bytes"},
75 );
76 
77 static const struct dd_flag dd_oflag[] = TAGGED_ARRAY(DD_oflag,
78   {"seek_bytes"},
79 );
80 
statusnull81 static void status()
82 {
83   double seconds;
84   struct timeval now;
85 
86   gettimeofday(&now, NULL);
87   seconds = ((now.tv_sec * 1000000 + now.tv_usec) -
88       (TT.start.tv_sec * 1000000 + TT.start.tv_usec))/1000000.0;
89 
90   if (TT.show_records)
91     fprintf(stderr, "%llu+%llu records in\n%llu+%llu records out\n",
92             TT.in_full, TT.in_part, TT.out_full, TT.out_part);
93 
94   if (TT.show_xfer) {
95     human_readable(toybuf, TT.bytes, HR_SPACE|HR_B);
96     fprintf(stderr, "%llu bytes (%s) copied, ", TT.bytes, toybuf);
97     human_readable(toybuf, TT.bytes/seconds, HR_SPACE|HR_B);
98     fprintf(stderr, "%f s, %s/s\n", seconds, toybuf);
99   }
100 }
101 
dd_sigint(int sig)102 static void dd_sigint(int sig) {
103   status();
104   toys.exitval = sig|128;
105   xexit();
106 }
107 
write_out(int all)108 static void write_out(int all)
109 {
110   TT.out.bp = TT.out.buff;
111   while (TT.out.count) {
112     ssize_t nw = writeall(TT.out.fd, TT.out.bp, ((all)? TT.out.count : TT.out.sz));
113 
114     all = 0; //further writes will be on obs
115     if (nw <= 0) perror_exit("%s: write error", TT.out.name);
116     if (nw == TT.out.sz) TT.out_full++;
117     else TT.out_part++;
118     TT.out.count -= nw;
119     TT.out.bp += nw;
120     TT.bytes += nw;
121     if (TT.out.count < TT.out.sz) break;
122   }
123   if (TT.out.count) memmove(TT.out.buff, TT.out.bp, TT.out.count); //move remainder to front
124 }
125 
parse_flags(char *what, char *arg, const struct dd_flag* flags, int flag_count, unsigned *result)126 static void parse_flags(char *what, char *arg,
127     const struct dd_flag* flags, int flag_count, unsigned *result)
128 {
129   char *pre = xstrdup(arg);
130   int i;
131 
132   for (i=0; i<flag_count; ++i) {
133     while (comma_remove(pre, flags[i].name)) *result |= 1<<i;
134   }
135   if (*pre) error_exit("bad %s=%s", what, pre);
136   free(pre);
137 }
138 
dd_mainnull139 void dd_main()
140 {
141   char **args;
142   unsigned long long bs = 0;
143   int trunc = O_TRUNC;
144 
145   TT.show_xfer = TT.show_records = 1;
146   TT.c_count = ULLONG_MAX;
147 
148   TT.in.sz = TT.out.sz = 512; //default io block size
149   for (args = toys.optargs; *args; args++) {
150     char *arg = *args;
151 
152     // set BS_MAX_SIZE to 1073741824(1G) to avoid malloc too much space later.
153     if (strstart(&arg, "bs=")) bs = atolx_range(arg, 1, BS_MAX_SIZE);
154     else if (strstart(&arg, "ibs=")) TT.in.sz = atolx_range(arg, 1, BS_MAX_SIZE);
155     else if (strstart(&arg, "obs=")) TT.out.sz = atolx_range(arg, 1, BS_MAX_SIZE);
156     else if (strstart(&arg, "count="))
157       TT.c_count = atolx_range(arg, 0, LLONG_MAX);
158     else if (strstart(&arg, "if=")) TT.in.name = arg;
159     else if (strstart(&arg, "of=")) TT.out.name = arg;
160     else if (strstart(&arg, "seek="))
161       TT.out.offset = atolx_range(arg, 0, LLONG_MAX);
162     else if (strstart(&arg, "skip="))
163       TT.in.offset = atolx_range(arg, 0, LLONG_MAX);
164     else if (strstart(&arg, "status=")) {
165       if (!strcmp(arg, "noxfer")) TT.show_xfer = 0;
166       else if (!strcmp(arg, "none")) TT.show_xfer = TT.show_records = 0;
167       else error_exit("unknown status '%s'", arg);
168     } else if (strstart(&arg, "conv=")) {
169       parse_flags("conv", arg, dd_conv, ARRAY_LEN(dd_conv), &TT.conv);
170       fprintf(stderr, "conv=%x\n", TT.conv);
171     } else if (strstart(&arg, "iflag="))
172       parse_flags("iflag", arg, dd_iflag, ARRAY_LEN(dd_iflag), &TT.iflag);
173     else if (strstart(&arg, "oflag="))
174       parse_flags("oflag", arg, dd_oflag, ARRAY_LEN(dd_oflag), &TT.oflag);
175     else error_exit("bad arg %s", arg);
176   }
177   if (bs) TT.in.sz = TT.out.sz = bs;
178 
179   signal(SIGINT, dd_sigint);
180   signal(SIGUSR1, generic_signal);
181   gettimeofday(&TT.start, NULL);
182 
183   // For bs=, in/out is done as it is. so only in.sz is enough.
184   // With Single buffer there will be overflow in a read following partial read.
185   TT.in.buff = TT.out.buff = xmalloc(TT.in.sz + (bs ? 0 : TT.out.sz));
186   TT.in.bp = TT.out.bp = TT.in.buff;
187 
188   if (!TT.in.name) TT.in.name = "stdin";
189   else TT.in.fd = xopenro(TT.in.name);
190 
191   if (TT.conv & _DD_conv_notrunc) trunc = 0;
192 
193   if (!TT.out.name) {
194     TT.out.name = "stdout";
195     TT.out.fd = 1;
196   } else TT.out.fd = xcreate(TT.out.name,
197     O_WRONLY|O_CREAT|(trunc*!TT.out.offset), 0666);
198 
199   // Implement skip=
200   if (TT.in.offset) {
201     off_t off = TT.in.offset;
202 
203     if (!(TT.iflag & _DD_iflag_skip_bytes)) off *= TT.in.sz;
204     if (lseek(TT.in.fd, off, SEEK_CUR) < 0) {
205       while (off > 0) {
206         int chunk = off < TT.in.sz ? off : TT.in.sz;
207         ssize_t n = read(TT.in.fd, TT.in.bp, chunk);
208 
209         if (n < 0) {
210           perror_msg("%s", TT.in.name);
211           if (TT.conv & _DD_conv_noerror) status();
212           else return;
213         } else if (!n) {
214           xprintf("%s: Can't skip\n", TT.in.name);
215           return;
216         }
217         off -= n;
218       }
219     }
220   }
221 
222   // Implement seek= and truncate as necessary. We handled position zero
223   // truncate with O_TRUNC on open, so output to /dev/null and such doesn't
224   // error.
225   bs = TT.out.offset;
226   if (!(TT.oflag & _DD_oflag_seek_bytes)) bs *= TT.out.sz;
227   if (bs) {
228     xlseek(TT.out.fd, bs, SEEK_CUR);
229     if (trunc && ftruncate(TT.out.fd, bs)) perror_exit("ftruncate");
230   }
231 
232   unsigned long long bytes_left = TT.c_count;
233   if (TT.c_count != ULLONG_MAX && !(TT.iflag & _DD_iflag_count_bytes)) {
234     bytes_left *= TT.in.sz;
235   }
236   while (bytes_left) {
237     int chunk = bytes_left < TT.in.sz ? bytes_left : TT.in.sz;
238     ssize_t n;
239 
240     // Show progress and exit on SIGINT or just continue on SIGUSR1.
241     if (toys.signal) {
242       status();
243       if (toys.signal==SIGINT) exit_signal(toys.signal);
244       toys.signal = 0;
245     }
246 
247     TT.in.bp = TT.in.buff + TT.in.count;
248     if (TT.conv & _DD_conv_sync) memset(TT.in.bp, 0, TT.in.sz);
249     if (!(n = read(TT.in.fd, TT.in.bp, chunk))) break;
250     if (n < 0) {
251       if (errno == EINTR) continue;
252       //read error case.
253       perror_msg("%s: read error", TT.in.name);
254       if (!(TT.conv & _DD_conv_noerror)) exit(1);
255       status();
256       xlseek(TT.in.fd, TT.in.sz, SEEK_CUR);
257       if (!(TT.conv & _DD_conv_sync)) continue;
258       // if SYNC, then treat as full block of nuls
259       n = TT.in.sz;
260     }
261     if (n == TT.in.sz) {
262       TT.in_full++;
263       TT.in.count += n;
264     } else {
265       TT.in_part++;
266       if (TT.conv & _DD_conv_sync) TT.in.count += TT.in.sz;
267       else TT.in.count += n;
268     }
269     bytes_left -= n;
270 
271     TT.out.count = TT.in.count;
272     if (bs) {
273       write_out(1);
274       TT.in.count = 0;
275       continue;
276     }
277 
278     if (TT.in.count >= TT.out.sz) {
279       write_out(0);
280       TT.in.count = TT.out.count;
281     }
282   }
283   if (TT.out.count) write_out(1); //write any remaining input blocks
284   if ((TT.conv & _DD_conv_fsync) && fsync(TT.out.fd)<0)
285     perror_exit("%s: fsync", TT.out.name);
286 
287   close(TT.in.fd);
288   close(TT.out.fd);
289   if (TT.in.buff) free(TT.in.buff);
290 
291   status();
292 }
293