blob: 4507b5e0c31974fe3f13ba70a78de4fd3c997043 [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
Eric Andersencbe31da2001-02-20 06:14:08 +000011#include "busybox.h"
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000012#include <signal.h> /* For FEATURE_DD_SIGNAL_HANDLING */
Eric Andersencbe31da2001-02-20 06:14:08 +000013
Matt Kraaia164c642001-02-05 17:50:03 +000014static const struct suffix_mult dd_suffixes[] = {
Matt Kraai24ac0172000-12-18 21:38:57 +000015 { "c", 1 },
16 { "w", 2 },
17 { "b", 512 },
18 { "kD", 1000 },
19 { "k", 1024 },
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000020 { "K", 1024 }, /* compat with coreutils dd */
Matt Kraai24ac0172000-12-18 21:38:57 +000021 { "MD", 1000000 },
22 { "M", 1048576 },
23 { "GD", 1000000000 },
24 { "G", 1073741824 },
25 { NULL, 0 }
26};
27
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000028struct globals {
29 off_t out_full, out_part, in_full, in_part;
30};
31#define G (*(struct globals*)&bb_common_bufsiz1)
Rob Landleyc5598172006-05-02 22:44:04 +000032
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000033static void dd_output_status(int ATTRIBUTE_UNUSED cur_signal)
Rob Landleyc5598172006-05-02 22:44:04 +000034{
Denis Vlasenkocf30cc82006-11-24 14:53:18 +000035 fprintf(stderr, "%"OFF_FMT"d+%"OFF_FMT"d records in\n"
36 "%"OFF_FMT"d+%"OFF_FMT"d records out\n",
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000037 G.in_full, G.in_part,
38 G.out_full, G.out_part);
Rob Landleyc5598172006-05-02 22:44:04 +000039}
40
Denis Vlasenko3b8ff682006-10-31 15:55:56 +000041static ssize_t full_write_or_warn(int fd, const void *buf, size_t len,
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000042 const char * const filename)
Denis Vlasenko3b8ff682006-10-31 15:55:56 +000043{
44 ssize_t n = full_write(fd, buf, len);
45 if (n < 0)
46 bb_perror_msg("writing '%s'", filename);
47 return n;
48}
49
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000050static bool write_and_stats(int fd, const void *buf, size_t len, size_t obs,
51 const char * const filename)
52{
53 ssize_t n = full_write_or_warn(fd, buf, len, filename);
54 if (n < 0)
55 return 1;
56 if (n == obs)
57 G.out_full++;
58 else if (n > 0)
59 G.out_part++;
60 return 0;
61}
62
Denis Vlasenko5dd27b12006-11-25 14:46:21 +000063#if ENABLE_LFS
64#define XATOU_SFX xatoull_sfx
65#else
66#define XATOU_SFX xatoul_sfx
67#endif
68
Denis Vlasenko06af2162007-02-03 17:28:39 +000069int dd_main(int argc, char **argv);
Matt Kraai24ac0172000-12-18 21:38:57 +000070int dd_main(int argc, char **argv)
Eric Andersen3cf52d11999-10-12 22:26:06 +000071{
Denis Vlasenko3b8ff682006-10-31 15:55:56 +000072 enum {
73 sync_flag = 1 << 0,
74 noerror = 1 << 1,
75 trunc_flag = 1 << 2,
76 twobufs_flag = 1 << 3,
77 };
Denis Vlasenko240a1cf2007-04-08 16:07:02 +000078 static const char * const keywords[] = {
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000079 "bs=", "count=", "seek=", "skip=", "if=", "of=",
80#if ENABLE_FEATURE_DD_IBS_OBS
81 "ibs=", "obs=", "conv=", "notrunc", "sync", "noerror",
82#endif
83 NULL
84 };
85#define OP_bs 0 + 1
86#define OP_count OP_bs + 1
87#define OP_seek OP_count + 1
88#define OP_skip OP_seek + 1
89#define OP_if OP_skip + 1
90#define OP_of OP_if + 1
91#define OP_ibs OP_of + ENABLE_FEATURE_DD_IBS_OBS
92#define OP_obs OP_ibs + ENABLE_FEATURE_DD_IBS_OBS
93#define OP_conv OP_obs + ENABLE_FEATURE_DD_IBS_OBS
94#define OP_conv_notrunc OP_conv + ENABLE_FEATURE_DD_IBS_OBS
95#define OP_conv_sync OP_conv_notrunc + ENABLE_FEATURE_DD_IBS_OBS
96#define OP_conv_noerror OP_conv_sync + ENABLE_FEATURE_DD_IBS_OBS
97
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000098 int flags = trunc_flag;
Denis Vlasenko56254152006-10-07 16:24:46 +000099 size_t oc = 0, ibs = 512, obs = 512;
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000100 ssize_t n, w;
Denis Vlasenko7039a662006-10-08 17:54:47 +0000101 off_t seek = 0, skip = 0, count = OFF_T_MAX;
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000102 int ifd, ofd;
Rob Landley2686d3b2006-05-16 16:52:12 +0000103 const char *infile = NULL, *outfile = NULL;
104 char *ibuf, *obuf;
Eric Andersenddea3682000-11-29 22:33:02 +0000105
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000106 if (ENABLE_FEATURE_DD_SIGNAL_HANDLING) {
Rob Landleyc5598172006-05-02 22:44:04 +0000107 struct sigaction sa;
108
109 memset(&sa, 0, sizeof(sa));
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000110 sa.sa_handler = dd_output_status;
Rob Landleyc5598172006-05-02 22:44:04 +0000111 sa.sa_flags = SA_RESTART;
112 sigemptyset(&sa.sa_mask);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000113 sigaction(SIGUSR1, &sa, 0);
Rob Landleyc5598172006-05-02 22:44:04 +0000114 }
115
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000116 for (n = 1; n < argc; n++) {
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000117 smalluint key_len, what;
118 char *key;
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000119 char *arg = argv[n];
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000120
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000121//XXX:FIXME: we reject plain "dd --" This would cost ~20 bytes, so..
122//if (*arg == '-' && *++arg == '-' && !*++arg) continue;
123 key = strstr(arg, "=");
124 if (key == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000125 bb_show_usage();
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000126 key_len = key - arg + 1;
127 key = xstrndup(arg, key_len);
128 what = index_in_str_array(keywords, key) + 1;
129 if (ENABLE_FEATURE_CLEAN_UP)
130 free(key);
131 if (what == 0)
132 bb_show_usage();
133 arg += key_len;
134 /* Must fit into positive ssize_t */
135 if (ENABLE_FEATURE_DD_IBS_OBS) {
136 if (what == OP_ibs) {
137 ibs = xatoul_range_sfx(arg, 1, ((size_t)-1L)/2, dd_suffixes);
138 continue;
139 }
140 if (what == OP_obs) {
141 obs = xatoul_range_sfx(arg, 1, ((size_t)-1L)/2, dd_suffixes);
142 continue;
143 }
144 if (what == OP_conv) {
145 while (1) {
146 /* find ',', replace them with nil so we can use arg for
147 * index_in_str_array without copying.
148 * We rely on arg being non-null, else strstr would fault.
149 */
150 key = strstr(arg, ",");
151 if (key)
152 *key = '\0';
153 what = index_in_str_array(keywords, arg) + 1;
154 if (what < OP_conv_notrunc)
155 bb_error_msg_and_die(bb_msg_invalid_arg, arg, "conv");
156 if (what == OP_conv_notrunc)
157 flags &= ~trunc_flag;
158 if (what == OP_conv_sync)
159 flags |= sync_flag;
160 if (what == OP_conv_noerror)
161 flags |= noerror;
162 if (!key) /* no ',' left, so this was the last specifier */
163 break;
164 arg += key - arg + 1; /* skip this keyword plus ',' */
165 }
166 continue;
167 }
168 }
169 if (what == OP_bs) {
170 ibs = obs = xatoul_range_sfx(arg, 1, ((size_t)-1L)/2, dd_suffixes);
171 continue;
172 }
173 /* These can be large: */
174 if (what == OP_count) {
175 count = XATOU_SFX(arg, dd_suffixes);
176 continue;
177 }
178 if (what == OP_seek) {
179 seek = XATOU_SFX(arg, dd_suffixes);
180 continue;
181 }
182 if (what == skip) {
183 skip = XATOU_SFX(arg, dd_suffixes);
184 continue;
185 }
186 if (what == OP_if) {
187 infile = arg;
188 continue;
189 }
190 if (what == OP_of)
191 outfile = arg;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000192 }
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000193//XXX:FIXME for huge ibs or obs, malloc'ing them isn't the brightest idea ever
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000194 ibuf = obuf = xmalloc(ibs);
195 if (ibs != obs) {
196 flags |= twobufs_flag;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000197 obuf = xmalloc(obs);
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000198 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000199 if (infile != NULL)
Denis Vlasenko7039a662006-10-08 17:54:47 +0000200 ifd = xopen(infile, O_RDONLY);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000201 else {
Matt Kraai24ac0172000-12-18 21:38:57 +0000202 ifd = STDIN_FILENO;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000203 infile = bb_msg_standard_input;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000204 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000205 if (outfile != NULL) {
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000206 int oflag = O_WRONLY | O_CREAT;
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000207
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000208 if (!seek && (flags & trunc_flag))
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000209 oflag |= O_TRUNC;
210
Denis Vlasenkocf749bc2006-11-26 15:45:17 +0000211 ofd = xopen(outfile, oflag);
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000212
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000213 if (seek && (flags & trunc_flag)) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000214 if (ftruncate(ofd, seek * obs) < 0) {
Matt Kraaieb834782002-02-05 15:28:54 +0000215 struct stat st;
216
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000217 if (fstat(ofd, &st) < 0 || S_ISREG(st.st_mode) ||
218 S_ISDIR(st.st_mode))
219 goto die_outfile;
Matt Kraaieb834782002-02-05 15:28:54 +0000220 }
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000221 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000222 } else {
223 ofd = STDOUT_FILENO;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000224 outfile = bb_msg_standard_output;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000225 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000226 if (skip) {
Denis Vlasenko7039a662006-10-08 17:54:47 +0000227 if (lseek(ifd, skip * ibs, SEEK_CUR) < 0) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000228 while (skip-- > 0) {
229 n = safe_read(ifd, ibuf, ibs);
230 if (n < 0)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000231 goto die_infile;
Rob Landley2686d3b2006-05-16 16:52:12 +0000232 if (n == 0)
233 break;
234 }
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000235 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000236 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000237 if (seek) {
Denis Vlasenko7039a662006-10-08 17:54:47 +0000238 if (lseek(ofd, seek * obs, SEEK_CUR) < 0)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000239 goto die_outfile;
Matt Kraai24ac0172000-12-18 21:38:57 +0000240 }
Eric Andersenddea3682000-11-29 22:33:02 +0000241
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000242 while (G.in_full + G.in_part != count) {
243 if (flags & noerror) /* Pre-zero the buffer when for noerror */
Rob Landley2686d3b2006-05-16 16:52:12 +0000244 memset(ibuf, '\0', ibs);
Rob Landley2686d3b2006-05-16 16:52:12 +0000245 n = safe_read(ifd, ibuf, ibs);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000246 if (n == 0)
Rob Landley2686d3b2006-05-16 16:52:12 +0000247 break;
Eric Andersenef38b392002-04-27 01:31:43 +0000248 if (n < 0) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000249 if (flags & noerror) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000250 n = ibs;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000251 bb_perror_msg("%s", infile);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000252 } else
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000253 goto die_infile;
Eric Andersenef38b392002-04-27 01:31:43 +0000254 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000255 if ((size_t)n == ibs)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000256 G.in_full++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000257 else {
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000258 G.in_part++;
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000259 if (flags & sync_flag) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000260 memset(ibuf + n, '\0', ibs - n);
261 n = ibs;
262 }
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000263 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000264 if (flags & twobufs_flag) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000265 char *tmp = ibuf;
266 while (n) {
267 size_t d = obs - oc;
268
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000269 if (d > n)
270 d = n;
Rob Landley2686d3b2006-05-16 16:52:12 +0000271 memcpy(obuf + oc, tmp, d);
272 n -= d;
273 tmp += d;
274 oc += d;
275 if (oc == obs) {
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000276 if (write_and_stats(ofd, obuf, obs, obs, outfile))
277 goto out_status;
Rob Landley2686d3b2006-05-16 16:52:12 +0000278 oc = 0;
279 }
280 }
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000281 } else
282 if (write_and_stats(ofd, ibuf, n, obs, outfile))
283 goto out_status;
Glenn L McGrathf0b073f2000-09-11 00:32:13 +0000284 }
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000285
Rob Landley2686d3b2006-05-16 16:52:12 +0000286 if (ENABLE_FEATURE_DD_IBS_OBS && oc) {
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000287 w = full_write_or_warn(ofd, obuf, oc, outfile);
288 if (w < 0) goto out_status;
289 if (w > 0)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000290 G.out_part++;
Rob Landley2686d3b2006-05-16 16:52:12 +0000291 }
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000292 if (close(ifd) < 0) {
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000293die_infile:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000294 bb_perror_msg_and_die("%s", infile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000295 }
Matt Kraai3eeab3b2001-12-07 16:27:37 +0000296
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000297 if (close(ofd) < 0) {
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000298die_outfile:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000299 bb_perror_msg_and_die("%s", outfile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000300 }
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000301out_status:
Rob Landleyc5598172006-05-02 22:44:04 +0000302 dd_output_status(0);
Matt Kraai24ac0172000-12-18 21:38:57 +0000303
Matt Kraai3e856ce2000-12-01 02:55:13 +0000304 return EXIT_SUCCESS;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000305}