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.24 2003/10/26 16:05:47 stelian Exp $";
49 #include <sys/types.h>
50 #include <sys/param.h>
55 #ifdef HAVE_EXT2FS_EXT2_FS_H
56 #include <ext2fs/ext2_fs.h>
58 #include <linux/ext2_fs.h>
60 #include <bsdcompat.h>
63 #include <sys/fcntl.h>
64 #include <bsdcompat.h>
66 #include <ufs/ufs/dinode.h>
67 #include <ufs/ufs/dir.h>
69 #endif /* __linux__ */
70 #include <protocols/dumprestore.h>
72 #include <compaterr.h>
83 #include <arpa/nameser_compat.h>
85 #include <machine/endian.h>
89 #include "pathnames.h"
94 * Symbol table of directories read from tape.
97 #define INOHASH(val) (val % HASHSIZE)
99 struct inotab *t_next;
104 static struct inotab *inotab[HASHSIZE];
107 * Information retained about directories.
111 struct timeval timep[2];
119 * Definitions for library routines operating on directories.
122 #define DIRBLKSIZ 1024
127 char dd_buf[DIRBLKSIZ];
131 * Global variables for this file.
134 static FILE *df, *mf;
135 static RST_DIR *dirp;
136 static char dirfile[MAXPATHLEN] = "#"; /* No file */
137 static char modefile[MAXPATHLEN] = "#"; /* No file */
138 static char dot[2] = "."; /* So it can be modified */
141 * Format of old style directories.
146 char d_name[ODIRSIZ];
149 #if defined(__linux__) || defined(sunos)
150 static struct inotab *allocinotab __P((dump_ino_t, struct new_bsd_inode *, long));
152 static struct inotab *allocinotab __P((dump_ino_t, struct dinode *, long));
154 static void dcvt __P((struct odirect *, struct direct *));
155 static void flushent __P((void));
156 static struct inotab *inotablookup __P((dump_ino_t));
157 static RST_DIR *opendirfile __P((const char *));
158 static void putdir __P((char *, size_t));
159 static void putent __P((struct direct *));
160 static void rst_seekdir __P((RST_DIR *, long, long));
161 static long rst_telldir __P((RST_DIR *));
162 static struct direct *searchdir __P((dump_ino_t, char *));
169 * Extract directory contents, building up a directory structure
170 * on disk for extraction by name.
171 * If genmode is requested, save mode, owner, and times for all
172 * directories on the tape.
175 extractdirs(int genmode)
178 #if defined(__linux__) || defined(sunos)
179 struct new_bsd_inode *ip;
184 struct direct nulldir;
187 Vprintf(stdout, "Extract directories from tape\n");
188 (void) snprintf(dirfile, sizeof(dirfile), "%s/rstdir%ld", tmpdir,
190 if (command != 'r' && command != 'R') {
191 (void) strncat(dirfile, "-XXXXXX",
192 sizeof(dirfile) - strlen(dirfile));
193 fd = mkstemp(dirfile);
195 fd = open(dirfile, O_RDWR|O_CREAT|O_EXCL, 0666);
196 if (fd == -1 || (df = fdopen(fd, "w")) == NULL) {
199 err(1, "cannot create directory temporary %s", dirfile);
202 (void) snprintf(modefile, sizeof(modefile), "%s/rstmode%ld", tmpdir, (long)dumpdate);
203 if (command != 'r' && command != 'R') {
204 (void) strncat(modefile, "-XXXXXX",
205 sizeof(modefile) - strlen(modefile));
206 fd = mkstemp(modefile);
208 fd = open(modefile, O_RDWR|O_CREAT|O_EXCL, 0666);
209 if (fd == -1 || (mf = fdopen(fd, "w")) == NULL) {
212 err(1, "cannot create modefile %s", modefile);
216 nulldir.d_type = DT_DIR;
217 nulldir.d_namlen = 1;
218 nulldir.d_name[0] = '/';
219 nulldir.d_name[1] = '\0';
220 nulldir.d_reclen = DIRSIZ(0, &nulldir);
222 curfile.name = "<directory file - name unknown>";
223 curfile.action = USING;
225 if (ip == NULL || (ip->di_mode & IFMT) != IFDIR) {
226 if ( fclose(df) == EOF )
227 err(1, "cannot write to file %s", dirfile);
228 dirp = opendirfile(dirfile);
231 if (mf != NULL && fclose(mf) == EOF )
232 err(1, "cannot write to file %s", dirfile);
235 panic("Root directory is not on tape\n");
238 itp = allocinotab(curfile.ino, ip, seekpt);
239 getfile(putdir, xtrnull);
242 itp->t_size = seekpt - itp->t_seekpt;
247 * skip over all the directories on the tape
253 while (curfile.dip && (curfile.dip->di_mode & IFMT) == IFDIR) {
259 * Recursively find names and inumbers of all files in subtree
260 * pname and pass them off to be processed.
263 treescan(char *pname, dump_ino_t ino, long (*todo) __P((char *, dump_ino_t, int)))
269 char locname[MAXPATHLEN + 1];
271 itp = inotablookup(ino);
274 * Pname is name of a simple file or an unchanged directory.
276 (void) (*todo)(pname, ino, LEAF);
280 * Pname is a dumped directory name.
282 if ((*todo)(pname, ino, NODE) == FAIL)
285 * begin search through the directory
286 * skipping over "." and ".."
288 namelen = snprintf(locname, sizeof(locname), "%s/", pname);
289 if (namelen >= (int)sizeof(locname))
290 namelen = sizeof(locname) - 1;
291 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
292 dp = rst_readdir(dirp); /* "." */
293 if (dp != NULL && strcmp(dp->d_name, ".") == 0)
294 dp = rst_readdir(dirp); /* ".." */
296 fprintf(stderr, "Warning: `.' missing from directory %s\n",
298 if (dp != NULL && strcmp(dp->d_name, "..") == 0)
299 dp = rst_readdir(dirp); /* first real entry */
301 fprintf(stderr, "Warning: `..' missing from directory %s\n",
303 bpt = rst_telldir(dirp);
305 * a zero inode signals end of directory
308 locname[namelen] = '\0';
309 if (namelen + dp->d_namlen >= (int)sizeof(locname)) {
310 fprintf(stderr, "%s%s: name exceeds %ld char\n",
311 locname, dp->d_name, (long)sizeof(locname) - 1);
313 (void) strncat(locname, dp->d_name, (int)dp->d_namlen);
314 treescan(locname, dp->d_ino, todo);
315 rst_seekdir(dirp, bpt, itp->t_seekpt);
317 dp = rst_readdir(dirp);
318 bpt = rst_telldir(dirp);
323 * Lookup a pathname which is always assumed to start from the ROOTINO.
326 pathsearch(const char *pathname)
330 char *path, *name, buffer[MAXPATHLEN];
332 strcpy(buffer, pathname);
339 while ((name = strsep(&path, "/")) != NULL && *name /* != NULL */) {
341 while ((name = strtok_r(NULL, "/", &path)) != NULL && *name /* != NULL */) {
343 if ((dp = searchdir(ino, name)) == NULL)
351 * Lookup the requested name in directory inum.
352 * Return its inode number if found, zero if it does not exist.
354 static struct direct *
355 searchdir(dump_ino_t inum, char *name)
361 itp = inotablookup(inum);
364 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
367 dp = rst_readdir(dirp);
370 } while (dp->d_namlen != len || strncmp(dp->d_name, name, len) != 0);
375 * Put the directory entries in the directory file
378 putdir(char *buf, size_t size)
380 struct direct cvtbuf;
382 struct odirect *eodp;
387 eodp = (struct odirect *)&buf[size];
388 for (odp = (struct odirect *)buf; odp < eodp; odp++)
389 if (odp->d_ino != 0) {
394 for (loc = 0; loc < (long)size; ) {
395 dp = (struct direct *)(buf + loc);
397 printf ("reclen = %d, namlen = %d, type = %d\n",
398 dp->d_reclen, dp->d_namlen, dp->d_type);
401 swabst((u_char *)"is", (u_char *) dp);
402 if (oldinofmt && dp->d_ino != 0) {
403 # if BYTE_ORDER == BIG_ENDIAN
405 dp->d_namlen = dp->d_type;
408 dp->d_namlen = dp->d_type;
410 if (dp->d_namlen == 0 && dp->d_type != 0)
411 dp->d_namlen = dp->d_type;
412 dp->d_type = DT_UNKNOWN;
415 printf ("reclen = %d, namlen = %d, type = %d\n",
416 dp->d_reclen, dp->d_namlen, dp->d_type);
418 i = DIRBLKSIZ - (loc & (DIRBLKSIZ - 1));
419 if ((dp->d_reclen & 0x3) != 0 ||
421 dp->d_reclen < DIRSIZ(0, dp) ||
422 dp->d_namlen > MAXNAMLEN) {
423 Vprintf(stdout, "Mangled directory: ");
424 if ((dp->d_reclen & 0x3) != 0)
426 "reclen not multiple of 4 ");
427 if (dp->d_reclen < DIRSIZ(0, dp))
429 "reclen less than DIRSIZ (%d < %d) ",
430 dp->d_reclen, DIRSIZ(0, dp));
431 if (dp->d_namlen > MAXNAMLEN)
433 "reclen name too big (%d > %d) ",
434 dp->d_namlen, MAXNAMLEN);
435 Vprintf(stdout, "\n");
440 if (dp->d_ino != 0) {
448 * These variables are "local" to the following two functions.
450 static char dirbuf[DIRBLKSIZ];
451 static long dirloc = 0;
452 static long prev = 0;
455 * add a new directory entry to a file.
458 putent(struct direct *dp)
460 dp->d_reclen = DIRSIZ(0, dp);
461 if (dirloc + dp->d_reclen > DIRBLKSIZ) {
462 ((struct direct *)(dirbuf + prev))->d_reclen =
464 if ( fwrite(dirbuf, 1, DIRBLKSIZ, df) != DIRBLKSIZ )
465 err(1,"cannot write to file %s", dirfile);
468 memmove(dirbuf + dirloc, dp, (size_t)dp->d_reclen);
470 dirloc += dp->d_reclen;
474 * flush out a directory that is finished.
479 ((struct direct *)(dirbuf + prev))->d_reclen = DIRBLKSIZ - prev;
480 if ( fwrite(dirbuf, (int)dirloc, 1, df) != 1 )
481 err(1, "cannot write to file %s", dirfile);
484 err(1, "cannot write to file %s", dirfile);
489 dcvt(struct odirect *odp, struct direct *ndp)
492 memset(ndp, 0, (size_t)(sizeof *ndp));
493 ndp->d_ino = odp->d_ino;
494 ndp->d_type = DT_UNKNOWN;
495 (void) strncpy(ndp->d_name, odp->d_name, ODIRSIZ);
496 ndp->d_namlen = strlen(ndp->d_name);
497 ndp->d_reclen = DIRSIZ(0, ndp);
501 * Seek to an entry in a directory.
502 * Only values returned by rst_telldir should be passed to rst_seekdir.
503 * This routine handles many directories in a single file.
504 * It takes the base of the directory in the file, plus
505 * the desired seek offset into it.
508 rst_seekdir(RST_DIR *dirp, long loc, long base)
511 if (loc == rst_telldir(dirp))
515 fprintf(stderr, "bad seek pointer to rst_seekdir %ld\n", loc);
516 (void) lseek(dirp->dd_fd, base + (loc & ~(DIRBLKSIZ - 1)), SEEK_SET);
517 dirp->dd_loc = loc & (DIRBLKSIZ - 1);
518 if (dirp->dd_loc != 0)
519 dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ);
523 * get next entry in a directory.
526 rst_readdir(RST_DIR *dirp)
531 if (dirp->dd_loc == 0) {
532 dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf,
534 if (dirp->dd_size <= 0) {
535 Dprintf(stderr, "error reading directory\n");
539 if (dirp->dd_loc >= dirp->dd_size) {
543 dp = (struct direct *)(dirp->dd_buf + dirp->dd_loc);
544 if (dp->d_reclen == 0 ||
545 dp->d_reclen > DIRBLKSIZ + 1 - dirp->dd_loc) {
546 Dprintf(stderr, "corrupted directory: bad reclen %d\n",
550 dirp->dd_loc += dp->d_reclen;
551 if (dp->d_ino == 0 && strcmp(dp->d_name, "/") == 0)
553 if (dp->d_ino >= maxino) {
554 Dprintf(stderr, "corrupted directory: bad inum %d\n",
563 * Simulate the opening of a directory
566 rst_opendir(const char *name)
572 if ((ino = dirlookup(name)) > 0 &&
573 (itp = inotablookup(ino)) != NULL) {
574 dirp = opendirfile(dirfile);
575 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
582 * In our case, there is nothing to do when closing a directory.
585 rst_closedir(RST_DIR *dirp)
588 (void)close(dirp->dd_fd);
594 * Simulate finding the current offset in the directory.
597 rst_telldir(RST_DIR *dirp)
599 return ((long)lseek(dirp->dd_fd,
600 (OFF_T)0, SEEK_CUR) - dirp->dd_size + dirp->dd_loc);
604 * Open a directory file.
607 opendirfile(const char *name)
612 if ((fd = open(name, O_RDONLY)) == -1)
614 if ((dirp = malloc(sizeof(RST_DIR))) == NULL) {
624 * Set the mode, owner, and times for all new or changed directories
627 setdirmodes(int flags)
630 struct modeinfo node;
634 Vprintf(stdout, "Set directory mode, owner, and times.\n");
635 if (command == 'r' || command == 'R')
636 (void) snprintf(modefile, sizeof(modefile), "%s/rstmode%lu", tmpdir, (long)dumpdate);
637 if (modefile[0] == '#') {
638 panic("modefile not defined\n");
639 fprintf(stderr, "directory mode, owner, and times not set\n");
642 mf = fopen(modefile, "r");
645 fprintf(stderr, "cannot open mode file %s\n", modefile);
646 fprintf(stderr, "directory mode, owner, and times not set\n");
651 (void) fread((char *)&node, 1, sizeof(struct modeinfo), mf);
654 ep = lookupino(node.ino);
655 if (command == 'i' || command == 'x') {
658 if ((flags & FORCE) == 0 && ep->e_flags & EXISTED) {
662 if ((flags & FORCE) == 0 &&
663 node.ino == ROOTINO &&
664 reply("set owner/mode for '.'") == FAIL)
668 panic("cannot find directory inode %d\n", node.ino);
671 (void) chown(cp, node.uid, node.gid);
672 (void) chmod(cp, node.mode);
675 (void) fsetflags(cp, node.flags);
679 (void) chflags(cp, node.flags);
682 utimes(cp, node.timep);
687 panic("error setting directory modes\n");
692 * Generate a literal copy of a directory.
695 genliteraldir(char *name, dump_ino_t ino)
698 int ofile, dp, i, size;
701 itp = inotablookup(ino);
703 panic("Cannot find directory inode %d named %s\n", ino, name);
704 if ((ofile = open(name, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
705 warn("%s: cannot create file\n", name);
708 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
709 dp = dup(dirp->dd_fd);
710 for (i = itp->t_size; i > 0; i -= BUFSIZ) {
711 size = i < BUFSIZ ? i : BUFSIZ;
712 if (read(dp, buf, (int) size) == -1) {
713 warnx("write error extracting inode %lu, name %s\n",
714 (unsigned long)curfile.ino, curfile.name);
717 if (!Nflag && write(ofile, buf, (int) size) == -1) {
718 warnx("write error extracting inode %lu, name %s\n",
719 (unsigned long)curfile.ino, curfile.name);
729 * Determine the type of an inode
732 inodetype(dump_ino_t ino)
736 itp = inotablookup(ino);
743 * Allocate and initialize a directory inode entry.
744 * If requested, save its pertinent mode, owner, and time info.
746 static struct inotab *
747 #if defined(__linux__) || defined(sunos)
748 allocinotab(dump_ino_t ino, struct new_bsd_inode *dip, long seekpt)
750 allocinotab(dump_ino_t ino, struct dinode *dip, long seekpt)
754 struct modeinfo node;
756 itp = calloc(1, sizeof(struct inotab));
758 panic("no memory directory table\n");
759 itp->t_next = inotab[INOHASH(ino)];
760 inotab[INOHASH(ino)] = itp;
762 itp->t_seekpt = seekpt;
766 #if defined(__linux__) || defined(sunos)
767 node.timep[0].tv_sec = dip->di_atime.tv_sec;
768 node.timep[0].tv_usec = dip->di_atime.tv_usec;
769 node.timep[1].tv_sec = dip->di_mtime.tv_sec;
770 node.timep[1].tv_usec = dip->di_mtime.tv_usec;
771 #else /* __linux__ || sunos */
772 node.timep[0].tv_sec = dip->di_atime;
773 node.timep[0].tv_usec = dip->di_atimensec / 1000;
774 node.timep[1].tv_sec = dip->di_mtime;
775 node.timep[1].tv_usec = dip->di_mtimensec / 1000;
776 #endif /* __linux__ || sunos */
777 node.mode = dip->di_mode;
778 node.flags = dip->di_flags;
779 node.uid = dip->di_uid;
780 node.gid = dip->di_gid;
781 if ( fwrite((char *)&node, 1, sizeof(struct modeinfo), mf) != sizeof(struct modeinfo) )
782 err(1,"cannot write to file %s", modefile);
787 * Look up an inode in the table of directories
789 static struct inotab *
790 inotablookup(dump_ino_t ino)
794 for (itp = inotab[INOHASH(ino)]; itp != NULL; itp = itp->t_next)
795 if (itp->t_ino == ino)
807 if (modefile[0] != '#')
808 (void) unlink(modefile);
809 if (dirfile[0] != '#')
810 (void) unlink(dirfile);