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 */
113 * Format of old style directories.
118 char d_name[ODIRSIZ];
121 static struct inotab *allocinotab(struct context *, long);
122 static void dcvt(struct odirect *, struct direct *);
123 static void flushent(void);
124 static struct inotab *inotablookup(ino_t);
125 static RST_DIR *opendirfile(const char *);
126 static void putdir(char *, long);
127 static void putent(struct direct *);
128 static void rst_seekdir(RST_DIR *, long, long);
129 static long rst_telldir(RST_DIR *);
130 static struct direct *searchdir(ino_t, char *);
133 * Extract directory contents, building up a directory structure
134 * on disk for extraction by name.
135 * If genmode is requested, save mode, owner, and times for all
136 * directories on the tape.
139 extractdirs(int genmode)
142 struct direct nulldir;
146 vprintf(stdout, "Extract directories from tape\n");
147 if ((tmpdir = getenv("TMPDIR")) == NULL || tmpdir[0] == '\0')
149 (void) sprintf(dirfile, "%s/rstdir%d", tmpdir, dumpdate);
150 if (command != 'r' && command != 'R') {
151 (void *) strcat(dirfile, "-XXXXXX");
152 fd = mkstemp(dirfile);
154 fd = open(dirfile, O_RDWR|O_CREAT|O_EXCL, 0666);
155 if (fd == -1 || (df = fdopen(fd, "w")) == NULL) {
158 warn("%s - cannot create directory temporary\nfopen", dirfile);
162 (void) sprintf(modefile, "%s/rstmode%d", tmpdir, dumpdate);
163 if (command != 'r' && command != 'R') {
164 (void *) strcat(modefile, "-XXXXXX");
165 fd = mkstemp(modefile);
167 fd = open(modefile, O_RDWR|O_CREAT|O_EXCL, 0666);
168 if (fd == -1 || (mf = fdopen(fd, "w")) == NULL) {
171 warn("%s - cannot create modefile\nfopen", modefile);
176 nulldir.d_type = DT_DIR;
177 nulldir.d_namlen = 1;
178 (void) strcpy(nulldir.d_name, "/");
179 nulldir.d_reclen = DIRSIZ(0, &nulldir);
181 curfile.name = "<directory file - name unknown>";
182 curfile.action = USING;
183 if (curfile.mode == 0 || (curfile.mode & IFMT) != IFDIR) {
185 dirp = opendirfile(dirfile);
187 fprintf(stderr, "opendirfile: %s\n",
193 panic("Root directory is not on tape\n");
196 itp = allocinotab(&curfile, seekpt);
197 getfile(putdir, xtrnull);
200 itp->t_size = seekpt - itp->t_seekpt;
205 * skip over all the directories on the tape
211 while (curfile.ino && (curfile.mode & IFMT) == IFDIR) {
217 * Recursively find names and inumbers of all files in subtree
218 * pname and pass them off to be processed.
221 treescan(char *pname, ino_t ino, long (*todo)(char *, ino_t, int))
227 char locname[MAXPATHLEN + 1];
229 itp = inotablookup(ino);
232 * Pname is name of a simple file or an unchanged directory.
234 (void) (*todo)(pname, ino, LEAF);
238 * Pname is a dumped directory name.
240 if ((*todo)(pname, ino, NODE) == FAIL)
243 * begin search through the directory
244 * skipping over "." and ".."
246 (void) strncpy(locname, pname, sizeof(locname) - 1);
247 locname[sizeof(locname) - 1] = '\0';
248 (void) strncat(locname, "/", sizeof(locname) - strlen(locname));
249 namelen = strlen(locname);
250 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
251 dp = rst_readdir(dirp); /* "." */
252 if (dp != NULL && strcmp(dp->d_name, ".") == 0)
253 dp = rst_readdir(dirp); /* ".." */
255 fprintf(stderr, "Warning: `.' missing from directory %s\n",
257 if (dp != NULL && strcmp(dp->d_name, "..") == 0)
258 dp = rst_readdir(dirp); /* first real entry */
260 fprintf(stderr, "Warning: `..' missing from directory %s\n",
262 bpt = rst_telldir(dirp);
264 * a zero inode signals end of directory
267 locname[namelen] = '\0';
268 if (namelen + dp->d_namlen >= sizeof(locname)) {
269 fprintf(stderr, "%s%s: name exceeds %d char\n",
270 locname, dp->d_name, sizeof(locname) - 1);
272 (void) strncat(locname, dp->d_name, (int)dp->d_namlen);
273 treescan(locname, dp->d_ino, todo);
274 rst_seekdir(dirp, bpt, itp->t_seekpt);
276 dp = rst_readdir(dirp);
277 bpt = rst_telldir(dirp);
282 * Lookup a pathname which is always assumed to start from the ROOTINO.
285 pathsearch(const char *pathname)
289 char *path, *name, buffer[MAXPATHLEN];
291 strcpy(buffer, pathname);
297 while ((name = strsep(&path, "/")) != NULL && *name != '\0') {
298 if ((dp = searchdir(ino, name)) == NULL)
306 * Lookup the requested name in directory inum.
307 * Return its inode number if found, zero if it does not exist.
309 static struct direct *
310 searchdir(ino_t inum, char *name)
316 itp = inotablookup(inum);
319 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
322 dp = rst_readdir(dirp);
325 } while (dp->d_namlen != len || strncmp(dp->d_name, name, len) != 0);
330 * Put the directory entries in the directory file
333 putdir(char *buf, long size)
335 struct direct cvtbuf;
337 struct odirect *eodp;
341 for (loc = 0; loc < size; ) {
342 dp = (struct direct *)(buf + loc);
344 swabst((u_char *)"ls", (u_char *) dp);
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 < %d) ",
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 (void) fwrite(dirbuf, 1, DIRBLKSIZ, df);
398 memmove(dirbuf + dirloc, dp, (long)dp->d_reclen);
400 dirloc += dp->d_reclen;
404 * flush out a directory that is finished.
409 ((struct direct *)(dirbuf + prev))->d_reclen = DIRBLKSIZ - prev;
410 (void) fwrite(dirbuf, (int)dirloc, 1, df);
416 dcvt(struct odirect *odp, struct direct *ndp)
419 memset(ndp, 0, (long)(sizeof *ndp));
420 ndp->d_ino = odp->d_ino;
421 ndp->d_type = DT_UNKNOWN;
422 (void) strncpy(ndp->d_name, odp->d_name, ODIRSIZ);
423 ndp->d_namlen = strlen(ndp->d_name);
424 ndp->d_reclen = DIRSIZ(0, ndp);
428 * Seek to an entry in a directory.
429 * Only values returned by rst_telldir should be passed to rst_seekdir.
430 * This routine handles many directories in a single file.
431 * It takes the base of the directory in the file, plus
432 * the desired seek offset into it.
435 rst_seekdir(RST_DIR *dirp, long loc, long base)
438 if (loc == rst_telldir(dirp))
442 fprintf(stderr, "bad seek pointer to rst_seekdir %ld\n", loc);
443 (void) lseek(dirp->dd_fd, base + (loc & ~(DIRBLKSIZ - 1)), SEEK_SET);
444 dirp->dd_loc = loc & (DIRBLKSIZ - 1);
445 if (dirp->dd_loc != 0)
446 dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ);
450 * get next entry in a directory.
453 rst_readdir(RST_DIR *dirp)
458 if (dirp->dd_loc == 0) {
459 dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf,
461 if (dirp->dd_size <= 0) {
462 dprintf(stderr, "error reading directory\n");
466 if (dirp->dd_loc >= dirp->dd_size) {
470 dp = (struct direct *)(dirp->dd_buf + dirp->dd_loc);
471 if (dp->d_reclen == 0 ||
472 dp->d_reclen > DIRBLKSIZ + 1 - dirp->dd_loc) {
473 dprintf(stderr, "corrupted directory: bad reclen %d\n",
477 dirp->dd_loc += dp->d_reclen;
478 if (dp->d_ino == 0 && strcmp(dp->d_name, "/") == 0)
480 if (dp->d_ino >= maxino) {
481 dprintf(stderr, "corrupted directory: bad inum %d\n",
490 * Simulate the opening of a directory
493 rst_opendir(const char *name)
499 if ((ino = dirlookup(name)) > 0 &&
500 (itp = inotablookup(ino)) != NULL) {
501 dirp = opendirfile(dirfile);
502 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
509 * In our case, there is nothing to do when closing a directory.
512 rst_closedir(void *arg)
517 (void)close(dirp->dd_fd);
523 * Simulate finding the current offset in the directory.
526 rst_telldir(RST_DIR *dirp)
528 return ((long)lseek(dirp->dd_fd,
529 (off_t)0, SEEK_CUR) - dirp->dd_size + dirp->dd_loc);
533 * Open a directory file.
536 opendirfile(const char *name)
541 if ((fd = open(name, O_RDONLY)) == -1)
543 if ((dirp = malloc(sizeof(RST_DIR))) == NULL) {
553 * Set the mode, owner, and times for all new or changed directories
556 setdirmodes(int flags)
559 struct modeinfo node;
564 vprintf(stdout, "Set directory mode, owner, and times.\n");
565 if ((tmpdir = getenv("TMPDIR")) == NULL || tmpdir[0] == '\0')
567 if (command == 'r' || command == 'R')
568 (void) sprintf(modefile, "%s/rstmode%d", tmpdir, dumpdate);
569 if (modefile[0] == '#') {
570 panic("modefile not defined\n");
571 fprintf(stderr, "directory mode, owner, and times not set\n");
574 mf = fopen(modefile, "r");
576 fprintf(stderr, "fopen: %s\n", strerror(errno));
577 fprintf(stderr, "cannot open mode file %s\n", modefile);
578 fprintf(stderr, "directory mode, owner, and times not set\n");
583 (void) fread((char *)&node, 1, sizeof(struct modeinfo), mf);
586 ep = lookupino(node.ino);
587 if (command == 'i' || command == 'x') {
590 if ((flags & FORCE) == 0 && ep->e_flags & EXISTED) {
594 if (node.ino == ROOTINO &&
595 reply("set owner/mode for '.'") == FAIL)
599 panic("cannot find directory inode %d\n", node.ino);
603 (void) chown(cp, node.uid, node.gid);
604 (void) chmod(cp, node.mode);
605 utimes(cp, node.ctimep);
606 utimes(cp, node.mtimep);
607 (void) chflags(cp, node.flags);
613 panic("error setting directory modes\n");
618 * Generate a literal copy of a directory.
621 genliteraldir(char *name, ino_t ino)
624 int ofile, dp, i, size;
627 itp = inotablookup(ino);
629 panic("Cannot find directory inode %d named %s\n", ino, name);
630 if ((ofile = open(name, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
631 fprintf(stderr, "%s: ", name);
632 (void) fflush(stderr);
633 fprintf(stderr, "cannot create file: %s\n", strerror(errno));
636 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
637 dp = dup(dirp->dd_fd);
638 for (i = itp->t_size; i > 0; i -= BUFSIZ) {
639 size = i < BUFSIZ ? i : BUFSIZ;
640 if (read(dp, buf, (int) size) == -1) {
642 "write error extracting inode %d, name %s\n",
643 curfile.ino, curfile.name);
644 fprintf(stderr, "read: %s\n", strerror(errno));
647 if (!Nflag && write(ofile, buf, (int) size) == -1) {
649 "write error extracting inode %d, name %s\n",
650 curfile.ino, curfile.name);
651 fprintf(stderr, "write: %s\n", strerror(errno));
661 * Determine the type of an inode
668 itp = inotablookup(ino);
675 * Allocate and initialize a directory inode entry.
676 * If requested, save its pertinent mode, owner, and time info.
678 static struct inotab *
679 allocinotab(struct context *ctxp, long seekpt)
682 struct modeinfo node;
684 itp = calloc(1, sizeof(struct inotab));
686 panic("no memory directory table\n");
687 itp->t_next = inotab[INOHASH(ctxp->ino)];
688 inotab[INOHASH(ctxp->ino)] = itp;
689 itp->t_ino = ctxp->ino;
690 itp->t_seekpt = seekpt;
693 node.ino = ctxp->ino;
694 node.mtimep[0].tv_sec = ctxp->atime_sec;
695 node.mtimep[0].tv_usec = ctxp->atime_nsec / 1000;
696 node.mtimep[1].tv_sec = ctxp->mtime_sec;
697 node.mtimep[1].tv_usec = ctxp->mtime_nsec / 1000;
698 node.ctimep[0].tv_sec = ctxp->atime_sec;
699 node.ctimep[0].tv_usec = ctxp->atime_nsec / 1000;
700 node.ctimep[1].tv_sec = ctxp->birthtime_sec;
701 node.ctimep[1].tv_usec = ctxp->birthtime_nsec / 1000;
702 node.mode = ctxp->mode;
703 node.flags = ctxp->file_flags;
704 node.uid = ctxp->uid;
705 node.gid = ctxp->gid;
706 (void) fwrite((char *)&node, 1, sizeof(struct modeinfo), mf);
711 * Look up an inode in the table of directories
713 static struct inotab *
714 inotablookup(ino_t ino)
718 for (itp = inotab[INOHASH(ino)]; itp != NULL; itp = itp->t_next)
719 if (itp->t_ino == ino)
732 if (modefile[0] != '#')
733 (void) unlink(modefile);
734 if (dirfile[0] != '#')
735 (void) unlink(dirfile);