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