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