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.28 2004/05/25 10:39:30 stelian Exp $";
49 #include <compatlfs.h>
50 #include <sys/types.h>
51 #include <sys/param.h>
56 #ifdef HAVE_EXT2FS_EXT2_FS_H
57 #include <ext2fs/ext2_fs.h>
59 #include <linux/ext2_fs.h>
61 #include <bsdcompat.h>
64 #include <sys/fcntl.h>
65 #include <bsdcompat.h>
67 #include <ufs/ufs/dinode.h>
68 #include <ufs/ufs/dir.h>
70 #endif /* __linux__ */
71 #include <protocols/dumprestore.h>
73 #include <compaterr.h>
84 #include <arpa/nameser_compat.h>
86 #include <machine/endian.h>
90 #include "pathnames.h"
95 * Symbol table of directories read from tape.
98 #define INOHASH(val) (val % HASHSIZE)
100 struct inotab *t_next;
105 static struct inotab *inotab[HASHSIZE];
108 * Information retained about directories.
112 struct timeval timep[2];
120 * Definitions for library routines operating on directories.
123 #define DIRBLKSIZ 1024
128 char dd_buf[DIRBLKSIZ];
132 * Global variables for this file.
135 static FILE *df, *mf;
136 static RST_DIR *dirp;
137 static char dirfile[MAXPATHLEN] = "#"; /* No file */
138 static char modefile[MAXPATHLEN] = "#"; /* No file */
139 static char dot[2] = "."; /* So it can be modified */
142 * Format of old style directories.
147 char d_name[ODIRSIZ];
150 #if defined(__linux__) || defined(sunos)
151 static struct inotab *allocinotab __P((dump_ino_t, struct new_bsd_inode *, OFF_T));
153 static struct inotab *allocinotab __P((dump_ino_t, struct dinode *, OFF_T));
155 static void dcvt __P((struct odirect *, struct direct *));
156 static void flushent __P((void));
157 static struct inotab *inotablookup __P((dump_ino_t));
158 static RST_DIR *opendirfile __P((const char *));
159 static void putdir __P((char *, size_t));
160 static void putent __P((struct direct *));
161 static void rst_seekdir __P((RST_DIR *, OFF_T, OFF_T));
162 static OFF_T rst_telldir __P((RST_DIR *));
163 static struct direct *searchdir __P((dump_ino_t, char *));
170 * Extract directory contents, building up a directory structure
171 * on disk for extraction by name.
172 * If genmode is requested, save mode, owner, and times for all
173 * directories on the tape.
176 extractdirs(int genmode)
179 #if defined(__linux__) || defined(sunos)
180 struct new_bsd_inode *ip;
185 struct direct nulldir;
188 Vprintf(stdout, "Extract directories from tape\n");
189 (void) snprintf(dirfile, sizeof(dirfile), "%s/rstdir%ld", tmpdir,
191 if (command != 'r' && command != 'R') {
192 (void) strncat(dirfile, "-XXXXXX",
193 sizeof(dirfile) - strlen(dirfile));
194 fd = MKSTEMP(dirfile);
196 fd = OPEN(dirfile, O_RDWR|O_CREAT|O_EXCL, 0666);
197 if (fd == -1 || (df = fdopen(fd, "w")) == NULL) {
200 err(1, "cannot create directory temporary %s", dirfile);
203 (void) snprintf(modefile, sizeof(modefile), "%s/rstmode%ld", tmpdir, (long)dumpdate);
204 if (command != 'r' && command != 'R') {
205 (void) strncat(modefile, "-XXXXXX",
206 sizeof(modefile) - strlen(modefile));
207 fd = MKSTEMP(modefile);
209 fd = OPEN(modefile, O_RDWR|O_CREAT|O_EXCL, 0666);
210 if (fd == -1 || (mf = fdopen(fd, "w")) == NULL) {
213 err(1, "cannot create modefile %s", modefile);
217 nulldir.d_type = DT_DIR;
218 nulldir.d_namlen = 1;
219 nulldir.d_name[0] = '/';
220 nulldir.d_name[1] = '\0';
221 nulldir.d_reclen = DIRSIZ(0, &nulldir);
223 curfile.name = "<directory file - name unknown>";
224 curfile.action = USING;
226 if (ip == NULL || (ip->di_mode & IFMT) != IFDIR) {
227 if ( fclose(df) == EOF )
228 err(1, "cannot write to file %s", dirfile);
229 dirp = opendirfile(dirfile);
232 if (mf != NULL && fclose(mf) == EOF )
233 err(1, "cannot write to file %s", dirfile);
236 panic("Root directory is not on tape\n");
239 itp = allocinotab(curfile.ino, ip, seekpt);
240 getfile(putdir, xtrnull);
243 itp->t_size = seekpt - itp->t_seekpt;
248 * skip over all the directories on the tape
254 while (curfile.dip && (curfile.dip->di_mode & IFMT) == IFDIR) {
260 * Recursively find names and inumbers of all files in subtree
261 * pname and pass them off to be processed.
264 treescan(char *pname, dump_ino_t ino, long (*todo) __P((char *, dump_ino_t, int)))
270 char locname[MAXPATHLEN + 1];
272 itp = inotablookup(ino);
275 * Pname is name of a simple file or an unchanged directory.
277 (void) (*todo)(pname, ino, LEAF);
281 * Pname is a dumped directory name.
283 if ((*todo)(pname, ino, NODE) == FAIL)
286 * begin search through the directory
287 * skipping over "." and ".."
289 namelen = snprintf(locname, sizeof(locname), "%s/", pname);
290 if (namelen >= (int)sizeof(locname))
291 namelen = sizeof(locname) - 1;
292 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
293 dp = rst_readdir(dirp); /* "." */
294 if (dp != NULL && strcmp(dp->d_name, ".") == 0)
295 dp = rst_readdir(dirp); /* ".." */
297 fprintf(stderr, "Warning: `.' missing from directory %s\n",
299 if (dp != NULL && strcmp(dp->d_name, "..") == 0)
300 dp = rst_readdir(dirp); /* first real entry */
302 fprintf(stderr, "Warning: `..' missing from directory %s\n",
304 bpt = rst_telldir(dirp);
306 * a zero inode signals end of directory
309 locname[namelen] = '\0';
310 if (namelen + dp->d_namlen >= (int)sizeof(locname)) {
311 fprintf(stderr, "%s%s: name exceeds %ld char\n",
312 locname, dp->d_name, (long)sizeof(locname) - 1);
314 (void) strncat(locname, dp->d_name, (int)dp->d_namlen);
315 treescan(locname, dp->d_ino, todo);
316 rst_seekdir(dirp, bpt, itp->t_seekpt);
318 dp = rst_readdir(dirp);
319 bpt = rst_telldir(dirp);
324 * Lookup a pathname which is always assumed to start from the ROOTINO.
327 pathsearch(const char *pathname)
331 char *path, *name, buffer[MAXPATHLEN];
333 strcpy(buffer, pathname);
340 while ((name = strsep(&path, "/")) != NULL && *name /* != NULL */) {
342 while ((name = strtok_r(NULL, "/", &path)) != NULL && *name /* != NULL */) {
344 if ((dp = searchdir(ino, name)) == NULL)
352 * Lookup the requested name in directory inum.
353 * Return its inode number if found, zero if it does not exist.
355 static struct direct *
356 searchdir(dump_ino_t inum, char *name)
362 itp = inotablookup(inum);
365 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
368 dp = rst_readdir(dirp);
371 } while (dp->d_namlen != len || strncmp(dp->d_name, name, len) != 0);
376 * Put the directory entries in the directory file
379 putdir(char *buf, size_t size)
381 struct direct cvtbuf;
383 struct odirect *eodp;
387 if (cvtflag && !ufs2flag) {
388 eodp = (struct odirect *)&buf[size];
389 for (odp = (struct odirect *)buf; odp < eodp; odp++)
390 if (odp->d_ino != 0) {
395 for (loc = 0; loc < (long)size; ) {
396 dp = (struct direct *)(buf + loc);
398 printf ("reclen = %d, namlen = %d, type = %d\n",
399 dp->d_reclen, dp->d_namlen, dp->d_type);
402 swabst((u_char *)"is", (u_char *) dp);
403 if (oldinofmt && dp->d_ino != 0) {
404 # if BYTE_ORDER == BIG_ENDIAN
406 dp->d_namlen = dp->d_type;
409 dp->d_namlen = dp->d_type;
411 if (dp->d_namlen == 0 && dp->d_type != 0)
412 dp->d_namlen = dp->d_type;
413 dp->d_type = DT_UNKNOWN;
416 printf ("reclen = %d, namlen = %d, type = %d\n",
417 dp->d_reclen, dp->d_namlen, dp->d_type);
419 i = DIRBLKSIZ - (loc & (DIRBLKSIZ - 1));
420 if ((dp->d_reclen & 0x3) != 0 ||
422 dp->d_reclen < DIRSIZ(0, dp)
424 || dp->d_namlen > MAXNAMLEN
427 Vprintf(stdout, "Mangled directory: ");
428 if ((dp->d_reclen & 0x3) != 0)
430 "reclen not multiple of 4 ");
431 if (dp->d_reclen < DIRSIZ(0, dp))
433 "reclen less than DIRSIZ (%d < %d) ",
434 dp->d_reclen, DIRSIZ(0, dp));
436 if (dp->d_namlen > MAXNAMLEN)
438 "reclen name too big (%d > %d) ",
439 dp->d_namlen, MAXNAMLEN);
441 Vprintf(stdout, "\n");
446 if (dp->d_ino != 0) {
454 * These variables are "local" to the following two functions.
456 static char dirbuf[DIRBLKSIZ];
457 static long dirloc = 0;
458 static long prev = 0;
461 * add a new directory entry to a file.
464 putent(struct direct *dp)
466 dp->d_reclen = DIRSIZ(0, dp);
467 if (dirloc + dp->d_reclen > DIRBLKSIZ) {
468 ((struct direct *)(dirbuf + prev))->d_reclen =
470 if ( fwrite(dirbuf, 1, DIRBLKSIZ, df) != DIRBLKSIZ )
471 err(1,"cannot write to file %s", dirfile);
474 memmove(dirbuf + dirloc, dp, (size_t)dp->d_reclen);
476 dirloc += dp->d_reclen;
480 * flush out a directory that is finished.
485 ((struct direct *)(dirbuf + prev))->d_reclen = DIRBLKSIZ - prev;
486 if ( fwrite(dirbuf, (int)dirloc, 1, df) != 1 )
487 err(1, "cannot write to file %s", dirfile);
490 err(1, "cannot write to file %s", dirfile);
495 dcvt(struct odirect *odp, struct direct *ndp)
498 memset(ndp, 0, (size_t)(sizeof *ndp));
499 ndp->d_ino = odp->d_ino;
500 ndp->d_type = DT_UNKNOWN;
501 (void) strncpy(ndp->d_name, odp->d_name, ODIRSIZ);
502 ndp->d_namlen = strlen(ndp->d_name);
503 ndp->d_reclen = DIRSIZ(0, ndp);
507 * Seek to an entry in a directory.
508 * Only values returned by rst_telldir should be passed to rst_seekdir.
509 * This routine handles many directories in a single file.
510 * It takes the base of the directory in the file, plus
511 * the desired seek offset into it.
514 rst_seekdir(RST_DIR *dirp, OFF_T loc, OFF_T base)
517 if (loc == rst_telldir(dirp))
521 fprintf(stderr, "bad seek pointer to rst_seekdir %lld\n", (long long int)loc);
522 (void) LSEEK(dirp->dd_fd, base + (loc & ~(DIRBLKSIZ - 1)), SEEK_SET);
523 dirp->dd_loc = loc & (DIRBLKSIZ - 1);
524 if (dirp->dd_loc != 0)
525 dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ);
529 * get next entry in a directory.
532 rst_readdir(RST_DIR *dirp)
537 if (dirp->dd_loc == 0) {
538 dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf,
540 if (dirp->dd_size <= 0) {
541 Dprintf(stderr, "error reading directory\n");
545 if (dirp->dd_loc >= dirp->dd_size) {
549 dp = (struct direct *)(dirp->dd_buf + dirp->dd_loc);
550 if (dp->d_reclen == 0 ||
551 dp->d_reclen > DIRBLKSIZ + 1 - dirp->dd_loc) {
552 Dprintf(stderr, "corrupted directory: bad reclen %d\n",
556 dirp->dd_loc += dp->d_reclen;
557 if (dp->d_ino == 0 && strcmp(dp->d_name, "/") == 0)
559 if (dp->d_ino >= maxino) {
560 Dprintf(stderr, "corrupted directory: bad inum %d\n",
569 * Simulate the opening of a directory
572 rst_opendir(const char *name)
578 if ((ino = dirlookup(name)) > 0 &&
579 (itp = inotablookup(ino)) != NULL) {
580 dirp = opendirfile(dirfile);
581 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
588 * In our case, there is nothing to do when closing a directory.
591 rst_closedir(RST_DIR *dirp)
594 (void)close(dirp->dd_fd);
600 * Simulate finding the current offset in the directory.
603 rst_telldir(RST_DIR *dirp)
605 return ((OFF_T)LSEEK(dirp->dd_fd,
606 (OFF_T)0, SEEK_CUR) - dirp->dd_size + dirp->dd_loc);
610 * Open a directory file.
613 opendirfile(const char *name)
618 if ((fd = OPEN(name, O_RDONLY)) == -1)
620 if ((dirp = malloc(sizeof(RST_DIR))) == NULL) {
630 * Set the mode, owner, and times for all new or changed directories
633 setdirmodes(int flags)
636 struct modeinfo node;
640 Vprintf(stdout, "Set directory mode, owner, and times.\n");
641 if (command == 'r' || command == 'R')
642 (void) snprintf(modefile, sizeof(modefile), "%s/rstmode%lu", tmpdir, (long)dumpdate);
643 if (modefile[0] == '#') {
644 panic("modefile not defined\n");
645 fprintf(stderr, "directory mode, owner, and times not set\n");
648 mf = fopen(modefile, "r");
651 fprintf(stderr, "cannot open mode file %s\n", modefile);
652 fprintf(stderr, "directory mode, owner, and times not set\n");
657 (void) fread((char *)&node, 1, sizeof(struct modeinfo), mf);
660 ep = lookupino(node.ino);
661 if (command == 'i' || command == 'x') {
664 if ((flags & FORCE) == 0 && ep->e_flags & EXISTED) {
668 if ((flags & FORCE) == 0 &&
669 node.ino == ROOTINO &&
670 reply("set owner/mode for '.'") == FAIL)
674 panic("cannot find directory inode %d\n", node.ino);
677 (void) chown(cp, node.uid, node.gid);
678 (void) chmod(cp, node.mode);
681 (void) fsetflags(cp, node.flags);
685 (void) chflags(cp, node.flags);
688 utimes(cp, node.timep);
693 panic("error setting directory modes\n");
698 * Generate a literal copy of a directory.
701 genliteraldir(char *name, dump_ino_t ino)
704 int ofile, dp, i, size;
707 itp = inotablookup(ino);
709 panic("Cannot find directory inode %d named %s\n", ino, name);
710 if ((ofile = open(name, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
711 warn("%s: cannot create file\n", name);
714 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
715 dp = dup(dirp->dd_fd);
716 for (i = itp->t_size; i > 0; i -= BUFSIZ) {
717 size = i < BUFSIZ ? i : BUFSIZ;
718 if (read(dp, buf, (int) size) == -1) {
719 warnx("write error extracting inode %lu, name %s\n",
720 (unsigned long)curfile.ino, curfile.name);
723 if (!Nflag && write(ofile, buf, (int) size) == -1) {
724 warnx("write error extracting inode %lu, name %s\n",
725 (unsigned long)curfile.ino, curfile.name);
735 * Determine the type of an inode
738 inodetype(dump_ino_t ino)
742 itp = inotablookup(ino);
749 * Allocate and initialize a directory inode entry.
750 * If requested, save its pertinent mode, owner, and time info.
752 static struct inotab *
753 #if defined(__linux__) || defined(sunos)
754 allocinotab(dump_ino_t ino, struct new_bsd_inode *dip, OFF_T seekpt)
756 allocinotab(dump_ino_t ino, struct dinode *dip, OFF_T seekpt)
760 struct modeinfo node;
762 itp = calloc(1, sizeof(struct inotab));
764 panic("no memory directory table\n");
765 itp->t_next = inotab[INOHASH(ino)];
766 inotab[INOHASH(ino)] = itp;
768 itp->t_seekpt = seekpt;
772 #if defined(__linux__) || defined(sunos)
773 node.timep[0].tv_sec = dip->di_atime.tv_sec;
774 node.timep[0].tv_usec = dip->di_atime.tv_usec;
775 node.timep[1].tv_sec = dip->di_mtime.tv_sec;
776 node.timep[1].tv_usec = dip->di_mtime.tv_usec;
777 #else /* __linux__ || sunos */
778 node.timep[0].tv_sec = dip->di_atime;
779 node.timep[0].tv_usec = dip->di_atimensec / 1000;
780 node.timep[1].tv_sec = dip->di_mtime;
781 node.timep[1].tv_usec = dip->di_mtimensec / 1000;
782 #endif /* __linux__ || sunos */
783 node.mode = dip->di_mode;
784 node.flags = dip->di_flags;
785 node.uid = dip->di_uid;
786 node.gid = dip->di_gid;
787 if ( fwrite((char *)&node, 1, sizeof(struct modeinfo), mf) != sizeof(struct modeinfo) )
788 err(1,"cannot write to file %s", modefile);
793 * Look up an inode in the table of directories
795 static struct inotab *
796 inotablookup(dump_ino_t ino)
800 for (itp = inotab[INOHASH(ino)]; itp != NULL; itp = itp->t_next)
801 if (itp->t_ino == ino)
813 if (modefile[0] != '#')
814 (void) unlink(modefile);
815 if (dirfile[0] != '#')
816 (void) unlink(dirfile);