3
0

dd.c 8.0 KB

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