2 * Ported to Linux's Second Extended File System as part of the
3 * dump and restore backup suit
4 * Remy Card <card@Linux.EU.Org>, 1994-1997
5 * Stelian Pop <stelian@popies.net>, 1999-2000
6 * Stelian Pop <stelian@popies.net> - AlcĂ´ve <www.alcove.com>, 2000-2002
10 * Copyright (c) 1983, 1993
11 * The Regents of the University of California. All rights reserved.
12 * (c) UNIX System Laboratories, Inc.
13 * All or some portions of this file are derived from material licensed
14 * to the University of California by American Telephone and Telegraph
15 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
16 * the permission of UNIX System Laboratories, Inc.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 3. Neither the name of the University nor the names of its contributors
27 * may be used to endorse or promote products derived from this software
28 * without specific prior written permission.
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 static const char rcsid[] =
45 "$Id: tape.c,v 1.95 2009/12/04 16:29:18 stelian Exp $";
49 #include <compatlfs.h>
50 #include <sys/types.h>
52 #include <compaterr.h>
60 #include <sys/param.h>
68 #ifdef HAVE_EXT2FS_EXT2_FS_H
69 #include <ext2fs/ext2_fs.h>
71 #include <linux/ext2_fs.h>
73 #include <ext2fs/ext2fs.h>
74 #include <bsdcompat.h>
77 #define quad_t int64_t
79 #include <sys/fcntl.h>
80 #include <bsdcompat.h>
82 #include <ufs/ufs/dinode.h>
84 #endif /* __linux__ */
88 #include <protocols/dumprestore.h>
92 #endif /* HAVE_ZLIB */
96 #endif /* HAVE_BZLIB */
100 #endif /* HAVE_LZO */
104 #include "pathnames.h"
107 int noresyncmesg = 0;
109 static long fssize = MAXBSIZE;
112 static int magtapein = 0; /* input is from magtape */
113 static char magtape[MAXPATHLEN];
114 static char magtapeprefix[MAXPATHLEN];
117 static char *tapebuf; /* input buffer for read */
118 static int bufsize; /* buffer size without prefix */
119 static char *tbufptr = NULL; /* active tape buffer */
120 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
121 static char *comprbuf; /* uncompress work buf */
122 static size_t comprlen; /* size including prefix */
124 static union u_spcl endoftapemark;
125 static long blksread; /* blocks read since last header */
126 static long tpblksread = 0; /* TP_BSIZE blocks read */
127 static long tapesread;
128 static sigjmp_buf restart;
129 static int gettingfile = 0; /* restart has a valid frame */
134 static char lnkbuf[MAXPATHLEN + 1];
137 int oldinofmt; /* old inode format conversion required */
138 int Bcvt; /* Swap Bytes (for CCI or sun) */
139 static int Qcvt; /* Swap quads (for sun) */
141 #define FLUSHTAPEBUF() blkcnt = ntrec + 1
143 static void accthdr __P((struct s_spcl *));
144 static int checksum __P((int *));
145 static void findinode __P((struct s_spcl *));
146 static void findtapeblksize __P((void));
147 static int gethead __P((struct s_spcl *));
148 static int converthead __P((struct s_spcl *));
149 static void converttapebuf __P((struct tapebuf *));
150 static void readtape __P((char *));
151 static void setdumpnum __P((void));
153 static void xtrfilefinderinfo __P((char *, size_t));
156 static u_int swabi __P((u_int));
158 static u_long swabl __P((u_long));
160 static u_char *swab64 __P((u_char *, int));
161 static u_char *swab32 __P((u_char *, int));
162 static u_char *swab16 __P((u_char *, int));
163 static void terminateinput __P((void));
164 static void xtrfile __P((char *, size_t));
165 static void xtrlnkfile __P((char *, size_t));
166 static void xtrlnkskip __P((char *, size_t));
167 static void xtrmap __P((char *, size_t));
168 static void xtrmapskip __P((char *, size_t));
169 static void xtrskip __P((char *, size_t));
170 static void xtrxattr __P((char *, size_t));
171 static void setmagtapein __P((void));
172 static int extractattr __P((char *));
173 static void compareattr __P((char *));
175 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
176 static void newcomprbuf __P((int));
177 static void (*readtape_func) __P((char *));
178 static void readtape_set __P((char *));
179 static void readtape_uncompr __P((char *));
180 static void readtape_comprfile __P((char *));
181 static void readtape_comprtape __P((char *));
182 static char *decompress_tapebuf __P((struct tapebuf *, int));
183 static void msg_read_error __P((char *));
185 static int read_a_block __P((int, char *, size_t, long *));
186 #define PREFIXSIZE sizeof(struct tapebuf)
188 #define COMPARE_ONTHEFLY 1
191 static int ifile; /* input file for compare */
192 static int cmperror; /* compare error */
193 static void xtrcmpfile __P((char *, size_t));
194 static void xtrcmpskip __P((char *, size_t));
197 static int readmapflag;
198 static int readingmaps; /* set to 1 while reading the maps */
200 static char xattrbuf[XATTR_MAXSIZE];
204 static DumpFinderInfo gFndrInfo;
208 * Set up an input source. This is called from main.c before setup() is.
211 setinput(char *source)
220 newtapebuf(NTREC > HIGHDENSITYTREC ? NTREC : HIGHDENSITYTREC);
224 if ((n = strchr(source, ':'))) {
225 for (i = 0; i < (n - source); i++) {
226 if (source[i] == '/')
229 if (source[i] != '/') {
231 source = strchr(host, ':');
233 if (rmthost(host) == 0)
238 if (strcmp(source, "-") == 0) {
240 * Since input is coming from a pipe we must establish
241 * our own connection to the terminal.
243 terminal = fopen(_PATH_TTY, "r");
244 if (terminal == NULL) {
245 warn("cannot open %s", _PATH_TTY);
246 terminal = fopen(_PATH_DEVNULL, "r");
247 if (terminal == NULL)
248 err(1, "cannot open %s", _PATH_DEVNULL);
252 setuid(getuid()); /* no longer need or want root privileges */
254 strncpy(magtapeprefix, source, MAXPATHLEN);
255 magtapeprefix[MAXPATHLEN-1] = '\0';
256 snprintf(magtape, MAXPATHLEN, "%s%03d", source, 1);
259 strncpy(magtape, source, MAXPATHLEN);
260 magtape[MAXPATHLEN - 1] = '\0';
264 newtapebuf(long size)
266 static int tapebufsize = -1;
269 bufsize = ntrec * TP_BSIZE;
270 if (size <= tapebufsize)
274 tapebuf = malloc(size * TP_BSIZE + sizeof(struct tapebuf));
276 errx(1, "Cannot allocate space for tape buffer");
280 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
282 newcomprbuf(int size)
284 size_t buf_size = (size+1) * TP_BSIZE + sizeof(struct tapebuf);
285 if (buf_size <= comprlen)
288 if (comprbuf != NULL)
290 comprbuf = malloc(comprlen);
291 if (comprbuf == NULL)
292 errx(1, "Cannot allocate space for decompress buffer");
294 #endif /* HAVE_ZLIB || HAVE_BZLIB || HAVE_LZO */
297 * Verify that the tape drive can be accessed and
298 * that it actually is a dump tape.
303 int i, j, *ip, bot_code;
307 Vprintf(stdout, "Verify tape and initialize maps\n");
308 if (Afile == NULL && bot_script) {
309 msg("Launching %s\n", bot_script);
310 bot_code = system_command(bot_script, magtape, 1);
311 if (bot_code != 0 && bot_code != 1) {
312 msg("Restore aborted by the beginning of tape script\n");
324 mt = rmtopen(temptape, O_RDONLY);
330 mt = OPEN(temptape, O_RDONLY, 0);
332 err(1, "%s", temptape);
338 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
339 readtape_func = readtape_set;
340 #if defined(HAVE_LZO)
341 if (lzo_init() != LZO_E_OK) {
342 msg("internal error - lzo_init failed \n");
350 if (gethead(&spcl) == FAIL) {
351 blkcnt--; /* push back this block */
355 if (gethead(&spcl) == FAIL)
356 errx(1, "Tape is not a dump tape");
357 fprintf(stderr, "Converting to new file system format.\n");
361 fprintf(stderr, "Dump tape is compressed.\n");
362 #if !defined(HAVE_ZLIB) && !defined(HAVE_BZLIB) && !defined(HAVE_LZO)
363 errx(1,"This restore version doesn't support decompression");
364 #endif /* !HAVE_ZLIB && !HAVE_BZLIB */
367 endoftapemark.s_spcl.c_magic = cvtflag ? OFS_MAGIC : NFS_MAGIC;
368 endoftapemark.s_spcl.c_type = TS_END;
369 ip = (int *)&endoftapemark;
370 j = sizeof(union u_spcl) / sizeof(int);
375 endoftapemark.s_spcl.c_checksum = CHECKSUM - i;
377 if (vflag || command == 't' || command == 'C')
380 if (tapeposflag && (unsigned long)spcl.c_date != qfadumpdate)
381 errx(1, "different QFA/dumpdates detected\n");
383 if (filesys[0] == '\0') {
385 strncpy(filesys, spcl.c_filesys, NAMELEN);
386 filesys[NAMELEN - 1] = '\0';
387 dirptr = strstr(filesys, " (dir");
391 dumptime = spcl.c_ddate;
392 dumpdate = spcl.c_date;
393 if (STAT(".", &stbuf) < 0)
394 err(1, "cannot stat .");
395 if (stbuf.st_blksize > 0 && stbuf.st_blksize < TP_BSIZE )
397 if (stbuf.st_blksize >= TP_BSIZE && stbuf.st_blksize <= MAXBSIZE)
398 fssize = stbuf.st_blksize;
399 if (((fssize - 1) & fssize) != 0)
400 errx(1, "bad block size %ld", fssize);
401 if (spcl.c_volume != 1)
402 errx(1, "Tape is not volume 1 of the dump");
403 if (gethead(&spcl) == FAIL) {
404 Dprintf(stdout, "header read failed at %ld blocks\n", (long)blksread);
405 panic("no header after volume mark!\n");
409 if (spcl.c_type != TS_CLRI)
410 errx(1, "Cannot find file removal list");
411 maxino = (spcl.c_count * TP_BSIZE * NBBY) + 1;
412 map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
414 errx(1, "no memory for active inode map");
416 curfile.action = USING;
417 getfile(xtrmap, xtrmapskip);
418 while (spcl.c_type == TS_ADDR) {
419 /* Recompute maxino and the map */
420 dump_ino_t oldmaxino = maxino;
421 maxino += (spcl.c_count * TP_BSIZE * NBBY) + 1;
422 resizemaps(oldmaxino, maxino);
425 spcl.c_dinode.di_size = spcl.c_count * TP_BSIZE;
426 getfile(xtrmap, xtrmapskip);
428 Dprintf(stdout, "maxino = %lu\n", (unsigned long)maxino);
429 if (spcl.c_type != TS_BITS) {
430 if (spcl.c_type == TS_END) {
431 msg("Cannot find file dump list, assuming empty tape\n");
434 errx(1, "Cannot find file dump list");
436 map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
437 if (map == (char *)NULL)
438 errx(1, "no memory for file dump list");
440 curfile.action = USING;
441 getfile(xtrmap, xtrmapskip);
442 while (spcl.c_type == TS_ADDR) {
443 spcl.c_dinode.di_size = spcl.c_count * TP_BSIZE;
444 getfile(xtrmap, xtrmapskip);
447 * If there may be whiteout entries on the tape, pretend that the
448 * whiteout inode exists, so that the whiteout entries can be
452 SETINO(WINO, dumpmap);
458 * Prompt user to load a new dump volume.
459 * "Nextvol" is the next suggested volume to use.
460 * This suggested volume is enforced when doing full
461 * or incremental restores, but can be overridden by
462 * the user when only extracting a subset of the files.
467 long newvol = 0, wantnext = 0, i;
468 long saved_blksread = 0, saved_tpblksread = 0;
469 union u_spcl tmpspcl;
470 # define tmpbuf tmpspcl.s_spcl
472 int haderror = 0, bot_code = 1;
481 panic("Changing volumes on pipe input?\n");
486 saved_blksread = blksread;
487 saved_tpblksread = tpblksread;
488 #if defined(USE_QFA) && defined(sunos)
489 if (createtapeposflag || tapeposflag)
494 exit(1); /* pipes do not get a second chance */
495 if (aflag || curfile.action != SKIP) {
502 while (newvol <= 0) {
503 if (tapesread == 0) {
504 fprintf(stderr, "%s%s%s%s%s",
505 "You have not read any volumes yet.\n",
506 "Unless you know which volume your",
507 " file(s) are on you should start\n",
508 "with the last volume and work",
509 " towards the first.\n");
511 fprintf(stderr, "You have read volumes");
513 for (i = 1; i < 32; i++)
514 if (tapesread & (1 << i)) {
515 fprintf(stderr, "%s%ld", buf, (long)i);
518 fprintf(stderr, "\n");
521 fprintf(stderr, "Specify next volume # (none if no more volumes): ");
522 (void) fflush(stderr);
523 (void) fgets(buf, TP_BSIZE, terminal);
524 } while (!feof(terminal) && buf[0] == '\n');
527 if (!strcmp(buf, "none\n")) {
534 "Volume numbers are positive numerics\n");
537 if (newvol == volno) {
538 tapesread |= 1 << volno;
539 #if defined(USE_QFA) && defined(sunos)
540 if (createtapeposflag || tapeposflag) {
541 if (OpenSMTCmt(magtape) < 0) {
553 * if using an archive file, reset its name so readtape()
554 * could properly use remote access.
559 snprintf(magtape, MAXPATHLEN, "%s%03ld", magtapeprefix, newvol);
560 magtape[MAXPATHLEN - 1] = '\0';
562 if (bot_script && !haderror) {
563 msg("Launching %s\n", bot_script);
564 bot_code = system_command(bot_script, magtape, newvol);
565 if (bot_code != 0 && bot_code != 1) {
566 msg("Restore aborted by the beginning of tape script\n");
570 if (haderror || (bot_code && !Mflag)) {
573 fprintf(stderr, "%d compare errors so far\n", compare_errors);
575 fprintf(stderr, "Mount volume %ld\n", (long)newvol);
577 fprintf(stderr, "Mount tape volume %ld\n", (long)newvol);
579 fprintf(stderr, "Enter ``none'' if there are no more tapes\n");
581 fprintf(stderr, "then enter volume name (default: %s) ", magtape);
583 fprintf(stderr, "otherwise enter tape name (default: %s) ", magtape);
585 (void) fflush(stderr);
586 (void) fgets(buf, TP_BSIZE, terminal);
589 if (!strcmp(buf, "none\n")) {
593 if (buf[0] != '\n') {
595 (void) strncpy(magtape, buf, sizeof(magtape));
596 magtape[sizeof(magtape) - 1] = '\0';
597 if ((pos = strchr(magtape, '\n')))
598 magtape[pos - magtape] = '\0';
601 #if defined(USE_QFA) && defined(sunos)
602 if (createtapeposflag || tapeposflag) {
603 if (OpenSMTCmt(magtape) < 0) {
612 mt = rmtopen(magtape, O_RDONLY);
615 mt = OPEN(magtape, O_RDONLY, 0);
618 fprintf(stderr, "Cannot open %s\n", magtape);
625 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
626 readtape_func = readtape_set;
632 if (gethead(&tmpbuf) == FAIL) {
633 Dprintf(stdout, "header read failed at %ld blocks\n", (long)blksread);
634 fprintf(stderr, "tape is not dump tape\n");
637 blksread = saved_blksread;
638 tpblksread = saved_tpblksread;
641 if (tmpbuf.c_volume != volno) {
642 fprintf(stderr, "Wrong volume (%d)\n", tmpbuf.c_volume);
645 blksread = saved_blksread;
646 tpblksread = saved_tpblksread;
649 if (tmpbuf.c_date != dumpdate || tmpbuf.c_ddate != dumptime) {
650 fprintf(stderr, "Wrong dump date\n\tgot: %s",
652 ctime(&tmpbuf.c_date));
654 ctime4(&tmpbuf.c_date));
656 fprintf(stderr, "\twanted: %s", ctime(&dumpdate));
659 blksread = saved_blksread;
660 tpblksread = saved_tpblksread;
663 tapesread |= 1 << volno;
665 * If continuing from the previous volume, skip over any
666 * blocks read already at the end of the previous volume.
668 * If coming to this volume at random, skip to the beginning
669 * of the next record.
672 fprintf(stderr, "Dump tape is compressed.\n");
673 #if !defined(HAVE_ZLIB) && !defined(HAVE_BZLIB) && !defined(HAVE_LZO)
674 errx(1,"This restore version doesn't support decompression");
675 #endif /* !HAVE_ZLIB && !HAVE_BZLIB */
677 Dprintf(stdout, "read %ld recs, tape starts with %ld\n",
678 tpblksread - 1, (long)tmpbuf.c_firstrec);
679 if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER)) {
681 tpblksread = tmpbuf.c_firstrec + 1;
682 for (i = tmpbuf.c_count; i > 0; i--)
684 } else if (tmpbuf.c_firstrec > 0 &&
685 tmpbuf.c_firstrec < tpblksread - 1) {
687 * -1 since we've read the volume header
689 i = tpblksread - tmpbuf.c_firstrec - 1;
690 Dprintf(stderr, "Skipping %ld duplicate record%s.\n",
691 (long)i, i > 1 ? "s" : "");
696 if (curfile.action == USING) {
698 panic("active file into volume 1\n");
702 * Skip up to the beginning of the next record
704 if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER))
705 for (i = tmpbuf.c_count; i > 0; i--)
707 (void) gethead(&spcl);
711 siglongjmp(restart, 1);
716 * Handle unexpected EOF.
722 if (gettingfile && curfile.action == USING) {
723 printf("Warning: %s %s\n",
724 "End-of-input encountered while extracting", curfile.name);
726 curfile.name = "<name unknown>";
727 curfile.action = UNKNOWN;
729 curfile.ino = maxino;
732 siglongjmp(restart, 1);
737 * handle multiple dumps per tape by skipping forward to the
745 if (dumpnum == 1 || volno != 1)
748 errx(1, "Cannot have multiple dumps on pipe input");
750 tcom.mt_count = dumpnum - 1;
753 if (rmtioctl(MTFSF, dumpnum - 1) < 0) {
754 fprintf(stderr, "rmtioctl MTFSF: %s\n", strerror(errno));
759 if (ioctl(mt, (int)MTIOCTOP, (char *)&tcom) < 0) {
760 fprintf(stderr, "rmtioctl MTFSF: %s\n", strerror(errno));
762 /* warn("ioctl MTFSF"); */
770 Vprintf(stdout, "Dump date: %s", ctime(&spcl.c_date));
771 Vprintf(stdout, "Dumped from: %s",
772 (spcl.c_ddate == 0) ? "the epoch\n" : ctime(&spcl.c_ddate));
773 if (spcl.c_host[0] == '\0')
775 Vprintf(stdout, "Level %d dump of %s on %s:%s\n",
776 spcl.c_level, spcl.c_filesys, spcl.c_host, spcl.c_dev);
777 Vprintf(stdout, "Label: %s\n", spcl.c_label);
779 fprintf(stdout, "Dump date: %s", ctime4(&spcl.c_date));
780 fprintf(stdout, "Dumped from: %s",
781 (spcl.c_ddate == 0) ? "the epoch\n" : ctime4(&spcl.c_ddate));
782 if (spcl.c_host[0] == '\0')
784 fprintf(stdout, "Level %d dump of %s on %s:%s\n",
785 spcl.c_level, spcl.c_filesys, spcl.c_host, spcl.c_dev);
786 fprintf(stdout, "Label: %s\n", spcl.c_label);
795 if (volinfo[1] == ROOTINO) {
796 printf("Starting inode numbers by volume:\n");
797 for (i = 1; i < (int)TP_NINOS && volinfo[i] != 0; ++i)
798 printf("\tVolume %d: %lu\n", i, (unsigned long)volinfo[i]);
804 extractfile(struct entry *ep, int doremove)
808 struct timeval timep[2];
809 char *name = myname(ep);
811 /* If removal is requested (-r mode) do remove it unless
812 * we are extracting a metadata only inode */
813 if (spcl.c_flags & DR_METAONLY) {
814 Vprintf(stdout, "file %s is metadata only\n", name);
819 ep->e_flags &= ~REMOVED;
824 curfile.action = USING;
825 #if defined(__linux__) || defined(sunos)
826 timep[0].tv_sec = curfile.dip->di_atime.tv_sec;
827 timep[0].tv_usec = curfile.dip->di_atime.tv_usec;
828 timep[1].tv_sec = curfile.dip->di_mtime.tv_sec;
829 timep[1].tv_usec = curfile.dip->di_mtime.tv_usec;
830 #else /* __linux__ || sunos */
831 timep[0].tv_sec = curfile.dip->di_atime;
832 timep[0].tv_usec = curfile.dip->di_atimensec / 1000;
833 timep[1].tv_sec = curfile.dip->di_mtime;
834 timep[1].tv_usec = curfile.dip->di_mtimensec / 1000;
835 #endif /* __linux__ */
836 mode = curfile.dip->di_mode;
837 flags = curfile.dip->di_flags;
838 switch (mode & IFMT) {
841 fprintf(stderr, "%s: unknown file mode 0%o\n", name, mode);
846 Vprintf(stdout, "skipped socket %s\n", name);
854 if (ep == NULL || ep->e_flags & EXTRACT)
855 panic("unextracted directory %s\n", name);
859 Vprintf(stdout, "extract dir %s\n", name);
860 ret = genliteraldir(name, curfile.ino);
868 uid_t luid = curfile.dip->di_uid;
869 gid_t lgid = curfile.dip->di_gid;
871 if (! (spcl.c_flags & DR_METAONLY)) {
874 getfile(xtrlnkfile, xtrlnkskip);
877 "%s: zero length symbolic link (ignored)\n", name);
880 if (linkit(lnkbuf, name, SYMLINK) == FAIL)
887 (void) lchown(name, luid, lgid);
895 uid_t luid = curfile.dip->di_uid;
896 gid_t lgid = curfile.dip->di_gid;
898 Vprintf(stdout, "extract fifo %s\n", name);
902 if (! (spcl.c_flags & DR_METAONLY)) {
905 if (mkfifo(name, mode) < 0) {
906 warn("%s: cannot create fifo", name);
910 (void) chown(name, luid, lgid);
911 (void) chmod(name, mode);
916 (void) lsetflags(name, flags);
920 warn("%s: cannot call chflags", name);
921 /* (void) chflags(name, flags); */
924 (void) chflags(name, flags);
932 uid_t luid = curfile.dip->di_uid;
933 gid_t lgid = curfile.dip->di_gid;
934 int lrdev = (int)curfile.dip->di_rdev;
936 Vprintf(stdout, "extract special file %s\n", name);
940 if (! (spcl.c_flags & DR_METAONLY)) {
943 if (mknod(name, mode, lrdev) < 0) {
944 warn("%s: cannot create special file", name);
948 (void) chown(name, luid, lgid);
949 (void) chmod(name, mode);
955 warn("%s: lsetflags called on a special file", name);
956 (void) lsetflags(name, flags);
961 warn("%s: cannot call chflags on a special file", name);
962 /* (void) chflags(name, flags); */
966 warn("%s: chflags called on a special file", name);
967 (void) chflags(name, flags);
975 uid_t luid = curfile.dip->di_uid;
976 gid_t lgid = curfile.dip->di_gid;
978 Vprintf(stdout, "extract file %s\n", name);
983 if (! (spcl.c_flags & DR_METAONLY)) {
986 if ((ofile = OPEN(name, O_WRONLY | O_CREAT | O_TRUNC,
988 warn("%s: cannot create file", name);
992 getfile(xtrfile, xtrskip);
997 (void) chown(name, luid, lgid);
998 (void) chmod(name, mode);
1000 utimes(name, timep);
1003 (void) lsetflags(name, flags);
1007 warn("%s: cannot call chflags", name);
1008 /* (void) chflags(name, flags); */
1011 (void) chflags(name, flags);
1021 extractattr(char *path)
1023 while (spcl.c_flags & DR_EXTATTRIBUTES) {
1024 switch (spcl.c_extattributes) {
1025 case EXT_MACOSFNDRINFO:
1027 (void)extractfinderinfoufs(path);
1029 msg("MacOSX not supported in this version, skipping\n");
1033 case EXT_MACOSRESFORK:
1035 (void)extractresourceufs(path);
1037 msg("MacOSX not supported in this version, skipping\n");
1042 char xattr[XATTR_MAXSIZE];
1044 if (readxattr(xattr) == GOOD) {
1045 xattr_extract(path, xattr);
1050 msg("unexpected inode extension %ld, skipping\n", spcl.c_extattributes);
1060 extractfinderinfoufs(char *name)
1063 char oFileRsrc[MAXPATHLEN];
1066 struct timeval timep[2];
1069 char path[MAXPATHLEN], fname[MAXPATHLEN];
1071 curfile.name = name;
1072 curfile.action = USING;
1073 timep[0].tv_sec = curfile.dip->di_atime.tv_sec;
1074 timep[0].tv_usec = curfile.dip->di_atime.tv_usec;
1075 timep[1].tv_sec = curfile.dip->di_mtime.tv_sec;
1076 timep[1].tv_usec = curfile.dip->di_mtime.tv_usec;
1077 mode = curfile.dip->di_mode;
1078 flags = curfile.dip->di_flags;
1079 uid = curfile.dip->di_uid;
1080 gid = curfile.dip->di_gid;
1082 switch (mode & IFMT) {
1085 fprintf(stderr, "%s: (extr. finfoufs) unknown file mode 0%o\n", name, mode);
1090 fprintf(stderr, "%s: (extr. finfoufs[IFDIR]) unknown file mode 0%o\n", name, mode);
1099 Vprintf(stdout, "extract finderinfo file %s\n", name);
1104 getfile(xtrfilefinderinfo, xtrskip);
1106 GetPathFile(name, path, fname);
1107 strcpy(oFileRsrc, path);
1108 strcat(oFileRsrc, "._");
1109 strcat(oFileRsrc, fname);
1111 if ((err = CreateAppleDoubleFileRes(oFileRsrc, &gFndrInfo.fndrinfo,
1112 mode, flags, timep, uid, gid)) != 0) {
1113 fprintf(stderr, "%s: cannot create finderinfo: %s\n",
1114 name, strerror(errno));
1125 extractresourceufs(char *name)
1127 char oFileRsrc[MAXPATHLEN];
1130 struct timeval timep[2];
1131 char path[MAXPATHLEN], fname[MAXPATHLEN];
1141 curfile.name = name;
1142 curfile.action = USING;
1143 timep[0].tv_sec = curfile.dip->di_atime.tv_sec;
1144 timep[0].tv_usec = curfile.dip->di_atime.tv_usec;
1145 timep[1].tv_sec = curfile.dip->di_mtime.tv_sec;
1146 timep[1].tv_usec = curfile.dip->di_mtime.tv_usec;
1147 mode = curfile.dip->di_mode;
1148 flags = curfile.dip->di_flags;
1149 uid = curfile.dip->di_uid;
1150 gid = curfile.dip->di_gid;
1151 di_size = curfile.dip->di_size;
1153 switch (mode & IFMT) {
1156 fprintf(stderr, "%s: (extr. resufs) unknown file mode 0%o\n", name, mode);
1161 fprintf(stderr, "%s: (extr. resufs [IFDIR]) unknown file mode 0%o\n", name, mode);
1170 Vprintf(stdout, "extract resource file %s\n", name);
1176 GetPathFile(name, path, fname);
1177 strcpy(oFileRsrc, path);
1178 strcat(oFileRsrc, "._");
1179 strcat(oFileRsrc, fname);
1181 if ((ofile = open(oFileRsrc, O_RDONLY, 0)) < 0) {
1182 fprintf(stderr, "%s: cannot read finderinfo: %s\n",
1183 name, strerror(errno));
1187 read(ofile, buf, 70);
1188 (void) close(ofile);
1190 hp = (ASDHeaderPtr)p;
1193 p += sizeof(ASDHeader) - CORRECT;
1194 ep = (ASDEntryPtr)p;
1195 /* the finderinfo entry */
1196 ep->offset += sizeof(ASDEntry);
1199 p += sizeof(ASDEntry);
1200 /* the finderinfo data */
1201 bcopy(p, p + sizeof(ASDEntry), INFOLEN);
1202 ep = (ASDEntryPtr)p;
1203 /* the new resourcefork entry */
1204 ep->entryID = EntryRSRCFork;
1205 ep->offset = loff + INFOLEN;
1207 /* write the new appledouble entries to the file */
1208 if ((ofile = open(oFileRsrc, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
1209 fprintf(stderr, "%s: cannot create resource file: %s\n",
1210 name, strerror(errno));
1214 write(ofile, buf, 70 + sizeof(ASDEntry));
1215 /* and add the resource data from tape */
1216 getfile(xtrfile, xtrskip);
1218 (void) fchown(ofile, uid, gid);
1219 (void) fchmod(ofile, mode);
1220 (void) close(ofile);
1221 utimes(oFileRsrc, timep);
1222 (void) lsetflags(oFileRsrc, flags);
1227 #endif /* DUMP_MACOSX */
1230 readxattr(char *buffer)
1233 msg("reading EA data for inode %lu\n", curfile.ino);
1235 curfile.name = "EA block";
1236 if (curfile.dip->di_size > XATTR_MAXSIZE) {
1237 fprintf(stderr, "EA size too big (%ld)", (long)curfile.dip->di_size);
1242 memset(xattrbuf, 0, XATTR_MAXSIZE);
1245 getfile(xtrxattr, xtrnull);
1247 memcpy(buffer, xattrbuf, XATTR_MAXSIZE);
1253 * skip over bit maps on the tape
1259 while (spcl.c_type == TS_BITS || spcl.c_type == TS_CLRI)
1264 * skip over a file on the tape
1270 curfile.action = SKIP;
1271 getfile(xtrnull, xtrnull);
1275 * skip over any extended attributes.
1281 while (spcl.c_flags & DR_EXTATTRIBUTES)
1286 * Extract a file from the tape.
1287 * When an allocated block is found it is passed to the fill function;
1288 * when an unallocated block (hole) is found, a zeroed buffer is passed
1289 * to the skip function.
1292 getfile(void (*fill) __P((char *, size_t)), void (*skip) __P((char *, size_t)))
1295 volatile int curblk = 0;
1296 volatile quad_t size = spcl.c_dinode.di_size;
1297 volatile int last_write_was_hole = 0;
1298 quad_t origsize = size;
1299 static char clearedbuf[MAXBSIZE];
1300 char buf[MAXBSIZE / TP_BSIZE][TP_BSIZE];
1301 char junk[TP_BSIZE];
1303 if (spcl.c_type == TS_END)
1304 panic("ran off end of tape\n");
1305 if (spcl.c_magic != NFS_MAGIC)
1306 panic("not at beginning of a file\n");
1307 if (!gettingfile && setjmp(restart) != 0)
1311 for (i = 0; i < spcl.c_count; i++) {
1312 if (readmapflag || spcl.c_addr[i]) {
1313 readtape(&buf[curblk++][0]);
1314 if (curblk == fssize / TP_BSIZE) {
1315 (*fill)((char *)buf, (size_t)(size > TP_BSIZE ?
1316 fssize : (curblk - 1) * TP_BSIZE + size));
1318 last_write_was_hole = 0;
1322 (*fill)((char *)buf, (size_t)(size > TP_BSIZE ?
1324 (curblk - 1) * TP_BSIZE + size));
1327 (*skip)(clearedbuf, (long)(size > TP_BSIZE ?
1329 last_write_was_hole = 1;
1331 if ((size -= TP_BSIZE) <= 0) {
1332 for (i++; i < spcl.c_count; i++)
1333 if (readmapflag || spcl.c_addr[i])
1338 while (gethead(&spcl) != GOOD) {
1339 fprintf(stderr, "Incorrect block for %s at %ld blocks\n",
1340 curfile.name, (long)blksread);
1343 if (spcl.c_type == TS_ADDR)
1346 "Missing address (header) block for %s at %ld blocks\n",
1347 curfile.name, (long)blksread);
1350 (*fill)((char *)buf, (size_t)((curblk * TP_BSIZE) + size));
1351 last_write_was_hole = 0;
1354 fprintf(stderr, "Missing blocks at the end of %s, assuming hole\n", curfile.name);
1356 size_t skp = size > TP_BSIZE ? TP_BSIZE : size;
1357 (*skip)(clearedbuf, skp);
1360 last_write_was_hole = 1;
1362 if (last_write_was_hole) {
1363 FTRUNCATE(ofile, origsize);
1371 * Write out the next block of a file.
1374 xtrfile(char *buf, size_t size)
1379 if (write(ofile, buf, (int) size) == -1)
1380 err(1, "write error extracting inode %lu, name %s\nwrite",
1381 (unsigned long)curfile.ino, curfile.name);
1386 xtrfilefinderinfo(char *buf, size_t size)
1388 bcopy(buf, &gFndrInfo, size);
1390 #endif /* DUMP_MACOSX */
1393 * Skip over a hole in a file.
1397 xtrskip(UNUSED(char *buf), size_t size)
1400 if (LSEEK(ofile, (OFF_T)size, SEEK_CUR) == -1)
1401 err(1, "seek error extracting inode %lu, name %s\nlseek",
1402 (unsigned long)curfile.ino, curfile.name);
1406 * Collect the next block of a symbolic link.
1409 xtrlnkfile(char *buf, size_t size)
1413 if (pathlen > MAXPATHLEN) {
1414 buf[size - 1] = '\0';
1415 errx(1, "symbolic link name: %s->%s%s; too long %d",
1416 curfile.name, lnkbuf, buf, pathlen);
1418 (void) strcat(lnkbuf, buf);
1419 lnkbuf[pathlen] = '\0';
1423 * Skip over a hole in a symbolic link (should never happen).
1427 xtrlnkskip(UNUSED(char *buf), UNUSED(size_t size))
1430 errx(1, "unallocated block in symbolic link %s", curfile.name);
1434 * Collect the next block of a bit map.
1437 xtrmap(char *buf, size_t size)
1440 memmove(map, buf, size);
1445 * Skip over a hole in a bit map (should never happen).
1449 xtrmapskip(UNUSED(char *buf), size_t size)
1452 panic("hole in map\n");
1457 * Noop, when an extraction function is not needed.
1461 xtrnull(UNUSED(char *buf), UNUSED(size_t size))
1467 #if COMPARE_ONTHEFLY
1469 * Compare the next block of a file.
1472 xtrcmpfile(char *buf, size_t size)
1474 static char cmpbuf[MAXBSIZE];
1479 if (read(ifile, cmpbuf, size) != (ssize_t)size) {
1480 fprintf(stderr, "%s: size has changed.\n",
1486 if (memcmp(buf, cmpbuf, size) != 0) {
1487 fprintf(stderr, "%s: tape and disk copies are different\n",
1495 * Skip over a hole in a file.
1498 xtrcmpskip(UNUSED(char *buf), size_t size)
1500 static char cmpbuf[MAXBSIZE];
1506 if (read(ifile, cmpbuf, size) != (ssize_t)size) {
1507 fprintf(stderr, "%s: size has changed.\n",
1513 for (i = 0; i < (int)size; ++i)
1514 if (cmpbuf[i] != '\0') {
1515 fprintf(stderr, "%s: tape and disk copies are different\n",
1521 #endif /* COMPARE_ONTHEFLY */
1524 xtrxattr(char *buf, size_t size)
1526 if (xattrlen + size > XATTR_MAXSIZE) {
1527 fprintf(stderr, "EA size too big (%ld)", (long)xattrlen + size);
1530 memcpy(xattrbuf + xattrlen, buf, size);
1534 #if !COMPARE_ONTHEFLY
1536 do_cmpfiles(int fd_tape, int fd_disk, OFF_T size)
1538 static char buf_tape[BUFSIZ];
1539 static char buf_disk[BUFSIZ];
1544 if ((n_tape = read(fd_tape, buf_tape, sizeof(buf_tape))) < 1) {
1545 close(fd_tape), close(fd_disk);
1546 panic("do_cmpfiles: unexpected EOF[1]");
1548 if ((n_disk = read(fd_disk, buf_disk, sizeof(buf_tape))) < 1) {
1549 close(fd_tape), close(fd_disk);
1550 panic("do_cmpfiles: unexpected EOF[2]");
1552 if (n_tape != n_disk) {
1553 close(fd_tape), close(fd_disk);
1554 panic("do_cmpfiles: sizes different!");
1556 if (memcmp(buf_tape, buf_disk, (size_t)n_tape) != 0) return (1);
1562 /* for debugging compare problems */
1563 #undef COMPARE_FAIL_KEEP_FILE
1566 #ifdef COMPARE_FAIL_KEEP_FILE
1567 /* return true if tapefile should be unlinked after compare */
1572 cmpfiles(char *tapefile, char *diskfile, struct STAT *sbuf_disk)
1574 struct STAT sbuf_tape;
1575 int fd_tape, fd_disk;
1577 if (STAT(tapefile, &sbuf_tape) != 0) {
1578 panic("can't lstat tmp file %s: %s\n", tapefile,
1583 if (sbuf_disk->st_size != sbuf_tape.st_size) {
1585 "%s: size changed from %lld to %lld.\n",
1586 diskfile, (long long)sbuf_tape.st_size, (long long)sbuf_disk->st_size);
1588 #ifdef COMPARE_FAIL_KEEP_FILE
1595 if ((fd_tape = OPEN(tapefile, O_RDONLY)) < 0) {
1596 panic("can't open %s: %s\n", tapefile, strerror(errno));
1599 if ((fd_disk = OPEN(diskfile, O_RDONLY)) < 0) {
1601 panic("can't open %s: %s\n", diskfile, strerror(errno));
1605 if (do_cmpfiles(fd_tape, fd_disk, sbuf_tape.st_size)) {
1606 fprintf(stderr, "%s: tape and disk copies are different\n",
1611 #ifdef COMPARE_FAIL_KEEP_FILE
1612 /* rename the file to live in /tmp */
1613 /* rename `tapefile' to /tmp/<basename of diskfile> */
1615 char *p = strrchr(diskfile, '/');
1616 char newname[MAXPATHLEN];
1618 panic("can't find / in %s\n", diskfile);
1620 snprintf(newname, sizeof(newname), "%s/debug/%s", tmpdir, p + 1);
1621 if (rename(tapefile, newname)) {
1622 panic("rename from %s to %s failed: %s\n",
1626 fprintf(stderr, "*** %s saved to %s\n",
1631 /* don't unlink the file (it's not there anymore */
1640 #ifdef COMPARE_FAIL_KEEP_FILE
1644 #endif /* !COMPARE_ONTHEFLY */
1647 compareattr(char *name)
1651 while (spcl.c_flags & DR_EXTATTRIBUTES) {
1652 switch (spcl.c_extattributes) {
1653 case EXT_MACOSFNDRINFO:
1654 msg("MacOSX not supported for comparision in this version, skipping\n");
1657 case EXT_MACOSRESFORK:
1658 msg("MacOSX not supported for comparision in this version, skipping\n");
1662 char xattr[XATTR_MAXSIZE];
1664 if (readxattr(xattr) == GOOD) {
1665 if (xattr_compare(name, xattr) == FAIL)
1674 msg("unexpected inode extension %ld, skipping\n", spcl.c_extattributes);
1679 if (!xattr_done && xattr_compare(name, NULL) == FAIL)
1683 #if !COMPARE_ONTHEFLY
1684 static char tmpfilename[MAXPATHLEN];
1688 comparefile(char *name)
1694 unsigned long newflags;
1697 #if !COMPARE_ONTHEFLY
1698 static char *tmpfile = NULL;
1701 curfile.name = name;
1702 curfile.action = USING;
1703 mode = curfile.dip->di_mode;
1704 flags = curfile.dip->di_flags;
1705 uid = curfile.dip->di_uid;
1706 gid = curfile.dip->di_gid;
1708 if ((mode & IFMT) == IFSOCK) {
1709 Vprintf(stdout, "skipped socket %s\n", name);
1714 if ((r = LSTAT(name, &sb)) != 0) {
1715 warn("unable to stat %s", name);
1721 Vprintf(stdout, "comparing %s (size: %lld, mode: 0%o)\n", name,
1722 (long long)sb.st_size, mode);
1724 if (sb.st_mode != mode) {
1725 fprintf(stderr, "%s: mode changed from 0%o to 0%o.\n",
1726 name, mode & 07777, sb.st_mode & 07777);
1729 if (sb.st_uid != uid) {
1730 fprintf(stderr, "%s: uid changed from %d to %d.\n",
1731 name, uid, sb.st_uid);
1734 if (sb.st_gid != gid) {
1735 fprintf(stderr, "%s: gid changed from %d to %d.\n",
1736 name, gid, sb.st_gid);
1740 if (lgetflags(name, &newflags) < 0) {
1742 warn("%s: lgetflags failed", name);
1747 if (newflags != flags) {
1748 fprintf(stderr, "%s: flags changed from 0x%08x to 0x%08lx.\n",
1749 name, flags, newflags);
1754 if (spcl.c_flags & DR_METAONLY) {
1758 switch (mode & IFMT) {
1773 char lbuf[MAXPATHLEN + 1];
1776 if (!(sb.st_mode & S_IFLNK)) {
1777 fprintf(stderr, "%s: is no longer a symbolic link\n",
1784 getfile(xtrlnkfile, xtrlnkskip);
1787 "%s: zero length symbolic link (ignored)\n",
1792 if ((lsize = readlink(name, lbuf, MAXPATHLEN)) < 0) {
1793 panic("readlink of %s failed: %s\n", name,
1798 if (strcmp(lbuf, lnkbuf) != 0) {
1800 "%s: symbolic link changed from %s to %s.\n",
1801 name, lnkbuf, lbuf);
1811 if (!(sb.st_mode & (S_IFCHR|S_IFBLK))) {
1812 fprintf(stderr, "%s: no longer a special file\n",
1819 if (sb.st_rdev != (dev_t)curfile.dip->di_rdev) {
1821 "%s: device changed from %d,%d to %d,%d.\n",
1823 major(curfile.dip->di_rdev),
1824 minor(curfile.dip->di_rdev),
1834 #if COMPARE_ONTHEFLY
1835 if ((ifile = OPEN(name, O_RDONLY)) < 0) {
1836 warn("can't open %s", name);
1842 getfile(xtrcmpfile, xtrcmpskip);
1845 if (read(ifile, &c, 1) != 0) {
1846 fprintf(stderr, "%s: size has changed.\n",
1856 if (tmpfile == NULL) {
1857 /* argument to mktemp() must not be in RO space: */
1858 snprintf(tmpfilename, sizeof(tmpfilename), "%s/restoreCXXXXXX", tmpdir);
1859 tmpfile = mktemp(&tmpfilename[0]);
1861 if ((STAT(tmpfile, &stemp) == 0) && (unlink(tmpfile) != 0)) {
1862 panic("cannot delete tmp file %s: %s\n",
1863 tmpfile, strerror(errno));
1865 if ((ofile = OPEN(tmpfile, O_WRONLY | O_CREAT | O_TRUNC, 0600)) < 0) {
1866 panic("cannot create file temp file %s: %s\n",
1867 name, strerror(errno));
1869 getfile(xtrfile, xtrskip);
1870 (void) close(ofile);
1871 #ifdef COMPARE_FAIL_KEEP_FILE
1872 if (cmpfiles(tmpfile, name, &sb))
1875 cmpfiles(tmpfile, name, &sb);
1878 #endif /* COMPARE_ONTHEFLY */
1885 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
1886 static void (*readtape_func)(char *) = readtape_set;
1889 * Read TP_BSIZE blocks from the input.
1890 * Handle read errors, and end of media.
1891 * Decompress compressed blocks.
1896 (*readtape_func)(buf); /* call the actual processing routine */
1900 * Set function pointer for readtape() routine. zflag and magtapein must
1901 * be correctly set before the first call to readtape().
1904 readtape_set(char *buf)
1907 readtape_func = readtape_uncompr;
1911 readtape_func = readtape_comprtape;
1913 readtape_func = readtape_comprfile;
1918 #endif /* HAVE_ZLIB || HAVE_BZLIB || HAVE_LZO */
1921 * This is the original readtape(), it's used for reading uncompressed input.
1922 * Read TP_BSIZE blocks from the input.
1923 * Handle read errors, and end of media.
1926 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
1927 readtape_uncompr(char *buf)
1932 ssize_t rd, newvol, i;
1933 int cnt, seek_failed;
1935 if (blkcnt < numtrec) {
1936 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1942 for (i = 0; i < ntrec; i++)
1943 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1946 cnt = ntrec * TP_BSIZE;
1949 if (createtapeposflag)
1950 (void)GetTapePos(&curtapepos);
1955 i = rmtread(&tapebuf[rd], cnt);
1958 i = read(mt, &tapebuf[rd], cnt);
1961 * Check for mid-tape short read error.
1962 * If found, skip rest of buffer and start with the next.
1964 if (!pipein && numtrec < ntrec && i > 0) {
1965 Dprintf(stdout, "mid-media short read error.\n");
1969 * Handle partial block read.
1971 if (pipein && i == 0 && rd > 0)
1973 else if (i > 0 && i != ntrec * TP_BSIZE) {
1982 * Short read. Process the blocks read.
1984 if (i % TP_BSIZE != 0)
1986 "partial block read: %ld should be %ld\n",
1987 (long)i, ntrec * TP_BSIZE);
1988 numtrec = i / TP_BSIZE;
1992 * Handle read error.
1995 fprintf(stderr, "Tape read error while ");
1996 switch (curfile.action) {
1998 fprintf(stderr, "trying to set up tape\n");
2001 fprintf(stderr, "trying to resynchronize\n");
2004 fprintf(stderr, "restoring %s\n", curfile.name);
2007 fprintf(stderr, "skipping over inode %lu\n",
2008 (unsigned long)curfile.ino);
2011 if (!yflag && !reply("continue"))
2013 i = ntrec * TP_BSIZE;
2014 memset(tapebuf, 0, (size_t)i);
2017 seek_failed = (rmtseek(i, 1) < 0);
2020 seek_failed = (LSEEK(mt, i, SEEK_CUR) == (OFF_T)-1);
2023 warn("continuation failed");
2024 if (!yflag && !reply("assume end-of-tape and continue"))
2030 * Handle end of tape.
2033 Vprintf(stdout, "End-of-tape encountered\n");
2042 if (rd % TP_BSIZE != 0)
2043 panic("partial block read: %d should be %d\n",
2044 rd, ntrec * TP_BSIZE);
2046 memmove(&tapebuf[rd], &endoftapemark, TP_BSIZE);
2049 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
2054 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
2057 * Read a compressed format block from a file or pipe and uncompress it.
2058 * Attempt to handle read errors, and end of file.
2061 readtape_comprfile(char *buf)
2063 long rl, size, i, ret;
2065 struct tapebuf *tpb;
2067 if (blkcnt < numtrec) {
2068 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
2073 /* need to read the next block */
2075 for (i = 0; i < ntrec; i++)
2076 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
2078 tpb = (struct tapebuf *) tapebuf;
2080 /* read the block prefix */
2081 ret = read_a_block(mt, tapebuf, PREFIXSIZE, &rl);
2082 converttapebuf(tpb);
2084 if (Vflag && (ret == 0 || rl < (int)PREFIXSIZE || tpb->length == 0))
2091 if (size > bufsize) {
2092 /* something's wrong */
2093 Vprintf(stdout, "Prefix size error, max size %d, got %ld\n",
2096 tpb->length = bufsize;
2098 ret = read_a_block(mt, tpb->buf, size, &rl);
2102 tbufptr = decompress_tapebuf(tpb, rl + PREFIXSIZE);
2103 if (tbufptr == NULL) {
2104 msg_read_error("File decompression error while");
2105 if (!yflag && !reply("continue"))
2107 memset(tapebuf, 0, bufsize);
2112 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
2118 /* Errors while reading from a file or pipe are catastrophic. Since
2119 * there are no block boundaries, it's impossible to bypass the
2120 * block in error and find the start of the next block.
2123 /* It's possible to have multiple input files using -M
2124 * and -f file1,file2...
2126 Vprintf(stdout, "End-of-File encountered\n");
2136 msg_read_error("Read error while");
2137 /* if (!yflag && !reply("continue")) */
2142 * Read compressed data from a tape and uncompress it.
2143 * Handle read errors, and end of media.
2144 * Since a tape consists of separate physical blocks, we try
2145 * to recover from errors by repositioning the tape to the next
2149 readtape_comprtape(char *buf)
2153 struct tapebuf *tpb;
2156 if (blkcnt < numtrec) {
2157 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
2162 /* need to read the next block */
2164 for (i = 0; i < ntrec; i++)
2165 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
2167 tpb = (struct tapebuf *) tapebuf;
2169 /* read the block */
2170 size = bufsize + PREFIXSIZE;
2171 ret = read_a_block(mt, tapebuf, size, &rl);
2175 converttapebuf(tpb);
2176 tbufptr = decompress_tapebuf(tpb, rl);
2177 if (tbufptr == NULL) {
2178 msg_read_error("Tape decompression error while");
2179 if (!yflag && !reply("continue"))
2181 memset(tapebuf, 0, PREFIXSIZE + bufsize);
2187 /* Handle errors: EOT switches to the next volume, other errors
2188 * attempt to position the tape to the next block.
2191 Vprintf(stdout, "End-of-tape encountered\n");
2200 msg_read_error("Tape read error while");
2201 if (!yflag && !reply("continue"))
2203 memset(tapebuf, 0, PREFIXSIZE + bufsize);
2208 rl = rmtioctl(MTFSR, 1);
2214 rl = ioctl(mt, MTIOCTOP, &tcom);
2218 warn("continuation failed");
2219 if (!yflag && !reply("assume end-of-tape and continue"))
2221 ret = 0; /* end of tape */
2227 memmove(buf, &tbufptr[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
2233 * Decompress a struct tapebuf into a buffer. readsize is the size read
2234 * from the tape/file and is used for error messages. Returns a pointer
2235 * to the location of the uncompressed buffer or NULL on errors.
2236 * Adjust numtrec and complain for a short block.
2239 decompress_tapebuf(struct tapebuf *tpbin, int readsize)
2241 /* If zflag is on, all blocks have a struct tapebuf prefix */
2242 /* zflag gets set in setup() from the dump header */
2243 int cresult, blocklen;
2244 unsigned long worklen;
2245 char *output = NULL,*reason = NULL, *lengtherr = NULL;
2247 /* build a length error message */
2248 blocklen = tpbin->length;
2249 if (readsize < blocklen + (int)PREFIXSIZE)
2250 lengtherr = "short";
2252 if (readsize > blocklen + (int)PREFIXSIZE)
2257 if (tpbin->compressed) {
2258 /* uncompress whatever we read, if it fails, complain later */
2259 if (tpbin->flags == COMPRESS_ZLIB) {
2261 errx(1,"This restore version doesn't support zlib decompression");
2263 cresult = uncompress(comprbuf, &worklen,
2264 tpbin->buf, blocklen);
2270 reason = "not enough memory";
2273 reason = "buffer too small";
2276 reason = "data error";
2281 if (cresult == Z_OK)
2285 #endif /* HAVE_ZLIB */
2287 if (tpbin->flags == COMPRESS_BZLIB) {
2289 errx(1,"This restore version doesn't support bzlib decompression");
2291 unsigned int worklen2 = worklen;
2292 cresult = BZ2_bzBuffToBuffDecompress(
2293 comprbuf, &worklen2,
2294 tpbin->buf, blocklen, 0, 0);
2301 reason = "not enough memory";
2303 case BZ_OUTBUFF_FULL:
2304 reason = "buffer too small";
2307 case BZ_DATA_ERROR_MAGIC:
2308 case BZ_UNEXPECTED_EOF:
2309 reason = "data error";
2314 if (cresult == BZ_OK)
2318 #endif /* HAVE_BZLIB */
2320 if (tpbin->flags == COMPRESS_LZO) {
2322 errx(1,"This restore version doesn't support lzo decompression");
2324 lzo_uint worklen2 = worklen;
2325 cresult = lzo1x_decompress(tpbin->buf, blocklen,
2326 comprbuf, &worklen2, NULL);
2333 case LZO_E_EOF_NOT_FOUND:
2334 reason = "data error";
2339 if (cresult == LZO_E_OK)
2343 #endif /* HAVE_LZO */
2347 output = tpbin->buf;
2351 numtrec = worklen / TP_BSIZE;
2352 if (worklen % TP_BSIZE != 0)
2353 reason = "length mismatch";
2357 fprintf(stderr, "%s compressed block: %d expected: %u\n",
2358 lengtherr, readsize, tpbin->length + PREFIXSIZE);
2359 fprintf(stderr, "decompression error, block %ld: %s\n",
2360 tpblksread+1, reason);
2368 * Print an error message for a read error.
2369 * This was exteracted from the original readtape().
2372 msg_read_error(char *m)
2374 switch (curfile.action) {
2376 fprintf(stderr, "%s trying to set up tape\n", m);
2379 fprintf(stderr, "%s trying to resynchronize\n", m);
2382 fprintf(stderr, "%s restoring %s\n", m, curfile.name);
2385 fprintf(stderr, "%s skipping over inode %lu\n", m,
2386 (unsigned long)curfile.ino);
2390 #endif /* HAVE_ZLIB || HAVE_BZLIB || HAVE_LZO */
2393 * Read the first block and get the blocksize from it. Test
2394 * for a compressed dump tape/file. setup() will make the final
2395 * determination by checking the compressed flag if gethead()
2396 * finds a valid header. The test here is necessary to offset the buffer
2397 * by the size of the compressed prefix. zflag is set here so that
2398 * readtape_set can set the correct function pointer for readtape().
2399 * Note that the first block of each tape/file is not compressed
2400 * and does not have a prefix.
2403 findtapeblksize(void)
2407 struct tapebuf *tpb = (struct tapebuf *) tapebuf;
2408 struct s_spcl spclpt;
2410 for (i = 0; i < ntrec; i++)
2411 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
2415 * For a pipe or file, read in the first record. For a tape, read
2418 len = magtapein ? ntrec * TP_BSIZE : TP_BSIZE;
2420 if (read_a_block(mt, tapebuf, len, &i) <= 0)
2421 errx(1, "Tape read error on first record");
2423 memcpy(&spclpt, tapebuf, TP_BSIZE);
2425 if (converthead(&spclpt) == FAIL) {
2427 if (converthead(&spclpt) == FAIL) {
2428 /* Special case for old compressed tapes with prefix */
2429 if (magtapein && (i % TP_BSIZE != 0))
2431 errx(1, "Tape is not a dump tape");
2433 fprintf(stderr, "Converting to new file system format.\n");
2436 * If the input is from a file or a pipe, we read TP_BSIZE
2437 * bytes looking for a dump header. If the dump is compressed
2438 * we need to read in the rest of the block, as determined
2439 * by c_ntrec in the dump header. The first block of the
2440 * dump is not compressed and does not have a prefix.
2443 if (spclpt.c_type == TS_TAPE
2444 && spclpt.c_flags & DR_COMPRESSED) {
2445 /* It's a compressed dump file, read in the */
2446 /* rest of the block based on spclpt.c_ntrec. */
2447 if (spclpt.c_ntrec > ntrec)
2448 errx(1, "Tape blocksize is too large, use "
2449 "\'-b %d\' ", spclpt.c_ntrec);
2450 ntrec = spclpt.c_ntrec;
2451 len = (ntrec - 1) * TP_BSIZE;
2455 /* read in the rest of the block based on bufsize */
2456 len = bufsize - TP_BSIZE;
2458 if (read_a_block(mt, tapebuf+TP_BSIZE, len, &i) < 0
2459 || (i != (long)len && i % TP_BSIZE != 0))
2460 errx(1,"Error reading dump file header");
2463 Vprintf(stdout, "Input block size is %ld\n", ntrec);
2465 } /* if (!magtapein) */
2468 * If the input is a tape, we tried to read ntrec * TP_BSIZE bytes.
2469 * If the value of ntrec is too large, we read less than
2470 * what we asked for; adjust the value of ntrec and test for
2471 * a compressed dump tape.
2473 if (i % TP_BSIZE != 0) {
2475 /* may be old format compressed dump tape with a prefix */
2476 memcpy(&spclpt, tpb->buf, TP_BSIZE);
2478 if (converthead(&spclpt) == FAIL) {
2480 if (converthead(&spclpt) == FAIL)
2481 errx(1, "Tape is not a dump tape");
2482 fprintf(stderr, "Converting to new file system format.\n");
2484 if (i % TP_BSIZE == PREFIXSIZE
2485 && tpb->compressed == 0
2486 && spclpt.c_type == TS_TAPE
2487 && spclpt.c_flags & DR_COMPRESSED) {
2490 if (tpb->length > bufsize)
2491 errx(1, "Tape blocksize is too large, use "
2492 "\'-b %d\' ", tpb->length / TP_BSIZE);
2495 errx(1, "Tape block size (%ld) is not a multiple of dump block size (%d)",
2498 ntrec = i / TP_BSIZE;
2499 if (spclpt.c_type == TS_TAPE) {
2500 if (spclpt.c_flags & DR_COMPRESSED)
2502 if (spclpt.c_ntrec > ntrec)
2503 errx(1, "Tape blocksize is too large, use "
2504 "\'-b %d\' ", spclpt.c_ntrec);
2507 Vprintf(stdout, "Tape block size is %ld\n", ntrec);
2511 * Read a block of data handling all of the messy details.
2513 static int read_a_block(int fd, char *buf, size_t len, long *lengthread)
2521 i = rmtread(buf, size);
2524 i = read(fd, buf, size);
2527 break; /* EOD or error */
2530 break; /* block at a time for mt */
2533 *lengthread = len - size;
2552 setmagtapein(void) {
2553 struct mtget mt_stat;
2554 static int done = 0;
2559 /* need to know if input is really from a tape */
2565 magtapein = ioctl(mt, MTIOCGET, (char *)&mt_stat) == 0;
2568 Vprintf(stdout,"Input is from a %s %s\n",
2569 host ? "remote" : "local",
2570 magtapein ? "tape" :
2571 Vflag ? "multi-volume (no tape)" : "file/pipe");
2575 * Read the next block from the tape.
2576 * Check to see if it is one of several vintage headers.
2577 * If it is an old style header, convert it to a new style header.
2578 * If it is not any valid header, return an error.
2581 gethead(struct s_spcl *buf)
2583 readtape((char *)buf);
2584 return converthead(buf);
2588 converthead(struct s_spcl *buf)
2596 char dummy[TP_BSIZE];
2603 u_int16_t c_inumber;
2608 u_int16_t odi_nlink;
2624 if (buf->c_magic != NFS_MAGIC) {
2625 if (swabi(buf->c_magic) != NFS_MAGIC)
2628 Vprintf(stdout, "Note: Doing Byte swapping\n");
2632 if (checksum((int *)buf) == FAIL)
2635 swabst((u_char *)"8i4s1l29i528bi192b4i", (u_char *)buf);
2638 memcpy(&u_ospcl.s_ospcl, buf, TP_BSIZE);
2639 if (checksum((int *)(&u_ospcl.s_ospcl)) == FAIL)
2641 if (u_ospcl.s_ospcl.c_magic == OFS_MAGIC) {
2642 memset((char *)buf, 0, (long)TP_BSIZE);
2643 buf->c_type = u_ospcl.s_ospcl.c_type;
2644 buf->c_date = u_ospcl.s_ospcl.c_date;
2645 buf->c_ddate = u_ospcl.s_ospcl.c_ddate;
2646 buf->c_volume = u_ospcl.s_ospcl.c_volume;
2647 buf->c_tapea = u_ospcl.s_ospcl.c_tapea;
2648 buf->c_inumber = u_ospcl.s_ospcl.c_inumber;
2649 buf->c_checksum = u_ospcl.s_ospcl.c_checksum;
2650 buf->c_magic = u_ospcl.s_ospcl.c_magic;
2651 buf->c_dinode.di_mode = u_ospcl.s_ospcl.c_dinode.odi_mode;
2652 buf->c_dinode.di_nlink = u_ospcl.s_ospcl.c_dinode.odi_nlink;
2653 buf->c_dinode.di_uid = u_ospcl.s_ospcl.c_dinode.odi_uid;
2654 buf->c_dinode.di_gid = u_ospcl.s_ospcl.c_dinode.odi_gid;
2655 buf->c_dinode.di_size = u_ospcl.s_ospcl.c_dinode.odi_size;
2656 buf->c_dinode.di_rdev = u_ospcl.s_ospcl.c_dinode.odi_rdev;
2657 #if defined(__linux__) || defined(sunos)
2658 buf->c_dinode.di_atime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_atime;
2659 buf->c_dinode.di_mtime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_mtime;
2660 buf->c_dinode.di_ctime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_ctime;
2661 #else /* __linux__ || sunos */
2662 buf->c_dinode.di_atime = u_ospcl.s_ospcl.c_dinode.odi_atime;
2663 buf->c_dinode.di_mtime = u_ospcl.s_ospcl.c_dinode.odi_mtime;
2664 buf->c_dinode.di_ctime = u_ospcl.s_ospcl.c_dinode.odi_ctime;
2665 #endif /* __linux__ || sunos */
2666 buf->c_count = u_ospcl.s_ospcl.c_count;
2667 memmove(buf->c_addr, u_ospcl.s_ospcl.c_fill, (long)256);
2669 else if (u_ospcl.s_ospcl.c_magic == FS_UFS2_MAGIC) {
2670 buf->c_date = (int32_t)(*(int64_t *)&u_ospcl.dummy[896]);
2671 buf->c_ddate = (int32_t)(*(int64_t *)&u_ospcl.dummy[904]);
2672 buf->c_tapea = (int32_t)(*(int64_t *)&u_ospcl.dummy[912]);
2673 buf->c_firstrec = (int32_t)(*(int64_t *)&u_ospcl.dummy[920]);
2675 buf->c_extattributes = 0;
2676 buf->c_flags |= DR_NEWINODEFMT;
2681 buf->c_magic = NFS_MAGIC;
2684 if ((buf->c_dinode.di_size == 0 || buf->c_dinode.di_size > 0xfffffff) &&
2685 (buf->c_dinode.di_mode & IFMT) == IFDIR && Qcvt == 0) {
2686 qcvt.qval = buf->c_dinode.di_size;
2687 if (qcvt.val[0] || qcvt.val[1]) {
2688 Vprintf(stdout, "Note: Doing Quad swapping\n");
2693 qcvt.qval = buf->c_dinode.di_size;
2695 qcvt.val[1] = qcvt.val[0];
2697 buf->c_dinode.di_size = qcvt.qval;
2701 switch (buf->c_type) {
2706 * Have to patch up missing information in bit map headers
2709 buf->c_dinode.di_size = buf->c_count * TP_BSIZE;
2710 if (buf->c_count > TP_NINDIR)
2713 for (i = 0; i < buf->c_count; i++)
2718 if ((buf->c_flags & DR_NEWINODEFMT) == 0)
2723 if (buf->c_flags & DR_INODEINFO) {
2724 memcpy(volinfo, buf->c_inos, TP_NINOS * sizeof(dump_ino_t));
2726 swabst((u_char *)"128i", (u_char *)volinfo);
2735 panic("gethead: unknown inode type %d\n", buf->c_type);
2739 * If we are restoring a filesystem with old format inodes,
2740 * copy the uid/gid to the new location.
2743 buf->c_dinode.di_uid = buf->c_dinode.di_ouid;
2744 buf->c_dinode.di_gid = buf->c_dinode.di_ogid;
2752 converttapebuf(struct tapebuf *tpb)
2756 unsigned int length:28;
2757 unsigned int flags:3;
2758 unsigned int compressed:1;
2760 swabst((u_char *)"i", (u_char *)tpb);
2761 memcpy(&tb, tpb, 4);
2762 tpb->length = tb.length;
2763 tpb->flags = tb.flags;
2764 tpb->compressed = tb.compressed;
2769 * Check that a header is where it belongs and predict the next header
2772 accthdr(struct s_spcl *header)
2774 static dump_ino_t previno = 0x7fffffff;
2775 static int prevtype;
2776 static long predict;
2779 if (header->c_type == TS_TAPE) {
2780 fprintf(stderr, "Volume header (%s inode format) ",
2781 oldinofmt ? "old" : "new");
2782 if (header->c_firstrec)
2783 fprintf(stderr, "begins with record %d",
2784 header->c_firstrec);
2785 fprintf(stderr, "\n");
2786 previno = 0x7fffffff;
2789 if (previno == 0x7fffffff)
2793 fprintf(stderr, "Dumped inodes map header");
2796 fprintf(stderr, "Used inodes map header");
2799 fprintf(stderr, "File header, ino %lu", (unsigned long)previno);
2802 fprintf(stderr, "File continuation header, ino %ld", (long)previno);
2805 fprintf(stderr, "End of tape header");
2808 if (predict != blksread - 1)
2809 fprintf(stderr, "; predicted %ld blocks, got %ld blocks",
2810 predict, blksread - 1);
2811 fprintf(stderr, "\n");
2814 if (header->c_type != TS_END)
2815 for (i = 0; i < header->c_count; i++)
2816 if (readmapflag || header->c_addr[i] != 0)
2820 prevtype = header->c_type;
2821 previno = header->c_inumber;
2825 * Find an inode header.
2826 * Complain if had to skip, and complain is set.
2829 findinode(struct s_spcl *header)
2831 static long skipcnt = 0;
2835 curfile.name = "<name unknown>";
2836 curfile.action = UNKNOWN;
2840 if (header->c_magic != NFS_MAGIC) {
2842 while (gethead(header) == FAIL ||
2843 header->c_date != dumpdate)
2846 switch (header->c_type) {
2850 * Skip up to the beginning of the next record
2852 for (i = 0; i < header->c_count; i++)
2853 if (header->c_addr[i])
2855 while (gethead(header) == FAIL ||
2856 header->c_date != dumpdate)
2861 curfile.dip = &header->c_dinode;
2862 curfile.ino = header->c_inumber;
2866 curfile.ino = maxino;
2870 curfile.name = "<file removal list>";
2874 curfile.name = "<file dump list>";
2878 panic("unexpected tape header\n");
2882 panic("unknown tape header type %d\n", spcl.c_type);
2886 } while (header->c_type == TS_ADDR);
2891 fprintf(stderr, "resync restore, skipped %ld blocks\n",
2901 j = sizeof(union u_spcl) / sizeof(int);
2908 /* What happens if we want to read restore tapes
2909 for a 16bit int machine??? */
2915 if (i != CHECKSUM) {
2916 fprintf(stderr, "Checksum error %o, inode %lu file %s\n", i,
2917 (unsigned long)curfile.ino, curfile.name);
2927 #include <varargs.h>
2932 msg(const char *fmt, ...)
2945 (void)vfprintf(stderr, fmt, ap);
2948 #endif /* RRESTORE */
2951 swab16(u_char *sp, int n)
2956 c = sp[0]; sp[0] = sp[1]; sp[1] = c;
2963 swab32(u_char *sp, int n)
2968 c = sp[0]; sp[0] = sp[3]; sp[3] = c;
2969 c = sp[1]; sp[1] = sp[2]; sp[2] = c;
2976 swab64(u_char *sp, int n)
2981 c = sp[0]; sp[0] = sp[7]; sp[7] = c;
2982 c = sp[1]; sp[1] = sp[6]; sp[6] = c;
2983 c = sp[2]; sp[2] = sp[5]; sp[5] = c;
2984 c = sp[3]; sp[3] = sp[4]; sp[4] = c;
2991 swabst(u_char *cp, u_char *sp)
2997 case '0': case '1': case '2': case '3': case '4':
2998 case '5': case '6': case '7': case '8': case '9':
2999 n = (n * 10) + (*cp++ - '0');
3002 case 's': case 'w': case 'h':
3020 default: /* Any other character, like 'b' counts as byte. */
3034 swabst((u_char *)"i", (u_char *)&x);
3042 swabst((u_char *)"l", (u_char *)&x);
3048 RequestVol(long tnum)
3059 int uscsi_flags; /* read, write, etc. see below */
3060 short uscsi_status; /* resulting status */
3061 short uscsi_timeout; /* Command Timeout */
3062 caddr_t uscsi_cdb; /* cdb to send to target */
3063 caddr_t uscsi_bufaddr; /* i/o source/destination */
3064 u_int uscsi_buflen; /* size of i/o to take place */
3065 u_int uscsi_resid; /* resid from i/o operation */
3066 u_char uscsi_cdblen; /* # of valid cdb bytes */
3067 u_char uscsi_rqlen; /* size of uscsi_rqbuf */
3068 u_char uscsi_rqstatus; /* status of request sense cmd */
3069 u_char uscsi_rqresid; /* resid of request sense cmd */
3070 caddr_t uscsi_rqbuf; /* request sense buffer */
3071 void *uscsi_reserved_5; /* Reserved for Future Use */
3074 #define CDB_GROUP0 6 /* 6-byte cdb's */
3075 #define CDB_GROUP1 10 /* 10-byte cdb's */
3076 #define CDB_GROUP2 10 /* 10-byte cdb's */
3077 #define CDB_GROUP3 0 /* reserved */
3078 #define CDB_GROUP4 16 /* 16-byte cdb's */
3079 #define CDB_GROUP5 12 /* 12-byte cdb's */
3080 #define CDB_GROUP6 0 /* reserved */
3081 #define CDB_GROUP7 0 /* reserved */
3083 #define USCSI_WRITE 0x00000 /* send data to device */
3084 #define USCSI_SILENT 0x00001 /* no error messages */
3085 #define USCSI_DIAGNOSE 0x00002 /* fail if any error occurs */
3086 #define USCSI_ISOLATE 0x00004 /* isolate from normal commands */
3087 #define USCSI_READ 0x00008 /* get data from device */
3088 #define USCSI_RESET 0x04000 /* Reset target */
3089 #define USCSI_RESET_ALL 0x08000 /* Reset all targets */
3090 #define USCSI_RQENABLE 0x10000 /* Enable Request Sense extensions */
3092 #define USCSIIOC (0x04 << 8)
3093 #define USCSICMD (USCSIIOC|201) /* user scsi command */
3095 #define USCSI_TIMEOUT 30
3096 #define USCSI_SHORT_TIMEOUT 900
3097 #define USCSI_LONG_TIMEOUT 14000
3099 #define B(s,i) ((unsigned char)((s) >> i))
3100 #define B1(s) ((unsigned char)(s))
3102 #define MSB4(s,v) *(s)=B(v,24),(s)[1]=B(v,16), (s)[2]=B(v,8), (s)[3]=B1(v)
3106 GetTapePos(long long *pos)
3109 struct uscsi_cmd scmd;
3114 (void)memset((void *)buf, 0, sizeof(buf));
3115 (void)memset((void *)&scmd, 0, sizeof(scmd));
3116 scmd.uscsi_flags = USCSI_READ|USCSI_SILENT;
3117 scmd.uscsi_timeout = USCSI_TIMEOUT;
3118 scmd.uscsi_cdb = buf;
3119 scmd.uscsi_cdblen = CDB_GROUP1;
3120 buf[0] = 0x34; /* read position */
3122 (void)memset((void *)parm, 0, 512);
3123 scmd.uscsi_bufaddr = parm;
3124 scmd.uscsi_buflen = 56;
3125 if (ioctl(fdsmtc, USCSICMD, &scmd) == -1) {
3129 (void)memcpy(&lpos, &parm[4], sizeof(long));
3135 GotoTapePos(long long pos)
3138 struct uscsi_cmd scmd;
3141 long lpos = (long)pos;
3143 (void)memset((void *)buf, 0, sizeof(buf));
3144 (void)memset((void *)&scmd, 0, sizeof(scmd));
3145 scmd.uscsi_flags = USCSI_WRITE|USCSI_SILENT;
3146 scmd.uscsi_timeout = 360; /* 5 Minutes */
3147 scmd.uscsi_cdb = buf;
3148 scmd.uscsi_cdblen = CDB_GROUP1;
3149 buf[0] = 0x2b; /* locate */
3151 MSB4(&buf[3], lpos);
3152 (void)memset((void *)parm, 0, 512);
3153 scmd.uscsi_bufaddr = NULL;
3154 scmd.uscsi_buflen = 0;
3155 if (ioctl(fdsmtc, USCSICMD, &scmd) == -1) {
3163 #define LSEEK_GET_TAPEPOS 10
3164 #define LSEEK_GO2_TAPEPOS 11
3167 typedef struct mt_pos {
3170 } MTPosRec, *MTPosPtr;
3174 * get the current position of the tape
3177 GetTapePos(long long *pos)
3183 *pos = (long long) rmtseek((OFF_T)0, (int)LSEEK_GET_TAPEPOS);
3192 err = (ioctl(mt, MTIOCPOS, &mtpos) < 0);
3193 *pos = (long long)mtpos;
3196 *pos = LSEEK(mt, 0, SEEK_CUR);
3202 fprintf(stdout, "[%ld] error: %d (getting tapepos: %lld)\n",
3203 (unsigned long)getpid(), err, *pos);
3210 * go to specified position on tape
3213 GotoTapePos(long long pos)
3219 err = (rmtseek((OFF_T)pos, (int)LSEEK_GO2_TAPEPOS) < 0);
3226 buf.mt_count = (int) pos;
3227 err = (ioctl(mt, MTIOCTOP, &buf) < 0);
3230 pos = LSEEK(mt, pos, SEEK_SET);
3236 fprintf(stdout, "[%ld] error: %d (setting tapepos: %lld)\n",
3237 (unsigned long)getpid(), err, pos);
3242 #endif /* __linux__ */
3245 * read next data from tape to re-sync
3248 ReReadFromTape(void)
3252 if (gethead(&spcl) == FAIL) {
3254 fprintf(stdout, "DEBUG 1 gethead failed\n");
3262 ReReadInodeFromTape(dump_ino_t theino)
3271 } while (!(spcl.c_inumber == theino && spcl.c_type == TS_INODE && spcl.c_date == dumpdate));
3273 fprintf(stderr, "DEBUG: %ld reads\n", cntloop);
3274 fprintf(stderr, "DEBUG: bufsize %ld\n", bufsize);
3275 fprintf(stderr, "DEBUG: ntrec %ld\n", ntrec);
3276 fprintf(stderr, "DEBUG: %ld reads\n", cntloop);
3284 OpenSMTCmt(char *themagtape)
3286 if (GetSCSIIDFromPath(themagtape, &scsiid)) {
3287 fprintf(stderr, "can't get SCSI-ID for %s\n", themagtape);
3291 fprintf(stderr, "can't get SCSI-ID for %s\n", themagtape);
3294 sprintf(smtcpath, "/dev/rsmtc%ld,0", scsiid);
3295 if ((fdsmtc = open(smtcpath, O_RDWR)) == -1) {
3296 fprintf(stderr, "can't open smtc device: %s, %d\n", smtcpath, errno);
3302 #endif /* USE_QFA */