]>
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.89 2005/05/02 15:10:46 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");
349 if (gethead(&spcl
) == FAIL
) {
350 blkcnt
--; /* push back this block */
354 if (gethead(&spcl
) == FAIL
)
355 errx(1, "Tape is not a dump tape");
356 fprintf(stderr
, "Converting to new file system format.\n");
360 fprintf(stderr
, "Dump tape is compressed.\n");
361 #if !defined(HAVE_ZLIB) && !defined(HAVE_BZLIB) && !defined(HAVE_LZO)
362 errx(1,"This restore version doesn't support decompression");
363 #endif /* !HAVE_ZLIB && !HAVE_BZLIB */
366 endoftapemark
.s_spcl
.c_magic
= cvtflag
? OFS_MAGIC
: NFS_MAGIC
;
367 endoftapemark
.s_spcl
.c_type
= TS_END
;
368 ip
= (int *)&endoftapemark
;
369 j
= sizeof(union u_spcl
) / sizeof(int);
374 endoftapemark
.s_spcl
.c_checksum
= CHECKSUM
- i
;
376 if (vflag
|| command
== 't' || command
== 'C')
379 if (tapeposflag
&& (unsigned long)spcl
.c_date
!= qfadumpdate
)
380 errx(1, "different QFA/dumpdates detected\n");
382 if (filesys
[0] == '\0') {
384 strncpy(filesys
, spcl
.c_filesys
, NAMELEN
);
385 filesys
[NAMELEN
- 1] = '\0';
386 dirptr
= strstr(filesys
, " (dir");
390 dumptime
= spcl
.c_ddate
;
391 dumpdate
= spcl
.c_date
;
392 if (STAT(".", &stbuf
) < 0)
393 err(1, "cannot stat .");
394 if (stbuf
.st_blksize
> 0 && stbuf
.st_blksize
< TP_BSIZE
)
396 if (stbuf
.st_blksize
>= TP_BSIZE
&& stbuf
.st_blksize
<= MAXBSIZE
)
397 fssize
= stbuf
.st_blksize
;
398 if (((fssize
- 1) & fssize
) != 0)
399 errx(1, "bad block size %ld", fssize
);
400 if (spcl
.c_volume
!= 1)
401 errx(1, "Tape is not volume 1 of the dump");
402 if (gethead(&spcl
) == FAIL
) {
403 Dprintf(stdout
, "header read failed at %ld blocks\n", (long)blksread
);
404 panic("no header after volume mark!\n");
408 if (spcl
.c_type
!= TS_CLRI
)
409 errx(1, "Cannot find file removal list");
410 maxino
= (spcl
.c_count
* TP_BSIZE
* NBBY
) + 1;
411 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
413 errx(1, "no memory for active inode map");
415 curfile
.action
= USING
;
416 getfile(xtrmap
, xtrmapskip
);
417 while (spcl
.c_type
== TS_ADDR
) {
418 /* Recompute maxino and the map */
419 dump_ino_t oldmaxino
= maxino
;
420 maxino
+= (spcl
.c_count
* TP_BSIZE
* NBBY
) + 1;
421 resizemaps(oldmaxino
, maxino
);
424 spcl
.c_dinode
.di_size
= spcl
.c_count
* TP_BSIZE
;
425 getfile(xtrmap
, xtrmapskip
);
427 Dprintf(stdout
, "maxino = %lu\n", (unsigned long)maxino
);
428 if (spcl
.c_type
!= TS_BITS
) {
429 if (spcl
.c_type
== TS_END
) {
430 msg("Cannot find file dump list, assuming empty tape\n");
433 errx(1, "Cannot find file dump list");
435 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
436 if (map
== (char *)NULL
)
437 errx(1, "no memory for file dump list");
439 curfile
.action
= USING
;
440 getfile(xtrmap
, xtrmapskip
);
441 while (spcl
.c_type
== TS_ADDR
) {
442 spcl
.c_dinode
.di_size
= spcl
.c_count
* TP_BSIZE
;
443 getfile(xtrmap
, xtrmapskip
);
446 * If there may be whiteout entries on the tape, pretend that the
447 * whiteout inode exists, so that the whiteout entries can be
451 SETINO(WINO
, dumpmap
);
457 * Prompt user to load a new dump volume.
458 * "Nextvol" is the next suggested volume to use.
459 * This suggested volume is enforced when doing full
460 * or incremental restores, but can be overridden by
461 * the user when only extracting a subset of the files.
466 long newvol
= 0, wantnext
= 0, i
;
467 long saved_blksread
= 0, saved_tpblksread
= 0;
468 union u_spcl tmpspcl
;
469 # define tmpbuf tmpspcl.s_spcl
471 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
, "Mount volume %ld\n", (long)newvol
);
575 fprintf(stderr
, "Mount tape volume %ld\n", (long)newvol
);
577 fprintf(stderr
, "Enter ``none'' if there are no more tapes\n");
579 fprintf(stderr
, "then enter volume name (default: %s) ", magtape
);
581 fprintf(stderr
, "otherwise enter tape name (default: %s) ", magtape
);
583 (void) fflush(stderr
);
584 (void) fgets(buf
, TP_BSIZE
, terminal
);
587 if (!strcmp(buf
, "none\n")) {
591 if (buf
[0] != '\n') {
593 (void) strncpy(magtape
, buf
, sizeof(magtape
));
594 magtape
[sizeof(magtape
) - 1] = '\0';
595 if ((pos
= strchr(magtape
, '\n')))
596 magtape
[pos
- magtape
] = '\0';
599 #if defined(USE_QFA) && defined(sunos)
600 if (createtapeposflag
|| tapeposflag
) {
601 if (OpenSMTCmt(magtape
) < 0) {
610 mt
= rmtopen(magtape
, O_RDONLY
);
613 mt
= OPEN(magtape
, O_RDONLY
, 0);
616 fprintf(stderr
, "Cannot open %s\n", magtape
);
623 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
624 readtape_func
= readtape_set
;
630 if (gethead(&tmpbuf
) == FAIL
) {
631 Dprintf(stdout
, "header read failed at %ld blocks\n", (long)blksread
);
632 fprintf(stderr
, "tape is not dump tape\n");
635 blksread
= saved_blksread
;
636 tpblksread
= saved_tpblksread
;
639 if (tmpbuf
.c_volume
!= volno
) {
640 fprintf(stderr
, "Wrong volume (%d)\n", tmpbuf
.c_volume
);
643 blksread
= saved_blksread
;
644 tpblksread
= saved_tpblksread
;
647 if (tmpbuf
.c_date
!= dumpdate
|| tmpbuf
.c_ddate
!= dumptime
) {
648 fprintf(stderr
, "Wrong dump date\n\tgot: %s",
650 ctime(&tmpbuf
.c_date
));
652 ctime4(&tmpbuf
.c_date
));
654 fprintf(stderr
, "\twanted: %s", ctime(&dumpdate
));
657 blksread
= saved_blksread
;
658 tpblksread
= saved_tpblksread
;
661 tapesread
|= 1 << volno
;
663 * If continuing from the previous volume, skip over any
664 * blocks read already at the end of the previous volume.
666 * If coming to this volume at random, skip to the beginning
667 * of the next record.
670 fprintf(stderr
, "Dump tape is compressed.\n");
671 #if !defined(HAVE_ZLIB) && !defined(HAVE_BZLIB) && !defined(HAVE_LZO)
672 errx(1,"This restore version doesn't support decompression");
673 #endif /* !HAVE_ZLIB && !HAVE_BZLIB */
675 Dprintf(stdout
, "read %ld recs, tape starts with %ld\n",
676 tpblksread
- 1, (long)tmpbuf
.c_firstrec
);
677 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
)) {
679 tpblksread
= tmpbuf
.c_firstrec
+ 1;
680 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
682 } else if (tmpbuf
.c_firstrec
> 0 &&
683 tmpbuf
.c_firstrec
< tpblksread
- 1) {
685 * -1 since we've read the volume header
687 i
= tpblksread
- tmpbuf
.c_firstrec
- 1;
688 Dprintf(stderr
, "Skipping %ld duplicate record%s.\n",
689 (long)i
, i
> 1 ? "s" : "");
694 if (curfile
.action
== USING
) {
696 panic("active file into volume 1\n");
700 * Skip up to the beginning of the next record
702 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
))
703 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
705 (void) gethead(&spcl
);
709 siglongjmp(restart
, 1);
714 * Handle unexpected EOF.
720 if (gettingfile
&& curfile
.action
== USING
) {
721 printf("Warning: %s %s\n",
722 "End-of-input encountered while extracting", curfile
.name
);
724 curfile
.name
= "<name unknown>";
725 curfile
.action
= UNKNOWN
;
727 curfile
.ino
= maxino
;
730 siglongjmp(restart
, 1);
735 * handle multiple dumps per tape by skipping forward to the
743 if (dumpnum
== 1 || volno
!= 1)
746 errx(1, "Cannot have multiple dumps on pipe input");
748 tcom
.mt_count
= dumpnum
- 1;
751 if (rmtioctl(MTFSF
, dumpnum
- 1) < 0) {
752 fprintf(stderr
, "rmtioctl MTFSF: %s\n", strerror(errno
));
757 if (ioctl(mt
, (int)MTIOCTOP
, (char *)&tcom
) < 0) {
758 fprintf(stderr
, "rmtioctl MTFSF: %s\n", strerror(errno
));
760 /* warn("ioctl MTFSF"); */
768 Vprintf(stdout
, "Dump date: %s", ctime(&spcl
.c_date
));
769 Vprintf(stdout
, "Dumped from: %s",
770 (spcl
.c_ddate
== 0) ? "the epoch\n" : ctime(&spcl
.c_ddate
));
771 if (spcl
.c_host
[0] == '\0')
773 Vprintf(stdout
, "Level %d dump of %s on %s:%s\n",
774 spcl
.c_level
, spcl
.c_filesys
, spcl
.c_host
, spcl
.c_dev
);
775 Vprintf(stdout
, "Label: %s\n", spcl
.c_label
);
777 fprintf(stdout
, "Dump date: %s", ctime4(&spcl
.c_date
));
778 fprintf(stdout
, "Dumped from: %s",
779 (spcl
.c_ddate
== 0) ? "the epoch\n" : ctime4(&spcl
.c_ddate
));
780 if (spcl
.c_host
[0] == '\0')
782 fprintf(stdout
, "Level %d dump of %s on %s:%s\n",
783 spcl
.c_level
, spcl
.c_filesys
, spcl
.c_host
, spcl
.c_dev
);
784 fprintf(stdout
, "Label: %s\n", spcl
.c_label
);
793 if (volinfo
[1] == ROOTINO
) {
794 printf("Starting inode numbers by volume:\n");
795 for (i
= 1; i
< (int)TP_NINOS
&& volinfo
[i
] != 0; ++i
)
796 printf("\tVolume %d: %lu\n", i
, (unsigned long)volinfo
[i
]);
802 extractfile(struct entry
*ep
, int doremove
)
806 struct timeval timep
[2];
807 char *name
= myname(ep
);
809 /* If removal is requested (-r mode) do remove it unless
810 * we are extracting a metadata only inode */
811 if (spcl
.c_flags
& DR_METAONLY
) {
812 Vprintf(stdout
, "file %s is metadata only\n", name
);
817 ep
->e_flags
&= ~REMOVED
;
822 curfile
.action
= USING
;
823 #if defined(__linux__) || defined(sunos)
824 timep
[0].tv_sec
= curfile
.dip
->di_atime
.tv_sec
;
825 timep
[0].tv_usec
= curfile
.dip
->di_atime
.tv_usec
;
826 timep
[1].tv_sec
= curfile
.dip
->di_mtime
.tv_sec
;
827 timep
[1].tv_usec
= curfile
.dip
->di_mtime
.tv_usec
;
828 #else /* __linux__ || sunos */
829 timep
[0].tv_sec
= curfile
.dip
->di_atime
;
830 timep
[0].tv_usec
= curfile
.dip
->di_atimensec
/ 1000;
831 timep
[1].tv_sec
= curfile
.dip
->di_mtime
;
832 timep
[1].tv_usec
= curfile
.dip
->di_mtimensec
/ 1000;
833 #endif /* __linux__ */
834 mode
= curfile
.dip
->di_mode
;
835 flags
= curfile
.dip
->di_flags
;
836 switch (mode
& IFMT
) {
839 fprintf(stderr
, "%s: unknown file mode 0%o\n", name
, mode
);
844 Vprintf(stdout
, "skipped socket %s\n", name
);
852 if (ep
== NULL
|| ep
->e_flags
& EXTRACT
)
853 panic("unextracted directory %s\n", name
);
857 Vprintf(stdout
, "extract dir %s\n", name
);
858 ret
= genliteraldir(name
, curfile
.ino
);
866 uid_t luid
= curfile
.dip
->di_uid
;
867 gid_t lgid
= curfile
.dip
->di_gid
;
869 if (! (spcl
.c_flags
& DR_METAONLY
)) {
872 getfile(xtrlnkfile
, xtrlnkskip
);
875 "%s: zero length symbolic link (ignored)\n", name
);
878 if (linkit(lnkbuf
, name
, SYMLINK
) == FAIL
)
885 (void) lchown(name
, luid
, lgid
);
892 Vprintf(stdout
, "extract fifo %s\n", name
);
897 if (! (spcl
.c_flags
& DR_METAONLY
)) {
900 if (mkfifo(name
, mode
) < 0) {
901 warn("%s: cannot create fifo", name
);
906 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
907 (void) chmod(name
, mode
);
910 (void) lsetflags(name
, flags
);
914 warn("%s: cannot call chflags", name
);
915 /* (void) chflags(name, flags); */
918 (void) chflags(name
, flags
);
928 Vprintf(stdout
, "extract special file %s\n", name
);
933 if (! (spcl
.c_flags
& DR_METAONLY
)) {
936 if (mknod(name
, mode
, (int)curfile
.dip
->di_rdev
) < 0) {
937 warn("%s: cannot create special file", name
);
942 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
943 (void) chmod(name
, mode
);
947 warn("%s: lsetflags called on a special file", name
);
948 (void) lsetflags(name
, flags
);
953 warn("%s: cannot call chflags on a special file", name
);
954 /* (void) chflags(name, flags); */
958 warn("%s: chflags called on a special file", name
);
959 (void) chflags(name
, flags
);
970 uid_t luid
= curfile
.dip
->di_uid
;
971 gid_t lgid
= curfile
.dip
->di_gid
;
973 Vprintf(stdout
, "extract file %s\n", name
);
978 if (! (spcl
.c_flags
& DR_METAONLY
)) {
981 if ((ofile
= OPEN(name
, O_WRONLY
| O_CREAT
| O_TRUNC
,
983 warn("%s: cannot create file", name
);
987 getfile(xtrfile
, xtrskip
);
992 (void) chown(name
, luid
, lgid
);
993 (void) chmod(name
, mode
);
996 (void) lsetflags(name
, flags
);
1000 warn("%s: cannot call chflags", name
);
1001 /* (void) chflags(name, flags); */
1004 (void) chflags(name
, flags
);
1008 utimes(name
, timep
);
1016 extractattr(char *path
)
1018 while (spcl
.c_flags
& DR_EXTATTRIBUTES
) {
1019 switch (spcl
.c_extattributes
) {
1020 case EXT_MACOSFNDRINFO
:
1022 (void)extractfinderinfoufs(path
);
1024 msg("MacOSX not supported in this version, skipping\n");
1028 case EXT_MACOSRESFORK
:
1030 (void)extractresourceufs(path
);
1032 msg("MacOSX not supported in this version, skipping\n");
1037 char xattr
[XATTR_MAXSIZE
];
1039 if (readxattr(xattr
) == GOOD
) {
1040 xattr_extract(path
, xattr
);
1045 msg("unexpected inode extension %ld, skipping\n", spcl
.c_extattributes
);
1055 extractfinderinfoufs(char *name
)
1058 char oFileRsrc
[MAXPATHLEN
];
1061 struct timeval timep
[2];
1064 char path
[MAXPATHLEN
], fname
[MAXPATHLEN
];
1066 curfile
.name
= name
;
1067 curfile
.action
= USING
;
1068 timep
[0].tv_sec
= curfile
.dip
->di_atime
.tv_sec
;
1069 timep
[0].tv_usec
= curfile
.dip
->di_atime
.tv_usec
;
1070 timep
[1].tv_sec
= curfile
.dip
->di_mtime
.tv_sec
;
1071 timep
[1].tv_usec
= curfile
.dip
->di_mtime
.tv_usec
;
1072 mode
= curfile
.dip
->di_mode
;
1073 flags
= curfile
.dip
->di_flags
;
1074 uid
= curfile
.dip
->di_uid
;
1075 gid
= curfile
.dip
->di_gid
;
1077 switch (mode
& IFMT
) {
1080 fprintf(stderr
, "%s: (extr. finfoufs) unknown file mode 0%o\n", name
, mode
);
1085 fprintf(stderr
, "%s: (extr. finfoufs[IFDIR]) unknown file mode 0%o\n", name
, mode
);
1094 Vprintf(stdout
, "extract finderinfo file %s\n", name
);
1099 getfile(xtrfilefinderinfo
, xtrskip
);
1101 GetPathFile(name
, path
, fname
);
1102 strcpy(oFileRsrc
, path
);
1103 strcat(oFileRsrc
, "._");
1104 strcat(oFileRsrc
, fname
);
1106 if ((err
= CreateAppleDoubleFileRes(oFileRsrc
, &gFndrInfo
.fndrinfo
,
1107 mode
, flags
, timep
, uid
, gid
)) != 0) {
1108 fprintf(stderr
, "%s: cannot create finderinfo: %s\n",
1109 name
, strerror(errno
));
1120 extractresourceufs(char *name
)
1122 char oFileRsrc
[MAXPATHLEN
];
1125 struct timeval timep
[2];
1126 char path
[MAXPATHLEN
], fname
[MAXPATHLEN
];
1136 curfile
.name
= name
;
1137 curfile
.action
= USING
;
1138 timep
[0].tv_sec
= curfile
.dip
->di_atime
.tv_sec
;
1139 timep
[0].tv_usec
= curfile
.dip
->di_atime
.tv_usec
;
1140 timep
[1].tv_sec
= curfile
.dip
->di_mtime
.tv_sec
;
1141 timep
[1].tv_usec
= curfile
.dip
->di_mtime
.tv_usec
;
1142 mode
= curfile
.dip
->di_mode
;
1143 flags
= curfile
.dip
->di_flags
;
1144 uid
= curfile
.dip
->di_uid
;
1145 gid
= curfile
.dip
->di_gid
;
1146 di_size
= curfile
.dip
->di_size
;
1148 switch (mode
& IFMT
) {
1151 fprintf(stderr
, "%s: (extr. resufs) unknown file mode 0%o\n", name
, mode
);
1156 fprintf(stderr
, "%s: (extr. resufs [IFDIR]) unknown file mode 0%o\n", name
, mode
);
1165 Vprintf(stdout
, "extract resource file %s\n", name
);
1171 GetPathFile(name
, path
, fname
);
1172 strcpy(oFileRsrc
, path
);
1173 strcat(oFileRsrc
, "._");
1174 strcat(oFileRsrc
, fname
);
1176 if ((ofile
= open(oFileRsrc
, O_RDONLY
, 0)) < 0) {
1177 fprintf(stderr
, "%s: cannot read finderinfo: %s\n",
1178 name
, strerror(errno
));
1182 read(ofile
, buf
, 70);
1183 (void) close(ofile
);
1185 hp
= (ASDHeaderPtr
)p
;
1188 p
+= sizeof(ASDHeader
) - CORRECT
;
1189 ep
= (ASDEntryPtr
)p
;
1190 /* the finderinfo entry */
1191 ep
->offset
+= sizeof(ASDEntry
);
1194 p
+= sizeof(ASDEntry
);
1195 /* the finderinfo data */
1196 bcopy(p
, p
+ sizeof(ASDEntry
), INFOLEN
);
1197 ep
= (ASDEntryPtr
)p
;
1198 /* the new resourcefork entry */
1199 ep
->entryID
= EntryRSRCFork
;
1200 ep
->offset
= loff
+ INFOLEN
;
1202 /* write the new appledouble entries to the file */
1203 if ((ofile
= open(oFileRsrc
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666)) < 0) {
1204 fprintf(stderr
, "%s: cannot create resource file: %s\n",
1205 name
, strerror(errno
));
1209 write(ofile
, buf
, 70 + sizeof(ASDEntry
));
1210 /* and add the resource data from tape */
1211 getfile(xtrfile
, xtrskip
);
1213 (void) fchown(ofile
, uid
, gid
);
1214 (void) fchmod(ofile
, mode
);
1215 (void) close(ofile
);
1216 (void) lsetflags(oFileRsrc
, flags
);
1217 utimes(oFileRsrc
, timep
);
1222 #endif /* DUMP_MACOSX */
1225 readxattr(char *buffer
)
1228 msg("reading EA data for inode %lu\n", curfile
.ino
);
1230 curfile
.name
= "EA block";
1231 if (curfile
.dip
->di_size
> XATTR_MAXSIZE
) {
1232 fprintf(stderr
, "EA size too big (%ld)", (long)curfile
.dip
->di_size
);
1237 memset(xattrbuf
, 0, XATTR_MAXSIZE
);
1240 getfile(xtrxattr
, xtrnull
);
1242 memcpy(buffer
, xattrbuf
, XATTR_MAXSIZE
);
1248 * skip over bit maps on the tape
1254 while (spcl
.c_type
== TS_BITS
|| spcl
.c_type
== TS_CLRI
)
1259 * skip over a file on the tape
1265 curfile
.action
= SKIP
;
1266 getfile(xtrnull
, xtrnull
);
1270 * skip over any extended attributes.
1276 while (spcl
.c_flags
& DR_EXTATTRIBUTES
)
1281 * Extract a file from the tape.
1282 * When an allocated block is found it is passed to the fill function;
1283 * when an unallocated block (hole) is found, a zeroed buffer is passed
1284 * to the skip function.
1287 getfile(void (*fill
) __P((char *, size_t)), void (*skip
) __P((char *, size_t)))
1290 volatile int curblk
= 0;
1291 volatile quad_t size
= spcl
.c_dinode
.di_size
;
1292 volatile int last_write_was_hole
= 0;
1293 quad_t origsize
= size
;
1294 static char clearedbuf
[MAXBSIZE
];
1295 char buf
[MAXBSIZE
/ TP_BSIZE
][TP_BSIZE
];
1296 char junk
[TP_BSIZE
];
1298 if (spcl
.c_type
== TS_END
)
1299 panic("ran off end of tape\n");
1300 if (spcl
.c_magic
!= NFS_MAGIC
)
1301 panic("not at beginning of a file\n");
1302 if (!gettingfile
&& setjmp(restart
) != 0)
1306 for (i
= 0; i
< spcl
.c_count
; i
++) {
1307 if (readmapflag
|| spcl
.c_addr
[i
]) {
1308 readtape(&buf
[curblk
++][0]);
1309 if (curblk
== fssize
/ TP_BSIZE
) {
1310 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
1311 fssize
: (curblk
- 1) * TP_BSIZE
+ size
));
1313 last_write_was_hole
= 0;
1317 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
1319 (curblk
- 1) * TP_BSIZE
+ size
));
1322 (*skip
)(clearedbuf
, (long)(size
> TP_BSIZE
?
1324 last_write_was_hole
= 1;
1326 if ((size
-= TP_BSIZE
) <= 0) {
1327 for (i
++; i
< spcl
.c_count
; i
++)
1328 if (readmapflag
|| spcl
.c_addr
[i
])
1333 if (gethead(&spcl
) == GOOD
&& size
> 0) {
1334 if (spcl
.c_type
== TS_ADDR
)
1337 "Missing address (header) block for %s at %ld blocks\n",
1338 curfile
.name
, (long)blksread
);
1341 (*fill
)((char *)buf
, (size_t)((curblk
* TP_BSIZE
) + size
));
1342 last_write_was_hole
= 0;
1345 fprintf(stderr
, "Missing blocks at the end of %s, assuming hole\n", curfile
.name
);
1347 size_t skp
= size
> TP_BSIZE
? TP_BSIZE
: size
;
1348 (*skip
)(clearedbuf
, skp
);
1351 last_write_was_hole
= 1;
1353 if (last_write_was_hole
) {
1354 FTRUNCATE(ofile
, origsize
);
1362 * Write out the next block of a file.
1365 xtrfile(char *buf
, size_t size
)
1370 if (write(ofile
, buf
, (int) size
) == -1)
1371 err(1, "write error extracting inode %lu, name %s\nwrite",
1372 (unsigned long)curfile
.ino
, curfile
.name
);
1377 xtrfilefinderinfo(char *buf
, size_t size
)
1379 bcopy(buf
, &gFndrInfo
, size
);
1381 #endif /* DUMP_MACOSX */
1384 * Skip over a hole in a file.
1388 xtrskip(UNUSED(char *buf
), size_t size
)
1391 if (LSEEK(ofile
, (OFF_T
)size
, SEEK_CUR
) == -1)
1392 err(1, "seek error extracting inode %lu, name %s\nlseek",
1393 (unsigned long)curfile
.ino
, curfile
.name
);
1397 * Collect the next block of a symbolic link.
1400 xtrlnkfile(char *buf
, size_t size
)
1404 if (pathlen
> MAXPATHLEN
) {
1405 buf
[size
- 1] = '\0';
1406 errx(1, "symbolic link name: %s->%s%s; too long %d",
1407 curfile
.name
, lnkbuf
, buf
, pathlen
);
1409 (void) strcat(lnkbuf
, buf
);
1410 lnkbuf
[pathlen
] = '\0';
1414 * Skip over a hole in a symbolic link (should never happen).
1418 xtrlnkskip(UNUSED(char *buf
), UNUSED(size_t size
))
1421 errx(1, "unallocated block in symbolic link %s", curfile
.name
);
1425 * Collect the next block of a bit map.
1428 xtrmap(char *buf
, size_t size
)
1431 memmove(map
, buf
, size
);
1436 * Skip over a hole in a bit map (should never happen).
1440 xtrmapskip(UNUSED(char *buf
), size_t size
)
1443 panic("hole in map\n");
1448 * Noop, when an extraction function is not needed.
1452 xtrnull(UNUSED(char *buf
), UNUSED(size_t size
))
1458 #if COMPARE_ONTHEFLY
1460 * Compare the next block of a file.
1463 xtrcmpfile(char *buf
, size_t size
)
1465 static char cmpbuf
[MAXBSIZE
];
1470 if (read(ifile
, cmpbuf
, size
) != (ssize_t
)size
) {
1471 fprintf(stderr
, "%s: size has changed.\n",
1477 if (memcmp(buf
, cmpbuf
, size
) != 0) {
1478 fprintf(stderr
, "%s: tape and disk copies are different\n",
1486 * Skip over a hole in a file.
1489 xtrcmpskip(UNUSED(char *buf
), size_t size
)
1491 static char cmpbuf
[MAXBSIZE
];
1497 if (read(ifile
, cmpbuf
, size
) != (ssize_t
)size
) {
1498 fprintf(stderr
, "%s: size has changed.\n",
1504 for (i
= 0; i
< (int)size
; ++i
)
1505 if (cmpbuf
[i
] != '\0') {
1506 fprintf(stderr
, "%s: tape and disk copies are different\n",
1512 #endif /* COMPARE_ONTHEFLY */
1515 xtrxattr(char *buf
, size_t size
)
1517 if (xattrlen
+ size
> XATTR_MAXSIZE
) {
1518 fprintf(stderr
, "EA size too big (%ld)", (long)xattrlen
+ size
);
1521 memcpy(xattrbuf
+ xattrlen
, buf
, size
);
1525 #if !COMPARE_ONTHEFLY
1527 do_cmpfiles(int fd_tape
, int fd_disk
, OFF_T size
)
1529 static char buf_tape
[BUFSIZ
];
1530 static char buf_disk
[BUFSIZ
];
1535 if ((n_tape
= read(fd_tape
, buf_tape
, sizeof(buf_tape
))) < 1) {
1536 close(fd_tape
), close(fd_disk
);
1537 panic("do_cmpfiles: unexpected EOF[1]");
1539 if ((n_disk
= read(fd_disk
, buf_disk
, sizeof(buf_tape
))) < 1) {
1540 close(fd_tape
), close(fd_disk
);
1541 panic("do_cmpfiles: unexpected EOF[2]");
1543 if (n_tape
!= n_disk
) {
1544 close(fd_tape
), close(fd_disk
);
1545 panic("do_cmpfiles: sizes different!");
1547 if (memcmp(buf_tape
, buf_disk
, (size_t)n_tape
) != 0) return (1);
1553 /* for debugging compare problems */
1554 #undef COMPARE_FAIL_KEEP_FILE
1557 #ifdef COMPARE_FAIL_KEEP_FILE
1558 /* return true if tapefile should be unlinked after compare */
1563 cmpfiles(char *tapefile
, char *diskfile
, struct STAT
*sbuf_disk
)
1565 struct STAT sbuf_tape
;
1566 int fd_tape
, fd_disk
;
1568 if (STAT(tapefile
, &sbuf_tape
) != 0) {
1569 panic("can't lstat tmp file %s: %s\n", tapefile
,
1574 if (sbuf_disk
->st_size
!= sbuf_tape
.st_size
) {
1576 "%s: size changed from %lld to %lld.\n",
1577 diskfile
, (long long)sbuf_tape
.st_size
, (long long)sbuf_disk
->st_size
);
1579 #ifdef COMPARE_FAIL_KEEP_FILE
1586 if ((fd_tape
= OPEN(tapefile
, O_RDONLY
)) < 0) {
1587 panic("can't open %s: %s\n", tapefile
, strerror(errno
));
1590 if ((fd_disk
= OPEN(diskfile
, O_RDONLY
)) < 0) {
1592 panic("can't open %s: %s\n", diskfile
, strerror(errno
));
1596 if (do_cmpfiles(fd_tape
, fd_disk
, sbuf_tape
.st_size
)) {
1597 fprintf(stderr
, "%s: tape and disk copies are different\n",
1602 #ifdef COMPARE_FAIL_KEEP_FILE
1603 /* rename the file to live in /tmp */
1604 /* rename `tapefile' to /tmp/<basename of diskfile> */
1606 char *p
= strrchr(diskfile
, '/');
1607 char newname
[MAXPATHLEN
];
1609 panic("can't find / in %s\n", diskfile
);
1611 snprintf(newname
, sizeof(newname
), "%s/debug/%s", tmpdir
, p
+ 1);
1612 if (rename(tapefile
, newname
)) {
1613 panic("rename from %s to %s failed: %s\n",
1617 fprintf(stderr
, "*** %s saved to %s\n",
1622 /* don't unlink the file (it's not there anymore */
1631 #ifdef COMPARE_FAIL_KEEP_FILE
1635 #endif /* !COMPARE_ONTHEFLY */
1638 compareattr(char *name
)
1642 while (spcl
.c_flags
& DR_EXTATTRIBUTES
) {
1643 switch (spcl
.c_extattributes
) {
1644 case EXT_MACOSFNDRINFO
:
1645 msg("MacOSX not supported for comparision in this version, skipping\n");
1648 case EXT_MACOSRESFORK
:
1649 msg("MacOSX not supported for comparision in this version, skipping\n");
1653 char xattr
[XATTR_MAXSIZE
];
1655 if (readxattr(xattr
) == GOOD
) {
1656 if (xattr_compare(name
, xattr
) == FAIL
)
1665 msg("unexpected inode extension %ld, skipping\n", spcl
.c_extattributes
);
1670 if (!xattr_done
&& xattr_compare(name
, NULL
) == FAIL
)
1674 #if !COMPARE_ONTHEFLY
1675 static char tmpfilename
[MAXPATHLEN
];
1679 comparefile(char *name
)
1685 unsigned long newflags
;
1688 #if !COMPARE_ONTHEFLY
1689 static char *tmpfile
= NULL
;
1692 curfile
.name
= name
;
1693 curfile
.action
= USING
;
1694 mode
= curfile
.dip
->di_mode
;
1695 flags
= curfile
.dip
->di_flags
;
1696 uid
= curfile
.dip
->di_uid
;
1697 gid
= curfile
.dip
->di_gid
;
1699 if ((mode
& IFMT
) == IFSOCK
) {
1700 Vprintf(stdout
, "skipped socket %s\n", name
);
1705 if ((r
= LSTAT(name
, &sb
)) != 0) {
1706 warn("unable to stat %s", name
);
1712 Vprintf(stdout
, "comparing %s (size: %lld, mode: 0%o)\n", name
,
1713 (long long)sb
.st_size
, mode
);
1715 if (sb
.st_mode
!= mode
) {
1716 fprintf(stderr
, "%s: mode changed from 0%o to 0%o.\n",
1717 name
, mode
& 07777, sb
.st_mode
& 07777);
1720 if (sb
.st_uid
!= uid
) {
1721 fprintf(stderr
, "%s: uid changed from %d to %d.\n",
1722 name
, uid
, sb
.st_uid
);
1725 if (sb
.st_gid
!= gid
) {
1726 fprintf(stderr
, "%s: gid changed from %d to %d.\n",
1727 name
, gid
, sb
.st_gid
);
1731 if (lgetflags(name
, &newflags
) < 0) {
1733 warn("%s: lgetflags failed", name
);
1738 if (newflags
!= flags
) {
1739 fprintf(stderr
, "%s: flags changed from 0x%08x to 0x%08lx.\n",
1740 name
, flags
, newflags
);
1745 if (spcl
.c_flags
& DR_METAONLY
) {
1749 switch (mode
& IFMT
) {
1764 char lbuf
[MAXPATHLEN
+ 1];
1767 if (!(sb
.st_mode
& S_IFLNK
)) {
1768 fprintf(stderr
, "%s: is no longer a symbolic link\n",
1775 getfile(xtrlnkfile
, xtrlnkskip
);
1778 "%s: zero length symbolic link (ignored)\n",
1783 if ((lsize
= readlink(name
, lbuf
, MAXPATHLEN
)) < 0) {
1784 panic("readlink of %s failed: %s\n", name
,
1789 if (strcmp(lbuf
, lnkbuf
) != 0) {
1791 "%s: symbolic link changed from %s to %s.\n",
1792 name
, lnkbuf
, lbuf
);
1802 if (!(sb
.st_mode
& (S_IFCHR
|S_IFBLK
))) {
1803 fprintf(stderr
, "%s: no longer a special file\n",
1810 if (sb
.st_rdev
!= (dev_t
)curfile
.dip
->di_rdev
) {
1812 "%s: device changed from %d,%d to %d,%d.\n",
1814 major(curfile
.dip
->di_rdev
),
1815 minor(curfile
.dip
->di_rdev
),
1825 #if COMPARE_ONTHEFLY
1826 if ((ifile
= OPEN(name
, O_RDONLY
)) < 0) {
1827 warn("can't open %s", name
);
1833 getfile(xtrcmpfile
, xtrcmpskip
);
1836 if (read(ifile
, &c
, 1) != 0) {
1837 fprintf(stderr
, "%s: size has changed.\n",
1847 if (tmpfile
== NULL
) {
1848 /* argument to mktemp() must not be in RO space: */
1849 snprintf(tmpfilename
, sizeof(tmpfilename
), "%s/restoreCXXXXXX", tmpdir
);
1850 tmpfile
= mktemp(&tmpfilename
[0]);
1852 if ((STAT(tmpfile
, &stemp
) == 0) && (unlink(tmpfile
) != 0)) {
1853 panic("cannot delete tmp file %s: %s\n",
1854 tmpfile
, strerror(errno
));
1856 if ((ofile
= OPEN(tmpfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600)) < 0) {
1857 panic("cannot create file temp file %s: %s\n",
1858 name
, strerror(errno
));
1860 getfile(xtrfile
, xtrskip
);
1861 (void) close(ofile
);
1862 #ifdef COMPARE_FAIL_KEEP_FILE
1863 if (cmpfiles(tmpfile
, name
, &sb
))
1866 cmpfiles(tmpfile
, name
, &sb
);
1869 #endif /* COMPARE_ONTHEFLY */
1876 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
1877 static void (*readtape_func
)(char *) = readtape_set
;
1880 * Read TP_BSIZE blocks from the input.
1881 * Handle read errors, and end of media.
1882 * Decompress compressed blocks.
1887 (*readtape_func
)(buf
); /* call the actual processing routine */
1891 * Set function pointer for readtape() routine. zflag and magtapein must
1892 * be correctly set before the first call to readtape().
1895 readtape_set(char *buf
)
1898 readtape_func
= readtape_uncompr
;
1902 readtape_func
= readtape_comprtape
;
1904 readtape_func
= readtape_comprfile
;
1909 #endif /* HAVE_ZLIB || HAVE_BZLIB || HAVE_LZO */
1912 * This is the original readtape(), it's used for reading uncompressed input.
1913 * Read TP_BSIZE blocks from the input.
1914 * Handle read errors, and end of media.
1917 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
1918 readtape_uncompr(char *buf
)
1923 ssize_t rd
, newvol
, i
;
1924 int cnt
, seek_failed
;
1926 if (blkcnt
< numtrec
) {
1927 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1933 for (i
= 0; i
< ntrec
; i
++)
1934 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1937 cnt
= ntrec
* TP_BSIZE
;
1940 if (createtapeposflag
)
1941 (void)GetTapePos(&curtapepos
);
1946 i
= rmtread(&tapebuf
[rd
], cnt
);
1949 i
= read(mt
, &tapebuf
[rd
], cnt
);
1952 * Check for mid-tape short read error.
1953 * If found, skip rest of buffer and start with the next.
1955 if (!pipein
&& numtrec
< ntrec
&& i
> 0) {
1956 Dprintf(stdout
, "mid-media short read error.\n");
1960 * Handle partial block read.
1962 if (pipein
&& i
== 0 && rd
> 0)
1964 else if (i
> 0 && i
!= ntrec
* TP_BSIZE
) {
1973 * Short read. Process the blocks read.
1975 if (i
% TP_BSIZE
!= 0)
1977 "partial block read: %ld should be %ld\n",
1978 (long)i
, ntrec
* TP_BSIZE
);
1979 numtrec
= i
/ TP_BSIZE
;
1983 * Handle read error.
1986 fprintf(stderr
, "Tape read error while ");
1987 switch (curfile
.action
) {
1989 fprintf(stderr
, "trying to set up tape\n");
1992 fprintf(stderr
, "trying to resynchronize\n");
1995 fprintf(stderr
, "restoring %s\n", curfile
.name
);
1998 fprintf(stderr
, "skipping over inode %lu\n",
1999 (unsigned long)curfile
.ino
);
2002 if (!yflag
&& !reply("continue"))
2004 i
= ntrec
* TP_BSIZE
;
2005 memset(tapebuf
, 0, (size_t)i
);
2008 seek_failed
= (rmtseek(i
, 1) < 0);
2011 seek_failed
= (LSEEK(mt
, i
, SEEK_CUR
) == (OFF_T
)-1);
2014 warn("continuation failed");
2015 if (!yflag
&& !reply("assume end-of-tape and continue"))
2021 * Handle end of tape.
2024 Vprintf(stdout
, "End-of-tape encountered\n");
2033 if (rd
% TP_BSIZE
!= 0)
2034 panic("partial block read: %d should be %d\n",
2035 rd
, ntrec
* TP_BSIZE
);
2037 memmove(&tapebuf
[rd
], &endoftapemark
, TP_BSIZE
);
2040 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
2045 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
2048 * Read a compressed format block from a file or pipe and uncompress it.
2049 * Attempt to handle read errors, and end of file.
2052 readtape_comprfile(char *buf
)
2054 long rl
, size
, i
, ret
;
2056 struct tapebuf
*tpb
;
2058 if (blkcnt
< numtrec
) {
2059 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
2064 /* need to read the next block */
2066 for (i
= 0; i
< ntrec
; i
++)
2067 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
2069 tpb
= (struct tapebuf
*) tapebuf
;
2071 /* read the block prefix */
2072 ret
= read_a_block(mt
, tapebuf
, PREFIXSIZE
, &rl
);
2073 converttapebuf(tpb
);
2075 if (Vflag
&& (ret
== 0 || rl
< (int)PREFIXSIZE
|| tpb
->length
== 0))
2082 if (size
> bufsize
) {
2083 /* something's wrong */
2084 Vprintf(stdout
, "Prefix size error, max size %d, got %ld\n",
2087 tpb
->length
= bufsize
;
2089 ret
= read_a_block(mt
, tpb
->buf
, size
, &rl
);
2093 tbufptr
= decompress_tapebuf(tpb
, rl
+ PREFIXSIZE
);
2094 if (tbufptr
== NULL
) {
2095 msg_read_error("File decompression error while");
2096 if (!yflag
&& !reply("continue"))
2098 memset(tapebuf
, 0, bufsize
);
2103 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
2109 /* Errors while reading from a file or pipe are catastrophic. Since
2110 * there are no block boundaries, it's impossible to bypass the
2111 * block in error and find the start of the next block.
2114 /* It's possible to have multiple input files using -M
2115 * and -f file1,file2...
2117 Vprintf(stdout
, "End-of-File encountered\n");
2127 msg_read_error("Read error while");
2128 /* if (!yflag && !reply("continue")) */
2133 * Read compressed data from a tape and uncompress it.
2134 * Handle read errors, and end of media.
2135 * Since a tape consists of separate physical blocks, we try
2136 * to recover from errors by repositioning the tape to the next
2140 readtape_comprtape(char *buf
)
2144 struct tapebuf
*tpb
;
2147 if (blkcnt
< numtrec
) {
2148 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
2153 /* need to read the next block */
2155 for (i
= 0; i
< ntrec
; i
++)
2156 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
2158 tpb
= (struct tapebuf
*) tapebuf
;
2160 /* read the block */
2161 size
= bufsize
+ PREFIXSIZE
;
2162 ret
= read_a_block(mt
, tapebuf
, size
, &rl
);
2166 converttapebuf(tpb
);
2167 tbufptr
= decompress_tapebuf(tpb
, rl
);
2168 if (tbufptr
== NULL
) {
2169 msg_read_error("Tape decompression error while");
2170 if (!yflag
&& !reply("continue"))
2172 memset(tapebuf
, 0, PREFIXSIZE
+ bufsize
);
2178 /* Handle errors: EOT switches to the next volume, other errors
2179 * attempt to position the tape to the next block.
2182 Vprintf(stdout
, "End-of-tape encountered\n");
2191 msg_read_error("Tape read error while");
2192 if (!yflag
&& !reply("continue"))
2194 memset(tapebuf
, 0, PREFIXSIZE
+ bufsize
);
2199 rl
= rmtioctl(MTFSR
, 1);
2205 rl
= ioctl(mt
, MTIOCTOP
, &tcom
);
2209 warn("continuation failed");
2210 if (!yflag
&& !reply("assume end-of-tape and continue"))
2212 ret
= 0; /* end of tape */
2218 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
2224 * Decompress a struct tapebuf into a buffer. readsize is the size read
2225 * from the tape/file and is used for error messages. Returns a pointer
2226 * to the location of the uncompressed buffer or NULL on errors.
2227 * Adjust numtrec and complain for a short block.
2230 decompress_tapebuf(struct tapebuf
*tpbin
, int readsize
)
2232 /* If zflag is on, all blocks have a struct tapebuf prefix */
2233 /* zflag gets set in setup() from the dump header */
2234 int cresult
, blocklen
;
2235 unsigned long worklen
;
2236 char *output
= NULL
,*reason
= NULL
, *lengtherr
= NULL
;
2238 /* build a length error message */
2239 blocklen
= tpbin
->length
;
2240 if (readsize
< blocklen
+ (int)PREFIXSIZE
)
2241 lengtherr
= "short";
2243 if (readsize
> blocklen
+ (int)PREFIXSIZE
)
2248 if (tpbin
->compressed
) {
2249 /* uncompress whatever we read, if it fails, complain later */
2250 if (tpbin
->flags
== COMPRESS_ZLIB
) {
2252 errx(1,"This restore version doesn't support zlib decompression");
2254 cresult
= uncompress(comprbuf
, &worklen
,
2255 tpbin
->buf
, blocklen
);
2261 reason
= "not enough memory";
2264 reason
= "buffer too small";
2267 reason
= "data error";
2272 if (cresult
== Z_OK
)
2276 #endif /* HAVE_ZLIB */
2278 if (tpbin
->flags
== COMPRESS_BZLIB
) {
2280 errx(1,"This restore version doesn't support bzlib decompression");
2282 unsigned int worklen2
= worklen
;
2283 cresult
= BZ2_bzBuffToBuffDecompress(
2284 comprbuf
, &worklen2
,
2285 tpbin
->buf
, blocklen
, 0, 0);
2292 reason
= "not enough memory";
2294 case BZ_OUTBUFF_FULL
:
2295 reason
= "buffer too small";
2298 case BZ_DATA_ERROR_MAGIC
:
2299 case BZ_UNEXPECTED_EOF
:
2300 reason
= "data error";
2305 if (cresult
== BZ_OK
)
2309 #endif /* HAVE_BZLIB */
2311 if (tpbin
->flags
== COMPRESS_LZO
) {
2313 errx(1,"This restore version doesn't support lzo decompression");
2315 lzo_uint worklen2
= worklen
;
2316 cresult
= lzo1x_decompress(tpbin
->buf
, blocklen
,
2317 comprbuf
, &worklen2
, NULL
);
2324 case LZO_E_EOF_NOT_FOUND
:
2325 reason
= "data error";
2330 if (cresult
== LZO_E_OK
)
2334 #endif /* HAVE_LZO */
2338 output
= tpbin
->buf
;
2342 numtrec
= worklen
/ TP_BSIZE
;
2343 if (worklen
% TP_BSIZE
!= 0)
2344 reason
= "length mismatch";
2348 fprintf(stderr
, "%s compressed block: %d expected: %u\n",
2349 lengtherr
, readsize
, tpbin
->length
+ PREFIXSIZE
);
2350 fprintf(stderr
, "decompression error, block %ld: %s\n",
2351 tpblksread
+1, reason
);
2359 * Print an error message for a read error.
2360 * This was exteracted from the original readtape().
2363 msg_read_error(char *m
)
2365 switch (curfile
.action
) {
2367 fprintf(stderr
, "%s trying to set up tape\n", m
);
2370 fprintf(stderr
, "%s trying to resynchronize\n", m
);
2373 fprintf(stderr
, "%s restoring %s\n", m
, curfile
.name
);
2376 fprintf(stderr
, "%s skipping over inode %lu\n", m
,
2377 (unsigned long)curfile
.ino
);
2381 #endif /* HAVE_ZLIB || HAVE_BZLIB || HAVE_LZO */
2384 * Read the first block and get the blocksize from it. Test
2385 * for a compressed dump tape/file. setup() will make the final
2386 * determination by checking the compressed flag if gethead()
2387 * finds a valid header. The test here is necessary to offset the buffer
2388 * by the size of the compressed prefix. zflag is set here so that
2389 * readtape_set can set the correct function pointer for readtape().
2390 * Note that the first block of each tape/file is not compressed
2391 * and does not have a prefix.
2394 findtapeblksize(void)
2398 struct tapebuf
*tpb
= (struct tapebuf
*) tapebuf
;
2399 struct s_spcl spclpt
;
2401 for (i
= 0; i
< ntrec
; i
++)
2402 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
2406 * For a pipe or file, read in the first record. For a tape, read
2409 len
= magtapein
? ntrec
* TP_BSIZE
: TP_BSIZE
;
2411 if (read_a_block(mt
, tapebuf
, len
, &i
) <= 0)
2412 errx(1, "Tape read error on first record");
2414 memcpy(&spclpt
, tapebuf
, TP_BSIZE
);
2415 if (converthead(&spclpt
) == FAIL
) {
2417 if (converthead(&spclpt
) == FAIL
) {
2418 /* Special case for old compressed tapes with prefix */
2419 if (magtapein
&& (i
% TP_BSIZE
!= 0))
2421 errx(1, "Tape is not a dump tape");
2423 fprintf(stderr
, "Converting to new file system format.\n");
2426 * If the input is from a file or a pipe, we read TP_BSIZE
2427 * bytes looking for a dump header. If the dump is compressed
2428 * we need to read in the rest of the block, as determined
2429 * by c_ntrec in the dump header. The first block of the
2430 * dump is not compressed and does not have a prefix.
2433 if (spclpt
.c_type
== TS_TAPE
2434 && spclpt
.c_flags
& DR_COMPRESSED
) {
2435 /* It's a compressed dump file, read in the */
2436 /* rest of the block based on spclpt.c_ntrec. */
2437 if (spclpt
.c_ntrec
> ntrec
)
2438 errx(1, "Tape blocksize is too large, use "
2439 "\'-b %d\' ", spclpt
.c_ntrec
);
2440 ntrec
= spclpt
.c_ntrec
;
2441 len
= (ntrec
- 1) * TP_BSIZE
;
2445 /* read in the rest of the block based on bufsize */
2446 len
= bufsize
- TP_BSIZE
;
2448 if (read_a_block(mt
, tapebuf
+TP_BSIZE
, len
, &i
) < 0
2449 || (i
!= (long)len
&& i
% TP_BSIZE
!= 0))
2450 errx(1,"Error reading dump file header");
2453 Vprintf(stdout
, "Input block size is %ld\n", ntrec
);
2455 } /* if (!magtapein) */
2458 * If the input is a tape, we tried to read ntrec * TP_BSIZE bytes.
2459 * If the value of ntrec is too large, we read less than
2460 * what we asked for; adjust the value of ntrec and test for
2461 * a compressed dump tape.
2463 if (i
% TP_BSIZE
!= 0) {
2465 /* may be old format compressed dump tape with a prefix */
2466 memcpy(&spclpt
, tpb
->buf
, TP_BSIZE
);
2468 if (converthead(&spclpt
) == FAIL
) {
2470 if (converthead(&spclpt
) == FAIL
)
2471 errx(1, "Tape is not a dump tape");
2472 fprintf(stderr
, "Converting to new file system format.\n");
2474 if (i
% TP_BSIZE
== PREFIXSIZE
2475 && tpb
->compressed
== 0
2476 && spclpt
.c_type
== TS_TAPE
2477 && spclpt
.c_flags
& DR_COMPRESSED
) {
2480 if (tpb
->length
> bufsize
)
2481 errx(1, "Tape blocksize is too large, use "
2482 "\'-b %d\' ", tpb
->length
/ TP_BSIZE
);
2485 errx(1, "Tape block size (%ld) is not a multiple of dump block size (%d)",
2488 ntrec
= i
/ TP_BSIZE
;
2489 if (spclpt
.c_type
== TS_TAPE
) {
2490 if (spclpt
.c_flags
& DR_COMPRESSED
)
2492 if (spclpt
.c_ntrec
> ntrec
)
2493 errx(1, "Tape blocksize is too large, use "
2494 "\'-b %d\' ", spclpt
.c_ntrec
);
2497 Vprintf(stdout
, "Tape block size is %ld\n", ntrec
);
2501 * Read a block of data handling all of the messy details.
2503 static int read_a_block(int fd
, char *buf
, size_t len
, long *lengthread
)
2511 i
= rmtread(buf
, size
);
2514 i
= read(fd
, buf
, size
);
2517 break; /* EOD or error */
2520 break; /* block at a time for mt */
2523 *lengthread
= len
- size
;
2542 setmagtapein(void) {
2543 struct mtget mt_stat
;
2544 static int done
= 0;
2549 /* need to know if input is really from a tape */
2555 magtapein
= ioctl(mt
, MTIOCGET
, (char *)&mt_stat
) == 0;
2558 Vprintf(stdout
,"Input is from a %s %s\n",
2559 host
? "remote" : "local",
2560 magtapein
? "tape" :
2561 Vflag
? "multi-volume (no tape)" : "file/pipe");
2565 * Read the next block from the tape.
2566 * Check to see if it is one of several vintage headers.
2567 * If it is an old style header, convert it to a new style header.
2568 * If it is not any valid header, return an error.
2571 gethead(struct s_spcl
*buf
)
2573 readtape((char *)buf
);
2574 return converthead(buf
);
2578 converthead(struct s_spcl
*buf
)
2586 char dummy
[TP_BSIZE
];
2593 u_int16_t c_inumber
;
2598 u_int16_t odi_nlink
;
2614 if (buf
->c_magic
!= NFS_MAGIC
) {
2615 if (swabi(buf
->c_magic
) != NFS_MAGIC
)
2618 Vprintf(stdout
, "Note: Doing Byte swapping\n");
2622 if (checksum((int *)buf
) == FAIL
)
2625 swabst((u_char
*)"8i4s31i528bi192b3i", (u_char
*)buf
);
2628 memcpy(&u_ospcl
.s_ospcl
, buf
, TP_BSIZE
);
2629 if (checksum((int *)(&u_ospcl
.s_ospcl
)) == FAIL
)
2631 if (u_ospcl
.s_ospcl
.c_magic
== OFS_MAGIC
) {
2632 memset((char *)buf
, 0, (long)TP_BSIZE
);
2633 buf
->c_type
= u_ospcl
.s_ospcl
.c_type
;
2634 buf
->c_date
= u_ospcl
.s_ospcl
.c_date
;
2635 buf
->c_ddate
= u_ospcl
.s_ospcl
.c_ddate
;
2636 buf
->c_volume
= u_ospcl
.s_ospcl
.c_volume
;
2637 buf
->c_tapea
= u_ospcl
.s_ospcl
.c_tapea
;
2638 buf
->c_inumber
= u_ospcl
.s_ospcl
.c_inumber
;
2639 buf
->c_checksum
= u_ospcl
.s_ospcl
.c_checksum
;
2640 buf
->c_magic
= u_ospcl
.s_ospcl
.c_magic
;
2641 buf
->c_dinode
.di_mode
= u_ospcl
.s_ospcl
.c_dinode
.odi_mode
;
2642 buf
->c_dinode
.di_nlink
= u_ospcl
.s_ospcl
.c_dinode
.odi_nlink
;
2643 buf
->c_dinode
.di_uid
= u_ospcl
.s_ospcl
.c_dinode
.odi_uid
;
2644 buf
->c_dinode
.di_gid
= u_ospcl
.s_ospcl
.c_dinode
.odi_gid
;
2645 buf
->c_dinode
.di_size
= u_ospcl
.s_ospcl
.c_dinode
.odi_size
;
2646 buf
->c_dinode
.di_rdev
= u_ospcl
.s_ospcl
.c_dinode
.odi_rdev
;
2647 #if defined(__linux__) || defined(sunos)
2648 buf
->c_dinode
.di_atime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
2649 buf
->c_dinode
.di_mtime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
2650 buf
->c_dinode
.di_ctime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
2651 #else /* __linux__ || sunos */
2652 buf
->c_dinode
.di_atime
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
2653 buf
->c_dinode
.di_mtime
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
2654 buf
->c_dinode
.di_ctime
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
2655 #endif /* __linux__ || sunos */
2656 buf
->c_count
= u_ospcl
.s_ospcl
.c_count
;
2657 memmove(buf
->c_addr
, u_ospcl
.s_ospcl
.c_fill
, (long)256);
2659 else if (u_ospcl
.s_ospcl
.c_magic
== FS_UFS2_MAGIC
) {
2660 buf
->c_date
= (int32_t)(*(int64_t *)&u_ospcl
.dummy
[896]);
2661 buf
->c_ddate
= (int32_t)(*(int64_t *)&u_ospcl
.dummy
[904]);
2662 buf
->c_tapea
= (int32_t)(*(int64_t *)&u_ospcl
.dummy
[912]);
2663 buf
->c_firstrec
= (int32_t)(*(int64_t *)&u_ospcl
.dummy
[920]);
2665 buf
->c_extattributes
= 0;
2666 buf
->c_flags
|= DR_NEWINODEFMT
;
2671 buf
->c_magic
= NFS_MAGIC
;
2674 if ((buf
->c_dinode
.di_size
== 0 || buf
->c_dinode
.di_size
> 0xfffffff) &&
2675 (buf
->c_dinode
.di_mode
& IFMT
) == IFDIR
&& Qcvt
== 0) {
2676 qcvt
.qval
= buf
->c_dinode
.di_size
;
2677 if (qcvt
.val
[0] || qcvt
.val
[1]) {
2678 Vprintf(stdout
, "Note: Doing Quad swapping\n");
2683 qcvt
.qval
= buf
->c_dinode
.di_size
;
2685 qcvt
.val
[1] = qcvt
.val
[0];
2687 buf
->c_dinode
.di_size
= qcvt
.qval
;
2691 switch (buf
->c_type
) {
2696 * Have to patch up missing information in bit map headers
2699 buf
->c_dinode
.di_size
= buf
->c_count
* TP_BSIZE
;
2700 if (buf
->c_count
> TP_NINDIR
)
2703 for (i
= 0; i
< buf
->c_count
; i
++)
2708 if ((buf
->c_flags
& DR_NEWINODEFMT
) == 0)
2713 if (buf
->c_flags
& DR_INODEINFO
) {
2714 memcpy(volinfo
, buf
->c_inos
, TP_NINOS
* sizeof(dump_ino_t
));
2716 swabst((u_char
*)"128i", (u_char
*)volinfo
);
2725 panic("gethead: unknown inode type %d\n", buf
->c_type
);
2729 * If we are restoring a filesystem with old format inodes,
2730 * copy the uid/gid to the new location.
2733 buf
->c_dinode
.di_uid
= buf
->c_dinode
.di_ouid
;
2734 buf
->c_dinode
.di_gid
= buf
->c_dinode
.di_ogid
;
2742 converttapebuf(struct tapebuf
*tpb
)
2746 unsigned int length
:28;
2747 unsigned int flags
:3;
2748 unsigned int compressed
:1;
2750 swabst((u_char
*)"i", (u_char
*)tpb
);
2751 memcpy(&tb
, tpb
, 4);
2752 tpb
->length
= tb
.length
;
2753 tpb
->flags
= tb
.flags
;
2754 tpb
->compressed
= tb
.compressed
;
2759 * Check that a header is where it belongs and predict the next header
2762 accthdr(struct s_spcl
*header
)
2764 static dump_ino_t previno
= 0x7fffffff;
2765 static int prevtype
;
2766 static long predict
;
2769 if (header
->c_type
== TS_TAPE
) {
2770 fprintf(stderr
, "Volume header (%s inode format) ",
2771 oldinofmt
? "old" : "new");
2772 if (header
->c_firstrec
)
2773 fprintf(stderr
, "begins with record %d",
2774 header
->c_firstrec
);
2775 fprintf(stderr
, "\n");
2776 previno
= 0x7fffffff;
2779 if (previno
== 0x7fffffff)
2783 fprintf(stderr
, "Dumped inodes map header");
2786 fprintf(stderr
, "Used inodes map header");
2789 fprintf(stderr
, "File header, ino %lu", (unsigned long)previno
);
2792 fprintf(stderr
, "File continuation header, ino %ld", (long)previno
);
2795 fprintf(stderr
, "End of tape header");
2798 if (predict
!= blksread
- 1)
2799 fprintf(stderr
, "; predicted %ld blocks, got %ld blocks",
2800 predict
, blksread
- 1);
2801 fprintf(stderr
, "\n");
2804 if (header
->c_type
!= TS_END
)
2805 for (i
= 0; i
< header
->c_count
; i
++)
2806 if (readmapflag
|| header
->c_addr
[i
] != 0)
2810 prevtype
= header
->c_type
;
2811 previno
= header
->c_inumber
;
2815 * Find an inode header.
2816 * Complain if had to skip, and complain is set.
2819 findinode(struct s_spcl
*header
)
2821 static long skipcnt
= 0;
2825 curfile
.name
= "<name unknown>";
2826 curfile
.action
= UNKNOWN
;
2830 if (header
->c_magic
!= NFS_MAGIC
) {
2832 while (gethead(header
) == FAIL
||
2833 header
->c_date
!= dumpdate
)
2836 switch (header
->c_type
) {
2840 * Skip up to the beginning of the next record
2842 for (i
= 0; i
< header
->c_count
; i
++)
2843 if (header
->c_addr
[i
])
2845 while (gethead(header
) == FAIL
||
2846 header
->c_date
!= dumpdate
)
2851 curfile
.dip
= &header
->c_dinode
;
2852 curfile
.ino
= header
->c_inumber
;
2856 curfile
.ino
= maxino
;
2860 curfile
.name
= "<file removal list>";
2864 curfile
.name
= "<file dump list>";
2868 panic("unexpected tape header\n");
2872 panic("unknown tape header type %d\n", spcl
.c_type
);
2876 } while (header
->c_type
== TS_ADDR
);
2881 fprintf(stderr
, "resync restore, skipped %ld blocks\n",
2891 j
= sizeof(union u_spcl
) / sizeof(int);
2898 /* What happens if we want to read restore tapes
2899 for a 16bit int machine??? */
2905 if (i
!= CHECKSUM
) {
2906 fprintf(stderr
, "Checksum error %o, inode %lu file %s\n", i
,
2907 (unsigned long)curfile
.ino
, curfile
.name
);
2917 #include <varargs.h>
2922 msg(const char *fmt
, ...)
2935 (void)vfprintf(stderr
, fmt
, ap
);
2938 #endif /* RRESTORE */
2941 swab16(u_char
*sp
, int n
)
2946 c
= sp
[0]; sp
[0] = sp
[1]; sp
[1] = c
;
2953 swab32(u_char
*sp
, int n
)
2958 c
= sp
[0]; sp
[0] = sp
[3]; sp
[3] = c
;
2959 c
= sp
[1]; sp
[1] = sp
[2]; sp
[2] = c
;
2966 swab64(u_char
*sp
, int n
)
2971 c
= sp
[0]; sp
[0] = sp
[7]; sp
[7] = c
;
2972 c
= sp
[1]; sp
[1] = sp
[6]; sp
[6] = c
;
2973 c
= sp
[2]; sp
[2] = sp
[5]; sp
[5] = c
;
2974 c
= sp
[3]; sp
[3] = sp
[4]; sp
[4] = c
;
2981 swabst(u_char
*cp
, u_char
*sp
)
2987 case '0': case '1': case '2': case '3': case '4':
2988 case '5': case '6': case '7': case '8': case '9':
2989 n
= (n
* 10) + (*cp
++ - '0');
2992 case 's': case 'w': case 'h':
3010 default: /* Any other character, like 'b' counts as byte. */
3024 swabst((u_char
*)"i", (u_char
*)&x
);
3032 swabst((u_char
*)"l", (u_char
*)&x
);
3038 RequestVol(long tnum
)
3049 int uscsi_flags
; /* read, write, etc. see below */
3050 short uscsi_status
; /* resulting status */
3051 short uscsi_timeout
; /* Command Timeout */
3052 caddr_t uscsi_cdb
; /* cdb to send to target */
3053 caddr_t uscsi_bufaddr
; /* i/o source/destination */
3054 u_int uscsi_buflen
; /* size of i/o to take place */
3055 u_int uscsi_resid
; /* resid from i/o operation */
3056 u_char uscsi_cdblen
; /* # of valid cdb bytes */
3057 u_char uscsi_rqlen
; /* size of uscsi_rqbuf */
3058 u_char uscsi_rqstatus
; /* status of request sense cmd */
3059 u_char uscsi_rqresid
; /* resid of request sense cmd */
3060 caddr_t uscsi_rqbuf
; /* request sense buffer */
3061 void *uscsi_reserved_5
; /* Reserved for Future Use */
3064 #define CDB_GROUP0 6 /* 6-byte cdb's */
3065 #define CDB_GROUP1 10 /* 10-byte cdb's */
3066 #define CDB_GROUP2 10 /* 10-byte cdb's */
3067 #define CDB_GROUP3 0 /* reserved */
3068 #define CDB_GROUP4 16 /* 16-byte cdb's */
3069 #define CDB_GROUP5 12 /* 12-byte cdb's */
3070 #define CDB_GROUP6 0 /* reserved */
3071 #define CDB_GROUP7 0 /* reserved */
3073 #define USCSI_WRITE 0x00000 /* send data to device */
3074 #define USCSI_SILENT 0x00001 /* no error messages */
3075 #define USCSI_DIAGNOSE 0x00002 /* fail if any error occurs */
3076 #define USCSI_ISOLATE 0x00004 /* isolate from normal commands */
3077 #define USCSI_READ 0x00008 /* get data from device */
3078 #define USCSI_RESET 0x04000 /* Reset target */
3079 #define USCSI_RESET_ALL 0x08000 /* Reset all targets */
3080 #define USCSI_RQENABLE 0x10000 /* Enable Request Sense extensions */
3082 #define USCSIIOC (0x04 << 8)
3083 #define USCSICMD (USCSIIOC|201) /* user scsi command */
3085 #define USCSI_TIMEOUT 30
3086 #define USCSI_SHORT_TIMEOUT 900
3087 #define USCSI_LONG_TIMEOUT 14000
3089 #define B(s,i) ((unsigned char)((s) >> i))
3090 #define B1(s) ((unsigned char)(s))
3092 #define MSB4(s,v) *(s)=B(v,24),(s)[1]=B(v,16), (s)[2]=B(v,8), (s)[3]=B1(v)
3096 GetTapePos(long long *pos
)
3099 struct uscsi_cmd scmd
;
3104 (void)memset((void *)buf
, 0, sizeof(buf
));
3105 (void)memset((void *)&scmd
, 0, sizeof(scmd
));
3106 scmd
.uscsi_flags
= USCSI_READ
|USCSI_SILENT
;
3107 scmd
.uscsi_timeout
= USCSI_TIMEOUT
;
3108 scmd
.uscsi_cdb
= buf
;
3109 scmd
.uscsi_cdblen
= CDB_GROUP1
;
3110 buf
[0] = 0x34; /* read position */
3112 (void)memset((void *)parm
, 0, 512);
3113 scmd
.uscsi_bufaddr
= parm
;
3114 scmd
.uscsi_buflen
= 56;
3115 if (ioctl(fdsmtc
, USCSICMD
, &scmd
) == -1) {
3119 (void)memcpy(&lpos
, &parm
[4], sizeof(long));
3125 GotoTapePos(long long pos
)
3128 struct uscsi_cmd scmd
;
3131 long lpos
= (long)pos
;
3133 (void)memset((void *)buf
, 0, sizeof(buf
));
3134 (void)memset((void *)&scmd
, 0, sizeof(scmd
));
3135 scmd
.uscsi_flags
= USCSI_WRITE
|USCSI_SILENT
;
3136 scmd
.uscsi_timeout
= 360; /* 5 Minutes */
3137 scmd
.uscsi_cdb
= buf
;
3138 scmd
.uscsi_cdblen
= CDB_GROUP1
;
3139 buf
[0] = 0x2b; /* locate */
3141 MSB4(&buf
[3], lpos
);
3142 (void)memset((void *)parm
, 0, 512);
3143 scmd
.uscsi_bufaddr
= NULL
;
3144 scmd
.uscsi_buflen
= 0;
3145 if (ioctl(fdsmtc
, USCSICMD
, &scmd
) == -1) {
3153 #define LSEEK_GET_TAPEPOS 10
3154 #define LSEEK_GO2_TAPEPOS 11
3157 typedef struct mt_pos
{
3160 } MTPosRec
, *MTPosPtr
;
3164 * get the current position of the tape
3167 GetTapePos(long long *pos
)
3173 *pos
= (long long) rmtseek((OFF_T
)0, (int)LSEEK_GET_TAPEPOS
);
3182 err
= (ioctl(mt
, MTIOCPOS
, &mtpos
) < 0);
3183 *pos
= (long long)mtpos
;
3186 *pos
= LSEEK(mt
, 0, SEEK_CUR
);
3192 fprintf(stdout
, "[%ld] error: %d (getting tapepos: %lld)\n",
3193 (unsigned long)getpid(), err
, *pos
);
3200 * go to specified position on tape
3203 GotoTapePos(long long pos
)
3209 err
= (rmtseek((OFF_T
)pos
, (int)LSEEK_GO2_TAPEPOS
) < 0);
3216 buf
.mt_count
= (int) pos
;
3217 err
= (ioctl(mt
, MTIOCTOP
, &buf
) < 0);
3220 pos
= LSEEK(mt
, pos
, SEEK_SET
);
3226 fprintf(stdout
, "[%ld] error: %d (setting tapepos: %lld)\n",
3227 (unsigned long)getpid(), err
, pos
);
3232 #endif /* __linux__ */
3235 * read next data from tape to re-sync
3238 ReReadFromTape(void)
3242 if (gethead(&spcl
) == FAIL
) {
3244 fprintf(stdout
, "DEBUG 1 gethead failed\n");
3252 ReReadInodeFromTape(dump_ino_t theino
)
3261 } while (!(spcl
.c_inumber
== theino
&& spcl
.c_type
== TS_INODE
&& spcl
.c_date
== dumpdate
));
3263 fprintf(stderr
, "DEBUG: %ld reads\n", cntloop
);
3264 fprintf(stderr
, "DEBUG: bufsize %ld\n", bufsize
);
3265 fprintf(stderr
, "DEBUG: ntrec %ld\n", ntrec
);
3266 fprintf(stderr
, "DEBUG: %ld reads\n", cntloop
);
3274 OpenSMTCmt(char *themagtape
)
3276 if (GetSCSIIDFromPath(themagtape
, &scsiid
)) {
3277 fprintf(stderr
, "can't get SCSI-ID for %s\n", themagtape
);
3281 fprintf(stderr
, "can't get SCSI-ID for %s\n", themagtape
);
3284 sprintf(smtcpath
, "/dev/rsmtc%ld,0", scsiid
);
3285 if ((fdsmtc
= open(smtcpath
, O_RDWR
)) == -1) {
3286 fprintf(stderr
, "can't open smtc device: %s, %d\n", smtcpath
, errno
);
3292 #endif /* USE_QFA */