dd.c revision 1.36 1 /* $NetBSD: dd.c,v 1.36 2004/01/17 20:48:57 dbj Exp $ */
2
3 /*-
4 * Copyright (c) 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Keith Muller of the University of California, San Diego and Lance
9 * Visser of Convex Computer Corporation.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1991, 1993, 1994\n\
39 The Regents of the University of California. All rights reserved.\n");
40 #endif /* not lint */
41
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)dd.c 8.5 (Berkeley) 4/2/94";
45 #else
46 __RCSID("$NetBSD: dd.c,v 1.36 2004/01/17 20:48:57 dbj Exp $");
47 #endif
48 #endif /* not lint */
49
50 #include <sys/param.h>
51 #include <sys/stat.h>
52 #include <sys/ioctl.h>
53 #include <sys/mtio.h>
54 #include <sys/time.h>
55
56 #include <ctype.h>
57 #include <err.h>
58 #include <errno.h>
59 #include <fcntl.h>
60 #include <signal.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <unistd.h>
65
66 #include "dd.h"
67 #include "extern.h"
68
69 static void dd_close(void);
70 static void dd_in(void);
71 static void getfdtype(IO *);
72 static int redup_clean_fd(int);
73 static void setup(void);
74
75 int main(int, char *[]);
76
77 IO in, out; /* input/output state */
78 STAT st; /* statistics */
79 void (*cfunc)(void); /* conversion function */
80 uint64_t cpy_cnt; /* # of blocks to copy */
81 static off_t pending = 0; /* pending seek if sparse */
82 u_int ddflags; /* conversion options */
83 uint64_t cbsz; /* conversion block size */
84 u_int files_cnt = 1; /* # of files to copy */
85 int progress = 0; /* display sign of life */
86 const u_char *ctab; /* conversion table */
87 sigset_t infoset; /* a set blocking SIGINFO */
88
89 int
90 main(int argc, char *argv[])
91 {
92 int ch;
93
94 while ((ch = getopt(argc, argv, "")) != -1) {
95 switch (ch) {
96 default:
97 errx(EXIT_FAILURE, "usage: dd [operand ...]");
98 /* NOTREACHED */
99 }
100 }
101 argc -= (optind - 1);
102 argv += (optind - 1);
103
104 jcl(argv);
105 setup();
106
107 (void)signal(SIGINFO, summaryx);
108 (void)signal(SIGINT, terminate);
109 (void)sigemptyset(&infoset);
110 (void)sigaddset(&infoset, SIGINFO);
111
112 (void)atexit(summary);
113
114 while (files_cnt--)
115 dd_in();
116
117 dd_close();
118 exit(0);
119 /* NOTREACHED */
120 }
121
122 static void
123 setup(void)
124 {
125
126 if (in.name == NULL) {
127 in.name = "stdin";
128 in.fd = STDIN_FILENO;
129 } else {
130 in.fd = open(in.name, O_RDONLY, 0);
131 if (in.fd < 0)
132 err(EXIT_FAILURE, "%s", in.name);
133 /* NOTREACHED */
134
135 /* Ensure in.fd is outside the stdio descriptor range */
136 in.fd = redup_clean_fd(in.fd);
137 }
138
139 getfdtype(&in);
140
141 if (files_cnt > 1 && !(in.flags & ISTAPE)) {
142 errx(EXIT_FAILURE, "files is not supported for non-tape devices");
143 /* NOTREACHED */
144 }
145
146 if (out.name == NULL) {
147 /* No way to check for read access here. */
148 out.fd = STDOUT_FILENO;
149 out.name = "stdout";
150 } else {
151 #define OFLAGS \
152 (O_CREAT | (ddflags & (C_SEEK | C_NOTRUNC) ? 0 : O_TRUNC))
153 out.fd = open(out.name, O_RDWR | OFLAGS, DEFFILEMODE);
154 /*
155 * May not have read access, so try again with write only.
156 * Without read we may have a problem if output also does
157 * not support seeks.
158 */
159 if (out.fd < 0) {
160 out.fd = open(out.name, O_WRONLY | OFLAGS, DEFFILEMODE);
161 out.flags |= NOREAD;
162 }
163 if (out.fd < 0) {
164 err(EXIT_FAILURE, "%s", out.name);
165 /* NOTREACHED */
166 }
167
168 /* Ensure out.fd is outside the stdio descriptor range */
169 out.fd = redup_clean_fd(out.fd);
170 }
171
172 getfdtype(&out);
173
174 /*
175 * Allocate space for the input and output buffers. If not doing
176 * record oriented I/O, only need a single buffer.
177 */
178 if (!(ddflags & (C_BLOCK|C_UNBLOCK))) {
179 if ((in.db = malloc(out.dbsz + in.dbsz - 1)) == NULL) {
180 err(EXIT_FAILURE, NULL);
181 /* NOTREACHED */
182 }
183 out.db = in.db;
184 } else if ((in.db =
185 malloc((u_int)(MAX(in.dbsz, cbsz) + cbsz))) == NULL ||
186 (out.db = malloc((u_int)(out.dbsz + cbsz))) == NULL) {
187 err(EXIT_FAILURE, NULL);
188 /* NOTREACHED */
189 }
190 in.dbp = in.db;
191 out.dbp = out.db;
192
193 /* Position the input/output streams. */
194 if (in.offset)
195 pos_in();
196 if (out.offset)
197 pos_out();
198
199 /*
200 * Truncate the output file; ignore errors because it fails on some
201 * kinds of output files, tapes, for example.
202 */
203 if ((ddflags & (C_OF | C_SEEK | C_NOTRUNC)) == (C_OF | C_SEEK))
204 (void)ftruncate(out.fd, (off_t)out.offset * out.dbsz);
205
206 /*
207 * If converting case at the same time as another conversion, build a
208 * table that does both at once. If just converting case, use the
209 * built-in tables.
210 */
211 if (ddflags & (C_LCASE|C_UCASE)) {
212 #ifdef NO_CONV
213 /* Should not get here, but just in case... */
214 errx(EXIT_FAILURE, "case conv and -DNO_CONV");
215 /* NOTREACHED */
216 #else /* NO_CONV */
217 u_int cnt;
218
219 if (ddflags & C_ASCII || ddflags & C_EBCDIC) {
220 if (ddflags & C_LCASE) {
221 for (cnt = 0; cnt < 0377; ++cnt)
222 casetab[cnt] = tolower(ctab[cnt]);
223 } else {
224 for (cnt = 0; cnt < 0377; ++cnt)
225 casetab[cnt] = toupper(ctab[cnt]);
226 }
227 } else {
228 if (ddflags & C_LCASE) {
229 for (cnt = 0; cnt < 0377; ++cnt)
230 casetab[cnt] = tolower(cnt);
231 } else {
232 for (cnt = 0; cnt < 0377; ++cnt)
233 casetab[cnt] = toupper(cnt);
234 }
235 }
236
237 ctab = casetab;
238 #endif /* NO_CONV */
239 }
240
241 (void)gettimeofday(&st.start, NULL); /* Statistics timestamp. */
242 }
243
244 static void
245 getfdtype(IO *io)
246 {
247 struct mtget mt;
248 struct stat sb;
249
250 if (fstat(io->fd, &sb)) {
251 err(EXIT_FAILURE, "%s", io->name);
252 /* NOTREACHED */
253 }
254 if (S_ISCHR(sb.st_mode))
255 io->flags |= ioctl(io->fd, MTIOCGET, &mt) ? ISCHR : ISTAPE;
256 else if (lseek(io->fd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE)
257 io->flags |= ISPIPE; /* XXX fixed in 4.4BSD */
258 }
259
260 /*
261 * Move the parameter file descriptor to a descriptor that is outside the
262 * stdio descriptor range, if necessary. This is required to avoid
263 * accidentally outputting completion or error messages into the
264 * output file that were intended for the tty.
265 */
266 static int
267 redup_clean_fd(int fd)
268 {
269 int newfd;
270
271 if (fd != STDIN_FILENO && fd != STDOUT_FILENO &&
272 fd != STDERR_FILENO)
273 /* File descriptor is ok, return immediately. */
274 return fd;
275
276 /*
277 * 3 is the first descriptor greater than STD*_FILENO. Any
278 * free descriptor valued 3 or above is acceptable...
279 */
280 newfd = fcntl(fd, F_DUPFD, 3);
281 if (newfd < 0) {
282 err(EXIT_FAILURE, "dupfd IO");
283 /* NOTREACHED */
284 }
285
286 close(fd);
287
288 return newfd;
289 }
290
291 static void
292 dd_in(void)
293 {
294 int flags;
295 int64_t n;
296
297 for (flags = ddflags;;) {
298 if (cpy_cnt && (st.in_full + st.in_part) >= cpy_cnt)
299 return;
300
301 /*
302 * Clear the buffer first if doing "sync" on input.
303 * If doing block operations use spaces. This will
304 * affect not only the C_NOERROR case, but also the
305 * last partial input block which should be padded
306 * with zero and not garbage.
307 */
308 if (flags & C_SYNC) {
309 if (flags & (C_BLOCK|C_UNBLOCK))
310 (void)memset(in.dbp, ' ', in.dbsz);
311 else
312 (void)memset(in.dbp, 0, in.dbsz);
313 }
314
315 n = read(in.fd, in.dbp, in.dbsz);
316 if (n == 0) {
317 in.dbrcnt = 0;
318 return;
319 }
320
321 /* Read error. */
322 if (n < 0) {
323
324 /*
325 * If noerror not specified, die. POSIX requires that
326 * the warning message be followed by an I/O display.
327 */
328 if (!(flags & C_NOERROR)) {
329 err(EXIT_FAILURE, "%s", in.name);
330 /* NOTREACHED */
331 }
332 warn("%s", in.name);
333 summary();
334
335 /*
336 * If it's not a tape drive or a pipe, seek past the
337 * error. If your OS doesn't do the right thing for
338 * raw disks this section should be modified to re-read
339 * in sector size chunks.
340 */
341 if (!(in.flags & (ISPIPE|ISTAPE)) &&
342 lseek(in.fd, (off_t)in.dbsz, SEEK_CUR))
343 warn("%s", in.name);
344
345 /* If sync not specified, omit block and continue. */
346 if (!(ddflags & C_SYNC))
347 continue;
348
349 /* Read errors count as full blocks. */
350 in.dbcnt += in.dbrcnt = in.dbsz;
351 ++st.in_full;
352
353 /* Handle full input blocks. */
354 } else if (n == in.dbsz) {
355 in.dbcnt += in.dbrcnt = n;
356 ++st.in_full;
357
358 /* Handle partial input blocks. */
359 } else {
360 /* If sync, use the entire block. */
361 if (ddflags & C_SYNC)
362 in.dbcnt += in.dbrcnt = in.dbsz;
363 else
364 in.dbcnt += in.dbrcnt = n;
365 ++st.in_part;
366 }
367
368 /*
369 * POSIX states that if bs is set and no other conversions
370 * than noerror, notrunc or sync are specified, the block
371 * is output without buffering as it is read.
372 */
373 if (ddflags & C_BS) {
374 out.dbcnt = in.dbcnt;
375 dd_out(1);
376 in.dbcnt = 0;
377 continue;
378 }
379
380 if (ddflags & C_SWAB) {
381 if ((n = in.dbrcnt) & 1) {
382 ++st.swab;
383 --n;
384 }
385 swab(in.dbp, in.dbp, n);
386 }
387
388 in.dbp += in.dbrcnt;
389 (*cfunc)();
390 }
391 }
392
393 /*
394 * Cleanup any remaining I/O and flush output. If necesssary, output file
395 * is truncated.
396 */
397 static void
398 dd_close(void)
399 {
400
401 if (cfunc == def)
402 def_close();
403 else if (cfunc == block)
404 block_close();
405 else if (cfunc == unblock)
406 unblock_close();
407 if (ddflags & C_OSYNC && out.dbcnt < out.dbsz) {
408 (void)memset(out.dbp, 0, out.dbsz - out.dbcnt);
409 out.dbcnt = out.dbsz;
410 }
411 /* If there are pending sparse blocks, make sure
412 * to write out the final block un-sparse
413 */
414 if ((out.dbcnt == 0) && pending) {
415 memset(out.db, 0, out.dbsz);
416 out.dbcnt = out.dbsz;
417 pending -= out.dbsz;
418 }
419 if (out.dbcnt)
420 dd_out(1);
421
422 /*
423 * Reporting nfs write error may be defered until next
424 * write(2) or close(2) system call. So, we need to do an
425 * extra check. If an output is stdout, the file structure
426 * may be shared among with other processes and close(2) just
427 * decreases the reference count.
428 */
429 if (out.fd == STDOUT_FILENO && fsync(out.fd) == -1 && errno != EINVAL) {
430 err(EXIT_FAILURE, "fsync stdout");
431 /* NOTREACHED */
432 }
433 if (close(out.fd) == -1) {
434 err(EXIT_FAILURE, "close");
435 /* NOTREACHED */
436 }
437 }
438
439 void
440 dd_out(int force)
441 {
442 static int warned;
443 int64_t cnt, n, nw;
444 u_char *outp;
445
446 /*
447 * Write one or more blocks out. The common case is writing a full
448 * output block in a single write; increment the full block stats.
449 * Otherwise, we're into partial block writes. If a partial write,
450 * and it's a character device, just warn. If a tape device, quit.
451 *
452 * The partial writes represent two cases. 1: Where the input block
453 * was less than expected so the output block was less than expected.
454 * 2: Where the input block was the right size but we were forced to
455 * write the block in multiple chunks. The original versions of dd(1)
456 * never wrote a block in more than a single write, so the latter case
457 * never happened.
458 *
459 * One special case is if we're forced to do the write -- in that case
460 * we play games with the buffer size, and it's usually a partial write.
461 */
462 outp = out.db;
463 for (n = force ? out.dbcnt : out.dbsz;; n = out.dbsz) {
464 for (cnt = n;; cnt -= nw) {
465
466 if (!force && ddflags & C_SPARSE) {
467 int sparse, i;
468 sparse = 1; /* Is buffer sparse? */
469 for (i = 0; i < cnt; i++)
470 if (outp[i] != 0) {
471 sparse = 0;
472 break;
473 }
474 if (sparse) {
475 pending += cnt;
476 outp += cnt;
477 nw = 0;
478 break;
479 }
480 }
481 if (pending != 0) {
482 if (lseek(out.fd, pending, SEEK_CUR) ==
483 -1)
484 err(EXIT_FAILURE, "%s: seek error creating sparse file",
485 out.name);
486 }
487 nw = bwrite(out.fd, outp, cnt);
488 if (nw <= 0) {
489 if (nw == 0)
490 errx(EXIT_FAILURE,
491 "%s: end of device", out.name);
492 /* NOTREACHED */
493 if (errno != EINTR)
494 err(EXIT_FAILURE, "%s", out.name);
495 /* NOTREACHED */
496 nw = 0;
497 }
498 if (pending) {
499 st.bytes += pending;
500 st.sparse += pending/out.dbsz;
501 st.out_full += pending/out.dbsz;
502 pending = 0;
503 }
504 outp += nw;
505 st.bytes += nw;
506 if (nw == n) {
507 if (n != out.dbsz)
508 ++st.out_part;
509 else
510 ++st.out_full;
511 break;
512 }
513 ++st.out_part;
514 if (nw == cnt)
515 break;
516 if (out.flags & ISCHR && !warned) {
517 warned = 1;
518 warnx("%s: short write on character device", out.name);
519 }
520 if (out.flags & ISTAPE)
521 errx(EXIT_FAILURE,
522 "%s: short write on tape device", out.name);
523 /* NOTREACHED */
524
525 }
526 if ((out.dbcnt -= n) < out.dbsz)
527 break;
528 }
529
530 /* Reassemble the output block. */
531 if (out.dbcnt)
532 (void)memmove(out.db, out.dbp - out.dbcnt, out.dbcnt);
533 out.dbp = out.db + out.dbcnt;
534
535 if (progress)
536 (void)write(STDERR_FILENO, ".", 1);
537 }
538
539 /*
540 * A protected against SIGINFO write
541 */
542 ssize_t
543 bwrite(int fd, const void *buf, size_t len)
544 {
545 sigset_t oset;
546 ssize_t rv;
547 int oerrno;
548
549 (void)sigprocmask(SIG_BLOCK, &infoset, &oset);
550 rv = write(fd, buf, len);
551 oerrno = errno;
552 (void)sigprocmask(SIG_SETMASK, &oset, NULL);
553 errno = oerrno;
554 return (rv);
555 }
556