2 * Ported to Linux's Second Extended File System as part of the
3 * dump and restore backup suit
4 * Remy Card <card@Linux.EU.Org>, 1994-1997
5 * Stelian Pop <pop@noos.fr>, 1999-2000
6 * Stelian Pop <pop@noos.fr> - AlcĂ´ve <www.alcove.fr>, 2000
10 * Copyright (c) 1983, 1993
11 * The Regents of the University of California. All rights reserved.
12 * (c) UNIX System Laboratories, Inc.
13 * All or some portions of this file are derived from material licensed
14 * to the University of California by American Telephone and Telegraph
15 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
16 * the permission of UNIX System Laboratories, Inc.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 3. All advertising materials mentioning features or use of this software
27 * must display the following acknowledgement:
28 * This product includes software developed by the University of
29 * California, Berkeley and its contributors.
30 * 4. Neither the name of the University nor the names of its contributors
31 * may be used to endorse or promote products derived from this software
32 * without specific prior written permission.
34 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
35 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 static const char rcsid[] =
49 "$Id: dirs.c,v 1.14 2001/03/20 10:02:48 stelian Exp $";
53 #include <sys/param.h>
58 #include <linux/ext2_fs.h>
59 #include <bsdcompat.h>
61 #include <ufs/ufs/dinode.h>
62 #include <ufs/ufs/dir.h>
63 #endif /* __linux__ */
64 #include <protocols/dumprestore.h>
66 #include <compaterr.h>
76 #include <machine/endian.h>
79 #include "pathnames.h"
84 * Symbol table of directories read from tape.
87 #define INOHASH(val) (val % HASHSIZE)
89 struct inotab *t_next;
94 static struct inotab *inotab[HASHSIZE];
97 * Information retained about directories.
101 struct timeval timep[2];
109 * Definitions for library routines operating on directories.
112 #define DIRBLKSIZ 1024
117 char dd_buf[DIRBLKSIZ];
121 * Global variables for this file.
124 static FILE *df, *mf;
125 static RST_DIR *dirp;
126 static char dirfile[MAXPATHLEN] = "#"; /* No file */
127 static char modefile[MAXPATHLEN] = "#"; /* No file */
128 static char dot[2] = "."; /* So it can be modified */
131 * Format of old style directories.
136 char d_name[ODIRSIZ];
140 static struct inotab *allocinotab __P((dump_ino_t, struct new_bsd_inode *, long));
142 static struct inotab *allocinotab __P((dump_ino_t, struct dinode *, long));
144 static void dcvt __P((struct odirect *, struct direct *));
145 static void flushent __P((void));
146 static struct inotab *inotablookup __P((dump_ino_t));
147 static RST_DIR *opendirfile __P((const char *));
148 static void putdir __P((char *, size_t));
149 static void putent __P((struct direct *));
150 static void rst_seekdir __P((RST_DIR *, long, long));
151 static long rst_telldir __P((RST_DIR *));
152 static struct direct *searchdir __P((dump_ino_t, char *));
155 * Extract directory contents, building up a directory structure
156 * on disk for extraction by name.
157 * If genmode is requested, save mode, owner, and times for all
158 * directories on the tape.
161 extractdirs(int genmode)
165 register struct new_bsd_inode *ip;
167 register struct dinode *ip;
170 struct direct nulldir;
173 Vprintf(stdout, "Extract directories from tape\n");
174 (void) snprintf(dirfile, sizeof(dirfile), "%s/rstdir%ld", tmpdir,
176 if (command != 'r' && command != 'R') {
177 (void) strncat(dirfile, "-XXXXXX",
178 sizeof(dirfile) - strlen(dirfile));
179 fd = mkstemp(dirfile);
181 fd = open(dirfile, O_RDWR|O_CREAT|O_EXCL, 0666);
182 if (fd == -1 || (df = fdopen(fd, "w")) == NULL) {
185 err(1, "cannot create directory temporary %s", dirfile);
188 (void) snprintf(modefile, sizeof(modefile), "%s/rstmode%ld", tmpdir, (long)dumpdate);
189 if (command != 'r' && command != 'R') {
190 (void) strncat(modefile, "-XXXXXX",
191 sizeof(modefile) - strlen(modefile));
192 fd = mkstemp(modefile);
194 fd = open(modefile, O_RDWR|O_CREAT|O_EXCL, 0666);
195 if (fd == -1 || (mf = fdopen(fd, "w")) == NULL) {
198 err(1, "cannot create modefile %s", modefile);
202 nulldir.d_type = DT_DIR;
203 nulldir.d_namlen = 1;
204 nulldir.d_name[0] = '/';
205 nulldir.d_name[1] = '\0';
206 nulldir.d_reclen = DIRSIZ(0, &nulldir);
208 curfile.name = "<directory file - name unknown>";
209 curfile.action = USING;
211 if (ip == NULL || (ip->di_mode & IFMT) != IFDIR) {
212 if ( fclose(df) == EOF )
213 err(1, "cannot write to file %s", dirfile);
214 dirp = opendirfile(dirfile);
217 if (mf != NULL && fclose(mf) == EOF )
218 err(1, "cannot write to file %s", dirfile);
221 panic("Root directory is not on tape\n");
224 itp = allocinotab(curfile.ino, ip, seekpt);
225 getfile(putdir, xtrnull);
228 itp->t_size = seekpt - itp->t_seekpt;
233 * skip over all the directories on the tape
239 while (curfile.dip && (curfile.dip->di_mode & IFMT) == IFDIR) {
245 * Recursively find names and inumbers of all files in subtree
246 * pname and pass them off to be processed.
249 treescan(char *pname, dump_ino_t ino, long (*todo) __P((char *, dump_ino_t, int)))
251 register struct inotab *itp;
252 register struct direct *dp;
255 char locname[MAXPATHLEN + 1];
257 itp = inotablookup(ino);
260 * Pname is name of a simple file or an unchanged directory.
262 (void) (*todo)(pname, ino, LEAF);
266 * Pname is a dumped directory name.
268 if ((*todo)(pname, ino, NODE) == FAIL)
271 * begin search through the directory
272 * skipping over "." and ".."
274 namelen = snprintf(locname, sizeof(locname), "%s/", pname);
275 if (namelen >= sizeof(locname))
276 namelen = sizeof(locname) - 1;
277 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
278 dp = rst_readdir(dirp); /* "." */
279 if (dp != NULL && strcmp(dp->d_name, ".") == 0)
280 dp = rst_readdir(dirp); /* ".." */
282 fprintf(stderr, "Warning: `.' missing from directory %s\n",
284 if (dp != NULL && strcmp(dp->d_name, "..") == 0)
285 dp = rst_readdir(dirp); /* first real entry */
287 fprintf(stderr, "Warning: `..' missing from directory %s\n",
289 bpt = rst_telldir(dirp);
291 * a zero inode signals end of directory
294 locname[namelen] = '\0';
295 if (namelen + dp->d_namlen >= sizeof(locname)) {
296 fprintf(stderr, "%s%s: name exceeds %ld char\n",
297 locname, dp->d_name, (long)sizeof(locname) - 1);
299 (void) strncat(locname, dp->d_name, (int)dp->d_namlen);
300 treescan(locname, dp->d_ino, todo);
301 rst_seekdir(dirp, bpt, itp->t_seekpt);
303 dp = rst_readdir(dirp);
304 bpt = rst_telldir(dirp);
309 * Lookup a pathname which is always assumed to start from the ROOTINO.
312 pathsearch(const char *pathname)
316 char *path, *name, buffer[MAXPATHLEN];
318 strcpy(buffer, pathname);
324 while ((name = strsep(&path, "/")) != NULL && *name /* != NULL */) {
325 if ((dp = searchdir(ino, name)) == NULL)
333 * Lookup the requested name in directory inum.
334 * Return its inode number if found, zero if it does not exist.
336 static struct direct *
337 searchdir(dump_ino_t inum, char *name)
339 register struct direct *dp;
340 register struct inotab *itp;
343 itp = inotablookup(inum);
346 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
349 dp = rst_readdir(dirp);
352 } while (dp->d_namlen != len || strncmp(dp->d_name, name, len) != 0);
357 * Put the directory entries in the directory file
360 putdir(char *buf, size_t size)
362 struct direct cvtbuf;
363 register struct odirect *odp;
364 struct odirect *eodp;
365 register struct direct *dp;
369 eodp = (struct odirect *)&buf[size];
370 for (odp = (struct odirect *)buf; odp < eodp; odp++)
371 if (odp->d_ino != 0) {
376 for (loc = 0; loc < size; ) {
377 dp = (struct direct *)(buf + loc);
379 printf ("reclen = %d, namlen = %d, type = %d\n",
380 dp->d_reclen, dp->d_namlen, dp->d_type);
383 swabst((u_char *)"is", (u_char *) dp);
384 if (oldinofmt && dp->d_ino != 0) {
385 # if BYTE_ORDER == BIG_ENDIAN
387 dp->d_namlen = dp->d_type;
390 dp->d_namlen = dp->d_type;
392 dp->d_type = DT_UNKNOWN;
395 printf ("reclen = %d, namlen = %d, type = %d\n",
396 dp->d_reclen, dp->d_namlen, dp->d_type);
398 i = DIRBLKSIZ - (loc & (DIRBLKSIZ - 1));
399 if ((dp->d_reclen & 0x3) != 0 ||
401 dp->d_reclen < DIRSIZ(0, dp) ||
402 dp->d_namlen > MAXNAMLEN) {
403 Vprintf(stdout, "Mangled directory: ");
404 if ((dp->d_reclen & 0x3) != 0)
406 "reclen not multiple of 4 ");
407 if (dp->d_reclen < DIRSIZ(0, dp))
409 "reclen less than DIRSIZ (%d < %d) ",
410 dp->d_reclen, DIRSIZ(0, dp));
411 if (dp->d_namlen > MAXNAMLEN)
413 "reclen name too big (%d > %d) ",
414 dp->d_namlen, MAXNAMLEN);
415 Vprintf(stdout, "\n");
420 if (dp->d_ino != 0) {
428 * These variables are "local" to the following two functions.
430 char dirbuf[DIRBLKSIZ];
435 * add a new directory entry to a file.
438 putent(struct direct *dp)
440 dp->d_reclen = DIRSIZ(0, dp);
441 if (dirloc + dp->d_reclen > DIRBLKSIZ) {
442 ((struct direct *)(dirbuf + prev))->d_reclen =
444 if ( fwrite(dirbuf, 1, DIRBLKSIZ, df) != DIRBLKSIZ )
445 err(1,"cannot write to file %s", dirfile);
448 memmove(dirbuf + dirloc, dp, (size_t)dp->d_reclen);
450 dirloc += dp->d_reclen;
454 * flush out a directory that is finished.
459 ((struct direct *)(dirbuf + prev))->d_reclen = DIRBLKSIZ - prev;
460 if ( fwrite(dirbuf, (int)dirloc, 1, df) != 1 )
461 err(1, "cannot write to file %s", dirfile);
464 err(1, "cannot write to file %s", dirfile);
469 dcvt(struct odirect *odp, struct direct *ndp)
472 memset(ndp, 0, (size_t)(sizeof *ndp));
473 ndp->d_ino = odp->d_ino;
474 ndp->d_type = DT_UNKNOWN;
475 (void) strncpy(ndp->d_name, odp->d_name, ODIRSIZ);
476 ndp->d_namlen = strlen(ndp->d_name);
477 ndp->d_reclen = DIRSIZ(0, ndp);
481 * Seek to an entry in a directory.
482 * Only values returned by rst_telldir should be passed to rst_seekdir.
483 * This routine handles many directories in a single file.
484 * It takes the base of the directory in the file, plus
485 * the desired seek offset into it.
488 rst_seekdir(RST_DIR *dirp, long loc, long base)
491 if (loc == rst_telldir(dirp))
495 fprintf(stderr, "bad seek pointer to rst_seekdir %ld\n", loc);
496 (void) lseek(dirp->dd_fd, base + (loc & ~(DIRBLKSIZ - 1)), SEEK_SET);
497 dirp->dd_loc = loc & (DIRBLKSIZ - 1);
498 if (dirp->dd_loc != 0)
499 dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ);
503 * get next entry in a directory.
506 rst_readdir(RST_DIR *dirp)
508 register struct direct *dp;
511 if (dirp->dd_loc == 0) {
512 dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf,
514 if (dirp->dd_size <= 0) {
515 Dprintf(stderr, "error reading directory\n");
519 if (dirp->dd_loc >= dirp->dd_size) {
523 dp = (struct direct *)(dirp->dd_buf + dirp->dd_loc);
524 if (dp->d_reclen == 0 ||
525 dp->d_reclen > DIRBLKSIZ + 1 - dirp->dd_loc) {
526 Dprintf(stderr, "corrupted directory: bad reclen %d\n",
530 dirp->dd_loc += dp->d_reclen;
531 if (dp->d_ino == 0 && strcmp(dp->d_name, "/") == 0)
533 if (dp->d_ino >= maxino) {
534 Dprintf(stderr, "corrupted directory: bad inum %d\n",
543 * Simulate the opening of a directory
546 rst_opendir(const char *name)
552 if ((ino = dirlookup(name)) > 0 &&
553 (itp = inotablookup(ino)) != NULL) {
554 dirp = opendirfile(dirfile);
555 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
562 * In our case, there is nothing to do when closing a directory.
565 rst_closedir(RST_DIR *dirp)
568 (void)close(dirp->dd_fd);
574 * Simulate finding the current offset in the directory.
577 rst_telldir(RST_DIR *dirp)
579 return ((long)lseek(dirp->dd_fd,
580 (off_t)0, SEEK_CUR) - dirp->dd_size + dirp->dd_loc);
584 * Open a directory file.
587 opendirfile(const char *name)
589 register RST_DIR *dirp;
592 if ((fd = open(name, O_RDONLY)) == -1)
594 if ((dirp = malloc(sizeof(RST_DIR))) == NULL) {
604 * Set the mode, owner, and times for all new or changed directories
607 setdirmodes(int flags)
610 struct modeinfo node;
614 Vprintf(stdout, "Set directory mode, owner, and times.\n");
615 if (command == 'r' || command == 'R')
616 (void) snprintf(modefile, sizeof(modefile), "%s/rstmode%lu", tmpdir, (long)dumpdate);
617 if (modefile[0] == '#') {
618 panic("modefile not defined\n");
619 fprintf(stderr, "directory mode, owner, and times not set\n");
622 mf = fopen(modefile, "r");
625 fprintf(stderr, "cannot open mode file %s\n", modefile);
626 fprintf(stderr, "directory mode, owner, and times not set\n");
631 (void) fread((char *)&node, 1, sizeof(struct modeinfo), mf);
634 ep = lookupino(node.ino);
635 if (command == 'i' || command == 'x') {
638 if ((flags & FORCE) == 0 && ep->e_flags & EXISTED) {
642 if (node.ino == ROOTINO &&
643 reply("set owner/mode for '.'") == FAIL)
647 panic("cannot find directory inode %d\n", node.ino);
650 (void) chown(cp, node.uid, node.gid);
651 (void) chmod(cp, node.mode);
654 (void) fsetflags(cp, node.flags);
656 (void) chflags(cp, node.flags);
658 utimes(cp, node.timep);
663 panic("error setting directory modes\n");
668 * Generate a literal copy of a directory.
671 genliteraldir(char *name, dump_ino_t ino)
673 register struct inotab *itp;
674 int ofile, dp, i, size;
677 itp = inotablookup(ino);
679 panic("Cannot find directory inode %d named %s\n", ino, name);
680 if ((ofile = open(name, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
681 warn("%s: cannot create file\n", name);
684 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
685 dp = dup(dirp->dd_fd);
686 for (i = itp->t_size; i > 0; i -= BUFSIZ) {
687 size = i < BUFSIZ ? i : BUFSIZ;
688 if (read(dp, buf, (int) size) == -1) {
689 warnx("write error extracting inode %lu, name %s\n",
690 (unsigned long)curfile.ino, curfile.name);
693 if (!Nflag && write(ofile, buf, (int) size) == -1) {
694 warnx("write error extracting inode %lu, name %s\n",
695 (unsigned long)curfile.ino, curfile.name);
705 * Determine the type of an inode
708 inodetype(dump_ino_t ino)
712 itp = inotablookup(ino);
719 * Allocate and initialize a directory inode entry.
720 * If requested, save its pertinent mode, owner, and time info.
722 static struct inotab *
724 allocinotab(dump_ino_t ino, struct new_bsd_inode *dip, long seekpt)
726 allocinotab(dump_ino_t ino, struct dinode *dip, long seekpt)
729 register struct inotab *itp;
730 struct modeinfo node;
732 itp = calloc(1, sizeof(struct inotab));
734 panic("no memory directory table\n");
735 itp->t_next = inotab[INOHASH(ino)];
736 inotab[INOHASH(ino)] = itp;
738 itp->t_seekpt = seekpt;
743 node.timep[0].tv_sec = dip->di_atime.tv_sec;
744 node.timep[0].tv_usec = dip->di_atime.tv_usec;
745 node.timep[1].tv_sec = dip->di_mtime.tv_sec;
746 node.timep[1].tv_usec = dip->di_mtime.tv_usec;
747 #else /* __linux__ */
748 node.timep[0].tv_sec = dip->di_atime;
749 node.timep[0].tv_usec = dip->di_atimensec / 1000;
750 node.timep[1].tv_sec = dip->di_mtime;
751 node.timep[1].tv_usec = dip->di_mtimensec / 1000;
752 #endif /* __linux__ */
753 node.mode = dip->di_mode;
754 node.flags = dip->di_flags;
755 node.uid = dip->di_uid;
756 node.gid = dip->di_gid;
757 if ( fwrite((char *)&node, 1, sizeof(struct modeinfo), mf) != sizeof(struct modeinfo) )
758 err(1,"cannot write to file %s", modefile);
763 * Look up an inode in the table of directories
765 static struct inotab *
766 inotablookup(dump_ino_t ino)
768 register struct inotab *itp;
770 for (itp = inotab[INOHASH(ino)]; itp != NULL; itp = itp->t_next)
771 if (itp->t_ino == ino)
783 if (modefile[0] != '#')
784 (void) unlink(modefile);
785 if (dirfile[0] != '#')
786 (void) unlink(dirfile);