Commit | Line | Data |
---|---|---|
169d8a33 RG |
1 | /* |
2 | * Copyright (c) UNIX System Laboratories, Inc. All or some portions | |
3 | * of this file are derived from material licensed to the | |
4 | * University of California by American Telephone and Telegraph Co. | |
5 | * or UNIX System Laboratories, Inc. and are reproduced herein with | |
6 | * the permission of UNIX System Laboratories, Inc. | |
7 | * | |
8 | * $Id$ | |
9 | */ | |
15637ed4 RG |
10 | /* |
11 | * Copyright (c) 1983 The Regents of the University of California. | |
12 | * All rights reserved. | |
13 | * | |
14 | * Redistribution and use in source and binary forms, with or without | |
15 | * modification, are permitted provided that the following conditions | |
16 | * are met: | |
17 | * 1. Redistributions of source code must retain the above copyright | |
18 | * notice, this list of conditions and the following disclaimer. | |
19 | * 2. Redistributions in binary form must reproduce the above copyright | |
20 | * notice, this list of conditions and the following disclaimer in the | |
21 | * documentation and/or other materials provided with the distribution. | |
22 | * 3. All advertising materials mentioning features or use of this software | |
23 | * must display the following acknowledgement: | |
24 | * This product includes software developed by the University of | |
25 | * California, Berkeley and its contributors. | |
26 | * 4. Neither the name of the University nor the names of its contributors | |
27 | * may be used to endorse or promote products derived from this software | |
28 | * without specific prior written permission. | |
29 | * | |
30 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
31 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
32 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
33 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
34 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
35 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
36 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
37 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
38 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
39 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
40 | * SUCH DAMAGE. | |
41 | */ | |
42 | ||
43 | #ifndef lint | |
44 | static char sccsid[] = "@(#)tape.c 5.21 (Berkeley) 2/22/91"; | |
45 | #endif /* not lint */ | |
46 | ||
47 | #include "restore.h" | |
48 | #include <protocols/dumprestore.h> | |
49 | #include <sys/ioctl.h> | |
50 | #include <sys/mtio.h> | |
51 | #include <sys/file.h> | |
52 | #include <setjmp.h> | |
53 | #include <sys/stat.h> | |
54 | #include "pathnames.h" | |
55 | ||
56 | static long fssize = MAXBSIZE; | |
57 | static int mt = -1; | |
58 | static int pipein = 0; | |
59 | static char magtape[BUFSIZ]; | |
60 | static int bct; | |
61 | static int numtrec; | |
62 | static char *tbf; | |
63 | static union u_spcl endoftapemark; | |
64 | static long blksread; | |
65 | static long tapesread; | |
66 | static jmp_buf restart; | |
67 | static int gettingfile = 0; /* restart has a valid frame */ | |
68 | ||
69 | static int ofile; | |
70 | static char *map; | |
71 | static char lnkbuf[MAXPATHLEN + 1]; | |
72 | static int pathlen; | |
73 | ||
74 | int Bcvt; /* Swap Bytes (for CCI or sun) */ | |
75 | static int Qcvt; /* Swap quads (for sun) */ | |
76 | u_long swabl(); | |
77 | /* | |
78 | * Set up an input source | |
79 | */ | |
80 | setinput(source) | |
81 | char *source; | |
82 | { | |
83 | extern int errno; | |
84 | #ifdef RRESTORE | |
85 | char *host, *tape; | |
2fbce9b3 | 86 | char *ruser; |
15637ed4 RG |
87 | #endif RRESTORE |
88 | char *strerror(); | |
89 | ||
90 | flsht(); | |
91 | if (bflag) | |
92 | newtapebuf(ntrec); | |
93 | else | |
94 | newtapebuf(NTREC > HIGHDENSITYTREC ? NTREC : HIGHDENSITYTREC); | |
95 | terminal = stdin; | |
96 | #ifdef RRESTORE | |
97 | host = source; | |
98 | tape = index(host, ':'); | |
2fbce9b3 JH |
99 | if((host = index(source, '@'))) |
100 | { | |
101 | *host++ = '\0'; | |
102 | ruser = source; | |
103 | } | |
104 | else | |
105 | { | |
106 | host = source; | |
107 | ruser = NULL; | |
108 | } | |
15637ed4 RG |
109 | if (tape == 0) { |
110 | nohost: | |
111 | msg("need keyletter ``f'' and device ``host:tape''\n"); | |
112 | done(1); | |
113 | } | |
114 | *tape++ = '\0'; | |
115 | (void) strcpy(magtape, tape); | |
2fbce9b3 | 116 | if (rmthost(host, ruser) == 0) |
15637ed4 RG |
117 | done(1); |
118 | setuid(getuid()); /* no longer need or want root privileges */ | |
119 | #else | |
120 | if (strcmp(source, "-") == 0) { | |
121 | /* | |
122 | * Since input is coming from a pipe we must establish | |
123 | * our own connection to the terminal. | |
124 | */ | |
125 | terminal = fopen(_PATH_TTY, "r"); | |
126 | if (terminal == NULL) { | |
127 | (void)fprintf(stderr, "Cannot open %s: %s\n", | |
128 | _PATH_TTY, strerror(errno)); | |
129 | terminal = fopen(_PATH_DEVNULL, "r"); | |
130 | if (terminal == NULL) { | |
131 | (void)fprintf(stderr, "Cannot open %s: %s\n", | |
132 | _PATH_DEVNULL, strerror(errno)); | |
133 | done(1); | |
134 | } | |
135 | } | |
136 | pipein++; | |
137 | } | |
138 | (void) strcpy(magtape, source); | |
139 | #endif RRESTORE | |
140 | } | |
141 | ||
142 | newtapebuf(size) | |
143 | long size; | |
144 | { | |
145 | static tbfsize = -1; | |
146 | ||
147 | ntrec = size; | |
148 | if (size <= tbfsize) | |
149 | return; | |
150 | if (tbf != NULL) | |
151 | free(tbf); | |
152 | tbf = (char *)malloc(size * TP_BSIZE); | |
153 | if (tbf == NULL) { | |
154 | fprintf(stderr, "Cannot allocate space for tape buffer\n"); | |
155 | done(1); | |
156 | } | |
157 | tbfsize = size; | |
158 | } | |
159 | ||
160 | /* | |
161 | * Verify that the tape drive can be accessed and | |
162 | * that it actually is a dump tape. | |
163 | */ | |
164 | setup() | |
165 | { | |
166 | int i, j, *ip; | |
167 | struct stat stbuf; | |
168 | extern int xtrmap(), xtrmapskip(); | |
169 | ||
170 | vprintf(stdout, "Verify tape and initialize maps\n"); | |
171 | #ifdef RRESTORE | |
172 | if ((mt = rmtopen(magtape, 0)) < 0) | |
173 | #else | |
174 | if (pipein) | |
175 | mt = 0; | |
176 | else if ((mt = open(magtape, 0)) < 0) | |
177 | #endif | |
178 | { | |
179 | perror(magtape); | |
180 | done(1); | |
181 | } | |
182 | volno = 1; | |
183 | setdumpnum(); | |
184 | flsht(); | |
185 | if (!pipein && !bflag) | |
186 | findtapeblksize(); | |
187 | if (gethead(&spcl) == FAIL) { | |
188 | bct--; /* push back this block */ | |
189 | cvtflag++; | |
190 | if (gethead(&spcl) == FAIL) { | |
191 | fprintf(stderr, "Tape is not a dump tape\n"); | |
192 | done(1); | |
193 | } | |
194 | fprintf(stderr, "Converting to new file system format.\n"); | |
195 | } | |
196 | if (pipein) { | |
197 | endoftapemark.s_spcl.c_magic = cvtflag ? OFS_MAGIC : NFS_MAGIC; | |
198 | endoftapemark.s_spcl.c_type = TS_END; | |
199 | ip = (int *)&endoftapemark; | |
200 | j = sizeof(union u_spcl) / sizeof(int); | |
201 | i = 0; | |
202 | do | |
203 | i += *ip++; | |
204 | while (--j); | |
205 | endoftapemark.s_spcl.c_checksum = CHECKSUM - i; | |
206 | } | |
207 | if (vflag || command == 't') | |
208 | printdumpinfo(); | |
209 | dumptime = spcl.c_ddate; | |
210 | dumpdate = spcl.c_date; | |
211 | if (stat(".", &stbuf) < 0) { | |
212 | perror("cannot stat ."); | |
213 | done(1); | |
214 | } | |
215 | if (stbuf.st_blksize > 0 && stbuf.st_blksize <= MAXBSIZE) | |
216 | fssize = stbuf.st_blksize; | |
217 | if (((fssize - 1) & fssize) != 0) { | |
218 | fprintf(stderr, "bad block size %d\n", fssize); | |
219 | done(1); | |
220 | } | |
221 | if (checkvol(&spcl, (long)1) == FAIL) { | |
222 | fprintf(stderr, "Tape is not volume 1 of the dump\n"); | |
223 | done(1); | |
224 | } | |
225 | if (readhdr(&spcl) == FAIL) | |
226 | panic("no header after volume mark!\n"); | |
227 | findinode(&spcl); | |
228 | if (checktype(&spcl, TS_CLRI) == FAIL) { | |
229 | fprintf(stderr, "Cannot find file removal list\n"); | |
230 | done(1); | |
231 | } | |
232 | maxino = (spcl.c_count * TP_BSIZE * NBBY) + 1; | |
233 | dprintf(stdout, "maxino = %d\n", maxino); | |
234 | map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY)); | |
235 | if (map == (char *)NIL) | |
236 | panic("no memory for file removal list\n"); | |
237 | clrimap = map; | |
238 | curfile.action = USING; | |
239 | getfile(xtrmap, xtrmapskip); | |
240 | if (checktype(&spcl, TS_BITS) == FAIL) { | |
241 | fprintf(stderr, "Cannot find file dump list\n"); | |
242 | done(1); | |
243 | } | |
244 | map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY)); | |
245 | if (map == (char *)NULL) | |
246 | panic("no memory for file dump list\n"); | |
247 | dumpmap = map; | |
248 | curfile.action = USING; | |
249 | getfile(xtrmap, xtrmapskip); | |
250 | } | |
251 | ||
252 | /* | |
253 | * Prompt user to load a new dump volume. | |
254 | * "Nextvol" is the next suggested volume to use. | |
255 | * This suggested volume is enforced when doing full | |
256 | * or incremental restores, but can be overrridden by | |
257 | * the user when only extracting a subset of the files. | |
258 | */ | |
259 | getvol(nextvol) | |
260 | long nextvol; | |
261 | { | |
262 | long newvol; | |
263 | long savecnt, i; | |
264 | union u_spcl tmpspcl; | |
265 | # define tmpbuf tmpspcl.s_spcl | |
266 | char buf[TP_BSIZE]; | |
267 | extern char *ctime(); | |
268 | ||
269 | if (nextvol == 1) { | |
270 | tapesread = 0; | |
271 | gettingfile = 0; | |
272 | } | |
273 | if (pipein) { | |
274 | if (nextvol != 1) | |
275 | panic("Changing volumes on pipe input?\n"); | |
276 | if (volno == 1) | |
277 | return; | |
278 | goto gethdr; | |
279 | } | |
280 | savecnt = blksread; | |
281 | again: | |
282 | if (pipein) | |
283 | done(1); /* pipes do not get a second chance */ | |
284 | if (command == 'R' || command == 'r' || curfile.action != SKIP) | |
285 | newvol = nextvol; | |
286 | else | |
287 | newvol = 0; | |
288 | while (newvol <= 0) { | |
289 | if (tapesread == 0) { | |
290 | fprintf(stderr, "%s%s%s%s%s", | |
291 | "You have not read any tapes yet.\n", | |
292 | "Unless you know which volume your", | |
293 | " file(s) are on you should start\n", | |
294 | "with the last volume and work", | |
295 | " towards towards the first.\n"); | |
296 | } else { | |
297 | fprintf(stderr, "You have read volumes"); | |
298 | strcpy(tbf, ": "); | |
299 | for (i = 1; i < 32; i++) | |
300 | if (tapesread & (1 << i)) { | |
301 | fprintf(stderr, "%s%d", tbf, i); | |
302 | strcpy(tbf, ", "); | |
303 | } | |
304 | fprintf(stderr, "\n"); | |
305 | } | |
306 | do { | |
307 | fprintf(stderr, "Specify next volume #: "); | |
308 | (void) fflush(stderr); | |
309 | (void) fgets(tbf, BUFSIZ, terminal); | |
310 | } while (!feof(terminal) && tbf[0] == '\n'); | |
311 | if (feof(terminal)) | |
312 | done(1); | |
313 | newvol = atoi(tbf); | |
314 | if (newvol <= 0) { | |
315 | fprintf(stderr, | |
316 | "Volume numbers are positive numerics\n"); | |
317 | } | |
318 | } | |
319 | if (newvol == volno) { | |
320 | tapesread |= 1 << volno; | |
321 | return; | |
322 | } | |
323 | closemt(); | |
324 | fprintf(stderr, "Mount tape volume %d\n", newvol); | |
325 | fprintf(stderr, "Enter ``none'' if there are no more tapes\n"); | |
326 | fprintf(stderr, "otherwise enter tape name (default: %s) ", magtape); | |
327 | (void) fflush(stderr); | |
328 | (void) fgets(tbf, BUFSIZ, terminal); | |
329 | if (feof(terminal)) | |
330 | done(1); | |
331 | if (!strcmp(tbf, "none\n")) { | |
332 | curfile.name = "<name unknown>"; | |
333 | curfile.action = UNKNOWN; | |
334 | curfile.dip = (struct dinode *)NIL; | |
335 | curfile.ino = maxino; | |
336 | if (gettingfile) { | |
337 | gettingfile = 0; | |
338 | longjmp(restart, 1); | |
339 | } | |
340 | } | |
341 | if (tbf[0] != '\n') { | |
342 | (void) strcpy(magtape, tbf); | |
343 | magtape[strlen(magtape) - 1] = '\0'; | |
344 | } | |
345 | #ifdef RRESTORE | |
346 | if ((mt = rmtopen(magtape, 0)) == -1) | |
347 | #else | |
348 | if ((mt = open(magtape, 0)) == -1) | |
349 | #endif | |
350 | { | |
351 | fprintf(stderr, "Cannot open %s\n", magtape); | |
352 | volno = -1; | |
353 | goto again; | |
354 | } | |
355 | gethdr: | |
356 | volno = newvol; | |
357 | setdumpnum(); | |
358 | flsht(); | |
359 | if (readhdr(&tmpbuf) == FAIL) { | |
360 | fprintf(stderr, "tape is not dump tape\n"); | |
361 | volno = 0; | |
362 | goto again; | |
363 | } | |
364 | if (checkvol(&tmpbuf, volno) == FAIL) { | |
365 | fprintf(stderr, "Wrong volume (%d)\n", tmpbuf.c_volume); | |
366 | volno = 0; | |
367 | goto again; | |
368 | } | |
369 | if (tmpbuf.c_date != dumpdate || tmpbuf.c_ddate != dumptime) { | |
370 | fprintf(stderr, "Wrong dump date\n\tgot: %s", | |
371 | ctime(&tmpbuf.c_date)); | |
372 | fprintf(stderr, "\twanted: %s", ctime(&dumpdate)); | |
373 | volno = 0; | |
374 | goto again; | |
375 | } | |
376 | tapesread |= 1 << volno; | |
377 | blksread = savecnt; | |
378 | if (curfile.action == USING) { | |
379 | if (volno == 1) | |
380 | panic("active file into volume 1\n"); | |
381 | return; | |
382 | } | |
383 | /* | |
384 | * Skip up to the beginning of the next record | |
385 | */ | |
386 | if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER)) | |
387 | for (i = tmpbuf.c_count; i > 0; i--) | |
388 | readtape(buf); | |
389 | (void) gethead(&spcl); | |
390 | findinode(&spcl); | |
391 | if (gettingfile) { | |
392 | gettingfile = 0; | |
393 | longjmp(restart, 1); | |
394 | } | |
395 | } | |
396 | ||
397 | /* | |
398 | * handle multiple dumps per tape by skipping forward to the | |
399 | * appropriate one. | |
400 | */ | |
401 | setdumpnum() | |
402 | { | |
403 | struct mtop tcom; | |
404 | ||
405 | if (dumpnum == 1 || volno != 1) | |
406 | return; | |
407 | if (pipein) { | |
408 | fprintf(stderr, "Cannot have multiple dumps on pipe input\n"); | |
409 | done(1); | |
410 | } | |
411 | tcom.mt_op = MTFSF; | |
412 | tcom.mt_count = dumpnum - 1; | |
413 | #ifdef RRESTORE | |
414 | rmtioctl(MTFSF, dumpnum - 1); | |
415 | #else | |
416 | if (ioctl(mt, (int)MTIOCTOP, (char *)&tcom) < 0) | |
417 | perror("ioctl MTFSF"); | |
418 | #endif | |
419 | } | |
420 | ||
421 | printdumpinfo() | |
422 | { | |
423 | extern char *ctime(); | |
424 | ||
425 | fprintf(stdout, "Dump date: %s", ctime(&spcl.c_date)); | |
426 | fprintf(stdout, "Dumped from: %s", | |
427 | (spcl.c_ddate == 0) ? "the epoch\n" : ctime(&spcl.c_ddate)); | |
428 | if (spcl.c_host[0] == '\0') | |
429 | return; | |
430 | fprintf(stderr, "Level %d dump of %s on %s:%s\n", | |
431 | spcl.c_level, spcl.c_filesys, spcl.c_host, spcl.c_dev); | |
432 | fprintf(stderr, "Label: %s\n", spcl.c_label); | |
433 | } | |
434 | ||
435 | extractfile(name) | |
436 | char *name; | |
437 | { | |
438 | int mode; | |
439 | struct timeval timep[2]; | |
440 | struct entry *ep; | |
441 | extern int xtrlnkfile(), xtrlnkskip(); | |
442 | extern int xtrfile(), xtrskip(); | |
443 | ||
444 | curfile.name = name; | |
445 | curfile.action = USING; | |
446 | timep[0].tv_sec = curfile.dip->di_atime; | |
447 | timep[0].tv_usec = 0; | |
448 | timep[1].tv_sec = curfile.dip->di_mtime; | |
449 | timep[1].tv_usec = 0; | |
450 | mode = curfile.dip->di_mode; | |
451 | switch (mode & IFMT) { | |
452 | ||
453 | default: | |
454 | fprintf(stderr, "%s: unknown file mode 0%o\n", name, mode); | |
455 | skipfile(); | |
456 | return (FAIL); | |
457 | ||
458 | case IFSOCK: | |
459 | vprintf(stdout, "skipped socket %s\n", name); | |
460 | skipfile(); | |
461 | return (GOOD); | |
462 | ||
463 | case IFDIR: | |
464 | if (mflag) { | |
465 | ep = lookupname(name); | |
466 | if (ep == NIL || ep->e_flags & EXTRACT) | |
467 | panic("unextracted directory %s\n", name); | |
468 | skipfile(); | |
469 | return (GOOD); | |
470 | } | |
471 | vprintf(stdout, "extract file %s\n", name); | |
472 | return (genliteraldir(name, curfile.ino)); | |
473 | ||
474 | case IFLNK: | |
475 | lnkbuf[0] = '\0'; | |
476 | pathlen = 0; | |
477 | getfile(xtrlnkfile, xtrlnkskip); | |
478 | if (pathlen == 0) { | |
479 | vprintf(stdout, | |
480 | "%s: zero length symbolic link (ignored)\n", name); | |
481 | return (GOOD); | |
482 | } | |
483 | return (linkit(lnkbuf, name, SYMLINK)); | |
484 | ||
485 | case IFCHR: | |
486 | case IFBLK: | |
487 | vprintf(stdout, "extract special file %s\n", name); | |
488 | if (Nflag) { | |
489 | skipfile(); | |
490 | return (GOOD); | |
491 | } | |
492 | if (mknod(name, mode, (int)curfile.dip->di_rdev) < 0) { | |
493 | fprintf(stderr, "%s: ", name); | |
494 | (void) fflush(stderr); | |
495 | perror("cannot create special file"); | |
496 | skipfile(); | |
497 | return (FAIL); | |
498 | } | |
499 | (void) chown(name, curfile.dip->di_uid, curfile.dip->di_gid); | |
500 | (void) chmod(name, mode); | |
501 | skipfile(); | |
502 | utimes(name, timep); | |
503 | return (GOOD); | |
504 | ||
505 | case IFREG: | |
506 | vprintf(stdout, "extract file %s\n", name); | |
507 | if (Nflag) { | |
508 | skipfile(); | |
509 | return (GOOD); | |
510 | } | |
511 | if ((ofile = creat(name, 0666)) < 0) { | |
512 | fprintf(stderr, "%s: ", name); | |
513 | (void) fflush(stderr); | |
514 | perror("cannot create file"); | |
515 | skipfile(); | |
516 | return (FAIL); | |
517 | } | |
518 | (void) fchown(ofile, curfile.dip->di_uid, curfile.dip->di_gid); | |
519 | (void) fchmod(ofile, mode); | |
520 | getfile(xtrfile, xtrskip); | |
521 | (void) close(ofile); | |
522 | utimes(name, timep); | |
523 | return (GOOD); | |
524 | } | |
525 | /* NOTREACHED */ | |
526 | } | |
527 | ||
528 | /* | |
529 | * skip over bit maps on the tape | |
530 | */ | |
531 | skipmaps() | |
532 | { | |
533 | ||
534 | while (checktype(&spcl, TS_CLRI) == GOOD || | |
535 | checktype(&spcl, TS_BITS) == GOOD) | |
536 | skipfile(); | |
537 | } | |
538 | ||
539 | /* | |
540 | * skip over a file on the tape | |
541 | */ | |
542 | skipfile() | |
543 | { | |
544 | extern int null(); | |
545 | ||
546 | curfile.action = SKIP; | |
547 | getfile(null, null); | |
548 | } | |
549 | ||
550 | /* | |
551 | * Do the file extraction, calling the supplied functions | |
552 | * with the blocks | |
553 | */ | |
554 | getfile(f1, f2) | |
555 | int (*f2)(), (*f1)(); | |
556 | { | |
557 | register int i; | |
558 | int curblk = 0; | |
559 | off_t size = spcl.c_dinode.di_size; | |
560 | static char clearedbuf[MAXBSIZE]; | |
561 | char buf[MAXBSIZE / TP_BSIZE][TP_BSIZE]; | |
562 | char junk[TP_BSIZE]; | |
563 | ||
564 | if (checktype(&spcl, TS_END) == GOOD) | |
565 | panic("ran off end of tape\n"); | |
566 | if (ishead(&spcl) == FAIL) | |
567 | panic("not at beginning of a file\n"); | |
568 | if (!gettingfile && setjmp(restart) != 0) | |
569 | return; | |
570 | gettingfile++; | |
571 | loop: | |
572 | for (i = 0; i < spcl.c_count; i++) { | |
573 | if (spcl.c_addr[i]) { | |
574 | readtape(&buf[curblk++][0]); | |
575 | if (curblk == fssize / TP_BSIZE) { | |
576 | (*f1)(buf, size > TP_BSIZE ? | |
577 | (long) (fssize) : | |
578 | (curblk - 1) * TP_BSIZE + size); | |
579 | curblk = 0; | |
580 | } | |
581 | } else { | |
582 | if (curblk > 0) { | |
583 | (*f1)(buf, size > TP_BSIZE ? | |
584 | (long) (curblk * TP_BSIZE) : | |
585 | (curblk - 1) * TP_BSIZE + size); | |
586 | curblk = 0; | |
587 | } | |
588 | (*f2)(clearedbuf, size > TP_BSIZE ? | |
589 | (long) TP_BSIZE : size); | |
590 | } | |
591 | if ((size -= TP_BSIZE) <= 0) { | |
592 | for (i++; i < spcl.c_count; i++) | |
593 | if (spcl.c_addr[i]) | |
594 | readtape(junk); | |
595 | break; | |
596 | } | |
597 | } | |
598 | if (readhdr(&spcl) == GOOD && size > 0) { | |
599 | if (checktype(&spcl, TS_ADDR) == GOOD) | |
600 | goto loop; | |
601 | dprintf(stdout, "Missing address (header) block for %s\n", | |
602 | curfile.name); | |
603 | } | |
604 | if (curblk > 0) | |
605 | (*f1)(buf, (curblk * TP_BSIZE) + size); | |
606 | findinode(&spcl); | |
607 | gettingfile = 0; | |
608 | } | |
609 | ||
610 | /* | |
611 | * The next routines are called during file extraction to | |
612 | * put the data into the right form and place. | |
613 | */ | |
614 | xtrfile(buf, size) | |
615 | char *buf; | |
616 | long size; | |
617 | { | |
618 | ||
619 | if (Nflag) | |
620 | return; | |
621 | if (write(ofile, buf, (int) size) == -1) { | |
622 | fprintf(stderr, "write error extracting inode %d, name %s\n", | |
623 | curfile.ino, curfile.name); | |
624 | perror("write"); | |
625 | done(1); | |
626 | } | |
627 | } | |
628 | ||
629 | xtrskip(buf, size) | |
630 | char *buf; | |
631 | long size; | |
632 | { | |
633 | ||
634 | #ifdef lint | |
635 | buf = buf; | |
636 | #endif | |
637 | if (lseek(ofile, size, 1) == (long)-1) { | |
638 | fprintf(stderr, "seek error extracting inode %d, name %s\n", | |
639 | curfile.ino, curfile.name); | |
640 | perror("lseek"); | |
641 | done(1); | |
642 | } | |
643 | } | |
644 | ||
645 | xtrlnkfile(buf, size) | |
646 | char *buf; | |
647 | long size; | |
648 | { | |
649 | ||
650 | pathlen += size; | |
651 | if (pathlen > MAXPATHLEN) { | |
652 | fprintf(stderr, "symbolic link name: %s->%s%s; too long %d\n", | |
653 | curfile.name, lnkbuf, buf, pathlen); | |
654 | done(1); | |
655 | } | |
656 | (void) strcat(lnkbuf, buf); | |
657 | } | |
658 | ||
659 | xtrlnkskip(buf, size) | |
660 | char *buf; | |
661 | long size; | |
662 | { | |
663 | ||
664 | #ifdef lint | |
665 | buf = buf, size = size; | |
666 | #endif | |
667 | fprintf(stderr, "unallocated block in symbolic link %s\n", | |
668 | curfile.name); | |
669 | done(1); | |
670 | } | |
671 | ||
672 | xtrmap(buf, size) | |
673 | char *buf; | |
674 | long size; | |
675 | { | |
676 | ||
677 | bcopy(buf, map, size); | |
678 | map += size; | |
679 | } | |
680 | ||
681 | xtrmapskip(buf, size) | |
682 | char *buf; | |
683 | long size; | |
684 | { | |
685 | ||
686 | #ifdef lint | |
687 | buf = buf; | |
688 | #endif | |
689 | panic("hole in map\n"); | |
690 | map += size; | |
691 | } | |
692 | ||
693 | null() {;} | |
694 | ||
695 | /* | |
696 | * Do the tape i/o, dealing with volume changes | |
697 | * etc.. | |
698 | */ | |
699 | readtape(b) | |
700 | char *b; | |
701 | { | |
702 | register long i; | |
703 | long rd, newvol; | |
704 | int cnt; | |
705 | ||
706 | top: | |
707 | if (bct < numtrec) { | |
708 | bcopy(&tbf[(bct++*TP_BSIZE)], b, (long)TP_BSIZE); | |
709 | blksread++; | |
710 | return; | |
711 | } | |
712 | for (i = 0; i < ntrec; i++) | |
713 | ((struct s_spcl *)&tbf[i*TP_BSIZE])->c_magic = 0; | |
714 | if (numtrec == 0) | |
715 | numtrec = ntrec; | |
716 | cnt = ntrec*TP_BSIZE; | |
717 | rd = 0; | |
718 | getmore: | |
719 | #ifdef RRESTORE | |
720 | i = rmtread(&tbf[rd], cnt); | |
721 | #else | |
722 | i = read(mt, &tbf[rd], cnt); | |
723 | #endif | |
724 | /* | |
725 | * Check for mid-tape short read error. | |
726 | * If found, return rest of buffer. | |
727 | */ | |
728 | if (numtrec < ntrec && i != 0) { | |
729 | numtrec = ntrec; | |
730 | goto top; | |
731 | } | |
732 | /* | |
733 | * Handle partial block read. | |
734 | */ | |
735 | if (i > 0 && i != ntrec*TP_BSIZE) { | |
736 | if (pipein) { | |
737 | rd += i; | |
738 | cnt -= i; | |
739 | if (cnt > 0) | |
740 | goto getmore; | |
741 | i = rd; | |
742 | } else { | |
743 | if (i % TP_BSIZE != 0) | |
744 | panic("partial block read: %d should be %d\n", | |
745 | i, ntrec * TP_BSIZE); | |
746 | numtrec = i / TP_BSIZE; | |
747 | } | |
748 | } | |
749 | /* | |
750 | * Handle read error. | |
751 | */ | |
752 | if (i < 0) { | |
753 | fprintf(stderr, "Tape read error while "); | |
754 | switch (curfile.action) { | |
755 | default: | |
756 | fprintf(stderr, "trying to set up tape\n"); | |
757 | break; | |
758 | case UNKNOWN: | |
759 | fprintf(stderr, "trying to resynchronize\n"); | |
760 | break; | |
761 | case USING: | |
762 | fprintf(stderr, "restoring %s\n", curfile.name); | |
763 | break; | |
764 | case SKIP: | |
765 | fprintf(stderr, "skipping over inode %d\n", | |
766 | curfile.ino); | |
767 | break; | |
768 | } | |
769 | if (!yflag && !reply("continue")) | |
770 | done(1); | |
771 | i = ntrec*TP_BSIZE; | |
772 | bzero(tbf, i); | |
773 | #ifdef RRESTORE | |
774 | if (rmtseek(i, 1) < 0) | |
775 | #else | |
776 | if (lseek(mt, i, 1) == (long)-1) | |
777 | #endif | |
778 | { | |
779 | perror("continuation failed"); | |
780 | done(1); | |
781 | } | |
782 | } | |
783 | /* | |
784 | * Handle end of tape. | |
785 | */ | |
786 | if (i == 0) { | |
787 | if (!pipein) { | |
788 | newvol = volno + 1; | |
789 | volno = 0; | |
790 | numtrec = 0; | |
791 | getvol(newvol); | |
792 | readtape(b); | |
793 | return; | |
794 | } | |
795 | if (rd % TP_BSIZE != 0) | |
796 | panic("partial block read: %d should be %d\n", | |
797 | rd, ntrec * TP_BSIZE); | |
798 | bcopy((char *)&endoftapemark, &tbf[rd], (long)TP_BSIZE); | |
799 | } | |
800 | bct = 0; | |
801 | bcopy(&tbf[(bct++*TP_BSIZE)], b, (long)TP_BSIZE); | |
802 | blksread++; | |
803 | } | |
804 | ||
805 | findtapeblksize() | |
806 | { | |
807 | register long i; | |
808 | ||
809 | for (i = 0; i < ntrec; i++) | |
810 | ((struct s_spcl *)&tbf[i * TP_BSIZE])->c_magic = 0; | |
811 | bct = 0; | |
812 | #ifdef RRESTORE | |
813 | i = rmtread(tbf, ntrec * TP_BSIZE); | |
814 | #else | |
815 | i = read(mt, tbf, ntrec * TP_BSIZE); | |
816 | #endif | |
817 | if (i <= 0) { | |
818 | perror("Tape read error"); | |
819 | done(1); | |
820 | } | |
821 | if (i % TP_BSIZE != 0) { | |
822 | fprintf(stderr, "Tape block size (%d) %s (%d)\n", | |
823 | i, "is not a multiple of dump block size", TP_BSIZE); | |
824 | done(1); | |
825 | } | |
826 | ntrec = i / TP_BSIZE; | |
827 | numtrec = ntrec; | |
828 | vprintf(stdout, "Tape block size is %d\n", ntrec); | |
829 | } | |
830 | ||
831 | flsht() | |
832 | { | |
833 | ||
834 | bct = ntrec+1; | |
835 | } | |
836 | ||
837 | closemt() | |
838 | { | |
839 | if (mt < 0) | |
840 | return; | |
841 | #ifdef RRESTORE | |
842 | rmtclose(); | |
843 | #else | |
844 | (void) close(mt); | |
845 | #endif | |
846 | } | |
847 | ||
848 | checkvol(b, t) | |
849 | struct s_spcl *b; | |
850 | long t; | |
851 | { | |
852 | ||
853 | if (b->c_volume != t) | |
854 | return(FAIL); | |
855 | return(GOOD); | |
856 | } | |
857 | ||
858 | readhdr(b) | |
859 | struct s_spcl *b; | |
860 | { | |
861 | ||
862 | if (gethead(b) == FAIL) { | |
863 | dprintf(stdout, "readhdr fails at %d blocks\n", blksread); | |
864 | return(FAIL); | |
865 | } | |
866 | return(GOOD); | |
867 | } | |
868 | ||
869 | /* | |
870 | * read the tape into buf, then return whether or | |
871 | * or not it is a header block. | |
872 | */ | |
873 | gethead(buf) | |
874 | struct s_spcl *buf; | |
875 | { | |
876 | long i; | |
877 | union u_ospcl { | |
878 | char dummy[TP_BSIZE]; | |
879 | struct s_ospcl { | |
880 | long c_type; | |
881 | long c_date; | |
882 | long c_ddate; | |
883 | long c_volume; | |
884 | long c_tapea; | |
885 | u_short c_inumber; | |
886 | long c_magic; | |
887 | long c_checksum; | |
888 | struct odinode { | |
889 | unsigned short odi_mode; | |
890 | u_short odi_nlink; | |
891 | u_short odi_uid; | |
892 | u_short odi_gid; | |
893 | long odi_size; | |
894 | long odi_rdev; | |
895 | char odi_addr[36]; | |
896 | long odi_atime; | |
897 | long odi_mtime; | |
898 | long odi_ctime; | |
899 | } c_dinode; | |
900 | long c_count; | |
901 | char c_addr[256]; | |
902 | } s_ospcl; | |
903 | } u_ospcl; | |
904 | ||
905 | if (!cvtflag) { | |
906 | readtape((char *)buf); | |
907 | if (buf->c_magic != NFS_MAGIC) { | |
908 | if (swabl(buf->c_magic) != NFS_MAGIC) | |
909 | return (FAIL); | |
910 | if (!Bcvt) { | |
911 | vprintf(stdout, "Note: Doing Byte swapping\n"); | |
912 | Bcvt = 1; | |
913 | } | |
914 | } | |
915 | if (checksum((int *)buf) == FAIL) | |
916 | return (FAIL); | |
917 | if (Bcvt) | |
918 | swabst("8l4s31l", (char *)buf); | |
919 | goto good; | |
920 | } | |
921 | readtape((char *)(&u_ospcl.s_ospcl)); | |
922 | bzero((char *)buf, (long)TP_BSIZE); | |
923 | buf->c_type = u_ospcl.s_ospcl.c_type; | |
924 | buf->c_date = u_ospcl.s_ospcl.c_date; | |
925 | buf->c_ddate = u_ospcl.s_ospcl.c_ddate; | |
926 | buf->c_volume = u_ospcl.s_ospcl.c_volume; | |
927 | buf->c_tapea = u_ospcl.s_ospcl.c_tapea; | |
928 | buf->c_inumber = u_ospcl.s_ospcl.c_inumber; | |
929 | buf->c_checksum = u_ospcl.s_ospcl.c_checksum; | |
930 | buf->c_magic = u_ospcl.s_ospcl.c_magic; | |
931 | buf->c_dinode.di_mode = u_ospcl.s_ospcl.c_dinode.odi_mode; | |
932 | buf->c_dinode.di_nlink = u_ospcl.s_ospcl.c_dinode.odi_nlink; | |
933 | buf->c_dinode.di_uid = u_ospcl.s_ospcl.c_dinode.odi_uid; | |
934 | buf->c_dinode.di_gid = u_ospcl.s_ospcl.c_dinode.odi_gid; | |
935 | buf->c_dinode.di_size = u_ospcl.s_ospcl.c_dinode.odi_size; | |
936 | buf->c_dinode.di_rdev = u_ospcl.s_ospcl.c_dinode.odi_rdev; | |
937 | buf->c_dinode.di_atime = u_ospcl.s_ospcl.c_dinode.odi_atime; | |
938 | buf->c_dinode.di_mtime = u_ospcl.s_ospcl.c_dinode.odi_mtime; | |
939 | buf->c_dinode.di_ctime = u_ospcl.s_ospcl.c_dinode.odi_ctime; | |
940 | buf->c_count = u_ospcl.s_ospcl.c_count; | |
941 | bcopy(u_ospcl.s_ospcl.c_addr, buf->c_addr, (long)256); | |
942 | if (u_ospcl.s_ospcl.c_magic != OFS_MAGIC || | |
943 | checksum((int *)(&u_ospcl.s_ospcl)) == FAIL) | |
944 | return(FAIL); | |
945 | buf->c_magic = NFS_MAGIC; | |
946 | ||
947 | good: | |
948 | if (buf->c_dinode.di_size == 0 && | |
949 | (buf->c_dinode.di_mode & IFMT) == IFDIR && Qcvt == 0) { | |
950 | if (buf->c_dinode.di_qsize.val[0] || | |
951 | buf->c_dinode.di_qsize.val[1]) { | |
952 | printf("Note: Doing Quad swapping\n"); | |
953 | Qcvt = 1; | |
954 | } | |
955 | } | |
956 | if (Qcvt) { | |
957 | i = buf->c_dinode.di_qsize.val[1]; | |
958 | buf->c_dinode.di_qsize.val[1] = buf->c_dinode.di_qsize.val[0]; | |
959 | buf->c_dinode.di_qsize.val[0] = i; | |
960 | } | |
961 | switch (buf->c_type) { | |
962 | ||
963 | case TS_CLRI: | |
964 | case TS_BITS: | |
965 | /* | |
966 | * Have to patch up missing information in bit map headers | |
967 | */ | |
968 | buf->c_inumber = 0; | |
969 | buf->c_dinode.di_size = buf->c_count * TP_BSIZE; | |
970 | for (i = 0; i < buf->c_count; i++) | |
971 | buf->c_addr[i]++; | |
972 | break; | |
973 | ||
974 | case TS_TAPE: | |
975 | case TS_END: | |
976 | buf->c_inumber = 0; | |
977 | break; | |
978 | ||
979 | case TS_INODE: | |
980 | case TS_ADDR: | |
981 | break; | |
982 | ||
983 | default: | |
984 | panic("gethead: unknown inode type %d\n", buf->c_type); | |
985 | break; | |
986 | } | |
987 | if (dflag) | |
988 | accthdr(buf); | |
989 | return(GOOD); | |
990 | } | |
991 | ||
992 | /* | |
993 | * Check that a header is where it belongs and predict the next header | |
994 | */ | |
995 | accthdr(header) | |
996 | struct s_spcl *header; | |
997 | { | |
998 | static ino_t previno = 0x7fffffff; | |
999 | static int prevtype; | |
1000 | static long predict; | |
1001 | long blks, i; | |
1002 | ||
1003 | if (header->c_type == TS_TAPE) { | |
1004 | fprintf(stderr, "Volume header\n"); | |
1005 | previno = 0x7fffffff; | |
1006 | return; | |
1007 | } | |
1008 | if (previno == 0x7fffffff) | |
1009 | goto newcalc; | |
1010 | switch (prevtype) { | |
1011 | case TS_BITS: | |
1012 | fprintf(stderr, "Dump mask header"); | |
1013 | break; | |
1014 | case TS_CLRI: | |
1015 | fprintf(stderr, "Remove mask header"); | |
1016 | break; | |
1017 | case TS_INODE: | |
1018 | fprintf(stderr, "File header, ino %d", previno); | |
1019 | break; | |
1020 | case TS_ADDR: | |
1021 | fprintf(stderr, "File continuation header, ino %d", previno); | |
1022 | break; | |
1023 | case TS_END: | |
1024 | fprintf(stderr, "End of tape header"); | |
1025 | break; | |
1026 | } | |
1027 | if (predict != blksread - 1) | |
1028 | fprintf(stderr, "; predicted %d blocks, got %d blocks", | |
1029 | predict, blksread - 1); | |
1030 | fprintf(stderr, "\n"); | |
1031 | newcalc: | |
1032 | blks = 0; | |
1033 | if (header->c_type != TS_END) | |
1034 | for (i = 0; i < header->c_count; i++) | |
1035 | if (header->c_addr[i] != 0) | |
1036 | blks++; | |
1037 | predict = blks; | |
1038 | blksread = 0; | |
1039 | prevtype = header->c_type; | |
1040 | previno = header->c_inumber; | |
1041 | } | |
1042 | ||
1043 | /* | |
1044 | * Find an inode header. | |
1045 | * Complain if had to skip, and complain is set. | |
1046 | */ | |
1047 | findinode(header) | |
1048 | struct s_spcl *header; | |
1049 | { | |
1050 | static long skipcnt = 0; | |
1051 | long i; | |
1052 | char buf[TP_BSIZE]; | |
1053 | ||
1054 | curfile.name = "<name unknown>"; | |
1055 | curfile.action = UNKNOWN; | |
1056 | curfile.dip = (struct dinode *)NIL; | |
1057 | curfile.ino = 0; | |
1058 | if (ishead(header) == FAIL) { | |
1059 | skipcnt++; | |
1060 | while (gethead(header) == FAIL || header->c_date != dumpdate) | |
1061 | skipcnt++; | |
1062 | } | |
1063 | for (;;) { | |
1064 | if (checktype(header, TS_ADDR) == GOOD) { | |
1065 | /* | |
1066 | * Skip up to the beginning of the next record | |
1067 | */ | |
1068 | for (i = 0; i < header->c_count; i++) | |
1069 | if (header->c_addr[i]) | |
1070 | readtape(buf); | |
1071 | (void) gethead(header); | |
1072 | continue; | |
1073 | } | |
1074 | if (checktype(header, TS_INODE) == GOOD) { | |
1075 | curfile.dip = &header->c_dinode; | |
1076 | curfile.ino = header->c_inumber; | |
1077 | break; | |
1078 | } | |
1079 | if (checktype(header, TS_END) == GOOD) { | |
1080 | curfile.ino = maxino; | |
1081 | break; | |
1082 | } | |
1083 | if (checktype(header, TS_CLRI) == GOOD) { | |
1084 | curfile.name = "<file removal list>"; | |
1085 | break; | |
1086 | } | |
1087 | if (checktype(header, TS_BITS) == GOOD) { | |
1088 | curfile.name = "<file dump list>"; | |
1089 | break; | |
1090 | } | |
1091 | while (gethead(header) == FAIL) | |
1092 | skipcnt++; | |
1093 | } | |
1094 | if (skipcnt > 0) | |
1095 | fprintf(stderr, "resync restore, skipped %d blocks\n", skipcnt); | |
1096 | skipcnt = 0; | |
1097 | } | |
1098 | ||
1099 | /* | |
1100 | * return whether or not the buffer contains a header block | |
1101 | */ | |
1102 | ishead(buf) | |
1103 | struct s_spcl *buf; | |
1104 | { | |
1105 | ||
1106 | if (buf->c_magic != NFS_MAGIC) | |
1107 | return(FAIL); | |
1108 | return(GOOD); | |
1109 | } | |
1110 | ||
1111 | checktype(b, t) | |
1112 | struct s_spcl *b; | |
1113 | int t; | |
1114 | { | |
1115 | ||
1116 | if (b->c_type != t) | |
1117 | return(FAIL); | |
1118 | return(GOOD); | |
1119 | } | |
1120 | ||
1121 | checksum(b) | |
1122 | register int *b; | |
1123 | { | |
1124 | register int i, j; | |
1125 | ||
1126 | j = sizeof(union u_spcl) / sizeof(int); | |
1127 | i = 0; | |
1128 | if(!Bcvt) { | |
1129 | do | |
1130 | i += *b++; | |
1131 | while (--j); | |
1132 | } else { | |
1133 | /* What happens if we want to read restore tapes | |
1134 | for a 16bit int machine??? */ | |
1135 | do | |
1136 | i += swabl(*b++); | |
1137 | while (--j); | |
1138 | } | |
1139 | ||
1140 | if (i != CHECKSUM) { | |
1141 | fprintf(stderr, "Checksum error %o, inode %d file %s\n", i, | |
1142 | curfile.ino, curfile.name); | |
1143 | return(FAIL); | |
1144 | } | |
1145 | return(GOOD); | |
1146 | } | |
1147 | ||
1148 | #ifdef RRESTORE | |
1149 | /* VARARGS1 */ | |
1150 | msg(cp, a1, a2, a3) | |
1151 | char *cp; | |
1152 | { | |
1153 | ||
1154 | fprintf(stderr, cp, a1, a2, a3); | |
1155 | } | |
1156 | #endif RRESTORE | |
1157 | ||
1158 | u_char * | |
1159 | swabshort(sp, n) | |
1160 | register u_char *sp; | |
1161 | register int n; | |
1162 | { | |
1163 | char c; | |
1164 | ||
1165 | while (--n >= 0) { | |
1166 | c = sp[0]; sp[0] = sp[1]; sp[1] = c; | |
1167 | sp += 2; | |
1168 | } | |
1169 | return (sp); | |
1170 | } | |
1171 | ||
1172 | u_char * | |
1173 | swablong(sp, n) | |
1174 | register u_char *sp; | |
1175 | register int n; | |
1176 | { | |
1177 | char c; | |
1178 | ||
1179 | while (--n >= 0) { | |
1180 | c = sp[0]; sp[0] = sp[3]; sp[3] = c; | |
1181 | c = sp[2]; sp[2] = sp[1]; sp[1] = c; | |
1182 | sp += 4; | |
1183 | } | |
1184 | return (sp); | |
1185 | } | |
1186 | ||
1187 | swabst(cp, sp) | |
1188 | register u_char *cp, *sp; | |
1189 | { | |
1190 | int n = 0; | |
1191 | u_char c; | |
1192 | ||
1193 | while (*cp) { | |
1194 | switch (*cp) { | |
1195 | case '0': case '1': case '2': case '3': case '4': | |
1196 | case '5': case '6': case '7': case '8': case '9': | |
1197 | n = (n * 10) + (*cp++ - '0'); | |
1198 | continue; | |
1199 | ||
1200 | case 's': case 'w': case 'h': | |
1201 | if (n == 0) | |
1202 | n = 1; | |
1203 | sp = swabshort(sp, n); | |
1204 | break; | |
1205 | ||
1206 | case 'l': | |
1207 | if (n == 0) | |
1208 | n = 1; | |
1209 | sp = swablong(sp, n); | |
1210 | break; | |
1211 | ||
1212 | default: /* Any other character, like 'b' counts as byte. */ | |
1213 | if (n == 0) | |
1214 | n = 1; | |
1215 | sp += n; | |
1216 | break; | |
1217 | } | |
1218 | cp++; | |
1219 | n = 0; | |
1220 | } | |
1221 | } | |
1222 | ||
1223 | u_long | |
1224 | swabl(x) | |
1225 | u_long x; | |
1226 | { | |
1227 | swabst("l", (char *)&x); | |
1228 | return (x); | |
1229 | } |