]> git.wh0rd.org - dump.git/blame - restore/tape.c
Use lchown instead of chown (security problem of symlinks ownership)
[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[] =
08ebf8d7 48 "$Id: tape.c,v 1.7 1999/11/05 22:02:19 tiniou 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
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);
08ebf8d7
SP
582#ifdef HAVE_LCHOWN
583 (void) lchown(name, luid, lgid);
584#endif
ddd2ef55
SP
585 return (GOOD);
586 }
1227625a 587
b45f51d6 588 case IFIFO:
ddd2ef55 589 Vprintf(stdout, "extract fifo %s\n", name);
1227625a
SP
590 if (Nflag) {
591 skipfile();
592 return (GOOD);
593 }
b45f51d6
SP
594 if (uflag && !Nflag)
595 (void)unlink(name);
596 if (mkfifo(name, mode) < 0) {
ddd2ef55 597 warn("%s: cannot create fifo", name);
1227625a
SP
598 skipfile();
599 return (FAIL);
600 }
601 (void) chown(name, curfile.dip->di_uid, curfile.dip->di_gid);
602 (void) chmod(name, mode);
8d4197bb 603 if (flags)
b45f51d6 604#ifdef __linux__
8d4197bb 605 (void) fsetflags(name, flags);
1227625a 606#else
8d4197bb 607 (void) chflags(name, flags);
1227625a
SP
608#endif
609 skipfile();
610 utimes(name, timep);
611 return (GOOD);
612
b45f51d6
SP
613 case IFCHR:
614 case IFBLK:
ddd2ef55 615 Vprintf(stdout, "extract special file %s\n", name);
1227625a
SP
616 if (Nflag) {
617 skipfile();
618 return (GOOD);
619 }
b45f51d6
SP
620 if (uflag)
621 (void)unlink(name);
622 if (mknod(name, mode, (int)curfile.dip->di_rdev) < 0) {
ddd2ef55 623 warn("%s: cannot create special file", name);
1227625a
SP
624 skipfile();
625 return (FAIL);
626 }
627 (void) chown(name, curfile.dip->di_uid, curfile.dip->di_gid);
628 (void) chmod(name, mode);
8d4197bb 629 if (flags)
1227625a 630#ifdef __linux__
8d4197bb
SP
631 {
632 warn("%s: fsetflags called on a special file", name);
633 (void) fsetflags(name, flags);
634 }
1227625a 635#else
8d4197bb 636 (void) chflags(name, flags);
1227625a
SP
637#endif
638 skipfile();
639 utimes(name, timep);
640 return (GOOD);
641
642 case IFREG:
ddd2ef55 643 Vprintf(stdout, "extract file %s\n", name);
1227625a
SP
644 if (Nflag) {
645 skipfile();
646 return (GOOD);
647 }
b45f51d6
SP
648 if (uflag)
649 (void)unlink(name);
1227625a
SP
650 if ((ofile = open(name, O_WRONLY | O_CREAT | O_TRUNC,
651 0666)) < 0) {
ddd2ef55 652 warn("%s: cannot create file", name);
1227625a
SP
653 skipfile();
654 return (FAIL);
655 }
656 (void) fchown(ofile, curfile.dip->di_uid, curfile.dip->di_gid);
657 (void) fchmod(ofile, mode);
8d4197bb 658 if (flags)
1227625a 659#ifdef __linux__
8d4197bb 660 (void) setflags(ofile, flags);
1227625a 661#else
8d4197bb 662 (void) fchflags(ofile, flags);
1227625a
SP
663#endif
664 getfile(xtrfile, xtrskip);
665 (void) close(ofile);
666 utimes(name, timep);
667 return (GOOD);
668 }
669 /* NOTREACHED */
670}
671
672/*
673 * skip over bit maps on the tape
674 */
675void
ddd2ef55 676skipmaps(void)
1227625a
SP
677{
678
679 while (spcl.c_type == TS_BITS || spcl.c_type == TS_CLRI)
680 skipfile();
681}
682
683/*
684 * skip over a file on the tape
685 */
686void
ddd2ef55 687skipfile(void)
1227625a
SP
688{
689
690 curfile.action = SKIP;
691 getfile(xtrnull, xtrnull);
692}
693
694/*
695 * Extract a file from the tape.
696 * When an allocated block is found it is passed to the fill function;
697 * when an unallocated block (hole) is found, a zeroed buffer is passed
698 * to the skip function.
699 */
700void
ddd2ef55 701getfile(void (*fill) __P((char *, size_t)), void (*skip) __P((char *, size_t)))
1227625a
SP
702{
703 register int i;
ddd2ef55
SP
704 volatile int curblk = 0;
705 volatile quad_t size = spcl.c_dinode.di_size;
706 volatile int last_write_was_hole = 0;
707 quad_t origsize = size;
1227625a
SP
708 static char clearedbuf[MAXBSIZE];
709 char buf[MAXBSIZE / TP_BSIZE][TP_BSIZE];
710 char junk[TP_BSIZE];
711
712 if (spcl.c_type == TS_END)
713 panic("ran off end of tape\n");
714 if (spcl.c_magic != NFS_MAGIC)
715 panic("not at beginning of a file\n");
716 if (!gettingfile && setjmp(restart) != 0)
717 return;
718 gettingfile++;
719loop:
720 for (i = 0; i < spcl.c_count; i++) {
b45f51d6 721 if (readmapflag || spcl.c_addr[i]) {
1227625a
SP
722 readtape(&buf[curblk++][0]);
723 if (curblk == fssize / TP_BSIZE) {
ddd2ef55 724 (*fill)((char *)buf, (size_t)(size > TP_BSIZE ?
1227625a
SP
725 fssize : (curblk - 1) * TP_BSIZE + size));
726 curblk = 0;
727 last_write_was_hole = 0;
728 }
729 } else {
730 if (curblk > 0) {
ddd2ef55 731 (*fill)((char *)buf, (size_t)(size > TP_BSIZE ?
1227625a
SP
732 curblk * TP_BSIZE :
733 (curblk - 1) * TP_BSIZE + size));
734 curblk = 0;
735 }
736 (*skip)(clearedbuf, (long)(size > TP_BSIZE ?
737 TP_BSIZE : size));
738 last_write_was_hole = 1;
739 }
740 if ((size -= TP_BSIZE) <= 0) {
741 for (i++; i < spcl.c_count; i++)
b45f51d6 742 if (readmapflag || spcl.c_addr[i])
1227625a
SP
743 readtape(junk);
744 break;
745 }
746 }
747 if (gethead(&spcl) == GOOD && size > 0) {
748 if (spcl.c_type == TS_ADDR)
749 goto loop;
ddd2ef55 750 Dprintf(stdout,
b45f51d6 751 "Missing address (header) block for %s at %ld blocks\n",
ddd2ef55 752 curfile.name, (long)blksread);
1227625a
SP
753 }
754 if (curblk > 0) {
ddd2ef55 755 (*fill)((char *)buf, (size_t)(curblk * TP_BSIZE) + size);
1227625a
SP
756 last_write_was_hole = 0;
757 }
758 if (last_write_was_hole) {
759 ftruncate(ofile, origsize);
760 }
761 findinode(&spcl);
762 gettingfile = 0;
763}
764
765/*
766 * Write out the next block of a file.
767 */
768static void
ddd2ef55 769xtrfile(char *buf, size_t size)
1227625a
SP
770{
771
772 if (Nflag)
773 return;
ddd2ef55
SP
774 if (write(ofile, buf, (int) size) == -1)
775 err(1, "write error extracting inode %lu, name %s\nwrite",
776 (unsigned long)curfile.ino, curfile.name);
1227625a
SP
777}
778
779/*
780 * Skip over a hole in a file.
781 */
782/* ARGSUSED */
783static void
ddd2ef55 784xtrskip(char *buf, size_t size)
1227625a
SP
785{
786
ddd2ef55
SP
787 if (lseek(ofile, (off_t)size, SEEK_CUR) == -1)
788 err(1, "seek error extracting inode %lu, name %s\nlseek",
789 (unsigned long)curfile.ino, curfile.name);
1227625a
SP
790}
791
792/*
793 * Collect the next block of a symbolic link.
794 */
795static void
ddd2ef55 796xtrlnkfile(char *buf, size_t size)
1227625a
SP
797{
798
799 pathlen += size;
ddd2ef55
SP
800 if (pathlen > MAXPATHLEN)
801 errx(1, "symbolic link name: %s->%s%s; too long %d",
1227625a 802 curfile.name, lnkbuf, buf, pathlen);
1227625a
SP
803 (void) strcat(lnkbuf, buf);
804}
805
806/*
807 * Skip over a hole in a symbolic link (should never happen).
808 */
809/* ARGSUSED */
810static void
ddd2ef55 811xtrlnkskip(char *buf, size_t size)
1227625a
SP
812{
813
ddd2ef55 814 errx(1, "unallocated block in symbolic link %s", curfile.name);
1227625a
SP
815}
816
817/*
818 * Collect the next block of a bit map.
819 */
820static void
ddd2ef55 821xtrmap(char *buf, size_t size)
1227625a
SP
822{
823
824 memmove(map, buf, size);
825 map += size;
826}
827
828/*
829 * Skip over a hole in a bit map (should never happen).
830 */
831/* ARGSUSED */
832static void
ddd2ef55 833xtrmapskip(char *buf, size_t size)
1227625a
SP
834{
835
836 panic("hole in map\n");
837 map += size;
838}
839
840/*
841 * Noop, when an extraction function is not needed.
842 */
843/* ARGSUSED */
844void
ddd2ef55 845xtrnull(char *buf, size_t size)
1227625a
SP
846{
847
848 return;
849}
850
ddd2ef55 851static int
1227625a
SP
852do_cmpfiles(int fd_tape, int fd_disk, long size)
853{
1227625a
SP
854 static char buf_tape[BUFSIZ];
855 static char buf_disk[BUFSIZ];
ddd2ef55
SP
856 ssize_t n_tape;
857 ssize_t n_disk;
1227625a
SP
858
859 while (size > 0) {
ddd2ef55 860 if ((n_tape = read(fd_tape, buf_tape, sizeof(buf_tape))) < 1) {
1227625a
SP
861 close(fd_tape), close(fd_disk);
862 panic("do_cmpfiles: unexpected EOF[1]");
863 }
ddd2ef55 864 if ((n_disk = read(fd_disk, buf_disk, sizeof(buf_tape))) < 1) {
1227625a
SP
865 close(fd_tape), close(fd_disk);
866 panic("do_cmpfiles: unexpected EOF[2]");
867 }
868 if (n_tape != n_disk) {
869 close(fd_tape), close(fd_disk);
870 panic("do_cmpfiles: sizes different!");
871 }
ddd2ef55 872 if (memcmp(buf_tape, buf_disk, (size_t)n_tape) != 0) return (1);
1227625a
SP
873 size -= n_tape;
874 }
875 return (0);
876}
877
878/* for debugging compare problems */
879#undef COMPARE_FAIL_KEEP_FILE
880
ddd2ef55 881static
1227625a
SP
882#ifdef COMPARE_FAIL_KEEP_FILE
883/* return true if tapefile should be unlinked after compare */
884int
885#else
886void
887#endif
888cmpfiles(char *tapefile, char *diskfile, struct stat *sbuf_disk)
889{
890 struct stat sbuf_tape;
891 int fd_tape, fd_disk;
892
893 if (stat(tapefile, &sbuf_tape) != 0) {
894 panic("Can't lstat tmp file %s: %s\n", tapefile,
895 strerror(errno));
896 }
897
898 if (sbuf_disk->st_size != sbuf_tape.st_size) {
899 fprintf(stderr,
b45f51d6 900 "%s: size changed from %ld to %ld.\n",
ddd2ef55 901 diskfile, (long)sbuf_tape.st_size, (long)sbuf_disk->st_size);
1227625a
SP
902#ifdef COMPARE_FAIL_KEEP_FILE
903 return (0);
904#else
905 return;
906#endif
907 }
908
909 if ((fd_tape = open(tapefile, O_RDONLY)) < 0) {
910 panic("Can't open %s: %s\n", tapefile, strerror(errno));
911 }
912 if ((fd_disk = open(diskfile, O_RDONLY)) < 0) {
913 close(fd_tape);
914 panic("Can't open %s: %s\n", diskfile, strerror(errno));
915 }
916
917 if (do_cmpfiles(fd_tape, fd_disk, sbuf_tape.st_size)) {
918 fprintf(stderr, "%s: tape and disk copies are different\n",
919 diskfile);
920 close(fd_tape);
921 close(fd_disk);
922#ifdef COMPARE_FAIL_KEEP_FILE
923 /* rename the file to live in /tmp */
924 /* rename `tapefile' to /tmp/<basename of diskfile> */
925 {
926 char *p = strrchr(diskfile, '/');
927 char newname[MAXPATHLEN];
928 if (!p) {
929 panic("can't find / in %s\n", diskfile);
930 }
ddd2ef55 931 snprintf(newname, sizeof(newname), "%s/debug/%s", tmpdir, p + 1);
1227625a
SP
932 if (rename(tapefile, newname)) {
933 panic("rename from %s to %s failed: %s\n",
934 tapefile, newname,
935 strerror(errno));
936 } else {
937 fprintf(stderr, "*** %s saved to %s\n",
938 tapefile, newname);
939 }
940 }
941
942 /* don't unlink the file (it's not there anymore */
943 /* anyway) */
944 return (0);
945#else
946 return;
947#endif
948 }
949 close(fd_tape);
950 close(fd_disk);
951#ifdef COMPARE_FAIL_KEEP_FILE
952 return (1);
953#endif
954}
955
ddd2ef55 956static char tmpfilename[MAXPATHLEN];
1227625a
SP
957
958void
ddd2ef55 959comparefile(char *name)
1227625a
SP
960{
961 static char *tmpfile = NULL;
962 int mode;
963 struct stat sb, stemp;
964 int r;
965
966 if ((r = lstat(name, &sb)) != 0) {
ddd2ef55 967 warn("%s: does not exist (%d)", name, r);
1227625a
SP
968 skipfile();
969 return;
970 }
971
972 curfile.name = name;
b45f51d6 973 curfile.action = USING;
1227625a
SP
974 mode = curfile.dip->di_mode;
975
ddd2ef55
SP
976 Vprintf(stdout, "comparing %s (size: %ld, mode: 0%o)\n", name,
977 (long)sb.st_size, mode);
1227625a
SP
978
979 if (sb.st_mode != mode) {
980 fprintf(stderr, "%s: mode changed from 0%o to 0%o.\n",
981 name, mode & 07777, sb.st_mode & 07777);
982 }
983 switch (mode & IFMT) {
984 default:
985 skipfile();
986 return;
987
988 case IFSOCK:
989 skipfile();
990 return;
991
992 case IFDIR:
993 skipfile();
994 return;
995
996 case IFLNK: {
997 char lbuf[MAXPATHLEN + 1];
998 int lsize;
999
1000 if (!(sb.st_mode & S_IFLNK)) {
1001 fprintf(stderr, "%s: is no longer a symbolic link\n",
1002 name);
1003 return;
1004 }
1005 lnkbuf[0] = '\0';
1006 pathlen = 0;
1007 getfile(xtrlnkfile, xtrlnkskip);
1008 if (pathlen == 0) {
1009 fprintf(stderr,
1010 "%s: zero length symbolic link (ignored)\n",
1011 name);
1012 return;
1013 }
1014 if ((lsize = readlink(name, lbuf, MAXPATHLEN)) < 0) {
1015 panic("readlink of %s failed: %s", name,
1016 strerror(errno));
1017 }
1018 lbuf[lsize] = 0;
1019 if (strcmp(lbuf, lnkbuf) != 0) {
1020 fprintf(stderr,
1021 "%s: symbolic link changed from %s to %s.\n",
1022 name, lnkbuf, lbuf);
1023 return;
1024 }
1025 return;
1026 }
1027
1028 case IFCHR:
1029 case IFBLK:
1030 if (!(sb.st_mode & (S_IFCHR|S_IFBLK))) {
1031 fprintf(stderr, "%s: no longer a special file\n",
1032 name);
1033 skipfile();
1034 return;
1035 }
1036
1037 if (sb.st_rdev != (int)curfile.dip->di_rdev) {
1038 fprintf(stderr,
1039 "%s: device changed from %d,%d to %d,%d.\n",
1040 name,
ddd2ef55
SP
1041 ((int)curfile.dip->di_rdev >> 8) & 0xff,
1042 (int)curfile.dip->di_rdev & 0xff,
1043 ((int)sb.st_rdev >> 8) & 0xff,
1044 (int)sb.st_rdev & 0xff);
1227625a
SP
1045 }
1046 skipfile();
1047 return;
1048
1049 case IFREG:
1050 if (tmpfile == NULL) {
1051 /* argument to mktemp() must not be in RO space: */
ddd2ef55 1052 snprintf(tmpfilename, sizeof(tmpfilename), "%s/restoreCXXXXXX", tmpdir);
1227625a
SP
1053 tmpfile = mktemp(&tmpfilename[0]);
1054 }
1055 if ((stat(tmpfile, &stemp) == 0) && (unlink(tmpfile) != 0)) {
1056 panic("cannot delete tmp file %s: %s\n",
1057 tmpfile, strerror(errno));
1058 }
1059 if ((ofile = creat(tmpfile, 0600)) < 0) {
1060 panic("cannot create file temp file %s: %s\n",
1061 name, strerror(errno));
1062 }
1063 getfile(xtrfile, xtrskip);
1064 (void) close(ofile);
1065#ifdef COMPARE_FAIL_KEEP_FILE
1066 if (cmpfiles(tmpfile, name, &sb))
1067 unlink(tmpfile);
1068#else
1069 cmpfiles(tmpfile, name, &sb);
1070 unlink(tmpfile);
1071#endif
1072 return;
1073 }
1074 /* NOTREACHED */
1075}
1076
1077/*
1078 * Read TP_BSIZE blocks from the input.
1079 * Handle read errors, and end of media.
1080 */
1081static void
ddd2ef55 1082readtape(char *buf)
1227625a 1083{
ddd2ef55 1084 ssize_t rd, newvol, i;
1227625a
SP
1085 int cnt, seek_failed;
1086
1087 if (blkcnt < numtrec) {
ddd2ef55 1088 memmove(buf, &tapebuf[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1227625a
SP
1089 blksread++;
1090 tpblksread++;
1091 return;
1092 }
1093 for (i = 0; i < ntrec; i++)
1094 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1095 if (numtrec == 0)
1096 numtrec = ntrec;
1097 cnt = ntrec * TP_BSIZE;
1098 rd = 0;
1099getmore:
1100#ifdef RRESTORE
1101 if (host)
1102 i = rmtread(&tapebuf[rd], cnt);
1103 else
1104#endif
1105 i = read(mt, &tapebuf[rd], cnt);
1106 /*
1107 * Check for mid-tape short read error.
1108 * If found, skip rest of buffer and start with the next.
1109 */
1110 if (!pipein && numtrec < ntrec && i > 0) {
ddd2ef55 1111 Dprintf(stdout, "mid-media short read error.\n");
1227625a
SP
1112 numtrec = ntrec;
1113 }
1114 /*
1115 * Handle partial block read.
1116 */
1117 if (pipein && i == 0 && rd > 0)
1118 i = rd;
1119 else if (i > 0 && i != ntrec * TP_BSIZE) {
1120 if (pipein) {
1121 rd += i;
1122 cnt -= i;
1123 if (cnt > 0)
1124 goto getmore;
1125 i = rd;
1126 } else {
1127 /*
1128 * Short read. Process the blocks read.
1129 */
1130 if (i % TP_BSIZE != 0)
ddd2ef55 1131 Vprintf(stdout,
b45f51d6 1132 "partial block read: %ld should be %ld\n",
ddd2ef55 1133 (long)i, ntrec * TP_BSIZE);
1227625a
SP
1134 numtrec = i / TP_BSIZE;
1135 }
1136 }
1137 /*
1138 * Handle read error.
1139 */
1140 if (i < 0) {
1141 fprintf(stderr, "Tape read error while ");
1142 switch (curfile.action) {
1143 default:
1144 fprintf(stderr, "trying to set up tape\n");
1145 break;
1146 case UNKNOWN:
1147 fprintf(stderr, "trying to resynchronize\n");
1148 break;
1149 case USING:
1150 fprintf(stderr, "restoring %s\n", curfile.name);
1151 break;
1152 case SKIP:
ddd2ef55
SP
1153 fprintf(stderr, "skipping over inode %lu\n",
1154 (unsigned long)curfile.ino);
1227625a
SP
1155 break;
1156 }
1157 if (!yflag && !reply("continue"))
ddd2ef55 1158 exit(1);
1227625a 1159 i = ntrec * TP_BSIZE;
ddd2ef55 1160 memset(tapebuf, 0, (size_t)i);
1227625a
SP
1161#ifdef RRESTORE
1162 if (host)
1163 seek_failed = (rmtseek(i, 1) < 0);
1164 else
1165#endif
1166 seek_failed = (lseek(mt, i, SEEK_CUR) == (off_t)-1);
1167
ddd2ef55
SP
1168 if (seek_failed)
1169 err(1, "continuation failed");
1227625a
SP
1170 }
1171 /*
1172 * Handle end of tape.
1173 */
1174 if (i == 0) {
ddd2ef55 1175 Vprintf(stdout, "End-of-tape encountered\n");
1227625a
SP
1176 if (!pipein) {
1177 newvol = volno + 1;
1178 volno = 0;
1179 numtrec = 0;
1180 getvol(newvol);
1181 readtape(buf);
1182 return;
1183 }
1184 if (rd % TP_BSIZE != 0)
1185 panic("partial block read: %d should be %d\n",
1186 rd, ntrec * TP_BSIZE);
1187 terminateinput();
ddd2ef55 1188 memmove(&tapebuf[rd], &endoftapemark, TP_BSIZE);
1227625a
SP
1189 }
1190 blkcnt = 0;
ddd2ef55 1191 memmove(buf, &tapebuf[(blkcnt++ * TP_BSIZE)], TP_BSIZE);
1227625a
SP
1192 blksread++;
1193 tpblksread++;
1194}
1195
1196static void
ddd2ef55 1197findtapeblksize(void)
1227625a
SP
1198{
1199 register long i;
1200
1201 for (i = 0; i < ntrec; i++)
1202 ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
1203 blkcnt = 0;
1204#ifdef RRESTORE
1205 if (host)
ddd2ef55 1206 i = rmtread(tapebuf, (size_t)(ntrec * TP_BSIZE));
1227625a
SP
1207 else
1208#endif
ddd2ef55 1209 i = read(mt, tapebuf, (size_t)(ntrec * TP_BSIZE));
1227625a 1210
ddd2ef55
SP
1211 if (i <= 0)
1212 err(1, "tape read error");
1213 if (i % TP_BSIZE != 0)
1214 errx(1, "Tape block size (%ld) is not a multiple of dump block size (%d)",
1215 (long)i, TP_BSIZE);
1227625a
SP
1216 ntrec = i / TP_BSIZE;
1217 numtrec = ntrec;
ddd2ef55 1218 Vprintf(stdout, "Tape block size is %ld\n", ntrec);
1227625a
SP
1219}
1220
1221void
ddd2ef55 1222closemt(void)
1227625a
SP
1223{
1224
1225 if (mt < 0)
1226 return;
1227#ifdef RRESTORE
1228 if (host)
1229 rmtclose();
1230 else
1231#endif
1232 (void) close(mt);
1233}
1234
1235/*
1236 * Read the next block from the tape.
1237 * Check to see if it is one of several vintage headers.
1238 * If it is an old style header, convert it to a new style header.
1239 * If it is not any valid header, return an error.
1240 */
1241static int
ddd2ef55 1242gethead(struct s_spcl *buf)
1227625a 1243{
ddd2ef55 1244 int32_t i;
1227625a
SP
1245 union {
1246 quad_t qval;
b45f51d6 1247 int32_t val[2];
1227625a
SP
1248 } qcvt;
1249 union u_ospcl {
1250 char dummy[TP_BSIZE];
1251 struct s_ospcl {
b45f51d6
SP
1252 int32_t c_type;
1253 int32_t c_date;
1254 int32_t c_ddate;
1255 int32_t c_volume;
1256 int32_t c_tapea;
ddd2ef55 1257 u_int16_t c_inumber;
b45f51d6
SP
1258 int32_t c_magic;
1259 int32_t c_checksum;
1227625a 1260 struct odinode {
ddd2ef55
SP
1261 u_int16_t odi_mode;
1262 u_int16_t odi_nlink;
1263 u_int16_t odi_uid;
1264 u_int16_t odi_gid;
b45f51d6
SP
1265 int32_t odi_size;
1266 int32_t odi_rdev;
1227625a 1267 char odi_addr[36];
b45f51d6
SP
1268 int32_t odi_atime;
1269 int32_t odi_mtime;
1270 int32_t odi_ctime;
1227625a 1271 } c_dinode;
b45f51d6 1272 int32_t c_count;
1227625a
SP
1273 char c_addr[256];
1274 } s_ospcl;
1275 } u_ospcl;
1276
1277 if (!cvtflag) {
1278 readtape((char *)buf);
1279 if (buf->c_magic != NFS_MAGIC) {
ddd2ef55 1280 if (swabi(buf->c_magic) != NFS_MAGIC)
1227625a
SP
1281 return (FAIL);
1282 if (!Bcvt) {
ddd2ef55 1283 Vprintf(stdout, "Note: Doing Byte swapping\n");
1227625a
SP
1284 Bcvt = 1;
1285 }
1286 }
1287 if (checksum((int *)buf) == FAIL)
1288 return (FAIL);
ddd2ef55
SP
1289 if (Bcvt)
1290 swabst((u_char *)"8i4s31i528bi192b2i", (u_char *)buf);
1227625a
SP
1291 goto good;
1292 }
1293 readtape((char *)(&u_ospcl.s_ospcl));
ddd2ef55 1294 memset((char *)buf, 0, (long)TP_BSIZE);
1227625a
SP
1295 buf->c_type = u_ospcl.s_ospcl.c_type;
1296 buf->c_date = u_ospcl.s_ospcl.c_date;
1297 buf->c_ddate = u_ospcl.s_ospcl.c_ddate;
1298 buf->c_volume = u_ospcl.s_ospcl.c_volume;
1299 buf->c_tapea = u_ospcl.s_ospcl.c_tapea;
1300 buf->c_inumber = u_ospcl.s_ospcl.c_inumber;
1301 buf->c_checksum = u_ospcl.s_ospcl.c_checksum;
1302 buf->c_magic = u_ospcl.s_ospcl.c_magic;
1303 buf->c_dinode.di_mode = u_ospcl.s_ospcl.c_dinode.odi_mode;
1304 buf->c_dinode.di_nlink = u_ospcl.s_ospcl.c_dinode.odi_nlink;
1305 buf->c_dinode.di_uid = u_ospcl.s_ospcl.c_dinode.odi_uid;
1306 buf->c_dinode.di_gid = u_ospcl.s_ospcl.c_dinode.odi_gid;
1307 buf->c_dinode.di_size = u_ospcl.s_ospcl.c_dinode.odi_size;
1308 buf->c_dinode.di_rdev = u_ospcl.s_ospcl.c_dinode.odi_rdev;
1309#ifdef __linux__
1310 buf->c_dinode.di_atime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_atime;
1311 buf->c_dinode.di_mtime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_mtime;
1312 buf->c_dinode.di_ctime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_ctime;
1313#else /* __linux__ */
1314 buf->c_dinode.di_atime = u_ospcl.s_ospcl.c_dinode.odi_atime;
1315 buf->c_dinode.di_mtime = u_ospcl.s_ospcl.c_dinode.odi_mtime;
1316 buf->c_dinode.di_ctime = u_ospcl.s_ospcl.c_dinode.odi_ctime;
1317#endif /* __linux__ */
1318 buf->c_count = u_ospcl.s_ospcl.c_count;
1319 memmove(buf->c_addr, u_ospcl.s_ospcl.c_addr, (long)256);
1320 if (u_ospcl.s_ospcl.c_magic != OFS_MAGIC ||
1321 checksum((int *)(&u_ospcl.s_ospcl)) == FAIL)
1322 return(FAIL);
1323 buf->c_magic = NFS_MAGIC;
1324
1325good:
1326 if ((buf->c_dinode.di_size == 0 || buf->c_dinode.di_size > 0xfffffff) &&
1327 (buf->c_dinode.di_mode & IFMT) == IFDIR && Qcvt == 0) {
1328 qcvt.qval = buf->c_dinode.di_size;
1329 if (qcvt.val[0] || qcvt.val[1]) {
1330 printf("Note: Doing Quad swapping\n");
1331 Qcvt = 1;
1332 }
1333 }
1334 if (Qcvt) {
1335 qcvt.qval = buf->c_dinode.di_size;
1336 i = qcvt.val[1];
1337 qcvt.val[1] = qcvt.val[0];
1338 qcvt.val[0] = i;
1339 buf->c_dinode.di_size = qcvt.qval;
1340 }
b45f51d6 1341 readmapflag = 0;
1227625a
SP
1342
1343 switch (buf->c_type) {
1344
1345 case TS_CLRI:
1346 case TS_BITS:
1347 /*
1348 * Have to patch up missing information in bit map headers
1349 */
1350 buf->c_inumber = 0;
1351 buf->c_dinode.di_size = buf->c_count * TP_BSIZE;
b45f51d6
SP
1352 if (buf->c_count > TP_NINDIR)
1353 readmapflag = 1;
1354 else
1355 for (i = 0; i < buf->c_count; i++)
1356 buf->c_addr[i]++;
1227625a
SP
1357 break;
1358
1359 case TS_TAPE:
1360 if ((buf->c_flags & DR_NEWINODEFMT) == 0)
1361 oldinofmt = 1;
1362 /* fall through */
1363 case TS_END:
1364 buf->c_inumber = 0;
1365 break;
1366
1367 case TS_INODE:
1368 case TS_ADDR:
1369 break;
1370
1371 default:
1372 panic("gethead: unknown inode type %d\n", buf->c_type);
1373 break;
1374 }
1375 /*
b45f51d6 1376 * If we are restoring a filesystem with old format inodes,
1227625a
SP
1377 * copy the uid/gid to the new location.
1378 */
1379 if (oldinofmt) {
1380 buf->c_dinode.di_uid = buf->c_dinode.di_ouid;
1381 buf->c_dinode.di_gid = buf->c_dinode.di_ogid;
1382 }
1383 if (dflag)
1384 accthdr(buf);
1385 return(GOOD);
1386}
1387
1388/*
1389 * Check that a header is where it belongs and predict the next header
1390 */
1391static void
ddd2ef55 1392accthdr(struct s_spcl *header)
1227625a
SP
1393{
1394 static ino_t previno = 0x7fffffff;
1395 static int prevtype;
1396 static long predict;
1397 long blks, i;
1398
1399 if (header->c_type == TS_TAPE) {
1400 fprintf(stderr, "Volume header (%s inode format) ",
1401 oldinofmt ? "old" : "new");
1402 if (header->c_firstrec)
1403 fprintf(stderr, "begins with record %d",
1404 header->c_firstrec);
1405 fprintf(stderr, "\n");
1406 previno = 0x7fffffff;
1407 return;
1408 }
1409 if (previno == 0x7fffffff)
1410 goto newcalc;
1411 switch (prevtype) {
1412 case TS_BITS:
1413 fprintf(stderr, "Dumped inodes map header");
1414 break;
1415 case TS_CLRI:
1416 fprintf(stderr, "Used inodes map header");
1417 break;
1418 case TS_INODE:
ddd2ef55 1419 fprintf(stderr, "File header, ino %lu", (unsigned long)previno);
1227625a
SP
1420 break;
1421 case TS_ADDR:
b45f51d6 1422 fprintf(stderr, "File continuation header, ino %ld", previno);
1227625a
SP
1423 break;
1424 case TS_END:
1425 fprintf(stderr, "End of tape header");
1426 break;
1427 }
1428 if (predict != blksread - 1)
b45f51d6 1429 fprintf(stderr, "; predicted %ld blocks, got %ld blocks",
1227625a
SP
1430 predict, blksread - 1);
1431 fprintf(stderr, "\n");
1432newcalc:
1433 blks = 0;
1434 if (header->c_type != TS_END)
1435 for (i = 0; i < header->c_count; i++)
b45f51d6 1436 if (readmapflag || header->c_addr[i] != 0)
1227625a
SP
1437 blks++;
1438 predict = blks;
1439 blksread = 0;
1440 prevtype = header->c_type;
1441 previno = header->c_inumber;
1442}
1443
1444/*
1445 * Find an inode header.
1446 * Complain if had to skip, and complain is set.
1447 */
1448static void
ddd2ef55 1449findinode(struct s_spcl *header)
1227625a
SP
1450{
1451 static long skipcnt = 0;
1452 long i;
1453 char buf[TP_BSIZE];
1454
1455 curfile.name = "<name unknown>";
1456 curfile.action = UNKNOWN;
1457 curfile.dip = NULL;
1458 curfile.ino = 0;
1459 do {
1460 if (header->c_magic != NFS_MAGIC) {
1461 skipcnt++;
1462 while (gethead(header) == FAIL ||
1463 header->c_date != dumpdate)
1464 skipcnt++;
1465 }
1466 switch (header->c_type) {
1467
1468 case TS_ADDR:
1469 /*
1470 * Skip up to the beginning of the next record
1471 */
1472 for (i = 0; i < header->c_count; i++)
1473 if (header->c_addr[i])
1474 readtape(buf);
1475 while (gethead(header) == FAIL ||
1476 header->c_date != dumpdate)
1477 skipcnt++;
1478 break;
1479
1480 case TS_INODE:
1481 curfile.dip = &header->c_dinode;
1482 curfile.ino = header->c_inumber;
1483 break;
1484
1485 case TS_END:
1486 curfile.ino = maxino;
1487 break;
1488
1489 case TS_CLRI:
1490 curfile.name = "<file removal list>";
1491 break;
1492
1493 case TS_BITS:
1494 curfile.name = "<file dump list>";
1495 break;
1496
1497 case TS_TAPE:
1498 panic("unexpected tape header\n");
1499 /* NOTREACHED */
1500
1501 default:
1502 panic("unknown tape header type %d\n", spcl.c_type);
1503 /* NOTREACHED */
1504
1505 }
1506 } while (header->c_type == TS_ADDR);
1507 if (skipcnt > 0)
b45f51d6
SP
1508 fprintf(stderr, "resync restore, skipped %ld blocks\n",
1509 skipcnt);
1227625a
SP
1510 skipcnt = 0;
1511}
1512
1513static int
ddd2ef55 1514checksum(int *buf)
1227625a
SP
1515{
1516 register int i, j;
1517
1518 j = sizeof(union u_spcl) / sizeof(int);
1519 i = 0;
1520 if(!Bcvt) {
1521 do
1522 i += *buf++;
1523 while (--j);
1524 } else {
1525 /* What happens if we want to read restore tapes
1526 for a 16bit int machine??? */
b45f51d6 1527 do
ddd2ef55 1528 i += swabi(*buf++);
1227625a
SP
1529 while (--j);
1530 }
b45f51d6 1531
1227625a 1532 if (i != CHECKSUM) {
ddd2ef55
SP
1533 fprintf(stderr, "Checksum error %o, inode %lu file %s\n", i,
1534 (unsigned long)curfile.ino, curfile.name);
1227625a
SP
1535 return(FAIL);
1536 }
1537 return(GOOD);
1538}
1539
1540#ifdef RRESTORE
ddd2ef55 1541#ifdef __STDC__
1227625a
SP
1542#include <stdarg.h>
1543#else
1544#include <varargs.h>
1545#endif
1546
1547void
ddd2ef55 1548#ifdef __STDC__
1227625a
SP
1549msg(const char *fmt, ...)
1550#else
1551msg(fmt, va_alist)
1552 char *fmt;
1553 va_dcl
1554#endif
1555{
1556 va_list ap;
ddd2ef55 1557#ifdef __STDC__
1227625a
SP
1558 va_start(ap, fmt);
1559#else
1560 va_start(ap);
1561#endif
1562 (void)vfprintf(stderr, fmt, ap);
1563 va_end(ap);
1564}
1565#endif /* RRESTORE */
1566
1567static u_char *
ddd2ef55 1568swab16(u_char *sp, int n)
1227625a
SP
1569{
1570 char c;
1571
1572 while (--n >= 0) {
1573 c = sp[0]; sp[0] = sp[1]; sp[1] = c;
1574 sp += 2;
1575 }
1576 return (sp);
1577}
1578
1579static u_char *
ddd2ef55 1580swab32(u_char *sp, int n)
1227625a
SP
1581{
1582 char c;
1583
1584 while (--n >= 0) {
1585 c = sp[0]; sp[0] = sp[3]; sp[3] = c;
ddd2ef55 1586 c = sp[1]; sp[1] = sp[2]; sp[2] = c;
1227625a
SP
1587 sp += 4;
1588 }
1589 return (sp);
1590}
1591
ddd2ef55
SP
1592static u_char *
1593swab64(u_char *sp, int n)
1594{
1595 char c;
1596
1597 while (--n >= 0) {
1598 c = sp[0]; sp[0] = sp[7]; sp[7] = c;
1599 c = sp[1]; sp[1] = sp[6]; sp[6] = c;
1600 c = sp[2]; sp[2] = sp[5]; sp[5] = c;
1601 c = sp[3]; sp[3] = sp[4]; sp[4] = c;
1602 sp += 8;
1603 }
1604 return (sp);
1605}
1606
1227625a 1607void
ddd2ef55 1608swabst(u_char *cp, u_char *sp)
1227625a
SP
1609{
1610 int n = 0;
1611
1612 while (*cp) {
1613 switch (*cp) {
1614 case '0': case '1': case '2': case '3': case '4':
1615 case '5': case '6': case '7': case '8': case '9':
1616 n = (n * 10) + (*cp++ - '0');
1617 continue;
b45f51d6 1618
1227625a
SP
1619 case 's': case 'w': case 'h':
1620 if (n == 0)
1621 n = 1;
ddd2ef55
SP
1622 sp = swab16(sp, n);
1623 break;
1624
1625 case 'i':
1626 if (n == 0)
1627 n = 1;
1628 sp = swab32(sp, n);
1227625a
SP
1629 break;
1630
1631 case 'l':
1632 if (n == 0)
1633 n = 1;
ddd2ef55 1634 sp = swab64(sp, n);
1227625a
SP
1635 break;
1636
1637 default: /* Any other character, like 'b' counts as byte. */
1638 if (n == 0)
1639 n = 1;
1640 sp += n;
1641 break;
1642 }
1643 cp++;
1644 n = 0;
1645 }
1646}
1647
ddd2ef55
SP
1648static u_int
1649swabi(u_int x)
1650{
1651 swabst((u_char *)"i", (u_char *)&x);
1652 return (x);
1653}
1654
1227625a 1655static u_long
ddd2ef55 1656swabl(u_long x)
1227625a
SP
1657{
1658 swabst((u_char *)"l", (u_char *)&x);
1659 return (x);
1660}