/* * Position input/output data streams before starting the copy. Device type * dependent. Seekable devices use lseek, and the rest position by reading. * Seeking past the end of file can cause null blocks to be written to the * output. */ void pos_in(void) { int bcnt, cnt, nr, warned; /* If not a pipe or tape device, try to seek on it. */ if (!(in.flags & (ISPIPE|ISTAPE))) { if (ddop_lseek(in, in.fd, (off_t)in.offset * (off_t)in.dbsz, SEEK_CUR) == -1) { err(EXIT_FAILURE, "%s", in.name); /* NOTREACHED */ } return; /* NOTREACHED */ } /* * Read the data. If a pipe, read until satisfy the number of bytes * being skipped. No differentiation for reading complete and partial * blocks for other devices. */ for (bcnt = in.dbsz, cnt = in.offset, warned = 0; cnt;) { if ((nr = ddop_read(in, in.fd, in.db, bcnt)) > 0) { if (in.flags & ISPIPE) { if (!(bcnt -= nr)) { bcnt = in.dbsz; --cnt; } } else --cnt; continue; } if (nr == 0) { if (files_cnt > 1) { --files_cnt; continue; } errx(EXIT_FAILURE, "skip reached end of input"); /* NOTREACHED */ } /* * Input error -- either EOF with no more files, or I/O error. * If noerror not set die. POSIX requires that the warning * message be followed by an I/O display. */ if (ddflags & C_NOERROR) { if (!warned) { warn("%s", in.name); warned = 1; summary(); } continue; } err(EXIT_FAILURE, "%s", in.name); /* NOTREACHED */ } }
void pos_out(void) { struct mtop t_op; int n; uint64_t cnt; /* * If not a tape, try seeking on the file. Seeking on a pipe is * going to fail, but don't protect the user -- they shouldn't * have specified the seek operand. */ if (!(out.flags & ISTAPE)) { if (ddop_lseek(out, out.fd, (off_t)out.offset * (off_t)out.dbsz, SEEK_SET) == -1) err(EXIT_FAILURE, "%s", out.name); /* NOTREACHED */ return; } /* If no read access, try using mtio. */ if (out.flags & NOREAD) { t_op.mt_op = MTFSR; t_op.mt_count = out.offset; if (ddop_ioctl(out, out.fd, MTIOCTOP, &t_op) < 0) err(EXIT_FAILURE, "%s", out.name); /* NOTREACHED */ return; } /* Read it. */ for (cnt = 0; cnt < out.offset; ++cnt) { if ((n = ddop_read(out, out.fd, out.db, out.dbsz)) > 0) continue; if (n < 0) err(EXIT_FAILURE, "%s", out.name); /* NOTREACHED */ /* * If reach EOF, fill with NUL characters; first, back up over * the EOF mark. Note, cnt has not yet been incremented, so * the EOF read does not count as a seek'd block. */ t_op.mt_op = MTBSR; t_op.mt_count = 1; if (ddop_ioctl(out, out.fd, MTIOCTOP, &t_op) == -1) err(EXIT_FAILURE, "%s", out.name); /* NOTREACHED */ while (cnt++ < out.offset) if ((uint64_t)(n = bwrite(&out, out.db, out.dbsz)) != out.dbsz) err(EXIT_FAILURE, "%s", out.name); /* NOTREACHED */ break; } }
static void dd_in(void) { int flags; int64_t n; for (flags = ddflags;;) { if (cpy_cnt && (st.in_full + st.in_part) >= cpy_cnt) return; /* * Clear the buffer first if doing "sync" on input. * If doing block operations use spaces. This will * affect not only the C_NOERROR case, but also the * last partial input block which should be padded * with zero and not garbage. */ if (flags & C_SYNC) { if (flags & (C_BLOCK|C_UNBLOCK)) (void)memset(in.dbp, ' ', in.dbsz); else (void)memset(in.dbp, 0, in.dbsz); } n = ddop_read(in, in.fd, in.dbp, in.dbsz); if (n == 0) { in.dbrcnt = 0; return; } /* Read error. */ if (n < 0) { /* * If noerror not specified, die. POSIX requires that * the warning message be followed by an I/O display. */ if (!(flags & C_NOERROR)) { err(EXIT_FAILURE, "%s", in.name); /* NOTREACHED */ } warn("%s", in.name); summary(); /* * If it's not a tape drive or a pipe, seek past the * error. If your OS doesn't do the right thing for * raw disks this section should be modified to re-read * in sector size chunks. */ if (!(in.flags & (ISPIPE|ISTAPE)) && ddop_lseek(in, in.fd, (off_t)in.dbsz, SEEK_CUR)) warn("%s", in.name); /* If sync not specified, omit block and continue. */ if (!(ddflags & C_SYNC)) continue; /* Read errors count as full blocks. */ in.dbcnt += in.dbrcnt = in.dbsz; ++st.in_full; /* Handle full input blocks. */ } else if ((uint64_t)n == in.dbsz) { in.dbcnt += in.dbrcnt = n; ++st.in_full; /* Handle partial input blocks. */ } else { /* If sync, use the entire block. */ if (ddflags & C_SYNC) in.dbcnt += in.dbrcnt = in.dbsz; else in.dbcnt += in.dbrcnt = n; ++st.in_part; } /* * POSIX states that if bs is set and no other conversions * than noerror, notrunc or sync are specified, the block * is output without buffering as it is read. */ if (ddflags & C_BS) { out.dbcnt = in.dbcnt; dd_out(1); in.dbcnt = 0; continue; } if (ddflags & C_SWAB) { if ((n = in.dbrcnt) & 1) { ++st.swab; --n; } swab(in.dbp, in.dbp, n); } in.dbp += in.dbrcnt; (*cfunc)(); } }