]>
git.wh0rd.org - dump.git/blob - restore/dirs.c
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
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
46 * $Id: dirs.c,v 1.5 1999/10/11 13:31:12 stelian Exp $
49 #include <sys/param.h>
54 #include <linux/ext2_fs.h>
55 #include <bsdcompat.h>
57 #include <ufs/ufs/dinode.h>
58 #include <ufs/ufs/dir.h>
59 #endif /* __linux__ */
60 #include <protocols/dumprestore.h>
62 #include <compaterr.h>
72 #include <machine/endian.h>
75 #include "pathnames.h"
80 * Symbol table of directories read from tape.
83 #define INOHASH(val) (val % HASHSIZE)
85 struct inotab
*t_next
;
90 static struct inotab
*inotab
[HASHSIZE
];
93 * Information retained about directories.
97 struct timeval timep
[2];
105 * Definitions for library routines operating on directories.
108 #define DIRBLKSIZ 1024
113 char dd_buf
[DIRBLKSIZ
];
117 * Global variables for this file.
120 static FILE *df
, *mf
;
121 static RST_DIR
*dirp
;
122 static char dirfile
[MAXPATHLEN
] = "#"; /* No file */
123 static char modefile
[MAXPATHLEN
] = "#"; /* No file */
124 static char dot
[2] = "."; /* So it can be modified */
127 * Format of old style directories.
132 char d_name
[ODIRSIZ
];
136 static struct inotab
*allocinotab
__P((ino_t
, struct new_bsd_inode
*, long));
138 static struct inotab
*allocinotab
__P((ino_t
, struct dinode
*, long));
140 static void dcvt
__P((struct odirect
*, struct direct
*));
141 static void flushent
__P((void));
142 static struct inotab
*inotablookup
__P((ino_t
));
143 static RST_DIR
*opendirfile
__P((const char *));
144 static void putdir
__P((char *, size_t));
145 static void putent
__P((struct direct
*));
146 static void rst_seekdir
__P((RST_DIR
*, long, long));
147 static long rst_telldir
__P((RST_DIR
*));
148 static struct direct
*searchdir
__P((ino_t
, char *));
151 * Extract directory contents, building up a directory structure
152 * on disk for extraction by name.
153 * If genmode is requested, save mode, owner, and times for all
154 * directories on the tape.
157 extractdirs(int genmode
)
161 register struct new_bsd_inode
*ip
;
163 register struct dinode
*ip
;
166 struct direct nulldir
;
169 Vprintf(stdout
, "Extract directories from tape\n");
170 (void) snprintf(dirfile
, sizeof(dirfile
), "%s/rstdir%ld", tmpdir
,
172 if (command
!= 'r' && command
!= 'R') {
173 (void) strncat(dirfile
, "-XXXXXX",
174 sizeof(dirfile
) - strlen(dirfile
));
175 fd
= mkstemp(dirfile
);
177 fd
= open(dirfile
, O_RDWR
|O_CREAT
|O_EXCL
, 0666);
178 if (fd
== -1 || (df
= fdopen(fd
, "w")) == NULL
) {
181 err(1, "cannot create directory temporary %s", dirfile
);
184 (void) snprintf(modefile
, sizeof(modefile
), "%s/rstmode%ld", tmpdir
, (long)dumpdate
);
185 if (command
!= 'r' && command
!= 'R') {
186 (void) strncat(modefile
, "-XXXXXX",
187 sizeof(modefile
) - strlen(modefile
));
188 fd
= mkstemp(modefile
);
190 fd
= open(modefile
, O_RDWR
|O_CREAT
|O_EXCL
, 0666);
191 if (fd
== -1 || (mf
= fdopen(fd
, "w")) == NULL
) {
194 err(1, "cannot create modefile %s", modefile
);
198 nulldir
.d_type
= DT_DIR
;
199 nulldir
.d_namlen
= 1;
200 nulldir
.d_name
[0] = '/';
201 nulldir
.d_name
[1] = '\0';
202 nulldir
.d_reclen
= DIRSIZ(0, &nulldir
);
204 curfile
.name
= "<directory file - name unknown>";
205 curfile
.action
= USING
;
207 if (ip
== NULL
|| (ip
->di_mode
& IFMT
) != IFDIR
) {
209 dirp
= opendirfile(dirfile
);
216 panic("Root directory is not on tape\n");
219 itp
= allocinotab(curfile
.ino
, ip
, seekpt
);
220 getfile(putdir
, xtrnull
);
223 itp
->t_size
= seekpt
- itp
->t_seekpt
;
228 * skip over all the directories on the tape
234 while (curfile
.dip
&& (curfile
.dip
->di_mode
& IFMT
) == IFDIR
) {
240 * Recursively find names and inumbers of all files in subtree
241 * pname and pass them off to be processed.
244 treescan(char *pname
, ino_t ino
, long (*todo
) __P((char *, ino_t
, int)))
246 register struct inotab
*itp
;
247 register struct direct
*dp
;
250 char locname
[MAXPATHLEN
+ 1];
252 itp
= inotablookup(ino
);
255 * Pname is name of a simple file or an unchanged directory.
257 (void) (*todo
)(pname
, ino
, LEAF
);
261 * Pname is a dumped directory name.
263 if ((*todo
)(pname
, ino
, NODE
) == FAIL
)
266 * begin search through the directory
267 * skipping over "." and ".."
269 namelen
= snprintf(locname
, sizeof(locname
), "%s/", pname
);
270 if (namelen
>= sizeof(locname
))
271 namelen
= sizeof(locname
) - 1;
272 rst_seekdir(dirp
, itp
->t_seekpt
, itp
->t_seekpt
);
273 dp
= rst_readdir(dirp
); /* "." */
274 if (dp
!= NULL
&& strcmp(dp
->d_name
, ".") == 0)
275 dp
= rst_readdir(dirp
); /* ".." */
277 fprintf(stderr
, "Warning: `.' missing from directory %s\n",
279 if (dp
!= NULL
&& strcmp(dp
->d_name
, "..") == 0)
280 dp
= rst_readdir(dirp
); /* first real entry */
282 fprintf(stderr
, "Warning: `..' missing from directory %s\n",
284 bpt
= rst_telldir(dirp
);
286 * a zero inode signals end of directory
289 locname
[namelen
] = '\0';
290 if (namelen
+ dp
->d_namlen
>= sizeof(locname
)) {
291 fprintf(stderr
, "%s%s: name exceeds %ld char\n",
292 locname
, dp
->d_name
, (long)sizeof(locname
) - 1);
294 (void) strncat(locname
, dp
->d_name
, (int)dp
->d_namlen
);
295 treescan(locname
, dp
->d_ino
, todo
);
296 rst_seekdir(dirp
, bpt
, itp
->t_seekpt
);
298 dp
= rst_readdir(dirp
);
299 bpt
= rst_telldir(dirp
);
304 * Lookup a pathname which is always assumed to start from the ROOTINO.
307 pathsearch(const char *pathname
)
311 char *path
, *name
, buffer
[MAXPATHLEN
];
313 strcpy(buffer
, pathname
);
319 while ((name
= strsep(&path
, "/")) != NULL
&& *name
/* != NULL */) {
320 if ((dp
= searchdir(ino
, name
)) == NULL
)
328 * Lookup the requested name in directory inum.
329 * Return its inode number if found, zero if it does not exist.
331 static struct direct
*
332 searchdir(ino_t inum
, char *name
)
334 register struct direct
*dp
;
335 register struct inotab
*itp
;
338 itp
= inotablookup(inum
);
341 rst_seekdir(dirp
, itp
->t_seekpt
, itp
->t_seekpt
);
344 dp
= rst_readdir(dirp
);
347 } while (dp
->d_namlen
!= len
|| strncmp(dp
->d_name
, name
, len
) != 0);
352 * Put the directory entries in the directory file
355 putdir(char *buf
, size_t size
)
357 struct direct cvtbuf
;
358 register struct odirect
*odp
;
359 struct odirect
*eodp
;
360 register struct direct
*dp
;
364 eodp
= (struct odirect
*)&buf
[size
];
365 for (odp
= (struct odirect
*)buf
; odp
< eodp
; odp
++)
366 if (odp
->d_ino
!= 0) {
371 for (loc
= 0; loc
< size
; ) {
372 dp
= (struct direct
*)(buf
+ loc
);
374 printf ("reclen = %d, namlen = %d, type = %d\n",
375 dp
->d_reclen
, dp
->d_namlen
, dp
->d_type
);
378 swabst((u_char
*)"is", (u_char
*) dp
);
379 if (oldinofmt
&& dp
->d_ino
!= 0) {
382 swabst((u_char
*)"s", (u_char
*)&dp
->d_namlen
);
384 # if BYTE_ORDER == BIG_ENDIAN
386 dp
->d_namlen
= dp
->d_type
;
389 dp
->d_namlen
= dp
->d_type
;
391 #endif /* __linux__ */
392 dp
->d_type
= DT_UNKNOWN
;
396 * Horrible hack to read FreeBSD 2.0 dumps
399 swabst((u_char
*)"6bs", (u_char
*) dp
);
400 #endif /* __linux__ */
402 printf ("reclen = %d, namlen = %d, type = %d\n",
403 dp
->d_reclen
, dp
->d_namlen
, dp
->d_type
);
405 i
= DIRBLKSIZ
- (loc
& (DIRBLKSIZ
- 1));
406 if ((dp
->d_reclen
& 0x3) != 0 ||
408 dp
->d_reclen
< DIRSIZ(0, dp
) ||
409 dp
->d_namlen
> NAME_MAX
) {
410 Vprintf(stdout
, "Mangled directory: ");
411 if ((dp
->d_reclen
& 0x3) != 0)
413 "reclen not multiple of 4 ");
414 if (dp
->d_reclen
< DIRSIZ(0, dp
))
416 "reclen less than DIRSIZ (%d < %d) ",
417 dp
->d_reclen
, DIRSIZ(0, dp
));
418 if (dp
->d_namlen
> NAME_MAX
)
420 "reclen name too big (%d > %d) ",
421 dp
->d_namlen
, NAME_MAX
);
422 Vprintf(stdout
, "\n");
427 if (dp
->d_ino
!= 0) {
435 * These variables are "local" to the following two functions.
437 char dirbuf
[DIRBLKSIZ
];
442 * add a new directory entry to a file.
445 putent(struct direct
*dp
)
447 dp
->d_reclen
= DIRSIZ(0, dp
);
448 if (dirloc
+ dp
->d_reclen
> DIRBLKSIZ
) {
449 ((struct direct
*)(dirbuf
+ prev
))->d_reclen
=
451 (void) fwrite(dirbuf
, 1, DIRBLKSIZ
, df
);
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 (void) fwrite(dirbuf
, (int)dirloc
, 1, df
);
472 dcvt(struct odirect
*odp
, struct direct
*ndp
)
475 memset(ndp
, 0, (size_t)(sizeof *ndp
));
476 ndp
->d_ino
= odp
->d_ino
;
477 ndp
->d_type
= DT_UNKNOWN
;
478 (void) strncpy(ndp
->d_name
, odp
->d_name
, ODIRSIZ
);
479 ndp
->d_namlen
= strlen(ndp
->d_name
);
480 ndp
->d_reclen
= DIRSIZ(0, ndp
);
484 * Seek to an entry in a directory.
485 * Only values returned by rst_telldir should be passed to rst_seekdir.
486 * This routine handles many directories in a single file.
487 * It takes the base of the directory in the file, plus
488 * the desired seek offset into it.
491 rst_seekdir(RST_DIR
*dirp
, long loc
, long base
)
494 if (loc
== rst_telldir(dirp
))
498 fprintf(stderr
, "bad seek pointer to rst_seekdir %ld\n", loc
);
499 (void) lseek(dirp
->dd_fd
, base
+ (loc
& ~(DIRBLKSIZ
- 1)), SEEK_SET
);
500 dirp
->dd_loc
= loc
& (DIRBLKSIZ
- 1);
501 if (dirp
->dd_loc
!= 0)
502 dirp
->dd_size
= read(dirp
->dd_fd
, dirp
->dd_buf
, DIRBLKSIZ
);
506 * get next entry in a directory.
509 rst_readdir(RST_DIR
*dirp
)
511 register struct direct
*dp
;
514 if (dirp
->dd_loc
== 0) {
515 dirp
->dd_size
= read(dirp
->dd_fd
, dirp
->dd_buf
,
517 if (dirp
->dd_size
<= 0) {
518 Dprintf(stderr
, "error reading directory\n");
522 if (dirp
->dd_loc
>= dirp
->dd_size
) {
526 dp
= (struct direct
*)(dirp
->dd_buf
+ dirp
->dd_loc
);
527 if (dp
->d_reclen
== 0 ||
528 dp
->d_reclen
> DIRBLKSIZ
+ 1 - dirp
->dd_loc
) {
529 Dprintf(stderr
, "corrupted directory: bad reclen %d\n",
533 dirp
->dd_loc
+= dp
->d_reclen
;
534 if (dp
->d_ino
== 0 && strcmp(dp
->d_name
, "/") == 0)
536 if (dp
->d_ino
>= maxino
) {
537 Dprintf(stderr
, "corrupted directory: bad inum %d\n",
546 * Simulate the opening of a directory
549 rst_opendir(const char *name
)
555 if ((ino
= dirlookup(name
)) > 0 &&
556 (itp
= inotablookup(ino
)) != NULL
) {
557 dirp
= opendirfile(dirfile
);
558 rst_seekdir(dirp
, itp
->t_seekpt
, itp
->t_seekpt
);
565 * In our case, there is nothing to do when closing a directory.
568 rst_closedir(RST_DIR
*dirp
)
571 (void)close(dirp
->dd_fd
);
577 * Simulate finding the current offset in the directory.
580 rst_telldir(RST_DIR
*dirp
)
582 return ((long)lseek(dirp
->dd_fd
,
583 (off_t
)0, SEEK_CUR
) - dirp
->dd_size
+ dirp
->dd_loc
);
587 * Open a directory file.
590 opendirfile(const char *name
)
592 register RST_DIR
*dirp
;
595 if ((fd
= open(name
, O_RDONLY
)) == -1)
597 if ((dirp
= malloc(sizeof(RST_DIR
))) == NULL
) {
607 * Set the mode, owner, and times for all new or changed directories
610 setdirmodes(int flags
)
613 struct modeinfo node
;
617 Vprintf(stdout
, "Set directory mode, owner, and times.\n");
618 if (command
== 'r' || command
== 'R')
619 (void) snprintf(modefile
, sizeof(modefile
), "%s/rstmode%lu", tmpdir
, (long)dumpdate
);
620 if (modefile
[0] == '#') {
621 panic("modefile not defined\n");
622 fprintf(stderr
, "directory mode, owner, and times not set\n");
625 mf
= fopen(modefile
, "r");
628 fprintf(stderr
, "cannot open mode file %s\n", modefile
);
629 fprintf(stderr
, "directory mode, owner, and times not set\n");
634 (void) fread((char *)&node
, 1, sizeof(struct modeinfo
), mf
);
637 ep
= lookupino(node
.ino
);
638 if (command
== 'i' || command
== 'x') {
641 if ((flags
& FORCE
) == 0 && ep
->e_flags
& EXISTED
) {
645 if (node
.ino
== ROOTINO
&&
646 reply("set owner/mode for '.'") == FAIL
)
650 panic("cannot find directory inode %d\n", node
.ino
);
653 (void) chown(cp
, node
.uid
, node
.gid
);
654 (void) chmod(cp
, node
.mode
);
657 (void) fsetflags(cp
, node
.flags
);
659 (void) chflags(cp
, node
.flags
);
661 utimes(cp
, node
.timep
);
666 panic("error setting directory modes\n");
671 * Generate a literal copy of a directory.
674 genliteraldir(char *name
, ino_t ino
)
676 register struct inotab
*itp
;
677 int ofile
, dp
, i
, size
;
680 itp
= inotablookup(ino
);
682 panic("Cannot find directory inode %d named %s\n", ino
, name
);
683 if ((ofile
= open(name
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666)) < 0) {
684 warn("%s: cannot create file\n", name
);
687 rst_seekdir(dirp
, itp
->t_seekpt
, itp
->t_seekpt
);
688 dp
= dup(dirp
->dd_fd
);
689 for (i
= itp
->t_size
; i
> 0; i
-= BUFSIZ
) {
690 size
= i
< BUFSIZ
? i
: BUFSIZ
;
691 if (read(dp
, buf
, (int) size
) == -1) {
692 warnx("write error extracting inode %lu, name %s\n",
693 (unsigned long)curfile
.ino
, curfile
.name
);
696 if (!Nflag
&& write(ofile
, buf
, (int) size
) == -1) {
697 warnx("write error extracting inode %lu, name %s\n",
698 (unsigned long)curfile
.ino
, curfile
.name
);
708 * Determine the type of an inode
715 itp
= inotablookup(ino
);
722 * Allocate and initialize a directory inode entry.
723 * If requested, save its pertinent mode, owner, and time info.
725 static struct inotab
*
727 allocinotab(ino_t ino
, struct new_bsd_inode
*dip
, long seekpt
)
729 allocinotab(ino_t ino
, struct dinode
*dip
, long seekpt
)
732 register struct inotab
*itp
;
733 struct modeinfo node
;
735 itp
= calloc(1, sizeof(struct inotab
));
737 panic("no memory directory table\n");
738 itp
->t_next
= inotab
[INOHASH(ino
)];
739 inotab
[INOHASH(ino
)] = itp
;
741 itp
->t_seekpt
= seekpt
;
746 node
.timep
[0].tv_sec
= dip
->di_atime
.tv_sec
;
747 node
.timep
[0].tv_usec
= dip
->di_atime
.tv_usec
;
748 node
.timep
[1].tv_sec
= dip
->di_mtime
.tv_sec
;
749 node
.timep
[1].tv_usec
= dip
->di_mtime
.tv_usec
;
750 #else /* __linux__ */
751 node
.timep
[0].tv_sec
= dip
->di_atime
;
752 node
.timep
[0].tv_usec
= dip
->di_atimensec
/ 1000;
753 node
.timep
[1].tv_sec
= dip
->di_mtime
;
754 node
.timep
[1].tv_usec
= dip
->di_mtimensec
/ 1000;
755 #endif /* __linux__ */
756 node
.mode
= dip
->di_mode
;
757 node
.flags
= dip
->di_flags
;
758 node
.uid
= dip
->di_uid
;
759 node
.gid
= dip
->di_gid
;
760 (void) fwrite((char *)&node
, 1, sizeof(struct modeinfo
), mf
);
765 * Look up an inode in the table of directories
767 static struct inotab
*
768 inotablookup(ino_t ino
)
770 register struct inotab
*itp
;
772 for (itp
= inotab
[INOHASH(ino
)]; itp
!= NULL
; itp
= itp
->t_next
)
773 if (itp
->t_ino
== ino
)
785 if (modefile
[0] != '#')
786 (void) unlink(modefile
);
787 if (dirfile
[0] != '#')
788 (void) unlink(dirfile
);