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
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.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
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.
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
44 static const char rcsid[] =
45 "$Id: dirs.c,v 1.33 2005/05/28 18:34:47 stelian Exp $";
49 #include <compatlfs.h>
50 #include <sys/types.h>
51 #include <sys/param.h>
56 #ifdef HAVE_EXT2FS_EXT2_FS_H
57 #include <ext2fs/ext2_fs.h>
59 #include <linux/ext2_fs.h>
61 #include <bsdcompat.h>
64 #include <sys/fcntl.h>
65 #include <bsdcompat.h>
67 #include <ufs/ufs/dinode.h>
68 #include <ufs/ufs/dir.h>
70 #endif /* __linux__ */
71 #include <protocols/dumprestore.h>
73 #include <compaterr.h>
84 #include <arpa/nameser_compat.h>
86 #include <machine/endian.h>
90 #include "pathnames.h"
95 * Symbol table of directories read from tape.
98 #define INOHASH(val) (val % HASHSIZE)
100 struct inotab *t_next;
105 static struct inotab *inotab[HASHSIZE];
108 * Information retained about directories.
112 struct timeval timep[2];
121 * Definitions for library routines operating on directories.
124 #define DIRBLKSIZ 1024
129 char dd_buf[DIRBLKSIZ];
133 * Global variables for this file.
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 */
143 * Format of old style directories.
148 char d_name[ODIRSIZ];
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 *));
155 static struct inotab *allocinotab __P((dump_ino_t, OFF_T));
156 static void savemodeinfo __P((dump_ino_t, struct dinode *, char *));
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 *));
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.
179 extractdirs(int genmode)
182 #if defined(__linux__) || defined(sunos)
183 struct new_bsd_inode ip;
188 struct direct nulldir;
190 char xattr[XATTR_MAXSIZE];
194 Vprintf(stdout, "Extract directories from tape\n");
195 (void) snprintf(dirfile, sizeof(dirfile), "%s/rstdir%ld", tmpdir,
197 if (command != 'r' && command != 'R') {
198 (void) strncat(dirfile, "-XXXXXX",
199 sizeof(dirfile) - strlen(dirfile));
200 fd = MKSTEMP(dirfile);
202 fd = OPEN(dirfile, O_RDWR|O_CREAT|O_EXCL, 0666);
203 if (fd == -1 || (df = fdopen(fd, "w")) == NULL) {
206 err(1, "cannot create directory temporary %s", dirfile);
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);
215 fd = OPEN(modefile, O_RDWR|O_CREAT|O_EXCL, 0666);
216 if (fd == -1 || (mf = fdopen(fd, "w")) == NULL) {
219 err(1, "cannot create modefile %s", modefile);
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);
229 curfile.name = "<directory file - name unknown>";
230 curfile.action = USING;
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);
238 if (mf != NULL && fclose(mf) == EOF )
239 err(1, "cannot write to file %s", dirfile);
242 panic("Root directory is not on tape\n");
245 memcpy(&ip, curfile.dip, sizeof(ip));
246 itp = allocinotab(ino, seekpt);
247 getfile(putdir, xtrnull);
249 while (spcl.c_flags & DR_EXTATTRIBUTES) {
250 switch (spcl.c_extattributes) {
251 case EXT_MACOSFNDRINFO:
252 msg("MacOSX attributes not supported, skipping\n");
255 case EXT_MACOSRESFORK:
256 msg("MacOSX attributes not supported, skipping\n");
260 if (readxattr(xattr) == GOOD)
266 savemodeinfo(ino, &ip, xattr);
268 savemodeinfo(ino, &ip, NULL);
271 itp->t_size = seekpt - itp->t_seekpt;
276 * skip over all the directories on the tape
282 while (curfile.dip && (curfile.dip->di_mode & IFMT) == IFDIR) {
288 * Recursively find names and inumbers of all files in subtree
289 * pname and pass them off to be processed.
292 treescan(char *pname, dump_ino_t ino, long (*todo) __P((char *, dump_ino_t, int)))
298 char locname[MAXPATHLEN + 1];
300 itp = inotablookup(ino);
303 * Pname is name of a simple file or an unchanged directory.
305 (void) (*todo)(pname, ino, LEAF);
309 * Pname is a dumped directory name.
311 if ((*todo)(pname, ino, NODE) == FAIL)
314 * begin search through the directory
315 * skipping over "." and ".."
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); /* ".." */
325 fprintf(stderr, "Warning: `.' missing from directory %s\n",
327 if (dp != NULL && strcmp(dp->d_name, "..") == 0)
328 dp = rst_readdir(dirp); /* first real entry */
330 fprintf(stderr, "Warning: `..' missing from directory %s\n",
332 bpt = rst_telldir(dirp);
334 * a zero inode signals end of directory
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);
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);
346 dp = rst_readdir(dirp);
347 bpt = rst_telldir(dirp);
352 * Lookup a pathname which is always assumed to start from the ROOTINO.
355 pathsearch(const char *pathname)
359 char *path, *name, buffer[MAXPATHLEN];
361 strcpy(buffer, pathname);
368 while ((name = strsep(&path, "/")) != NULL && *name /* != NULL */) {
370 while ((name = strtok_r(NULL, "/", &path)) != NULL && *name /* != NULL */) {
372 if ((dp = searchdir(ino, name)) == NULL)
380 * Lookup the requested name in directory inum.
381 * Return its inode number if found, zero if it does not exist.
383 static struct direct *
384 searchdir(dump_ino_t inum, char *name)
390 itp = inotablookup(inum);
393 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
396 dp = rst_readdir(dirp);
399 } while (dp->d_namlen != len || strncmp(dp->d_name, name, len) != 0);
404 * Put the directory entries in the directory file
407 putdir(char *buf, size_t size)
409 struct direct cvtbuf;
411 struct odirect *eodp;
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) {
423 for (loc = 0; loc < (long)size; ) {
424 dp = (struct direct *)(buf + loc);
426 printf ("reclen = %d, namlen = %d, type = %d\n",
427 dp->d_reclen, dp->d_namlen, dp->d_type);
430 swabst((u_char *)"is", (u_char *) dp);
431 if (oldinofmt && dp->d_ino != 0) {
432 # if BYTE_ORDER == BIG_ENDIAN
434 dp->d_namlen = dp->d_type;
437 dp->d_namlen = dp->d_type;
439 if (dp->d_namlen == 0 && dp->d_type != 0)
440 dp->d_namlen = dp->d_type;
441 dp->d_type = DT_UNKNOWN;
444 printf ("reclen = %d, namlen = %d, type = %d\n",
445 dp->d_reclen, dp->d_namlen, dp->d_type);
447 i = DIRBLKSIZ - (loc & (DIRBLKSIZ - 1));
448 if ((dp->d_reclen & 0x3) != 0 ||
450 dp->d_reclen < DIRSIZ(0, dp)
452 || dp->d_namlen > MAXNAMLEN
455 Vprintf(stdout, "Mangled directory: ");
456 if ((dp->d_reclen & 0x3) != 0)
458 "reclen not multiple of 4 ");
459 if (dp->d_reclen < DIRSIZ(0, dp))
461 "reclen less than DIRSIZ (%d < %d) ",
462 dp->d_reclen, DIRSIZ(0, dp));
464 if (dp->d_namlen > MAXNAMLEN)
466 "reclen name too big (%d > %d) ",
467 dp->d_namlen, MAXNAMLEN);
469 Vprintf(stdout, "\n");
474 if (dp->d_ino != 0) {
482 * These variables are "local" to the following two functions.
484 static char dirbuf[DIRBLKSIZ];
485 static long dirloc = 0;
486 static long prev = 0;
489 * add a new directory entry to a file.
492 putent(struct direct *dp)
494 dp->d_reclen = DIRSIZ(0, dp);
495 if (dirloc + dp->d_reclen > DIRBLKSIZ) {
496 ((struct direct *)(dirbuf + prev))->d_reclen =
498 if ( fwrite(dirbuf, 1, DIRBLKSIZ, df) != DIRBLKSIZ )
499 err(1,"cannot write to file %s", dirfile);
502 memmove(dirbuf + dirloc, dp, (size_t)dp->d_reclen);
504 dirloc += dp->d_reclen;
508 * flush out a directory that is finished.
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);
518 err(1, "cannot write to file %s", dirfile);
523 dcvt(struct odirect *odp, struct direct *ndp)
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);
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.
542 rst_seekdir(RST_DIR *dirp, OFF_T loc, OFF_T base)
545 if (loc == rst_telldir(dirp))
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);
557 * get next entry in a directory.
560 rst_readdir(RST_DIR *dirp)
565 if (dirp->dd_loc == 0) {
566 dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf,
568 if (dirp->dd_size <= 0) {
569 Dprintf(stderr, "error reading directory\n");
573 if (dirp->dd_loc >= dirp->dd_size) {
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",
584 dirp->dd_loc += dp->d_reclen;
585 if (dp->d_ino == 0 && strcmp(dp->d_name, "/") == 0)
587 if (dp->d_ino >= maxino) {
588 Dprintf(stderr, "corrupted directory: bad inum %d\n",
597 * Simulate the opening of a directory
600 rst_opendir(const char *name)
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);
616 * In our case, there is nothing to do when closing a directory.
619 rst_closedir(RST_DIR *dirp)
622 (void)close(dirp->dd_fd);
628 * Simulate finding the current offset in the directory.
631 rst_telldir(RST_DIR *dirp)
633 return ((OFF_T)LSEEK(dirp->dd_fd,
634 (OFF_T)0, SEEK_CUR) - dirp->dd_size + dirp->dd_loc);
638 * Open a directory file.
641 opendirfile(const char *name)
646 if ((fd = OPEN(name, O_RDONLY)) == -1)
648 if ((dirp = malloc(sizeof(RST_DIR))) == NULL) {
658 * Set the mode, owner, and times for all new or changed directories
661 setdirmodes(int flags)
664 struct modeinfo node;
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");
676 mf = fopen(modefile, "r");
679 fprintf(stderr, "cannot open mode file %s\n", modefile);
680 fprintf(stderr, "directory mode, owner, and times not set\n");
685 char xattr[XATTR_MAXSIZE];
686 (void) fread((char *)&node, 1, sizeof(struct modeinfo), mf);
690 (void) fread(xattr, 1, XATTR_MAXSIZE, mf);
694 ep = lookupino(node.ino);
695 if (command == 'i' || command == 'x') {
698 if ((flags & FORCE) == 0 && ep->e_flags & EXISTED) {
702 if ((flags & FORCE) == 0 &&
703 node.ino == ROOTINO &&
704 reply("set owner/mode for '.'") == FAIL)
708 panic("cannot find directory inode %d\n", node.ino);
711 (void) chown(cp, node.uid, node.gid);
712 (void) chmod(cp, node.mode);
715 (void) lsetflags(cp, node.flags);
719 (void) chflags(cp, node.flags);
722 utimes(cp, node.timep);
724 xattr_extract(cp, xattr);
729 panic("error setting directory modes\n");
734 * In restore -C mode, tests the attributes for all directories
737 comparedirmodes(void)
740 struct modeinfo node;
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");
750 mf = fopen(modefile, "r");
753 fprintf(stderr, "cannot open mode file %s\n", modefile);
754 fprintf(stderr, "directory mode, owner, and times not set\n");
759 char xattr[XATTR_MAXSIZE];
760 (void) fread((char *)&node, 1, sizeof(struct modeinfo), mf);
764 (void) fread(xattr, 1, XATTR_MAXSIZE, mf);
768 ep = lookupino(node.ino);
770 panic("cannot find directory inode %d\n", node.ino);
773 unsigned long newflags;
776 if (LSTAT(cp, &sb) < 0) {
777 warn("unable to stat %s", cp);
782 Vprintf(stdout, "comparing directory %s\n", cp);
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);
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);
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);
800 if (lgetflags(cp, &newflags) < 0) {
801 if (node.flags != 0) {
802 warn("%s: lgetflags failed", cp);
807 if (newflags != node.flags) {
808 fprintf(stderr, "%s: flags changed from 0x%08x to 0x%08lx.\n",
809 cp, node.flags, newflags);
815 if (xattr_compare(cp, xattr) == FAIL)
819 if (xattr_compare(cp, NULL) == FAIL)
826 panic("error setting directory modes\n");
831 * Generate a literal copy of a directory.
834 genliteraldir(char *name, dump_ino_t ino)
837 int ofile, dp, i, size;
840 itp = inotablookup(ino);
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);
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);
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);
868 * Determine the type of an inode
871 inodetype(dump_ino_t ino)
875 itp = inotablookup(ino);
882 * Allocate and initialize a directory inode entry.
883 * If requested, save its pertinent mode, owner, and time info.
885 static struct inotab *
886 #if defined(__linux__) || defined(sunos)
887 allocinotab(dump_ino_t ino, OFF_T seekpt)
889 allocinotab(dump_ino_t ino, OFF_T seekpt)
894 itp = calloc(1, sizeof(struct inotab));
896 panic("no memory directory table\n");
897 itp->t_next = inotab[INOHASH(ino)];
898 inotab[INOHASH(ino)] = itp;
900 itp->t_seekpt = seekpt;
905 #if defined(__linux__) || defined(sunos)
906 savemodeinfo(dump_ino_t ino, struct new_bsd_inode *dip, char *xattr) {
908 savemodeinfo(dump_ino_t ino, struct dinode *dip, char *xattr) {
910 struct modeinfo node;
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);
934 if ( fwrite(xattr, 1, XATTR_MAXSIZE, mf) != XATTR_MAXSIZE)
935 err(1,"cannot write to file %s", modefile);
939 * Look up an inode in the table of directories
941 static struct inotab *
942 inotablookup(dump_ino_t ino)
946 for (itp = inotab[INOHASH(ino)]; itp != NULL; itp = itp->t_next)
947 if (itp->t_ino == ino)
959 if (modefile[0] != '#')
960 (void) unlink(modefile);
961 if (dirfile[0] != '#')
962 (void) unlink(dirfile);