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