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