xref: /freebsd-src/bin/dd/position.c (revision e043f37205ffbde5627ff299ad25cd532f2956f0)
14b88c807SRodney W. Grimes /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
44b88c807SRodney W. Grimes  * Copyright (c) 1991, 1993, 1994
54b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
64b88c807SRodney W. Grimes  *
74b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
84b88c807SRodney W. Grimes  * Keith Muller of the University of California, San Diego and Lance
94b88c807SRodney W. Grimes  * Visser of Convex Computer Corporation.
104b88c807SRodney W. Grimes  *
114b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
124b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
134b88c807SRodney W. Grimes  * are met:
144b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
154b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
164b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
174b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
184b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
19fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
204b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
214b88c807SRodney W. Grimes  *    without specific prior written permission.
224b88c807SRodney W. Grimes  *
234b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
244b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
254b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
264b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
274b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
284b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
294b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
304b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
314b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
324b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
334b88c807SRodney W. Grimes  * SUCH DAMAGE.
344b88c807SRodney W. Grimes  */
354b88c807SRodney W. Grimes 
361ba0e048SPhilippe Charnier #include <sys/types.h>
374b88c807SRodney W. Grimes #include <sys/mtio.h>
384b88c807SRodney W. Grimes 
394b88c807SRodney W. Grimes #include <err.h>
40e14f7e78SBrian Feldman #include <errno.h>
417503d74fSMark Murray #include <inttypes.h>
42b156d221SConrad Meyer #include <limits.h>
434ac11639SEitan Adler #include <signal.h>
444b88c807SRodney W. Grimes #include <unistd.h>
454b88c807SRodney W. Grimes 
464b88c807SRodney W. Grimes #include "dd.h"
474b88c807SRodney W. Grimes #include "extern.h"
484b88c807SRodney W. Grimes 
49b156d221SConrad Meyer static off_t
seek_offset(IO * io)50b156d221SConrad Meyer seek_offset(IO *io)
51b156d221SConrad Meyer {
52b156d221SConrad Meyer 	off_t n;
53b156d221SConrad Meyer 	size_t sz;
54b156d221SConrad Meyer 
55b156d221SConrad Meyer 	n = io->offset;
56b156d221SConrad Meyer 	sz = io->dbsz;
57b156d221SConrad Meyer 
58b156d221SConrad Meyer 	_Static_assert(sizeof(io->offset) == sizeof(int64_t), "64-bit off_t");
59b156d221SConrad Meyer 
60b156d221SConrad Meyer 	/*
61b156d221SConrad Meyer 	 * If the lseek offset will be negative, verify that this is a special
62b156d221SConrad Meyer 	 * device file.  Some such files (e.g. /dev/kmem) permit "negative"
63b156d221SConrad Meyer 	 * offsets.
64b156d221SConrad Meyer 	 *
65b156d221SConrad Meyer 	 * Bail out if the calculation of a file offset would overflow.
66b156d221SConrad Meyer 	 */
67af2b6af3SAlan Somers 	if ((io->flags & ISCHR) == 0 && (n < 0 || n > OFF_MAX / (ssize_t)sz))
68b156d221SConrad Meyer 		errx(1, "seek offsets cannot be larger than %jd",
69b156d221SConrad Meyer 		    (intmax_t)OFF_MAX);
70b156d221SConrad Meyer 	else if ((io->flags & ISCHR) != 0 && (uint64_t)n > UINT64_MAX / sz)
71b156d221SConrad Meyer 		errx(1, "seek offsets cannot be larger than %ju",
72b156d221SConrad Meyer 		    (uintmax_t)UINT64_MAX);
73b156d221SConrad Meyer 
74b156d221SConrad Meyer 	return ((off_t)( (uint64_t)n * sz ));
75b156d221SConrad Meyer }
76b156d221SConrad Meyer 
774b88c807SRodney W. Grimes /*
784b88c807SRodney W. Grimes  * Position input/output data streams before starting the copy.  Device type
794b88c807SRodney W. Grimes  * dependent.  Seekable devices use lseek, and the rest position by reading.
804b88c807SRodney W. Grimes  * Seeking past the end of file can cause null blocks to be written to the
814b88c807SRodney W. Grimes  * output.
824b88c807SRodney W. Grimes  */
834b88c807SRodney W. Grimes void
pos_in(void)84f9bcb0beSWarner Losh pos_in(void)
854b88c807SRodney W. Grimes {
86767bc8adSBrian Feldman 	off_t cnt;
8758687472SBrian Feldman 	int warned;
8858687472SBrian Feldman 	ssize_t nr;
8958687472SBrian Feldman 	size_t bcnt;
904b88c807SRodney W. Grimes 
917599187eSBrian Feldman 	/* If known to be seekable, try to seek on it. */
927599187eSBrian Feldman 	if (in.flags & ISSEEK) {
93e14f7e78SBrian Feldman 		errno = 0;
94b156d221SConrad Meyer 		if (lseek(in.fd, seek_offset(&in), SEEK_CUR) == -1 &&
9558687472SBrian Feldman 		    errno != 0)
964b88c807SRodney W. Grimes 			err(1, "%s", in.name);
974b88c807SRodney W. Grimes 		return;
984b88c807SRodney W. Grimes 	}
994b88c807SRodney W. Grimes 
10053885065SBrian Feldman 	/* Don't try to read a really weird amount (like negative). */
10153885065SBrian Feldman 	if (in.offset < 0)
10253885065SBrian Feldman 		errx(1, "%s: illegal offset", "iseek/skip");
10353885065SBrian Feldman 
1044b88c807SRodney W. Grimes 	/*
1054b88c807SRodney W. Grimes 	 * Read the data.  If a pipe, read until satisfy the number of bytes
1064b88c807SRodney W. Grimes 	 * being skipped.  No differentiation for reading complete and partial
1074b88c807SRodney W. Grimes 	 * blocks for other devices.
1084b88c807SRodney W. Grimes 	 */
1094b88c807SRodney W. Grimes 	for (bcnt = in.dbsz, cnt = in.offset, warned = 0; cnt;) {
1104b88c807SRodney W. Grimes 		if ((nr = read(in.fd, in.db, bcnt)) > 0) {
1114b88c807SRodney W. Grimes 			if (in.flags & ISPIPE) {
1124b88c807SRodney W. Grimes 				if (!(bcnt -= nr)) {
1134b88c807SRodney W. Grimes 					bcnt = in.dbsz;
1144b88c807SRodney W. Grimes 					--cnt;
1154b88c807SRodney W. Grimes 				}
1164b88c807SRodney W. Grimes 			} else
1174b88c807SRodney W. Grimes 				--cnt;
1184ac11639SEitan Adler 			if (need_summary)
1194ac11639SEitan Adler 				summary();
1208acbb227SKyle Evans 			if (need_progress)
1218acbb227SKyle Evans 				progress();
1224b88c807SRodney W. Grimes 			continue;
1234b88c807SRodney W. Grimes 		}
1244b88c807SRodney W. Grimes 
1254b88c807SRodney W. Grimes 		if (nr == 0) {
1264b88c807SRodney W. Grimes 			if (files_cnt > 1) {
1274b88c807SRodney W. Grimes 				--files_cnt;
1284b88c807SRodney W. Grimes 				continue;
1294b88c807SRodney W. Grimes 			}
1304b88c807SRodney W. Grimes 			errx(1, "skip reached end of input");
1314b88c807SRodney W. Grimes 		}
1324b88c807SRodney W. Grimes 
1334b88c807SRodney W. Grimes 		/*
1344b88c807SRodney W. Grimes 		 * Input error -- either EOF with no more files, or I/O error.
1354b88c807SRodney W. Grimes 		 * If noerror not set die.  POSIX requires that the warning
1364b88c807SRodney W. Grimes 		 * message be followed by an I/O display.
1374b88c807SRodney W. Grimes 		 */
1384b88c807SRodney W. Grimes 		if (ddflags & C_NOERROR) {
1394b88c807SRodney W. Grimes 			if (!warned) {
1404b88c807SRodney W. Grimes 				warn("%s", in.name);
1414b88c807SRodney W. Grimes 				warned = 1;
1424b88c807SRodney W. Grimes 				summary();
1434b88c807SRodney W. Grimes 			}
1444b88c807SRodney W. Grimes 			continue;
1454b88c807SRodney W. Grimes 		}
1464b88c807SRodney W. Grimes 		err(1, "%s", in.name);
1474b88c807SRodney W. Grimes 	}
1484b88c807SRodney W. Grimes }
1494b88c807SRodney W. Grimes 
1504b88c807SRodney W. Grimes void
pos_out(void)151f9bcb0beSWarner Losh pos_out(void)
1524b88c807SRodney W. Grimes {
1534b88c807SRodney W. Grimes 	struct mtop t_op;
154767bc8adSBrian Feldman 	off_t cnt;
155767bc8adSBrian Feldman 	ssize_t n;
1564b88c807SRodney W. Grimes 
1577599187eSBrian Feldman 	/*
1587599187eSBrian Feldman 	 * If not a tape, try seeking on the file.  Seeking on a pipe is
1597599187eSBrian Feldman 	 * going to fail, but don't protect the user -- they shouldn't
1607599187eSBrian Feldman 	 * have specified the seek operand.
1617599187eSBrian Feldman 	 */
162c15c898eSBrian Feldman 	if (out.flags & (ISSEEK | ISPIPE)) {
163e14f7e78SBrian Feldman 		errno = 0;
164b156d221SConrad Meyer 		if (lseek(out.fd, seek_offset(&out), SEEK_CUR) == -1 &&
16558687472SBrian Feldman 		    errno != 0)
1664b88c807SRodney W. Grimes 			err(1, "%s", out.name);
1674b88c807SRodney W. Grimes 		return;
1684b88c807SRodney W. Grimes 	}
1694b88c807SRodney W. Grimes 
17053885065SBrian Feldman 	/* Don't try to read a really weird amount (like negative). */
17153885065SBrian Feldman 	if (out.offset < 0)
17253885065SBrian Feldman 		errx(1, "%s: illegal offset", "oseek/seek");
17353885065SBrian Feldman 
1744b88c807SRodney W. Grimes 	/* If no read access, try using mtio. */
1754b88c807SRodney W. Grimes 	if (out.flags & NOREAD) {
1764b88c807SRodney W. Grimes 		t_op.mt_op = MTFSR;
1774b88c807SRodney W. Grimes 		t_op.mt_count = out.offset;
1784b88c807SRodney W. Grimes 
179767bc8adSBrian Feldman 		if (ioctl(out.fd, MTIOCTOP, &t_op) == -1)
1804b88c807SRodney W. Grimes 			err(1, "%s", out.name);
1814b88c807SRodney W. Grimes 		return;
1824b88c807SRodney W. Grimes 	}
1834b88c807SRodney W. Grimes 
1844b88c807SRodney W. Grimes 	/* Read it. */
1854b88c807SRodney W. Grimes 	for (cnt = 0; cnt < out.offset; ++cnt) {
186*8dad5eceSKonstantin Belousov 		before_io();
187*8dad5eceSKonstantin Belousov 		n = read(out.fd, out.db, out.dbsz);
188*8dad5eceSKonstantin Belousov 		after_io();
189*8dad5eceSKonstantin Belousov 		if (n > 0)
1904b88c807SRodney W. Grimes 			continue;
191767bc8adSBrian Feldman 		if (n == -1)
1924b88c807SRodney W. Grimes 			err(1, "%s", out.name);
1934b88c807SRodney W. Grimes 
1944b88c807SRodney W. Grimes 		/*
1954b88c807SRodney W. Grimes 		 * If reach EOF, fill with NUL characters; first, back up over
1964b88c807SRodney W. Grimes 		 * the EOF mark.  Note, cnt has not yet been incremented, so
1974b88c807SRodney W. Grimes 		 * the EOF read does not count as a seek'd block.
1984b88c807SRodney W. Grimes 		 */
1994b88c807SRodney W. Grimes 		t_op.mt_op = MTBSR;
2004b88c807SRodney W. Grimes 		t_op.mt_count = 1;
2014b88c807SRodney W. Grimes 		if (ioctl(out.fd, MTIOCTOP, &t_op) == -1)
2024b88c807SRodney W. Grimes 			err(1, "%s", out.name);
2034b88c807SRodney W. Grimes 
204c15c898eSBrian Feldman 		while (cnt++ < out.offset) {
205*8dad5eceSKonstantin Belousov 			before_io();
206c15c898eSBrian Feldman 			n = write(out.fd, out.db, out.dbsz);
207*8dad5eceSKonstantin Belousov 			after_io();
208c15c898eSBrian Feldman 			if (n == -1)
2094b88c807SRodney W. Grimes 				err(1, "%s", out.name);
21077a798b3SAlan Somers 			if (n != out.dbsz)
211c15c898eSBrian Feldman 				errx(1, "%s: write failure", out.name);
212c15c898eSBrian Feldman 		}
2134b88c807SRodney W. Grimes 		break;
2144b88c807SRodney W. Grimes 	}
2154b88c807SRodney W. Grimes }
216