2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 static char sccsid[] = "@(#)dirs.c 8.7 (Berkeley) 5/1/95";
39 static const char rcsid[] =
43 #include <sys/param.h>
48 #include <ufs/ufs/dinode.h>
49 #include <ufs/ufs/dir.h>
50 #include <protocols/dumprestore.h>
66 * Symbol table of directories read from tape.
69 #define INOHASH(val) (val % HASHSIZE)
71 struct inotab *t_next;
76 static struct inotab *inotab[HASHSIZE];
79 * Information retained about directories.
83 struct timeval ctimep[2];
84 struct timeval mtimep[2];
93 * Definitions for library routines operating on directories.
96 #define DIRBLKSIZ 1024
101 char dd_buf[DIRBLKSIZ];
105 * Global variables for this file.
108 static FILE *df, *mf;
109 static RST_DIR *dirp;
110 static char dirfile[MAXPATHLEN] = "#"; /* No file */
111 static char modefile[MAXPATHLEN] = "#"; /* No file */
112 static char dot[2] = "."; /* So it can be modified */
114 static struct inotab *allocinotab(struct context *, long);
115 static void flushent(void);
116 static struct inotab *inotablookup(ino_t);
117 static RST_DIR *opendirfile(const char *);
118 static void putdir(char *, long);
119 static void putdirattrs(char *, long);
120 static void putent(struct direct *);
121 static void rst_seekdir(RST_DIR *, long, long);
122 static long rst_telldir(RST_DIR *);
123 static struct direct *searchdir(ino_t, char *);
124 static void fail_dirtmp(char *);
127 * Extract directory contents, building up a directory structure
128 * on disk for extraction by name.
129 * If genmode is requested, save mode, owner, and times for all
130 * directories on the tape.
133 extractdirs(int genmode)
136 struct direct nulldir;
140 vprintf(stdout, "Extract directories from tape\n");
141 if ((tmpdir = getenv("TMPDIR")) == NULL || tmpdir[0] == '\0')
143 (void) sprintf(dirfile, "%s/rstdir%jd", tmpdir, (intmax_t)dumpdate);
144 if (command != 'r' && command != 'R') {
145 (void) strcat(dirfile, "-XXXXXX");
146 fd = mkstemp(dirfile);
148 fd = open(dirfile, O_RDWR|O_CREAT|O_EXCL, 0666);
149 if (fd == -1 || (df = fdopen(fd, "w")) == NULL) {
152 warn("%s: cannot create directory database", dirfile);
156 (void) sprintf(modefile, "%s/rstmode%jd", tmpdir,
158 if (command != 'r' && command != 'R') {
159 (void) strcat(modefile, "-XXXXXX");
160 fd = mkstemp(modefile);
162 fd = open(modefile, O_RDWR|O_CREAT|O_EXCL, 0666);
163 if (fd == -1 || (mf = fdopen(fd, "w")) == NULL) {
166 warn("%s: cannot create modefile", modefile);
171 nulldir.d_type = DT_DIR;
172 nulldir.d_namlen = 1;
173 (void) strcpy(nulldir.d_name, "/");
174 nulldir.d_reclen = DIRSIZ(0, &nulldir);
176 curfile.name = "<directory file - name unknown>";
177 curfile.action = USING;
178 if (curfile.mode == 0 || (curfile.mode & IFMT) != IFDIR)
180 itp = allocinotab(&curfile, seekpt);
181 getfile(putdir, putdirattrs, xtrnull);
184 itp->t_size = seekpt - itp->t_seekpt;
187 fail_dirtmp(dirfile);
188 dirp = opendirfile(dirfile);
190 fprintf(stderr, "opendirfile: %s\n", strerror(errno));
191 if (mf != NULL && fclose(mf) != 0)
192 fail_dirtmp(modefile);
195 panic("Root directory is not on tape\n");
199 * skip over all the directories on the tape
205 while (curfile.ino && (curfile.mode & IFMT) == IFDIR) {
211 * Recursively find names and inumbers of all files in subtree
212 * pname and pass them off to be processed.
215 treescan(char *pname, ino_t ino, long (*todo)(char *, ino_t, int))
221 char locname[MAXPATHLEN];
223 itp = inotablookup(ino);
226 * Pname is name of a simple file or an unchanged directory.
228 (void) (*todo)(pname, ino, LEAF);
232 * Pname is a dumped directory name.
234 if ((*todo)(pname, ino, NODE) == FAIL)
237 * begin search through the directory
238 * skipping over "." and ".."
240 (void) strlcpy(locname, pname, sizeof(locname));
241 (void) strlcat(locname, "/", sizeof(locname));
242 namelen = strlen(locname);
243 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
244 dp = rst_readdir(dirp); /* "." */
245 if (dp != NULL && strcmp(dp->d_name, ".") == 0)
246 dp = rst_readdir(dirp); /* ".." */
248 fprintf(stderr, "Warning: `.' missing from directory %s\n",
250 if (dp != NULL && strcmp(dp->d_name, "..") == 0)
251 dp = rst_readdir(dirp); /* first real entry */
253 fprintf(stderr, "Warning: `..' missing from directory %s\n",
255 bpt = rst_telldir(dirp);
257 * a zero inode signals end of directory
260 locname[namelen] = '\0';
261 if (namelen + dp->d_namlen >= sizeof(locname)) {
262 fprintf(stderr, "%s%s: name exceeds %zu char\n",
263 locname, dp->d_name, sizeof(locname) - 1);
265 (void)strlcat(locname, dp->d_name, sizeof(locname));
266 treescan(locname, dp->d_ino, todo);
267 rst_seekdir(dirp, bpt, itp->t_seekpt);
269 dp = rst_readdir(dirp);
270 bpt = rst_telldir(dirp);
275 * Lookup a pathname which is always assumed to start from the ROOTINO.
278 pathsearch(const char *pathname)
282 char *path, *name, buffer[MAXPATHLEN];
284 strcpy(buffer, pathname);
290 while ((name = strsep(&path, "/")) != NULL && *name != '\0') {
291 if ((dp = searchdir(ino, name)) == NULL)
299 * Lookup the requested name in directory inum.
300 * Return its inode number if found, zero if it does not exist.
302 static struct direct *
303 searchdir(ino_t inum, char *name)
309 itp = inotablookup(inum);
312 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
315 dp = rst_readdir(dirp);
318 } while (dp->d_namlen != len || strncmp(dp->d_name, name, len) != 0);
323 * Put the directory entries in the directory file
326 putdir(char *buf, long size)
331 for (loc = 0; loc < size; ) {
332 dp = (struct direct *)(buf + loc);
334 swabst((u_char *)"ls", (u_char *) dp);
335 if (oldinofmt && dp->d_ino != 0) {
336 #if BYTE_ORDER == BIG_ENDIAN
338 dp->d_namlen = dp->d_type;
340 if (!Bcvt && dp->d_namlen == 0)
341 dp->d_namlen = dp->d_type;
343 dp->d_type = DT_UNKNOWN;
345 i = DIRBLKSIZ - (loc & (DIRBLKSIZ - 1));
346 if ((dp->d_reclen & 0x3) != 0 ||
348 dp->d_reclen < DIRSIZ(0, dp)
350 || dp->d_namlen > NAME_MAX
353 vprintf(stdout, "Mangled directory: ");
354 if ((dp->d_reclen & 0x3) != 0)
356 "reclen not multiple of 4 ");
357 if (dp->d_reclen < DIRSIZ(0, dp))
359 "reclen less than DIRSIZ (%d < %zu) ",
360 dp->d_reclen, DIRSIZ(0, dp));
362 if (dp->d_namlen > NAME_MAX)
364 "reclen name too big (%d > %d) ",
365 dp->d_namlen, NAME_MAX);
367 vprintf(stdout, "\n");
372 if (dp->d_ino != 0) {
379 * These variables are "local" to the following two functions.
381 char dirbuf[DIRBLKSIZ];
386 * add a new directory entry to a file.
389 putent(struct direct *dp)
391 dp->d_reclen = DIRSIZ(0, dp);
392 if (dirloc + dp->d_reclen > DIRBLKSIZ) {
393 ((struct direct *)(dirbuf + prev))->d_reclen =
395 if (fwrite(dirbuf, DIRBLKSIZ, 1, df) != 1)
396 fail_dirtmp(dirfile);
399 memmove(dirbuf + dirloc, dp, (long)dp->d_reclen);
401 dirloc += dp->d_reclen;
405 * flush out a directory that is finished.
410 ((struct direct *)(dirbuf + prev))->d_reclen = DIRBLKSIZ - prev;
411 if (fwrite(dirbuf, (int)dirloc, 1, df) != 1)
412 fail_dirtmp(dirfile);
418 * Save extended attributes for a directory entry to a file.
421 putdirattrs(char *buf, long size)
424 if (mf != NULL && fwrite(buf, size, 1, mf) != 1)
425 fail_dirtmp(modefile);
429 * Seek to an entry in a directory.
430 * Only values returned by rst_telldir should be passed to rst_seekdir.
431 * This routine handles many directories in a single file.
432 * It takes the base of the directory in the file, plus
433 * the desired seek offset into it.
436 rst_seekdir(RST_DIR *dirp, long loc, long base)
439 if (loc == rst_telldir(dirp))
443 fprintf(stderr, "bad seek pointer to rst_seekdir %ld\n", loc);
444 (void) lseek(dirp->dd_fd, base + (loc & ~(DIRBLKSIZ - 1)), SEEK_SET);
445 dirp->dd_loc = loc & (DIRBLKSIZ - 1);
446 if (dirp->dd_loc != 0)
447 dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ);
451 * get next entry in a directory.
454 rst_readdir(RST_DIR *dirp)
459 if (dirp->dd_loc == 0) {
460 dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf,
462 if (dirp->dd_size <= 0) {
463 dprintf(stderr, "error reading directory\n");
467 if (dirp->dd_loc >= dirp->dd_size) {
471 dp = (struct direct *)(dirp->dd_buf + dirp->dd_loc);
472 if (dp->d_reclen == 0 ||
473 dp->d_reclen > DIRBLKSIZ + 1 - dirp->dd_loc) {
474 dprintf(stderr, "corrupted directory: bad reclen %d\n",
478 dirp->dd_loc += dp->d_reclen;
479 if (dp->d_ino == 0 && strcmp(dp->d_name, "/") == 0)
481 if (dp->d_ino >= maxino) {
482 dprintf(stderr, "corrupted directory: bad inum %d\n",
491 * Simulate the opening of a directory
494 rst_opendir(const char *name)
500 if ((ino = dirlookup(name)) > 0 &&
501 (itp = inotablookup(ino)) != NULL) {
502 dirp = opendirfile(dirfile);
503 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
510 * In our case, there is nothing to do when closing a directory.
513 rst_closedir(void *arg)
518 (void)close(dirp->dd_fd);
524 * Simulate finding the current offset in the directory.
527 rst_telldir(RST_DIR *dirp)
529 return ((long)lseek(dirp->dd_fd,
530 (off_t)0, SEEK_CUR) - dirp->dd_size + dirp->dd_loc);
534 * Open a directory file.
537 opendirfile(const char *name)
542 if ((fd = open(name, O_RDONLY)) == -1)
544 if ((dirp = malloc(sizeof(RST_DIR))) == NULL) {
554 * Set the mode, owner, and times for all new or changed directories
557 setdirmodes(int flags)
560 struct modeinfo node;
567 vprintf(stdout, "Set directory mode, owner, and times.\n");
568 if ((tmpdir = getenv("TMPDIR")) == NULL || tmpdir[0] == '\0')
570 if (command == 'r' || command == 'R')
571 (void) sprintf(modefile, "%s/rstmode%jd", tmpdir,
573 if (modefile[0] == '#') {
574 panic("modefile not defined\n");
575 fprintf(stderr, "directory mode, owner, and times not set\n");
578 mf = fopen(modefile, "r");
580 fprintf(stderr, "fopen: %s\n", strerror(errno));
581 fprintf(stderr, "cannot open mode file %s\n", modefile);
582 fprintf(stderr, "directory mode, owner, and times not set\n");
589 (void) fread((char *)&node, 1, sizeof(struct modeinfo), mf);
591 warn("%s: cannot read modefile.", modefile);
592 fprintf(stderr, "Mode, owner, and times not set.\n");
597 if (node.extsize > 0) {
598 if (bufsize < node.extsize) {
601 if ((buf = malloc(node.extsize)) != 0) {
602 bufsize = node.extsize;
607 if (bufsize >= node.extsize) {
608 (void) fread(buf, 1, node.extsize, mf);
610 warn("%s: cannot read modefile.",
612 fprintf(stderr, "Not all external ");
613 fprintf(stderr, "attributes set.\n");
617 (void) fseek(mf, node.extsize, SEEK_CUR);
619 warn("%s: cannot seek in modefile.",
621 fprintf(stderr, "Not all directory ");
622 fprintf(stderr, "attributes set.\n");
627 ep = lookupino(node.ino);
628 if (command == 'i' || command == 'x') {
631 if ((flags & FORCE) == 0 && ep->e_flags & EXISTED) {
635 if (node.ino == ROOTINO &&
636 reply("set owner/mode for '.'") == FAIL)
640 panic("cannot find directory inode %ju\n",
641 (uintmax_t)node.ino);
646 if (node.extsize > 0) {
647 if (bufsize >= node.extsize) {
648 set_extattr_file(cp, buf, node.extsize);
650 fprintf(stderr, "Cannot restore %s%s\n",
651 "extended attributes for ", cp);
655 (void) chown(cp, myuid, node.gid);
657 (void) chown(cp, node.uid, node.gid);
658 (void) chmod(cp, node.mode);
659 utimes(cp, node.ctimep);
660 utimes(cp, node.mtimep);
661 (void) chflags(cp, node.flags);
671 * Generate a literal copy of a directory.
674 genliteraldir(char *name, ino_t ino)
677 int ofile, dp, i, size;
680 itp = inotablookup(ino);
682 panic("Cannot find directory inode %ju named %s\n",
683 (uintmax_t)ino, name);
684 if ((ofile = open(name, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
685 fprintf(stderr, "%s: ", name);
686 (void) fflush(stderr);
687 fprintf(stderr, "cannot create file: %s\n", strerror(errno));
690 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
691 dp = dup(dirp->dd_fd);
692 for (i = itp->t_size; i > 0; i -= BUFSIZ) {
693 size = i < BUFSIZ ? i : BUFSIZ;
694 if (read(dp, buf, (int) size) == -1) {
696 "write error extracting inode %ju, name %s\n",
697 (uintmax_t)curfile.ino, curfile.name);
698 fprintf(stderr, "read: %s\n", strerror(errno));
701 if (!Nflag && write(ofile, buf, (int) size) == -1) {
703 "write error extracting inode %ju, name %s\n",
704 (uintmax_t)curfile.ino, curfile.name);
705 fprintf(stderr, "write: %s\n", strerror(errno));
715 * Determine the type of an inode
722 itp = inotablookup(ino);
729 * Allocate and initialize a directory inode entry.
730 * If requested, save its pertinent mode, owner, and time info.
732 static struct inotab *
733 allocinotab(struct context *ctxp, long seekpt)
736 struct modeinfo node;
738 itp = calloc(1, sizeof(struct inotab));
740 panic("no memory for directory table\n");
741 itp->t_next = inotab[INOHASH(ctxp->ino)];
742 inotab[INOHASH(ctxp->ino)] = itp;
743 itp->t_ino = ctxp->ino;
744 itp->t_seekpt = seekpt;
747 node.ino = ctxp->ino;
748 node.mtimep[0].tv_sec = ctxp->atime_sec;
749 node.mtimep[0].tv_usec = ctxp->atime_nsec / 1000;
750 node.mtimep[1].tv_sec = ctxp->mtime_sec;
751 node.mtimep[1].tv_usec = ctxp->mtime_nsec / 1000;
752 node.ctimep[0].tv_sec = ctxp->atime_sec;
753 node.ctimep[0].tv_usec = ctxp->atime_nsec / 1000;
754 node.ctimep[1].tv_sec = ctxp->birthtime_sec;
755 node.ctimep[1].tv_usec = ctxp->birthtime_nsec / 1000;
756 node.extsize = ctxp->extsize;
757 node.mode = ctxp->mode;
758 node.flags = ctxp->file_flags;
759 node.uid = ctxp->uid;
760 node.gid = ctxp->gid;
761 if (fwrite((char *)&node, sizeof(struct modeinfo), 1, mf) != 1)
762 fail_dirtmp(modefile);
767 * Look up an inode in the table of directories
769 static struct inotab *
770 inotablookup(ino_t ino)
774 for (itp = inotab[INOHASH(ino)]; itp != NULL; itp = itp->t_next)
775 if (itp->t_ino == ino)
788 if (modefile[0] != '#') {
789 (void) truncate(modefile, 0);
790 (void) unlink(modefile);
792 if (dirfile[0] != '#') {
793 (void) truncate(dirfile, 0);
794 (void) unlink(dirfile);
800 * Print out information about the failure to save directory,
801 * extended attribute, and mode information.
804 fail_dirtmp(char *filename)
808 warn("%s: cannot write directory database", filename);
809 if (errno == ENOSPC) {
810 if ((tmpdir = getenv("TMPDIR")) == NULL || tmpdir[0] == '\0')
812 fprintf(stderr, "Try making space in %s, %s\n%s\n", tmpdir,
813 "or set environment variable TMPDIR",
814 "to an alternate location with more disk space.");