blob: 8173d403d0dec6261203b22df595347cbdd5f2d4 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersencc8ed391999-10-05 16:24:54 +00002/*
Eric Andersenc4996011999-10-20 22:08:37 +00003 * Mini dd implementation for busybox
4 *
Eric Andersenc4996011999-10-20 22:08:37 +00005 *
Matt Kraai3eeab3b2001-12-07 16:27:37 +00006 * Copyright (C) 2000,2001 Matt Kraai
Eric Andersenc4996011999-10-20 22:08:37 +00007 *
Bernhard Reutner-Fischerc2cb0f32006-04-13 12:45:04 +00008 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersencc8ed391999-10-05 16:24:54 +00009 */
10
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000011#include "libbb.h"
Denis Vlasenko99912ca2007-04-10 15:43:37 +000012
13/* This is a NOEXEC applet. Be very careful! */
14
Eric Andersencbe31da2001-02-20 06:14:08 +000015
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +000016enum {
17 ifd = STDIN_FILENO,
18 ofd = STDOUT_FILENO,
19};
20
Matt Kraaia164c642001-02-05 17:50:03 +000021static const struct suffix_mult dd_suffixes[] = {
Matt Kraai24ac0172000-12-18 21:38:57 +000022 { "c", 1 },
23 { "w", 2 },
24 { "b", 512 },
25 { "kD", 1000 },
26 { "k", 1024 },
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000027 { "K", 1024 }, /* compat with coreutils dd */
Matt Kraai24ac0172000-12-18 21:38:57 +000028 { "MD", 1000000 },
29 { "M", 1048576 },
30 { "GD", 1000000000 },
31 { "G", 1073741824 },
Denys Vlasenko043b1e52009-09-06 12:47:55 +020032 { "", 0 }
Matt Kraai24ac0172000-12-18 21:38:57 +000033};
34
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000035struct globals {
36 off_t out_full, out_part, in_full, in_part;
Gabor Heja4e5b07b2009-10-14 00:29:28 +020037#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
38 unsigned long long total_bytes;
39 unsigned long long begin_time_us;
40#endif
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000041};
42#define G (*(struct globals*)&bb_common_bufsiz1)
Denys Vlasenko1b34d4f2009-09-30 02:39:57 +020043#define INIT_G() do { \
44 /* we have to zero it out because of NOEXEC */ \
45 memset(&G, 0, sizeof(G)); \
46} while (0)
Denis Vlasenko75878702007-07-27 15:02:00 +000047
Rob Landleyc5598172006-05-02 22:44:04 +000048
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000049static void dd_output_status(int UNUSED_PARAM cur_signal)
Rob Landleyc5598172006-05-02 22:44:04 +000050{
Gabor Heja4e5b07b2009-10-14 00:29:28 +020051#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
Denys Vlasenko7eabffa2009-10-14 17:43:41 +020052 double seconds;
53 unsigned long long bytes_sec;
54 unsigned long long now_us = monotonic_us(); /* before fprintf */
Gabor Heja4e5b07b2009-10-14 00:29:28 +020055#endif
56
Denis Vlasenko980864d2007-07-30 10:58:09 +000057 /* Deliberately using %u, not %d */
58 fprintf(stderr, "%"OFF_FMT"u+%"OFF_FMT"u records in\n"
59 "%"OFF_FMT"u+%"OFF_FMT"u records out\n",
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000060 G.in_full, G.in_part,
61 G.out_full, G.out_part);
Gabor Heja4e5b07b2009-10-14 00:29:28 +020062
63#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
64 fprintf(stderr, "%llu bytes (%sB) copied, ",
65 G.total_bytes,
66 /* show fractional digit, use suffixes */
67 make_human_readable_str(G.total_bytes, 1, 0)
68 );
69 /* Corner cases:
70 * ./busybox dd </dev/null >/dev/null
71 * ./busybox dd bs=1M count=2000 </dev/zero >/dev/null
72 * (echo DONE) | ./busybox dd >/dev/null
73 * (sleep 1; echo DONE) | ./busybox dd >/dev/null
74 */
Denys Vlasenko7eabffa2009-10-14 17:43:41 +020075 seconds = (now_us - G.begin_time_us) / 1000000.0;
76 bytes_sec = G.total_bytes / seconds;
Gabor Heja4e5b07b2009-10-14 00:29:28 +020077 fprintf(stderr, "%f seconds, %sB/s\n",
Denys Vlasenko7eabffa2009-10-14 17:43:41 +020078 seconds,
Gabor Heja4e5b07b2009-10-14 00:29:28 +020079 /* show fractional digit, use suffixes */
Denys Vlasenko7eabffa2009-10-14 17:43:41 +020080 make_human_readable_str(bytes_sec, 1, 0)
Gabor Heja4e5b07b2009-10-14 00:29:28 +020081 );
82#endif
Rob Landleyc5598172006-05-02 22:44:04 +000083}
84
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +000085static ssize_t full_write_or_warn(const void *buf, size_t len,
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000086 const char *const filename)
Denis Vlasenko3b8ff682006-10-31 15:55:56 +000087{
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +000088 ssize_t n = full_write(ofd, buf, len);
Denis Vlasenko3b8ff682006-10-31 15:55:56 +000089 if (n < 0)
90 bb_perror_msg("writing '%s'", filename);
91 return n;
92}
93
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +000094static bool write_and_stats(const void *buf, size_t len, size_t obs,
Denis Vlasenko990d0f62007-07-24 15:54:42 +000095 const char *filename)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000096{
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +000097 ssize_t n = full_write_or_warn(buf, len, filename);
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000098 if (n < 0)
99 return 1;
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000100 if ((size_t)n == obs)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000101 G.out_full++;
Denis Vlasenkod1801a42007-04-19 20:08:19 +0000102 else if (n) /* > 0 */
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000103 G.out_part++;
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200104#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
105 G.total_bytes += n;
106#endif
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000107 return 0;
108}
109
Denis Vlasenko5dd27b12006-11-25 14:46:21 +0000110#if ENABLE_LFS
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200111# define XATOU_SFX xatoull_sfx
Denis Vlasenko5dd27b12006-11-25 14:46:21 +0000112#else
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200113# define XATOU_SFX xatoul_sfx
Denis Vlasenko5dd27b12006-11-25 14:46:21 +0000114#endif
115
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000116int dd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000117int dd_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen3cf52d11999-10-12 22:26:06 +0000118{
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000119 enum {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000120 /* Must be in the same order as OP_conv_XXX! */
121 /* (see "flags |= (1 << what)" below) */
122 FLAG_NOTRUNC = 1 << 0,
123 FLAG_SYNC = 1 << 1,
124 FLAG_NOERROR = 1 << 2,
125 FLAG_FSYNC = 1 << 3,
126 /* end of conv flags */
127 FLAG_TWOBUFS = 1 << 4,
128 FLAG_COUNT = 1 << 5,
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000129 };
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000130 static const char keywords[] ALIGN1 =
Denis Vlasenko9d4a17e2008-04-23 05:44:59 +0000131 "bs\0""count\0""seek\0""skip\0""if\0""of\0"
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000132#if ENABLE_FEATURE_DD_IBS_OBS
Denis Vlasenko9d4a17e2008-04-23 05:44:59 +0000133 "ibs\0""obs\0""conv\0"
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000134#endif
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000135 ;
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000136#if ENABLE_FEATURE_DD_IBS_OBS
137 static const char conv_words[] ALIGN1 =
138 "notrunc\0""sync\0""noerror\0""fsync\0";
139#endif
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000140 enum {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000141 OP_bs = 0,
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000142 OP_count,
143 OP_seek,
144 OP_skip,
145 OP_if,
146 OP_of,
Bernhard Reutner-Fischere468ef22007-04-16 12:21:05 +0000147#if ENABLE_FEATURE_DD_IBS_OBS
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000148 OP_ibs,
149 OP_obs,
150 OP_conv,
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000151 /* Must be in the same order as FLAG_XXX! */
152 OP_conv_notrunc = 0,
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000153 OP_conv_sync,
Denis Vlasenkof6250a32007-04-19 20:16:14 +0000154 OP_conv_noerror,
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000155 OP_conv_fsync,
156 /* Unimplemented conv=XXX: */
157 //nocreat do not create the output file
158 //excl fail if the output file already exists
159 //fdatasync physically write output file data before finishing
160 //swab swap every pair of input bytes
161 //lcase change upper case to lower case
162 //ucase change lower case to upper case
163 //block pad newline-terminated records with spaces to cbs-size
164 //unblock replace trailing spaces in cbs-size records with newline
165 //ascii from EBCDIC to ASCII
166 //ebcdic from ASCII to EBCDIC
167 //ibm from ASCII to alternate EBCDIC
Bernhard Reutner-Fischere468ef22007-04-16 12:21:05 +0000168#endif
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000169 };
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000170 int exitcode = EXIT_FAILURE;
Denis Vlasenko75878702007-07-27 15:02:00 +0000171 size_t ibs = 512, obs = 512;
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000172 ssize_t n, w;
Rob Landley2686d3b2006-05-16 16:52:12 +0000173 char *ibuf, *obuf;
Denis Vlasenko75878702007-07-27 15:02:00 +0000174 /* And these are all zeroed at once! */
175 struct {
176 int flags;
Denis Vlasenko75878702007-07-27 15:02:00 +0000177 size_t oc;
178 off_t count;
179 off_t seek, skip;
180 const char *infile, *outfile;
Denis Vlasenko75878702007-07-27 15:02:00 +0000181 } Z;
182#define flags (Z.flags )
Denis Vlasenko75878702007-07-27 15:02:00 +0000183#define oc (Z.oc )
184#define count (Z.count )
185#define seek (Z.seek )
186#define skip (Z.skip )
187#define infile (Z.infile )
188#define outfile (Z.outfile)
Eric Andersenddea3682000-11-29 22:33:02 +0000189
Denis Vlasenko75878702007-07-27 15:02:00 +0000190 memset(&Z, 0, sizeof(Z));
191 INIT_G();
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100192 //fflush_all(); - is this needed because of NOEXEC?
Denis Vlasenkof6250a32007-04-19 20:16:14 +0000193
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000194 for (n = 1; argv[n]; n++) {
195 int what;
196 char *val;
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000197 char *arg = argv[n];
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000198
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000199#if ENABLE_DESKTOP
200 /* "dd --". NB: coreutils 6.9 will complain if they see
201 * more than one of them. We wouldn't. */
202 if (arg[0] == '-' && arg[1] == '-' && arg[2] == '\0')
203 continue;
204#endif
205 val = strchr(arg, '=');
206 if (val == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000207 bb_show_usage();
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000208 *val = '\0';
209 what = index_in_strings(keywords, arg);
210 if (what < 0)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000211 bb_show_usage();
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000212 /* *val = '='; - to preserve ps listing? */
213 val++;
Bernhard Reutner-Fischere468ef22007-04-16 12:21:05 +0000214#if ENABLE_FEATURE_DD_IBS_OBS
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000215 if (what == OP_ibs) {
216 /* Must fit into positive ssize_t */
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000217 ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes);
218 /*continue;*/
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000219 }
220 if (what == OP_obs) {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000221 obs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes);
222 /*continue;*/
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000223 }
224 if (what == OP_conv) {
225 while (1) {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000226 /* find ',', replace them with NUL so we can use val for
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000227 * index_in_strings() without copying.
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000228 * We rely on val being non-null, else strchr would fault.
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000229 */
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000230 arg = strchr(val, ',');
231 if (arg)
232 *arg = '\0';
233 what = index_in_strings(conv_words, val);
234 if (what < 0)
235 bb_error_msg_and_die(bb_msg_invalid_arg, val, "conv");
236 flags |= (1 << what);
237 if (!arg) /* no ',' left, so this was the last specifier */
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000238 break;
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000239 /* *arg = ','; - to preserve ps listing? */
240 val = arg + 1; /* skip this keyword and ',' */
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000241 }
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000242 continue; /* we trashed 'what', can't fall through */
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000243 }
Bernhard Reutner-Fischere468ef22007-04-16 12:21:05 +0000244#endif
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000245 if (what == OP_bs) {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000246 ibs = obs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes);
247 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000248 }
249 /* These can be large: */
250 if (what == OP_count) {
Denis Vlasenko75878702007-07-27 15:02:00 +0000251 flags |= FLAG_COUNT;
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000252 count = XATOU_SFX(val, dd_suffixes);
253 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000254 }
255 if (what == OP_seek) {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000256 seek = XATOU_SFX(val, dd_suffixes);
257 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000258 }
Denis Vlasenkod1801a42007-04-19 20:08:19 +0000259 if (what == OP_skip) {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000260 skip = XATOU_SFX(val, dd_suffixes);
261 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000262 }
263 if (what == OP_if) {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000264 infile = val;
265 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000266 }
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000267 if (what == OP_of) {
268 outfile = val;
269 /*continue;*/
270 }
271 } /* end of "for (argv[n])" */
272
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000273//XXX:FIXME for huge ibs or obs, malloc'ing them isn't the brightest idea ever
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000274 ibuf = obuf = xmalloc(ibs);
275 if (ibs != obs) {
Denis Vlasenko75878702007-07-27 15:02:00 +0000276 flags |= FLAG_TWOBUFS;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000277 obuf = xmalloc(obs);
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000278 }
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200279
280#if ENABLE_FEATURE_DD_SIGNAL_HANDLING
281 signal_SA_RESTART_empty_mask(SIGUSR1, dd_output_status);
282#endif
283#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
284 G.begin_time_us = monotonic_us();
285#endif
286
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000287 if (infile != NULL)
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000288 xmove_fd(xopen(infile, O_RDONLY), ifd);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000289 else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000290 infile = bb_msg_standard_input;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000291 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000292 if (outfile != NULL) {
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000293 int oflag = O_WRONLY | O_CREAT;
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000294
Denis Vlasenko75878702007-07-27 15:02:00 +0000295 if (!seek && !(flags & FLAG_NOTRUNC))
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000296 oflag |= O_TRUNC;
297
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000298 xmove_fd(xopen(outfile, oflag), ofd);
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000299
Denis Vlasenko75878702007-07-27 15:02:00 +0000300 if (seek && !(flags & FLAG_NOTRUNC)) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000301 if (ftruncate(ofd, seek * obs) < 0) {
Matt Kraaieb834782002-02-05 15:28:54 +0000302 struct stat st;
303
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100304 if (fstat(ofd, &st) < 0
305 || S_ISREG(st.st_mode)
306 || S_ISDIR(st.st_mode)
307 ) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000308 goto die_outfile;
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100309 }
Matt Kraaieb834782002-02-05 15:28:54 +0000310 }
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000311 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000312 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000313 outfile = bb_msg_standard_output;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000314 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000315 if (skip) {
Denis Vlasenko7039a662006-10-08 17:54:47 +0000316 if (lseek(ifd, skip * ibs, SEEK_CUR) < 0) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000317 while (skip-- > 0) {
318 n = safe_read(ifd, ibuf, ibs);
319 if (n < 0)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000320 goto die_infile;
Rob Landley2686d3b2006-05-16 16:52:12 +0000321 if (n == 0)
322 break;
323 }
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000324 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000325 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000326 if (seek) {
Denis Vlasenko7039a662006-10-08 17:54:47 +0000327 if (lseek(ofd, seek * obs, SEEK_CUR) < 0)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000328 goto die_outfile;
Matt Kraai24ac0172000-12-18 21:38:57 +0000329 }
Eric Andersenddea3682000-11-29 22:33:02 +0000330
Denis Vlasenko2b03e5e2007-07-29 14:59:06 +0000331 while (!(flags & FLAG_COUNT) || (G.in_full + G.in_part != count)) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000332 n = safe_read(ifd, ibuf, ibs);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000333 if (n == 0)
Rob Landley2686d3b2006-05-16 16:52:12 +0000334 break;
Eric Andersenef38b392002-04-27 01:31:43 +0000335 if (n < 0) {
Denys Vlasenkof00cfdf2009-09-18 20:49:36 +0200336 /* "Bad block" */
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000337 if (!(flags & FLAG_NOERROR))
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000338 goto die_infile;
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000339 bb_simple_perror_msg(infile);
Denys Vlasenkof00cfdf2009-09-18 20:49:36 +0200340 /* GNU dd with conv=noerror skips over bad blocks */
Denys Vlasenko334fcc82009-08-05 23:16:27 +0200341 xlseek(ifd, ibs, SEEK_CUR);
Denys Vlasenkof00cfdf2009-09-18 20:49:36 +0200342 /* conv=noerror,sync writes NULs,
343 * conv=noerror just ignores input bad blocks */
344 n = 0;
Eric Andersenef38b392002-04-27 01:31:43 +0000345 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000346 if ((size_t)n == ibs)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000347 G.in_full++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000348 else {
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000349 G.in_part++;
Denis Vlasenko75878702007-07-27 15:02:00 +0000350 if (flags & FLAG_SYNC) {
Denys Vlasenkof00cfdf2009-09-18 20:49:36 +0200351 memset(ibuf + n, 0, ibs - n);
Rob Landley2686d3b2006-05-16 16:52:12 +0000352 n = ibs;
353 }
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000354 }
Denis Vlasenko75878702007-07-27 15:02:00 +0000355 if (flags & FLAG_TWOBUFS) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000356 char *tmp = ibuf;
357 while (n) {
358 size_t d = obs - oc;
359
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000360 if (d > (size_t)n)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000361 d = n;
Rob Landley2686d3b2006-05-16 16:52:12 +0000362 memcpy(obuf + oc, tmp, d);
363 n -= d;
364 tmp += d;
365 oc += d;
366 if (oc == obs) {
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000367 if (write_and_stats(obuf, obs, obs, outfile))
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000368 goto out_status;
Rob Landley2686d3b2006-05-16 16:52:12 +0000369 oc = 0;
370 }
371 }
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000372 } else if (write_and_stats(ibuf, n, obs, outfile))
Denis Vlasenkod1801a42007-04-19 20:08:19 +0000373 goto out_status;
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000374
375 if (flags & FLAG_FSYNC) {
376 if (fsync(ofd) < 0)
377 goto die_outfile;
378 }
Glenn L McGrathf0b073f2000-09-11 00:32:13 +0000379 }
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000380
Rob Landley2686d3b2006-05-16 16:52:12 +0000381 if (ENABLE_FEATURE_DD_IBS_OBS && oc) {
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000382 w = full_write_or_warn(obuf, oc, outfile);
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000383 if (w < 0) goto out_status;
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000384 if (w > 0) G.out_part++;
Rob Landley2686d3b2006-05-16 16:52:12 +0000385 }
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000386 if (close(ifd) < 0) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000387 die_infile:
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000388 bb_simple_perror_msg_and_die(infile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000389 }
Matt Kraai3eeab3b2001-12-07 16:27:37 +0000390
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000391 if (close(ofd) < 0) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000392 die_outfile:
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000393 bb_simple_perror_msg_and_die(outfile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000394 }
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000395
396 exitcode = EXIT_SUCCESS;
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000397 out_status:
Rob Landleyc5598172006-05-02 22:44:04 +0000398 dd_output_status(0);
Matt Kraai24ac0172000-12-18 21:38:57 +0000399
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000400 return exitcode;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000401}