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 <stelian@popies.net>, 1999-2000
6 * Stelian Pop <stelian@popies.net> - AlcĂ´ve <www.alcove.com>, 2000-2002
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. Neither the name of the University nor the names of its contributors
27 * may be used to endorse or promote products derived from this software
28 * without specific prior written permission.
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 static const char rcsid[] =
45 "$Id: dirs.c,v 1.23 2003/03/30 15:40:38 stelian Exp $";
49 #include <sys/param.h>
54 #ifdef HAVE_EXT2FS_EXT2_FS_H
55 #include <ext2fs/ext2_fs.h>
57 #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];
139 #if defined(__linux__) || defined(sunos)
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)
164 #if defined(__linux__) || defined(sunos)
165 struct new_bsd_inode *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)))
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 >= (int)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 >= (int)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);
325 while ((name = strsep(&path, "/")) != NULL && *name /* != NULL */) {
327 while ((name = strtok_r(NULL, "/", &path)) != NULL && *name /* != NULL */) {
329 if ((dp = searchdir(ino, name)) == NULL)
337 * Lookup the requested name in directory inum.
338 * Return its inode number if found, zero if it does not exist.
340 static struct direct *
341 searchdir(dump_ino_t inum, char *name)
347 itp = inotablookup(inum);
350 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
353 dp = rst_readdir(dirp);
356 } while (dp->d_namlen != len || strncmp(dp->d_name, name, len) != 0);
361 * Put the directory entries in the directory file
364 putdir(char *buf, size_t size)
366 struct direct cvtbuf;
368 struct odirect *eodp;
373 eodp = (struct odirect *)&buf[size];
374 for (odp = (struct odirect *)buf; odp < eodp; odp++)
375 if (odp->d_ino != 0) {
380 for (loc = 0; loc < (long)size; ) {
381 dp = (struct direct *)(buf + loc);
383 printf ("reclen = %d, namlen = %d, type = %d\n",
384 dp->d_reclen, dp->d_namlen, dp->d_type);
387 swabst((u_char *)"is", (u_char *) dp);
388 if (oldinofmt && dp->d_ino != 0) {
389 # if BYTE_ORDER == BIG_ENDIAN
391 dp->d_namlen = dp->d_type;
394 dp->d_namlen = dp->d_type;
396 if (dp->d_namlen == 0 && dp->d_type != 0)
397 dp->d_namlen = dp->d_type;
398 dp->d_type = DT_UNKNOWN;
401 printf ("reclen = %d, namlen = %d, type = %d\n",
402 dp->d_reclen, dp->d_namlen, dp->d_type);
404 i = DIRBLKSIZ - (loc & (DIRBLKSIZ - 1));
405 if ((dp->d_reclen & 0x3) != 0 ||
407 dp->d_reclen < DIRSIZ(0, dp) ||
408 dp->d_namlen > MAXNAMLEN) {
409 Vprintf(stdout, "Mangled directory: ");
410 if ((dp->d_reclen & 0x3) != 0)
412 "reclen not multiple of 4 ");
413 if (dp->d_reclen < DIRSIZ(0, dp))
415 "reclen less than DIRSIZ (%d < %d) ",
416 dp->d_reclen, DIRSIZ(0, dp));
417 if (dp->d_namlen > MAXNAMLEN)
419 "reclen name too big (%d > %d) ",
420 dp->d_namlen, MAXNAMLEN);
421 Vprintf(stdout, "\n");
426 if (dp->d_ino != 0) {
434 * These variables are "local" to the following two functions.
436 static char dirbuf[DIRBLKSIZ];
437 static long dirloc = 0;
438 static long prev = 0;
441 * add a new directory entry to a file.
444 putent(struct direct *dp)
446 dp->d_reclen = DIRSIZ(0, dp);
447 if (dirloc + dp->d_reclen > DIRBLKSIZ) {
448 ((struct direct *)(dirbuf + prev))->d_reclen =
450 if ( fwrite(dirbuf, 1, DIRBLKSIZ, df) != DIRBLKSIZ )
451 err(1,"cannot write to file %s", dirfile);
454 memmove(dirbuf + dirloc, dp, (size_t)dp->d_reclen);
456 dirloc += dp->d_reclen;
460 * flush out a directory that is finished.
465 ((struct direct *)(dirbuf + prev))->d_reclen = DIRBLKSIZ - prev;
466 if ( fwrite(dirbuf, (int)dirloc, 1, df) != 1 )
467 err(1, "cannot write to file %s", dirfile);
470 err(1, "cannot write to file %s", dirfile);
475 dcvt(struct odirect *odp, struct direct *ndp)
478 memset(ndp, 0, (size_t)(sizeof *ndp));
479 ndp->d_ino = odp->d_ino;
480 ndp->d_type = DT_UNKNOWN;
481 (void) strncpy(ndp->d_name, odp->d_name, ODIRSIZ);
482 ndp->d_namlen = strlen(ndp->d_name);
483 ndp->d_reclen = DIRSIZ(0, ndp);
487 * Seek to an entry in a directory.
488 * Only values returned by rst_telldir should be passed to rst_seekdir.
489 * This routine handles many directories in a single file.
490 * It takes the base of the directory in the file, plus
491 * the desired seek offset into it.
494 rst_seekdir(RST_DIR *dirp, long loc, long base)
497 if (loc == rst_telldir(dirp))
501 fprintf(stderr, "bad seek pointer to rst_seekdir %ld\n", loc);
502 (void) lseek(dirp->dd_fd, base + (loc & ~(DIRBLKSIZ - 1)), SEEK_SET);
503 dirp->dd_loc = loc & (DIRBLKSIZ - 1);
504 if (dirp->dd_loc != 0)
505 dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ);
509 * get next entry in a directory.
512 rst_readdir(RST_DIR *dirp)
517 if (dirp->dd_loc == 0) {
518 dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf,
520 if (dirp->dd_size <= 0) {
521 Dprintf(stderr, "error reading directory\n");
525 if (dirp->dd_loc >= dirp->dd_size) {
529 dp = (struct direct *)(dirp->dd_buf + dirp->dd_loc);
530 if (dp->d_reclen == 0 ||
531 dp->d_reclen > DIRBLKSIZ + 1 - dirp->dd_loc) {
532 Dprintf(stderr, "corrupted directory: bad reclen %d\n",
536 dirp->dd_loc += dp->d_reclen;
537 if (dp->d_ino == 0 && strcmp(dp->d_name, "/") == 0)
539 if (dp->d_ino >= maxino) {
540 Dprintf(stderr, "corrupted directory: bad inum %d\n",
549 * Simulate the opening of a directory
552 rst_opendir(const char *name)
558 if ((ino = dirlookup(name)) > 0 &&
559 (itp = inotablookup(ino)) != NULL) {
560 dirp = opendirfile(dirfile);
561 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
568 * In our case, there is nothing to do when closing a directory.
571 rst_closedir(RST_DIR *dirp)
574 (void)close(dirp->dd_fd);
580 * Simulate finding the current offset in the directory.
583 rst_telldir(RST_DIR *dirp)
585 return ((long)lseek(dirp->dd_fd,
586 (OFF_T)0, SEEK_CUR) - dirp->dd_size + dirp->dd_loc);
590 * Open a directory file.
593 opendirfile(const char *name)
598 if ((fd = open(name, O_RDONLY)) == -1)
600 if ((dirp = malloc(sizeof(RST_DIR))) == NULL) {
610 * Set the mode, owner, and times for all new or changed directories
613 setdirmodes(int flags)
616 struct modeinfo node;
620 Vprintf(stdout, "Set directory mode, owner, and times.\n");
621 if (command == 'r' || command == 'R')
622 (void) snprintf(modefile, sizeof(modefile), "%s/rstmode%lu", tmpdir, (long)dumpdate);
623 if (modefile[0] == '#') {
624 panic("modefile not defined\n");
625 fprintf(stderr, "directory mode, owner, and times not set\n");
628 mf = fopen(modefile, "r");
631 fprintf(stderr, "cannot open mode file %s\n", modefile);
632 fprintf(stderr, "directory mode, owner, and times not set\n");
637 (void) fread((char *)&node, 1, sizeof(struct modeinfo), mf);
640 ep = lookupino(node.ino);
641 if (command == 'i' || command == 'x') {
644 if ((flags & FORCE) == 0 && ep->e_flags & EXISTED) {
648 if ((flags & FORCE) == 0 &&
649 node.ino == ROOTINO &&
650 reply("set owner/mode for '.'") == FAIL)
654 panic("cannot find directory inode %d\n", node.ino);
657 (void) chown(cp, node.uid, node.gid);
658 (void) chmod(cp, node.mode);
661 (void) fsetflags(cp, node.flags);
663 (void) chflags(cp, node.flags);
665 utimes(cp, node.timep);
670 panic("error setting directory modes\n");
675 * Generate a literal copy of a directory.
678 genliteraldir(char *name, dump_ino_t ino)
681 int ofile, dp, i, size;
684 itp = inotablookup(ino);
686 panic("Cannot find directory inode %d named %s\n", ino, name);
687 if ((ofile = open(name, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
688 warn("%s: cannot create file\n", name);
691 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
692 dp = dup(dirp->dd_fd);
693 for (i = itp->t_size; i > 0; i -= BUFSIZ) {
694 size = i < BUFSIZ ? i : BUFSIZ;
695 if (read(dp, buf, (int) size) == -1) {
696 warnx("write error extracting inode %lu, name %s\n",
697 (unsigned long)curfile.ino, curfile.name);
700 if (!Nflag && write(ofile, buf, (int) size) == -1) {
701 warnx("write error extracting inode %lu, name %s\n",
702 (unsigned long)curfile.ino, curfile.name);
712 * Determine the type of an inode
715 inodetype(dump_ino_t ino)
719 itp = inotablookup(ino);
726 * Allocate and initialize a directory inode entry.
727 * If requested, save its pertinent mode, owner, and time info.
729 static struct inotab *
731 allocinotab(dump_ino_t ino, struct new_bsd_inode *dip, long seekpt)
733 allocinotab(dump_ino_t ino, struct dinode *dip, long seekpt)
737 struct modeinfo node;
739 itp = calloc(1, sizeof(struct inotab));
741 panic("no memory directory table\n");
742 itp->t_next = inotab[INOHASH(ino)];
743 inotab[INOHASH(ino)] = itp;
745 itp->t_seekpt = seekpt;
750 node.timep[0].tv_sec = dip->di_atime.tv_sec;
751 node.timep[0].tv_usec = dip->di_atime.tv_usec;
752 node.timep[1].tv_sec = dip->di_mtime.tv_sec;
753 node.timep[1].tv_usec = dip->di_mtime.tv_usec;
754 #else /* __linux__ */
755 node.timep[0].tv_sec = dip->di_atime;
756 node.timep[0].tv_usec = dip->di_atimensec / 1000;
757 node.timep[1].tv_sec = dip->di_mtime;
758 node.timep[1].tv_usec = dip->di_mtimensec / 1000;
759 #endif /* __linux__ */
760 node.mode = dip->di_mode;
761 node.flags = dip->di_flags;
762 node.uid = dip->di_uid;
763 node.gid = dip->di_gid;
764 if ( fwrite((char *)&node, 1, sizeof(struct modeinfo), mf) != sizeof(struct modeinfo) )
765 err(1,"cannot write to file %s", modefile);
770 * Look up an inode in the table of directories
772 static struct inotab *
773 inotablookup(dump_ino_t ino)
777 for (itp = inotab[INOHASH(ino)]; itp != NULL; itp = itp->t_next)
778 if (itp->t_ino == ino)
790 if (modefile[0] != '#')
791 (void) unlink(modefile);
792 if (dirfile[0] != '#')
793 (void) unlink(dirfile);