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
39 static char sccsid[] = "@(#)tape.c 8.9 (Berkeley) 5/1/95";
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include <sys/param.h>
51 #include <sys/extattr.h>
54 #include <ufs/ufs/extattr.h>
55 #include <ufs/ufs/dinode.h>
56 #include <protocols/dumprestore.h>
73 static long fssize = MAXBSIZE;
75 static int pipein = 0;
76 static int pipecmdin = 0;
77 static FILE *popenfp = NULL;
82 static union u_spcl endoftapemark;
83 static long byteslide = 0;
84 static long blksread; /* blocks read since last header */
85 static int64_t tapeaddr = 0; /* current TP_BSIZE tape record */
86 static long tapesread;
87 static jmp_buf restart;
88 static int gettingfile = 0; /* restart has a valid frame */
89 static char *host = NULL;
90 static int readmapflag;
94 static char lnkbuf[MAXPATHLEN + 1];
97 int Bcvt; /* Swap Bytes */
98 int oldinofmt; /* FreeBSD 1 inode format needs cvt */
100 #define FLUSHTAPEBUF() blkcnt = ntrec + 1
102 char *namespace_names[] = EXTATTR_NAMESPACE_NAMES;
104 static void accthdr(struct s_spcl *);
105 static int checksum(int *);
106 static void findinode(struct s_spcl *);
107 static void findtapeblksize(void);
108 static char *setupextattr(int);
109 static void xtrattr(char *, size_t);
110 static void skiphole(void (*)(char *, size_t), size_t *);
111 static int gethead(struct s_spcl *);
112 static void readtape(char *);
113 static void setdumpnum(void);
114 static u_long swabl(u_long);
115 static u_char *swablong(u_char *, int);
116 static u_char *swabshort(u_char *, int);
117 static void terminateinput(void);
118 static void xtrfile(char *, size_t);
119 static void xtrlnkfile(char *, size_t);
120 static void xtrlnkskip(char *, size_t);
121 static void xtrmap(char *, size_t);
122 static void xtrmapskip(char *, size_t);
123 static void xtrskip(char *, size_t);
126 * Set up an input source
129 setinput(char *source, int ispipecommand)
135 newtapebuf(MAX(NTREC, HIGHDENSITYTREC));
142 if (strchr(source, ':')) {
144 source = strchr(host, ':');
146 if (rmthost(host) == 0)
150 if (strcmp(source, "-") == 0) {
152 * Since input is coming from a pipe we must establish
153 * our own connection to the terminal.
155 terminal = fopen(_PATH_TTY, "r");
156 if (terminal == NULL) {
157 (void)fprintf(stderr, "cannot open %s: %s\n",
158 _PATH_TTY, strerror(errno));
159 terminal = fopen(_PATH_DEVNULL, "r");
160 if (terminal == NULL) {
161 (void)fprintf(stderr, "cannot open %s: %s\n",
162 _PATH_DEVNULL, strerror(errno));
168 /* no longer need or want root privileges */
169 if (setuid(getuid()) != 0) {
170 fprintf(stderr, "setuid failed\n");
173 magtape = strdup(source);
174 if (magtape == NULL) {
175 fprintf(stderr, "Cannot allocate space for magtape buffer\n");
181 newtapebuf(long size)
183 static int tapebufsize = -1;
186 if (size <= tapebufsize)
189 free(tapebuf - TP_BSIZE);
190 tapebuf = malloc((size+1) * TP_BSIZE);
191 if (tapebuf == NULL) {
192 fprintf(stderr, "Cannot allocate space for tape buffer\n");
200 * Verify that the tape drive can be accessed and
201 * that it actually is a dump tape.
209 vprintf(stdout, "Verify tape and initialize maps\n");
211 if (setenv("RESTORE_VOLUME", "1", 1) == -1) {
212 fprintf(stderr, "Cannot set $RESTORE_VOLUME: %s\n",
216 popenfp = popen(magtape, "r");
217 mt = popenfp ? fileno(popenfp) : -1;
221 mt = rmtopen(magtape, 0);
227 mt = open(magtape, O_RDONLY, 0);
229 fprintf(stderr, "%s: %s\n", magtape, strerror(errno));
235 if (!pipein && !pipecmdin && !bflag)
237 if (gethead(&spcl) == FAIL) {
238 fprintf(stderr, "Tape is not a dump tape\n");
242 endoftapemark.s_spcl.c_magic = FS_UFS2_MAGIC;
243 endoftapemark.s_spcl.c_type = TS_END;
244 ip = (int *)&endoftapemark;
245 j = sizeof(union u_spcl) / sizeof(int);
250 endoftapemark.s_spcl.c_checksum = CHECKSUM - i;
252 if (vflag || command == 't')
254 dumptime = _time64_to_time(spcl.c_ddate);
255 dumpdate = _time64_to_time(spcl.c_date);
256 if (stat(".", &stbuf) < 0) {
257 fprintf(stderr, "cannot stat .: %s\n", strerror(errno));
260 if (stbuf.st_blksize > 0 && stbuf.st_blksize < TP_BSIZE )
262 if (stbuf.st_blksize >= TP_BSIZE && stbuf.st_blksize <= MAXBSIZE)
263 fssize = stbuf.st_blksize;
264 if (((TP_BSIZE - 1) & stbuf.st_blksize) != 0) {
265 fprintf(stderr, "Warning: filesystem with non-multiple-of-%d "
266 "blocksize (%d);\n", TP_BSIZE, stbuf.st_blksize);
267 fssize = roundup(fssize, TP_BSIZE);
268 fprintf(stderr, "\twriting using blocksize %ld\n", fssize);
270 if (spcl.c_volume != 1) {
271 fprintf(stderr, "Tape is not volume 1 of the dump\n");
274 if (gethead(&spcl) == FAIL) {
275 dprintf(stdout, "header read failed at %ld blocks\n", blksread);
276 panic("no header after volume mark!\n");
279 if (spcl.c_type != TS_CLRI) {
280 fprintf(stderr, "Cannot find file removal list\n");
283 maxino = (spcl.c_count * TP_BSIZE * NBBY) + 1;
284 dprintf(stdout, "maxino = %ju\n", (uintmax_t)maxino);
285 map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
287 panic("no memory for active inode map\n");
289 curfile.action = USING;
290 getfile(xtrmap, xtrmapskip, xtrmapskip);
291 if (spcl.c_type != TS_BITS) {
292 fprintf(stderr, "Cannot find file dump list\n");
295 map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
296 if (map == (char *)NULL)
297 panic("no memory for file dump list\n");
299 curfile.action = USING;
300 getfile(xtrmap, xtrmapskip, xtrmapskip);
302 * If there may be whiteout entries on the tape, pretend that the
303 * whiteout inode exists, so that the whiteout entries can be
306 SETINO(UFS_WINO, dumpmap);
307 /* 'r' restores don't call getvol() for tape 1, so mark it as read. */
313 * Prompt user to load a new dump volume.
314 * "Nextvol" is the next suggested volume to use.
315 * This suggested volume is enforced when doing full
316 * or incremental restores, but can be overridden by
317 * the user when only extracting a subset of the files.
323 long i, newvol, savecnt;
324 union u_spcl tmpspcl;
325 # define tmpbuf tmpspcl.s_spcl
332 prevtapea = tapeaddr;
336 panic("Changing volumes on pipe input?\n");
337 /* Avoid looping if we couldn't ask the user. */
338 if (yflag || ferror(terminal) || feof(terminal))
348 done(1); /* pipes do not get a second chance */
349 if (command == 'R' || command == 'r' || curfile.action != SKIP)
353 while (newvol <= 0) {
354 if (tapesread == 0) {
355 fprintf(stderr, "%s%s%s%s%s%s%s",
356 "You have not read any tapes yet.\n",
357 "If you are extracting just a few files,",
358 " start with the last volume\n",
359 "and work towards the first; restore",
360 " can quickly skip tapes that\n",
361 "have no further files to extract.",
362 " Otherwise, begin with volume 1.\n");
364 fprintf(stderr, "You have read volumes");
366 for (i = 0; i < 32; i++)
367 if (tapesread & (1 << i)) {
368 fprintf(stderr, "%s%ld", buf, i + 1);
371 fprintf(stderr, "\n");
374 fprintf(stderr, "Specify next volume #: ");
375 (void) fflush(stderr);
376 if (fgets(buf, BUFSIZ, terminal) == NULL)
378 } while (buf[0] == '\n');
382 "Volume numbers are positive numerics\n");
385 if (newvol == volno) {
386 tapesread |= 1 << (volno - 1);
390 fprintf(stderr, "Mount tape volume %ld\n", newvol);
391 fprintf(stderr, "Enter ``none'' if there are no more tapes\n");
392 fprintf(stderr, "otherwise enter tape name (default: %s) ", magtape);
393 (void) fflush(stderr);
394 if (fgets(buf, BUFSIZ, terminal) == NULL)
396 if (!strcmp(buf, "none\n")) {
400 if (buf[0] != '\n') {
401 (void) strcpy(magtape, buf);
402 magtape[strlen(magtape) - 1] = '\0';
405 char volno[sizeof("2147483647")];
407 (void)sprintf(volno, "%ld", newvol);
408 if (setenv("RESTORE_VOLUME", volno, 1) == -1) {
409 fprintf(stderr, "Cannot set $RESTORE_VOLUME: %s\n",
413 popenfp = popen(magtape, "r");
414 mt = popenfp ? fileno(popenfp) : -1;
418 mt = rmtopen(magtape, 0);
421 mt = open(magtape, O_RDONLY, 0);
424 fprintf(stderr, "Cannot open %s\n", magtape);
432 if (gethead(&tmpbuf) == FAIL) {
433 dprintf(stdout, "header read failed at %ld blocks\n", blksread);
434 fprintf(stderr, "tape is not dump tape\n");
438 if (tmpbuf.c_volume != volno) {
439 fprintf(stderr, "Wrong volume (%jd)\n",
440 (intmax_t)tmpbuf.c_volume);
444 if (_time64_to_time(tmpbuf.c_date) != dumpdate ||
445 _time64_to_time(tmpbuf.c_ddate) != dumptime) {
446 time_t t = _time64_to_time(tmpbuf.c_date);
447 fprintf(stderr, "Wrong dump date\n\tgot: %s", ctime(&t));
448 fprintf(stderr, "\twanted: %s", ctime(&dumpdate));
452 tapesread |= 1 << (volno - 1);
455 * If continuing from the previous volume, skip over any
456 * blocks read already at the end of the previous volume.
458 * If coming to this volume at random, skip to the beginning
459 * of the next record.
461 dprintf(stdout, "last rec %jd, tape starts with %jd\n",
462 (intmax_t)prevtapea, (intmax_t)tmpbuf.c_tapea);
463 if (tmpbuf.c_type == TS_TAPE) {
464 if (curfile.action != USING) {
466 * XXX Dump incorrectly sets c_count to 1 in the
467 * volume header of the first tape, so ignore
468 * c_count when volno == 1.
471 for (i = tmpbuf.c_count; i > 0; i--)
473 } else if (tmpbuf.c_tapea <= prevtapea) {
475 * Normally the value of c_tapea in the volume
476 * header is the record number of the header itself.
477 * However in the volume header following an EOT-
478 * terminated tape, it is the record number of the
479 * first continuation data block (dump bug?).
481 * The next record we want is `prevtapea + 1'.
483 i = prevtapea + 1 - tmpbuf.c_tapea;
484 dprintf(stderr, "Skipping %ld duplicate record%s.\n",
485 i, i > 1 ? "s" : "");
490 if (curfile.action == USING) {
492 panic("active file into volume 1\n");
495 (void) gethead(&spcl);
504 * Handle unexpected EOF.
510 if (gettingfile && curfile.action == USING) {
511 printf("Warning: %s %s\n",
512 "End-of-input encountered while extracting", curfile.name);
514 curfile.name = "<name unknown>";
515 curfile.action = UNKNOWN;
517 curfile.ino = maxino;
525 * handle multiple dumps per tape by skipping forward to the
533 if (dumpnum == 1 || volno != 1)
536 fprintf(stderr, "Cannot have multiple dumps on pipe input\n");
540 tcom.mt_count = dumpnum - 1;
543 rmtioctl(MTFSF, dumpnum - 1);
546 if (!pipecmdin && ioctl(mt, MTIOCTOP, (char *)&tcom) < 0)
547 fprintf(stderr, "ioctl MTFSF: %s\n", strerror(errno));
554 t = _time64_to_time(spcl.c_date);
555 fprintf(stdout, "Dump date: %s", ctime(&t));
556 t = _time64_to_time(spcl.c_ddate);
557 fprintf(stdout, "Dumped from: %s",
558 (spcl.c_ddate == 0) ? "the epoch\n" : ctime(&t));
559 if (spcl.c_host[0] == '\0')
561 fprintf(stderr, "Level %jd dump of %s on %s:%s\n",
562 (intmax_t)spcl.c_level, spcl.c_filesys, spcl.c_host, spcl.c_dev);
563 fprintf(stderr, "Label: %s\n", spcl.c_label);
567 extractfile(char *name)
574 struct timespec mtimep[2], ctimep[2];
579 curfile.action = USING;
580 mtimep[0].tv_sec = curfile.atime_sec;
581 mtimep[0].tv_nsec = curfile.atime_nsec;
582 mtimep[1].tv_sec = curfile.mtime_sec;
583 mtimep[1].tv_nsec = curfile.mtime_nsec;
584 ctimep[0].tv_sec = curfile.atime_sec;
585 ctimep[0].tv_nsec = curfile.atime_nsec;
586 ctimep[1].tv_sec = curfile.birthtime_sec;
587 ctimep[1].tv_nsec = curfile.birthtime_nsec;
588 extsize = curfile.extsize;
594 flags = curfile.file_flags;
595 switch (mode & IFMT) {
598 fprintf(stderr, "%s: unknown file mode 0%o\n", name, mode);
603 vprintf(stdout, "skipped socket %s\n", name);
609 ep = lookupname(name);
610 if (ep == NULL || ep->e_flags & EXTRACT)
611 panic("unextracted directory %s\n", name);
615 vprintf(stdout, "extract file %s\n", name);
616 return (genliteraldir(name, curfile.ino));
621 buf = setupextattr(extsize);
622 getfile(xtrlnkfile, xtrattr, xtrlnkskip);
625 "%s: zero length symbolic link (ignored)\n", name);
628 if (linkit(lnkbuf, name, SYMLINK) == GOOD) {
630 set_extattr(-1, name, buf, extsize, SXA_LINK);
631 (void) lchown(name, uid, gid);
632 (void) lchmod(name, mode);
633 (void) utimensat(AT_FDCWD, name, ctimep,
634 AT_SYMLINK_NOFOLLOW);
635 (void) utimensat(AT_FDCWD, name, mtimep,
636 AT_SYMLINK_NOFOLLOW);
637 (void) lchflags(name, flags);
643 vprintf(stdout, "extract fifo %s\n", name);
650 if (mkfifo(name, 0600) < 0) {
651 fprintf(stderr, "%s: cannot create fifo: %s\n",
652 name, strerror(errno));
659 buf = setupextattr(extsize);
660 getfile(xtrnull, xtrattr, xtrnull);
661 set_extattr(-1, name, buf, extsize, SXA_FILE);
663 (void) chown(name, uid, gid);
664 (void) chmod(name, mode);
665 (void) utimensat(AT_FDCWD, name, ctimep, 0);
666 (void) utimensat(AT_FDCWD, name, mtimep, 0);
667 (void) chflags(name, flags);
672 vprintf(stdout, "extract special file %s\n", name);
679 if (mknod(name, (mode & (IFCHR | IFBLK)) | 0600,
680 (int)curfile.rdev) < 0) {
681 fprintf(stderr, "%s: cannot create special file: %s\n",
682 name, strerror(errno));
689 buf = setupextattr(extsize);
690 getfile(xtrnull, xtrattr, xtrnull);
691 set_extattr(-1, name, buf, extsize, SXA_FILE);
693 (void) chown(name, uid, gid);
694 (void) chmod(name, mode);
695 (void) utimensat(AT_FDCWD, name, ctimep, 0);
696 (void) utimensat(AT_FDCWD, name, mtimep, 0);
697 (void) chflags(name, flags);
701 vprintf(stdout, "extract file %s\n", name);
708 if ((ofile = open(name, O_WRONLY | O_CREAT | O_TRUNC,
710 fprintf(stderr, "%s: cannot create file: %s\n",
711 name, strerror(errno));
715 buf = setupextattr(extsize);
716 getfile(xtrfile, xtrattr, xtrskip);
718 set_extattr(ofile, name, buf, extsize, SXA_FD);
719 (void) fchown(ofile, uid, gid);
720 (void) fchmod(ofile, mode);
721 (void) futimens(ofile, ctimep);
722 (void) futimens(ofile, mtimep);
723 (void) fchflags(ofile, flags);
731 * Set attributes on a file descriptor, link, or file.
734 set_extattr(int fd, char *name, void *buf, int size, enum set_extattr_mode mode)
736 struct extattr *eap, *eaend;
740 char eaname[EXTATTR_MAXNAMELEN + 1];
742 vprintf(stdout, "Set attributes for %s:", name);
744 for (eap = buf; eap < eaend; eap = EXTATTR_NEXT(eap)) {
746 * Make sure this entry is complete.
748 if (EXTATTR_NEXT(eap) > eaend || eap->ea_length <= 0) {
749 dprintf(stdout, "\n\t%scorrupted",
750 eap == buf ? "" : "remainder ");
753 if (eap->ea_namespace == EXTATTR_NAMESPACE_EMPTY)
755 snprintf(eaname, sizeof(eaname), "%.*s",
756 (int)eap->ea_namelength, eap->ea_name);
757 vprintf(stdout, "\n\t%s, (%d bytes), %s",
758 namespace_names[eap->ea_namespace], eap->ea_length,
761 * First we try the general attribute setting interface.
762 * However, some attributes can only be set by root or
763 * by using special interfaces (for example, ACLs).
765 if (mode == SXA_FD) {
766 res = extattr_set_fd(fd, eap->ea_namespace,
767 eaname, EXTATTR_CONTENT(eap),
768 EXTATTR_CONTENT_SIZE(eap));
769 method = "extattr_set_fd";
770 } else if (mode == SXA_LINK) {
771 res = extattr_set_link(name, eap->ea_namespace,
772 eaname, EXTATTR_CONTENT(eap),
773 EXTATTR_CONTENT_SIZE(eap));
774 method = "extattr_set_link";
775 } else if (mode == SXA_FILE) {
776 res = extattr_set_file(name, eap->ea_namespace,
777 eaname, EXTATTR_CONTENT(eap),
778 EXTATTR_CONTENT_SIZE(eap));
779 method = "extattr_set_file";
782 dprintf(stdout, " (set using %s)", method);
786 * If the general interface refuses to set the attribute,
787 * then we try all the specialized interfaces that we
790 if (eap->ea_namespace == EXTATTR_NAMESPACE_SYSTEM &&
791 strcmp(eaname, POSIX1E_ACL_ACCESS_EXTATTR_NAME) == 0) {
792 if (mode == SXA_FD) {
793 error = acl_set_fd(fd, EXTATTR_CONTENT(eap));
794 method = "acl_set_fd";
795 } else if (mode == SXA_LINK) {
796 error = acl_set_link_np(name, ACL_TYPE_ACCESS,
797 EXTATTR_CONTENT(eap));
798 method = "acl_set_link_np";
799 } else if (mode == SXA_FILE) {
800 error = acl_set_file(name, ACL_TYPE_ACCESS,
801 EXTATTR_CONTENT(eap));
802 method = "acl_set_file";
805 dprintf(stdout, " (set using %s)", method);
809 if (eap->ea_namespace == EXTATTR_NAMESPACE_SYSTEM &&
810 strcmp(eaname, POSIX1E_ACL_DEFAULT_EXTATTR_NAME) == 0) {
811 if (mode == SXA_LINK) {
812 error = acl_set_link_np(name, ACL_TYPE_DEFAULT,
813 EXTATTR_CONTENT(eap));
814 method = "acl_set_link_np";
816 error = acl_set_file(name, ACL_TYPE_DEFAULT,
817 EXTATTR_CONTENT(eap));
818 method = "acl_set_file";
821 dprintf(stdout, " (set using %s)", method);
825 vprintf(stdout, " (unable to set)");
827 vprintf(stdout, "\n");
831 * skip over bit maps on the tape
837 while (spcl.c_type == TS_BITS || spcl.c_type == TS_CLRI)
842 * skip over a file on the tape
848 curfile.action = SKIP;
849 getfile(xtrnull, xtrnull, xtrnull);
853 * Skip a hole in an output file
856 skiphole(void (*skip)(char *, size_t), size_t *seekpos)
861 (*skip)(buf, *seekpos);
867 * Extract a file from the tape.
868 * When an allocated block is found it is passed to the fill function;
869 * when an unallocated block (hole) is found, a zeroed buffer is passed
870 * to the skip function.
873 getfile(void (*datafill)(char *, size_t), void (*attrfill)(char *, size_t),
874 void (*skip)(char *, size_t))
879 int curblk, attrsize;
880 void (*fillit)(char *, size_t);
881 char buf[MAXBSIZE / TP_BSIZE][TP_BSIZE];
887 attrsize = spcl.c_extsize;
888 if (spcl.c_type == TS_END)
889 panic("ran off end of tape\n");
890 if (spcl.c_magic != FS_UFS2_MAGIC)
891 panic("not at beginning of a file\n");
892 if (!gettingfile && setjmp(restart) != 0)
896 if (size == 0 && attrsize > 0) {
902 for (i = 0; i < spcl.c_count; i++) {
903 if (!readmapflag && i > TP_NINDIR) {
905 fprintf(stderr, "spcl.c_count = %jd\n",
906 (intmax_t)spcl.c_count);
909 panic("spcl.c_count = %jd\n",
910 (intmax_t)spcl.c_count);
912 if (readmapflag || spcl.c_addr[i]) {
913 readtape(&buf[curblk++][0]);
914 if (curblk == fssize / TP_BSIZE) {
915 skiphole(skip, &seekpos);
916 (*fillit)((char *)buf, (long)(size > TP_BSIZE ?
917 fssize : (curblk - 1) * TP_BSIZE + size));
922 skiphole(skip, &seekpos);
923 (*fillit)((char *)buf, (long)(size > TP_BSIZE ?
925 (curblk - 1) * TP_BSIZE + size));
929 * We have a block of a hole. Don't skip it
930 * now, because there may be next adjacent
931 * block of the hole in the file. Postpone the
932 * seek until next file write.
934 seekpos += (long)MIN(TP_BSIZE, size);
936 if ((size -= TP_BSIZE) <= 0) {
937 if (size > -TP_BSIZE && curblk > 0) {
938 skiphole(skip, &seekpos);
939 (*fillit)((char *)buf,
940 (long)((curblk * TP_BSIZE) + size));
949 if (spcl.c_count - i > 1)
950 dprintf(stdout, "skipping %d junk block(s)\n",
951 spcl.c_count - i - 1);
952 for (i++; i < spcl.c_count; i++) {
953 if (!readmapflag && i > TP_NINDIR) {
956 "spcl.c_count = %jd\n",
957 (intmax_t)spcl.c_count);
960 panic("spcl.c_count = %jd\n",
961 (intmax_t)spcl.c_count);
963 if (readmapflag || spcl.c_addr[i])
969 if (gethead(&spcl) == GOOD && size > 0) {
970 if (spcl.c_type == TS_ADDR)
973 "Missing address (header) block for %s at %ld blocks\n",
974 curfile.name, blksread);
977 panic("getfile: lost data\n");
983 * These variables are shared between the next two functions.
985 static int extbufsize = 0;
990 * Allocate a buffer into which to extract extended attributes.
993 setupextattr(int extsize)
997 if (extsize <= extbufsize)
1001 if ((extbuf = malloc(extsize)) != NULL) {
1002 extbufsize = extsize;
1007 fprintf(stderr, "Cannot extract %d bytes %s for inode %ju, name %s\n",
1008 extsize, "of extended attributes", (uintmax_t)curfile.ino,
1014 * Extract the next block of extended attributes.
1017 xtrattr(char *buf, size_t size)
1020 if (extloc + size > extbufsize)
1021 panic("overrun attribute buffer\n");
1022 memmove(&extbuf[extloc], buf, size);
1027 * Write out the next block of a file.
1030 xtrfile(char *buf, size_t size)
1035 if (write(ofile, buf, (int) size) == -1) {
1037 "write error extracting inode %ju, name %s\nwrite: %s\n",
1038 (uintmax_t)curfile.ino, curfile.name, strerror(errno));
1043 * Skip over a hole in a file.
1047 xtrskip(char *buf, size_t size)
1050 if (lseek(ofile, size, SEEK_CUR) == -1) {
1052 "seek error extracting inode %ju, name %s\nlseek: %s\n",
1053 (uintmax_t)curfile.ino, curfile.name, strerror(errno));
1059 * Collect the next block of a symbolic link.
1062 xtrlnkfile(char *buf, size_t size)
1066 if (pathlen > MAXPATHLEN) {
1067 fprintf(stderr, "symbolic link name: %s->%s%s; too long %d\n",
1068 curfile.name, lnkbuf, buf, pathlen);
1071 (void) strcat(lnkbuf, buf);
1075 * Skip over a hole in a symbolic link (should never happen).
1079 xtrlnkskip(char *buf, size_t size)
1082 fprintf(stderr, "unallocated block in symbolic link %s\n",
1088 * Collect the next block of a bit map.
1091 xtrmap(char *buf, size_t size)
1094 memmove(map, buf, size);
1099 * Skip over a hole in a bit map (should never happen).
1103 xtrmapskip(char *buf, size_t size)
1106 panic("hole in map\n");
1111 * Noop, when an extraction function is not needed.
1115 xtrnull(char *buf, size_t size)
1122 * Read TP_BSIZE blocks from the input.
1123 * Handle read errors, and end of media.
1128 long rd, newvol, i, oldnumtrec;
1129 int cnt, seek_failed;
1131 if (blkcnt + (byteslide > 0) < numtrec) {
1132 memmove(buf, &tapebuf[(blkcnt++ * TP_BSIZE) + byteslide], (long)TP_BSIZE);
1138 memmove(&tapebuf[-TP_BSIZE],
1139 &tapebuf[(numtrec-1) * TP_BSIZE], (long)TP_BSIZE);
1140 oldnumtrec = numtrec;
1141 for (i = 0; i < ntrec; i++)
1142 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1145 cnt = ntrec * TP_BSIZE;
1150 i = rmtread(&tapebuf[rd], cnt);
1153 i = read(mt, &tapebuf[rd], cnt);
1155 * Check for mid-tape short read error.
1156 * If found, skip rest of buffer and start with the next.
1158 if (!pipein && !pipecmdin && numtrec < ntrec && i > 0) {
1159 dprintf(stdout, "mid-media short read error.\n");
1163 * Handle partial block read.
1165 if ((pipein || pipecmdin) && i == 0 && rd > 0)
1167 else if (i > 0 && i != ntrec * TP_BSIZE) {
1168 if (pipein || pipecmdin) {
1176 * Short read. Process the blocks read.
1178 if (i % TP_BSIZE != 0)
1180 "partial block read: %ld should be %ld\n",
1181 i, ntrec * TP_BSIZE);
1182 numtrec = i / TP_BSIZE;
1186 * Handle read error.
1189 fprintf(stderr, "Tape read error while ");
1190 switch (curfile.action) {
1192 fprintf(stderr, "trying to set up tape\n");
1195 fprintf(stderr, "trying to resynchronize\n");
1198 fprintf(stderr, "restoring %s\n", curfile.name);
1201 fprintf(stderr, "skipping over inode %ju\n",
1202 (uintmax_t)curfile.ino);
1205 if (!yflag && !reply("continue"))
1207 i = ntrec * TP_BSIZE;
1208 memset(tapebuf, 0, i);
1211 seek_failed = (rmtseek(i, 1) < 0);
1214 seek_failed = (lseek(mt, i, SEEK_CUR) == (off_t)-1);
1218 "continuation failed: %s\n", strerror(errno));
1223 * Handle end of tape.
1226 vprintf(stdout, "End-of-tape encountered\n");
1235 if (rd % TP_BSIZE != 0)
1236 panic("partial block read: %ld should be %ld\n",
1237 rd, ntrec * TP_BSIZE);
1239 memmove(&tapebuf[rd], &endoftapemark, (long)TP_BSIZE);
1241 if (oldnumtrec == 0)
1244 blkcnt -= oldnumtrec;
1246 &tapebuf[(blkcnt++ * TP_BSIZE) + byteslide], (long)TP_BSIZE);
1252 findtapeblksize(void)
1256 for (i = 0; i < ntrec; i++)
1257 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1261 i = rmtread(tapebuf, ntrec * TP_BSIZE);
1264 i = read(mt, tapebuf, ntrec * TP_BSIZE);
1267 fprintf(stderr, "tape read error: %s\n", strerror(errno));
1270 if (i % TP_BSIZE != 0) {
1271 fprintf(stderr, "Tape block size (%ld) %s (%d)\n",
1272 i, "is not a multiple of dump block size", TP_BSIZE);
1275 ntrec = i / TP_BSIZE;
1277 vprintf(stdout, "Tape block size is %ld\n", ntrec);
1299 * Read the next block from the tape.
1300 * If it is not any valid header, return an error.
1303 gethead(struct s_spcl *buf)
1307 readtape((char *)buf);
1308 if (buf->c_magic != FS_UFS2_MAGIC && buf->c_magic != NFS_MAGIC) {
1309 if (buf->c_magic == OFS_MAGIC) {
1311 "Format of dump tape is too old. Must use\n");
1313 "a version of restore from before 2002.\n");
1316 if (swabl(buf->c_magic) != FS_UFS2_MAGIC &&
1317 buf->c_magic != NFS_MAGIC) {
1318 if (buf->c_magic == OFS_MAGIC) {
1320 "Format of dump tape is too old. Must use\n");
1322 "a version of restore from before 2002.\n");
1327 vprintf(stdout, "Note: Doing Byte swapping\n");
1331 if (checksum((int *)buf) == FAIL)
1334 swabst((u_char *)"8l4s1q8l2q17l", (u_char *)buf);
1335 swabst((u_char *)"l",(u_char *) &buf->c_level);
1336 swabst((u_char *)"2l4q",(u_char *) &buf->c_flags);
1340 switch (buf->c_type) {
1345 * Have to patch up missing information in bit map headers
1347 buf->c_size = buf->c_count * TP_BSIZE;
1348 if (buf->c_count > TP_NINDIR)
1351 for (i = 0; i < buf->c_count; i++)
1356 if (buf->c_magic == NFS_MAGIC &&
1357 (buf->c_flags & NFS_DR_NEWINODEFMT) == 0)
1368 * For old dump tapes, have to copy up old fields to
1371 if (buf->c_magic == NFS_MAGIC) {
1372 buf->c_tapea = buf->c_old_tapea;
1373 buf->c_firstrec = buf->c_old_firstrec;
1374 buf->c_date = _time32_to_time(buf->c_old_date);
1375 buf->c_ddate = _time32_to_time(buf->c_old_ddate);
1376 buf->c_atime = _time32_to_time(buf->c_old_atime);
1377 buf->c_mtime = _time32_to_time(buf->c_old_mtime);
1378 buf->c_birthtime = 0;
1379 buf->c_birthtimensec = 0;
1385 panic("gethead: unknown inode type %d\n", buf->c_type);
1388 if (dumpdate != 0 && _time64_to_time(buf->c_date) != dumpdate)
1389 fprintf(stderr, "Header with wrong dumpdate.\n");
1391 * If we're restoring a filesystem with the old (FreeBSD 1)
1392 * format inodes, copy the uid/gid to the new location
1395 buf->c_uid = buf->c_spare1[1];
1396 buf->c_gid = buf->c_spare1[2];
1398 buf->c_magic = FS_UFS2_MAGIC;
1399 tapeaddr = buf->c_tapea;
1406 * Check that a header is where it belongs and predict the next header
1409 accthdr(struct s_spcl *header)
1411 static ino_t previno = 0x7fffffff;
1412 static int prevtype;
1413 static long predict;
1416 if (header->c_type == TS_TAPE) {
1417 fprintf(stderr, "Volume header ");
1418 if (header->c_firstrec)
1419 fprintf(stderr, "begins with record %jd",
1420 (intmax_t)header->c_firstrec);
1421 fprintf(stderr, "\n");
1422 previno = 0x7fffffff;
1425 if (previno == 0x7fffffff)
1429 fprintf(stderr, "Dumped inodes map header");
1432 fprintf(stderr, "Used inodes map header");
1435 fprintf(stderr, "File header, ino %ju", (uintmax_t)previno);
1438 fprintf(stderr, "File continuation header, ino %ju",
1439 (uintmax_t)previno);
1442 fprintf(stderr, "End of tape header");
1445 if (predict != blksread - 1)
1446 fprintf(stderr, "; predicted %ld blocks, got %ld blocks",
1447 predict, blksread - 1);
1448 fprintf(stderr, "\n");
1451 if (header->c_type != TS_END)
1452 for (i = 0; i < header->c_count; i++)
1453 if (readmapflag || header->c_addr[i] != 0)
1457 prevtype = header->c_type;
1458 previno = header->c_inumber;
1462 * Find an inode header.
1463 * Complain if had to skip.
1466 findinode(struct s_spcl *header)
1468 static long skipcnt = 0;
1473 curfile.name = "<name unknown>";
1474 curfile.action = UNKNOWN;
1478 htype = header->c_type;
1483 * Skip up to the beginning of the next record
1485 for (i = 0; i < header->c_count; i++)
1486 if (header->c_addr[i])
1488 while (gethead(header) == FAIL ||
1489 _time64_to_time(header->c_date) != dumpdate) {
1493 if (byteslide < TP_BSIZE) {
1503 curfile.mode = header->c_mode;
1504 curfile.uid = header->c_uid;
1505 curfile.gid = header->c_gid;
1506 curfile.file_flags = header->c_file_flags;
1507 curfile.rdev = header->c_rdev;
1508 curfile.atime_sec = header->c_atime;
1509 curfile.atime_nsec = header->c_atimensec;
1510 curfile.mtime_sec = header->c_mtime;
1511 curfile.mtime_nsec = header->c_mtimensec;
1512 curfile.birthtime_sec = header->c_birthtime;
1513 curfile.birthtime_nsec = header->c_birthtimensec;
1514 curfile.extsize = header->c_extsize;
1515 curfile.size = header->c_size;
1516 curfile.ino = header->c_inumber;
1520 /* If we missed some tapes, get another volume. */
1521 if (tapesread & (tapesread + 1)) {
1525 curfile.ino = maxino;
1529 curfile.name = "<file removal list>";
1533 curfile.name = "<file dump list>";
1538 fprintf(stderr, "unexpected tape header\n");
1540 panic("unexpected tape header\n");
1544 fprintf(stderr, "unknown tape header type %d\n",
1547 panic("unknown tape header type %d\n",
1549 while (gethead(header) == FAIL ||
1550 _time64_to_time(header->c_date) != dumpdate) {
1554 if (byteslide < TP_BSIZE) {
1563 } while (htype == TS_ADDR);
1565 fprintf(stderr, "resync restore, skipped %ld %s\n",
1566 skipcnt, Dflag ? "bytes" : "blocks");
1575 j = sizeof(union u_spcl) / sizeof(int);
1582 /* What happens if we want to read restore tapes
1583 for a 16bit int machine??? */
1589 if (i != CHECKSUM) {
1590 fprintf(stderr, "Checksum error %o, inode %ju file %s\n", i,
1591 (uintmax_t)curfile.ino, curfile.name);
1601 msg(const char *fmt, ...)
1605 (void)vfprintf(stderr, fmt, ap);
1608 #endif /* RRESTORE */
1611 swabshort(u_char *sp, int n)
1616 c = sp[0]; sp[0] = sp[1]; sp[1] = c;
1623 swablong(u_char *sp, int n)
1628 c = sp[0]; sp[0] = sp[3]; sp[3] = c;
1629 c = sp[2]; sp[2] = sp[1]; sp[1] = c;
1636 swabquad(u_char *sp, int n)
1641 c = sp[0]; sp[0] = sp[7]; sp[7] = c;
1642 c = sp[1]; sp[1] = sp[6]; sp[6] = c;
1643 c = sp[2]; sp[2] = sp[5]; sp[5] = c;
1644 c = sp[3]; sp[3] = sp[4]; sp[4] = c;
1651 swabst(u_char *cp, u_char *sp)
1657 case '0': case '1': case '2': case '3': case '4':
1658 case '5': case '6': case '7': case '8': case '9':
1659 n = (n * 10) + (*cp++ - '0');
1662 case 's': case 'w': case 'h':
1665 sp = swabshort(sp, n);
1671 sp = swablong(sp, n);
1677 sp = swabquad(sp, n);
1687 fprintf(stderr, "Unknown conversion character: %c\n",
1700 swabst((u_char *)"l", (u_char *)&x);