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