Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 2 | /* |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 3 | * Mini dd implementation for busybox |
| 4 | * |
Matt Kraai | 3eeab3b | 2001-12-07 16:27:37 +0000 | [diff] [blame] | 5 | * Copyright (C) 2000,2001 Matt Kraai |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 8 | */ |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 9 | //config:config DD |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 10 | //config: bool "dd (7.5 kb)" |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 11 | //config: default y |
| 12 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 13 | //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 Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 16 | //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 Vlasenko | 18e781d | 2017-08-02 14:12:48 +0200 | [diff] [blame] | 22 | //config: Sending a SIGUSR1 signal to a running 'dd' process makes it |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 23 | //config: print to standard error the number of records read and written |
| 24 | //config: so far, then to resume copying. |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 25 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 26 | //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 Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 30 | //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 Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 36 | //config: Displays a coreutils-like third status line with transferred bytes, |
| 37 | //config: elapsed time and speed. |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 38 | //config: |
| 39 | //config:config FEATURE_DD_IBS_OBS |
Rostislav Skudnov | 3f6a978 | 2018-10-15 10:26:15 +0000 | [diff] [blame] | 40 | //config: bool "Enable ibs, obs, iflag, oflag and conv options" |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 41 | //config: default y |
| 42 | //config: depends on DD |
| 43 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 44 | //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 Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 46 | //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 Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 52 | //config: Enable support for status=noxfer/none option. |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 53 | |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 54 | //applet:IF_DD(APPLET_NOEXEC(dd, dd, BB_DIR_BIN, BB_SUID_DROP, dd)) |
| 55 | |
| 56 | //kbuild:lib-$(CONFIG_DD) += dd.o |
| 57 | |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 58 | //usage:#define dd_trivial_usage |
Denys Vlasenko | 9ab5a8d | 2018-10-30 17:04:52 +0100 | [diff] [blame] | 59 | //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" |
Rafał Miłecki | 4eb46e1 | 2021-03-24 16:01:42 +0100 | [diff] [blame] | 62 | //usage: " [iflag=skip_bytes|count_bytes|fullblock|direct] [oflag=seek_bytes|append|direct]" |
Denys Vlasenko | 9ab5a8d | 2018-10-30 17:04:52 +0100 | [diff] [blame] | 63 | //usage: ) |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 64 | //usage:#define dd_full_usage "\n\n" |
| 65 | //usage: "Copy a file with converting and formatting\n" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 66 | //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 Vlasenko | b941316 | 2013-08-19 09:01:39 +0200 | [diff] [blame] | 83 | //usage: "\n conv=swab Swap every pair of bytes" |
Rafał Miłecki | c30a5b1 | 2016-02-01 02:17:28 +0100 | [diff] [blame] | 84 | //usage: "\n iflag=skip_bytes skip=N is in bytes" |
Rafał Miłecki | 4eb46e1 | 2021-03-24 16:01:42 +0100 | [diff] [blame] | 85 | //usage: "\n iflag=count_bytes count=N is in bytes" |
Rostislav Skudnov | 3f6a978 | 2018-10-15 10:26:15 +0000 | [diff] [blame] | 86 | //usage: "\n oflag=seek_bytes seek=N is in bytes" |
Denys Vlasenko | 2aaacc1 | 2020-12-14 20:27:57 +0100 | [diff] [blame] | 87 | //usage: "\n iflag=direct O_DIRECT input" |
| 88 | //usage: "\n oflag=direct O_DIRECT output" |
| 89 | //usage: "\n iflag=fullblock Read full blocks" |
| 90 | //usage: "\n oflag=append Open output in append mode" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 91 | //usage: ) |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 92 | //usage: IF_FEATURE_DD_STATUS( |
| 93 | //usage: "\n status=noxfer Suppress rate output" |
| 94 | //usage: "\n status=none Suppress all output" |
| 95 | //usage: ) |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 96 | //usage: "\n" |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 97 | //usage: "\nN may be suffixed by c (1), w (2), b (512), kB (1000), k (1024), MB, M, GB, G" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 98 | //usage: |
| 99 | //usage:#define dd_example_usage |
| 100 | //usage: "$ dd if=/dev/zero of=/dev/ram1 bs=1M count=4\n" |
| 101 | //usage: "4+0 records in\n" |
| 102 | //usage: "4+0 records out\n" |
| 103 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 104 | #include "libbb.h" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 105 | #include "common_bufsiz.h" |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 106 | |
| 107 | /* This is a NOEXEC applet. Be very careful! */ |
| 108 | |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 109 | |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 110 | enum { |
| 111 | ifd = STDIN_FILENO, |
| 112 | ofd = STDOUT_FILENO, |
| 113 | }; |
| 114 | |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 115 | struct globals { |
| 116 | off_t out_full, out_part, in_full, in_part; |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 117 | #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE |
| 118 | unsigned long long total_bytes; |
| 119 | unsigned long long begin_time_us; |
| 120 | #endif |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 121 | int flags; |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 122 | } FIX_ALIASING; |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 123 | #define G (*(struct globals*)bb_common_bufsiz1) |
Denys Vlasenko | 1b34d4f | 2009-09-30 02:39:57 +0200 | [diff] [blame] | 124 | #define INIT_G() do { \ |
Denys Vlasenko | 47cfbf3 | 2016-04-21 18:18:48 +0200 | [diff] [blame] | 125 | setup_common_bufsiz(); \ |
Denys Vlasenko | 1b34d4f | 2009-09-30 02:39:57 +0200 | [diff] [blame] | 126 | /* we have to zero it out because of NOEXEC */ \ |
| 127 | memset(&G, 0, sizeof(G)); \ |
| 128 | } while (0) |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 129 | |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 130 | enum { |
| 131 | /* Must be in the same order as OP_conv_XXX! */ |
| 132 | /* (see "flags |= (1 << what)" below) */ |
| 133 | FLAG_NOTRUNC = (1 << 0) * ENABLE_FEATURE_DD_IBS_OBS, |
| 134 | FLAG_SYNC = (1 << 1) * ENABLE_FEATURE_DD_IBS_OBS, |
| 135 | FLAG_NOERROR = (1 << 2) * ENABLE_FEATURE_DD_IBS_OBS, |
| 136 | FLAG_FSYNC = (1 << 3) * ENABLE_FEATURE_DD_IBS_OBS, |
| 137 | FLAG_SWAB = (1 << 4) * ENABLE_FEATURE_DD_IBS_OBS, |
| 138 | /* end of conv flags */ |
Rafał Miłecki | c30a5b1 | 2016-02-01 02:17:28 +0100 | [diff] [blame] | 139 | /* start of input flags */ |
Rafał Miłecki | 4eb46e1 | 2021-03-24 16:01:42 +0100 | [diff] [blame] | 140 | FLAG_IFLAG_SHIFT = 5, |
| 141 | FLAG_SKIP_BYTES = (1 << 5) * ENABLE_FEATURE_DD_IBS_OBS, |
| 142 | FLAG_COUNT_BYTES = (1 << 6) * ENABLE_FEATURE_DD_IBS_OBS, |
| 143 | FLAG_FULLBLOCK = (1 << 7) * ENABLE_FEATURE_DD_IBS_OBS, |
| 144 | FLAG_IDIRECT = (1 << 8) * ENABLE_FEATURE_DD_IBS_OBS, |
Rafał Miłecki | c30a5b1 | 2016-02-01 02:17:28 +0100 | [diff] [blame] | 145 | /* end of input flags */ |
Rostislav Skudnov | 3f6a978 | 2018-10-15 10:26:15 +0000 | [diff] [blame] | 146 | /* start of output flags */ |
Rafał Miłecki | 4eb46e1 | 2021-03-24 16:01:42 +0100 | [diff] [blame] | 147 | FLAG_OFLAG_SHIFT = 9, |
| 148 | FLAG_SEEK_BYTES = (1 << 9) * ENABLE_FEATURE_DD_IBS_OBS, |
| 149 | FLAG_APPEND = (1 << 10) * ENABLE_FEATURE_DD_IBS_OBS, |
| 150 | FLAG_ODIRECT = (1 << 11) * ENABLE_FEATURE_DD_IBS_OBS, |
Rostislav Skudnov | 3f6a978 | 2018-10-15 10:26:15 +0000 | [diff] [blame] | 151 | /* end of output flags */ |
Rafał Miłecki | 4eb46e1 | 2021-03-24 16:01:42 +0100 | [diff] [blame] | 152 | FLAG_TWOBUFS = (1 << 12) * ENABLE_FEATURE_DD_IBS_OBS, |
| 153 | FLAG_COUNT = 1 << 13, |
| 154 | FLAG_STATUS_NONE = 1 << 14, |
| 155 | FLAG_STATUS_NOXFER = 1 << 15, |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 156 | }; |
Rob Landley | c559817 | 2006-05-02 22:44:04 +0000 | [diff] [blame] | 157 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 158 | static void dd_output_status(int UNUSED_PARAM cur_signal) |
Rob Landley | c559817 | 2006-05-02 22:44:04 +0000 | [diff] [blame] | 159 | { |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 160 | #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE |
Denys Vlasenko | 7eabffa | 2009-10-14 17:43:41 +0200 | [diff] [blame] | 161 | double seconds; |
| 162 | unsigned long long bytes_sec; |
| 163 | unsigned long long now_us = monotonic_us(); /* before fprintf */ |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 164 | #endif |
| 165 | |
Denis Vlasenko | 980864d | 2007-07-30 10:58:09 +0000 | [diff] [blame] | 166 | /* Deliberately using %u, not %d */ |
| 167 | fprintf(stderr, "%"OFF_FMT"u+%"OFF_FMT"u records in\n" |
| 168 | "%"OFF_FMT"u+%"OFF_FMT"u records out\n", |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 169 | G.in_full, G.in_part, |
| 170 | G.out_full, G.out_part); |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 171 | |
| 172 | #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 173 | # if ENABLE_FEATURE_DD_STATUS |
| 174 | if (G.flags & FLAG_STATUS_NOXFER) /* status=noxfer active? */ |
| 175 | return; |
| 176 | //TODO: should status=none make dd stop reacting to USR1 entirely? |
| 177 | //So far we react to it (we print the stats), |
| 178 | //status=none only suppresses final, non-USR1 generated status message. |
| 179 | # endif |
Rafał Miłecki | 4eb46e1 | 2021-03-24 16:01:42 +0100 | [diff] [blame] | 180 | fprintf(stderr, /*G.total_bytes < 1024 |
| 181 | ? "%llu bytes copied, " : */ "%llu bytes (%sB) copied, " |
| 182 | , G.total_bytes, |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 183 | /* show fractional digit, use suffixes */ |
| 184 | make_human_readable_str(G.total_bytes, 1, 0) |
| 185 | ); |
| 186 | /* Corner cases: |
| 187 | * ./busybox dd </dev/null >/dev/null |
| 188 | * ./busybox dd bs=1M count=2000 </dev/zero >/dev/null |
| 189 | * (echo DONE) | ./busybox dd >/dev/null |
| 190 | * (sleep 1; echo DONE) | ./busybox dd >/dev/null |
| 191 | */ |
Denys Vlasenko | 7eabffa | 2009-10-14 17:43:41 +0200 | [diff] [blame] | 192 | seconds = (now_us - G.begin_time_us) / 1000000.0; |
| 193 | bytes_sec = G.total_bytes / seconds; |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 194 | fprintf(stderr, "%f seconds, %sB/s\n", |
Denys Vlasenko | 7eabffa | 2009-10-14 17:43:41 +0200 | [diff] [blame] | 195 | seconds, |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 196 | /* show fractional digit, use suffixes */ |
Denys Vlasenko | 7eabffa | 2009-10-14 17:43:41 +0200 | [diff] [blame] | 197 | make_human_readable_str(bytes_sec, 1, 0) |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 198 | ); |
| 199 | #endif |
Rob Landley | c559817 | 2006-05-02 22:44:04 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Denys Vlasenko | 2aaacc1 | 2020-12-14 20:27:57 +0100 | [diff] [blame] | 202 | #if ENABLE_FEATURE_DD_IBS_OBS |
| 203 | static int clear_O_DIRECT(int fd) |
| 204 | { |
| 205 | if (errno == EINVAL) { |
| 206 | int fl = fcntl(fd, F_GETFL); |
| 207 | if (fl & O_DIRECT) { |
| 208 | fcntl(fd, F_SETFL, fl & ~O_DIRECT); |
| 209 | return 1; |
| 210 | } |
| 211 | } |
| 212 | return 0; |
| 213 | } |
| 214 | #endif |
| 215 | |
| 216 | static ssize_t dd_read(void *ibuf, size_t ibs) |
| 217 | { |
| 218 | ssize_t n; |
| 219 | |
| 220 | #if ENABLE_FEATURE_DD_IBS_OBS |
| 221 | read_again: |
| 222 | if (G.flags & FLAG_FULLBLOCK) |
| 223 | n = full_read(ifd, ibuf, ibs); |
| 224 | else |
| 225 | #endif |
| 226 | n = safe_read(ifd, ibuf, ibs); |
| 227 | #if ENABLE_FEATURE_DD_IBS_OBS |
| 228 | if (n < 0 && (G.flags & FLAG_IDIRECT) && clear_O_DIRECT(ifd)) |
| 229 | goto read_again; |
| 230 | #endif |
| 231 | return n; |
| 232 | } |
| 233 | |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 234 | static bool write_and_stats(const void *buf, size_t len, size_t obs, |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 235 | const char *filename) |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 236 | { |
Denys Vlasenko | 875ce09 | 2019-05-14 17:46:18 +0200 | [diff] [blame] | 237 | ssize_t n; |
| 238 | |
Denys Vlasenko | 2aaacc1 | 2020-12-14 20:27:57 +0100 | [diff] [blame] | 239 | IF_FEATURE_DD_IBS_OBS(write_again:) |
Denys Vlasenko | 875ce09 | 2019-05-14 17:46:18 +0200 | [diff] [blame] | 240 | n = full_write(ofd, buf, len); |
Denys Vlasenko | 2aaacc1 | 2020-12-14 20:27:57 +0100 | [diff] [blame] | 241 | #if ENABLE_FEATURE_DD_IBS_OBS |
| 242 | if (n < 0 && (G.flags & FLAG_ODIRECT) && clear_O_DIRECT(ofd)) |
| 243 | goto write_again; |
| 244 | #endif |
| 245 | |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 246 | #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE |
Denys Vlasenko | 875ce09 | 2019-05-14 17:46:18 +0200 | [diff] [blame] | 247 | if (n > 0) |
| 248 | G.total_bytes += n; |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 249 | #endif |
Denys Vlasenko | 5afd63a | 2018-01-10 10:58:55 +0100 | [diff] [blame] | 250 | if ((size_t)n == obs) { |
| 251 | G.out_full++; |
| 252 | return 0; |
| 253 | } |
Denys Vlasenko | 2c87677 | 2018-01-10 11:04:09 +0100 | [diff] [blame] | 254 | if ((size_t)n == len) { |
Denys Vlasenko | 5afd63a | 2018-01-10 10:58:55 +0100 | [diff] [blame] | 255 | G.out_part++; |
Denys Vlasenko | 2c87677 | 2018-01-10 11:04:09 +0100 | [diff] [blame] | 256 | return 0; |
| 257 | } |
Denys Vlasenko | 875ce09 | 2019-05-14 17:46:18 +0200 | [diff] [blame] | 258 | /* n is < len (and possibly is -1). |
| 259 | * Even if n >= 0, errno is usually set correctly. |
| 260 | * For example, if writing to block device and getting ENOSPC, |
| 261 | * full_write() first sees a short write, then tries to write |
| 262 | * the remainder and gets errno set to ENOSPC. |
| 263 | * It returns n > 0 (the amount which it did write). |
| 264 | */ |
| 265 | bb_perror_msg("error writing '%s'", filename); |
Denys Vlasenko | 5afd63a | 2018-01-10 10:58:55 +0100 | [diff] [blame] | 266 | return 1; |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Denis Vlasenko | 5dd27b1 | 2006-11-25 14:46:21 +0000 | [diff] [blame] | 269 | #if ENABLE_LFS |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 270 | # define XATOU_SFX xatoull_sfx |
Denis Vlasenko | 5dd27b1 | 2006-11-25 14:46:21 +0000 | [diff] [blame] | 271 | #else |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 272 | # define XATOU_SFX xatoul_sfx |
Denis Vlasenko | 5dd27b1 | 2006-11-25 14:46:21 +0000 | [diff] [blame] | 273 | #endif |
| 274 | |
Rafał Miłecki | c30a5b1 | 2016-02-01 02:17:28 +0100 | [diff] [blame] | 275 | #if ENABLE_FEATURE_DD_IBS_OBS |
| 276 | static int parse_comma_flags(char *val, const char *words, const char *error_in) |
| 277 | { |
| 278 | int flags = 0; |
| 279 | while (1) { |
| 280 | int n; |
| 281 | char *arg; |
| 282 | /* find ',', replace them with NUL so we can use val for |
| 283 | * index_in_strings() without copying. |
| 284 | * We rely on val being non-null, else strchr would fault. |
| 285 | */ |
| 286 | arg = strchr(val, ','); |
| 287 | if (arg) |
| 288 | *arg = '\0'; |
| 289 | n = index_in_strings(words, val); |
| 290 | if (n < 0) |
| 291 | bb_error_msg_and_die(bb_msg_invalid_arg_to, val, error_in); |
| 292 | flags |= (1 << n); |
| 293 | if (!arg) /* no ',' left, so this was the last specifier */ |
| 294 | break; |
| 295 | *arg = ','; /* to preserve ps listing */ |
| 296 | val = arg + 1; /* skip this keyword and ',' */ |
| 297 | } |
| 298 | return flags; |
| 299 | } |
| 300 | #endif |
| 301 | |
Denys Vlasenko | 2aaacc1 | 2020-12-14 20:27:57 +0100 | [diff] [blame] | 302 | static void *alloc_buf(size_t size) |
| 303 | { |
| 304 | /* Important for "{i,o}flag=direct" - buffers must be page aligned */ |
| 305 | if (size >= bb_getpagesize()) |
| 306 | return xmmap_anon(size); |
| 307 | return xmalloc(size); |
| 308 | } |
| 309 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 310 | int dd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 311 | int dd_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 312 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 313 | static const char keywords[] ALIGN1 = |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 314 | "bs\0""count\0""seek\0""skip\0""if\0""of\0"IF_FEATURE_DD_STATUS("status\0") |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 315 | #if ENABLE_FEATURE_DD_IBS_OBS |
Rostislav Skudnov | 3f6a978 | 2018-10-15 10:26:15 +0000 | [diff] [blame] | 316 | "ibs\0""obs\0""conv\0""iflag\0""oflag\0" |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 317 | #endif |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 318 | ; |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 319 | #if ENABLE_FEATURE_DD_IBS_OBS |
| 320 | static const char conv_words[] ALIGN1 = |
Denys Vlasenko | b941316 | 2013-08-19 09:01:39 +0200 | [diff] [blame] | 321 | "notrunc\0""sync\0""noerror\0""fsync\0""swab\0"; |
Rafał Miłecki | c30a5b1 | 2016-02-01 02:17:28 +0100 | [diff] [blame] | 322 | static const char iflag_words[] ALIGN1 = |
Rafał Miłecki | 4eb46e1 | 2021-03-24 16:01:42 +0100 | [diff] [blame] | 323 | "skip_bytes\0""count_bytes\0""fullblock\0""direct\0"; |
Rostislav Skudnov | 3f6a978 | 2018-10-15 10:26:15 +0000 | [diff] [blame] | 324 | static const char oflag_words[] ALIGN1 = |
Denys Vlasenko | 2aaacc1 | 2020-12-14 20:27:57 +0100 | [diff] [blame] | 325 | "seek_bytes\0append\0""direct\0"; |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 326 | #endif |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 327 | #if ENABLE_FEATURE_DD_STATUS |
| 328 | static const char status_words[] ALIGN1 = |
| 329 | "none\0""noxfer\0"; |
| 330 | #endif |
Bernhard Reutner-Fischer | 99003b8 | 2007-04-10 19:30:50 +0000 | [diff] [blame] | 331 | enum { |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 332 | OP_bs = 0, |
Bernhard Reutner-Fischer | 99003b8 | 2007-04-10 19:30:50 +0000 | [diff] [blame] | 333 | OP_count, |
| 334 | OP_seek, |
| 335 | OP_skip, |
| 336 | OP_if, |
| 337 | OP_of, |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 338 | IF_FEATURE_DD_STATUS(OP_status,) |
Bernhard Reutner-Fischer | e468ef2 | 2007-04-16 12:21:05 +0000 | [diff] [blame] | 339 | #if ENABLE_FEATURE_DD_IBS_OBS |
Bernhard Reutner-Fischer | 99003b8 | 2007-04-10 19:30:50 +0000 | [diff] [blame] | 340 | OP_ibs, |
| 341 | OP_obs, |
| 342 | OP_conv, |
Rafał Miłecki | c30a5b1 | 2016-02-01 02:17:28 +0100 | [diff] [blame] | 343 | OP_iflag, |
Rostislav Skudnov | 3f6a978 | 2018-10-15 10:26:15 +0000 | [diff] [blame] | 344 | OP_oflag, |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 345 | /* Must be in the same order as FLAG_XXX! */ |
| 346 | OP_conv_notrunc = 0, |
Bernhard Reutner-Fischer | 99003b8 | 2007-04-10 19:30:50 +0000 | [diff] [blame] | 347 | OP_conv_sync, |
Denis Vlasenko | f6250a3 | 2007-04-19 20:16:14 +0000 | [diff] [blame] | 348 | OP_conv_noerror, |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 349 | OP_conv_fsync, |
Denys Vlasenko | b941316 | 2013-08-19 09:01:39 +0200 | [diff] [blame] | 350 | OP_conv_swab, |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 351 | /* Unimplemented conv=XXX: */ |
| 352 | //nocreat do not create the output file |
| 353 | //excl fail if the output file already exists |
| 354 | //fdatasync physically write output file data before finishing |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 355 | //lcase change upper case to lower case |
| 356 | //ucase change lower case to upper case |
| 357 | //block pad newline-terminated records with spaces to cbs-size |
| 358 | //unblock replace trailing spaces in cbs-size records with newline |
| 359 | //ascii from EBCDIC to ASCII |
| 360 | //ebcdic from ASCII to EBCDIC |
| 361 | //ibm from ASCII to alternate EBCDIC |
Denys Vlasenko | b941316 | 2013-08-19 09:01:39 +0200 | [diff] [blame] | 362 | /* Partially implemented: */ |
| 363 | //swab swap every pair of input bytes: will abort on non-even reads |
Rafał Miłecki | c30a5b1 | 2016-02-01 02:17:28 +0100 | [diff] [blame] | 364 | OP_iflag_skip_bytes, |
Rafał Miłecki | 4eb46e1 | 2021-03-24 16:01:42 +0100 | [diff] [blame] | 365 | OP_iflag_count_bytes, |
Nicholas Clark | 77a6678 | 2018-01-25 19:00:19 +0100 | [diff] [blame] | 366 | OP_iflag_fullblock, |
Denys Vlasenko | 2aaacc1 | 2020-12-14 20:27:57 +0100 | [diff] [blame] | 367 | OP_iflag_direct, |
Rostislav Skudnov | 3f6a978 | 2018-10-15 10:26:15 +0000 | [diff] [blame] | 368 | OP_oflag_seek_bytes, |
Denys Vlasenko | 2aaacc1 | 2020-12-14 20:27:57 +0100 | [diff] [blame] | 369 | OP_oflag_direct, |
Bernhard Reutner-Fischer | e468ef2 | 2007-04-16 12:21:05 +0000 | [diff] [blame] | 370 | #endif |
Bernhard Reutner-Fischer | 99003b8 | 2007-04-10 19:30:50 +0000 | [diff] [blame] | 371 | }; |
Denys Vlasenko | 5b9910f | 2013-08-20 02:50:49 +0200 | [diff] [blame] | 372 | smallint exitcode = EXIT_FAILURE; |
Denys Vlasenko | 4b1896c | 2013-08-19 09:00:08 +0200 | [diff] [blame] | 373 | int i; |
Denys Vlasenko | 4502bb1 | 2013-08-20 12:08:46 +0200 | [diff] [blame] | 374 | size_t ibs = 512; |
| 375 | char *ibuf; |
| 376 | #if ENABLE_FEATURE_DD_IBS_OBS |
| 377 | size_t obs = 512; |
| 378 | char *obuf; |
| 379 | #else |
| 380 | # define obs ibs |
| 381 | # define obuf ibuf |
| 382 | #endif |
| 383 | /* These are all zeroed at once! */ |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 384 | struct { |
Denys Vlasenko | 9ab5a8d | 2018-10-30 17:04:52 +0100 | [diff] [blame] | 385 | IF_FEATURE_DD_IBS_OBS(size_t ocount;) |
Denys Vlasenko | 5b9910f | 2013-08-20 02:50:49 +0200 | [diff] [blame] | 386 | ssize_t prev_read_size; /* for detecting swab failure */ |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 387 | off_t count; |
| 388 | off_t seek, skip; |
| 389 | const char *infile, *outfile; |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 390 | } Z; |
Denys Vlasenko | 9ab5a8d | 2018-10-30 17:04:52 +0100 | [diff] [blame] | 391 | #define ocount (Z.ocount ) |
Denys Vlasenko | 5b9910f | 2013-08-20 02:50:49 +0200 | [diff] [blame] | 392 | #define prev_read_size (Z.prev_read_size) |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 393 | #define count (Z.count ) |
| 394 | #define seek (Z.seek ) |
| 395 | #define skip (Z.skip ) |
| 396 | #define infile (Z.infile ) |
| 397 | #define outfile (Z.outfile) |
Eric Andersen | ddea368 | 2000-11-29 22:33:02 +0000 | [diff] [blame] | 398 | |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 399 | memset(&Z, 0, sizeof(Z)); |
| 400 | INIT_G(); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 401 | //fflush_all(); - is this needed because of NOEXEC? |
Denis Vlasenko | f6250a3 | 2007-04-19 20:16:14 +0000 | [diff] [blame] | 402 | |
Denys Vlasenko | 4b1896c | 2013-08-19 09:00:08 +0200 | [diff] [blame] | 403 | for (i = 1; argv[i]; i++) { |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 404 | int what; |
| 405 | char *val; |
Denys Vlasenko | 4b1896c | 2013-08-19 09:00:08 +0200 | [diff] [blame] | 406 | char *arg = argv[i]; |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 407 | |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 408 | #if ENABLE_DESKTOP |
| 409 | /* "dd --". NB: coreutils 6.9 will complain if they see |
| 410 | * more than one of them. We wouldn't. */ |
| 411 | if (arg[0] == '-' && arg[1] == '-' && arg[2] == '\0') |
| 412 | continue; |
| 413 | #endif |
| 414 | val = strchr(arg, '='); |
| 415 | if (val == NULL) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 416 | bb_show_usage(); |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 417 | *val = '\0'; |
| 418 | what = index_in_strings(keywords, arg); |
| 419 | if (what < 0) |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 420 | bb_show_usage(); |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 421 | /* *val = '='; - to preserve ps listing? */ |
| 422 | val++; |
Bernhard Reutner-Fischer | e468ef2 | 2007-04-16 12:21:05 +0000 | [diff] [blame] | 423 | #if ENABLE_FEATURE_DD_IBS_OBS |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 424 | if (what == OP_ibs) { |
| 425 | /* Must fit into positive ssize_t */ |
Ari Sundholm | 8893023 | 2015-03-04 18:46:48 +0200 | [diff] [blame] | 426 | ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, cwbkMG_suffixes); |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 427 | /*continue;*/ |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 428 | } |
| 429 | if (what == OP_obs) { |
Ari Sundholm | 8893023 | 2015-03-04 18:46:48 +0200 | [diff] [blame] | 430 | obs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, cwbkMG_suffixes); |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 431 | /*continue;*/ |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 432 | } |
| 433 | if (what == OP_conv) { |
Rafał Miłecki | c30a5b1 | 2016-02-01 02:17:28 +0100 | [diff] [blame] | 434 | G.flags |= parse_comma_flags(val, conv_words, "conv"); |
| 435 | /*continue;*/ |
| 436 | } |
| 437 | if (what == OP_iflag) { |
| 438 | G.flags |= parse_comma_flags(val, iflag_words, "iflag") << FLAG_IFLAG_SHIFT; |
Denys Vlasenko | 4502bb1 | 2013-08-20 12:08:46 +0200 | [diff] [blame] | 439 | /*continue;*/ |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 440 | } |
Rostislav Skudnov | 3f6a978 | 2018-10-15 10:26:15 +0000 | [diff] [blame] | 441 | if (what == OP_oflag) { |
| 442 | G.flags |= parse_comma_flags(val, oflag_words, "oflag") << FLAG_OFLAG_SHIFT; |
| 443 | /*continue;*/ |
| 444 | } |
Bernhard Reutner-Fischer | e468ef2 | 2007-04-16 12:21:05 +0000 | [diff] [blame] | 445 | #endif |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 446 | if (what == OP_bs) { |
Ari Sundholm | 8893023 | 2015-03-04 18:46:48 +0200 | [diff] [blame] | 447 | ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, cwbkMG_suffixes); |
Denys Vlasenko | 4502bb1 | 2013-08-20 12:08:46 +0200 | [diff] [blame] | 448 | obs = ibs; |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 449 | /*continue;*/ |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 450 | } |
| 451 | /* These can be large: */ |
| 452 | if (what == OP_count) { |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 453 | G.flags |= FLAG_COUNT; |
Ari Sundholm | 8893023 | 2015-03-04 18:46:48 +0200 | [diff] [blame] | 454 | count = XATOU_SFX(val, cwbkMG_suffixes); |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 455 | /*continue;*/ |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 456 | } |
| 457 | if (what == OP_seek) { |
Ari Sundholm | 8893023 | 2015-03-04 18:46:48 +0200 | [diff] [blame] | 458 | seek = XATOU_SFX(val, cwbkMG_suffixes); |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 459 | /*continue;*/ |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 460 | } |
Denis Vlasenko | d1801a4 | 2007-04-19 20:08:19 +0000 | [diff] [blame] | 461 | if (what == OP_skip) { |
Ari Sundholm | 8893023 | 2015-03-04 18:46:48 +0200 | [diff] [blame] | 462 | skip = XATOU_SFX(val, cwbkMG_suffixes); |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 463 | /*continue;*/ |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 464 | } |
| 465 | if (what == OP_if) { |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 466 | infile = val; |
| 467 | /*continue;*/ |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 468 | } |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 469 | if (what == OP_of) { |
| 470 | outfile = val; |
| 471 | /*continue;*/ |
| 472 | } |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 473 | #if ENABLE_FEATURE_DD_STATUS |
| 474 | if (what == OP_status) { |
| 475 | int n; |
| 476 | n = index_in_strings(status_words, val); |
| 477 | if (n < 0) |
Denys Vlasenko | 0f296a3 | 2015-10-14 13:21:01 +0200 | [diff] [blame] | 478 | bb_error_msg_and_die(bb_msg_invalid_arg_to, val, "status"); |
Denys Vlasenko | d5b98e2 | 2017-07-14 13:44:30 +0200 | [diff] [blame] | 479 | G.flags |= FLAG_STATUS_NONE << n; |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 480 | /*continue;*/ |
| 481 | } |
| 482 | #endif |
Denys Vlasenko | 4b1896c | 2013-08-19 09:00:08 +0200 | [diff] [blame] | 483 | } /* end of "for (argv[i])" */ |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 484 | |
Denys Vlasenko | 2aaacc1 | 2020-12-14 20:27:57 +0100 | [diff] [blame] | 485 | ibuf = alloc_buf(ibs); |
Denys Vlasenko | 4502bb1 | 2013-08-20 12:08:46 +0200 | [diff] [blame] | 486 | obuf = ibuf; |
| 487 | #if ENABLE_FEATURE_DD_IBS_OBS |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 488 | if (ibs != obs) { |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 489 | G.flags |= FLAG_TWOBUFS; |
Denys Vlasenko | 2aaacc1 | 2020-12-14 20:27:57 +0100 | [diff] [blame] | 490 | obuf = alloc_buf(obs); |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 491 | } |
Denys Vlasenko | 4502bb1 | 2013-08-20 12:08:46 +0200 | [diff] [blame] | 492 | #endif |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 493 | |
| 494 | #if ENABLE_FEATURE_DD_SIGNAL_HANDLING |
| 495 | signal_SA_RESTART_empty_mask(SIGUSR1, dd_output_status); |
| 496 | #endif |
| 497 | #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE |
| 498 | G.begin_time_us = monotonic_us(); |
| 499 | #endif |
| 500 | |
Denys Vlasenko | 4502bb1 | 2013-08-20 12:08:46 +0200 | [diff] [blame] | 501 | if (infile) { |
Denys Vlasenko | 2aaacc1 | 2020-12-14 20:27:57 +0100 | [diff] [blame] | 502 | int iflag = O_RDONLY; |
| 503 | #if ENABLE_FEATURE_DD_IBS_OBS |
| 504 | if (G.flags & FLAG_IDIRECT) |
| 505 | iflag |= O_DIRECT; |
| 506 | #endif |
| 507 | xmove_fd(xopen(infile, iflag), ifd); |
Denys Vlasenko | 4502bb1 | 2013-08-20 12:08:46 +0200 | [diff] [blame] | 508 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 509 | infile = bb_msg_standard_input; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 510 | } |
Denys Vlasenko | 4502bb1 | 2013-08-20 12:08:46 +0200 | [diff] [blame] | 511 | if (outfile) { |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 512 | int oflag = O_WRONLY | O_CREAT; |
Matt Kraai | c9acf8c | 2001-01-17 00:21:05 +0000 | [diff] [blame] | 513 | |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 514 | if (!seek && !(G.flags & FLAG_NOTRUNC)) |
Matt Kraai | c9acf8c | 2001-01-17 00:21:05 +0000 | [diff] [blame] | 515 | oflag |= O_TRUNC; |
Rostislav Skudnov | d712edc | 2019-02-06 11:57:15 +0000 | [diff] [blame] | 516 | if (G.flags & FLAG_APPEND) |
| 517 | oflag |= O_APPEND; |
Denys Vlasenko | 2aaacc1 | 2020-12-14 20:27:57 +0100 | [diff] [blame] | 518 | #if ENABLE_FEATURE_DD_IBS_OBS |
| 519 | if (G.flags & FLAG_ODIRECT) |
| 520 | oflag |= O_DIRECT; |
| 521 | #endif |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 522 | xmove_fd(xopen(outfile, oflag), ofd); |
Matt Kraai | c9acf8c | 2001-01-17 00:21:05 +0000 | [diff] [blame] | 523 | |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 524 | if (seek && !(G.flags & FLAG_NOTRUNC)) { |
Rostislav Skudnov | 3f6a978 | 2018-10-15 10:26:15 +0000 | [diff] [blame] | 525 | size_t blocksz = (G.flags & FLAG_SEEK_BYTES) ? 1 : obs; |
| 526 | if (ftruncate(ofd, seek * blocksz) < 0) { |
Matt Kraai | eb83478 | 2002-02-05 15:28:54 +0000 | [diff] [blame] | 527 | struct stat st; |
| 528 | |
Denys Vlasenko | 6b9f163 | 2010-01-28 02:24:24 +0100 | [diff] [blame] | 529 | if (fstat(ofd, &st) < 0 |
| 530 | || S_ISREG(st.st_mode) |
| 531 | || S_ISDIR(st.st_mode) |
| 532 | ) { |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 533 | goto die_outfile; |
Denys Vlasenko | 6b9f163 | 2010-01-28 02:24:24 +0100 | [diff] [blame] | 534 | } |
Matt Kraai | eb83478 | 2002-02-05 15:28:54 +0000 | [diff] [blame] | 535 | } |
Matt Kraai | c9acf8c | 2001-01-17 00:21:05 +0000 | [diff] [blame] | 536 | } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 537 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 538 | outfile = bb_msg_standard_output; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 539 | } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 540 | if (skip) { |
Rafał Miłecki | c30a5b1 | 2016-02-01 02:17:28 +0100 | [diff] [blame] | 541 | size_t blocksz = (G.flags & FLAG_SKIP_BYTES) ? 1 : ibs; |
| 542 | if (lseek(ifd, skip * blocksz, SEEK_CUR) < 0) { |
Denys Vlasenko | 1b57fe1 | 2013-08-20 12:45:05 +0200 | [diff] [blame] | 543 | do { |
Denys Vlasenko | 2aaacc1 | 2020-12-14 20:27:57 +0100 | [diff] [blame] | 544 | ssize_t n = dd_read(ibuf, blocksz); |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 545 | if (n < 0) |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 546 | goto die_infile; |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 547 | if (n == 0) |
| 548 | break; |
Denys Vlasenko | 1b57fe1 | 2013-08-20 12:45:05 +0200 | [diff] [blame] | 549 | } while (--skip != 0); |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 550 | } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 551 | } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 552 | if (seek) { |
Rostislav Skudnov | 3f6a978 | 2018-10-15 10:26:15 +0000 | [diff] [blame] | 553 | size_t blocksz = (G.flags & FLAG_SEEK_BYTES) ? 1 : obs; |
| 554 | if (lseek(ofd, seek * blocksz, SEEK_CUR) < 0) |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 555 | goto die_outfile; |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 556 | } |
Eric Andersen | ddea368 | 2000-11-29 22:33:02 +0000 | [diff] [blame] | 557 | |
Rafał Miłecki | 4eb46e1 | 2021-03-24 16:01:42 +0100 | [diff] [blame] | 558 | while (1) { |
| 559 | ssize_t n = ibs; |
| 560 | |
| 561 | if (G.flags & FLAG_COUNT) { |
| 562 | if (count == 0) |
| 563 | break; |
| 564 | if ((G.flags & FLAG_COUNT_BYTES) && count < ibs) |
| 565 | n = count; |
| 566 | } |
| 567 | |
| 568 | n = dd_read(ibuf, n); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 569 | if (n == 0) |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 570 | break; |
Eric Andersen | ef38b39 | 2002-04-27 01:31:43 +0000 | [diff] [blame] | 571 | if (n < 0) { |
Denys Vlasenko | f00cfdf | 2009-09-18 20:49:36 +0200 | [diff] [blame] | 572 | /* "Bad block" */ |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 573 | if (!(G.flags & FLAG_NOERROR)) |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 574 | goto die_infile; |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 575 | bb_simple_perror_msg(infile); |
Denys Vlasenko | f00cfdf | 2009-09-18 20:49:36 +0200 | [diff] [blame] | 576 | /* GNU dd with conv=noerror skips over bad blocks */ |
Denys Vlasenko | 334fcc8 | 2009-08-05 23:16:27 +0200 | [diff] [blame] | 577 | xlseek(ifd, ibs, SEEK_CUR); |
Denys Vlasenko | f00cfdf | 2009-09-18 20:49:36 +0200 | [diff] [blame] | 578 | /* conv=noerror,sync writes NULs, |
| 579 | * conv=noerror just ignores input bad blocks */ |
| 580 | n = 0; |
Eric Andersen | ef38b39 | 2002-04-27 01:31:43 +0000 | [diff] [blame] | 581 | } |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 582 | if (G.flags & FLAG_SWAB) { |
Denys Vlasenko | 0ff0b32 | 2013-08-20 12:49:28 +0200 | [diff] [blame] | 583 | uint16_t *p16; |
| 584 | ssize_t n2; |
Denys Vlasenko | 5b9910f | 2013-08-20 02:50:49 +0200 | [diff] [blame] | 585 | |
| 586 | /* Our code allows only last read to be odd-sized */ |
| 587 | if (prev_read_size & 1) |
| 588 | bb_error_msg_and_die("can't swab %lu byte buffer", |
| 589 | (unsigned long)prev_read_size); |
| 590 | prev_read_size = n; |
| 591 | |
Denys Vlasenko | 90e9cfd | 2013-08-20 12:27:19 +0200 | [diff] [blame] | 592 | /* If n is odd, last byte is not swapped: |
Denys Vlasenko | 8395bd3 | 2013-08-19 10:30:55 +0200 | [diff] [blame] | 593 | * echo -n "qwe" | dd conv=swab |
Denys Vlasenko | b941316 | 2013-08-19 09:01:39 +0200 | [diff] [blame] | 594 | * prints "wqe". |
Denys Vlasenko | b941316 | 2013-08-19 09:01:39 +0200 | [diff] [blame] | 595 | */ |
Denys Vlasenko | 5b9910f | 2013-08-20 02:50:49 +0200 | [diff] [blame] | 596 | p16 = (void*) ibuf; |
Denys Vlasenko | 0ff0b32 | 2013-08-20 12:49:28 +0200 | [diff] [blame] | 597 | n2 = (n >> 1); |
| 598 | while (--n2 >= 0) { |
Denys Vlasenko | b941316 | 2013-08-19 09:01:39 +0200 | [diff] [blame] | 599 | *p16 = bswap_16(*p16); |
| 600 | p16++; |
| 601 | } |
| 602 | } |
Rafał Miłecki | 4eb46e1 | 2021-03-24 16:01:42 +0100 | [diff] [blame] | 603 | count -= (G.flags & FLAG_COUNT_BYTES) ? n : 1; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 604 | if ((size_t)n == ibs) |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 605 | G.in_full++; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 606 | else { |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 607 | G.in_part++; |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 608 | if (G.flags & FLAG_SYNC) { |
Denys Vlasenko | f00cfdf | 2009-09-18 20:49:36 +0200 | [diff] [blame] | 609 | memset(ibuf + n, 0, ibs - n); |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 610 | n = ibs; |
| 611 | } |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 612 | } |
Denys Vlasenko | 9ab5a8d | 2018-10-30 17:04:52 +0100 | [diff] [blame] | 613 | #if ENABLE_FEATURE_DD_IBS_OBS |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 614 | if (G.flags & FLAG_TWOBUFS) { |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 615 | char *tmp = ibuf; |
| 616 | while (n) { |
Denys Vlasenko | 9ab5a8d | 2018-10-30 17:04:52 +0100 | [diff] [blame] | 617 | size_t d = obs - ocount; |
Denis Vlasenko | 77ad97f | 2008-05-13 02:27:31 +0000 | [diff] [blame] | 618 | if (d > (size_t)n) |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 619 | d = n; |
Denys Vlasenko | 9ab5a8d | 2018-10-30 17:04:52 +0100 | [diff] [blame] | 620 | memcpy(obuf + ocount, tmp, d); |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 621 | n -= d; |
| 622 | tmp += d; |
Denys Vlasenko | 9ab5a8d | 2018-10-30 17:04:52 +0100 | [diff] [blame] | 623 | ocount += d; |
| 624 | if (ocount == obs) { |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 625 | if (write_and_stats(obuf, obs, obs, outfile)) |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 626 | goto out_status; |
Denys Vlasenko | 9ab5a8d | 2018-10-30 17:04:52 +0100 | [diff] [blame] | 627 | ocount = 0; |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 628 | } |
| 629 | } |
Denys Vlasenko | 9ab5a8d | 2018-10-30 17:04:52 +0100 | [diff] [blame] | 630 | } else |
| 631 | #endif |
| 632 | { |
Denys Vlasenko | f3114a3 | 2013-08-20 12:40:29 +0200 | [diff] [blame] | 633 | if (write_and_stats(ibuf, n, obs, outfile)) |
| 634 | goto out_status; |
| 635 | } |
Rostislav Skudnov | dba0dc1 | 2017-03-16 20:54:35 +0100 | [diff] [blame] | 636 | } |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 637 | |
Rostislav Skudnov | dba0dc1 | 2017-03-16 20:54:35 +0100 | [diff] [blame] | 638 | if (G.flags & FLAG_FSYNC) { |
| 639 | if (fsync(ofd) < 0) |
| 640 | goto die_outfile; |
Glenn L McGrath | f0b073f | 2000-09-11 00:32:13 +0000 | [diff] [blame] | 641 | } |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 642 | |
Denys Vlasenko | 9ab5a8d | 2018-10-30 17:04:52 +0100 | [diff] [blame] | 643 | #if ENABLE_FEATURE_DD_IBS_OBS |
| 644 | if (ocount != 0) { |
| 645 | if (write_and_stats(obuf, ocount, obs, outfile)) |
Denys Vlasenko | f3114a3 | 2013-08-20 12:40:29 +0200 | [diff] [blame] | 646 | goto out_status; |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 647 | } |
Denys Vlasenko | 9ab5a8d | 2018-10-30 17:04:52 +0100 | [diff] [blame] | 648 | #endif |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 649 | if (close(ifd) < 0) { |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 650 | die_infile: |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 651 | bb_simple_perror_msg_and_die(infile); |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 652 | } |
Matt Kraai | 3eeab3b | 2001-12-07 16:27:37 +0000 | [diff] [blame] | 653 | |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 654 | if (close(ofd) < 0) { |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 655 | die_outfile: |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 656 | bb_simple_perror_msg_and_die(outfile); |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 657 | } |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 658 | |
| 659 | exitcode = EXIT_SUCCESS; |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 660 | out_status: |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 661 | if (!ENABLE_FEATURE_DD_STATUS || !(G.flags & FLAG_STATUS_NONE)) |
| 662 | dd_output_status(0); |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 663 | |
Denys Vlasenko | 2aaacc1 | 2020-12-14 20:27:57 +0100 | [diff] [blame] | 664 | #if 0 /* can't just free(), they can be mmap()ed */ |
Alexey Soloviev | f922154 | 2011-03-16 10:39:46 +0300 | [diff] [blame] | 665 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 666 | free(obuf); |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 667 | if (G.flags & FLAG_TWOBUFS) |
Alexey Soloviev | f922154 | 2011-03-16 10:39:46 +0300 | [diff] [blame] | 668 | free(ibuf); |
| 669 | } |
Denys Vlasenko | 2aaacc1 | 2020-12-14 20:27:57 +0100 | [diff] [blame] | 670 | #endif |
Alexey Soloviev | f922154 | 2011-03-16 10:39:46 +0300 | [diff] [blame] | 671 | |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 672 | return exitcode; |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 673 | } |