]>
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.84 2004/12/15 11:00:01 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 setmagtapein
__P((void));
171 static int extractattr
__P((char *));
172 static void compareattr
__P((char *));
174 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
175 static void newcomprbuf
__P((int));
176 static void (*readtape_func
) __P((char *));
177 static void readtape_set
__P((char *));
178 static void readtape_uncompr
__P((char *));
179 static void readtape_comprfile
__P((char *));
180 static void readtape_comprtape
__P((char *));
181 static char *decompress_tapebuf
__P((struct tapebuf
*, int));
182 static void msg_read_error
__P((char *));
184 static int read_a_block
__P((int, char *, size_t, long *));
185 #define PREFIXSIZE sizeof(struct tapebuf)
187 #define COMPARE_ONTHEFLY 1
190 static int ifile
; /* input file for compare */
191 static int cmperror
; /* compare error */
192 static void xtrcmpfile
__P((char *, size_t));
193 static void xtrcmpskip
__P((char *, size_t));
196 static int readmapflag
;
197 static int readingmaps
; /* set to 1 while reading the maps */
200 static DumpFinderInfo gFndrInfo
;
204 * Set up an input source. This is called from main.c before setup() is.
207 setinput(char *source
)
216 newtapebuf(NTREC
> HIGHDENSITYTREC
? NTREC
: HIGHDENSITYTREC
);
220 if ((n
= strchr(source
, ':'))) {
221 for (i
= 0; i
< (n
- source
); i
++) {
222 if (source
[i
] == '/')
225 if (source
[i
] != '/') {
227 source
= strchr(host
, ':');
229 if (rmthost(host
) == 0)
234 if (strcmp(source
, "-") == 0) {
236 * Since input is coming from a pipe we must establish
237 * our own connection to the terminal.
239 terminal
= fopen(_PATH_TTY
, "r");
240 if (terminal
== NULL
) {
241 warn("cannot open %s", _PATH_TTY
);
242 terminal
= fopen(_PATH_DEVNULL
, "r");
243 if (terminal
== NULL
)
244 err(1, "cannot open %s", _PATH_DEVNULL
);
248 setuid(getuid()); /* no longer need or want root privileges */
250 strncpy(magtapeprefix
, source
, MAXPATHLEN
);
251 magtapeprefix
[MAXPATHLEN
-1] = '\0';
252 snprintf(magtape
, MAXPATHLEN
, "%s%03d", source
, 1);
255 strncpy(magtape
, source
, MAXPATHLEN
);
256 magtape
[MAXPATHLEN
- 1] = '\0';
260 newtapebuf(long size
)
262 static int tapebufsize
= -1;
265 bufsize
= ntrec
* TP_BSIZE
;
266 if (size
<= tapebufsize
)
270 tapebuf
= malloc(size
* TP_BSIZE
+ sizeof(struct tapebuf
));
272 errx(1, "Cannot allocate space for tape buffer");
276 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
278 newcomprbuf(int size
)
280 size_t buf_size
= (size
+1) * TP_BSIZE
+ sizeof(struct tapebuf
);
281 if (buf_size
<= comprlen
)
284 if (comprbuf
!= NULL
)
286 comprbuf
= malloc(comprlen
);
287 if (comprbuf
== NULL
)
288 errx(1, "Cannot allocate space for decompress buffer");
290 #endif /* HAVE_ZLIB || HAVE_BZLIB || HAVE_LZO */
293 * Verify that the tape drive can be accessed and
294 * that it actually is a dump tape.
299 int i
, j
, *ip
, bot_code
;
303 Vprintf(stdout
, "Verify tape and initialize maps\n");
304 if (Afile
== NULL
&& bot_script
) {
305 msg("Launching %s\n", bot_script
);
306 bot_code
= system_command(bot_script
, magtape
, 1);
307 if (bot_code
!= 0 && bot_code
!= 1) {
308 msg("Restore aborted by the beginning of tape script\n");
320 mt
= rmtopen(temptape
, O_RDONLY
);
326 mt
= OPEN(temptape
, O_RDONLY
, 0);
328 err(1, "%s", temptape
);
334 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
335 readtape_func
= readtape_set
;
336 #if defined(HAVE_LZO)
337 if (lzo_init() != LZO_E_OK
) {
338 msg("internal error - lzo_init failed \n");
345 if (gethead(&spcl
) == FAIL
) {
346 blkcnt
--; /* push back this block */
350 if (gethead(&spcl
) == FAIL
)
351 errx(1, "Tape is not a dump tape");
352 fprintf(stderr
, "Converting to new file system format.\n");
356 fprintf(stderr
, "Dump tape is compressed.\n");
357 #if !defined(HAVE_ZLIB) && !defined(HAVE_BZLIB) && !defined(HAVE_LZO)
358 errx(1,"This restore version doesn't support decompression");
359 #endif /* !HAVE_ZLIB && !HAVE_BZLIB */
362 endoftapemark
.s_spcl
.c_magic
= cvtflag
? OFS_MAGIC
: NFS_MAGIC
;
363 endoftapemark
.s_spcl
.c_type
= TS_END
;
364 ip
= (int *)&endoftapemark
;
365 j
= sizeof(union u_spcl
) / sizeof(int);
370 endoftapemark
.s_spcl
.c_checksum
= CHECKSUM
- i
;
372 if (vflag
|| command
== 't' || command
== 'C')
375 if (tapeposflag
&& (unsigned long)spcl
.c_date
!= qfadumpdate
)
376 errx(1, "different QFA/dumpdates detected\n");
378 if (filesys
[0] == '\0') {
380 strncpy(filesys
, spcl
.c_filesys
, NAMELEN
);
381 filesys
[NAMELEN
- 1] = '\0';
382 dirptr
= strstr(filesys
, " (dir");
386 dumptime
= spcl
.c_ddate
;
387 dumpdate
= spcl
.c_date
;
388 if (STAT(".", &stbuf
) < 0)
389 err(1, "cannot stat .");
390 if (stbuf
.st_blksize
> 0 && stbuf
.st_blksize
< TP_BSIZE
)
392 if (stbuf
.st_blksize
>= TP_BSIZE
&& stbuf
.st_blksize
<= MAXBSIZE
)
393 fssize
= stbuf
.st_blksize
;
394 if (((fssize
- 1) & fssize
) != 0)
395 errx(1, "bad block size %ld", fssize
);
396 if (spcl
.c_volume
!= 1)
397 errx(1, "Tape is not volume 1 of the dump");
398 if (gethead(&spcl
) == FAIL
) {
399 Dprintf(stdout
, "header read failed at %ld blocks\n", (long)blksread
);
400 panic("no header after volume mark!\n");
404 if (spcl
.c_type
!= TS_CLRI
)
405 errx(1, "Cannot find file removal list");
406 maxino
= (spcl
.c_count
* TP_BSIZE
* NBBY
) + 1;
407 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
409 errx(1, "no memory for active inode map");
411 curfile
.action
= USING
;
412 getfile(xtrmap
, xtrmapskip
);
413 while (spcl
.c_type
== TS_ADDR
) {
414 /* Recompute maxino and the map */
415 dump_ino_t oldmaxino
= maxino
;
416 maxino
+= (spcl
.c_count
* TP_BSIZE
* NBBY
) + 1;
417 resizemaps(oldmaxino
, maxino
);
420 spcl
.c_dinode
.di_size
= spcl
.c_count
* TP_BSIZE
;
421 getfile(xtrmap
, xtrmapskip
);
423 Dprintf(stdout
, "maxino = %lu\n", (unsigned long)maxino
);
424 if (spcl
.c_type
!= TS_BITS
) {
425 if (spcl
.c_type
== TS_END
) {
426 msg("Cannot find file dump list, assuming empty tape\n");
429 errx(1, "Cannot find file dump list");
431 map
= calloc((unsigned)1, (unsigned)howmany(maxino
, NBBY
));
432 if (map
== (char *)NULL
)
433 errx(1, "no memory for file dump list");
435 curfile
.action
= USING
;
436 getfile(xtrmap
, xtrmapskip
);
437 while (spcl
.c_type
== TS_ADDR
) {
438 spcl
.c_dinode
.di_size
= spcl
.c_count
* TP_BSIZE
;
439 getfile(xtrmap
, xtrmapskip
);
442 * If there may be whiteout entries on the tape, pretend that the
443 * whiteout inode exists, so that the whiteout entries can be
447 SETINO(WINO
, dumpmap
);
453 * Prompt user to load a new dump volume.
454 * "Nextvol" is the next suggested volume to use.
455 * This suggested volume is enforced when doing full
456 * or incremental restores, but can be overridden by
457 * the user when only extracting a subset of the files.
462 long newvol
= 0, wantnext
= 0, i
;
463 long saved_blksread
= 0, saved_tpblksread
= 0;
464 union u_spcl tmpspcl
;
465 # define tmpbuf tmpspcl.s_spcl
467 int haderror
= 0, bot_code
= 1;
477 panic("Changing volumes on pipe input?\n");
482 saved_blksread
= blksread
;
483 saved_tpblksread
= tpblksread
;
484 #if defined(USE_QFA) && defined(sunos)
485 if (createtapeposflag
|| tapeposflag
)
490 exit(1); /* pipes do not get a second chance */
491 if (aflag
|| curfile
.action
!= SKIP
) {
498 while (newvol
<= 0) {
499 if (tapesread
== 0) {
500 fprintf(stderr
, "%s%s%s%s%s",
501 "You have not read any volumes yet.\n",
502 "Unless you know which volume your",
503 " file(s) are on you should start\n",
504 "with the last volume and work",
505 " towards the first.\n");
507 fprintf(stderr
, "You have read volumes");
509 for (i
= 1; i
< 32; i
++)
510 if (tapesread
& (1 << i
)) {
511 fprintf(stderr
, "%s%ld", buf
, (long)i
);
514 fprintf(stderr
, "\n");
517 fprintf(stderr
, "Specify next volume # (none if no more volumes): ");
518 (void) fflush(stderr
);
519 (void) fgets(buf
, TP_BSIZE
, terminal
);
520 } while (!feof(terminal
) && buf
[0] == '\n');
523 if (!strcmp(buf
, "none\n")) {
530 "Volume numbers are positive numerics\n");
533 if (newvol
== volno
) {
534 tapesread
|= 1 << volno
;
535 #if defined(USE_QFA) && defined(sunos)
536 if (createtapeposflag
|| tapeposflag
) {
537 if (OpenSMTCmt(magtape
) < 0) {
549 * if using an archive file, reset its name so readtape()
550 * could properly use remote access.
555 snprintf(magtape
, MAXPATHLEN
, "%s%03ld", magtapeprefix
, newvol
);
556 magtape
[MAXPATHLEN
- 1] = '\0';
558 if (bot_script
&& !haderror
) {
559 msg("Launching %s\n", bot_script
);
560 bot_code
= system_command(bot_script
, magtape
, newvol
);
561 if (bot_code
!= 0 && bot_code
!= 1) {
562 msg("Restore aborted by the beginning of tape script\n");
566 if (haderror
|| (bot_code
&& !Mflag
)) {
569 fprintf(stderr
, "Mount volume %ld\n", (long)newvol
);
571 fprintf(stderr
, "Mount tape volume %ld\n", (long)newvol
);
573 fprintf(stderr
, "Enter ``none'' if there are no more tapes\n");
575 fprintf(stderr
, "then enter volume name (default: %s) ", magtape
);
577 fprintf(stderr
, "otherwise enter tape name (default: %s) ", magtape
);
579 (void) fflush(stderr
);
580 (void) fgets(buf
, TP_BSIZE
, terminal
);
583 if (!strcmp(buf
, "none\n")) {
587 if (buf
[0] != '\n') {
589 (void) strncpy(magtape
, buf
, sizeof(magtape
));
590 magtape
[sizeof(magtape
) - 1] = '\0';
591 if ((pos
= strchr(magtape
, '\n')))
592 magtape
[pos
- magtape
] = '\0';
595 #if defined(USE_QFA) && defined(sunos)
596 if (createtapeposflag
|| tapeposflag
) {
597 if (OpenSMTCmt(magtape
) < 0) {
606 mt
= rmtopen(magtape
, O_RDONLY
);
609 mt
= OPEN(magtape
, O_RDONLY
, 0);
612 fprintf(stderr
, "Cannot open %s\n", magtape
);
619 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
620 readtape_func
= readtape_set
;
626 if (gethead(&tmpbuf
) == FAIL
) {
627 Dprintf(stdout
, "header read failed at %ld blocks\n", (long)blksread
);
628 fprintf(stderr
, "tape is not dump tape\n");
631 blksread
= saved_blksread
;
632 tpblksread
= saved_tpblksread
;
635 if (tmpbuf
.c_volume
!= volno
) {
636 fprintf(stderr
, "Wrong volume (%d)\n", tmpbuf
.c_volume
);
639 blksread
= saved_blksread
;
640 tpblksread
= saved_tpblksread
;
643 if (tmpbuf
.c_date
!= dumpdate
|| tmpbuf
.c_ddate
!= dumptime
) {
644 fprintf(stderr
, "Wrong dump date\n\tgot: %s",
646 ctime(&tmpbuf
.c_date
));
648 ctime4(&tmpbuf
.c_date
));
650 fprintf(stderr
, "\twanted: %s", ctime(&dumpdate
));
653 blksread
= saved_blksread
;
654 tpblksread
= saved_tpblksread
;
657 tapesread
|= 1 << volno
;
659 * If continuing from the previous volume, skip over any
660 * blocks read already at the end of the previous volume.
662 * If coming to this volume at random, skip to the beginning
663 * of the next record.
666 fprintf(stderr
, "Dump tape is compressed.\n");
667 #if !defined(HAVE_ZLIB) && !defined(HAVE_BZLIB) && !defined(HAVE_LZO)
668 errx(1,"This restore version doesn't support decompression");
669 #endif /* !HAVE_ZLIB && !HAVE_BZLIB */
671 Dprintf(stdout
, "read %ld recs, tape starts with %ld\n",
672 tpblksread
- 1, (long)tmpbuf
.c_firstrec
);
673 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
)) {
675 tpblksread
= tmpbuf
.c_firstrec
+ 1;
676 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
678 } else if (tmpbuf
.c_firstrec
> 0 &&
679 tmpbuf
.c_firstrec
< tpblksread
- 1) {
681 * -1 since we've read the volume header
683 i
= tpblksread
- tmpbuf
.c_firstrec
- 1;
684 Dprintf(stderr
, "Skipping %ld duplicate record%s.\n",
685 (long)i
, i
> 1 ? "s" : "");
690 if (curfile
.action
== USING
) {
692 panic("active file into volume 1\n");
696 * Skip up to the beginning of the next record
698 if (tmpbuf
.c_type
== TS_TAPE
&& (tmpbuf
.c_flags
& DR_NEWHEADER
))
699 for (i
= tmpbuf
.c_count
; i
> 0; i
--)
701 (void) gethead(&spcl
);
705 siglongjmp(restart
, 1);
710 * Handle unexpected EOF.
716 if (gettingfile
&& curfile
.action
== USING
) {
717 printf("Warning: %s %s\n",
718 "End-of-input encountered while extracting", curfile
.name
);
720 curfile
.name
= "<name unknown>";
721 curfile
.action
= UNKNOWN
;
723 curfile
.ino
= maxino
;
726 siglongjmp(restart
, 1);
731 * handle multiple dumps per tape by skipping forward to the
739 if (dumpnum
== 1 || volno
!= 1)
742 errx(1, "Cannot have multiple dumps on pipe input");
744 tcom
.mt_count
= dumpnum
- 1;
747 if (rmtioctl(MTFSF
, dumpnum
- 1) < 0) {
748 fprintf(stderr
, "rmtioctl MTFSF: %s\n", strerror(errno
));
753 if (ioctl(mt
, (int)MTIOCTOP
, (char *)&tcom
) < 0) {
754 fprintf(stderr
, "rmtioctl MTFSF: %s\n", strerror(errno
));
756 /* warn("ioctl MTFSF"); */
764 Vprintf(stdout
, "Dump date: %s", ctime(&spcl
.c_date
));
765 Vprintf(stdout
, "Dumped from: %s",
766 (spcl
.c_ddate
== 0) ? "the epoch\n" : ctime(&spcl
.c_ddate
));
767 if (spcl
.c_host
[0] == '\0')
769 Vprintf(stdout
, "Level %d dump of %s on %s:%s\n",
770 spcl
.c_level
, spcl
.c_filesys
, spcl
.c_host
, spcl
.c_dev
);
771 Vprintf(stdout
, "Label: %s\n", spcl
.c_label
);
773 fprintf(stdout
, "Dump date: %s", ctime4(&spcl
.c_date
));
774 fprintf(stdout
, "Dumped from: %s",
775 (spcl
.c_ddate
== 0) ? "the epoch\n" : ctime4(&spcl
.c_ddate
));
776 if (spcl
.c_host
[0] == '\0')
778 fprintf(stdout
, "Level %d dump of %s on %s:%s\n",
779 spcl
.c_level
, spcl
.c_filesys
, spcl
.c_host
, spcl
.c_dev
);
780 fprintf(stdout
, "Label: %s\n", spcl
.c_label
);
789 if (volinfo
[1] == ROOTINO
) {
790 printf("Starting inode numbers by volume:\n");
791 for (i
= 1; i
< (int)TP_NINOS
&& volinfo
[i
] != 0; ++i
)
792 printf("\tVolume %d: %lu\n", i
, (unsigned long)volinfo
[i
]);
798 extractfile(struct entry
*ep
, int doremove
)
802 struct timeval timep
[2];
803 char *name
= myname(ep
);
805 /* If removal is requested (-r mode) do remove it unless
806 * we are extracting a metadata only inode */
807 if (spcl
.c_flags
& DR_METAONLY
) {
808 Vprintf(stdout
, "file %s is metadata only\n", name
);
813 ep
->e_flags
&= ~REMOVED
;
818 curfile
.action
= USING
;
819 #if defined(__linux__) || defined(sunos)
820 timep
[0].tv_sec
= curfile
.dip
->di_atime
.tv_sec
;
821 timep
[0].tv_usec
= curfile
.dip
->di_atime
.tv_usec
;
822 timep
[1].tv_sec
= curfile
.dip
->di_mtime
.tv_sec
;
823 timep
[1].tv_usec
= curfile
.dip
->di_mtime
.tv_usec
;
824 #else /* __linux__ || sunos */
825 timep
[0].tv_sec
= curfile
.dip
->di_atime
;
826 timep
[0].tv_usec
= curfile
.dip
->di_atimensec
/ 1000;
827 timep
[1].tv_sec
= curfile
.dip
->di_mtime
;
828 timep
[1].tv_usec
= curfile
.dip
->di_mtimensec
/ 1000;
829 #endif /* __linux__ */
830 mode
= curfile
.dip
->di_mode
;
831 flags
= curfile
.dip
->di_flags
;
832 switch (mode
& IFMT
) {
835 fprintf(stderr
, "%s: unknown file mode 0%o\n", name
, mode
);
840 Vprintf(stdout
, "skipped socket %s\n", name
);
848 if (ep
== NULL
|| ep
->e_flags
& EXTRACT
)
849 panic("unextracted directory %s\n", name
);
853 Vprintf(stdout
, "extract dir %s\n", name
);
854 ret
= genliteraldir(name
, curfile
.ino
);
862 uid_t luid
= curfile
.dip
->di_uid
;
863 gid_t lgid
= curfile
.dip
->di_gid
;
865 if (! (spcl
.c_flags
& DR_METAONLY
)) {
868 getfile(xtrlnkfile
, xtrlnkskip
);
871 "%s: zero length symbolic link (ignored)\n", name
);
874 if (linkit(lnkbuf
, name
, SYMLINK
) == FAIL
)
881 (void) lchown(name
, luid
, lgid
);
888 Vprintf(stdout
, "extract fifo %s\n", name
);
893 if (! (spcl
.c_flags
& DR_METAONLY
)) {
896 if (mkfifo(name
, mode
) < 0) {
897 warn("%s: cannot create fifo", name
);
902 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
903 (void) chmod(name
, mode
);
906 (void) lsetflags(name
, flags
);
910 warn("%s: cannot call chflags", name
);
911 /* (void) chflags(name, flags); */
914 (void) chflags(name
, flags
);
924 Vprintf(stdout
, "extract special file %s\n", name
);
929 if (! (spcl
.c_flags
& DR_METAONLY
)) {
932 if (mknod(name
, mode
, (int)curfile
.dip
->di_rdev
) < 0) {
933 warn("%s: cannot create special file", name
);
938 (void) chown(name
, curfile
.dip
->di_uid
, curfile
.dip
->di_gid
);
939 (void) chmod(name
, mode
);
943 warn("%s: lsetflags called on a special file", name
);
944 (void) lsetflags(name
, flags
);
949 warn("%s: cannot call chflags on a special file", name
);
950 /* (void) chflags(name, flags); */
954 warn("%s: chflags called on a special file", name
);
955 (void) chflags(name
, flags
);
966 uid_t luid
= curfile
.dip
->di_uid
;
967 gid_t lgid
= curfile
.dip
->di_gid
;
969 Vprintf(stdout
, "extract file %s\n", name
);
974 if (! (spcl
.c_flags
& DR_METAONLY
)) {
977 if ((ofile
= OPEN(name
, O_WRONLY
| O_CREAT
| O_TRUNC
,
979 warn("%s: cannot create file", name
);
983 getfile(xtrfile
, xtrskip
);
988 (void) chown(name
, luid
, lgid
);
989 (void) chmod(name
, mode
);
992 (void) lsetflags(name
, flags
);
996 warn("%s: cannot call chflags", name
);
997 /* (void) chflags(name, flags); */
1000 (void) chflags(name
, flags
);
1004 utimes(name
, timep
);
1012 extractattr(char *path
)
1014 while (spcl
.c_flags
& DR_EXTATTRIBUTES
) {
1015 switch (spcl
.c_extattributes
) {
1016 case EXT_MACOSFNDRINFO
:
1018 (void)extractfinderinfoufs(path
);
1020 msg("MacOSX not supported in this version, skipping\n");
1024 case EXT_MACOSRESFORK
:
1026 (void)extractresourceufs(path
);
1028 msg("MacOSX not supported in this version, skipping\n");
1033 msg("EA/ACLs not supported in this version, skipping\n");
1037 msg("unexpected inode extension %ld, skipping\n", spcl
.c_extattributes
);
1047 extractfinderinfoufs(char *name
)
1050 char oFileRsrc
[MAXPATHLEN
];
1053 struct timeval timep
[2];
1056 char path
[MAXPATHLEN
], fname
[MAXPATHLEN
];
1059 curfile
.name
= name
;
1060 curfile
.action
= USING
;
1061 timep
[0].tv_sec
= curfile
.dip
->di_atime
.tv_sec
;
1062 timep
[0].tv_usec
= curfile
.dip
->di_atime
.tv_usec
;
1063 timep
[1].tv_sec
= curfile
.dip
->di_mtime
.tv_sec
;
1064 timep
[1].tv_usec
= curfile
.dip
->di_mtime
.tv_usec
;
1065 mode
= curfile
.dip
->di_mode
;
1066 flags
= curfile
.dip
->di_flags
;
1067 uid
= curfile
.dip
->di_uid
;
1068 gid
= curfile
.dip
->di_gid
;
1070 switch (mode
& IFMT
) {
1073 fprintf(stderr
, "%s: (extr. finfoufs) unknown file mode 0%o\n", name
, mode
);
1078 fprintf(stderr
, "%s: (extr. finfoufs[IFDIR]) unknown file mode 0%o\n", name
, mode
);
1087 Vprintf(stdout
, "extract finderinfo file %s\n", name
);
1092 getfile(xtrfilefinderinfo
, xtrskip
);
1094 GetPathFile(name
, path
, fname
);
1095 strcpy(oFileRsrc
, path
);
1096 strcat(oFileRsrc
, "._");
1097 strcat(oFileRsrc
, fname
);
1099 if ((err
= CreateAppleDoubleFileRes(oFileRsrc
, &gFndrInfo
.fndrinfo
,
1100 mode
, flags
, timep
, uid
, gid
)) != 0) {
1101 fprintf(stderr
, "%s: cannot create finderinfo: %s\n",
1102 name
, strerror(errno
));
1113 extractresourceufs(char *name
)
1115 char oFileRsrc
[MAXPATHLEN
];
1118 struct timeval timep
[2];
1119 char path
[MAXPATHLEN
], fname
[MAXPATHLEN
];
1129 curfile
.name
= name
;
1130 curfile
.action
= USING
;
1131 timep
[0].tv_sec
= curfile
.dip
->di_atime
.tv_sec
;
1132 timep
[0].tv_usec
= curfile
.dip
->di_atime
.tv_usec
;
1133 timep
[1].tv_sec
= curfile
.dip
->di_mtime
.tv_sec
;
1134 timep
[1].tv_usec
= curfile
.dip
->di_mtime
.tv_usec
;
1135 mode
= curfile
.dip
->di_mode
;
1136 flags
= curfile
.dip
->di_flags
;
1137 uid
= curfile
.dip
->di_uid
;
1138 gid
= curfile
.dip
->di_gid
;
1139 di_size
= curfile
.dip
->di_size
;
1141 switch (mode
& IFMT
) {
1144 fprintf(stderr
, "%s: (extr. resufs) unknown file mode 0%o\n", name
, mode
);
1149 fprintf(stderr
, "%s: (extr. resufs [IFDIR]) unknown file mode 0%o\n", name
, mode
);
1158 Vprintf(stdout
, "extract resource file %s\n", name
);
1164 GetPathFile(name
, path
, fname
);
1165 strcpy(oFileRsrc
, path
);
1166 strcat(oFileRsrc
, "._");
1167 strcat(oFileRsrc
, fname
);
1169 if ((ofile
= open(oFileRsrc
, O_RDONLY
, 0)) < 0) {
1170 fprintf(stderr
, "%s: cannot read finderinfo: %s\n",
1171 name
, strerror(errno
));
1175 read(ofile
, buf
, 70);
1176 (void) close(ofile
);
1178 hp
= (ASDHeaderPtr
)p
;
1181 p
+= sizeof(ASDHeader
) - CORRECT
;
1182 ep
= (ASDEntryPtr
)p
;
1183 /* the finderinfo entry */
1184 ep
->offset
+= sizeof(ASDEntry
);
1187 p
+= sizeof(ASDEntry
);
1188 /* the finderinfo data */
1189 bcopy(p
, p
+ sizeof(ASDEntry
), INFOLEN
);
1190 ep
= (ASDEntryPtr
)p
;
1191 /* the new resourcefork entry */
1192 ep
->entryID
= EntryRSRCFork
;
1193 ep
->offset
= loff
+ INFOLEN
;
1195 /* write the new appledouble entries to the file */
1196 if ((ofile
= open(oFileRsrc
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666)) < 0) {
1197 fprintf(stderr
, "%s: cannot create resource file: %s\n",
1198 name
, strerror(errno
));
1202 write(ofile
, buf
, 70 + sizeof(ASDEntry
));
1203 /* and add the resource data from tape */
1204 getfile(xtrfile
, xtrskip
);
1206 (void) fchown(ofile
, uid
, gid
);
1207 (void) fchmod(ofile
, mode
);
1208 (void) close(ofile
);
1209 (void) lsetflags(oFileRsrc
, flags
);
1210 utimes(oFileRsrc
, timep
);
1215 #endif /* DUMP_MACOSX */
1218 * skip over bit maps on the tape
1224 while (spcl
.c_type
== TS_BITS
|| spcl
.c_type
== TS_CLRI
)
1229 * skip over a file on the tape
1235 curfile
.action
= SKIP
;
1236 getfile(xtrnull
, xtrnull
);
1240 * skip over any extended attributes.
1246 while (spcl
.c_flags
& DR_EXTATTRIBUTES
)
1251 * Extract a file from the tape.
1252 * When an allocated block is found it is passed to the fill function;
1253 * when an unallocated block (hole) is found, a zeroed buffer is passed
1254 * to the skip function.
1257 getfile(void (*fill
) __P((char *, size_t)), void (*skip
) __P((char *, size_t)))
1260 volatile int curblk
= 0;
1261 volatile quad_t size
= spcl
.c_dinode
.di_size
;
1262 volatile int last_write_was_hole
= 0;
1263 quad_t origsize
= size
;
1264 static char clearedbuf
[MAXBSIZE
];
1265 char buf
[MAXBSIZE
/ TP_BSIZE
][TP_BSIZE
];
1266 char junk
[TP_BSIZE
];
1268 if (spcl
.c_type
== TS_END
)
1269 panic("ran off end of tape\n");
1270 if (spcl
.c_magic
!= NFS_MAGIC
)
1271 panic("not at beginning of a file\n");
1272 if (!gettingfile
&& setjmp(restart
) != 0)
1276 for (i
= 0; i
< spcl
.c_count
; i
++) {
1277 if (readmapflag
|| spcl
.c_addr
[i
]) {
1278 readtape(&buf
[curblk
++][0]);
1279 if (curblk
== fssize
/ TP_BSIZE
) {
1280 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
1281 fssize
: (curblk
- 1) * TP_BSIZE
+ size
));
1283 last_write_was_hole
= 0;
1287 (*fill
)((char *)buf
, (size_t)(size
> TP_BSIZE
?
1289 (curblk
- 1) * TP_BSIZE
+ size
));
1292 (*skip
)(clearedbuf
, (long)(size
> TP_BSIZE
?
1294 last_write_was_hole
= 1;
1296 if ((size
-= TP_BSIZE
) <= 0) {
1297 for (i
++; i
< spcl
.c_count
; i
++)
1298 if (readmapflag
|| spcl
.c_addr
[i
])
1303 if (gethead(&spcl
) == GOOD
&& size
> 0) {
1304 if (spcl
.c_type
== TS_ADDR
)
1307 "Missing address (header) block for %s at %ld blocks\n",
1308 curfile
.name
, (long)blksread
);
1311 (*fill
)((char *)buf
, (size_t)((curblk
* TP_BSIZE
) + size
));
1312 last_write_was_hole
= 0;
1315 fprintf(stderr
, "Missing blocks at the end of %s, assuming hole\n", curfile
.name
);
1317 size_t skp
= size
> TP_BSIZE
? TP_BSIZE
: size
;
1318 (*skip
)(clearedbuf
, skp
);
1321 last_write_was_hole
= 1;
1323 if (last_write_was_hole
) {
1324 FTRUNCATE(ofile
, origsize
);
1332 * Write out the next block of a file.
1335 xtrfile(char *buf
, size_t size
)
1340 if (write(ofile
, buf
, (int) size
) == -1)
1341 err(1, "write error extracting inode %lu, name %s\nwrite",
1342 (unsigned long)curfile
.ino
, curfile
.name
);
1347 xtrfilefinderinfo(char *buf
, size_t size
)
1351 bcopy(buf
, &gFndrInfo
, size
);
1353 #endif /* DUMP_MACOSX */
1356 * Skip over a hole in a file.
1360 xtrskip(UNUSED(char *buf
), size_t size
)
1363 if (LSEEK(ofile
, (OFF_T
)size
, SEEK_CUR
) == -1)
1364 err(1, "seek error extracting inode %lu, name %s\nlseek",
1365 (unsigned long)curfile
.ino
, curfile
.name
);
1369 * Collect the next block of a symbolic link.
1372 xtrlnkfile(char *buf
, size_t size
)
1376 if (pathlen
> MAXPATHLEN
) {
1377 buf
[size
- 1] = '\0';
1378 errx(1, "symbolic link name: %s->%s%s; too long %d",
1379 curfile
.name
, lnkbuf
, buf
, pathlen
);
1381 (void) strcat(lnkbuf
, buf
);
1382 lnkbuf
[pathlen
] = '\0';
1386 * Skip over a hole in a symbolic link (should never happen).
1390 xtrlnkskip(UNUSED(char *buf
), UNUSED(size_t size
))
1393 errx(1, "unallocated block in symbolic link %s", curfile
.name
);
1397 * Collect the next block of a bit map.
1400 xtrmap(char *buf
, size_t size
)
1403 memmove(map
, buf
, size
);
1408 * Skip over a hole in a bit map (should never happen).
1412 xtrmapskip(UNUSED(char *buf
), size_t size
)
1415 panic("hole in map\n");
1420 * Noop, when an extraction function is not needed.
1424 xtrnull(UNUSED(char *buf
), UNUSED(size_t size
))
1430 #if COMPARE_ONTHEFLY
1432 * Compare the next block of a file.
1435 xtrcmpfile(char *buf
, size_t size
)
1437 static char cmpbuf
[MAXBSIZE
];
1442 if (read(ifile
, cmpbuf
, size
) != (ssize_t
)size
) {
1443 fprintf(stderr
, "%s: size has changed.\n",
1449 if (memcmp(buf
, cmpbuf
, size
) != 0) {
1450 fprintf(stderr
, "%s: tape and disk copies are different\n",
1458 * Skip over a hole in a file.
1461 xtrcmpskip(UNUSED(char *buf
), size_t size
)
1463 static char cmpbuf
[MAXBSIZE
];
1469 if (read(ifile
, cmpbuf
, size
) != (ssize_t
)size
) {
1470 fprintf(stderr
, "%s: size has changed.\n",
1476 for (i
= 0; i
< (int)size
; ++i
)
1477 if (cmpbuf
[i
] != '\0') {
1478 fprintf(stderr
, "%s: tape and disk copies are different\n",
1484 #endif /* COMPARE_ONTHEFLY */
1486 #if !COMPARE_ONTHEFLY
1488 do_cmpfiles(int fd_tape
, int fd_disk
, OFF_T size
)
1490 static char buf_tape
[BUFSIZ
];
1491 static char buf_disk
[BUFSIZ
];
1496 if ((n_tape
= read(fd_tape
, buf_tape
, sizeof(buf_tape
))) < 1) {
1497 close(fd_tape
), close(fd_disk
);
1498 panic("do_cmpfiles: unexpected EOF[1]");
1500 if ((n_disk
= read(fd_disk
, buf_disk
, sizeof(buf_tape
))) < 1) {
1501 close(fd_tape
), close(fd_disk
);
1502 panic("do_cmpfiles: unexpected EOF[2]");
1504 if (n_tape
!= n_disk
) {
1505 close(fd_tape
), close(fd_disk
);
1506 panic("do_cmpfiles: sizes different!");
1508 if (memcmp(buf_tape
, buf_disk
, (size_t)n_tape
) != 0) return (1);
1514 /* for debugging compare problems */
1515 #undef COMPARE_FAIL_KEEP_FILE
1518 #ifdef COMPARE_FAIL_KEEP_FILE
1519 /* return true if tapefile should be unlinked after compare */
1524 cmpfiles(char *tapefile
, char *diskfile
, struct STAT
*sbuf_disk
)
1526 struct STAT sbuf_tape
;
1527 int fd_tape
, fd_disk
;
1529 if (STAT(tapefile
, &sbuf_tape
) != 0) {
1530 panic("Can't lstat tmp file %s: %s\n", tapefile
,
1535 if (sbuf_disk
->st_size
!= sbuf_tape
.st_size
) {
1537 "%s: size changed from %lld to %lld.\n",
1538 diskfile
, (long long)sbuf_tape
.st_size
, (long long)sbuf_disk
->st_size
);
1540 #ifdef COMPARE_FAIL_KEEP_FILE
1547 if ((fd_tape
= OPEN(tapefile
, O_RDONLY
)) < 0) {
1548 panic("Can't open %s: %s\n", tapefile
, strerror(errno
));
1551 if ((fd_disk
= OPEN(diskfile
, O_RDONLY
)) < 0) {
1553 panic("Can't open %s: %s\n", diskfile
, strerror(errno
));
1557 if (do_cmpfiles(fd_tape
, fd_disk
, sbuf_tape
.st_size
)) {
1558 fprintf(stderr
, "%s: tape and disk copies are different\n",
1563 #ifdef COMPARE_FAIL_KEEP_FILE
1564 /* rename the file to live in /tmp */
1565 /* rename `tapefile' to /tmp/<basename of diskfile> */
1567 char *p
= strrchr(diskfile
, '/');
1568 char newname
[MAXPATHLEN
];
1570 panic("can't find / in %s\n", diskfile
);
1572 snprintf(newname
, sizeof(newname
), "%s/debug/%s", tmpdir
, p
+ 1);
1573 if (rename(tapefile
, newname
)) {
1574 panic("rename from %s to %s failed: %s\n",
1578 fprintf(stderr
, "*** %s saved to %s\n",
1583 /* don't unlink the file (it's not there anymore */
1592 #ifdef COMPARE_FAIL_KEEP_FILE
1596 #endif /* !COMPARE_ONTHEFLY */
1599 compareattr(char *name
)
1601 while (spcl
.c_flags
& DR_EXTATTRIBUTES
) {
1602 switch (spcl
.c_extattributes
) {
1603 case EXT_MACOSFNDRINFO
:
1604 msg("MacOSX not supported for comparision in this version, skipping\n");
1607 case EXT_MACOSRESFORK
:
1608 msg("MacOSX not supported for comparision in this version, skipping\n");
1612 msg("EA/ACLs not supported for comparision in this version, skipping\n");
1616 msg("unexpected inode extension %ld, skipping\n", spcl
.c_extattributes
);
1623 #if !COMPARE_ONTHEFLY
1624 static char tmpfilename
[MAXPATHLEN
];
1628 comparefile(char *name
)
1634 unsigned long newflags
;
1637 #if !COMPARE_ONTHEFLY
1638 static char *tmpfile
= NULL
;
1641 curfile
.name
= name
;
1642 curfile
.action
= USING
;
1643 mode
= curfile
.dip
->di_mode
;
1644 flags
= curfile
.dip
->di_flags
;
1645 uid
= curfile
.dip
->di_uid
;
1646 gid
= curfile
.dip
->di_gid
;
1648 if ((mode
& IFMT
) == IFSOCK
) {
1649 Vprintf(stdout
, "skipped socket %s\n", name
);
1654 if ((r
= LSTAT(name
, &sb
)) != 0) {
1655 warn("%s: does not exist (%d)", name
, r
);
1661 Vprintf(stdout
, "comparing %s (size: %lld, mode: 0%o)\n", name
,
1662 (long long)sb
.st_size
, mode
);
1664 if (sb
.st_mode
!= mode
) {
1665 fprintf(stderr
, "%s: mode changed from 0%o to 0%o.\n",
1666 name
, mode
& 07777, sb
.st_mode
& 07777);
1669 if (sb
.st_uid
!= uid
) {
1670 fprintf(stderr
, "%s: uid changed from %d to %d.\n",
1671 name
, uid
, sb
.st_uid
);
1674 if (sb
.st_gid
!= gid
) {
1675 fprintf(stderr
, "%s: gid changed from %d to %d.\n",
1676 name
, gid
, sb
.st_gid
);
1680 if (lgetflags(name
, &newflags
) < 0) {
1681 warn("%s: lgetflags failed", name
);
1685 if (newflags
!= flags
) {
1686 fprintf(stderr
, "%s: flags changed from 0x%08x to 0x%08lx.\n",
1687 name
, flags
, newflags
);
1692 if (spcl
.c_flags
& DR_METAONLY
) {
1696 switch (mode
& IFMT
) {
1711 char lbuf
[MAXPATHLEN
+ 1];
1714 if (!(sb
.st_mode
& S_IFLNK
)) {
1715 fprintf(stderr
, "%s: is no longer a symbolic link\n",
1722 getfile(xtrlnkfile
, xtrlnkskip
);
1725 "%s: zero length symbolic link (ignored)\n",
1730 if ((lsize
= readlink(name
, lbuf
, MAXPATHLEN
)) < 0) {
1731 panic("readlink of %s failed: %s", name
,
1736 if (strcmp(lbuf
, lnkbuf
) != 0) {
1738 "%s: symbolic link changed from %s to %s.\n",
1739 name
, lnkbuf
, lbuf
);
1749 if (!(sb
.st_mode
& (S_IFCHR
|S_IFBLK
))) {
1750 fprintf(stderr
, "%s: no longer a special file\n",
1757 if (sb
.st_rdev
!= (dev_t
)curfile
.dip
->di_rdev
) {
1759 "%s: device changed from %d,%d to %d,%d.\n",
1761 major(curfile
.dip
->di_rdev
),
1762 minor(curfile
.dip
->di_rdev
),
1772 #if COMPARE_ONTHEFLY
1773 if ((ifile
= OPEN(name
, O_RDONLY
)) < 0) {
1774 panic("Can't open %s: %s\n", name
, strerror(errno
));
1780 getfile(xtrcmpfile
, xtrcmpskip
);
1783 if (read(ifile
, &c
, 1) != 0) {
1784 fprintf(stderr
, "%s: size has changed.\n",
1794 if (tmpfile
== NULL
) {
1795 /* argument to mktemp() must not be in RO space: */
1796 snprintf(tmpfilename
, sizeof(tmpfilename
), "%s/restoreCXXXXXX", tmpdir
);
1797 tmpfile
= mktemp(&tmpfilename
[0]);
1799 if ((STAT(tmpfile
, &stemp
) == 0) && (unlink(tmpfile
) != 0)) {
1800 panic("cannot delete tmp file %s: %s\n",
1801 tmpfile
, strerror(errno
));
1803 if ((ofile
= OPEN(tmpfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600)) < 0) {
1804 panic("cannot create file temp file %s: %s\n",
1805 name
, strerror(errno
));
1807 getfile(xtrfile
, xtrskip
);
1808 (void) close(ofile
);
1809 #ifdef COMPARE_FAIL_KEEP_FILE
1810 if (cmpfiles(tmpfile
, name
, &sb
))
1813 cmpfiles(tmpfile
, name
, &sb
);
1816 #endif /* COMPARE_ONTHEFLY */
1823 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
1824 static void (*readtape_func
)(char *) = readtape_set
;
1827 * Read TP_BSIZE blocks from the input.
1828 * Handle read errors, and end of media.
1829 * Decompress compressed blocks.
1834 (*readtape_func
)(buf
); /* call the actual processing routine */
1838 * Set function pointer for readtape() routine. zflag and magtapein must
1839 * be correctly set before the first call to readtape().
1842 readtape_set(char *buf
)
1845 readtape_func
= readtape_uncompr
;
1849 readtape_func
= readtape_comprtape
;
1851 readtape_func
= readtape_comprfile
;
1856 #endif /* HAVE_ZLIB || HAVE_BZLIB || HAVE_LZO */
1859 * This is the original readtape(), it's used for reading uncompressed input.
1860 * Read TP_BSIZE blocks from the input.
1861 * Handle read errors, and end of media.
1864 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
1865 readtape_uncompr(char *buf
)
1870 ssize_t rd
, newvol
, i
;
1871 int cnt
, seek_failed
;
1873 if (blkcnt
< numtrec
) {
1874 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1880 for (i
= 0; i
< ntrec
; i
++)
1881 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
1884 cnt
= ntrec
* TP_BSIZE
;
1887 if (createtapeposflag
)
1888 (void)GetTapePos(&curtapepos
);
1893 i
= rmtread(&tapebuf
[rd
], cnt
);
1896 i
= read(mt
, &tapebuf
[rd
], cnt
);
1899 * Check for mid-tape short read error.
1900 * If found, skip rest of buffer and start with the next.
1902 if (!pipein
&& numtrec
< ntrec
&& i
> 0) {
1903 Dprintf(stdout
, "mid-media short read error.\n");
1907 * Handle partial block read.
1909 if (pipein
&& i
== 0 && rd
> 0)
1911 else if (i
> 0 && i
!= ntrec
* TP_BSIZE
) {
1920 * Short read. Process the blocks read.
1922 if (i
% TP_BSIZE
!= 0)
1924 "partial block read: %ld should be %ld\n",
1925 (long)i
, ntrec
* TP_BSIZE
);
1926 numtrec
= i
/ TP_BSIZE
;
1930 * Handle read error.
1933 fprintf(stderr
, "Tape read error while ");
1934 switch (curfile
.action
) {
1936 fprintf(stderr
, "trying to set up tape\n");
1939 fprintf(stderr
, "trying to resynchronize\n");
1942 fprintf(stderr
, "restoring %s\n", curfile
.name
);
1945 fprintf(stderr
, "skipping over inode %lu\n",
1946 (unsigned long)curfile
.ino
);
1949 if (!yflag
&& !reply("continue"))
1951 i
= ntrec
* TP_BSIZE
;
1952 memset(tapebuf
, 0, (size_t)i
);
1955 seek_failed
= (rmtseek(i
, 1) < 0);
1958 seek_failed
= (LSEEK(mt
, i
, SEEK_CUR
) == (OFF_T
)-1);
1961 warn("continuation failed");
1962 if (!yflag
&& !reply("assume end-of-tape and continue"))
1968 * Handle end of tape.
1971 Vprintf(stdout
, "End-of-tape encountered\n");
1980 if (rd
% TP_BSIZE
!= 0)
1981 panic("partial block read: %d should be %d\n",
1982 rd
, ntrec
* TP_BSIZE
);
1984 memmove(&tapebuf
[rd
], &endoftapemark
, TP_BSIZE
);
1987 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
1992 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
1995 * Read a compressed format block from a file or pipe and uncompress it.
1996 * Attempt to handle read errors, and end of file.
1999 readtape_comprfile(char *buf
)
2001 long rl
, size
, i
, ret
;
2003 struct tapebuf
*tpb
;
2005 if (blkcnt
< numtrec
) {
2006 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
2011 /* need to read the next block */
2013 for (i
= 0; i
< ntrec
; i
++)
2014 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
2016 tpb
= (struct tapebuf
*) tapebuf
;
2018 /* read the block prefix */
2019 ret
= read_a_block(mt
, tapebuf
, PREFIXSIZE
, &rl
);
2020 converttapebuf(tpb
);
2022 if (Vflag
&& (ret
== 0 || rl
< (int)PREFIXSIZE
|| tpb
->length
== 0))
2029 if (size
> bufsize
) {
2030 /* something's wrong */
2031 Vprintf(stdout
, "Prefix size error, max size %d, got %ld\n",
2034 tpb
->length
= bufsize
;
2036 ret
= read_a_block(mt
, tpb
->buf
, size
, &rl
);
2040 tbufptr
= decompress_tapebuf(tpb
, rl
+ PREFIXSIZE
);
2041 if (tbufptr
== NULL
) {
2042 msg_read_error("File decompression error while");
2043 if (!yflag
&& !reply("continue"))
2045 memset(tapebuf
, 0, bufsize
);
2050 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
2056 /* Errors while reading from a file or pipe are catastrophic. Since
2057 * there are no block boundaries, it's impossible to bypass the
2058 * block in error and find the start of the next block.
2061 /* It's possible to have multiple input files using -M
2062 * and -f file1,file2...
2064 Vprintf(stdout
, "End-of-File encountered\n");
2074 msg_read_error("Read error while");
2075 /* if (!yflag && !reply("continue")) */
2080 * Read compressed data from a tape and uncompress it.
2081 * Handle read errors, and end of media.
2082 * Since a tape consists of separate physical blocks, we try
2083 * to recover from errors by repositioning the tape to the next
2087 readtape_comprtape(char *buf
)
2091 struct tapebuf
*tpb
;
2094 if (blkcnt
< numtrec
) {
2095 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
2100 /* need to read the next block */
2102 for (i
= 0; i
< ntrec
; i
++)
2103 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
2105 tpb
= (struct tapebuf
*) tapebuf
;
2107 /* read the block */
2108 size
= bufsize
+ PREFIXSIZE
;
2109 ret
= read_a_block(mt
, tapebuf
, size
, &rl
);
2113 converttapebuf(tpb
);
2114 tbufptr
= decompress_tapebuf(tpb
, rl
);
2115 if (tbufptr
== NULL
) {
2116 msg_read_error("Tape decompression error while");
2117 if (!yflag
&& !reply("continue"))
2119 memset(tapebuf
, 0, PREFIXSIZE
+ bufsize
);
2125 /* Handle errors: EOT switches to the next volume, other errors
2126 * attempt to position the tape to the next block.
2129 Vprintf(stdout
, "End-of-tape encountered\n");
2138 msg_read_error("Tape read error while");
2139 if (!yflag
&& !reply("continue"))
2141 memset(tapebuf
, 0, PREFIXSIZE
+ bufsize
);
2146 rl
= rmtioctl(MTFSR
, 1);
2152 rl
= ioctl(mt
, MTIOCTOP
, &tcom
);
2156 warn("continuation failed");
2157 if (!yflag
&& !reply("assume end-of-tape and continue"))
2159 ret
= 0; /* end of tape */
2165 memmove(buf
, &tbufptr
[(blkcnt
++ * TP_BSIZE
)], TP_BSIZE
);
2171 * Decompress a struct tapebuf into a buffer. readsize is the size read
2172 * from the tape/file and is used for error messages. Returns a pointer
2173 * to the location of the uncompressed buffer or NULL on errors.
2174 * Adjust numtrec and complain for a short block.
2177 decompress_tapebuf(struct tapebuf
*tpbin
, int readsize
)
2179 /* If zflag is on, all blocks have a struct tapebuf prefix */
2180 /* zflag gets set in setup() from the dump header */
2181 int cresult
, blocklen
;
2182 unsigned long worklen
;
2183 char *output
= NULL
,*reason
= NULL
, *lengtherr
= NULL
;
2185 /* build a length error message */
2186 blocklen
= tpbin
->length
;
2187 if (readsize
< blocklen
+ (int)PREFIXSIZE
)
2188 lengtherr
= "short";
2190 if (readsize
> blocklen
+ (int)PREFIXSIZE
)
2195 if (tpbin
->compressed
) {
2196 /* uncompress whatever we read, if it fails, complain later */
2197 if (tpbin
->flags
== COMPRESS_ZLIB
) {
2199 errx(1,"This restore version doesn't support zlib decompression");
2201 cresult
= uncompress(comprbuf
, &worklen
,
2202 tpbin
->buf
, blocklen
);
2208 reason
= "not enough memory";
2211 reason
= "buffer too small";
2214 reason
= "data error";
2219 if (cresult
== Z_OK
)
2223 #endif /* HAVE_ZLIB */
2225 if (tpbin
->flags
== COMPRESS_BZLIB
) {
2227 errx(1,"This restore version doesn't support bzlib decompression");
2229 unsigned int worklen2
= worklen
;
2230 cresult
= BZ2_bzBuffToBuffDecompress(
2231 comprbuf
, &worklen2
,
2232 tpbin
->buf
, blocklen
, 0, 0);
2239 reason
= "not enough memory";
2241 case BZ_OUTBUFF_FULL
:
2242 reason
= "buffer too small";
2245 case BZ_DATA_ERROR_MAGIC
:
2246 case BZ_UNEXPECTED_EOF
:
2247 reason
= "data error";
2252 if (cresult
== BZ_OK
)
2256 #endif /* HAVE_BZLIB */
2258 if (tpbin
->flags
== COMPRESS_LZO
) {
2260 errx(1,"This restore version doesn't support lzo decompression");
2262 lzo_uint worklen2
= worklen
;
2263 cresult
= lzo1x_decompress(tpbin
->buf
, blocklen
,
2264 comprbuf
, &worklen2
, NULL
);
2271 case LZO_E_EOF_NOT_FOUND
:
2272 reason
= "data error";
2277 if (cresult
== LZO_E_OK
)
2281 #endif /* HAVE_LZO */
2285 output
= tpbin
->buf
;
2289 numtrec
= worklen
/ TP_BSIZE
;
2290 if (worklen
% TP_BSIZE
!= 0)
2291 reason
= "length mismatch";
2295 fprintf(stderr
, "%s compressed block: %d expected: %u\n",
2296 lengtherr
, readsize
, tpbin
->length
+ PREFIXSIZE
);
2297 fprintf(stderr
, "decompression error, block %ld: %s\n",
2298 tpblksread
+1, reason
);
2306 * Print an error message for a read error.
2307 * This was exteracted from the original readtape().
2310 msg_read_error(char *m
)
2312 switch (curfile
.action
) {
2314 fprintf(stderr
, "%s trying to set up tape\n", m
);
2317 fprintf(stderr
, "%s trying to resynchronize\n", m
);
2320 fprintf(stderr
, "%s restoring %s\n", m
, curfile
.name
);
2323 fprintf(stderr
, "%s skipping over inode %lu\n", m
,
2324 (unsigned long)curfile
.ino
);
2328 #endif /* HAVE_ZLIB || HAVE_BZLIB || HAVE_LZO */
2331 * Read the first block and get the blocksize from it. Test
2332 * for a compressed dump tape/file. setup() will make the final
2333 * determination by checking the compressed flag if gethead()
2334 * finds a valid header. The test here is necessary to offset the buffer
2335 * by the size of the compressed prefix. zflag is set here so that
2336 * readtape_set can set the correct function pointer for readtape().
2337 * Note that the first block of each tape/file is not compressed
2338 * and does not have a prefix.
2341 findtapeblksize(void)
2345 struct tapebuf
*tpb
= (struct tapebuf
*) tapebuf
;
2346 struct s_spcl spclpt
;
2348 for (i
= 0; i
< ntrec
; i
++)
2349 ((struct s_spcl
*)&tapebuf
[i
* TP_BSIZE
])->c_magic
= 0;
2353 * For a pipe or file, read in the first record. For a tape, read
2356 len
= magtapein
? ntrec
* TP_BSIZE
: TP_BSIZE
;
2358 if (read_a_block(mt
, tapebuf
, len
, &i
) <= 0)
2359 errx(1, "Tape read error on first record");
2361 memcpy(&spclpt
, tapebuf
, TP_BSIZE
);
2362 if (converthead(&spclpt
) == FAIL
) {
2364 if (converthead(&spclpt
) == FAIL
) {
2365 /* Special case for old compressed tapes with prefix */
2366 if (magtapein
&& (i
% TP_BSIZE
!= 0))
2368 errx(1, "Tape is not a dump tape");
2370 fprintf(stderr
, "Converting to new file system format.\n");
2373 * If the input is from a file or a pipe, we read TP_BSIZE
2374 * bytes looking for a dump header. If the dump is compressed
2375 * we need to read in the rest of the block, as determined
2376 * by c_ntrec in the dump header. The first block of the
2377 * dump is not compressed and does not have a prefix.
2380 if (spclpt
.c_type
== TS_TAPE
2381 && spclpt
.c_flags
& DR_COMPRESSED
) {
2382 /* It's a compressed dump file, read in the */
2383 /* rest of the block based on spclpt.c_ntrec. */
2384 if (spclpt
.c_ntrec
> ntrec
)
2385 errx(1, "Tape blocksize is too large, use "
2386 "\'-b %d\' ", spclpt
.c_ntrec
);
2387 ntrec
= spclpt
.c_ntrec
;
2388 len
= (ntrec
- 1) * TP_BSIZE
;
2392 /* read in the rest of the block based on bufsize */
2393 len
= bufsize
- TP_BSIZE
;
2395 if (read_a_block(mt
, tapebuf
+TP_BSIZE
, len
, &i
) < 0
2396 || (i
!= (long)len
&& i
% TP_BSIZE
!= 0))
2397 errx(1,"Error reading dump file header");
2400 Vprintf(stdout
, "Input block size is %ld\n", ntrec
);
2402 } /* if (!magtapein) */
2405 * If the input is a tape, we tried to read ntrec * TP_BSIZE bytes.
2406 * If the value of ntrec is too large, we read less than
2407 * what we asked for; adjust the value of ntrec and test for
2408 * a compressed dump tape.
2410 if (i
% TP_BSIZE
!= 0) {
2412 /* may be old format compressed dump tape with a prefix */
2413 memcpy(&spclpt
, tpb
->buf
, TP_BSIZE
);
2415 if (converthead(&spclpt
) == FAIL
) {
2417 if (converthead(&spclpt
) == FAIL
)
2418 errx(1, "Tape is not a dump tape");
2419 fprintf(stderr
, "Converting to new file system format.\n");
2421 if (i
% TP_BSIZE
== PREFIXSIZE
2422 && tpb
->compressed
== 0
2423 && spclpt
.c_type
== TS_TAPE
2424 && spclpt
.c_flags
& DR_COMPRESSED
) {
2427 if (tpb
->length
> bufsize
)
2428 errx(1, "Tape blocksize is too large, use "
2429 "\'-b %d\' ", tpb
->length
/ TP_BSIZE
);
2432 errx(1, "Tape block size (%ld) is not a multiple of dump block size (%d)",
2435 ntrec
= i
/ TP_BSIZE
;
2436 if (spclpt
.c_type
== TS_TAPE
) {
2437 if (spclpt
.c_flags
& DR_COMPRESSED
)
2439 if (spclpt
.c_ntrec
> ntrec
)
2440 errx(1, "Tape blocksize is too large, use "
2441 "\'-b %d\' ", spclpt
.c_ntrec
);
2444 Vprintf(stdout
, "Tape block size is %ld\n", ntrec
);
2448 * Read a block of data handling all of the messy details.
2450 static int read_a_block(int fd
, char *buf
, size_t len
, long *lengthread
)
2458 i
= rmtread(buf
, size
);
2461 i
= read(fd
, buf
, size
);
2464 break; /* EOD or error */
2467 break; /* block at a time for mt */
2470 *lengthread
= len
- size
;
2489 setmagtapein(void) {
2490 struct mtget mt_stat
;
2491 static int done
= 0;
2496 /* need to know if input is really from a tape */
2502 magtapein
= ioctl(mt
, MTIOCGET
, (char *)&mt_stat
) == 0;
2505 Vprintf(stdout
,"Input is from a %s %s\n",
2506 host
? "remote" : "local",
2507 magtapein
? "tape" :
2508 Vflag
? "multi-volume (no tape)" : "file/pipe");
2512 * Read the next block from the tape.
2513 * Check to see if it is one of several vintage headers.
2514 * If it is an old style header, convert it to a new style header.
2515 * If it is not any valid header, return an error.
2518 gethead(struct s_spcl
*buf
)
2520 readtape((char *)buf
);
2521 return converthead(buf
);
2525 converthead(struct s_spcl
*buf
)
2533 char dummy
[TP_BSIZE
];
2540 u_int16_t c_inumber
;
2545 u_int16_t odi_nlink
;
2561 if (buf
->c_magic
!= NFS_MAGIC
) {
2562 if (swabi(buf
->c_magic
) != NFS_MAGIC
)
2565 Vprintf(stdout
, "Note: Doing Byte swapping\n");
2569 if (checksum((int *)buf
) == FAIL
)
2572 swabst((u_char
*)"8i4s31i528bi192b3i", (u_char
*)buf
);
2575 memcpy(&u_ospcl
.s_ospcl
, buf
, TP_BSIZE
);
2576 if (checksum((int *)(&u_ospcl
.s_ospcl
)) == FAIL
)
2578 if (u_ospcl
.s_ospcl
.c_magic
== OFS_MAGIC
) {
2579 memset((char *)buf
, 0, (long)TP_BSIZE
);
2580 buf
->c_type
= u_ospcl
.s_ospcl
.c_type
;
2581 buf
->c_date
= u_ospcl
.s_ospcl
.c_date
;
2582 buf
->c_ddate
= u_ospcl
.s_ospcl
.c_ddate
;
2583 buf
->c_volume
= u_ospcl
.s_ospcl
.c_volume
;
2584 buf
->c_tapea
= u_ospcl
.s_ospcl
.c_tapea
;
2585 buf
->c_inumber
= u_ospcl
.s_ospcl
.c_inumber
;
2586 buf
->c_checksum
= u_ospcl
.s_ospcl
.c_checksum
;
2587 buf
->c_magic
= u_ospcl
.s_ospcl
.c_magic
;
2588 buf
->c_dinode
.di_mode
= u_ospcl
.s_ospcl
.c_dinode
.odi_mode
;
2589 buf
->c_dinode
.di_nlink
= u_ospcl
.s_ospcl
.c_dinode
.odi_nlink
;
2590 buf
->c_dinode
.di_uid
= u_ospcl
.s_ospcl
.c_dinode
.odi_uid
;
2591 buf
->c_dinode
.di_gid
= u_ospcl
.s_ospcl
.c_dinode
.odi_gid
;
2592 buf
->c_dinode
.di_size
= u_ospcl
.s_ospcl
.c_dinode
.odi_size
;
2593 buf
->c_dinode
.di_rdev
= u_ospcl
.s_ospcl
.c_dinode
.odi_rdev
;
2594 #if defined(__linux__) || defined(sunos)
2595 buf
->c_dinode
.di_atime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
2596 buf
->c_dinode
.di_mtime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
2597 buf
->c_dinode
.di_ctime
.tv_sec
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
2598 #else /* __linux__ || sunos */
2599 buf
->c_dinode
.di_atime
= u_ospcl
.s_ospcl
.c_dinode
.odi_atime
;
2600 buf
->c_dinode
.di_mtime
= u_ospcl
.s_ospcl
.c_dinode
.odi_mtime
;
2601 buf
->c_dinode
.di_ctime
= u_ospcl
.s_ospcl
.c_dinode
.odi_ctime
;
2602 #endif /* __linux__ || sunos */
2603 buf
->c_count
= u_ospcl
.s_ospcl
.c_count
;
2604 memmove(buf
->c_addr
, u_ospcl
.s_ospcl
.c_fill
, (long)256);
2606 else if (u_ospcl
.s_ospcl
.c_magic
== FS_UFS2_MAGIC
) {
2607 buf
->c_date
= (int32_t)(*(int64_t *)&u_ospcl
.dummy
[896]);
2608 buf
->c_ddate
= (int32_t)(*(int64_t *)&u_ospcl
.dummy
[904]);
2609 buf
->c_tapea
= (int32_t)(*(int64_t *)&u_ospcl
.dummy
[912]);
2610 buf
->c_firstrec
= (int32_t)(*(int64_t *)&u_ospcl
.dummy
[920]);
2612 buf
->c_extattributes
= 0;
2613 buf
->c_flags
|= DR_NEWINODEFMT
;
2618 buf
->c_magic
= NFS_MAGIC
;
2621 if ((buf
->c_dinode
.di_size
== 0 || buf
->c_dinode
.di_size
> 0xfffffff) &&
2622 (buf
->c_dinode
.di_mode
& IFMT
) == IFDIR
&& Qcvt
== 0) {
2623 qcvt
.qval
= buf
->c_dinode
.di_size
;
2624 if (qcvt
.val
[0] || qcvt
.val
[1]) {
2625 Vprintf(stdout
, "Note: Doing Quad swapping\n");
2630 qcvt
.qval
= buf
->c_dinode
.di_size
;
2632 qcvt
.val
[1] = qcvt
.val
[0];
2634 buf
->c_dinode
.di_size
= qcvt
.qval
;
2638 switch (buf
->c_type
) {
2643 * Have to patch up missing information in bit map headers
2646 buf
->c_dinode
.di_size
= buf
->c_count
* TP_BSIZE
;
2647 if (buf
->c_count
> TP_NINDIR
)
2650 for (i
= 0; i
< buf
->c_count
; i
++)
2655 if ((buf
->c_flags
& DR_NEWINODEFMT
) == 0)
2660 if (buf
->c_flags
& DR_INODEINFO
) {
2661 memcpy(volinfo
, buf
->c_inos
, TP_NINOS
* sizeof(dump_ino_t
));
2663 swabst((u_char
*)"128i", (u_char
*)volinfo
);
2672 panic("gethead: unknown inode type %d\n", buf
->c_type
);
2676 * If we are restoring a filesystem with old format inodes,
2677 * copy the uid/gid to the new location.
2680 buf
->c_dinode
.di_uid
= buf
->c_dinode
.di_ouid
;
2681 buf
->c_dinode
.di_gid
= buf
->c_dinode
.di_ogid
;
2689 converttapebuf(struct tapebuf
*tpb
)
2693 unsigned int length
:28;
2694 unsigned int flags
:3;
2695 unsigned int compressed
:1;
2697 swabst((u_char
*)"i", (u_char
*)tpb
);
2698 memcpy(&tb
, tpb
, 4);
2699 tpb
->length
= tb
.length
;
2700 tpb
->flags
= tb
.flags
;
2701 tpb
->compressed
= tb
.compressed
;
2706 * Check that a header is where it belongs and predict the next header
2709 accthdr(struct s_spcl
*header
)
2711 static dump_ino_t previno
= 0x7fffffff;
2712 static int prevtype
;
2713 static long predict
;
2716 if (header
->c_type
== TS_TAPE
) {
2717 fprintf(stderr
, "Volume header (%s inode format) ",
2718 oldinofmt
? "old" : "new");
2719 if (header
->c_firstrec
)
2720 fprintf(stderr
, "begins with record %d",
2721 header
->c_firstrec
);
2722 fprintf(stderr
, "\n");
2723 previno
= 0x7fffffff;
2726 if (previno
== 0x7fffffff)
2730 fprintf(stderr
, "Dumped inodes map header");
2733 fprintf(stderr
, "Used inodes map header");
2736 fprintf(stderr
, "File header, ino %lu", (unsigned long)previno
);
2739 fprintf(stderr
, "File continuation header, ino %ld", (long)previno
);
2742 fprintf(stderr
, "End of tape header");
2745 if (predict
!= blksread
- 1)
2746 fprintf(stderr
, "; predicted %ld blocks, got %ld blocks",
2747 predict
, blksread
- 1);
2748 fprintf(stderr
, "\n");
2751 if (header
->c_type
!= TS_END
)
2752 for (i
= 0; i
< header
->c_count
; i
++)
2753 if (readmapflag
|| header
->c_addr
[i
] != 0)
2757 prevtype
= header
->c_type
;
2758 previno
= header
->c_inumber
;
2762 * Find an inode header.
2763 * Complain if had to skip, and complain is set.
2766 findinode(struct s_spcl
*header
)
2768 static long skipcnt
= 0;
2772 curfile
.name
= "<name unknown>";
2773 curfile
.action
= UNKNOWN
;
2777 if (header
->c_magic
!= NFS_MAGIC
) {
2779 while (gethead(header
) == FAIL
||
2780 header
->c_date
!= dumpdate
)
2783 switch (header
->c_type
) {
2787 * Skip up to the beginning of the next record
2789 for (i
= 0; i
< header
->c_count
; i
++)
2790 if (header
->c_addr
[i
])
2792 while (gethead(header
) == FAIL
||
2793 header
->c_date
!= dumpdate
)
2798 curfile
.dip
= &header
->c_dinode
;
2799 curfile
.ino
= header
->c_inumber
;
2803 curfile
.ino
= maxino
;
2807 curfile
.name
= "<file removal list>";
2811 curfile
.name
= "<file dump list>";
2815 panic("unexpected tape header\n");
2819 panic("unknown tape header type %d\n", spcl
.c_type
);
2823 } while (header
->c_type
== TS_ADDR
);
2828 fprintf(stderr
, "resync restore, skipped %ld blocks\n",
2838 j
= sizeof(union u_spcl
) / sizeof(int);
2845 /* What happens if we want to read restore tapes
2846 for a 16bit int machine??? */
2852 if (i
!= CHECKSUM
) {
2853 fprintf(stderr
, "Checksum error %o, inode %lu file %s\n", i
,
2854 (unsigned long)curfile
.ino
, curfile
.name
);
2864 #include <varargs.h>
2869 msg(const char *fmt
, ...)
2882 (void)vfprintf(stderr
, fmt
, ap
);
2885 #endif /* RRESTORE */
2888 swab16(u_char
*sp
, int n
)
2893 c
= sp
[0]; sp
[0] = sp
[1]; sp
[1] = c
;
2900 swab32(u_char
*sp
, int n
)
2905 c
= sp
[0]; sp
[0] = sp
[3]; sp
[3] = c
;
2906 c
= sp
[1]; sp
[1] = sp
[2]; sp
[2] = c
;
2913 swab64(u_char
*sp
, int n
)
2918 c
= sp
[0]; sp
[0] = sp
[7]; sp
[7] = c
;
2919 c
= sp
[1]; sp
[1] = sp
[6]; sp
[6] = c
;
2920 c
= sp
[2]; sp
[2] = sp
[5]; sp
[5] = c
;
2921 c
= sp
[3]; sp
[3] = sp
[4]; sp
[4] = c
;
2928 swabst(u_char
*cp
, u_char
*sp
)
2934 case '0': case '1': case '2': case '3': case '4':
2935 case '5': case '6': case '7': case '8': case '9':
2936 n
= (n
* 10) + (*cp
++ - '0');
2939 case 's': case 'w': case 'h':
2957 default: /* Any other character, like 'b' counts as byte. */
2971 swabst((u_char
*)"i", (u_char
*)&x
);
2979 swabst((u_char
*)"l", (u_char
*)&x
);
2985 RequestVol(long tnum
)
2996 int uscsi_flags
; /* read, write, etc. see below */
2997 short uscsi_status
; /* resulting status */
2998 short uscsi_timeout
; /* Command Timeout */
2999 caddr_t uscsi_cdb
; /* cdb to send to target */
3000 caddr_t uscsi_bufaddr
; /* i/o source/destination */
3001 u_int uscsi_buflen
; /* size of i/o to take place */
3002 u_int uscsi_resid
; /* resid from i/o operation */
3003 u_char uscsi_cdblen
; /* # of valid cdb bytes */
3004 u_char uscsi_rqlen
; /* size of uscsi_rqbuf */
3005 u_char uscsi_rqstatus
; /* status of request sense cmd */
3006 u_char uscsi_rqresid
; /* resid of request sense cmd */
3007 caddr_t uscsi_rqbuf
; /* request sense buffer */
3008 void *uscsi_reserved_5
; /* Reserved for Future Use */
3011 #define CDB_GROUP0 6 /* 6-byte cdb's */
3012 #define CDB_GROUP1 10 /* 10-byte cdb's */
3013 #define CDB_GROUP2 10 /* 10-byte cdb's */
3014 #define CDB_GROUP3 0 /* reserved */
3015 #define CDB_GROUP4 16 /* 16-byte cdb's */
3016 #define CDB_GROUP5 12 /* 12-byte cdb's */
3017 #define CDB_GROUP6 0 /* reserved */
3018 #define CDB_GROUP7 0 /* reserved */
3020 #define USCSI_WRITE 0x00000 /* send data to device */
3021 #define USCSI_SILENT 0x00001 /* no error messages */
3022 #define USCSI_DIAGNOSE 0x00002 /* fail if any error occurs */
3023 #define USCSI_ISOLATE 0x00004 /* isolate from normal commands */
3024 #define USCSI_READ 0x00008 /* get data from device */
3025 #define USCSI_RESET 0x04000 /* Reset target */
3026 #define USCSI_RESET_ALL 0x08000 /* Reset all targets */
3027 #define USCSI_RQENABLE 0x10000 /* Enable Request Sense extensions */
3029 #define USCSIIOC (0x04 << 8)
3030 #define USCSICMD (USCSIIOC|201) /* user scsi command */
3032 #define USCSI_TIMEOUT 30
3033 #define USCSI_SHORT_TIMEOUT 900
3034 #define USCSI_LONG_TIMEOUT 14000
3036 #define B(s,i) ((unsigned char)((s) >> i))
3037 #define B1(s) ((unsigned char)(s))
3039 #define MSB4(s,v) *(s)=B(v,24),(s)[1]=B(v,16), (s)[2]=B(v,8), (s)[3]=B1(v)
3043 GetTapePos(long long *pos
)
3046 struct uscsi_cmd scmd
;
3051 (void)memset((void *)buf
, 0, sizeof(buf
));
3052 (void)memset((void *)&scmd
, 0, sizeof(scmd
));
3053 scmd
.uscsi_flags
= USCSI_READ
|USCSI_SILENT
;
3054 scmd
.uscsi_timeout
= USCSI_TIMEOUT
;
3055 scmd
.uscsi_cdb
= buf
;
3056 scmd
.uscsi_cdblen
= CDB_GROUP1
;
3057 buf
[0] = 0x34; /* read position */
3059 (void)memset((void *)parm
, 0, 512);
3060 scmd
.uscsi_bufaddr
= parm
;
3061 scmd
.uscsi_buflen
= 56;
3062 if (ioctl(fdsmtc
, USCSICMD
, &scmd
) == -1) {
3066 (void)memcpy(&lpos
, &parm
[4], sizeof(long));
3072 GotoTapePos(long long pos
)
3075 struct uscsi_cmd scmd
;
3078 long lpos
= (long)pos
;
3080 (void)memset((void *)buf
, 0, sizeof(buf
));
3081 (void)memset((void *)&scmd
, 0, sizeof(scmd
));
3082 scmd
.uscsi_flags
= USCSI_WRITE
|USCSI_SILENT
;
3083 scmd
.uscsi_timeout
= 360; /* 5 Minutes */
3084 scmd
.uscsi_cdb
= buf
;
3085 scmd
.uscsi_cdblen
= CDB_GROUP1
;
3086 buf
[0] = 0x2b; /* locate */
3088 MSB4(&buf
[3], lpos
);
3089 (void)memset((void *)parm
, 0, 512);
3090 scmd
.uscsi_bufaddr
= NULL
;
3091 scmd
.uscsi_buflen
= 0;
3092 if (ioctl(fdsmtc
, USCSICMD
, &scmd
) == -1) {
3100 #define LSEEK_GET_TAPEPOS 10
3101 #define LSEEK_GO2_TAPEPOS 11
3104 typedef struct mt_pos
{
3107 } MTPosRec
, *MTPosPtr
;
3111 * get the current position of the tape
3114 GetTapePos(long long *pos
)
3120 *pos
= (long long) rmtseek((OFF_T
)0, (int)LSEEK_GET_TAPEPOS
);
3129 err
= (ioctl(mt
, MTIOCPOS
, &mtpos
) < 0);
3130 *pos
= (long long)mtpos
;
3133 *pos
= LSEEK(mt
, 0, SEEK_CUR
);
3139 fprintf(stdout
, "[%ld] error: %d (getting tapepos: %lld)\n",
3140 (unsigned long)getpid(), err
, *pos
);
3147 * go to specified position on tape
3150 GotoTapePos(long long pos
)
3156 err
= (rmtseek((OFF_T
)pos
, (int)LSEEK_GO2_TAPEPOS
) < 0);
3163 buf
.mt_count
= (int) pos
;
3164 err
= (ioctl(mt
, MTIOCTOP
, &buf
) < 0);
3167 pos
= LSEEK(mt
, pos
, SEEK_SET
);
3173 fprintf(stdout
, "[%ld] error: %d (setting tapepos: %lld)\n",
3174 (unsigned long)getpid(), err
, pos
);
3179 #endif /* __linux__ */
3182 * read next data from tape to re-sync
3185 ReReadFromTape(void)
3189 if (gethead(&spcl
) == FAIL
) {
3191 fprintf(stdout
, "DEBUG 1 gethead failed\n");
3199 ReReadInodeFromTape(dump_ino_t theino
)
3208 } while (!(spcl
.c_inumber
== theino
&& spcl
.c_type
== TS_INODE
&& spcl
.c_date
== dumpdate
));
3210 fprintf(stderr
, "DEBUG: %ld reads\n", cntloop
);
3211 fprintf(stderr
, "DEBUG: bufsize %ld\n", bufsize
);
3212 fprintf(stderr
, "DEBUG: ntrec %ld\n", ntrec
);
3213 fprintf(stderr
, "DEBUG: %ld reads\n", cntloop
);
3221 OpenSMTCmt(char *themagtape
)
3223 if (GetSCSIIDFromPath(themagtape
, &scsiid
)) {
3224 fprintf(stderr
, "can't get SCSI-ID for %s\n", themagtape
);
3228 fprintf(stderr
, "can't get SCSI-ID for %s\n", themagtape
);
3231 sprintf(smtcpath
, "/dev/rsmtc%ld,0", scsiid
);
3232 if ((fdsmtc
= open(smtcpath
, O_RDWR
)) == -1) {
3233 fprintf(stderr
, "can't open smtc device: %s, %d\n", smtcpath
, errno
);
3239 #endif /* USE_QFA */