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