]> git.wh0rd.org - dump.git/blob - restore/dirs.c
Relicensed dump/restore under the revised BSD license, as per ftp://ftp.cs.berkeley...
[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. Neither the name of the University nor the names of its contributors
27 * may be used to endorse or promote products derived from this software
28 * without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * SUCH DAMAGE.
41 */
42
43 #ifndef lint
44 static const char rcsid[] =
45 "$Id: dirs.c,v 1.23 2003/03/30 15:40:38 stelian Exp $";
46 #endif /* not lint */
47
48 #include <config.h>
49 #include <sys/param.h>
50 #include <sys/file.h>
51 #include <sys/stat.h>
52
53 #ifdef __linux__
54 #ifdef HAVE_EXT2FS_EXT2_FS_H
55 #include <ext2fs/ext2_fs.h>
56 #else
57 #include <linux/ext2_fs.h>
58 #endif
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 #if defined(__linux__) || defined(sunos)
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 int i;
164 #if defined(__linux__) || defined(sunos)
165 struct new_bsd_inode *ip;
166 #else
167 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 struct inotab *itp;
252 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 >= (int)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 >= (int)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 #ifdef __linux__
325 while ((name = strsep(&path, "/")) != NULL && *name /* != NULL */) {
326 #else
327 while ((name = strtok_r(NULL, "/", &path)) != NULL && *name /* != NULL */) {
328 #endif
329 if ((dp = searchdir(ino, name)) == NULL)
330 return (NULL);
331 ino = dp->d_ino;
332 }
333 return (dp);
334 }
335
336 /*
337 * Lookup the requested name in directory inum.
338 * Return its inode number if found, zero if it does not exist.
339 */
340 static struct direct *
341 searchdir(dump_ino_t inum, char *name)
342 {
343 struct direct *dp;
344 struct inotab *itp;
345 int len;
346
347 itp = inotablookup(inum);
348 if (itp == NULL)
349 return (NULL);
350 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
351 len = strlen(name);
352 do {
353 dp = rst_readdir(dirp);
354 if (dp == NULL)
355 return (NULL);
356 } while (dp->d_namlen != len || strncmp(dp->d_name, name, len) != 0);
357 return (dp);
358 }
359
360 /*
361 * Put the directory entries in the directory file
362 */
363 static void
364 putdir(char *buf, size_t size)
365 {
366 struct direct cvtbuf;
367 struct odirect *odp;
368 struct odirect *eodp;
369 struct direct *dp;
370 long loc, i;
371
372 if (cvtflag) {
373 eodp = (struct odirect *)&buf[size];
374 for (odp = (struct odirect *)buf; odp < eodp; odp++)
375 if (odp->d_ino != 0) {
376 dcvt(odp, &cvtbuf);
377 putent(&cvtbuf);
378 }
379 } else {
380 for (loc = 0; loc < (long)size; ) {
381 dp = (struct direct *)(buf + loc);
382 #ifdef DIRDEBUG
383 printf ("reclen = %d, namlen = %d, type = %d\n",
384 dp->d_reclen, dp->d_namlen, dp->d_type);
385 #endif
386 if (Bcvt)
387 swabst((u_char *)"is", (u_char *) dp);
388 if (oldinofmt && dp->d_ino != 0) {
389 # if BYTE_ORDER == BIG_ENDIAN
390 if (Bcvt)
391 dp->d_namlen = dp->d_type;
392 # else
393 if (!Bcvt)
394 dp->d_namlen = dp->d_type;
395 # endif
396 if (dp->d_namlen == 0 && dp->d_type != 0)
397 dp->d_namlen = dp->d_type;
398 dp->d_type = DT_UNKNOWN;
399 }
400 #ifdef DIRDEBUG
401 printf ("reclen = %d, namlen = %d, type = %d\n",
402 dp->d_reclen, dp->d_namlen, dp->d_type);
403 #endif
404 i = DIRBLKSIZ - (loc & (DIRBLKSIZ - 1));
405 if ((dp->d_reclen & 0x3) != 0 ||
406 dp->d_reclen > i ||
407 dp->d_reclen < DIRSIZ(0, dp) ||
408 dp->d_namlen > MAXNAMLEN) {
409 Vprintf(stdout, "Mangled directory: ");
410 if ((dp->d_reclen & 0x3) != 0)
411 Vprintf(stdout,
412 "reclen not multiple of 4 ");
413 if (dp->d_reclen < DIRSIZ(0, dp))
414 Vprintf(stdout,
415 "reclen less than DIRSIZ (%d < %d) ",
416 dp->d_reclen, DIRSIZ(0, dp));
417 if (dp->d_namlen > MAXNAMLEN)
418 Vprintf(stdout,
419 "reclen name too big (%d > %d) ",
420 dp->d_namlen, MAXNAMLEN);
421 Vprintf(stdout, "\n");
422 loc += i;
423 continue;
424 }
425 loc += dp->d_reclen;
426 if (dp->d_ino != 0) {
427 putent(dp);
428 }
429 }
430 }
431 }
432
433 /*
434 * These variables are "local" to the following two functions.
435 */
436 static char dirbuf[DIRBLKSIZ];
437 static long dirloc = 0;
438 static long prev = 0;
439
440 /*
441 * add a new directory entry to a file.
442 */
443 static void
444 putent(struct direct *dp)
445 {
446 dp->d_reclen = DIRSIZ(0, dp);
447 if (dirloc + dp->d_reclen > DIRBLKSIZ) {
448 ((struct direct *)(dirbuf + prev))->d_reclen =
449 DIRBLKSIZ - prev;
450 if ( fwrite(dirbuf, 1, DIRBLKSIZ, df) != DIRBLKSIZ )
451 err(1,"cannot write to file %s", dirfile);
452 dirloc = 0;
453 }
454 memmove(dirbuf + dirloc, dp, (size_t)dp->d_reclen);
455 prev = dirloc;
456 dirloc += dp->d_reclen;
457 }
458
459 /*
460 * flush out a directory that is finished.
461 */
462 static void
463 flushent(void)
464 {
465 ((struct direct *)(dirbuf + prev))->d_reclen = DIRBLKSIZ - prev;
466 if ( fwrite(dirbuf, (int)dirloc, 1, df) != 1 )
467 err(1, "cannot write to file %s", dirfile);
468 seekpt = ftell(df);
469 if (seekpt == -1)
470 err(1, "cannot write to file %s", dirfile);
471 dirloc = 0;
472 }
473
474 static void
475 dcvt(struct odirect *odp, struct direct *ndp)
476 {
477
478 memset(ndp, 0, (size_t)(sizeof *ndp));
479 ndp->d_ino = odp->d_ino;
480 ndp->d_type = DT_UNKNOWN;
481 (void) strncpy(ndp->d_name, odp->d_name, ODIRSIZ);
482 ndp->d_namlen = strlen(ndp->d_name);
483 ndp->d_reclen = DIRSIZ(0, ndp);
484 }
485
486 /*
487 * Seek to an entry in a directory.
488 * Only values returned by rst_telldir should be passed to rst_seekdir.
489 * This routine handles many directories in a single file.
490 * It takes the base of the directory in the file, plus
491 * the desired seek offset into it.
492 */
493 static void
494 rst_seekdir(RST_DIR *dirp, long loc, long base)
495 {
496
497 if (loc == rst_telldir(dirp))
498 return;
499 loc -= base;
500 if (loc < 0)
501 fprintf(stderr, "bad seek pointer to rst_seekdir %ld\n", loc);
502 (void) lseek(dirp->dd_fd, base + (loc & ~(DIRBLKSIZ - 1)), SEEK_SET);
503 dirp->dd_loc = loc & (DIRBLKSIZ - 1);
504 if (dirp->dd_loc != 0)
505 dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ);
506 }
507
508 /*
509 * get next entry in a directory.
510 */
511 struct direct *
512 rst_readdir(RST_DIR *dirp)
513 {
514 struct direct *dp;
515
516 for (;;) {
517 if (dirp->dd_loc == 0) {
518 dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf,
519 DIRBLKSIZ);
520 if (dirp->dd_size <= 0) {
521 Dprintf(stderr, "error reading directory\n");
522 return (NULL);
523 }
524 }
525 if (dirp->dd_loc >= dirp->dd_size) {
526 dirp->dd_loc = 0;
527 continue;
528 }
529 dp = (struct direct *)(dirp->dd_buf + dirp->dd_loc);
530 if (dp->d_reclen == 0 ||
531 dp->d_reclen > DIRBLKSIZ + 1 - dirp->dd_loc) {
532 Dprintf(stderr, "corrupted directory: bad reclen %d\n",
533 dp->d_reclen);
534 return (NULL);
535 }
536 dirp->dd_loc += dp->d_reclen;
537 if (dp->d_ino == 0 && strcmp(dp->d_name, "/") == 0)
538 return (NULL);
539 if (dp->d_ino >= maxino) {
540 Dprintf(stderr, "corrupted directory: bad inum %d\n",
541 dp->d_ino);
542 continue;
543 }
544 return (dp);
545 }
546 }
547
548 /*
549 * Simulate the opening of a directory
550 */
551 RST_DIR *
552 rst_opendir(const char *name)
553 {
554 struct inotab *itp;
555 RST_DIR *dirp;
556 dump_ino_t ino;
557
558 if ((ino = dirlookup(name)) > 0 &&
559 (itp = inotablookup(ino)) != NULL) {
560 dirp = opendirfile(dirfile);
561 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
562 return (dirp);
563 }
564 return (NULL);
565 }
566
567 /*
568 * In our case, there is nothing to do when closing a directory.
569 */
570 void
571 rst_closedir(RST_DIR *dirp)
572 {
573
574 (void)close(dirp->dd_fd);
575 free(dirp);
576 return;
577 }
578
579 /*
580 * Simulate finding the current offset in the directory.
581 */
582 static long
583 rst_telldir(RST_DIR *dirp)
584 {
585 return ((long)lseek(dirp->dd_fd,
586 (OFF_T)0, SEEK_CUR) - dirp->dd_size + dirp->dd_loc);
587 }
588
589 /*
590 * Open a directory file.
591 */
592 static RST_DIR *
593 opendirfile(const char *name)
594 {
595 RST_DIR *dirp;
596 int fd;
597
598 if ((fd = open(name, O_RDONLY)) == -1)
599 return (NULL);
600 if ((dirp = malloc(sizeof(RST_DIR))) == NULL) {
601 (void)close(fd);
602 return (NULL);
603 }
604 dirp->dd_fd = fd;
605 dirp->dd_loc = 0;
606 return (dirp);
607 }
608
609 /*
610 * Set the mode, owner, and times for all new or changed directories
611 */
612 void
613 setdirmodes(int flags)
614 {
615 FILE *mf;
616 struct modeinfo node;
617 struct entry *ep;
618 char *cp;
619
620 Vprintf(stdout, "Set directory mode, owner, and times.\n");
621 if (command == 'r' || command == 'R')
622 (void) snprintf(modefile, sizeof(modefile), "%s/rstmode%lu", tmpdir, (long)dumpdate);
623 if (modefile[0] == '#') {
624 panic("modefile not defined\n");
625 fprintf(stderr, "directory mode, owner, and times not set\n");
626 return;
627 }
628 mf = fopen(modefile, "r");
629 if (mf == NULL) {
630 warn("fopen");
631 fprintf(stderr, "cannot open mode file %s\n", modefile);
632 fprintf(stderr, "directory mode, owner, and times not set\n");
633 return;
634 }
635 clearerr(mf);
636 for (;;) {
637 (void) fread((char *)&node, 1, sizeof(struct modeinfo), mf);
638 if (feof(mf))
639 break;
640 ep = lookupino(node.ino);
641 if (command == 'i' || command == 'x') {
642 if (ep == NULL)
643 continue;
644 if ((flags & FORCE) == 0 && ep->e_flags & EXISTED) {
645 ep->e_flags &= ~NEW;
646 continue;
647 }
648 if ((flags & FORCE) == 0 &&
649 node.ino == ROOTINO &&
650 reply("set owner/mode for '.'") == FAIL)
651 continue;
652 }
653 if (ep == NULL) {
654 panic("cannot find directory inode %d\n", node.ino);
655 } else {
656 cp = myname(ep);
657 (void) chown(cp, node.uid, node.gid);
658 (void) chmod(cp, node.mode);
659 if (node.flags)
660 #ifdef __linux__
661 (void) fsetflags(cp, node.flags);
662 #else
663 (void) chflags(cp, node.flags);
664 #endif
665 utimes(cp, node.timep);
666 ep->e_flags &= ~NEW;
667 }
668 }
669 if (ferror(mf))
670 panic("error setting directory modes\n");
671 (void) fclose(mf);
672 }
673
674 /*
675 * Generate a literal copy of a directory.
676 */
677 int
678 genliteraldir(char *name, dump_ino_t ino)
679 {
680 struct inotab *itp;
681 int ofile, dp, i, size;
682 char buf[BUFSIZ];
683
684 itp = inotablookup(ino);
685 if (itp == NULL)
686 panic("Cannot find directory inode %d named %s\n", ino, name);
687 if ((ofile = open(name, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
688 warn("%s: cannot create file\n", name);
689 return (FAIL);
690 }
691 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
692 dp = dup(dirp->dd_fd);
693 for (i = itp->t_size; i > 0; i -= BUFSIZ) {
694 size = i < BUFSIZ ? i : BUFSIZ;
695 if (read(dp, buf, (int) size) == -1) {
696 warnx("write error extracting inode %lu, name %s\n",
697 (unsigned long)curfile.ino, curfile.name);
698 err(1, "read");
699 }
700 if (!Nflag && write(ofile, buf, (int) size) == -1) {
701 warnx("write error extracting inode %lu, name %s\n",
702 (unsigned long)curfile.ino, curfile.name);
703 err(1, "write");
704 }
705 }
706 (void) close(dp);
707 (void) close(ofile);
708 return (GOOD);
709 }
710
711 /*
712 * Determine the type of an inode
713 */
714 int
715 inodetype(dump_ino_t ino)
716 {
717 struct inotab *itp;
718
719 itp = inotablookup(ino);
720 if (itp == NULL)
721 return (LEAF);
722 return (NODE);
723 }
724
725 /*
726 * Allocate and initialize a directory inode entry.
727 * If requested, save its pertinent mode, owner, and time info.
728 */
729 static struct inotab *
730 #ifdef __linux__
731 allocinotab(dump_ino_t ino, struct new_bsd_inode *dip, long seekpt)
732 #else
733 allocinotab(dump_ino_t ino, struct dinode *dip, long seekpt)
734 #endif
735 {
736 struct inotab *itp;
737 struct modeinfo node;
738
739 itp = calloc(1, sizeof(struct inotab));
740 if (itp == NULL)
741 panic("no memory directory table\n");
742 itp->t_next = inotab[INOHASH(ino)];
743 inotab[INOHASH(ino)] = itp;
744 itp->t_ino = ino;
745 itp->t_seekpt = seekpt;
746 if (mf == NULL)
747 return (itp);
748 node.ino = ino;
749 #ifdef __linux__
750 node.timep[0].tv_sec = dip->di_atime.tv_sec;
751 node.timep[0].tv_usec = dip->di_atime.tv_usec;
752 node.timep[1].tv_sec = dip->di_mtime.tv_sec;
753 node.timep[1].tv_usec = dip->di_mtime.tv_usec;
754 #else /* __linux__ */
755 node.timep[0].tv_sec = dip->di_atime;
756 node.timep[0].tv_usec = dip->di_atimensec / 1000;
757 node.timep[1].tv_sec = dip->di_mtime;
758 node.timep[1].tv_usec = dip->di_mtimensec / 1000;
759 #endif /* __linux__ */
760 node.mode = dip->di_mode;
761 node.flags = dip->di_flags;
762 node.uid = dip->di_uid;
763 node.gid = dip->di_gid;
764 if ( fwrite((char *)&node, 1, sizeof(struct modeinfo), mf) != sizeof(struct modeinfo) )
765 err(1,"cannot write to file %s", modefile);
766 return (itp);
767 }
768
769 /*
770 * Look up an inode in the table of directories
771 */
772 static struct inotab *
773 inotablookup(dump_ino_t ino)
774 {
775 struct inotab *itp;
776
777 for (itp = inotab[INOHASH(ino)]; itp != NULL; itp = itp->t_next)
778 if (itp->t_ino == ino)
779 return (itp);
780 return (NULL);
781 }
782
783 /*
784 * Clean up and exit
785 */
786 void
787 cleanup(void)
788 {
789 closemt();
790 if (modefile[0] != '#')
791 (void) unlink(modefile);
792 if (dirfile[0] != '#')
793 (void) unlink(dirfile);
794 }