tcopy.c revision 209570
1/*
2 * Copyright (c) 1985, 1987, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#include <sys/cdefs.h>
35
36__FBSDID("$FreeBSD: head/usr.bin/tcopy/tcopy.c 209570 2010-06-28 12:00:20Z gavin $");
37
38#ifndef lint
39static const char copyright[] =
40"@(#) Copyright (c) 1985, 1987, 1993\n\
41	The Regents of the University of California.  All rights reserved.\n";
42#endif
43
44#ifndef lint
45static const char sccsid[] = "@(#)tcopy.c	8.2 (Berkeley) 4/17/94";
46#endif
47
48#include <sys/types.h>
49#include <sys/stat.h>
50#include <sys/ioctl.h>
51#include <sys/mtio.h>
52
53#include <err.h>
54#include <errno.h>
55#include <fcntl.h>
56#include <paths.h>
57#include <signal.h>
58#include <stdint.h>
59#include <stdio.h>
60#include <stdlib.h>
61#include <string.h>
62#include <unistd.h>
63
64#define	MAXREC	(64 * 1024)
65#define	NOCOUNT	(-2)
66
67int	filen, guesslen, maxblk = MAXREC;
68u_int64_t	lastrec, record, size, tsize;
69FILE	*msg;
70
71void	*getspace(int);
72void	 intr(int);
73static void	 usage(void) __dead2;
74void	 verify(int, int, char *);
75void	 writeop(int, int);
76void	rewind_tape(int);
77
78int
79main(int argc, char *argv[])
80{
81	int lastnread, nread, nw, inp, outp;
82	enum {READ, VERIFY, COPY, COPYVERIFY} op = READ;
83	sig_t oldsig;
84	int ch, needeof;
85	char *buff;
86	const char *inf;
87
88	msg = stdout;
89	guesslen = 1;
90	outp = -1;
91	while ((ch = getopt(argc, argv, "cs:vx")) != -1)
92		switch((char)ch) {
93		case 'c':
94			op = COPYVERIFY;
95			break;
96		case 's':
97			maxblk = atoi(optarg);
98			if (maxblk <= 0) {
99				warnx("illegal block size");
100				usage();
101			}
102			guesslen = 0;
103			break;
104		case 'v':
105			op = VERIFY;
106			break;
107		case 'x':
108			msg = stderr;
109			break;
110		case '?':
111		default:
112			usage();
113		}
114	argc -= optind;
115	argv += optind;
116
117	switch(argc) {
118	case 0:
119		if (op != READ)
120			usage();
121		inf = _PATH_DEFTAPE;
122		break;
123	case 1:
124		if (op != READ)
125			usage();
126		inf = argv[0];
127		break;
128	case 2:
129		if (op == READ)
130			op = COPY;
131		inf = argv[0];
132		if ((outp = open(argv[1], op == VERIFY ? O_RDONLY :
133		    op == COPY ? O_WRONLY : O_RDWR, DEFFILEMODE)) < 0)
134			err(3, "%s", argv[1]);
135		break;
136	default:
137		usage();
138	}
139
140	if ((inp = open(inf, O_RDONLY, 0)) < 0)
141		err(1, "%s", inf);
142
143	buff = getspace(maxblk);
144
145	if (op == VERIFY) {
146		verify(inp, outp, buff);
147		exit(0);
148	}
149
150	if ((oldsig = signal(SIGINT, SIG_IGN)) != SIG_IGN)
151		(void) signal(SIGINT, intr);
152
153	needeof = 0;
154	for (lastnread = NOCOUNT;;) {
155		if ((nread = read(inp, buff, maxblk)) == -1) {
156			while (errno == EINVAL && (maxblk -= 1024)) {
157				nread = read(inp, buff, maxblk);
158				if (nread >= 0)
159					goto r1;
160			}
161			err(1, "read error, file %d, record %ju", filen, (intmax_t)record);
162		} else if (nread != lastnread) {
163			if (lastnread != 0 && lastnread != NOCOUNT) {
164				if (lastrec == 0 && nread == 0)
165					fprintf(msg, "%ju records\n", (intmax_t)record);
166				else if (record - lastrec > 1)
167					fprintf(msg, "records %ju to %ju\n",
168					    (intmax_t)lastrec, (intmax_t)record);
169				else
170					fprintf(msg, "record %ju\n", (intmax_t)lastrec);
171			}
172			if (nread != 0)
173				fprintf(msg, "file %d: block size %d: ",
174				    filen, nread);
175			(void) fflush(stdout);
176			lastrec = record;
177		}
178r1:		guesslen = 0;
179		if (nread > 0) {
180			if (op == COPY || op == COPYVERIFY) {
181				if (needeof) {
182					writeop(outp, MTWEOF);
183					needeof = 0;
184				}
185				nw = write(outp, buff, nread);
186				if (nw != nread) {
187					if (nw == -1) {
188						warn("write error, file %d, record %ju", filen,
189						    (intmax_t)record);
190					} else {
191						warnx("write error, file %d, record %ju", filen,
192						    (intmax_t)record);
193						warnx("write (%d) != read (%d)", nw, nread);
194					}
195					errx(5, "copy aborted");
196				}
197			}
198			size += nread;
199			record++;
200		} else {
201			if (lastnread <= 0 && lastnread != NOCOUNT) {
202				fprintf(msg, "eot\n");
203				break;
204			}
205			fprintf(msg,
206			    "file %d: eof after %ju records: %ju bytes\n",
207			    filen, (intmax_t)record, (intmax_t)size);
208			needeof = 1;
209			filen++;
210			tsize += size;
211			size = record = lastrec = 0;
212			lastnread = 0;
213		}
214		lastnread = nread;
215	}
216	fprintf(msg, "total length: %ju bytes\n", (intmax_t)tsize);
217	(void)signal(SIGINT, oldsig);
218	if (op == COPY || op == COPYVERIFY) {
219		writeop(outp, MTWEOF);
220		writeop(outp, MTWEOF);
221		if (op == COPYVERIFY) {
222			rewind_tape(outp);
223			rewind_tape(inp);
224			verify(inp, outp, buff);
225		}
226	}
227	exit(0);
228}
229
230void
231verify(int inp, int outp, char *outb)
232{
233	int eot, inmaxblk, inn, outmaxblk, outn;
234	char *inb;
235
236	inb = getspace(maxblk);
237	inmaxblk = outmaxblk = maxblk;
238	for (eot = 0;; guesslen = 0) {
239		if ((inn = read(inp, inb, inmaxblk)) == -1) {
240			if (guesslen)
241				while (errno == EINVAL && (inmaxblk -= 1024)) {
242					inn = read(inp, inb, inmaxblk);
243					if (inn >= 0)
244						goto r1;
245				}
246			warn("read error");
247			break;
248		}
249r1:		if ((outn = read(outp, outb, outmaxblk)) == -1) {
250			if (guesslen)
251				while (errno == EINVAL && (outmaxblk -= 1024)) {
252					outn = read(outp, outb, outmaxblk);
253					if (outn >= 0)
254						goto r2;
255				}
256			warn("read error");
257			break;
258		}
259r2:		if (inn != outn) {
260			fprintf(msg,
261			    "%s: tapes have different block sizes; %d != %d.\n",
262			    "tcopy", inn, outn);
263			break;
264		}
265		if (!inn) {
266			if (eot++) {
267				fprintf(msg, "tcopy: tapes are identical.\n");
268				return;
269			}
270		} else {
271			if (bcmp(inb, outb, inn)) {
272				fprintf(msg,
273				    "tcopy: tapes have different data.\n");
274				break;
275			}
276			eot = 0;
277		}
278	}
279	exit(1);
280}
281
282void
283intr(int signo __unused)
284{
285	if (record) {
286		if (record - lastrec > 1)
287			fprintf(msg, "records %ju to %ju\n", (intmax_t)lastrec, (intmax_t)record);
288		else
289			fprintf(msg, "record %ju\n", (intmax_t)lastrec);
290	}
291	fprintf(msg, "interrupt at file %d: record %ju\n", filen, (intmax_t)record);
292	fprintf(msg, "total length: %ju bytes\n", (uintmax_t)(tsize + size));
293	exit(1);
294}
295
296void *
297getspace(int blk)
298{
299	void *bp;
300
301	if ((bp = malloc((size_t)blk)) == NULL)
302		errx(11, "no memory");
303	return (bp);
304}
305
306void
307writeop(int fd, int type)
308{
309	struct mtop op;
310
311	op.mt_op = type;
312	op.mt_count = (daddr_t)1;
313	if (ioctl(fd, MTIOCTOP, (char *)&op) < 0)
314		err(6, "tape op");
315}
316
317static void
318usage(void)
319{
320	fprintf(stderr, "usage: tcopy [-cvx] [-s maxblk] [src [dest]]\n");
321	exit(1);
322}
323
324void
325rewind_tape(int fd)
326{
327	struct stat sp;
328
329	if(fstat(fd, &sp))
330		errx(12, "fstat in rewind");
331
332	/*
333	 * don't want to do tape ioctl on regular files:
334	 */
335	if( S_ISREG(sp.st_mode) ) {
336		if( lseek(fd, 0, SEEK_SET) == -1 )
337			errx(13, "lseek");
338	} else
339		/*  assume its a tape	*/
340		writeop(fd, MTREW);
341}
342