]>
Commit | Line | Data |
---|---|---|
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 |
47 | static const char rcsid[] = | |
0c8a8bf9 | 48 | "$Id: tape.c,v 1.14 2000/03/03 11:00:55 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 | ||
81 | static long fssize = MAXBSIZE; | |
82 | static int mt = -1; | |
83 | static int pipein = 0; | |
92a9bf12 SP |
84 | static char magtape[MAXPATHLEN]; |
85 | static char magtapeprefix[MAXPATHLEN]; | |
1227625a SP |
86 | static int blkcnt; |
87 | static int numtrec; | |
88 | static char *tapebuf; | |
89 | static union u_spcl endoftapemark; | |
90 | static long blksread; /* blocks read since last header */ | |
91 | static long tpblksread = 0; /* TP_BSIZE blocks read */ | |
92 | static long tapesread; | |
ddd2ef55 | 93 | static sigjmp_buf restart; |
1227625a SP |
94 | static int gettingfile = 0; /* restart has a valid frame */ |
95 | static char *host = NULL; | |
96 | ||
97 | static int ofile; | |
98 | static char *map; | |
99 | static char lnkbuf[MAXPATHLEN + 1]; | |
100 | static int pathlen; | |
101 | ||
102 | int oldinofmt; /* old inode format conversion required */ | |
103 | int Bcvt; /* Swap Bytes (for CCI or sun) */ | |
104 | static int Qcvt; /* Swap quads (for sun) */ | |
105 | ||
106 | #define FLUSHTAPEBUF() blkcnt = ntrec + 1 | |
107 | ||
108 | static void accthdr __P((struct s_spcl *)); | |
109 | static int checksum __P((int *)); | |
110 | static void findinode __P((struct s_spcl *)); | |
111 | static void findtapeblksize __P((void)); | |
112 | static int gethead __P((struct s_spcl *)); | |
113 | static void readtape __P((char *)); | |
114 | static void setdumpnum __P((void)); | |
ddd2ef55 | 115 | static u_int swabi __P((u_int)); |
1227625a | 116 | static u_long swabl __P((u_long)); |
ddd2ef55 SP |
117 | static u_char *swab64 __P((u_char *, int)); |
118 | static u_char *swab32 __P((u_char *, int)); | |
119 | static u_char *swab16 __P((u_char *, int)); | |
1227625a | 120 | static void terminateinput __P((void)); |
ddd2ef55 SP |
121 | static void xtrfile __P((char *, size_t)); |
122 | static void xtrlnkfile __P((char *, size_t)); | |
123 | static void xtrlnkskip __P((char *, size_t)); | |
124 | static void xtrmap __P((char *, size_t)); | |
125 | static void xtrmapskip __P((char *, size_t)); | |
126 | static void xtrskip __P((char *, size_t)); | |
1227625a | 127 | |
b45f51d6 SP |
128 | static int readmapflag; |
129 | ||
1227625a SP |
130 | /* |
131 | * Set up an input source | |
132 | */ | |
133 | void | |
ddd2ef55 | 134 | setinput(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 | ||
177 | void | |
ddd2ef55 | 178 | newtapebuf(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 | */ | |
197 | void | |
ddd2ef55 | 198 | setup(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 | */ | |
301 | void | |
ddd2ef55 | 302 | getvol(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; | |
322 | again: | |
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 | } | |
403 | gethdr: | |
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 | */ | |
484 | static void | |
ddd2ef55 | 485 | terminateinput(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 | */ | |
506 | static void | |
ddd2ef55 | 507 | setdumpnum(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 | ||
526 | void | |
ddd2ef55 | 527 | printdumpinfo(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 | ||
545 | int | |
ddd2ef55 | 546 | extractfile(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 | */ | |
700 | void | |
ddd2ef55 | 701 | skipmaps(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 | */ | |
711 | void | |
ddd2ef55 | 712 | skipfile(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 | */ | |
725 | void | |
ddd2ef55 | 726 | getfile(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++; | |
744 | loop: | |
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 | */ | |
793 | static void | |
ddd2ef55 | 794 | xtrfile(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 */ | |
808 | static void | |
ddd2ef55 | 809 | xtrskip(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 | */ | |
820 | static void | |
ddd2ef55 | 821 | xtrlnkfile(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 */ | |
835 | static void | |
ddd2ef55 | 836 | xtrlnkskip(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 | */ | |
845 | static void | |
ddd2ef55 | 846 | xtrmap(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 */ | |
857 | static void | |
ddd2ef55 | 858 | xtrmapskip(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 */ | |
869 | void | |
ddd2ef55 | 870 | xtrnull(char *buf, size_t size) |
1227625a SP |
871 | { |
872 | ||
873 | return; | |
874 | } | |
875 | ||
ddd2ef55 | 876 | static int |
1227625a SP |
877 | do_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 | 906 | static |
1227625a SP |
907 | #ifdef COMPARE_FAIL_KEEP_FILE |
908 | /* return true if tapefile should be unlinked after compare */ | |
909 | int | |
910 | #else | |
911 | void | |
912 | #endif | |
913 | cmpfiles(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)); | |
921 | } | |
922 | ||
923 | if (sbuf_disk->st_size != sbuf_tape.st_size) { | |
924 | fprintf(stderr, | |
b45f51d6 | 925 | "%s: size changed from %ld to %ld.\n", |
ddd2ef55 | 926 | diskfile, (long)sbuf_tape.st_size, (long)sbuf_disk->st_size); |
1227625a SP |
927 | #ifdef COMPARE_FAIL_KEEP_FILE |
928 | return (0); | |
929 | #else | |
930 | return; | |
931 | #endif | |
932 | } | |
933 | ||
934 | if ((fd_tape = open(tapefile, O_RDONLY)) < 0) { | |
935 | panic("Can't open %s: %s\n", tapefile, strerror(errno)); | |
936 | } | |
937 | if ((fd_disk = open(diskfile, O_RDONLY)) < 0) { | |
938 | close(fd_tape); | |
939 | panic("Can't open %s: %s\n", diskfile, strerror(errno)); | |
940 | } | |
941 | ||
942 | if (do_cmpfiles(fd_tape, fd_disk, sbuf_tape.st_size)) { | |
943 | fprintf(stderr, "%s: tape and disk copies are different\n", | |
944 | diskfile); | |
945 | close(fd_tape); | |
946 | close(fd_disk); | |
947 | #ifdef COMPARE_FAIL_KEEP_FILE | |
948 | /* rename the file to live in /tmp */ | |
949 | /* rename `tapefile' to /tmp/<basename of diskfile> */ | |
950 | { | |
951 | char *p = strrchr(diskfile, '/'); | |
952 | char newname[MAXPATHLEN]; | |
953 | if (!p) { | |
954 | panic("can't find / in %s\n", diskfile); | |
955 | } | |
ddd2ef55 | 956 | snprintf(newname, sizeof(newname), "%s/debug/%s", tmpdir, p + 1); |
1227625a SP |
957 | if (rename(tapefile, newname)) { |
958 | panic("rename from %s to %s failed: %s\n", | |
959 | tapefile, newname, | |
960 | strerror(errno)); | |
961 | } else { | |
962 | fprintf(stderr, "*** %s saved to %s\n", | |
963 | tapefile, newname); | |
964 | } | |
965 | } | |
966 | ||
967 | /* don't unlink the file (it's not there anymore */ | |
968 | /* anyway) */ | |
969 | return (0); | |
970 | #else | |
971 | return; | |
972 | #endif | |
973 | } | |
974 | close(fd_tape); | |
975 | close(fd_disk); | |
976 | #ifdef COMPARE_FAIL_KEEP_FILE | |
977 | return (1); | |
978 | #endif | |
979 | } | |
980 | ||
ddd2ef55 | 981 | static char tmpfilename[MAXPATHLEN]; |
1227625a SP |
982 | |
983 | void | |
ddd2ef55 | 984 | comparefile(char *name) |
1227625a SP |
985 | { |
986 | static char *tmpfile = NULL; | |
987 | int mode; | |
988 | struct stat sb, stemp; | |
989 | int r; | |
990 | ||
991 | if ((r = lstat(name, &sb)) != 0) { | |
ddd2ef55 | 992 | warn("%s: does not exist (%d)", name, r); |
1227625a SP |
993 | skipfile(); |
994 | return; | |
995 | } | |
996 | ||
997 | curfile.name = name; | |
b45f51d6 | 998 | curfile.action = USING; |
1227625a SP |
999 | mode = curfile.dip->di_mode; |
1000 | ||
ddd2ef55 SP |
1001 | Vprintf(stdout, "comparing %s (size: %ld, mode: 0%o)\n", name, |
1002 | (long)sb.st_size, mode); | |
1227625a SP |
1003 | |
1004 | if (sb.st_mode != mode) { | |
1005 | fprintf(stderr, "%s: mode changed from 0%o to 0%o.\n", | |
1006 | name, mode & 07777, sb.st_mode & 07777); | |
1007 | } | |
1008 | switch (mode & IFMT) { | |
1009 | default: | |
1010 | skipfile(); | |
1011 | return; | |
1012 | ||
1013 | case IFSOCK: | |
1014 | skipfile(); | |
1015 | return; | |
1016 | ||
1017 | case IFDIR: | |
1018 | skipfile(); | |
1019 | return; | |
1020 | ||
1021 | case IFLNK: { | |
1022 | char lbuf[MAXPATHLEN + 1]; | |
1023 | int lsize; | |
1024 | ||
1025 | if (!(sb.st_mode & S_IFLNK)) { | |
1026 | fprintf(stderr, "%s: is no longer a symbolic link\n", | |
1027 | name); | |
1028 | return; | |
1029 | } | |
1030 | lnkbuf[0] = '\0'; | |
1031 | pathlen = 0; | |
1032 | getfile(xtrlnkfile, xtrlnkskip); | |
1033 | if (pathlen == 0) { | |
1034 | fprintf(stderr, | |
1035 | "%s: zero length symbolic link (ignored)\n", | |
1036 | name); | |
1037 | return; | |
1038 | } | |
1039 | if ((lsize = readlink(name, lbuf, MAXPATHLEN)) < 0) { | |
1040 | panic("readlink of %s failed: %s", name, | |
1041 | strerror(errno)); | |
1042 | } | |
1043 | lbuf[lsize] = 0; | |
1044 | if (strcmp(lbuf, lnkbuf) != 0) { | |
1045 | fprintf(stderr, | |
1046 | "%s: symbolic link changed from %s to %s.\n", | |
1047 | name, lnkbuf, lbuf); | |
1048 | return; | |
1049 | } | |
1050 | return; | |
1051 | } | |
1052 | ||
1053 | case IFCHR: | |
1054 | case IFBLK: | |
1055 | if (!(sb.st_mode & (S_IFCHR|S_IFBLK))) { | |
1056 | fprintf(stderr, "%s: no longer a special file\n", | |
1057 | name); | |
1058 | skipfile(); | |
1059 | return; | |
1060 | } | |
1061 | ||
1062 | if (sb.st_rdev != (int)curfile.dip->di_rdev) { | |
1063 | fprintf(stderr, | |
1064 | "%s: device changed from %d,%d to %d,%d.\n", | |
1065 | name, | |
ddd2ef55 SP |
1066 | ((int)curfile.dip->di_rdev >> 8) & 0xff, |
1067 | (int)curfile.dip->di_rdev & 0xff, | |
1068 | ((int)sb.st_rdev >> 8) & 0xff, | |
1069 | (int)sb.st_rdev & 0xff); | |
1227625a SP |
1070 | } |
1071 | skipfile(); | |
1072 | return; | |
1073 | ||
1074 | case IFREG: | |
1075 | if (tmpfile == NULL) { | |
1076 | /* argument to mktemp() must not be in RO space: */ | |
ddd2ef55 | 1077 | snprintf(tmpfilename, sizeof(tmpfilename), "%s/restoreCXXXXXX", tmpdir); |
1227625a SP |
1078 | tmpfile = mktemp(&tmpfilename[0]); |
1079 | } | |
1080 | if ((stat(tmpfile, &stemp) == 0) && (unlink(tmpfile) != 0)) { | |
1081 | panic("cannot delete tmp file %s: %s\n", | |
1082 | tmpfile, strerror(errno)); | |
1083 | } | |
1084 | if ((ofile = creat(tmpfile, 0600)) < 0) { | |
1085 | panic("cannot create file temp file %s: %s\n", | |
1086 | name, strerror(errno)); | |
1087 | } | |
1088 | getfile(xtrfile, xtrskip); | |
1089 | (void) close(ofile); | |
1090 | #ifdef COMPARE_FAIL_KEEP_FILE | |
1091 | if (cmpfiles(tmpfile, name, &sb)) | |
1092 | unlink(tmpfile); | |
1093 | #else | |
1094 | cmpfiles(tmpfile, name, &sb); | |
1095 | unlink(tmpfile); | |
1096 | #endif | |
1097 | return; | |
1098 | } | |
1099 | /* NOTREACHED */ | |
1100 | } | |
1101 | ||
1102 | /* | |
1103 | * Read TP_BSIZE blocks from the input. | |
1104 | * Handle read errors, and end of media. | |
1105 | */ | |
1106 | static void | |
ddd2ef55 | 1107 | readtape(char *buf) |
1227625a | 1108 | { |
ddd2ef55 | 1109 | ssize_t rd, newvol, i; |
1227625a SP |
1110 | int cnt, seek_failed; |
1111 | ||
1112 | if (blkcnt < numtrec) { | |
ddd2ef55 | 1113 | memmove(buf, &tapebuf[(blkcnt++ * TP_BSIZE)], TP_BSIZE); |
1227625a SP |
1114 | blksread++; |
1115 | tpblksread++; | |
1116 | return; | |
1117 | } | |
1118 | for (i = 0; i < ntrec; i++) | |
1119 | ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0; | |
1120 | if (numtrec == 0) | |
1121 | numtrec = ntrec; | |
1122 | cnt = ntrec * TP_BSIZE; | |
1123 | rd = 0; | |
1124 | getmore: | |
1125 | #ifdef RRESTORE | |
1126 | if (host) | |
1127 | i = rmtread(&tapebuf[rd], cnt); | |
1128 | else | |
1129 | #endif | |
1130 | i = read(mt, &tapebuf[rd], cnt); | |
1131 | /* | |
1132 | * Check for mid-tape short read error. | |
1133 | * If found, skip rest of buffer and start with the next. | |
1134 | */ | |
1135 | if (!pipein && numtrec < ntrec && i > 0) { | |
ddd2ef55 | 1136 | Dprintf(stdout, "mid-media short read error.\n"); |
1227625a SP |
1137 | numtrec = ntrec; |
1138 | } | |
1139 | /* | |
1140 | * Handle partial block read. | |
1141 | */ | |
1142 | if (pipein && i == 0 && rd > 0) | |
1143 | i = rd; | |
1144 | else if (i > 0 && i != ntrec * TP_BSIZE) { | |
1145 | if (pipein) { | |
1146 | rd += i; | |
1147 | cnt -= i; | |
1148 | if (cnt > 0) | |
1149 | goto getmore; | |
1150 | i = rd; | |
1151 | } else { | |
1152 | /* | |
1153 | * Short read. Process the blocks read. | |
1154 | */ | |
1155 | if (i % TP_BSIZE != 0) | |
ddd2ef55 | 1156 | Vprintf(stdout, |
b45f51d6 | 1157 | "partial block read: %ld should be %ld\n", |
ddd2ef55 | 1158 | (long)i, ntrec * TP_BSIZE); |
1227625a SP |
1159 | numtrec = i / TP_BSIZE; |
1160 | } | |
1161 | } | |
1162 | /* | |
1163 | * Handle read error. | |
1164 | */ | |
1165 | if (i < 0) { | |
1166 | fprintf(stderr, "Tape read error while "); | |
1167 | switch (curfile.action) { | |
1168 | default: | |
1169 | fprintf(stderr, "trying to set up tape\n"); | |
1170 | break; | |
1171 | case UNKNOWN: | |
1172 | fprintf(stderr, "trying to resynchronize\n"); | |
1173 | break; | |
1174 | case USING: | |
1175 | fprintf(stderr, "restoring %s\n", curfile.name); | |
1176 | break; | |
1177 | case SKIP: | |
ddd2ef55 SP |
1178 | fprintf(stderr, "skipping over inode %lu\n", |
1179 | (unsigned long)curfile.ino); | |
1227625a SP |
1180 | break; |
1181 | } | |
1182 | if (!yflag && !reply("continue")) | |
ddd2ef55 | 1183 | exit(1); |
1227625a | 1184 | i = ntrec * TP_BSIZE; |
ddd2ef55 | 1185 | memset(tapebuf, 0, (size_t)i); |
1227625a SP |
1186 | #ifdef RRESTORE |
1187 | if (host) | |
1188 | seek_failed = (rmtseek(i, 1) < 0); | |
1189 | else | |
1190 | #endif | |
1191 | seek_failed = (lseek(mt, i, SEEK_CUR) == (off_t)-1); | |
1192 | ||
ddd2ef55 SP |
1193 | if (seek_failed) |
1194 | err(1, "continuation failed"); | |
1227625a SP |
1195 | } |
1196 | /* | |
1197 | * Handle end of tape. | |
1198 | */ | |
1199 | if (i == 0) { | |
ddd2ef55 | 1200 | Vprintf(stdout, "End-of-tape encountered\n"); |
1227625a SP |
1201 | if (!pipein) { |
1202 | newvol = volno + 1; | |
1203 | volno = 0; | |
1204 | numtrec = 0; | |
1205 | getvol(newvol); | |
1206 | readtape(buf); | |
1207 | return; | |
1208 | } | |
1209 | if (rd % TP_BSIZE != 0) | |
1210 | panic("partial block read: %d should be %d\n", | |
1211 | rd, ntrec * TP_BSIZE); | |
1212 | terminateinput(); | |
ddd2ef55 | 1213 | memmove(&tapebuf[rd], &endoftapemark, TP_BSIZE); |
1227625a SP |
1214 | } |
1215 | blkcnt = 0; | |
ddd2ef55 | 1216 | memmove(buf, &tapebuf[(blkcnt++ * TP_BSIZE)], TP_BSIZE); |
1227625a SP |
1217 | blksread++; |
1218 | tpblksread++; | |
1219 | } | |
1220 | ||
1221 | static void | |
ddd2ef55 | 1222 | findtapeblksize(void) |
1227625a SP |
1223 | { |
1224 | register long i; | |
1225 | ||
1226 | for (i = 0; i < ntrec; i++) | |
1227 | ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0; | |
1228 | blkcnt = 0; | |
1229 | #ifdef RRESTORE | |
1230 | if (host) | |
ddd2ef55 | 1231 | i = rmtread(tapebuf, (size_t)(ntrec * TP_BSIZE)); |
1227625a SP |
1232 | else |
1233 | #endif | |
ddd2ef55 | 1234 | i = read(mt, tapebuf, (size_t)(ntrec * TP_BSIZE)); |
1227625a | 1235 | |
ddd2ef55 SP |
1236 | if (i <= 0) |
1237 | err(1, "tape read error"); | |
1238 | if (i % TP_BSIZE != 0) | |
1239 | errx(1, "Tape block size (%ld) is not a multiple of dump block size (%d)", | |
1240 | (long)i, TP_BSIZE); | |
1227625a SP |
1241 | ntrec = i / TP_BSIZE; |
1242 | numtrec = ntrec; | |
ddd2ef55 | 1243 | Vprintf(stdout, "Tape block size is %ld\n", ntrec); |
1227625a SP |
1244 | } |
1245 | ||
1246 | void | |
ddd2ef55 | 1247 | closemt(void) |
1227625a SP |
1248 | { |
1249 | ||
1250 | if (mt < 0) | |
1251 | return; | |
1252 | #ifdef RRESTORE | |
1253 | if (host) | |
1254 | rmtclose(); | |
1255 | else | |
1256 | #endif | |
1257 | (void) close(mt); | |
1258 | } | |
1259 | ||
1260 | /* | |
1261 | * Read the next block from the tape. | |
1262 | * Check to see if it is one of several vintage headers. | |
1263 | * If it is an old style header, convert it to a new style header. | |
1264 | * If it is not any valid header, return an error. | |
1265 | */ | |
1266 | static int | |
ddd2ef55 | 1267 | gethead(struct s_spcl *buf) |
1227625a | 1268 | { |
ddd2ef55 | 1269 | int32_t i; |
1227625a SP |
1270 | union { |
1271 | quad_t qval; | |
b45f51d6 | 1272 | int32_t val[2]; |
1227625a SP |
1273 | } qcvt; |
1274 | union u_ospcl { | |
1275 | char dummy[TP_BSIZE]; | |
1276 | struct s_ospcl { | |
b45f51d6 SP |
1277 | int32_t c_type; |
1278 | int32_t c_date; | |
1279 | int32_t c_ddate; | |
1280 | int32_t c_volume; | |
1281 | int32_t c_tapea; | |
ddd2ef55 | 1282 | u_int16_t c_inumber; |
b45f51d6 SP |
1283 | int32_t c_magic; |
1284 | int32_t c_checksum; | |
1227625a | 1285 | struct odinode { |
ddd2ef55 SP |
1286 | u_int16_t odi_mode; |
1287 | u_int16_t odi_nlink; | |
1288 | u_int16_t odi_uid; | |
1289 | u_int16_t odi_gid; | |
b45f51d6 SP |
1290 | int32_t odi_size; |
1291 | int32_t odi_rdev; | |
1227625a | 1292 | char odi_addr[36]; |
b45f51d6 SP |
1293 | int32_t odi_atime; |
1294 | int32_t odi_mtime; | |
1295 | int32_t odi_ctime; | |
1227625a | 1296 | } c_dinode; |
b45f51d6 | 1297 | int32_t c_count; |
1227625a SP |
1298 | char c_addr[256]; |
1299 | } s_ospcl; | |
1300 | } u_ospcl; | |
1301 | ||
1302 | if (!cvtflag) { | |
1303 | readtape((char *)buf); | |
1304 | if (buf->c_magic != NFS_MAGIC) { | |
ddd2ef55 | 1305 | if (swabi(buf->c_magic) != NFS_MAGIC) |
1227625a SP |
1306 | return (FAIL); |
1307 | if (!Bcvt) { | |
ddd2ef55 | 1308 | Vprintf(stdout, "Note: Doing Byte swapping\n"); |
1227625a SP |
1309 | Bcvt = 1; |
1310 | } | |
1311 | } | |
1312 | if (checksum((int *)buf) == FAIL) | |
1313 | return (FAIL); | |
ddd2ef55 SP |
1314 | if (Bcvt) |
1315 | swabst((u_char *)"8i4s31i528bi192b2i", (u_char *)buf); | |
1227625a SP |
1316 | goto good; |
1317 | } | |
1318 | readtape((char *)(&u_ospcl.s_ospcl)); | |
ddd2ef55 | 1319 | memset((char *)buf, 0, (long)TP_BSIZE); |
1227625a SP |
1320 | buf->c_type = u_ospcl.s_ospcl.c_type; |
1321 | buf->c_date = u_ospcl.s_ospcl.c_date; | |
1322 | buf->c_ddate = u_ospcl.s_ospcl.c_ddate; | |
1323 | buf->c_volume = u_ospcl.s_ospcl.c_volume; | |
1324 | buf->c_tapea = u_ospcl.s_ospcl.c_tapea; | |
1325 | buf->c_inumber = u_ospcl.s_ospcl.c_inumber; | |
1326 | buf->c_checksum = u_ospcl.s_ospcl.c_checksum; | |
1327 | buf->c_magic = u_ospcl.s_ospcl.c_magic; | |
1328 | buf->c_dinode.di_mode = u_ospcl.s_ospcl.c_dinode.odi_mode; | |
1329 | buf->c_dinode.di_nlink = u_ospcl.s_ospcl.c_dinode.odi_nlink; | |
1330 | buf->c_dinode.di_uid = u_ospcl.s_ospcl.c_dinode.odi_uid; | |
1331 | buf->c_dinode.di_gid = u_ospcl.s_ospcl.c_dinode.odi_gid; | |
1332 | buf->c_dinode.di_size = u_ospcl.s_ospcl.c_dinode.odi_size; | |
1333 | buf->c_dinode.di_rdev = u_ospcl.s_ospcl.c_dinode.odi_rdev; | |
1334 | #ifdef __linux__ | |
1335 | buf->c_dinode.di_atime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_atime; | |
1336 | buf->c_dinode.di_mtime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_mtime; | |
1337 | buf->c_dinode.di_ctime.tv_sec = u_ospcl.s_ospcl.c_dinode.odi_ctime; | |
1338 | #else /* __linux__ */ | |
1339 | buf->c_dinode.di_atime = u_ospcl.s_ospcl.c_dinode.odi_atime; | |
1340 | buf->c_dinode.di_mtime = u_ospcl.s_ospcl.c_dinode.odi_mtime; | |
1341 | buf->c_dinode.di_ctime = u_ospcl.s_ospcl.c_dinode.odi_ctime; | |
1342 | #endif /* __linux__ */ | |
1343 | buf->c_count = u_ospcl.s_ospcl.c_count; | |
1344 | memmove(buf->c_addr, u_ospcl.s_ospcl.c_addr, (long)256); | |
1345 | if (u_ospcl.s_ospcl.c_magic != OFS_MAGIC || | |
1346 | checksum((int *)(&u_ospcl.s_ospcl)) == FAIL) | |
1347 | return(FAIL); | |
1348 | buf->c_magic = NFS_MAGIC; | |
1349 | ||
1350 | good: | |
1351 | if ((buf->c_dinode.di_size == 0 || buf->c_dinode.di_size > 0xfffffff) && | |
1352 | (buf->c_dinode.di_mode & IFMT) == IFDIR && Qcvt == 0) { | |
1353 | qcvt.qval = buf->c_dinode.di_size; | |
1354 | if (qcvt.val[0] || qcvt.val[1]) { | |
1355 | printf("Note: Doing Quad swapping\n"); | |
1356 | Qcvt = 1; | |
1357 | } | |
1358 | } | |
1359 | if (Qcvt) { | |
1360 | qcvt.qval = buf->c_dinode.di_size; | |
1361 | i = qcvt.val[1]; | |
1362 | qcvt.val[1] = qcvt.val[0]; | |
1363 | qcvt.val[0] = i; | |
1364 | buf->c_dinode.di_size = qcvt.qval; | |
1365 | } | |
b45f51d6 | 1366 | readmapflag = 0; |
1227625a SP |
1367 | |
1368 | switch (buf->c_type) { | |
1369 | ||
1370 | case TS_CLRI: | |
1371 | case TS_BITS: | |
1372 | /* | |
1373 | * Have to patch up missing information in bit map headers | |
1374 | */ | |
1375 | buf->c_inumber = 0; | |
1376 | buf->c_dinode.di_size = buf->c_count * TP_BSIZE; | |
b45f51d6 SP |
1377 | if (buf->c_count > TP_NINDIR) |
1378 | readmapflag = 1; | |
1379 | else | |
1380 | for (i = 0; i < buf->c_count; i++) | |
1381 | buf->c_addr[i]++; | |
1227625a SP |
1382 | break; |
1383 | ||
1384 | case TS_TAPE: | |
1385 | if ((buf->c_flags & DR_NEWINODEFMT) == 0) | |
1386 | oldinofmt = 1; | |
1387 | /* fall through */ | |
1388 | case TS_END: | |
1389 | buf->c_inumber = 0; | |
1390 | break; | |
1391 | ||
1392 | case TS_INODE: | |
1393 | case TS_ADDR: | |
1394 | break; | |
1395 | ||
1396 | default: | |
1397 | panic("gethead: unknown inode type %d\n", buf->c_type); | |
1398 | break; | |
1399 | } | |
1400 | /* | |
b45f51d6 | 1401 | * If we are restoring a filesystem with old format inodes, |
1227625a SP |
1402 | * copy the uid/gid to the new location. |
1403 | */ | |
1404 | if (oldinofmt) { | |
1405 | buf->c_dinode.di_uid = buf->c_dinode.di_ouid; | |
1406 | buf->c_dinode.di_gid = buf->c_dinode.di_ogid; | |
1407 | } | |
1408 | if (dflag) | |
1409 | accthdr(buf); | |
1410 | return(GOOD); | |
1411 | } | |
1412 | ||
1413 | /* | |
1414 | * Check that a header is where it belongs and predict the next header | |
1415 | */ | |
1416 | static void | |
ddd2ef55 | 1417 | accthdr(struct s_spcl *header) |
1227625a SP |
1418 | { |
1419 | static ino_t previno = 0x7fffffff; | |
1420 | static int prevtype; | |
1421 | static long predict; | |
1422 | long blks, i; | |
1423 | ||
1424 | if (header->c_type == TS_TAPE) { | |
1425 | fprintf(stderr, "Volume header (%s inode format) ", | |
1426 | oldinofmt ? "old" : "new"); | |
1427 | if (header->c_firstrec) | |
1428 | fprintf(stderr, "begins with record %d", | |
1429 | header->c_firstrec); | |
1430 | fprintf(stderr, "\n"); | |
1431 | previno = 0x7fffffff; | |
1432 | return; | |
1433 | } | |
1434 | if (previno == 0x7fffffff) | |
1435 | goto newcalc; | |
1436 | switch (prevtype) { | |
1437 | case TS_BITS: | |
1438 | fprintf(stderr, "Dumped inodes map header"); | |
1439 | break; | |
1440 | case TS_CLRI: | |
1441 | fprintf(stderr, "Used inodes map header"); | |
1442 | break; | |
1443 | case TS_INODE: | |
ddd2ef55 | 1444 | fprintf(stderr, "File header, ino %lu", (unsigned long)previno); |
1227625a SP |
1445 | break; |
1446 | case TS_ADDR: | |
b9c89cf4 | 1447 | fprintf(stderr, "File continuation header, ino %ld", (long)previno); |
1227625a SP |
1448 | break; |
1449 | case TS_END: | |
1450 | fprintf(stderr, "End of tape header"); | |
1451 | break; | |
1452 | } | |
1453 | if (predict != blksread - 1) | |
b45f51d6 | 1454 | fprintf(stderr, "; predicted %ld blocks, got %ld blocks", |
1227625a SP |
1455 | predict, blksread - 1); |
1456 | fprintf(stderr, "\n"); | |
1457 | newcalc: | |
1458 | blks = 0; | |
1459 | if (header->c_type != TS_END) | |
1460 | for (i = 0; i < header->c_count; i++) | |
b45f51d6 | 1461 | if (readmapflag || header->c_addr[i] != 0) |
1227625a SP |
1462 | blks++; |
1463 | predict = blks; | |
1464 | blksread = 0; | |
1465 | prevtype = header->c_type; | |
1466 | previno = header->c_inumber; | |
1467 | } | |
1468 | ||
1469 | /* | |
1470 | * Find an inode header. | |
1471 | * Complain if had to skip, and complain is set. | |
1472 | */ | |
1473 | static void | |
ddd2ef55 | 1474 | findinode(struct s_spcl *header) |
1227625a SP |
1475 | { |
1476 | static long skipcnt = 0; | |
1477 | long i; | |
1478 | char buf[TP_BSIZE]; | |
1479 | ||
1480 | curfile.name = "<name unknown>"; | |
1481 | curfile.action = UNKNOWN; | |
1482 | curfile.dip = NULL; | |
1483 | curfile.ino = 0; | |
1484 | do { | |
1485 | if (header->c_magic != NFS_MAGIC) { | |
1486 | skipcnt++; | |
1487 | while (gethead(header) == FAIL || | |
1488 | header->c_date != dumpdate) | |
1489 | skipcnt++; | |
1490 | } | |
1491 | switch (header->c_type) { | |
1492 | ||
1493 | case TS_ADDR: | |
1494 | /* | |
1495 | * Skip up to the beginning of the next record | |
1496 | */ | |
1497 | for (i = 0; i < header->c_count; i++) | |
1498 | if (header->c_addr[i]) | |
1499 | readtape(buf); | |
1500 | while (gethead(header) == FAIL || | |
1501 | header->c_date != dumpdate) | |
1502 | skipcnt++; | |
1503 | break; | |
1504 | ||
1505 | case TS_INODE: | |
1506 | curfile.dip = &header->c_dinode; | |
1507 | curfile.ino = header->c_inumber; | |
1508 | break; | |
1509 | ||
1510 | case TS_END: | |
1511 | curfile.ino = maxino; | |
1512 | break; | |
1513 | ||
1514 | case TS_CLRI: | |
1515 | curfile.name = "<file removal list>"; | |
1516 | break; | |
1517 | ||
1518 | case TS_BITS: | |
1519 | curfile.name = "<file dump list>"; | |
1520 | break; | |
1521 | ||
1522 | case TS_TAPE: | |
1523 | panic("unexpected tape header\n"); | |
1524 | /* NOTREACHED */ | |
1525 | ||
1526 | default: | |
1527 | panic("unknown tape header type %d\n", spcl.c_type); | |
1528 | /* NOTREACHED */ | |
1529 | ||
1530 | } | |
1531 | } while (header->c_type == TS_ADDR); | |
1532 | if (skipcnt > 0) | |
b45f51d6 SP |
1533 | fprintf(stderr, "resync restore, skipped %ld blocks\n", |
1534 | skipcnt); | |
1227625a SP |
1535 | skipcnt = 0; |
1536 | } | |
1537 | ||
1538 | static int | |
ddd2ef55 | 1539 | checksum(int *buf) |
1227625a SP |
1540 | { |
1541 | register int i, j; | |
1542 | ||
1543 | j = sizeof(union u_spcl) / sizeof(int); | |
1544 | i = 0; | |
1545 | if(!Bcvt) { | |
1546 | do | |
1547 | i += *buf++; | |
1548 | while (--j); | |
1549 | } else { | |
1550 | /* What happens if we want to read restore tapes | |
1551 | for a 16bit int machine??? */ | |
b45f51d6 | 1552 | do |
ddd2ef55 | 1553 | i += swabi(*buf++); |
1227625a SP |
1554 | while (--j); |
1555 | } | |
b45f51d6 | 1556 | |
1227625a | 1557 | if (i != CHECKSUM) { |
ddd2ef55 SP |
1558 | fprintf(stderr, "Checksum error %o, inode %lu file %s\n", i, |
1559 | (unsigned long)curfile.ino, curfile.name); | |
1227625a SP |
1560 | return(FAIL); |
1561 | } | |
1562 | return(GOOD); | |
1563 | } | |
1564 | ||
1565 | #ifdef RRESTORE | |
ddd2ef55 | 1566 | #ifdef __STDC__ |
1227625a SP |
1567 | #include <stdarg.h> |
1568 | #else | |
1569 | #include <varargs.h> | |
1570 | #endif | |
1571 | ||
1572 | void | |
ddd2ef55 | 1573 | #ifdef __STDC__ |
1227625a SP |
1574 | msg(const char *fmt, ...) |
1575 | #else | |
1576 | msg(fmt, va_alist) | |
1577 | char *fmt; | |
1578 | va_dcl | |
1579 | #endif | |
1580 | { | |
1581 | va_list ap; | |
ddd2ef55 | 1582 | #ifdef __STDC__ |
1227625a SP |
1583 | va_start(ap, fmt); |
1584 | #else | |
1585 | va_start(ap); | |
1586 | #endif | |
1587 | (void)vfprintf(stderr, fmt, ap); | |
1588 | va_end(ap); | |
1589 | } | |
1590 | #endif /* RRESTORE */ | |
1591 | ||
1592 | static u_char * | |
ddd2ef55 | 1593 | swab16(u_char *sp, int n) |
1227625a SP |
1594 | { |
1595 | char c; | |
1596 | ||
1597 | while (--n >= 0) { | |
1598 | c = sp[0]; sp[0] = sp[1]; sp[1] = c; | |
1599 | sp += 2; | |
1600 | } | |
1601 | return (sp); | |
1602 | } | |
1603 | ||
1604 | static u_char * | |
ddd2ef55 | 1605 | swab32(u_char *sp, int n) |
1227625a SP |
1606 | { |
1607 | char c; | |
1608 | ||
1609 | while (--n >= 0) { | |
1610 | c = sp[0]; sp[0] = sp[3]; sp[3] = c; | |
ddd2ef55 | 1611 | c = sp[1]; sp[1] = sp[2]; sp[2] = c; |
1227625a SP |
1612 | sp += 4; |
1613 | } | |
1614 | return (sp); | |
1615 | } | |
1616 | ||
ddd2ef55 SP |
1617 | static u_char * |
1618 | swab64(u_char *sp, int n) | |
1619 | { | |
1620 | char c; | |
1621 | ||
1622 | while (--n >= 0) { | |
1623 | c = sp[0]; sp[0] = sp[7]; sp[7] = c; | |
1624 | c = sp[1]; sp[1] = sp[6]; sp[6] = c; | |
1625 | c = sp[2]; sp[2] = sp[5]; sp[5] = c; | |
1626 | c = sp[3]; sp[3] = sp[4]; sp[4] = c; | |
1627 | sp += 8; | |
1628 | } | |
1629 | return (sp); | |
1630 | } | |
1631 | ||
1227625a | 1632 | void |
ddd2ef55 | 1633 | swabst(u_char *cp, u_char *sp) |
1227625a SP |
1634 | { |
1635 | int n = 0; | |
1636 | ||
1637 | while (*cp) { | |
1638 | switch (*cp) { | |
1639 | case '0': case '1': case '2': case '3': case '4': | |
1640 | case '5': case '6': case '7': case '8': case '9': | |
1641 | n = (n * 10) + (*cp++ - '0'); | |
1642 | continue; | |
b45f51d6 | 1643 | |
1227625a SP |
1644 | case 's': case 'w': case 'h': |
1645 | if (n == 0) | |
1646 | n = 1; | |
ddd2ef55 SP |
1647 | sp = swab16(sp, n); |
1648 | break; | |
1649 | ||
1650 | case 'i': | |
1651 | if (n == 0) | |
1652 | n = 1; | |
1653 | sp = swab32(sp, n); | |
1227625a SP |
1654 | break; |
1655 | ||
1656 | case 'l': | |
1657 | if (n == 0) | |
1658 | n = 1; | |
ddd2ef55 | 1659 | sp = swab64(sp, n); |
1227625a SP |
1660 | break; |
1661 | ||
1662 | default: /* Any other character, like 'b' counts as byte. */ | |
1663 | if (n == 0) | |
1664 | n = 1; | |
1665 | sp += n; | |
1666 | break; | |
1667 | } | |
1668 | cp++; | |
1669 | n = 0; | |
1670 | } | |
1671 | } | |
1672 | ||
ddd2ef55 SP |
1673 | static u_int |
1674 | swabi(u_int x) | |
1675 | { | |
1676 | swabst((u_char *)"i", (u_char *)&x); | |
1677 | return (x); | |
1678 | } | |
1679 | ||
1227625a | 1680 | static u_long |
ddd2ef55 | 1681 | swabl(u_long x) |
1227625a SP |
1682 | { |
1683 | swabst((u_char *)"l", (u_char *)&x); | |
1684 | return (x); | |
1685 | } |