xref: /freebsd/bin/dd/position.c (revision 732a02b4e77866604a120a275c082bb6221bd2ff)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
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 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)position.c	8.3 (Berkeley) 4/2/94";
39 #endif
40 #endif /* not lint */
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43 
44 #include <sys/types.h>
45 #include <sys/mtio.h>
46 
47 #include <err.h>
48 #include <errno.h>
49 #include <inttypes.h>
50 #include <limits.h>
51 #include <signal.h>
52 #include <unistd.h>
53 
54 #include "dd.h"
55 #include "extern.h"
56 
57 static off_t
58 seek_offset(IO *io)
59 {
60 	off_t n;
61 	size_t sz;
62 
63 	n = io->offset;
64 	sz = io->dbsz;
65 
66 	_Static_assert(sizeof(io->offset) == sizeof(int64_t), "64-bit off_t");
67 
68 	/*
69 	 * If the lseek offset will be negative, verify that this is a special
70 	 * device file.  Some such files (e.g. /dev/kmem) permit "negative"
71 	 * offsets.
72 	 *
73 	 * Bail out if the calculation of a file offset would overflow.
74 	 */
75 	if ((io->flags & ISCHR) == 0 && (n < 0 || n > OFF_MAX / (ssize_t)sz))
76 		errx(1, "seek offsets cannot be larger than %jd",
77 		    (intmax_t)OFF_MAX);
78 	else if ((io->flags & ISCHR) != 0 && (uint64_t)n > UINT64_MAX / sz)
79 		errx(1, "seek offsets cannot be larger than %ju",
80 		    (uintmax_t)UINT64_MAX);
81 
82 	return ((off_t)( (uint64_t)n * sz ));
83 }
84 
85 /*
86  * Position input/output data streams before starting the copy.  Device type
87  * dependent.  Seekable devices use lseek, and the rest position by reading.
88  * Seeking past the end of file can cause null blocks to be written to the
89  * output.
90  */
91 void
92 pos_in(void)
93 {
94 	off_t cnt;
95 	int warned;
96 	ssize_t nr;
97 	size_t bcnt;
98 
99 	/* If known to be seekable, try to seek on it. */
100 	if (in.flags & ISSEEK) {
101 		errno = 0;
102 		if (lseek(in.fd, seek_offset(&in), SEEK_CUR) == -1 &&
103 		    errno != 0)
104 			err(1, "%s", in.name);
105 		return;
106 	}
107 
108 	/* Don't try to read a really weird amount (like negative). */
109 	if (in.offset < 0)
110 		errx(1, "%s: illegal offset", "iseek/skip");
111 
112 	/*
113 	 * Read the data.  If a pipe, read until satisfy the number of bytes
114 	 * being skipped.  No differentiation for reading complete and partial
115 	 * blocks for other devices.
116 	 */
117 	for (bcnt = in.dbsz, cnt = in.offset, warned = 0; cnt;) {
118 		if ((nr = read(in.fd, in.db, bcnt)) > 0) {
119 			if (in.flags & ISPIPE) {
120 				if (!(bcnt -= nr)) {
121 					bcnt = in.dbsz;
122 					--cnt;
123 				}
124 			} else
125 				--cnt;
126 			if (need_summary)
127 				summary();
128 			if (need_progress)
129 				progress();
130 			continue;
131 		}
132 
133 		if (nr == 0) {
134 			if (files_cnt > 1) {
135 				--files_cnt;
136 				continue;
137 			}
138 			errx(1, "skip reached end of input");
139 		}
140 
141 		/*
142 		 * Input error -- either EOF with no more files, or I/O error.
143 		 * If noerror not set die.  POSIX requires that the warning
144 		 * message be followed by an I/O display.
145 		 */
146 		if (ddflags & C_NOERROR) {
147 			if (!warned) {
148 				warn("%s", in.name);
149 				warned = 1;
150 				summary();
151 			}
152 			continue;
153 		}
154 		err(1, "%s", in.name);
155 	}
156 }
157 
158 void
159 pos_out(void)
160 {
161 	struct mtop t_op;
162 	off_t cnt;
163 	ssize_t n;
164 
165 	/*
166 	 * If not a tape, try seeking on the file.  Seeking on a pipe is
167 	 * going to fail, but don't protect the user -- they shouldn't
168 	 * have specified the seek operand.
169 	 */
170 	if (out.flags & (ISSEEK | ISPIPE)) {
171 		errno = 0;
172 		if (lseek(out.fd, seek_offset(&out), SEEK_CUR) == -1 &&
173 		    errno != 0)
174 			err(1, "%s", out.name);
175 		return;
176 	}
177 
178 	/* Don't try to read a really weird amount (like negative). */
179 	if (out.offset < 0)
180 		errx(1, "%s: illegal offset", "oseek/seek");
181 
182 	/* If no read access, try using mtio. */
183 	if (out.flags & NOREAD) {
184 		t_op.mt_op = MTFSR;
185 		t_op.mt_count = out.offset;
186 
187 		if (ioctl(out.fd, MTIOCTOP, &t_op) == -1)
188 			err(1, "%s", out.name);
189 		return;
190 	}
191 
192 	/* Read it. */
193 	for (cnt = 0; cnt < out.offset; ++cnt) {
194 		if ((n = read(out.fd, out.db, out.dbsz)) > 0)
195 			continue;
196 
197 		if (n == -1)
198 			err(1, "%s", out.name);
199 
200 		/*
201 		 * If reach EOF, fill with NUL characters; first, back up over
202 		 * the EOF mark.  Note, cnt has not yet been incremented, so
203 		 * the EOF read does not count as a seek'd block.
204 		 */
205 		t_op.mt_op = MTBSR;
206 		t_op.mt_count = 1;
207 		if (ioctl(out.fd, MTIOCTOP, &t_op) == -1)
208 			err(1, "%s", out.name);
209 
210 		while (cnt++ < out.offset) {
211 			n = write(out.fd, out.db, out.dbsz);
212 			if (n == -1)
213 				err(1, "%s", out.name);
214 			if (n != out.dbsz)
215 				errx(1, "%s: write failure", out.name);
216 		}
217 		break;
218 	}
219 }
220