]>
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-2000
6 * Stelian Pop <pop@cybercable.fr> - Alcôve <www.alcove.fr>, 2000
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
48 static const char rcsid
[] =
49 "$Id: dirs.c,v 1.10 2000/11/10 14:42:25 stelian Exp $";
52 #include <sys/param.h>
57 #include <linux/ext2_fs.h>
58 #include <bsdcompat.h>
60 #include <ufs/ufs/dinode.h>
61 #include <ufs/ufs/dir.h>
62 #endif /* __linux__ */
63 #include <protocols/dumprestore.h>
65 #include <compaterr.h>
75 #include <machine/endian.h>
78 #include "pathnames.h"
83 * Symbol table of directories read from tape.
86 #define INOHASH(val) (val % HASHSIZE)
88 struct inotab
*t_next
;
93 static struct inotab
*inotab
[HASHSIZE
];
96 * Information retained about directories.
100 struct timeval timep
[2];
108 * Definitions for library routines operating on directories.
111 #define DIRBLKSIZ 1024
116 char dd_buf
[DIRBLKSIZ
];
120 * Global variables for this file.
123 static FILE *df
, *mf
;
124 static RST_DIR
*dirp
;
125 static char dirfile
[MAXPATHLEN
] = "#"; /* No file */
126 static char modefile
[MAXPATHLEN
] = "#"; /* No file */
127 static char dot
[2] = "."; /* So it can be modified */
130 * Format of old style directories.
135 char d_name
[ODIRSIZ
];
139 static struct inotab
*allocinotab
__P((ino_t
, struct new_bsd_inode
*, long));
141 static struct inotab
*allocinotab
__P((ino_t
, struct dinode
*, long));
143 static void dcvt
__P((struct odirect
*, struct direct
*));
144 static void flushent
__P((void));
145 static struct inotab
*inotablookup
__P((ino_t
));
146 static RST_DIR
*opendirfile
__P((const char *));
147 static void putdir
__P((char *, size_t));
148 static void putent
__P((struct direct
*));
149 static void rst_seekdir
__P((RST_DIR
*, long, long));
150 static long rst_telldir
__P((RST_DIR
*));
151 static struct direct
*searchdir
__P((ino_t
, char *));
154 * Extract directory contents, building up a directory structure
155 * on disk for extraction by name.
156 * If genmode is requested, save mode, owner, and times for all
157 * directories on the tape.
160 extractdirs(int genmode
)
164 register struct new_bsd_inode
*ip
;
166 register struct dinode
*ip
;
169 struct direct nulldir
;
172 Vprintf(stdout
, "Extract directories from tape\n");
173 (void) snprintf(dirfile
, sizeof(dirfile
), "%s/rstdir%ld", tmpdir
,
175 if (command
!= 'r' && command
!= 'R') {
176 (void) strncat(dirfile
, "-XXXXXX",
177 sizeof(dirfile
) - strlen(dirfile
));
178 fd
= mkstemp(dirfile
);
180 fd
= open(dirfile
, O_RDWR
|O_CREAT
|O_EXCL
, 0666);
181 if (fd
== -1 || (df
= fdopen(fd
, "w")) == NULL
) {
184 err(1, "cannot create directory temporary %s", dirfile
);
187 (void) snprintf(modefile
, sizeof(modefile
), "%s/rstmode%ld", tmpdir
, (long)dumpdate
);
188 if (command
!= 'r' && command
!= 'R') {
189 (void) strncat(modefile
, "-XXXXXX",
190 sizeof(modefile
) - strlen(modefile
));
191 fd
= mkstemp(modefile
);
193 fd
= open(modefile
, O_RDWR
|O_CREAT
|O_EXCL
, 0666);
194 if (fd
== -1 || (mf
= fdopen(fd
, "w")) == NULL
) {
197 err(1, "cannot create modefile %s", modefile
);
201 nulldir
.d_type
= DT_DIR
;
202 nulldir
.d_namlen
= 1;
203 nulldir
.d_name
[0] = '/';
204 nulldir
.d_name
[1] = '\0';
205 nulldir
.d_reclen
= DIRSIZ(0, &nulldir
);
207 curfile
.name
= "<directory file - name unknown>";
208 curfile
.action
= USING
;
210 if (ip
== NULL
|| (ip
->di_mode
& IFMT
) != IFDIR
) {
212 dirp
= opendirfile(dirfile
);
219 panic("Root directory is not on tape\n");
222 itp
= allocinotab(curfile
.ino
, ip
, seekpt
);
223 getfile(putdir
, xtrnull
);
226 itp
->t_size
= seekpt
- itp
->t_seekpt
;
231 * skip over all the directories on the tape
237 while (curfile
.dip
&& (curfile
.dip
->di_mode
& IFMT
) == IFDIR
) {
243 * Recursively find names and inumbers of all files in subtree
244 * pname and pass them off to be processed.
247 treescan(char *pname
, ino_t ino
, long (*todo
) __P((char *, ino_t
, int)))
249 register struct inotab
*itp
;
250 register struct direct
*dp
;
253 char locname
[MAXPATHLEN
+ 1];
255 itp
= inotablookup(ino
);
258 * Pname is name of a simple file or an unchanged directory.
260 (void) (*todo
)(pname
, ino
, LEAF
);
264 * Pname is a dumped directory name.
266 if ((*todo
)(pname
, ino
, NODE
) == FAIL
)
269 * begin search through the directory
270 * skipping over "." and ".."
272 namelen
= snprintf(locname
, sizeof(locname
), "%s/", pname
);
273 if (namelen
>= sizeof(locname
))
274 namelen
= sizeof(locname
) - 1;
275 rst_seekdir(dirp
, itp
->t_seekpt
, itp
->t_seekpt
);
276 dp
= rst_readdir(dirp
); /* "." */
277 if (dp
!= NULL
&& strcmp(dp
->d_name
, ".") == 0)
278 dp
= rst_readdir(dirp
); /* ".." */
280 fprintf(stderr
, "Warning: `.' missing from directory %s\n",
282 if (dp
!= NULL
&& strcmp(dp
->d_name
, "..") == 0)
283 dp
= rst_readdir(dirp
); /* first real entry */
285 fprintf(stderr
, "Warning: `..' missing from directory %s\n",
287 bpt
= rst_telldir(dirp
);
289 * a zero inode signals end of directory
292 locname
[namelen
] = '\0';
293 if (namelen
+ dp
->d_namlen
>= sizeof(locname
)) {
294 fprintf(stderr
, "%s%s: name exceeds %ld char\n",
295 locname
, dp
->d_name
, (long)sizeof(locname
) - 1);
297 (void) strncat(locname
, dp
->d_name
, (int)dp
->d_namlen
);
298 treescan(locname
, dp
->d_ino
, todo
);
299 rst_seekdir(dirp
, bpt
, itp
->t_seekpt
);
301 dp
= rst_readdir(dirp
);
302 bpt
= rst_telldir(dirp
);
307 * Lookup a pathname which is always assumed to start from the ROOTINO.
310 pathsearch(const char *pathname
)
314 char *path
, *name
, buffer
[MAXPATHLEN
];
316 strcpy(buffer
, pathname
);
322 while ((name
= strsep(&path
, "/")) != NULL
&& *name
/* != NULL */) {
323 if ((dp
= searchdir(ino
, name
)) == NULL
)
331 * Lookup the requested name in directory inum.
332 * Return its inode number if found, zero if it does not exist.
334 static struct direct
*
335 searchdir(ino_t inum
, char *name
)
337 register struct direct
*dp
;
338 register struct inotab
*itp
;
341 itp
= inotablookup(inum
);
344 rst_seekdir(dirp
, itp
->t_seekpt
, itp
->t_seekpt
);
347 dp
= rst_readdir(dirp
);
350 } while (dp
->d_namlen
!= len
|| strncmp(dp
->d_name
, name
, len
) != 0);
355 * Put the directory entries in the directory file
358 putdir(char *buf
, size_t size
)
360 struct direct cvtbuf
;
361 register struct odirect
*odp
;
362 struct odirect
*eodp
;
363 register struct direct
*dp
;
367 eodp
= (struct odirect
*)&buf
[size
];
368 for (odp
= (struct odirect
*)buf
; odp
< eodp
; odp
++)
369 if (odp
->d_ino
!= 0) {
374 for (loc
= 0; loc
< size
; ) {
375 dp
= (struct direct
*)(buf
+ loc
);
377 printf ("reclen = %d, namlen = %d, type = %d\n",
378 dp
->d_reclen
, dp
->d_namlen
, dp
->d_type
);
381 swabst((u_char
*)"is", (u_char
*) dp
);
382 if (oldinofmt
&& dp
->d_ino
!= 0) {
383 # if BYTE_ORDER == BIG_ENDIAN
385 dp
->d_namlen
= dp
->d_type
;
388 dp
->d_namlen
= dp
->d_type
;
390 dp
->d_type
= DT_UNKNOWN
;
393 printf ("reclen = %d, namlen = %d, type = %d\n",
394 dp
->d_reclen
, dp
->d_namlen
, dp
->d_type
);
396 i
= DIRBLKSIZ
- (loc
& (DIRBLKSIZ
- 1));
397 if ((dp
->d_reclen
& 0x3) != 0 ||
399 dp
->d_reclen
< DIRSIZ(0, dp
) ||
400 dp
->d_namlen
> MAXNAMLEN
) {
401 Vprintf(stdout
, "Mangled directory: ");
402 if ((dp
->d_reclen
& 0x3) != 0)
404 "reclen not multiple of 4 ");
405 if (dp
->d_reclen
< DIRSIZ(0, dp
))
407 "reclen less than DIRSIZ (%d < %d) ",
408 dp
->d_reclen
, DIRSIZ(0, dp
));
409 if (dp
->d_namlen
> MAXNAMLEN
)
411 "reclen name too big (%d > %d) ",
412 dp
->d_namlen
, MAXNAMLEN
);
413 Vprintf(stdout
, "\n");
418 if (dp
->d_ino
!= 0) {
426 * These variables are "local" to the following two functions.
428 char dirbuf
[DIRBLKSIZ
];
433 * add a new directory entry to a file.
436 putent(struct direct
*dp
)
438 dp
->d_reclen
= DIRSIZ(0, dp
);
439 if (dirloc
+ dp
->d_reclen
> DIRBLKSIZ
) {
440 ((struct direct
*)(dirbuf
+ prev
))->d_reclen
=
442 (void) fwrite(dirbuf
, 1, DIRBLKSIZ
, df
);
445 memmove(dirbuf
+ dirloc
, dp
, (size_t)dp
->d_reclen
);
447 dirloc
+= dp
->d_reclen
;
451 * flush out a directory that is finished.
456 ((struct direct
*)(dirbuf
+ prev
))->d_reclen
= DIRBLKSIZ
- prev
;
457 (void) fwrite(dirbuf
, (int)dirloc
, 1, df
);
463 dcvt(struct odirect
*odp
, struct direct
*ndp
)
466 memset(ndp
, 0, (size_t)(sizeof *ndp
));
467 ndp
->d_ino
= odp
->d_ino
;
468 ndp
->d_type
= DT_UNKNOWN
;
469 (void) strncpy(ndp
->d_name
, odp
->d_name
, ODIRSIZ
);
470 ndp
->d_namlen
= strlen(ndp
->d_name
);
471 ndp
->d_reclen
= DIRSIZ(0, ndp
);
475 * Seek to an entry in a directory.
476 * Only values returned by rst_telldir should be passed to rst_seekdir.
477 * This routine handles many directories in a single file.
478 * It takes the base of the directory in the file, plus
479 * the desired seek offset into it.
482 rst_seekdir(RST_DIR
*dirp
, long loc
, long base
)
485 if (loc
== rst_telldir(dirp
))
489 fprintf(stderr
, "bad seek pointer to rst_seekdir %ld\n", loc
);
490 (void) lseek(dirp
->dd_fd
, base
+ (loc
& ~(DIRBLKSIZ
- 1)), SEEK_SET
);
491 dirp
->dd_loc
= loc
& (DIRBLKSIZ
- 1);
492 if (dirp
->dd_loc
!= 0)
493 dirp
->dd_size
= read(dirp
->dd_fd
, dirp
->dd_buf
, DIRBLKSIZ
);
497 * get next entry in a directory.
500 rst_readdir(RST_DIR
*dirp
)
502 register struct direct
*dp
;
505 if (dirp
->dd_loc
== 0) {
506 dirp
->dd_size
= read(dirp
->dd_fd
, dirp
->dd_buf
,
508 if (dirp
->dd_size
<= 0) {
509 Dprintf(stderr
, "error reading directory\n");
513 if (dirp
->dd_loc
>= dirp
->dd_size
) {
517 dp
= (struct direct
*)(dirp
->dd_buf
+ dirp
->dd_loc
);
518 if (dp
->d_reclen
== 0 ||
519 dp
->d_reclen
> DIRBLKSIZ
+ 1 - dirp
->dd_loc
) {
520 Dprintf(stderr
, "corrupted directory: bad reclen %d\n",
524 dirp
->dd_loc
+= dp
->d_reclen
;
525 if (dp
->d_ino
== 0 && strcmp(dp
->d_name
, "/") == 0)
527 if (dp
->d_ino
>= maxino
) {
528 Dprintf(stderr
, "corrupted directory: bad inum %d\n",
537 * Simulate the opening of a directory
540 rst_opendir(const char *name
)
546 if ((ino
= dirlookup(name
)) > 0 &&
547 (itp
= inotablookup(ino
)) != NULL
) {
548 dirp
= opendirfile(dirfile
);
549 rst_seekdir(dirp
, itp
->t_seekpt
, itp
->t_seekpt
);
556 * In our case, there is nothing to do when closing a directory.
559 rst_closedir(RST_DIR
*dirp
)
562 (void)close(dirp
->dd_fd
);
568 * Simulate finding the current offset in the directory.
571 rst_telldir(RST_DIR
*dirp
)
573 return ((long)lseek(dirp
->dd_fd
,
574 (off_t
)0, SEEK_CUR
) - dirp
->dd_size
+ dirp
->dd_loc
);
578 * Open a directory file.
581 opendirfile(const char *name
)
583 register RST_DIR
*dirp
;
586 if ((fd
= open(name
, O_RDONLY
)) == -1)
588 if ((dirp
= malloc(sizeof(RST_DIR
))) == NULL
) {
598 * Set the mode, owner, and times for all new or changed directories
601 setdirmodes(int flags
)
604 struct modeinfo node
;
608 Vprintf(stdout
, "Set directory mode, owner, and times.\n");
609 if (command
== 'r' || command
== 'R')
610 (void) snprintf(modefile
, sizeof(modefile
), "%s/rstmode%lu", tmpdir
, (long)dumpdate
);
611 if (modefile
[0] == '#') {
612 panic("modefile not defined\n");
613 fprintf(stderr
, "directory mode, owner, and times not set\n");
616 mf
= fopen(modefile
, "r");
619 fprintf(stderr
, "cannot open mode file %s\n", modefile
);
620 fprintf(stderr
, "directory mode, owner, and times not set\n");
625 (void) fread((char *)&node
, 1, sizeof(struct modeinfo
), mf
);
628 ep
= lookupino(node
.ino
);
629 if (command
== 'i' || command
== 'x') {
632 if ((flags
& FORCE
) == 0 && ep
->e_flags
& EXISTED
) {
636 if (node
.ino
== ROOTINO
&&
637 reply("set owner/mode for '.'") == FAIL
)
641 panic("cannot find directory inode %d\n", node
.ino
);
644 (void) chown(cp
, node
.uid
, node
.gid
);
645 (void) chmod(cp
, node
.mode
);
648 (void) fsetflags(cp
, node
.flags
);
650 (void) chflags(cp
, node
.flags
);
652 utimes(cp
, node
.timep
);
657 panic("error setting directory modes\n");
662 * Generate a literal copy of a directory.
665 genliteraldir(char *name
, ino_t ino
)
667 register struct inotab
*itp
;
668 int ofile
, dp
, i
, size
;
671 itp
= inotablookup(ino
);
673 panic("Cannot find directory inode %d named %s\n", ino
, name
);
674 if ((ofile
= open(name
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666)) < 0) {
675 warn("%s: cannot create file\n", name
);
678 rst_seekdir(dirp
, itp
->t_seekpt
, itp
->t_seekpt
);
679 dp
= dup(dirp
->dd_fd
);
680 for (i
= itp
->t_size
; i
> 0; i
-= BUFSIZ
) {
681 size
= i
< BUFSIZ
? i
: BUFSIZ
;
682 if (read(dp
, buf
, (int) size
) == -1) {
683 warnx("write error extracting inode %lu, name %s\n",
684 (unsigned long)curfile
.ino
, curfile
.name
);
687 if (!Nflag
&& write(ofile
, buf
, (int) size
) == -1) {
688 warnx("write error extracting inode %lu, name %s\n",
689 (unsigned long)curfile
.ino
, curfile
.name
);
699 * Determine the type of an inode
706 itp
= inotablookup(ino
);
713 * Allocate and initialize a directory inode entry.
714 * If requested, save its pertinent mode, owner, and time info.
716 static struct inotab
*
718 allocinotab(ino_t ino
, struct new_bsd_inode
*dip
, long seekpt
)
720 allocinotab(ino_t ino
, struct dinode
*dip
, long seekpt
)
723 register struct inotab
*itp
;
724 struct modeinfo node
;
726 itp
= calloc(1, sizeof(struct inotab
));
728 panic("no memory directory table\n");
729 itp
->t_next
= inotab
[INOHASH(ino
)];
730 inotab
[INOHASH(ino
)] = itp
;
732 itp
->t_seekpt
= seekpt
;
737 node
.timep
[0].tv_sec
= dip
->di_atime
.tv_sec
;
738 node
.timep
[0].tv_usec
= dip
->di_atime
.tv_usec
;
739 node
.timep
[1].tv_sec
= dip
->di_mtime
.tv_sec
;
740 node
.timep
[1].tv_usec
= dip
->di_mtime
.tv_usec
;
741 #else /* __linux__ */
742 node
.timep
[0].tv_sec
= dip
->di_atime
;
743 node
.timep
[0].tv_usec
= dip
->di_atimensec
/ 1000;
744 node
.timep
[1].tv_sec
= dip
->di_mtime
;
745 node
.timep
[1].tv_usec
= dip
->di_mtimensec
/ 1000;
746 #endif /* __linux__ */
747 node
.mode
= dip
->di_mode
;
748 node
.flags
= dip
->di_flags
;
749 node
.uid
= dip
->di_uid
;
750 node
.gid
= dip
->di_gid
;
751 (void) fwrite((char *)&node
, 1, sizeof(struct modeinfo
), mf
);
756 * Look up an inode in the table of directories
758 static struct inotab
*
759 inotablookup(ino_t ino
)
761 register struct inotab
*itp
;
763 for (itp
= inotab
[INOHASH(ino
)]; itp
!= NULL
; itp
= itp
->t_next
)
764 if (itp
->t_ino
== ino
)
776 if (modefile
[0] != '#')
777 (void) unlink(modefile
);
778 if (dirfile
[0] != '#')
779 (void) unlink(dirfile
);