2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 static char sccsid[] = "@(#)tape.c 8.9 (Berkeley) 5/1/95";
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/param.h>
50 #include <ufs/ufs/dinode.h>
51 #include <protocols/dumprestore.h>
67 static long fssize = MAXBSIZE;
69 static int pipein = 0;
70 static int pipecmdin = 0;
71 static FILE *popenfp = NULL;
76 static union u_spcl endoftapemark;
77 static long blksread; /* blocks read since last header */
78 static int64_t tapeaddr = 0; /* current TP_BSIZE tape record */
79 static long tapesread;
80 static jmp_buf restart;
81 static int gettingfile = 0; /* restart has a valid frame */
82 static char *host = NULL;
83 static int readmapflag;
87 static char lnkbuf[MAXPATHLEN + 1];
90 int Bcvt; /* Swap Bytes */
91 int oldinofmt; /* FreeBSD 1 inode format needs cvt */
93 #define FLUSHTAPEBUF() blkcnt = ntrec + 1
95 static void accthdr(struct s_spcl *);
96 static int checksum(int *);
97 static void findinode(struct s_spcl *);
98 static void findtapeblksize(void);
99 static int gethead(struct s_spcl *);
100 static void readtape(char *);
101 static void setdumpnum(void);
102 static u_long swabl(u_long);
103 static u_char *swablong(u_char *, int);
104 static u_char *swabshort(u_char *, int);
105 static void terminateinput(void);
106 static void xtrfile(char *, long);
107 static void xtrlnkfile(char *, long);
108 static void xtrlnkskip(char *, long);
109 static void xtrmap(char *, long);
110 static void xtrmapskip(char *, long);
111 static void xtrskip(char *, long);
114 * Set up an input source
117 setinput(char *source, int ispipecommand)
123 newtapebuf(NTREC > HIGHDENSITYTREC ? NTREC : HIGHDENSITYTREC);
130 if (strchr(source, ':')) {
132 source = strchr(host, ':');
134 if (rmthost(host) == 0)
138 if (strcmp(source, "-") == 0) {
140 * Since input is coming from a pipe we must establish
141 * our own connection to the terminal.
143 terminal = fopen(_PATH_TTY, "r");
144 if (terminal == NULL) {
145 (void)fprintf(stderr, "cannot open %s: %s\n",
146 _PATH_TTY, strerror(errno));
147 terminal = fopen(_PATH_DEVNULL, "r");
148 if (terminal == NULL) {
149 (void)fprintf(stderr, "cannot open %s: %s\n",
150 _PATH_DEVNULL, strerror(errno));
156 setuid(getuid()); /* no longer need or want root privileges */
157 magtape = strdup(source);
158 if (magtape == NULL) {
159 fprintf(stderr, "Cannot allocate space for magtape buffer\n");
165 newtapebuf(long size)
167 static int tapebufsize = -1;
170 if (size <= tapebufsize)
174 tapebuf = malloc(size * TP_BSIZE);
175 if (tapebuf == NULL) {
176 fprintf(stderr, "Cannot allocate space for tape buffer\n");
183 * Verify that the tape drive can be accessed and
184 * that it actually is a dump tape.
192 vprintf(stdout, "Verify tape and initialize maps\n");
194 if (setenv("RESTORE_VOLUME", "1", 1) == -1) {
195 fprintf(stderr, "Cannot set $RESTORE_VOLUME: %s\n",
199 popenfp = popen(magtape, "r");
200 mt = popenfp ? fileno(popenfp) : -1;
204 mt = rmtopen(magtape, 0);
210 mt = open(magtape, O_RDONLY, 0);
212 fprintf(stderr, "%s: %s\n", magtape, strerror(errno));
218 if (!pipein && !bflag)
220 if (gethead(&spcl) == FAIL) {
221 fprintf(stderr, "Tape is not a dump tape\n");
225 endoftapemark.s_spcl.c_magic = FS_UFS2_MAGIC;
226 endoftapemark.s_spcl.c_type = TS_END;
227 ip = (int *)&endoftapemark;
228 j = sizeof(union u_spcl) / sizeof(int);
233 endoftapemark.s_spcl.c_checksum = CHECKSUM - i;
235 if (vflag || command == 't')
237 dumptime = _time64_to_time(spcl.c_ddate);
238 dumpdate = _time64_to_time(spcl.c_date);
239 if (stat(".", &stbuf) < 0) {
240 fprintf(stderr, "cannot stat .: %s\n", strerror(errno));
243 if (stbuf.st_blksize > 0 && stbuf.st_blksize < TP_BSIZE )
245 if (stbuf.st_blksize >= TP_BSIZE && stbuf.st_blksize <= MAXBSIZE)
246 fssize = stbuf.st_blksize;
247 if (((fssize - 1) & fssize) != 0) {
248 fprintf(stderr, "bad block size %ld\n", fssize);
251 if (spcl.c_volume != 1) {
252 fprintf(stderr, "Tape is not volume 1 of the dump\n");
255 if (gethead(&spcl) == FAIL) {
256 dprintf(stdout, "header read failed at %ld blocks\n", blksread);
257 panic("no header after volume mark!\n");
260 if (spcl.c_type != TS_CLRI) {
261 fprintf(stderr, "Cannot find file removal list\n");
264 maxino = (spcl.c_count * TP_BSIZE * NBBY) + 1;
265 dprintf(stdout, "maxino = %d\n", maxino);
266 map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
268 panic("no memory for active inode map\n");
270 curfile.action = USING;
271 getfile(xtrmap, xtrmapskip);
272 if (spcl.c_type != TS_BITS) {
273 fprintf(stderr, "Cannot find file dump list\n");
276 map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
277 if (map == (char *)NULL)
278 panic("no memory for file dump list\n");
280 curfile.action = USING;
281 getfile(xtrmap, xtrmapskip);
283 * If there may be whiteout entries on the tape, pretend that the
284 * whiteout inode exists, so that the whiteout entries can be
287 SETINO(WINO, dumpmap);
288 /* 'r' restores don't call getvol() for tape 1, so mark it as read. */
294 * Prompt user to load a new dump volume.
295 * "Nextvol" is the next suggested volume to use.
296 * This suggested volume is enforced when doing full
297 * or incremental restores, but can be overridden by
298 * the user when only extracting a subset of the files.
304 long i, newvol, savecnt;
305 union u_spcl tmpspcl;
306 # define tmpbuf tmpspcl.s_spcl
313 prevtapea = tapeaddr;
317 panic("Changing volumes on pipe input?\n");
318 /* Avoid looping if we couldn't ask the user. */
319 if (yflag || ferror(terminal) || feof(terminal))
332 done(1); /* pipes do not get a second chance */
333 if (command == 'R' || command == 'r' || curfile.action != SKIP)
337 while (newvol <= 0) {
338 if (tapesread == 0) {
339 fprintf(stderr, "%s%s%s%s%s%s%s",
340 "You have not read any tapes yet.\n",
341 "If you are extracting just a few files,",
342 " start with the last volume\n",
343 "and work towards the first; restore",
344 " can quickly skip tapes that\n",
345 "have no further files to extract.",
346 " Otherwise, begin with volume 1.\n");
348 fprintf(stderr, "You have read volumes");
350 for (i = 0; i < 32; i++)
351 if (tapesread & (1 << i)) {
352 fprintf(stderr, "%s%ld", buf, i + 1);
355 fprintf(stderr, "\n");
358 fprintf(stderr, "Specify next volume #: ");
359 (void) fflush(stderr);
360 if (fgets(buf, BUFSIZ, terminal) == NULL)
362 } while (buf[0] == '\n');
366 "Volume numbers are positive numerics\n");
369 if (newvol == volno) {
370 tapesread |= 1 << (volno - 1);
374 fprintf(stderr, "Mount tape volume %ld\n", newvol);
375 fprintf(stderr, "Enter ``none'' if there are no more tapes\n");
376 fprintf(stderr, "otherwise enter tape name (default: %s) ", magtape);
377 (void) fflush(stderr);
378 if (fgets(buf, BUFSIZ, terminal) == NULL)
380 if (!strcmp(buf, "none\n")) {
384 if (buf[0] != '\n') {
385 (void) strcpy(magtape, buf);
386 magtape[strlen(magtape) - 1] = '\0';
389 char volno[sizeof("2147483647")];
392 (void)sprintf(volno, "%d", newvol);
393 if (setenv("RESTORE_VOLUME", volno, 1) == -1) {
394 fprintf(stderr, "Cannot set $RESTORE_VOLUME: %s\n",
398 popenfp = popen(magtape, "r");
399 mt = popenfp ? fileno(popenfp) : -1;
403 mt = rmtopen(magtape, 0);
406 mt = open(magtape, O_RDONLY, 0);
409 fprintf(stderr, "Cannot open %s\n", magtape);
417 if (gethead(&tmpbuf) == FAIL) {
418 dprintf(stdout, "header read failed at %ld blocks\n", blksread);
419 fprintf(stderr, "tape is not dump tape\n");
423 if (tmpbuf.c_volume != volno) {
424 fprintf(stderr, "Wrong volume (%ld)\n", tmpbuf.c_volume);
428 if (_time64_to_time(tmpbuf.c_date) != dumpdate ||
429 _time64_to_time(tmpbuf.c_ddate) != dumptime) {
430 time_t t = _time64_to_time(tmpbuf.c_date);
431 fprintf(stderr, "Wrong dump date\n\tgot: %s", ctime(&t));
432 fprintf(stderr, "\twanted: %s", ctime(&dumpdate));
436 tapesread |= 1 << (volno - 1);
439 * If continuing from the previous volume, skip over any
440 * blocks read already at the end of the previous volume.
442 * If coming to this volume at random, skip to the beginning
443 * of the next record.
445 dprintf(stdout, "last rec %qd, tape starts with %qd\n", prevtapea,
447 if (tmpbuf.c_type == TS_TAPE) {
448 if (curfile.action != USING) {
450 * XXX Dump incorrectly sets c_count to 1 in the
451 * volume header of the first tape, so ignore
452 * c_count when volno == 1.
455 for (i = tmpbuf.c_count; i > 0; i--)
457 } else if (tmpbuf.c_tapea <= prevtapea) {
459 * Normally the value of c_tapea in the volume
460 * header is the record number of the header itself.
461 * However in the volume header following an EOT-
462 * terminated tape, it is the record number of the
463 * first continuation data block (dump bug?).
465 * The next record we want is `prevtapea + 1'.
467 i = prevtapea + 1 - tmpbuf.c_tapea;
468 dprintf(stderr, "Skipping %ld duplicate record%s.\n",
469 i, i > 1 ? "s" : "");
474 if (curfile.action == USING) {
476 panic("active file into volume 1\n");
479 (void) gethead(&spcl);
488 * Handle unexpected EOF.
494 if (gettingfile && curfile.action == USING) {
495 printf("Warning: %s %s\n",
496 "End-of-input encountered while extracting", curfile.name);
498 curfile.name = "<name unknown>";
499 curfile.action = UNKNOWN;
501 curfile.ino = maxino;
509 * handle multiple dumps per tape by skipping forward to the
517 if (dumpnum == 1 || volno != 1)
520 fprintf(stderr, "Cannot have multiple dumps on pipe input\n");
524 tcom.mt_count = dumpnum - 1;
527 rmtioctl(MTFSF, dumpnum - 1);
530 if (!pipecmdin && ioctl(mt, MTIOCTOP, (char *)&tcom) < 0)
531 fprintf(stderr, "ioctl MTFSF: %s\n", strerror(errno));
538 t = _time64_to_time(spcl.c_date);
539 fprintf(stdout, "Dump date: %s", ctime(&t));
540 t = _time64_to_time(spcl.c_ddate);
541 fprintf(stdout, "Dumped from: %s",
542 (spcl.c_ddate == 0) ? "the epoch\n" : ctime(&t));
543 if (spcl.c_host[0] == '\0')
545 fprintf(stderr, "Level %ld dump of %s on %s:%s\n",
546 spcl.c_level, spcl.c_filesys, spcl.c_host, spcl.c_dev);
547 fprintf(stderr, "Label: %s\n", spcl.c_label);
551 extractfile(char *name)
555 struct timeval mtimep[2], ctimep[2];
559 curfile.action = USING;
560 mtimep[0].tv_sec = curfile.atime_sec;
561 mtimep[0].tv_usec = curfile.atime_nsec / 1000;
562 mtimep[1].tv_sec = curfile.mtime_sec;
563 mtimep[1].tv_usec = curfile.mtime_nsec / 1000;
564 ctimep[0].tv_sec = curfile.atime_sec;
565 ctimep[0].tv_usec = curfile.atime_nsec / 1000;
566 ctimep[1].tv_sec = curfile.birthtime_sec;
567 ctimep[1].tv_usec = curfile.birthtime_nsec / 1000;
569 flags = curfile.file_flags;
570 switch (mode & IFMT) {
573 fprintf(stderr, "%s: unknown file mode 0%o\n", name, mode);
578 vprintf(stdout, "skipped socket %s\n", name);
584 ep = lookupname(name);
585 if (ep == NULL || ep->e_flags & EXTRACT)
586 panic("unextracted directory %s\n", name);
590 vprintf(stdout, "extract file %s\n", name);
591 return (genliteraldir(name, curfile.ino));
596 getfile(xtrlnkfile, xtrlnkskip);
599 "%s: zero length symbolic link (ignored)\n", name);
602 if (linkit(lnkbuf, name, SYMLINK) == GOOD) {
603 (void) lchown(name, curfile.uid, curfile.gid);
604 (void) lchmod(name, mode);
605 (void) lutimes(name, ctimep);
606 (void) lutimes(name, mtimep);
612 vprintf(stdout, "extract fifo %s\n", name);
619 if (mkfifo(name, mode) < 0) {
620 fprintf(stderr, "%s: cannot create fifo: %s\n",
621 name, strerror(errno));
625 (void) chown(name, curfile.uid, curfile.gid);
626 (void) chmod(name, mode);
627 (void) utimes(name, ctimep);
628 (void) utimes(name, mtimep);
629 (void) chflags(name, flags);
635 vprintf(stdout, "extract special file %s\n", name);
642 if (mknod(name, mode, (int)curfile.rdev) < 0) {
643 fprintf(stderr, "%s: cannot create special file: %s\n",
644 name, strerror(errno));
648 (void) chown(name, curfile.uid, curfile.gid);
649 (void) chmod(name, mode);
650 (void) utimes(name, ctimep);
651 (void) utimes(name, mtimep);
652 (void) chflags(name, flags);
657 vprintf(stdout, "extract file %s\n", name);
664 if ((ofile = open(name, O_WRONLY | O_CREAT | O_TRUNC,
666 fprintf(stderr, "%s: cannot create file: %s\n",
667 name, strerror(errno));
671 (void) fchown(ofile, curfile.uid, curfile.gid);
672 (void) fchmod(ofile, mode);
673 getfile(xtrfile, xtrskip);
675 (void) utimes(name, ctimep);
676 (void) utimes(name, mtimep);
677 (void) chflags(name, flags);
684 * skip over bit maps on the tape
690 while (spcl.c_type == TS_BITS || spcl.c_type == TS_CLRI)
695 * skip over a file on the tape
701 curfile.action = SKIP;
702 getfile(xtrnull, xtrnull);
706 * Extract a file from the tape.
707 * When an allocated block is found it is passed to the fill function;
708 * when an unallocated block (hole) is found, a zeroed buffer is passed
709 * to the skip function.
712 getfile(void (*fill)(char *, long), void (*skip)(char *, long))
716 quad_t size = spcl.c_size;
717 static char clearedbuf[MAXBSIZE];
718 char buf[MAXBSIZE / TP_BSIZE][TP_BSIZE];
721 if (spcl.c_type == TS_END)
722 panic("ran off end of tape\n");
723 if (spcl.c_magic != FS_UFS2_MAGIC)
724 panic("not at beginning of a file\n");
725 if (!gettingfile && setjmp(restart) != 0)
729 for (i = 0; i < spcl.c_count; i++) {
730 if (readmapflag || spcl.c_addr[i]) {
731 readtape(&buf[curblk++][0]);
732 if (curblk == fssize / TP_BSIZE) {
733 (*fill)((char *)buf, (long)(size > TP_BSIZE ?
734 fssize : (curblk - 1) * TP_BSIZE + size));
739 (*fill)((char *)buf, (long)(size > TP_BSIZE ?
741 (curblk - 1) * TP_BSIZE + size));
744 (*skip)(clearedbuf, (long)(size > TP_BSIZE ?
747 if ((size -= TP_BSIZE) <= 0) {
748 for (i++; i < spcl.c_count; i++)
749 if (readmapflag || spcl.c_addr[i])
754 if (gethead(&spcl) == GOOD && size > 0) {
755 if (spcl.c_type == TS_ADDR)
758 "Missing address (header) block for %s at %ld blocks\n",
759 curfile.name, blksread);
762 (*fill)((char *)buf, (long)((curblk * TP_BSIZE) + size));
768 * Write out the next block of a file.
771 xtrfile(char *buf, long size)
776 if (write(ofile, buf, (int) size) == -1) {
778 "write error extracting inode %d, name %s\nwrite: %s\n",
779 curfile.ino, curfile.name, strerror(errno));
784 * Skip over a hole in a file.
788 xtrskip(char *buf, long size)
791 if (lseek(ofile, size, SEEK_CUR) == -1) {
793 "seek error extracting inode %d, name %s\nlseek: %s\n",
794 curfile.ino, curfile.name, strerror(errno));
800 * Collect the next block of a symbolic link.
803 xtrlnkfile(char *buf, long size)
807 if (pathlen > MAXPATHLEN) {
808 fprintf(stderr, "symbolic link name: %s->%s%s; too long %d\n",
809 curfile.name, lnkbuf, buf, pathlen);
812 (void) strcat(lnkbuf, buf);
816 * Skip over a hole in a symbolic link (should never happen).
820 xtrlnkskip(char *buf, long size)
823 fprintf(stderr, "unallocated block in symbolic link %s\n",
829 * Collect the next block of a bit map.
832 xtrmap(char *buf, long size)
835 memmove(map, buf, size);
840 * Skip over a hole in a bit map (should never happen).
844 xtrmapskip(char *buf, long size)
847 panic("hole in map\n");
852 * Noop, when an extraction function is not needed.
856 xtrnull(char *buf, long size)
863 * Read TP_BSIZE blocks from the input.
864 * Handle read errors, and end of media.
870 int cnt, seek_failed;
872 if (blkcnt < numtrec) {
873 memmove(buf, &tapebuf[(blkcnt++ * TP_BSIZE)], (long)TP_BSIZE);
878 for (i = 0; i < ntrec; i++)
879 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
882 cnt = ntrec * TP_BSIZE;
887 i = rmtread(&tapebuf[rd], cnt);
890 i = read(mt, &tapebuf[rd], cnt);
892 * Check for mid-tape short read error.
893 * If found, skip rest of buffer and start with the next.
895 if (!pipein && numtrec < ntrec && i > 0) {
896 dprintf(stdout, "mid-media short read error.\n");
900 * Handle partial block read.
902 if (pipein && i == 0 && rd > 0)
904 else if (i > 0 && i != ntrec * TP_BSIZE) {
913 * Short read. Process the blocks read.
915 if (i % TP_BSIZE != 0)
917 "partial block read: %ld should be %ld\n",
918 i, ntrec * TP_BSIZE);
919 numtrec = i / TP_BSIZE;
926 fprintf(stderr, "Tape read error while ");
927 switch (curfile.action) {
929 fprintf(stderr, "trying to set up tape\n");
932 fprintf(stderr, "trying to resynchronize\n");
935 fprintf(stderr, "restoring %s\n", curfile.name);
938 fprintf(stderr, "skipping over inode %d\n",
942 if (!yflag && !reply("continue"))
944 i = ntrec * TP_BSIZE;
945 memset(tapebuf, 0, i);
948 seek_failed = (rmtseek(i, 1) < 0);
951 seek_failed = (lseek(mt, i, SEEK_CUR) == (off_t)-1);
955 "continuation failed: %s\n", strerror(errno));
960 * Handle end of tape.
963 vprintf(stdout, "End-of-tape encountered\n");
972 if (rd % TP_BSIZE != 0)
973 panic("partial block read: %d should be %d\n",
974 rd, ntrec * TP_BSIZE);
976 memmove(&tapebuf[rd], &endoftapemark, (long)TP_BSIZE);
979 memmove(buf, &tapebuf[(blkcnt++ * TP_BSIZE)], (long)TP_BSIZE);
985 findtapeblksize(void)
989 for (i = 0; i < ntrec; i++)
990 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
994 i = rmtread(tapebuf, ntrec * TP_BSIZE);
997 i = read(mt, tapebuf, ntrec * TP_BSIZE);
1000 fprintf(stderr, "tape read error: %s\n", strerror(errno));
1003 if (i % TP_BSIZE != 0) {
1004 fprintf(stderr, "Tape block size (%ld) %s (%d)\n",
1005 i, "is not a multiple of dump block size", TP_BSIZE);
1008 ntrec = i / TP_BSIZE;
1010 vprintf(stdout, "Tape block size is %ld\n", ntrec);
1032 * Read the next block from the tape.
1033 * If it is not any valid header, return an error.
1036 gethead(struct s_spcl *buf)
1040 readtape((char *)buf);
1041 if (buf->c_magic != FS_UFS2_MAGIC && buf->c_magic != NFS_MAGIC) {
1042 if (buf->c_magic == OFS_MAGIC) {
1044 "Format of dump tape is too old. Must use\n");
1046 "a version of restore from before 2002.\n");
1049 if (swabl(buf->c_magic) != FS_UFS2_MAGIC &&
1050 buf->c_magic != NFS_MAGIC) {
1051 if (buf->c_magic == OFS_MAGIC) {
1053 "Format of dump tape is too old. Must use\n");
1055 "a version of restore from before 2002.\n");
1060 vprintf(stdout, "Note: Doing Byte swapping\n");
1064 if (checksum((int *)buf) == FAIL)
1067 swabst((u_char *)"8l4s1q8l2q17l", (u_char *)buf);
1068 swabst((u_char *)"l",(u_char *) &buf->c_level);
1069 swabst((u_char *)"2l4q",(u_char *) &buf->c_flags);
1073 switch (buf->c_type) {
1078 * Have to patch up missing information in bit map headers
1081 buf->c_size = buf->c_count * TP_BSIZE;
1082 if (buf->c_count > TP_NINDIR)
1085 for (i = 0; i < buf->c_count; i++)
1090 if (buf->c_magic == NFS_MAGIC) {
1091 if ((buf->c_flags & NFS_DR_NEWINODEFMT) == 0)
1093 buf->c_date = _time32_to_time(buf->c_old_date);
1094 buf->c_ddate = _time32_to_time(buf->c_old_ddate);
1095 buf->c_tapea = buf->c_old_tapea;
1096 buf->c_firstrec = buf->c_old_firstrec;
1104 * For old dump tapes, have to copy up old fields to
1107 if (buf->c_magic == NFS_MAGIC) {
1108 buf->c_tapea = buf->c_old_tapea;
1109 buf->c_firstrec = buf->c_old_firstrec;
1110 buf->c_date = _time32_to_time(buf->c_old_date);
1111 buf->c_ddate = _time32_to_time(buf->c_old_ddate);
1112 buf->c_atime = _time32_to_time(buf->c_old_atime);
1113 buf->c_mtime = _time32_to_time(buf->c_old_mtime);
1121 panic("gethead: unknown inode type %d\n", buf->c_type);
1125 * If we're restoring a filesystem with the old (FreeBSD 1)
1126 * format inodes, copy the uid/gid to the new location
1129 buf->c_uid = buf->c_spare1[1];
1130 buf->c_gid = buf->c_spare1[2];
1132 buf->c_magic = FS_UFS2_MAGIC;
1133 tapeaddr = buf->c_tapea;
1140 * Check that a header is where it belongs and predict the next header
1143 accthdr(struct s_spcl *header)
1145 static ino_t previno = 0x7fffffff;
1146 static int prevtype;
1147 static long predict;
1150 if (header->c_type == TS_TAPE) {
1151 fprintf(stderr, "Volume header ");
1152 if (header->c_firstrec)
1153 fprintf(stderr, "begins with record %qd",
1154 header->c_firstrec);
1155 fprintf(stderr, "\n");
1156 previno = 0x7fffffff;
1159 if (previno == 0x7fffffff)
1163 fprintf(stderr, "Dumped inodes map header");
1166 fprintf(stderr, "Used inodes map header");
1169 fprintf(stderr, "File header, ino %d", previno);
1172 fprintf(stderr, "File continuation header, ino %d", previno);
1175 fprintf(stderr, "End of tape header");
1178 if (predict != blksread - 1)
1179 fprintf(stderr, "; predicted %ld blocks, got %ld blocks",
1180 predict, blksread - 1);
1181 fprintf(stderr, "\n");
1184 if (header->c_type != TS_END)
1185 for (i = 0; i < header->c_count; i++)
1186 if (readmapflag || header->c_addr[i] != 0)
1190 prevtype = header->c_type;
1191 previno = header->c_inumber;
1195 * Find an inode header.
1196 * Complain if had to skip.
1199 findinode(struct s_spcl *header)
1201 static long skipcnt = 0;
1206 curfile.name = "<name unknown>";
1207 curfile.action = UNKNOWN;
1211 htype = header->c_type;
1216 * Skip up to the beginning of the next record
1218 for (i = 0; i < header->c_count; i++)
1219 if (header->c_addr[i])
1221 while (gethead(header) == FAIL ||
1222 _time64_to_time(header->c_date) != dumpdate)
1227 curfile.mode = header->c_mode;
1228 curfile.uid = header->c_uid;
1229 curfile.gid = header->c_gid;
1230 curfile.file_flags = header->c_file_flags;
1231 curfile.rdev = header->c_rdev;
1232 curfile.atime_sec = header->c_atime;
1233 curfile.atime_nsec = header->c_atimensec;
1234 curfile.mtime_sec = header->c_mtime;
1235 curfile.mtime_nsec = header->c_mtimensec;
1236 curfile.birthtime_sec = header->c_birthtime;
1237 curfile.birthtime_nsec = header->c_birthtimensec;
1238 curfile.size = header->c_size;
1239 curfile.ino = header->c_inumber;
1243 /* If we missed some tapes, get another volume. */
1244 if (tapesread & (tapesread + 1)) {
1248 curfile.ino = maxino;
1252 curfile.name = "<file removal list>";
1256 curfile.name = "<file dump list>";
1260 panic("unexpected tape header\n");
1264 panic("unknown tape header type %d\n", spcl.c_type);
1268 } while (htype == TS_ADDR);
1270 fprintf(stderr, "resync restore, skipped %ld blocks\n",
1280 j = sizeof(union u_spcl) / sizeof(int);
1287 /* What happens if we want to read restore tapes
1288 for a 16bit int machine??? */
1294 if (i != CHECKSUM) {
1295 fprintf(stderr, "Checksum error %o, inode %d file %s\n", i,
1296 curfile.ino, curfile.name);
1306 msg(const char *fmt, ...)
1310 (void)vfprintf(stderr, fmt, ap);
1313 #endif /* RRESTORE */
1316 swabshort(u_char *sp, int n)
1321 c = sp[0]; sp[0] = sp[1]; sp[1] = c;
1328 swablong(u_char *sp, int n)
1333 c = sp[0]; sp[0] = sp[3]; sp[3] = c;
1334 c = sp[2]; sp[2] = sp[1]; sp[1] = c;
1341 swabquad(u_char *sp, int n)
1346 c = sp[0]; sp[0] = sp[7]; sp[7] = c;
1347 c = sp[1]; sp[1] = sp[6]; sp[6] = c;
1348 c = sp[2]; sp[2] = sp[5]; sp[5] = c;
1349 c = sp[3]; sp[3] = sp[4]; sp[4] = c;
1356 swabst(u_char *cp, u_char *sp)
1362 case '0': case '1': case '2': case '3': case '4':
1363 case '5': case '6': case '7': case '8': case '9':
1364 n = (n * 10) + (*cp++ - '0');
1367 case 's': case 'w': case 'h':
1370 sp = swabshort(sp, n);
1376 sp = swablong(sp, n);
1382 sp = swabquad(sp, n);
1392 fprintf(stderr, "Unknown conversion character: %c\n",
1405 swabst((u_char *)"l", (u_char *)&x);