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