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