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 (((TP_BSIZE - 1) & stbuf.st_blksize) != 0) {
264 fprintf(stderr, "Warning: filesystem with non-multiple-of-%d "
265 "blocksize (%d);\n", TP_BSIZE, stbuf.st_blksize);
266 fssize = roundup(fssize, TP_BSIZE);
267 fprintf(stderr, "\twriting using blocksize %ld\n", fssize);
269 if (spcl.c_volume != 1) {
270 fprintf(stderr, "Tape is not volume 1 of the dump\n");
273 if (gethead(&spcl) == FAIL) {
274 dprintf(stdout, "header read failed at %ld blocks\n", blksread);
275 panic("no header after volume mark!\n");
278 if (spcl.c_type != TS_CLRI) {
279 fprintf(stderr, "Cannot find file removal list\n");
282 maxino = (spcl.c_count * TP_BSIZE * NBBY) + 1;
283 dprintf(stdout, "maxino = %ju\n", (uintmax_t)maxino);
284 map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
286 panic("no memory for active inode map\n");
288 curfile.action = USING;
289 getfile(xtrmap, xtrmapskip, xtrmapskip);
290 if (spcl.c_type != TS_BITS) {
291 fprintf(stderr, "Cannot find file dump list\n");
294 map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
295 if (map == (char *)NULL)
296 panic("no memory for file dump list\n");
298 curfile.action = USING;
299 getfile(xtrmap, xtrmapskip, xtrmapskip);
301 * If there may be whiteout entries on the tape, pretend that the
302 * whiteout inode exists, so that the whiteout entries can be
305 SETINO(WINO, dumpmap);
306 /* 'r' restores don't call getvol() for tape 1, so mark it as read. */
312 * Prompt user to load a new dump volume.
313 * "Nextvol" is the next suggested volume to use.
314 * This suggested volume is enforced when doing full
315 * or incremental restores, but can be overridden by
316 * the user when only extracting a subset of the files.
322 long i, newvol, savecnt;
323 union u_spcl tmpspcl;
324 # define tmpbuf tmpspcl.s_spcl
331 prevtapea = tapeaddr;
335 panic("Changing volumes on pipe input?\n");
336 /* Avoid looping if we couldn't ask the user. */
337 if (yflag || ferror(terminal) || feof(terminal))
346 done(1); /* pipes do not get a second chance */
347 if (command == 'R' || command == 'r' || curfile.action != SKIP)
351 while (newvol <= 0) {
352 if (tapesread == 0) {
353 fprintf(stderr, "%s%s%s%s%s%s%s",
354 "You have not read any tapes yet.\n",
355 "If you are extracting just a few files,",
356 " start with the last volume\n",
357 "and work towards the first; restore",
358 " can quickly skip tapes that\n",
359 "have no further files to extract.",
360 " Otherwise, begin with volume 1.\n");
362 fprintf(stderr, "You have read volumes");
364 for (i = 0; i < 32; i++)
365 if (tapesread & (1 << i)) {
366 fprintf(stderr, "%s%ld", buf, i + 1);
369 fprintf(stderr, "\n");
372 fprintf(stderr, "Specify next volume #: ");
373 (void) fflush(stderr);
374 if (fgets(buf, BUFSIZ, terminal) == NULL)
376 } while (buf[0] == '\n');
380 "Volume numbers are positive numerics\n");
383 if (newvol == volno) {
384 tapesread |= 1 << (volno - 1);
388 fprintf(stderr, "Mount tape volume %ld\n", newvol);
389 fprintf(stderr, "Enter ``none'' if there are no more tapes\n");
390 fprintf(stderr, "otherwise enter tape name (default: %s) ", magtape);
391 (void) fflush(stderr);
392 if (fgets(buf, BUFSIZ, terminal) == NULL)
394 if (!strcmp(buf, "none\n")) {
398 if (buf[0] != '\n') {
399 (void) strcpy(magtape, buf);
400 magtape[strlen(magtape) - 1] = '\0';
403 char volno[sizeof("2147483647")];
405 (void)sprintf(volno, "%ld", newvol);
406 if (setenv("RESTORE_VOLUME", volno, 1) == -1) {
407 fprintf(stderr, "Cannot set $RESTORE_VOLUME: %s\n",
411 popenfp = popen(magtape, "r");
412 mt = popenfp ? fileno(popenfp) : -1;
416 mt = rmtopen(magtape, 0);
419 mt = open(magtape, O_RDONLY, 0);
422 fprintf(stderr, "Cannot open %s\n", magtape);
430 if (gethead(&tmpbuf) == FAIL) {
431 dprintf(stdout, "header read failed at %ld blocks\n", blksread);
432 fprintf(stderr, "tape is not dump tape\n");
436 if (tmpbuf.c_volume != volno) {
437 fprintf(stderr, "Wrong volume (%jd)\n",
438 (intmax_t)tmpbuf.c_volume);
442 if (_time64_to_time(tmpbuf.c_date) != dumpdate ||
443 _time64_to_time(tmpbuf.c_ddate) != dumptime) {
444 time_t t = _time64_to_time(tmpbuf.c_date);
445 fprintf(stderr, "Wrong dump date\n\tgot: %s", ctime(&t));
446 fprintf(stderr, "\twanted: %s", ctime(&dumpdate));
450 tapesread |= 1 << (volno - 1);
453 * If continuing from the previous volume, skip over any
454 * blocks read already at the end of the previous volume.
456 * If coming to this volume at random, skip to the beginning
457 * of the next record.
459 dprintf(stdout, "last rec %jd, tape starts with %jd\n",
460 (intmax_t)prevtapea, (intmax_t)tmpbuf.c_tapea);
461 if (tmpbuf.c_type == TS_TAPE) {
462 if (curfile.action != USING) {
464 * XXX Dump incorrectly sets c_count to 1 in the
465 * volume header of the first tape, so ignore
466 * c_count when volno == 1.
469 for (i = tmpbuf.c_count; i > 0; i--)
471 } else if (tmpbuf.c_tapea <= prevtapea) {
473 * Normally the value of c_tapea in the volume
474 * header is the record number of the header itself.
475 * However in the volume header following an EOT-
476 * terminated tape, it is the record number of the
477 * first continuation data block (dump bug?).
479 * The next record we want is `prevtapea + 1'.
481 i = prevtapea + 1 - tmpbuf.c_tapea;
482 dprintf(stderr, "Skipping %ld duplicate record%s.\n",
483 i, i > 1 ? "s" : "");
488 if (curfile.action == USING) {
490 panic("active file into volume 1\n");
493 (void) gethead(&spcl);
502 * Handle unexpected EOF.
508 if (gettingfile && curfile.action == USING) {
509 printf("Warning: %s %s\n",
510 "End-of-input encountered while extracting", curfile.name);
512 curfile.name = "<name unknown>";
513 curfile.action = UNKNOWN;
515 curfile.ino = maxino;
523 * handle multiple dumps per tape by skipping forward to the
531 if (dumpnum == 1 || volno != 1)
534 fprintf(stderr, "Cannot have multiple dumps on pipe input\n");
538 tcom.mt_count = dumpnum - 1;
541 rmtioctl(MTFSF, dumpnum - 1);
544 if (!pipecmdin && ioctl(mt, MTIOCTOP, (char *)&tcom) < 0)
545 fprintf(stderr, "ioctl MTFSF: %s\n", strerror(errno));
552 t = _time64_to_time(spcl.c_date);
553 fprintf(stdout, "Dump date: %s", ctime(&t));
554 t = _time64_to_time(spcl.c_ddate);
555 fprintf(stdout, "Dumped from: %s",
556 (spcl.c_ddate == 0) ? "the epoch\n" : ctime(&t));
557 if (spcl.c_host[0] == '\0')
559 fprintf(stderr, "Level %jd dump of %s on %s:%s\n",
560 (intmax_t)spcl.c_level, spcl.c_filesys, spcl.c_host, spcl.c_dev);
561 fprintf(stderr, "Label: %s\n", spcl.c_label);
565 extractfile(char *name)
572 struct timespec mtimep[2], ctimep[2];
577 curfile.action = USING;
578 mtimep[0].tv_sec = curfile.atime_sec;
579 mtimep[0].tv_nsec = curfile.atime_nsec;
580 mtimep[1].tv_sec = curfile.mtime_sec;
581 mtimep[1].tv_nsec = curfile.mtime_nsec;
582 ctimep[0].tv_sec = curfile.atime_sec;
583 ctimep[0].tv_nsec = curfile.atime_nsec;
584 ctimep[1].tv_sec = curfile.birthtime_sec;
585 ctimep[1].tv_nsec = curfile.birthtime_nsec;
586 extsize = curfile.extsize;
592 flags = curfile.file_flags;
593 switch (mode & IFMT) {
596 fprintf(stderr, "%s: unknown file mode 0%o\n", name, mode);
601 vprintf(stdout, "skipped socket %s\n", name);
607 ep = lookupname(name);
608 if (ep == NULL || ep->e_flags & EXTRACT)
609 panic("unextracted directory %s\n", name);
613 vprintf(stdout, "extract file %s\n", name);
614 return (genliteraldir(name, curfile.ino));
619 buf = setupextattr(extsize);
620 getfile(xtrlnkfile, xtrattr, xtrlnkskip);
623 "%s: zero length symbolic link (ignored)\n", name);
626 if (linkit(lnkbuf, name, SYMLINK) == GOOD) {
628 set_extattr_link(name, buf, extsize);
629 (void) lchown(name, uid, gid);
630 (void) lchmod(name, mode);
631 (void) utimensat(AT_FDCWD, name, ctimep,
632 AT_SYMLINK_NOFOLLOW);
633 (void) utimensat(AT_FDCWD, name, mtimep,
634 AT_SYMLINK_NOFOLLOW);
635 (void) lchflags(name, flags);
641 vprintf(stdout, "extract fifo %s\n", name);
648 if (mkfifo(name, 0600) < 0) {
649 fprintf(stderr, "%s: cannot create fifo: %s\n",
650 name, strerror(errno));
657 buf = setupextattr(extsize);
658 getfile(xtrnull, xtrattr, xtrnull);
659 set_extattr_file(name, buf, extsize);
661 (void) chown(name, uid, gid);
662 (void) chmod(name, mode);
663 (void) utimensat(AT_FDCWD, name, ctimep, 0);
664 (void) utimensat(AT_FDCWD, name, mtimep, 0);
665 (void) chflags(name, flags);
670 vprintf(stdout, "extract special file %s\n", name);
677 if (mknod(name, (mode & (IFCHR | IFBLK)) | 0600,
678 (int)curfile.rdev) < 0) {
679 fprintf(stderr, "%s: cannot create special file: %s\n",
680 name, strerror(errno));
687 buf = setupextattr(extsize);
688 getfile(xtrnull, xtrattr, xtrnull);
689 set_extattr_file(name, buf, extsize);
691 (void) chown(name, uid, gid);
692 (void) chmod(name, mode);
693 (void) utimensat(AT_FDCWD, name, ctimep, 0);
694 (void) utimensat(AT_FDCWD, name, mtimep, 0);
695 (void) chflags(name, flags);
699 vprintf(stdout, "extract file %s\n", name);
706 if ((ofile = open(name, O_WRONLY | O_CREAT | O_TRUNC,
708 fprintf(stderr, "%s: cannot create file: %s\n",
709 name, strerror(errno));
713 buf = setupextattr(extsize);
714 getfile(xtrfile, xtrattr, xtrskip);
716 set_extattr_fd(ofile, name, buf, extsize);
717 (void) fchown(ofile, uid, gid);
718 (void) fchmod(ofile, mode);
719 (void) futimens(ofile, ctimep);
720 (void) futimens(ofile, mtimep);
721 (void) fchflags(ofile, flags);
729 * Set attributes for a file.
732 set_extattr_file(char *name, void *buf, int size)
734 struct extattr *eap, *eaend;
736 vprintf(stdout, "Set attributes for %s:", name);
738 for (eap = buf; eap < eaend; eap = EXTATTR_NEXT(eap)) {
740 * Make sure this entry is complete.
742 if (EXTATTR_NEXT(eap) > eaend || eap->ea_length <= 0) {
743 dprintf(stdout, "\n\t%scorrupted",
744 eap == buf ? "" : "remainder ");
747 if (eap->ea_namespace == EXTATTR_NAMESPACE_EMPTY)
749 vprintf(stdout, "\n\t%s, (%d bytes), %*s",
750 namespace_names[eap->ea_namespace], eap->ea_length,
751 eap->ea_namelength, eap->ea_name);
753 * First we try the general attribute setting interface.
754 * However, some attributes can only be set by root or
755 * by using special interfaces (for example, ACLs).
757 if (extattr_set_file(name, eap->ea_namespace, eap->ea_name,
758 EXTATTR_CONTENT(eap), EXTATTR_CONTENT_SIZE(eap)) != -1) {
759 dprintf(stdout, " (set using extattr_set_file)");
763 * If the general interface refuses to set the attribute,
764 * then we try all the specialized interfaces that we
767 if (eap->ea_namespace == EXTATTR_NAMESPACE_SYSTEM &&
768 !strcmp(eap->ea_name, POSIX1E_ACL_ACCESS_EXTATTR_NAME)) {
769 if (acl_set_file(name, ACL_TYPE_ACCESS,
770 EXTATTR_CONTENT(eap)) != -1) {
771 dprintf(stdout, " (set using acl_set_file)");
775 if (eap->ea_namespace == EXTATTR_NAMESPACE_SYSTEM &&
776 !strcmp(eap->ea_name, POSIX1E_ACL_DEFAULT_EXTATTR_NAME)) {
777 if (acl_set_file(name, ACL_TYPE_DEFAULT,
778 EXTATTR_CONTENT(eap)) != -1) {
779 dprintf(stdout, " (set using acl_set_file)");
783 vprintf(stdout, " (unable to set)");
785 vprintf(stdout, "\n");
789 * Set attributes for a symbolic link.
792 set_extattr_link(char *name, void *buf, int size)
794 struct extattr *eap, *eaend;
796 vprintf(stdout, "Set attributes for %s:", name);
798 for (eap = buf; eap < eaend; eap = EXTATTR_NEXT(eap)) {
800 * Make sure this entry is complete.
802 if (EXTATTR_NEXT(eap) > eaend || eap->ea_length <= 0) {
803 dprintf(stdout, "\n\t%scorrupted",
804 eap == buf ? "" : "remainder ");
807 if (eap->ea_namespace == EXTATTR_NAMESPACE_EMPTY)
809 vprintf(stdout, "\n\t%s, (%d bytes), %*s",
810 namespace_names[eap->ea_namespace], eap->ea_length,
811 eap->ea_namelength, eap->ea_name);
813 * First we try the general attribute setting interface.
814 * However, some attributes can only be set by root or
815 * by using special interfaces (for example, ACLs).
817 if (extattr_set_link(name, eap->ea_namespace, eap->ea_name,
818 EXTATTR_CONTENT(eap), EXTATTR_CONTENT_SIZE(eap)) != -1) {
819 dprintf(stdout, " (set using extattr_set_link)");
823 * If the general interface refuses to set the attribute,
824 * then we try all the specialized interfaces that we
827 if (eap->ea_namespace == EXTATTR_NAMESPACE_SYSTEM &&
828 !strcmp(eap->ea_name, POSIX1E_ACL_ACCESS_EXTATTR_NAME)) {
829 if (acl_set_link_np(name, ACL_TYPE_ACCESS,
830 EXTATTR_CONTENT(eap)) != -1) {
831 dprintf(stdout, " (set using acl_set_link_np)");
835 if (eap->ea_namespace == EXTATTR_NAMESPACE_SYSTEM &&
836 !strcmp(eap->ea_name, POSIX1E_ACL_DEFAULT_EXTATTR_NAME)) {
837 if (acl_set_link_np(name, ACL_TYPE_DEFAULT,
838 EXTATTR_CONTENT(eap)) != -1) {
839 dprintf(stdout, " (set using acl_set_link_np)");
843 vprintf(stdout, " (unable to set)");
845 vprintf(stdout, "\n");
849 * Set attributes on a file descriptor.
852 set_extattr_fd(int fd, char *name, void *buf, int size)
854 struct extattr *eap, *eaend;
856 vprintf(stdout, "Set attributes for %s:", name);
858 for (eap = buf; eap < eaend; eap = EXTATTR_NEXT(eap)) {
860 * Make sure this entry is complete.
862 if (EXTATTR_NEXT(eap) > eaend || eap->ea_length <= 0) {
863 dprintf(stdout, "\n\t%scorrupted",
864 eap == buf ? "" : "remainder ");
867 if (eap->ea_namespace == EXTATTR_NAMESPACE_EMPTY)
869 vprintf(stdout, "\n\t%s, (%d bytes), %*s",
870 namespace_names[eap->ea_namespace], eap->ea_length,
871 eap->ea_namelength, eap->ea_name);
873 * First we try the general attribute setting interface.
874 * However, some attributes can only be set by root or
875 * by using special interfaces (for example, ACLs).
877 if (extattr_set_fd(fd, eap->ea_namespace, eap->ea_name,
878 EXTATTR_CONTENT(eap), EXTATTR_CONTENT_SIZE(eap)) != -1) {
879 dprintf(stdout, " (set using extattr_set_fd)");
883 * If the general interface refuses to set the attribute,
884 * then we try all the specialized interfaces that we
887 if (eap->ea_namespace == EXTATTR_NAMESPACE_SYSTEM &&
888 !strcmp(eap->ea_name, POSIX1E_ACL_ACCESS_EXTATTR_NAME)) {
889 if (acl_set_fd(fd, EXTATTR_CONTENT(eap)) != -1) {
890 dprintf(stdout, " (set using acl_set_fd)");
894 if (eap->ea_namespace == EXTATTR_NAMESPACE_SYSTEM &&
895 !strcmp(eap->ea_name, POSIX1E_ACL_DEFAULT_EXTATTR_NAME)) {
896 if (acl_set_file(name, ACL_TYPE_DEFAULT,
897 EXTATTR_CONTENT(eap)) != -1) {
898 dprintf(stdout, " (set using acl_set_file)");
902 vprintf(stdout, " (unable to set)");
904 vprintf(stdout, "\n");
908 * skip over bit maps on the tape
914 while (spcl.c_type == TS_BITS || spcl.c_type == TS_CLRI)
919 * skip over a file on the tape
925 curfile.action = SKIP;
926 getfile(xtrnull, xtrnull, xtrnull);
930 * Extract a file from the tape.
931 * When an allocated block is found it is passed to the fill function;
932 * when an unallocated block (hole) is found, a zeroed buffer is passed
933 * to the skip function.
936 getfile(void (*datafill)(char *, long), void (*attrfill)(char *, long),
937 void (*skip)(char *, long))
941 int curblk, attrsize;
942 void (*fillit)(char *, long);
943 static char clearedbuf[MAXBSIZE];
944 char buf[MAXBSIZE / TP_BSIZE][TP_BSIZE];
949 attrsize = spcl.c_extsize;
950 if (spcl.c_type == TS_END)
951 panic("ran off end of tape\n");
952 if (spcl.c_magic != FS_UFS2_MAGIC)
953 panic("not at beginning of a file\n");
954 if (!gettingfile && setjmp(restart) != 0)
958 if (size == 0 && attrsize > 0) {
964 for (i = 0; i < spcl.c_count; i++) {
965 if (!readmapflag && i > TP_NINDIR) {
967 fprintf(stderr, "spcl.c_count = %jd\n",
968 (intmax_t)spcl.c_count);
971 panic("spcl.c_count = %jd\n",
972 (intmax_t)spcl.c_count);
974 if (readmapflag || spcl.c_addr[i]) {
975 readtape(&buf[curblk++][0]);
976 if (curblk == fssize / TP_BSIZE) {
977 (*fillit)((char *)buf, (long)(size > TP_BSIZE ?
978 fssize : (curblk - 1) * TP_BSIZE + size));
983 (*fillit)((char *)buf, (long)(size > TP_BSIZE ?
985 (curblk - 1) * TP_BSIZE + size));
988 (*skip)(clearedbuf, (long)(size > TP_BSIZE ?
991 if ((size -= TP_BSIZE) <= 0) {
992 if (size > -TP_BSIZE && curblk > 0) {
993 (*fillit)((char *)buf,
994 (long)((curblk * TP_BSIZE) + size));
1003 if (spcl.c_count - i > 1)
1004 dprintf(stdout, "skipping %d junk block(s)\n",
1005 spcl.c_count - i - 1);
1006 for (i++; i < spcl.c_count; i++) {
1007 if (!readmapflag && i > TP_NINDIR) {
1010 "spcl.c_count = %jd\n",
1011 (intmax_t)spcl.c_count);
1014 panic("spcl.c_count = %jd\n",
1015 (intmax_t)spcl.c_count);
1017 if (readmapflag || spcl.c_addr[i])
1023 if (gethead(&spcl) == GOOD && size > 0) {
1024 if (spcl.c_type == TS_ADDR)
1027 "Missing address (header) block for %s at %ld blocks\n",
1028 curfile.name, blksread);
1031 panic("getfile: lost data\n");
1037 * These variables are shared between the next two functions.
1039 static int extbufsize = 0;
1040 static char *extbuf;
1044 * Allocate a buffer into which to extract extended attributes.
1047 setupextattr(int extsize)
1051 if (extsize <= extbufsize)
1055 if ((extbuf = malloc(extsize)) != NULL) {
1056 extbufsize = extsize;
1061 fprintf(stderr, "Cannot extract %d bytes %s for inode %ju, name %s\n",
1062 extsize, "of extended attributes", (uintmax_t)curfile.ino,
1068 * Extract the next block of extended attributes.
1071 xtrattr(char *buf, long size)
1074 if (extloc + size > extbufsize)
1075 panic("overrun attribute buffer\n");
1076 memmove(&extbuf[extloc], buf, size);
1081 * Write out the next block of a file.
1084 xtrfile(char *buf, long size)
1089 if (write(ofile, buf, (int) size) == -1) {
1091 "write error extracting inode %ju, name %s\nwrite: %s\n",
1092 (uintmax_t)curfile.ino, curfile.name, strerror(errno));
1097 * Skip over a hole in a file.
1101 xtrskip(char *buf, long size)
1104 if (lseek(ofile, size, SEEK_CUR) == -1) {
1106 "seek error extracting inode %ju, name %s\nlseek: %s\n",
1107 (uintmax_t)curfile.ino, curfile.name, strerror(errno));
1113 * Collect the next block of a symbolic link.
1116 xtrlnkfile(char *buf, long size)
1120 if (pathlen > MAXPATHLEN) {
1121 fprintf(stderr, "symbolic link name: %s->%s%s; too long %d\n",
1122 curfile.name, lnkbuf, buf, pathlen);
1125 (void) strcat(lnkbuf, buf);
1129 * Skip over a hole in a symbolic link (should never happen).
1133 xtrlnkskip(char *buf, long size)
1136 fprintf(stderr, "unallocated block in symbolic link %s\n",
1142 * Collect the next block of a bit map.
1145 xtrmap(char *buf, long size)
1148 memmove(map, buf, size);
1153 * Skip over a hole in a bit map (should never happen).
1157 xtrmapskip(char *buf, long size)
1160 panic("hole in map\n");
1165 * Noop, when an extraction function is not needed.
1169 xtrnull(char *buf, long size)
1176 * Read TP_BSIZE blocks from the input.
1177 * Handle read errors, and end of media.
1182 long rd, newvol, i, oldnumtrec;
1183 int cnt, seek_failed;
1185 if (blkcnt + (byteslide > 0) < numtrec) {
1186 memmove(buf, &tapebuf[(blkcnt++ * TP_BSIZE) + byteslide], (long)TP_BSIZE);
1192 memmove(&tapebuf[-TP_BSIZE],
1193 &tapebuf[(numtrec-1) * TP_BSIZE], (long)TP_BSIZE);
1194 oldnumtrec = numtrec;
1195 for (i = 0; i < ntrec; i++)
1196 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1199 cnt = ntrec * TP_BSIZE;
1204 i = rmtread(&tapebuf[rd], cnt);
1207 i = read(mt, &tapebuf[rd], cnt);
1209 * Check for mid-tape short read error.
1210 * If found, skip rest of buffer and start with the next.
1212 if (!pipein && !pipecmdin && numtrec < ntrec && i > 0) {
1213 dprintf(stdout, "mid-media short read error.\n");
1217 * Handle partial block read.
1219 if ((pipein || pipecmdin) && i == 0 && rd > 0)
1221 else if (i > 0 && i != ntrec * TP_BSIZE) {
1222 if (pipein || pipecmdin) {
1230 * Short read. Process the blocks read.
1232 if (i % TP_BSIZE != 0)
1234 "partial block read: %ld should be %ld\n",
1235 i, ntrec * TP_BSIZE);
1236 numtrec = i / TP_BSIZE;
1240 * Handle read error.
1243 fprintf(stderr, "Tape read error while ");
1244 switch (curfile.action) {
1246 fprintf(stderr, "trying to set up tape\n");
1249 fprintf(stderr, "trying to resynchronize\n");
1252 fprintf(stderr, "restoring %s\n", curfile.name);
1255 fprintf(stderr, "skipping over inode %ju\n",
1256 (uintmax_t)curfile.ino);
1259 if (!yflag && !reply("continue"))
1261 i = ntrec * TP_BSIZE;
1262 memset(tapebuf, 0, i);
1265 seek_failed = (rmtseek(i, 1) < 0);
1268 seek_failed = (lseek(mt, i, SEEK_CUR) == (off_t)-1);
1272 "continuation failed: %s\n", strerror(errno));
1277 * Handle end of tape.
1280 vprintf(stdout, "End-of-tape encountered\n");
1289 if (rd % TP_BSIZE != 0)
1290 panic("partial block read: %ld should be %ld\n",
1291 rd, ntrec * TP_BSIZE);
1293 memmove(&tapebuf[rd], &endoftapemark, (long)TP_BSIZE);
1295 if (oldnumtrec == 0)
1298 blkcnt -= oldnumtrec;
1300 &tapebuf[(blkcnt++ * TP_BSIZE) + byteslide], (long)TP_BSIZE);
1306 findtapeblksize(void)
1310 for (i = 0; i < ntrec; i++)
1311 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1315 i = rmtread(tapebuf, ntrec * TP_BSIZE);
1318 i = read(mt, tapebuf, ntrec * TP_BSIZE);
1321 fprintf(stderr, "tape read error: %s\n", strerror(errno));
1324 if (i % TP_BSIZE != 0) {
1325 fprintf(stderr, "Tape block size (%ld) %s (%d)\n",
1326 i, "is not a multiple of dump block size", TP_BSIZE);
1329 ntrec = i / TP_BSIZE;
1331 vprintf(stdout, "Tape block size is %ld\n", ntrec);
1353 * Read the next block from the tape.
1354 * If it is not any valid header, return an error.
1357 gethead(struct s_spcl *buf)
1361 readtape((char *)buf);
1362 if (buf->c_magic != FS_UFS2_MAGIC && 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");
1370 if (swabl(buf->c_magic) != FS_UFS2_MAGIC &&
1371 buf->c_magic != NFS_MAGIC) {
1372 if (buf->c_magic == OFS_MAGIC) {
1374 "Format of dump tape is too old. Must use\n");
1376 "a version of restore from before 2002.\n");
1381 vprintf(stdout, "Note: Doing Byte swapping\n");
1385 if (checksum((int *)buf) == FAIL)
1388 swabst((u_char *)"8l4s1q8l2q17l", (u_char *)buf);
1389 swabst((u_char *)"l",(u_char *) &buf->c_level);
1390 swabst((u_char *)"2l4q",(u_char *) &buf->c_flags);
1394 switch (buf->c_type) {
1399 * Have to patch up missing information in bit map headers
1401 buf->c_size = buf->c_count * TP_BSIZE;
1402 if (buf->c_count > TP_NINDIR)
1405 for (i = 0; i < buf->c_count; i++)
1410 if (buf->c_magic == NFS_MAGIC &&
1411 (buf->c_flags & NFS_DR_NEWINODEFMT) == 0)
1422 * For old dump tapes, have to copy up old fields to
1425 if (buf->c_magic == NFS_MAGIC) {
1426 buf->c_tapea = buf->c_old_tapea;
1427 buf->c_firstrec = buf->c_old_firstrec;
1428 buf->c_date = _time32_to_time(buf->c_old_date);
1429 buf->c_ddate = _time32_to_time(buf->c_old_ddate);
1430 buf->c_atime = _time32_to_time(buf->c_old_atime);
1431 buf->c_mtime = _time32_to_time(buf->c_old_mtime);
1432 buf->c_birthtime = 0;
1433 buf->c_birthtimensec = 0;
1439 panic("gethead: unknown inode type %d\n", buf->c_type);
1442 if (dumpdate != 0 && _time64_to_time(buf->c_date) != dumpdate)
1443 fprintf(stderr, "Header with wrong dumpdate.\n");
1445 * If we're restoring a filesystem with the old (FreeBSD 1)
1446 * format inodes, copy the uid/gid to the new location
1449 buf->c_uid = buf->c_spare1[1];
1450 buf->c_gid = buf->c_spare1[2];
1452 buf->c_magic = FS_UFS2_MAGIC;
1453 tapeaddr = buf->c_tapea;
1460 * Check that a header is where it belongs and predict the next header
1463 accthdr(struct s_spcl *header)
1465 static ino_t previno = 0x7fffffff;
1466 static int prevtype;
1467 static long predict;
1470 if (header->c_type == TS_TAPE) {
1471 fprintf(stderr, "Volume header ");
1472 if (header->c_firstrec)
1473 fprintf(stderr, "begins with record %jd",
1474 (intmax_t)header->c_firstrec);
1475 fprintf(stderr, "\n");
1476 previno = 0x7fffffff;
1479 if (previno == 0x7fffffff)
1483 fprintf(stderr, "Dumped inodes map header");
1486 fprintf(stderr, "Used inodes map header");
1489 fprintf(stderr, "File header, ino %ju", (uintmax_t)previno);
1492 fprintf(stderr, "File continuation header, ino %ju",
1493 (uintmax_t)previno);
1496 fprintf(stderr, "End of tape header");
1499 if (predict != blksread - 1)
1500 fprintf(stderr, "; predicted %ld blocks, got %ld blocks",
1501 predict, blksread - 1);
1502 fprintf(stderr, "\n");
1505 if (header->c_type != TS_END)
1506 for (i = 0; i < header->c_count; i++)
1507 if (readmapflag || header->c_addr[i] != 0)
1511 prevtype = header->c_type;
1512 previno = header->c_inumber;
1516 * Find an inode header.
1517 * Complain if had to skip.
1520 findinode(struct s_spcl *header)
1522 static long skipcnt = 0;
1527 curfile.name = "<name unknown>";
1528 curfile.action = UNKNOWN;
1532 htype = header->c_type;
1537 * Skip up to the beginning of the next record
1539 for (i = 0; i < header->c_count; i++)
1540 if (header->c_addr[i])
1542 while (gethead(header) == FAIL ||
1543 _time64_to_time(header->c_date) != dumpdate) {
1547 if (byteslide < TP_BSIZE) {
1557 curfile.mode = header->c_mode;
1558 curfile.uid = header->c_uid;
1559 curfile.gid = header->c_gid;
1560 curfile.file_flags = header->c_file_flags;
1561 curfile.rdev = header->c_rdev;
1562 curfile.atime_sec = header->c_atime;
1563 curfile.atime_nsec = header->c_atimensec;
1564 curfile.mtime_sec = header->c_mtime;
1565 curfile.mtime_nsec = header->c_mtimensec;
1566 curfile.birthtime_sec = header->c_birthtime;
1567 curfile.birthtime_nsec = header->c_birthtimensec;
1568 curfile.extsize = header->c_extsize;
1569 curfile.size = header->c_size;
1570 curfile.ino = header->c_inumber;
1574 /* If we missed some tapes, get another volume. */
1575 if (tapesread & (tapesread + 1)) {
1579 curfile.ino = maxino;
1583 curfile.name = "<file removal list>";
1587 curfile.name = "<file dump list>";
1592 fprintf(stderr, "unexpected tape header\n");
1594 panic("unexpected tape header\n");
1598 fprintf(stderr, "unknown tape header type %d\n",
1601 panic("unknown tape header type %d\n",
1603 while (gethead(header) == FAIL ||
1604 _time64_to_time(header->c_date) != dumpdate) {
1608 if (byteslide < TP_BSIZE) {
1617 } while (htype == TS_ADDR);
1619 fprintf(stderr, "resync restore, skipped %ld %s\n",
1620 skipcnt, Dflag ? "bytes" : "blocks");
1629 j = sizeof(union u_spcl) / sizeof(int);
1636 /* What happens if we want to read restore tapes
1637 for a 16bit int machine??? */
1643 if (i != CHECKSUM) {
1644 fprintf(stderr, "Checksum error %o, inode %ju file %s\n", i,
1645 (uintmax_t)curfile.ino, curfile.name);
1655 msg(const char *fmt, ...)
1659 (void)vfprintf(stderr, fmt, ap);
1662 #endif /* RRESTORE */
1665 swabshort(u_char *sp, int n)
1670 c = sp[0]; sp[0] = sp[1]; sp[1] = c;
1677 swablong(u_char *sp, int n)
1682 c = sp[0]; sp[0] = sp[3]; sp[3] = c;
1683 c = sp[2]; sp[2] = sp[1]; sp[1] = c;
1690 swabquad(u_char *sp, int n)
1695 c = sp[0]; sp[0] = sp[7]; sp[7] = c;
1696 c = sp[1]; sp[1] = sp[6]; sp[6] = c;
1697 c = sp[2]; sp[2] = sp[5]; sp[5] = c;
1698 c = sp[3]; sp[3] = sp[4]; sp[4] = c;
1705 swabst(u_char *cp, u_char *sp)
1711 case '0': case '1': case '2': case '3': case '4':
1712 case '5': case '6': case '7': case '8': case '9':
1713 n = (n * 10) + (*cp++ - '0');
1716 case 's': case 'w': case 'h':
1719 sp = swabshort(sp, n);
1725 sp = swablong(sp, n);
1731 sp = swabquad(sp, n);
1741 fprintf(stderr, "Unknown conversion character: %c\n",
1754 swabst((u_char *)"l", (u_char *)&x);