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 | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 10 | //config: bool "dd (7.1 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 |
Nicholas Clark | 77a6678 | 2018-01-25 19:00:19 +0100 | [diff] [blame^] | 40 | //config: bool "Enable ibs, obs, iflag 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 |
| 59 | //usage: "[if=FILE] [of=FILE] " IF_FEATURE_DD_IBS_OBS("[ibs=N] [obs=N] ") "[bs=N] [count=N] [skip=N]\n" |
Nicholas Clark | 77a6678 | 2018-01-25 19:00:19 +0100 | [diff] [blame^] | 60 | //usage: " [seek=N]" IF_FEATURE_DD_IBS_OBS(" [conv=notrunc|noerror|sync|fsync] [iflag=skip_bytes|fullblock]") |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 61 | //usage:#define dd_full_usage "\n\n" |
| 62 | //usage: "Copy a file with converting and formatting\n" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 63 | //usage: "\n if=FILE Read from FILE instead of stdin" |
| 64 | //usage: "\n of=FILE Write to FILE instead of stdout" |
| 65 | //usage: "\n bs=N Read and write N bytes at a time" |
| 66 | //usage: IF_FEATURE_DD_IBS_OBS( |
| 67 | //usage: "\n ibs=N Read N bytes at a time" |
| 68 | //usage: ) |
| 69 | //usage: IF_FEATURE_DD_IBS_OBS( |
| 70 | //usage: "\n obs=N Write N bytes at a time" |
| 71 | //usage: ) |
| 72 | //usage: "\n count=N Copy only N input blocks" |
| 73 | //usage: "\n skip=N Skip N input blocks" |
| 74 | //usage: "\n seek=N Skip N output blocks" |
| 75 | //usage: IF_FEATURE_DD_IBS_OBS( |
| 76 | //usage: "\n conv=notrunc Don't truncate output file" |
| 77 | //usage: "\n conv=noerror Continue after read errors" |
| 78 | //usage: "\n conv=sync Pad blocks with zeros" |
| 79 | //usage: "\n conv=fsync Physically write data out before finishing" |
Denys Vlasenko | b941316 | 2013-08-19 09:01:39 +0200 | [diff] [blame] | 80 | //usage: "\n conv=swab Swap every pair of bytes" |
Rafał Miłecki | c30a5b1 | 2016-02-01 02:17:28 +0100 | [diff] [blame] | 81 | //usage: "\n iflag=skip_bytes skip=N is in bytes" |
Nicholas Clark | 77a6678 | 2018-01-25 19:00:19 +0100 | [diff] [blame^] | 82 | //usage: "\n iflag=fullblock Read full blocks" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 83 | //usage: ) |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 84 | //usage: IF_FEATURE_DD_STATUS( |
| 85 | //usage: "\n status=noxfer Suppress rate output" |
| 86 | //usage: "\n status=none Suppress all output" |
| 87 | //usage: ) |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 88 | //usage: "\n" |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 89 | //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] | 90 | //usage: |
| 91 | //usage:#define dd_example_usage |
| 92 | //usage: "$ dd if=/dev/zero of=/dev/ram1 bs=1M count=4\n" |
| 93 | //usage: "4+0 records in\n" |
| 94 | //usage: "4+0 records out\n" |
| 95 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 96 | #include "libbb.h" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 97 | #include "common_bufsiz.h" |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 98 | |
| 99 | /* This is a NOEXEC applet. Be very careful! */ |
| 100 | |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 101 | |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 102 | enum { |
| 103 | ifd = STDIN_FILENO, |
| 104 | ofd = STDOUT_FILENO, |
| 105 | }; |
| 106 | |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 107 | struct globals { |
| 108 | off_t out_full, out_part, in_full, in_part; |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 109 | #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE |
| 110 | unsigned long long total_bytes; |
| 111 | unsigned long long begin_time_us; |
| 112 | #endif |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 113 | int flags; |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 114 | } FIX_ALIASING; |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 115 | #define G (*(struct globals*)bb_common_bufsiz1) |
Denys Vlasenko | 1b34d4f | 2009-09-30 02:39:57 +0200 | [diff] [blame] | 116 | #define INIT_G() do { \ |
Denys Vlasenko | 47cfbf3 | 2016-04-21 18:18:48 +0200 | [diff] [blame] | 117 | setup_common_bufsiz(); \ |
Denys Vlasenko | 1b34d4f | 2009-09-30 02:39:57 +0200 | [diff] [blame] | 118 | /* we have to zero it out because of NOEXEC */ \ |
| 119 | memset(&G, 0, sizeof(G)); \ |
| 120 | } while (0) |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 121 | |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 122 | enum { |
| 123 | /* Must be in the same order as OP_conv_XXX! */ |
| 124 | /* (see "flags |= (1 << what)" below) */ |
| 125 | FLAG_NOTRUNC = (1 << 0) * ENABLE_FEATURE_DD_IBS_OBS, |
| 126 | FLAG_SYNC = (1 << 1) * ENABLE_FEATURE_DD_IBS_OBS, |
| 127 | FLAG_NOERROR = (1 << 2) * ENABLE_FEATURE_DD_IBS_OBS, |
| 128 | FLAG_FSYNC = (1 << 3) * ENABLE_FEATURE_DD_IBS_OBS, |
| 129 | FLAG_SWAB = (1 << 4) * ENABLE_FEATURE_DD_IBS_OBS, |
| 130 | /* end of conv flags */ |
Rafał Miłecki | c30a5b1 | 2016-02-01 02:17:28 +0100 | [diff] [blame] | 131 | /* start of input flags */ |
| 132 | FLAG_IFLAG_SHIFT = 5, |
| 133 | FLAG_SKIP_BYTES = (1 << 5) * ENABLE_FEATURE_DD_IBS_OBS, |
Nicholas Clark | 77a6678 | 2018-01-25 19:00:19 +0100 | [diff] [blame^] | 134 | FLAG_FULLBLOCK = (1 << 6) * ENABLE_FEATURE_DD_IBS_OBS, |
Rafał Miłecki | c30a5b1 | 2016-02-01 02:17:28 +0100 | [diff] [blame] | 135 | /* end of input flags */ |
Nicholas Clark | 77a6678 | 2018-01-25 19:00:19 +0100 | [diff] [blame^] | 136 | FLAG_TWOBUFS = (1 << 7) * ENABLE_FEATURE_DD_IBS_OBS, |
| 137 | FLAG_COUNT = 1 << 8, |
| 138 | FLAG_STATUS_NONE = 1 << 9, |
| 139 | FLAG_STATUS_NOXFER = 1 << 10, |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 140 | }; |
Rob Landley | c559817 | 2006-05-02 22:44:04 +0000 | [diff] [blame] | 141 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 142 | static void dd_output_status(int UNUSED_PARAM cur_signal) |
Rob Landley | c559817 | 2006-05-02 22:44:04 +0000 | [diff] [blame] | 143 | { |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 144 | #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE |
Denys Vlasenko | 7eabffa | 2009-10-14 17:43:41 +0200 | [diff] [blame] | 145 | double seconds; |
| 146 | unsigned long long bytes_sec; |
| 147 | unsigned long long now_us = monotonic_us(); /* before fprintf */ |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 148 | #endif |
| 149 | |
Denis Vlasenko | 980864d | 2007-07-30 10:58:09 +0000 | [diff] [blame] | 150 | /* Deliberately using %u, not %d */ |
| 151 | fprintf(stderr, "%"OFF_FMT"u+%"OFF_FMT"u records in\n" |
| 152 | "%"OFF_FMT"u+%"OFF_FMT"u records out\n", |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 153 | G.in_full, G.in_part, |
| 154 | G.out_full, G.out_part); |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 155 | |
| 156 | #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 157 | # if ENABLE_FEATURE_DD_STATUS |
| 158 | if (G.flags & FLAG_STATUS_NOXFER) /* status=noxfer active? */ |
| 159 | return; |
| 160 | //TODO: should status=none make dd stop reacting to USR1 entirely? |
| 161 | //So far we react to it (we print the stats), |
| 162 | //status=none only suppresses final, non-USR1 generated status message. |
| 163 | # endif |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 164 | fprintf(stderr, "%llu bytes (%sB) copied, ", |
| 165 | G.total_bytes, |
| 166 | /* show fractional digit, use suffixes */ |
| 167 | make_human_readable_str(G.total_bytes, 1, 0) |
| 168 | ); |
| 169 | /* Corner cases: |
| 170 | * ./busybox dd </dev/null >/dev/null |
| 171 | * ./busybox dd bs=1M count=2000 </dev/zero >/dev/null |
| 172 | * (echo DONE) | ./busybox dd >/dev/null |
| 173 | * (sleep 1; echo DONE) | ./busybox dd >/dev/null |
| 174 | */ |
Denys Vlasenko | 7eabffa | 2009-10-14 17:43:41 +0200 | [diff] [blame] | 175 | seconds = (now_us - G.begin_time_us) / 1000000.0; |
| 176 | bytes_sec = G.total_bytes / seconds; |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 177 | fprintf(stderr, "%f seconds, %sB/s\n", |
Denys Vlasenko | 7eabffa | 2009-10-14 17:43:41 +0200 | [diff] [blame] | 178 | seconds, |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 179 | /* show fractional digit, use suffixes */ |
Denys Vlasenko | 7eabffa | 2009-10-14 17:43:41 +0200 | [diff] [blame] | 180 | make_human_readable_str(bytes_sec, 1, 0) |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 181 | ); |
| 182 | #endif |
Rob Landley | c559817 | 2006-05-02 22:44:04 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 185 | static ssize_t full_write_or_warn(const void *buf, size_t len, |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 186 | const char *const filename) |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 187 | { |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 188 | ssize_t n = full_write(ofd, buf, len); |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 189 | if (n < 0) |
| 190 | bb_perror_msg("writing '%s'", filename); |
| 191 | return n; |
| 192 | } |
| 193 | |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 194 | 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] | 195 | const char *filename) |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 196 | { |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 197 | ssize_t n = full_write_or_warn(buf, len, filename); |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 198 | if (n < 0) |
| 199 | return 1; |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 200 | #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE |
| 201 | G.total_bytes += n; |
| 202 | #endif |
Denys Vlasenko | 5afd63a | 2018-01-10 10:58:55 +0100 | [diff] [blame] | 203 | if ((size_t)n == obs) { |
| 204 | G.out_full++; |
| 205 | return 0; |
| 206 | } |
Denys Vlasenko | 2c87677 | 2018-01-10 11:04:09 +0100 | [diff] [blame] | 207 | if ((size_t)n == len) { |
Denys Vlasenko | 5afd63a | 2018-01-10 10:58:55 +0100 | [diff] [blame] | 208 | G.out_part++; |
Denys Vlasenko | 2c87677 | 2018-01-10 11:04:09 +0100 | [diff] [blame] | 209 | return 0; |
| 210 | } |
Denys Vlasenko | 5afd63a | 2018-01-10 10:58:55 +0100 | [diff] [blame] | 211 | return 1; |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Denis Vlasenko | 5dd27b1 | 2006-11-25 14:46:21 +0000 | [diff] [blame] | 214 | #if ENABLE_LFS |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 215 | # define XATOU_SFX xatoull_sfx |
Denis Vlasenko | 5dd27b1 | 2006-11-25 14:46:21 +0000 | [diff] [blame] | 216 | #else |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 217 | # define XATOU_SFX xatoul_sfx |
Denis Vlasenko | 5dd27b1 | 2006-11-25 14:46:21 +0000 | [diff] [blame] | 218 | #endif |
| 219 | |
Rafał Miłecki | c30a5b1 | 2016-02-01 02:17:28 +0100 | [diff] [blame] | 220 | #if ENABLE_FEATURE_DD_IBS_OBS |
| 221 | static int parse_comma_flags(char *val, const char *words, const char *error_in) |
| 222 | { |
| 223 | int flags = 0; |
| 224 | while (1) { |
| 225 | int n; |
| 226 | char *arg; |
| 227 | /* find ',', replace them with NUL so we can use val for |
| 228 | * index_in_strings() without copying. |
| 229 | * We rely on val being non-null, else strchr would fault. |
| 230 | */ |
| 231 | arg = strchr(val, ','); |
| 232 | if (arg) |
| 233 | *arg = '\0'; |
| 234 | n = index_in_strings(words, val); |
| 235 | if (n < 0) |
| 236 | bb_error_msg_and_die(bb_msg_invalid_arg_to, val, error_in); |
| 237 | flags |= (1 << n); |
| 238 | if (!arg) /* no ',' left, so this was the last specifier */ |
| 239 | break; |
| 240 | *arg = ','; /* to preserve ps listing */ |
| 241 | val = arg + 1; /* skip this keyword and ',' */ |
| 242 | } |
| 243 | return flags; |
| 244 | } |
| 245 | #endif |
| 246 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 247 | int dd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 248 | int dd_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 249 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 250 | static const char keywords[] ALIGN1 = |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 251 | "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] | 252 | #if ENABLE_FEATURE_DD_IBS_OBS |
Rafał Miłecki | c30a5b1 | 2016-02-01 02:17:28 +0100 | [diff] [blame] | 253 | "ibs\0""obs\0""conv\0""iflag\0" |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 254 | #endif |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 255 | ; |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 256 | #if ENABLE_FEATURE_DD_IBS_OBS |
| 257 | static const char conv_words[] ALIGN1 = |
Denys Vlasenko | b941316 | 2013-08-19 09:01:39 +0200 | [diff] [blame] | 258 | "notrunc\0""sync\0""noerror\0""fsync\0""swab\0"; |
Rafał Miłecki | c30a5b1 | 2016-02-01 02:17:28 +0100 | [diff] [blame] | 259 | static const char iflag_words[] ALIGN1 = |
Nicholas Clark | 77a6678 | 2018-01-25 19:00:19 +0100 | [diff] [blame^] | 260 | "skip_bytes\0""fullblock\0"; |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 261 | #endif |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 262 | #if ENABLE_FEATURE_DD_STATUS |
| 263 | static const char status_words[] ALIGN1 = |
| 264 | "none\0""noxfer\0"; |
| 265 | #endif |
Bernhard Reutner-Fischer | 99003b8 | 2007-04-10 19:30:50 +0000 | [diff] [blame] | 266 | enum { |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 267 | OP_bs = 0, |
Bernhard Reutner-Fischer | 99003b8 | 2007-04-10 19:30:50 +0000 | [diff] [blame] | 268 | OP_count, |
| 269 | OP_seek, |
| 270 | OP_skip, |
| 271 | OP_if, |
| 272 | OP_of, |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 273 | IF_FEATURE_DD_STATUS(OP_status,) |
Bernhard Reutner-Fischer | e468ef2 | 2007-04-16 12:21:05 +0000 | [diff] [blame] | 274 | #if ENABLE_FEATURE_DD_IBS_OBS |
Bernhard Reutner-Fischer | 99003b8 | 2007-04-10 19:30:50 +0000 | [diff] [blame] | 275 | OP_ibs, |
| 276 | OP_obs, |
| 277 | OP_conv, |
Rafał Miłecki | c30a5b1 | 2016-02-01 02:17:28 +0100 | [diff] [blame] | 278 | OP_iflag, |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 279 | /* Must be in the same order as FLAG_XXX! */ |
| 280 | OP_conv_notrunc = 0, |
Bernhard Reutner-Fischer | 99003b8 | 2007-04-10 19:30:50 +0000 | [diff] [blame] | 281 | OP_conv_sync, |
Denis Vlasenko | f6250a3 | 2007-04-19 20:16:14 +0000 | [diff] [blame] | 282 | OP_conv_noerror, |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 283 | OP_conv_fsync, |
Denys Vlasenko | b941316 | 2013-08-19 09:01:39 +0200 | [diff] [blame] | 284 | OP_conv_swab, |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 285 | /* Unimplemented conv=XXX: */ |
| 286 | //nocreat do not create the output file |
| 287 | //excl fail if the output file already exists |
| 288 | //fdatasync physically write output file data before finishing |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 289 | //lcase change upper case to lower case |
| 290 | //ucase change lower case to upper case |
| 291 | //block pad newline-terminated records with spaces to cbs-size |
| 292 | //unblock replace trailing spaces in cbs-size records with newline |
| 293 | //ascii from EBCDIC to ASCII |
| 294 | //ebcdic from ASCII to EBCDIC |
| 295 | //ibm from ASCII to alternate EBCDIC |
Denys Vlasenko | b941316 | 2013-08-19 09:01:39 +0200 | [diff] [blame] | 296 | /* Partially implemented: */ |
| 297 | //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] | 298 | OP_iflag_skip_bytes, |
Nicholas Clark | 77a6678 | 2018-01-25 19:00:19 +0100 | [diff] [blame^] | 299 | OP_iflag_fullblock, |
Bernhard Reutner-Fischer | e468ef2 | 2007-04-16 12:21:05 +0000 | [diff] [blame] | 300 | #endif |
Bernhard Reutner-Fischer | 99003b8 | 2007-04-10 19:30:50 +0000 | [diff] [blame] | 301 | }; |
Denys Vlasenko | 5b9910f | 2013-08-20 02:50:49 +0200 | [diff] [blame] | 302 | smallint exitcode = EXIT_FAILURE; |
Denys Vlasenko | 4b1896c | 2013-08-19 09:00:08 +0200 | [diff] [blame] | 303 | int i; |
Denys Vlasenko | 4502bb1 | 2013-08-20 12:08:46 +0200 | [diff] [blame] | 304 | size_t ibs = 512; |
| 305 | char *ibuf; |
| 306 | #if ENABLE_FEATURE_DD_IBS_OBS |
| 307 | size_t obs = 512; |
| 308 | char *obuf; |
| 309 | #else |
| 310 | # define obs ibs |
| 311 | # define obuf ibuf |
| 312 | #endif |
| 313 | /* These are all zeroed at once! */ |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 314 | struct { |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 315 | size_t oc; |
Denys Vlasenko | 5b9910f | 2013-08-20 02:50:49 +0200 | [diff] [blame] | 316 | ssize_t prev_read_size; /* for detecting swab failure */ |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 317 | off_t count; |
| 318 | off_t seek, skip; |
| 319 | const char *infile, *outfile; |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 320 | } Z; |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 321 | #define oc (Z.oc ) |
Denys Vlasenko | 5b9910f | 2013-08-20 02:50:49 +0200 | [diff] [blame] | 322 | #define prev_read_size (Z.prev_read_size) |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 323 | #define count (Z.count ) |
| 324 | #define seek (Z.seek ) |
| 325 | #define skip (Z.skip ) |
| 326 | #define infile (Z.infile ) |
| 327 | #define outfile (Z.outfile) |
Eric Andersen | ddea368 | 2000-11-29 22:33:02 +0000 | [diff] [blame] | 328 | |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 329 | memset(&Z, 0, sizeof(Z)); |
| 330 | INIT_G(); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 331 | //fflush_all(); - is this needed because of NOEXEC? |
Denis Vlasenko | f6250a3 | 2007-04-19 20:16:14 +0000 | [diff] [blame] | 332 | |
Denys Vlasenko | 4b1896c | 2013-08-19 09:00:08 +0200 | [diff] [blame] | 333 | for (i = 1; argv[i]; i++) { |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 334 | int what; |
| 335 | char *val; |
Denys Vlasenko | 4b1896c | 2013-08-19 09:00:08 +0200 | [diff] [blame] | 336 | char *arg = argv[i]; |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 337 | |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 338 | #if ENABLE_DESKTOP |
| 339 | /* "dd --". NB: coreutils 6.9 will complain if they see |
| 340 | * more than one of them. We wouldn't. */ |
| 341 | if (arg[0] == '-' && arg[1] == '-' && arg[2] == '\0') |
| 342 | continue; |
| 343 | #endif |
| 344 | val = strchr(arg, '='); |
| 345 | if (val == NULL) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 346 | bb_show_usage(); |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 347 | *val = '\0'; |
| 348 | what = index_in_strings(keywords, arg); |
| 349 | if (what < 0) |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 350 | bb_show_usage(); |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 351 | /* *val = '='; - to preserve ps listing? */ |
| 352 | val++; |
Bernhard Reutner-Fischer | e468ef2 | 2007-04-16 12:21:05 +0000 | [diff] [blame] | 353 | #if ENABLE_FEATURE_DD_IBS_OBS |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 354 | if (what == OP_ibs) { |
| 355 | /* Must fit into positive ssize_t */ |
Ari Sundholm | 8893023 | 2015-03-04 18:46:48 +0200 | [diff] [blame] | 356 | ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, cwbkMG_suffixes); |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 357 | /*continue;*/ |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 358 | } |
| 359 | if (what == OP_obs) { |
Ari Sundholm | 8893023 | 2015-03-04 18:46:48 +0200 | [diff] [blame] | 360 | obs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, cwbkMG_suffixes); |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 361 | /*continue;*/ |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 362 | } |
| 363 | if (what == OP_conv) { |
Rafał Miłecki | c30a5b1 | 2016-02-01 02:17:28 +0100 | [diff] [blame] | 364 | G.flags |= parse_comma_flags(val, conv_words, "conv"); |
| 365 | /*continue;*/ |
| 366 | } |
| 367 | if (what == OP_iflag) { |
| 368 | G.flags |= parse_comma_flags(val, iflag_words, "iflag") << FLAG_IFLAG_SHIFT; |
Denys Vlasenko | 4502bb1 | 2013-08-20 12:08:46 +0200 | [diff] [blame] | 369 | /*continue;*/ |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 370 | } |
Bernhard Reutner-Fischer | e468ef2 | 2007-04-16 12:21:05 +0000 | [diff] [blame] | 371 | #endif |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 372 | if (what == OP_bs) { |
Ari Sundholm | 8893023 | 2015-03-04 18:46:48 +0200 | [diff] [blame] | 373 | ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, cwbkMG_suffixes); |
Denys Vlasenko | 4502bb1 | 2013-08-20 12:08:46 +0200 | [diff] [blame] | 374 | obs = ibs; |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 375 | /*continue;*/ |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 376 | } |
| 377 | /* These can be large: */ |
| 378 | if (what == OP_count) { |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 379 | G.flags |= FLAG_COUNT; |
Ari Sundholm | 8893023 | 2015-03-04 18:46:48 +0200 | [diff] [blame] | 380 | count = XATOU_SFX(val, cwbkMG_suffixes); |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 381 | /*continue;*/ |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 382 | } |
| 383 | if (what == OP_seek) { |
Ari Sundholm | 8893023 | 2015-03-04 18:46:48 +0200 | [diff] [blame] | 384 | seek = XATOU_SFX(val, cwbkMG_suffixes); |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 385 | /*continue;*/ |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 386 | } |
Denis Vlasenko | d1801a4 | 2007-04-19 20:08:19 +0000 | [diff] [blame] | 387 | if (what == OP_skip) { |
Ari Sundholm | 8893023 | 2015-03-04 18:46:48 +0200 | [diff] [blame] | 388 | skip = XATOU_SFX(val, cwbkMG_suffixes); |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 389 | /*continue;*/ |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 390 | } |
| 391 | if (what == OP_if) { |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 392 | infile = val; |
| 393 | /*continue;*/ |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 394 | } |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 395 | if (what == OP_of) { |
| 396 | outfile = val; |
| 397 | /*continue;*/ |
| 398 | } |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 399 | #if ENABLE_FEATURE_DD_STATUS |
| 400 | if (what == OP_status) { |
| 401 | int n; |
| 402 | n = index_in_strings(status_words, val); |
| 403 | if (n < 0) |
Denys Vlasenko | 0f296a3 | 2015-10-14 13:21:01 +0200 | [diff] [blame] | 404 | bb_error_msg_and_die(bb_msg_invalid_arg_to, val, "status"); |
Denys Vlasenko | d5b98e2 | 2017-07-14 13:44:30 +0200 | [diff] [blame] | 405 | G.flags |= FLAG_STATUS_NONE << n; |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 406 | /*continue;*/ |
| 407 | } |
| 408 | #endif |
Denys Vlasenko | 4b1896c | 2013-08-19 09:00:08 +0200 | [diff] [blame] | 409 | } /* end of "for (argv[i])" */ |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 410 | |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 411 | //XXX:FIXME for huge ibs or obs, malloc'ing them isn't the brightest idea ever |
Denys Vlasenko | 4502bb1 | 2013-08-20 12:08:46 +0200 | [diff] [blame] | 412 | ibuf = xmalloc(ibs); |
| 413 | obuf = ibuf; |
| 414 | #if ENABLE_FEATURE_DD_IBS_OBS |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 415 | if (ibs != obs) { |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 416 | G.flags |= FLAG_TWOBUFS; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 417 | obuf = xmalloc(obs); |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 418 | } |
Denys Vlasenko | 4502bb1 | 2013-08-20 12:08:46 +0200 | [diff] [blame] | 419 | #endif |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 420 | |
| 421 | #if ENABLE_FEATURE_DD_SIGNAL_HANDLING |
| 422 | signal_SA_RESTART_empty_mask(SIGUSR1, dd_output_status); |
| 423 | #endif |
| 424 | #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE |
| 425 | G.begin_time_us = monotonic_us(); |
| 426 | #endif |
| 427 | |
Denys Vlasenko | 4502bb1 | 2013-08-20 12:08:46 +0200 | [diff] [blame] | 428 | if (infile) { |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 429 | xmove_fd(xopen(infile, O_RDONLY), ifd); |
Denys Vlasenko | 4502bb1 | 2013-08-20 12:08:46 +0200 | [diff] [blame] | 430 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 431 | infile = bb_msg_standard_input; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 432 | } |
Denys Vlasenko | 4502bb1 | 2013-08-20 12:08:46 +0200 | [diff] [blame] | 433 | if (outfile) { |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 434 | int oflag = O_WRONLY | O_CREAT; |
Matt Kraai | c9acf8c | 2001-01-17 00:21:05 +0000 | [diff] [blame] | 435 | |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 436 | if (!seek && !(G.flags & FLAG_NOTRUNC)) |
Matt Kraai | c9acf8c | 2001-01-17 00:21:05 +0000 | [diff] [blame] | 437 | oflag |= O_TRUNC; |
| 438 | |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 439 | xmove_fd(xopen(outfile, oflag), ofd); |
Matt Kraai | c9acf8c | 2001-01-17 00:21:05 +0000 | [diff] [blame] | 440 | |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 441 | if (seek && !(G.flags & FLAG_NOTRUNC)) { |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 442 | if (ftruncate(ofd, seek * obs) < 0) { |
Matt Kraai | eb83478 | 2002-02-05 15:28:54 +0000 | [diff] [blame] | 443 | struct stat st; |
| 444 | |
Denys Vlasenko | 6b9f163 | 2010-01-28 02:24:24 +0100 | [diff] [blame] | 445 | if (fstat(ofd, &st) < 0 |
| 446 | || S_ISREG(st.st_mode) |
| 447 | || S_ISDIR(st.st_mode) |
| 448 | ) { |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 449 | goto die_outfile; |
Denys Vlasenko | 6b9f163 | 2010-01-28 02:24:24 +0100 | [diff] [blame] | 450 | } |
Matt Kraai | eb83478 | 2002-02-05 15:28:54 +0000 | [diff] [blame] | 451 | } |
Matt Kraai | c9acf8c | 2001-01-17 00:21:05 +0000 | [diff] [blame] | 452 | } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 453 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 454 | outfile = bb_msg_standard_output; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 455 | } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 456 | if (skip) { |
Rafał Miłecki | c30a5b1 | 2016-02-01 02:17:28 +0100 | [diff] [blame] | 457 | size_t blocksz = (G.flags & FLAG_SKIP_BYTES) ? 1 : ibs; |
| 458 | if (lseek(ifd, skip * blocksz, SEEK_CUR) < 0) { |
Denys Vlasenko | 1b57fe1 | 2013-08-20 12:45:05 +0200 | [diff] [blame] | 459 | do { |
Nicholas Clark | 77a6678 | 2018-01-25 19:00:19 +0100 | [diff] [blame^] | 460 | ssize_t n; |
| 461 | #if ENABLE_FEATURE_DD_IBS_OBS |
| 462 | if (G.flags & FLAG_FULLBLOCK) |
| 463 | n = full_read(ifd, ibuf, blocksz); |
| 464 | else |
| 465 | #endif |
| 466 | n = safe_read(ifd, ibuf, blocksz); |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 467 | if (n < 0) |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 468 | goto die_infile; |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 469 | if (n == 0) |
| 470 | break; |
Denys Vlasenko | 1b57fe1 | 2013-08-20 12:45:05 +0200 | [diff] [blame] | 471 | } while (--skip != 0); |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 472 | } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 473 | } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 474 | if (seek) { |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 475 | if (lseek(ofd, seek * obs, SEEK_CUR) < 0) |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 476 | goto die_outfile; |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 477 | } |
Eric Andersen | ddea368 | 2000-11-29 22:33:02 +0000 | [diff] [blame] | 478 | |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 479 | while (!(G.flags & FLAG_COUNT) || (G.in_full + G.in_part != count)) { |
Denys Vlasenko | 4b1896c | 2013-08-19 09:00:08 +0200 | [diff] [blame] | 480 | ssize_t n; |
Nicholas Clark | 77a6678 | 2018-01-25 19:00:19 +0100 | [diff] [blame^] | 481 | #if ENABLE_FEATURE_DD_IBS_OBS |
| 482 | if (G.flags & FLAG_FULLBLOCK) |
| 483 | n = full_read(ifd, ibuf, ibs); |
| 484 | else |
| 485 | #endif |
| 486 | n = safe_read(ifd, ibuf, ibs); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 487 | if (n == 0) |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 488 | break; |
Eric Andersen | ef38b39 | 2002-04-27 01:31:43 +0000 | [diff] [blame] | 489 | if (n < 0) { |
Denys Vlasenko | f00cfdf | 2009-09-18 20:49:36 +0200 | [diff] [blame] | 490 | /* "Bad block" */ |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 491 | if (!(G.flags & FLAG_NOERROR)) |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 492 | goto die_infile; |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 493 | bb_simple_perror_msg(infile); |
Denys Vlasenko | f00cfdf | 2009-09-18 20:49:36 +0200 | [diff] [blame] | 494 | /* GNU dd with conv=noerror skips over bad blocks */ |
Denys Vlasenko | 334fcc8 | 2009-08-05 23:16:27 +0200 | [diff] [blame] | 495 | xlseek(ifd, ibs, SEEK_CUR); |
Denys Vlasenko | f00cfdf | 2009-09-18 20:49:36 +0200 | [diff] [blame] | 496 | /* conv=noerror,sync writes NULs, |
| 497 | * conv=noerror just ignores input bad blocks */ |
| 498 | n = 0; |
Eric Andersen | ef38b39 | 2002-04-27 01:31:43 +0000 | [diff] [blame] | 499 | } |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 500 | if (G.flags & FLAG_SWAB) { |
Denys Vlasenko | 0ff0b32 | 2013-08-20 12:49:28 +0200 | [diff] [blame] | 501 | uint16_t *p16; |
| 502 | ssize_t n2; |
Denys Vlasenko | 5b9910f | 2013-08-20 02:50:49 +0200 | [diff] [blame] | 503 | |
| 504 | /* Our code allows only last read to be odd-sized */ |
| 505 | if (prev_read_size & 1) |
| 506 | bb_error_msg_and_die("can't swab %lu byte buffer", |
| 507 | (unsigned long)prev_read_size); |
| 508 | prev_read_size = n; |
| 509 | |
Denys Vlasenko | 90e9cfd | 2013-08-20 12:27:19 +0200 | [diff] [blame] | 510 | /* If n is odd, last byte is not swapped: |
Denys Vlasenko | 8395bd3 | 2013-08-19 10:30:55 +0200 | [diff] [blame] | 511 | * echo -n "qwe" | dd conv=swab |
Denys Vlasenko | b941316 | 2013-08-19 09:01:39 +0200 | [diff] [blame] | 512 | * prints "wqe". |
Denys Vlasenko | b941316 | 2013-08-19 09:01:39 +0200 | [diff] [blame] | 513 | */ |
Denys Vlasenko | 5b9910f | 2013-08-20 02:50:49 +0200 | [diff] [blame] | 514 | p16 = (void*) ibuf; |
Denys Vlasenko | 0ff0b32 | 2013-08-20 12:49:28 +0200 | [diff] [blame] | 515 | n2 = (n >> 1); |
| 516 | while (--n2 >= 0) { |
Denys Vlasenko | b941316 | 2013-08-19 09:01:39 +0200 | [diff] [blame] | 517 | *p16 = bswap_16(*p16); |
| 518 | p16++; |
| 519 | } |
| 520 | } |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 521 | if ((size_t)n == ibs) |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 522 | G.in_full++; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 523 | else { |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 524 | G.in_part++; |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 525 | if (G.flags & FLAG_SYNC) { |
Denys Vlasenko | f00cfdf | 2009-09-18 20:49:36 +0200 | [diff] [blame] | 526 | memset(ibuf + n, 0, ibs - n); |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 527 | n = ibs; |
| 528 | } |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 529 | } |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 530 | if (G.flags & FLAG_TWOBUFS) { |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 531 | char *tmp = ibuf; |
| 532 | while (n) { |
| 533 | size_t d = obs - oc; |
| 534 | |
Denis Vlasenko | 77ad97f | 2008-05-13 02:27:31 +0000 | [diff] [blame] | 535 | if (d > (size_t)n) |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 536 | d = n; |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 537 | memcpy(obuf + oc, tmp, d); |
| 538 | n -= d; |
| 539 | tmp += d; |
| 540 | oc += d; |
| 541 | if (oc == obs) { |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 542 | if (write_and_stats(obuf, obs, obs, outfile)) |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 543 | goto out_status; |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 544 | oc = 0; |
| 545 | } |
| 546 | } |
Denys Vlasenko | f3114a3 | 2013-08-20 12:40:29 +0200 | [diff] [blame] | 547 | } else { |
| 548 | if (write_and_stats(ibuf, n, obs, outfile)) |
| 549 | goto out_status; |
| 550 | } |
Rostislav Skudnov | dba0dc1 | 2017-03-16 20:54:35 +0100 | [diff] [blame] | 551 | } |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 552 | |
Rostislav Skudnov | dba0dc1 | 2017-03-16 20:54:35 +0100 | [diff] [blame] | 553 | if (G.flags & FLAG_FSYNC) { |
| 554 | if (fsync(ofd) < 0) |
| 555 | goto die_outfile; |
Glenn L McGrath | f0b073f | 2000-09-11 00:32:13 +0000 | [diff] [blame] | 556 | } |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 557 | |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 558 | if (ENABLE_FEATURE_DD_IBS_OBS && oc) { |
Denys Vlasenko | f3114a3 | 2013-08-20 12:40:29 +0200 | [diff] [blame] | 559 | if (write_and_stats(obuf, oc, obs, outfile)) |
| 560 | goto out_status; |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 561 | } |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 562 | if (close(ifd) < 0) { |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 563 | die_infile: |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 564 | bb_simple_perror_msg_and_die(infile); |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 565 | } |
Matt Kraai | 3eeab3b | 2001-12-07 16:27:37 +0000 | [diff] [blame] | 566 | |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 567 | if (close(ofd) < 0) { |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 568 | die_outfile: |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 569 | bb_simple_perror_msg_and_die(outfile); |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 570 | } |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 571 | |
| 572 | exitcode = EXIT_SUCCESS; |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 573 | out_status: |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 574 | if (!ENABLE_FEATURE_DD_STATUS || !(G.flags & FLAG_STATUS_NONE)) |
| 575 | dd_output_status(0); |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 576 | |
Alexey Soloviev | f922154 | 2011-03-16 10:39:46 +0300 | [diff] [blame] | 577 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 578 | free(obuf); |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 579 | if (G.flags & FLAG_TWOBUFS) |
Alexey Soloviev | f922154 | 2011-03-16 10:39:46 +0300 | [diff] [blame] | 580 | free(ibuf); |
| 581 | } |
| 582 | |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 583 | return exitcode; |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 584 | } |