tcopy.c revision 92922
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 92922 2002-03-22 01:42:45Z imp $");
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 <stdio.h>
59#include <stdlib.h>
60#include <string.h>
61#include <unistd.h>
62
63#define	MAXREC	(64 * 1024)
64#define	NOCOUNT	(-2)
65
66int	filen, guesslen, maxblk = MAXREC;
67u_int64_t	lastrec, record, size, tsize;
68FILE	*msg;
69
70void	*getspace(int);
71void	 intr(int);
72static void	 usage(void);
73void	 verify(int, int, char *);
74void	 writeop(int, int);
75void	rewind_tape(int);
76
77int
78main(argc, argv)
79	int argc;
80	char *argv[];
81{
82	register int lastnread, nread, nw, inp, outp;
83	enum {READ, VERIFY, COPY, COPYVERIFY} op = READ;
84	sig_t oldsig;
85	int ch, needeof;
86	char *buff;
87	const char *inf;
88
89	msg = stdout;
90	guesslen = 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 %qu", filen, record);
162		} else if (nread != lastnread) {
163			if (lastnread != 0 && lastnread != NOCOUNT) {
164				if (lastrec == 0 && nread == 0)
165					fprintf(msg, "%qu records\n", record);
166				else if (record - lastrec > 1)
167					fprintf(msg, "records %qu to %qu\n",
168					    lastrec, record);
169				else
170					fprintf(msg, "record %qu\n", 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 %qu", filen, record);
189					} else {
190					warnx("write error, file %d, record %qu", filen, record);
191					warnx("write (%d) != read (%d)", nw, nread);
192					}
193					errx(5, "copy aborted");
194				}
195			}
196			size += nread;
197			record++;
198		} else {
199			if (lastnread <= 0 && lastnread != NOCOUNT) {
200				fprintf(msg, "eot\n");
201				break;
202			}
203			fprintf(msg,
204			    "file %d: eof after %qu records: %qu bytes\n",
205			    filen, record, size);
206			needeof = 1;
207			filen++;
208			tsize += size;
209			size = record = lastrec = 0;
210			lastnread = 0;
211		}
212		lastnread = nread;
213	}
214	fprintf(msg, "total length: %qu bytes\n", tsize);
215	(void)signal(SIGINT, oldsig);
216	if (op == COPY || op == COPYVERIFY) {
217		writeop(outp, MTWEOF);
218		writeop(outp, MTWEOF);
219		if (op == COPYVERIFY) {
220			rewind_tape(outp);
221			rewind_tape(inp);
222			verify(inp, outp, buff);
223		}
224	}
225	exit(0);
226}
227
228void
229verify(inp, outp, outb)
230	register int inp, outp;
231	register char *outb;
232{
233	register int eot, inmaxblk, inn, outmaxblk, outn;
234	register 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(signo)
284	int signo __unused;
285{
286	if (record) {
287		if (record - lastrec > 1)
288			fprintf(msg, "records %qu to %qu\n", lastrec, record);
289		else
290			fprintf(msg, "record %qu\n", lastrec);
291	}
292	fprintf(msg, "interrupt at file %d: record %qu\n", filen, record);
293	fprintf(msg, "total length: %ju bytes\n", (uintmax_t)(tsize + size));
294	exit(1);
295}
296
297void *
298getspace(blk)
299	int blk;
300{
301	void *bp;
302
303	if ((bp = malloc((size_t)blk)) == NULL)
304		errx(11, "no memory");
305	return (bp);
306}
307
308void
309writeop(fd, type)
310	int fd, type;
311{
312	struct mtop op;
313
314	op.mt_op = type;
315	op.mt_count = (daddr_t)1;
316	if (ioctl(fd, MTIOCTOP, (char *)&op) < 0)
317		err(6, "tape op");
318}
319
320static void
321usage()
322{
323	fprintf(stderr, "usage: tcopy [-cvx] [-s maxblk] [src [dest]]\n");
324	exit(1);
325}
326
327void
328rewind_tape(int fd)
329{
330	struct stat sp;
331
332	if(fstat(fd, &sp))
333		errx(12, "fstat in rewind");
334
335	/*
336	 * don't want to do tape ioctl on regular files:
337	 */
338	if( S_ISREG(sp.st_mode) ) {
339		if( lseek(fd, 0, SEEK_SET) == -1 )
340			errx(13, "lseek");
341	} else
342		/*  assume its a tape	*/
343		writeop(fd, MTREW);
344}
345