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>
65 * Symbol table of directories read from tape.
68 #define INOHASH(val) (val % HASHSIZE)
70 struct inotab *t_next;
75 static struct inotab *inotab[HASHSIZE];
78 * Information retained about directories.
82 struct timeval ctimep[2];
83 struct timeval mtimep[2];
91 * Definitions for library routines operating on directories.
94 #define DIRBLKSIZ 1024
99 char dd_buf[DIRBLKSIZ];
103 * Global variables for this file.
106 static FILE *df, *mf;
107 static RST_DIR *dirp;
108 static char dirfile[MAXPATHLEN] = "#"; /* No file */
109 static char modefile[MAXPATHLEN] = "#"; /* No file */
110 static char dot[2] = "."; /* So it can be modified */
112 static struct inotab *allocinotab(struct context *, long);
113 static void flushent(void);
114 static struct inotab *inotablookup(ino_t);
115 static RST_DIR *opendirfile(const char *);
116 static void putdir(char *, long);
117 static void putent(struct direct *);
118 static void rst_seekdir(RST_DIR *, long, long);
119 static long rst_telldir(RST_DIR *);
120 static struct direct *searchdir(ino_t, char *);
123 * Extract directory contents, building up a directory structure
124 * on disk for extraction by name.
125 * If genmode is requested, save mode, owner, and times for all
126 * directories on the tape.
129 extractdirs(int genmode)
132 struct direct nulldir;
136 vprintf(stdout, "Extract directories from tape\n");
137 if ((tmpdir = getenv("TMPDIR")) == NULL || tmpdir[0] == '\0')
139 (void) sprintf(dirfile, "%s/rstdir%d", tmpdir, dumpdate);
140 if (command != 'r' && command != 'R') {
141 (void *) strcat(dirfile, "-XXXXXX");
142 fd = mkstemp(dirfile);
144 fd = open(dirfile, O_RDWR|O_CREAT|O_EXCL, 0666);
145 if (fd == -1 || (df = fdopen(fd, "w")) == NULL) {
148 warn("%s - cannot create directory temporary\nfopen", dirfile);
152 (void) sprintf(modefile, "%s/rstmode%d", tmpdir, dumpdate);
153 if (command != 'r' && command != 'R') {
154 (void *) strcat(modefile, "-XXXXXX");
155 fd = mkstemp(modefile);
157 fd = open(modefile, O_RDWR|O_CREAT|O_EXCL, 0666);
158 if (fd == -1 || (mf = fdopen(fd, "w")) == NULL) {
161 warn("%s - cannot create modefile\nfopen", modefile);
166 nulldir.d_type = DT_DIR;
167 nulldir.d_namlen = 1;
168 (void) strcpy(nulldir.d_name, "/");
169 nulldir.d_reclen = DIRSIZ(0, &nulldir);
171 curfile.name = "<directory file - name unknown>";
172 curfile.action = USING;
173 if (curfile.mode == 0 || (curfile.mode & IFMT) != IFDIR) {
175 dirp = opendirfile(dirfile);
177 fprintf(stderr, "opendirfile: %s\n",
183 panic("Root directory is not on tape\n");
186 itp = allocinotab(&curfile, seekpt);
187 getfile(putdir, xtrnull);
190 itp->t_size = seekpt - itp->t_seekpt;
195 * skip over all the directories on the tape
201 while (curfile.ino && (curfile.mode & IFMT) == IFDIR) {
207 * Recursively find names and inumbers of all files in subtree
208 * pname and pass them off to be processed.
211 treescan(char *pname, ino_t ino, long (*todo)(char *, ino_t, int))
217 char locname[MAXPATHLEN + 1];
219 itp = inotablookup(ino);
222 * Pname is name of a simple file or an unchanged directory.
224 (void) (*todo)(pname, ino, LEAF);
228 * Pname is a dumped directory name.
230 if ((*todo)(pname, ino, NODE) == FAIL)
233 * begin search through the directory
234 * skipping over "." and ".."
236 (void) strncpy(locname, pname, sizeof(locname) - 1);
237 locname[sizeof(locname) - 1] = '\0';
238 (void) strncat(locname, "/", sizeof(locname) - strlen(locname));
239 namelen = strlen(locname);
240 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
241 dp = rst_readdir(dirp); /* "." */
242 if (dp != NULL && strcmp(dp->d_name, ".") == 0)
243 dp = rst_readdir(dirp); /* ".." */
245 fprintf(stderr, "Warning: `.' missing from directory %s\n",
247 if (dp != NULL && strcmp(dp->d_name, "..") == 0)
248 dp = rst_readdir(dirp); /* first real entry */
250 fprintf(stderr, "Warning: `..' missing from directory %s\n",
252 bpt = rst_telldir(dirp);
254 * a zero inode signals end of directory
257 locname[namelen] = '\0';
258 if (namelen + dp->d_namlen >= sizeof(locname)) {
259 fprintf(stderr, "%s%s: name exceeds %d char\n",
260 locname, dp->d_name, sizeof(locname) - 1);
262 (void) strncat(locname, dp->d_name, (int)dp->d_namlen);
263 treescan(locname, dp->d_ino, todo);
264 rst_seekdir(dirp, bpt, itp->t_seekpt);
266 dp = rst_readdir(dirp);
267 bpt = rst_telldir(dirp);
272 * Lookup a pathname which is always assumed to start from the ROOTINO.
275 pathsearch(const char *pathname)
279 char *path, *name, buffer[MAXPATHLEN];
281 strcpy(buffer, pathname);
287 while ((name = strsep(&path, "/")) != NULL && *name != '\0') {
288 if ((dp = searchdir(ino, name)) == NULL)
296 * Lookup the requested name in directory inum.
297 * Return its inode number if found, zero if it does not exist.
299 static struct direct *
300 searchdir(ino_t inum, char *name)
306 itp = inotablookup(inum);
309 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
312 dp = rst_readdir(dirp);
315 } while (dp->d_namlen != len || strncmp(dp->d_name, name, len) != 0);
320 * Put the directory entries in the directory file
323 putdir(char *buf, long size)
328 for (loc = 0; loc < size; ) {
329 dp = (struct direct *)(buf + loc);
331 swabst((u_char *)"ls", (u_char *) dp);
332 if (oldinofmt && dp->d_ino != 0) {
333 #if BYTE_ORDER == BIG_ENDIAN
335 dp->d_namlen = dp->d_type;
337 if (!Bcvt && dp->d_namlen == 0)
338 dp->d_namlen = dp->d_type;
340 dp->d_type = DT_UNKNOWN;
342 i = DIRBLKSIZ - (loc & (DIRBLKSIZ - 1));
343 if ((dp->d_reclen & 0x3) != 0 ||
345 dp->d_reclen < DIRSIZ(0, dp)
347 || dp->d_namlen > NAME_MAX
350 vprintf(stdout, "Mangled directory: ");
351 if ((dp->d_reclen & 0x3) != 0)
353 "reclen not multiple of 4 ");
354 if (dp->d_reclen < DIRSIZ(0, dp))
356 "reclen less than DIRSIZ (%d < %d) ",
357 dp->d_reclen, DIRSIZ(0, dp));
359 if (dp->d_namlen > NAME_MAX)
361 "reclen name too big (%d > %d) ",
362 dp->d_namlen, NAME_MAX);
364 vprintf(stdout, "\n");
369 if (dp->d_ino != 0) {
376 * These variables are "local" to the following two functions.
378 char dirbuf[DIRBLKSIZ];
383 * add a new directory entry to a file.
386 putent(struct direct *dp)
388 dp->d_reclen = DIRSIZ(0, dp);
389 if (dirloc + dp->d_reclen > DIRBLKSIZ) {
390 ((struct direct *)(dirbuf + prev))->d_reclen =
392 (void) fwrite(dirbuf, 1, DIRBLKSIZ, df);
395 memmove(dirbuf + dirloc, dp, (long)dp->d_reclen);
397 dirloc += dp->d_reclen;
401 * flush out a directory that is finished.
406 ((struct direct *)(dirbuf + prev))->d_reclen = DIRBLKSIZ - prev;
407 (void) fwrite(dirbuf, (int)dirloc, 1, df);
413 * Seek to an entry in a directory.
414 * Only values returned by rst_telldir should be passed to rst_seekdir.
415 * This routine handles many directories in a single file.
416 * It takes the base of the directory in the file, plus
417 * the desired seek offset into it.
420 rst_seekdir(RST_DIR *dirp, long loc, long base)
423 if (loc == rst_telldir(dirp))
427 fprintf(stderr, "bad seek pointer to rst_seekdir %ld\n", loc);
428 (void) lseek(dirp->dd_fd, base + (loc & ~(DIRBLKSIZ - 1)), SEEK_SET);
429 dirp->dd_loc = loc & (DIRBLKSIZ - 1);
430 if (dirp->dd_loc != 0)
431 dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ);
435 * get next entry in a directory.
438 rst_readdir(RST_DIR *dirp)
443 if (dirp->dd_loc == 0) {
444 dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf,
446 if (dirp->dd_size <= 0) {
447 dprintf(stderr, "error reading directory\n");
451 if (dirp->dd_loc >= dirp->dd_size) {
455 dp = (struct direct *)(dirp->dd_buf + dirp->dd_loc);
456 if (dp->d_reclen == 0 ||
457 dp->d_reclen > DIRBLKSIZ + 1 - dirp->dd_loc) {
458 dprintf(stderr, "corrupted directory: bad reclen %d\n",
462 dirp->dd_loc += dp->d_reclen;
463 if (dp->d_ino == 0 && strcmp(dp->d_name, "/") == 0)
465 if (dp->d_ino >= maxino) {
466 dprintf(stderr, "corrupted directory: bad inum %d\n",
475 * Simulate the opening of a directory
478 rst_opendir(const char *name)
484 if ((ino = dirlookup(name)) > 0 &&
485 (itp = inotablookup(ino)) != NULL) {
486 dirp = opendirfile(dirfile);
487 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
494 * In our case, there is nothing to do when closing a directory.
497 rst_closedir(void *arg)
502 (void)close(dirp->dd_fd);
508 * Simulate finding the current offset in the directory.
511 rst_telldir(RST_DIR *dirp)
513 return ((long)lseek(dirp->dd_fd,
514 (off_t)0, SEEK_CUR) - dirp->dd_size + dirp->dd_loc);
518 * Open a directory file.
521 opendirfile(const char *name)
526 if ((fd = open(name, O_RDONLY)) == -1)
528 if ((dirp = malloc(sizeof(RST_DIR))) == NULL) {
538 * Set the mode, owner, and times for all new or changed directories
541 setdirmodes(int flags)
544 struct modeinfo node;
549 vprintf(stdout, "Set directory mode, owner, and times.\n");
550 if ((tmpdir = getenv("TMPDIR")) == NULL || tmpdir[0] == '\0')
552 if (command == 'r' || command == 'R')
553 (void) sprintf(modefile, "%s/rstmode%d", tmpdir, dumpdate);
554 if (modefile[0] == '#') {
555 panic("modefile not defined\n");
556 fprintf(stderr, "directory mode, owner, and times not set\n");
559 mf = fopen(modefile, "r");
561 fprintf(stderr, "fopen: %s\n", strerror(errno));
562 fprintf(stderr, "cannot open mode file %s\n", modefile);
563 fprintf(stderr, "directory mode, owner, and times not set\n");
568 (void) fread((char *)&node, 1, sizeof(struct modeinfo), mf);
571 ep = lookupino(node.ino);
572 if (command == 'i' || command == 'x') {
575 if ((flags & FORCE) == 0 && ep->e_flags & EXISTED) {
579 if (node.ino == ROOTINO &&
580 reply("set owner/mode for '.'") == FAIL)
584 panic("cannot find directory inode %d\n", node.ino);
588 (void) chown(cp, node.uid, node.gid);
589 (void) chmod(cp, node.mode);
590 utimes(cp, node.ctimep);
591 utimes(cp, node.mtimep);
592 (void) chflags(cp, node.flags);
598 panic("error setting directory modes\n");
603 * Generate a literal copy of a directory.
606 genliteraldir(char *name, ino_t ino)
609 int ofile, dp, i, size;
612 itp = inotablookup(ino);
614 panic("Cannot find directory inode %d named %s\n", ino, name);
615 if ((ofile = open(name, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
616 fprintf(stderr, "%s: ", name);
617 (void) fflush(stderr);
618 fprintf(stderr, "cannot create file: %s\n", strerror(errno));
621 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
622 dp = dup(dirp->dd_fd);
623 for (i = itp->t_size; i > 0; i -= BUFSIZ) {
624 size = i < BUFSIZ ? i : BUFSIZ;
625 if (read(dp, buf, (int) size) == -1) {
627 "write error extracting inode %d, name %s\n",
628 curfile.ino, curfile.name);
629 fprintf(stderr, "read: %s\n", strerror(errno));
632 if (!Nflag && write(ofile, buf, (int) size) == -1) {
634 "write error extracting inode %d, name %s\n",
635 curfile.ino, curfile.name);
636 fprintf(stderr, "write: %s\n", strerror(errno));
646 * Determine the type of an inode
653 itp = inotablookup(ino);
660 * Allocate and initialize a directory inode entry.
661 * If requested, save its pertinent mode, owner, and time info.
663 static struct inotab *
664 allocinotab(struct context *ctxp, long seekpt)
667 struct modeinfo node;
669 itp = calloc(1, sizeof(struct inotab));
671 panic("no memory directory table\n");
672 itp->t_next = inotab[INOHASH(ctxp->ino)];
673 inotab[INOHASH(ctxp->ino)] = itp;
674 itp->t_ino = ctxp->ino;
675 itp->t_seekpt = seekpt;
678 node.ino = ctxp->ino;
679 node.mtimep[0].tv_sec = ctxp->atime_sec;
680 node.mtimep[0].tv_usec = ctxp->atime_nsec / 1000;
681 node.mtimep[1].tv_sec = ctxp->mtime_sec;
682 node.mtimep[1].tv_usec = ctxp->mtime_nsec / 1000;
683 node.ctimep[0].tv_sec = ctxp->atime_sec;
684 node.ctimep[0].tv_usec = ctxp->atime_nsec / 1000;
685 node.ctimep[1].tv_sec = ctxp->birthtime_sec;
686 node.ctimep[1].tv_usec = ctxp->birthtime_nsec / 1000;
687 node.mode = ctxp->mode;
688 node.flags = ctxp->file_flags;
689 node.uid = ctxp->uid;
690 node.gid = ctxp->gid;
691 (void) fwrite((char *)&node, 1, sizeof(struct modeinfo), mf);
696 * Look up an inode in the table of directories
698 static struct inotab *
699 inotablookup(ino_t ino)
703 for (itp = inotab[INOHASH(ino)]; itp != NULL; itp = itp->t_next)
704 if (itp->t_ino == ino)
717 if (modefile[0] != '#')
718 (void) unlink(modefile);
719 if (dirfile[0] != '#')
720 (void) unlink(dirfile);