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>
49 #include <sys/extattr.h>
52 #include <ufs/ufs/extattr.h>
53 #include <ufs/ufs/dinode.h>
54 #include <protocols/dumprestore.h>
71 static long fssize = MAXBSIZE;
73 static int pipein = 0;
74 static int pipecmdin = 0;
75 static FILE *popenfp = NULL;
80 static union u_spcl endoftapemark;
81 static long byteslide = 0;
82 static long blksread; /* blocks read since last header */
83 static int64_t tapeaddr = 0; /* current TP_BSIZE tape record */
84 static long tapesread;
85 static jmp_buf restart;
86 static int gettingfile = 0; /* restart has a valid frame */
87 static char *host = NULL;
88 static int readmapflag;
92 static char lnkbuf[MAXPATHLEN + 1];
95 int Bcvt; /* Swap Bytes */
96 int oldinofmt; /* FreeBSD 1 inode format needs cvt */
98 #define FLUSHTAPEBUF() blkcnt = ntrec + 1
100 char *namespace_names[] = EXTATTR_NAMESPACE_NAMES;
102 static void accthdr(struct s_spcl *);
103 static int checksum(int *);
104 static void findinode(struct s_spcl *);
105 static void findtapeblksize(void);
106 static char *setupextattr(int);
107 static void xtrattr(char *, long);
108 static void set_extattr_link(char *, void *, int);
109 static void set_extattr_fd(int, char *, void *, int);
110 static int gethead(struct s_spcl *);
111 static void readtape(char *);
112 static void setdumpnum(void);
113 static u_long swabl(u_long);
114 static u_char *swablong(u_char *, int);
115 static u_char *swabshort(u_char *, int);
116 static void terminateinput(void);
117 static void xtrfile(char *, long);
118 static void xtrlnkfile(char *, long);
119 static void xtrlnkskip(char *, long);
120 static void xtrmap(char *, long);
121 static void xtrmapskip(char *, long);
122 static void xtrskip(char *, long);
125 * Set up an input source
128 setinput(char *source, int ispipecommand)
134 newtapebuf(NTREC > HIGHDENSITYTREC ? NTREC : HIGHDENSITYTREC);
141 if (strchr(source, ':')) {
143 source = strchr(host, ':');
145 if (rmthost(host) == 0)
149 if (strcmp(source, "-") == 0) {
151 * Since input is coming from a pipe we must establish
152 * our own connection to the terminal.
154 terminal = fopen(_PATH_TTY, "r");
155 if (terminal == NULL) {
156 (void)fprintf(stderr, "cannot open %s: %s\n",
157 _PATH_TTY, strerror(errno));
158 terminal = fopen(_PATH_DEVNULL, "r");
159 if (terminal == NULL) {
160 (void)fprintf(stderr, "cannot open %s: %s\n",
161 _PATH_DEVNULL, strerror(errno));
167 /* no longer need or want root privileges */
168 if (setuid(getuid()) != 0) {
169 fprintf(stderr, "setuid failed\n");
172 magtape = strdup(source);
173 if (magtape == NULL) {
174 fprintf(stderr, "Cannot allocate space for magtape buffer\n");
180 newtapebuf(long size)
182 static int tapebufsize = -1;
185 if (size <= tapebufsize)
188 free(tapebuf - TP_BSIZE);
189 tapebuf = malloc((size+1) * TP_BSIZE);
190 if (tapebuf == NULL) {
191 fprintf(stderr, "Cannot allocate space for tape buffer\n");
199 * Verify that the tape drive can be accessed and
200 * that it actually is a dump tape.
208 vprintf(stdout, "Verify tape and initialize maps\n");
210 if (setenv("RESTORE_VOLUME", "1", 1) == -1) {
211 fprintf(stderr, "Cannot set $RESTORE_VOLUME: %s\n",
215 popenfp = popen(magtape, "r");
216 mt = popenfp ? fileno(popenfp) : -1;
220 mt = rmtopen(magtape, 0);
226 mt = open(magtape, O_RDONLY, 0);
228 fprintf(stderr, "%s: %s\n", magtape, strerror(errno));
234 if (!pipein && !pipecmdin && !bflag)
236 if (gethead(&spcl) == FAIL) {
237 fprintf(stderr, "Tape is not a dump tape\n");
241 endoftapemark.s_spcl.c_magic = FS_UFS2_MAGIC;
242 endoftapemark.s_spcl.c_type = TS_END;
243 ip = (int *)&endoftapemark;
244 j = sizeof(union u_spcl) / sizeof(int);
249 endoftapemark.s_spcl.c_checksum = CHECKSUM - i;
251 if (vflag || command == 't')
253 dumptime = _time64_to_time(spcl.c_ddate);
254 dumpdate = _time64_to_time(spcl.c_date);
255 if (stat(".", &stbuf) < 0) {
256 fprintf(stderr, "cannot stat .: %s\n", strerror(errno));
259 if (stbuf.st_blksize > 0 && stbuf.st_blksize < TP_BSIZE )
261 if (stbuf.st_blksize >= TP_BSIZE && stbuf.st_blksize <= MAXBSIZE)
262 fssize = stbuf.st_blksize;
263 if (((fssize - 1) & fssize) != 0) {
264 fprintf(stderr, "bad block size %ld\n", fssize);
267 if (spcl.c_volume != 1) {
268 fprintf(stderr, "Tape is not volume 1 of the dump\n");
271 if (gethead(&spcl) == FAIL) {
272 dprintf(stdout, "header read failed at %ld blocks\n", blksread);
273 panic("no header after volume mark!\n");
276 if (spcl.c_type != TS_CLRI) {
277 fprintf(stderr, "Cannot find file removal list\n");
280 maxino = (spcl.c_count * TP_BSIZE * NBBY) + 1;
281 dprintf(stdout, "maxino = %d\n", maxino);
282 map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
284 panic("no memory for active inode map\n");
286 curfile.action = USING;
287 getfile(xtrmap, xtrmapskip, xtrmapskip);
288 if (spcl.c_type != TS_BITS) {
289 fprintf(stderr, "Cannot find file dump list\n");
292 map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
293 if (map == (char *)NULL)
294 panic("no memory for file dump list\n");
296 curfile.action = USING;
297 getfile(xtrmap, xtrmapskip, xtrmapskip);
299 * If there may be whiteout entries on the tape, pretend that the
300 * whiteout inode exists, so that the whiteout entries can be
303 SETINO(WINO, dumpmap);
304 /* 'r' restores don't call getvol() for tape 1, so mark it as read. */
310 * Prompt user to load a new dump volume.
311 * "Nextvol" is the next suggested volume to use.
312 * This suggested volume is enforced when doing full
313 * or incremental restores, but can be overridden by
314 * the user when only extracting a subset of the files.
320 long i, newvol, savecnt;
321 union u_spcl tmpspcl;
322 # define tmpbuf tmpspcl.s_spcl
329 prevtapea = tapeaddr;
333 panic("Changing volumes on pipe input?\n");
334 /* Avoid looping if we couldn't ask the user. */
335 if (yflag || ferror(terminal) || feof(terminal))
344 done(1); /* pipes do not get a second chance */
345 if (command == 'R' || command == 'r' || curfile.action != SKIP)
349 while (newvol <= 0) {
350 if (tapesread == 0) {
351 fprintf(stderr, "%s%s%s%s%s%s%s",
352 "You have not read any tapes yet.\n",
353 "If you are extracting just a few files,",
354 " start with the last volume\n",
355 "and work towards the first; restore",
356 " can quickly skip tapes that\n",
357 "have no further files to extract.",
358 " Otherwise, begin with volume 1.\n");
360 fprintf(stderr, "You have read volumes");
362 for (i = 0; i < 32; i++)
363 if (tapesread & (1 << i)) {
364 fprintf(stderr, "%s%ld", buf, i + 1);
367 fprintf(stderr, "\n");
370 fprintf(stderr, "Specify next volume #: ");
371 (void) fflush(stderr);
372 if (fgets(buf, BUFSIZ, terminal) == NULL)
374 } while (buf[0] == '\n');
378 "Volume numbers are positive numerics\n");
381 if (newvol == volno) {
382 tapesread |= 1 << (volno - 1);
386 fprintf(stderr, "Mount tape volume %ld\n", newvol);
387 fprintf(stderr, "Enter ``none'' if there are no more tapes\n");
388 fprintf(stderr, "otherwise enter tape name (default: %s) ", magtape);
389 (void) fflush(stderr);
390 if (fgets(buf, BUFSIZ, terminal) == NULL)
392 if (!strcmp(buf, "none\n")) {
396 if (buf[0] != '\n') {
397 (void) strcpy(magtape, buf);
398 magtape[strlen(magtape) - 1] = '\0';
401 char volno[sizeof("2147483647")];
403 (void)sprintf(volno, "%ld", newvol);
404 if (setenv("RESTORE_VOLUME", volno, 1) == -1) {
405 fprintf(stderr, "Cannot set $RESTORE_VOLUME: %s\n",
409 popenfp = popen(magtape, "r");
410 mt = popenfp ? fileno(popenfp) : -1;
414 mt = rmtopen(magtape, 0);
417 mt = open(magtape, O_RDONLY, 0);
420 fprintf(stderr, "Cannot open %s\n", magtape);
428 if (gethead(&tmpbuf) == FAIL) {
429 dprintf(stdout, "header read failed at %ld blocks\n", blksread);
430 fprintf(stderr, "tape is not dump tape\n");
434 if (tmpbuf.c_volume != volno) {
435 fprintf(stderr, "Wrong volume (%jd)\n",
436 (intmax_t)tmpbuf.c_volume);
440 if (_time64_to_time(tmpbuf.c_date) != dumpdate ||
441 _time64_to_time(tmpbuf.c_ddate) != dumptime) {
442 time_t t = _time64_to_time(tmpbuf.c_date);
443 fprintf(stderr, "Wrong dump date\n\tgot: %s", ctime(&t));
444 fprintf(stderr, "\twanted: %s", ctime(&dumpdate));
448 tapesread |= 1 << (volno - 1);
451 * If continuing from the previous volume, skip over any
452 * blocks read already at the end of the previous volume.
454 * If coming to this volume at random, skip to the beginning
455 * of the next record.
457 dprintf(stdout, "last rec %jd, tape starts with %jd\n",
458 (intmax_t)prevtapea, (intmax_t)tmpbuf.c_tapea);
459 if (tmpbuf.c_type == TS_TAPE) {
460 if (curfile.action != USING) {
462 * XXX Dump incorrectly sets c_count to 1 in the
463 * volume header of the first tape, so ignore
464 * c_count when volno == 1.
467 for (i = tmpbuf.c_count; i > 0; i--)
469 } else if (tmpbuf.c_tapea <= prevtapea) {
471 * Normally the value of c_tapea in the volume
472 * header is the record number of the header itself.
473 * However in the volume header following an EOT-
474 * terminated tape, it is the record number of the
475 * first continuation data block (dump bug?).
477 * The next record we want is `prevtapea + 1'.
479 i = prevtapea + 1 - tmpbuf.c_tapea;
480 dprintf(stderr, "Skipping %ld duplicate record%s.\n",
481 i, i > 1 ? "s" : "");
486 if (curfile.action == USING) {
488 panic("active file into volume 1\n");
491 (void) gethead(&spcl);
500 * Handle unexpected EOF.
506 if (gettingfile && curfile.action == USING) {
507 printf("Warning: %s %s\n",
508 "End-of-input encountered while extracting", curfile.name);
510 curfile.name = "<name unknown>";
511 curfile.action = UNKNOWN;
513 curfile.ino = maxino;
521 * handle multiple dumps per tape by skipping forward to the
529 if (dumpnum == 1 || volno != 1)
532 fprintf(stderr, "Cannot have multiple dumps on pipe input\n");
536 tcom.mt_count = dumpnum - 1;
539 rmtioctl(MTFSF, dumpnum - 1);
542 if (!pipecmdin && ioctl(mt, MTIOCTOP, (char *)&tcom) < 0)
543 fprintf(stderr, "ioctl MTFSF: %s\n", strerror(errno));
550 t = _time64_to_time(spcl.c_date);
551 fprintf(stdout, "Dump date: %s", ctime(&t));
552 t = _time64_to_time(spcl.c_ddate);
553 fprintf(stdout, "Dumped from: %s",
554 (spcl.c_ddate == 0) ? "the epoch\n" : ctime(&t));
555 if (spcl.c_host[0] == '\0')
557 fprintf(stderr, "Level %jd dump of %s on %s:%s\n",
558 (intmax_t)spcl.c_level, spcl.c_filesys, spcl.c_host, spcl.c_dev);
559 fprintf(stderr, "Label: %s\n", spcl.c_label);
563 extractfile(char *name)
570 struct timeval mtimep[2], ctimep[2];
575 curfile.action = USING;
576 mtimep[0].tv_sec = curfile.atime_sec;
577 mtimep[0].tv_usec = curfile.atime_nsec / 1000;
578 mtimep[1].tv_sec = curfile.mtime_sec;
579 mtimep[1].tv_usec = curfile.mtime_nsec / 1000;
580 ctimep[0].tv_sec = curfile.atime_sec;
581 ctimep[0].tv_usec = curfile.atime_nsec / 1000;
582 ctimep[1].tv_sec = curfile.birthtime_sec;
583 ctimep[1].tv_usec = curfile.birthtime_nsec / 1000;
584 extsize = curfile.extsize;
590 flags = curfile.file_flags;
591 switch (mode & IFMT) {
594 fprintf(stderr, "%s: unknown file mode 0%o\n", name, mode);
599 vprintf(stdout, "skipped socket %s\n", name);
605 ep = lookupname(name);
606 if (ep == NULL || ep->e_flags & EXTRACT)
607 panic("unextracted directory %s\n", name);
611 vprintf(stdout, "extract file %s\n", name);
612 return (genliteraldir(name, curfile.ino));
617 buf = setupextattr(extsize);
618 getfile(xtrlnkfile, xtrattr, xtrlnkskip);
621 "%s: zero length symbolic link (ignored)\n", name);
624 if (linkit(lnkbuf, name, SYMLINK) == GOOD) {
626 set_extattr_link(name, buf, extsize);
627 (void) lchown(name, uid, gid);
628 (void) lchmod(name, mode);
629 (void) lutimes(name, ctimep);
630 (void) lutimes(name, mtimep);
631 (void) lchflags(name, flags);
637 vprintf(stdout, "extract fifo %s\n", name);
644 if (mkfifo(name, 0600) < 0) {
645 fprintf(stderr, "%s: cannot create fifo: %s\n",
646 name, strerror(errno));
653 buf = setupextattr(extsize);
654 getfile(xtrnull, xtrattr, xtrnull);
655 set_extattr_file(name, buf, extsize);
657 (void) chown(name, uid, gid);
658 (void) chmod(name, mode);
659 (void) utimes(name, ctimep);
660 (void) utimes(name, mtimep);
661 (void) chflags(name, flags);
666 vprintf(stdout, "extract special file %s\n", name);
673 if (mknod(name, (mode & (IFCHR | IFBLK)) | 0600,
674 (int)curfile.rdev) < 0) {
675 fprintf(stderr, "%s: cannot create special file: %s\n",
676 name, strerror(errno));
683 buf = setupextattr(extsize);
684 getfile(xtrnull, xtrattr, xtrnull);
685 set_extattr_file(name, buf, extsize);
687 (void) chown(name, uid, gid);
688 (void) chmod(name, mode);
689 (void) utimes(name, ctimep);
690 (void) utimes(name, mtimep);
691 (void) chflags(name, flags);
695 vprintf(stdout, "extract file %s\n", name);
702 if ((ofile = open(name, O_WRONLY | O_CREAT | O_TRUNC,
704 fprintf(stderr, "%s: cannot create file: %s\n",
705 name, strerror(errno));
709 buf = setupextattr(extsize);
710 getfile(xtrfile, xtrattr, xtrskip);
712 set_extattr_fd(ofile, name, buf, extsize);
713 (void) fchown(ofile, uid, gid);
714 (void) fchmod(ofile, mode);
715 (void) futimes(ofile, ctimep);
716 (void) futimes(ofile, mtimep);
717 (void) fchflags(ofile, flags);
725 * Set attributes for a file.
728 set_extattr_file(char *name, void *buf, int size)
730 struct extattr *eap, *eaend;
732 vprintf(stdout, "Set attributes for %s:", name);
734 for (eap = buf; eap < eaend; eap = EXTATTR_NEXT(eap)) {
736 * Make sure this entry is complete.
738 if (EXTATTR_NEXT(eap) > eaend || eap->ea_length <= 0) {
739 dprintf(stdout, "\n\t%scorrupted",
740 eap == buf ? "" : "remainder ");
743 if (eap->ea_namespace == EXTATTR_NAMESPACE_EMPTY)
745 vprintf(stdout, "\n\t%s, (%d bytes), %*s",
746 namespace_names[eap->ea_namespace], eap->ea_length,
747 eap->ea_namelength, eap->ea_name);
749 * First we try the general attribute setting interface.
750 * However, some attributes can only be set by root or
751 * by using special interfaces (for example, ACLs).
753 if (extattr_set_file(name, eap->ea_namespace, eap->ea_name,
754 EXTATTR_CONTENT(eap), EXTATTR_CONTENT_SIZE(eap)) != -1) {
755 dprintf(stdout, " (set using extattr_set_file)");
759 * If the general interface refuses to set the attribute,
760 * then we try all the specialized interfaces that we
763 if (eap->ea_namespace == EXTATTR_NAMESPACE_SYSTEM &&
764 !strcmp(eap->ea_name, POSIX1E_ACL_ACCESS_EXTATTR_NAME)) {
765 if (acl_set_file(name, ACL_TYPE_ACCESS,
766 EXTATTR_CONTENT(eap)) != -1) {
767 dprintf(stdout, " (set using acl_set_file)");
771 if (eap->ea_namespace == EXTATTR_NAMESPACE_SYSTEM &&
772 !strcmp(eap->ea_name, POSIX1E_ACL_DEFAULT_EXTATTR_NAME)) {
773 if (acl_set_file(name, ACL_TYPE_DEFAULT,
774 EXTATTR_CONTENT(eap)) != -1) {
775 dprintf(stdout, " (set using acl_set_file)");
779 vprintf(stdout, " (unable to set)");
781 vprintf(stdout, "\n");
785 * Set attributes for a symbolic link.
788 set_extattr_link(char *name, void *buf, int size)
790 struct extattr *eap, *eaend;
792 vprintf(stdout, "Set attributes for %s:", name);
794 for (eap = buf; eap < eaend; eap = EXTATTR_NEXT(eap)) {
796 * Make sure this entry is complete.
798 if (EXTATTR_NEXT(eap) > eaend || eap->ea_length <= 0) {
799 dprintf(stdout, "\n\t%scorrupted",
800 eap == buf ? "" : "remainder ");
803 if (eap->ea_namespace == EXTATTR_NAMESPACE_EMPTY)
805 vprintf(stdout, "\n\t%s, (%d bytes), %*s",
806 namespace_names[eap->ea_namespace], eap->ea_length,
807 eap->ea_namelength, eap->ea_name);
809 * First we try the general attribute setting interface.
810 * However, some attributes can only be set by root or
811 * by using special interfaces (for example, ACLs).
813 if (extattr_set_link(name, eap->ea_namespace, eap->ea_name,
814 EXTATTR_CONTENT(eap), EXTATTR_CONTENT_SIZE(eap)) != -1) {
815 dprintf(stdout, " (set using extattr_set_link)");
819 * If the general interface refuses to set the attribute,
820 * then we try all the specialized interfaces that we
823 if (eap->ea_namespace == EXTATTR_NAMESPACE_SYSTEM &&
824 !strcmp(eap->ea_name, POSIX1E_ACL_ACCESS_EXTATTR_NAME)) {
825 if (acl_set_link_np(name, ACL_TYPE_ACCESS,
826 EXTATTR_CONTENT(eap)) != -1) {
827 dprintf(stdout, " (set using acl_set_link_np)");
831 if (eap->ea_namespace == EXTATTR_NAMESPACE_SYSTEM &&
832 !strcmp(eap->ea_name, POSIX1E_ACL_DEFAULT_EXTATTR_NAME)) {
833 if (acl_set_link_np(name, ACL_TYPE_DEFAULT,
834 EXTATTR_CONTENT(eap)) != -1) {
835 dprintf(stdout, " (set using acl_set_link_np)");
839 vprintf(stdout, " (unable to set)");
841 vprintf(stdout, "\n");
845 * Set attributes on a file descriptor.
848 set_extattr_fd(int fd, char *name, void *buf, int size)
850 struct extattr *eap, *eaend;
852 vprintf(stdout, "Set attributes for %s:", name);
854 for (eap = buf; eap < eaend; eap = EXTATTR_NEXT(eap)) {
856 * Make sure this entry is complete.
858 if (EXTATTR_NEXT(eap) > eaend || eap->ea_length <= 0) {
859 dprintf(stdout, "\n\t%scorrupted",
860 eap == buf ? "" : "remainder ");
863 if (eap->ea_namespace == EXTATTR_NAMESPACE_EMPTY)
865 vprintf(stdout, "\n\t%s, (%d bytes), %*s",
866 namespace_names[eap->ea_namespace], eap->ea_length,
867 eap->ea_namelength, eap->ea_name);
869 * First we try the general attribute setting interface.
870 * However, some attributes can only be set by root or
871 * by using special interfaces (for example, ACLs).
873 if (extattr_set_fd(fd, eap->ea_namespace, eap->ea_name,
874 EXTATTR_CONTENT(eap), EXTATTR_CONTENT_SIZE(eap)) != -1) {
875 dprintf(stdout, " (set using extattr_set_fd)");
879 * If the general interface refuses to set the attribute,
880 * then we try all the specialized interfaces that we
883 if (eap->ea_namespace == EXTATTR_NAMESPACE_SYSTEM &&
884 !strcmp(eap->ea_name, POSIX1E_ACL_ACCESS_EXTATTR_NAME)) {
885 if (acl_set_fd(fd, EXTATTR_CONTENT(eap)) != -1) {
886 dprintf(stdout, " (set using acl_set_fd)");
890 if (eap->ea_namespace == EXTATTR_NAMESPACE_SYSTEM &&
891 !strcmp(eap->ea_name, POSIX1E_ACL_DEFAULT_EXTATTR_NAME)) {
892 if (acl_set_file(name, ACL_TYPE_DEFAULT,
893 EXTATTR_CONTENT(eap)) != -1) {
894 dprintf(stdout, " (set using acl_set_file)");
898 vprintf(stdout, " (unable to set)");
900 vprintf(stdout, "\n");
904 * skip over bit maps on the tape
910 while (spcl.c_type == TS_BITS || spcl.c_type == TS_CLRI)
915 * skip over a file on the tape
921 curfile.action = SKIP;
922 getfile(xtrnull, xtrnull, xtrnull);
926 * Extract a file from the tape.
927 * When an allocated block is found it is passed to the fill function;
928 * when an unallocated block (hole) is found, a zeroed buffer is passed
929 * to the skip function.
932 getfile(void (*datafill)(char *, long), void (*attrfill)(char *, long),
933 void (*skip)(char *, long))
937 int curblk, attrsize;
938 void (*fillit)(char *, long);
939 static char clearedbuf[MAXBSIZE];
940 char buf[MAXBSIZE / TP_BSIZE][TP_BSIZE];
945 attrsize = spcl.c_extsize;
946 if (spcl.c_type == TS_END)
947 panic("ran off end of tape\n");
948 if (spcl.c_magic != FS_UFS2_MAGIC)
949 panic("not at beginning of a file\n");
950 if (!gettingfile && setjmp(restart) != 0)
954 if (size == 0 && attrsize > 0) {
960 for (i = 0; i < spcl.c_count; i++) {
961 if (!readmapflag && i > TP_NINDIR) {
963 fprintf(stderr, "spcl.c_count = %jd\n",
964 (intmax_t)spcl.c_count);
967 panic("spcl.c_count = %jd\n",
968 (intmax_t)spcl.c_count);
970 if (readmapflag || spcl.c_addr[i]) {
971 readtape(&buf[curblk++][0]);
972 if (curblk == fssize / TP_BSIZE) {
973 (*fillit)((char *)buf, (long)(size > TP_BSIZE ?
974 fssize : (curblk - 1) * TP_BSIZE + size));
979 (*fillit)((char *)buf, (long)(size > TP_BSIZE ?
981 (curblk - 1) * TP_BSIZE + size));
984 (*skip)(clearedbuf, (long)(size > TP_BSIZE ?
987 if ((size -= TP_BSIZE) <= 0) {
988 if (size > -TP_BSIZE && curblk > 0) {
989 (*fillit)((char *)buf,
990 (long)((curblk * TP_BSIZE) + size));
999 if (spcl.c_count - i > 1)
1000 dprintf(stdout, "skipping %d junk block(s)\n",
1001 spcl.c_count - i - 1);
1002 for (i++; i < spcl.c_count; i++) {
1003 if (!readmapflag && i > TP_NINDIR) {
1006 "spcl.c_count = %jd\n",
1007 (intmax_t)spcl.c_count);
1010 panic("spcl.c_count = %jd\n",
1011 (intmax_t)spcl.c_count);
1013 if (readmapflag || spcl.c_addr[i])
1019 if (gethead(&spcl) == GOOD && size > 0) {
1020 if (spcl.c_type == TS_ADDR)
1023 "Missing address (header) block for %s at %ld blocks\n",
1024 curfile.name, blksread);
1027 panic("getfile: lost data\n");
1033 * These variables are shared between the next two functions.
1035 static int extbufsize = 0;
1036 static char *extbuf;
1040 * Allocate a buffer into which to extract extended attributes.
1043 setupextattr(int extsize)
1047 if (extsize <= extbufsize)
1051 if ((extbuf = malloc(extsize)) != NULL) {
1052 extbufsize = extsize;
1057 fprintf(stderr, "Cannot extract %d bytes %s for inode %d, name %s\n",
1058 extsize, "of extended attributes", curfile.ino, curfile.name);
1063 * Extract the next block of extended attributes.
1066 xtrattr(char *buf, long size)
1069 if (extloc + size > extbufsize)
1070 panic("overrun attribute buffer\n");
1071 memmove(&extbuf[extloc], buf, size);
1076 * Write out the next block of a file.
1079 xtrfile(char *buf, long size)
1084 if (write(ofile, buf, (int) size) == -1) {
1086 "write error extracting inode %d, name %s\nwrite: %s\n",
1087 curfile.ino, curfile.name, strerror(errno));
1092 * Skip over a hole in a file.
1096 xtrskip(char *buf, long size)
1099 if (lseek(ofile, size, SEEK_CUR) == -1) {
1101 "seek error extracting inode %d, name %s\nlseek: %s\n",
1102 curfile.ino, curfile.name, strerror(errno));
1108 * Collect the next block of a symbolic link.
1111 xtrlnkfile(char *buf, long size)
1115 if (pathlen > MAXPATHLEN) {
1116 fprintf(stderr, "symbolic link name: %s->%s%s; too long %d\n",
1117 curfile.name, lnkbuf, buf, pathlen);
1120 (void) strcat(lnkbuf, buf);
1124 * Skip over a hole in a symbolic link (should never happen).
1128 xtrlnkskip(char *buf, long size)
1131 fprintf(stderr, "unallocated block in symbolic link %s\n",
1137 * Collect the next block of a bit map.
1140 xtrmap(char *buf, long size)
1143 memmove(map, buf, size);
1148 * Skip over a hole in a bit map (should never happen).
1152 xtrmapskip(char *buf, long size)
1155 panic("hole in map\n");
1160 * Noop, when an extraction function is not needed.
1164 xtrnull(char *buf, long size)
1171 * Read TP_BSIZE blocks from the input.
1172 * Handle read errors, and end of media.
1177 long rd, newvol, i, oldnumtrec;
1178 int cnt, seek_failed;
1180 if (blkcnt + (byteslide > 0) < numtrec) {
1181 memmove(buf, &tapebuf[(blkcnt++ * TP_BSIZE) + byteslide], (long)TP_BSIZE);
1187 memmove(&tapebuf[-TP_BSIZE],
1188 &tapebuf[(numtrec-1) * TP_BSIZE], (long)TP_BSIZE);
1189 oldnumtrec = numtrec;
1190 for (i = 0; i < ntrec; i++)
1191 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1194 cnt = ntrec * TP_BSIZE;
1199 i = rmtread(&tapebuf[rd], cnt);
1202 i = read(mt, &tapebuf[rd], cnt);
1204 * Check for mid-tape short read error.
1205 * If found, skip rest of buffer and start with the next.
1207 if (!pipein && !pipecmdin && numtrec < ntrec && i > 0) {
1208 dprintf(stdout, "mid-media short read error.\n");
1212 * Handle partial block read.
1214 if ((pipein || pipecmdin) && i == 0 && rd > 0)
1216 else if (i > 0 && i != ntrec * TP_BSIZE) {
1217 if (pipein || pipecmdin) {
1225 * Short read. Process the blocks read.
1227 if (i % TP_BSIZE != 0)
1229 "partial block read: %ld should be %ld\n",
1230 i, ntrec * TP_BSIZE);
1231 numtrec = i / TP_BSIZE;
1235 * Handle read error.
1238 fprintf(stderr, "Tape read error while ");
1239 switch (curfile.action) {
1241 fprintf(stderr, "trying to set up tape\n");
1244 fprintf(stderr, "trying to resynchronize\n");
1247 fprintf(stderr, "restoring %s\n", curfile.name);
1250 fprintf(stderr, "skipping over inode %d\n",
1254 if (!yflag && !reply("continue"))
1256 i = ntrec * TP_BSIZE;
1257 memset(tapebuf, 0, i);
1260 seek_failed = (rmtseek(i, 1) < 0);
1263 seek_failed = (lseek(mt, i, SEEK_CUR) == (off_t)-1);
1267 "continuation failed: %s\n", strerror(errno));
1272 * Handle end of tape.
1275 vprintf(stdout, "End-of-tape encountered\n");
1284 if (rd % TP_BSIZE != 0)
1285 panic("partial block read: %ld should be %ld\n",
1286 rd, ntrec * TP_BSIZE);
1288 memmove(&tapebuf[rd], &endoftapemark, (long)TP_BSIZE);
1290 if (oldnumtrec == 0)
1293 blkcnt -= oldnumtrec;
1295 &tapebuf[(blkcnt++ * TP_BSIZE) + byteslide], (long)TP_BSIZE);
1301 findtapeblksize(void)
1305 for (i = 0; i < ntrec; i++)
1306 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1310 i = rmtread(tapebuf, ntrec * TP_BSIZE);
1313 i = read(mt, tapebuf, ntrec * TP_BSIZE);
1316 fprintf(stderr, "tape read error: %s\n", strerror(errno));
1319 if (i % TP_BSIZE != 0) {
1320 fprintf(stderr, "Tape block size (%ld) %s (%d)\n",
1321 i, "is not a multiple of dump block size", TP_BSIZE);
1324 ntrec = i / TP_BSIZE;
1326 vprintf(stdout, "Tape block size is %ld\n", ntrec);
1348 * Read the next block from the tape.
1349 * If it is not any valid header, return an error.
1352 gethead(struct s_spcl *buf)
1356 readtape((char *)buf);
1357 if (buf->c_magic != FS_UFS2_MAGIC && buf->c_magic != NFS_MAGIC) {
1358 if (buf->c_magic == OFS_MAGIC) {
1360 "Format of dump tape is too old. Must use\n");
1362 "a version of restore from before 2002.\n");
1365 if (swabl(buf->c_magic) != FS_UFS2_MAGIC &&
1366 buf->c_magic != NFS_MAGIC) {
1367 if (buf->c_magic == OFS_MAGIC) {
1369 "Format of dump tape is too old. Must use\n");
1371 "a version of restore from before 2002.\n");
1376 vprintf(stdout, "Note: Doing Byte swapping\n");
1380 if (checksum((int *)buf) == FAIL)
1383 swabst((u_char *)"8l4s1q8l2q17l", (u_char *)buf);
1384 swabst((u_char *)"l",(u_char *) &buf->c_level);
1385 swabst((u_char *)"2l4q",(u_char *) &buf->c_flags);
1389 switch (buf->c_type) {
1394 * Have to patch up missing information in bit map headers
1396 buf->c_size = buf->c_count * TP_BSIZE;
1397 if (buf->c_count > TP_NINDIR)
1400 for (i = 0; i < buf->c_count; i++)
1405 if (buf->c_magic == NFS_MAGIC &&
1406 (buf->c_flags & NFS_DR_NEWINODEFMT) == 0)
1417 * For old dump tapes, have to copy up old fields to
1420 if (buf->c_magic == NFS_MAGIC) {
1421 buf->c_tapea = buf->c_old_tapea;
1422 buf->c_firstrec = buf->c_old_firstrec;
1423 buf->c_date = _time32_to_time(buf->c_old_date);
1424 buf->c_ddate = _time32_to_time(buf->c_old_ddate);
1425 buf->c_atime = _time32_to_time(buf->c_old_atime);
1426 buf->c_mtime = _time32_to_time(buf->c_old_mtime);
1427 buf->c_birthtime = 0;
1428 buf->c_birthtimensec = 0;
1434 panic("gethead: unknown inode type %d\n", buf->c_type);
1437 if (dumpdate != 0 && _time64_to_time(buf->c_date) != dumpdate)
1438 fprintf(stderr, "Header with wrong dumpdate.\n");
1440 * If we're restoring a filesystem with the old (FreeBSD 1)
1441 * format inodes, copy the uid/gid to the new location
1444 buf->c_uid = buf->c_spare1[1];
1445 buf->c_gid = buf->c_spare1[2];
1447 buf->c_magic = FS_UFS2_MAGIC;
1448 tapeaddr = buf->c_tapea;
1455 * Check that a header is where it belongs and predict the next header
1458 accthdr(struct s_spcl *header)
1460 static ino_t previno = 0x7fffffff;
1461 static int prevtype;
1462 static long predict;
1465 if (header->c_type == TS_TAPE) {
1466 fprintf(stderr, "Volume header ");
1467 if (header->c_firstrec)
1468 fprintf(stderr, "begins with record %jd",
1469 (intmax_t)header->c_firstrec);
1470 fprintf(stderr, "\n");
1471 previno = 0x7fffffff;
1474 if (previno == 0x7fffffff)
1478 fprintf(stderr, "Dumped inodes map header");
1481 fprintf(stderr, "Used inodes map header");
1484 fprintf(stderr, "File header, ino %d", previno);
1487 fprintf(stderr, "File continuation header, ino %d", previno);
1490 fprintf(stderr, "End of tape header");
1493 if (predict != blksread - 1)
1494 fprintf(stderr, "; predicted %ld blocks, got %ld blocks",
1495 predict, blksread - 1);
1496 fprintf(stderr, "\n");
1499 if (header->c_type != TS_END)
1500 for (i = 0; i < header->c_count; i++)
1501 if (readmapflag || header->c_addr[i] != 0)
1505 prevtype = header->c_type;
1506 previno = header->c_inumber;
1510 * Find an inode header.
1511 * Complain if had to skip.
1514 findinode(struct s_spcl *header)
1516 static long skipcnt = 0;
1521 curfile.name = "<name unknown>";
1522 curfile.action = UNKNOWN;
1526 htype = header->c_type;
1531 * Skip up to the beginning of the next record
1533 for (i = 0; i < header->c_count; i++)
1534 if (header->c_addr[i])
1536 while (gethead(header) == FAIL ||
1537 _time64_to_time(header->c_date) != dumpdate) {
1541 if (byteslide < TP_BSIZE) {
1551 curfile.mode = header->c_mode;
1552 curfile.uid = header->c_uid;
1553 curfile.gid = header->c_gid;
1554 curfile.file_flags = header->c_file_flags;
1555 curfile.rdev = header->c_rdev;
1556 curfile.atime_sec = header->c_atime;
1557 curfile.atime_nsec = header->c_atimensec;
1558 curfile.mtime_sec = header->c_mtime;
1559 curfile.mtime_nsec = header->c_mtimensec;
1560 curfile.birthtime_sec = header->c_birthtime;
1561 curfile.birthtime_nsec = header->c_birthtimensec;
1562 curfile.extsize = header->c_extsize;
1563 curfile.size = header->c_size;
1564 curfile.ino = header->c_inumber;
1568 /* If we missed some tapes, get another volume. */
1569 if (tapesread & (tapesread + 1)) {
1573 curfile.ino = maxino;
1577 curfile.name = "<file removal list>";
1581 curfile.name = "<file dump list>";
1586 fprintf(stderr, "unexpected tape header\n");
1588 panic("unexpected tape header\n");
1592 fprintf(stderr, "unknown tape header type %d\n",
1595 panic("unknown tape header type %d\n",
1597 while (gethead(header) == FAIL ||
1598 _time64_to_time(header->c_date) != dumpdate) {
1602 if (byteslide < TP_BSIZE) {
1611 } while (htype == TS_ADDR);
1613 fprintf(stderr, "resync restore, skipped %ld %s\n",
1614 skipcnt, Dflag ? "bytes" : "blocks");
1623 j = sizeof(union u_spcl) / sizeof(int);
1630 /* What happens if we want to read restore tapes
1631 for a 16bit int machine??? */
1637 if (i != CHECKSUM) {
1638 fprintf(stderr, "Checksum error %o, inode %d file %s\n", i,
1639 curfile.ino, curfile.name);
1649 msg(const char *fmt, ...)
1653 (void)vfprintf(stderr, fmt, ap);
1656 #endif /* RRESTORE */
1659 swabshort(u_char *sp, int n)
1664 c = sp[0]; sp[0] = sp[1]; sp[1] = c;
1671 swablong(u_char *sp, int n)
1676 c = sp[0]; sp[0] = sp[3]; sp[3] = c;
1677 c = sp[2]; sp[2] = sp[1]; sp[1] = c;
1684 swabquad(u_char *sp, int n)
1689 c = sp[0]; sp[0] = sp[7]; sp[7] = c;
1690 c = sp[1]; sp[1] = sp[6]; sp[6] = c;
1691 c = sp[2]; sp[2] = sp[5]; sp[5] = c;
1692 c = sp[3]; sp[3] = sp[4]; sp[4] = c;
1699 swabst(u_char *cp, u_char *sp)
1705 case '0': case '1': case '2': case '3': case '4':
1706 case '5': case '6': case '7': case '8': case '9':
1707 n = (n * 10) + (*cp++ - '0');
1710 case 's': case 'w': case 'h':
1713 sp = swabshort(sp, n);
1719 sp = swablong(sp, n);
1725 sp = swabquad(sp, n);
1735 fprintf(stderr, "Unknown conversion character: %c\n",
1748 swabst((u_char *)"l", (u_char *)&x);