Imported Upstream version 0.4b41
[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.33 2005/05/28 18:34:47 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, 1, DIRBLKSIZ, df) != DIRBLKSIZ )
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                 (void) fread((char *)&node, 1, sizeof(struct modeinfo), mf);
687                 if (feof(mf))
688                         break;
689                 if (node.xattr) {
690                         (void) fread(xattr, 1, XATTR_MAXSIZE, mf);
691                         if (feof(mf))
692                                 break;
693                 }
694                 ep = lookupino(node.ino);
695                 if (command == 'i' || command == 'x') {
696                         if (ep == NULL)
697                                 continue;
698                         if ((flags & FORCE) == 0 && ep->e_flags & EXISTED) {
699                                 ep->e_flags &= ~NEW;
700                                 continue;
701                         }
702                         if ((flags & FORCE) == 0 &&
703                             node.ino == ROOTINO &&
704                             reply("set owner/mode for '.'") == FAIL)
705                                 continue;
706                 }
707                 if (ep == NULL) {
708                         panic("cannot find directory inode %d\n", node.ino);
709                 } else {
710                         cp = myname(ep);
711                         (void) chown(cp, node.uid, node.gid);
712                         (void) chmod(cp, node.mode);
713                         if (node.flags)
714 #ifdef  __linux__
715                                 (void) lsetflags(cp, node.flags);
716 #else
717 #ifdef sunos
718 #else
719                                 (void) chflags(cp, node.flags);
720 #endif
721 #endif
722                         utimes(cp, node.timep);
723                         if (node.xattr)
724                                 xattr_extract(cp, xattr);
725                         ep->e_flags &= ~NEW;
726                 }
727         }
728         if (ferror(mf))
729                 panic("error setting directory modes\n");
730         (void) fclose(mf);
731 }
732
733 /*
734  * In restore -C mode, tests the attributes for all directories
735  */
736 void
737 comparedirmodes(void)
738 {
739         FILE *mf;
740         struct modeinfo node;
741         struct entry *ep;
742         char *cp;
743
744         Vprintf(stdout, "Compare directories modes, owner, attributes.\n");
745         if (modefile[0] == '#') {
746                 panic("modefile not defined\n");
747                 fprintf(stderr, "directory mode, owner, and times not set\n");
748                 return;
749         }
750         mf = fopen(modefile, "r");
751         if (mf == NULL) {
752                 warn("fopen");
753                 fprintf(stderr, "cannot open mode file %s\n", modefile);
754                 fprintf(stderr, "directory mode, owner, and times not set\n");
755                 return;
756         }
757         clearerr(mf);
758         for (;;) {
759                 char xattr[XATTR_MAXSIZE];
760                 (void) fread((char *)&node, 1, sizeof(struct modeinfo), mf);
761                 if (feof(mf))
762                         break;
763                 if (node.xattr) {
764                         (void) fread(xattr, 1, XATTR_MAXSIZE, mf);
765                         if (feof(mf))
766                                 break;
767                 }
768                 ep = lookupino(node.ino);
769                 if (ep == NULL) {
770                         panic("cannot find directory inode %d\n", node.ino);
771                 } else {
772                         struct STAT sb;
773                         unsigned long newflags;
774
775                         cp = myname(ep);
776                         if (LSTAT(cp, &sb) < 0) {
777                                 warn("unable to stat %s", cp);
778                                 do_compare_error;
779                                 continue;
780                         }
781
782                         Vprintf(stdout, "comparing directory %s\n", cp);
783
784                         if (sb.st_mode != node.mode) {
785                                 fprintf(stderr, "%s: mode changed from 0%o to 0%o.\n",
786                                         cp, node.mode & 07777, sb.st_mode & 07777);
787                                 do_compare_error;
788                         }
789                         if (sb.st_uid != node.uid) {
790                                 fprintf(stderr, "%s: uid changed from %d to %d.\n",
791                                         cp, node.uid, sb.st_uid);
792                                 do_compare_error;
793                         }
794                         if (sb.st_gid != node.gid) {
795                                 fprintf(stderr, "%s: gid changed from %d to %d.\n",
796                                         cp, node.gid, sb.st_gid);
797                                 do_compare_error;
798                         }
799 #ifdef  __linux__
800                         if (lgetflags(cp, &newflags) < 0) {
801                                 if (node.flags != 0) {
802                                         warn("%s: lgetflags failed", cp);
803                                         do_compare_error;
804                                 }
805                         }
806                         else {
807                                 if (newflags != node.flags) {
808                                         fprintf(stderr, "%s: flags changed from 0x%08x to 0x%08lx.\n",
809                                                 cp, node.flags, newflags);
810                                         do_compare_error;
811                                 }
812                         }
813 #endif
814                         if (node.xattr) {
815                                 if (xattr_compare(cp, xattr) == FAIL)
816                                         do_compare_error;
817                         }
818                         else {
819                                 if (xattr_compare(cp, NULL) == FAIL)
820                                         do_compare_error;
821                         }
822                         ep->e_flags &= ~NEW;
823                 }
824         }
825         if (ferror(mf))
826                 panic("error setting directory modes\n");
827         (void) fclose(mf);
828 }
829
830 /*
831  * Generate a literal copy of a directory.
832  */
833 int
834 genliteraldir(char *name, dump_ino_t ino)
835 {
836         struct inotab *itp;
837         int ofile, dp, i, size;
838         char buf[BUFSIZ];
839
840         itp = inotablookup(ino);
841         if (itp == NULL)
842                 panic("Cannot find directory inode %d named %s\n", ino, name);
843         if ((ofile = open(name, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
844                 warn("%s: cannot create file\n", name);
845                 return (FAIL);
846         }
847         rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
848         dp = dup(dirp->dd_fd);
849         for (i = itp->t_size; i > 0; i -= BUFSIZ) {
850                 size = i < BUFSIZ ? i : BUFSIZ;
851                 if (read(dp, buf, (int) size) == -1) {
852                         warnx("write error extracting inode %lu, name %s\n",
853                                 (unsigned long)curfile.ino, curfile.name);
854                         err(1, "read");
855                 }
856                 if (!Nflag && write(ofile, buf, (int) size) == -1) {
857                         warnx("write error extracting inode %lu, name %s\n",
858                                 (unsigned long)curfile.ino, curfile.name);
859                         err(1, "write");
860                 }
861         }
862         (void) close(dp);
863         (void) close(ofile);
864         return (GOOD);
865 }
866
867 /*
868  * Determine the type of an inode
869  */
870 int
871 inodetype(dump_ino_t ino)
872 {
873         struct inotab *itp;
874
875         itp = inotablookup(ino);
876         if (itp == NULL)
877                 return (LEAF);
878         return (NODE);
879 }
880
881 /*
882  * Allocate and initialize a directory inode entry.
883  * If requested, save its pertinent mode, owner, and time info.
884  */
885 static struct inotab *
886 #if defined(__linux__) || defined(sunos)
887 allocinotab(dump_ino_t ino, OFF_T seekpt)
888 #else
889 allocinotab(dump_ino_t ino, OFF_T seekpt)
890 #endif
891 {
892         struct inotab   *itp;
893
894         itp = calloc(1, sizeof(struct inotab));
895         if (itp == NULL)
896                 panic("no memory directory table\n");
897         itp->t_next = inotab[INOHASH(ino)];
898         inotab[INOHASH(ino)] = itp;
899         itp->t_ino = ino;
900         itp->t_seekpt = seekpt;
901         return itp;
902 }
903
904 static void
905 #if defined(__linux__) || defined(sunos)
906 savemodeinfo(dump_ino_t ino, struct new_bsd_inode *dip, char *xattr) {
907 #else
908 savemodeinfo(dump_ino_t ino, struct dinode *dip, char *xattr) {
909 #endif
910         struct modeinfo node;
911
912         if (mf == NULL)
913                 return;
914         node.ino = ino;
915 #if defined(__linux__) || defined(sunos)
916         node.timep[0].tv_sec = dip->di_atime.tv_sec;
917         node.timep[0].tv_usec = dip->di_atime.tv_usec;
918         node.timep[1].tv_sec = dip->di_mtime.tv_sec;
919         node.timep[1].tv_usec = dip->di_mtime.tv_usec;
920 #else   /* __linux__  || sunos */
921         node.timep[0].tv_sec = dip->di_atime;
922         node.timep[0].tv_usec = dip->di_atimensec / 1000;
923         node.timep[1].tv_sec = dip->di_mtime;
924         node.timep[1].tv_usec = dip->di_mtimensec / 1000;
925 #endif  /* __linux__  || sunos */
926         node.mode = dip->di_mode;
927         node.flags = dip->di_flags;
928         node.uid = dip->di_uid;
929         node.gid = dip->di_gid;
930         node.xattr = xattr ? 1 : 0;
931         if ( fwrite((char *)&node, 1, sizeof(struct modeinfo), mf) != sizeof(struct modeinfo) )
932                 err(1,"cannot write to file %s", modefile);
933         if (xattr)
934                 if ( fwrite(xattr, 1, XATTR_MAXSIZE, mf) != XATTR_MAXSIZE)
935                         err(1,"cannot write to file %s", modefile);
936 }
937
938 /*
939  * Look up an inode in the table of directories
940  */
941 static struct inotab *
942 inotablookup(dump_ino_t ino)
943 {
944         struct inotab *itp;
945
946         for (itp = inotab[INOHASH(ino)]; itp != NULL; itp = itp->t_next)
947                 if (itp->t_ino == ino)
948                         return (itp);
949         return (NULL);
950 }
951
952 /*
953  * Clean up and exit
954  */
955 void
956 cleanup(void)
957 {
958         closemt();
959         if (modefile[0] != '#')
960                 (void) unlink(modefile);
961         if (dirfile[0] != '#')
962                 (void) unlink(dirfile);
963 }