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 setuid(getuid()); /* no longer need or want root privileges */
168 magtape = strdup(source);
169 if (magtape == NULL) {
170 fprintf(stderr, "Cannot allocate space for magtape buffer\n");
176 newtapebuf(long size)
178 static int tapebufsize = -1;
181 if (size <= tapebufsize)
184 free(tapebuf - TP_BSIZE);
185 tapebuf = malloc((size+1) * TP_BSIZE);
186 if (tapebuf == NULL) {
187 fprintf(stderr, "Cannot allocate space for tape buffer\n");
195 * Verify that the tape drive can be accessed and
196 * that it actually is a dump tape.
204 vprintf(stdout, "Verify tape and initialize maps\n");
206 if (setenv("RESTORE_VOLUME", "1", 1) == -1) {
207 fprintf(stderr, "Cannot set $RESTORE_VOLUME: %s\n",
211 popenfp = popen(magtape, "r");
212 mt = popenfp ? fileno(popenfp) : -1;
216 mt = rmtopen(magtape, 0);
222 mt = open(magtape, O_RDONLY, 0);
224 fprintf(stderr, "%s: %s\n", magtape, strerror(errno));
230 if (!pipein && !pipecmdin && !bflag)
232 if (gethead(&spcl) == FAIL) {
233 fprintf(stderr, "Tape is not a dump tape\n");
237 endoftapemark.s_spcl.c_magic = FS_UFS2_MAGIC;
238 endoftapemark.s_spcl.c_type = TS_END;
239 ip = (int *)&endoftapemark;
240 j = sizeof(union u_spcl) / sizeof(int);
245 endoftapemark.s_spcl.c_checksum = CHECKSUM - i;
247 if (vflag || command == 't')
249 dumptime = _time64_to_time(spcl.c_ddate);
250 dumpdate = _time64_to_time(spcl.c_date);
251 if (stat(".", &stbuf) < 0) {
252 fprintf(stderr, "cannot stat .: %s\n", strerror(errno));
255 if (stbuf.st_blksize > 0 && stbuf.st_blksize < TP_BSIZE )
257 if (stbuf.st_blksize >= TP_BSIZE && stbuf.st_blksize <= MAXBSIZE)
258 fssize = stbuf.st_blksize;
259 if (((fssize - 1) & fssize) != 0) {
260 fprintf(stderr, "bad block size %ld\n", fssize);
263 if (spcl.c_volume != 1) {
264 fprintf(stderr, "Tape is not volume 1 of the dump\n");
267 if (gethead(&spcl) == FAIL) {
268 dprintf(stdout, "header read failed at %ld blocks\n", blksread);
269 panic("no header after volume mark!\n");
272 if (spcl.c_type != TS_CLRI) {
273 fprintf(stderr, "Cannot find file removal list\n");
276 maxino = (spcl.c_count * TP_BSIZE * NBBY) + 1;
277 dprintf(stdout, "maxino = %d\n", maxino);
278 map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
280 panic("no memory for active inode map\n");
282 curfile.action = USING;
283 getfile(xtrmap, xtrmapskip, xtrmapskip);
284 if (spcl.c_type != TS_BITS) {
285 fprintf(stderr, "Cannot find file dump list\n");
288 map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
289 if (map == (char *)NULL)
290 panic("no memory for file dump list\n");
292 curfile.action = USING;
293 getfile(xtrmap, xtrmapskip, xtrmapskip);
295 * If there may be whiteout entries on the tape, pretend that the
296 * whiteout inode exists, so that the whiteout entries can be
299 SETINO(WINO, dumpmap);
300 /* 'r' restores don't call getvol() for tape 1, so mark it as read. */
306 * Prompt user to load a new dump volume.
307 * "Nextvol" is the next suggested volume to use.
308 * This suggested volume is enforced when doing full
309 * or incremental restores, but can be overridden by
310 * the user when only extracting a subset of the files.
316 long i, newvol, savecnt;
317 union u_spcl tmpspcl;
318 # define tmpbuf tmpspcl.s_spcl
325 prevtapea = tapeaddr;
329 panic("Changing volumes on pipe input?\n");
330 /* Avoid looping if we couldn't ask the user. */
331 if (yflag || ferror(terminal) || feof(terminal))
340 done(1); /* pipes do not get a second chance */
341 if (command == 'R' || command == 'r' || curfile.action != SKIP)
345 while (newvol <= 0) {
346 if (tapesread == 0) {
347 fprintf(stderr, "%s%s%s%s%s%s%s",
348 "You have not read any tapes yet.\n",
349 "If you are extracting just a few files,",
350 " start with the last volume\n",
351 "and work towards the first; restore",
352 " can quickly skip tapes that\n",
353 "have no further files to extract.",
354 " Otherwise, begin with volume 1.\n");
356 fprintf(stderr, "You have read volumes");
358 for (i = 0; i < 32; i++)
359 if (tapesread & (1 << i)) {
360 fprintf(stderr, "%s%ld", buf, i + 1);
363 fprintf(stderr, "\n");
366 fprintf(stderr, "Specify next volume #: ");
367 (void) fflush(stderr);
368 if (fgets(buf, BUFSIZ, terminal) == NULL)
370 } while (buf[0] == '\n');
374 "Volume numbers are positive numerics\n");
377 if (newvol == volno) {
378 tapesread |= 1 << (volno - 1);
382 fprintf(stderr, "Mount tape volume %ld\n", newvol);
383 fprintf(stderr, "Enter ``none'' if there are no more tapes\n");
384 fprintf(stderr, "otherwise enter tape name (default: %s) ", magtape);
385 (void) fflush(stderr);
386 if (fgets(buf, BUFSIZ, terminal) == NULL)
388 if (!strcmp(buf, "none\n")) {
392 if (buf[0] != '\n') {
393 (void) strcpy(magtape, buf);
394 magtape[strlen(magtape) - 1] = '\0';
397 char volno[sizeof("2147483647")];
399 (void)sprintf(volno, "%ld", newvol);
400 if (setenv("RESTORE_VOLUME", volno, 1) == -1) {
401 fprintf(stderr, "Cannot set $RESTORE_VOLUME: %s\n",
405 popenfp = popen(magtape, "r");
406 mt = popenfp ? fileno(popenfp) : -1;
410 mt = rmtopen(magtape, 0);
413 mt = open(magtape, O_RDONLY, 0);
416 fprintf(stderr, "Cannot open %s\n", magtape);
424 if (gethead(&tmpbuf) == FAIL) {
425 dprintf(stdout, "header read failed at %ld blocks\n", blksread);
426 fprintf(stderr, "tape is not dump tape\n");
430 if (tmpbuf.c_volume != volno) {
431 fprintf(stderr, "Wrong volume (%jd)\n",
432 (intmax_t)tmpbuf.c_volume);
436 if (_time64_to_time(tmpbuf.c_date) != dumpdate ||
437 _time64_to_time(tmpbuf.c_ddate) != dumptime) {
438 time_t t = _time64_to_time(tmpbuf.c_date);
439 fprintf(stderr, "Wrong dump date\n\tgot: %s", ctime(&t));
440 fprintf(stderr, "\twanted: %s", ctime(&dumpdate));
444 tapesread |= 1 << (volno - 1);
447 * If continuing from the previous volume, skip over any
448 * blocks read already at the end of the previous volume.
450 * If coming to this volume at random, skip to the beginning
451 * of the next record.
453 dprintf(stdout, "last rec %jd, tape starts with %jd\n",
454 (intmax_t)prevtapea, (intmax_t)tmpbuf.c_tapea);
455 if (tmpbuf.c_type == TS_TAPE) {
456 if (curfile.action != USING) {
458 * XXX Dump incorrectly sets c_count to 1 in the
459 * volume header of the first tape, so ignore
460 * c_count when volno == 1.
463 for (i = tmpbuf.c_count; i > 0; i--)
465 } else if (tmpbuf.c_tapea <= prevtapea) {
467 * Normally the value of c_tapea in the volume
468 * header is the record number of the header itself.
469 * However in the volume header following an EOT-
470 * terminated tape, it is the record number of the
471 * first continuation data block (dump bug?).
473 * The next record we want is `prevtapea + 1'.
475 i = prevtapea + 1 - tmpbuf.c_tapea;
476 dprintf(stderr, "Skipping %ld duplicate record%s.\n",
477 i, i > 1 ? "s" : "");
482 if (curfile.action == USING) {
484 panic("active file into volume 1\n");
487 (void) gethead(&spcl);
496 * Handle unexpected EOF.
502 if (gettingfile && curfile.action == USING) {
503 printf("Warning: %s %s\n",
504 "End-of-input encountered while extracting", curfile.name);
506 curfile.name = "<name unknown>";
507 curfile.action = UNKNOWN;
509 curfile.ino = maxino;
517 * handle multiple dumps per tape by skipping forward to the
525 if (dumpnum == 1 || volno != 1)
528 fprintf(stderr, "Cannot have multiple dumps on pipe input\n");
532 tcom.mt_count = dumpnum - 1;
535 rmtioctl(MTFSF, dumpnum - 1);
538 if (!pipecmdin && ioctl(mt, MTIOCTOP, (char *)&tcom) < 0)
539 fprintf(stderr, "ioctl MTFSF: %s\n", strerror(errno));
546 t = _time64_to_time(spcl.c_date);
547 fprintf(stdout, "Dump date: %s", ctime(&t));
548 t = _time64_to_time(spcl.c_ddate);
549 fprintf(stdout, "Dumped from: %s",
550 (spcl.c_ddate == 0) ? "the epoch\n" : ctime(&t));
551 if (spcl.c_host[0] == '\0')
553 fprintf(stderr, "Level %jd dump of %s on %s:%s\n",
554 (intmax_t)spcl.c_level, spcl.c_filesys, spcl.c_host, spcl.c_dev);
555 fprintf(stderr, "Label: %s\n", spcl.c_label);
559 extractfile(char *name)
566 struct timeval mtimep[2], ctimep[2];
571 curfile.action = USING;
572 mtimep[0].tv_sec = curfile.atime_sec;
573 mtimep[0].tv_usec = curfile.atime_nsec / 1000;
574 mtimep[1].tv_sec = curfile.mtime_sec;
575 mtimep[1].tv_usec = curfile.mtime_nsec / 1000;
576 ctimep[0].tv_sec = curfile.atime_sec;
577 ctimep[0].tv_usec = curfile.atime_nsec / 1000;
578 ctimep[1].tv_sec = curfile.birthtime_sec;
579 ctimep[1].tv_usec = curfile.birthtime_nsec / 1000;
580 extsize = curfile.extsize;
586 flags = curfile.file_flags;
587 switch (mode & IFMT) {
590 fprintf(stderr, "%s: unknown file mode 0%o\n", name, mode);
595 vprintf(stdout, "skipped socket %s\n", name);
601 ep = lookupname(name);
602 if (ep == NULL || ep->e_flags & EXTRACT)
603 panic("unextracted directory %s\n", name);
607 vprintf(stdout, "extract file %s\n", name);
608 return (genliteraldir(name, curfile.ino));
613 buf = setupextattr(extsize);
614 getfile(xtrlnkfile, xtrattr, xtrlnkskip);
617 "%s: zero length symbolic link (ignored)\n", name);
620 if (linkit(lnkbuf, name, SYMLINK) == GOOD) {
622 set_extattr_link(name, buf, extsize);
623 (void) lchown(name, uid, gid);
624 (void) lchmod(name, mode);
625 (void) lutimes(name, ctimep);
626 (void) lutimes(name, mtimep);
627 (void) lchflags(name, flags);
633 vprintf(stdout, "extract fifo %s\n", name);
640 if (mkfifo(name, 0600) < 0) {
641 fprintf(stderr, "%s: cannot create fifo: %s\n",
642 name, strerror(errno));
649 buf = setupextattr(extsize);
650 getfile(xtrnull, xtrattr, xtrnull);
651 set_extattr_file(name, buf, extsize);
653 (void) chown(name, uid, gid);
654 (void) chmod(name, mode);
655 (void) utimes(name, ctimep);
656 (void) utimes(name, mtimep);
657 (void) chflags(name, flags);
662 vprintf(stdout, "extract special file %s\n", name);
669 if (mknod(name, (mode & (IFCHR | IFBLK)) | 0600,
670 (int)curfile.rdev) < 0) {
671 fprintf(stderr, "%s: cannot create special file: %s\n",
672 name, strerror(errno));
679 buf = setupextattr(extsize);
680 getfile(xtrnull, xtrattr, xtrnull);
681 set_extattr_file(name, buf, extsize);
683 (void) chown(name, uid, gid);
684 (void) chmod(name, mode);
685 (void) utimes(name, ctimep);
686 (void) utimes(name, mtimep);
687 (void) chflags(name, flags);
691 vprintf(stdout, "extract file %s\n", name);
698 if ((ofile = open(name, O_WRONLY | O_CREAT | O_TRUNC,
700 fprintf(stderr, "%s: cannot create file: %s\n",
701 name, strerror(errno));
705 buf = setupextattr(extsize);
706 getfile(xtrfile, xtrattr, xtrskip);
708 set_extattr_fd(ofile, name, buf, extsize);
709 (void) fchown(ofile, uid, gid);
710 (void) fchmod(ofile, mode);
711 (void) futimes(ofile, ctimep);
712 (void) futimes(ofile, mtimep);
713 (void) fchflags(ofile, flags);
721 * Set attributes for a file.
724 set_extattr_file(char *name, void *buf, int size)
726 struct extattr *eap, *eaend;
728 vprintf(stdout, "Set attributes for %s:", name);
730 for (eap = buf; eap < eaend; eap = EXTATTR_NEXT(eap)) {
732 * Make sure this entry is complete.
734 if (EXTATTR_NEXT(eap) > eaend || eap->ea_length <= 0) {
735 dprintf(stdout, "\n\t%scorrupted",
736 eap == buf ? "" : "remainder ");
739 if (eap->ea_namespace == EXTATTR_NAMESPACE_EMPTY)
741 vprintf(stdout, "\n\t%s, (%d bytes), %*s",
742 namespace_names[eap->ea_namespace], eap->ea_length,
743 eap->ea_namelength, eap->ea_name);
745 * First we try the general attribute setting interface.
746 * However, some attributes can only be set by root or
747 * by using special interfaces (for example, ACLs).
749 if (extattr_set_file(name, eap->ea_namespace, eap->ea_name,
750 EXTATTR_CONTENT(eap), EXTATTR_CONTENT_SIZE(eap)) != -1) {
751 dprintf(stdout, " (set using extattr_set_file)");
755 * If the general interface refuses to set the attribute,
756 * then we try all the specialized interfaces that we
759 if (eap->ea_namespace == EXTATTR_NAMESPACE_SYSTEM &&
760 !strcmp(eap->ea_name, POSIX1E_ACL_ACCESS_EXTATTR_NAME)) {
761 if (acl_set_file(name, ACL_TYPE_ACCESS,
762 EXTATTR_CONTENT(eap)) != -1) {
763 dprintf(stdout, " (set using acl_set_file)");
767 if (eap->ea_namespace == EXTATTR_NAMESPACE_SYSTEM &&
768 !strcmp(eap->ea_name, POSIX1E_ACL_DEFAULT_EXTATTR_NAME)) {
769 if (acl_set_file(name, ACL_TYPE_DEFAULT,
770 EXTATTR_CONTENT(eap)) != -1) {
771 dprintf(stdout, " (set using acl_set_file)");
775 vprintf(stdout, " (unable to set)");
777 vprintf(stdout, "\n");
781 * Set attributes for a symbolic link.
784 set_extattr_link(char *name, void *buf, int size)
786 struct extattr *eap, *eaend;
788 vprintf(stdout, "Set attributes for %s:", name);
790 for (eap = buf; eap < eaend; eap = EXTATTR_NEXT(eap)) {
792 * Make sure this entry is complete.
794 if (EXTATTR_NEXT(eap) > eaend || eap->ea_length <= 0) {
795 dprintf(stdout, "\n\t%scorrupted",
796 eap == buf ? "" : "remainder ");
799 if (eap->ea_namespace == EXTATTR_NAMESPACE_EMPTY)
801 vprintf(stdout, "\n\t%s, (%d bytes), %*s",
802 namespace_names[eap->ea_namespace], eap->ea_length,
803 eap->ea_namelength, eap->ea_name);
805 * First we try the general attribute setting interface.
806 * However, some attributes can only be set by root or
807 * by using special interfaces (for example, ACLs).
809 if (extattr_set_link(name, eap->ea_namespace, eap->ea_name,
810 EXTATTR_CONTENT(eap), EXTATTR_CONTENT_SIZE(eap)) != -1) {
811 dprintf(stdout, " (set using extattr_set_link)");
815 * If the general interface refuses to set the attribute,
816 * then we try all the specialized interfaces that we
819 if (eap->ea_namespace == EXTATTR_NAMESPACE_SYSTEM &&
820 !strcmp(eap->ea_name, POSIX1E_ACL_ACCESS_EXTATTR_NAME)) {
821 if (acl_set_link_np(name, ACL_TYPE_ACCESS,
822 EXTATTR_CONTENT(eap)) != -1) {
823 dprintf(stdout, " (set using acl_set_link_np)");
827 if (eap->ea_namespace == EXTATTR_NAMESPACE_SYSTEM &&
828 !strcmp(eap->ea_name, POSIX1E_ACL_DEFAULT_EXTATTR_NAME)) {
829 if (acl_set_link_np(name, ACL_TYPE_DEFAULT,
830 EXTATTR_CONTENT(eap)) != -1) {
831 dprintf(stdout, " (set using acl_set_link_np)");
835 vprintf(stdout, " (unable to set)");
837 vprintf(stdout, "\n");
841 * Set attributes on a file descriptor.
844 set_extattr_fd(int fd, char *name, void *buf, int size)
846 struct extattr *eap, *eaend;
848 vprintf(stdout, "Set attributes for %s:", name);
850 for (eap = buf; eap < eaend; eap = EXTATTR_NEXT(eap)) {
852 * Make sure this entry is complete.
854 if (EXTATTR_NEXT(eap) > eaend || eap->ea_length <= 0) {
855 dprintf(stdout, "\n\t%scorrupted",
856 eap == buf ? "" : "remainder ");
859 if (eap->ea_namespace == EXTATTR_NAMESPACE_EMPTY)
861 vprintf(stdout, "\n\t%s, (%d bytes), %*s",
862 namespace_names[eap->ea_namespace], eap->ea_length,
863 eap->ea_namelength, eap->ea_name);
865 * First we try the general attribute setting interface.
866 * However, some attributes can only be set by root or
867 * by using special interfaces (for example, ACLs).
869 if (extattr_set_fd(fd, eap->ea_namespace, eap->ea_name,
870 EXTATTR_CONTENT(eap), EXTATTR_CONTENT_SIZE(eap)) != -1) {
871 dprintf(stdout, " (set using extattr_set_fd)");
875 * If the general interface refuses to set the attribute,
876 * then we try all the specialized interfaces that we
879 if (eap->ea_namespace == EXTATTR_NAMESPACE_SYSTEM &&
880 !strcmp(eap->ea_name, POSIX1E_ACL_ACCESS_EXTATTR_NAME)) {
881 if (acl_set_fd(fd, EXTATTR_CONTENT(eap)) != -1) {
882 dprintf(stdout, " (set using acl_set_fd)");
886 if (eap->ea_namespace == EXTATTR_NAMESPACE_SYSTEM &&
887 !strcmp(eap->ea_name, POSIX1E_ACL_DEFAULT_EXTATTR_NAME)) {
888 if (acl_set_file(name, ACL_TYPE_DEFAULT,
889 EXTATTR_CONTENT(eap)) != -1) {
890 dprintf(stdout, " (set using acl_set_file)");
894 vprintf(stdout, " (unable to set)");
896 vprintf(stdout, "\n");
900 * skip over bit maps on the tape
906 while (spcl.c_type == TS_BITS || spcl.c_type == TS_CLRI)
911 * skip over a file on the tape
917 curfile.action = SKIP;
918 getfile(xtrnull, xtrnull, xtrnull);
922 * Extract a file from the tape.
923 * When an allocated block is found it is passed to the fill function;
924 * when an unallocated block (hole) is found, a zeroed buffer is passed
925 * to the skip function.
928 getfile(void (*datafill)(char *, long), void (*attrfill)(char *, long),
929 void (*skip)(char *, long))
933 int curblk, attrsize;
934 void (*fillit)(char *, long);
935 static char clearedbuf[MAXBSIZE];
936 char buf[MAXBSIZE / TP_BSIZE][TP_BSIZE];
941 attrsize = spcl.c_extsize;
942 if (spcl.c_type == TS_END)
943 panic("ran off end of tape\n");
944 if (spcl.c_magic != FS_UFS2_MAGIC)
945 panic("not at beginning of a file\n");
946 if (!gettingfile && setjmp(restart) != 0)
950 if (size == 0 && attrsize > 0) {
956 for (i = 0; i < spcl.c_count; i++) {
957 if (!readmapflag && i > TP_NINDIR) {
959 fprintf(stderr, "spcl.c_count = %jd\n",
960 (intmax_t)spcl.c_count);
963 panic("spcl.c_count = %jd\n",
964 (intmax_t)spcl.c_count);
966 if (readmapflag || spcl.c_addr[i]) {
967 readtape(&buf[curblk++][0]);
968 if (curblk == fssize / TP_BSIZE) {
969 (*fillit)((char *)buf, (long)(size > TP_BSIZE ?
970 fssize : (curblk - 1) * TP_BSIZE + size));
975 (*fillit)((char *)buf, (long)(size > TP_BSIZE ?
977 (curblk - 1) * TP_BSIZE + size));
980 (*skip)(clearedbuf, (long)(size > TP_BSIZE ?
983 if ((size -= TP_BSIZE) <= 0) {
984 if (size > -TP_BSIZE && curblk > 0) {
985 (*fillit)((char *)buf,
986 (long)((curblk * TP_BSIZE) + size));
995 if (spcl.c_count - i > 1)
996 dprintf(stdout, "skipping %d junk block(s)\n",
997 spcl.c_count - i - 1);
998 for (i++; i < spcl.c_count; i++) {
999 if (!readmapflag && i > TP_NINDIR) {
1002 "spcl.c_count = %jd\n",
1003 (intmax_t)spcl.c_count);
1006 panic("spcl.c_count = %jd\n",
1007 (intmax_t)spcl.c_count);
1009 if (readmapflag || spcl.c_addr[i])
1015 if (gethead(&spcl) == GOOD && size > 0) {
1016 if (spcl.c_type == TS_ADDR)
1019 "Missing address (header) block for %s at %ld blocks\n",
1020 curfile.name, blksread);
1023 panic("getfile: lost data\n");
1029 * These variables are shared between the next two functions.
1031 static int extbufsize = 0;
1032 static char *extbuf;
1036 * Allocate a buffer into which to extract extended attributes.
1039 setupextattr(int extsize)
1043 if (extsize <= extbufsize)
1047 if ((extbuf = malloc(extsize)) != NULL) {
1048 extbufsize = extsize;
1053 fprintf(stderr, "Cannot extract %d bytes %s for inode %d, name %s\n",
1054 extsize, "of extended attributes", curfile.ino, curfile.name);
1059 * Extract the next block of extended attributes.
1062 xtrattr(char *buf, long size)
1065 if (extloc + size > extbufsize)
1066 panic("overrun attribute buffer\n");
1067 memmove(&extbuf[extloc], buf, size);
1072 * Write out the next block of a file.
1075 xtrfile(char *buf, long size)
1080 if (write(ofile, buf, (int) size) == -1) {
1082 "write error extracting inode %d, name %s\nwrite: %s\n",
1083 curfile.ino, curfile.name, strerror(errno));
1088 * Skip over a hole in a file.
1092 xtrskip(char *buf, long size)
1095 if (lseek(ofile, size, SEEK_CUR) == -1) {
1097 "seek error extracting inode %d, name %s\nlseek: %s\n",
1098 curfile.ino, curfile.name, strerror(errno));
1104 * Collect the next block of a symbolic link.
1107 xtrlnkfile(char *buf, long size)
1111 if (pathlen > MAXPATHLEN) {
1112 fprintf(stderr, "symbolic link name: %s->%s%s; too long %d\n",
1113 curfile.name, lnkbuf, buf, pathlen);
1116 (void) strcat(lnkbuf, buf);
1120 * Skip over a hole in a symbolic link (should never happen).
1124 xtrlnkskip(char *buf, long size)
1127 fprintf(stderr, "unallocated block in symbolic link %s\n",
1133 * Collect the next block of a bit map.
1136 xtrmap(char *buf, long size)
1139 memmove(map, buf, size);
1144 * Skip over a hole in a bit map (should never happen).
1148 xtrmapskip(char *buf, long size)
1151 panic("hole in map\n");
1156 * Noop, when an extraction function is not needed.
1160 xtrnull(char *buf, long size)
1167 * Read TP_BSIZE blocks from the input.
1168 * Handle read errors, and end of media.
1173 long rd, newvol, i, oldnumtrec;
1174 int cnt, seek_failed;
1176 if (blkcnt + (byteslide > 0) < numtrec) {
1177 memmove(buf, &tapebuf[(blkcnt++ * TP_BSIZE) + byteslide], (long)TP_BSIZE);
1183 memmove(&tapebuf[-TP_BSIZE],
1184 &tapebuf[(numtrec-1) * TP_BSIZE], (long)TP_BSIZE);
1185 oldnumtrec = numtrec;
1186 for (i = 0; i < ntrec; i++)
1187 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1190 cnt = ntrec * TP_BSIZE;
1195 i = rmtread(&tapebuf[rd], cnt);
1198 i = read(mt, &tapebuf[rd], cnt);
1200 * Check for mid-tape short read error.
1201 * If found, skip rest of buffer and start with the next.
1203 if (!pipein && !pipecmdin && numtrec < ntrec && i > 0) {
1204 dprintf(stdout, "mid-media short read error.\n");
1208 * Handle partial block read.
1210 if ((pipein || pipecmdin) && i == 0 && rd > 0)
1212 else if (i > 0 && i != ntrec * TP_BSIZE) {
1213 if (pipein || pipecmdin) {
1221 * Short read. Process the blocks read.
1223 if (i % TP_BSIZE != 0)
1225 "partial block read: %ld should be %ld\n",
1226 i, ntrec * TP_BSIZE);
1227 numtrec = i / TP_BSIZE;
1231 * Handle read error.
1234 fprintf(stderr, "Tape read error while ");
1235 switch (curfile.action) {
1237 fprintf(stderr, "trying to set up tape\n");
1240 fprintf(stderr, "trying to resynchronize\n");
1243 fprintf(stderr, "restoring %s\n", curfile.name);
1246 fprintf(stderr, "skipping over inode %d\n",
1250 if (!yflag && !reply("continue"))
1252 i = ntrec * TP_BSIZE;
1253 memset(tapebuf, 0, i);
1256 seek_failed = (rmtseek(i, 1) < 0);
1259 seek_failed = (lseek(mt, i, SEEK_CUR) == (off_t)-1);
1263 "continuation failed: %s\n", strerror(errno));
1268 * Handle end of tape.
1271 vprintf(stdout, "End-of-tape encountered\n");
1280 if (rd % TP_BSIZE != 0)
1281 panic("partial block read: %ld should be %ld\n",
1282 rd, ntrec * TP_BSIZE);
1284 memmove(&tapebuf[rd], &endoftapemark, (long)TP_BSIZE);
1286 if (oldnumtrec == 0)
1289 blkcnt -= oldnumtrec;
1291 &tapebuf[(blkcnt++ * TP_BSIZE) + byteslide], (long)TP_BSIZE);
1297 findtapeblksize(void)
1301 for (i = 0; i < ntrec; i++)
1302 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1306 i = rmtread(tapebuf, ntrec * TP_BSIZE);
1309 i = read(mt, tapebuf, ntrec * TP_BSIZE);
1312 fprintf(stderr, "tape read error: %s\n", strerror(errno));
1315 if (i % TP_BSIZE != 0) {
1316 fprintf(stderr, "Tape block size (%ld) %s (%d)\n",
1317 i, "is not a multiple of dump block size", TP_BSIZE);
1320 ntrec = i / TP_BSIZE;
1322 vprintf(stdout, "Tape block size is %ld\n", ntrec);
1344 * Read the next block from the tape.
1345 * If it is not any valid header, return an error.
1348 gethead(struct s_spcl *buf)
1352 readtape((char *)buf);
1353 if (buf->c_magic != FS_UFS2_MAGIC && buf->c_magic != NFS_MAGIC) {
1354 if (buf->c_magic == OFS_MAGIC) {
1356 "Format of dump tape is too old. Must use\n");
1358 "a version of restore from before 2002.\n");
1361 if (swabl(buf->c_magic) != FS_UFS2_MAGIC &&
1362 buf->c_magic != NFS_MAGIC) {
1363 if (buf->c_magic == OFS_MAGIC) {
1365 "Format of dump tape is too old. Must use\n");
1367 "a version of restore from before 2002.\n");
1372 vprintf(stdout, "Note: Doing Byte swapping\n");
1376 if (checksum((int *)buf) == FAIL)
1379 swabst((u_char *)"8l4s1q8l2q17l", (u_char *)buf);
1380 swabst((u_char *)"l",(u_char *) &buf->c_level);
1381 swabst((u_char *)"2l4q",(u_char *) &buf->c_flags);
1385 switch (buf->c_type) {
1390 * Have to patch up missing information in bit map headers
1392 buf->c_size = buf->c_count * TP_BSIZE;
1393 if (buf->c_count > TP_NINDIR)
1396 for (i = 0; i < buf->c_count; i++)
1401 if (buf->c_magic == NFS_MAGIC &&
1402 (buf->c_flags & NFS_DR_NEWINODEFMT) == 0)
1413 * For old dump tapes, have to copy up old fields to
1416 if (buf->c_magic == NFS_MAGIC) {
1417 buf->c_tapea = buf->c_old_tapea;
1418 buf->c_firstrec = buf->c_old_firstrec;
1419 buf->c_date = _time32_to_time(buf->c_old_date);
1420 buf->c_ddate = _time32_to_time(buf->c_old_ddate);
1421 buf->c_atime = _time32_to_time(buf->c_old_atime);
1422 buf->c_mtime = _time32_to_time(buf->c_old_mtime);
1423 buf->c_birthtime = 0;
1424 buf->c_birthtimensec = 0;
1430 panic("gethead: unknown inode type %d\n", buf->c_type);
1433 if (dumpdate != 0 && _time64_to_time(buf->c_date) != dumpdate)
1434 fprintf(stderr, "Header with wrong dumpdate.\n");
1436 * If we're restoring a filesystem with the old (FreeBSD 1)
1437 * format inodes, copy the uid/gid to the new location
1440 buf->c_uid = buf->c_spare1[1];
1441 buf->c_gid = buf->c_spare1[2];
1443 buf->c_magic = FS_UFS2_MAGIC;
1444 tapeaddr = buf->c_tapea;
1451 * Check that a header is where it belongs and predict the next header
1454 accthdr(struct s_spcl *header)
1456 static ino_t previno = 0x7fffffff;
1457 static int prevtype;
1458 static long predict;
1461 if (header->c_type == TS_TAPE) {
1462 fprintf(stderr, "Volume header ");
1463 if (header->c_firstrec)
1464 fprintf(stderr, "begins with record %jd",
1465 (intmax_t)header->c_firstrec);
1466 fprintf(stderr, "\n");
1467 previno = 0x7fffffff;
1470 if (previno == 0x7fffffff)
1474 fprintf(stderr, "Dumped inodes map header");
1477 fprintf(stderr, "Used inodes map header");
1480 fprintf(stderr, "File header, ino %d", previno);
1483 fprintf(stderr, "File continuation header, ino %d", previno);
1486 fprintf(stderr, "End of tape header");
1489 if (predict != blksread - 1)
1490 fprintf(stderr, "; predicted %ld blocks, got %ld blocks",
1491 predict, blksread - 1);
1492 fprintf(stderr, "\n");
1495 if (header->c_type != TS_END)
1496 for (i = 0; i < header->c_count; i++)
1497 if (readmapflag || header->c_addr[i] != 0)
1501 prevtype = header->c_type;
1502 previno = header->c_inumber;
1506 * Find an inode header.
1507 * Complain if had to skip.
1510 findinode(struct s_spcl *header)
1512 static long skipcnt = 0;
1517 curfile.name = "<name unknown>";
1518 curfile.action = UNKNOWN;
1522 htype = header->c_type;
1527 * Skip up to the beginning of the next record
1529 for (i = 0; i < header->c_count; i++)
1530 if (header->c_addr[i])
1532 while (gethead(header) == FAIL ||
1533 _time64_to_time(header->c_date) != dumpdate) {
1537 if (byteslide < TP_BSIZE) {
1547 curfile.mode = header->c_mode;
1548 curfile.uid = header->c_uid;
1549 curfile.gid = header->c_gid;
1550 curfile.file_flags = header->c_file_flags;
1551 curfile.rdev = header->c_rdev;
1552 curfile.atime_sec = header->c_atime;
1553 curfile.atime_nsec = header->c_atimensec;
1554 curfile.mtime_sec = header->c_mtime;
1555 curfile.mtime_nsec = header->c_mtimensec;
1556 curfile.birthtime_sec = header->c_birthtime;
1557 curfile.birthtime_nsec = header->c_birthtimensec;
1558 curfile.extsize = header->c_extsize;
1559 curfile.size = header->c_size;
1560 curfile.ino = header->c_inumber;
1564 /* If we missed some tapes, get another volume. */
1565 if (tapesread & (tapesread + 1)) {
1569 curfile.ino = maxino;
1573 curfile.name = "<file removal list>";
1577 curfile.name = "<file dump list>";
1582 fprintf(stderr, "unexpected tape header\n");
1584 panic("unexpected tape header\n");
1588 fprintf(stderr, "unknown tape header type %d\n",
1591 panic("unknown tape header type %d\n",
1593 while (gethead(header) == FAIL ||
1594 _time64_to_time(header->c_date) != dumpdate) {
1598 if (byteslide < TP_BSIZE) {
1607 } while (htype == TS_ADDR);
1609 fprintf(stderr, "resync restore, skipped %ld %s\n",
1610 skipcnt, Dflag ? "bytes" : "blocks");
1619 j = sizeof(union u_spcl) / sizeof(int);
1626 /* What happens if we want to read restore tapes
1627 for a 16bit int machine??? */
1633 if (i != CHECKSUM) {
1634 fprintf(stderr, "Checksum error %o, inode %d file %s\n", i,
1635 curfile.ino, curfile.name);
1645 msg(const char *fmt, ...)
1649 (void)vfprintf(stderr, fmt, ap);
1652 #endif /* RRESTORE */
1655 swabshort(u_char *sp, int n)
1660 c = sp[0]; sp[0] = sp[1]; sp[1] = c;
1667 swablong(u_char *sp, int n)
1672 c = sp[0]; sp[0] = sp[3]; sp[3] = c;
1673 c = sp[2]; sp[2] = sp[1]; sp[1] = c;
1680 swabquad(u_char *sp, int n)
1685 c = sp[0]; sp[0] = sp[7]; sp[7] = c;
1686 c = sp[1]; sp[1] = sp[6]; sp[6] = c;
1687 c = sp[2]; sp[2] = sp[5]; sp[5] = c;
1688 c = sp[3]; sp[3] = sp[4]; sp[4] = c;
1695 swabst(u_char *cp, u_char *sp)
1701 case '0': case '1': case '2': case '3': case '4':
1702 case '5': case '6': case '7': case '8': case '9':
1703 n = (n * 10) + (*cp++ - '0');
1706 case 's': case 'w': case 'h':
1709 sp = swabshort(sp, n);
1715 sp = swablong(sp, n);
1721 sp = swabquad(sp, n);
1731 fprintf(stderr, "Unknown conversion character: %c\n",
1744 swabst((u_char *)"l", (u_char *)&x);