]>
git.wh0rd.org - dump.git/blob - restore/dirs.c
2d559fea248efd3078ad63a21c6eb5cf8e9098d1
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@cybercable.fr>, 1999-2000
9 * Copyright (c) 1983, 1993
10 * The Regents of the University of California. All rights reserved.
11 * (c) UNIX System Laboratories, Inc.
12 * All or some portions of this file are derived from material licensed
13 * to the University of California by American Telephone and Telegraph
14 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
15 * the permission of UNIX System Laboratories, Inc.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 * 3. All advertising materials mentioning features or use of this software
26 * must display the following acknowledgement:
27 * This product includes software developed by the University of
28 * California, Berkeley and its contributors.
29 * 4. Neither the name of the University nor the names of its contributors
30 * may be used to endorse or promote products derived from this software
31 * without specific prior written permission.
33 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
34 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
37 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 static const char rcsid
[] =
48 "$Id: dirs.c,v 1.9 2000/03/01 10:16:05 stelian Exp $";
51 #include <sys/param.h>
56 #include <linux/ext2_fs.h>
57 #include <bsdcompat.h>
59 #include <ufs/ufs/dinode.h>
60 #include <ufs/ufs/dir.h>
61 #endif /* __linux__ */
62 #include <protocols/dumprestore.h>
64 #include <compaterr.h>
74 #include <machine/endian.h>
77 #include "pathnames.h"
82 * Symbol table of directories read from tape.
85 #define INOHASH(val) (val % HASHSIZE)
87 struct inotab
*t_next
;
92 static struct inotab
*inotab
[HASHSIZE
];
95 * Information retained about directories.
99 struct timeval timep
[2];
107 * Definitions for library routines operating on directories.
110 #define DIRBLKSIZ 1024
115 char dd_buf
[DIRBLKSIZ
];
119 * Global variables for this file.
122 static FILE *df
, *mf
;
123 static RST_DIR
*dirp
;
124 static char dirfile
[MAXPATHLEN
] = "#"; /* No file */
125 static char modefile
[MAXPATHLEN
] = "#"; /* No file */
126 static char dot
[2] = "."; /* So it can be modified */
129 * Format of old style directories.
134 char d_name
[ODIRSIZ
];
138 static struct inotab
*allocinotab
__P((ino_t
, struct new_bsd_inode
*, long));
140 static struct inotab
*allocinotab
__P((ino_t
, struct dinode
*, long));
142 static void dcvt
__P((struct odirect
*, struct direct
*));
143 static void flushent
__P((void));
144 static struct inotab
*inotablookup
__P((ino_t
));
145 static RST_DIR
*opendirfile
__P((const char *));
146 static void putdir
__P((char *, size_t));
147 static void putent
__P((struct direct
*));
148 static void rst_seekdir
__P((RST_DIR
*, long, long));
149 static long rst_telldir
__P((RST_DIR
*));
150 static struct direct
*searchdir
__P((ino_t
, char *));
153 * Extract directory contents, building up a directory structure
154 * on disk for extraction by name.
155 * If genmode is requested, save mode, owner, and times for all
156 * directories on the tape.
159 extractdirs(int genmode
)
163 register struct new_bsd_inode
*ip
;
165 register struct dinode
*ip
;
168 struct direct nulldir
;
171 Vprintf(stdout
, "Extract directories from tape\n");
172 (void) snprintf(dirfile
, sizeof(dirfile
), "%s/rstdir%ld", tmpdir
,
174 if (command
!= 'r' && command
!= 'R') {
175 (void) strncat(dirfile
, "-XXXXXX",
176 sizeof(dirfile
) - strlen(dirfile
));
177 fd
= mkstemp(dirfile
);
179 fd
= open(dirfile
, O_RDWR
|O_CREAT
|O_EXCL
, 0666);
180 if (fd
== -1 || (df
= fdopen(fd
, "w")) == NULL
) {
183 err(1, "cannot create directory temporary %s", dirfile
);
186 (void) snprintf(modefile
, sizeof(modefile
), "%s/rstmode%ld", tmpdir
, (long)dumpdate
);
187 if (command
!= 'r' && command
!= 'R') {
188 (void) strncat(modefile
, "-XXXXXX",
189 sizeof(modefile
) - strlen(modefile
));
190 fd
= mkstemp(modefile
);
192 fd
= open(modefile
, O_RDWR
|O_CREAT
|O_EXCL
, 0666);
193 if (fd
== -1 || (mf
= fdopen(fd
, "w")) == NULL
) {
196 err(1, "cannot create modefile %s", modefile
);
200 nulldir
.d_type
= DT_DIR
;
201 nulldir
.d_namlen
= 1;
202 nulldir
.d_name
[0] = '/';
203 nulldir
.d_name
[1] = '\0';
204 nulldir
.d_reclen
= DIRSIZ(0, &nulldir
);
206 curfile
.name
= "<directory file - name unknown>";
207 curfile
.action
= USING
;
209 if (ip
== NULL
|| (ip
->di_mode
& IFMT
) != IFDIR
) {
211 dirp
= opendirfile(dirfile
);
218 panic("Root directory is not on tape\n");
221 itp
= allocinotab(curfile
.ino
, ip
, seekpt
);
222 getfile(putdir
, xtrnull
);
225 itp
->t_size
= seekpt
- itp
->t_seekpt
;
230 * skip over all the directories on the tape
236 while (curfile
.dip
&& (curfile
.dip
->di_mode
& IFMT
) == IFDIR
) {
242 * Recursively find names and inumbers of all files in subtree
243 * pname and pass them off to be processed.
246 treescan(char *pname
, ino_t ino
, long (*todo
) __P((char *, ino_t
, int)))
248 register struct inotab
*itp
;
249 register struct direct
*dp
;
252 char locname
[MAXPATHLEN
+ 1];
254 itp
= inotablookup(ino
);
257 * Pname is name of a simple file or an unchanged directory.
259 (void) (*todo
)(pname
, ino
, LEAF
);
263 * Pname is a dumped directory name.
265 if ((*todo
)(pname
, ino
, NODE
) == FAIL
)
268 * begin search through the directory
269 * skipping over "." and ".."
271 namelen
= snprintf(locname
, sizeof(locname
), "%s/", pname
);
272 if (namelen
>= sizeof(locname
))
273 namelen
= sizeof(locname
) - 1;
274 rst_seekdir(dirp
, itp
->t_seekpt
, itp
->t_seekpt
);
275 dp
= rst_readdir(dirp
); /* "." */
276 if (dp
!= NULL
&& strcmp(dp
->d_name
, ".") == 0)
277 dp
= rst_readdir(dirp
); /* ".." */
279 fprintf(stderr
, "Warning: `.' missing from directory %s\n",
281 if (dp
!= NULL
&& strcmp(dp
->d_name
, "..") == 0)
282 dp
= rst_readdir(dirp
); /* first real entry */
284 fprintf(stderr
, "Warning: `..' missing from directory %s\n",
286 bpt
= rst_telldir(dirp
);
288 * a zero inode signals end of directory
291 locname
[namelen
] = '\0';
292 if (namelen
+ dp
->d_namlen
>= sizeof(locname
)) {
293 fprintf(stderr
, "%s%s: name exceeds %ld char\n",
294 locname
, dp
->d_name
, (long)sizeof(locname
) - 1);
296 (void) strncat(locname
, dp
->d_name
, (int)dp
->d_namlen
);
297 treescan(locname
, dp
->d_ino
, todo
);
298 rst_seekdir(dirp
, bpt
, itp
->t_seekpt
);
300 dp
= rst_readdir(dirp
);
301 bpt
= rst_telldir(dirp
);
306 * Lookup a pathname which is always assumed to start from the ROOTINO.
309 pathsearch(const char *pathname
)
313 char *path
, *name
, buffer
[MAXPATHLEN
];
315 strcpy(buffer
, pathname
);
321 while ((name
= strsep(&path
, "/")) != NULL
&& *name
/* != NULL */) {
322 if ((dp
= searchdir(ino
, name
)) == NULL
)
330 * Lookup the requested name in directory inum.
331 * Return its inode number if found, zero if it does not exist.
333 static struct direct
*
334 searchdir(ino_t inum
, char *name
)
336 register struct direct
*dp
;
337 register struct inotab
*itp
;
340 itp
= inotablookup(inum
);
343 rst_seekdir(dirp
, itp
->t_seekpt
, itp
->t_seekpt
);
346 dp
= rst_readdir(dirp
);
349 } while (dp
->d_namlen
!= len
|| strncmp(dp
->d_name
, name
, len
) != 0);
354 * Put the directory entries in the directory file
357 putdir(char *buf
, size_t size
)
359 struct direct cvtbuf
;
360 register struct odirect
*odp
;
361 struct odirect
*eodp
;
362 register struct direct
*dp
;
366 eodp
= (struct odirect
*)&buf
[size
];
367 for (odp
= (struct odirect
*)buf
; odp
< eodp
; odp
++)
368 if (odp
->d_ino
!= 0) {
373 for (loc
= 0; loc
< size
; ) {
374 dp
= (struct direct
*)(buf
+ loc
);
376 printf ("reclen = %d, namlen = %d, type = %d\n",
377 dp
->d_reclen
, dp
->d_namlen
, dp
->d_type
);
380 swabst((u_char
*)"is", (u_char
*) dp
);
381 if (oldinofmt
&& dp
->d_ino
!= 0) {
382 # if BYTE_ORDER == BIG_ENDIAN
384 dp
->d_namlen
= dp
->d_type
;
387 dp
->d_namlen
= dp
->d_type
;
389 dp
->d_type
= DT_UNKNOWN
;
392 printf ("reclen = %d, namlen = %d, type = %d\n",
393 dp
->d_reclen
, dp
->d_namlen
, dp
->d_type
);
395 i
= DIRBLKSIZ
- (loc
& (DIRBLKSIZ
- 1));
396 if ((dp
->d_reclen
& 0x3) != 0 ||
398 dp
->d_reclen
< DIRSIZ(0, dp
) ||
399 dp
->d_namlen
> MAXNAMLEN
) {
400 Vprintf(stdout
, "Mangled directory: ");
401 if ((dp
->d_reclen
& 0x3) != 0)
403 "reclen not multiple of 4 ");
404 if (dp
->d_reclen
< DIRSIZ(0, dp
))
406 "reclen less than DIRSIZ (%d < %d) ",
407 dp
->d_reclen
, DIRSIZ(0, dp
));
408 if (dp
->d_namlen
> MAXNAMLEN
)
410 "reclen name too big (%d > %d) ",
411 dp
->d_namlen
, MAXNAMLEN
);
412 Vprintf(stdout
, "\n");
417 if (dp
->d_ino
!= 0) {
425 * These variables are "local" to the following two functions.
427 char dirbuf
[DIRBLKSIZ
];
432 * add a new directory entry to a file.
435 putent(struct direct
*dp
)
437 dp
->d_reclen
= DIRSIZ(0, dp
);
438 if (dirloc
+ dp
->d_reclen
> DIRBLKSIZ
) {
439 ((struct direct
*)(dirbuf
+ prev
))->d_reclen
=
441 (void) fwrite(dirbuf
, 1, DIRBLKSIZ
, df
);
444 memmove(dirbuf
+ dirloc
, dp
, (size_t)dp
->d_reclen
);
446 dirloc
+= dp
->d_reclen
;
450 * flush out a directory that is finished.
455 ((struct direct
*)(dirbuf
+ prev
))->d_reclen
= DIRBLKSIZ
- prev
;
456 (void) fwrite(dirbuf
, (int)dirloc
, 1, df
);
462 dcvt(struct odirect
*odp
, struct direct
*ndp
)
465 memset(ndp
, 0, (size_t)(sizeof *ndp
));
466 ndp
->d_ino
= odp
->d_ino
;
467 ndp
->d_type
= DT_UNKNOWN
;
468 (void) strncpy(ndp
->d_name
, odp
->d_name
, ODIRSIZ
);
469 ndp
->d_namlen
= strlen(ndp
->d_name
);
470 ndp
->d_reclen
= DIRSIZ(0, ndp
);
474 * Seek to an entry in a directory.
475 * Only values returned by rst_telldir should be passed to rst_seekdir.
476 * This routine handles many directories in a single file.
477 * It takes the base of the directory in the file, plus
478 * the desired seek offset into it.
481 rst_seekdir(RST_DIR
*dirp
, long loc
, long base
)
484 if (loc
== rst_telldir(dirp
))
488 fprintf(stderr
, "bad seek pointer to rst_seekdir %ld\n", loc
);
489 (void) lseek(dirp
->dd_fd
, base
+ (loc
& ~(DIRBLKSIZ
- 1)), SEEK_SET
);
490 dirp
->dd_loc
= loc
& (DIRBLKSIZ
- 1);
491 if (dirp
->dd_loc
!= 0)
492 dirp
->dd_size
= read(dirp
->dd_fd
, dirp
->dd_buf
, DIRBLKSIZ
);
496 * get next entry in a directory.
499 rst_readdir(RST_DIR
*dirp
)
501 register struct direct
*dp
;
504 if (dirp
->dd_loc
== 0) {
505 dirp
->dd_size
= read(dirp
->dd_fd
, dirp
->dd_buf
,
507 if (dirp
->dd_size
<= 0) {
508 Dprintf(stderr
, "error reading directory\n");
512 if (dirp
->dd_loc
>= dirp
->dd_size
) {
516 dp
= (struct direct
*)(dirp
->dd_buf
+ dirp
->dd_loc
);
517 if (dp
->d_reclen
== 0 ||
518 dp
->d_reclen
> DIRBLKSIZ
+ 1 - dirp
->dd_loc
) {
519 Dprintf(stderr
, "corrupted directory: bad reclen %d\n",
523 dirp
->dd_loc
+= dp
->d_reclen
;
524 if (dp
->d_ino
== 0 && strcmp(dp
->d_name
, "/") == 0)
526 if (dp
->d_ino
>= maxino
) {
527 Dprintf(stderr
, "corrupted directory: bad inum %d\n",
536 * Simulate the opening of a directory
539 rst_opendir(const char *name
)
545 if ((ino
= dirlookup(name
)) > 0 &&
546 (itp
= inotablookup(ino
)) != NULL
) {
547 dirp
= opendirfile(dirfile
);
548 rst_seekdir(dirp
, itp
->t_seekpt
, itp
->t_seekpt
);
555 * In our case, there is nothing to do when closing a directory.
558 rst_closedir(RST_DIR
*dirp
)
561 (void)close(dirp
->dd_fd
);
567 * Simulate finding the current offset in the directory.
570 rst_telldir(RST_DIR
*dirp
)
572 return ((long)lseek(dirp
->dd_fd
,
573 (off_t
)0, SEEK_CUR
) - dirp
->dd_size
+ dirp
->dd_loc
);
577 * Open a directory file.
580 opendirfile(const char *name
)
582 register RST_DIR
*dirp
;
585 if ((fd
= open(name
, O_RDONLY
)) == -1)
587 if ((dirp
= malloc(sizeof(RST_DIR
))) == NULL
) {
597 * Set the mode, owner, and times for all new or changed directories
600 setdirmodes(int flags
)
603 struct modeinfo node
;
607 Vprintf(stdout
, "Set directory mode, owner, and times.\n");
608 if (command
== 'r' || command
== 'R')
609 (void) snprintf(modefile
, sizeof(modefile
), "%s/rstmode%lu", tmpdir
, (long)dumpdate
);
610 if (modefile
[0] == '#') {
611 panic("modefile not defined\n");
612 fprintf(stderr
, "directory mode, owner, and times not set\n");
615 mf
= fopen(modefile
, "r");
618 fprintf(stderr
, "cannot open mode file %s\n", modefile
);
619 fprintf(stderr
, "directory mode, owner, and times not set\n");
624 (void) fread((char *)&node
, 1, sizeof(struct modeinfo
), mf
);
627 ep
= lookupino(node
.ino
);
628 if (command
== 'i' || command
== 'x') {
631 if ((flags
& FORCE
) == 0 && ep
->e_flags
& EXISTED
) {
635 if (node
.ino
== ROOTINO
&&
636 reply("set owner/mode for '.'") == FAIL
)
640 panic("cannot find directory inode %d\n", node
.ino
);
643 (void) chown(cp
, node
.uid
, node
.gid
);
644 (void) chmod(cp
, node
.mode
);
647 (void) fsetflags(cp
, node
.flags
);
649 (void) chflags(cp
, node
.flags
);
651 utimes(cp
, node
.timep
);
656 panic("error setting directory modes\n");
661 * Generate a literal copy of a directory.
664 genliteraldir(char *name
, ino_t ino
)
666 register struct inotab
*itp
;
667 int ofile
, dp
, i
, size
;
670 itp
= inotablookup(ino
);
672 panic("Cannot find directory inode %d named %s\n", ino
, name
);
673 if ((ofile
= open(name
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666)) < 0) {
674 warn("%s: cannot create file\n", name
);
677 rst_seekdir(dirp
, itp
->t_seekpt
, itp
->t_seekpt
);
678 dp
= dup(dirp
->dd_fd
);
679 for (i
= itp
->t_size
; i
> 0; i
-= BUFSIZ
) {
680 size
= i
< BUFSIZ
? i
: BUFSIZ
;
681 if (read(dp
, buf
, (int) size
) == -1) {
682 warnx("write error extracting inode %lu, name %s\n",
683 (unsigned long)curfile
.ino
, curfile
.name
);
686 if (!Nflag
&& write(ofile
, buf
, (int) size
) == -1) {
687 warnx("write error extracting inode %lu, name %s\n",
688 (unsigned long)curfile
.ino
, curfile
.name
);
698 * Determine the type of an inode
705 itp
= inotablookup(ino
);
712 * Allocate and initialize a directory inode entry.
713 * If requested, save its pertinent mode, owner, and time info.
715 static struct inotab
*
717 allocinotab(ino_t ino
, struct new_bsd_inode
*dip
, long seekpt
)
719 allocinotab(ino_t ino
, struct dinode
*dip
, long seekpt
)
722 register struct inotab
*itp
;
723 struct modeinfo node
;
725 itp
= calloc(1, sizeof(struct inotab
));
727 panic("no memory directory table\n");
728 itp
->t_next
= inotab
[INOHASH(ino
)];
729 inotab
[INOHASH(ino
)] = itp
;
731 itp
->t_seekpt
= seekpt
;
736 node
.timep
[0].tv_sec
= dip
->di_atime
.tv_sec
;
737 node
.timep
[0].tv_usec
= dip
->di_atime
.tv_usec
;
738 node
.timep
[1].tv_sec
= dip
->di_mtime
.tv_sec
;
739 node
.timep
[1].tv_usec
= dip
->di_mtime
.tv_usec
;
740 #else /* __linux__ */
741 node
.timep
[0].tv_sec
= dip
->di_atime
;
742 node
.timep
[0].tv_usec
= dip
->di_atimensec
/ 1000;
743 node
.timep
[1].tv_sec
= dip
->di_mtime
;
744 node
.timep
[1].tv_usec
= dip
->di_mtimensec
/ 1000;
745 #endif /* __linux__ */
746 node
.mode
= dip
->di_mode
;
747 node
.flags
= dip
->di_flags
;
748 node
.uid
= dip
->di_uid
;
749 node
.gid
= dip
->di_gid
;
750 (void) fwrite((char *)&node
, 1, sizeof(struct modeinfo
), mf
);
755 * Look up an inode in the table of directories
757 static struct inotab
*
758 inotablookup(ino_t ino
)
760 register struct inotab
*itp
;
762 for (itp
= inotab
[INOHASH(ino
)]; itp
!= NULL
; itp
= itp
->t_next
)
763 if (itp
->t_ino
== ino
)
775 if (modefile
[0] != '#')
776 (void) unlink(modefile
);
777 if (dirfile
[0] != '#')
778 (void) unlink(dirfile
);