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