]>
git.wh0rd.org - dump.git/blob - restore/tape.c
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.91 2007/02/22 20:12:50 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;
482 panic("Changing volumes on pipe input?\n");
487 saved_blksread
= blksread
;
488 saved_tpblksread
= tpblksread
;
489 #if defined(USE_QFA) && defined(sunos)
490 if (createtapeposflag
|| tapeposflag
)
495 exit(1); /* pipes do not get a second chance */
496 if (aflag
|| curfile
.action
!= SKIP
) {
503 while (newvol
<= 0) {
504 if (tapesread
== 0) {
505 fprintf(stderr
, "%s%s%s%s%s",
506 "You have not read any volumes yet.\n",
507 "Unless you know which volume your",
508 " file(s) are on you should start\n",
509 "with the last volume and work",
510 " towards the first.\n");
512 fprintf(stderr
, "You have read volumes");
514 for (i
= 1; i
< 32; i
++)
515 if (tapesread
& (1 << i
)) {
516 fprintf(stderr
, "%s%ld", buf
, (long)i
);
519 fprintf(stderr
, "\n");
522 fprintf(stderr
, "Specify next volume # (none if no more volumes): ");
523 (void) fflush(stderr
);
524 (void) fgets(buf
, TP_BSIZE
, terminal
);
525 } while (!feof(terminal
) && buf
[0] == '\n');
528 if (!strcmp(buf
, "none\n")) {
535 "Volume numbers are positive numerics\n");
538 if (newvol
== volno
) {
539 tapesread
|= 1 << volno
;
540 #if defined(USE_QFA) && defined(sunos)
541 if (createtapeposflag
|| tapeposflag
) {
542 if (OpenSMTCmt(magtape
) < 0) {
554 * if using an archive file, reset its name so readtape()
555 * could properly use remote access.
560 snprintf(magtape
, MAXPATHLEN
, "%s%03ld", magtapeprefix
, newvol
);
561 magtape
[MAXPATHLEN
- 1] = '\0';
563 if (bot_script
&& !haderror
) {
564 msg("Launching %s\n", bot_script
);
565 bot_code
= system_command(bot_script
, magtape
, newvol
);
566 if (bot_code
!= 0 && bot_code
!= 1) {
567 msg("Restore aborted by the beginning of tape script\n");
571 if (haderror
|| (bot_code
&& !Mflag
)) {
574 fprintf(stderr
, "%d compare errors so far\n", compare_errors
);
576 fprintf(stderr
, "Mount volume %ld\n", (long)newvol
);
578 fprintf(stderr
, "Mount tape volume %ld\n", (long)newvol
);
580 fprintf(stderr
, "Enter ``none'' if there are no more tapes\n");
582 fprintf(stderr
, "then enter volume name (default: %s) ", magtape
);
584 fprintf(stderr
, "otherwise enter tape name (default: %s) ", magtape
);
586 (void) fflush(stderr
);
587 (void) fgets(buf
, TP_BSIZE
, terminal
);
590 if (!strcmp(buf
, "none\n")) {
594 if (buf
[0] != '\n') {
596 (void) strncpy(magtape
, buf
, sizeof(magtape
));
597 magtape
[sizeof(magtape
) - 1] = '\0';
598 if ((pos
= strchr(magtape
, '\n')))
599 magtape
[pos
- magtape
] = '\0';
602 #if defined(USE_QFA) && defined(sunos)
603 if (createtapeposflag
|| tapeposflag
) {
604 if (OpenSMTCmt(magtape
) < 0) {
613 mt
= rmtopen(magtape
, O_RDONLY
);
616 mt
= OPEN(magtape
, O_RDONLY
, 0);
619 fprintf(stderr
, "Cannot open %s\n", magtape
);
626 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
627 readtape_func
= readtape_set
;
633 if (gethead(&tmpbuf
) == FAIL
) {
634 Dprintf(stdout
, "header read failed at %ld blocks\n", (long)blksread
);
635 fprintf(stderr
, "tape is not dump tape\n");
638 blksread
= saved_blksread
;
639 tpblksread
= saved_tpblksread
;
642 if (tmpbuf
.c_volume
!= volno
) {
643 fprintf(stderr
, "Wrong volume (%d)\n", tmpbuf
.c_volume
);
646 blksread
= saved_blksread
;
647 tpblksread
= saved_tpblksread
;
650 if (tmpbuf
.c_date
!= dumpdate
|| tmpbuf
.c_ddate
!= dumptime
) {
651 fprintf(stderr
, "Wrong dump date\n\tgot: %s",
653 ctime(&tmpbuf
.c_date
));
655 ctime4(&tmpbuf
.c_date
));
657 fprintf(stderr
, "\twanted: %s", ctime(&dumpdate
));
660 blksread
= saved_blksread
;
661 tpblksread
= saved_tpblksread
;
664 tapesread
|= 1 << volno
;
666 * If continuing from the previous volume, skip over any
667 * blocks read already at the end of the previous volume.
669 * If coming to this volume at random, skip to the beginning
670 * of the next record.
673 fprintf(stderr
, "Dump tape is compressed.\n");
674 #if !defined(HAVE_ZLIB) && !defined(HAVE_BZLIB) && !defined(HAVE_LZO)
675 errx(1,"This restore version doesn't support decompression");
676 #endif /* !HAVE_ZLIB && !HAVE_BZLIB */
678 Dprintf(stdout
, "read %ld recs, tape starts with %ld\n",
679 tpblksread
- 1, (long)tmpbuf
.c_firstrec
);
680 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
)) {
682 tpblksread
= tmpbuf
.c_firstrec
+ 1;
683 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
685 } else if (tmpbuf
.c_firstrec
> 0 &&
686 tmpbuf
.c_firstrec
< tpblksread
- 1) {
688 * -1 since we've read the volume header
690 i
= tpblksread
- tmpbuf
.c_firstrec
- 1;
691 Dprintf(stderr
, "Skipping %ld duplicate record%s.\n",
692 (long)i
, i
> 1 ? "s" : "");
697 if (curfile
.action
== USING
) {
699 panic("active file into volume 1\n");
703 * Skip up to the beginning of the next record
705 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
))
706 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
708 (void) gethead(&spcl
);
712 siglongjmp(restart
, 1);
717 * Handle unexpected EOF.
723 if (gettingfile
&& curfile
.action
== USING
) {
724 printf("Warning: %s %s\n",
725 "End-of-input encountered while extracting", curfile
.name
);
727 curfile
.name
= "<name unknown>";
728 curfile
.action
= UNKNOWN
;
730 curfile
.ino
= maxino
;
733 siglongjmp(restart
, 1);
738 * handle multiple dumps per tape by skipping forward to the
746 if (dumpnum
== 1 || volno
!= 1)
749 errx(1, "Cannot have multiple dumps on pipe input");
751 tcom
.mt_count
= dumpnum
- 1;
754 if (rmtioctl(MTFSF
, dumpnum
- 1) < 0) {
755 fprintf(stderr
, "rmtioctl MTFSF: %s\n", strerror(errno
));
760 if (ioctl(mt
, (int)MTIOCTOP
, (char *)&tcom
) < 0) {
761 fprintf(stderr
, "rmtioctl MTFSF: %s\n", strerror(errno
));
763 /* warn("ioctl MTFSF"); */
771 Vprintf(stdout
, "Dump date: %s", ctime(&spcl
.c_date
));
772 Vprintf(stdout
, "Dumped from: %s",
773 (spcl
.c_ddate
== 0) ? "the epoch\n" : ctime(&spcl
.c_ddate
));
774 if (spcl
.c_host
[0] == '\0')
776 Vprintf(stdout
, "Level %d dump of %s on %s:%s\n",
777 spcl
.c_level
, spcl
.c_filesys
, spcl
.c_host
, spcl
.c_dev
);
778 Vprintf(stdout
, "Label: %s\n", spcl
.c_label
);
780 fprintf(stdout
, "Dump date: %s", ctime4(&spcl
.c_date
));
781 fprintf(stdout
, "Dumped from: %s",
782 (spcl
.c_ddate
== 0) ? "the epoch\n" : ctime4(&spcl
.c_ddate
));
783 if (spcl
.c_host
[0] == '\0')
785 fprintf(stdout
, "Level %d dump of %s on %s:%s\n",
786 spcl
.c_level
, spcl
.c_filesys
, spcl
.c_host
, spcl
.c_dev
);
787 fprintf(stdout
, "Label: %s\n", spcl
.c_label
);
796 if (volinfo
[1] == ROOTINO
) {
797 printf("Starting inode numbers by volume:\n");
798 for (i
= 1; i
< (int)TP_NINOS
&& volinfo
[i
] != 0; ++i
)
799 printf("\tVolume %d: %lu\n", i
, (unsigned long)volinfo
[i
]);
805 extractfile(struct entry
*ep
, int doremove
)
809 struct timeval timep
[2];
810 char *name
= myname(ep
);
812 /* If removal is requested (-r mode) do remove it unless
813 * we are extracting a metadata only inode */
814 if (spcl
.c_flags
& DR_METAONLY
) {
815 Vprintf(stdout
, "file %s is metadata only\n", name
);
820 ep
->e_flags
&= ~REMOVED
;
825 curfile
.action
= USING
;
826 #if defined(__linux__) || defined(sunos)
827 timep
[0].tv_sec
= curfile
.dip
->di_atime
.tv_sec
;
828 timep
[0].tv_usec
= curfile
.dip
->di_atime
.tv_usec
;
829 timep
[1].tv_sec
= curfile
.dip
->di_mtime
.tv_sec
;
830 timep
[1].tv_usec
= curfile
.dip
->di_mtime
.tv_usec
;
831 #else /* __linux__ || sunos */
832 timep
[0].tv_sec
= curfile
.dip
->di_atime
;
833 timep
[0].tv_usec
= curfile
.dip
->di_atimensec
/ 1000;
834 timep
[1].tv_sec
= curfile
.dip
->di_mtime
;
835 timep
[1].tv_usec
= curfile
.dip
->di_mtimensec
/ 1000;
836 #endif /* __linux__ */
837 mode
= curfile
.dip
->di_mode
;
838 flags
= curfile
.dip
->di_flags
;
839 switch (mode
& IFMT
) {
842 fprintf(stderr
, "%s: unknown file mode 0%o\n", name
, mode
);
847 Vprintf(stdout
, "skipped socket %s\n", name
);
855 if (ep
== NULL
|| ep
->e_flags
& EXTRACT
)
856 panic("unextracted directory %s\n", name
);
860 Vprintf(stdout
, "extract dir %s\n", name
);
861 ret
= genliteraldir(name
, curfile
.ino
);
869 uid_t luid
= curfile
.dip
->di_uid
;
870 gid_t lgid
= curfile
.dip
->di_gid
;
872 if (! (spcl
.c_flags
& DR_METAONLY
)) {
875 getfile(xtrlnkfile
, xtrlnkskip
);
878 "%s: zero length symbolic link (ignored)\n", name
);
881 if (linkit(lnkbuf
, name
, SYMLINK
) == FAIL
)
888 (void) lchown(name
, luid
, lgid
);
895 Vprintf(stdout
, "extract fifo %s\n", name
);
900 if (! (spcl
.c_flags
& DR_METAONLY
)) {
903 if (mkfifo(name
, mode
) < 0) {
904 warn("%s: cannot create fifo", name
);
909 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
910 (void) chmod(name
, mode
);
913 (void) lsetflags(name
, flags
);
917 warn("%s: cannot call chflags", name
);
918 /* (void) chflags(name, flags); */
921 (void) chflags(name
, flags
);
931 Vprintf(stdout
, "extract special file %s\n", name
);
936 if (! (spcl
.c_flags
& DR_METAONLY
)) {
939 if (mknod(name
, mode
, (int)curfile
.dip
->di_rdev
) < 0) {
940 warn("%s: cannot create special file", name
);
945 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
946 (void) chmod(name
, mode
);
950 warn("%s: lsetflags called on a special file", name
);
951 (void) lsetflags(name
, flags
);
956 warn("%s: cannot call chflags on a special file", name
);
957 /* (void) chflags(name, flags); */
961 warn("%s: chflags called on a special file", name
);
962 (void) chflags(name
, flags
);
973 uid_t luid
= curfile
.dip
->di_uid
;
974 gid_t lgid
= curfile
.dip
->di_gid
;
976 Vprintf(stdout
, "extract file %s\n", name
);
981 if (! (spcl
.c_flags
& DR_METAONLY
)) {
984 if ((ofile
= OPEN(name
, O_WRONLY
| O_CREAT
| O_TRUNC
,
986 warn("%s: cannot create file", name
);
990 getfile(xtrfile
, xtrskip
);
995 (void) chown(name
, luid
, lgid
);
996 (void) chmod(name
, mode
);
999 (void) lsetflags(name
, flags
);
1003 warn("%s: cannot call chflags", name
);
1004 /* (void) chflags(name, flags); */
1007 (void) chflags(name
, flags
);
1011 utimes(name
, timep
);
1019 extractattr(char *path
)
1021 while (spcl
.c_flags
& DR_EXTATTRIBUTES
) {
1022 switch (spcl
.c_extattributes
) {
1023 case EXT_MACOSFNDRINFO
:
1025 (void)extractfinderinfoufs(path
);
1027 msg("MacOSX not supported in this version, skipping\n");
1031 case EXT_MACOSRESFORK
:
1033 (void)extractresourceufs(path
);
1035 msg("MacOSX not supported in this version, skipping\n");
1040 char xattr
[XATTR_MAXSIZE
];
1042 if (readxattr(xattr
) == GOOD
) {
1043 xattr_extract(path
, xattr
);
1048 msg("unexpected inode extension %ld, skipping\n", spcl
.c_extattributes
);
1058 extractfinderinfoufs(char *name
)
1061 char oFileRsrc
[MAXPATHLEN
];
1064 struct timeval timep
[2];
1067 char path
[MAXPATHLEN
], fname
[MAXPATHLEN
];
1069 curfile
.name
= name
;
1070 curfile
.action
= USING
;
1071 timep
[0].tv_sec
= curfile
.dip
->di_atime
.tv_sec
;
1072 timep
[0].tv_usec
= curfile
.dip
->di_atime
.tv_usec
;
1073 timep
[1].tv_sec
= curfile
.dip
->di_mtime
.tv_sec
;
1074 timep
[1].tv_usec
= curfile
.dip
->di_mtime
.tv_usec
;
1075 mode
= curfile
.dip
->di_mode
;
1076 flags
= curfile
.dip
->di_flags
;
1077 uid
= curfile
.dip
->di_uid
;
1078 gid
= curfile
.dip
->di_gid
;
1080 switch (mode
& IFMT
) {
1083 fprintf(stderr
, "%s: (extr. finfoufs) unknown file mode 0%o\n", name
, mode
);
1088 fprintf(stderr
, "%s: (extr. finfoufs[IFDIR]) unknown file mode 0%o\n", name
, mode
);
1097 Vprintf(stdout
, "extract finderinfo file %s\n", name
);
1102 getfile(xtrfilefinderinfo
, xtrskip
);
1104 GetPathFile(name
, path
, fname
);
1105 strcpy(oFileRsrc
, path
);
1106 strcat(oFileRsrc
, "._");
1107 strcat(oFileRsrc
, fname
);
1109 if ((err
= CreateAppleDoubleFileRes(oFileRsrc
, &gFndrInfo
.fndrinfo
,
1110 mode
, flags
, timep
, uid
, gid
)) != 0) {
1111 fprintf(stderr
, "%s: cannot create finderinfo: %s\n",
1112 name
, strerror(errno
));
1123 extractresourceufs(char *name
)
1125 char oFileRsrc
[MAXPATHLEN
];
1128 struct timeval timep
[2];
1129 char path
[MAXPATHLEN
], fname
[MAXPATHLEN
];
1139 curfile
.name
= name
;
1140 curfile
.action
= USING
;
1141 timep
[0].tv_sec
= curfile
.dip
->di_atime
.tv_sec
;
1142 timep
[0].tv_usec
= curfile
.dip
->di_atime
.tv_usec
;
1143 timep
[1].tv_sec
= curfile
.dip
->di_mtime
.tv_sec
;
1144 timep
[1].tv_usec
= curfile
.dip
->di_mtime
.tv_usec
;
1145 mode
= curfile
.dip
->di_mode
;
1146 flags
= curfile
.dip
->di_flags
;
1147 uid
= curfile
.dip
->di_uid
;
1148 gid
= curfile
.dip
->di_gid
;
1149 di_size
= curfile
.dip
->di_size
;
1151 switch (mode
& IFMT
) {
1154 fprintf(stderr
, "%s: (extr. resufs) unknown file mode 0%o\n", name
, mode
);
1159 fprintf(stderr
, "%s: (extr. resufs [IFDIR]) unknown file mode 0%o\n", name
, mode
);
1168 Vprintf(stdout
, "extract resource file %s\n", name
);
1174 GetPathFile(name
, path
, fname
);
1175 strcpy(oFileRsrc
, path
);
1176 strcat(oFileRsrc
, "._");
1177 strcat(oFileRsrc
, fname
);
1179 if ((ofile
= open(oFileRsrc
, O_RDONLY
, 0)) < 0) {
1180 fprintf(stderr
, "%s: cannot read finderinfo: %s\n",
1181 name
, strerror(errno
));
1185 read(ofile
, buf
, 70);
1186 (void) close(ofile
);
1188 hp
= (ASDHeaderPtr
)p
;
1191 p
+= sizeof(ASDHeader
) - CORRECT
;
1192 ep
= (ASDEntryPtr
)p
;
1193 /* the finderinfo entry */
1194 ep
->offset
+= sizeof(ASDEntry
);
1197 p
+= sizeof(ASDEntry
);
1198 /* the finderinfo data */
1199 bcopy(p
, p
+ sizeof(ASDEntry
), INFOLEN
);
1200 ep
= (ASDEntryPtr
)p
;
1201 /* the new resourcefork entry */
1202 ep
->entryID
= EntryRSRCFork
;
1203 ep
->offset
= loff
+ INFOLEN
;
1205 /* write the new appledouble entries to the file */
1206 if ((ofile
= open(oFileRsrc
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666)) < 0) {
1207 fprintf(stderr
, "%s: cannot create resource file: %s\n",
1208 name
, strerror(errno
));
1212 write(ofile
, buf
, 70 + sizeof(ASDEntry
));
1213 /* and add the resource data from tape */
1214 getfile(xtrfile
, xtrskip
);
1216 (void) fchown(ofile
, uid
, gid
);
1217 (void) fchmod(ofile
, mode
);
1218 (void) close(ofile
);
1219 (void) lsetflags(oFileRsrc
, flags
);
1220 utimes(oFileRsrc
, timep
);
1225 #endif /* DUMP_MACOSX */
1228 readxattr(char *buffer
)
1231 msg("reading EA data for inode %lu\n", curfile
.ino
);
1233 curfile
.name
= "EA block";
1234 if (curfile
.dip
->di_size
> XATTR_MAXSIZE
) {
1235 fprintf(stderr
, "EA size too big (%ld)", (long)curfile
.dip
->di_size
);
1240 memset(xattrbuf
, 0, XATTR_MAXSIZE
);
1243 getfile(xtrxattr
, xtrnull
);
1245 memcpy(buffer
, xattrbuf
, XATTR_MAXSIZE
);
1251 * skip over bit maps on the tape
1257 while (spcl
.c_type
== TS_BITS
|| spcl
.c_type
== TS_CLRI
)
1262 * skip over a file on the tape
1268 curfile
.action
= SKIP
;
1269 getfile(xtrnull
, xtrnull
);
1273 * skip over any extended attributes.
1279 while (spcl
.c_flags
& DR_EXTATTRIBUTES
)
1284 * Extract a file from the tape.
1285 * When an allocated block is found it is passed to the fill function;
1286 * when an unallocated block (hole) is found, a zeroed buffer is passed
1287 * to the skip function.
1290 getfile(void (*fill
) __P((char *, size_t)), void (*skip
) __P((char *, size_t)))
1293 volatile int curblk
= 0;
1294 volatile quad_t size
= spcl
.c_dinode
.di_size
;
1295 volatile int last_write_was_hole
= 0;
1296 quad_t origsize
= size
;
1297 static char clearedbuf
[MAXBSIZE
];
1298 char buf
[MAXBSIZE
/ TP_BSIZE
][TP_BSIZE
];
1299 char junk
[TP_BSIZE
];
1301 if (spcl
.c_type
== TS_END
)
1302 panic("ran off end of tape\n");
1303 if (spcl
.c_magic
!= NFS_MAGIC
)
1304 panic("not at beginning of a file\n");
1305 if (!gettingfile
&& setjmp(restart
) != 0)
1309 for (i
= 0; i
< spcl
.c_count
; i
++) {
1310 if (readmapflag
|| spcl
.c_addr
[i
]) {
1311 readtape(&buf
[curblk
++][0]);
1312 if (curblk
== fssize
/ TP_BSIZE
) {
1313 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
1314 fssize
: (curblk
- 1) * TP_BSIZE
+ size
));
1316 last_write_was_hole
= 0;
1320 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
1322 (curblk
- 1) * TP_BSIZE
+ size
));
1325 (*skip
)(clearedbuf
, (long)(size
> TP_BSIZE
?
1327 last_write_was_hole
= 1;
1329 if ((size
-= TP_BSIZE
) <= 0) {
1330 for (i
++; i
< spcl
.c_count
; i
++)
1331 if (readmapflag
|| spcl
.c_addr
[i
])
1336 if (gethead(&spcl
) == GOOD
&& size
> 0) {
1337 if (spcl
.c_type
== TS_ADDR
)
1340 "Missing address (header) block for %s at %ld blocks\n",
1341 curfile
.name
, (long)blksread
);
1344 (*fill
)((char *)buf
, (size_t)((curblk
* TP_BSIZE
) + size
));
1345 last_write_was_hole
= 0;
1348 fprintf(stderr
, "Missing blocks at the end of %s, assuming hole\n", curfile
.name
);
1350 size_t skp
= size
> TP_BSIZE
? TP_BSIZE
: size
;
1351 (*skip
)(clearedbuf
, skp
);
1354 last_write_was_hole
= 1;
1356 if (last_write_was_hole
) {
1357 FTRUNCATE(ofile
, origsize
);
1365 * Write out the next block of a file.
1368 xtrfile(char *buf
, size_t size
)
1373 if (write(ofile
, buf
, (int) size
) == -1)
1374 err(1, "write error extracting inode %lu, name %s\nwrite",
1375 (unsigned long)curfile
.ino
, curfile
.name
);
1380 xtrfilefinderinfo(char *buf
, size_t size
)
1382 bcopy(buf
, &gFndrInfo
, size
);
1384 #endif /* DUMP_MACOSX */
1387 * Skip over a hole in a file.
1391 xtrskip(UNUSED(char *buf
), size_t size
)
1394 if (LSEEK(ofile
, (OFF_T
)size
, SEEK_CUR
) == -1)
1395 err(1, "seek error extracting inode %lu, name %s\nlseek",
1396 (unsigned long)curfile
.ino
, curfile
.name
);
1400 * Collect the next block of a symbolic link.
1403 xtrlnkfile(char *buf
, size_t size
)
1407 if (pathlen
> MAXPATHLEN
) {
1408 buf
[size
- 1] = '\0';
1409 errx(1, "symbolic link name: %s->%s%s; too long %d",
1410 curfile
.name
, lnkbuf
, buf
, pathlen
);
1412 (void) strcat(lnkbuf
, buf
);
1413 lnkbuf
[pathlen
] = '\0';
1417 * Skip over a hole in a symbolic link (should never happen).
1421 xtrlnkskip(UNUSED(char *buf
), UNUSED(size_t size
))
1424 errx(1, "unallocated block in symbolic link %s", curfile
.name
);
1428 * Collect the next block of a bit map.
1431 xtrmap(char *buf
, size_t size
)
1434 memmove(map
, buf
, size
);
1439 * Skip over a hole in a bit map (should never happen).
1443 xtrmapskip(UNUSED(char *buf
), size_t size
)
1446 panic("hole in map\n");
1451 * Noop, when an extraction function is not needed.
1455 xtrnull(UNUSED(char *buf
), UNUSED(size_t size
))
1461 #if COMPARE_ONTHEFLY
1463 * Compare the next block of a file.
1466 xtrcmpfile(char *buf
, size_t size
)
1468 static char cmpbuf
[MAXBSIZE
];
1473 if (read(ifile
, cmpbuf
, size
) != (ssize_t
)size
) {
1474 fprintf(stderr
, "%s: size has changed.\n",
1480 if (memcmp(buf
, cmpbuf
, size
) != 0) {
1481 fprintf(stderr
, "%s: tape and disk copies are different\n",
1489 * Skip over a hole in a file.
1492 xtrcmpskip(UNUSED(char *buf
), size_t size
)
1494 static char cmpbuf
[MAXBSIZE
];
1500 if (read(ifile
, cmpbuf
, size
) != (ssize_t
)size
) {
1501 fprintf(stderr
, "%s: size has changed.\n",
1507 for (i
= 0; i
< (int)size
; ++i
)
1508 if (cmpbuf
[i
] != '\0') {
1509 fprintf(stderr
, "%s: tape and disk copies are different\n",
1515 #endif /* COMPARE_ONTHEFLY */
1518 xtrxattr(char *buf
, size_t size
)
1520 if (xattrlen
+ size
> XATTR_MAXSIZE
) {
1521 fprintf(stderr
, "EA size too big (%ld)", (long)xattrlen
+ size
);
1524 memcpy(xattrbuf
+ xattrlen
, buf
, size
);
1528 #if !COMPARE_ONTHEFLY
1530 do_cmpfiles(int fd_tape
, int fd_disk
, OFF_T size
)
1532 static char buf_tape
[BUFSIZ
];
1533 static char buf_disk
[BUFSIZ
];
1538 if ((n_tape
= read(fd_tape
, buf_tape
, sizeof(buf_tape
))) < 1) {
1539 close(fd_tape
), close(fd_disk
);
1540 panic("do_cmpfiles: unexpected EOF[1]");
1542 if ((n_disk
= read(fd_disk
, buf_disk
, sizeof(buf_tape
))) < 1) {
1543 close(fd_tape
), close(fd_disk
);
1544 panic("do_cmpfiles: unexpected EOF[2]");
1546 if (n_tape
!= n_disk
) {
1547 close(fd_tape
), close(fd_disk
);
1548 panic("do_cmpfiles: sizes different!");
1550 if (memcmp(buf_tape
, buf_disk
, (size_t)n_tape
) != 0) return (1);
1556 /* for debugging compare problems */
1557 #undef COMPARE_FAIL_KEEP_FILE
1560 #ifdef COMPARE_FAIL_KEEP_FILE
1561 /* return true if tapefile should be unlinked after compare */
1566 cmpfiles(char *tapefile
, char *diskfile
, struct STAT
*sbuf_disk
)
1568 struct STAT sbuf_tape
;
1569 int fd_tape
, fd_disk
;
1571 if (STAT(tapefile
, &sbuf_tape
) != 0) {
1572 panic("can't lstat tmp file %s: %s\n", tapefile
,
1577 if (sbuf_disk
->st_size
!= sbuf_tape
.st_size
) {
1579 "%s: size changed from %lld to %lld.\n",
1580 diskfile
, (long long)sbuf_tape
.st_size
, (long long)sbuf_disk
->st_size
);
1582 #ifdef COMPARE_FAIL_KEEP_FILE
1589 if ((fd_tape
= OPEN(tapefile
, O_RDONLY
)) < 0) {
1590 panic("can't open %s: %s\n", tapefile
, strerror(errno
));
1593 if ((fd_disk
= OPEN(diskfile
, O_RDONLY
)) < 0) {
1595 panic("can't open %s: %s\n", diskfile
, strerror(errno
));
1599 if (do_cmpfiles(fd_tape
, fd_disk
, sbuf_tape
.st_size
)) {
1600 fprintf(stderr
, "%s: tape and disk copies are different\n",
1605 #ifdef COMPARE_FAIL_KEEP_FILE
1606 /* rename the file to live in /tmp */
1607 /* rename `tapefile' to /tmp/<basename of diskfile> */
1609 char *p
= strrchr(diskfile
, '/');
1610 char newname
[MAXPATHLEN
];
1612 panic("can't find / in %s\n", diskfile
);
1614 snprintf(newname
, sizeof(newname
), "%s/debug/%s", tmpdir
, p
+ 1);
1615 if (rename(tapefile
, newname
)) {
1616 panic("rename from %s to %s failed: %s\n",
1620 fprintf(stderr
, "*** %s saved to %s\n",
1625 /* don't unlink the file (it's not there anymore */
1634 #ifdef COMPARE_FAIL_KEEP_FILE
1638 #endif /* !COMPARE_ONTHEFLY */
1641 compareattr(char *name
)
1645 while (spcl
.c_flags
& DR_EXTATTRIBUTES
) {
1646 switch (spcl
.c_extattributes
) {
1647 case EXT_MACOSFNDRINFO
:
1648 msg("MacOSX not supported for comparision in this version, skipping\n");
1651 case EXT_MACOSRESFORK
:
1652 msg("MacOSX not supported for comparision in this version, skipping\n");
1656 char xattr
[XATTR_MAXSIZE
];
1658 if (readxattr(xattr
) == GOOD
) {
1659 if (xattr_compare(name
, xattr
) == FAIL
)
1668 msg("unexpected inode extension %ld, skipping\n", spcl
.c_extattributes
);
1673 if (!xattr_done
&& xattr_compare(name
, NULL
) == FAIL
)
1677 #if !COMPARE_ONTHEFLY
1678 static char tmpfilename
[MAXPATHLEN
];
1682 comparefile(char *name
)
1688 unsigned long newflags
;
1691 #if !COMPARE_ONTHEFLY
1692 static char *tmpfile
= NULL
;
1695 curfile
.name
= name
;
1696 curfile
.action
= USING
;
1697 mode
= curfile
.dip
->di_mode
;
1698 flags
= curfile
.dip
->di_flags
;
1699 uid
= curfile
.dip
->di_uid
;
1700 gid
= curfile
.dip
->di_gid
;
1702 if ((mode
& IFMT
) == IFSOCK
) {
1703 Vprintf(stdout
, "skipped socket %s\n", name
);
1708 if ((r
= LSTAT(name
, &sb
)) != 0) {
1709 warn("unable to stat %s", name
);
1715 Vprintf(stdout
, "comparing %s (size: %lld, mode: 0%o)\n", name
,
1716 (long long)sb
.st_size
, mode
);
1718 if (sb
.st_mode
!= mode
) {
1719 fprintf(stderr
, "%s: mode changed from 0%o to 0%o.\n",
1720 name
, mode
& 07777, sb
.st_mode
& 07777);
1723 if (sb
.st_uid
!= uid
) {
1724 fprintf(stderr
, "%s: uid changed from %d to %d.\n",
1725 name
, uid
, sb
.st_uid
);
1728 if (sb
.st_gid
!= gid
) {
1729 fprintf(stderr
, "%s: gid changed from %d to %d.\n",
1730 name
, gid
, sb
.st_gid
);
1734 if (lgetflags(name
, &newflags
) < 0) {
1736 warn("%s: lgetflags failed", name
);
1741 if (newflags
!= flags
) {
1742 fprintf(stderr
, "%s: flags changed from 0x%08x to 0x%08lx.\n",
1743 name
, flags
, newflags
);
1748 if (spcl
.c_flags
& DR_METAONLY
) {
1752 switch (mode
& IFMT
) {
1767 char lbuf
[MAXPATHLEN
+ 1];
1770 if (!(sb
.st_mode
& S_IFLNK
)) {
1771 fprintf(stderr
, "%s: is no longer a symbolic link\n",
1778 getfile(xtrlnkfile
, xtrlnkskip
);
1781 "%s: zero length symbolic link (ignored)\n",
1786 if ((lsize
= readlink(name
, lbuf
, MAXPATHLEN
)) < 0) {
1787 panic("readlink of %s failed: %s\n", name
,
1792 if (strcmp(lbuf
, lnkbuf
) != 0) {
1794 "%s: symbolic link changed from %s to %s.\n",
1795 name
, lnkbuf
, lbuf
);
1805 if (!(sb
.st_mode
& (S_IFCHR
|S_IFBLK
))) {
1806 fprintf(stderr
, "%s: no longer a special file\n",
1813 if (sb
.st_rdev
!= (dev_t
)curfile
.dip
->di_rdev
) {
1815 "%s: device changed from %d,%d to %d,%d.\n",
1817 major(curfile
.dip
->di_rdev
),
1818 minor(curfile
.dip
->di_rdev
),
1828 #if COMPARE_ONTHEFLY
1829 if ((ifile
= OPEN(name
, O_RDONLY
)) < 0) {
1830 warn("can't open %s", name
);
1836 getfile(xtrcmpfile
, xtrcmpskip
);
1839 if (read(ifile
, &c
, 1) != 0) {
1840 fprintf(stderr
, "%s: size has changed.\n",
1850 if (tmpfile
== NULL
) {
1851 /* argument to mktemp() must not be in RO space: */
1852 snprintf(tmpfilename
, sizeof(tmpfilename
), "%s/restoreCXXXXXX", tmpdir
);
1853 tmpfile
= mktemp(&tmpfilename
[0]);
1855 if ((STAT(tmpfile
, &stemp
) == 0) && (unlink(tmpfile
) != 0)) {
1856 panic("cannot delete tmp file %s: %s\n",
1857 tmpfile
, strerror(errno
));
1859 if ((ofile
= OPEN(tmpfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600)) < 0) {
1860 panic("cannot create file temp file %s: %s\n",
1861 name
, strerror(errno
));
1863 getfile(xtrfile
, xtrskip
);
1864 (void) close(ofile
);
1865 #ifdef COMPARE_FAIL_KEEP_FILE
1866 if (cmpfiles(tmpfile
, name
, &sb
))
1869 cmpfiles(tmpfile
, name
, &sb
);
1872 #endif /* COMPARE_ONTHEFLY */
1879 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
1880 static void (*readtape_func
)(char *) = readtape_set
;
1883 * Read TP_BSIZE blocks from the input.
1884 * Handle read errors, and end of media.
1885 * Decompress compressed blocks.
1890 (*readtape_func
)(buf
); /* call the actual processing routine */
1894 * Set function pointer for readtape() routine. zflag and magtapein must
1895 * be correctly set before the first call to readtape().
1898 readtape_set(char *buf
)
1901 readtape_func
= readtape_uncompr
;
1905 readtape_func
= readtape_comprtape
;
1907 readtape_func
= readtape_comprfile
;
1912 #endif /* HAVE_ZLIB || HAVE_BZLIB || HAVE_LZO */
1915 * This is the original readtape(), it's used for reading uncompressed input.
1916 * Read TP_BSIZE blocks from the input.
1917 * Handle read errors, and end of media.
1920 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
1921 readtape_uncompr(char *buf
)
1926 ssize_t rd
, newvol
, i
;
1927 int cnt
, seek_failed
;
1929 if (blkcnt
< numtrec
) {
1930 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1936 for (i
= 0; i
< ntrec
; i
++)
1937 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1940 cnt
= ntrec
* TP_BSIZE
;
1943 if (createtapeposflag
)
1944 (void)GetTapePos(&curtapepos
);
1949 i
= rmtread(&tapebuf
[rd
], cnt
);
1952 i
= read(mt
, &tapebuf
[rd
], cnt
);
1955 * Check for mid-tape short read error.
1956 * If found, skip rest of buffer and start with the next.
1958 if (!pipein
&& numtrec
< ntrec
&& i
> 0) {
1959 Dprintf(stdout
, "mid-media short read error.\n");
1963 * Handle partial block read.
1965 if (pipein
&& i
== 0 && rd
> 0)
1967 else if (i
> 0 && i
!= ntrec
* TP_BSIZE
) {
1976 * Short read. Process the blocks read.
1978 if (i
% TP_BSIZE
!= 0)
1980 "partial block read: %ld should be %ld\n",
1981 (long)i
, ntrec
* TP_BSIZE
);
1982 numtrec
= i
/ TP_BSIZE
;
1986 * Handle read error.
1989 fprintf(stderr
, "Tape read error while ");
1990 switch (curfile
.action
) {
1992 fprintf(stderr
, "trying to set up tape\n");
1995 fprintf(stderr
, "trying to resynchronize\n");
1998 fprintf(stderr
, "restoring %s\n", curfile
.name
);
2001 fprintf(stderr
, "skipping over inode %lu\n",
2002 (unsigned long)curfile
.ino
);
2005 if (!yflag
&& !reply("continue"))
2007 i
= ntrec
* TP_BSIZE
;
2008 memset(tapebuf
, 0, (size_t)i
);
2011 seek_failed
= (rmtseek(i
, 1) < 0);
2014 seek_failed
= (LSEEK(mt
, i
, SEEK_CUR
) == (OFF_T
)-1);
2017 warn("continuation failed");
2018 if (!yflag
&& !reply("assume end-of-tape and continue"))
2024 * Handle end of tape.
2027 Vprintf(stdout
, "End-of-tape encountered\n");
2036 if (rd
% TP_BSIZE
!= 0)
2037 panic("partial block read: %d should be %d\n",
2038 rd
, ntrec
* TP_BSIZE
);
2040 memmove(&tapebuf
[rd
], &endoftapemark
, TP_BSIZE
);
2043 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
2048 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
2051 * Read a compressed format block from a file or pipe and uncompress it.
2052 * Attempt to handle read errors, and end of file.
2055 readtape_comprfile(char *buf
)
2057 long rl
, size
, i
, ret
;
2059 struct tapebuf
*tpb
;
2061 if (blkcnt
< numtrec
) {
2062 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
2067 /* need to read the next block */
2069 for (i
= 0; i
< ntrec
; i
++)
2070 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
2072 tpb
= (struct tapebuf
*) tapebuf
;
2074 /* read the block prefix */
2075 ret
= read_a_block(mt
, tapebuf
, PREFIXSIZE
, &rl
);
2076 converttapebuf(tpb
);
2078 if (Vflag
&& (ret
== 0 || rl
< (int)PREFIXSIZE
|| tpb
->length
== 0))
2085 if (size
> bufsize
) {
2086 /* something's wrong */
2087 Vprintf(stdout
, "Prefix size error, max size %d, got %ld\n",
2090 tpb
->length
= bufsize
;
2092 ret
= read_a_block(mt
, tpb
->buf
, size
, &rl
);
2096 tbufptr
= decompress_tapebuf(tpb
, rl
+ PREFIXSIZE
);
2097 if (tbufptr
== NULL
) {
2098 msg_read_error("File decompression error while");
2099 if (!yflag
&& !reply("continue"))
2101 memset(tapebuf
, 0, bufsize
);
2106 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
2112 /* Errors while reading from a file or pipe are catastrophic. Since
2113 * there are no block boundaries, it's impossible to bypass the
2114 * block in error and find the start of the next block.
2117 /* It's possible to have multiple input files using -M
2118 * and -f file1,file2...
2120 Vprintf(stdout
, "End-of-File encountered\n");
2130 msg_read_error("Read error while");
2131 /* if (!yflag && !reply("continue")) */
2136 * Read compressed data from a tape and uncompress it.
2137 * Handle read errors, and end of media.
2138 * Since a tape consists of separate physical blocks, we try
2139 * to recover from errors by repositioning the tape to the next
2143 readtape_comprtape(char *buf
)
2147 struct tapebuf
*tpb
;
2150 if (blkcnt
< numtrec
) {
2151 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
2156 /* need to read the next block */
2158 for (i
= 0; i
< ntrec
; i
++)
2159 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
2161 tpb
= (struct tapebuf
*) tapebuf
;
2163 /* read the block */
2164 size
= bufsize
+ PREFIXSIZE
;
2165 ret
= read_a_block(mt
, tapebuf
, size
, &rl
);
2169 converttapebuf(tpb
);
2170 tbufptr
= decompress_tapebuf(tpb
, rl
);
2171 if (tbufptr
== NULL
) {
2172 msg_read_error("Tape decompression error while");
2173 if (!yflag
&& !reply("continue"))
2175 memset(tapebuf
, 0, PREFIXSIZE
+ bufsize
);
2181 /* Handle errors: EOT switches to the next volume, other errors
2182 * attempt to position the tape to the next block.
2185 Vprintf(stdout
, "End-of-tape encountered\n");
2194 msg_read_error("Tape read error while");
2195 if (!yflag
&& !reply("continue"))
2197 memset(tapebuf
, 0, PREFIXSIZE
+ bufsize
);
2202 rl
= rmtioctl(MTFSR
, 1);
2208 rl
= ioctl(mt
, MTIOCTOP
, &tcom
);
2212 warn("continuation failed");
2213 if (!yflag
&& !reply("assume end-of-tape and continue"))
2215 ret
= 0; /* end of tape */
2221 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
2227 * Decompress a struct tapebuf into a buffer. readsize is the size read
2228 * from the tape/file and is used for error messages. Returns a pointer
2229 * to the location of the uncompressed buffer or NULL on errors.
2230 * Adjust numtrec and complain for a short block.
2233 decompress_tapebuf(struct tapebuf
*tpbin
, int readsize
)
2235 /* If zflag is on, all blocks have a struct tapebuf prefix */
2236 /* zflag gets set in setup() from the dump header */
2237 int cresult
, blocklen
;
2238 unsigned long worklen
;
2239 char *output
= NULL
,*reason
= NULL
, *lengtherr
= NULL
;
2241 /* build a length error message */
2242 blocklen
= tpbin
->length
;
2243 if (readsize
< blocklen
+ (int)PREFIXSIZE
)
2244 lengtherr
= "short";
2246 if (readsize
> blocklen
+ (int)PREFIXSIZE
)
2251 if (tpbin
->compressed
) {
2252 /* uncompress whatever we read, if it fails, complain later */
2253 if (tpbin
->flags
== COMPRESS_ZLIB
) {
2255 errx(1,"This restore version doesn't support zlib decompression");
2257 cresult
= uncompress(comprbuf
, &worklen
,
2258 tpbin
->buf
, blocklen
);
2264 reason
= "not enough memory";
2267 reason
= "buffer too small";
2270 reason
= "data error";
2275 if (cresult
== Z_OK
)
2279 #endif /* HAVE_ZLIB */
2281 if (tpbin
->flags
== COMPRESS_BZLIB
) {
2283 errx(1,"This restore version doesn't support bzlib decompression");
2285 unsigned int worklen2
= worklen
;
2286 cresult
= BZ2_bzBuffToBuffDecompress(
2287 comprbuf
, &worklen2
,
2288 tpbin
->buf
, blocklen
, 0, 0);
2295 reason
= "not enough memory";
2297 case BZ_OUTBUFF_FULL
:
2298 reason
= "buffer too small";
2301 case BZ_DATA_ERROR_MAGIC
:
2302 case BZ_UNEXPECTED_EOF
:
2303 reason
= "data error";
2308 if (cresult
== BZ_OK
)
2312 #endif /* HAVE_BZLIB */
2314 if (tpbin
->flags
== COMPRESS_LZO
) {
2316 errx(1,"This restore version doesn't support lzo decompression");
2318 lzo_uint worklen2
= worklen
;
2319 cresult
= lzo1x_decompress(tpbin
->buf
, blocklen
,
2320 comprbuf
, &worklen2
, NULL
);
2327 case LZO_E_EOF_NOT_FOUND
:
2328 reason
= "data error";
2333 if (cresult
== LZO_E_OK
)
2337 #endif /* HAVE_LZO */
2341 output
= tpbin
->buf
;
2345 numtrec
= worklen
/ TP_BSIZE
;
2346 if (worklen
% TP_BSIZE
!= 0)
2347 reason
= "length mismatch";
2351 fprintf(stderr
, "%s compressed block: %d expected: %u\n",
2352 lengtherr
, readsize
, tpbin
->length
+ PREFIXSIZE
);
2353 fprintf(stderr
, "decompression error, block %ld: %s\n",
2354 tpblksread
+1, reason
);
2362 * Print an error message for a read error.
2363 * This was exteracted from the original readtape().
2366 msg_read_error(char *m
)
2368 switch (curfile
.action
) {
2370 fprintf(stderr
, "%s trying to set up tape\n", m
);
2373 fprintf(stderr
, "%s trying to resynchronize\n", m
);
2376 fprintf(stderr
, "%s restoring %s\n", m
, curfile
.name
);
2379 fprintf(stderr
, "%s skipping over inode %lu\n", m
,
2380 (unsigned long)curfile
.ino
);
2384 #endif /* HAVE_ZLIB || HAVE_BZLIB || HAVE_LZO */
2387 * Read the first block and get the blocksize from it. Test
2388 * for a compressed dump tape/file. setup() will make the final
2389 * determination by checking the compressed flag if gethead()
2390 * finds a valid header. The test here is necessary to offset the buffer
2391 * by the size of the compressed prefix. zflag is set here so that
2392 * readtape_set can set the correct function pointer for readtape().
2393 * Note that the first block of each tape/file is not compressed
2394 * and does not have a prefix.
2397 findtapeblksize(void)
2401 struct tapebuf
*tpb
= (struct tapebuf
*) tapebuf
;
2402 struct s_spcl spclpt
;
2404 for (i
= 0; i
< ntrec
; i
++)
2405 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
2409 * For a pipe or file, read in the first record. For a tape, read
2412 len
= magtapein
? ntrec
* TP_BSIZE
: TP_BSIZE
;
2414 if (read_a_block(mt
, tapebuf
, len
, &i
) <= 0)
2415 errx(1, "Tape read error on first record");
2417 memcpy(&spclpt
, tapebuf
, TP_BSIZE
);
2419 if (converthead(&spclpt
) == FAIL
) {
2421 if (converthead(&spclpt
) == FAIL
) {
2422 /* Special case for old compressed tapes with prefix */
2423 if (magtapein
&& (i
% TP_BSIZE
!= 0))
2425 errx(1, "Tape is not a dump tape");
2427 fprintf(stderr
, "Converting to new file system format.\n");
2430 * If the input is from a file or a pipe, we read TP_BSIZE
2431 * bytes looking for a dump header. If the dump is compressed
2432 * we need to read in the rest of the block, as determined
2433 * by c_ntrec in the dump header. The first block of the
2434 * dump is not compressed and does not have a prefix.
2437 if (spclpt
.c_type
== TS_TAPE
2438 && spclpt
.c_flags
& DR_COMPRESSED
) {
2439 /* It's a compressed dump file, read in the */
2440 /* rest of the block based on spclpt.c_ntrec. */
2441 if (spclpt
.c_ntrec
> ntrec
)
2442 errx(1, "Tape blocksize is too large, use "
2443 "\'-b %d\' ", spclpt
.c_ntrec
);
2444 ntrec
= spclpt
.c_ntrec
;
2445 len
= (ntrec
- 1) * TP_BSIZE
;
2449 /* read in the rest of the block based on bufsize */
2450 len
= bufsize
- TP_BSIZE
;
2452 if (read_a_block(mt
, tapebuf
+TP_BSIZE
, len
, &i
) < 0
2453 || (i
!= (long)len
&& i
% TP_BSIZE
!= 0))
2454 errx(1,"Error reading dump file header");
2457 Vprintf(stdout
, "Input block size is %ld\n", ntrec
);
2459 } /* if (!magtapein) */
2462 * If the input is a tape, we tried to read ntrec * TP_BSIZE bytes.
2463 * If the value of ntrec is too large, we read less than
2464 * what we asked for; adjust the value of ntrec and test for
2465 * a compressed dump tape.
2467 if (i
% TP_BSIZE
!= 0) {
2469 /* may be old format compressed dump tape with a prefix */
2470 memcpy(&spclpt
, tpb
->buf
, TP_BSIZE
);
2472 if (converthead(&spclpt
) == FAIL
) {
2474 if (converthead(&spclpt
) == FAIL
)
2475 errx(1, "Tape is not a dump tape");
2476 fprintf(stderr
, "Converting to new file system format.\n");
2478 if (i
% TP_BSIZE
== PREFIXSIZE
2479 && tpb
->compressed
== 0
2480 && spclpt
.c_type
== TS_TAPE
2481 && spclpt
.c_flags
& DR_COMPRESSED
) {
2484 if (tpb
->length
> bufsize
)
2485 errx(1, "Tape blocksize is too large, use "
2486 "\'-b %d\' ", tpb
->length
/ TP_BSIZE
);
2489 errx(1, "Tape block size (%ld) is not a multiple of dump block size (%d)",
2492 ntrec
= i
/ TP_BSIZE
;
2493 if (spclpt
.c_type
== TS_TAPE
) {
2494 if (spclpt
.c_flags
& DR_COMPRESSED
)
2496 if (spclpt
.c_ntrec
> ntrec
)
2497 errx(1, "Tape blocksize is too large, use "
2498 "\'-b %d\' ", spclpt
.c_ntrec
);
2501 Vprintf(stdout
, "Tape block size is %ld\n", ntrec
);
2505 * Read a block of data handling all of the messy details.
2507 static int read_a_block(int fd
, char *buf
, size_t len
, long *lengthread
)
2515 i
= rmtread(buf
, size
);
2518 i
= read(fd
, buf
, size
);
2521 break; /* EOD or error */
2524 break; /* block at a time for mt */
2527 *lengthread
= len
- size
;
2546 setmagtapein(void) {
2547 struct mtget mt_stat
;
2548 static int done
= 0;
2553 /* need to know if input is really from a tape */
2559 magtapein
= ioctl(mt
, MTIOCGET
, (char *)&mt_stat
) == 0;
2562 Vprintf(stdout
,"Input is from a %s %s\n",
2563 host
? "remote" : "local",
2564 magtapein
? "tape" :
2565 Vflag
? "multi-volume (no tape)" : "file/pipe");
2569 * Read the next block from the tape.
2570 * Check to see if it is one of several vintage headers.
2571 * If it is an old style header, convert it to a new style header.
2572 * If it is not any valid header, return an error.
2575 gethead(struct s_spcl
*buf
)
2577 readtape((char *)buf
);
2578 return converthead(buf
);
2582 converthead(struct s_spcl
*buf
)
2590 char dummy
[TP_BSIZE
];
2597 u_int16_t c_inumber
;
2602 u_int16_t odi_nlink
;
2618 if (buf
->c_magic
!= NFS_MAGIC
) {
2619 if (swabi(buf
->c_magic
) != NFS_MAGIC
)
2622 Vprintf(stdout
, "Note: Doing Byte swapping\n");
2626 if (checksum((int *)buf
) == FAIL
)
2629 swabst((u_char
*)"8i4s1l29i528bi192b4i", (u_char
*)buf
);
2632 memcpy(&u_ospcl
.s_ospcl
, buf
, TP_BSIZE
);
2633 if (checksum((int *)(&u_ospcl
.s_ospcl
)) == FAIL
)
2635 if (u_ospcl
.s_ospcl
.c_magic
== OFS_MAGIC
) {
2636 memset((char *)buf
, 0, (long)TP_BSIZE
);
2637 buf
->c_type
= u_ospcl
.s_ospcl
.c_type
;
2638 buf
->c_date
= u_ospcl
.s_ospcl
.c_date
;
2639 buf
->c_ddate
= u_ospcl
.s_ospcl
.c_ddate
;
2640 buf
->c_volume
= u_ospcl
.s_ospcl
.c_volume
;
2641 buf
->c_tapea
= u_ospcl
.s_ospcl
.c_tapea
;
2642 buf
->c_inumber
= u_ospcl
.s_ospcl
.c_inumber
;
2643 buf
->c_checksum
= u_ospcl
.s_ospcl
.c_checksum
;
2644 buf
->c_magic
= u_ospcl
.s_ospcl
.c_magic
;
2645 buf
->c_dinode
.di_mode
= u_ospcl
.s_ospcl
.c_dinode
.odi_mode
;
2646 buf
->c_dinode
.di_nlink
= u_ospcl
.s_ospcl
.c_dinode
.odi_nlink
;
2647 buf
->c_dinode
.di_uid
= u_ospcl
.s_ospcl
.c_dinode
.odi_uid
;
2648 buf
->c_dinode
.di_gid
= u_ospcl
.s_ospcl
.c_dinode
.odi_gid
;
2649 buf
->c_dinode
.di_size
= u_ospcl
.s_ospcl
.c_dinode
.odi_size
;
2650 buf
->c_dinode
.di_rdev
= u_ospcl
.s_ospcl
.c_dinode
.odi_rdev
;
2651 #if defined(__linux__) || defined(sunos)
2652 buf
->c_dinode
.di_atime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
2653 buf
->c_dinode
.di_mtime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
2654 buf
->c_dinode
.di_ctime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
2655 #else /* __linux__ || sunos */
2656 buf
->c_dinode
.di_atime
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
2657 buf
->c_dinode
.di_mtime
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
2658 buf
->c_dinode
.di_ctime
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
2659 #endif /* __linux__ || sunos */
2660 buf
->c_count
= u_ospcl
.s_ospcl
.c_count
;
2661 memmove(buf
->c_addr
, u_ospcl
.s_ospcl
.c_fill
, (long)256);
2663 else if (u_ospcl
.s_ospcl
.c_magic
== FS_UFS2_MAGIC
) {
2664 buf
->c_date
= (int32_t)(*(int64_t *)&u_ospcl
.dummy
[896]);
2665 buf
->c_ddate
= (int32_t)(*(int64_t *)&u_ospcl
.dummy
[904]);
2666 buf
->c_tapea
= (int32_t)(*(int64_t *)&u_ospcl
.dummy
[912]);
2667 buf
->c_firstrec
= (int32_t)(*(int64_t *)&u_ospcl
.dummy
[920]);
2669 buf
->c_extattributes
= 0;
2670 buf
->c_flags
|= DR_NEWINODEFMT
;
2675 buf
->c_magic
= NFS_MAGIC
;
2678 if ((buf
->c_dinode
.di_size
== 0 || buf
->c_dinode
.di_size
> 0xfffffff) &&
2679 (buf
->c_dinode
.di_mode
& IFMT
) == IFDIR
&& Qcvt
== 0) {
2680 qcvt
.qval
= buf
->c_dinode
.di_size
;
2681 if (qcvt
.val
[0] || qcvt
.val
[1]) {
2682 Vprintf(stdout
, "Note: Doing Quad swapping\n");
2687 qcvt
.qval
= buf
->c_dinode
.di_size
;
2689 qcvt
.val
[1] = qcvt
.val
[0];
2691 buf
->c_dinode
.di_size
= qcvt
.qval
;
2695 switch (buf
->c_type
) {
2700 * Have to patch up missing information in bit map headers
2703 buf
->c_dinode
.di_size
= buf
->c_count
* TP_BSIZE
;
2704 if (buf
->c_count
> TP_NINDIR
)
2707 for (i
= 0; i
< buf
->c_count
; i
++)
2712 if ((buf
->c_flags
& DR_NEWINODEFMT
) == 0)
2717 if (buf
->c_flags
& DR_INODEINFO
) {
2718 memcpy(volinfo
, buf
->c_inos
, TP_NINOS
* sizeof(dump_ino_t
));
2720 swabst((u_char
*)"128i", (u_char
*)volinfo
);
2729 panic("gethead: unknown inode type %d\n", buf
->c_type
);
2733 * If we are restoring a filesystem with old format inodes,
2734 * copy the uid/gid to the new location.
2737 buf
->c_dinode
.di_uid
= buf
->c_dinode
.di_ouid
;
2738 buf
->c_dinode
.di_gid
= buf
->c_dinode
.di_ogid
;
2746 converttapebuf(struct tapebuf
*tpb
)
2750 unsigned int length
:28;
2751 unsigned int flags
:3;
2752 unsigned int compressed
:1;
2754 swabst((u_char
*)"i", (u_char
*)tpb
);
2755 memcpy(&tb
, tpb
, 4);
2756 tpb
->length
= tb
.length
;
2757 tpb
->flags
= tb
.flags
;
2758 tpb
->compressed
= tb
.compressed
;
2763 * Check that a header is where it belongs and predict the next header
2766 accthdr(struct s_spcl
*header
)
2768 static dump_ino_t previno
= 0x7fffffff;
2769 static int prevtype
;
2770 static long predict
;
2773 if (header
->c_type
== TS_TAPE
) {
2774 fprintf(stderr
, "Volume header (%s inode format) ",
2775 oldinofmt
? "old" : "new");
2776 if (header
->c_firstrec
)
2777 fprintf(stderr
, "begins with record %d",
2778 header
->c_firstrec
);
2779 fprintf(stderr
, "\n");
2780 previno
= 0x7fffffff;
2783 if (previno
== 0x7fffffff)
2787 fprintf(stderr
, "Dumped inodes map header");
2790 fprintf(stderr
, "Used inodes map header");
2793 fprintf(stderr
, "File header, ino %lu", (unsigned long)previno
);
2796 fprintf(stderr
, "File continuation header, ino %ld", (long)previno
);
2799 fprintf(stderr
, "End of tape header");
2802 if (predict
!= blksread
- 1)
2803 fprintf(stderr
, "; predicted %ld blocks, got %ld blocks",
2804 predict
, blksread
- 1);
2805 fprintf(stderr
, "\n");
2808 if (header
->c_type
!= TS_END
)
2809 for (i
= 0; i
< header
->c_count
; i
++)
2810 if (readmapflag
|| header
->c_addr
[i
] != 0)
2814 prevtype
= header
->c_type
;
2815 previno
= header
->c_inumber
;
2819 * Find an inode header.
2820 * Complain if had to skip, and complain is set.
2823 findinode(struct s_spcl
*header
)
2825 static long skipcnt
= 0;
2829 curfile
.name
= "<name unknown>";
2830 curfile
.action
= UNKNOWN
;
2834 if (header
->c_magic
!= NFS_MAGIC
) {
2836 while (gethead(header
) == FAIL
||
2837 header
->c_date
!= dumpdate
)
2840 switch (header
->c_type
) {
2844 * Skip up to the beginning of the next record
2846 for (i
= 0; i
< header
->c_count
; i
++)
2847 if (header
->c_addr
[i
])
2849 while (gethead(header
) == FAIL
||
2850 header
->c_date
!= dumpdate
)
2855 curfile
.dip
= &header
->c_dinode
;
2856 curfile
.ino
= header
->c_inumber
;
2860 curfile
.ino
= maxino
;
2864 curfile
.name
= "<file removal list>";
2868 curfile
.name
= "<file dump list>";
2872 panic("unexpected tape header\n");
2876 panic("unknown tape header type %d\n", spcl
.c_type
);
2880 } while (header
->c_type
== TS_ADDR
);
2885 fprintf(stderr
, "resync restore, skipped %ld blocks\n",
2895 j
= sizeof(union u_spcl
) / sizeof(int);
2902 /* What happens if we want to read restore tapes
2903 for a 16bit int machine??? */
2909 if (i
!= CHECKSUM
) {
2910 fprintf(stderr
, "Checksum error %o, inode %lu file %s\n", i
,
2911 (unsigned long)curfile
.ino
, curfile
.name
);
2921 #include <varargs.h>
2926 msg(const char *fmt
, ...)
2939 (void)vfprintf(stderr
, fmt
, ap
);
2942 #endif /* RRESTORE */
2945 swab16(u_char
*sp
, int n
)
2950 c
= sp
[0]; sp
[0] = sp
[1]; sp
[1] = c
;
2957 swab32(u_char
*sp
, int n
)
2962 c
= sp
[0]; sp
[0] = sp
[3]; sp
[3] = c
;
2963 c
= sp
[1]; sp
[1] = sp
[2]; sp
[2] = c
;
2970 swab64(u_char
*sp
, int n
)
2975 c
= sp
[0]; sp
[0] = sp
[7]; sp
[7] = c
;
2976 c
= sp
[1]; sp
[1] = sp
[6]; sp
[6] = c
;
2977 c
= sp
[2]; sp
[2] = sp
[5]; sp
[5] = c
;
2978 c
= sp
[3]; sp
[3] = sp
[4]; sp
[4] = c
;
2985 swabst(u_char
*cp
, u_char
*sp
)
2991 case '0': case '1': case '2': case '3': case '4':
2992 case '5': case '6': case '7': case '8': case '9':
2993 n
= (n
* 10) + (*cp
++ - '0');
2996 case 's': case 'w': case 'h':
3014 default: /* Any other character, like 'b' counts as byte. */
3028 swabst((u_char
*)"i", (u_char
*)&x
);
3036 swabst((u_char
*)"l", (u_char
*)&x
);
3042 RequestVol(long tnum
)
3053 int uscsi_flags
; /* read, write, etc. see below */
3054 short uscsi_status
; /* resulting status */
3055 short uscsi_timeout
; /* Command Timeout */
3056 caddr_t uscsi_cdb
; /* cdb to send to target */
3057 caddr_t uscsi_bufaddr
; /* i/o source/destination */
3058 u_int uscsi_buflen
; /* size of i/o to take place */
3059 u_int uscsi_resid
; /* resid from i/o operation */
3060 u_char uscsi_cdblen
; /* # of valid cdb bytes */
3061 u_char uscsi_rqlen
; /* size of uscsi_rqbuf */
3062 u_char uscsi_rqstatus
; /* status of request sense cmd */
3063 u_char uscsi_rqresid
; /* resid of request sense cmd */
3064 caddr_t uscsi_rqbuf
; /* request sense buffer */
3065 void *uscsi_reserved_5
; /* Reserved for Future Use */
3068 #define CDB_GROUP0 6 /* 6-byte cdb's */
3069 #define CDB_GROUP1 10 /* 10-byte cdb's */
3070 #define CDB_GROUP2 10 /* 10-byte cdb's */
3071 #define CDB_GROUP3 0 /* reserved */
3072 #define CDB_GROUP4 16 /* 16-byte cdb's */
3073 #define CDB_GROUP5 12 /* 12-byte cdb's */
3074 #define CDB_GROUP6 0 /* reserved */
3075 #define CDB_GROUP7 0 /* reserved */
3077 #define USCSI_WRITE 0x00000 /* send data to device */
3078 #define USCSI_SILENT 0x00001 /* no error messages */
3079 #define USCSI_DIAGNOSE 0x00002 /* fail if any error occurs */
3080 #define USCSI_ISOLATE 0x00004 /* isolate from normal commands */
3081 #define USCSI_READ 0x00008 /* get data from device */
3082 #define USCSI_RESET 0x04000 /* Reset target */
3083 #define USCSI_RESET_ALL 0x08000 /* Reset all targets */
3084 #define USCSI_RQENABLE 0x10000 /* Enable Request Sense extensions */
3086 #define USCSIIOC (0x04 << 8)
3087 #define USCSICMD (USCSIIOC|201) /* user scsi command */
3089 #define USCSI_TIMEOUT 30
3090 #define USCSI_SHORT_TIMEOUT 900
3091 #define USCSI_LONG_TIMEOUT 14000
3093 #define B(s,i) ((unsigned char)((s) >> i))
3094 #define B1(s) ((unsigned char)(s))
3096 #define MSB4(s,v) *(s)=B(v,24),(s)[1]=B(v,16), (s)[2]=B(v,8), (s)[3]=B1(v)
3100 GetTapePos(long long *pos
)
3103 struct uscsi_cmd scmd
;
3108 (void)memset((void *)buf
, 0, sizeof(buf
));
3109 (void)memset((void *)&scmd
, 0, sizeof(scmd
));
3110 scmd
.uscsi_flags
= USCSI_READ
|USCSI_SILENT
;
3111 scmd
.uscsi_timeout
= USCSI_TIMEOUT
;
3112 scmd
.uscsi_cdb
= buf
;
3113 scmd
.uscsi_cdblen
= CDB_GROUP1
;
3114 buf
[0] = 0x34; /* read position */
3116 (void)memset((void *)parm
, 0, 512);
3117 scmd
.uscsi_bufaddr
= parm
;
3118 scmd
.uscsi_buflen
= 56;
3119 if (ioctl(fdsmtc
, USCSICMD
, &scmd
) == -1) {
3123 (void)memcpy(&lpos
, &parm
[4], sizeof(long));
3129 GotoTapePos(long long pos
)
3132 struct uscsi_cmd scmd
;
3135 long lpos
= (long)pos
;
3137 (void)memset((void *)buf
, 0, sizeof(buf
));
3138 (void)memset((void *)&scmd
, 0, sizeof(scmd
));
3139 scmd
.uscsi_flags
= USCSI_WRITE
|USCSI_SILENT
;
3140 scmd
.uscsi_timeout
= 360; /* 5 Minutes */
3141 scmd
.uscsi_cdb
= buf
;
3142 scmd
.uscsi_cdblen
= CDB_GROUP1
;
3143 buf
[0] = 0x2b; /* locate */
3145 MSB4(&buf
[3], lpos
);
3146 (void)memset((void *)parm
, 0, 512);
3147 scmd
.uscsi_bufaddr
= NULL
;
3148 scmd
.uscsi_buflen
= 0;
3149 if (ioctl(fdsmtc
, USCSICMD
, &scmd
) == -1) {
3157 #define LSEEK_GET_TAPEPOS 10
3158 #define LSEEK_GO2_TAPEPOS 11
3161 typedef struct mt_pos
{
3164 } MTPosRec
, *MTPosPtr
;
3168 * get the current position of the tape
3171 GetTapePos(long long *pos
)
3177 *pos
= (long long) rmtseek((OFF_T
)0, (int)LSEEK_GET_TAPEPOS
);
3186 err
= (ioctl(mt
, MTIOCPOS
, &mtpos
) < 0);
3187 *pos
= (long long)mtpos
;
3190 *pos
= LSEEK(mt
, 0, SEEK_CUR
);
3196 fprintf(stdout
, "[%ld] error: %d (getting tapepos: %lld)\n",
3197 (unsigned long)getpid(), err
, *pos
);
3204 * go to specified position on tape
3207 GotoTapePos(long long pos
)
3213 err
= (rmtseek((OFF_T
)pos
, (int)LSEEK_GO2_TAPEPOS
) < 0);
3220 buf
.mt_count
= (int) pos
;
3221 err
= (ioctl(mt
, MTIOCTOP
, &buf
) < 0);
3224 pos
= LSEEK(mt
, pos
, SEEK_SET
);
3230 fprintf(stdout
, "[%ld] error: %d (setting tapepos: %lld)\n",
3231 (unsigned long)getpid(), err
, pos
);
3236 #endif /* __linux__ */
3239 * read next data from tape to re-sync
3242 ReReadFromTape(void)
3246 if (gethead(&spcl
) == FAIL
) {
3248 fprintf(stdout
, "DEBUG 1 gethead failed\n");
3256 ReReadInodeFromTape(dump_ino_t theino
)
3265 } while (!(spcl
.c_inumber
== theino
&& spcl
.c_type
== TS_INODE
&& spcl
.c_date
== dumpdate
));
3267 fprintf(stderr
, "DEBUG: %ld reads\n", cntloop
);
3268 fprintf(stderr
, "DEBUG: bufsize %ld\n", bufsize
);
3269 fprintf(stderr
, "DEBUG: ntrec %ld\n", ntrec
);
3270 fprintf(stderr
, "DEBUG: %ld reads\n", cntloop
);
3278 OpenSMTCmt(char *themagtape
)
3280 if (GetSCSIIDFromPath(themagtape
, &scsiid
)) {
3281 fprintf(stderr
, "can't get SCSI-ID for %s\n", themagtape
);
3285 fprintf(stderr
, "can't get SCSI-ID for %s\n", themagtape
);
3288 sprintf(smtcpath
, "/dev/rsmtc%ld,0", scsiid
);
3289 if ((fdsmtc
= open(smtcpath
, O_RDWR
)) == -1) {
3290 fprintf(stderr
, "can't open smtc device: %s, %d\n", smtcpath
, errno
);
3296 #endif /* USE_QFA */