]> git.wh0rd.org - dump.git/blob - restore/dirs.c
Largefile version of seek in rmt.
[dump.git] / restore / dirs.c
1 /*
2 * Ported to Linux's Second Extended File System as part of the
3 * dump and restore backup suit
4 * Remy Card <card@Linux.EU.Org>, 1994-1997
5 * Stelian Pop <stelian@popies.net>, 1999-2000
6 * Stelian Pop <stelian@popies.net> - AlcĂ´ve <www.alcove.com>, 2000-2002
7 */
8
9 /*
10 * Copyright (c) 1983, 1993
11 * The Regents of the University of California. All rights reserved.
12 * (c) UNIX System Laboratories, Inc.
13 * All or some portions of this file are derived from material licensed
14 * to the University of California by American Telephone and Telegraph
15 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
16 * the permission of UNIX System Laboratories, Inc.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 3. All advertising materials mentioning features or use of this software
27 * must display the following acknowledgement:
28 * This product includes software developed by the University of
29 * California, Berkeley and its contributors.
30 * 4. Neither the name of the University nor the names of its contributors
31 * may be used to endorse or promote products derived from this software
32 * without specific prior written permission.
33 *
34 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
35 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 * SUCH DAMAGE.
45 */
46
47 #ifndef lint
48 static const char rcsid[] =
49 "$Id: dirs.c,v 1.22 2003/01/10 14:42:51 stelian Exp $";
50 #endif /* not lint */
51
52 #include <config.h>
53 #include <sys/param.h>
54 #include <sys/file.h>
55 #include <sys/stat.h>
56
57 #ifdef __linux__
58 #ifdef HAVE_EXT2FS_EXT2_FS_H
59 #include <ext2fs/ext2_fs.h>
60 #else
61 #include <linux/ext2_fs.h>
62 #endif
63 #include <bsdcompat.h>
64 #else /* __linux__ */
65 #include <ufs/ufs/dinode.h>
66 #include <ufs/ufs/dir.h>
67 #endif /* __linux__ */
68 #include <protocols/dumprestore.h>
69
70 #include <compaterr.h>
71 #include <errno.h>
72 #include <stdio.h>
73 #include <stdlib.h>
74 #include <string.h>
75 #include <unistd.h>
76
77 #ifdef __linux__
78 #include <endian.h>
79 #else
80 #include <machine/endian.h>
81 #endif
82
83 #include "pathnames.h"
84 #include "restore.h"
85 #include "extern.h"
86
87 /*
88 * Symbol table of directories read from tape.
89 */
90 #define HASHSIZE 1000
91 #define INOHASH(val) (val % HASHSIZE)
92 struct inotab {
93 struct inotab *t_next;
94 dump_ino_t t_ino;
95 int32_t t_seekpt;
96 int32_t t_size;
97 };
98 static struct inotab *inotab[HASHSIZE];
99
100 /*
101 * Information retained about directories.
102 */
103 struct modeinfo {
104 dump_ino_t ino;
105 struct timeval timep[2];
106 mode_t mode;
107 uid_t uid;
108 gid_t gid;
109 unsigned int flags;
110 };
111
112 /*
113 * Definitions for library routines operating on directories.
114 */
115 #undef DIRBLKSIZ
116 #define DIRBLKSIZ 1024
117 struct rstdirdesc {
118 int dd_fd;
119 int32_t dd_loc;
120 int32_t dd_size;
121 char dd_buf[DIRBLKSIZ];
122 };
123
124 /*
125 * Global variables for this file.
126 */
127 static long seekpt;
128 static FILE *df, *mf;
129 static RST_DIR *dirp;
130 static char dirfile[MAXPATHLEN] = "#"; /* No file */
131 static char modefile[MAXPATHLEN] = "#"; /* No file */
132 static char dot[2] = "."; /* So it can be modified */
133
134 /*
135 * Format of old style directories.
136 */
137 #define ODIRSIZ 14
138 struct odirect {
139 u_short d_ino;
140 char d_name[ODIRSIZ];
141 };
142
143 #if defined(__linux__) || defined(sunos)
144 static struct inotab *allocinotab __P((dump_ino_t, struct new_bsd_inode *, long));
145 #else
146 static struct inotab *allocinotab __P((dump_ino_t, struct dinode *, long));
147 #endif
148 static void dcvt __P((struct odirect *, struct direct *));
149 static void flushent __P((void));
150 static struct inotab *inotablookup __P((dump_ino_t));
151 static RST_DIR *opendirfile __P((const char *));
152 static void putdir __P((char *, size_t));
153 static void putent __P((struct direct *));
154 static void rst_seekdir __P((RST_DIR *, long, long));
155 static long rst_telldir __P((RST_DIR *));
156 static struct direct *searchdir __P((dump_ino_t, char *));
157
158 /*
159 * Extract directory contents, building up a directory structure
160 * on disk for extraction by name.
161 * If genmode is requested, save mode, owner, and times for all
162 * directories on the tape.
163 */
164 void
165 extractdirs(int genmode)
166 {
167 int i;
168 #if defined(__linux__) || defined(sunos)
169 struct new_bsd_inode *ip;
170 #else
171 struct dinode *ip;
172 #endif
173 struct inotab *itp;
174 struct direct nulldir;
175 int fd;
176
177 Vprintf(stdout, "Extract directories from tape\n");
178 (void) snprintf(dirfile, sizeof(dirfile), "%s/rstdir%ld", tmpdir,
179 (long)dumpdate);
180 if (command != 'r' && command != 'R') {
181 (void) strncat(dirfile, "-XXXXXX",
182 sizeof(dirfile) - strlen(dirfile));
183 fd = mkstemp(dirfile);
184 } else
185 fd = open(dirfile, O_RDWR|O_CREAT|O_EXCL, 0666);
186 if (fd == -1 || (df = fdopen(fd, "w")) == NULL) {
187 if (fd != -1)
188 close(fd);
189 err(1, "cannot create directory temporary %s", dirfile);
190 }
191 if (genmode != 0) {
192 (void) snprintf(modefile, sizeof(modefile), "%s/rstmode%ld", tmpdir, (long)dumpdate);
193 if (command != 'r' && command != 'R') {
194 (void) strncat(modefile, "-XXXXXX",
195 sizeof(modefile) - strlen(modefile));
196 fd = mkstemp(modefile);
197 } else
198 fd = open(modefile, O_RDWR|O_CREAT|O_EXCL, 0666);
199 if (fd == -1 || (mf = fdopen(fd, "w")) == NULL) {
200 if (fd != -1)
201 close(fd);
202 err(1, "cannot create modefile %s", modefile);
203 }
204 }
205 nulldir.d_ino = 0;
206 nulldir.d_type = DT_DIR;
207 nulldir.d_namlen = 1;
208 nulldir.d_name[0] = '/';
209 nulldir.d_name[1] = '\0';
210 nulldir.d_reclen = DIRSIZ(0, &nulldir);
211 for (;;) {
212 curfile.name = "<directory file - name unknown>";
213 curfile.action = USING;
214 ip = curfile.dip;
215 if (ip == NULL || (ip->di_mode & IFMT) != IFDIR) {
216 if ( fclose(df) == EOF )
217 err(1, "cannot write to file %s", dirfile);
218 dirp = opendirfile(dirfile);
219 if (dirp == NULL)
220 warn("opendirfile");
221 if (mf != NULL && fclose(mf) == EOF )
222 err(1, "cannot write to file %s", dirfile);
223 i = dirlookup(dot);
224 if (i == 0)
225 panic("Root directory is not on tape\n");
226 return;
227 }
228 itp = allocinotab(curfile.ino, ip, seekpt);
229 getfile(putdir, xtrnull);
230 putent(&nulldir);
231 flushent();
232 itp->t_size = seekpt - itp->t_seekpt;
233 }
234 }
235
236 /*
237 * skip over all the directories on the tape
238 */
239 void
240 skipdirs(void)
241 {
242
243 while (curfile.dip && (curfile.dip->di_mode & IFMT) == IFDIR) {
244 skipfile();
245 }
246 }
247
248 /*
249 * Recursively find names and inumbers of all files in subtree
250 * pname and pass them off to be processed.
251 */
252 void
253 treescan(char *pname, dump_ino_t ino, long (*todo) __P((char *, dump_ino_t, int)))
254 {
255 struct inotab *itp;
256 struct direct *dp;
257 int namelen;
258 long bpt;
259 char locname[MAXPATHLEN + 1];
260
261 itp = inotablookup(ino);
262 if (itp == NULL) {
263 /*
264 * Pname is name of a simple file or an unchanged directory.
265 */
266 (void) (*todo)(pname, ino, LEAF);
267 return;
268 }
269 /*
270 * Pname is a dumped directory name.
271 */
272 if ((*todo)(pname, ino, NODE) == FAIL)
273 return;
274 /*
275 * begin search through the directory
276 * skipping over "." and ".."
277 */
278 namelen = snprintf(locname, sizeof(locname), "%s/", pname);
279 if (namelen >= (int)sizeof(locname))
280 namelen = sizeof(locname) - 1;
281 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
282 dp = rst_readdir(dirp); /* "." */
283 if (dp != NULL && strcmp(dp->d_name, ".") == 0)
284 dp = rst_readdir(dirp); /* ".." */
285 else
286 fprintf(stderr, "Warning: `.' missing from directory %s\n",
287 pname);
288 if (dp != NULL && strcmp(dp->d_name, "..") == 0)
289 dp = rst_readdir(dirp); /* first real entry */
290 else
291 fprintf(stderr, "Warning: `..' missing from directory %s\n",
292 pname);
293 bpt = rst_telldir(dirp);
294 /*
295 * a zero inode signals end of directory
296 */
297 while (dp != NULL) {
298 locname[namelen] = '\0';
299 if (namelen + dp->d_namlen >= (int)sizeof(locname)) {
300 fprintf(stderr, "%s%s: name exceeds %ld char\n",
301 locname, dp->d_name, (long)sizeof(locname) - 1);
302 } else {
303 (void) strncat(locname, dp->d_name, (int)dp->d_namlen);
304 treescan(locname, dp->d_ino, todo);
305 rst_seekdir(dirp, bpt, itp->t_seekpt);
306 }
307 dp = rst_readdir(dirp);
308 bpt = rst_telldir(dirp);
309 }
310 }
311
312 /*
313 * Lookup a pathname which is always assumed to start from the ROOTINO.
314 */
315 struct direct *
316 pathsearch(const char *pathname)
317 {
318 dump_ino_t ino;
319 struct direct *dp;
320 char *path, *name, buffer[MAXPATHLEN];
321
322 strcpy(buffer, pathname);
323 path = buffer;
324 ino = ROOTINO;
325 while (*path == '/')
326 path++;
327 dp = NULL;
328 #ifdef __linux__
329 while ((name = strsep(&path, "/")) != NULL && *name /* != NULL */) {
330 #else
331 while ((name = strtok_r(NULL, "/", &path)) != NULL && *name /* != NULL */) {
332 #endif
333 if ((dp = searchdir(ino, name)) == NULL)
334 return (NULL);
335 ino = dp->d_ino;
336 }
337 return (dp);
338 }
339
340 /*
341 * Lookup the requested name in directory inum.
342 * Return its inode number if found, zero if it does not exist.
343 */
344 static struct direct *
345 searchdir(dump_ino_t inum, char *name)
346 {
347 struct direct *dp;
348 struct inotab *itp;
349 int len;
350
351 itp = inotablookup(inum);
352 if (itp == NULL)
353 return (NULL);
354 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
355 len = strlen(name);
356 do {
357 dp = rst_readdir(dirp);
358 if (dp == NULL)
359 return (NULL);
360 } while (dp->d_namlen != len || strncmp(dp->d_name, name, len) != 0);
361 return (dp);
362 }
363
364 /*
365 * Put the directory entries in the directory file
366 */
367 static void
368 putdir(char *buf, size_t size)
369 {
370 struct direct cvtbuf;
371 struct odirect *odp;
372 struct odirect *eodp;
373 struct direct *dp;
374 long loc, i;
375
376 if (cvtflag) {
377 eodp = (struct odirect *)&buf[size];
378 for (odp = (struct odirect *)buf; odp < eodp; odp++)
379 if (odp->d_ino != 0) {
380 dcvt(odp, &cvtbuf);
381 putent(&cvtbuf);
382 }
383 } else {
384 for (loc = 0; loc < (long)size; ) {
385 dp = (struct direct *)(buf + loc);
386 #ifdef DIRDEBUG
387 printf ("reclen = %d, namlen = %d, type = %d\n",
388 dp->d_reclen, dp->d_namlen, dp->d_type);
389 #endif
390 if (Bcvt)
391 swabst((u_char *)"is", (u_char *) dp);
392 if (oldinofmt && dp->d_ino != 0) {
393 # if BYTE_ORDER == BIG_ENDIAN
394 if (Bcvt)
395 dp->d_namlen = dp->d_type;
396 # else
397 if (!Bcvt)
398 dp->d_namlen = dp->d_type;
399 # endif
400 if (dp->d_namlen == 0 && dp->d_type != 0)
401 dp->d_namlen = dp->d_type;
402 dp->d_type = DT_UNKNOWN;
403 }
404 #ifdef DIRDEBUG
405 printf ("reclen = %d, namlen = %d, type = %d\n",
406 dp->d_reclen, dp->d_namlen, dp->d_type);
407 #endif
408 i = DIRBLKSIZ - (loc & (DIRBLKSIZ - 1));
409 if ((dp->d_reclen & 0x3) != 0 ||
410 dp->d_reclen > i ||
411 dp->d_reclen < DIRSIZ(0, dp) ||
412 dp->d_namlen > MAXNAMLEN) {
413 Vprintf(stdout, "Mangled directory: ");
414 if ((dp->d_reclen & 0x3) != 0)
415 Vprintf(stdout,
416 "reclen not multiple of 4 ");
417 if (dp->d_reclen < DIRSIZ(0, dp))
418 Vprintf(stdout,
419 "reclen less than DIRSIZ (%d < %d) ",
420 dp->d_reclen, DIRSIZ(0, dp));
421 if (dp->d_namlen > MAXNAMLEN)
422 Vprintf(stdout,
423 "reclen name too big (%d > %d) ",
424 dp->d_namlen, MAXNAMLEN);
425 Vprintf(stdout, "\n");
426 loc += i;
427 continue;
428 }
429 loc += dp->d_reclen;
430 if (dp->d_ino != 0) {
431 putent(dp);
432 }
433 }
434 }
435 }
436
437 /*
438 * These variables are "local" to the following two functions.
439 */
440 static char dirbuf[DIRBLKSIZ];
441 static long dirloc = 0;
442 static long prev = 0;
443
444 /*
445 * add a new directory entry to a file.
446 */
447 static void
448 putent(struct direct *dp)
449 {
450 dp->d_reclen = DIRSIZ(0, dp);
451 if (dirloc + dp->d_reclen > DIRBLKSIZ) {
452 ((struct direct *)(dirbuf + prev))->d_reclen =
453 DIRBLKSIZ - prev;
454 if ( fwrite(dirbuf, 1, DIRBLKSIZ, df) != DIRBLKSIZ )
455 err(1,"cannot write to file %s", dirfile);
456 dirloc = 0;
457 }
458 memmove(dirbuf + dirloc, dp, (size_t)dp->d_reclen);
459 prev = dirloc;
460 dirloc += dp->d_reclen;
461 }
462
463 /*
464 * flush out a directory that is finished.
465 */
466 static void
467 flushent(void)
468 {
469 ((struct direct *)(dirbuf + prev))->d_reclen = DIRBLKSIZ - prev;
470 if ( fwrite(dirbuf, (int)dirloc, 1, df) != 1 )
471 err(1, "cannot write to file %s", dirfile);
472 seekpt = ftell(df);
473 if (seekpt == -1)
474 err(1, "cannot write to file %s", dirfile);
475 dirloc = 0;
476 }
477
478 static void
479 dcvt(struct odirect *odp, struct direct *ndp)
480 {
481
482 memset(ndp, 0, (size_t)(sizeof *ndp));
483 ndp->d_ino = odp->d_ino;
484 ndp->d_type = DT_UNKNOWN;
485 (void) strncpy(ndp->d_name, odp->d_name, ODIRSIZ);
486 ndp->d_namlen = strlen(ndp->d_name);
487 ndp->d_reclen = DIRSIZ(0, ndp);
488 }
489
490 /*
491 * Seek to an entry in a directory.
492 * Only values returned by rst_telldir should be passed to rst_seekdir.
493 * This routine handles many directories in a single file.
494 * It takes the base of the directory in the file, plus
495 * the desired seek offset into it.
496 */
497 static void
498 rst_seekdir(RST_DIR *dirp, long loc, long base)
499 {
500
501 if (loc == rst_telldir(dirp))
502 return;
503 loc -= base;
504 if (loc < 0)
505 fprintf(stderr, "bad seek pointer to rst_seekdir %ld\n", loc);
506 (void) lseek(dirp->dd_fd, base + (loc & ~(DIRBLKSIZ - 1)), SEEK_SET);
507 dirp->dd_loc = loc & (DIRBLKSIZ - 1);
508 if (dirp->dd_loc != 0)
509 dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ);
510 }
511
512 /*
513 * get next entry in a directory.
514 */
515 struct direct *
516 rst_readdir(RST_DIR *dirp)
517 {
518 struct direct *dp;
519
520 for (;;) {
521 if (dirp->dd_loc == 0) {
522 dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf,
523 DIRBLKSIZ);
524 if (dirp->dd_size <= 0) {
525 Dprintf(stderr, "error reading directory\n");
526 return (NULL);
527 }
528 }
529 if (dirp->dd_loc >= dirp->dd_size) {
530 dirp->dd_loc = 0;
531 continue;
532 }
533 dp = (struct direct *)(dirp->dd_buf + dirp->dd_loc);
534 if (dp->d_reclen == 0 ||
535 dp->d_reclen > DIRBLKSIZ + 1 - dirp->dd_loc) {
536 Dprintf(stderr, "corrupted directory: bad reclen %d\n",
537 dp->d_reclen);
538 return (NULL);
539 }
540 dirp->dd_loc += dp->d_reclen;
541 if (dp->d_ino == 0 && strcmp(dp->d_name, "/") == 0)
542 return (NULL);
543 if (dp->d_ino >= maxino) {
544 Dprintf(stderr, "corrupted directory: bad inum %d\n",
545 dp->d_ino);
546 continue;
547 }
548 return (dp);
549 }
550 }
551
552 /*
553 * Simulate the opening of a directory
554 */
555 RST_DIR *
556 rst_opendir(const char *name)
557 {
558 struct inotab *itp;
559 RST_DIR *dirp;
560 dump_ino_t ino;
561
562 if ((ino = dirlookup(name)) > 0 &&
563 (itp = inotablookup(ino)) != NULL) {
564 dirp = opendirfile(dirfile);
565 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
566 return (dirp);
567 }
568 return (NULL);
569 }
570
571 /*
572 * In our case, there is nothing to do when closing a directory.
573 */
574 void
575 rst_closedir(RST_DIR *dirp)
576 {
577
578 (void)close(dirp->dd_fd);
579 free(dirp);
580 return;
581 }
582
583 /*
584 * Simulate finding the current offset in the directory.
585 */
586 static long
587 rst_telldir(RST_DIR *dirp)
588 {
589 return ((long)lseek(dirp->dd_fd,
590 (OFF_T)0, SEEK_CUR) - dirp->dd_size + dirp->dd_loc);
591 }
592
593 /*
594 * Open a directory file.
595 */
596 static RST_DIR *
597 opendirfile(const char *name)
598 {
599 RST_DIR *dirp;
600 int fd;
601
602 if ((fd = open(name, O_RDONLY)) == -1)
603 return (NULL);
604 if ((dirp = malloc(sizeof(RST_DIR))) == NULL) {
605 (void)close(fd);
606 return (NULL);
607 }
608 dirp->dd_fd = fd;
609 dirp->dd_loc = 0;
610 return (dirp);
611 }
612
613 /*
614 * Set the mode, owner, and times for all new or changed directories
615 */
616 void
617 setdirmodes(int flags)
618 {
619 FILE *mf;
620 struct modeinfo node;
621 struct entry *ep;
622 char *cp;
623
624 Vprintf(stdout, "Set directory mode, owner, and times.\n");
625 if (command == 'r' || command == 'R')
626 (void) snprintf(modefile, sizeof(modefile), "%s/rstmode%lu", tmpdir, (long)dumpdate);
627 if (modefile[0] == '#') {
628 panic("modefile not defined\n");
629 fprintf(stderr, "directory mode, owner, and times not set\n");
630 return;
631 }
632 mf = fopen(modefile, "r");
633 if (mf == NULL) {
634 warn("fopen");
635 fprintf(stderr, "cannot open mode file %s\n", modefile);
636 fprintf(stderr, "directory mode, owner, and times not set\n");
637 return;
638 }
639 clearerr(mf);
640 for (;;) {
641 (void) fread((char *)&node, 1, sizeof(struct modeinfo), mf);
642 if (feof(mf))
643 break;
644 ep = lookupino(node.ino);
645 if (command == 'i' || command == 'x') {
646 if (ep == NULL)
647 continue;
648 if ((flags & FORCE) == 0 && ep->e_flags & EXISTED) {
649 ep->e_flags &= ~NEW;
650 continue;
651 }
652 if ((flags & FORCE) == 0 &&
653 node.ino == ROOTINO &&
654 reply("set owner/mode for '.'") == FAIL)
655 continue;
656 }
657 if (ep == NULL) {
658 panic("cannot find directory inode %d\n", node.ino);
659 } else {
660 cp = myname(ep);
661 (void) chown(cp, node.uid, node.gid);
662 (void) chmod(cp, node.mode);
663 if (node.flags)
664 #ifdef __linux__
665 (void) fsetflags(cp, node.flags);
666 #else
667 (void) chflags(cp, node.flags);
668 #endif
669 utimes(cp, node.timep);
670 ep->e_flags &= ~NEW;
671 }
672 }
673 if (ferror(mf))
674 panic("error setting directory modes\n");
675 (void) fclose(mf);
676 }
677
678 /*
679 * Generate a literal copy of a directory.
680 */
681 int
682 genliteraldir(char *name, dump_ino_t ino)
683 {
684 struct inotab *itp;
685 int ofile, dp, i, size;
686 char buf[BUFSIZ];
687
688 itp = inotablookup(ino);
689 if (itp == NULL)
690 panic("Cannot find directory inode %d named %s\n", ino, name);
691 if ((ofile = open(name, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
692 warn("%s: cannot create file\n", name);
693 return (FAIL);
694 }
695 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
696 dp = dup(dirp->dd_fd);
697 for (i = itp->t_size; i > 0; i -= BUFSIZ) {
698 size = i < BUFSIZ ? i : BUFSIZ;
699 if (read(dp, buf, (int) size) == -1) {
700 warnx("write error extracting inode %lu, name %s\n",
701 (unsigned long)curfile.ino, curfile.name);
702 err(1, "read");
703 }
704 if (!Nflag && write(ofile, buf, (int) size) == -1) {
705 warnx("write error extracting inode %lu, name %s\n",
706 (unsigned long)curfile.ino, curfile.name);
707 err(1, "write");
708 }
709 }
710 (void) close(dp);
711 (void) close(ofile);
712 return (GOOD);
713 }
714
715 /*
716 * Determine the type of an inode
717 */
718 int
719 inodetype(dump_ino_t ino)
720 {
721 struct inotab *itp;
722
723 itp = inotablookup(ino);
724 if (itp == NULL)
725 return (LEAF);
726 return (NODE);
727 }
728
729 /*
730 * Allocate and initialize a directory inode entry.
731 * If requested, save its pertinent mode, owner, and time info.
732 */
733 static struct inotab *
734 #ifdef __linux__
735 allocinotab(dump_ino_t ino, struct new_bsd_inode *dip, long seekpt)
736 #else
737 allocinotab(dump_ino_t ino, struct dinode *dip, long seekpt)
738 #endif
739 {
740 struct inotab *itp;
741 struct modeinfo node;
742
743 itp = calloc(1, sizeof(struct inotab));
744 if (itp == NULL)
745 panic("no memory directory table\n");
746 itp->t_next = inotab[INOHASH(ino)];
747 inotab[INOHASH(ino)] = itp;
748 itp->t_ino = ino;
749 itp->t_seekpt = seekpt;
750 if (mf == NULL)
751 return (itp);
752 node.ino = ino;
753 #ifdef __linux__
754 node.timep[0].tv_sec = dip->di_atime.tv_sec;
755 node.timep[0].tv_usec = dip->di_atime.tv_usec;
756 node.timep[1].tv_sec = dip->di_mtime.tv_sec;
757 node.timep[1].tv_usec = dip->di_mtime.tv_usec;
758 #else /* __linux__ */
759 node.timep[0].tv_sec = dip->di_atime;
760 node.timep[0].tv_usec = dip->di_atimensec / 1000;
761 node.timep[1].tv_sec = dip->di_mtime;
762 node.timep[1].tv_usec = dip->di_mtimensec / 1000;
763 #endif /* __linux__ */
764 node.mode = dip->di_mode;
765 node.flags = dip->di_flags;
766 node.uid = dip->di_uid;
767 node.gid = dip->di_gid;
768 if ( fwrite((char *)&node, 1, sizeof(struct modeinfo), mf) != sizeof(struct modeinfo) )
769 err(1,"cannot write to file %s", modefile);
770 return (itp);
771 }
772
773 /*
774 * Look up an inode in the table of directories
775 */
776 static struct inotab *
777 inotablookup(dump_ino_t ino)
778 {
779 struct inotab *itp;
780
781 for (itp = inotab[INOHASH(ino)]; itp != NULL; itp = itp->t_next)
782 if (itp->t_ino == ino)
783 return (itp);
784 return (NULL);
785 }
786
787 /*
788 * Clean up and exit
789 */
790 void
791 cleanup(void)
792 {
793 closemt();
794 if (modefile[0] != '#')
795 (void) unlink(modefile);
796 if (dirfile[0] != '#')
797 (void) unlink(dirfile);
798 }