added option for upgrade
[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
dad5e0ef 9static char sccsid[] = "@(#)tape.c 5.31 (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 25static int bct;
c8fb0747 26static int numtrec;
19230a53 27static char *tbf;
2cb5dabb 28static union u_spcl endoftapemark;
2532f4a9 29static long blksread; /* blocks read since last header */
3b29a62c 30static long tpblksread = 0; /* TP_BSIZE blocks read */
170639fd 31static long tapesread;
2cb5dabb
KM
32static jmp_buf restart;
33static int gettingfile = 0; /* restart has a valid frame */
2532f4a9 34static char *host = NULL;
2cb5dabb
KM
35
36static int ofile;
37static char *map;
38static char lnkbuf[MAXPATHLEN + 1];
39static int pathlen;
f78e116c 40
dad5e0ef 41int oldinofmt; /* old inode format conversion required */
b4c41341
KS
42int Bcvt; /* Swap Bytes (for CCI or sun) */
43static int Qcvt; /* Swap quads (for sun) */
025e61b9 44u_long swabl();
2532f4a9 45
f78e116c
KM
46/*
47 * Set up an input source
48 */
49setinput(source)
50 char *source;
51{
7abf8d65 52 extern int errno;
7abf8d65 53 char *strerror();
f78e116c 54
19230a53 55 flsht();
b97e998a
KM
56 if (bflag)
57 newtapebuf(ntrec);
58 else
59 newtapebuf(NTREC > HIGHDENSITYTREC ? NTREC : HIGHDENSITYTREC);
9c695c61 60 terminal = stdin;
2532f4a9 61
e9e7ecc4 62#ifdef RRESTORE
2532f4a9
KM
63 if (index(source, ':')) {
64 host = source;
65 source = index(host, ':');
66 *source++ = '\0';
67 if (rmthost(host) == 0)
68 done(1);
69 } else
70#endif
9c695c61 71 if (strcmp(source, "-") == 0) {
b677c5d7
KM
72 /*
73 * Since input is coming from a pipe we must establish
74 * our own connection to the terminal.
75 */
7abf8d65 76 terminal = fopen(_PATH_TTY, "r");
b677c5d7 77 if (terminal == NULL) {
7abf8d65 78 (void)fprintf(stderr, "Cannot open %s: %s\n",
2532f4a9 79 _PATH_TTY, strerror(errno));
7abf8d65 80 terminal = fopen(_PATH_DEVNULL, "r");
b8eb5f1f 81 if (terminal == NULL) {
2532f4a9
KM
82 (void)fprintf(stderr, "Cannot open %s: %s\n",
83 _PATH_DEVNULL, strerror(errno));
b8eb5f1f
KM
84 done(1);
85 }
b677c5d7 86 }
2cb5dabb 87 pipein++;
2cb5dabb 88 }
2532f4a9 89 setuid(getuid()); /* no longer need or want root privileges */
7eb08dfe 90 (void) strcpy(magtape, source);
f78e116c
KM
91}
92
b97e998a
KM
93newtapebuf(size)
94 long size;
95{
96 static tbfsize = -1;
97
98 ntrec = size;
99 if (size <= tbfsize)
100 return;
101 if (tbf != NULL)
102 free(tbf);
103 tbf = (char *)malloc(size * TP_BSIZE);
104 if (tbf == NULL) {
105 fprintf(stderr, "Cannot allocate space for tape buffer\n");
106 done(1);
107 }
108 tbfsize = size;
109}
110
f78e116c
KM
111/*
112 * Verify that the tape drive can be accessed and
113 * that it actually is a dump tape.
114 */
115setup()
116{
2cb5dabb 117 int i, j, *ip;
f78e116c 118 struct stat stbuf;
f78e116c
KM
119 extern int xtrmap(), xtrmapskip();
120
121 vprintf(stdout, "Verify tape and initialize maps\n");
e9e7ecc4 122#ifdef RRESTORE
2532f4a9
KM
123 if (host)
124 mt = rmtopen(magtape, 0);
125 else
126#endif
2cb5dabb
KM
127 if (pipein)
128 mt = 0;
2532f4a9
KM
129 else
130 mt = open(magtape, 0);
131 if (mt < 0) {
e9e7ecc4 132 perror(magtape);
f78e116c
KM
133 done(1);
134 }
768eb687
KM
135 volno = 1;
136 setdumpnum();
f78e116c 137 flsht();
b97e998a
KM
138 if (!pipein && !bflag)
139 findtapeblksize();
5c5f44c7 140 if (gethead(&spcl) == FAIL) {
f78e116c 141 bct--; /* push back this block */
2532f4a9 142 blksread--;
3b29a62c 143 tpblksread--;
f78e116c 144 cvtflag++;
5c5f44c7 145 if (gethead(&spcl) == FAIL) {
f78e116c
KM
146 fprintf(stderr, "Tape is not a dump tape\n");
147 done(1);
148 }
149 fprintf(stderr, "Converting to new file system format.\n");
150 }
2cb5dabb
KM
151 if (pipein) {
152 endoftapemark.s_spcl.c_magic = cvtflag ? OFS_MAGIC : NFS_MAGIC;
153 endoftapemark.s_spcl.c_type = TS_END;
154 ip = (int *)&endoftapemark;
155 j = sizeof(union u_spcl) / sizeof(int);
156 i = 0;
157 do
158 i += *ip++;
159 while (--j);
160 endoftapemark.s_spcl.c_checksum = CHECKSUM - i;
161 }
b32320ae
KM
162 if (vflag || command == 't')
163 printdumpinfo();
f78e116c 164 dumptime = spcl.c_ddate;
2cb5dabb 165 dumpdate = spcl.c_date;
f78e116c 166 if (stat(".", &stbuf) < 0) {
e9e7ecc4 167 perror("cannot stat .");
f78e116c
KM
168 done(1);
169 }
bdeceadb
KM
170 if (stbuf.st_blksize > 0 && stbuf.st_blksize <= MAXBSIZE)
171 fssize = stbuf.st_blksize;
172 if (((fssize - 1) & fssize) != 0) {
f78e116c
KM
173 fprintf(stderr, "bad block size %d\n", fssize);
174 done(1);
175 }
2cb5dabb 176 if (checkvol(&spcl, (long)1) == FAIL) {
f78e116c
KM
177 fprintf(stderr, "Tape is not volume 1 of the dump\n");
178 done(1);
179 }
2cb5dabb
KM
180 if (readhdr(&spcl) == FAIL)
181 panic("no header after volume mark!\n");
b32320ae 182 findinode(&spcl);
2cb5dabb 183 if (checktype(&spcl, TS_CLRI) == FAIL) {
f78e116c
KM
184 fprintf(stderr, "Cannot find file removal list\n");
185 done(1);
186 }
2cb5dabb 187 maxino = (spcl.c_count * TP_BSIZE * NBBY) + 1;
768eb687 188 dprintf(stdout, "maxino = %d\n", maxino);
7851e15d 189 map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
f78e116c
KM
190 if (map == (char *)NIL)
191 panic("no memory for file removal list\n");
cf6db0ab 192 clrimap = map;
f78e116c
KM
193 curfile.action = USING;
194 getfile(xtrmap, xtrmapskip);
2cb5dabb 195 if (checktype(&spcl, TS_BITS) == FAIL) {
f78e116c
KM
196 fprintf(stderr, "Cannot find file dump list\n");
197 done(1);
198 }
7851e15d 199 map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
f78e116c
KM
200 if (map == (char *)NULL)
201 panic("no memory for file dump list\n");
cf6db0ab 202 dumpmap = map;
f78e116c
KM
203 curfile.action = USING;
204 getfile(xtrmap, xtrmapskip);
f78e116c
KM
205}
206
170639fd
KM
207/*
208 * Prompt user to load a new dump volume.
209 * "Nextvol" is the next suggested volume to use.
210 * This suggested volume is enforced when doing full
211 * or incremental restores, but can be overrridden by
212 * the user when only extracting a subset of the files.
213 */
f78e116c
KM
214getvol(nextvol)
215 long nextvol;
216{
3b29a62c 217 long newvol, savecnt, wantnext, i;
f78e116c
KM
218 union u_spcl tmpspcl;
219# define tmpbuf tmpspcl.s_spcl
b32320ae 220 char buf[TP_BSIZE];
cb2a63da 221 extern char *ctime();
f78e116c 222
a162cc0b 223 if (nextvol == 1) {
170639fd 224 tapesread = 0;
a162cc0b
KM
225 gettingfile = 0;
226 }
2cb5dabb 227 if (pipein) {
e108d3a2 228 if (nextvol != 1)
2cb5dabb 229 panic("Changing volumes on pipe input?\n");
e108d3a2
KM
230 if (volno == 1)
231 return;
232 goto gethdr;
2cb5dabb
KM
233 }
234 savecnt = blksread;
f78e116c 235again:
e108d3a2
KM
236 if (pipein)
237 done(1); /* pipes do not get a second chance */
3b29a62c 238 if (command == 'R' || command == 'r' || curfile.action != SKIP) {
f78e116c 239 newvol = nextvol;
3b29a62c
KM
240 wantnext = 1;
241 } else {
f78e116c 242 newvol = 0;
3b29a62c
KM
243 wantnext = 0;
244 }
f78e116c 245 while (newvol <= 0) {
170639fd
KM
246 if (tapesread == 0) {
247 fprintf(stderr, "%s%s%s%s%s",
248 "You have not read any tapes yet.\n",
249 "Unless you know which volume your",
250 " file(s) are on you should start\n",
251 "with the last volume and work",
b4c41341 252 " towards towards the first.\n");
170639fd
KM
253 } else {
254 fprintf(stderr, "You have read volumes");
255 strcpy(tbf, ": ");
256 for (i = 1; i < 32; i++)
257 if (tapesread & (1 << i)) {
258 fprintf(stderr, "%s%d", tbf, i);
259 strcpy(tbf, ", ");
260 }
261 fprintf(stderr, "\n");
262 }
538cb7bb 263 do {
b677c5d7
KM
264 fprintf(stderr, "Specify next volume #: ");
265 (void) fflush(stderr);
266 (void) fgets(tbf, BUFSIZ, terminal);
267 } while (!feof(terminal) && tbf[0] == '\n');
268 if (feof(terminal))
538cb7bb 269 done(1);
f78e116c
KM
270 newvol = atoi(tbf);
271 if (newvol <= 0) {
272 fprintf(stderr,
273 "Volume numbers are positive numerics\n");
274 }
275 }
170639fd
KM
276 if (newvol == volno) {
277 tapesread |= 1 << volno;
f78e116c 278 return;
170639fd 279 }
f78e116c 280 closemt();
7eb08dfe 281 fprintf(stderr, "Mount tape volume %d\n", newvol);
eb317d51
KM
282 fprintf(stderr, "Enter ``none'' if there are no more tapes\n");
283 fprintf(stderr, "otherwise enter tape name (default: %s) ", magtape);
b677c5d7 284 (void) fflush(stderr);
7eb08dfe
KM
285 (void) fgets(tbf, BUFSIZ, terminal);
286 if (feof(terminal))
287 done(1);
eb317d51 288 if (!strcmp(tbf, "none\n")) {
2657795e
KM
289 terminateinput();
290 return;
eb317d51 291 }
7eb08dfe
KM
292 if (tbf[0] != '\n') {
293 (void) strcpy(magtape, tbf);
294 magtape[strlen(magtape) - 1] = '\0';
295 }
e9e7ecc4 296#ifdef RRESTORE
2532f4a9
KM
297 if (host)
298 mt = rmtopen(magtape, 0);
299 else
f78e116c 300#endif
2532f4a9
KM
301 mt = open(magtape, 0);
302
303 if (mt == -1) {
7eb08dfe
KM
304 fprintf(stderr, "Cannot open %s\n", magtape);
305 volno = -1;
f78e116c
KM
306 goto again;
307 }
e108d3a2 308gethdr:
f78e116c 309 volno = newvol;
768eb687 310 setdumpnum();
f78e116c 311 flsht();
2cb5dabb 312 if (readhdr(&tmpbuf) == FAIL) {
f78e116c
KM
313 fprintf(stderr, "tape is not dump tape\n");
314 volno = 0;
315 goto again;
316 }
2cb5dabb 317 if (checkvol(&tmpbuf, volno) == FAIL) {
f78e116c
KM
318 fprintf(stderr, "Wrong volume (%d)\n", tmpbuf.c_volume);
319 volno = 0;
320 goto again;
321 }
2cb5dabb 322 if (tmpbuf.c_date != dumpdate || tmpbuf.c_ddate != dumptime) {
3dbddecd
KM
323 fprintf(stderr, "Wrong dump date\n\tgot: %s",
324 ctime(&tmpbuf.c_date));
325 fprintf(stderr, "\twanted: %s", ctime(&dumpdate));
2cb5dabb
KM
326 volno = 0;
327 goto again;
328 }
170639fd 329 tapesread |= 1 << volno;
5c5f44c7 330 blksread = savecnt;
3b29a62c
KM
331 /*
332 * If continuing from the previous volume, skip over any
333 * blocks read already at the end of the previous volume.
334 *
335 * If coming to this volume at random, skip to the beginning
336 * of the next record.
337 */
338 dprintf(stdout, "read %ld recs, tape starts with %ld\n",
339 tpblksread, tmpbuf.c_firstrec);
340 if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER)) {
341 if (!wantnext) {
342 tpblksread = tmpbuf.c_firstrec;
343 for (i = tmpbuf.c_count; i > 0; i--)
344 readtape(buf);
345 } else if (tmpbuf.c_firstrec > 0 &&
346 tmpbuf.c_firstrec < tpblksread - 1) {
347 /*
348 * -1 since we've read the volume header
349 */
350 i = tpblksread - tmpbuf.c_firstrec - 1;
351 dprintf(stderr, "Skipping %d duplicate record%s.\n",
352 i, i > 1 ? "s" : "");
353 while (--i >= 0)
354 readtape(buf);
355 }
356 }
f78e116c
KM
357 if (curfile.action == USING) {
358 if (volno == 1)
359 panic("active file into volume 1\n");
360 return;
361 }
b32320ae
KM
362 /*
363 * Skip up to the beginning of the next record
364 */
8d412b7c 365 if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER))
b32320ae
KM
366 for (i = tmpbuf.c_count; i > 0; i--)
367 readtape(buf);
5c5f44c7 368 (void) gethead(&spcl);
b32320ae 369 findinode(&spcl);
f78e116c
KM
370 if (gettingfile) {
371 gettingfile = 0;
372 longjmp(restart, 1);
373 }
374}
375
2657795e
KM
376/*
377 * Handle unexpected EOF.
378 */
379terminateinput()
380{
381
382 if (gettingfile && curfile.action == USING) {
383 printf("Warning: %s %s\n",
384 "End-of-input encountered while extracting", curfile.name);
385 }
386 curfile.name = "<name unknown>";
387 curfile.action = UNKNOWN;
388 curfile.dip = (struct dinode *)NIL;
389 curfile.ino = maxino;
390 if (gettingfile) {
391 gettingfile = 0;
392 longjmp(restart, 1);
393 }
394}
395
768eb687
KM
396/*
397 * handle multiple dumps per tape by skipping forward to the
398 * appropriate one.
399 */
400setdumpnum()
401{
402 struct mtop tcom;
403
404 if (dumpnum == 1 || volno != 1)
405 return;
406 if (pipein) {
407 fprintf(stderr, "Cannot have multiple dumps on pipe input\n");
408 done(1);
409 }
410 tcom.mt_op = MTFSF;
411 tcom.mt_count = dumpnum - 1;
e9e7ecc4 412#ifdef RRESTORE
2532f4a9
KM
413 if (host)
414 rmtioctl(MTFSF, dumpnum - 1);
415 else
768eb687 416#endif
2532f4a9
KM
417 if (ioctl(mt, (int)MTIOCTOP, (char *)&tcom) < 0)
418 perror("ioctl MTFSF");
768eb687
KM
419}
420
b32320ae
KM
421printdumpinfo()
422{
cb2a63da 423 extern char *ctime();
b32320ae
KM
424
425 fprintf(stdout, "Dump date: %s", ctime(&spcl.c_date));
f441de4a
KM
426 fprintf(stdout, "Dumped from: %s",
427 (spcl.c_ddate == 0) ? "the epoch\n" : ctime(&spcl.c_ddate));
b32320ae
KM
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
f78e116c
KM
435extractfile(name)
436 char *name;
437{
438 int mode;
165b1daf 439 struct timeval timep[2];
f78e116c
KM
440 struct entry *ep;
441 extern int xtrlnkfile(), xtrlnkskip();
442 extern int xtrfile(), xtrskip();
443
444 curfile.name = name;
445 curfile.action = USING;
4f57cc9f
KM
446 timep[0].tv_sec = curfile.dip->di_atime.ts_sec;
447 timep[0].tv_usec = curfile.dip->di_atime.ts_nsec / 1000;
448 timep[1].tv_sec = curfile.dip->di_mtime.ts_sec;
449 timep[1].tv_usec = curfile.dip->di_mtime.ts_nsec / 1000;
f78e116c
KM
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
74e940f5
KM
458 case IFSOCK:
459 vprintf(stdout, "skipped socket %s\n", name);
460 skipfile();
461 return (GOOD);
462
f78e116c
KM
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);
bdeceadb
KM
481 return (GOOD);
482 }
483 return (linkit(lnkbuf, name, SYMLINK));
f78e116c
KM
484
485 case IFCHR:
486 case IFBLK:
487 vprintf(stdout, "extract special file %s\n", name);
414c4f09
KM
488 if (Nflag) {
489 skipfile();
490 return (GOOD);
491 }
f78e116c 492 if (mknod(name, mode, (int)curfile.dip->di_rdev) < 0) {
e9e7ecc4
KM
493 fprintf(stderr, "%s: ", name);
494 (void) fflush(stderr);
495 perror("cannot create special file");
f78e116c
KM
496 skipfile();
497 return (FAIL);
498 }
768eb687
KM
499 (void) chown(name, curfile.dip->di_uid, curfile.dip->di_gid);
500 (void) chmod(name, mode);
f78e116c 501 skipfile();
165b1daf 502 utimes(name, timep);
f78e116c
KM
503 return (GOOD);
504
505 case IFREG:
506 vprintf(stdout, "extract file %s\n", name);
414c4f09
KM
507 if (Nflag) {
508 skipfile();
509 return (GOOD);
510 }
15aeb519 511 if ((ofile = creat(name, 0666)) < 0) {
e9e7ecc4
KM
512 fprintf(stderr, "%s: ", name);
513 (void) fflush(stderr);
514 perror("cannot create file");
f78e116c
KM
515 skipfile();
516 return (FAIL);
517 }
768eb687
KM
518 (void) fchown(ofile, curfile.dip->di_uid, curfile.dip->di_gid);
519 (void) fchmod(ofile, mode);
f78e116c 520 getfile(xtrfile, xtrskip);
768eb687 521 (void) close(ofile);
165b1daf 522 utimes(name, timep);
f78e116c
KM
523 return (GOOD);
524 }
525 /* NOTREACHED */
526}
527
e108d3a2
KM
528/*
529 * skip over bit maps on the tape
530 */
531skipmaps()
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 */
f78e116c
KM
542skipfile()
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 */
554getfile(f1, f2)
555 int (*f2)(), (*f1)();
556{
557 register int i;
558 int curblk = 0;
dad5e0ef 559 long size = spcl.c_dinode.di_size;
f78e116c
KM
560 static char clearedbuf[MAXBSIZE];
561 char buf[MAXBSIZE / TP_BSIZE][TP_BSIZE];
4f8ea093 562 char junk[TP_BSIZE];
f78e116c 563
2cb5dabb 564 if (checktype(&spcl, TS_END) == GOOD)
f78e116c 565 panic("ran off end of tape\n");
e108d3a2 566 if (ishead(&spcl) == FAIL)
f78e116c 567 panic("not at beginning of a file\n");
e108d3a2 568 if (!gettingfile && setjmp(restart) != 0)
f78e116c
KM
569 return;
570 gettingfile++;
571loop:
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 }
4f8ea093
KM
591 if ((size -= TP_BSIZE) <= 0) {
592 for (i++; i < spcl.c_count; i++)
593 if (spcl.c_addr[i])
594 readtape(junk);
9f13f26d 595 break;
4f8ea093 596 }
f78e116c 597 }
5c5f44c7 598 if (readhdr(&spcl) == GOOD && size > 0) {
9f13f26d
KM
599 if (checktype(&spcl, TS_ADDR) == GOOD)
600 goto loop;
768eb687 601 dprintf(stdout, "Missing address (header) block for %s\n",
f78e116c 602 curfile.name);
f78e116c 603 }
9f13f26d 604 if (curblk > 0)
f78e116c 605 (*f1)(buf, (curblk * TP_BSIZE) + size);
b32320ae 606 findinode(&spcl);
f78e116c
KM
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 */
614xtrfile(buf, size)
615 char *buf;
616 long size;
617{
618
414c4f09
KM
619 if (Nflag)
620 return;
f78e116c
KM
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
629xtrskip(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
645xtrlnkfile(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 }
538cb7bb 656 (void) strcat(lnkbuf, buf);
f78e116c
KM
657}
658
659xtrlnkskip(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
672xtrmap(buf, size)
673 char *buf;
674 long size;
675{
676
677 bcopy(buf, map, size);
cf6db0ab 678 map += size;
f78e116c
KM
679}
680
681xtrmapskip(buf, size)
682 char *buf;
683 long size;
684{
685
686#ifdef lint
687 buf = buf;
f78e116c
KM
688#endif
689 panic("hole in map\n");
cf6db0ab 690 map += size;
f78e116c
KM
691}
692
693null() {;}
694
695/*
696 * Do the tape i/o, dealing with volume changes
697 * etc..
698 */
699readtape(b)
700 char *b;
701{
702 register long i;
7851e15d
KM
703 long rd, newvol;
704 int cnt;
2532f4a9 705 int seek_failed;
f78e116c 706
c8fb0747 707 if (bct < numtrec) {
b97e998a
KM
708 bcopy(&tbf[(bct++*TP_BSIZE)], b, (long)TP_BSIZE);
709 blksread++;
3b29a62c 710 tpblksread++;
b97e998a
KM
711 return;
712 }
713 for (i = 0; i < ntrec; i++)
714 ((struct s_spcl *)&tbf[i*TP_BSIZE])->c_magic = 0;
c8fb0747
KM
715 if (numtrec == 0)
716 numtrec = ntrec;
2532f4a9 717 cnt = ntrec * TP_BSIZE;
b97e998a
KM
718 rd = 0;
719getmore:
e9e7ecc4 720#ifdef RRESTORE
2532f4a9
KM
721 if (host)
722 i = rmtread(&tbf[rd], cnt);
723 else
f78e116c 724#endif
2532f4a9 725 i = read(mt, &tbf[rd], cnt);
c8fb0747
KM
726 /*
727 * Check for mid-tape short read error.
2532f4a9 728 * If found, skip rest of buffer and start with the next.
c8fb0747 729 */
2532f4a9
KM
730 if (!pipein && numtrec < ntrec && i > 0) {
731 dprintf(stdout, "mid-media short read error.\n");
c8fb0747 732 numtrec = ntrec;
c8fb0747
KM
733 }
734 /*
735 * Handle partial block read.
736 */
4f57cc9f
KM
737 if (pipein && i == 0 && rd > 0)
738 i = rd;
739 else if (i > 0 && i != ntrec * TP_BSIZE) {
b97e998a 740 if (pipein) {
74025ab9
KM
741 rd += i;
742 cnt -= i;
743 if (cnt > 0)
744 goto getmore;
745 i = rd;
b97e998a 746 } else {
2532f4a9
KM
747 /*
748 * Short read. Process the blocks read.
749 */
b97e998a 750 if (i % TP_BSIZE != 0)
2532f4a9
KM
751 vprintf(stdout,
752 "partial block read: %d should be %d\n",
753 i, ntrec * TP_BSIZE);
c8fb0747 754 numtrec = i / TP_BSIZE;
74025ab9 755 }
b97e998a 756 }
c8fb0747
KM
757 /*
758 * Handle read error.
759 */
b97e998a
KM
760 if (i < 0) {
761 fprintf(stderr, "Tape read error while ");
762 switch (curfile.action) {
763 default:
764 fprintf(stderr, "trying to set up tape\n");
765 break;
766 case UNKNOWN:
bf7abb47 767 fprintf(stderr, "trying to resynchronize\n");
b97e998a
KM
768 break;
769 case USING:
770 fprintf(stderr, "restoring %s\n", curfile.name);
771 break;
772 case SKIP:
773 fprintf(stderr, "skipping over inode %d\n",
774 curfile.ino);
775 break;
776 }
777 if (!yflag && !reply("continue"))
778 done(1);
779 i = ntrec*TP_BSIZE;
780 bzero(tbf, i);
e9e7ecc4 781#ifdef RRESTORE
2532f4a9
KM
782 if (host)
783 seek_failed = (rmtseek(i, 1) < 0);
784 else
f78e116c 785#endif
2532f4a9
KM
786 seek_failed = (lseek(mt, i, 1) == (long)-1);
787
788 if (seek_failed) {
b97e998a
KM
789 perror("continuation failed");
790 done(1);
f78e116c 791 }
b97e998a 792 }
c8fb0747
KM
793 /*
794 * Handle end of tape.
795 */
b97e998a 796 if (i == 0) {
2532f4a9 797 vprintf(stdout, "End-of-tape encountered\n");
ad073f89
KM
798 if (!pipein) {
799 newvol = volno + 1;
800 volno = 0;
c8fb0747 801 numtrec = 0;
ad073f89
KM
802 getvol(newvol);
803 readtape(b);
f78e116c
KM
804 return;
805 }
ad073f89
KM
806 if (rd % TP_BSIZE != 0)
807 panic("partial block read: %d should be %d\n",
808 rd, ntrec * TP_BSIZE);
2657795e 809 terminateinput();
ad073f89 810 bcopy((char *)&endoftapemark, &tbf[rd], (long)TP_BSIZE);
f78e116c 811 }
c8fb0747 812 bct = 0;
f78e116c 813 bcopy(&tbf[(bct++*TP_BSIZE)], b, (long)TP_BSIZE);
2cb5dabb 814 blksread++;
3b29a62c 815 tpblksread++;
f78e116c
KM
816}
817
b97e998a
KM
818findtapeblksize()
819{
820 register long i;
821
822 for (i = 0; i < ntrec; i++)
823 ((struct s_spcl *)&tbf[i * TP_BSIZE])->c_magic = 0;
824 bct = 0;
825#ifdef RRESTORE
2532f4a9
KM
826 if (host)
827 i = rmtread(tbf, ntrec * TP_BSIZE);
828 else
b97e998a 829#endif
2532f4a9
KM
830 i = read(mt, tbf, ntrec * TP_BSIZE);
831
b97e998a
KM
832 if (i <= 0) {
833 perror("Tape read error");
834 done(1);
835 }
836 if (i % TP_BSIZE != 0) {
837 fprintf(stderr, "Tape block size (%d) %s (%d)\n",
838 i, "is not a multiple of dump block size", TP_BSIZE);
839 done(1);
840 }
841 ntrec = i / TP_BSIZE;
c8fb0747 842 numtrec = ntrec;
b97e998a
KM
843 vprintf(stdout, "Tape block size is %d\n", ntrec);
844}
845
f78e116c
KM
846flsht()
847{
848
19230a53 849 bct = ntrec+1;
f78e116c
KM
850}
851
f78e116c
KM
852closemt()
853{
854 if (mt < 0)
855 return;
e9e7ecc4 856#ifdef RRESTORE
2532f4a9
KM
857 if (host)
858 rmtclose();
859 else
f78e116c 860#endif
2532f4a9 861 (void) close(mt);
f78e116c
KM
862}
863
864checkvol(b, t)
865 struct s_spcl *b;
866 long t;
867{
868
2cb5dabb
KM
869 if (b->c_volume != t)
870 return(FAIL);
871 return(GOOD);
f78e116c
KM
872}
873
874readhdr(b)
875 struct s_spcl *b;
876{
877
5c5f44c7 878 if (gethead(b) == FAIL) {
768eb687 879 dprintf(stdout, "readhdr fails at %d blocks\n", blksread);
2cb5dabb 880 return(FAIL);
5c5f44c7 881 }
2cb5dabb 882 return(GOOD);
f78e116c
KM
883}
884
885/*
886 * read the tape into buf, then return whether or
887 * or not it is a header block.
888 */
889gethead(buf)
890 struct s_spcl *buf;
891{
eb317d51 892 long i;
6e5750fa
KM
893 union {
894 quad_t qval;
895 long val[2];
896 } qcvt;
f78e116c
KM
897 union u_ospcl {
898 char dummy[TP_BSIZE];
899 struct s_ospcl {
2cb5dabb
KM
900 long c_type;
901 long c_date;
902 long c_ddate;
903 long c_volume;
904 long c_tapea;
cf6db0ab 905 u_short c_inumber;
2cb5dabb
KM
906 long c_magic;
907 long c_checksum;
f78e116c
KM
908 struct odinode {
909 unsigned short odi_mode;
cf6db0ab
KM
910 u_short odi_nlink;
911 u_short odi_uid;
912 u_short odi_gid;
2cb5dabb
KM
913 long odi_size;
914 long odi_rdev;
f78e116c 915 char odi_addr[36];
2cb5dabb
KM
916 long odi_atime;
917 long odi_mtime;
918 long odi_ctime;
f78e116c 919 } c_dinode;
2cb5dabb
KM
920 long c_count;
921 char c_addr[256];
f78e116c
KM
922 } s_ospcl;
923 } u_ospcl;
924
925 if (!cvtflag) {
926 readtape((char *)buf);
b4c41341
KS
927 if (buf->c_magic != NFS_MAGIC) {
928 if (swabl(buf->c_magic) != NFS_MAGIC)
929 return (FAIL);
930 if (!Bcvt) {
931 vprintf(stdout, "Note: Doing Byte swapping\n");
932 Bcvt = 1;
933 }
934 }
935 if (checksum((int *)buf) == FAIL)
936 return (FAIL);
937 if (Bcvt)
938 swabst("8l4s31l", (char *)buf);
5c5f44c7 939 goto good;
f78e116c
KM
940 }
941 readtape((char *)(&u_ospcl.s_ospcl));
942 bzero((char *)buf, (long)TP_BSIZE);
943 buf->c_type = u_ospcl.s_ospcl.c_type;
944 buf->c_date = u_ospcl.s_ospcl.c_date;
945 buf->c_ddate = u_ospcl.s_ospcl.c_ddate;
946 buf->c_volume = u_ospcl.s_ospcl.c_volume;
947 buf->c_tapea = u_ospcl.s_ospcl.c_tapea;
948 buf->c_inumber = u_ospcl.s_ospcl.c_inumber;
949 buf->c_checksum = u_ospcl.s_ospcl.c_checksum;
950 buf->c_magic = u_ospcl.s_ospcl.c_magic;
951 buf->c_dinode.di_mode = u_ospcl.s_ospcl.c_dinode.odi_mode;
952 buf->c_dinode.di_nlink = u_ospcl.s_ospcl.c_dinode.odi_nlink;
953 buf->c_dinode.di_uid = u_ospcl.s_ospcl.c_dinode.odi_uid;
954 buf->c_dinode.di_gid = u_ospcl.s_ospcl.c_dinode.odi_gid;
955 buf->c_dinode.di_size = u_ospcl.s_ospcl.c_dinode.odi_size;
956 buf->c_dinode.di_rdev = u_ospcl.s_ospcl.c_dinode.odi_rdev;
4f57cc9f
KM
957 buf->c_dinode.di_atime.ts_sec = u_ospcl.s_ospcl.c_dinode.odi_atime;
958 buf->c_dinode.di_mtime.ts_sec = u_ospcl.s_ospcl.c_dinode.odi_mtime;
959 buf->c_dinode.di_ctime.ts_sec = u_ospcl.s_ospcl.c_dinode.odi_ctime;
f78e116c 960 buf->c_count = u_ospcl.s_ospcl.c_count;
2cb5dabb 961 bcopy(u_ospcl.s_ospcl.c_addr, buf->c_addr, (long)256);
f78e116c 962 if (u_ospcl.s_ospcl.c_magic != OFS_MAGIC ||
5c5f44c7 963 checksum((int *)(&u_ospcl.s_ospcl)) == FAIL)
2cb5dabb 964 return(FAIL);
f78e116c 965 buf->c_magic = NFS_MAGIC;
5c5f44c7
KM
966
967good:
15822e16 968 if ((buf->c_dinode.di_size == 0 || buf->c_dinode.di_size > 0xfffffff) &&
025e61b9 969 (buf->c_dinode.di_mode & IFMT) == IFDIR && Qcvt == 0) {
6e5750fa
KM
970 qcvt.qval = buf->c_dinode.di_size;
971 if (qcvt.val[0] || qcvt.val[1]) {
b4c41341
KS
972 printf("Note: Doing Quad swapping\n");
973 Qcvt = 1;
974 }
975 }
976 if (Qcvt) {
6e5750fa
KM
977 qcvt.qval = buf->c_dinode.di_size;
978 i = qcvt.val[1];
979 qcvt.val[1] = qcvt.val[0];
980 qcvt.val[0] = i;
b4c41341 981 }
2532f4a9 982
5c5f44c7
KM
983 switch (buf->c_type) {
984
985 case TS_CLRI:
986 case TS_BITS:
987 /*
988 * Have to patch up missing information in bit map headers
989 */
990 buf->c_inumber = 0;
991 buf->c_dinode.di_size = buf->c_count * TP_BSIZE;
992 for (i = 0; i < buf->c_count; i++)
993 buf->c_addr[i]++;
994 break;
995
996 case TS_TAPE:
dad5e0ef
KM
997 if ((buf->c_flags & DR_NEWINODEFMT) == 0)
998 oldinofmt = 1;
999 /* fall through */
5c5f44c7
KM
1000 case TS_END:
1001 buf->c_inumber = 0;
1002 break;
1003
1004 case TS_INODE:
1005 case TS_ADDR:
1006 break;
1007
1008 default:
1009 panic("gethead: unknown inode type %d\n", buf->c_type);
1010 break;
1011 }
dad5e0ef
KM
1012 /*
1013 * If we are restoring a filesystem with old format inodes,
1014 * copy the uid/gid to the new location.
1015 */
1016 if (oldinofmt) {
1017 buf->c_dinode.di_uid = buf->c_dinode.di_ouid;
1018 buf->c_dinode.di_gid = buf->c_dinode.di_ogid;
1019 }
2cb5dabb
KM
1020 if (dflag)
1021 accthdr(buf);
1022 return(GOOD);
1023}
1024
1025/*
1026 * Check that a header is where it belongs and predict the next header
1027 */
1028accthdr(header)
1029 struct s_spcl *header;
1030{
7851e15d 1031 static ino_t previno = 0x7fffffff;
2cb5dabb
KM
1032 static int prevtype;
1033 static long predict;
1034 long blks, i;
1035
8d412b7c 1036 if (header->c_type == TS_TAPE) {
3b29a62c
KM
1037 fprintf(stderr, "Volume header");
1038 if (header->c_firstrec)
1039 fprintf(stderr, " begins with record %d",
1040 header->c_firstrec);
1041 fprintf(stderr, "\n");
b8f4e820 1042 previno = 0x7fffffff;
5c5f44c7
KM
1043 return;
1044 }
7851e15d 1045 if (previno == 0x7fffffff)
2cb5dabb
KM
1046 goto newcalc;
1047 switch (prevtype) {
2cb5dabb 1048 case TS_BITS:
5c5f44c7 1049 fprintf(stderr, "Dump mask header");
2cb5dabb
KM
1050 break;
1051 case TS_CLRI:
5c5f44c7 1052 fprintf(stderr, "Remove mask header");
2cb5dabb
KM
1053 break;
1054 case TS_INODE:
5c5f44c7 1055 fprintf(stderr, "File header, ino %d", previno);
2cb5dabb
KM
1056 break;
1057 case TS_ADDR:
5c5f44c7 1058 fprintf(stderr, "File continuation header, ino %d", previno);
2cb5dabb
KM
1059 break;
1060 case TS_END:
5c5f44c7 1061 fprintf(stderr, "End of tape header");
2cb5dabb
KM
1062 break;
1063 }
2cb5dabb
KM
1064 if (predict != blksread - 1)
1065 fprintf(stderr, "; predicted %d blocks, got %d blocks",
1066 predict, blksread - 1);
1067 fprintf(stderr, "\n");
1068newcalc:
1069 blks = 0;
5c5f44c7 1070 if (header->c_type != TS_END)
2cb5dabb
KM
1071 for (i = 0; i < header->c_count; i++)
1072 if (header->c_addr[i] != 0)
1073 blks++;
1074 predict = blks;
1075 blksread = 0;
1076 prevtype = header->c_type;
1077 previno = header->c_inumber;
f78e116c
KM
1078}
1079
1080/*
1081 * Find an inode header.
1082 * Complain if had to skip, and complain is set.
1083 */
b32320ae 1084findinode(header)
f78e116c 1085 struct s_spcl *header;
f78e116c 1086{
2cb5dabb 1087 static long skipcnt = 0;
b8f4e820
KM
1088 long i;
1089 char buf[TP_BSIZE];
f78e116c
KM
1090
1091 curfile.name = "<name unknown>";
1092 curfile.action = UNKNOWN;
1093 curfile.dip = (struct dinode *)NIL;
1094 curfile.ino = 0;
2cb5dabb
KM
1095 if (ishead(header) == FAIL) {
1096 skipcnt++;
7702e9a8 1097 while (gethead(header) == FAIL || header->c_date != dumpdate)
f78e116c 1098 skipcnt++;
2cb5dabb 1099 }
f78e116c 1100 for (;;) {
b8f4e820
KM
1101 if (checktype(header, TS_ADDR) == GOOD) {
1102 /*
1103 * Skip up to the beginning of the next record
1104 */
1105 for (i = 0; i < header->c_count; i++)
1106 if (header->c_addr[i])
1107 readtape(buf);
1108 (void) gethead(header);
1109 continue;
1110 }
2cb5dabb 1111 if (checktype(header, TS_INODE) == GOOD) {
f78e116c
KM
1112 curfile.dip = &header->c_dinode;
1113 curfile.ino = header->c_inumber;
1114 break;
1115 }
2cb5dabb 1116 if (checktype(header, TS_END) == GOOD) {
f78e116c
KM
1117 curfile.ino = maxino;
1118 break;
1119 }
2cb5dabb 1120 if (checktype(header, TS_CLRI) == GOOD) {
f78e116c 1121 curfile.name = "<file removal list>";
e108d3a2 1122 break;
f78e116c 1123 }
2cb5dabb 1124 if (checktype(header, TS_BITS) == GOOD) {
f78e116c 1125 curfile.name = "<file dump list>";
e108d3a2 1126 break;
f78e116c 1127 }
2cb5dabb 1128 while (gethead(header) == FAIL)
f78e116c
KM
1129 skipcnt++;
1130 }
b32320ae 1131 if (skipcnt > 0)
dd1900f1 1132 fprintf(stderr, "resync restore, skipped %d blocks\n", skipcnt);
f78e116c
KM
1133 skipcnt = 0;
1134}
1135
1136/*
1137 * return whether or not the buffer contains a header block
1138 */
1139ishead(buf)
1140 struct s_spcl *buf;
1141{
1142
1143 if (buf->c_magic != NFS_MAGIC)
2cb5dabb
KM
1144 return(FAIL);
1145 return(GOOD);
f78e116c
KM
1146}
1147
1148checktype(b, t)
1149 struct s_spcl *b;
1150 int t;
1151{
1152
2cb5dabb
KM
1153 if (b->c_type != t)
1154 return(FAIL);
1155 return(GOOD);
f78e116c
KM
1156}
1157
1158checksum(b)
1159 register int *b;
1160{
1161 register int i, j;
1162
1163 j = sizeof(union u_spcl) / sizeof(int);
1164 i = 0;
b4c41341
KS
1165 if(!Bcvt) {
1166 do
1167 i += *b++;
1168 while (--j);
1169 } else {
1170 /* What happens if we want to read restore tapes
1171 for a 16bit int machine??? */
1172 do
1173 i += swabl(*b++);
1174 while (--j);
1175 }
1176
f78e116c
KM
1177 if (i != CHECKSUM) {
1178 fprintf(stderr, "Checksum error %o, inode %d file %s\n", i,
1179 curfile.ino, curfile.name);
2cb5dabb 1180 return(FAIL);
f78e116c 1181 }
2cb5dabb 1182 return(GOOD);
f78e116c
KM
1183}
1184
e9e7ecc4 1185#ifdef RRESTORE
74025ab9 1186/* VARARGS1 */
f78e116c
KM
1187msg(cp, a1, a2, a3)
1188 char *cp;
1189{
1190
1191 fprintf(stderr, cp, a1, a2, a3);
1192}
2532f4a9 1193#endif /* RRESTORE */
b4c41341 1194
025e61b9
KM
1195u_char *
1196swabshort(sp, n)
1197 register u_char *sp;
1198 register int n;
1199{
1200 char c;
1201
1202 while (--n >= 0) {
1203 c = sp[0]; sp[0] = sp[1]; sp[1] = c;
1204 sp += 2;
1205 }
1206 return (sp);
1207}
1208
1209u_char *
1210swablong(sp, n)
1211 register u_char *sp;
1212 register int n;
1213{
1214 char c;
1215
1216 while (--n >= 0) {
1217 c = sp[0]; sp[0] = sp[3]; sp[3] = c;
1218 c = sp[2]; sp[2] = sp[1]; sp[1] = c;
1219 sp += 4;
1220 }
1221 return (sp);
1222}
1223
b4c41341 1224swabst(cp, sp)
025e61b9 1225 register u_char *cp, *sp;
b4c41341
KS
1226{
1227 int n = 0;
025e61b9
KM
1228 u_char c;
1229
1230 while (*cp) {
b4c41341
KS
1231 switch (*cp) {
1232 case '0': case '1': case '2': case '3': case '4':
1233 case '5': case '6': case '7': case '8': case '9':
1234 n = (n * 10) + (*cp++ - '0');
1235 continue;
1236
1237 case 's': case 'w': case 'h':
025e61b9
KM
1238 if (n == 0)
1239 n = 1;
1240 sp = swabshort(sp, n);
b4c41341
KS
1241 break;
1242
1243 case 'l':
025e61b9
KM
1244 if (n == 0)
1245 n = 1;
1246 sp = swablong(sp, n);
1247 break;
1248
1249 default: /* Any other character, like 'b' counts as byte. */
1250 if (n == 0)
1251 n = 1;
1252 sp += n;
1253 break;
b4c41341 1254 }
025e61b9
KM
1255 cp++;
1256 n = 0;
b4c41341
KS
1257 }
1258}
025e61b9
KM
1259
1260u_long
1261swabl(x)
1262 u_long x;
1263{
1264 swabst("l", (char *)&x);
1265 return (x);
1266}
7f327977
KM
1267
1268#ifdef sunos
1269char *
1270strerror(errnum)
1271 int errnum;
1272{
1273 extern int sys_nerr;
1274 extern char *sys_errlist[];
1275
1276 if (errnum < sys_nerr) {
1277 return(sys_errlist[errnum]);
1278 } else {
1279 return("bogus errno in strerror");
1280 }
1281}
1282#endif