]>
git.wh0rd.org - dump.git/blob - restore/dirs.c
60f5b23673b15eebe42576aa3887ded3b2e8c894
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. 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.17 2002/01/25 15:08:59 stelian Exp $";
53 #include <sys/param.h>
58 #ifdef HAVE_EXT2FS_EXT2_FS_H
59 #include <ext2fs/ext2_fs.h>
61 #include <linux/ext2_fs.h>
63 #include <bsdcompat.h>
65 #include <ufs/ufs/dinode.h>
66 #include <ufs/ufs/dir.h>
67 #endif /* __linux__ */
68 #include <protocols/dumprestore.h>
70 #include <compaterr.h>
80 #include <machine/endian.h>
83 #include "pathnames.h"
88 * Symbol table of directories read from tape.
91 #define INOHASH(val) (val % HASHSIZE)
93 struct inotab
*t_next
;
98 static struct inotab
*inotab
[HASHSIZE
];
101 * Information retained about directories.
105 struct timeval timep
[2];
113 * Definitions for library routines operating on directories.
116 #define DIRBLKSIZ 1024
121 char dd_buf
[DIRBLKSIZ
];
125 * Global variables for this file.
128 static FILE *df
, *mf
;
129 static RST_DIR
*dirp
;
130 static char dirfile
[MAXPATHLEN
] = "#"; /* No file */
131 static char modefile
[MAXPATHLEN
] = "#"; /* No file */
132 static char dot
[2] = "."; /* So it can be modified */
135 * Format of old style directories.
140 char d_name
[ODIRSIZ
];
144 static struct inotab
*allocinotab
__P((dump_ino_t
, struct new_bsd_inode
*, long));
146 static struct inotab
*allocinotab
__P((dump_ino_t
, struct dinode
*, long));
148 static void dcvt
__P((struct odirect
*, struct direct
*));
149 static void flushent
__P((void));
150 static struct inotab
*inotablookup
__P((dump_ino_t
));
151 static RST_DIR
*opendirfile
__P((const char *));
152 static void putdir
__P((char *, size_t));
153 static void putent
__P((struct direct
*));
154 static void rst_seekdir
__P((RST_DIR
*, long, long));
155 static long rst_telldir
__P((RST_DIR
*));
156 static struct direct
*searchdir
__P((dump_ino_t
, char *));
159 * Extract directory contents, building up a directory structure
160 * on disk for extraction by name.
161 * If genmode is requested, save mode, owner, and times for all
162 * directories on the tape.
165 extractdirs(int genmode
)
169 struct new_bsd_inode
*ip
;
174 struct direct nulldir
;
177 Vprintf(stdout
, "Extract directories from tape\n");
178 (void) snprintf(dirfile
, sizeof(dirfile
), "%s/rstdir%ld", tmpdir
,
180 if (command
!= 'r' && command
!= 'R') {
181 (void) strncat(dirfile
, "-XXXXXX",
182 sizeof(dirfile
) - strlen(dirfile
));
183 fd
= mkstemp(dirfile
);
185 fd
= open(dirfile
, O_RDWR
|O_CREAT
|O_EXCL
, 0666);
186 if (fd
== -1 || (df
= fdopen(fd
, "w")) == NULL
) {
189 err(1, "cannot create directory temporary %s", dirfile
);
192 (void) snprintf(modefile
, sizeof(modefile
), "%s/rstmode%ld", tmpdir
, (long)dumpdate
);
193 if (command
!= 'r' && command
!= 'R') {
194 (void) strncat(modefile
, "-XXXXXX",
195 sizeof(modefile
) - strlen(modefile
));
196 fd
= mkstemp(modefile
);
198 fd
= open(modefile
, O_RDWR
|O_CREAT
|O_EXCL
, 0666);
199 if (fd
== -1 || (mf
= fdopen(fd
, "w")) == NULL
) {
202 err(1, "cannot create modefile %s", modefile
);
206 nulldir
.d_type
= DT_DIR
;
207 nulldir
.d_namlen
= 1;
208 nulldir
.d_name
[0] = '/';
209 nulldir
.d_name
[1] = '\0';
210 nulldir
.d_reclen
= DIRSIZ(0, &nulldir
);
212 curfile
.name
= "<directory file - name unknown>";
213 curfile
.action
= USING
;
215 if (ip
== NULL
|| (ip
->di_mode
& IFMT
) != IFDIR
) {
216 if ( fclose(df
) == EOF
)
217 err(1, "cannot write to file %s", dirfile
);
218 dirp
= opendirfile(dirfile
);
221 if (mf
!= NULL
&& fclose(mf
) == EOF
)
222 err(1, "cannot write to file %s", dirfile
);
225 panic("Root directory is not on tape\n");
228 itp
= allocinotab(curfile
.ino
, ip
, seekpt
);
229 getfile(putdir
, xtrnull
);
232 itp
->t_size
= seekpt
- itp
->t_seekpt
;
237 * skip over all the directories on the tape
243 while (curfile
.dip
&& (curfile
.dip
->di_mode
& IFMT
) == IFDIR
) {
249 * Recursively find names and inumbers of all files in subtree
250 * pname and pass them off to be processed.
253 treescan(char *pname
, dump_ino_t ino
, long (*todo
) __P((char *, dump_ino_t
, int)))
259 char locname
[MAXPATHLEN
+ 1];
261 itp
= inotablookup(ino
);
264 * Pname is name of a simple file or an unchanged directory.
266 (void) (*todo
)(pname
, ino
, LEAF
);
270 * Pname is a dumped directory name.
272 if ((*todo
)(pname
, ino
, NODE
) == FAIL
)
275 * begin search through the directory
276 * skipping over "." and ".."
278 namelen
= snprintf(locname
, sizeof(locname
), "%s/", pname
);
279 if (namelen
>= sizeof(locname
))
280 namelen
= sizeof(locname
) - 1;
281 rst_seekdir(dirp
, itp
->t_seekpt
, itp
->t_seekpt
);
282 dp
= rst_readdir(dirp
); /* "." */
283 if (dp
!= NULL
&& strcmp(dp
->d_name
, ".") == 0)
284 dp
= rst_readdir(dirp
); /* ".." */
286 fprintf(stderr
, "Warning: `.' missing from directory %s\n",
288 if (dp
!= NULL
&& strcmp(dp
->d_name
, "..") == 0)
289 dp
= rst_readdir(dirp
); /* first real entry */
291 fprintf(stderr
, "Warning: `..' missing from directory %s\n",
293 bpt
= rst_telldir(dirp
);
295 * a zero inode signals end of directory
298 locname
[namelen
] = '\0';
299 if (namelen
+ dp
->d_namlen
>= sizeof(locname
)) {
300 fprintf(stderr
, "%s%s: name exceeds %ld char\n",
301 locname
, dp
->d_name
, (long)sizeof(locname
) - 1);
303 (void) strncat(locname
, dp
->d_name
, (int)dp
->d_namlen
);
304 treescan(locname
, dp
->d_ino
, todo
);
305 rst_seekdir(dirp
, bpt
, itp
->t_seekpt
);
307 dp
= rst_readdir(dirp
);
308 bpt
= rst_telldir(dirp
);
313 * Lookup a pathname which is always assumed to start from the ROOTINO.
316 pathsearch(const char *pathname
)
320 char *path
, *name
, buffer
[MAXPATHLEN
];
322 strcpy(buffer
, pathname
);
328 while ((name
= strsep(&path
, "/")) != NULL
&& *name
/* != NULL */) {
329 if ((dp
= searchdir(ino
, name
)) == NULL
)
337 * Lookup the requested name in directory inum.
338 * Return its inode number if found, zero if it does not exist.
340 static struct direct
*
341 searchdir(dump_ino_t inum
, char *name
)
347 itp
= inotablookup(inum
);
350 rst_seekdir(dirp
, itp
->t_seekpt
, itp
->t_seekpt
);
353 dp
= rst_readdir(dirp
);
356 } while (dp
->d_namlen
!= len
|| strncmp(dp
->d_name
, name
, len
) != 0);
361 * Put the directory entries in the directory file
364 putdir(char *buf
, size_t size
)
366 struct direct cvtbuf
;
368 struct odirect
*eodp
;
373 eodp
= (struct odirect
*)&buf
[size
];
374 for (odp
= (struct odirect
*)buf
; odp
< eodp
; odp
++)
375 if (odp
->d_ino
!= 0) {
380 for (loc
= 0; loc
< size
; ) {
381 dp
= (struct direct
*)(buf
+ loc
);
383 printf ("reclen = %d, namlen = %d, type = %d\n",
384 dp
->d_reclen
, dp
->d_namlen
, dp
->d_type
);
387 swabst((u_char
*)"is", (u_char
*) dp
);
388 if (oldinofmt
&& dp
->d_ino
!= 0) {
389 # if BYTE_ORDER == BIG_ENDIAN
391 dp
->d_namlen
= dp
->d_type
;
394 dp
->d_namlen
= dp
->d_type
;
396 dp
->d_type
= DT_UNKNOWN
;
399 printf ("reclen = %d, namlen = %d, type = %d\n",
400 dp
->d_reclen
, dp
->d_namlen
, dp
->d_type
);
402 i
= DIRBLKSIZ
- (loc
& (DIRBLKSIZ
- 1));
403 if ((dp
->d_reclen
& 0x3) != 0 ||
405 dp
->d_reclen
< DIRSIZ(0, dp
) ||
406 dp
->d_namlen
> MAXNAMLEN
) {
407 Vprintf(stdout
, "Mangled directory: ");
408 if ((dp
->d_reclen
& 0x3) != 0)
410 "reclen not multiple of 4 ");
411 if (dp
->d_reclen
< DIRSIZ(0, dp
))
413 "reclen less than DIRSIZ (%d < %d) ",
414 dp
->d_reclen
, DIRSIZ(0, dp
));
415 if (dp
->d_namlen
> MAXNAMLEN
)
417 "reclen name too big (%d > %d) ",
418 dp
->d_namlen
, MAXNAMLEN
);
419 Vprintf(stdout
, "\n");
424 if (dp
->d_ino
!= 0) {
432 * These variables are "local" to the following two functions.
434 char dirbuf
[DIRBLKSIZ
];
439 * add a new directory entry to a file.
442 putent(struct direct
*dp
)
444 dp
->d_reclen
= DIRSIZ(0, dp
);
445 if (dirloc
+ dp
->d_reclen
> DIRBLKSIZ
) {
446 ((struct direct
*)(dirbuf
+ prev
))->d_reclen
=
448 if ( fwrite(dirbuf
, 1, DIRBLKSIZ
, df
) != DIRBLKSIZ
)
449 err(1,"cannot write to file %s", dirfile
);
452 memmove(dirbuf
+ dirloc
, dp
, (size_t)dp
->d_reclen
);
454 dirloc
+= dp
->d_reclen
;
458 * flush out a directory that is finished.
463 ((struct direct
*)(dirbuf
+ prev
))->d_reclen
= DIRBLKSIZ
- prev
;
464 if ( fwrite(dirbuf
, (int)dirloc
, 1, df
) != 1 )
465 err(1, "cannot write to file %s", dirfile
);
468 err(1, "cannot write to file %s", dirfile
);
473 dcvt(struct odirect
*odp
, struct direct
*ndp
)
476 memset(ndp
, 0, (size_t)(sizeof *ndp
));
477 ndp
->d_ino
= odp
->d_ino
;
478 ndp
->d_type
= DT_UNKNOWN
;
479 (void) strncpy(ndp
->d_name
, odp
->d_name
, ODIRSIZ
);
480 ndp
->d_namlen
= strlen(ndp
->d_name
);
481 ndp
->d_reclen
= DIRSIZ(0, ndp
);
485 * Seek to an entry in a directory.
486 * Only values returned by rst_telldir should be passed to rst_seekdir.
487 * This routine handles many directories in a single file.
488 * It takes the base of the directory in the file, plus
489 * the desired seek offset into it.
492 rst_seekdir(RST_DIR
*dirp
, long loc
, long base
)
495 if (loc
== rst_telldir(dirp
))
499 fprintf(stderr
, "bad seek pointer to rst_seekdir %ld\n", loc
);
500 (void) lseek(dirp
->dd_fd
, base
+ (loc
& ~(DIRBLKSIZ
- 1)), SEEK_SET
);
501 dirp
->dd_loc
= loc
& (DIRBLKSIZ
- 1);
502 if (dirp
->dd_loc
!= 0)
503 dirp
->dd_size
= read(dirp
->dd_fd
, dirp
->dd_buf
, DIRBLKSIZ
);
507 * get next entry in a directory.
510 rst_readdir(RST_DIR
*dirp
)
515 if (dirp
->dd_loc
== 0) {
516 dirp
->dd_size
= read(dirp
->dd_fd
, dirp
->dd_buf
,
518 if (dirp
->dd_size
<= 0) {
519 Dprintf(stderr
, "error reading directory\n");
523 if (dirp
->dd_loc
>= dirp
->dd_size
) {
527 dp
= (struct direct
*)(dirp
->dd_buf
+ dirp
->dd_loc
);
528 if (dp
->d_reclen
== 0 ||
529 dp
->d_reclen
> DIRBLKSIZ
+ 1 - dirp
->dd_loc
) {
530 Dprintf(stderr
, "corrupted directory: bad reclen %d\n",
534 dirp
->dd_loc
+= dp
->d_reclen
;
535 if (dp
->d_ino
== 0 && strcmp(dp
->d_name
, "/") == 0)
537 if (dp
->d_ino
>= maxino
) {
538 Dprintf(stderr
, "corrupted directory: bad inum %d\n",
547 * Simulate the opening of a directory
550 rst_opendir(const char *name
)
556 if ((ino
= dirlookup(name
)) > 0 &&
557 (itp
= inotablookup(ino
)) != NULL
) {
558 dirp
= opendirfile(dirfile
);
559 rst_seekdir(dirp
, itp
->t_seekpt
, itp
->t_seekpt
);
566 * In our case, there is nothing to do when closing a directory.
569 rst_closedir(RST_DIR
*dirp
)
572 (void)close(dirp
->dd_fd
);
578 * Simulate finding the current offset in the directory.
581 rst_telldir(RST_DIR
*dirp
)
583 return ((long)lseek(dirp
->dd_fd
,
584 (off_t
)0, SEEK_CUR
) - dirp
->dd_size
+ dirp
->dd_loc
);
588 * Open a directory file.
591 opendirfile(const char *name
)
596 if ((fd
= open(name
, O_RDONLY
)) == -1)
598 if ((dirp
= malloc(sizeof(RST_DIR
))) == NULL
) {
608 * Set the mode, owner, and times for all new or changed directories
611 setdirmodes(int flags
)
614 struct modeinfo node
;
618 Vprintf(stdout
, "Set directory mode, owner, and times.\n");
619 if (command
== 'r' || command
== 'R')
620 (void) snprintf(modefile
, sizeof(modefile
), "%s/rstmode%lu", tmpdir
, (long)dumpdate
);
621 if (modefile
[0] == '#') {
622 panic("modefile not defined\n");
623 fprintf(stderr
, "directory mode, owner, and times not set\n");
626 mf
= fopen(modefile
, "r");
629 fprintf(stderr
, "cannot open mode file %s\n", modefile
);
630 fprintf(stderr
, "directory mode, owner, and times not set\n");
635 (void) fread((char *)&node
, 1, sizeof(struct modeinfo
), mf
);
638 ep
= lookupino(node
.ino
);
639 if (command
== 'i' || command
== 'x') {
642 if ((flags
& FORCE
) == 0 && ep
->e_flags
& EXISTED
) {
646 if (node
.ino
== ROOTINO
&&
647 reply("set owner/mode for '.'") == FAIL
)
651 panic("cannot find directory inode %d\n", node
.ino
);
654 (void) chown(cp
, node
.uid
, node
.gid
);
655 (void) chmod(cp
, node
.mode
);
658 (void) fsetflags(cp
, node
.flags
);
660 (void) chflags(cp
, node
.flags
);
662 utimes(cp
, node
.timep
);
667 panic("error setting directory modes\n");
672 * Generate a literal copy of a directory.
675 genliteraldir(char *name
, dump_ino_t ino
)
678 int ofile
, dp
, i
, size
;
681 itp
= inotablookup(ino
);
683 panic("Cannot find directory inode %d named %s\n", ino
, name
);
684 if ((ofile
= open(name
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666)) < 0) {
685 warn("%s: cannot create file\n", name
);
688 rst_seekdir(dirp
, itp
->t_seekpt
, itp
->t_seekpt
);
689 dp
= dup(dirp
->dd_fd
);
690 for (i
= itp
->t_size
; i
> 0; i
-= BUFSIZ
) {
691 size
= i
< BUFSIZ
? i
: BUFSIZ
;
692 if (read(dp
, buf
, (int) size
) == -1) {
693 warnx("write error extracting inode %lu, name %s\n",
694 (unsigned long)curfile
.ino
, curfile
.name
);
697 if (!Nflag
&& write(ofile
, buf
, (int) size
) == -1) {
698 warnx("write error extracting inode %lu, name %s\n",
699 (unsigned long)curfile
.ino
, curfile
.name
);
709 * Determine the type of an inode
712 inodetype(dump_ino_t ino
)
716 itp
= inotablookup(ino
);
723 * Allocate and initialize a directory inode entry.
724 * If requested, save its pertinent mode, owner, and time info.
726 static struct inotab
*
728 allocinotab(dump_ino_t ino
, struct new_bsd_inode
*dip
, long seekpt
)
730 allocinotab(dump_ino_t ino
, struct dinode
*dip
, long seekpt
)
734 struct modeinfo node
;
736 itp
= calloc(1, sizeof(struct inotab
));
738 panic("no memory directory table\n");
739 itp
->t_next
= inotab
[INOHASH(ino
)];
740 inotab
[INOHASH(ino
)] = itp
;
742 itp
->t_seekpt
= seekpt
;
747 node
.timep
[0].tv_sec
= dip
->di_atime
.tv_sec
;
748 node
.timep
[0].tv_usec
= dip
->di_atime
.tv_usec
;
749 node
.timep
[1].tv_sec
= dip
->di_mtime
.tv_sec
;
750 node
.timep
[1].tv_usec
= dip
->di_mtime
.tv_usec
;
751 #else /* __linux__ */
752 node
.timep
[0].tv_sec
= dip
->di_atime
;
753 node
.timep
[0].tv_usec
= dip
->di_atimensec
/ 1000;
754 node
.timep
[1].tv_sec
= dip
->di_mtime
;
755 node
.timep
[1].tv_usec
= dip
->di_mtimensec
/ 1000;
756 #endif /* __linux__ */
757 node
.mode
= dip
->di_mode
;
758 node
.flags
= dip
->di_flags
;
759 node
.uid
= dip
->di_uid
;
760 node
.gid
= dip
->di_gid
;
761 if ( fwrite((char *)&node
, 1, sizeof(struct modeinfo
), mf
) != sizeof(struct modeinfo
) )
762 err(1,"cannot write to file %s", modefile
);
767 * Look up an inode in the table of directories
769 static struct inotab
*
770 inotablookup(dump_ino_t ino
)
774 for (itp
= inotab
[INOHASH(ino
)]; itp
!= NULL
; itp
= itp
->t_next
)
775 if (itp
->t_ino
== ino
)
787 if (modefile
[0] != '#')
788 (void) unlink(modefile
);
789 if (dirfile
[0] != '#')
790 (void) unlink(dirfile
);