Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.bin / tcopy / tcopy.c
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 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1985, 1987, 1993\n\
37         The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)tcopy.c     8.2 (Berkeley) 4/17/94";
43 #endif
44 static const char rcsid[] =
45   "$FreeBSD: src/usr.bin/tcopy/tcopy.c,v 1.7.2.1 2002/11/07 17:54:42 imp Exp $";
46 #endif /* not lint */
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 <signal.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <unistd.h>
61
62 #include "pathnames.h"
63
64 #define MAXREC  (64 * 1024)
65 #define NOCOUNT (-2)
66
67 int     filen, guesslen, maxblk = MAXREC;
68 u_int64_t       lastrec, record, size, tsize;
69 FILE    *msg;
70
71 void    *getspace __P((int));
72 void     intr __P((int));
73 static void      usage __P((void));
74 void     verify __P((int, int, char *));
75 void     writeop __P((int, int));
76 void    rewind_tape(int);
77
78 int
79 main(argc, argv)
80         int argc;
81         char *argv[];
82 {
83         register int lastnread, nread, nw, inp, outp;
84         enum {READ, VERIFY, COPY, COPYVERIFY} op = READ;
85         sig_t oldsig;
86         int ch, needeof;
87         char *buff, *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                 }
178 r1:             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
228 void
229 verify(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                 }
249 r1:             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                 }
259 r2:             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
282 void
283 intr(signo)
284         int signo;
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: %ld bytes\n", tsize + size);
294         exit(1);
295 }
296
297 void *
298 getspace(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
308 void
309 writeop(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
320 static void
321 usage()
322 {
323         fprintf(stderr, "usage: tcopy [-cvx] [-s maxblk] [src [dest]]\n");
324         exit(1);
325 }
326
327 void
328 rewind_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 }