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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 static char sccsid[] = "@(#)tape.c 8.9 (Berkeley) 5/1/95";
43 static const char rcsid[] =
47 #include <sys/param.h>
53 #include <ufs/ufs/dinode.h>
54 #include <protocols/dumprestore.h>
69 static long fssize = MAXBSIZE;
71 static int pipein = 0;
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 */
92 #define FLUSHTAPEBUF() blkcnt = ntrec + 1
94 static void accthdr(struct s_spcl *);
95 static int checksum(int *);
96 static void findinode(struct s_spcl *);
97 static void findtapeblksize(void);
98 static int gethead(struct s_spcl *);
99 static void readtape(char *);
100 static void setdumpnum(void);
101 static u_long swabl(u_long);
102 static u_char *swablong(u_char *, int);
103 static u_char *swabshort(u_char *, int);
104 static void terminateinput(void);
105 static void xtrfile(char *, long);
106 static void xtrlnkfile(char *, long);
107 static void xtrlnkskip(char *, long);
108 static void xtrmap(char *, long);
109 static void xtrmapskip(char *, long);
110 static void xtrskip(char *, long);
113 * Set up an input source
116 setinput(char *source)
122 newtapebuf(NTREC > HIGHDENSITYTREC ? NTREC : HIGHDENSITYTREC);
126 if (strchr(source, ':')) {
128 source = strchr(host, ':');
130 if (rmthost(host) == 0)
134 if (strcmp(source, "-") == 0) {
136 * Since input is coming from a pipe we must establish
137 * our own connection to the terminal.
139 terminal = fopen(_PATH_TTY, "r");
140 if (terminal == NULL) {
141 (void)fprintf(stderr, "cannot open %s: %s\n",
142 _PATH_TTY, strerror(errno));
143 terminal = fopen(_PATH_DEVNULL, "r");
144 if (terminal == NULL) {
145 (void)fprintf(stderr, "cannot open %s: %s\n",
146 _PATH_DEVNULL, strerror(errno));
152 setuid(getuid()); /* no longer need or want root privileges */
153 magtape = strdup(source);
154 if (magtape == NULL) {
155 fprintf(stderr, "Cannot allocate space for magtape buffer\n");
161 newtapebuf(long size)
163 static int tapebufsize = -1;
166 if (size <= tapebufsize)
170 tapebuf = malloc(size * TP_BSIZE);
171 if (tapebuf == NULL) {
172 fprintf(stderr, "Cannot allocate space for tape buffer\n");
179 * Verify that the tape drive can be accessed and
180 * that it actually is a dump tape.
188 vprintf(stdout, "Verify tape and initialize maps\n");
191 mt = rmtopen(magtape, 0);
197 mt = open(magtape, O_RDONLY, 0);
199 fprintf(stderr, "%s: %s\n", magtape, strerror(errno));
205 if (!pipein && !bflag)
207 if (gethead(&spcl) == FAIL) {
208 fprintf(stderr, "Tape is not a dump tape\n");
212 endoftapemark.s_spcl.c_magic = FS_UFS2_MAGIC;
213 endoftapemark.s_spcl.c_type = TS_END;
214 ip = (int *)&endoftapemark;
215 j = sizeof(union u_spcl) / sizeof(int);
220 endoftapemark.s_spcl.c_checksum = CHECKSUM - i;
222 if (vflag || command == 't')
224 dumptime = _time64_to_time(spcl.c_ddate);
225 dumpdate = _time64_to_time(spcl.c_date);
226 if (stat(".", &stbuf) < 0) {
227 fprintf(stderr, "cannot stat .: %s\n", strerror(errno));
230 if (stbuf.st_blksize > 0 && stbuf.st_blksize < TP_BSIZE )
232 if (stbuf.st_blksize >= TP_BSIZE && stbuf.st_blksize <= MAXBSIZE)
233 fssize = stbuf.st_blksize;
234 if (((fssize - 1) & fssize) != 0) {
235 fprintf(stderr, "bad block size %ld\n", fssize);
238 if (spcl.c_volume != 1) {
239 fprintf(stderr, "Tape is not volume 1 of the dump\n");
242 if (gethead(&spcl) == FAIL) {
243 dprintf(stdout, "header read failed at %ld blocks\n", blksread);
244 panic("no header after volume mark!\n");
247 if (spcl.c_type != TS_CLRI) {
248 fprintf(stderr, "Cannot find file removal list\n");
251 maxino = (spcl.c_count * TP_BSIZE * NBBY) + 1;
252 dprintf(stdout, "maxino = %d\n", maxino);
253 map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
255 panic("no memory for active inode map\n");
257 curfile.action = USING;
258 getfile(xtrmap, xtrmapskip);
259 if (spcl.c_type != TS_BITS) {
260 fprintf(stderr, "Cannot find file dump list\n");
263 map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
264 if (map == (char *)NULL)
265 panic("no memory for file dump list\n");
267 curfile.action = USING;
268 getfile(xtrmap, xtrmapskip);
270 * If there may be whiteout entries on the tape, pretend that the
271 * whiteout inode exists, so that the whiteout entries can be
274 SETINO(WINO, dumpmap);
275 /* 'r' restores don't call getvol() for tape 1, so mark it as read. */
281 * Prompt user to load a new dump volume.
282 * "Nextvol" is the next suggested volume to use.
283 * This suggested volume is enforced when doing full
284 * or incremental restores, but can be overridden by
285 * the user when only extracting a subset of the files.
291 long i, newvol, savecnt;
292 union u_spcl tmpspcl;
293 # define tmpbuf tmpspcl.s_spcl
300 prevtapea = tapeaddr;
304 panic("Changing volumes on pipe input?\n");
305 /* Avoid looping if we couldn't ask the user. */
306 if (yflag || ferror(terminal) || feof(terminal))
315 done(1); /* pipes do not get a second chance */
316 if (command == 'R' || command == 'r' || curfile.action != SKIP)
320 while (newvol <= 0) {
321 if (tapesread == 0) {
322 fprintf(stderr, "%s%s%s%s%s%s%s",
323 "You have not read any tapes yet.\n",
324 "If you are extracting just a few files,",
325 " start with the last volume\n",
326 "and work towards the first; restore",
327 " can quickly skip tapes that\n",
328 "have no further files to extract.",
329 " Otherwise, begin with volume 1.\n");
331 fprintf(stderr, "You have read volumes");
333 for (i = 0; i < 32; i++)
334 if (tapesread & (1 << i)) {
335 fprintf(stderr, "%s%ld", buf, i + 1);
338 fprintf(stderr, "\n");
341 fprintf(stderr, "Specify next volume #: ");
342 (void) fflush(stderr);
343 if (fgets(buf, BUFSIZ, terminal) == NULL)
345 } while (buf[0] == '\n');
349 "Volume numbers are positive numerics\n");
352 if (newvol == volno) {
353 tapesread |= 1 << (volno - 1);
357 fprintf(stderr, "Mount tape volume %ld\n", newvol);
358 fprintf(stderr, "Enter ``none'' if there are no more tapes\n");
359 fprintf(stderr, "otherwise enter tape name (default: %s) ", magtape);
360 (void) fflush(stderr);
361 if (fgets(buf, BUFSIZ, terminal) == NULL)
363 if (!strcmp(buf, "none\n")) {
367 if (buf[0] != '\n') {
368 (void) strcpy(magtape, buf);
369 magtape[strlen(magtape) - 1] = '\0';
373 mt = rmtopen(magtape, 0);
376 mt = open(magtape, O_RDONLY, 0);
379 fprintf(stderr, "Cannot open %s\n", magtape);
387 if (gethead(&tmpbuf) == FAIL) {
388 dprintf(stdout, "header read failed at %ld blocks\n", blksread);
389 fprintf(stderr, "tape is not dump tape\n");
393 if (tmpbuf.c_volume != volno) {
394 fprintf(stderr, "Wrong volume (%ld)\n", tmpbuf.c_volume);
398 if (_time64_to_time(tmpbuf.c_date) != dumpdate ||
399 _time64_to_time(tmpbuf.c_ddate) != dumptime) {
400 time_t t = _time64_to_time(tmpbuf.c_date);
401 fprintf(stderr, "Wrong dump date\n\tgot: %s", ctime(&t));
402 fprintf(stderr, "\twanted: %s", ctime(&dumpdate));
406 tapesread |= 1 << (volno - 1);
409 * If continuing from the previous volume, skip over any
410 * blocks read already at the end of the previous volume.
412 * If coming to this volume at random, skip to the beginning
413 * of the next record.
415 dprintf(stdout, "last rec %qd, tape starts with %qd\n", prevtapea,
417 if (tmpbuf.c_type == TS_TAPE) {
418 if (curfile.action != USING) {
420 * XXX Dump incorrectly sets c_count to 1 in the
421 * volume header of the first tape, so ignore
422 * c_count when volno == 1.
425 for (i = tmpbuf.c_count; i > 0; i--)
427 } else if (tmpbuf.c_tapea <= prevtapea) {
429 * Normally the value of c_tapea in the volume
430 * header is the record number of the header itself.
431 * However in the volume header following an EOT-
432 * terminated tape, it is the record number of the
433 * first continuation data block (dump bug?).
435 * The next record we want is `prevtapea + 1'.
437 i = prevtapea + 1 - tmpbuf.c_tapea;
438 dprintf(stderr, "Skipping %ld duplicate record%s.\n",
439 i, i > 1 ? "s" : "");
444 if (curfile.action == USING) {
446 panic("active file into volume 1\n");
449 (void) gethead(&spcl);
458 * Handle unexpected EOF.
464 if (gettingfile && curfile.action == USING) {
465 printf("Warning: %s %s\n",
466 "End-of-input encountered while extracting", curfile.name);
468 curfile.name = "<name unknown>";
469 curfile.action = UNKNOWN;
471 curfile.ino = maxino;
479 * handle multiple dumps per tape by skipping forward to the
487 if (dumpnum == 1 || volno != 1)
490 fprintf(stderr, "Cannot have multiple dumps on pipe input\n");
494 tcom.mt_count = dumpnum - 1;
497 rmtioctl(MTFSF, dumpnum - 1);
500 if (ioctl(mt, MTIOCTOP, (char *)&tcom) < 0)
501 fprintf(stderr, "ioctl MTFSF: %s\n", strerror(errno));
508 t = _time64_to_time(spcl.c_date);
509 fprintf(stdout, "Dump date: %s", ctime(&t));
510 t = _time64_to_time(spcl.c_ddate);
511 fprintf(stdout, "Dumped from: %s",
512 (spcl.c_ddate == 0) ? "the epoch\n" : ctime(&t));
513 if (spcl.c_host[0] == '\0')
515 fprintf(stderr, "Level %ld dump of %s on %s:%s\n",
516 spcl.c_level, spcl.c_filesys, spcl.c_host, spcl.c_dev);
517 fprintf(stderr, "Label: %s\n", spcl.c_label);
521 extractfile(char *name)
525 struct timeval mtimep[2], ctimep[2];
529 curfile.action = USING;
530 mtimep[0].tv_sec = curfile.atime_sec;
531 mtimep[0].tv_usec = curfile.atime_nsec / 1000;
532 mtimep[1].tv_sec = curfile.mtime_sec;
533 mtimep[1].tv_usec = curfile.mtime_nsec / 1000;
534 ctimep[0].tv_sec = curfile.atime_sec;
535 ctimep[0].tv_usec = curfile.atime_nsec / 1000;
536 ctimep[1].tv_sec = curfile.birthtime_sec;
537 ctimep[1].tv_usec = curfile.birthtime_nsec / 1000;
539 flags = curfile.file_flags;
540 switch (mode & IFMT) {
543 fprintf(stderr, "%s: unknown file mode 0%o\n", name, mode);
548 vprintf(stdout, "skipped socket %s\n", name);
554 ep = lookupname(name);
555 if (ep == NULL || ep->e_flags & EXTRACT)
556 panic("unextracted directory %s\n", name);
560 vprintf(stdout, "extract file %s\n", name);
561 return (genliteraldir(name, curfile.ino));
566 getfile(xtrlnkfile, xtrlnkskip);
569 "%s: zero length symbolic link (ignored)\n", name);
572 if (linkit(lnkbuf, name, SYMLINK) == GOOD) {
573 (void) lchown(name, curfile.uid, curfile.gid);
574 (void) lchmod(name, mode);
575 (void) lutimes(name, ctimep);
576 (void) lutimes(name, mtimep);
582 vprintf(stdout, "extract fifo %s\n", name);
589 if (mkfifo(name, mode) < 0) {
590 fprintf(stderr, "%s: cannot create fifo: %s\n",
591 name, strerror(errno));
595 (void) chown(name, curfile.uid, curfile.gid);
596 (void) chmod(name, mode);
597 (void) utimes(name, ctimep);
598 (void) utimes(name, mtimep);
599 (void) chflags(name, flags);
605 vprintf(stdout, "extract special file %s\n", name);
612 if (mknod(name, mode, (int)curfile.rdev) < 0) {
613 fprintf(stderr, "%s: cannot create special file: %s\n",
614 name, strerror(errno));
618 (void) chown(name, curfile.uid, curfile.gid);
619 (void) chmod(name, mode);
620 (void) utimes(name, ctimep);
621 (void) utimes(name, mtimep);
622 (void) chflags(name, flags);
627 vprintf(stdout, "extract file %s\n", name);
634 if ((ofile = open(name, O_WRONLY | O_CREAT | O_TRUNC,
636 fprintf(stderr, "%s: cannot create file: %s\n",
637 name, strerror(errno));
641 (void) fchown(ofile, curfile.uid, curfile.gid);
642 (void) fchmod(ofile, mode);
643 getfile(xtrfile, xtrskip);
645 (void) utimes(name, ctimep);
646 (void) utimes(name, mtimep);
647 (void) chflags(name, flags);
654 * skip over bit maps on the tape
660 while (spcl.c_type == TS_BITS || spcl.c_type == TS_CLRI)
665 * skip over a file on the tape
671 curfile.action = SKIP;
672 getfile(xtrnull, xtrnull);
676 * Extract a file from the tape.
677 * When an allocated block is found it is passed to the fill function;
678 * when an unallocated block (hole) is found, a zeroed buffer is passed
679 * to the skip function.
682 getfile(void (*fill)(char *, long), void (*skip)(char *, long))
686 quad_t size = spcl.c_size;
687 static char clearedbuf[MAXBSIZE];
688 char buf[MAXBSIZE / TP_BSIZE][TP_BSIZE];
691 if (spcl.c_type == TS_END)
692 panic("ran off end of tape\n");
693 if (spcl.c_magic != FS_UFS2_MAGIC)
694 panic("not at beginning of a file\n");
695 if (!gettingfile && setjmp(restart) != 0)
699 for (i = 0; i < spcl.c_count; i++) {
700 if (readmapflag || spcl.c_addr[i]) {
701 readtape(&buf[curblk++][0]);
702 if (curblk == fssize / TP_BSIZE) {
703 (*fill)((char *)buf, (long)(size > TP_BSIZE ?
704 fssize : (curblk - 1) * TP_BSIZE + size));
709 (*fill)((char *)buf, (long)(size > TP_BSIZE ?
711 (curblk - 1) * TP_BSIZE + size));
714 (*skip)(clearedbuf, (long)(size > TP_BSIZE ?
717 if ((size -= TP_BSIZE) <= 0) {
718 for (i++; i < spcl.c_count; i++)
719 if (readmapflag || spcl.c_addr[i])
724 if (gethead(&spcl) == GOOD && size > 0) {
725 if (spcl.c_type == TS_ADDR)
728 "Missing address (header) block for %s at %ld blocks\n",
729 curfile.name, blksread);
732 (*fill)((char *)buf, (long)((curblk * TP_BSIZE) + size));
738 * Write out the next block of a file.
741 xtrfile(char *buf, long size)
746 if (write(ofile, buf, (int) size) == -1) {
748 "write error extracting inode %d, name %s\nwrite: %s\n",
749 curfile.ino, curfile.name, strerror(errno));
754 * Skip over a hole in a file.
758 xtrskip(char *buf, long size)
761 if (lseek(ofile, size, SEEK_CUR) == -1) {
763 "seek error extracting inode %d, name %s\nlseek: %s\n",
764 curfile.ino, curfile.name, strerror(errno));
770 * Collect the next block of a symbolic link.
773 xtrlnkfile(char *buf, long size)
777 if (pathlen > MAXPATHLEN) {
778 fprintf(stderr, "symbolic link name: %s->%s%s; too long %d\n",
779 curfile.name, lnkbuf, buf, pathlen);
782 (void) strcat(lnkbuf, buf);
786 * Skip over a hole in a symbolic link (should never happen).
790 xtrlnkskip(char *buf, long size)
793 fprintf(stderr, "unallocated block in symbolic link %s\n",
799 * Collect the next block of a bit map.
802 xtrmap(char *buf, long size)
805 memmove(map, buf, size);
810 * Skip over a hole in a bit map (should never happen).
814 xtrmapskip(char *buf, long size)
817 panic("hole in map\n");
822 * Noop, when an extraction function is not needed.
826 xtrnull(char *buf, long size)
833 * Read TP_BSIZE blocks from the input.
834 * Handle read errors, and end of media.
840 int cnt, seek_failed;
842 if (blkcnt < numtrec) {
843 memmove(buf, &tapebuf[(blkcnt++ * TP_BSIZE)], (long)TP_BSIZE);
848 for (i = 0; i < ntrec; i++)
849 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
852 cnt = ntrec * TP_BSIZE;
857 i = rmtread(&tapebuf[rd], cnt);
860 i = read(mt, &tapebuf[rd], cnt);
862 * Check for mid-tape short read error.
863 * If found, skip rest of buffer and start with the next.
865 if (!pipein && numtrec < ntrec && i > 0) {
866 dprintf(stdout, "mid-media short read error.\n");
870 * Handle partial block read.
872 if (pipein && i == 0 && rd > 0)
874 else if (i > 0 && i != ntrec * TP_BSIZE) {
883 * Short read. Process the blocks read.
885 if (i % TP_BSIZE != 0)
887 "partial block read: %ld should be %ld\n",
888 i, ntrec * TP_BSIZE);
889 numtrec = i / TP_BSIZE;
896 fprintf(stderr, "Tape read error while ");
897 switch (curfile.action) {
899 fprintf(stderr, "trying to set up tape\n");
902 fprintf(stderr, "trying to resynchronize\n");
905 fprintf(stderr, "restoring %s\n", curfile.name);
908 fprintf(stderr, "skipping over inode %d\n",
912 if (!yflag && !reply("continue"))
914 i = ntrec * TP_BSIZE;
915 memset(tapebuf, 0, i);
918 seek_failed = (rmtseek(i, 1) < 0);
921 seek_failed = (lseek(mt, i, SEEK_CUR) == (off_t)-1);
925 "continuation failed: %s\n", strerror(errno));
930 * Handle end of tape.
933 vprintf(stdout, "End-of-tape encountered\n");
942 if (rd % TP_BSIZE != 0)
943 panic("partial block read: %d should be %d\n",
944 rd, ntrec * TP_BSIZE);
946 memmove(&tapebuf[rd], &endoftapemark, (long)TP_BSIZE);
949 memmove(buf, &tapebuf[(blkcnt++ * TP_BSIZE)], (long)TP_BSIZE);
955 findtapeblksize(void)
959 for (i = 0; i < ntrec; i++)
960 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
964 i = rmtread(tapebuf, ntrec * TP_BSIZE);
967 i = read(mt, tapebuf, ntrec * TP_BSIZE);
970 fprintf(stderr, "tape read error: %s\n", strerror(errno));
973 if (i % TP_BSIZE != 0) {
974 fprintf(stderr, "Tape block size (%ld) %s (%d)\n",
975 i, "is not a multiple of dump block size", TP_BSIZE);
978 ntrec = i / TP_BSIZE;
980 vprintf(stdout, "Tape block size is %ld\n", ntrec);
998 * Read the next block from the tape.
999 * If it is not any valid header, return an error.
1002 gethead(struct s_spcl *buf)
1006 readtape((char *)buf);
1007 if (buf->c_magic != FS_UFS2_MAGIC && buf->c_magic != NFS_MAGIC) {
1008 if (buf->c_magic == OFS_MAGIC) {
1010 "Format of dump tape is too old. Must use\n");
1012 "a version of restore from before 2002.\n");
1015 if (swabl(buf->c_magic) != FS_UFS2_MAGIC &&
1016 buf->c_magic != NFS_MAGIC) {
1017 if (buf->c_magic == OFS_MAGIC) {
1019 "Format of dump tape is too old. Must use\n");
1021 "a version of restore from before 2002.\n");
1026 vprintf(stdout, "Note: Doing Byte swapping\n");
1030 if (checksum((int *)buf) == FAIL)
1033 swabst((u_char *)"8l4s1q8l2q17l", (u_char *)buf);
1034 swabst((u_char *)"l",(u_char *) &buf->c_level);
1035 swabst((u_char *)"2l4q",(u_char *) &buf->c_flags);
1039 switch (buf->c_type) {
1044 * Have to patch up missing information in bit map headers
1047 buf->c_size = buf->c_count * TP_BSIZE;
1048 if (buf->c_count > TP_NINDIR)
1051 for (i = 0; i < buf->c_count; i++)
1062 * For old dump tapes, have to copy up old fields to
1065 if (buf->c_magic == NFS_MAGIC) {
1066 buf->c_tapea = buf->c_old_tapea;
1067 buf->c_firstrec = buf->c_old_firstrec;
1068 buf->c_date = _time32_to_time(buf->c_old_date);
1069 buf->c_ddate = _time32_to_time(buf->c_old_ddate);
1070 buf->c_atime = _time32_to_time(buf->c_old_atime);
1071 buf->c_mtime = _time32_to_time(buf->c_old_mtime);
1079 panic("gethead: unknown inode type %d\n", buf->c_type);
1082 buf->c_magic = FS_UFS2_MAGIC;
1083 tapeaddr = buf->c_tapea;
1090 * Check that a header is where it belongs and predict the next header
1093 accthdr(struct s_spcl *header)
1095 static ino_t previno = 0x7fffffff;
1096 static int prevtype;
1097 static long predict;
1100 if (header->c_type == TS_TAPE) {
1101 fprintf(stderr, "Volume header ");
1102 if (header->c_firstrec)
1103 fprintf(stderr, "begins with record %qd",
1104 header->c_firstrec);
1105 fprintf(stderr, "\n");
1106 previno = 0x7fffffff;
1109 if (previno == 0x7fffffff)
1113 fprintf(stderr, "Dumped inodes map header");
1116 fprintf(stderr, "Used inodes map header");
1119 fprintf(stderr, "File header, ino %d", previno);
1122 fprintf(stderr, "File continuation header, ino %d", previno);
1125 fprintf(stderr, "End of tape header");
1128 if (predict != blksread - 1)
1129 fprintf(stderr, "; predicted %ld blocks, got %ld blocks",
1130 predict, blksread - 1);
1131 fprintf(stderr, "\n");
1134 if (header->c_type != TS_END)
1135 for (i = 0; i < header->c_count; i++)
1136 if (readmapflag || header->c_addr[i] != 0)
1140 prevtype = header->c_type;
1141 previno = header->c_inumber;
1145 * Find an inode header.
1146 * Complain if had to skip.
1149 findinode(struct s_spcl *header)
1151 static long skipcnt = 0;
1156 curfile.name = "<name unknown>";
1157 curfile.action = UNKNOWN;
1161 htype = header->c_type;
1166 * Skip up to the beginning of the next record
1168 for (i = 0; i < header->c_count; i++)
1169 if (header->c_addr[i])
1171 while (gethead(header) == FAIL ||
1172 _time64_to_time(header->c_date) != dumpdate)
1177 curfile.mode = header->c_mode;
1178 curfile.uid = header->c_uid;
1179 curfile.gid = header->c_gid;
1180 curfile.file_flags = header->c_file_flags;
1181 curfile.rdev = header->c_rdev;
1182 curfile.atime_sec = header->c_atime;
1183 curfile.atime_nsec = header->c_atimensec;
1184 curfile.mtime_sec = header->c_mtime;
1185 curfile.mtime_nsec = header->c_mtimensec;
1186 curfile.birthtime_sec = header->c_birthtime;
1187 curfile.birthtime_nsec = header->c_birthtimensec;
1188 curfile.size = header->c_size;
1189 curfile.ino = header->c_inumber;
1193 /* If we missed some tapes, get another volume. */
1194 if (tapesread & (tapesread + 1)) {
1198 curfile.ino = maxino;
1202 curfile.name = "<file removal list>";
1206 curfile.name = "<file dump list>";
1210 panic("unexpected tape header\n");
1214 panic("unknown tape header type %d\n", spcl.c_type);
1218 } while (htype == TS_ADDR);
1220 fprintf(stderr, "resync restore, skipped %ld blocks\n",
1230 j = sizeof(union u_spcl) / sizeof(int);
1237 /* What happens if we want to read restore tapes
1238 for a 16bit int machine??? */
1244 if (i != CHECKSUM) {
1245 fprintf(stderr, "Checksum error %o, inode %d file %s\n", i,
1246 curfile.ino, curfile.name);
1256 msg(const char *fmt, ...)
1260 (void)vfprintf(stderr, fmt, ap);
1263 #endif /* RRESTORE */
1266 swabshort(u_char *sp, int n)
1271 c = sp[0]; sp[0] = sp[1]; sp[1] = c;
1278 swablong(u_char *sp, int n)
1283 c = sp[0]; sp[0] = sp[3]; sp[3] = c;
1284 c = sp[2]; sp[2] = sp[1]; sp[1] = c;
1291 swabquad(u_char *sp, int n)
1296 c = sp[0]; sp[0] = sp[7]; sp[7] = c;
1297 c = sp[1]; sp[1] = sp[6]; sp[6] = c;
1298 c = sp[2]; sp[2] = sp[5]; sp[5] = c;
1299 c = sp[3]; sp[3] = sp[4]; sp[4] = c;
1306 swabst(u_char *cp, u_char *sp)
1312 case '0': case '1': case '2': case '3': case '4':
1313 case '5': case '6': case '7': case '8': case '9':
1314 n = (n * 10) + (*cp++ - '0');
1317 case 's': case 'w': case 'h':
1320 sp = swabshort(sp, n);
1326 sp = swablong(sp, n);
1332 sp = swabquad(sp, n);
1342 fprintf(stderr, "Unknown conversion character: %c\n",
1355 swabst((u_char *)"l", (u_char *)&x);