dd.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini dd implementation for busybox
  4. *
  5. *
  6. * Copyright (C) 2000,2001 Matt Kraai
  7. *
  8. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  9. */
  10. #include "libbb.h"
  11. /* This is a NOEXEC applet. Be very careful! */
  12. enum {
  13. ifd = STDIN_FILENO,
  14. ofd = STDOUT_FILENO,
  15. };
  16. static const struct suffix_mult dd_suffixes[] = {
  17. { "c", 1 },
  18. { "w", 2 },
  19. { "b", 512 },
  20. { "kD", 1000 },
  21. { "k", 1024 },
  22. { "K", 1024 }, /* compat with coreutils dd */
  23. { "MD", 1000000 },
  24. { "M", 1048576 },
  25. { "GD", 1000000000 },
  26. { "G", 1073741824 },
  27. { "", 0 }
  28. };
  29. struct globals {
  30. off_t out_full, out_part, in_full, in_part;
  31. #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
  32. unsigned long long total_bytes;
  33. unsigned long long begin_time_us;
  34. #endif
  35. };
  36. #define G (*(struct globals*)&bb_common_bufsiz1)
  37. #define INIT_G() do { \
  38. /* we have to zero it out because of NOEXEC */ \
  39. memset(&G, 0, sizeof(G)); \
  40. } while (0)
  41. static void dd_output_status(int UNUSED_PARAM cur_signal)
  42. {
  43. #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
  44. double seconds;
  45. unsigned long long bytes_sec;
  46. unsigned long long now_us = monotonic_us(); /* before fprintf */
  47. #endif
  48. /* Deliberately using %u, not %d */
  49. fprintf(stderr, "%"OFF_FMT"u+%"OFF_FMT"u records in\n"
  50. "%"OFF_FMT"u+%"OFF_FMT"u records out\n",
  51. G.in_full, G.in_part,
  52. G.out_full, G.out_part);
  53. #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
  54. fprintf(stderr, "%llu bytes (%sB) copied, ",
  55. G.total_bytes,
  56. /* show fractional digit, use suffixes */
  57. make_human_readable_str(G.total_bytes, 1, 0)
  58. );
  59. /* Corner cases:
  60. * ./busybox dd </dev/null >/dev/null
  61. * ./busybox dd bs=1M count=2000 </dev/zero >/dev/null
  62. * (echo DONE) | ./busybox dd >/dev/null
  63. * (sleep 1; echo DONE) | ./busybox dd >/dev/null
  64. */
  65. seconds = (now_us - G.begin_time_us) / 1000000.0;
  66. bytes_sec = G.total_bytes / seconds;
  67. fprintf(stderr, "%f seconds, %sB/s\n",
  68. seconds,
  69. /* show fractional digit, use suffixes */
  70. make_human_readable_str(bytes_sec, 1, 0)
  71. );
  72. #endif
  73. }
  74. static ssize_t full_write_or_warn(const void *buf, size_t len,
  75. const char *const filename)
  76. {
  77. ssize_t n = full_write(ofd, buf, len);
  78. if (n < 0)
  79. bb_perror_msg("writing '%s'", filename);
  80. return n;
  81. }
  82. static bool write_and_stats(const void *buf, size_t len, size_t obs,
  83. const char *filename)
  84. {
  85. ssize_t n = full_write_or_warn(buf, len, filename);
  86. if (n < 0)
  87. return 1;
  88. if ((size_t)n == obs)
  89. G.out_full++;
  90. else if (n) /* > 0 */
  91. G.out_part++;
  92. #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
  93. G.total_bytes += n;
  94. #endif
  95. return 0;
  96. }
  97. #if ENABLE_LFS
  98. # define XATOU_SFX xatoull_sfx
  99. #else
  100. # define XATOU_SFX xatoul_sfx
  101. #endif
  102. int dd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  103. int dd_main(int argc UNUSED_PARAM, char **argv)
  104. {
  105. enum {
  106. /* Must be in the same order as OP_conv_XXX! */
  107. /* (see "flags |= (1 << what)" below) */
  108. FLAG_NOTRUNC = 1 << 0,
  109. FLAG_SYNC = 1 << 1,
  110. FLAG_NOERROR = 1 << 2,
  111. FLAG_FSYNC = 1 << 3,
  112. /* end of conv flags */
  113. FLAG_TWOBUFS = 1 << 4,
  114. FLAG_COUNT = 1 << 5,
  115. };
  116. static const char keywords[] ALIGN1 =
  117. "bs\0""count\0""seek\0""skip\0""if\0""of\0"
  118. #if ENABLE_FEATURE_DD_IBS_OBS
  119. "ibs\0""obs\0""conv\0"
  120. #endif
  121. ;
  122. #if ENABLE_FEATURE_DD_IBS_OBS
  123. static const char conv_words[] ALIGN1 =
  124. "notrunc\0""sync\0""noerror\0""fsync\0";
  125. #endif
  126. enum {
  127. OP_bs = 0,
  128. OP_count,
  129. OP_seek,
  130. OP_skip,
  131. OP_if,
  132. OP_of,
  133. #if ENABLE_FEATURE_DD_IBS_OBS
  134. OP_ibs,
  135. OP_obs,
  136. OP_conv,
  137. /* Must be in the same order as FLAG_XXX! */
  138. OP_conv_notrunc = 0,
  139. OP_conv_sync,
  140. OP_conv_noerror,
  141. OP_conv_fsync,
  142. /* Unimplemented conv=XXX: */
  143. //nocreat do not create the output file
  144. //excl fail if the output file already exists
  145. //fdatasync physically write output file data before finishing
  146. //swab swap every pair of input bytes
  147. //lcase change upper case to lower case
  148. //ucase change lower case to upper case
  149. //block pad newline-terminated records with spaces to cbs-size
  150. //unblock replace trailing spaces in cbs-size records with newline
  151. //ascii from EBCDIC to ASCII
  152. //ebcdic from ASCII to EBCDIC
  153. //ibm from ASCII to alternate EBCDIC
  154. #endif
  155. };
  156. int exitcode = EXIT_FAILURE;
  157. size_t ibs = 512, obs = 512;
  158. ssize_t n, w;
  159. char *ibuf, *obuf;
  160. /* And these are all zeroed at once! */
  161. struct {
  162. int flags;
  163. size_t oc;
  164. off_t count;
  165. off_t seek, skip;
  166. const char *infile, *outfile;
  167. } Z;
  168. #define flags (Z.flags )
  169. #define oc (Z.oc )
  170. #define count (Z.count )
  171. #define seek (Z.seek )
  172. #define skip (Z.skip )
  173. #define infile (Z.infile )
  174. #define outfile (Z.outfile)
  175. memset(&Z, 0, sizeof(Z));
  176. INIT_G();
  177. //fflush_all(); - is this needed because of NOEXEC?
  178. for (n = 1; argv[n]; n++) {
  179. int what;
  180. char *val;
  181. char *arg = argv[n];
  182. #if ENABLE_DESKTOP
  183. /* "dd --". NB: coreutils 6.9 will complain if they see
  184. * more than one of them. We wouldn't. */
  185. if (arg[0] == '-' && arg[1] == '-' && arg[2] == '\0')
  186. continue;
  187. #endif
  188. val = strchr(arg, '=');
  189. if (val == NULL)
  190. bb_show_usage();
  191. *val = '\0';
  192. what = index_in_strings(keywords, arg);
  193. if (what < 0)
  194. bb_show_usage();
  195. /* *val = '='; - to preserve ps listing? */
  196. val++;
  197. #if ENABLE_FEATURE_DD_IBS_OBS
  198. if (what == OP_ibs) {
  199. /* Must fit into positive ssize_t */
  200. ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes);
  201. /*continue;*/
  202. }
  203. if (what == OP_obs) {
  204. obs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes);
  205. /*continue;*/
  206. }
  207. if (what == OP_conv) {
  208. while (1) {
  209. /* find ',', replace them with NUL so we can use val for
  210. * index_in_strings() without copying.
  211. * We rely on val being non-null, else strchr would fault.
  212. */
  213. arg = strchr(val, ',');
  214. if (arg)
  215. *arg = '\0';
  216. what = index_in_strings(conv_words, val);
  217. if (what < 0)
  218. bb_error_msg_and_die(bb_msg_invalid_arg, val, "conv");
  219. flags |= (1 << what);
  220. if (!arg) /* no ',' left, so this was the last specifier */
  221. break;
  222. /* *arg = ','; - to preserve ps listing? */
  223. val = arg + 1; /* skip this keyword and ',' */
  224. }
  225. continue; /* we trashed 'what', can't fall through */
  226. }
  227. #endif
  228. if (what == OP_bs) {
  229. ibs = obs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes);
  230. /*continue;*/
  231. }
  232. /* These can be large: */
  233. if (what == OP_count) {
  234. flags |= FLAG_COUNT;
  235. count = XATOU_SFX(val, dd_suffixes);
  236. /*continue;*/
  237. }
  238. if (what == OP_seek) {
  239. seek = XATOU_SFX(val, dd_suffixes);
  240. /*continue;*/
  241. }
  242. if (what == OP_skip) {
  243. skip = XATOU_SFX(val, dd_suffixes);
  244. /*continue;*/
  245. }
  246. if (what == OP_if) {
  247. infile = val;
  248. /*continue;*/
  249. }
  250. if (what == OP_of) {
  251. outfile = val;
  252. /*continue;*/
  253. }
  254. } /* end of "for (argv[n])" */
  255. //XXX:FIXME for huge ibs or obs, malloc'ing them isn't the brightest idea ever
  256. ibuf = obuf = xmalloc(ibs);
  257. if (ibs != obs) {
  258. flags |= FLAG_TWOBUFS;
  259. obuf = xmalloc(obs);
  260. }
  261. #if ENABLE_FEATURE_DD_SIGNAL_HANDLING
  262. signal_SA_RESTART_empty_mask(SIGUSR1, dd_output_status);
  263. #endif
  264. #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
  265. G.begin_time_us = monotonic_us();
  266. #endif
  267. if (infile != NULL)
  268. xmove_fd(xopen(infile, O_RDONLY), ifd);
  269. else {
  270. infile = bb_msg_standard_input;
  271. }
  272. if (outfile != NULL) {
  273. int oflag = O_WRONLY | O_CREAT;
  274. if (!seek && !(flags & FLAG_NOTRUNC))
  275. oflag |= O_TRUNC;
  276. xmove_fd(xopen(outfile, oflag), ofd);
  277. if (seek && !(flags & FLAG_NOTRUNC)) {
  278. if (ftruncate(ofd, seek * obs) < 0) {
  279. struct stat st;
  280. if (fstat(ofd, &st) < 0 || S_ISREG(st.st_mode) ||
  281. S_ISDIR(st.st_mode))
  282. goto die_outfile;
  283. }
  284. }
  285. } else {
  286. outfile = bb_msg_standard_output;
  287. }
  288. if (skip) {
  289. if (lseek(ifd, skip * ibs, SEEK_CUR) < 0) {
  290. while (skip-- > 0) {
  291. n = safe_read(ifd, ibuf, ibs);
  292. if (n < 0)
  293. goto die_infile;
  294. if (n == 0)
  295. break;
  296. }
  297. }
  298. }
  299. if (seek) {
  300. if (lseek(ofd, seek * obs, SEEK_CUR) < 0)
  301. goto die_outfile;
  302. }
  303. while (!(flags & FLAG_COUNT) || (G.in_full + G.in_part != count)) {
  304. n = safe_read(ifd, ibuf, ibs);
  305. if (n == 0)
  306. break;
  307. if (n < 0) {
  308. /* "Bad block" */
  309. if (!(flags & FLAG_NOERROR))
  310. goto die_infile;
  311. bb_simple_perror_msg(infile);
  312. /* GNU dd with conv=noerror skips over bad blocks */
  313. xlseek(ifd, ibs, SEEK_CUR);
  314. /* conv=noerror,sync writes NULs,
  315. * conv=noerror just ignores input bad blocks */
  316. n = 0;
  317. }
  318. if ((size_t)n == ibs)
  319. G.in_full++;
  320. else {
  321. G.in_part++;
  322. if (flags & FLAG_SYNC) {
  323. memset(ibuf + n, 0, ibs - n);
  324. n = ibs;
  325. }
  326. }
  327. if (flags & FLAG_TWOBUFS) {
  328. char *tmp = ibuf;
  329. while (n) {
  330. size_t d = obs - oc;
  331. if (d > (size_t)n)
  332. d = n;
  333. memcpy(obuf + oc, tmp, d);
  334. n -= d;
  335. tmp += d;
  336. oc += d;
  337. if (oc == obs) {
  338. if (write_and_stats(obuf, obs, obs, outfile))
  339. goto out_status;
  340. oc = 0;
  341. }
  342. }
  343. } else if (write_and_stats(ibuf, n, obs, outfile))
  344. goto out_status;
  345. if (flags & FLAG_FSYNC) {
  346. if (fsync(ofd) < 0)
  347. goto die_outfile;
  348. }
  349. }
  350. if (ENABLE_FEATURE_DD_IBS_OBS && oc) {
  351. w = full_write_or_warn(obuf, oc, outfile);
  352. if (w < 0) goto out_status;
  353. if (w > 0) G.out_part++;
  354. }
  355. if (close(ifd) < 0) {
  356. die_infile:
  357. bb_simple_perror_msg_and_die(infile);
  358. }
  359. if (close(ofd) < 0) {
  360. die_outfile:
  361. bb_simple_perror_msg_and_die(outfile);
  362. }
  363. exitcode = EXIT_SUCCESS;
  364. out_status:
  365. dd_output_status(0);
  366. return exitcode;
  367. }