2 * Ported to Linux's Second Extended File System as part of the
3 * dump and restore backup suit
4 * Remy Card <card@Linux.EU.Org>, 1994-1997
5 * Stelian Pop <pop@cybercable.fr>, 1999
9 * Copyright (c) 1983, 1993
10 * The Regents of the University of California. All rights reserved.
11 * (c) UNIX System Laboratories, Inc.
12 * All or some portions of this file are derived from material licensed
13 * to the University of California by American Telephone and Telegraph
14 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
15 * the permission of UNIX System Laboratories, Inc.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 * 3. All advertising materials mentioning features or use of this software
26 * must display the following acknowledgement:
27 * This product includes software developed by the University of
28 * California, Berkeley and its contributors.
29 * 4. Neither the name of the University nor the names of its contributors
30 * may be used to endorse or promote products derived from this software
31 * without specific prior written permission.
33 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
34 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
37 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 static const char rcsid[] =
48 "$Id: tape.c,v 1.9 1999/11/22 21:39:42 tiniou Exp $";
51 #include <sys/param.h>
58 #include <linux/ext2_fs.h>
59 #include <bsdcompat.h>
61 #include <ufs/ufs/dinode.h>
62 #endif /* __linux__ */
63 #include <protocols/dumprestore.h>
66 #include <compaterr.h>
74 #include <ext2fs/ext2fs.h>
79 #include "pathnames.h"
81 static long fssize = MAXBSIZE;
83 static int pipein = 0;
84 static char magtape[NAME_MAX];
85 static char magtapeprefix[NAME_MAX];
89 static union u_spcl endoftapemark;
90 static long blksread; /* blocks read since last header */
91 static long tpblksread = 0; /* TP_BSIZE blocks read */
92 static long tapesread;
93 static sigjmp_buf restart;
94 static int gettingfile = 0; /* restart has a valid frame */
95 static char *host = NULL;
99 static char lnkbuf[MAXPATHLEN + 1];
102 int oldinofmt; /* old inode format conversion required */
103 int Bcvt; /* Swap Bytes (for CCI or sun) */
104 static int Qcvt; /* Swap quads (for sun) */
106 #define FLUSHTAPEBUF() blkcnt = ntrec + 1
108 static void accthdr __P((struct s_spcl *));
109 static int checksum __P((int *));
110 static void findinode __P((struct s_spcl *));
111 static void findtapeblksize __P((void));
112 static int gethead __P((struct s_spcl *));
113 static void readtape __P((char *));
114 static void setdumpnum __P((void));
115 static u_int swabi __P((u_int));
116 static u_long swabl __P((u_long));
117 static u_char *swab64 __P((u_char *, int));
118 static u_char *swab32 __P((u_char *, int));
119 static u_char *swab16 __P((u_char *, int));
120 static void terminateinput __P((void));
121 static void xtrfile __P((char *, size_t));
122 static void xtrlnkfile __P((char *, size_t));
123 static void xtrlnkskip __P((char *, size_t));
124 static void xtrmap __P((char *, size_t));
125 static void xtrmapskip __P((char *, size_t));
126 static void xtrskip __P((char *, size_t));
128 static int readmapflag;
131 * Set up an input source
134 setinput(char *source)
140 newtapebuf(NTREC > HIGHDENSITYTREC ? NTREC : HIGHDENSITYTREC);
144 if (strchr(source, ':')) {
146 source = strchr(host, ':');
148 if (rmthost(host) == 0)
152 if (strcmp(source, "-") == 0) {
154 * Since input is coming from a pipe we must establish
155 * our own connection to the terminal.
157 terminal = fopen(_PATH_TTY, "r");
158 if (terminal == NULL) {
159 warn("cannot open %s", _PATH_TTY);
160 terminal = fopen(_PATH_DEVNULL, "r");
161 if (terminal == NULL)
162 err(1, "cannot open %s", _PATH_DEVNULL);
166 setuid(getuid()); /* no longer need or want root privileges */
168 strncpy(magtapeprefix, source, NAME_MAX);
169 magtapeprefix[NAME_MAX-1] = '\0';
170 snprintf(magtape, NAME_MAX, "%s%03d", source, 1);
173 strncpy(magtape, source, NAME_MAX);
174 magtape[NAME_MAX - 1] = '\0';
178 newtapebuf(long size)
180 static int tapebufsize = -1;
183 if (size <= tapebufsize)
187 tapebuf = malloc(size * TP_BSIZE);
189 errx(1, "Cannot allocate space for tape buffer");
194 * Verify that the tape drive can be accessed and
195 * that it actually is a dump tape.
203 Vprintf(stdout, "Verify tape and initialize maps\n");
206 mt = rmtopen(magtape, 0);
212 mt = open(magtape, O_RDONLY, 0);
214 err(1, "%s", magtape);
218 if (!pipein && !bflag)
220 if (gethead(&spcl) == FAIL) {
221 blkcnt--; /* push back this block */
225 if (gethead(&spcl) == FAIL)
226 errx(1, "Tape is not a dump tape");
227 fprintf(stderr, "Converting to new file system format.\n");
230 endoftapemark.s_spcl.c_magic = cvtflag ? OFS_MAGIC : NFS_MAGIC;
231 endoftapemark.s_spcl.c_type = TS_END;
232 ip = (int *)&endoftapemark;
233 j = sizeof(union u_spcl) / sizeof(int);
238 endoftapemark.s_spcl.c_checksum = CHECKSUM - i;
240 if (vflag || command == 't' || command == 'C')
242 if (filesys == NULL) {
243 filesys = spcl.c_filesys;
245 dumptime = spcl.c_ddate;
246 dumpdate = spcl.c_date;
247 if (stat(".", &stbuf) < 0)
248 err(1, "cannot stat .");
249 if (stbuf.st_blksize > 0 && stbuf.st_blksize < TP_BSIZE )
251 if (stbuf.st_blksize >= TP_BSIZE && stbuf.st_blksize <= MAXBSIZE)
252 fssize = stbuf.st_blksize;
253 if (((fssize - 1) & fssize) != 0)
254 errx(1, "bad block size %ld", fssize);
255 if (spcl.c_volume != 1)
256 errx(1, "Tape is not volume 1 of the dump");
257 if (gethead(&spcl) == FAIL) {
258 Dprintf(stdout, "header read failed at %ld blocks\n", (long)blksread);
259 panic("no header after volume mark!\n");
262 if (spcl.c_type != TS_CLRI)
263 errx(1, "Cannot find file removal list");
264 maxino = (spcl.c_count * TP_BSIZE * NBBY) + 1;
265 Dprintf(stdout, "maxino = %ld\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 errx(1, "Cannot find file dump list");
274 map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
275 if (map == (char *)NULL)
276 panic("no memory for file dump list\n");
278 curfile.action = USING;
279 getfile(xtrmap, xtrmapskip);
281 * If there may be whiteout entries on the tape, pretend that the
282 * whiteout inode exists, so that the whiteout entries can be
286 SETINO(WINO, dumpmap);
290 * Prompt user to load a new dump volume.
291 * "Nextvol" is the next suggested volume to use.
292 * This suggested volume is enforced when doing full
293 * or incremental restores, but can be overridden by
294 * the user when only extracting a subset of the files.
299 long newvol = 0, savecnt = 0, wantnext = 0, i;
300 union u_spcl tmpspcl;
301 # define tmpbuf tmpspcl.s_spcl
311 panic("Changing volumes on pipe input?\n");
319 exit(1); /* pipes do not get a second chance */
320 if (command == 'R' || command == 'r' || curfile.action != SKIP) {
327 while (newvol <= 0) {
328 if (tapesread == 0) {
329 fprintf(stderr, "%s%s%s%s%s",
330 "You have not read any tapes yet.\n",
331 "Unless you know which volume your",
332 " file(s) are on you should start\n",
333 "with the last volume and work",
334 " towards the first.\n");
336 fprintf(stderr, "You have read volumes");
338 for (i = 1; i < 32; i++)
339 if (tapesread & (1 << i)) {
340 fprintf(stderr, "%s%ld", buf, (long)i);
343 fprintf(stderr, "\n");
346 fprintf(stderr, "Specify next volume #: ");
347 (void) fflush(stderr);
348 (void) fgets(buf, BUFSIZ, terminal);
349 } while (!feof(terminal) && buf[0] == '\n');
355 "Volume numbers are positive numerics\n");
358 if (newvol == volno) {
359 tapesread |= 1 << volno;
364 snprintf(magtape, NAME_MAX, "%s%03ld", magtapeprefix, newvol);
365 magtape[NAME_MAX - 1] = '\0';
367 if (!Mflag || haderror) {
369 fprintf(stderr, "Mount tape volume %ld\n", (long)newvol);
370 fprintf(stderr, "Enter ``none'' if there are no more tapes\n");
371 fprintf(stderr, "otherwise enter tape name (default: %s) ", magtape);
372 (void) fflush(stderr);
373 (void) fgets(buf, BUFSIZ, terminal);
376 if (!strcmp(buf, "none\n")) {
380 if (buf[0] != '\n') {
381 (void) strcpy(magtape, buf);
382 magtape[strlen(magtape) - 1] = '\0';
387 mt = rmtopen(magtape, 0);
390 mt = open(magtape, O_RDONLY, 0);
393 fprintf(stderr, "Cannot open %s\n", magtape);
402 if (gethead(&tmpbuf) == FAIL) {
403 Dprintf(stdout, "header read failed at %ld blocks\n", (long)blksread);
404 fprintf(stderr, "tape is not dump tape\n");
409 if (tmpbuf.c_volume != volno) {
410 fprintf(stderr, "Wrong volume (%d)\n", tmpbuf.c_volume);
415 if (tmpbuf.c_date != dumpdate || tmpbuf.c_ddate != dumptime) {
417 fprintf(stderr, "Wrong dump date\n\tgot: %s",
418 ctime4(&tmpbuf.c_date));
419 fprintf(stderr, "\twanted: %s", ctime4(&dumpdate));
421 fprintf(stderr, "Wrong dump date\n\tgot: %s",
422 ctime(&tmpbuf.c_date));
423 fprintf(stderr, "\twanted: %s", ctime(&dumpdate));
429 tapesread |= 1 << volno;
432 * If continuing from the previous volume, skip over any
433 * blocks read already at the end of the previous volume.
435 * If coming to this volume at random, skip to the beginning
436 * of the next record.
438 Dprintf(stdout, "read %ld recs, tape starts with %ld\n",
439 tpblksread, (long)tmpbuf.c_firstrec);
440 if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER)) {
442 tpblksread = tmpbuf.c_firstrec;
443 for (i = tmpbuf.c_count; i > 0; i--)
445 } else if (tmpbuf.c_firstrec > 0 &&
446 tmpbuf.c_firstrec < tpblksread - 1) {
448 * -1 since we've read the volume header
450 i = tpblksread - tmpbuf.c_firstrec - 1;
451 Dprintf(stderr, "Skipping %ld duplicate record%s.\n",
452 (long)i, i > 1 ? "s" : "");
457 if (curfile.action == USING) {
459 panic("active file into volume 1\n");
463 * Skip up to the beginning of the next record
465 if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER))
466 for (i = tmpbuf.c_count; i > 0; i--)
468 (void) gethead(&spcl);
472 siglongjmp(restart, 1);
477 * Handle unexpected EOF.
483 if (gettingfile && curfile.action == USING) {
484 printf("Warning: %s %s\n",
485 "End-of-input encountered while extracting", curfile.name);
487 curfile.name = "<name unknown>";
488 curfile.action = UNKNOWN;
490 curfile.ino = maxino;
493 siglongjmp(restart, 1);
498 * handle multiple dumps per tape by skipping forward to the
506 if (dumpnum == 1 || volno != 1)
509 errx(1, "Cannot have multiple dumps on pipe input");
511 tcom.mt_count = dumpnum - 1;
514 rmtioctl(MTFSF, dumpnum - 1);
517 if (ioctl(mt, (int)MTIOCTOP, (char *)&tcom) < 0)
525 fprintf(stdout, "Dump date: %s", ctime4(&spcl.c_date));
526 fprintf(stdout, "Dumped from: %s",
527 (spcl.c_ddate == 0) ? "the epoch\n" : ctime4(&spcl.c_ddate));
529 fprintf(stdout, "Dump date: %s", ctime(&spcl.c_date));
530 fprintf(stdout, "Dumped from: %s",
531 (spcl.c_ddate == 0) ? "the epoch\n" : ctime(&spcl.c_ddate));
533 if (spcl.c_host[0] == '\0')
535 fprintf(stderr, "Level %d dump of %s on %s:%s\n",
536 spcl.c_level, spcl.c_filesys, spcl.c_host, spcl.c_dev);
537 fprintf(stderr, "Label: %s\n", spcl.c_label);
541 extractfile(char *name)
545 struct timeval timep[2];
549 curfile.action = USING;
551 timep[0].tv_sec = curfile.dip->di_atime.tv_sec;
552 timep[0].tv_usec = curfile.dip->di_atime.tv_usec;
553 timep[1].tv_sec = curfile.dip->di_mtime.tv_sec;
554 timep[1].tv_usec = curfile.dip->di_mtime.tv_usec;
555 #else /* __linux__ */
556 timep[0].tv_sec = curfile.dip->di_atime;
557 timep[0].tv_usec = curfile.dip->di_atimensec / 1000;
558 timep[1].tv_sec = curfile.dip->di_mtime;
559 timep[1].tv_usec = curfile.dip->di_mtimensec / 1000;
560 #endif /* __linux__ */
561 mode = curfile.dip->di_mode;
562 flags = curfile.dip->di_flags;
563 switch (mode & IFMT) {
566 fprintf(stderr, "%s: unknown file mode 0%o\n", name, mode);
571 Vprintf(stdout, "skipped socket %s\n", name);
577 ep = lookupname(name);
578 if (ep == NULL || ep->e_flags & EXTRACT)
579 panic("unextracted directory %s\n", name);
583 Vprintf(stdout, "extract file %s\n", name);
584 return (genliteraldir(name, curfile.ino));
589 uid_t luid = curfile.dip->di_uid;
590 gid_t lgid = curfile.dip->di_gid;
594 getfile(xtrlnkfile, xtrlnkskip);
597 "%s: zero length symbolic link (ignored)\n", name);
600 if (linkit(lnkbuf, name, SYMLINK) == FAIL)
603 (void) lchown(name, luid, lgid);
609 Vprintf(stdout, "extract fifo %s\n", name);
616 if (mkfifo(name, mode) < 0) {
617 warn("%s: cannot create fifo", name);
621 (void) chown(name, curfile.dip->di_uid, curfile.dip->di_gid);
622 (void) chmod(name, mode);
625 (void) fsetflags(name, flags);
627 (void) chflags(name, flags);
635 Vprintf(stdout, "extract special file %s\n", name);
642 if (mknod(name, mode, (int)curfile.dip->di_rdev) < 0) {
643 warn("%s: cannot create special file", name);
647 (void) chown(name, curfile.dip->di_uid, curfile.dip->di_gid);
648 (void) chmod(name, mode);
652 warn("%s: fsetflags called on a special file", name);
653 (void) fsetflags(name, flags);
656 (void) chflags(name, flags);
663 Vprintf(stdout, "extract file %s\n", name);
670 if ((ofile = open(name, O_WRONLY | O_CREAT | O_TRUNC,
672 warn("%s: cannot create file", name);
676 (void) fchown(ofile, curfile.dip->di_uid, curfile.dip->di_gid);
677 (void) fchmod(ofile, mode);
680 (void) setflags(ofile, flags);
682 (void) fchflags(ofile, flags);
684 getfile(xtrfile, xtrskip);
693 * skip over bit maps on the tape
699 while (spcl.c_type == TS_BITS || spcl.c_type == TS_CLRI)
704 * skip over a file on the tape
710 curfile.action = SKIP;
711 getfile(xtrnull, xtrnull);
715 * Extract a file from the tape.
716 * When an allocated block is found it is passed to the fill function;
717 * when an unallocated block (hole) is found, a zeroed buffer is passed
718 * to the skip function.
721 getfile(void (*fill) __P((char *, size_t)), void (*skip) __P((char *, size_t)))
724 volatile int curblk = 0;
725 volatile quad_t size = spcl.c_dinode.di_size;
726 volatile int last_write_was_hole = 0;
727 quad_t origsize = size;
728 static char clearedbuf[MAXBSIZE];
729 char buf[MAXBSIZE / TP_BSIZE][TP_BSIZE];
732 if (spcl.c_type == TS_END)
733 panic("ran off end of tape\n");
734 if (spcl.c_magic != NFS_MAGIC)
735 panic("not at beginning of a file\n");
736 if (!gettingfile && setjmp(restart) != 0)
740 for (i = 0; i < spcl.c_count; i++) {
741 if (readmapflag || spcl.c_addr[i]) {
742 readtape(&buf[curblk++][0]);
743 if (curblk == fssize / TP_BSIZE) {
744 (*fill)((char *)buf, (size_t)(size > TP_BSIZE ?
745 fssize : (curblk - 1) * TP_BSIZE + size));
747 last_write_was_hole = 0;
751 (*fill)((char *)buf, (size_t)(size > TP_BSIZE ?
753 (curblk - 1) * TP_BSIZE + size));
756 (*skip)(clearedbuf, (long)(size > TP_BSIZE ?
758 last_write_was_hole = 1;
760 if ((size -= TP_BSIZE) <= 0) {
761 for (i++; i < spcl.c_count; i++)
762 if (readmapflag || spcl.c_addr[i])
767 if (gethead(&spcl) == GOOD && size > 0) {
768 if (spcl.c_type == TS_ADDR)
771 "Missing address (header) block for %s at %ld blocks\n",
772 curfile.name, (long)blksread);
775 (*fill)((char *)buf, (size_t)(curblk * TP_BSIZE) + size);
776 last_write_was_hole = 0;
778 if (last_write_was_hole) {
779 ftruncate(ofile, origsize);
786 * Write out the next block of a file.
789 xtrfile(char *buf, size_t size)
794 if (write(ofile, buf, (int) size) == -1)
795 err(1, "write error extracting inode %lu, name %s\nwrite",
796 (unsigned long)curfile.ino, curfile.name);
800 * Skip over a hole in a file.
804 xtrskip(char *buf, size_t size)
807 if (lseek(ofile, (off_t)size, SEEK_CUR) == -1)
808 err(1, "seek error extracting inode %lu, name %s\nlseek",
809 (unsigned long)curfile.ino, curfile.name);
813 * Collect the next block of a symbolic link.
816 xtrlnkfile(char *buf, size_t size)
820 if (pathlen > MAXPATHLEN)
821 errx(1, "symbolic link name: %s->%s%s; too long %d",
822 curfile.name, lnkbuf, buf, pathlen);
823 (void) strcat(lnkbuf, buf);
827 * Skip over a hole in a symbolic link (should never happen).
831 xtrlnkskip(char *buf, size_t size)
834 errx(1, "unallocated block in symbolic link %s", curfile.name);
838 * Collect the next block of a bit map.
841 xtrmap(char *buf, size_t size)
844 memmove(map, buf, size);
849 * Skip over a hole in a bit map (should never happen).
853 xtrmapskip(char *buf, size_t size)
856 panic("hole in map\n");
861 * Noop, when an extraction function is not needed.
865 xtrnull(char *buf, size_t size)
872 do_cmpfiles(int fd_tape, int fd_disk, long size)
874 static char buf_tape[BUFSIZ];
875 static char buf_disk[BUFSIZ];
880 if ((n_tape = read(fd_tape, buf_tape, sizeof(buf_tape))) < 1) {
881 close(fd_tape), close(fd_disk);
882 panic("do_cmpfiles: unexpected EOF[1]");
884 if ((n_disk = read(fd_disk, buf_disk, sizeof(buf_tape))) < 1) {
885 close(fd_tape), close(fd_disk);
886 panic("do_cmpfiles: unexpected EOF[2]");
888 if (n_tape != n_disk) {
889 close(fd_tape), close(fd_disk);
890 panic("do_cmpfiles: sizes different!");
892 if (memcmp(buf_tape, buf_disk, (size_t)n_tape) != 0) return (1);
898 /* for debugging compare problems */
899 #undef COMPARE_FAIL_KEEP_FILE
902 #ifdef COMPARE_FAIL_KEEP_FILE
903 /* return true if tapefile should be unlinked after compare */
908 cmpfiles(char *tapefile, char *diskfile, struct stat *sbuf_disk)
910 struct stat sbuf_tape;
911 int fd_tape, fd_disk;
913 if (stat(tapefile, &sbuf_tape) != 0) {
914 panic("Can't lstat tmp file %s: %s\n", tapefile,
918 if (sbuf_disk->st_size != sbuf_tape.st_size) {
920 "%s: size changed from %ld to %ld.\n",
921 diskfile, (long)sbuf_tape.st_size, (long)sbuf_disk->st_size);
922 #ifdef COMPARE_FAIL_KEEP_FILE
929 if ((fd_tape = open(tapefile, O_RDONLY)) < 0) {
930 panic("Can't open %s: %s\n", tapefile, strerror(errno));
932 if ((fd_disk = open(diskfile, O_RDONLY)) < 0) {
934 panic("Can't open %s: %s\n", diskfile, strerror(errno));
937 if (do_cmpfiles(fd_tape, fd_disk, sbuf_tape.st_size)) {
938 fprintf(stderr, "%s: tape and disk copies are different\n",
942 #ifdef COMPARE_FAIL_KEEP_FILE
943 /* rename the file to live in /tmp */
944 /* rename `tapefile' to /tmp/<basename of diskfile> */
946 char *p = strrchr(diskfile, '/');
947 char newname[MAXPATHLEN];
949 panic("can't find / in %s\n", diskfile);
951 snprintf(newname, sizeof(newname), "%s/debug/%s", tmpdir, p + 1);
952 if (rename(tapefile, newname)) {
953 panic("rename from %s to %s failed: %s\n",
957 fprintf(stderr, "*** %s saved to %s\n",
962 /* don't unlink the file (it's not there anymore */
971 #ifdef COMPARE_FAIL_KEEP_FILE
976 static char tmpfilename[MAXPATHLEN];
979 comparefile(char *name)
981 static char *tmpfile = NULL;
983 struct stat sb, stemp;
986 if ((r = lstat(name, &sb)) != 0) {
987 warn("%s: does not exist (%d)", name, r);
993 curfile.action = USING;
994 mode = curfile.dip->di_mode;
996 Vprintf(stdout, "comparing %s (size: %ld, mode: 0%o)\n", name,
997 (long)sb.st_size, mode);
999 if (sb.st_mode != mode) {
1000 fprintf(stderr, "%s: mode changed from 0%o to 0%o.\n",
1001 name, mode & 07777, sb.st_mode & 07777);
1003 switch (mode & IFMT) {
1017 char lbuf[MAXPATHLEN + 1];
1020 if (!(sb.st_mode & S_IFLNK)) {
1021 fprintf(stderr, "%s: is no longer a symbolic link\n",
1027 getfile(xtrlnkfile, xtrlnkskip);
1030 "%s: zero length symbolic link (ignored)\n",
1034 if ((lsize = readlink(name, lbuf, MAXPATHLEN)) < 0) {
1035 panic("readlink of %s failed: %s", name,
1039 if (strcmp(lbuf, lnkbuf) != 0) {
1041 "%s: symbolic link changed from %s to %s.\n",
1042 name, lnkbuf, lbuf);
1050 if (!(sb.st_mode & (S_IFCHR|S_IFBLK))) {
1051 fprintf(stderr, "%s: no longer a special file\n",
1057 if (sb.st_rdev != (int)curfile.dip->di_rdev) {
1059 "%s: device changed from %d,%d to %d,%d.\n",
1061 ((int)curfile.dip->di_rdev >> 8) & 0xff,
1062 (int)curfile.dip->di_rdev & 0xff,
1063 ((int)sb.st_rdev >> 8) & 0xff,
1064 (int)sb.st_rdev & 0xff);
1070 if (tmpfile == NULL) {
1071 /* argument to mktemp() must not be in RO space: */
1072 snprintf(tmpfilename, sizeof(tmpfilename), "%s/restoreCXXXXXX", tmpdir);
1073 tmpfile = mktemp(&tmpfilename[0]);
1075 if ((stat(tmpfile, &stemp) == 0) && (unlink(tmpfile) != 0)) {
1076 panic("cannot delete tmp file %s: %s\n",
1077 tmpfile, strerror(errno));
1079 if ((ofile = creat(tmpfile, 0600)) < 0) {
1080 panic("cannot create file temp file %s: %s\n",
1081 name, strerror(errno));
1083 getfile(xtrfile, xtrskip);
1084 (void) close(ofile);
1085 #ifdef COMPARE_FAIL_KEEP_FILE
1086 if (cmpfiles(tmpfile, name, &sb))
1089 cmpfiles(tmpfile, name, &sb);
1098 * Read TP_BSIZE blocks from the input.
1099 * Handle read errors, and end of media.
1104 ssize_t rd, newvol, i;
1105 int cnt, seek_failed;
1107 if (blkcnt < numtrec) {
1108 memmove(buf, &tapebuf[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1113 for (i = 0; i < ntrec; i++)
1114 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1117 cnt = ntrec * TP_BSIZE;
1122 i = rmtread(&tapebuf[rd], cnt);
1125 i = read(mt, &tapebuf[rd], cnt);
1127 * Check for mid-tape short read error.
1128 * If found, skip rest of buffer and start with the next.
1130 if (!pipein && numtrec < ntrec && i > 0) {
1131 Dprintf(stdout, "mid-media short read error.\n");
1135 * Handle partial block read.
1137 if (pipein && i == 0 && rd > 0)
1139 else if (i > 0 && i != ntrec * TP_BSIZE) {
1148 * Short read. Process the blocks read.
1150 if (i % TP_BSIZE != 0)
1152 "partial block read: %ld should be %ld\n",
1153 (long)i, ntrec * TP_BSIZE);
1154 numtrec = i / TP_BSIZE;
1158 * Handle read error.
1161 fprintf(stderr, "Tape read error while ");
1162 switch (curfile.action) {
1164 fprintf(stderr, "trying to set up tape\n");
1167 fprintf(stderr, "trying to resynchronize\n");
1170 fprintf(stderr, "restoring %s\n", curfile.name);
1173 fprintf(stderr, "skipping over inode %lu\n",
1174 (unsigned long)curfile.ino);
1177 if (!yflag && !reply("continue"))
1179 i = ntrec * TP_BSIZE;
1180 memset(tapebuf, 0, (size_t)i);
1183 seek_failed = (rmtseek(i, 1) < 0);
1186 seek_failed = (lseek(mt, i, SEEK_CUR) == (off_t)-1);
1189 err(1, "continuation failed");
1192 * Handle end of tape.
1195 Vprintf(stdout, "End-of-tape encountered\n");
1204 if (rd % TP_BSIZE != 0)
1205 panic("partial block read: %d should be %d\n",
1206 rd, ntrec * TP_BSIZE);
1208 memmove(&tapebuf[rd], &endoftapemark, TP_BSIZE);
1211 memmove(buf, &tapebuf[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1217 findtapeblksize(void)
1221 for (i = 0; i < ntrec; i++)
1222 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1226 i = rmtread(tapebuf, (size_t)(ntrec * TP_BSIZE));
1229 i = read(mt, tapebuf, (size_t)(ntrec * TP_BSIZE));
1232 err(1, "tape read error");
1233 if (i % TP_BSIZE != 0)
1234 errx(1, "Tape block size (%ld) is not a multiple of dump block size (%d)",
1236 ntrec = i / TP_BSIZE;
1238 Vprintf(stdout, "Tape block size is %ld\n", ntrec);
1256 * Read the next block from the tape.
1257 * Check to see if it is one of several vintage headers.
1258 * If it is an old style header, convert it to a new style header.
1259 * If it is not any valid header, return an error.
1262 gethead(struct s_spcl *buf)
1270 char dummy[TP_BSIZE];
1277 u_int16_t c_inumber;
1282 u_int16_t odi_nlink;
1298 readtape((char *)buf);
1299 if (buf->c_magic != NFS_MAGIC) {
1300 if (swabi(buf->c_magic) != NFS_MAGIC)
1303 Vprintf(stdout, "Note: Doing Byte swapping\n");
1307 if (checksum((int *)buf) == FAIL)
1310 swabst((u_char *)"8i4s31i528bi192b2i", (u_char *)buf);
1313 readtape((char *)(&u_ospcl.s_ospcl));
1314 memset((char *)buf, 0, (long)TP_BSIZE);
1315 buf->c_type = u_ospcl.s_ospcl.c_type;
1316 buf->c_date = u_ospcl.s_ospcl.c_date;
1317 buf->c_ddate = u_ospcl.s_ospcl.c_ddate;
1318 buf->c_volume = u_ospcl.s_ospcl.c_volume;
1319 buf->c_tapea = u_ospcl.s_ospcl.c_tapea;
1320 buf->c_inumber = u_ospcl.s_ospcl.c_inumber;
1321 buf->c_checksum = u_ospcl.s_ospcl.c_checksum;
1322 buf->c_magic = u_ospcl.s_ospcl.c_magic;
1323 buf->c_dinode.di_mode = u_ospcl.s_ospcl.c_dinode.odi_mode;
1324 buf->c_dinode.di_nlink = u_ospcl.s_ospcl.c_dinode.odi_nlink;
1325 buf->c_dinode.di_uid = u_ospcl.s_ospcl.c_dinode.odi_uid;
1326 buf->c_dinode.di_gid = u_ospcl.s_ospcl.c_dinode.odi_gid;
1327 buf->c_dinode.di_size = u_ospcl.s_ospcl.c_dinode.odi_size;
1328 buf->c_dinode.di_rdev = u_ospcl.s_ospcl.c_dinode.odi_rdev;
1330 buf->c_dinode.di_atime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_atime;
1331 buf->c_dinode.di_mtime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_mtime;
1332 buf->c_dinode.di_ctime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_ctime;
1333 #else /* __linux__ */
1334 buf->c_dinode.di_atime = u_ospcl.s_ospcl.c_dinode.odi_atime;
1335 buf->c_dinode.di_mtime = u_ospcl.s_ospcl.c_dinode.odi_mtime;
1336 buf->c_dinode.di_ctime = u_ospcl.s_ospcl.c_dinode.odi_ctime;
1337 #endif /* __linux__ */
1338 buf->c_count = u_ospcl.s_ospcl.c_count;
1339 memmove(buf->c_addr, u_ospcl.s_ospcl.c_addr, (long)256);
1340 if (u_ospcl.s_ospcl.c_magic != OFS_MAGIC ||
1341 checksum((int *)(&u_ospcl.s_ospcl)) == FAIL)
1343 buf->c_magic = NFS_MAGIC;
1346 if ((buf->c_dinode.di_size == 0 || buf->c_dinode.di_size > 0xfffffff) &&
1347 (buf->c_dinode.di_mode & IFMT) == IFDIR && Qcvt == 0) {
1348 qcvt.qval = buf->c_dinode.di_size;
1349 if (qcvt.val[0] || qcvt.val[1]) {
1350 printf("Note: Doing Quad swapping\n");
1355 qcvt.qval = buf->c_dinode.di_size;
1357 qcvt.val[1] = qcvt.val[0];
1359 buf->c_dinode.di_size = qcvt.qval;
1363 switch (buf->c_type) {
1368 * Have to patch up missing information in bit map headers
1371 buf->c_dinode.di_size = buf->c_count * TP_BSIZE;
1372 if (buf->c_count > TP_NINDIR)
1375 for (i = 0; i < buf->c_count; i++)
1380 if ((buf->c_flags & DR_NEWINODEFMT) == 0)
1392 panic("gethead: unknown inode type %d\n", buf->c_type);
1396 * If we are restoring a filesystem with old format inodes,
1397 * copy the uid/gid to the new location.
1400 buf->c_dinode.di_uid = buf->c_dinode.di_ouid;
1401 buf->c_dinode.di_gid = buf->c_dinode.di_ogid;
1409 * Check that a header is where it belongs and predict the next header
1412 accthdr(struct s_spcl *header)
1414 static ino_t previno = 0x7fffffff;
1415 static int prevtype;
1416 static long predict;
1419 if (header->c_type == TS_TAPE) {
1420 fprintf(stderr, "Volume header (%s inode format) ",
1421 oldinofmt ? "old" : "new");
1422 if (header->c_firstrec)
1423 fprintf(stderr, "begins with record %d",
1424 header->c_firstrec);
1425 fprintf(stderr, "\n");
1426 previno = 0x7fffffff;
1429 if (previno == 0x7fffffff)
1433 fprintf(stderr, "Dumped inodes map header");
1436 fprintf(stderr, "Used inodes map header");
1439 fprintf(stderr, "File header, ino %lu", (unsigned long)previno);
1442 fprintf(stderr, "File continuation header, ino %ld", previno);
1445 fprintf(stderr, "End of tape header");
1448 if (predict != blksread - 1)
1449 fprintf(stderr, "; predicted %ld blocks, got %ld blocks",
1450 predict, blksread - 1);
1451 fprintf(stderr, "\n");
1454 if (header->c_type != TS_END)
1455 for (i = 0; i < header->c_count; i++)
1456 if (readmapflag || header->c_addr[i] != 0)
1460 prevtype = header->c_type;
1461 previno = header->c_inumber;
1465 * Find an inode header.
1466 * Complain if had to skip, and complain is set.
1469 findinode(struct s_spcl *header)
1471 static long skipcnt = 0;
1475 curfile.name = "<name unknown>";
1476 curfile.action = UNKNOWN;
1480 if (header->c_magic != NFS_MAGIC) {
1482 while (gethead(header) == FAIL ||
1483 header->c_date != dumpdate)
1486 switch (header->c_type) {
1490 * Skip up to the beginning of the next record
1492 for (i = 0; i < header->c_count; i++)
1493 if (header->c_addr[i])
1495 while (gethead(header) == FAIL ||
1496 header->c_date != dumpdate)
1501 curfile.dip = &header->c_dinode;
1502 curfile.ino = header->c_inumber;
1506 curfile.ino = maxino;
1510 curfile.name = "<file removal list>";
1514 curfile.name = "<file dump list>";
1518 panic("unexpected tape header\n");
1522 panic("unknown tape header type %d\n", spcl.c_type);
1526 } while (header->c_type == TS_ADDR);
1528 fprintf(stderr, "resync restore, skipped %ld blocks\n",
1538 j = sizeof(union u_spcl) / sizeof(int);
1545 /* What happens if we want to read restore tapes
1546 for a 16bit int machine??? */
1552 if (i != CHECKSUM) {
1553 fprintf(stderr, "Checksum error %o, inode %lu file %s\n", i,
1554 (unsigned long)curfile.ino, curfile.name);
1564 #include <varargs.h>
1569 msg(const char *fmt, ...)
1582 (void)vfprintf(stderr, fmt, ap);
1585 #endif /* RRESTORE */
1588 swab16(u_char *sp, int n)
1593 c = sp[0]; sp[0] = sp[1]; sp[1] = c;
1600 swab32(u_char *sp, int n)
1605 c = sp[0]; sp[0] = sp[3]; sp[3] = c;
1606 c = sp[1]; sp[1] = sp[2]; sp[2] = c;
1613 swab64(u_char *sp, int n)
1618 c = sp[0]; sp[0] = sp[7]; sp[7] = c;
1619 c = sp[1]; sp[1] = sp[6]; sp[6] = c;
1620 c = sp[2]; sp[2] = sp[5]; sp[5] = c;
1621 c = sp[3]; sp[3] = sp[4]; sp[4] = c;
1628 swabst(u_char *cp, u_char *sp)
1634 case '0': case '1': case '2': case '3': case '4':
1635 case '5': case '6': case '7': case '8': case '9':
1636 n = (n * 10) + (*cp++ - '0');
1639 case 's': case 'w': case 'h':
1657 default: /* Any other character, like 'b' counts as byte. */
1671 swabst((u_char *)"i", (u_char *)&x);
1678 swabst((u_char *)"l", (u_char *)&x);