blob: 34a325ea6ffeee63b1aa5178ebb6cedb82b20558 [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
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000011#include <signal.h> /* For FEATURE_DD_SIGNAL_HANDLING */
Denis Vlasenko99912ca2007-04-10 15:43:37 +000012#include "busybox.h"
13
14/* This is a NOEXEC applet. Be very careful! */
15
Eric Andersencbe31da2001-02-20 06:14:08 +000016
Matt Kraaia164c642001-02-05 17:50:03 +000017static const struct suffix_mult dd_suffixes[] = {
Matt Kraai24ac0172000-12-18 21:38:57 +000018 { "c", 1 },
19 { "w", 2 },
20 { "b", 512 },
21 { "kD", 1000 },
22 { "k", 1024 },
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000023 { "K", 1024 }, /* compat with coreutils dd */
Matt Kraai24ac0172000-12-18 21:38:57 +000024 { "MD", 1000000 },
25 { "M", 1048576 },
26 { "GD", 1000000000 },
27 { "G", 1073741824 },
28 { NULL, 0 }
29};
30
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000031struct globals {
32 off_t out_full, out_part, in_full, in_part;
33};
34#define G (*(struct globals*)&bb_common_bufsiz1)
Rob Landleyc5598172006-05-02 22:44:04 +000035
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000036static void dd_output_status(int ATTRIBUTE_UNUSED cur_signal)
Rob Landleyc5598172006-05-02 22:44:04 +000037{
Denis Vlasenkocf30cc82006-11-24 14:53:18 +000038 fprintf(stderr, "%"OFF_FMT"d+%"OFF_FMT"d records in\n"
39 "%"OFF_FMT"d+%"OFF_FMT"d records out\n",
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000040 G.in_full, G.in_part,
41 G.out_full, G.out_part);
Rob Landleyc5598172006-05-02 22:44:04 +000042}
43
Denis Vlasenko3b8ff682006-10-31 15:55:56 +000044static ssize_t full_write_or_warn(int fd, const void *buf, size_t len,
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000045 const char * const filename)
Denis Vlasenko3b8ff682006-10-31 15:55:56 +000046{
47 ssize_t n = full_write(fd, buf, len);
48 if (n < 0)
49 bb_perror_msg("writing '%s'", filename);
50 return n;
51}
52
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000053static bool write_and_stats(int fd, const void *buf, size_t len, size_t obs,
54 const char * const filename)
55{
56 ssize_t n = full_write_or_warn(fd, buf, len, filename);
57 if (n < 0)
58 return 1;
59 if (n == obs)
60 G.out_full++;
61 else if (n > 0)
62 G.out_part++;
63 return 0;
64}
65
Denis Vlasenko5dd27b12006-11-25 14:46:21 +000066#if ENABLE_LFS
67#define XATOU_SFX xatoull_sfx
68#else
69#define XATOU_SFX xatoul_sfx
70#endif
71
Denis Vlasenko06af2162007-02-03 17:28:39 +000072int dd_main(int argc, char **argv);
Matt Kraai24ac0172000-12-18 21:38:57 +000073int dd_main(int argc, char **argv)
Eric Andersen3cf52d11999-10-12 22:26:06 +000074{
Denis Vlasenko3b8ff682006-10-31 15:55:56 +000075 enum {
76 sync_flag = 1 << 0,
77 noerror = 1 << 1,
78 trunc_flag = 1 << 2,
79 twobufs_flag = 1 << 3,
80 };
Denis Vlasenko240a1cf2007-04-08 16:07:02 +000081 static const char * const keywords[] = {
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000082 "bs=", "count=", "seek=", "skip=", "if=", "of=",
83#if ENABLE_FEATURE_DD_IBS_OBS
84 "ibs=", "obs=", "conv=", "notrunc", "sync", "noerror",
85#endif
86 NULL
87 };
88#define OP_bs 0 + 1
89#define OP_count OP_bs + 1
90#define OP_seek OP_count + 1
91#define OP_skip OP_seek + 1
92#define OP_if OP_skip + 1
93#define OP_of OP_if + 1
94#define OP_ibs OP_of + ENABLE_FEATURE_DD_IBS_OBS
95#define OP_obs OP_ibs + ENABLE_FEATURE_DD_IBS_OBS
96#define OP_conv OP_obs + ENABLE_FEATURE_DD_IBS_OBS
97#define OP_conv_notrunc OP_conv + ENABLE_FEATURE_DD_IBS_OBS
98#define OP_conv_sync OP_conv_notrunc + ENABLE_FEATURE_DD_IBS_OBS
99#define OP_conv_noerror OP_conv_sync + ENABLE_FEATURE_DD_IBS_OBS
100
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000101 int flags = trunc_flag;
Denis Vlasenko56254152006-10-07 16:24:46 +0000102 size_t oc = 0, ibs = 512, obs = 512;
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000103 ssize_t n, w;
Denis Vlasenko7039a662006-10-08 17:54:47 +0000104 off_t seek = 0, skip = 0, count = OFF_T_MAX;
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000105 int ifd, ofd;
Rob Landley2686d3b2006-05-16 16:52:12 +0000106 const char *infile = NULL, *outfile = NULL;
107 char *ibuf, *obuf;
Eric Andersenddea3682000-11-29 22:33:02 +0000108
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000109 if (ENABLE_FEATURE_DD_SIGNAL_HANDLING) {
Rob Landleyc5598172006-05-02 22:44:04 +0000110 struct sigaction sa;
111
112 memset(&sa, 0, sizeof(sa));
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000113 sa.sa_handler = dd_output_status;
Rob Landleyc5598172006-05-02 22:44:04 +0000114 sa.sa_flags = SA_RESTART;
115 sigemptyset(&sa.sa_mask);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000116 sigaction(SIGUSR1, &sa, 0);
Rob Landleyc5598172006-05-02 22:44:04 +0000117 }
118
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000119 for (n = 1; n < argc; n++) {
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000120 smalluint key_len, what;
121 char *key;
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000122 char *arg = argv[n];
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000123
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000124//XXX:FIXME: we reject plain "dd --" This would cost ~20 bytes, so..
125//if (*arg == '-' && *++arg == '-' && !*++arg) continue;
126 key = strstr(arg, "=");
127 if (key == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000128 bb_show_usage();
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000129 key_len = key - arg + 1;
130 key = xstrndup(arg, key_len);
131 what = index_in_str_array(keywords, key) + 1;
132 if (ENABLE_FEATURE_CLEAN_UP)
133 free(key);
134 if (what == 0)
135 bb_show_usage();
136 arg += key_len;
137 /* Must fit into positive ssize_t */
138 if (ENABLE_FEATURE_DD_IBS_OBS) {
139 if (what == OP_ibs) {
140 ibs = xatoul_range_sfx(arg, 1, ((size_t)-1L)/2, dd_suffixes);
141 continue;
142 }
143 if (what == OP_obs) {
144 obs = xatoul_range_sfx(arg, 1, ((size_t)-1L)/2, dd_suffixes);
145 continue;
146 }
147 if (what == OP_conv) {
148 while (1) {
149 /* find ',', replace them with nil so we can use arg for
150 * index_in_str_array without copying.
151 * We rely on arg being non-null, else strstr would fault.
152 */
153 key = strstr(arg, ",");
154 if (key)
155 *key = '\0';
156 what = index_in_str_array(keywords, arg) + 1;
157 if (what < OP_conv_notrunc)
158 bb_error_msg_and_die(bb_msg_invalid_arg, arg, "conv");
159 if (what == OP_conv_notrunc)
160 flags &= ~trunc_flag;
161 if (what == OP_conv_sync)
162 flags |= sync_flag;
163 if (what == OP_conv_noerror)
164 flags |= noerror;
165 if (!key) /* no ',' left, so this was the last specifier */
166 break;
167 arg += key - arg + 1; /* skip this keyword plus ',' */
168 }
169 continue;
170 }
171 }
172 if (what == OP_bs) {
173 ibs = obs = xatoul_range_sfx(arg, 1, ((size_t)-1L)/2, dd_suffixes);
174 continue;
175 }
176 /* These can be large: */
177 if (what == OP_count) {
178 count = XATOU_SFX(arg, dd_suffixes);
179 continue;
180 }
181 if (what == OP_seek) {
182 seek = XATOU_SFX(arg, dd_suffixes);
183 continue;
184 }
185 if (what == skip) {
186 skip = XATOU_SFX(arg, dd_suffixes);
187 continue;
188 }
189 if (what == OP_if) {
190 infile = arg;
191 continue;
192 }
193 if (what == OP_of)
194 outfile = arg;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000195 }
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000196//XXX:FIXME for huge ibs or obs, malloc'ing them isn't the brightest idea ever
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000197 ibuf = obuf = xmalloc(ibs);
198 if (ibs != obs) {
199 flags |= twobufs_flag;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000200 obuf = xmalloc(obs);
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000201 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000202 if (infile != NULL)
Denis Vlasenko7039a662006-10-08 17:54:47 +0000203 ifd = xopen(infile, O_RDONLY);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000204 else {
Matt Kraai24ac0172000-12-18 21:38:57 +0000205 ifd = STDIN_FILENO;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000206 infile = bb_msg_standard_input;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000207 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000208 if (outfile != NULL) {
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000209 int oflag = O_WRONLY | O_CREAT;
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000210
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000211 if (!seek && (flags & trunc_flag))
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000212 oflag |= O_TRUNC;
213
Denis Vlasenkocf749bc2006-11-26 15:45:17 +0000214 ofd = xopen(outfile, oflag);
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000215
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000216 if (seek && (flags & trunc_flag)) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000217 if (ftruncate(ofd, seek * obs) < 0) {
Matt Kraaieb834782002-02-05 15:28:54 +0000218 struct stat st;
219
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000220 if (fstat(ofd, &st) < 0 || S_ISREG(st.st_mode) ||
221 S_ISDIR(st.st_mode))
222 goto die_outfile;
Matt Kraaieb834782002-02-05 15:28:54 +0000223 }
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000224 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000225 } else {
226 ofd = STDOUT_FILENO;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000227 outfile = bb_msg_standard_output;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000228 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000229 if (skip) {
Denis Vlasenko7039a662006-10-08 17:54:47 +0000230 if (lseek(ifd, skip * ibs, SEEK_CUR) < 0) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000231 while (skip-- > 0) {
232 n = safe_read(ifd, ibuf, ibs);
233 if (n < 0)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000234 goto die_infile;
Rob Landley2686d3b2006-05-16 16:52:12 +0000235 if (n == 0)
236 break;
237 }
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000238 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000239 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000240 if (seek) {
Denis Vlasenko7039a662006-10-08 17:54:47 +0000241 if (lseek(ofd, seek * obs, SEEK_CUR) < 0)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000242 goto die_outfile;
Matt Kraai24ac0172000-12-18 21:38:57 +0000243 }
Eric Andersenddea3682000-11-29 22:33:02 +0000244
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000245 while (G.in_full + G.in_part != count) {
246 if (flags & noerror) /* Pre-zero the buffer when for noerror */
Rob Landley2686d3b2006-05-16 16:52:12 +0000247 memset(ibuf, '\0', ibs);
Rob Landley2686d3b2006-05-16 16:52:12 +0000248 n = safe_read(ifd, ibuf, ibs);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000249 if (n == 0)
Rob Landley2686d3b2006-05-16 16:52:12 +0000250 break;
Eric Andersenef38b392002-04-27 01:31:43 +0000251 if (n < 0) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000252 if (flags & noerror) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000253 n = ibs;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000254 bb_perror_msg("%s", infile);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000255 } else
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000256 goto die_infile;
Eric Andersenef38b392002-04-27 01:31:43 +0000257 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000258 if ((size_t)n == ibs)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000259 G.in_full++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000260 else {
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000261 G.in_part++;
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000262 if (flags & sync_flag) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000263 memset(ibuf + n, '\0', ibs - n);
264 n = ibs;
265 }
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000266 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000267 if (flags & twobufs_flag) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000268 char *tmp = ibuf;
269 while (n) {
270 size_t d = obs - oc;
271
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000272 if (d > n)
273 d = n;
Rob Landley2686d3b2006-05-16 16:52:12 +0000274 memcpy(obuf + oc, tmp, d);
275 n -= d;
276 tmp += d;
277 oc += d;
278 if (oc == obs) {
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000279 if (write_and_stats(ofd, obuf, obs, obs, outfile))
280 goto out_status;
Rob Landley2686d3b2006-05-16 16:52:12 +0000281 oc = 0;
282 }
283 }
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000284 } else
285 if (write_and_stats(ofd, ibuf, n, obs, outfile))
286 goto out_status;
Glenn L McGrathf0b073f2000-09-11 00:32:13 +0000287 }
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000288
Rob Landley2686d3b2006-05-16 16:52:12 +0000289 if (ENABLE_FEATURE_DD_IBS_OBS && oc) {
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000290 w = full_write_or_warn(ofd, obuf, oc, outfile);
291 if (w < 0) goto out_status;
292 if (w > 0)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000293 G.out_part++;
Rob Landley2686d3b2006-05-16 16:52:12 +0000294 }
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000295 if (close(ifd) < 0) {
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000296die_infile:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000297 bb_perror_msg_and_die("%s", infile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000298 }
Matt Kraai3eeab3b2001-12-07 16:27:37 +0000299
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000300 if (close(ofd) < 0) {
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000301die_outfile:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000302 bb_perror_msg_and_die("%s", outfile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000303 }
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000304out_status:
Rob Landleyc5598172006-05-02 22:44:04 +0000305 dd_output_status(0);
Matt Kraai24ac0172000-12-18 21:38:57 +0000306
Matt Kraai3e856ce2000-12-01 02:55:13 +0000307 return EXIT_SUCCESS;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000308}