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