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