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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 static char sccsid[] = "@(#)dirs.c 8.7 (Berkeley) 5/1/95";
43 static const char rcsid[] =
47 #include <sys/param.h>
52 #include <ufs/ufs/dinode.h>
53 #include <ufs/ufs/dir.h>
54 #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 timeval timep[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 */
115 * Format of old style directories.
120 char d_name[ODIRSIZ];
123 static struct inotab *allocinotab __P((ino_t, struct dinode *, long));
124 static void dcvt __P((struct odirect *, struct direct *));
125 static void flushent __P((void));
126 static struct inotab *inotablookup __P((ino_t));
127 static RST_DIR *opendirfile __P((const char *));
128 static void putdir __P((char *, long));
129 static void putent __P((struct direct *));
130 static void rst_seekdir __P((RST_DIR *, long, long));
131 static long rst_telldir __P((RST_DIR *));
132 static struct direct *searchdir __P((ino_t, char *));
135 * Extract directory contents, building up a directory structure
136 * on disk for extraction by name.
137 * If genmode is requested, save mode, owner, and times for all
138 * directories on the tape.
145 register struct dinode *ip;
147 struct direct nulldir;
150 vprintf(stdout, "Extract directories from tape\n");
151 (void) sprintf(dirfile, "%srstdir%d", _PATH_TMP, dumpdate);
152 if (command != 'r' && command != 'R') {
153 (void *) strcat(dirfile, "-XXXXXX");
154 fd = mkstemp(dirfile);
156 fd = open(dirfile, O_RDWR|O_CREAT|O_EXCL, 0666);
157 if (fd == -1 || (df = fdopen(fd, "w")) == NULL) {
160 warn("%s - cannot create directory temporary\nfopen", dirfile);
164 (void) sprintf(modefile, "%srstmode%d", _PATH_TMP, dumpdate);
165 if (command != 'r' && command != 'R') {
166 (void *) strcat(modefile, "-XXXXXX");
167 fd = mkstemp(modefile);
169 fd = open(modefile, O_RDWR|O_CREAT|O_EXCL, 0666);
170 if (fd == -1 || (mf = fdopen(fd, "w")) == NULL) {
173 warn("%s - cannot create modefile\nfopen", modefile);
178 nulldir.d_type = DT_DIR;
179 nulldir.d_namlen = 1;
180 (void) strcpy(nulldir.d_name, "/");
181 nulldir.d_reclen = DIRSIZ(0, &nulldir);
183 curfile.name = "<directory file - name unknown>";
184 curfile.action = USING;
186 if (ip == NULL || (ip->di_mode & IFMT) != IFDIR) {
188 dirp = opendirfile(dirfile);
190 fprintf(stderr, "opendirfile: %s\n",
196 panic("Root directory is not on tape\n");
199 itp = allocinotab(curfile.ino, ip, seekpt);
200 getfile(putdir, xtrnull);
203 itp->t_size = seekpt - itp->t_seekpt;
208 * skip over all the directories on the tape
214 while (curfile.dip && (curfile.dip->di_mode & IFMT) == IFDIR) {
220 * Recursively find names and inumbers of all files in subtree
221 * pname and pass them off to be processed.
224 treescan(pname, ino, todo)
227 long (*todo) __P((char *, ino_t, int));
229 register struct inotab *itp;
230 register struct direct *dp;
233 char locname[MAXPATHLEN + 1];
235 itp = inotablookup(ino);
238 * Pname is name of a simple file or an unchanged directory.
240 (void) (*todo)(pname, ino, LEAF);
244 * Pname is a dumped directory name.
246 if ((*todo)(pname, ino, NODE) == FAIL)
249 * begin search through the directory
250 * skipping over "." and ".."
252 (void) strncpy(locname, pname, sizeof(locname) - 1);
253 locname[sizeof(locname) - 1] = '\0';
254 (void) strncat(locname, "/", sizeof(locname) - strlen(locname));
255 namelen = strlen(locname);
256 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
257 dp = rst_readdir(dirp); /* "." */
258 if (dp != NULL && strcmp(dp->d_name, ".") == 0)
259 dp = rst_readdir(dirp); /* ".." */
261 fprintf(stderr, "Warning: `.' missing from directory %s\n",
263 if (dp != NULL && strcmp(dp->d_name, "..") == 0)
264 dp = rst_readdir(dirp); /* first real entry */
266 fprintf(stderr, "Warning: `..' missing from directory %s\n",
268 bpt = rst_telldir(dirp);
270 * a zero inode signals end of directory
273 locname[namelen] = '\0';
274 if (namelen + dp->d_namlen >= sizeof(locname)) {
275 fprintf(stderr, "%s%s: name exceeds %d char\n",
276 locname, dp->d_name, sizeof(locname) - 1);
278 (void) strncat(locname, dp->d_name, (int)dp->d_namlen);
279 treescan(locname, dp->d_ino, todo);
280 rst_seekdir(dirp, bpt, itp->t_seekpt);
282 dp = rst_readdir(dirp);
283 bpt = rst_telldir(dirp);
288 * Lookup a pathname which is always assumed to start from the ROOTINO.
292 const char *pathname;
296 char *path, *name, buffer[MAXPATHLEN];
298 strcpy(buffer, pathname);
304 while ((name = strsep(&path, "/")) != NULL && *name != '\0') {
305 if ((dp = searchdir(ino, name)) == NULL)
313 * Lookup the requested name in directory inum.
314 * Return its inode number if found, zero if it does not exist.
316 static struct direct *
317 searchdir(inum, name)
321 register struct direct *dp;
322 register struct inotab *itp;
325 itp = inotablookup(inum);
328 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
331 dp = rst_readdir(dirp);
334 } while (dp->d_namlen != len || strncmp(dp->d_name, name, len) != 0);
339 * Put the directory entries in the directory file
346 struct direct cvtbuf;
347 register struct odirect *odp;
348 struct odirect *eodp;
349 register struct direct *dp;
353 eodp = (struct odirect *)&buf[size];
354 for (odp = (struct odirect *)buf; odp < eodp; odp++)
355 if (odp->d_ino != 0) {
360 for (loc = 0; loc < size; ) {
361 dp = (struct direct *)(buf + loc);
363 swabst((u_char *)"ls", (u_char *) dp);
364 if (oldinofmt && dp->d_ino != 0) {
365 # if BYTE_ORDER == BIG_ENDIAN
367 dp->d_namlen = dp->d_type;
370 dp->d_namlen = dp->d_type;
372 dp->d_type = DT_UNKNOWN;
374 i = DIRBLKSIZ - (loc & (DIRBLKSIZ - 1));
375 if ((dp->d_reclen & 0x3) != 0 ||
377 dp->d_reclen < DIRSIZ(0, dp) ||
378 dp->d_namlen > NAME_MAX) {
379 vprintf(stdout, "Mangled directory: ");
380 if ((dp->d_reclen & 0x3) != 0)
382 "reclen not multiple of 4 ");
383 if (dp->d_reclen < DIRSIZ(0, dp))
385 "reclen less than DIRSIZ (%d < %d) ",
386 dp->d_reclen, DIRSIZ(0, dp));
387 if (dp->d_namlen > NAME_MAX)
389 "reclen name too big (%d > %d) ",
390 dp->d_namlen, NAME_MAX);
391 vprintf(stdout, "\n");
396 if (dp->d_ino != 0) {
404 * These variables are "local" to the following two functions.
406 char dirbuf[DIRBLKSIZ];
411 * add a new directory entry to a file.
417 dp->d_reclen = DIRSIZ(0, dp);
418 if (dirloc + dp->d_reclen > DIRBLKSIZ) {
419 ((struct direct *)(dirbuf + prev))->d_reclen =
421 (void) fwrite(dirbuf, 1, DIRBLKSIZ, df);
424 memmove(dirbuf + dirloc, dp, (long)dp->d_reclen);
426 dirloc += dp->d_reclen;
430 * flush out a directory that is finished.
435 ((struct direct *)(dirbuf + prev))->d_reclen = DIRBLKSIZ - prev;
436 (void) fwrite(dirbuf, (int)dirloc, 1, df);
443 register struct odirect *odp;
444 register struct direct *ndp;
447 memset(ndp, 0, (long)(sizeof *ndp));
448 ndp->d_ino = odp->d_ino;
449 ndp->d_type = DT_UNKNOWN;
450 (void) strncpy(ndp->d_name, odp->d_name, ODIRSIZ);
451 ndp->d_namlen = strlen(ndp->d_name);
452 ndp->d_reclen = DIRSIZ(0, ndp);
456 * Seek to an entry in a directory.
457 * Only values returned by rst_telldir should be passed to rst_seekdir.
458 * This routine handles many directories in a single file.
459 * It takes the base of the directory in the file, plus
460 * the desired seek offset into it.
463 rst_seekdir(dirp, loc, base)
464 register RST_DIR *dirp;
468 if (loc == rst_telldir(dirp))
472 fprintf(stderr, "bad seek pointer to rst_seekdir %ld\n", loc);
473 (void) lseek(dirp->dd_fd, base + (loc & ~(DIRBLKSIZ - 1)), SEEK_SET);
474 dirp->dd_loc = loc & (DIRBLKSIZ - 1);
475 if (dirp->dd_loc != 0)
476 dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ);
480 * get next entry in a directory.
484 register RST_DIR *dirp;
486 register struct direct *dp;
489 if (dirp->dd_loc == 0) {
490 dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf,
492 if (dirp->dd_size <= 0) {
493 dprintf(stderr, "error reading directory\n");
497 if (dirp->dd_loc >= dirp->dd_size) {
501 dp = (struct direct *)(dirp->dd_buf + dirp->dd_loc);
502 if (dp->d_reclen == 0 ||
503 dp->d_reclen > DIRBLKSIZ + 1 - dirp->dd_loc) {
504 dprintf(stderr, "corrupted directory: bad reclen %d\n",
508 dirp->dd_loc += dp->d_reclen;
509 if (dp->d_ino == 0 && strcmp(dp->d_name, "/") == 0)
511 if (dp->d_ino >= maxino) {
512 dprintf(stderr, "corrupted directory: bad inum %d\n",
521 * Simulate the opening of a directory
531 if ((ino = dirlookup(name)) > 0 &&
532 (itp = inotablookup(ino)) != NULL) {
533 dirp = opendirfile(dirfile);
534 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
541 * In our case, there is nothing to do when closing a directory.
548 (void)close(dirp->dd_fd);
554 * Simulate finding the current offset in the directory.
560 return ((long)lseek(dirp->dd_fd,
561 (off_t)0, SEEK_CUR) - dirp->dd_size + dirp->dd_loc);
565 * Open a directory file.
571 register RST_DIR *dirp;
574 if ((fd = open(name, O_RDONLY)) == -1)
576 if ((dirp = malloc(sizeof(RST_DIR))) == NULL) {
586 * Set the mode, owner, and times for all new or changed directories
593 struct modeinfo node;
597 vprintf(stdout, "Set directory mode, owner, and times.\n");
598 if (command == 'r' || command == 'R')
599 (void) sprintf(modefile, "%srstmode%d", _PATH_TMP, dumpdate);
600 if (modefile[0] == '#') {
601 panic("modefile not defined\n");
602 fprintf(stderr, "directory mode, owner, and times not set\n");
605 mf = fopen(modefile, "r");
607 fprintf(stderr, "fopen: %s\n", strerror(errno));
608 fprintf(stderr, "cannot open mode file %s\n", modefile);
609 fprintf(stderr, "directory mode, owner, and times not set\n");
614 (void) fread((char *)&node, 1, sizeof(struct modeinfo), mf);
617 ep = lookupino(node.ino);
618 if (command == 'i' || command == 'x') {
621 if ((flags & FORCE) == 0 && ep->e_flags & EXISTED) {
625 if (node.ino == ROOTINO &&
626 reply("set owner/mode for '.'") == FAIL)
630 panic("cannot find directory inode %d\n", node.ino);
633 (void) chown(cp, node.uid, node.gid);
634 (void) chmod(cp, node.mode);
635 utimes(cp, node.timep);
636 (void) chflags(cp, node.flags);
641 panic("error setting directory modes\n");
646 * Generate a literal copy of a directory.
649 genliteraldir(name, ino)
653 register struct inotab *itp;
654 int ofile, dp, i, size;
657 itp = inotablookup(ino);
659 panic("Cannot find directory inode %d named %s\n", ino, name);
660 if ((ofile = open(name, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
661 fprintf(stderr, "%s: ", name);
662 (void) fflush(stderr);
663 fprintf(stderr, "cannot create file: %s\n", strerror(errno));
666 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
667 dp = dup(dirp->dd_fd);
668 for (i = itp->t_size; i > 0; i -= BUFSIZ) {
669 size = i < BUFSIZ ? i : BUFSIZ;
670 if (read(dp, buf, (int) size) == -1) {
672 "write error extracting inode %d, name %s\n",
673 curfile.ino, curfile.name);
674 fprintf(stderr, "read: %s\n", strerror(errno));
677 if (!Nflag && write(ofile, buf, (int) size) == -1) {
679 "write error extracting inode %d, name %s\n",
680 curfile.ino, curfile.name);
681 fprintf(stderr, "write: %s\n", strerror(errno));
691 * Determine the type of an inode
699 itp = inotablookup(ino);
706 * Allocate and initialize a directory inode entry.
707 * If requested, save its pertinent mode, owner, and time info.
709 static struct inotab *
710 allocinotab(ino, dip, seekpt)
715 register struct inotab *itp;
716 struct modeinfo node;
718 itp = calloc(1, sizeof(struct inotab));
720 panic("no memory directory table\n");
721 itp->t_next = inotab[INOHASH(ino)];
722 inotab[INOHASH(ino)] = itp;
724 itp->t_seekpt = seekpt;
728 node.timep[0].tv_sec = dip->di_atime;
729 node.timep[0].tv_usec = dip->di_atimensec / 1000;
730 node.timep[1].tv_sec = dip->di_mtime;
731 node.timep[1].tv_usec = dip->di_mtimensec / 1000;
732 node.mode = dip->di_mode;
733 node.flags = dip->di_flags;
734 node.uid = dip->di_uid;
735 node.gid = dip->di_gid;
736 (void) fwrite((char *)&node, 1, sizeof(struct modeinfo), mf);
741 * Look up an inode in the table of directories
743 static struct inotab *
747 register struct inotab *itp;
749 for (itp = inotab[INOHASH(ino)]; itp != NULL; itp = itp->t_next)
750 if (itp->t_ino == ino)
764 if (modefile[0] != '#')
765 (void) unlink(modefile);
766 if (dirfile[0] != '#')
767 (void) unlink(dirfile);