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 | * |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 5 | * |
Matt Kraai | 3eeab3b | 2001-12-07 16:27:37 +0000 | [diff] [blame] | 6 | * Copyright (C) 2000,2001 Matt Kraai |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 7 | * |
Bernhard Reutner-Fischer | c2cb0f3 | 2006-04-13 12:45:04 +0000 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 11 | #include "libbb.h" |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 12 | |
| 13 | /* This is a NOEXEC applet. Be very careful! */ |
| 14 | |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 15 | |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 16 | enum { |
| 17 | ifd = STDIN_FILENO, |
| 18 | ofd = STDOUT_FILENO, |
| 19 | }; |
| 20 | |
Matt Kraai | a164c64 | 2001-02-05 17:50:03 +0000 | [diff] [blame] | 21 | static const struct suffix_mult dd_suffixes[] = { |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 22 | { "c", 1 }, |
| 23 | { "w", 2 }, |
| 24 | { "b", 512 }, |
| 25 | { "kD", 1000 }, |
| 26 | { "k", 1024 }, |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 27 | { "K", 1024 }, /* compat with coreutils dd */ |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 28 | { "MD", 1000000 }, |
| 29 | { "M", 1048576 }, |
| 30 | { "GD", 1000000000 }, |
| 31 | { "G", 1073741824 }, |
Denys Vlasenko | 043b1e5 | 2009-09-06 12:47:55 +0200 | [diff] [blame] | 32 | { "", 0 } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 33 | }; |
| 34 | |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 35 | struct globals { |
| 36 | off_t out_full, out_part, in_full, in_part; |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 37 | #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE |
| 38 | unsigned long long total_bytes; |
| 39 | unsigned long long begin_time_us; |
| 40 | #endif |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 41 | }; |
| 42 | #define G (*(struct globals*)&bb_common_bufsiz1) |
Denys Vlasenko | 1b34d4f | 2009-09-30 02:39:57 +0200 | [diff] [blame] | 43 | #define INIT_G() do { \ |
| 44 | /* we have to zero it out because of NOEXEC */ \ |
| 45 | memset(&G, 0, sizeof(G)); \ |
| 46 | } while (0) |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 47 | |
Rob Landley | c559817 | 2006-05-02 22:44:04 +0000 | [diff] [blame] | 48 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 49 | static void dd_output_status(int UNUSED_PARAM cur_signal) |
Rob Landley | c559817 | 2006-05-02 22:44:04 +0000 | [diff] [blame] | 50 | { |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 51 | #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE |
| 52 | unsigned long long total; |
| 53 | unsigned long long diff_scaled; |
| 54 | unsigned long long diff_us = monotonic_us(); /* before fprintf */ |
| 55 | #endif |
| 56 | |
Denis Vlasenko | 980864d | 2007-07-30 10:58:09 +0000 | [diff] [blame] | 57 | /* Deliberately using %u, not %d */ |
| 58 | fprintf(stderr, "%"OFF_FMT"u+%"OFF_FMT"u records in\n" |
| 59 | "%"OFF_FMT"u+%"OFF_FMT"u records out\n", |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 60 | G.in_full, G.in_part, |
| 61 | G.out_full, G.out_part); |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 62 | |
| 63 | #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE |
| 64 | fprintf(stderr, "%llu bytes (%sB) copied, ", |
| 65 | G.total_bytes, |
| 66 | /* show fractional digit, use suffixes */ |
| 67 | make_human_readable_str(G.total_bytes, 1, 0) |
| 68 | ); |
| 69 | /* Corner cases: |
| 70 | * ./busybox dd </dev/null >/dev/null |
| 71 | * ./busybox dd bs=1M count=2000 </dev/zero >/dev/null |
| 72 | * (echo DONE) | ./busybox dd >/dev/null |
| 73 | * (sleep 1; echo DONE) | ./busybox dd >/dev/null |
| 74 | */ |
| 75 | diff_us -= G.begin_time_us; |
Denys Vlasenko | a1a3972 | 2009-10-14 00:49:06 +0200 | [diff] [blame] | 76 | /* We need to calculate "(total * 1000000) / usec" without overflow. |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 77 | * this would work too, but is bigger than integer code below. |
Denys Vlasenko | a1a3972 | 2009-10-14 00:49:06 +0200 | [diff] [blame] | 78 | * total = G.total_bytes * (double)1000000 / (diff_us ? diff_us : 1); |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 79 | */ |
| 80 | diff_scaled = diff_us; |
| 81 | total = G.total_bytes; |
| 82 | while (total > MAXINT(unsigned long long) / (1024 * 1024)) { |
| 83 | total >>= 1; |
| 84 | diff_scaled >>= 1; |
| 85 | } |
Denys Vlasenko | a1a3972 | 2009-10-14 00:49:06 +0200 | [diff] [blame] | 86 | total *= (1024 * 1024); /* should be 1000000, but it's +45 bytes */ |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 87 | if (diff_scaled > 1) |
| 88 | total /= diff_scaled; |
| 89 | fprintf(stderr, "%f seconds, %sB/s\n", |
| 90 | diff_us / 1000000.0, |
| 91 | /* show fractional digit, use suffixes */ |
| 92 | make_human_readable_str(total, 1, 0) |
| 93 | ); |
| 94 | #endif |
Rob Landley | c559817 | 2006-05-02 22:44:04 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 97 | 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] | 98 | const char *const filename) |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 99 | { |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 100 | ssize_t n = full_write(ofd, buf, len); |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 101 | if (n < 0) |
| 102 | bb_perror_msg("writing '%s'", filename); |
| 103 | return n; |
| 104 | } |
| 105 | |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 106 | 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] | 107 | const char *filename) |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 108 | { |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 109 | ssize_t n = full_write_or_warn(buf, len, filename); |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 110 | if (n < 0) |
| 111 | return 1; |
Denis Vlasenko | 77ad97f | 2008-05-13 02:27:31 +0000 | [diff] [blame] | 112 | if ((size_t)n == obs) |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 113 | G.out_full++; |
Denis Vlasenko | d1801a4 | 2007-04-19 20:08:19 +0000 | [diff] [blame] | 114 | else if (n) /* > 0 */ |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 115 | G.out_part++; |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 116 | #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE |
| 117 | G.total_bytes += n; |
| 118 | #endif |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 119 | return 0; |
| 120 | } |
| 121 | |
Denis Vlasenko | 5dd27b1 | 2006-11-25 14:46:21 +0000 | [diff] [blame] | 122 | #if ENABLE_LFS |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 123 | # define XATOU_SFX xatoull_sfx |
Denis Vlasenko | 5dd27b1 | 2006-11-25 14:46:21 +0000 | [diff] [blame] | 124 | #else |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 125 | # define XATOU_SFX xatoul_sfx |
Denis Vlasenko | 5dd27b1 | 2006-11-25 14:46:21 +0000 | [diff] [blame] | 126 | #endif |
| 127 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 128 | int dd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 129 | int dd_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 130 | { |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 131 | enum { |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 132 | /* Must be in the same order as OP_conv_XXX! */ |
| 133 | /* (see "flags |= (1 << what)" below) */ |
| 134 | FLAG_NOTRUNC = 1 << 0, |
| 135 | FLAG_SYNC = 1 << 1, |
| 136 | FLAG_NOERROR = 1 << 2, |
| 137 | FLAG_FSYNC = 1 << 3, |
| 138 | /* end of conv flags */ |
| 139 | FLAG_TWOBUFS = 1 << 4, |
| 140 | FLAG_COUNT = 1 << 5, |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 141 | }; |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 142 | static const char keywords[] ALIGN1 = |
Denis Vlasenko | 9d4a17e | 2008-04-23 05:44:59 +0000 | [diff] [blame] | 143 | "bs\0""count\0""seek\0""skip\0""if\0""of\0" |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 144 | #if ENABLE_FEATURE_DD_IBS_OBS |
Denis Vlasenko | 9d4a17e | 2008-04-23 05:44:59 +0000 | [diff] [blame] | 145 | "ibs\0""obs\0""conv\0" |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 146 | #endif |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 147 | ; |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 148 | #if ENABLE_FEATURE_DD_IBS_OBS |
| 149 | static const char conv_words[] ALIGN1 = |
| 150 | "notrunc\0""sync\0""noerror\0""fsync\0"; |
| 151 | #endif |
Bernhard Reutner-Fischer | 99003b8 | 2007-04-10 19:30:50 +0000 | [diff] [blame] | 152 | enum { |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 153 | OP_bs = 0, |
Bernhard Reutner-Fischer | 99003b8 | 2007-04-10 19:30:50 +0000 | [diff] [blame] | 154 | OP_count, |
| 155 | OP_seek, |
| 156 | OP_skip, |
| 157 | OP_if, |
| 158 | OP_of, |
Bernhard Reutner-Fischer | e468ef2 | 2007-04-16 12:21:05 +0000 | [diff] [blame] | 159 | #if ENABLE_FEATURE_DD_IBS_OBS |
Bernhard Reutner-Fischer | 99003b8 | 2007-04-10 19:30:50 +0000 | [diff] [blame] | 160 | OP_ibs, |
| 161 | OP_obs, |
| 162 | OP_conv, |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 163 | /* Must be in the same order as FLAG_XXX! */ |
| 164 | OP_conv_notrunc = 0, |
Bernhard Reutner-Fischer | 99003b8 | 2007-04-10 19:30:50 +0000 | [diff] [blame] | 165 | OP_conv_sync, |
Denis Vlasenko | f6250a3 | 2007-04-19 20:16:14 +0000 | [diff] [blame] | 166 | OP_conv_noerror, |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 167 | OP_conv_fsync, |
| 168 | /* Unimplemented conv=XXX: */ |
| 169 | //nocreat do not create the output file |
| 170 | //excl fail if the output file already exists |
| 171 | //fdatasync physically write output file data before finishing |
| 172 | //swab swap every pair of input bytes |
| 173 | //lcase change upper case to lower case |
| 174 | //ucase change lower case to upper case |
| 175 | //block pad newline-terminated records with spaces to cbs-size |
| 176 | //unblock replace trailing spaces in cbs-size records with newline |
| 177 | //ascii from EBCDIC to ASCII |
| 178 | //ebcdic from ASCII to EBCDIC |
| 179 | //ibm from ASCII to alternate EBCDIC |
Bernhard Reutner-Fischer | e468ef2 | 2007-04-16 12:21:05 +0000 | [diff] [blame] | 180 | #endif |
Bernhard Reutner-Fischer | 99003b8 | 2007-04-10 19:30:50 +0000 | [diff] [blame] | 181 | }; |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 182 | int exitcode = EXIT_FAILURE; |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 183 | size_t ibs = 512, obs = 512; |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 184 | ssize_t n, w; |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 185 | char *ibuf, *obuf; |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 186 | /* And these are all zeroed at once! */ |
| 187 | struct { |
| 188 | int flags; |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 189 | size_t oc; |
| 190 | off_t count; |
| 191 | off_t seek, skip; |
| 192 | const char *infile, *outfile; |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 193 | } Z; |
| 194 | #define flags (Z.flags ) |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 195 | #define oc (Z.oc ) |
| 196 | #define count (Z.count ) |
| 197 | #define seek (Z.seek ) |
| 198 | #define skip (Z.skip ) |
| 199 | #define infile (Z.infile ) |
| 200 | #define outfile (Z.outfile) |
Eric Andersen | ddea368 | 2000-11-29 22:33:02 +0000 | [diff] [blame] | 201 | |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 202 | memset(&Z, 0, sizeof(Z)); |
| 203 | INIT_G(); |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 204 | //fflush(NULL); - is this needed because of NOEXEC? |
Denis Vlasenko | f6250a3 | 2007-04-19 20:16:14 +0000 | [diff] [blame] | 205 | |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 206 | for (n = 1; argv[n]; n++) { |
| 207 | int what; |
| 208 | char *val; |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 209 | char *arg = argv[n]; |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 210 | |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 211 | #if ENABLE_DESKTOP |
| 212 | /* "dd --". NB: coreutils 6.9 will complain if they see |
| 213 | * more than one of them. We wouldn't. */ |
| 214 | if (arg[0] == '-' && arg[1] == '-' && arg[2] == '\0') |
| 215 | continue; |
| 216 | #endif |
| 217 | val = strchr(arg, '='); |
| 218 | if (val == NULL) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 219 | bb_show_usage(); |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 220 | *val = '\0'; |
| 221 | what = index_in_strings(keywords, arg); |
| 222 | if (what < 0) |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 223 | bb_show_usage(); |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 224 | /* *val = '='; - to preserve ps listing? */ |
| 225 | val++; |
Bernhard Reutner-Fischer | e468ef2 | 2007-04-16 12:21:05 +0000 | [diff] [blame] | 226 | #if ENABLE_FEATURE_DD_IBS_OBS |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 227 | if (what == OP_ibs) { |
| 228 | /* Must fit into positive ssize_t */ |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 229 | ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes); |
| 230 | /*continue;*/ |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 231 | } |
| 232 | if (what == OP_obs) { |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 233 | obs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes); |
| 234 | /*continue;*/ |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 235 | } |
| 236 | if (what == OP_conv) { |
| 237 | while (1) { |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 238 | /* find ',', replace them with NUL so we can use val for |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 239 | * index_in_strings() without copying. |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 240 | * We rely on val being non-null, else strchr would fault. |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 241 | */ |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 242 | arg = strchr(val, ','); |
| 243 | if (arg) |
| 244 | *arg = '\0'; |
| 245 | what = index_in_strings(conv_words, val); |
| 246 | if (what < 0) |
| 247 | bb_error_msg_and_die(bb_msg_invalid_arg, val, "conv"); |
| 248 | flags |= (1 << what); |
| 249 | if (!arg) /* no ',' left, so this was the last specifier */ |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 250 | break; |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 251 | /* *arg = ','; - to preserve ps listing? */ |
| 252 | val = arg + 1; /* skip this keyword and ',' */ |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 253 | } |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 254 | continue; /* we trashed 'what', can't fall through */ |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 255 | } |
Bernhard Reutner-Fischer | e468ef2 | 2007-04-16 12:21:05 +0000 | [diff] [blame] | 256 | #endif |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 257 | if (what == OP_bs) { |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 258 | ibs = obs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes); |
| 259 | /*continue;*/ |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 260 | } |
| 261 | /* These can be large: */ |
| 262 | if (what == OP_count) { |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 263 | flags |= FLAG_COUNT; |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 264 | count = XATOU_SFX(val, dd_suffixes); |
| 265 | /*continue;*/ |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 266 | } |
| 267 | if (what == OP_seek) { |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 268 | seek = XATOU_SFX(val, dd_suffixes); |
| 269 | /*continue;*/ |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 270 | } |
Denis Vlasenko | d1801a4 | 2007-04-19 20:08:19 +0000 | [diff] [blame] | 271 | if (what == OP_skip) { |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 272 | skip = XATOU_SFX(val, dd_suffixes); |
| 273 | /*continue;*/ |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 274 | } |
| 275 | if (what == OP_if) { |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 276 | infile = val; |
| 277 | /*continue;*/ |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 278 | } |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 279 | if (what == OP_of) { |
| 280 | outfile = val; |
| 281 | /*continue;*/ |
| 282 | } |
| 283 | } /* end of "for (argv[n])" */ |
| 284 | |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 285 | //XXX:FIXME for huge ibs or obs, malloc'ing them isn't the brightest idea ever |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 286 | ibuf = obuf = xmalloc(ibs); |
| 287 | if (ibs != obs) { |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 288 | flags |= FLAG_TWOBUFS; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 289 | obuf = xmalloc(obs); |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 290 | } |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 291 | |
| 292 | #if ENABLE_FEATURE_DD_SIGNAL_HANDLING |
| 293 | signal_SA_RESTART_empty_mask(SIGUSR1, dd_output_status); |
| 294 | #endif |
| 295 | #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE |
| 296 | G.begin_time_us = monotonic_us(); |
| 297 | #endif |
| 298 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 299 | if (infile != NULL) |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 300 | xmove_fd(xopen(infile, O_RDONLY), ifd); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 301 | else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 302 | infile = bb_msg_standard_input; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 303 | } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 304 | if (outfile != NULL) { |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 305 | int oflag = O_WRONLY | O_CREAT; |
Matt Kraai | c9acf8c | 2001-01-17 00:21:05 +0000 | [diff] [blame] | 306 | |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 307 | if (!seek && !(flags & FLAG_NOTRUNC)) |
Matt Kraai | c9acf8c | 2001-01-17 00:21:05 +0000 | [diff] [blame] | 308 | oflag |= O_TRUNC; |
| 309 | |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 310 | xmove_fd(xopen(outfile, oflag), ofd); |
Matt Kraai | c9acf8c | 2001-01-17 00:21:05 +0000 | [diff] [blame] | 311 | |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 312 | if (seek && !(flags & FLAG_NOTRUNC)) { |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 313 | if (ftruncate(ofd, seek * obs) < 0) { |
Matt Kraai | eb83478 | 2002-02-05 15:28:54 +0000 | [diff] [blame] | 314 | struct stat st; |
| 315 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 316 | if (fstat(ofd, &st) < 0 || S_ISREG(st.st_mode) || |
| 317 | S_ISDIR(st.st_mode)) |
| 318 | goto die_outfile; |
Matt Kraai | eb83478 | 2002-02-05 15:28:54 +0000 | [diff] [blame] | 319 | } |
Matt Kraai | c9acf8c | 2001-01-17 00:21:05 +0000 | [diff] [blame] | 320 | } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 321 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 322 | outfile = bb_msg_standard_output; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 323 | } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 324 | if (skip) { |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 325 | if (lseek(ifd, skip * ibs, SEEK_CUR) < 0) { |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 326 | while (skip-- > 0) { |
| 327 | n = safe_read(ifd, ibuf, ibs); |
| 328 | if (n < 0) |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 329 | goto die_infile; |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 330 | if (n == 0) |
| 331 | break; |
| 332 | } |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 333 | } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 334 | } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 335 | if (seek) { |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 336 | if (lseek(ofd, seek * obs, SEEK_CUR) < 0) |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 337 | goto die_outfile; |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 338 | } |
Eric Andersen | ddea368 | 2000-11-29 22:33:02 +0000 | [diff] [blame] | 339 | |
Denis Vlasenko | 2b03e5e | 2007-07-29 14:59:06 +0000 | [diff] [blame] | 340 | while (!(flags & FLAG_COUNT) || (G.in_full + G.in_part != count)) { |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 341 | n = safe_read(ifd, ibuf, ibs); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 342 | if (n == 0) |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 343 | break; |
Eric Andersen | ef38b39 | 2002-04-27 01:31:43 +0000 | [diff] [blame] | 344 | if (n < 0) { |
Denys Vlasenko | f00cfdf | 2009-09-18 20:49:36 +0200 | [diff] [blame] | 345 | /* "Bad block" */ |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 346 | if (!(flags & FLAG_NOERROR)) |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 347 | goto die_infile; |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 348 | bb_simple_perror_msg(infile); |
Denys Vlasenko | f00cfdf | 2009-09-18 20:49:36 +0200 | [diff] [blame] | 349 | /* GNU dd with conv=noerror skips over bad blocks */ |
Denys Vlasenko | 334fcc8 | 2009-08-05 23:16:27 +0200 | [diff] [blame] | 350 | xlseek(ifd, ibs, SEEK_CUR); |
Denys Vlasenko | f00cfdf | 2009-09-18 20:49:36 +0200 | [diff] [blame] | 351 | /* conv=noerror,sync writes NULs, |
| 352 | * conv=noerror just ignores input bad blocks */ |
| 353 | n = 0; |
Eric Andersen | ef38b39 | 2002-04-27 01:31:43 +0000 | [diff] [blame] | 354 | } |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 355 | if ((size_t)n == ibs) |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 356 | G.in_full++; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 357 | else { |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 358 | G.in_part++; |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 359 | if (flags & FLAG_SYNC) { |
Denys Vlasenko | f00cfdf | 2009-09-18 20:49:36 +0200 | [diff] [blame] | 360 | memset(ibuf + n, 0, ibs - n); |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 361 | n = ibs; |
| 362 | } |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 363 | } |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 364 | if (flags & FLAG_TWOBUFS) { |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 365 | char *tmp = ibuf; |
| 366 | while (n) { |
| 367 | size_t d = obs - oc; |
| 368 | |
Denis Vlasenko | 77ad97f | 2008-05-13 02:27:31 +0000 | [diff] [blame] | 369 | if (d > (size_t)n) |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 370 | d = n; |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 371 | memcpy(obuf + oc, tmp, d); |
| 372 | n -= d; |
| 373 | tmp += d; |
| 374 | oc += d; |
| 375 | if (oc == obs) { |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 376 | if (write_and_stats(obuf, obs, obs, outfile)) |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 377 | goto out_status; |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 378 | oc = 0; |
| 379 | } |
| 380 | } |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 381 | } else if (write_and_stats(ibuf, n, obs, outfile)) |
Denis Vlasenko | d1801a4 | 2007-04-19 20:08:19 +0000 | [diff] [blame] | 382 | goto out_status; |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 383 | |
| 384 | if (flags & FLAG_FSYNC) { |
| 385 | if (fsync(ofd) < 0) |
| 386 | goto die_outfile; |
| 387 | } |
Glenn L McGrath | f0b073f | 2000-09-11 00:32:13 +0000 | [diff] [blame] | 388 | } |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 389 | |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 390 | if (ENABLE_FEATURE_DD_IBS_OBS && oc) { |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 391 | w = full_write_or_warn(obuf, oc, outfile); |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 392 | if (w < 0) goto out_status; |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 393 | if (w > 0) G.out_part++; |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 394 | } |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 395 | if (close(ifd) < 0) { |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 396 | die_infile: |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 397 | bb_simple_perror_msg_and_die(infile); |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 398 | } |
Matt Kraai | 3eeab3b | 2001-12-07 16:27:37 +0000 | [diff] [blame] | 399 | |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 400 | if (close(ofd) < 0) { |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 401 | die_outfile: |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 402 | bb_simple_perror_msg_and_die(outfile); |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 403 | } |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 404 | |
| 405 | exitcode = EXIT_SUCCESS; |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 406 | out_status: |
Rob Landley | c559817 | 2006-05-02 22:44:04 +0000 | [diff] [blame] | 407 | dd_output_status(0); |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 408 | |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 409 | return exitcode; |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 410 | } |