2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include <sys/param.h>
42 #include <sys/extattr.h>
45 #include <ufs/ufs/extattr.h>
46 #include <ufs/ufs/dinode.h>
47 #include <protocols/dumprestore.h>
64 static long fssize = MAXBSIZE;
66 static int pipein = 0;
67 static int pipecmdin = 0;
68 static FILE *popenfp = NULL;
73 static union u_spcl endoftapemark;
74 static long byteslide = 0;
75 static long blksread; /* blocks read since last header */
76 static int64_t tapeaddr = 0; /* current TP_BSIZE tape record */
77 static long tapesread;
78 static jmp_buf restart;
79 static int gettingfile = 0; /* restart has a valid frame */
80 static char *host = NULL;
81 static int readmapflag;
85 static char lnkbuf[MAXPATHLEN + 1];
88 struct context curfile; /* describes next file available on the tape */
89 union u_spcl u_spcl; /* mapping of variables in a control block */
90 int Bcvt; /* Swap Bytes */
91 int oldinofmt; /* FreeBSD 1 inode format needs cvt */
93 #define FLUSHTAPEBUF() blkcnt = ntrec + 1
95 char *namespace_names[] = EXTATTR_NAMESPACE_NAMES;
97 static void accthdr(struct s_spcl *);
98 static int checksum(int *);
99 static void findinode(struct s_spcl *);
100 static void findtapeblksize(void);
101 static char *setupextattr(int);
102 static void xtrattr(char *, size_t);
103 static void skiphole(void (*)(char *, size_t), size_t *);
104 static int gethead(struct s_spcl *);
105 static void readtape(char *);
106 static void setdumpnum(void);
107 static u_long swabl(u_long);
108 static u_char *swablong(u_char *, int);
109 static u_char *swabshort(u_char *, int);
110 static void terminateinput(void);
111 static void xtrfile(char *, size_t);
112 static void xtrlnkfile(char *, size_t);
113 static void xtrlnkskip(char *, size_t);
114 static void xtrmap(char *, size_t);
115 static void xtrmapskip(char *, size_t);
116 static void xtrskip(char *, size_t);
119 * Set up an input source
122 setinput(char *source, int ispipecommand)
128 newtapebuf(MAX(NTREC, HIGHDENSITYTREC));
135 if (strchr(source, ':')) {
137 source = strchr(host, ':');
139 if (rmthost(host) == 0)
143 if (strcmp(source, "-") == 0) {
145 * Since input is coming from a pipe we must establish
146 * our own connection to the terminal.
148 terminal = fopen(_PATH_TTY, "r");
149 if (terminal == NULL) {
150 (void)fprintf(stderr, "cannot open %s: %s\n",
151 _PATH_TTY, strerror(errno));
152 terminal = fopen(_PATH_DEVNULL, "r");
153 if (terminal == NULL) {
154 (void)fprintf(stderr, "cannot open %s: %s\n",
155 _PATH_DEVNULL, strerror(errno));
161 /* no longer need or want root privileges */
162 if (setuid(getuid()) != 0) {
163 fprintf(stderr, "setuid failed\n");
166 magtape = strdup(source);
167 if (magtape == NULL) {
168 fprintf(stderr, "Cannot allocate space for magtape buffer\n");
174 newtapebuf(long size)
176 static int tapebufsize = -1;
179 if (size <= tapebufsize)
182 free(tapebuf - TP_BSIZE);
183 tapebuf = malloc((size+1) * TP_BSIZE);
184 if (tapebuf == NULL) {
185 fprintf(stderr, "Cannot allocate space for tape buffer\n");
193 * Verify that the tape drive can be accessed and
194 * that it actually is a dump tape.
202 vprintf(stdout, "Verify tape and initialize maps\n");
204 if (setenv("RESTORE_VOLUME", "1", 1) == -1) {
205 fprintf(stderr, "Cannot set $RESTORE_VOLUME: %s\n",
209 popenfp = popen(magtape, "r");
210 mt = popenfp ? fileno(popenfp) : -1;
214 mt = rmtopen(magtape, 0);
220 mt = open(magtape, O_RDONLY, 0);
222 fprintf(stderr, "%s: %s\n", magtape, strerror(errno));
228 if (!pipein && !pipecmdin && !bflag)
230 if (gethead(&spcl) == FAIL) {
231 fprintf(stderr, "Tape is not a dump tape\n");
235 endoftapemark.s_spcl.c_magic = FS_UFS2_MAGIC;
236 endoftapemark.s_spcl.c_type = TS_END;
237 ip = (int *)&endoftapemark;
238 j = sizeof(union u_spcl) / sizeof(int);
243 endoftapemark.s_spcl.c_checksum = CHECKSUM - i;
245 if (vflag || command == 't')
247 dumptime = _time64_to_time(spcl.c_ddate);
248 dumpdate = _time64_to_time(spcl.c_date);
249 if (stat(".", &stbuf) < 0) {
250 fprintf(stderr, "cannot stat .: %s\n", strerror(errno));
253 if (stbuf.st_blksize > 0 && stbuf.st_blksize < TP_BSIZE )
255 if (stbuf.st_blksize >= TP_BSIZE && stbuf.st_blksize <= MAXBSIZE)
256 fssize = stbuf.st_blksize;
257 if (((TP_BSIZE - 1) & stbuf.st_blksize) != 0) {
258 fprintf(stderr, "Warning: filesystem with non-multiple-of-%d "
259 "blocksize (%d);\n", TP_BSIZE, stbuf.st_blksize);
260 fssize = roundup(fssize, TP_BSIZE);
261 fprintf(stderr, "\twriting using blocksize %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 = %ju\n", (uintmax_t)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(UFS_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))
341 done(1); /* pipes do not get a second chance */
342 if (command == 'R' || command == 'r' || curfile.action != SKIP)
346 while (newvol <= 0) {
347 if (tapesread == 0) {
348 fprintf(stderr, "%s%s%s%s%s%s%s",
349 "You have not read any tapes yet.\n",
350 "If you are extracting just a few files,",
351 " start with the last volume\n",
352 "and work towards the first; restore",
353 " can quickly skip tapes that\n",
354 "have no further files to extract.",
355 " Otherwise, begin with volume 1.\n");
357 fprintf(stderr, "You have read volumes");
359 for (i = 0; i < 32; i++)
360 if (tapesread & (1 << i)) {
361 fprintf(stderr, "%s%ld", buf, i + 1);
364 fprintf(stderr, "\n");
367 fprintf(stderr, "Specify next volume #: ");
368 (void) fflush(stderr);
369 if (fgets(buf, BUFSIZ, terminal) == NULL)
371 } while (buf[0] == '\n');
375 "Volume numbers are positive numerics\n");
378 if (newvol == volno) {
379 tapesread |= 1 << (volno - 1);
383 fprintf(stderr, "Mount tape volume %ld\n", newvol);
384 fprintf(stderr, "Enter ``none'' if there are no more tapes\n");
385 fprintf(stderr, "otherwise enter tape name (default: %s) ", magtape);
386 (void) fflush(stderr);
387 if (fgets(buf, BUFSIZ, terminal) == NULL)
389 if (!strcmp(buf, "none\n")) {
393 if (buf[0] != '\n') {
394 (void) strcpy(magtape, buf);
395 magtape[strlen(magtape) - 1] = '\0';
398 char volno[sizeof("2147483647")];
400 (void)sprintf(volno, "%ld", newvol);
401 if (setenv("RESTORE_VOLUME", volno, 1) == -1) {
402 fprintf(stderr, "Cannot set $RESTORE_VOLUME: %s\n",
406 popenfp = popen(magtape, "r");
407 mt = popenfp ? fileno(popenfp) : -1;
411 mt = rmtopen(magtape, 0);
414 mt = open(magtape, O_RDONLY, 0);
417 fprintf(stderr, "Cannot open %s\n", magtape);
425 if (gethead(&tmpbuf) == FAIL) {
426 dprintf(stdout, "header read failed at %ld blocks\n", blksread);
427 fprintf(stderr, "tape is not dump tape\n");
431 if (tmpbuf.c_volume != volno) {
432 fprintf(stderr, "Wrong volume (%jd)\n",
433 (intmax_t)tmpbuf.c_volume);
437 if (_time64_to_time(tmpbuf.c_date) != dumpdate ||
438 _time64_to_time(tmpbuf.c_ddate) != dumptime) {
439 time_t t = _time64_to_time(tmpbuf.c_date);
440 fprintf(stderr, "Wrong dump date\n\tgot: %s", ctime(&t));
441 fprintf(stderr, "\twanted: %s", ctime(&dumpdate));
445 tapesread |= 1 << (volno - 1);
448 * If continuing from the previous volume, skip over any
449 * blocks read already at the end of the previous volume.
451 * If coming to this volume at random, skip to the beginning
452 * of the next record.
454 dprintf(stdout, "last rec %jd, tape starts with %jd\n",
455 (intmax_t)prevtapea, (intmax_t)tmpbuf.c_tapea);
456 if (tmpbuf.c_type == TS_TAPE) {
457 if (curfile.action != USING) {
459 * XXX Dump incorrectly sets c_count to 1 in the
460 * volume header of the first tape, so ignore
461 * c_count when volno == 1.
464 for (i = tmpbuf.c_count; i > 0; i--)
466 } else if (tmpbuf.c_tapea <= prevtapea) {
468 * Normally the value of c_tapea in the volume
469 * header is the record number of the header itself.
470 * However in the volume header following an EOT-
471 * terminated tape, it is the record number of the
472 * first continuation data block (dump bug?).
474 * The next record we want is `prevtapea + 1'.
476 i = prevtapea + 1 - tmpbuf.c_tapea;
477 dprintf(stderr, "Skipping %ld duplicate record%s.\n",
478 i, i > 1 ? "s" : "");
483 if (curfile.action == USING) {
485 panic("active file into volume 1\n");
488 (void) gethead(&spcl);
497 * Handle unexpected EOF.
503 if (gettingfile && curfile.action == USING) {
504 printf("Warning: %s %s\n",
505 "End-of-input encountered while extracting", curfile.name);
507 curfile.name = "<name unknown>";
508 curfile.action = UNKNOWN;
510 curfile.ino = maxino;
518 * handle multiple dumps per tape by skipping forward to the
526 if (dumpnum == 1 || volno != 1)
529 fprintf(stderr, "Cannot have multiple dumps on pipe input\n");
533 tcom.mt_count = dumpnum - 1;
536 rmtioctl(MTFSF, dumpnum - 1);
539 if (!pipecmdin && ioctl(mt, MTIOCTOP, (char *)&tcom) < 0)
540 fprintf(stderr, "ioctl MTFSF: %s\n", strerror(errno));
547 t = _time64_to_time(spcl.c_date);
548 fprintf(stdout, "Dump date: %s", ctime(&t));
549 t = _time64_to_time(spcl.c_ddate);
550 fprintf(stdout, "Dumped from: %s",
551 (spcl.c_ddate == 0) ? "the epoch\n" : ctime(&t));
552 if (spcl.c_host[0] == '\0')
554 fprintf(stderr, "Level %jd dump of %s on %s:%s\n",
555 (intmax_t)spcl.c_level, spcl.c_filesys, spcl.c_host, spcl.c_dev);
556 fprintf(stderr, "Label: %s\n", spcl.c_label);
560 extractfile(char *name)
567 struct timespec mtimep[2], ctimep[2];
572 curfile.action = USING;
573 mtimep[0].tv_sec = curfile.atime_sec;
574 mtimep[0].tv_nsec = curfile.atime_nsec;
575 mtimep[1].tv_sec = curfile.mtime_sec;
576 mtimep[1].tv_nsec = curfile.mtime_nsec;
577 ctimep[0].tv_sec = curfile.atime_sec;
578 ctimep[0].tv_nsec = curfile.atime_nsec;
579 ctimep[1].tv_sec = curfile.birthtime_sec;
580 ctimep[1].tv_nsec = curfile.birthtime_nsec;
581 extsize = curfile.extsize;
587 flags = curfile.file_flags;
588 switch (mode & IFMT) {
591 fprintf(stderr, "%s: unknown file mode 0%o\n", name, mode);
596 vprintf(stdout, "skipped socket %s\n", name);
602 ep = lookupname(name);
603 if (ep == NULL || ep->e_flags & EXTRACT)
604 panic("unextracted directory %s\n", name);
608 vprintf(stdout, "extract file %s\n", name);
609 return (genliteraldir(name, curfile.ino));
614 buf = setupextattr(extsize);
615 getfile(xtrlnkfile, xtrattr, xtrlnkskip);
618 "%s: zero length symbolic link (ignored)\n", name);
621 if (linkit(lnkbuf, name, SYMLINK) == GOOD) {
622 (void) lchown(name, uid, gid);
623 (void) lchmod(name, mode);
625 set_extattr(-1, name, buf, extsize, SXA_LINK);
626 (void) utimensat(AT_FDCWD, name, ctimep,
627 AT_SYMLINK_NOFOLLOW);
628 (void) utimensat(AT_FDCWD, name, mtimep,
629 AT_SYMLINK_NOFOLLOW);
630 (void) lchflags(name, flags);
636 vprintf(stdout, "extract fifo %s\n", name);
643 if (mkfifo(name, 0600) < 0) {
644 fprintf(stderr, "%s: cannot create fifo: %s\n",
645 name, strerror(errno));
649 (void) chown(name, uid, gid);
650 (void) chmod(name, mode);
654 buf = setupextattr(extsize);
655 getfile(xtrnull, xtrattr, xtrnull);
656 set_extattr(-1, name, buf, extsize, SXA_FILE);
658 (void) utimensat(AT_FDCWD, name, ctimep, 0);
659 (void) utimensat(AT_FDCWD, name, mtimep, 0);
660 (void) chflags(name, flags);
665 vprintf(stdout, "extract special file %s\n", name);
672 if (mknod(name, (mode & (IFCHR | IFBLK)) | 0600,
673 (int)curfile.rdev) < 0) {
674 fprintf(stderr, "%s: cannot create special file: %s\n",
675 name, strerror(errno));
679 (void) chown(name, uid, gid);
680 (void) chmod(name, mode);
684 buf = setupextattr(extsize);
685 getfile(xtrnull, xtrattr, xtrnull);
686 set_extattr(-1, name, buf, extsize, SXA_FILE);
688 (void) utimensat(AT_FDCWD, name, ctimep, 0);
689 (void) utimensat(AT_FDCWD, name, mtimep, 0);
690 (void) chflags(name, flags);
694 vprintf(stdout, "extract file %s\n", name);
701 if ((ofile = open(name, O_WRONLY | O_CREAT | O_TRUNC,
703 fprintf(stderr, "%s: cannot create file: %s\n",
704 name, strerror(errno));
708 (void) fchown(ofile, uid, gid);
709 (void) fchmod(ofile, mode);
710 buf = setupextattr(extsize);
711 getfile(xtrfile, xtrattr, xtrskip);
713 set_extattr(ofile, name, buf, extsize, SXA_FD);
714 (void) futimens(ofile, ctimep);
715 (void) futimens(ofile, mtimep);
716 (void) fchflags(ofile, flags);
724 * Set attributes on a file descriptor, link, or file.
727 set_extattr(int fd, char *name, void *buf, int size, enum set_extattr_mode mode)
729 struct extattr *eap, *eaend;
733 char eaname[EXTATTR_MAXNAMELEN + 1];
735 vprintf(stdout, "Set attributes for %s:", name);
737 for (eap = buf; eap < eaend; eap = EXTATTR_NEXT(eap)) {
739 * Make sure this entry is complete.
741 if (EXTATTR_NEXT(eap) > eaend || eap->ea_length <= 0) {
742 dprintf(stdout, "\n\t%scorrupted",
743 eap == buf ? "" : "remainder ");
746 if (eap->ea_namespace == EXTATTR_NAMESPACE_EMPTY)
748 snprintf(eaname, sizeof(eaname), "%.*s",
749 (int)eap->ea_namelength, eap->ea_name);
750 vprintf(stdout, "\n\t%s, (%d bytes), %s",
751 namespace_names[eap->ea_namespace], eap->ea_length,
754 * First we try the general attribute setting interface.
755 * However, some attributes can only be set by root or
756 * by using special interfaces (for example, ACLs).
758 if (mode == SXA_FD) {
759 res = extattr_set_fd(fd, eap->ea_namespace,
760 eaname, EXTATTR_CONTENT(eap),
761 EXTATTR_CONTENT_SIZE(eap));
762 method = "extattr_set_fd";
763 } else if (mode == SXA_LINK) {
764 res = extattr_set_link(name, eap->ea_namespace,
765 eaname, EXTATTR_CONTENT(eap),
766 EXTATTR_CONTENT_SIZE(eap));
767 method = "extattr_set_link";
768 } else if (mode == SXA_FILE) {
769 res = extattr_set_file(name, eap->ea_namespace,
770 eaname, EXTATTR_CONTENT(eap),
771 EXTATTR_CONTENT_SIZE(eap));
772 method = "extattr_set_file";
775 dprintf(stdout, " (set using %s)", method);
779 * If the general interface refuses to set the attribute,
780 * then we try all the specialized interfaces that we
783 if (eap->ea_namespace == EXTATTR_NAMESPACE_SYSTEM &&
784 strcmp(eaname, POSIX1E_ACL_ACCESS_EXTATTR_NAME) == 0) {
785 if (mode == SXA_FD) {
786 error = acl_set_fd(fd, EXTATTR_CONTENT(eap));
787 method = "acl_set_fd";
788 } else if (mode == SXA_LINK) {
789 error = acl_set_link_np(name, ACL_TYPE_ACCESS,
790 EXTATTR_CONTENT(eap));
791 method = "acl_set_link_np";
792 } else if (mode == SXA_FILE) {
793 error = acl_set_file(name, ACL_TYPE_ACCESS,
794 EXTATTR_CONTENT(eap));
795 method = "acl_set_file";
798 dprintf(stdout, " (set using %s)", method);
802 if (eap->ea_namespace == EXTATTR_NAMESPACE_SYSTEM &&
803 strcmp(eaname, POSIX1E_ACL_DEFAULT_EXTATTR_NAME) == 0) {
804 if (mode == SXA_LINK) {
805 error = acl_set_link_np(name, ACL_TYPE_DEFAULT,
806 EXTATTR_CONTENT(eap));
807 method = "acl_set_link_np";
809 error = acl_set_file(name, ACL_TYPE_DEFAULT,
810 EXTATTR_CONTENT(eap));
811 method = "acl_set_file";
814 dprintf(stdout, " (set using %s)", method);
818 vprintf(stdout, " (unable to set)");
820 vprintf(stdout, "\n");
824 * skip over bit maps on the tape
830 while (spcl.c_type == TS_BITS || spcl.c_type == TS_CLRI)
835 * skip over a file on the tape
841 curfile.action = SKIP;
842 getfile(xtrnull, xtrnull, xtrnull);
846 * Skip a hole in an output file
849 skiphole(void (*skip)(char *, size_t), size_t *seekpos)
854 (*skip)(buf, *seekpos);
860 * Extract a file from the tape.
861 * When an allocated block is found it is passed to the fill function;
862 * when an unallocated block (hole) is found, a zeroed buffer is passed
863 * to the skip function.
866 getfile(void (*datafill)(char *, size_t), void (*attrfill)(char *, size_t),
867 void (*skip)(char *, size_t))
872 int curblk, attrsize;
873 void (*fillit)(char *, size_t);
874 char buf[MAXBSIZE / TP_BSIZE][TP_BSIZE];
880 attrsize = spcl.c_extsize;
881 if (spcl.c_type == TS_END)
882 panic("ran off end of tape\n");
883 if (spcl.c_magic != FS_UFS2_MAGIC)
884 panic("not at beginning of a file\n");
885 if (!gettingfile && setjmp(restart) != 0)
889 if (size == 0 && attrsize > 0) {
895 for (i = 0; i < spcl.c_count; i++) {
896 if (!readmapflag && i > TP_NINDIR) {
898 fprintf(stderr, "spcl.c_count = %jd\n",
899 (intmax_t)spcl.c_count);
902 panic("spcl.c_count = %jd\n",
903 (intmax_t)spcl.c_count);
905 if (readmapflag || spcl.c_addr[i]) {
906 readtape(&buf[curblk++][0]);
907 if (curblk == fssize / TP_BSIZE) {
908 skiphole(skip, &seekpos);
909 (*fillit)((char *)buf, (long)(size > TP_BSIZE ?
910 fssize : (curblk - 1) * TP_BSIZE + size));
915 skiphole(skip, &seekpos);
916 (*fillit)((char *)buf, (long)(size > TP_BSIZE ?
918 (curblk - 1) * TP_BSIZE + size));
922 * We have a block of a hole. Don't skip it
923 * now, because there may be next adjacent
924 * block of the hole in the file. Postpone the
925 * seek until next file write.
927 seekpos += (long)MIN(TP_BSIZE, size);
929 if ((size -= TP_BSIZE) <= 0) {
930 if (size > -TP_BSIZE && curblk > 0) {
931 skiphole(skip, &seekpos);
932 (*fillit)((char *)buf,
933 (long)((curblk * TP_BSIZE) + size));
942 if (spcl.c_count - i > 1)
943 dprintf(stdout, "skipping %d junk block(s)\n",
944 spcl.c_count - i - 1);
945 for (i++; i < spcl.c_count; i++) {
946 if (!readmapflag && i > TP_NINDIR) {
949 "spcl.c_count = %jd\n",
950 (intmax_t)spcl.c_count);
953 panic("spcl.c_count = %jd\n",
954 (intmax_t)spcl.c_count);
956 if (readmapflag || spcl.c_addr[i])
962 if (gethead(&spcl) == GOOD && size > 0) {
963 if (spcl.c_type == TS_ADDR)
966 "Missing address (header) block for %s at %ld blocks\n",
967 curfile.name, blksread);
970 panic("getfile: lost data\n");
976 * These variables are shared between the next two functions.
978 static int extbufsize = 0;
983 * Allocate a buffer into which to extract extended attributes.
986 setupextattr(int extsize)
990 if (extsize <= extbufsize)
994 if ((extbuf = malloc(extsize)) != NULL) {
995 extbufsize = extsize;
1000 fprintf(stderr, "Cannot extract %d bytes %s for inode %ju, name %s\n",
1001 extsize, "of extended attributes", (uintmax_t)curfile.ino,
1007 * Extract the next block of extended attributes.
1010 xtrattr(char *buf, size_t size)
1013 if (extloc + size > extbufsize)
1014 panic("overrun attribute buffer\n");
1015 memmove(&extbuf[extloc], buf, size);
1020 * Write out the next block of a file.
1023 xtrfile(char *buf, size_t size)
1028 if (write(ofile, buf, (int) size) == -1) {
1030 "write error extracting inode %ju, name %s\nwrite: %s\n",
1031 (uintmax_t)curfile.ino, curfile.name, strerror(errno));
1036 * Skip over a hole in a file.
1040 xtrskip(char *buf, size_t size)
1043 if (lseek(ofile, size, SEEK_CUR) == -1) {
1045 "seek error extracting inode %ju, name %s\nlseek: %s\n",
1046 (uintmax_t)curfile.ino, curfile.name, strerror(errno));
1052 * Collect the next block of a symbolic link.
1055 xtrlnkfile(char *buf, size_t size)
1059 if (pathlen > MAXPATHLEN) {
1060 fprintf(stderr, "symbolic link name: %s->%s%s; too long %d\n",
1061 curfile.name, lnkbuf, buf, pathlen);
1064 (void) strcat(lnkbuf, buf);
1068 * Skip over a hole in a symbolic link (should never happen).
1072 xtrlnkskip(char *buf, size_t size)
1075 fprintf(stderr, "unallocated block in symbolic link %s\n",
1081 * Collect the next block of a bit map.
1084 xtrmap(char *buf, size_t size)
1087 memmove(map, buf, size);
1092 * Skip over a hole in a bit map (should never happen).
1096 xtrmapskip(char *buf, size_t size)
1099 panic("hole in map\n");
1104 * Noop, when an extraction function is not needed.
1108 xtrnull(char *buf, size_t size)
1115 * Read TP_BSIZE blocks from the input.
1116 * Handle read errors, and end of media.
1121 long rd, newvol, i, oldnumtrec;
1122 int cnt, seek_failed;
1124 if (blkcnt + (byteslide > 0) < numtrec) {
1125 memmove(buf, &tapebuf[(blkcnt++ * TP_BSIZE) + byteslide], (long)TP_BSIZE);
1131 memmove(&tapebuf[-TP_BSIZE],
1132 &tapebuf[(numtrec-1) * TP_BSIZE], (long)TP_BSIZE);
1133 oldnumtrec = numtrec;
1134 for (i = 0; i < ntrec; i++)
1135 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1138 cnt = ntrec * TP_BSIZE;
1143 i = rmtread(&tapebuf[rd], cnt);
1146 i = read(mt, &tapebuf[rd], cnt);
1148 * Check for mid-tape short read error.
1149 * If found, skip rest of buffer and start with the next.
1151 if (!pipein && !pipecmdin && numtrec < ntrec && i > 0) {
1152 dprintf(stdout, "mid-media short read error.\n");
1156 * Handle partial block read.
1158 if ((pipein || pipecmdin) && i == 0 && rd > 0)
1160 else if (i > 0 && i != ntrec * TP_BSIZE) {
1161 if (pipein || pipecmdin) {
1169 * Short read. Process the blocks read.
1171 if (i % TP_BSIZE != 0)
1173 "partial block read: %ld should be %ld\n",
1174 i, ntrec * TP_BSIZE);
1175 numtrec = i / TP_BSIZE;
1179 * Handle read error.
1182 fprintf(stderr, "Tape read error while ");
1183 switch (curfile.action) {
1185 fprintf(stderr, "trying to set up tape\n");
1188 fprintf(stderr, "trying to resynchronize\n");
1191 fprintf(stderr, "restoring %s\n", curfile.name);
1194 fprintf(stderr, "skipping over inode %ju\n",
1195 (uintmax_t)curfile.ino);
1198 if (!yflag && !reply("continue"))
1200 i = ntrec * TP_BSIZE;
1201 memset(tapebuf, 0, i);
1204 seek_failed = (rmtseek(i, 1) < 0);
1207 seek_failed = (lseek(mt, i, SEEK_CUR) == (off_t)-1);
1211 "continuation failed: %s\n", strerror(errno));
1216 * Handle end of tape.
1219 vprintf(stdout, "End-of-tape encountered\n");
1228 if (rd % TP_BSIZE != 0)
1229 panic("partial block read: %ld should be %ld\n",
1230 rd, ntrec * TP_BSIZE);
1232 memmove(&tapebuf[rd], &endoftapemark, (long)TP_BSIZE);
1234 if (oldnumtrec == 0)
1237 blkcnt -= oldnumtrec;
1239 &tapebuf[(blkcnt++ * TP_BSIZE) + byteslide], (long)TP_BSIZE);
1245 findtapeblksize(void)
1249 for (i = 0; i < ntrec; i++)
1250 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1254 i = rmtread(tapebuf, ntrec * TP_BSIZE);
1257 i = read(mt, tapebuf, ntrec * TP_BSIZE);
1260 fprintf(stderr, "tape read error: %s\n", strerror(errno));
1263 if (i % TP_BSIZE != 0) {
1264 fprintf(stderr, "Tape block size (%ld) %s (%d)\n",
1265 i, "is not a multiple of dump block size", TP_BSIZE);
1268 ntrec = i / TP_BSIZE;
1270 vprintf(stdout, "Tape block size is %ld\n", ntrec);
1292 * Read the next block from the tape.
1293 * If it is not any valid header, return an error.
1296 gethead(struct s_spcl *buf)
1300 readtape((char *)buf);
1301 if (buf->c_magic != FS_UFS2_MAGIC && buf->c_magic != NFS_MAGIC) {
1302 if (buf->c_magic == OFS_MAGIC) {
1304 "Format of dump tape is too old. Must use\n");
1306 "a version of restore from before 2002.\n");
1309 if (swabl(buf->c_magic) != FS_UFS2_MAGIC &&
1310 swabl(buf->c_magic) != NFS_MAGIC) {
1311 if (swabl(buf->c_magic) == OFS_MAGIC) {
1313 "Format of dump tape is too old. Must use\n");
1315 "a version of restore from before 2002.\n");
1320 vprintf(stdout, "Note: Doing Byte swapping\n");
1324 if (checksum((int *)buf) == FAIL)
1327 swabst((u_char *)"8l4s1q8l2q17l", (u_char *)buf);
1328 swabst((u_char *)"l",(u_char *) &buf->c_level);
1329 swabst((u_char *)"2l4q",(u_char *) &buf->c_flags);
1333 switch (buf->c_type) {
1338 * Have to patch up missing information in bit map headers
1340 buf->c_size = buf->c_count * TP_BSIZE;
1341 if (buf->c_count > TP_NINDIR)
1344 for (i = 0; i < buf->c_count; i++)
1349 if (buf->c_magic == NFS_MAGIC &&
1350 (buf->c_flags & NFS_DR_NEWINODEFMT) == 0)
1361 * For old dump tapes, have to copy up old fields to
1364 if (buf->c_magic == NFS_MAGIC) {
1365 buf->c_tapea = buf->c_old_tapea;
1366 buf->c_firstrec = buf->c_old_firstrec;
1367 buf->c_date = _time32_to_time(buf->c_old_date);
1368 buf->c_ddate = _time32_to_time(buf->c_old_ddate);
1369 buf->c_atime = _time32_to_time(buf->c_old_atime);
1370 buf->c_mtime = _time32_to_time(buf->c_old_mtime);
1371 buf->c_birthtime = 0;
1372 buf->c_birthtimensec = 0;
1378 panic("gethead: unknown inode type %d\n", buf->c_type);
1381 if (dumpdate != 0 && _time64_to_time(buf->c_date) != dumpdate)
1382 fprintf(stderr, "Header with wrong dumpdate.\n");
1384 * If we're restoring a filesystem with the old (FreeBSD 1)
1385 * format inodes, copy the uid/gid to the new location
1388 buf->c_uid = buf->c_spare1[1];
1389 buf->c_gid = buf->c_spare1[2];
1391 buf->c_magic = FS_UFS2_MAGIC;
1392 tapeaddr = buf->c_tapea;
1399 * Check that a header is where it belongs and predict the next header
1402 accthdr(struct s_spcl *header)
1404 static ino_t previno = 0x7fffffff;
1405 static int prevtype;
1406 static long predict;
1409 if (header->c_type == TS_TAPE) {
1410 fprintf(stderr, "Volume header ");
1411 if (header->c_firstrec)
1412 fprintf(stderr, "begins with record %jd",
1413 (intmax_t)header->c_firstrec);
1414 fprintf(stderr, "\n");
1415 previno = 0x7fffffff;
1418 if (previno == 0x7fffffff)
1422 fprintf(stderr, "Dumped inodes map header");
1425 fprintf(stderr, "Used inodes map header");
1428 fprintf(stderr, "File header, ino %ju", (uintmax_t)previno);
1431 fprintf(stderr, "File continuation header, ino %ju",
1432 (uintmax_t)previno);
1435 fprintf(stderr, "End of tape header");
1438 if (predict != blksread - 1)
1439 fprintf(stderr, "; predicted %ld blocks, got %ld blocks",
1440 predict, blksread - 1);
1441 fprintf(stderr, "\n");
1444 if (header->c_type != TS_END)
1445 for (i = 0; i < header->c_count; i++)
1446 if (readmapflag || header->c_addr[i] != 0)
1450 prevtype = header->c_type;
1451 previno = header->c_inumber;
1455 * Find an inode header.
1456 * Complain if had to skip.
1459 findinode(struct s_spcl *header)
1461 static long skipcnt = 0;
1466 curfile.name = "<name unknown>";
1467 curfile.action = UNKNOWN;
1471 htype = header->c_type;
1476 * Skip up to the beginning of the next record
1478 for (i = 0; i < header->c_count; i++)
1479 if (header->c_addr[i])
1481 while (gethead(header) == FAIL ||
1482 _time64_to_time(header->c_date) != dumpdate) {
1486 if (byteslide < TP_BSIZE) {
1496 curfile.mode = header->c_mode;
1497 curfile.uid = header->c_uid;
1498 curfile.gid = header->c_gid;
1499 curfile.file_flags = header->c_file_flags;
1500 curfile.rdev = header->c_rdev;
1501 curfile.atime_sec = header->c_atime;
1502 curfile.atime_nsec = header->c_atimensec;
1503 curfile.mtime_sec = header->c_mtime;
1504 curfile.mtime_nsec = header->c_mtimensec;
1505 curfile.birthtime_sec = header->c_birthtime;
1506 curfile.birthtime_nsec = header->c_birthtimensec;
1507 curfile.extsize = header->c_extsize;
1508 curfile.size = header->c_size;
1509 curfile.ino = header->c_inumber;
1513 /* If we missed some tapes, get another volume. */
1514 if (tapesread & (tapesread + 1)) {
1518 curfile.ino = maxino;
1522 curfile.name = "<file removal list>";
1526 curfile.name = "<file dump list>";
1531 fprintf(stderr, "unexpected tape header\n");
1533 panic("unexpected tape header\n");
1537 fprintf(stderr, "unknown tape header type %d\n",
1540 panic("unknown tape header type %d\n",
1542 while (gethead(header) == FAIL ||
1543 _time64_to_time(header->c_date) != dumpdate) {
1547 if (byteslide < TP_BSIZE) {
1556 } while (htype == TS_ADDR);
1558 fprintf(stderr, "resync restore, skipped %ld %s\n",
1559 skipcnt, Dflag ? "bytes" : "blocks");
1568 j = sizeof(union u_spcl) / sizeof(int);
1575 /* What happens if we want to read restore tapes
1576 for a 16bit int machine??? */
1582 if (i != CHECKSUM) {
1583 fprintf(stderr, "Checksum error %o, inode %ju file %s\n", i,
1584 (uintmax_t)curfile.ino, curfile.name);
1594 msg(const char *fmt, ...)
1598 (void)vfprintf(stderr, fmt, ap);
1601 #endif /* RRESTORE */
1604 swabshort(u_char *sp, int n)
1609 c = sp[0]; sp[0] = sp[1]; sp[1] = c;
1616 swablong(u_char *sp, int n)
1621 c = sp[0]; sp[0] = sp[3]; sp[3] = c;
1622 c = sp[2]; sp[2] = sp[1]; sp[1] = c;
1629 swabquad(u_char *sp, int n)
1634 c = sp[0]; sp[0] = sp[7]; sp[7] = c;
1635 c = sp[1]; sp[1] = sp[6]; sp[6] = c;
1636 c = sp[2]; sp[2] = sp[5]; sp[5] = c;
1637 c = sp[3]; sp[3] = sp[4]; sp[4] = c;
1644 swabst(u_char *cp, u_char *sp)
1650 case '0': case '1': case '2': case '3': case '4':
1651 case '5': case '6': case '7': case '8': case '9':
1652 n = (n * 10) + (*cp++ - '0');
1655 case 's': case 'w': case 'h':
1658 sp = swabshort(sp, n);
1664 sp = swablong(sp, n);
1670 sp = swabquad(sp, n);
1680 fprintf(stderr, "Unknown conversion character: %c\n",
1693 swabst((u_char *)"l", (u_char *)&x);