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