2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 static char sccsid[] = "@(#)dirs.c 8.7 (Berkeley) 5/1/95";
41 static const char rcsid[] =
45 #include <sys/param.h>
50 #include <ufs/ufs/dinode.h>
51 #include <ufs/ufs/dir.h>
52 #include <protocols/dumprestore.h>
68 * Symbol table of directories read from tape.
71 #define INOHASH(val) (val % HASHSIZE)
73 struct inotab *t_next;
78 static struct inotab *inotab[HASHSIZE];
81 * Information retained about directories.
85 struct timespec ctimep[2];
86 struct timespec mtimep[2];
95 * Definitions for library routines operating on directories.
98 #define DIRBLKSIZ 1024
103 char dd_buf[DIRBLKSIZ];
107 * Global variables for this file.
110 static FILE *df, *mf;
111 static RST_DIR *dirp;
112 static char dirfile[MAXPATHLEN] = "#"; /* No file */
113 static char modefile[MAXPATHLEN] = "#"; /* No file */
114 static char dot[2] = "."; /* So it can be modified */
116 static struct inotab *allocinotab(struct context *, long);
117 static void flushent(void);
118 static struct inotab *inotablookup(ino_t);
119 static RST_DIR *opendirfile(const char *);
120 static void putdir(char *, size_t);
121 static void putdirattrs(char *, size_t);
122 static void putent(struct direct *);
123 static void rst_seekdir(RST_DIR *, long, long);
124 static long rst_telldir(RST_DIR *);
125 static struct direct *searchdir(ino_t, char *);
126 static void fail_dirtmp(char *);
129 * Extract directory contents, building up a directory structure
130 * on disk for extraction by name.
131 * If genmode is requested, save mode, owner, and times for all
132 * directories on the tape.
135 extractdirs(int genmode)
138 struct direct nulldir;
142 vprintf(stdout, "Extract directories from tape\n");
143 if ((tmpdir = getenv("TMPDIR")) == NULL || tmpdir[0] == '\0')
145 (void) snprintf(dirfile, sizeof(dirfile), "%s/rstdir%jd", tmpdir,
147 if (command != 'r' && command != 'R') {
148 (void) strcat(dirfile, "-XXXXXX");
149 fd = mkstemp(dirfile);
151 fd = open(dirfile, O_RDWR|O_CREAT|O_EXCL, 0666);
152 if (fd == -1 || (df = fdopen(fd, "w")) == NULL) {
155 warn("%s: cannot create directory database", dirfile);
159 (void) snprintf(modefile, sizeof(modefile), "%s/rstmode%jd",
160 tmpdir, (intmax_t)dumpdate);
161 if (command != 'r' && command != 'R') {
162 (void) strcat(modefile, "-XXXXXX");
163 fd = mkstemp(modefile);
165 fd = open(modefile, O_RDWR|O_CREAT|O_EXCL, 0666);
166 if (fd == -1 || (mf = fdopen(fd, "w")) == NULL) {
169 warn("%s: cannot create modefile", modefile);
174 nulldir.d_type = DT_DIR;
175 nulldir.d_namlen = 1;
176 (void) strcpy(nulldir.d_name, "/");
177 nulldir.d_reclen = DIRSIZ(0, &nulldir);
179 curfile.name = "<directory file - name unknown>";
180 curfile.action = USING;
181 if (curfile.mode == 0 || (curfile.mode & IFMT) != IFDIR)
183 itp = allocinotab(&curfile, seekpt);
184 getfile(putdir, putdirattrs, xtrnull);
187 itp->t_size = seekpt - itp->t_seekpt;
190 fail_dirtmp(dirfile);
191 dirp = opendirfile(dirfile);
193 fprintf(stderr, "opendirfile: %s\n", strerror(errno));
194 if (mf != NULL && fclose(mf) != 0)
195 fail_dirtmp(modefile);
198 panic("Root directory is not on tape\n");
202 * skip over all the directories on the tape
208 while (curfile.ino && (curfile.mode & IFMT) == IFDIR) {
214 * Recursively find names and inumbers of all files in subtree
215 * pname and pass them off to be processed.
218 treescan(char *pname, ino_t ino, long (*todo)(char *, ino_t, int))
224 char locname[MAXPATHLEN];
226 itp = inotablookup(ino);
229 * Pname is name of a simple file or an unchanged directory.
231 (void) (*todo)(pname, ino, LEAF);
235 * Pname is a dumped directory name.
237 if ((*todo)(pname, ino, NODE) == FAIL)
240 * begin search through the directory
241 * skipping over "." and ".."
243 (void) strlcpy(locname, pname, sizeof(locname));
244 (void) strlcat(locname, "/", sizeof(locname));
245 namelen = strlen(locname);
246 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
247 dp = rst_readdir(dirp); /* "." */
248 if (dp != NULL && strcmp(dp->d_name, ".") == 0)
249 dp = rst_readdir(dirp); /* ".." */
251 fprintf(stderr, "Warning: `.' missing from directory %s\n",
253 if (dp != NULL && strcmp(dp->d_name, "..") == 0)
254 dp = rst_readdir(dirp); /* first real entry */
256 fprintf(stderr, "Warning: `..' missing from directory %s\n",
258 bpt = rst_telldir(dirp);
260 * a zero inode signals end of directory
263 locname[namelen] = '\0';
264 if (namelen + dp->d_namlen >= sizeof(locname)) {
265 fprintf(stderr, "%s%s: name exceeds %zu char\n",
266 locname, dp->d_name, sizeof(locname) - 1);
268 (void)strlcat(locname, dp->d_name, sizeof(locname));
269 treescan(locname, dp->d_ino, todo);
270 rst_seekdir(dirp, bpt, itp->t_seekpt);
272 dp = rst_readdir(dirp);
273 bpt = rst_telldir(dirp);
278 * Lookup a pathname which is always assumed to start from the UFS_ROOTINO.
281 pathsearch(const char *pathname)
285 char *path, *name, buffer[MAXPATHLEN];
287 strcpy(buffer, pathname);
293 while ((name = strsep(&path, "/")) != NULL && *name != '\0') {
294 if ((dp = searchdir(ino, name)) == NULL)
302 * Lookup the requested name in directory inum.
303 * Return its inode number if found, zero if it does not exist.
305 static struct direct *
306 searchdir(ino_t inum, char *name)
312 itp = inotablookup(inum);
315 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
318 dp = rst_readdir(dirp);
321 } while (dp->d_namlen != len || strncmp(dp->d_name, name, len) != 0);
326 * Put the directory entries in the directory file
329 putdir(char *buf, size_t size)
334 for (loc = 0; loc < size; ) {
335 dp = (struct direct *)(buf + loc);
337 swabst((u_char *)"ls", (u_char *) dp);
338 if (oldinofmt && dp->d_ino != 0) {
339 #if BYTE_ORDER == BIG_ENDIAN
341 dp->d_namlen = dp->d_type;
343 if (!Bcvt && dp->d_namlen == 0)
344 dp->d_namlen = dp->d_type;
346 dp->d_type = DT_UNKNOWN;
348 i = DIRBLKSIZ - (loc & (DIRBLKSIZ - 1));
349 if ((dp->d_reclen & 0x3) != 0 ||
351 dp->d_reclen < DIRSIZ(0, dp)
353 || dp->d_namlen > NAME_MAX
356 vprintf(stdout, "Mangled directory: ");
357 if ((dp->d_reclen & 0x3) != 0)
359 "reclen not multiple of 4 ");
360 if (dp->d_reclen < DIRSIZ(0, dp))
362 "reclen less than DIRSIZ (%u < %zu) ",
363 dp->d_reclen, DIRSIZ(0, dp));
365 if (dp->d_namlen > NAME_MAX)
367 "reclen name too big (%u > %u) ",
368 dp->d_namlen, NAME_MAX);
370 vprintf(stdout, "\n");
375 if (dp->d_ino != 0) {
382 * These variables are "local" to the following two functions.
384 char dirbuf[DIRBLKSIZ];
389 * add a new directory entry to a file.
392 putent(struct direct *dp)
394 dp->d_reclen = DIRSIZ(0, dp);
395 if (dirloc + dp->d_reclen > DIRBLKSIZ) {
396 ((struct direct *)(dirbuf + prev))->d_reclen =
398 if (fwrite(dirbuf, DIRBLKSIZ, 1, df) != 1)
399 fail_dirtmp(dirfile);
402 memmove(dirbuf + dirloc, dp, (long)dp->d_reclen);
404 dirloc += dp->d_reclen;
408 * flush out a directory that is finished.
413 ((struct direct *)(dirbuf + prev))->d_reclen = DIRBLKSIZ - prev;
414 if (fwrite(dirbuf, (int)dirloc, 1, df) != 1)
415 fail_dirtmp(dirfile);
421 * Save extended attributes for a directory entry to a file.
424 putdirattrs(char *buf, size_t size)
427 if (mf != NULL && fwrite(buf, size, 1, mf) != 1)
428 fail_dirtmp(modefile);
432 * Seek to an entry in a directory.
433 * Only values returned by rst_telldir should be passed to rst_seekdir.
434 * This routine handles many directories in a single file.
435 * It takes the base of the directory in the file, plus
436 * the desired seek offset into it.
439 rst_seekdir(RST_DIR *dirp, long loc, long base)
442 if (loc == rst_telldir(dirp))
446 fprintf(stderr, "bad seek pointer to rst_seekdir %ld\n", loc);
447 (void) lseek(dirp->dd_fd, base + rounddown2(loc, DIRBLKSIZ), SEEK_SET);
448 dirp->dd_loc = loc & (DIRBLKSIZ - 1);
449 if (dirp->dd_loc != 0)
450 dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ);
454 * get next entry in a directory.
457 rst_readdir(RST_DIR *dirp)
462 if (dirp->dd_loc == 0) {
463 dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf,
465 if (dirp->dd_size <= 0) {
466 dprintf(stderr, "error reading directory\n");
470 if (dirp->dd_loc >= dirp->dd_size) {
474 dp = (struct direct *)(dirp->dd_buf + dirp->dd_loc);
475 if (dp->d_reclen == 0 ||
476 dp->d_reclen > DIRBLKSIZ + 1 - dirp->dd_loc) {
477 dprintf(stderr, "corrupted directory: bad reclen %d\n",
481 dirp->dd_loc += dp->d_reclen;
482 if (dp->d_ino == 0 && strcmp(dp->d_name, "/") == 0)
484 if (dp->d_ino >= maxino) {
485 dprintf(stderr, "corrupted directory: bad inum %d\n",
494 * Simulate the opening of a directory
497 rst_opendir(const char *name)
503 if ((ino = dirlookup(name)) > 0 &&
504 (itp = inotablookup(ino)) != NULL) {
505 dirp = opendirfile(dirfile);
506 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
513 * In our case, there is nothing to do when closing a directory.
516 rst_closedir(void *arg)
521 (void)close(dirp->dd_fd);
527 * Simulate finding the current offset in the directory.
530 rst_telldir(RST_DIR *dirp)
532 return ((long)lseek(dirp->dd_fd,
533 (off_t)0, SEEK_CUR) - dirp->dd_size + dirp->dd_loc);
537 * Open a directory file.
540 opendirfile(const char *name)
545 if ((fd = open(name, O_RDONLY)) == -1)
547 if ((dirp = malloc(sizeof(RST_DIR))) == NULL) {
557 * Set the mode, owner, and times for all new or changed directories
560 setdirmodes(int flags)
563 struct modeinfo node;
570 vprintf(stdout, "Set directory mode, owner, and times.\n");
571 if ((tmpdir = getenv("TMPDIR")) == NULL || tmpdir[0] == '\0')
573 if (command == 'r' || command == 'R')
574 (void) snprintf(modefile, sizeof(modefile), "%s/rstmode%jd",
575 tmpdir, (intmax_t)dumpdate);
576 if (modefile[0] == '#') {
577 panic("modefile not defined\n");
578 fprintf(stderr, "directory mode, owner, and times not set\n");
581 mf = fopen(modefile, "r");
583 fprintf(stderr, "fopen: %s\n", strerror(errno));
584 fprintf(stderr, "cannot open mode file %s\n", modefile);
585 fprintf(stderr, "directory mode, owner, and times not set\n");
592 (void) fread((char *)&node, 1, sizeof(struct modeinfo), mf);
594 warn("%s: cannot read modefile.", modefile);
595 fprintf(stderr, "Mode, owner, and times not set.\n");
600 if (node.extsize > 0) {
601 if (bufsize < node.extsize) {
604 if ((buf = malloc(node.extsize)) != NULL) {
605 bufsize = node.extsize;
610 if (bufsize >= node.extsize) {
611 (void) fread(buf, 1, node.extsize, mf);
613 warn("%s: cannot read modefile.",
615 fprintf(stderr, "Not all external ");
616 fprintf(stderr, "attributes set.\n");
620 (void) fseek(mf, node.extsize, SEEK_CUR);
622 warn("%s: cannot seek in modefile.",
624 fprintf(stderr, "Not all directory ");
625 fprintf(stderr, "attributes set.\n");
630 ep = lookupino(node.ino);
631 if (command == 'i' || command == 'x') {
634 if ((flags & FORCE) == 0 && ep->e_flags & EXISTED) {
638 if (node.ino == UFS_ROOTINO &&
639 reply("set owner/mode for '.'") == FAIL)
643 panic("cannot find directory inode %ju\n",
644 (uintmax_t)node.ino);
649 if (node.extsize > 0) {
650 if (bufsize >= node.extsize) {
651 set_extattr(-1, cp, buf, node.extsize, SXA_FILE);
653 fprintf(stderr, "Cannot restore %s%s\n",
654 "extended attributes for ", cp);
658 (void) chown(cp, myuid, node.gid);
660 (void) chown(cp, node.uid, node.gid);
661 (void) chmod(cp, node.mode);
662 utimensat(AT_FDCWD, cp, node.ctimep, 0);
663 utimensat(AT_FDCWD, cp, node.mtimep, 0);
664 (void) chflags(cp, node.flags);
674 * Generate a literal copy of a directory.
677 genliteraldir(char *name, ino_t ino)
680 int ofile, dp, i, size;
683 itp = inotablookup(ino);
685 panic("Cannot find directory inode %ju named %s\n",
686 (uintmax_t)ino, name);
687 if ((ofile = open(name, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
688 fprintf(stderr, "%s: ", name);
689 (void) fflush(stderr);
690 fprintf(stderr, "cannot create file: %s\n", strerror(errno));
693 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
694 dp = dup(dirp->dd_fd);
695 for (i = itp->t_size; i > 0; i -= BUFSIZ) {
696 size = MIN(i, BUFSIZ);
697 if (read(dp, buf, (int) size) == -1) {
699 "write error extracting inode %ju, name %s\n",
700 (uintmax_t)curfile.ino, curfile.name);
701 fprintf(stderr, "read: %s\n", strerror(errno));
704 if (!Nflag && write(ofile, buf, (int) size) == -1) {
706 "write error extracting inode %ju, name %s\n",
707 (uintmax_t)curfile.ino, curfile.name);
708 fprintf(stderr, "write: %s\n", strerror(errno));
718 * Determine the type of an inode
725 itp = inotablookup(ino);
732 * Allocate and initialize a directory inode entry.
733 * If requested, save its pertinent mode, owner, and time info.
735 static struct inotab *
736 allocinotab(struct context *ctxp, long seekpt)
739 struct modeinfo node;
741 itp = calloc(1, sizeof(struct inotab));
743 panic("no memory for directory table\n");
744 itp->t_next = inotab[INOHASH(ctxp->ino)];
745 inotab[INOHASH(ctxp->ino)] = itp;
746 itp->t_ino = ctxp->ino;
747 itp->t_seekpt = seekpt;
750 node.ino = ctxp->ino;
751 node.mtimep[0].tv_sec = ctxp->atime_sec;
752 node.mtimep[0].tv_nsec = ctxp->atime_nsec;
753 node.mtimep[1].tv_sec = ctxp->mtime_sec;
754 node.mtimep[1].tv_nsec = ctxp->mtime_nsec;
755 node.ctimep[0].tv_sec = ctxp->atime_sec;
756 node.ctimep[0].tv_nsec = ctxp->atime_nsec;
757 node.ctimep[1].tv_sec = ctxp->birthtime_sec;
758 node.ctimep[1].tv_nsec = ctxp->birthtime_nsec;
759 node.extsize = ctxp->extsize;
760 node.mode = ctxp->mode;
761 node.flags = ctxp->file_flags;
762 node.uid = ctxp->uid;
763 node.gid = ctxp->gid;
764 if (fwrite((char *)&node, sizeof(struct modeinfo), 1, mf) != 1)
765 fail_dirtmp(modefile);
770 * Look up an inode in the table of directories
772 static struct inotab *
773 inotablookup(ino_t ino)
777 for (itp = inotab[INOHASH(ino)]; itp != NULL; itp = itp->t_next)
778 if (itp->t_ino == ino)
791 if (modefile[0] != '#') {
792 (void) truncate(modefile, 0);
793 (void) unlink(modefile);
795 if (dirfile[0] != '#') {
796 (void) truncate(dirfile, 0);
797 (void) unlink(dirfile);
803 * Print out information about the failure to save directory,
804 * extended attribute, and mode information.
807 fail_dirtmp(char *filename)
811 warn("%s: cannot write directory database", filename);
812 if (errno == ENOSPC) {
813 if ((tmpdir = getenv("TMPDIR")) == NULL || tmpdir[0] == '\0')
815 fprintf(stderr, "Try making space in %s, %s\n%s\n", tmpdir,
816 "or set environment variable TMPDIR",
817 "to an alternate location with more disk space.");