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