blob: 24d7f0b840bba42e7e349bd77f03fbf4d5797fcd [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 *
Matt Kraai3eeab3b2001-12-07 16:27:37 +00005 * Copyright (C) 2000,2001 Matt Kraai
Eric Andersenc4996011999-10-20 22:08:37 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersencc8ed391999-10-05 16:24:54 +00008 */
Ari Sundholmf22a8382015-02-07 01:41:22 +01009//config:config DD
Denys Vlasenkob097a842018-12-28 03:20:17 +010010//config: bool "dd (7.5 kb)"
Ari Sundholmf22a8382015-02-07 01:41:22 +010011//config: default y
12//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020013//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.
Ari Sundholmf22a8382015-02-07 01:41:22 +010016//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
Denys Vlasenko18e781d2017-08-02 14:12:48 +020022//config: Sending a SIGUSR1 signal to a running 'dd' process makes it
Denys Vlasenko72089cf2017-07-21 09:50:55 +020023//config: print to standard error the number of records read and written
24//config: so far, then to resume copying.
Ari Sundholmf22a8382015-02-07 01:41:22 +010025//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020026//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
Ari Sundholmf22a8382015-02-07 01:41:22 +010030//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
Denys Vlasenko72089cf2017-07-21 09:50:55 +020036//config: Displays a coreutils-like third status line with transferred bytes,
37//config: elapsed time and speed.
Ari Sundholmf22a8382015-02-07 01:41:22 +010038//config:
39//config:config FEATURE_DD_IBS_OBS
Rostislav Skudnov3f6a9782018-10-15 10:26:15 +000040//config: bool "Enable ibs, obs, iflag, oflag and conv options"
Ari Sundholmf22a8382015-02-07 01:41:22 +010041//config: default y
42//config: depends on DD
43//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020044//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.
Ari Sundholmf22a8382015-02-07 01:41:22 +010046//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
Denys Vlasenko72089cf2017-07-21 09:50:55 +020052//config: Enable support for status=noxfer/none option.
Ari Sundholmf22a8382015-02-07 01:41:22 +010053
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010054//applet:IF_DD(APPLET_NOEXEC(dd, dd, BB_DIR_BIN, BB_SUID_DROP, dd))
55
56//kbuild:lib-$(CONFIG_DD) += dd.o
57
Pere Orga34425382011-03-31 14:43:25 +020058//usage:#define dd_trivial_usage
Denys Vlasenko9ab5a8d2018-10-30 17:04:52 +010059//usage: "[if=FILE] [of=FILE] [" IF_FEATURE_DD_IBS_OBS("ibs=N obs=N/") "bs=N] [count=N] [skip=N] [seek=N]\n"
60//usage: IF_FEATURE_DD_IBS_OBS(
61//usage: " [conv=notrunc|noerror|sync|fsync]\n"
Denys Vlasenko2aaacc12020-12-14 20:27:57 +010062//usage: " [iflag=skip_bytes|fullblock|direct] [oflag=seek_bytes|append|direct]"
Denys Vlasenko9ab5a8d2018-10-30 17:04:52 +010063//usage: )
Pere Orga34425382011-03-31 14:43:25 +020064//usage:#define dd_full_usage "\n\n"
65//usage: "Copy a file with converting and formatting\n"
Pere Orga34425382011-03-31 14:43:25 +020066//usage: "\n if=FILE Read from FILE instead of stdin"
67//usage: "\n of=FILE Write to FILE instead of stdout"
68//usage: "\n bs=N Read and write N bytes at a time"
69//usage: IF_FEATURE_DD_IBS_OBS(
70//usage: "\n ibs=N Read N bytes at a time"
71//usage: )
72//usage: IF_FEATURE_DD_IBS_OBS(
73//usage: "\n obs=N Write N bytes at a time"
74//usage: )
75//usage: "\n count=N Copy only N input blocks"
76//usage: "\n skip=N Skip N input blocks"
77//usage: "\n seek=N Skip N output blocks"
78//usage: IF_FEATURE_DD_IBS_OBS(
79//usage: "\n conv=notrunc Don't truncate output file"
80//usage: "\n conv=noerror Continue after read errors"
81//usage: "\n conv=sync Pad blocks with zeros"
82//usage: "\n conv=fsync Physically write data out before finishing"
Denys Vlasenkob9413162013-08-19 09:01:39 +020083//usage: "\n conv=swab Swap every pair of bytes"
Rafał Miłeckic30a5b12016-02-01 02:17:28 +010084//usage: "\n iflag=skip_bytes skip=N is in bytes"
Rostislav Skudnov3f6a9782018-10-15 10:26:15 +000085//usage: "\n oflag=seek_bytes seek=N is in bytes"
Denys Vlasenko2aaacc12020-12-14 20:27:57 +010086//usage: "\n iflag=direct O_DIRECT input"
87//usage: "\n oflag=direct O_DIRECT output"
88//usage: "\n iflag=fullblock Read full blocks"
89//usage: "\n oflag=append Open output in append mode"
Pere Orga34425382011-03-31 14:43:25 +020090//usage: )
Ari Sundholmf22a8382015-02-07 01:41:22 +010091//usage: IF_FEATURE_DD_STATUS(
92//usage: "\n status=noxfer Suppress rate output"
93//usage: "\n status=none Suppress all output"
94//usage: )
Pere Orga34425382011-03-31 14:43:25 +020095//usage: "\n"
Ari Sundholmf22a8382015-02-07 01:41:22 +010096//usage: "\nN may be suffixed by c (1), w (2), b (512), kB (1000), k (1024), MB, M, GB, G"
Pere Orga34425382011-03-31 14:43:25 +020097//usage:
98//usage:#define dd_example_usage
99//usage: "$ dd if=/dev/zero of=/dev/ram1 bs=1M count=4\n"
100//usage: "4+0 records in\n"
101//usage: "4+0 records out\n"
102
Denis Vlasenkob6adbf12007-05-26 19:00:18 +0000103#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200104#include "common_bufsiz.h"
Denis Vlasenko99912ca2007-04-10 15:43:37 +0000105
106/* This is a NOEXEC applet. Be very careful! */
107
Eric Andersencbe31da2001-02-20 06:14:08 +0000108
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000109enum {
110 ifd = STDIN_FILENO,
111 ofd = STDOUT_FILENO,
112};
113
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000114struct globals {
115 off_t out_full, out_part, in_full, in_part;
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200116#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
117 unsigned long long total_bytes;
118 unsigned long long begin_time_us;
119#endif
Ari Sundholmf22a8382015-02-07 01:41:22 +0100120 int flags;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100121} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200122#define G (*(struct globals*)bb_common_bufsiz1)
Denys Vlasenko1b34d4f2009-09-30 02:39:57 +0200123#define INIT_G() do { \
Denys Vlasenko47cfbf32016-04-21 18:18:48 +0200124 setup_common_bufsiz(); \
Denys Vlasenko1b34d4f2009-09-30 02:39:57 +0200125 /* we have to zero it out because of NOEXEC */ \
126 memset(&G, 0, sizeof(G)); \
127} while (0)
Denis Vlasenko75878702007-07-27 15:02:00 +0000128
Ari Sundholmf22a8382015-02-07 01:41:22 +0100129enum {
130 /* Must be in the same order as OP_conv_XXX! */
131 /* (see "flags |= (1 << what)" below) */
132 FLAG_NOTRUNC = (1 << 0) * ENABLE_FEATURE_DD_IBS_OBS,
133 FLAG_SYNC = (1 << 1) * ENABLE_FEATURE_DD_IBS_OBS,
134 FLAG_NOERROR = (1 << 2) * ENABLE_FEATURE_DD_IBS_OBS,
135 FLAG_FSYNC = (1 << 3) * ENABLE_FEATURE_DD_IBS_OBS,
136 FLAG_SWAB = (1 << 4) * ENABLE_FEATURE_DD_IBS_OBS,
137 /* end of conv flags */
Rafał Miłeckic30a5b12016-02-01 02:17:28 +0100138 /* start of input flags */
139 FLAG_IFLAG_SHIFT = 5,
140 FLAG_SKIP_BYTES = (1 << 5) * ENABLE_FEATURE_DD_IBS_OBS,
Nicholas Clark77a66782018-01-25 19:00:19 +0100141 FLAG_FULLBLOCK = (1 << 6) * ENABLE_FEATURE_DD_IBS_OBS,
Denys Vlasenko2aaacc12020-12-14 20:27:57 +0100142 FLAG_IDIRECT = (1 << 7) * ENABLE_FEATURE_DD_IBS_OBS,
Rafał Miłeckic30a5b12016-02-01 02:17:28 +0100143 /* end of input flags */
Rostislav Skudnov3f6a9782018-10-15 10:26:15 +0000144 /* start of output flags */
Denys Vlasenko2aaacc12020-12-14 20:27:57 +0100145 FLAG_OFLAG_SHIFT = 8,
146 FLAG_SEEK_BYTES = (1 << 8) * ENABLE_FEATURE_DD_IBS_OBS,
147 FLAG_APPEND = (1 << 9) * ENABLE_FEATURE_DD_IBS_OBS,
148 FLAG_ODIRECT = (1 << 10) * ENABLE_FEATURE_DD_IBS_OBS,
Rostislav Skudnov3f6a9782018-10-15 10:26:15 +0000149 /* end of output flags */
Denys Vlasenko2aaacc12020-12-14 20:27:57 +0100150 FLAG_TWOBUFS = (1 << 11) * ENABLE_FEATURE_DD_IBS_OBS,
151 FLAG_COUNT = 1 << 12,
152 FLAG_STATUS_NONE = 1 << 13,
153 FLAG_STATUS_NOXFER = 1 << 14,
Ari Sundholmf22a8382015-02-07 01:41:22 +0100154};
Rob Landleyc5598172006-05-02 22:44:04 +0000155
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000156static void dd_output_status(int UNUSED_PARAM cur_signal)
Rob Landleyc5598172006-05-02 22:44:04 +0000157{
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200158#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
Denys Vlasenko7eabffa2009-10-14 17:43:41 +0200159 double seconds;
160 unsigned long long bytes_sec;
161 unsigned long long now_us = monotonic_us(); /* before fprintf */
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200162#endif
163
Denis Vlasenko980864d2007-07-30 10:58:09 +0000164 /* Deliberately using %u, not %d */
165 fprintf(stderr, "%"OFF_FMT"u+%"OFF_FMT"u records in\n"
166 "%"OFF_FMT"u+%"OFF_FMT"u records out\n",
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000167 G.in_full, G.in_part,
168 G.out_full, G.out_part);
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200169
170#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
Ari Sundholmf22a8382015-02-07 01:41:22 +0100171# if ENABLE_FEATURE_DD_STATUS
172 if (G.flags & FLAG_STATUS_NOXFER) /* status=noxfer active? */
173 return;
174 //TODO: should status=none make dd stop reacting to USR1 entirely?
175 //So far we react to it (we print the stats),
176 //status=none only suppresses final, non-USR1 generated status message.
177# endif
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200178 fprintf(stderr, "%llu bytes (%sB) copied, ",
179 G.total_bytes,
180 /* show fractional digit, use suffixes */
181 make_human_readable_str(G.total_bytes, 1, 0)
182 );
183 /* Corner cases:
184 * ./busybox dd </dev/null >/dev/null
185 * ./busybox dd bs=1M count=2000 </dev/zero >/dev/null
186 * (echo DONE) | ./busybox dd >/dev/null
187 * (sleep 1; echo DONE) | ./busybox dd >/dev/null
188 */
Denys Vlasenko7eabffa2009-10-14 17:43:41 +0200189 seconds = (now_us - G.begin_time_us) / 1000000.0;
190 bytes_sec = G.total_bytes / seconds;
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200191 fprintf(stderr, "%f seconds, %sB/s\n",
Denys Vlasenko7eabffa2009-10-14 17:43:41 +0200192 seconds,
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200193 /* show fractional digit, use suffixes */
Denys Vlasenko7eabffa2009-10-14 17:43:41 +0200194 make_human_readable_str(bytes_sec, 1, 0)
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200195 );
196#endif
Rob Landleyc5598172006-05-02 22:44:04 +0000197}
198
Denys Vlasenko2aaacc12020-12-14 20:27:57 +0100199#if ENABLE_FEATURE_DD_IBS_OBS
200static int clear_O_DIRECT(int fd)
201{
202 if (errno == EINVAL) {
203 int fl = fcntl(fd, F_GETFL);
204 if (fl & O_DIRECT) {
205 fcntl(fd, F_SETFL, fl & ~O_DIRECT);
206 return 1;
207 }
208 }
209 return 0;
210}
211#endif
212
213static ssize_t dd_read(void *ibuf, size_t ibs)
214{
215 ssize_t n;
216
217#if ENABLE_FEATURE_DD_IBS_OBS
218 read_again:
219 if (G.flags & FLAG_FULLBLOCK)
220 n = full_read(ifd, ibuf, ibs);
221 else
222#endif
223 n = safe_read(ifd, ibuf, ibs);
224#if ENABLE_FEATURE_DD_IBS_OBS
225 if (n < 0 && (G.flags & FLAG_IDIRECT) && clear_O_DIRECT(ifd))
226 goto read_again;
227#endif
228 return n;
229}
230
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000231static bool write_and_stats(const void *buf, size_t len, size_t obs,
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000232 const char *filename)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000233{
Denys Vlasenko875ce092019-05-14 17:46:18 +0200234 ssize_t n;
235
Denys Vlasenko2aaacc12020-12-14 20:27:57 +0100236 IF_FEATURE_DD_IBS_OBS(write_again:)
Denys Vlasenko875ce092019-05-14 17:46:18 +0200237 n = full_write(ofd, buf, len);
Denys Vlasenko2aaacc12020-12-14 20:27:57 +0100238#if ENABLE_FEATURE_DD_IBS_OBS
239 if (n < 0 && (G.flags & FLAG_ODIRECT) && clear_O_DIRECT(ofd))
240 goto write_again;
241#endif
242
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200243#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
Denys Vlasenko875ce092019-05-14 17:46:18 +0200244 if (n > 0)
245 G.total_bytes += n;
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200246#endif
Denys Vlasenko5afd63a2018-01-10 10:58:55 +0100247 if ((size_t)n == obs) {
248 G.out_full++;
249 return 0;
250 }
Denys Vlasenko2c876772018-01-10 11:04:09 +0100251 if ((size_t)n == len) {
Denys Vlasenko5afd63a2018-01-10 10:58:55 +0100252 G.out_part++;
Denys Vlasenko2c876772018-01-10 11:04:09 +0100253 return 0;
254 }
Denys Vlasenko875ce092019-05-14 17:46:18 +0200255 /* n is < len (and possibly is -1).
256 * Even if n >= 0, errno is usually set correctly.
257 * For example, if writing to block device and getting ENOSPC,
258 * full_write() first sees a short write, then tries to write
259 * the remainder and gets errno set to ENOSPC.
260 * It returns n > 0 (the amount which it did write).
261 */
262 bb_perror_msg("error writing '%s'", filename);
Denys Vlasenko5afd63a2018-01-10 10:58:55 +0100263 return 1;
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000264}
265
Denis Vlasenko5dd27b12006-11-25 14:46:21 +0000266#if ENABLE_LFS
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200267# define XATOU_SFX xatoull_sfx
Denis Vlasenko5dd27b12006-11-25 14:46:21 +0000268#else
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200269# define XATOU_SFX xatoul_sfx
Denis Vlasenko5dd27b12006-11-25 14:46:21 +0000270#endif
271
Rafał Miłeckic30a5b12016-02-01 02:17:28 +0100272#if ENABLE_FEATURE_DD_IBS_OBS
273static int parse_comma_flags(char *val, const char *words, const char *error_in)
274{
275 int flags = 0;
276 while (1) {
277 int n;
278 char *arg;
279 /* find ',', replace them with NUL so we can use val for
280 * index_in_strings() without copying.
281 * We rely on val being non-null, else strchr would fault.
282 */
283 arg = strchr(val, ',');
284 if (arg)
285 *arg = '\0';
286 n = index_in_strings(words, val);
287 if (n < 0)
288 bb_error_msg_and_die(bb_msg_invalid_arg_to, val, error_in);
289 flags |= (1 << n);
290 if (!arg) /* no ',' left, so this was the last specifier */
291 break;
292 *arg = ','; /* to preserve ps listing */
293 val = arg + 1; /* skip this keyword and ',' */
294 }
295 return flags;
296}
297#endif
298
Denys Vlasenko2aaacc12020-12-14 20:27:57 +0100299static void *alloc_buf(size_t size)
300{
301 /* Important for "{i,o}flag=direct" - buffers must be page aligned */
302 if (size >= bb_getpagesize())
303 return xmmap_anon(size);
304 return xmalloc(size);
305}
306
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000307int dd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000308int dd_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen3cf52d11999-10-12 22:26:06 +0000309{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000310 static const char keywords[] ALIGN1 =
Ari Sundholmf22a8382015-02-07 01:41:22 +0100311 "bs\0""count\0""seek\0""skip\0""if\0""of\0"IF_FEATURE_DD_STATUS("status\0")
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000312#if ENABLE_FEATURE_DD_IBS_OBS
Rostislav Skudnov3f6a9782018-10-15 10:26:15 +0000313 "ibs\0""obs\0""conv\0""iflag\0""oflag\0"
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000314#endif
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000315 ;
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000316#if ENABLE_FEATURE_DD_IBS_OBS
317 static const char conv_words[] ALIGN1 =
Denys Vlasenkob9413162013-08-19 09:01:39 +0200318 "notrunc\0""sync\0""noerror\0""fsync\0""swab\0";
Rafał Miłeckic30a5b12016-02-01 02:17:28 +0100319 static const char iflag_words[] ALIGN1 =
Denys Vlasenko2aaacc12020-12-14 20:27:57 +0100320 "skip_bytes\0""fullblock\0""direct\0";
Rostislav Skudnov3f6a9782018-10-15 10:26:15 +0000321 static const char oflag_words[] ALIGN1 =
Denys Vlasenko2aaacc12020-12-14 20:27:57 +0100322 "seek_bytes\0append\0""direct\0";
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000323#endif
Ari Sundholmf22a8382015-02-07 01:41:22 +0100324#if ENABLE_FEATURE_DD_STATUS
325 static const char status_words[] ALIGN1 =
326 "none\0""noxfer\0";
327#endif
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000328 enum {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000329 OP_bs = 0,
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000330 OP_count,
331 OP_seek,
332 OP_skip,
333 OP_if,
334 OP_of,
Ari Sundholmf22a8382015-02-07 01:41:22 +0100335 IF_FEATURE_DD_STATUS(OP_status,)
Bernhard Reutner-Fischere468ef22007-04-16 12:21:05 +0000336#if ENABLE_FEATURE_DD_IBS_OBS
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000337 OP_ibs,
338 OP_obs,
339 OP_conv,
Rafał Miłeckic30a5b12016-02-01 02:17:28 +0100340 OP_iflag,
Rostislav Skudnov3f6a9782018-10-15 10:26:15 +0000341 OP_oflag,
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000342 /* Must be in the same order as FLAG_XXX! */
343 OP_conv_notrunc = 0,
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000344 OP_conv_sync,
Denis Vlasenkof6250a32007-04-19 20:16:14 +0000345 OP_conv_noerror,
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000346 OP_conv_fsync,
Denys Vlasenkob9413162013-08-19 09:01:39 +0200347 OP_conv_swab,
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000348 /* Unimplemented conv=XXX: */
349 //nocreat do not create the output file
350 //excl fail if the output file already exists
351 //fdatasync physically write output file data before finishing
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000352 //lcase change upper case to lower case
353 //ucase change lower case to upper case
354 //block pad newline-terminated records with spaces to cbs-size
355 //unblock replace trailing spaces in cbs-size records with newline
356 //ascii from EBCDIC to ASCII
357 //ebcdic from ASCII to EBCDIC
358 //ibm from ASCII to alternate EBCDIC
Denys Vlasenkob9413162013-08-19 09:01:39 +0200359 /* Partially implemented: */
360 //swab swap every pair of input bytes: will abort on non-even reads
Rafał Miłeckic30a5b12016-02-01 02:17:28 +0100361 OP_iflag_skip_bytes,
Nicholas Clark77a66782018-01-25 19:00:19 +0100362 OP_iflag_fullblock,
Denys Vlasenko2aaacc12020-12-14 20:27:57 +0100363 OP_iflag_direct,
Rostislav Skudnov3f6a9782018-10-15 10:26:15 +0000364 OP_oflag_seek_bytes,
Denys Vlasenko2aaacc12020-12-14 20:27:57 +0100365 OP_oflag_direct,
Bernhard Reutner-Fischere468ef22007-04-16 12:21:05 +0000366#endif
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000367 };
Denys Vlasenko5b9910f2013-08-20 02:50:49 +0200368 smallint exitcode = EXIT_FAILURE;
Denys Vlasenko4b1896c2013-08-19 09:00:08 +0200369 int i;
Denys Vlasenko4502bb12013-08-20 12:08:46 +0200370 size_t ibs = 512;
371 char *ibuf;
372#if ENABLE_FEATURE_DD_IBS_OBS
373 size_t obs = 512;
374 char *obuf;
375#else
376# define obs ibs
377# define obuf ibuf
378#endif
379 /* These are all zeroed at once! */
Denis Vlasenko75878702007-07-27 15:02:00 +0000380 struct {
Denys Vlasenko9ab5a8d2018-10-30 17:04:52 +0100381 IF_FEATURE_DD_IBS_OBS(size_t ocount;)
Denys Vlasenko5b9910f2013-08-20 02:50:49 +0200382 ssize_t prev_read_size; /* for detecting swab failure */
Denis Vlasenko75878702007-07-27 15:02:00 +0000383 off_t count;
384 off_t seek, skip;
385 const char *infile, *outfile;
Denis Vlasenko75878702007-07-27 15:02:00 +0000386 } Z;
Denys Vlasenko9ab5a8d2018-10-30 17:04:52 +0100387#define ocount (Z.ocount )
Denys Vlasenko5b9910f2013-08-20 02:50:49 +0200388#define prev_read_size (Z.prev_read_size)
Denis Vlasenko75878702007-07-27 15:02:00 +0000389#define count (Z.count )
390#define seek (Z.seek )
391#define skip (Z.skip )
392#define infile (Z.infile )
393#define outfile (Z.outfile)
Eric Andersenddea3682000-11-29 22:33:02 +0000394
Denis Vlasenko75878702007-07-27 15:02:00 +0000395 memset(&Z, 0, sizeof(Z));
396 INIT_G();
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100397 //fflush_all(); - is this needed because of NOEXEC?
Denis Vlasenkof6250a32007-04-19 20:16:14 +0000398
Denys Vlasenko4b1896c2013-08-19 09:00:08 +0200399 for (i = 1; argv[i]; i++) {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000400 int what;
401 char *val;
Denys Vlasenko4b1896c2013-08-19 09:00:08 +0200402 char *arg = argv[i];
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000403
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000404#if ENABLE_DESKTOP
405 /* "dd --". NB: coreutils 6.9 will complain if they see
406 * more than one of them. We wouldn't. */
407 if (arg[0] == '-' && arg[1] == '-' && arg[2] == '\0')
408 continue;
409#endif
410 val = strchr(arg, '=');
411 if (val == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000412 bb_show_usage();
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000413 *val = '\0';
414 what = index_in_strings(keywords, arg);
415 if (what < 0)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000416 bb_show_usage();
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000417 /* *val = '='; - to preserve ps listing? */
418 val++;
Bernhard Reutner-Fischere468ef22007-04-16 12:21:05 +0000419#if ENABLE_FEATURE_DD_IBS_OBS
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000420 if (what == OP_ibs) {
421 /* Must fit into positive ssize_t */
Ari Sundholm88930232015-03-04 18:46:48 +0200422 ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, cwbkMG_suffixes);
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000423 /*continue;*/
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000424 }
425 if (what == OP_obs) {
Ari Sundholm88930232015-03-04 18:46:48 +0200426 obs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, cwbkMG_suffixes);
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000427 /*continue;*/
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000428 }
429 if (what == OP_conv) {
Rafał Miłeckic30a5b12016-02-01 02:17:28 +0100430 G.flags |= parse_comma_flags(val, conv_words, "conv");
431 /*continue;*/
432 }
433 if (what == OP_iflag) {
434 G.flags |= parse_comma_flags(val, iflag_words, "iflag") << FLAG_IFLAG_SHIFT;
Denys Vlasenko4502bb12013-08-20 12:08:46 +0200435 /*continue;*/
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000436 }
Rostislav Skudnov3f6a9782018-10-15 10:26:15 +0000437 if (what == OP_oflag) {
438 G.flags |= parse_comma_flags(val, oflag_words, "oflag") << FLAG_OFLAG_SHIFT;
439 /*continue;*/
440 }
Bernhard Reutner-Fischere468ef22007-04-16 12:21:05 +0000441#endif
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000442 if (what == OP_bs) {
Ari Sundholm88930232015-03-04 18:46:48 +0200443 ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, cwbkMG_suffixes);
Denys Vlasenko4502bb12013-08-20 12:08:46 +0200444 obs = ibs;
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000445 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000446 }
447 /* These can be large: */
448 if (what == OP_count) {
Ari Sundholmf22a8382015-02-07 01:41:22 +0100449 G.flags |= FLAG_COUNT;
Ari Sundholm88930232015-03-04 18:46:48 +0200450 count = XATOU_SFX(val, cwbkMG_suffixes);
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000451 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000452 }
453 if (what == OP_seek) {
Ari Sundholm88930232015-03-04 18:46:48 +0200454 seek = XATOU_SFX(val, cwbkMG_suffixes);
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000455 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000456 }
Denis Vlasenkod1801a42007-04-19 20:08:19 +0000457 if (what == OP_skip) {
Ari Sundholm88930232015-03-04 18:46:48 +0200458 skip = XATOU_SFX(val, cwbkMG_suffixes);
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000459 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000460 }
461 if (what == OP_if) {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000462 infile = val;
463 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000464 }
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000465 if (what == OP_of) {
466 outfile = val;
467 /*continue;*/
468 }
Ari Sundholmf22a8382015-02-07 01:41:22 +0100469#if ENABLE_FEATURE_DD_STATUS
470 if (what == OP_status) {
471 int n;
472 n = index_in_strings(status_words, val);
473 if (n < 0)
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200474 bb_error_msg_and_die(bb_msg_invalid_arg_to, val, "status");
Denys Vlasenkod5b98e22017-07-14 13:44:30 +0200475 G.flags |= FLAG_STATUS_NONE << n;
Ari Sundholmf22a8382015-02-07 01:41:22 +0100476 /*continue;*/
477 }
478#endif
Denys Vlasenko4b1896c2013-08-19 09:00:08 +0200479 } /* end of "for (argv[i])" */
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000480
Denys Vlasenko2aaacc12020-12-14 20:27:57 +0100481 ibuf = alloc_buf(ibs);
Denys Vlasenko4502bb12013-08-20 12:08:46 +0200482 obuf = ibuf;
483#if ENABLE_FEATURE_DD_IBS_OBS
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000484 if (ibs != obs) {
Ari Sundholmf22a8382015-02-07 01:41:22 +0100485 G.flags |= FLAG_TWOBUFS;
Denys Vlasenko2aaacc12020-12-14 20:27:57 +0100486 obuf = alloc_buf(obs);
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000487 }
Denys Vlasenko4502bb12013-08-20 12:08:46 +0200488#endif
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200489
490#if ENABLE_FEATURE_DD_SIGNAL_HANDLING
491 signal_SA_RESTART_empty_mask(SIGUSR1, dd_output_status);
492#endif
493#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
494 G.begin_time_us = monotonic_us();
495#endif
496
Denys Vlasenko4502bb12013-08-20 12:08:46 +0200497 if (infile) {
Denys Vlasenko2aaacc12020-12-14 20:27:57 +0100498 int iflag = O_RDONLY;
499#if ENABLE_FEATURE_DD_IBS_OBS
500 if (G.flags & FLAG_IDIRECT)
501 iflag |= O_DIRECT;
502#endif
503 xmove_fd(xopen(infile, iflag), ifd);
Denys Vlasenko4502bb12013-08-20 12:08:46 +0200504 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000505 infile = bb_msg_standard_input;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000506 }
Denys Vlasenko4502bb12013-08-20 12:08:46 +0200507 if (outfile) {
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000508 int oflag = O_WRONLY | O_CREAT;
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000509
Ari Sundholmf22a8382015-02-07 01:41:22 +0100510 if (!seek && !(G.flags & FLAG_NOTRUNC))
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000511 oflag |= O_TRUNC;
Rostislav Skudnovd712edc2019-02-06 11:57:15 +0000512 if (G.flags & FLAG_APPEND)
513 oflag |= O_APPEND;
Denys Vlasenko2aaacc12020-12-14 20:27:57 +0100514#if ENABLE_FEATURE_DD_IBS_OBS
515 if (G.flags & FLAG_ODIRECT)
516 oflag |= O_DIRECT;
517#endif
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000518 xmove_fd(xopen(outfile, oflag), ofd);
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000519
Ari Sundholmf22a8382015-02-07 01:41:22 +0100520 if (seek && !(G.flags & FLAG_NOTRUNC)) {
Rostislav Skudnov3f6a9782018-10-15 10:26:15 +0000521 size_t blocksz = (G.flags & FLAG_SEEK_BYTES) ? 1 : obs;
522 if (ftruncate(ofd, seek * blocksz) < 0) {
Matt Kraaieb834782002-02-05 15:28:54 +0000523 struct stat st;
524
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100525 if (fstat(ofd, &st) < 0
526 || S_ISREG(st.st_mode)
527 || S_ISDIR(st.st_mode)
528 ) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000529 goto die_outfile;
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100530 }
Matt Kraaieb834782002-02-05 15:28:54 +0000531 }
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000532 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000533 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000534 outfile = bb_msg_standard_output;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000535 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000536 if (skip) {
Rafał Miłeckic30a5b12016-02-01 02:17:28 +0100537 size_t blocksz = (G.flags & FLAG_SKIP_BYTES) ? 1 : ibs;
538 if (lseek(ifd, skip * blocksz, SEEK_CUR) < 0) {
Denys Vlasenko1b57fe12013-08-20 12:45:05 +0200539 do {
Denys Vlasenko2aaacc12020-12-14 20:27:57 +0100540 ssize_t n = dd_read(ibuf, blocksz);
Rob Landley2686d3b2006-05-16 16:52:12 +0000541 if (n < 0)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000542 goto die_infile;
Rob Landley2686d3b2006-05-16 16:52:12 +0000543 if (n == 0)
544 break;
Denys Vlasenko1b57fe12013-08-20 12:45:05 +0200545 } while (--skip != 0);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000546 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000547 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000548 if (seek) {
Rostislav Skudnov3f6a9782018-10-15 10:26:15 +0000549 size_t blocksz = (G.flags & FLAG_SEEK_BYTES) ? 1 : obs;
550 if (lseek(ofd, seek * blocksz, SEEK_CUR) < 0)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000551 goto die_outfile;
Matt Kraai24ac0172000-12-18 21:38:57 +0000552 }
Eric Andersenddea3682000-11-29 22:33:02 +0000553
Ari Sundholmf22a8382015-02-07 01:41:22 +0100554 while (!(G.flags & FLAG_COUNT) || (G.in_full + G.in_part != count)) {
Denys Vlasenko2aaacc12020-12-14 20:27:57 +0100555 ssize_t n = dd_read(ibuf, ibs);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000556 if (n == 0)
Rob Landley2686d3b2006-05-16 16:52:12 +0000557 break;
Eric Andersenef38b392002-04-27 01:31:43 +0000558 if (n < 0) {
Denys Vlasenkof00cfdf2009-09-18 20:49:36 +0200559 /* "Bad block" */
Ari Sundholmf22a8382015-02-07 01:41:22 +0100560 if (!(G.flags & FLAG_NOERROR))
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000561 goto die_infile;
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000562 bb_simple_perror_msg(infile);
Denys Vlasenkof00cfdf2009-09-18 20:49:36 +0200563 /* GNU dd with conv=noerror skips over bad blocks */
Denys Vlasenko334fcc82009-08-05 23:16:27 +0200564 xlseek(ifd, ibs, SEEK_CUR);
Denys Vlasenkof00cfdf2009-09-18 20:49:36 +0200565 /* conv=noerror,sync writes NULs,
566 * conv=noerror just ignores input bad blocks */
567 n = 0;
Eric Andersenef38b392002-04-27 01:31:43 +0000568 }
Ari Sundholmf22a8382015-02-07 01:41:22 +0100569 if (G.flags & FLAG_SWAB) {
Denys Vlasenko0ff0b322013-08-20 12:49:28 +0200570 uint16_t *p16;
571 ssize_t n2;
Denys Vlasenko5b9910f2013-08-20 02:50:49 +0200572
573 /* Our code allows only last read to be odd-sized */
574 if (prev_read_size & 1)
575 bb_error_msg_and_die("can't swab %lu byte buffer",
576 (unsigned long)prev_read_size);
577 prev_read_size = n;
578
Denys Vlasenko90e9cfd2013-08-20 12:27:19 +0200579 /* If n is odd, last byte is not swapped:
Denys Vlasenko8395bd32013-08-19 10:30:55 +0200580 * echo -n "qwe" | dd conv=swab
Denys Vlasenkob9413162013-08-19 09:01:39 +0200581 * prints "wqe".
Denys Vlasenkob9413162013-08-19 09:01:39 +0200582 */
Denys Vlasenko5b9910f2013-08-20 02:50:49 +0200583 p16 = (void*) ibuf;
Denys Vlasenko0ff0b322013-08-20 12:49:28 +0200584 n2 = (n >> 1);
585 while (--n2 >= 0) {
Denys Vlasenkob9413162013-08-19 09:01:39 +0200586 *p16 = bswap_16(*p16);
587 p16++;
588 }
589 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000590 if ((size_t)n == ibs)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000591 G.in_full++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000592 else {
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000593 G.in_part++;
Ari Sundholmf22a8382015-02-07 01:41:22 +0100594 if (G.flags & FLAG_SYNC) {
Denys Vlasenkof00cfdf2009-09-18 20:49:36 +0200595 memset(ibuf + n, 0, ibs - n);
Rob Landley2686d3b2006-05-16 16:52:12 +0000596 n = ibs;
597 }
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000598 }
Denys Vlasenko9ab5a8d2018-10-30 17:04:52 +0100599#if ENABLE_FEATURE_DD_IBS_OBS
Ari Sundholmf22a8382015-02-07 01:41:22 +0100600 if (G.flags & FLAG_TWOBUFS) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000601 char *tmp = ibuf;
602 while (n) {
Denys Vlasenko9ab5a8d2018-10-30 17:04:52 +0100603 size_t d = obs - ocount;
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000604 if (d > (size_t)n)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000605 d = n;
Denys Vlasenko9ab5a8d2018-10-30 17:04:52 +0100606 memcpy(obuf + ocount, tmp, d);
Rob Landley2686d3b2006-05-16 16:52:12 +0000607 n -= d;
608 tmp += d;
Denys Vlasenko9ab5a8d2018-10-30 17:04:52 +0100609 ocount += d;
610 if (ocount == obs) {
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000611 if (write_and_stats(obuf, obs, obs, outfile))
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000612 goto out_status;
Denys Vlasenko9ab5a8d2018-10-30 17:04:52 +0100613 ocount = 0;
Rob Landley2686d3b2006-05-16 16:52:12 +0000614 }
615 }
Denys Vlasenko9ab5a8d2018-10-30 17:04:52 +0100616 } else
617#endif
618 {
Denys Vlasenkof3114a32013-08-20 12:40:29 +0200619 if (write_and_stats(ibuf, n, obs, outfile))
620 goto out_status;
621 }
Rostislav Skudnovdba0dc12017-03-16 20:54:35 +0100622 }
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000623
Rostislav Skudnovdba0dc12017-03-16 20:54:35 +0100624 if (G.flags & FLAG_FSYNC) {
625 if (fsync(ofd) < 0)
626 goto die_outfile;
Glenn L McGrathf0b073f2000-09-11 00:32:13 +0000627 }
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000628
Denys Vlasenko9ab5a8d2018-10-30 17:04:52 +0100629#if ENABLE_FEATURE_DD_IBS_OBS
630 if (ocount != 0) {
631 if (write_and_stats(obuf, ocount, obs, outfile))
Denys Vlasenkof3114a32013-08-20 12:40:29 +0200632 goto out_status;
Rob Landley2686d3b2006-05-16 16:52:12 +0000633 }
Denys Vlasenko9ab5a8d2018-10-30 17:04:52 +0100634#endif
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000635 if (close(ifd) < 0) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000636 die_infile:
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000637 bb_simple_perror_msg_and_die(infile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000638 }
Matt Kraai3eeab3b2001-12-07 16:27:37 +0000639
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000640 if (close(ofd) < 0) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000641 die_outfile:
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000642 bb_simple_perror_msg_and_die(outfile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000643 }
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000644
645 exitcode = EXIT_SUCCESS;
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000646 out_status:
Ari Sundholmf22a8382015-02-07 01:41:22 +0100647 if (!ENABLE_FEATURE_DD_STATUS || !(G.flags & FLAG_STATUS_NONE))
648 dd_output_status(0);
Matt Kraai24ac0172000-12-18 21:38:57 +0000649
Denys Vlasenko2aaacc12020-12-14 20:27:57 +0100650#if 0 /* can't just free(), they can be mmap()ed */
Alexey Solovievf9221542011-03-16 10:39:46 +0300651 if (ENABLE_FEATURE_CLEAN_UP) {
652 free(obuf);
Ari Sundholmf22a8382015-02-07 01:41:22 +0100653 if (G.flags & FLAG_TWOBUFS)
Alexey Solovievf9221542011-03-16 10:39:46 +0300654 free(ibuf);
655 }
Denys Vlasenko2aaacc12020-12-14 20:27:57 +0100656#endif
Alexey Solovievf9221542011-03-16 10:39:46 +0300657
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000658 return exitcode;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000659}