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.30 2005/01/14 13:01:34 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];
120 * Definitions for library routines operating on directories.
123 #define DIRBLKSIZ 1024
128 char dd_buf[DIRBLKSIZ];
132 * Global variables for this file.
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 */
142 * Format of old style directories.
147 char d_name[ODIRSIZ];
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 *));
154 static struct inotab *allocinotab __P((dump_ino_t, OFF_T));
155 static void savemodeinfo __P((dump_ino_t, struct dinode *));
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 *));
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.
178 extractdirs(int genmode)
181 #if defined(__linux__) || defined(sunos)
182 struct new_bsd_inode ip;
187 struct direct nulldir;
191 Vprintf(stdout, "Extract directories from tape\n");
192 (void) snprintf(dirfile, sizeof(dirfile), "%s/rstdir%ld", tmpdir,
194 if (command != 'r' && command != 'R') {
195 (void) strncat(dirfile, "-XXXXXX",
196 sizeof(dirfile) - strlen(dirfile));
197 fd = MKSTEMP(dirfile);
199 fd = OPEN(dirfile, O_RDWR|O_CREAT|O_EXCL, 0666);
200 if (fd == -1 || (df = fdopen(fd, "w")) == NULL) {
203 err(1, "cannot create directory temporary %s", dirfile);
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);
212 fd = OPEN(modefile, O_RDWR|O_CREAT|O_EXCL, 0666);
213 if (fd == -1 || (mf = fdopen(fd, "w")) == NULL) {
216 err(1, "cannot create modefile %s", modefile);
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);
226 curfile.name = "<directory file - name unknown>";
227 curfile.action = USING;
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);
235 if (mf != NULL && fclose(mf) == EOF )
236 err(1, "cannot write to file %s", dirfile);
239 panic("Root directory is not on tape\n");
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");
251 case EXT_MACOSRESFORK:
252 msg("MacOSX attributes not supported, skipping\n");
256 msg("EA/ACLs attributes not supported, skipping\n");
261 savemodeinfo(ino, &ip);
264 itp->t_size = seekpt - itp->t_seekpt;
269 * skip over all the directories on the tape
275 while (curfile.dip && (curfile.dip->di_mode & IFMT) == IFDIR) {
281 * Recursively find names and inumbers of all files in subtree
282 * pname and pass them off to be processed.
285 treescan(char *pname, dump_ino_t ino, long (*todo) __P((char *, dump_ino_t, int)))
291 char locname[MAXPATHLEN + 1];
293 itp = inotablookup(ino);
296 * Pname is name of a simple file or an unchanged directory.
298 (void) (*todo)(pname, ino, LEAF);
302 * Pname is a dumped directory name.
304 if ((*todo)(pname, ino, NODE) == FAIL)
307 * begin search through the directory
308 * skipping over "." and ".."
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); /* ".." */
318 fprintf(stderr, "Warning: `.' missing from directory %s\n",
320 if (dp != NULL && strcmp(dp->d_name, "..") == 0)
321 dp = rst_readdir(dirp); /* first real entry */
323 fprintf(stderr, "Warning: `..' missing from directory %s\n",
325 bpt = rst_telldir(dirp);
327 * a zero inode signals end of directory
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);
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);
339 dp = rst_readdir(dirp);
340 bpt = rst_telldir(dirp);
345 * Lookup a pathname which is always assumed to start from the ROOTINO.
348 pathsearch(const char *pathname)
352 char *path, *name, buffer[MAXPATHLEN];
354 strcpy(buffer, pathname);
361 while ((name = strsep(&path, "/")) != NULL && *name /* != NULL */) {
363 while ((name = strtok_r(NULL, "/", &path)) != NULL && *name /* != NULL */) {
365 if ((dp = searchdir(ino, name)) == NULL)
373 * Lookup the requested name in directory inum.
374 * Return its inode number if found, zero if it does not exist.
376 static struct direct *
377 searchdir(dump_ino_t inum, char *name)
383 itp = inotablookup(inum);
386 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
389 dp = rst_readdir(dirp);
392 } while (dp->d_namlen != len || strncmp(dp->d_name, name, len) != 0);
397 * Put the directory entries in the directory file
400 putdir(char *buf, size_t size)
402 struct direct cvtbuf;
404 struct odirect *eodp;
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) {
416 for (loc = 0; loc < (long)size; ) {
417 dp = (struct direct *)(buf + loc);
419 printf ("reclen = %d, namlen = %d, type = %d\n",
420 dp->d_reclen, dp->d_namlen, dp->d_type);
423 swabst((u_char *)"is", (u_char *) dp);
424 if (oldinofmt && dp->d_ino != 0) {
425 # if BYTE_ORDER == BIG_ENDIAN
427 dp->d_namlen = dp->d_type;
430 dp->d_namlen = dp->d_type;
432 if (dp->d_namlen == 0 && dp->d_type != 0)
433 dp->d_namlen = dp->d_type;
434 dp->d_type = DT_UNKNOWN;
437 printf ("reclen = %d, namlen = %d, type = %d\n",
438 dp->d_reclen, dp->d_namlen, dp->d_type);
440 i = DIRBLKSIZ - (loc & (DIRBLKSIZ - 1));
441 if ((dp->d_reclen & 0x3) != 0 ||
443 dp->d_reclen < DIRSIZ(0, dp)
445 || dp->d_namlen > MAXNAMLEN
448 Vprintf(stdout, "Mangled directory: ");
449 if ((dp->d_reclen & 0x3) != 0)
451 "reclen not multiple of 4 ");
452 if (dp->d_reclen < DIRSIZ(0, dp))
454 "reclen less than DIRSIZ (%d < %d) ",
455 dp->d_reclen, DIRSIZ(0, dp));
457 if (dp->d_namlen > MAXNAMLEN)
459 "reclen name too big (%d > %d) ",
460 dp->d_namlen, MAXNAMLEN);
462 Vprintf(stdout, "\n");
467 if (dp->d_ino != 0) {
475 * These variables are "local" to the following two functions.
477 static char dirbuf[DIRBLKSIZ];
478 static long dirloc = 0;
479 static long prev = 0;
482 * add a new directory entry to a file.
485 putent(struct direct *dp)
487 dp->d_reclen = DIRSIZ(0, dp);
488 if (dirloc + dp->d_reclen > DIRBLKSIZ) {
489 ((struct direct *)(dirbuf + prev))->d_reclen =
491 if ( fwrite(dirbuf, 1, DIRBLKSIZ, df) != DIRBLKSIZ )
492 err(1,"cannot write to file %s", dirfile);
495 memmove(dirbuf + dirloc, dp, (size_t)dp->d_reclen);
497 dirloc += dp->d_reclen;
501 * flush out a directory that is finished.
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);
511 err(1, "cannot write to file %s", dirfile);
516 dcvt(struct odirect *odp, struct direct *ndp)
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);
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.
535 rst_seekdir(RST_DIR *dirp, OFF_T loc, OFF_T base)
538 if (loc == rst_telldir(dirp))
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);
550 * get next entry in a directory.
553 rst_readdir(RST_DIR *dirp)
558 if (dirp->dd_loc == 0) {
559 dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf,
561 if (dirp->dd_size <= 0) {
562 Dprintf(stderr, "error reading directory\n");
566 if (dirp->dd_loc >= dirp->dd_size) {
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",
577 dirp->dd_loc += dp->d_reclen;
578 if (dp->d_ino == 0 && strcmp(dp->d_name, "/") == 0)
580 if (dp->d_ino >= maxino) {
581 Dprintf(stderr, "corrupted directory: bad inum %d\n",
590 * Simulate the opening of a directory
593 rst_opendir(const char *name)
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);
609 * In our case, there is nothing to do when closing a directory.
612 rst_closedir(RST_DIR *dirp)
615 (void)close(dirp->dd_fd);
621 * Simulate finding the current offset in the directory.
624 rst_telldir(RST_DIR *dirp)
626 return ((OFF_T)LSEEK(dirp->dd_fd,
627 (OFF_T)0, SEEK_CUR) - dirp->dd_size + dirp->dd_loc);
631 * Open a directory file.
634 opendirfile(const char *name)
639 if ((fd = OPEN(name, O_RDONLY)) == -1)
641 if ((dirp = malloc(sizeof(RST_DIR))) == NULL) {
651 * Set the mode, owner, and times for all new or changed directories
654 setdirmodes(int flags)
657 struct modeinfo node;
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");
669 mf = fopen(modefile, "r");
672 fprintf(stderr, "cannot open mode file %s\n", modefile);
673 fprintf(stderr, "directory mode, owner, and times not set\n");
678 (void) fread((char *)&node, 1, sizeof(struct modeinfo), mf);
681 ep = lookupino(node.ino);
682 if (command == 'i' || command == 'x') {
685 if ((flags & FORCE) == 0 && ep->e_flags & EXISTED) {
689 if ((flags & FORCE) == 0 &&
690 node.ino == ROOTINO &&
691 reply("set owner/mode for '.'") == FAIL)
695 panic("cannot find directory inode %d\n", node.ino);
698 (void) chown(cp, node.uid, node.gid);
699 (void) chmod(cp, node.mode);
702 (void) lsetflags(cp, node.flags);
706 (void) chflags(cp, node.flags);
709 utimes(cp, node.timep);
714 panic("error setting directory modes\n");
719 * In restore -C mode, tests the attributes for all directories
722 comparedirmodes(void)
725 struct modeinfo node;
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");
735 mf = fopen(modefile, "r");
738 fprintf(stderr, "cannot open mode file %s\n", modefile);
739 fprintf(stderr, "directory mode, owner, and times not set\n");
744 (void) fread((char *)&node, 1, sizeof(struct modeinfo), mf);
747 ep = lookupino(node.ino);
749 panic("cannot find directory inode %d\n", node.ino);
753 unsigned long newflags;
755 if (LSTAT(cp, &sb) < 0) {
756 warn("%s: does not exist", cp);
761 Vprintf(stdout, "comparing directory %s\n", cp);
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);
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);
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);
779 if (lgetflags(cp, &newflags) < 0) {
780 if (node.flags != 0) {
781 warn("%s: lgetflags failed", cp);
786 if (newflags != node.flags) {
787 fprintf(stderr, "%s: flags changed from 0x%08x to 0x%08lx.\n",
788 cp, node.flags, newflags);
797 panic("error setting directory modes\n");
802 * Generate a literal copy of a directory.
805 genliteraldir(char *name, dump_ino_t ino)
808 int ofile, dp, i, size;
811 itp = inotablookup(ino);
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);
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);
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);
839 * Determine the type of an inode
842 inodetype(dump_ino_t ino)
846 itp = inotablookup(ino);
853 * Allocate and initialize a directory inode entry.
854 * If requested, save its pertinent mode, owner, and time info.
856 static struct inotab *
857 #if defined(__linux__) || defined(sunos)
858 allocinotab(dump_ino_t ino, OFF_T seekpt)
860 allocinotab(dump_ino_t ino, OFF_T seekpt)
865 itp = calloc(1, sizeof(struct inotab));
867 panic("no memory directory table\n");
868 itp->t_next = inotab[INOHASH(ino)];
869 inotab[INOHASH(ino)] = itp;
871 itp->t_seekpt = seekpt;
876 #if defined(__linux__) || defined(sunos)
877 savemodeinfo(dump_ino_t ino, struct new_bsd_inode *dip) {
879 savemodeinfo(dump_ino_t ino, struct dinode *dip) {
881 struct modeinfo node;
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);
906 * Look up an inode in the table of directories
908 static struct inotab *
909 inotablookup(dump_ino_t ino)
913 for (itp = inotab[INOHASH(ino)]; itp != NULL; itp = itp->t_next)
914 if (itp->t_ino == ino)
926 if (modefile[0] != '#')
927 (void) unlink(modefile);
928 if (dirfile[0] != '#')
929 (void) unlink(dirfile);