]>
Commit | Line | Data |
---|---|---|
1227625a SP |
1 | /* |
2 | * Ported to Linux's Second Extended File System as part of the | |
3 | * dump and restore backup suit | |
b45f51d6 | 4 | * Remy Card <card@Linux.EU.Org>, 1994-1997 |
ebcbe7f6 | 5 | * Stelian Pop <pop@cybercable.fr>, 1999-2000 |
1227625a SP |
6 | */ |
7 | ||
8 | /* | |
9 | * Copyright (c) 1983, 1993 | |
10 | * The Regents of the University of California. All rights reserved. | |
11 | * (c) UNIX System Laboratories, Inc. | |
12 | * All or some portions of this file are derived from material licensed | |
13 | * to the University of California by American Telephone and Telegraph | |
14 | * Co. or Unix System Laboratories, Inc. and are reproduced herein with | |
15 | * the permission of UNIX System Laboratories, Inc. | |
16 | * | |
17 | * Redistribution and use in source and binary forms, with or without | |
18 | * modification, are permitted provided that the following conditions | |
19 | * are met: | |
20 | * 1. Redistributions of source code must retain the above copyright | |
21 | * notice, this list of conditions and the following disclaimer. | |
22 | * 2. Redistributions in binary form must reproduce the above copyright | |
23 | * notice, this list of conditions and the following disclaimer in the | |
24 | * documentation and/or other materials provided with the distribution. | |
25 | * 3. All advertising materials mentioning features or use of this software | |
26 | * must display the following acknowledgement: | |
27 | * This product includes software developed by the University of | |
28 | * California, Berkeley and its contributors. | |
29 | * 4. Neither the name of the University nor the names of its contributors | |
30 | * may be used to endorse or promote products derived from this software | |
31 | * without specific prior written permission. | |
32 | * | |
33 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
34 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
35 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
36 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
37 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
38 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
39 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
40 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
41 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
42 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
43 | * SUCH DAMAGE. | |
44 | */ | |
45 | ||
df9ae507 SP |
46 | #ifndef lint |
47 | static const char rcsid[] = | |
d572dbfd | 48 | "$Id: tape.c,v 1.18 2000/06/25 18:42:39 stelian Exp $"; |
df9ae507 SP |
49 | #endif /* not lint */ |
50 | ||
1227625a SP |
51 | #include <sys/param.h> |
52 | #include <sys/file.h> | |
1227625a SP |
53 | #include <sys/mtio.h> |
54 | #include <sys/stat.h> | |
55 | ||
56 | #ifdef __linux__ | |
57 | #include <sys/time.h> | |
58 | #include <linux/ext2_fs.h> | |
59 | #include <bsdcompat.h> | |
60 | #else /* __linux__ */ | |
61 | #include <ufs/ufs/dinode.h> | |
62 | #endif /* __linux__ */ | |
63 | #include <protocols/dumprestore.h> | |
64 | ||
65 | #include <errno.h> | |
ddd2ef55 | 66 | #include <compaterr.h> |
1227625a SP |
67 | #include <setjmp.h> |
68 | #include <stdio.h> | |
69 | #include <stdlib.h> | |
70 | #include <string.h> | |
71 | #include <unistd.h> | |
72 | ||
73 | #ifdef __linux__ | |
74 | #include <ext2fs/ext2fs.h> | |
75 | #endif | |
76 | ||
77 | #include "restore.h" | |
78 | #include "extern.h" | |
79 | #include "pathnames.h" | |
80 | ||
81 | static long fssize = MAXBSIZE; | |
82 | static int mt = -1; | |
83 | static int pipein = 0; | |
92a9bf12 SP |
84 | static char magtape[MAXPATHLEN]; |
85 | static char magtapeprefix[MAXPATHLEN]; | |
1227625a SP |
86 | static int blkcnt; |
87 | static int numtrec; | |
88 | static char *tapebuf; | |
89 | static union u_spcl endoftapemark; | |
90 | static long blksread; /* blocks read since last header */ | |
91 | static long tpblksread = 0; /* TP_BSIZE blocks read */ | |
92 | static long tapesread; | |
ddd2ef55 | 93 | static sigjmp_buf restart; |
1227625a SP |
94 | static int gettingfile = 0; /* restart has a valid frame */ |
95 | static char *host = NULL; | |
96 | ||
97 | static int ofile; | |
98 | static char *map; | |
99 | static char lnkbuf[MAXPATHLEN + 1]; | |
100 | static int pathlen; | |
101 | ||
102 | int oldinofmt; /* old inode format conversion required */ | |
103 | int Bcvt; /* Swap Bytes (for CCI or sun) */ | |
104 | static int Qcvt; /* Swap quads (for sun) */ | |
105 | ||
106 | #define FLUSHTAPEBUF() blkcnt = ntrec + 1 | |
107 | ||
108 | static void accthdr __P((struct s_spcl *)); | |
109 | static int checksum __P((int *)); | |
110 | static void findinode __P((struct s_spcl *)); | |
111 | static void findtapeblksize __P((void)); | |
112 | static int gethead __P((struct s_spcl *)); | |
113 | static void readtape __P((char *)); | |
114 | static void setdumpnum __P((void)); | |
ddd2ef55 | 115 | static u_int swabi __P((u_int)); |
1227625a | 116 | static u_long swabl __P((u_long)); |
ddd2ef55 SP |
117 | static u_char *swab64 __P((u_char *, int)); |
118 | static u_char *swab32 __P((u_char *, int)); | |
119 | static u_char *swab16 __P((u_char *, int)); | |
1227625a | 120 | static void terminateinput __P((void)); |
ddd2ef55 SP |
121 | static void xtrfile __P((char *, size_t)); |
122 | static void xtrlnkfile __P((char *, size_t)); | |
123 | static void xtrlnkskip __P((char *, size_t)); | |
124 | static void xtrmap __P((char *, size_t)); | |
125 | static void xtrmapskip __P((char *, size_t)); | |
126 | static void xtrskip __P((char *, size_t)); | |
1227625a | 127 | |
d8574d45 SP |
128 | #define COMPARE_ONTHEFLY 1 |
129 | ||
130 | #if COMPARE_ONTHEFLY | |
131 | static int ifile; /* input file for compare */ | |
132 | static int cmperror; /* compare error */ | |
133 | static void xtrcmpfile __P((char *, size_t)); | |
134 | static void xtrcmpskip __P((char *, size_t)); | |
135 | #endif | |
136 | ||
b45f51d6 SP |
137 | static int readmapflag; |
138 | ||
1227625a SP |
139 | /* |
140 | * Set up an input source | |
141 | */ | |
142 | void | |
ddd2ef55 | 143 | setinput(char *source) |
1227625a SP |
144 | { |
145 | FLUSHTAPEBUF(); | |
146 | if (bflag) | |
147 | newtapebuf(ntrec); | |
148 | else | |
149 | newtapebuf(NTREC > HIGHDENSITYTREC ? NTREC : HIGHDENSITYTREC); | |
150 | terminal = stdin; | |
151 | ||
152 | #ifdef RRESTORE | |
153 | if (strchr(source, ':')) { | |
154 | host = source; | |
155 | source = strchr(host, ':'); | |
156 | *source++ = '\0'; | |
157 | if (rmthost(host) == 0) | |
ddd2ef55 | 158 | exit(1); |
1227625a SP |
159 | } else |
160 | #endif | |
161 | if (strcmp(source, "-") == 0) { | |
162 | /* | |
163 | * Since input is coming from a pipe we must establish | |
164 | * our own connection to the terminal. | |
165 | */ | |
166 | terminal = fopen(_PATH_TTY, "r"); | |
167 | if (terminal == NULL) { | |
ddd2ef55 | 168 | warn("cannot open %s", _PATH_TTY); |
1227625a | 169 | terminal = fopen(_PATH_DEVNULL, "r"); |
ddd2ef55 SP |
170 | if (terminal == NULL) |
171 | err(1, "cannot open %s", _PATH_DEVNULL); | |
1227625a SP |
172 | } |
173 | pipein++; | |
174 | } | |
175 | setuid(getuid()); /* no longer need or want root privileges */ | |
394240ce | 176 | if (Mflag) { |
92a9bf12 SP |
177 | strncpy(magtapeprefix, source, MAXPATHLEN); |
178 | magtapeprefix[MAXPATHLEN-1] = '\0'; | |
179 | snprintf(magtape, MAXPATHLEN, "%s%03d", source, 1); | |
394240ce SP |
180 | } |
181 | else | |
92a9bf12 SP |
182 | strncpy(magtape, source, MAXPATHLEN); |
183 | magtape[MAXPATHLEN - 1] = '\0'; | |
1227625a SP |
184 | } |
185 | ||
186 | void | |
ddd2ef55 | 187 | newtapebuf(long size) |
1227625a | 188 | { |
ddd2ef55 | 189 | static int tapebufsize = -1; |
1227625a SP |
190 | |
191 | ntrec = size; | |
192 | if (size <= tapebufsize) | |
193 | return; | |
194 | if (tapebuf != NULL) | |
195 | free(tapebuf); | |
196 | tapebuf = malloc(size * TP_BSIZE); | |
ddd2ef55 SP |
197 | if (tapebuf == NULL) |
198 | errx(1, "Cannot allocate space for tape buffer"); | |
1227625a SP |
199 | tapebufsize = size; |
200 | } | |
201 | ||
202 | /* | |
203 | * Verify that the tape drive can be accessed and | |
204 | * that it actually is a dump tape. | |
205 | */ | |
206 | void | |
ddd2ef55 | 207 | setup(void) |
1227625a SP |
208 | { |
209 | int i, j, *ip; | |
210 | struct stat stbuf; | |
211 | ||
ddd2ef55 | 212 | Vprintf(stdout, "Verify tape and initialize maps\n"); |
1227625a SP |
213 | #ifdef RRESTORE |
214 | if (host) | |
215 | mt = rmtopen(magtape, 0); | |
216 | else | |
217 | #endif | |
218 | if (pipein) | |
219 | mt = 0; | |
220 | else | |
221 | mt = open(magtape, O_RDONLY, 0); | |
ddd2ef55 SP |
222 | if (mt < 0) |
223 | err(1, "%s", magtape); | |
1227625a SP |
224 | volno = 1; |
225 | setdumpnum(); | |
226 | FLUSHTAPEBUF(); | |
227 | if (!pipein && !bflag) | |
228 | findtapeblksize(); | |
229 | if (gethead(&spcl) == FAIL) { | |
230 | blkcnt--; /* push back this block */ | |
231 | blksread--; | |
232 | tpblksread--; | |
233 | cvtflag++; | |
ddd2ef55 SP |
234 | if (gethead(&spcl) == FAIL) |
235 | errx(1, "Tape is not a dump tape"); | |
1227625a SP |
236 | fprintf(stderr, "Converting to new file system format.\n"); |
237 | } | |
238 | if (pipein) { | |
239 | endoftapemark.s_spcl.c_magic = cvtflag ? OFS_MAGIC : NFS_MAGIC; | |
240 | endoftapemark.s_spcl.c_type = TS_END; | |
241 | ip = (int *)&endoftapemark; | |
242 | j = sizeof(union u_spcl) / sizeof(int); | |
243 | i = 0; | |
244 | do | |
245 | i += *ip++; | |
246 | while (--j); | |
247 | endoftapemark.s_spcl.c_checksum = CHECKSUM - i; | |
248 | } | |
249 | if (vflag || command == 't' || command == 'C') | |
250 | printdumpinfo(); | |
0d7af9c5 SP |
251 | if (filesys[0] == '\0') { |
252 | char *dirptr; | |
0d7af9c5 SP |
253 | strncpy(filesys, spcl.c_filesys, NAMELEN); |
254 | filesys[NAMELEN - 1] = '\0'; | |
255 | dirptr = strstr(filesys, " (dir"); | |
0d7af9c5 SP |
256 | if (dirptr != NULL) |
257 | *dirptr = '\0'; | |
1227625a SP |
258 | } |
259 | dumptime = spcl.c_ddate; | |
260 | dumpdate = spcl.c_date; | |
ddd2ef55 SP |
261 | if (stat(".", &stbuf) < 0) |
262 | err(1, "cannot stat ."); | |
b45f51d6 SP |
263 | if (stbuf.st_blksize > 0 && stbuf.st_blksize < TP_BSIZE ) |
264 | fssize = TP_BSIZE; | |
265 | if (stbuf.st_blksize >= TP_BSIZE && stbuf.st_blksize <= MAXBSIZE) | |
1227625a | 266 | fssize = stbuf.st_blksize; |
ddd2ef55 SP |
267 | if (((fssize - 1) & fssize) != 0) |
268 | errx(1, "bad block size %ld", fssize); | |
269 | if (spcl.c_volume != 1) | |
270 | errx(1, "Tape is not volume 1 of the dump"); | |
1227625a | 271 | if (gethead(&spcl) == FAIL) { |
ddd2ef55 | 272 | Dprintf(stdout, "header read failed at %ld blocks\n", (long)blksread); |
1227625a SP |
273 | panic("no header after volume mark!\n"); |
274 | } | |
275 | findinode(&spcl); | |
ddd2ef55 SP |
276 | if (spcl.c_type != TS_CLRI) |
277 | errx(1, "Cannot find file removal list"); | |
1227625a | 278 | maxino = (spcl.c_count * TP_BSIZE * NBBY) + 1; |
b9c89cf4 | 279 | Dprintf(stdout, "maxino = %ld\n", (long)maxino); |
1227625a SP |
280 | map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY)); |
281 | if (map == NULL) | |
d572dbfd | 282 | errx(1, "no memory for active inode map"); |
1227625a SP |
283 | usedinomap = map; |
284 | curfile.action = USING; | |
285 | getfile(xtrmap, xtrmapskip); | |
ddd2ef55 SP |
286 | if (spcl.c_type != TS_BITS) |
287 | errx(1, "Cannot find file dump list"); | |
1227625a SP |
288 | map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY)); |
289 | if (map == (char *)NULL) | |
d572dbfd | 290 | errx(1, "no memory for file dump list"); |
1227625a SP |
291 | dumpmap = map; |
292 | curfile.action = USING; | |
293 | getfile(xtrmap, xtrmapskip); | |
294 | /* | |
295 | * If there may be whiteout entries on the tape, pretend that the | |
296 | * whiteout inode exists, so that the whiteout entries can be | |
297 | * extracted. | |
298 | */ | |
299 | if (oldinofmt == 0) | |
300 | SETINO(WINO, dumpmap); | |
301 | } | |
302 | ||
303 | /* | |
304 | * Prompt user to load a new dump volume. | |
305 | * "Nextvol" is the next suggested volume to use. | |
306 | * This suggested volume is enforced when doing full | |
b45f51d6 | 307 | * or incremental restores, but can be overridden by |
1227625a SP |
308 | * the user when only extracting a subset of the files. |
309 | */ | |
310 | void | |
ddd2ef55 | 311 | getvol(long nextvol) |
1227625a | 312 | { |
ddd2ef55 | 313 | long newvol = 0, savecnt = 0, wantnext = 0, i; |
1227625a SP |
314 | union u_spcl tmpspcl; |
315 | # define tmpbuf tmpspcl.s_spcl | |
316 | char buf[TP_BSIZE]; | |
394240ce | 317 | int haderror = 0; |
1227625a SP |
318 | |
319 | if (nextvol == 1) { | |
320 | tapesread = 0; | |
321 | gettingfile = 0; | |
322 | } | |
323 | if (pipein) { | |
324 | if (nextvol != 1) | |
325 | panic("Changing volumes on pipe input?\n"); | |
326 | if (volno == 1) | |
327 | return; | |
328 | goto gethdr; | |
329 | } | |
330 | savecnt = blksread; | |
331 | again: | |
332 | if (pipein) | |
ddd2ef55 | 333 | exit(1); /* pipes do not get a second chance */ |
1227625a SP |
334 | if (command == 'R' || command == 'r' || curfile.action != SKIP) { |
335 | newvol = nextvol; | |
336 | wantnext = 1; | |
b45f51d6 | 337 | } else { |
1227625a SP |
338 | newvol = 0; |
339 | wantnext = 0; | |
340 | } | |
341 | while (newvol <= 0) { | |
342 | if (tapesread == 0) { | |
343 | fprintf(stderr, "%s%s%s%s%s", | |
344 | "You have not read any tapes yet.\n", | |
345 | "Unless you know which volume your", | |
346 | " file(s) are on you should start\n", | |
347 | "with the last volume and work", | |
348 | " towards the first.\n"); | |
349 | } else { | |
350 | fprintf(stderr, "You have read volumes"); | |
351 | strcpy(buf, ": "); | |
352 | for (i = 1; i < 32; i++) | |
353 | if (tapesread & (1 << i)) { | |
ddd2ef55 | 354 | fprintf(stderr, "%s%ld", buf, (long)i); |
1227625a SP |
355 | strcpy(buf, ", "); |
356 | } | |
357 | fprintf(stderr, "\n"); | |
358 | } | |
359 | do { | |
360 | fprintf(stderr, "Specify next volume #: "); | |
361 | (void) fflush(stderr); | |
ddf0df5b | 362 | (void) fgets(buf, TP_BSIZE, terminal); |
1227625a SP |
363 | } while (!feof(terminal) && buf[0] == '\n'); |
364 | if (feof(terminal)) | |
ddd2ef55 | 365 | exit(1); |
1227625a SP |
366 | newvol = atoi(buf); |
367 | if (newvol <= 0) { | |
368 | fprintf(stderr, | |
369 | "Volume numbers are positive numerics\n"); | |
370 | } | |
371 | } | |
372 | if (newvol == volno) { | |
373 | tapesread |= 1 << volno; | |
374 | return; | |
375 | } | |
376 | closemt(); | |
394240ce | 377 | if (Mflag) { |
92a9bf12 SP |
378 | snprintf(magtape, MAXPATHLEN, "%s%03ld", magtapeprefix, newvol); |
379 | magtape[MAXPATHLEN - 1] = '\0'; | |
1227625a | 380 | } |
394240ce SP |
381 | if (!Mflag || haderror) { |
382 | haderror = 0; | |
383 | fprintf(stderr, "Mount tape volume %ld\n", (long)newvol); | |
384 | fprintf(stderr, "Enter ``none'' if there are no more tapes\n"); | |
385 | fprintf(stderr, "otherwise enter tape name (default: %s) ", magtape); | |
386 | (void) fflush(stderr); | |
ddf0df5b | 387 | (void) fgets(buf, TP_BSIZE, terminal); |
394240ce SP |
388 | if (feof(terminal)) |
389 | exit(1); | |
390 | if (!strcmp(buf, "none\n")) { | |
391 | terminateinput(); | |
392 | return; | |
393 | } | |
394 | if (buf[0] != '\n') { | |
395 | (void) strcpy(magtape, buf); | |
396 | magtape[strlen(magtape) - 1] = '\0'; | |
397 | } | |
1227625a SP |
398 | } |
399 | #ifdef RRESTORE | |
400 | if (host) | |
401 | mt = rmtopen(magtape, 0); | |
402 | else | |
403 | #endif | |
404 | mt = open(magtape, O_RDONLY, 0); | |
405 | ||
406 | if (mt == -1) { | |
407 | fprintf(stderr, "Cannot open %s\n", magtape); | |
408 | volno = -1; | |
394240ce | 409 | haderror = 1; |
1227625a SP |
410 | goto again; |
411 | } | |
412 | gethdr: | |
413 | volno = newvol; | |
414 | setdumpnum(); | |
415 | FLUSHTAPEBUF(); | |
416 | if (gethead(&tmpbuf) == FAIL) { | |
ddd2ef55 | 417 | Dprintf(stdout, "header read failed at %ld blocks\n", (long)blksread); |
1227625a SP |
418 | fprintf(stderr, "tape is not dump tape\n"); |
419 | volno = 0; | |
394240ce | 420 | haderror = 1; |
1227625a SP |
421 | goto again; |
422 | } | |
423 | if (tmpbuf.c_volume != volno) { | |
424 | fprintf(stderr, "Wrong volume (%d)\n", tmpbuf.c_volume); | |
425 | volno = 0; | |
394240ce | 426 | haderror = 1; |
1227625a SP |
427 | goto again; |
428 | } | |
429 | if (tmpbuf.c_date != dumpdate || tmpbuf.c_ddate != dumptime) { | |
430 | #ifdef __linux__ | |
431 | fprintf(stderr, "Wrong dump date\n\tgot: %s", | |
432 | ctime4(&tmpbuf.c_date)); | |
433 | fprintf(stderr, "\twanted: %s", ctime4(&dumpdate)); | |
434 | #else | |
435 | fprintf(stderr, "Wrong dump date\n\tgot: %s", | |
436 | ctime(&tmpbuf.c_date)); | |
437 | fprintf(stderr, "\twanted: %s", ctime(&dumpdate)); | |
438 | #endif | |
439 | volno = 0; | |
394240ce | 440 | haderror = 1; |
1227625a SP |
441 | goto again; |
442 | } | |
443 | tapesread |= 1 << volno; | |
444 | blksread = savecnt; | |
445 | /* | |
446 | * If continuing from the previous volume, skip over any | |
447 | * blocks read already at the end of the previous volume. | |
448 | * | |
449 | * If coming to this volume at random, skip to the beginning | |
450 | * of the next record. | |
451 | */ | |
ddd2ef55 SP |
452 | Dprintf(stdout, "read %ld recs, tape starts with %ld\n", |
453 | tpblksread, (long)tmpbuf.c_firstrec); | |
1227625a SP |
454 | if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER)) { |
455 | if (!wantnext) { | |
456 | tpblksread = tmpbuf.c_firstrec; | |
457 | for (i = tmpbuf.c_count; i > 0; i--) | |
458 | readtape(buf); | |
459 | } else if (tmpbuf.c_firstrec > 0 && | |
460 | tmpbuf.c_firstrec < tpblksread - 1) { | |
461 | /* | |
462 | * -1 since we've read the volume header | |
463 | */ | |
464 | i = tpblksread - tmpbuf.c_firstrec - 1; | |
ddd2ef55 SP |
465 | Dprintf(stderr, "Skipping %ld duplicate record%s.\n", |
466 | (long)i, i > 1 ? "s" : ""); | |
1227625a SP |
467 | while (--i >= 0) |
468 | readtape(buf); | |
469 | } | |
470 | } | |
471 | if (curfile.action == USING) { | |
472 | if (volno == 1) | |
473 | panic("active file into volume 1\n"); | |
474 | return; | |
475 | } | |
476 | /* | |
477 | * Skip up to the beginning of the next record | |
478 | */ | |
479 | if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER)) | |
480 | for (i = tmpbuf.c_count; i > 0; i--) | |
481 | readtape(buf); | |
482 | (void) gethead(&spcl); | |
483 | findinode(&spcl); | |
484 | if (gettingfile) { | |
485 | gettingfile = 0; | |
ddd2ef55 | 486 | siglongjmp(restart, 1); |
1227625a SP |
487 | } |
488 | } | |
489 | ||
490 | /* | |
491 | * Handle unexpected EOF. | |
492 | */ | |
493 | static void | |
ddd2ef55 | 494 | terminateinput(void) |
1227625a SP |
495 | { |
496 | ||
497 | if (gettingfile && curfile.action == USING) { | |
498 | printf("Warning: %s %s\n", | |
499 | "End-of-input encountered while extracting", curfile.name); | |
500 | } | |
501 | curfile.name = "<name unknown>"; | |
502 | curfile.action = UNKNOWN; | |
503 | curfile.dip = NULL; | |
504 | curfile.ino = maxino; | |
505 | if (gettingfile) { | |
506 | gettingfile = 0; | |
ddd2ef55 | 507 | siglongjmp(restart, 1); |
1227625a SP |
508 | } |
509 | } | |
510 | ||
511 | /* | |
512 | * handle multiple dumps per tape by skipping forward to the | |
513 | * appropriate one. | |
514 | */ | |
515 | static void | |
ddd2ef55 | 516 | setdumpnum(void) |
1227625a SP |
517 | { |
518 | struct mtop tcom; | |
519 | ||
520 | if (dumpnum == 1 || volno != 1) | |
521 | return; | |
ddd2ef55 SP |
522 | if (pipein) |
523 | errx(1, "Cannot have multiple dumps on pipe input"); | |
1227625a SP |
524 | tcom.mt_op = MTFSF; |
525 | tcom.mt_count = dumpnum - 1; | |
526 | #ifdef RRESTORE | |
527 | if (host) | |
528 | rmtioctl(MTFSF, dumpnum - 1); | |
b45f51d6 | 529 | else |
1227625a SP |
530 | #endif |
531 | if (ioctl(mt, (int)MTIOCTOP, (char *)&tcom) < 0) | |
ddd2ef55 | 532 | warn("ioctl MTFSF"); |
1227625a SP |
533 | } |
534 | ||
535 | void | |
ddd2ef55 | 536 | printdumpinfo(void) |
1227625a SP |
537 | { |
538 | #ifdef __linux__ | |
539 | fprintf(stdout, "Dump date: %s", ctime4(&spcl.c_date)); | |
540 | fprintf(stdout, "Dumped from: %s", | |
541 | (spcl.c_ddate == 0) ? "the epoch\n" : ctime4(&spcl.c_ddate)); | |
542 | #else | |
543 | fprintf(stdout, "Dump date: %s", ctime(&spcl.c_date)); | |
544 | fprintf(stdout, "Dumped from: %s", | |
545 | (spcl.c_ddate == 0) ? "the epoch\n" : ctime(&spcl.c_ddate)); | |
546 | #endif | |
547 | if (spcl.c_host[0] == '\0') | |
548 | return; | |
549 | fprintf(stderr, "Level %d dump of %s on %s:%s\n", | |
550 | spcl.c_level, spcl.c_filesys, spcl.c_host, spcl.c_dev); | |
551 | fprintf(stderr, "Label: %s\n", spcl.c_label); | |
552 | } | |
553 | ||
554 | int | |
ddd2ef55 | 555 | extractfile(char *name) |
1227625a | 556 | { |
ddd2ef55 | 557 | unsigned int flags; |
1227625a SP |
558 | mode_t mode; |
559 | struct timeval timep[2]; | |
560 | struct entry *ep; | |
1227625a SP |
561 | |
562 | curfile.name = name; | |
563 | curfile.action = USING; | |
564 | #ifdef __linux__ | |
565 | timep[0].tv_sec = curfile.dip->di_atime.tv_sec; | |
566 | timep[0].tv_usec = curfile.dip->di_atime.tv_usec; | |
567 | timep[1].tv_sec = curfile.dip->di_mtime.tv_sec; | |
568 | timep[1].tv_usec = curfile.dip->di_mtime.tv_usec; | |
569 | #else /* __linux__ */ | |
570 | timep[0].tv_sec = curfile.dip->di_atime; | |
571 | timep[0].tv_usec = curfile.dip->di_atimensec / 1000; | |
572 | timep[1].tv_sec = curfile.dip->di_mtime; | |
573 | timep[1].tv_usec = curfile.dip->di_mtimensec / 1000; | |
574 | #endif /* __linux__ */ | |
575 | mode = curfile.dip->di_mode; | |
576 | flags = curfile.dip->di_flags; | |
577 | switch (mode & IFMT) { | |
578 | ||
579 | default: | |
580 | fprintf(stderr, "%s: unknown file mode 0%o\n", name, mode); | |
581 | skipfile(); | |
582 | return (FAIL); | |
583 | ||
584 | case IFSOCK: | |
ddd2ef55 | 585 | Vprintf(stdout, "skipped socket %s\n", name); |
1227625a SP |
586 | skipfile(); |
587 | return (GOOD); | |
588 | ||
589 | case IFDIR: | |
590 | if (mflag) { | |
591 | ep = lookupname(name); | |
592 | if (ep == NULL || ep->e_flags & EXTRACT) | |
593 | panic("unextracted directory %s\n", name); | |
594 | skipfile(); | |
595 | return (GOOD); | |
596 | } | |
ddd2ef55 | 597 | Vprintf(stdout, "extract file %s\n", name); |
1227625a SP |
598 | return (genliteraldir(name, curfile.ino)); |
599 | ||
600 | case IFLNK: | |
a95c4c36 SP |
601 | { |
602 | #ifdef HAVE_LCHOWN | |
603 | uid_t luid = curfile.dip->di_uid; | |
ddd2ef55 | 604 | gid_t lgid = curfile.dip->di_gid; |
a95c4c36 | 605 | #endif |
1227625a SP |
606 | lnkbuf[0] = '\0'; |
607 | pathlen = 0; | |
1227625a SP |
608 | getfile(xtrlnkfile, xtrlnkskip); |
609 | if (pathlen == 0) { | |
ddd2ef55 | 610 | Vprintf(stdout, |
1227625a SP |
611 | "%s: zero length symbolic link (ignored)\n", name); |
612 | return (GOOD); | |
613 | } | |
ddd2ef55 SP |
614 | if (linkit(lnkbuf, name, SYMLINK) == FAIL) |
615 | return (FAIL); | |
08ebf8d7 SP |
616 | #ifdef HAVE_LCHOWN |
617 | (void) lchown(name, luid, lgid); | |
618 | #endif | |
ddd2ef55 SP |
619 | return (GOOD); |
620 | } | |
1227625a | 621 | |
b45f51d6 | 622 | case IFIFO: |
ddd2ef55 | 623 | Vprintf(stdout, "extract fifo %s\n", name); |
1227625a SP |
624 | if (Nflag) { |
625 | skipfile(); | |
626 | return (GOOD); | |
627 | } | |
b45f51d6 SP |
628 | if (uflag && !Nflag) |
629 | (void)unlink(name); | |
630 | if (mkfifo(name, mode) < 0) { | |
ddd2ef55 | 631 | warn("%s: cannot create fifo", name); |
1227625a SP |
632 | skipfile(); |
633 | return (FAIL); | |
634 | } | |
635 | (void) chown(name, curfile.dip->di_uid, curfile.dip->di_gid); | |
636 | (void) chmod(name, mode); | |
8d4197bb | 637 | if (flags) |
b45f51d6 | 638 | #ifdef __linux__ |
8d4197bb | 639 | (void) fsetflags(name, flags); |
1227625a | 640 | #else |
8d4197bb | 641 | (void) chflags(name, flags); |
1227625a SP |
642 | #endif |
643 | skipfile(); | |
644 | utimes(name, timep); | |
645 | return (GOOD); | |
646 | ||
b45f51d6 SP |
647 | case IFCHR: |
648 | case IFBLK: | |
ddd2ef55 | 649 | Vprintf(stdout, "extract special file %s\n", name); |
1227625a SP |
650 | if (Nflag) { |
651 | skipfile(); | |
652 | return (GOOD); | |
653 | } | |
b45f51d6 SP |
654 | if (uflag) |
655 | (void)unlink(name); | |
656 | if (mknod(name, mode, (int)curfile.dip->di_rdev) < 0) { | |
ddd2ef55 | 657 | warn("%s: cannot create special file", name); |
1227625a SP |
658 | skipfile(); |
659 | return (FAIL); | |
660 | } | |
661 | (void) chown(name, curfile.dip->di_uid, curfile.dip->di_gid); | |
662 | (void) chmod(name, mode); | |
8d4197bb | 663 | if (flags) |
1227625a | 664 | #ifdef __linux__ |
8d4197bb SP |
665 | { |
666 | warn("%s: fsetflags called on a special file", name); | |
667 | (void) fsetflags(name, flags); | |
668 | } | |
1227625a | 669 | #else |
8d4197bb | 670 | (void) chflags(name, flags); |
1227625a SP |
671 | #endif |
672 | skipfile(); | |
673 | utimes(name, timep); | |
674 | return (GOOD); | |
675 | ||
676 | case IFREG: | |
ddd2ef55 | 677 | Vprintf(stdout, "extract file %s\n", name); |
1227625a SP |
678 | if (Nflag) { |
679 | skipfile(); | |
680 | return (GOOD); | |
681 | } | |
b45f51d6 SP |
682 | if (uflag) |
683 | (void)unlink(name); | |
1227625a SP |
684 | if ((ofile = open(name, O_WRONLY | O_CREAT | O_TRUNC, |
685 | 0666)) < 0) { | |
ddd2ef55 | 686 | warn("%s: cannot create file", name); |
1227625a SP |
687 | skipfile(); |
688 | return (FAIL); | |
689 | } | |
690 | (void) fchown(ofile, curfile.dip->di_uid, curfile.dip->di_gid); | |
691 | (void) fchmod(ofile, mode); | |
8d4197bb | 692 | if (flags) |
1227625a | 693 | #ifdef __linux__ |
8d4197bb | 694 | (void) setflags(ofile, flags); |
1227625a | 695 | #else |
8d4197bb | 696 | (void) fchflags(ofile, flags); |
1227625a SP |
697 | #endif |
698 | getfile(xtrfile, xtrskip); | |
699 | (void) close(ofile); | |
700 | utimes(name, timep); | |
701 | return (GOOD); | |
702 | } | |
703 | /* NOTREACHED */ | |
704 | } | |
705 | ||
706 | /* | |
707 | * skip over bit maps on the tape | |
708 | */ | |
709 | void | |
ddd2ef55 | 710 | skipmaps(void) |
1227625a SP |
711 | { |
712 | ||
713 | while (spcl.c_type == TS_BITS || spcl.c_type == TS_CLRI) | |
714 | skipfile(); | |
715 | } | |
716 | ||
717 | /* | |
718 | * skip over a file on the tape | |
719 | */ | |
720 | void | |
ddd2ef55 | 721 | skipfile(void) |
1227625a SP |
722 | { |
723 | ||
724 | curfile.action = SKIP; | |
725 | getfile(xtrnull, xtrnull); | |
726 | } | |
727 | ||
728 | /* | |
729 | * Extract a file from the tape. | |
730 | * When an allocated block is found it is passed to the fill function; | |
731 | * when an unallocated block (hole) is found, a zeroed buffer is passed | |
732 | * to the skip function. | |
733 | */ | |
734 | void | |
ddd2ef55 | 735 | getfile(void (*fill) __P((char *, size_t)), void (*skip) __P((char *, size_t))) |
1227625a SP |
736 | { |
737 | register int i; | |
ddd2ef55 SP |
738 | volatile int curblk = 0; |
739 | volatile quad_t size = spcl.c_dinode.di_size; | |
740 | volatile int last_write_was_hole = 0; | |
741 | quad_t origsize = size; | |
1227625a SP |
742 | static char clearedbuf[MAXBSIZE]; |
743 | char buf[MAXBSIZE / TP_BSIZE][TP_BSIZE]; | |
744 | char junk[TP_BSIZE]; | |
745 | ||
746 | if (spcl.c_type == TS_END) | |
747 | panic("ran off end of tape\n"); | |
748 | if (spcl.c_magic != NFS_MAGIC) | |
749 | panic("not at beginning of a file\n"); | |
750 | if (!gettingfile && setjmp(restart) != 0) | |
751 | return; | |
752 | gettingfile++; | |
753 | loop: | |
754 | for (i = 0; i < spcl.c_count; i++) { | |
b45f51d6 | 755 | if (readmapflag || spcl.c_addr[i]) { |
1227625a SP |
756 | readtape(&buf[curblk++][0]); |
757 | if (curblk == fssize / TP_BSIZE) { | |
ddd2ef55 | 758 | (*fill)((char *)buf, (size_t)(size > TP_BSIZE ? |
1227625a SP |
759 | fssize : (curblk - 1) * TP_BSIZE + size)); |
760 | curblk = 0; | |
761 | last_write_was_hole = 0; | |
762 | } | |
763 | } else { | |
764 | if (curblk > 0) { | |
ddd2ef55 | 765 | (*fill)((char *)buf, (size_t)(size > TP_BSIZE ? |
1227625a SP |
766 | curblk * TP_BSIZE : |
767 | (curblk - 1) * TP_BSIZE + size)); | |
768 | curblk = 0; | |
769 | } | |
770 | (*skip)(clearedbuf, (long)(size > TP_BSIZE ? | |
771 | TP_BSIZE : size)); | |
772 | last_write_was_hole = 1; | |
773 | } | |
774 | if ((size -= TP_BSIZE) <= 0) { | |
775 | for (i++; i < spcl.c_count; i++) | |
b45f51d6 | 776 | if (readmapflag || spcl.c_addr[i]) |
1227625a SP |
777 | readtape(junk); |
778 | break; | |
779 | } | |
780 | } | |
781 | if (gethead(&spcl) == GOOD && size > 0) { | |
782 | if (spcl.c_type == TS_ADDR) | |
783 | goto loop; | |
ddd2ef55 | 784 | Dprintf(stdout, |
b45f51d6 | 785 | "Missing address (header) block for %s at %ld blocks\n", |
ddd2ef55 | 786 | curfile.name, (long)blksread); |
1227625a SP |
787 | } |
788 | if (curblk > 0) { | |
ddd2ef55 | 789 | (*fill)((char *)buf, (size_t)(curblk * TP_BSIZE) + size); |
1227625a SP |
790 | last_write_was_hole = 0; |
791 | } | |
792 | if (last_write_was_hole) { | |
793 | ftruncate(ofile, origsize); | |
794 | } | |
795 | findinode(&spcl); | |
796 | gettingfile = 0; | |
797 | } | |
798 | ||
799 | /* | |
800 | * Write out the next block of a file. | |
801 | */ | |
802 | static void | |
ddd2ef55 | 803 | xtrfile(char *buf, size_t size) |
1227625a SP |
804 | { |
805 | ||
806 | if (Nflag) | |
807 | return; | |
ddd2ef55 SP |
808 | if (write(ofile, buf, (int) size) == -1) |
809 | err(1, "write error extracting inode %lu, name %s\nwrite", | |
810 | (unsigned long)curfile.ino, curfile.name); | |
1227625a SP |
811 | } |
812 | ||
813 | /* | |
814 | * Skip over a hole in a file. | |
815 | */ | |
816 | /* ARGSUSED */ | |
817 | static void | |
ddd2ef55 | 818 | xtrskip(char *buf, size_t size) |
1227625a SP |
819 | { |
820 | ||
ddd2ef55 SP |
821 | if (lseek(ofile, (off_t)size, SEEK_CUR) == -1) |
822 | err(1, "seek error extracting inode %lu, name %s\nlseek", | |
823 | (unsigned long)curfile.ino, curfile.name); | |
1227625a SP |
824 | } |
825 | ||
826 | /* | |
827 | * Collect the next block of a symbolic link. | |
828 | */ | |
829 | static void | |
ddd2ef55 | 830 | xtrlnkfile(char *buf, size_t size) |
1227625a SP |
831 | { |
832 | ||
833 | pathlen += size; | |
ddd2ef55 SP |
834 | if (pathlen > MAXPATHLEN) |
835 | errx(1, "symbolic link name: %s->%s%s; too long %d", | |
1227625a | 836 | curfile.name, lnkbuf, buf, pathlen); |
1227625a SP |
837 | (void) strcat(lnkbuf, buf); |
838 | } | |
839 | ||
840 | /* | |
841 | * Skip over a hole in a symbolic link (should never happen). | |
842 | */ | |
843 | /* ARGSUSED */ | |
844 | static void | |
ddd2ef55 | 845 | xtrlnkskip(char *buf, size_t size) |
1227625a SP |
846 | { |
847 | ||
ddd2ef55 | 848 | errx(1, "unallocated block in symbolic link %s", curfile.name); |
1227625a SP |
849 | } |
850 | ||
851 | /* | |
852 | * Collect the next block of a bit map. | |
853 | */ | |
854 | static void | |
ddd2ef55 | 855 | xtrmap(char *buf, size_t size) |
1227625a SP |
856 | { |
857 | ||
858 | memmove(map, buf, size); | |
859 | map += size; | |
860 | } | |
861 | ||
862 | /* | |
863 | * Skip over a hole in a bit map (should never happen). | |
864 | */ | |
865 | /* ARGSUSED */ | |
866 | static void | |
ddd2ef55 | 867 | xtrmapskip(char *buf, size_t size) |
1227625a SP |
868 | { |
869 | ||
870 | panic("hole in map\n"); | |
871 | map += size; | |
872 | } | |
873 | ||
874 | /* | |
875 | * Noop, when an extraction function is not needed. | |
876 | */ | |
877 | /* ARGSUSED */ | |
878 | void | |
ddd2ef55 | 879 | xtrnull(char *buf, size_t size) |
1227625a SP |
880 | { |
881 | ||
882 | return; | |
883 | } | |
884 | ||
d8574d45 SP |
885 | #if COMPARE_ONTHEFLY |
886 | /* | |
887 | * Compare the next block of a file. | |
888 | */ | |
889 | static void | |
890 | xtrcmpfile(char *buf, size_t size) | |
891 | { | |
892 | static char cmpbuf[MAXBSIZE]; | |
893 | ||
894 | if (cmperror) | |
895 | return; | |
896 | ||
897 | if (read(ifile, cmpbuf, size) != size) { | |
898 | fprintf(stderr, "%s: size has changed.\n", | |
899 | curfile.name); | |
900 | cmperror = 1; | |
901 | return; | |
902 | } | |
903 | ||
904 | if (memcmp(buf, cmpbuf, size) != 0) { | |
905 | fprintf(stderr, "%s: tape and disk copies are different\n", | |
906 | curfile.name); | |
907 | cmperror = 1; | |
908 | return; | |
909 | } | |
910 | } | |
911 | ||
912 | /* | |
913 | * Skip over a hole in a file. | |
914 | */ | |
915 | static void | |
916 | xtrcmpskip(char *buf, size_t size) | |
917 | { | |
918 | static char cmpbuf[MAXBSIZE]; | |
919 | int i; | |
920 | ||
921 | if (cmperror) | |
922 | return; | |
923 | ||
924 | if (read(ifile, cmpbuf, size) != size) { | |
925 | fprintf(stderr, "%s: size has changed.\n", | |
926 | curfile.name); | |
927 | cmperror = 1; | |
928 | return; | |
929 | } | |
930 | ||
931 | for (i = 0; i < size; ++i) | |
932 | if (cmpbuf[i] != '\0') { | |
933 | fprintf(stderr, "%s: tape and disk copies are different\n", | |
934 | curfile.name); | |
935 | cmperror = 1; | |
936 | return; | |
937 | } | |
938 | } | |
939 | #endif /* COMPARE_ONTHEFLY */ | |
940 | ||
941 | #if !COMPARE_ONTHEFLY | |
ddd2ef55 | 942 | static int |
1227625a SP |
943 | do_cmpfiles(int fd_tape, int fd_disk, long size) |
944 | { | |
1227625a SP |
945 | static char buf_tape[BUFSIZ]; |
946 | static char buf_disk[BUFSIZ]; | |
ddd2ef55 SP |
947 | ssize_t n_tape; |
948 | ssize_t n_disk; | |
1227625a SP |
949 | |
950 | while (size > 0) { | |
ddd2ef55 | 951 | if ((n_tape = read(fd_tape, buf_tape, sizeof(buf_tape))) < 1) { |
1227625a SP |
952 | close(fd_tape), close(fd_disk); |
953 | panic("do_cmpfiles: unexpected EOF[1]"); | |
954 | } | |
ddd2ef55 | 955 | if ((n_disk = read(fd_disk, buf_disk, sizeof(buf_tape))) < 1) { |
1227625a SP |
956 | close(fd_tape), close(fd_disk); |
957 | panic("do_cmpfiles: unexpected EOF[2]"); | |
958 | } | |
959 | if (n_tape != n_disk) { | |
960 | close(fd_tape), close(fd_disk); | |
961 | panic("do_cmpfiles: sizes different!"); | |
962 | } | |
ddd2ef55 | 963 | if (memcmp(buf_tape, buf_disk, (size_t)n_tape) != 0) return (1); |
1227625a SP |
964 | size -= n_tape; |
965 | } | |
966 | return (0); | |
967 | } | |
968 | ||
969 | /* for debugging compare problems */ | |
970 | #undef COMPARE_FAIL_KEEP_FILE | |
971 | ||
ddd2ef55 | 972 | static |
1227625a SP |
973 | #ifdef COMPARE_FAIL_KEEP_FILE |
974 | /* return true if tapefile should be unlinked after compare */ | |
975 | int | |
976 | #else | |
977 | void | |
978 | #endif | |
979 | cmpfiles(char *tapefile, char *diskfile, struct stat *sbuf_disk) | |
980 | { | |
981 | struct stat sbuf_tape; | |
982 | int fd_tape, fd_disk; | |
983 | ||
984 | if (stat(tapefile, &sbuf_tape) != 0) { | |
985 | panic("Can't lstat tmp file %s: %s\n", tapefile, | |
986 | strerror(errno)); | |
3d78f5f2 | 987 | compare_errors = 1; |
1227625a SP |
988 | } |
989 | ||
990 | if (sbuf_disk->st_size != sbuf_tape.st_size) { | |
991 | fprintf(stderr, | |
b45f51d6 | 992 | "%s: size changed from %ld to %ld.\n", |
ddd2ef55 | 993 | diskfile, (long)sbuf_tape.st_size, (long)sbuf_disk->st_size); |
3d78f5f2 | 994 | compare_errors = 1; |
1227625a SP |
995 | #ifdef COMPARE_FAIL_KEEP_FILE |
996 | return (0); | |
997 | #else | |
998 | return; | |
999 | #endif | |
1000 | } | |
1001 | ||
1002 | if ((fd_tape = open(tapefile, O_RDONLY)) < 0) { | |
1003 | panic("Can't open %s: %s\n", tapefile, strerror(errno)); | |
3d78f5f2 | 1004 | compare_errors = 1; |
1227625a SP |
1005 | } |
1006 | if ((fd_disk = open(diskfile, O_RDONLY)) < 0) { | |
1007 | close(fd_tape); | |
1008 | panic("Can't open %s: %s\n", diskfile, strerror(errno)); | |
3d78f5f2 | 1009 | compare_errors = 1; |
1227625a SP |
1010 | } |
1011 | ||
1012 | if (do_cmpfiles(fd_tape, fd_disk, sbuf_tape.st_size)) { | |
1013 | fprintf(stderr, "%s: tape and disk copies are different\n", | |
1014 | diskfile); | |
1015 | close(fd_tape); | |
1016 | close(fd_disk); | |
3d78f5f2 | 1017 | compare_errors = 1; |
1227625a SP |
1018 | #ifdef COMPARE_FAIL_KEEP_FILE |
1019 | /* rename the file to live in /tmp */ | |
1020 | /* rename `tapefile' to /tmp/<basename of diskfile> */ | |
1021 | { | |
1022 | char *p = strrchr(diskfile, '/'); | |
1023 | char newname[MAXPATHLEN]; | |
1024 | if (!p) { | |
1025 | panic("can't find / in %s\n", diskfile); | |
1026 | } | |
ddd2ef55 | 1027 | snprintf(newname, sizeof(newname), "%s/debug/%s", tmpdir, p + 1); |
1227625a SP |
1028 | if (rename(tapefile, newname)) { |
1029 | panic("rename from %s to %s failed: %s\n", | |
1030 | tapefile, newname, | |
1031 | strerror(errno)); | |
1032 | } else { | |
1033 | fprintf(stderr, "*** %s saved to %s\n", | |
1034 | tapefile, newname); | |
1035 | } | |
1036 | } | |
1037 | ||
1038 | /* don't unlink the file (it's not there anymore */ | |
1039 | /* anyway) */ | |
1040 | return (0); | |
1041 | #else | |
1042 | return; | |
1043 | #endif | |
1044 | } | |
1045 | close(fd_tape); | |
1046 | close(fd_disk); | |
1047 | #ifdef COMPARE_FAIL_KEEP_FILE | |
1048 | return (1); | |
1049 | #endif | |
1050 | } | |
d8574d45 | 1051 | #endif /* !COMPARE_ONTHEFLY */ |
1227625a | 1052 | |
d8574d45 | 1053 | #if !COMPARE_ONTHEFLY |
ddd2ef55 | 1054 | static char tmpfilename[MAXPATHLEN]; |
d8574d45 | 1055 | #endif |
1227625a SP |
1056 | |
1057 | void | |
ddd2ef55 | 1058 | comparefile(char *name) |
1227625a | 1059 | { |
1227625a | 1060 | int mode; |
d8574d45 | 1061 | struct stat sb; |
1227625a | 1062 | int r; |
d8574d45 SP |
1063 | #if !COMPARE_ONTHEFLY |
1064 | static char *tmpfile = NULL; | |
1065 | struct stat stemp; | |
1066 | #endif | |
1227625a SP |
1067 | |
1068 | if ((r = lstat(name, &sb)) != 0) { | |
ddd2ef55 | 1069 | warn("%s: does not exist (%d)", name, r); |
3d78f5f2 | 1070 | compare_errors = 1; |
1227625a SP |
1071 | skipfile(); |
1072 | return; | |
1073 | } | |
1074 | ||
1075 | curfile.name = name; | |
b45f51d6 | 1076 | curfile.action = USING; |
1227625a SP |
1077 | mode = curfile.dip->di_mode; |
1078 | ||
ddd2ef55 SP |
1079 | Vprintf(stdout, "comparing %s (size: %ld, mode: 0%o)\n", name, |
1080 | (long)sb.st_size, mode); | |
1227625a SP |
1081 | |
1082 | if (sb.st_mode != mode) { | |
1083 | fprintf(stderr, "%s: mode changed from 0%o to 0%o.\n", | |
1084 | name, mode & 07777, sb.st_mode & 07777); | |
3d78f5f2 | 1085 | compare_errors = 1; |
1227625a SP |
1086 | } |
1087 | switch (mode & IFMT) { | |
1088 | default: | |
1089 | skipfile(); | |
1090 | return; | |
1091 | ||
1092 | case IFSOCK: | |
1093 | skipfile(); | |
1094 | return; | |
1095 | ||
1096 | case IFDIR: | |
1097 | skipfile(); | |
1098 | return; | |
1099 | ||
1100 | case IFLNK: { | |
1101 | char lbuf[MAXPATHLEN + 1]; | |
1102 | int lsize; | |
1103 | ||
1104 | if (!(sb.st_mode & S_IFLNK)) { | |
1105 | fprintf(stderr, "%s: is no longer a symbolic link\n", | |
1106 | name); | |
3d78f5f2 | 1107 | compare_errors = 1; |
1227625a SP |
1108 | return; |
1109 | } | |
1110 | lnkbuf[0] = '\0'; | |
1111 | pathlen = 0; | |
1112 | getfile(xtrlnkfile, xtrlnkskip); | |
1113 | if (pathlen == 0) { | |
1114 | fprintf(stderr, | |
1115 | "%s: zero length symbolic link (ignored)\n", | |
1116 | name); | |
3d78f5f2 | 1117 | compare_errors = 1; |
1227625a SP |
1118 | return; |
1119 | } | |
1120 | if ((lsize = readlink(name, lbuf, MAXPATHLEN)) < 0) { | |
1121 | panic("readlink of %s failed: %s", name, | |
1122 | strerror(errno)); | |
3d78f5f2 | 1123 | compare_errors = 1; |
1227625a SP |
1124 | } |
1125 | lbuf[lsize] = 0; | |
1126 | if (strcmp(lbuf, lnkbuf) != 0) { | |
1127 | fprintf(stderr, | |
1128 | "%s: symbolic link changed from %s to %s.\n", | |
1129 | name, lnkbuf, lbuf); | |
3d78f5f2 | 1130 | compare_errors = 1; |
1227625a SP |
1131 | return; |
1132 | } | |
1133 | return; | |
1134 | } | |
1135 | ||
1136 | case IFCHR: | |
1137 | case IFBLK: | |
1138 | if (!(sb.st_mode & (S_IFCHR|S_IFBLK))) { | |
1139 | fprintf(stderr, "%s: no longer a special file\n", | |
1140 | name); | |
3d78f5f2 | 1141 | compare_errors = 1; |
1227625a SP |
1142 | skipfile(); |
1143 | return; | |
1144 | } | |
1145 | ||
1146 | if (sb.st_rdev != (int)curfile.dip->di_rdev) { | |
1147 | fprintf(stderr, | |
1148 | "%s: device changed from %d,%d to %d,%d.\n", | |
1149 | name, | |
ddd2ef55 SP |
1150 | ((int)curfile.dip->di_rdev >> 8) & 0xff, |
1151 | (int)curfile.dip->di_rdev & 0xff, | |
1152 | ((int)sb.st_rdev >> 8) & 0xff, | |
1153 | (int)sb.st_rdev & 0xff); | |
3d78f5f2 | 1154 | compare_errors = 1; |
1227625a SP |
1155 | } |
1156 | skipfile(); | |
1157 | return; | |
1158 | ||
1159 | case IFREG: | |
d8574d45 SP |
1160 | #if COMPARE_ONTHEFLY |
1161 | if ((ifile = open(name, O_RDONLY)) < 0) { | |
1162 | panic("Can't open %s: %s\n", name, strerror(errno)); | |
1163 | skipfile(); | |
1164 | compare_errors = 1; | |
1165 | } | |
1166 | else { | |
1167 | cmperror = 0; | |
1168 | getfile(xtrcmpfile, xtrcmpskip); | |
1169 | if (!cmperror) { | |
1170 | char c; | |
1171 | if (read(ifile, &c, 1) != 0) { | |
1172 | fprintf(stderr, "%s: size has changed.\n", | |
1173 | name); | |
1174 | cmperror = 1; | |
1175 | } | |
1176 | } | |
1177 | if (cmperror) | |
1178 | compare_errors = 1; | |
1179 | close(ifile); | |
1180 | } | |
1181 | #else | |
1227625a SP |
1182 | if (tmpfile == NULL) { |
1183 | /* argument to mktemp() must not be in RO space: */ | |
ddd2ef55 | 1184 | snprintf(tmpfilename, sizeof(tmpfilename), "%s/restoreCXXXXXX", tmpdir); |
1227625a SP |
1185 | tmpfile = mktemp(&tmpfilename[0]); |
1186 | } | |
1187 | if ((stat(tmpfile, &stemp) == 0) && (unlink(tmpfile) != 0)) { | |
1188 | panic("cannot delete tmp file %s: %s\n", | |
1189 | tmpfile, strerror(errno)); | |
1190 | } | |
1191 | if ((ofile = creat(tmpfile, 0600)) < 0) { | |
1192 | panic("cannot create file temp file %s: %s\n", | |
1193 | name, strerror(errno)); | |
1194 | } | |
1195 | getfile(xtrfile, xtrskip); | |
1196 | (void) close(ofile); | |
1197 | #ifdef COMPARE_FAIL_KEEP_FILE | |
1198 | if (cmpfiles(tmpfile, name, &sb)) | |
1199 | unlink(tmpfile); | |
1200 | #else | |
1201 | cmpfiles(tmpfile, name, &sb); | |
1202 | unlink(tmpfile); | |
1203 | #endif | |
d8574d45 | 1204 | #endif /* COMPARE_ONTHEFLY */ |
1227625a SP |
1205 | return; |
1206 | } | |
1207 | /* NOTREACHED */ | |
1208 | } | |
1209 | ||
1210 | /* | |
1211 | * Read TP_BSIZE blocks from the input. | |
1212 | * Handle read errors, and end of media. | |
1213 | */ | |
1214 | static void | |
ddd2ef55 | 1215 | readtape(char *buf) |
1227625a | 1216 | { |
ddd2ef55 | 1217 | ssize_t rd, newvol, i; |
1227625a SP |
1218 | int cnt, seek_failed; |
1219 | ||
1220 | if (blkcnt < numtrec) { | |
ddd2ef55 | 1221 | memmove(buf, &tapebuf[(blkcnt++ * TP_BSIZE)], TP_BSIZE); |
1227625a SP |
1222 | blksread++; |
1223 | tpblksread++; | |
1224 | return; | |
1225 | } | |
1226 | for (i = 0; i < ntrec; i++) | |
1227 | ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0; | |
1228 | if (numtrec == 0) | |
1229 | numtrec = ntrec; | |
1230 | cnt = ntrec * TP_BSIZE; | |
1231 | rd = 0; | |
1232 | getmore: | |
1233 | #ifdef RRESTORE | |
1234 | if (host) | |
1235 | i = rmtread(&tapebuf[rd], cnt); | |
1236 | else | |
1237 | #endif | |
1238 | i = read(mt, &tapebuf[rd], cnt); | |
1239 | /* | |
1240 | * Check for mid-tape short read error. | |
1241 | * If found, skip rest of buffer and start with the next. | |
1242 | */ | |
1243 | if (!pipein && numtrec < ntrec && i > 0) { | |
ddd2ef55 | 1244 | Dprintf(stdout, "mid-media short read error.\n"); |
1227625a SP |
1245 | numtrec = ntrec; |
1246 | } | |
1247 | /* | |
1248 | * Handle partial block read. | |
1249 | */ | |
1250 | if (pipein && i == 0 && rd > 0) | |
1251 | i = rd; | |
1252 | else if (i > 0 && i != ntrec * TP_BSIZE) { | |
1253 | if (pipein) { | |
1254 | rd += i; | |
1255 | cnt -= i; | |
1256 | if (cnt > 0) | |
1257 | goto getmore; | |
1258 | i = rd; | |
1259 | } else { | |
1260 | /* | |
1261 | * Short read. Process the blocks read. | |
1262 | */ | |
1263 | if (i % TP_BSIZE != 0) | |
ddd2ef55 | 1264 | Vprintf(stdout, |
b45f51d6 | 1265 | "partial block read: %ld should be %ld\n", |
ddd2ef55 | 1266 | (long)i, ntrec * TP_BSIZE); |
1227625a SP |
1267 | numtrec = i / TP_BSIZE; |
1268 | } | |
1269 | } | |
1270 | /* | |
1271 | * Handle read error. | |
1272 | */ | |
1273 | if (i < 0) { | |
1274 | fprintf(stderr, "Tape read error while "); | |
1275 | switch (curfile.action) { | |
1276 | default: | |
1277 | fprintf(stderr, "trying to set up tape\n"); | |
1278 | break; | |
1279 | case UNKNOWN: | |
1280 | fprintf(stderr, "trying to resynchronize\n"); | |
1281 | break; | |
1282 | case USING: | |
1283 | fprintf(stderr, "restoring %s\n", curfile.name); | |
1284 | break; | |
1285 | case SKIP: | |
ddd2ef55 SP |
1286 | fprintf(stderr, "skipping over inode %lu\n", |
1287 | (unsigned long)curfile.ino); | |
1227625a SP |
1288 | break; |
1289 | } | |
1290 | if (!yflag && !reply("continue")) | |
ddd2ef55 | 1291 | exit(1); |
1227625a | 1292 | i = ntrec * TP_BSIZE; |
ddd2ef55 | 1293 | memset(tapebuf, 0, (size_t)i); |
1227625a SP |
1294 | #ifdef RRESTORE |
1295 | if (host) | |
1296 | seek_failed = (rmtseek(i, 1) < 0); | |
1297 | else | |
1298 | #endif | |
1299 | seek_failed = (lseek(mt, i, SEEK_CUR) == (off_t)-1); | |
1300 | ||
ddd2ef55 SP |
1301 | if (seek_failed) |
1302 | err(1, "continuation failed"); | |
1227625a SP |
1303 | } |
1304 | /* | |
1305 | * Handle end of tape. | |
1306 | */ | |
1307 | if (i == 0) { | |
ddd2ef55 | 1308 | Vprintf(stdout, "End-of-tape encountered\n"); |
1227625a SP |
1309 | if (!pipein) { |
1310 | newvol = volno + 1; | |
1311 | volno = 0; | |
1312 | numtrec = 0; | |
1313 | getvol(newvol); | |
1314 | readtape(buf); | |
1315 | return; | |
1316 | } | |
1317 | if (rd % TP_BSIZE != 0) | |
1318 | panic("partial block read: %d should be %d\n", | |
1319 | rd, ntrec * TP_BSIZE); | |
1320 | terminateinput(); | |
ddd2ef55 | 1321 | memmove(&tapebuf[rd], &endoftapemark, TP_BSIZE); |
1227625a SP |
1322 | } |
1323 | blkcnt = 0; | |
ddd2ef55 | 1324 | memmove(buf, &tapebuf[(blkcnt++ * TP_BSIZE)], TP_BSIZE); |
1227625a SP |
1325 | blksread++; |
1326 | tpblksread++; | |
1327 | } | |
1328 | ||
1329 | static void | |
ddd2ef55 | 1330 | findtapeblksize(void) |
1227625a SP |
1331 | { |
1332 | register long i; | |
1333 | ||
1334 | for (i = 0; i < ntrec; i++) | |
1335 | ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0; | |
1336 | blkcnt = 0; | |
1337 | #ifdef RRESTORE | |
1338 | if (host) | |
ddd2ef55 | 1339 | i = rmtread(tapebuf, (size_t)(ntrec * TP_BSIZE)); |
1227625a SP |
1340 | else |
1341 | #endif | |
ddd2ef55 | 1342 | i = read(mt, tapebuf, (size_t)(ntrec * TP_BSIZE)); |
1227625a | 1343 | |
ddd2ef55 SP |
1344 | if (i <= 0) |
1345 | err(1, "tape read error"); | |
1346 | if (i % TP_BSIZE != 0) | |
1347 | errx(1, "Tape block size (%ld) is not a multiple of dump block size (%d)", | |
1348 | (long)i, TP_BSIZE); | |
1227625a SP |
1349 | ntrec = i / TP_BSIZE; |
1350 | numtrec = ntrec; | |
ddd2ef55 | 1351 | Vprintf(stdout, "Tape block size is %ld\n", ntrec); |
1227625a SP |
1352 | } |
1353 | ||
1354 | void | |
ddd2ef55 | 1355 | closemt(void) |
1227625a SP |
1356 | { |
1357 | ||
1358 | if (mt < 0) | |
1359 | return; | |
1360 | #ifdef RRESTORE | |
1361 | if (host) | |
1362 | rmtclose(); | |
1363 | else | |
1364 | #endif | |
1365 | (void) close(mt); | |
1366 | } | |
1367 | ||
1368 | /* | |
1369 | * Read the next block from the tape. | |
1370 | * Check to see if it is one of several vintage headers. | |
1371 | * If it is an old style header, convert it to a new style header. | |
1372 | * If it is not any valid header, return an error. | |
1373 | */ | |
1374 | static int | |
ddd2ef55 | 1375 | gethead(struct s_spcl *buf) |
1227625a | 1376 | { |
ddd2ef55 | 1377 | int32_t i; |
1227625a SP |
1378 | union { |
1379 | quad_t qval; | |
b45f51d6 | 1380 | int32_t val[2]; |
1227625a SP |
1381 | } qcvt; |
1382 | union u_ospcl { | |
1383 | char dummy[TP_BSIZE]; | |
1384 | struct s_ospcl { | |
b45f51d6 SP |
1385 | int32_t c_type; |
1386 | int32_t c_date; | |
1387 | int32_t c_ddate; | |
1388 | int32_t c_volume; | |
1389 | int32_t c_tapea; | |
ddd2ef55 | 1390 | u_int16_t c_inumber; |
b45f51d6 SP |
1391 | int32_t c_magic; |
1392 | int32_t c_checksum; | |
1227625a | 1393 | struct odinode { |
ddd2ef55 SP |
1394 | u_int16_t odi_mode; |
1395 | u_int16_t odi_nlink; | |
1396 | u_int16_t odi_uid; | |
1397 | u_int16_t odi_gid; | |
b45f51d6 SP |
1398 | int32_t odi_size; |
1399 | int32_t odi_rdev; | |
1227625a | 1400 | char odi_addr[36]; |
b45f51d6 SP |
1401 | int32_t odi_atime; |
1402 | int32_t odi_mtime; | |
1403 | int32_t odi_ctime; | |
1227625a | 1404 | } c_dinode; |
b45f51d6 | 1405 | int32_t c_count; |
1227625a SP |
1406 | char c_addr[256]; |
1407 | } s_ospcl; | |
1408 | } u_ospcl; | |
1409 | ||
1410 | if (!cvtflag) { | |
1411 | readtape((char *)buf); | |
1412 | if (buf->c_magic != NFS_MAGIC) { | |
ddd2ef55 | 1413 | if (swabi(buf->c_magic) != NFS_MAGIC) |
1227625a SP |
1414 | return (FAIL); |
1415 | if (!Bcvt) { | |
ddd2ef55 | 1416 | Vprintf(stdout, "Note: Doing Byte swapping\n"); |
1227625a SP |
1417 | Bcvt = 1; |
1418 | } | |
1419 | } | |
1420 | if (checksum((int *)buf) == FAIL) | |
1421 | return (FAIL); | |
ddd2ef55 SP |
1422 | if (Bcvt) |
1423 | swabst((u_char *)"8i4s31i528bi192b2i", (u_char *)buf); | |
1227625a SP |
1424 | goto good; |
1425 | } | |
1426 | readtape((char *)(&u_ospcl.s_ospcl)); | |
ddd2ef55 | 1427 | memset((char *)buf, 0, (long)TP_BSIZE); |
1227625a SP |
1428 | buf->c_type = u_ospcl.s_ospcl.c_type; |
1429 | buf->c_date = u_ospcl.s_ospcl.c_date; | |
1430 | buf->c_ddate = u_ospcl.s_ospcl.c_ddate; | |
1431 | buf->c_volume = u_ospcl.s_ospcl.c_volume; | |
1432 | buf->c_tapea = u_ospcl.s_ospcl.c_tapea; | |
1433 | buf->c_inumber = u_ospcl.s_ospcl.c_inumber; | |
1434 | buf->c_checksum = u_ospcl.s_ospcl.c_checksum; | |
1435 | buf->c_magic = u_ospcl.s_ospcl.c_magic; | |
1436 | buf->c_dinode.di_mode = u_ospcl.s_ospcl.c_dinode.odi_mode; | |
1437 | buf->c_dinode.di_nlink = u_ospcl.s_ospcl.c_dinode.odi_nlink; | |
1438 | buf->c_dinode.di_uid = u_ospcl.s_ospcl.c_dinode.odi_uid; | |
1439 | buf->c_dinode.di_gid = u_ospcl.s_ospcl.c_dinode.odi_gid; | |
1440 | buf->c_dinode.di_size = u_ospcl.s_ospcl.c_dinode.odi_size; | |
1441 | buf->c_dinode.di_rdev = u_ospcl.s_ospcl.c_dinode.odi_rdev; | |
1442 | #ifdef __linux__ | |
1443 | buf->c_dinode.di_atime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_atime; | |
1444 | buf->c_dinode.di_mtime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_mtime; | |
1445 | buf->c_dinode.di_ctime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_ctime; | |
1446 | #else /* __linux__ */ | |
1447 | buf->c_dinode.di_atime = u_ospcl.s_ospcl.c_dinode.odi_atime; | |
1448 | buf->c_dinode.di_mtime = u_ospcl.s_ospcl.c_dinode.odi_mtime; | |
1449 | buf->c_dinode.di_ctime = u_ospcl.s_ospcl.c_dinode.odi_ctime; | |
1450 | #endif /* __linux__ */ | |
1451 | buf->c_count = u_ospcl.s_ospcl.c_count; | |
1452 | memmove(buf->c_addr, u_ospcl.s_ospcl.c_addr, (long)256); | |
1453 | if (u_ospcl.s_ospcl.c_magic != OFS_MAGIC || | |
1454 | checksum((int *)(&u_ospcl.s_ospcl)) == FAIL) | |
1455 | return(FAIL); | |
1456 | buf->c_magic = NFS_MAGIC; | |
1457 | ||
1458 | good: | |
1459 | if ((buf->c_dinode.di_size == 0 || buf->c_dinode.di_size > 0xfffffff) && | |
1460 | (buf->c_dinode.di_mode & IFMT) == IFDIR && Qcvt == 0) { | |
1461 | qcvt.qval = buf->c_dinode.di_size; | |
1462 | if (qcvt.val[0] || qcvt.val[1]) { | |
1463 | printf("Note: Doing Quad swapping\n"); | |
1464 | Qcvt = 1; | |
1465 | } | |
1466 | } | |
1467 | if (Qcvt) { | |
1468 | qcvt.qval = buf->c_dinode.di_size; | |
1469 | i = qcvt.val[1]; | |
1470 | qcvt.val[1] = qcvt.val[0]; | |
1471 | qcvt.val[0] = i; | |
1472 | buf->c_dinode.di_size = qcvt.qval; | |
1473 | } | |
b45f51d6 | 1474 | readmapflag = 0; |
1227625a SP |
1475 | |
1476 | switch (buf->c_type) { | |
1477 | ||
1478 | case TS_CLRI: | |
1479 | case TS_BITS: | |
1480 | /* | |
1481 | * Have to patch up missing information in bit map headers | |
1482 | */ | |
1483 | buf->c_inumber = 0; | |
1484 | buf->c_dinode.di_size = buf->c_count * TP_BSIZE; | |
b45f51d6 SP |
1485 | if (buf->c_count > TP_NINDIR) |
1486 | readmapflag = 1; | |
1487 | else | |
1488 | for (i = 0; i < buf->c_count; i++) | |
1489 | buf->c_addr[i]++; | |
1227625a SP |
1490 | break; |
1491 | ||
1492 | case TS_TAPE: | |
1493 | if ((buf->c_flags & DR_NEWINODEFMT) == 0) | |
1494 | oldinofmt = 1; | |
1495 | /* fall through */ | |
1496 | case TS_END: | |
1497 | buf->c_inumber = 0; | |
1498 | break; | |
1499 | ||
1500 | case TS_INODE: | |
1501 | case TS_ADDR: | |
1502 | break; | |
1503 | ||
1504 | default: | |
1505 | panic("gethead: unknown inode type %d\n", buf->c_type); | |
1506 | break; | |
1507 | } | |
1508 | /* | |
b45f51d6 | 1509 | * If we are restoring a filesystem with old format inodes, |
1227625a SP |
1510 | * copy the uid/gid to the new location. |
1511 | */ | |
1512 | if (oldinofmt) { | |
1513 | buf->c_dinode.di_uid = buf->c_dinode.di_ouid; | |
1514 | buf->c_dinode.di_gid = buf->c_dinode.di_ogid; | |
1515 | } | |
1516 | if (dflag) | |
1517 | accthdr(buf); | |
1518 | return(GOOD); | |
1519 | } | |
1520 | ||
1521 | /* | |
1522 | * Check that a header is where it belongs and predict the next header | |
1523 | */ | |
1524 | static void | |
ddd2ef55 | 1525 | accthdr(struct s_spcl *header) |
1227625a SP |
1526 | { |
1527 | static ino_t previno = 0x7fffffff; | |
1528 | static int prevtype; | |
1529 | static long predict; | |
1530 | long blks, i; | |
1531 | ||
1532 | if (header->c_type == TS_TAPE) { | |
1533 | fprintf(stderr, "Volume header (%s inode format) ", | |
1534 | oldinofmt ? "old" : "new"); | |
1535 | if (header->c_firstrec) | |
1536 | fprintf(stderr, "begins with record %d", | |
1537 | header->c_firstrec); | |
1538 | fprintf(stderr, "\n"); | |
1539 | previno = 0x7fffffff; | |
1540 | return; | |
1541 | } | |
1542 | if (previno == 0x7fffffff) | |
1543 | goto newcalc; | |
1544 | switch (prevtype) { | |
1545 | case TS_BITS: | |
1546 | fprintf(stderr, "Dumped inodes map header"); | |
1547 | break; | |
1548 | case TS_CLRI: | |
1549 | fprintf(stderr, "Used inodes map header"); | |
1550 | break; | |
1551 | case TS_INODE: | |
ddd2ef55 | 1552 | fprintf(stderr, "File header, ino %lu", (unsigned long)previno); |
1227625a SP |
1553 | break; |
1554 | case TS_ADDR: | |
b9c89cf4 | 1555 | fprintf(stderr, "File continuation header, ino %ld", (long)previno); |
1227625a SP |
1556 | break; |
1557 | case TS_END: | |
1558 | fprintf(stderr, "End of tape header"); | |
1559 | break; | |
1560 | } | |
1561 | if (predict != blksread - 1) | |
b45f51d6 | 1562 | fprintf(stderr, "; predicted %ld blocks, got %ld blocks", |
1227625a SP |
1563 | predict, blksread - 1); |
1564 | fprintf(stderr, "\n"); | |
1565 | newcalc: | |
1566 | blks = 0; | |
1567 | if (header->c_type != TS_END) | |
1568 | for (i = 0; i < header->c_count; i++) | |
b45f51d6 | 1569 | if (readmapflag || header->c_addr[i] != 0) |
1227625a SP |
1570 | blks++; |
1571 | predict = blks; | |
1572 | blksread = 0; | |
1573 | prevtype = header->c_type; | |
1574 | previno = header->c_inumber; | |
1575 | } | |
1576 | ||
1577 | /* | |
1578 | * Find an inode header. | |
1579 | * Complain if had to skip, and complain is set. | |
1580 | */ | |
1581 | static void | |
ddd2ef55 | 1582 | findinode(struct s_spcl *header) |
1227625a SP |
1583 | { |
1584 | static long skipcnt = 0; | |
1585 | long i; | |
1586 | char buf[TP_BSIZE]; | |
1587 | ||
1588 | curfile.name = "<name unknown>"; | |
1589 | curfile.action = UNKNOWN; | |
1590 | curfile.dip = NULL; | |
1591 | curfile.ino = 0; | |
1592 | do { | |
1593 | if (header->c_magic != NFS_MAGIC) { | |
1594 | skipcnt++; | |
1595 | while (gethead(header) == FAIL || | |
1596 | header->c_date != dumpdate) | |
1597 | skipcnt++; | |
1598 | } | |
1599 | switch (header->c_type) { | |
1600 | ||
1601 | case TS_ADDR: | |
1602 | /* | |
1603 | * Skip up to the beginning of the next record | |
1604 | */ | |
1605 | for (i = 0; i < header->c_count; i++) | |
1606 | if (header->c_addr[i]) | |
1607 | readtape(buf); | |
1608 | while (gethead(header) == FAIL || | |
1609 | header->c_date != dumpdate) | |
1610 | skipcnt++; | |
1611 | break; | |
1612 | ||
1613 | case TS_INODE: | |
1614 | curfile.dip = &header->c_dinode; | |
1615 | curfile.ino = header->c_inumber; | |
1616 | break; | |
1617 | ||
1618 | case TS_END: | |
1619 | curfile.ino = maxino; | |
1620 | break; | |
1621 | ||
1622 | case TS_CLRI: | |
1623 | curfile.name = "<file removal list>"; | |
1624 | break; | |
1625 | ||
1626 | case TS_BITS: | |
1627 | curfile.name = "<file dump list>"; | |
1628 | break; | |
1629 | ||
1630 | case TS_TAPE: | |
1631 | panic("unexpected tape header\n"); | |
1632 | /* NOTREACHED */ | |
1633 | ||
1634 | default: | |
1635 | panic("unknown tape header type %d\n", spcl.c_type); | |
1636 | /* NOTREACHED */ | |
1637 | ||
1638 | } | |
1639 | } while (header->c_type == TS_ADDR); | |
1640 | if (skipcnt > 0) | |
b45f51d6 SP |
1641 | fprintf(stderr, "resync restore, skipped %ld blocks\n", |
1642 | skipcnt); | |
1227625a SP |
1643 | skipcnt = 0; |
1644 | } | |
1645 | ||
1646 | static int | |
ddd2ef55 | 1647 | checksum(int *buf) |
1227625a SP |
1648 | { |
1649 | register int i, j; | |
1650 | ||
1651 | j = sizeof(union u_spcl) / sizeof(int); | |
1652 | i = 0; | |
1653 | if(!Bcvt) { | |
1654 | do | |
1655 | i += *buf++; | |
1656 | while (--j); | |
1657 | } else { | |
1658 | /* What happens if we want to read restore tapes | |
1659 | for a 16bit int machine??? */ | |
b45f51d6 | 1660 | do |
ddd2ef55 | 1661 | i += swabi(*buf++); |
1227625a SP |
1662 | while (--j); |
1663 | } | |
b45f51d6 | 1664 | |
1227625a | 1665 | if (i != CHECKSUM) { |
ddd2ef55 SP |
1666 | fprintf(stderr, "Checksum error %o, inode %lu file %s\n", i, |
1667 | (unsigned long)curfile.ino, curfile.name); | |
1227625a SP |
1668 | return(FAIL); |
1669 | } | |
1670 | return(GOOD); | |
1671 | } | |
1672 | ||
1673 | #ifdef RRESTORE | |
ddd2ef55 | 1674 | #ifdef __STDC__ |
1227625a SP |
1675 | #include <stdarg.h> |
1676 | #else | |
1677 | #include <varargs.h> | |
1678 | #endif | |
1679 | ||
1680 | void | |
ddd2ef55 | 1681 | #ifdef __STDC__ |
1227625a SP |
1682 | msg(const char *fmt, ...) |
1683 | #else | |
1684 | msg(fmt, va_alist) | |
1685 | char *fmt; | |
1686 | va_dcl | |
1687 | #endif | |
1688 | { | |
1689 | va_list ap; | |
ddd2ef55 | 1690 | #ifdef __STDC__ |
1227625a SP |
1691 | va_start(ap, fmt); |
1692 | #else | |
1693 | va_start(ap); | |
1694 | #endif | |
1695 | (void)vfprintf(stderr, fmt, ap); | |
1696 | va_end(ap); | |
1697 | } | |
1698 | #endif /* RRESTORE */ | |
1699 | ||
1700 | static u_char * | |
ddd2ef55 | 1701 | swab16(u_char *sp, int n) |
1227625a SP |
1702 | { |
1703 | char c; | |
1704 | ||
1705 | while (--n >= 0) { | |
1706 | c = sp[0]; sp[0] = sp[1]; sp[1] = c; | |
1707 | sp += 2; | |
1708 | } | |
1709 | return (sp); | |
1710 | } | |
1711 | ||
1712 | static u_char * | |
ddd2ef55 | 1713 | swab32(u_char *sp, int n) |
1227625a SP |
1714 | { |
1715 | char c; | |
1716 | ||
1717 | while (--n >= 0) { | |
1718 | c = sp[0]; sp[0] = sp[3]; sp[3] = c; | |
ddd2ef55 | 1719 | c = sp[1]; sp[1] = sp[2]; sp[2] = c; |
1227625a SP |
1720 | sp += 4; |
1721 | } | |
1722 | return (sp); | |
1723 | } | |
1724 | ||
ddd2ef55 SP |
1725 | static u_char * |
1726 | swab64(u_char *sp, int n) | |
1727 | { | |
1728 | char c; | |
1729 | ||
1730 | while (--n >= 0) { | |
1731 | c = sp[0]; sp[0] = sp[7]; sp[7] = c; | |
1732 | c = sp[1]; sp[1] = sp[6]; sp[6] = c; | |
1733 | c = sp[2]; sp[2] = sp[5]; sp[5] = c; | |
1734 | c = sp[3]; sp[3] = sp[4]; sp[4] = c; | |
1735 | sp += 8; | |
1736 | } | |
1737 | return (sp); | |
1738 | } | |
1739 | ||
1227625a | 1740 | void |
ddd2ef55 | 1741 | swabst(u_char *cp, u_char *sp) |
1227625a SP |
1742 | { |
1743 | int n = 0; | |
1744 | ||
1745 | while (*cp) { | |
1746 | switch (*cp) { | |
1747 | case '0': case '1': case '2': case '3': case '4': | |
1748 | case '5': case '6': case '7': case '8': case '9': | |
1749 | n = (n * 10) + (*cp++ - '0'); | |
1750 | continue; | |
b45f51d6 | 1751 | |
1227625a SP |
1752 | case 's': case 'w': case 'h': |
1753 | if (n == 0) | |
1754 | n = 1; | |
ddd2ef55 SP |
1755 | sp = swab16(sp, n); |
1756 | break; | |
1757 | ||
1758 | case 'i': | |
1759 | if (n == 0) | |
1760 | n = 1; | |
1761 | sp = swab32(sp, n); | |
1227625a SP |
1762 | break; |
1763 | ||
1764 | case 'l': | |
1765 | if (n == 0) | |
1766 | n = 1; | |
ddd2ef55 | 1767 | sp = swab64(sp, n); |
1227625a SP |
1768 | break; |
1769 | ||
1770 | default: /* Any other character, like 'b' counts as byte. */ | |
1771 | if (n == 0) | |
1772 | n = 1; | |
1773 | sp += n; | |
1774 | break; | |
1775 | } | |
1776 | cp++; | |
1777 | n = 0; | |
1778 | } | |
1779 | } | |
1780 | ||
ddd2ef55 SP |
1781 | static u_int |
1782 | swabi(u_int x) | |
1783 | { | |
1784 | swabst((u_char *)"i", (u_char *)&x); | |
1785 | return (x); | |
1786 | } | |
1787 | ||
1227625a | 1788 | static u_long |
ddd2ef55 | 1789 | swabl(u_long x) |
1227625a SP |
1790 | { |
1791 | swabst((u_char *)"l", (u_char *)&x); | |
1792 | return (x); | |
1793 | } |