dd.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini dd implementation for busybox
  4. *
  5. * Copyright (C) 2000,2001 Matt Kraai
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. //config:config DD
  10. //config: bool "dd (8.3 kb)"
  11. //config: default y
  12. //config: help
  13. //config: dd copies a file (from standard input to standard output,
  14. //config: by default) using specific input and output blocksizes,
  15. //config: while optionally performing conversions on it.
  16. //config:
  17. //config:config FEATURE_DD_SIGNAL_HANDLING
  18. //config: bool "Enable signal handling for status reporting"
  19. //config: default y
  20. //config: depends on DD
  21. //config: help
  22. //config: Sending a SIGUSR1 signal to a running 'dd' process makes it
  23. //config: print to standard error the number of records read and written
  24. //config: so far, then to resume copying.
  25. //config:
  26. //config: $ dd if=/dev/zero of=/dev/null &
  27. //config: $ pid=$!; kill -USR1 $pid; sleep 1; kill $pid
  28. //config: 10899206+0 records in
  29. //config: 10899206+0 records out
  30. //config:
  31. //config:config FEATURE_DD_THIRD_STATUS_LINE
  32. //config: bool "Enable the third status line upon signal"
  33. //config: default y
  34. //config: depends on DD && FEATURE_DD_SIGNAL_HANDLING
  35. //config: help
  36. //config: Displays a coreutils-like third status line with transferred bytes,
  37. //config: elapsed time and speed.
  38. //config:
  39. //config:config FEATURE_DD_IBS_OBS
  40. //config: bool "Enable ibs, obs, iflag, oflag and conv options"
  41. //config: default y
  42. //config: depends on DD
  43. //config: help
  44. //config: Enable support for writing a certain number of bytes in and out,
  45. //config: at a time, and performing conversions on the data stream.
  46. //config:
  47. //config:config FEATURE_DD_STATUS
  48. //config: bool "Enable status display options"
  49. //config: default y
  50. //config: depends on DD
  51. //config: help
  52. //config: Enable support for status=noxfer/none option.
  53. //applet:IF_DD(APPLET_NOEXEC(dd, dd, BB_DIR_BIN, BB_SUID_DROP, dd))
  54. //kbuild:lib-$(CONFIG_DD) += dd.o
  55. //usage:#define dd_trivial_usage
  56. //usage: "[if=FILE] [of=FILE] [" IF_FEATURE_DD_IBS_OBS("ibs=N obs=N/") "bs=N] [count=N] [skip=N] [seek=N]"
  57. //usage: IF_FEATURE_DD_IBS_OBS("\n"
  58. //usage: " [conv=notrunc|noerror|sync|fsync]\n"
  59. //usage: " [iflag=skip_bytes|count_bytes|fullblock|direct] [oflag=seek_bytes|append|direct]"
  60. //usage: )
  61. //usage:#define dd_full_usage "\n\n"
  62. //usage: "Copy a file with converting and formatting\n"
  63. //usage: "\n if=FILE Read from FILE instead of stdin"
  64. //usage: "\n of=FILE Write to FILE instead of stdout"
  65. //usage: "\n bs=N Read and write N bytes at a time"
  66. //usage: IF_FEATURE_DD_IBS_OBS(
  67. //usage: "\n ibs=N Read N bytes at a time"
  68. //usage: )
  69. //usage: IF_FEATURE_DD_IBS_OBS(
  70. //usage: "\n obs=N Write N bytes at a time"
  71. //usage: )
  72. //usage: "\n count=N Copy only N input blocks"
  73. //usage: "\n skip=N Skip N input blocks"
  74. //usage: "\n seek=N Skip N output blocks"
  75. //usage: IF_FEATURE_DD_IBS_OBS(
  76. //usage: "\n conv=notrunc Don't truncate output file"
  77. //usage: "\n conv=noerror Continue after read errors"
  78. //usage: "\n conv=sync Pad blocks with zeros"
  79. //usage: "\n conv=fsync Physically write data out before finishing"
  80. //usage: "\n conv=swab Swap every pair of bytes"
  81. //usage: "\n iflag=skip_bytes skip=N is in bytes"
  82. //usage: "\n iflag=count_bytes count=N is in bytes"
  83. //usage: "\n oflag=seek_bytes seek=N is in bytes"
  84. //usage: "\n iflag=direct O_DIRECT input"
  85. //usage: "\n oflag=direct O_DIRECT output"
  86. //usage: "\n iflag=fullblock Read full blocks"
  87. //usage: "\n oflag=append Open output in append mode"
  88. //usage: )
  89. //usage: IF_FEATURE_DD_STATUS(
  90. //usage: "\n status=noxfer Suppress rate output"
  91. //usage: "\n status=none Suppress all output"
  92. //usage: )
  93. //usage: "\n"
  94. //usage: "\nN may be suffixed by c (1), w (2), b (512), kB (1000), k (1024), MB, M, GB, G"
  95. //usage:
  96. //usage:#define dd_example_usage
  97. //usage: "$ dd if=/dev/zero of=/dev/ram1 bs=1M count=4\n"
  98. //usage: "4+0 records in\n"
  99. //usage: "4+0 records out\n"
  100. #include "libbb.h"
  101. #include "common_bufsiz.h"
  102. /* This is a NOEXEC applet. Be very careful! */
  103. enum {
  104. ifd = STDIN_FILENO,
  105. ofd = STDOUT_FILENO,
  106. };
  107. struct globals {
  108. off_t out_full, out_part, in_full, in_part;
  109. #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
  110. unsigned long long total_bytes;
  111. unsigned long long begin_time_us;
  112. #endif
  113. int flags;
  114. } FIX_ALIASING;
  115. #define G (*(struct globals*)bb_common_bufsiz1)
  116. #define INIT_G() do { \
  117. setup_common_bufsiz(); \
  118. /* we have to zero it out because of NOEXEC */ \
  119. memset(&G, 0, sizeof(G)); \
  120. } while (0)
  121. enum {
  122. /* Must be in the same order as OP_conv_XXX! */
  123. /* (see "flags |= (1 << what)" below) */
  124. FLAG_NOTRUNC = (1 << 0) * ENABLE_FEATURE_DD_IBS_OBS,
  125. FLAG_SYNC = (1 << 1) * ENABLE_FEATURE_DD_IBS_OBS,
  126. FLAG_NOERROR = (1 << 2) * ENABLE_FEATURE_DD_IBS_OBS,
  127. FLAG_FSYNC = (1 << 3) * ENABLE_FEATURE_DD_IBS_OBS,
  128. FLAG_SWAB = (1 << 4) * ENABLE_FEATURE_DD_IBS_OBS,
  129. /* end of conv flags */
  130. /* start of input flags */
  131. FLAG_IFLAG_SHIFT = 5,
  132. FLAG_SKIP_BYTES = (1 << 5) * ENABLE_FEATURE_DD_IBS_OBS,
  133. FLAG_COUNT_BYTES = (1 << 6) * ENABLE_FEATURE_DD_IBS_OBS,
  134. FLAG_FULLBLOCK = (1 << 7) * ENABLE_FEATURE_DD_IBS_OBS,
  135. FLAG_IDIRECT = (1 << 8) * ENABLE_FEATURE_DD_IBS_OBS,
  136. /* end of input flags */
  137. /* start of output flags */
  138. FLAG_OFLAG_SHIFT = 9,
  139. FLAG_SEEK_BYTES = (1 << 9) * ENABLE_FEATURE_DD_IBS_OBS,
  140. FLAG_APPEND = (1 << 10) * ENABLE_FEATURE_DD_IBS_OBS,
  141. FLAG_ODIRECT = (1 << 11) * ENABLE_FEATURE_DD_IBS_OBS,
  142. /* end of output flags */
  143. FLAG_TWOBUFS = (1 << 12) * ENABLE_FEATURE_DD_IBS_OBS,
  144. FLAG_COUNT = 1 << 13,
  145. FLAG_STATUS_NONE = 1 << 14,
  146. FLAG_STATUS_NOXFER = 1 << 15,
  147. };
  148. static void dd_output_status(int UNUSED_PARAM cur_signal)
  149. {
  150. #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
  151. double seconds;
  152. unsigned long long bytes_sec;
  153. unsigned long long now_us = monotonic_us(); /* before fprintf */
  154. #endif
  155. /* Deliberately using %u, not %d */
  156. fprintf(stderr, "%"OFF_FMT"u+%"OFF_FMT"u records in\n"
  157. "%"OFF_FMT"u+%"OFF_FMT"u records out\n",
  158. G.in_full, G.in_part,
  159. G.out_full, G.out_part);
  160. #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
  161. # if ENABLE_FEATURE_DD_STATUS
  162. if (G.flags & FLAG_STATUS_NOXFER) /* status=noxfer active? */
  163. return;
  164. //TODO: should status=none make dd stop reacting to USR1 entirely?
  165. //So far we react to it (we print the stats),
  166. //status=none only suppresses final, non-USR1 generated status message.
  167. # endif
  168. fprintf(stderr, /*G.total_bytes < 1024
  169. ? "%llu bytes copied, " : */ "%llu bytes (%sB) copied, "
  170. , G.total_bytes,
  171. /* show fractional digit, use suffixes */
  172. make_human_readable_str(G.total_bytes, 1, 0)
  173. );
  174. /* Corner cases:
  175. * ./busybox dd </dev/null >/dev/null
  176. * ./busybox dd bs=1M count=2000 </dev/zero >/dev/null
  177. * (echo DONE) | ./busybox dd >/dev/null
  178. * (sleep 1; echo DONE) | ./busybox dd >/dev/null
  179. */
  180. seconds = (now_us - G.begin_time_us) / 1000000.0;
  181. bytes_sec = G.total_bytes / seconds;
  182. fprintf(stderr, "%f seconds, %sB/s\n",
  183. seconds,
  184. /* show fractional digit, use suffixes */
  185. make_human_readable_str(bytes_sec, 1, 0)
  186. );
  187. #endif
  188. }
  189. #if ENABLE_FEATURE_DD_IBS_OBS
  190. # ifdef O_DIRECT
  191. static int clear_O_DIRECT(int fd)
  192. {
  193. if (errno == EINVAL) {
  194. int fl = fcntl(fd, F_GETFL);
  195. if (fl & O_DIRECT) {
  196. fcntl(fd, F_SETFL, fl & ~O_DIRECT);
  197. return 1;
  198. }
  199. }
  200. return 0;
  201. }
  202. # endif
  203. #endif
  204. static ssize_t dd_read(void *ibuf, size_t ibs)
  205. {
  206. ssize_t n;
  207. #if ENABLE_FEATURE_DD_IBS_OBS
  208. read_again:
  209. if (G.flags & FLAG_FULLBLOCK)
  210. n = full_read(ifd, ibuf, ibs);
  211. else
  212. #endif
  213. n = safe_read(ifd, ibuf, ibs);
  214. #if ENABLE_FEATURE_DD_IBS_OBS
  215. # ifdef O_DIRECT
  216. if (n < 0 && (G.flags & FLAG_IDIRECT) && clear_O_DIRECT(ifd))
  217. goto read_again;
  218. # endif
  219. #endif
  220. return n;
  221. }
  222. static bool write_and_stats(const void *buf, size_t len, size_t obs,
  223. const char *filename)
  224. {
  225. ssize_t n;
  226. IF_FEATURE_DD_IBS_OBS(write_again:)
  227. n = full_write(ofd, buf, len);
  228. #if ENABLE_FEATURE_DD_IBS_OBS
  229. # ifdef O_DIRECT
  230. if (n < 0 && (G.flags & FLAG_ODIRECT) && clear_O_DIRECT(ofd))
  231. goto write_again;
  232. # endif
  233. #endif
  234. #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
  235. if (n > 0)
  236. G.total_bytes += n;
  237. #endif
  238. if ((size_t)n == obs) {
  239. G.out_full++;
  240. return 0;
  241. }
  242. if ((size_t)n == len) {
  243. G.out_part++;
  244. return 0;
  245. }
  246. /* n is < len (and possibly is -1).
  247. * Even if n >= 0, errno is usually set correctly.
  248. * For example, if writing to block device and getting ENOSPC,
  249. * full_write() first sees a short write, then tries to write
  250. * the remainder and gets errno set to ENOSPC.
  251. * It returns n > 0 (the amount which it did write).
  252. */
  253. bb_perror_msg("error writing '%s'", filename);
  254. return 1;
  255. }
  256. #if ENABLE_LFS
  257. # define XATOU_SFX xatoull_sfx
  258. #else
  259. # define XATOU_SFX xatoul_sfx
  260. #endif
  261. #if ENABLE_FEATURE_DD_IBS_OBS
  262. static int parse_comma_flags(char *val, const char *words, const char *error_in)
  263. {
  264. int flags = 0;
  265. while (1) {
  266. int n;
  267. char *arg;
  268. /* find ',', replace them with NUL so we can use val for
  269. * index_in_strings() without copying.
  270. * We rely on val being non-null, else strchr would fault.
  271. */
  272. arg = strchr(val, ',');
  273. if (arg)
  274. *arg = '\0';
  275. n = index_in_strings(words, val);
  276. if (n < 0)
  277. bb_error_msg_and_die(bb_msg_invalid_arg_to, val, error_in);
  278. flags |= (1 << n);
  279. if (!arg) /* no ',' left, so this was the last specifier */
  280. break;
  281. *arg = ','; /* to preserve ps listing */
  282. val = arg + 1; /* skip this keyword and ',' */
  283. }
  284. return flags;
  285. }
  286. #endif
  287. static void *alloc_buf(size_t size)
  288. {
  289. /* Important for "{i,o}flag=direct" - buffers must be page aligned */
  290. if (size >= bb_getpagesize())
  291. return xmmap_anon(size);
  292. return xmalloc(size);
  293. }
  294. int dd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  295. int dd_main(int argc UNUSED_PARAM, char **argv)
  296. {
  297. static const char keywords[] ALIGN1 =
  298. "bs\0""count\0""seek\0""skip\0""if\0""of\0"IF_FEATURE_DD_STATUS("status\0")
  299. #if ENABLE_FEATURE_DD_IBS_OBS
  300. "ibs\0""obs\0""conv\0""iflag\0""oflag\0"
  301. #endif
  302. ;
  303. #if ENABLE_FEATURE_DD_IBS_OBS
  304. static const char conv_words[] ALIGN1 =
  305. "notrunc\0""sync\0""noerror\0""fsync\0""swab\0";
  306. static const char iflag_words[] ALIGN1 =
  307. "skip_bytes\0""count_bytes\0""fullblock\0""direct\0";
  308. static const char oflag_words[] ALIGN1 =
  309. "seek_bytes\0append\0""direct\0";
  310. #endif
  311. #if ENABLE_FEATURE_DD_STATUS
  312. static const char status_words[] ALIGN1 =
  313. "none\0""noxfer\0";
  314. #endif
  315. enum {
  316. OP_bs = 0,
  317. OP_count,
  318. OP_seek,
  319. OP_skip,
  320. OP_if,
  321. OP_of,
  322. IF_FEATURE_DD_STATUS(OP_status,)
  323. #if ENABLE_FEATURE_DD_IBS_OBS
  324. OP_ibs,
  325. OP_obs,
  326. OP_conv,
  327. OP_iflag,
  328. OP_oflag,
  329. /* Must be in the same order as FLAG_XXX! */
  330. OP_conv_notrunc = 0,
  331. OP_conv_sync,
  332. OP_conv_noerror,
  333. OP_conv_fsync,
  334. OP_conv_swab,
  335. /* Unimplemented conv=XXX: */
  336. //nocreat do not create the output file
  337. //excl fail if the output file already exists
  338. //fdatasync physically write output file data before finishing
  339. //lcase change upper case to lower case
  340. //ucase change lower case to upper case
  341. //block pad newline-terminated records with spaces to cbs-size
  342. //unblock replace trailing spaces in cbs-size records with newline
  343. //ascii from EBCDIC to ASCII
  344. //ebcdic from ASCII to EBCDIC
  345. //ibm from ASCII to alternate EBCDIC
  346. /* Partially implemented: */
  347. //swab swap every pair of input bytes: will abort on non-even reads
  348. OP_iflag_skip_bytes,
  349. OP_iflag_count_bytes,
  350. OP_iflag_fullblock,
  351. OP_iflag_direct,
  352. OP_oflag_seek_bytes,
  353. OP_oflag_direct,
  354. #endif
  355. };
  356. exitcode_t exitcode = EXIT_FAILURE;
  357. int i;
  358. size_t ibs = 512;
  359. char *ibuf;
  360. #if ENABLE_FEATURE_DD_IBS_OBS
  361. size_t obs = 512;
  362. char *obuf;
  363. #else
  364. # define obs ibs
  365. # define obuf ibuf
  366. #endif
  367. /* These are all zeroed at once! */
  368. struct {
  369. IF_FEATURE_DD_IBS_OBS(size_t ocount;)
  370. ssize_t prev_read_size; /* for detecting swab failure */
  371. off_t count;
  372. off_t seek, skip;
  373. const char *infile, *outfile;
  374. } Z;
  375. #define ocount (Z.ocount )
  376. #define prev_read_size (Z.prev_read_size)
  377. #define count (Z.count )
  378. #define seek (Z.seek )
  379. #define skip (Z.skip )
  380. #define infile (Z.infile )
  381. #define outfile (Z.outfile)
  382. memset(&Z, 0, sizeof(Z));
  383. INIT_G();
  384. //fflush_all(); - is this needed because of NOEXEC?
  385. for (i = 1; argv[i]; i++) {
  386. int what;
  387. char *val;
  388. char *arg = argv[i];
  389. #if ENABLE_DESKTOP
  390. /* "dd --". NB: coreutils 6.9 will complain if they see
  391. * more than one of them. We wouldn't. */
  392. if (arg[0] == '-' && arg[1] == '-' && arg[2] == '\0')
  393. continue;
  394. #endif
  395. val = strchr(arg, '=');
  396. if (val == NULL)
  397. bb_show_usage();
  398. *val = '\0';
  399. what = index_in_strings(keywords, arg);
  400. if (what < 0)
  401. bb_show_usage();
  402. /* *val = '='; - to preserve ps listing? */
  403. val++;
  404. #if ENABLE_FEATURE_DD_IBS_OBS
  405. if (what == OP_ibs) {
  406. /* Must fit into positive ssize_t */
  407. ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, cwbkMG_suffixes);
  408. /*continue;*/
  409. }
  410. if (what == OP_obs) {
  411. obs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, cwbkMG_suffixes);
  412. /*continue;*/
  413. }
  414. if (what == OP_conv) {
  415. G.flags |= parse_comma_flags(val, conv_words, "conv");
  416. /*continue;*/
  417. }
  418. if (what == OP_iflag) {
  419. G.flags |= parse_comma_flags(val, iflag_words, "iflag") << FLAG_IFLAG_SHIFT;
  420. /*continue;*/
  421. }
  422. if (what == OP_oflag) {
  423. G.flags |= parse_comma_flags(val, oflag_words, "oflag") << FLAG_OFLAG_SHIFT;
  424. /*continue;*/
  425. }
  426. #endif
  427. if (what == OP_bs) {
  428. ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, cwbkMG_suffixes);
  429. obs = ibs;
  430. /*continue;*/
  431. }
  432. /* These can be large: */
  433. if (what == OP_count) {
  434. G.flags |= FLAG_COUNT;
  435. count = XATOU_SFX(val, cwbkMG_suffixes);
  436. /*continue;*/
  437. }
  438. if (what == OP_seek) {
  439. seek = XATOU_SFX(val, cwbkMG_suffixes);
  440. /*continue;*/
  441. }
  442. if (what == OP_skip) {
  443. skip = XATOU_SFX(val, cwbkMG_suffixes);
  444. /*continue;*/
  445. }
  446. if (what == OP_if) {
  447. infile = val;
  448. /*continue;*/
  449. }
  450. if (what == OP_of) {
  451. outfile = val;
  452. /*continue;*/
  453. }
  454. #if ENABLE_FEATURE_DD_STATUS
  455. if (what == OP_status) {
  456. int n;
  457. n = index_in_strings(status_words, val);
  458. if (n < 0)
  459. bb_error_msg_and_die(bb_msg_invalid_arg_to, val, "status");
  460. G.flags |= FLAG_STATUS_NONE << n;
  461. /*continue;*/
  462. }
  463. #endif
  464. } /* end of "for (argv[i])" */
  465. ibuf = alloc_buf(ibs);
  466. obuf = ibuf;
  467. #if ENABLE_FEATURE_DD_IBS_OBS
  468. if (ibs != obs) {
  469. G.flags |= FLAG_TWOBUFS;
  470. obuf = alloc_buf(obs);
  471. }
  472. #endif
  473. #if ENABLE_FEATURE_DD_SIGNAL_HANDLING
  474. signal_SA_RESTART_empty_mask(SIGUSR1, dd_output_status);
  475. #endif
  476. #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
  477. G.begin_time_us = monotonic_us();
  478. #endif
  479. if (infile) {
  480. int iflag = O_RDONLY;
  481. #if ENABLE_FEATURE_DD_IBS_OBS
  482. if (G.flags & FLAG_IDIRECT) {
  483. # ifdef O_DIRECT
  484. iflag |= O_DIRECT;
  485. # else
  486. bb_error_msg_and_die("O_DIRECT not supported on this platform");
  487. # endif
  488. }
  489. #endif
  490. xmove_fd(xopen(infile, iflag), ifd);
  491. } else {
  492. infile = bb_msg_standard_input;
  493. }
  494. if (outfile) {
  495. int oflag = O_WRONLY | O_CREAT;
  496. if (!seek && !(G.flags & FLAG_NOTRUNC))
  497. oflag |= O_TRUNC;
  498. if (G.flags & FLAG_APPEND)
  499. oflag |= O_APPEND;
  500. #if ENABLE_FEATURE_DD_IBS_OBS
  501. if (G.flags & FLAG_ODIRECT) {
  502. # ifdef O_DIRECT
  503. oflag |= O_DIRECT;
  504. # else
  505. bb_error_msg_and_die("O_DIRECT not supported on this platform");
  506. # endif
  507. }
  508. #endif
  509. xmove_fd(xopen(outfile, oflag), ofd);
  510. if (seek && !(G.flags & FLAG_NOTRUNC)) {
  511. size_t blocksz = (G.flags & FLAG_SEEK_BYTES) ? 1 : obs;
  512. if (ftruncate(ofd, seek * blocksz) < 0) {
  513. struct stat st;
  514. if (fstat(ofd, &st) < 0
  515. || S_ISREG(st.st_mode)
  516. || S_ISDIR(st.st_mode)
  517. ) {
  518. goto die_outfile;
  519. }
  520. }
  521. }
  522. } else {
  523. outfile = bb_msg_standard_output;
  524. }
  525. if (skip) {
  526. size_t blocksz = (G.flags & FLAG_SKIP_BYTES) ? 1 : ibs;
  527. if (lseek(ifd, skip * blocksz, SEEK_CUR) < 0) {
  528. do {
  529. ssize_t n = dd_read(ibuf, blocksz);
  530. if (n < 0)
  531. goto die_infile;
  532. if (n == 0)
  533. break;
  534. } while (--skip != 0);
  535. }
  536. }
  537. if (seek) {
  538. size_t blocksz = (G.flags & FLAG_SEEK_BYTES) ? 1 : obs;
  539. if (lseek(ofd, seek * blocksz, SEEK_CUR) < 0)
  540. goto die_outfile;
  541. }
  542. while (1) {
  543. ssize_t n = ibs;
  544. if (G.flags & FLAG_COUNT) {
  545. if (count == 0)
  546. break;
  547. if ((G.flags & FLAG_COUNT_BYTES) && count < ibs)
  548. n = count;
  549. }
  550. n = dd_read(ibuf, n);
  551. if (n == 0)
  552. break;
  553. if (n < 0) {
  554. /* "Bad block" */
  555. if (!(G.flags & FLAG_NOERROR))
  556. goto die_infile;
  557. bb_simple_perror_msg(infile);
  558. /* GNU dd with conv=noerror skips over bad blocks */
  559. xlseek(ifd, ibs, SEEK_CUR);
  560. /* conv=noerror,sync writes NULs,
  561. * conv=noerror just ignores input bad blocks */
  562. n = 0;
  563. }
  564. if (G.flags & FLAG_SWAB) {
  565. uint16_t *p16;
  566. ssize_t n2;
  567. /* Our code allows only last read to be odd-sized */
  568. if (prev_read_size & 1)
  569. bb_error_msg_and_die("can't swab %lu byte buffer",
  570. (unsigned long)prev_read_size);
  571. prev_read_size = n;
  572. /* If n is odd, last byte is not swapped:
  573. * echo -n "qwe" | dd conv=swab
  574. * prints "wqe".
  575. */
  576. p16 = (void*) ibuf;
  577. n2 = (n >> 1);
  578. while (--n2 >= 0) {
  579. *p16 = bswap_16(*p16);
  580. p16++;
  581. }
  582. }
  583. count -= (G.flags & FLAG_COUNT_BYTES) ? n : 1;
  584. if ((size_t)n == ibs)
  585. G.in_full++;
  586. else {
  587. G.in_part++;
  588. if (G.flags & FLAG_SYNC) {
  589. memset(ibuf + n, 0, ibs - n);
  590. n = ibs;
  591. }
  592. }
  593. #if ENABLE_FEATURE_DD_IBS_OBS
  594. if (G.flags & FLAG_TWOBUFS) {
  595. char *tmp = ibuf;
  596. while (n) {
  597. size_t d = obs - ocount;
  598. if (d > (size_t)n)
  599. d = n;
  600. memcpy(obuf + ocount, tmp, d);
  601. n -= d;
  602. tmp += d;
  603. ocount += d;
  604. if (ocount == obs) {
  605. if (write_and_stats(obuf, obs, obs, outfile))
  606. goto out_status;
  607. ocount = 0;
  608. }
  609. }
  610. } else
  611. #endif
  612. {
  613. if (write_and_stats(ibuf, n, obs, outfile))
  614. goto out_status;
  615. }
  616. }
  617. if (G.flags & FLAG_FSYNC) {
  618. if (fsync(ofd) < 0)
  619. goto die_outfile;
  620. }
  621. #if ENABLE_FEATURE_DD_IBS_OBS
  622. if (ocount != 0) {
  623. if (write_and_stats(obuf, ocount, obs, outfile))
  624. goto out_status;
  625. }
  626. #endif
  627. if (close(ifd) < 0) {
  628. die_infile:
  629. bb_simple_perror_msg_and_die(infile);
  630. }
  631. if (close(ofd) < 0) {
  632. die_outfile:
  633. bb_simple_perror_msg_and_die(outfile);
  634. }
  635. exitcode = EXIT_SUCCESS;
  636. out_status:
  637. if (!ENABLE_FEATURE_DD_STATUS || !(G.flags & FLAG_STATUS_NONE))
  638. dd_output_status(0);
  639. #if 0 /* can't just free(), they can be mmap()ed */
  640. if (ENABLE_FEATURE_CLEAN_UP) {
  641. free(obuf);
  642. if (G.flags & FLAG_TWOBUFS)
  643. free(ibuf);
  644. }
  645. #endif
  646. return exitcode;
  647. }