]>
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 <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.25 2004/03/29 13:57:29 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
;
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
) ||
423 dp
->d_namlen
> MAXNAMLEN
) {
424 Vprintf(stdout
, "Mangled directory: ");
425 if ((dp
->d_reclen
& 0x3) != 0)
427 "reclen not multiple of 4 ");
428 if (dp
->d_reclen
< DIRSIZ(0, dp
))
430 "reclen less than DIRSIZ (%d < %d) ",
431 dp
->d_reclen
, DIRSIZ(0, dp
));
432 if (dp
->d_namlen
> MAXNAMLEN
)
434 "reclen name too big (%d > %d) ",
435 dp
->d_namlen
, MAXNAMLEN
);
436 Vprintf(stdout
, "\n");
441 if (dp
->d_ino
!= 0) {
449 * These variables are "local" to the following two functions.
451 static char dirbuf
[DIRBLKSIZ
];
452 static long dirloc
= 0;
453 static long prev
= 0;
456 * add a new directory entry to a file.
459 putent(struct direct
*dp
)
461 dp
->d_reclen
= DIRSIZ(0, dp
);
462 if (dirloc
+ dp
->d_reclen
> DIRBLKSIZ
) {
463 ((struct direct
*)(dirbuf
+ prev
))->d_reclen
=
465 if ( fwrite(dirbuf
, 1, DIRBLKSIZ
, df
) != DIRBLKSIZ
)
466 err(1,"cannot write to file %s", dirfile
);
469 memmove(dirbuf
+ dirloc
, dp
, (size_t)dp
->d_reclen
);
471 dirloc
+= dp
->d_reclen
;
475 * flush out a directory that is finished.
480 ((struct direct
*)(dirbuf
+ prev
))->d_reclen
= DIRBLKSIZ
- prev
;
481 if ( fwrite(dirbuf
, (int)dirloc
, 1, df
) != 1 )
482 err(1, "cannot write to file %s", dirfile
);
485 err(1, "cannot write to file %s", dirfile
);
490 dcvt(struct odirect
*odp
, struct direct
*ndp
)
493 memset(ndp
, 0, (size_t)(sizeof *ndp
));
494 ndp
->d_ino
= odp
->d_ino
;
495 ndp
->d_type
= DT_UNKNOWN
;
496 (void) strncpy(ndp
->d_name
, odp
->d_name
, ODIRSIZ
);
497 ndp
->d_namlen
= strlen(ndp
->d_name
);
498 ndp
->d_reclen
= DIRSIZ(0, ndp
);
502 * Seek to an entry in a directory.
503 * Only values returned by rst_telldir should be passed to rst_seekdir.
504 * This routine handles many directories in a single file.
505 * It takes the base of the directory in the file, plus
506 * the desired seek offset into it.
509 rst_seekdir(RST_DIR
*dirp
, OFF_T loc
, OFF_T base
)
512 if (loc
== rst_telldir(dirp
))
516 fprintf(stderr
, "bad seek pointer to rst_seekdir %lld\n", loc
);
517 (void) LSEEK(dirp
->dd_fd
, base
+ (loc
& ~(DIRBLKSIZ
- 1)), SEEK_SET
);
518 dirp
->dd_loc
= loc
& (DIRBLKSIZ
- 1);
519 if (dirp
->dd_loc
!= 0)
520 dirp
->dd_size
= read(dirp
->dd_fd
, dirp
->dd_buf
, DIRBLKSIZ
);
524 * get next entry in a directory.
527 rst_readdir(RST_DIR
*dirp
)
532 if (dirp
->dd_loc
== 0) {
533 dirp
->dd_size
= read(dirp
->dd_fd
, dirp
->dd_buf
,
535 if (dirp
->dd_size
<= 0) {
536 Dprintf(stderr
, "error reading directory\n");
540 if (dirp
->dd_loc
>= dirp
->dd_size
) {
544 dp
= (struct direct
*)(dirp
->dd_buf
+ dirp
->dd_loc
);
545 if (dp
->d_reclen
== 0 ||
546 dp
->d_reclen
> DIRBLKSIZ
+ 1 - dirp
->dd_loc
) {
547 Dprintf(stderr
, "corrupted directory: bad reclen %d\n",
551 dirp
->dd_loc
+= dp
->d_reclen
;
552 if (dp
->d_ino
== 0 && strcmp(dp
->d_name
, "/") == 0)
554 if (dp
->d_ino
>= maxino
) {
555 Dprintf(stderr
, "corrupted directory: bad inum %d\n",
564 * Simulate the opening of a directory
567 rst_opendir(const char *name
)
573 if ((ino
= dirlookup(name
)) > 0 &&
574 (itp
= inotablookup(ino
)) != NULL
) {
575 dirp
= opendirfile(dirfile
);
576 rst_seekdir(dirp
, itp
->t_seekpt
, itp
->t_seekpt
);
583 * In our case, there is nothing to do when closing a directory.
586 rst_closedir(RST_DIR
*dirp
)
589 (void)close(dirp
->dd_fd
);
595 * Simulate finding the current offset in the directory.
598 rst_telldir(RST_DIR
*dirp
)
600 return ((OFF_T
)LSEEK(dirp
->dd_fd
,
601 (OFF_T
)0, SEEK_CUR
) - dirp
->dd_size
+ dirp
->dd_loc
);
605 * Open a directory file.
608 opendirfile(const char *name
)
613 if ((fd
= OPEN(name
, O_RDONLY
)) == -1)
615 if ((dirp
= malloc(sizeof(RST_DIR
))) == NULL
) {
625 * Set the mode, owner, and times for all new or changed directories
628 setdirmodes(int flags
)
631 struct modeinfo node
;
635 Vprintf(stdout
, "Set directory mode, owner, and times.\n");
636 if (command
== 'r' || command
== 'R')
637 (void) snprintf(modefile
, sizeof(modefile
), "%s/rstmode%lu", tmpdir
, (long)dumpdate
);
638 if (modefile
[0] == '#') {
639 panic("modefile not defined\n");
640 fprintf(stderr
, "directory mode, owner, and times not set\n");
643 mf
= fopen(modefile
, "r");
646 fprintf(stderr
, "cannot open mode file %s\n", modefile
);
647 fprintf(stderr
, "directory mode, owner, and times not set\n");
652 (void) fread((char *)&node
, 1, sizeof(struct modeinfo
), mf
);
655 ep
= lookupino(node
.ino
);
656 if (command
== 'i' || command
== 'x') {
659 if ((flags
& FORCE
) == 0 && ep
->e_flags
& EXISTED
) {
663 if ((flags
& FORCE
) == 0 &&
664 node
.ino
== ROOTINO
&&
665 reply("set owner/mode for '.'") == FAIL
)
669 panic("cannot find directory inode %d\n", node
.ino
);
672 (void) chown(cp
, node
.uid
, node
.gid
);
673 (void) chmod(cp
, node
.mode
);
676 (void) fsetflags(cp
, node
.flags
);
680 (void) chflags(cp
, node
.flags
);
683 utimes(cp
, node
.timep
);
688 panic("error setting directory modes\n");
693 * Generate a literal copy of a directory.
696 genliteraldir(char *name
, dump_ino_t ino
)
699 int ofile
, dp
, i
, size
;
702 itp
= inotablookup(ino
);
704 panic("Cannot find directory inode %d named %s\n", ino
, name
);
705 if ((ofile
= open(name
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666)) < 0) {
706 warn("%s: cannot create file\n", name
);
709 rst_seekdir(dirp
, itp
->t_seekpt
, itp
->t_seekpt
);
710 dp
= dup(dirp
->dd_fd
);
711 for (i
= itp
->t_size
; i
> 0; i
-= BUFSIZ
) {
712 size
= i
< BUFSIZ
? i
: BUFSIZ
;
713 if (read(dp
, buf
, (int) size
) == -1) {
714 warnx("write error extracting inode %lu, name %s\n",
715 (unsigned long)curfile
.ino
, curfile
.name
);
718 if (!Nflag
&& write(ofile
, buf
, (int) size
) == -1) {
719 warnx("write error extracting inode %lu, name %s\n",
720 (unsigned long)curfile
.ino
, curfile
.name
);
730 * Determine the type of an inode
733 inodetype(dump_ino_t ino
)
737 itp
= inotablookup(ino
);
744 * Allocate and initialize a directory inode entry.
745 * If requested, save its pertinent mode, owner, and time info.
747 static struct inotab
*
748 #if defined(__linux__) || defined(sunos)
749 allocinotab(dump_ino_t ino
, struct new_bsd_inode
*dip
, OFF_T seekpt
)
751 allocinotab(dump_ino_t ino
, struct dinode
*dip
, OFF_T seekpt
)
755 struct modeinfo node
;
757 itp
= calloc(1, sizeof(struct inotab
));
759 panic("no memory directory table\n");
760 itp
->t_next
= inotab
[INOHASH(ino
)];
761 inotab
[INOHASH(ino
)] = itp
;
763 itp
->t_seekpt
= seekpt
;
767 #if defined(__linux__) || defined(sunos)
768 node
.timep
[0].tv_sec
= dip
->di_atime
.tv_sec
;
769 node
.timep
[0].tv_usec
= dip
->di_atime
.tv_usec
;
770 node
.timep
[1].tv_sec
= dip
->di_mtime
.tv_sec
;
771 node
.timep
[1].tv_usec
= dip
->di_mtime
.tv_usec
;
772 #else /* __linux__ || sunos */
773 node
.timep
[0].tv_sec
= dip
->di_atime
;
774 node
.timep
[0].tv_usec
= dip
->di_atimensec
/ 1000;
775 node
.timep
[1].tv_sec
= dip
->di_mtime
;
776 node
.timep
[1].tv_usec
= dip
->di_mtimensec
/ 1000;
777 #endif /* __linux__ || sunos */
778 node
.mode
= dip
->di_mode
;
779 node
.flags
= dip
->di_flags
;
780 node
.uid
= dip
->di_uid
;
781 node
.gid
= dip
->di_gid
;
782 if ( fwrite((char *)&node
, 1, sizeof(struct modeinfo
), mf
) != sizeof(struct modeinfo
) )
783 err(1,"cannot write to file %s", modefile
);
788 * Look up an inode in the table of directories
790 static struct inotab
*
791 inotablookup(dump_ino_t ino
)
795 for (itp
= inotab
[INOHASH(ino
)]; itp
!= NULL
; itp
= itp
->t_next
)
796 if (itp
->t_ino
== ino
)
808 if (modefile
[0] != '#')
809 (void) unlink(modefile
);
810 if (dirfile
[0] != '#')
811 (void) unlink(dirfile
);