dd.c 8.3 KB

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