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 * todo: ctrl-c doesn't work, the read() is restarting.
9
10USE_DD(NEWTOY(dd, 0, TOYFLAG_USR|TOYFLAG_BIN))
11
12config DD
13  bool "dd"
14  default n
15  help
16    usage: dd [if=FILE] [of=FILE] [ibs=N] [obs=N] [bs=N] [count=N] [skip=N]
17            [seek=N] [conv=notrunc|noerror|sync|fsync] [status=noxfer|none]
18
19    Options:
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     Read N bytes at a time
24    obs=N     Write N bytes at a time
25    count=N   Copy only N input blocks
26    skip=N    Skip N input blocks
27    seek=N    Skip N output blocks
28    conv=notrunc  Don't truncate output file
29    conv=noerror  Continue after read errors
30    conv=sync     Pad blocks with zeros
31    conv=fsync    Physically write data out before finishing
32    status=noxfer Don't show transfer rate
33    status=none   Don't show transfer rate or records in/out
34
35    Numbers may be suffixed by c (*1), w (*2), b (*512), kD (*1000), k (*1024),
36    MD (*1000*1000), M (*1024*1024), GD (*1000*1000*1000) or G (*1024*1024*1024).
37*/
38
39#define FOR_dd
40#include "toys.h"
41
42GLOBALS(
43  int show_xfer, show_records;
44  unsigned long long bytes, c_count, in_full, in_part, out_full, out_part;
45  struct timeval start;
46  struct {
47    char *name;
48    int fd;
49    unsigned char *buff, *bp;
50    long sz, count;
51    unsigned long long offset;
52  } in, out;
53);
54
55#define C_FSYNC   1
56#define C_NOERROR 2
57#define C_NOTRUNC 4
58#define C_SYNC    8
59
60static void status()
61{
62  double seconds;
63  struct timeval now;
64
65  gettimeofday(&now, NULL);
66  seconds = ((now.tv_sec * 1000000 + now.tv_usec) -
67      (TT.start.tv_sec * 1000000 + TT.start.tv_usec))/1000000.0;
68
69  if (TT.show_records)
70    fprintf(stderr, "%llu+%llu records in\n%llu+%llu records out\n",
71            TT.in_full, TT.in_part, TT.out_full, TT.out_part);
72
73  if (TT.show_xfer) {
74    human_readable(toybuf, TT.bytes, HR_SPACE|HR_B);
75    fprintf(stderr, "%llu bytes (%s) copied, ", TT.bytes, toybuf);
76    human_readable(toybuf, TT.bytes/seconds, HR_SPACE|HR_B);
77    fprintf(stderr, "%f s, %s/s\n", seconds, toybuf);
78  }
79}
80
81static void write_out(int all)
82{
83  TT.out.bp = TT.out.buff;
84  while (TT.out.count) {
85    ssize_t nw = writeall(TT.out.fd, TT.out.bp, ((all)? TT.out.count : TT.out.sz));
86
87    all = 0; //further writes will be on obs
88    if (nw <= 0) perror_exit("%s: write error", TT.out.name);
89    if (nw == TT.out.sz) TT.out_full++;
90    else TT.out_part++;
91    TT.out.count -= nw;
92    TT.out.bp += nw;
93    TT.bytes += nw;
94    if (TT.out.count < TT.out.sz) break;
95  }
96  if (TT.out.count) memmove(TT.out.buff, TT.out.bp, TT.out.count); //move remainder to front
97}
98
99int strstarteq(char **a, char *b)
100{
101  char *aa = *a;
102
103  return strstart(&aa, b) && *aa == '=' && (*a = aa+1);
104}
105
106void dd_main()
107{
108  char **args;
109  unsigned long long bs = 0;
110  int trunc = O_TRUNC, conv = 0;
111
112  TT.show_xfer = TT.show_records = 1;
113  TT.c_count = ULLONG_MAX;
114
115  TT.in.sz = TT.out.sz = 512; //default io block size
116  for (args = toys.optargs; *args; args++) {
117    char *arg = *args;
118
119    if (strstarteq(&arg, "bs")) bs = atolx_range(arg, 1, LONG_MAX);
120    else if (strstarteq(&arg, "ibs")) TT.in.sz = atolx_range(arg, 1, LONG_MAX);
121    else if (strstarteq(&arg, "obs")) TT.out.sz = atolx_range(arg, 1, LONG_MAX);
122    else if (strstarteq(&arg, "count"))
123      TT.c_count = atolx_range(arg, 0, LLONG_MAX);
124    else if (strstarteq(&arg, "if")) TT.in.name = arg;
125    else if (strstarteq(&arg, "of")) TT.out.name = arg;
126    else if (strstarteq(&arg, "seek"))
127      TT.out.offset = atolx_range(arg, 0, LLONG_MAX);
128    else if (strstarteq(&arg, "skip"))
129      TT.in.offset = atolx_range(arg, 0, LLONG_MAX);
130    else if (strstarteq(&arg, "status")) {
131      if (!strcmp(arg, "noxfer")) TT.show_xfer = 0;
132      else if (!strcmp(arg, "none")) TT.show_xfer = TT.show_records = 0;
133      else error_exit("unknown status '%s'", arg);
134    } else if (strstarteq(&arg, "conv")) {
135      char *ss, *convs[] = {"fsync", "noerror", "notrunc", "sync"};
136      int i, len;
137
138      while ((ss = comma_iterate(&arg, &len))) {
139        for (i = 0; i<ARRAY_LEN(convs); i++)
140          if (len == strlen(convs[i]) && !strncmp(ss, convs[i], len)) break;
141        if (i == ARRAY_LEN(convs)) error_exit("bad conv=%.*s", len, ss);
142        conv |= 1<<i;
143      }
144    } else error_exit("bad arg %s", arg);
145  }
146  if (bs) TT.in.sz = TT.out.sz = bs;
147
148  signal(SIGINT, generic_signal);
149  signal(SIGUSR1, generic_signal);
150  gettimeofday(&TT.start, NULL);
151
152  /* for bs=, in/out is done as it is. so only in.sz is enough.
153   * With Single buffer there will be overflow in a read following partial read
154   */
155  TT.in.buff = TT.out.buff = xmalloc(TT.in.sz + (bs ? 0 : TT.out.sz));
156  TT.in.bp = TT.out.bp = TT.in.buff;
157  //setup input
158  if (!TT.in.name) TT.in.name = "stdin";
159  else TT.in.fd = xopenro(TT.in.name);
160
161  if (conv&C_NOTRUNC) trunc = 0;
162
163  //setup output
164  if (!TT.out.name) {
165    TT.out.name = "stdout";
166    TT.out.fd = 1;
167  } else TT.out.fd = xcreate(TT.out.name,
168    O_WRONLY|O_CREAT|(trunc*!TT.out.offset), 0666);
169
170  // Implement skip=
171  if (TT.in.offset) {
172    if (lseek(TT.in.fd, (off_t)(TT.in.offset * TT.in.sz), SEEK_CUR) < 0) {
173      while (TT.in.offset--) {
174        ssize_t n = read(TT.in.fd, TT.in.bp, TT.in.sz);
175
176        if (n < 0) {
177          perror_msg("%s", TT.in.name);
178          if (conv&C_NOERROR) status();
179          else return;
180        } else if (!n) {
181          xprintf("%s: Can't skip\n", TT.in.name);
182          return;
183        }
184      }
185    }
186  }
187
188  // seek/truncate as necessary. We handled position zero truncate with
189  // O_TRUNC on open, so output to /dev/null and such doesn't error.
190  if (TT.out.fd!=1 && (bs = TT.out.offset*TT.out.sz)) {
191    xlseek(TT.out.fd, bs, SEEK_CUR);
192    if (trunc && ftruncate(TT.out.fd, bs)) perror_exit("ftruncate");
193  }
194
195  while (TT.c_count==ULLONG_MAX || (TT.in_full + TT.in_part) < TT.c_count) {
196    ssize_t n;
197
198    // Show progress and exit on SIGINT or just continue on SIGUSR1.
199    if (toys.signal) {
200      status();
201      if (toys.signal==SIGINT) exit_signal(toys.signal);
202      toys.signal = 0;
203    }
204
205    TT.in.bp = TT.in.buff + TT.in.count;
206    if (conv&C_SYNC) memset(TT.in.bp, 0, TT.in.sz);
207    if (!(n = read(TT.in.fd, TT.in.bp, TT.in.sz))) break;
208    if (n < 0) {
209      if (errno == EINTR) continue;
210      //read error case.
211      perror_msg("%s: read error", TT.in.name);
212      if (!(conv&C_NOERROR)) exit(1);
213      status();
214      xlseek(TT.in.fd, TT.in.sz, SEEK_CUR);
215      if (!(conv&C_SYNC)) continue;
216      // if SYNC, then treat as full block of nuls
217      n = TT.in.sz;
218    }
219    if (n == TT.in.sz) {
220      TT.in_full++;
221      TT.in.count += n;
222    } else {
223      TT.in_part++;
224      if (conv&C_SYNC) TT.in.count += TT.in.sz;
225      else TT.in.count += n;
226    }
227
228    TT.out.count = TT.in.count;
229    if (bs) {
230      write_out(1);
231      TT.in.count = 0;
232      continue;
233    }
234
235    if (TT.in.count >= TT.out.sz) {
236      write_out(0);
237      TT.in.count = TT.out.count;
238    }
239  }
240  if (TT.out.count) write_out(1); //write any remaining input blocks
241  if ((conv&C_FSYNC) && fsync(TT.out.fd)<0)
242    perror_exit("%s: fsync", TT.out.name);
243
244  close(TT.in.fd);
245  close(TT.out.fd);
246  if (TT.in.buff) free(TT.in.buff);
247
248  status();
249}
250