blob: 627e7e7b54ea0793e28e6d2cd21a1343b20b83e8 [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;
37};
38#define G (*(struct globals*)&bb_common_bufsiz1)
Denis Vlasenko75878702007-07-27 15:02:00 +000039/* We have to zero it out because of NOEXEC */
40#define INIT_G() memset(&G, 0, sizeof(G))
41
Rob Landleyc5598172006-05-02 22:44:04 +000042
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000043static void dd_output_status(int UNUSED_PARAM cur_signal)
Rob Landleyc5598172006-05-02 22:44:04 +000044{
Denis Vlasenko980864d2007-07-30 10:58:09 +000045 /* Deliberately using %u, not %d */
46 fprintf(stderr, "%"OFF_FMT"u+%"OFF_FMT"u records in\n"
47 "%"OFF_FMT"u+%"OFF_FMT"u records out\n",
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000048 G.in_full, G.in_part,
49 G.out_full, G.out_part);
Rob Landleyc5598172006-05-02 22:44:04 +000050}
51
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +000052static ssize_t full_write_or_warn(const void *buf, size_t len,
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000053 const char *const filename)
Denis Vlasenko3b8ff682006-10-31 15:55:56 +000054{
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +000055 ssize_t n = full_write(ofd, buf, len);
Denis Vlasenko3b8ff682006-10-31 15:55:56 +000056 if (n < 0)
57 bb_perror_msg("writing '%s'", filename);
58 return n;
59}
60
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +000061static bool write_and_stats(const void *buf, size_t len, size_t obs,
Denis Vlasenko990d0f62007-07-24 15:54:42 +000062 const char *filename)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000063{
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +000064 ssize_t n = full_write_or_warn(buf, len, filename);
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000065 if (n < 0)
66 return 1;
Denis Vlasenko77ad97f2008-05-13 02:27:31 +000067 if ((size_t)n == obs)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000068 G.out_full++;
Denis Vlasenkod1801a42007-04-19 20:08:19 +000069 else if (n) /* > 0 */
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000070 G.out_part++;
71 return 0;
72}
73
Denis Vlasenko5dd27b12006-11-25 14:46:21 +000074#if ENABLE_LFS
75#define XATOU_SFX xatoull_sfx
76#else
77#define XATOU_SFX xatoul_sfx
78#endif
79
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000080int dd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000081int dd_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen3cf52d11999-10-12 22:26:06 +000082{
Denis Vlasenko3b8ff682006-10-31 15:55:56 +000083 enum {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +000084 /* Must be in the same order as OP_conv_XXX! */
85 /* (see "flags |= (1 << what)" below) */
86 FLAG_NOTRUNC = 1 << 0,
87 FLAG_SYNC = 1 << 1,
88 FLAG_NOERROR = 1 << 2,
89 FLAG_FSYNC = 1 << 3,
90 /* end of conv flags */
91 FLAG_TWOBUFS = 1 << 4,
92 FLAG_COUNT = 1 << 5,
Denis Vlasenko3b8ff682006-10-31 15:55:56 +000093 };
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000094 static const char keywords[] ALIGN1 =
Denis Vlasenko9d4a17e2008-04-23 05:44:59 +000095 "bs\0""count\0""seek\0""skip\0""if\0""of\0"
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000096#if ENABLE_FEATURE_DD_IBS_OBS
Denis Vlasenko9d4a17e2008-04-23 05:44:59 +000097 "ibs\0""obs\0""conv\0"
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000098#endif
Denis Vlasenko990d0f62007-07-24 15:54:42 +000099 ;
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000100#if ENABLE_FEATURE_DD_IBS_OBS
101 static const char conv_words[] ALIGN1 =
102 "notrunc\0""sync\0""noerror\0""fsync\0";
103#endif
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000104 enum {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000105 OP_bs = 0,
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000106 OP_count,
107 OP_seek,
108 OP_skip,
109 OP_if,
110 OP_of,
Bernhard Reutner-Fischere468ef22007-04-16 12:21:05 +0000111#if ENABLE_FEATURE_DD_IBS_OBS
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000112 OP_ibs,
113 OP_obs,
114 OP_conv,
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000115 /* Must be in the same order as FLAG_XXX! */
116 OP_conv_notrunc = 0,
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000117 OP_conv_sync,
Denis Vlasenkof6250a32007-04-19 20:16:14 +0000118 OP_conv_noerror,
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000119 OP_conv_fsync,
120 /* Unimplemented conv=XXX: */
121 //nocreat do not create the output file
122 //excl fail if the output file already exists
123 //fdatasync physically write output file data before finishing
124 //swab swap every pair of input bytes
125 //lcase change upper case to lower case
126 //ucase change lower case to upper case
127 //block pad newline-terminated records with spaces to cbs-size
128 //unblock replace trailing spaces in cbs-size records with newline
129 //ascii from EBCDIC to ASCII
130 //ebcdic from ASCII to EBCDIC
131 //ibm from ASCII to alternate EBCDIC
Bernhard Reutner-Fischere468ef22007-04-16 12:21:05 +0000132#endif
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000133 };
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000134 int exitcode = EXIT_FAILURE;
Denis Vlasenko75878702007-07-27 15:02:00 +0000135 size_t ibs = 512, obs = 512;
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000136 ssize_t n, w;
Rob Landley2686d3b2006-05-16 16:52:12 +0000137 char *ibuf, *obuf;
Denis Vlasenko75878702007-07-27 15:02:00 +0000138 /* And these are all zeroed at once! */
139 struct {
140 int flags;
Denis Vlasenko75878702007-07-27 15:02:00 +0000141 size_t oc;
142 off_t count;
143 off_t seek, skip;
144 const char *infile, *outfile;
Denis Vlasenko75878702007-07-27 15:02:00 +0000145 } Z;
146#define flags (Z.flags )
Denis Vlasenko75878702007-07-27 15:02:00 +0000147#define oc (Z.oc )
148#define count (Z.count )
149#define seek (Z.seek )
150#define skip (Z.skip )
151#define infile (Z.infile )
152#define outfile (Z.outfile)
Eric Andersenddea3682000-11-29 22:33:02 +0000153
Denis Vlasenko75878702007-07-27 15:02:00 +0000154 memset(&Z, 0, sizeof(Z));
155 INIT_G();
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000156 //fflush(NULL); - is this needed because of NOEXEC?
Denis Vlasenkof6250a32007-04-19 20:16:14 +0000157
Denis Vlasenko75878702007-07-27 15:02:00 +0000158#if ENABLE_FEATURE_DD_SIGNAL_HANDLING
Denis Vlasenko8e2cfec2008-03-12 23:19:35 +0000159 signal_SA_RESTART_empty_mask(SIGUSR1, dd_output_status);
Denis Vlasenko75878702007-07-27 15:02:00 +0000160#endif
Rob Landleyc5598172006-05-02 22:44:04 +0000161
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000162 for (n = 1; argv[n]; n++) {
163 int what;
164 char *val;
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000165 char *arg = argv[n];
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000166
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000167#if ENABLE_DESKTOP
168 /* "dd --". NB: coreutils 6.9 will complain if they see
169 * more than one of them. We wouldn't. */
170 if (arg[0] == '-' && arg[1] == '-' && arg[2] == '\0')
171 continue;
172#endif
173 val = strchr(arg, '=');
174 if (val == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000175 bb_show_usage();
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000176 *val = '\0';
177 what = index_in_strings(keywords, arg);
178 if (what < 0)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000179 bb_show_usage();
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000180 /* *val = '='; - to preserve ps listing? */
181 val++;
Bernhard Reutner-Fischere468ef22007-04-16 12:21:05 +0000182#if ENABLE_FEATURE_DD_IBS_OBS
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000183 if (what == OP_ibs) {
184 /* Must fit into positive ssize_t */
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000185 ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes);
186 /*continue;*/
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000187 }
188 if (what == OP_obs) {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000189 obs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes);
190 /*continue;*/
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000191 }
192 if (what == OP_conv) {
193 while (1) {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000194 /* find ',', replace them with NUL so we can use val for
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000195 * index_in_strings() without copying.
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000196 * We rely on val being non-null, else strchr would fault.
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000197 */
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000198 arg = strchr(val, ',');
199 if (arg)
200 *arg = '\0';
201 what = index_in_strings(conv_words, val);
202 if (what < 0)
203 bb_error_msg_and_die(bb_msg_invalid_arg, val, "conv");
204 flags |= (1 << what);
205 if (!arg) /* no ',' left, so this was the last specifier */
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000206 break;
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000207 /* *arg = ','; - to preserve ps listing? */
208 val = arg + 1; /* skip this keyword and ',' */
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000209 }
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000210 continue; /* we trashed 'what', can't fall through */
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000211 }
Bernhard Reutner-Fischere468ef22007-04-16 12:21:05 +0000212#endif
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000213 if (what == OP_bs) {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000214 ibs = obs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes);
215 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000216 }
217 /* These can be large: */
218 if (what == OP_count) {
Denis Vlasenko75878702007-07-27 15:02:00 +0000219 flags |= FLAG_COUNT;
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000220 count = XATOU_SFX(val, dd_suffixes);
221 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000222 }
223 if (what == OP_seek) {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000224 seek = XATOU_SFX(val, dd_suffixes);
225 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000226 }
Denis Vlasenkod1801a42007-04-19 20:08:19 +0000227 if (what == OP_skip) {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000228 skip = XATOU_SFX(val, dd_suffixes);
229 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000230 }
231 if (what == OP_if) {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000232 infile = val;
233 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000234 }
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000235 if (what == OP_of) {
236 outfile = val;
237 /*continue;*/
238 }
239 } /* end of "for (argv[n])" */
240
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000241//XXX:FIXME for huge ibs or obs, malloc'ing them isn't the brightest idea ever
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000242 ibuf = obuf = xmalloc(ibs);
243 if (ibs != obs) {
Denis Vlasenko75878702007-07-27 15:02:00 +0000244 flags |= FLAG_TWOBUFS;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000245 obuf = xmalloc(obs);
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000246 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000247 if (infile != NULL)
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000248 xmove_fd(xopen(infile, O_RDONLY), ifd);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000249 else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000250 infile = bb_msg_standard_input;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000251 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000252 if (outfile != NULL) {
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000253 int oflag = O_WRONLY | O_CREAT;
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000254
Denis Vlasenko75878702007-07-27 15:02:00 +0000255 if (!seek && !(flags & FLAG_NOTRUNC))
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000256 oflag |= O_TRUNC;
257
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000258 xmove_fd(xopen(outfile, oflag), ofd);
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000259
Denis Vlasenko75878702007-07-27 15:02:00 +0000260 if (seek && !(flags & FLAG_NOTRUNC)) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000261 if (ftruncate(ofd, seek * obs) < 0) {
Matt Kraaieb834782002-02-05 15:28:54 +0000262 struct stat st;
263
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000264 if (fstat(ofd, &st) < 0 || S_ISREG(st.st_mode) ||
265 S_ISDIR(st.st_mode))
266 goto die_outfile;
Matt Kraaieb834782002-02-05 15:28:54 +0000267 }
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000268 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000269 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000270 outfile = bb_msg_standard_output;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000271 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000272 if (skip) {
Denis Vlasenko7039a662006-10-08 17:54:47 +0000273 if (lseek(ifd, skip * ibs, SEEK_CUR) < 0) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000274 while (skip-- > 0) {
275 n = safe_read(ifd, ibuf, ibs);
276 if (n < 0)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000277 goto die_infile;
Rob Landley2686d3b2006-05-16 16:52:12 +0000278 if (n == 0)
279 break;
280 }
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000281 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000282 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000283 if (seek) {
Denis Vlasenko7039a662006-10-08 17:54:47 +0000284 if (lseek(ofd, seek * obs, SEEK_CUR) < 0)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000285 goto die_outfile;
Matt Kraai24ac0172000-12-18 21:38:57 +0000286 }
Eric Andersenddea3682000-11-29 22:33:02 +0000287
Denis Vlasenko2b03e5e2007-07-29 14:59:06 +0000288 while (!(flags & FLAG_COUNT) || (G.in_full + G.in_part != count)) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000289 n = safe_read(ifd, ibuf, ibs);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000290 if (n == 0)
Rob Landley2686d3b2006-05-16 16:52:12 +0000291 break;
Eric Andersenef38b392002-04-27 01:31:43 +0000292 if (n < 0) {
Denys Vlasenkof00cfdf2009-09-18 20:49:36 +0200293 /* "Bad block" */
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000294 if (!(flags & FLAG_NOERROR))
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000295 goto die_infile;
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000296 bb_simple_perror_msg(infile);
Denys Vlasenkof00cfdf2009-09-18 20:49:36 +0200297 /* GNU dd with conv=noerror skips over bad blocks */
Denys Vlasenko334fcc82009-08-05 23:16:27 +0200298 xlseek(ifd, ibs, SEEK_CUR);
Denys Vlasenkof00cfdf2009-09-18 20:49:36 +0200299 /* conv=noerror,sync writes NULs,
300 * conv=noerror just ignores input bad blocks */
301 n = 0;
Eric Andersenef38b392002-04-27 01:31:43 +0000302 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000303 if ((size_t)n == ibs)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000304 G.in_full++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000305 else {
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000306 G.in_part++;
Denis Vlasenko75878702007-07-27 15:02:00 +0000307 if (flags & FLAG_SYNC) {
Denys Vlasenkof00cfdf2009-09-18 20:49:36 +0200308 memset(ibuf + n, 0, ibs - n);
Rob Landley2686d3b2006-05-16 16:52:12 +0000309 n = ibs;
310 }
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000311 }
Denis Vlasenko75878702007-07-27 15:02:00 +0000312 if (flags & FLAG_TWOBUFS) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000313 char *tmp = ibuf;
314 while (n) {
315 size_t d = obs - oc;
316
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000317 if (d > (size_t)n)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000318 d = n;
Rob Landley2686d3b2006-05-16 16:52:12 +0000319 memcpy(obuf + oc, tmp, d);
320 n -= d;
321 tmp += d;
322 oc += d;
323 if (oc == obs) {
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000324 if (write_and_stats(obuf, obs, obs, outfile))
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000325 goto out_status;
Rob Landley2686d3b2006-05-16 16:52:12 +0000326 oc = 0;
327 }
328 }
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000329 } else if (write_and_stats(ibuf, n, obs, outfile))
Denis Vlasenkod1801a42007-04-19 20:08:19 +0000330 goto out_status;
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000331
332 if (flags & FLAG_FSYNC) {
333 if (fsync(ofd) < 0)
334 goto die_outfile;
335 }
Glenn L McGrathf0b073f2000-09-11 00:32:13 +0000336 }
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000337
Rob Landley2686d3b2006-05-16 16:52:12 +0000338 if (ENABLE_FEATURE_DD_IBS_OBS && oc) {
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000339 w = full_write_or_warn(obuf, oc, outfile);
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000340 if (w < 0) goto out_status;
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000341 if (w > 0) G.out_part++;
Rob Landley2686d3b2006-05-16 16:52:12 +0000342 }
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000343 if (close(ifd) < 0) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000344 die_infile:
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000345 bb_simple_perror_msg_and_die(infile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000346 }
Matt Kraai3eeab3b2001-12-07 16:27:37 +0000347
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000348 if (close(ofd) < 0) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000349 die_outfile:
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000350 bb_simple_perror_msg_and_die(outfile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000351 }
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000352
353 exitcode = EXIT_SUCCESS;
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000354 out_status:
Rob Landleyc5598172006-05-02 22:44:04 +0000355 dd_output_status(0);
Matt Kraai24ac0172000-12-18 21:38:57 +0000356
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000357 return exitcode;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000358}