dd.c 8.0 KB

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