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[] =
44 "$FreeBSD: src/sbin/restore/dirs.c,v 1.14.2.5 2001/10/15 13:44:45 dd Exp $";
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;
151 vprintf(stdout, "Extract directories from tape\n");
152 if ((tmpdir = getenv("TMPDIR")) == NULL || tmpdir[0] == '\0')
154 (void) sprintf(dirfile, "%s/rstdir%d", tmpdir, dumpdate);
155 if (command != 'r' && command != 'R') {
156 (void *) strcat(dirfile, "-XXXXXX");
157 fd = mkstemp(dirfile);
159 fd = open(dirfile, O_RDWR|O_CREAT|O_EXCL, 0666);
160 if (fd == -1 || (df = fdopen(fd, "w")) == NULL) {
163 warn("%s - cannot create directory temporary\nfopen", dirfile);
167 (void) sprintf(modefile, "%s/rstmode%d", tmpdir, dumpdate);
168 if (command != 'r' && command != 'R') {
169 (void *) strcat(modefile, "-XXXXXX");
170 fd = mkstemp(modefile);
172 fd = open(modefile, O_RDWR|O_CREAT|O_EXCL, 0666);
173 if (fd == -1 || (mf = fdopen(fd, "w")) == NULL) {
176 warn("%s - cannot create modefile\nfopen", modefile);
181 nulldir.d_type = DT_DIR;
182 nulldir.d_namlen = 1;
183 (void) strcpy(nulldir.d_name, "/");
184 nulldir.d_reclen = DIRSIZ(0, &nulldir);
186 curfile.name = "<directory file - name unknown>";
187 curfile.action = USING;
189 if (ip == NULL || (ip->di_mode & IFMT) != IFDIR) {
191 dirp = opendirfile(dirfile);
193 fprintf(stderr, "opendirfile: %s\n",
199 panic("Root directory is not on tape\n");
202 itp = allocinotab(curfile.ino, ip, seekpt);
203 getfile(putdir, xtrnull);
206 itp->t_size = seekpt - itp->t_seekpt;
211 * skip over all the directories on the tape
217 while (curfile.dip && (curfile.dip->di_mode & IFMT) == IFDIR) {
223 * Recursively find names and inumbers of all files in subtree
224 * pname and pass them off to be processed.
227 treescan(pname, ino, todo)
230 long (*todo) __P((char *, ino_t, int));
232 register struct inotab *itp;
233 register struct direct *dp;
236 char locname[MAXPATHLEN + 1];
238 itp = inotablookup(ino);
241 * Pname is name of a simple file or an unchanged directory.
243 (void) (*todo)(pname, ino, LEAF);
247 * Pname is a dumped directory name.
249 if ((*todo)(pname, ino, NODE) == FAIL)
252 * begin search through the directory
253 * skipping over "." and ".."
255 (void) strncpy(locname, pname, sizeof(locname) - 1);
256 locname[sizeof(locname) - 1] = '\0';
257 (void) strncat(locname, "/", sizeof(locname) - strlen(locname));
258 namelen = strlen(locname);
259 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
260 dp = rst_readdir(dirp); /* "." */
261 if (dp != NULL && strcmp(dp->d_name, ".") == 0)
262 dp = rst_readdir(dirp); /* ".." */
264 fprintf(stderr, "Warning: `.' missing from directory %s\n",
266 if (dp != NULL && strcmp(dp->d_name, "..") == 0)
267 dp = rst_readdir(dirp); /* first real entry */
269 fprintf(stderr, "Warning: `..' missing from directory %s\n",
271 bpt = rst_telldir(dirp);
273 * a zero inode signals end of directory
276 locname[namelen] = '\0';
277 if (namelen + dp->d_namlen >= sizeof(locname)) {
278 fprintf(stderr, "%s%s: name exceeds %d char\n",
279 locname, dp->d_name, sizeof(locname) - 1);
281 (void) strncat(locname, dp->d_name, (int)dp->d_namlen);
282 treescan(locname, dp->d_ino, todo);
283 rst_seekdir(dirp, bpt, itp->t_seekpt);
285 dp = rst_readdir(dirp);
286 bpt = rst_telldir(dirp);
291 * Lookup a pathname which is always assumed to start from the ROOTINO.
295 const char *pathname;
299 char *path, *name, buffer[MAXPATHLEN];
301 strcpy(buffer, pathname);
307 while ((name = strsep(&path, "/")) != NULL && *name != '\0') {
308 if ((dp = searchdir(ino, name)) == NULL)
316 * Lookup the requested name in directory inum.
317 * Return its inode number if found, zero if it does not exist.
319 static struct direct *
320 searchdir(inum, name)
324 register struct direct *dp;
325 register struct inotab *itp;
328 itp = inotablookup(inum);
331 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
334 dp = rst_readdir(dirp);
337 } while (dp->d_namlen != len || strncmp(dp->d_name, name, len) != 0);
342 * Put the directory entries in the directory file
349 struct direct cvtbuf;
350 register struct odirect *odp;
351 struct odirect *eodp;
352 register struct direct *dp;
356 eodp = (struct odirect *)&buf[size];
357 for (odp = (struct odirect *)buf; odp < eodp; odp++)
358 if (odp->d_ino != 0) {
363 for (loc = 0; loc < size; ) {
364 dp = (struct direct *)(buf + loc);
366 swabst((u_char *)"ls", (u_char *) dp);
367 if (oldinofmt && dp->d_ino != 0) {
368 # if BYTE_ORDER == BIG_ENDIAN
370 dp->d_namlen = dp->d_type;
373 dp->d_namlen = dp->d_type;
375 dp->d_type = DT_UNKNOWN;
377 i = DIRBLKSIZ - (loc & (DIRBLKSIZ - 1));
378 if ((dp->d_reclen & 0x3) != 0 ||
380 dp->d_reclen < DIRSIZ(0, dp) ||
381 dp->d_namlen > NAME_MAX) {
382 vprintf(stdout, "Mangled directory: ");
383 if ((dp->d_reclen & 0x3) != 0)
385 "reclen not multiple of 4 ");
386 if (dp->d_reclen < DIRSIZ(0, dp))
388 "reclen less than DIRSIZ (%d < %d) ",
389 dp->d_reclen, DIRSIZ(0, dp));
390 if (dp->d_namlen > NAME_MAX)
392 "reclen name too big (%d > %d) ",
393 dp->d_namlen, NAME_MAX);
394 vprintf(stdout, "\n");
399 if (dp->d_ino != 0) {
407 * These variables are "local" to the following two functions.
409 char dirbuf[DIRBLKSIZ];
414 * add a new directory entry to a file.
420 dp->d_reclen = DIRSIZ(0, dp);
421 if (dirloc + dp->d_reclen > DIRBLKSIZ) {
422 ((struct direct *)(dirbuf + prev))->d_reclen =
424 (void) fwrite(dirbuf, 1, DIRBLKSIZ, df);
427 memmove(dirbuf + dirloc, dp, (long)dp->d_reclen);
429 dirloc += dp->d_reclen;
433 * flush out a directory that is finished.
438 ((struct direct *)(dirbuf + prev))->d_reclen = DIRBLKSIZ - prev;
439 (void) fwrite(dirbuf, (int)dirloc, 1, df);
446 register struct odirect *odp;
447 register struct direct *ndp;
450 memset(ndp, 0, (long)(sizeof *ndp));
451 ndp->d_ino = odp->d_ino;
452 ndp->d_type = DT_UNKNOWN;
453 (void) strncpy(ndp->d_name, odp->d_name, ODIRSIZ);
454 ndp->d_namlen = strlen(ndp->d_name);
455 ndp->d_reclen = DIRSIZ(0, ndp);
459 * Seek to an entry in a directory.
460 * Only values returned by rst_telldir should be passed to rst_seekdir.
461 * This routine handles many directories in a single file.
462 * It takes the base of the directory in the file, plus
463 * the desired seek offset into it.
466 rst_seekdir(dirp, loc, base)
467 register RST_DIR *dirp;
471 if (loc == rst_telldir(dirp))
475 fprintf(stderr, "bad seek pointer to rst_seekdir %ld\n", loc);
476 (void) lseek(dirp->dd_fd, base + (loc & ~(DIRBLKSIZ - 1)), SEEK_SET);
477 dirp->dd_loc = loc & (DIRBLKSIZ - 1);
478 if (dirp->dd_loc != 0)
479 dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ);
483 * get next entry in a directory.
487 register RST_DIR *dirp;
489 register struct direct *dp;
492 if (dirp->dd_loc == 0) {
493 dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf,
495 if (dirp->dd_size <= 0) {
496 dprintf(stderr, "error reading directory\n");
500 if (dirp->dd_loc >= dirp->dd_size) {
504 dp = (struct direct *)(dirp->dd_buf + dirp->dd_loc);
505 if (dp->d_reclen == 0 ||
506 dp->d_reclen > DIRBLKSIZ + 1 - dirp->dd_loc) {
507 dprintf(stderr, "corrupted directory: bad reclen %d\n",
511 dirp->dd_loc += dp->d_reclen;
512 if (dp->d_ino == 0 && strcmp(dp->d_name, "/") == 0)
514 if (dp->d_ino >= maxino) {
515 dprintf(stderr, "corrupted directory: bad inum %d\n",
524 * Simulate the opening of a directory
534 if ((ino = dirlookup(name)) > 0 &&
535 (itp = inotablookup(ino)) != NULL) {
536 dirp = opendirfile(dirfile);
537 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
544 * In our case, there is nothing to do when closing a directory.
551 (void)close(dirp->dd_fd);
557 * Simulate finding the current offset in the directory.
563 return ((long)lseek(dirp->dd_fd,
564 (off_t)0, SEEK_CUR) - dirp->dd_size + dirp->dd_loc);
568 * Open a directory file.
574 register RST_DIR *dirp;
577 if ((fd = open(name, O_RDONLY)) == -1)
579 if ((dirp = malloc(sizeof(RST_DIR))) == NULL) {
589 * Set the mode, owner, and times for all new or changed directories
596 struct modeinfo node;
601 vprintf(stdout, "Set directory mode, owner, and times.\n");
602 if ((tmpdir = getenv("TMPDIR")) == NULL || tmpdir[0] == '\0')
604 if (command == 'r' || command == 'R')
605 (void) sprintf(modefile, "%s/rstmode%d", tmpdir, dumpdate);
606 if (modefile[0] == '#') {
607 panic("modefile not defined\n");
608 fprintf(stderr, "directory mode, owner, and times not set\n");
611 mf = fopen(modefile, "r");
613 fprintf(stderr, "fopen: %s\n", strerror(errno));
614 fprintf(stderr, "cannot open mode file %s\n", modefile);
615 fprintf(stderr, "directory mode, owner, and times not set\n");
620 (void) fread((char *)&node, 1, sizeof(struct modeinfo), mf);
623 ep = lookupino(node.ino);
624 if (command == 'i' || command == 'x') {
627 if ((flags & FORCE) == 0 && ep->e_flags & EXISTED) {
631 if (node.ino == ROOTINO &&
632 reply("set owner/mode for '.'") == FAIL)
636 panic("cannot find directory inode %d\n", node.ino);
640 (void) chown(cp, node.uid, node.gid);
641 (void) chmod(cp, node.mode);
642 utimes(cp, node.timep);
643 (void) chflags(cp, node.flags);
649 panic("error setting directory modes\n");
654 * Generate a literal copy of a directory.
657 genliteraldir(name, ino)
661 register struct inotab *itp;
662 int ofile, dp, i, size;
665 itp = inotablookup(ino);
667 panic("Cannot find directory inode %d named %s\n", ino, name);
668 if ((ofile = open(name, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
669 fprintf(stderr, "%s: ", name);
670 (void) fflush(stderr);
671 fprintf(stderr, "cannot create file: %s\n", strerror(errno));
674 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
675 dp = dup(dirp->dd_fd);
676 for (i = itp->t_size; i > 0; i -= BUFSIZ) {
677 size = i < BUFSIZ ? i : BUFSIZ;
678 if (read(dp, buf, (int) size) == -1) {
680 "write error extracting inode %d, name %s\n",
681 curfile.ino, curfile.name);
682 fprintf(stderr, "read: %s\n", strerror(errno));
685 if (!Nflag && write(ofile, buf, (int) size) == -1) {
687 "write error extracting inode %d, name %s\n",
688 curfile.ino, curfile.name);
689 fprintf(stderr, "write: %s\n", strerror(errno));
699 * Determine the type of an inode
707 itp = inotablookup(ino);
714 * Allocate and initialize a directory inode entry.
715 * If requested, save its pertinent mode, owner, and time info.
717 static struct inotab *
718 allocinotab(ino, dip, seekpt)
723 register struct inotab *itp;
724 struct modeinfo node;
726 itp = calloc(1, sizeof(struct inotab));
728 panic("no memory directory table\n");
729 itp->t_next = inotab[INOHASH(ino)];
730 inotab[INOHASH(ino)] = itp;
732 itp->t_seekpt = seekpt;
736 node.timep[0].tv_sec = dip->di_atime;
737 node.timep[0].tv_usec = dip->di_atimensec / 1000;
738 node.timep[1].tv_sec = dip->di_mtime;
739 node.timep[1].tv_usec = dip->di_mtimensec / 1000;
740 node.mode = dip->di_mode;
741 node.flags = dip->di_flags;
742 node.uid = dip->di_uid;
743 node.gid = dip->di_gid;
744 (void) fwrite((char *)&node, 1, sizeof(struct modeinfo), mf);
749 * Look up an inode in the table of directories
751 static struct inotab *
755 register struct inotab *itp;
757 for (itp = inotab[INOHASH(ino)]; itp != NULL; itp = itp->t_next)
758 if (itp->t_ino == ino)
772 if (modefile[0] != '#')
773 (void) unlink(modefile);
774 if (dirfile[0] != '#')
775 (void) unlink(dirfile);