]>
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
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.6 1999/10/13 09:57:20 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) {
384 swabst((u_char
*)"s", (u_char
*)&dp
->d_namlen
);
386 # if BYTE_ORDER == BIG_ENDIAN
388 dp
->d_namlen
= dp
->d_type
;
391 dp
->d_namlen
= dp
->d_type
;
393 #endif /* __linux__ */
394 dp
->d_type
= DT_UNKNOWN
;
398 * Horrible hack to read FreeBSD 2.0 dumps
401 swabst((u_char
*)"6bs", (u_char
*) dp
);
402 #endif /* __linux__ */
404 printf ("reclen = %d, namlen = %d, type = %d\n",
405 dp
->d_reclen
, dp
->d_namlen
, dp
->d_type
);
407 i
= DIRBLKSIZ
- (loc
& (DIRBLKSIZ
- 1));
408 if ((dp
->d_reclen
& 0x3) != 0 ||
410 dp
->d_reclen
< DIRSIZ(0, dp
) ||
411 dp
->d_namlen
> NAME_MAX
) {
412 Vprintf(stdout
, "Mangled directory: ");
413 if ((dp
->d_reclen
& 0x3) != 0)
415 "reclen not multiple of 4 ");
416 if (dp
->d_reclen
< DIRSIZ(0, dp
))
418 "reclen less than DIRSIZ (%d < %d) ",
419 dp
->d_reclen
, DIRSIZ(0, dp
));
420 if (dp
->d_namlen
> NAME_MAX
)
422 "reclen name too big (%d > %d) ",
423 dp
->d_namlen
, NAME_MAX
);
424 Vprintf(stdout
, "\n");
429 if (dp
->d_ino
!= 0) {
437 * These variables are "local" to the following two functions.
439 char dirbuf
[DIRBLKSIZ
];
444 * add a new directory entry to a file.
447 putent(struct direct
*dp
)
449 dp
->d_reclen
= DIRSIZ(0, dp
);
450 if (dirloc
+ dp
->d_reclen
> DIRBLKSIZ
) {
451 ((struct direct
*)(dirbuf
+ prev
))->d_reclen
=
453 (void) fwrite(dirbuf
, 1, DIRBLKSIZ
, df
);
456 memmove(dirbuf
+ dirloc
, dp
, (size_t)dp
->d_reclen
);
458 dirloc
+= dp
->d_reclen
;
462 * flush out a directory that is finished.
467 ((struct direct
*)(dirbuf
+ prev
))->d_reclen
= DIRBLKSIZ
- prev
;
468 (void) fwrite(dirbuf
, (int)dirloc
, 1, df
);
474 dcvt(struct odirect
*odp
, struct direct
*ndp
)
477 memset(ndp
, 0, (size_t)(sizeof *ndp
));
478 ndp
->d_ino
= odp
->d_ino
;
479 ndp
->d_type
= DT_UNKNOWN
;
480 (void) strncpy(ndp
->d_name
, odp
->d_name
, ODIRSIZ
);
481 ndp
->d_namlen
= strlen(ndp
->d_name
);
482 ndp
->d_reclen
= DIRSIZ(0, ndp
);
486 * Seek to an entry in a directory.
487 * Only values returned by rst_telldir should be passed to rst_seekdir.
488 * This routine handles many directories in a single file.
489 * It takes the base of the directory in the file, plus
490 * the desired seek offset into it.
493 rst_seekdir(RST_DIR
*dirp
, long loc
, long base
)
496 if (loc
== rst_telldir(dirp
))
500 fprintf(stderr
, "bad seek pointer to rst_seekdir %ld\n", loc
);
501 (void) lseek(dirp
->dd_fd
, base
+ (loc
& ~(DIRBLKSIZ
- 1)), SEEK_SET
);
502 dirp
->dd_loc
= loc
& (DIRBLKSIZ
- 1);
503 if (dirp
->dd_loc
!= 0)
504 dirp
->dd_size
= read(dirp
->dd_fd
, dirp
->dd_buf
, DIRBLKSIZ
);
508 * get next entry in a directory.
511 rst_readdir(RST_DIR
*dirp
)
513 register struct direct
*dp
;
516 if (dirp
->dd_loc
== 0) {
517 dirp
->dd_size
= read(dirp
->dd_fd
, dirp
->dd_buf
,
519 if (dirp
->dd_size
<= 0) {
520 Dprintf(stderr
, "error reading directory\n");
524 if (dirp
->dd_loc
>= dirp
->dd_size
) {
528 dp
= (struct direct
*)(dirp
->dd_buf
+ dirp
->dd_loc
);
529 if (dp
->d_reclen
== 0 ||
530 dp
->d_reclen
> DIRBLKSIZ
+ 1 - dirp
->dd_loc
) {
531 Dprintf(stderr
, "corrupted directory: bad reclen %d\n",
535 dirp
->dd_loc
+= dp
->d_reclen
;
536 if (dp
->d_ino
== 0 && strcmp(dp
->d_name
, "/") == 0)
538 if (dp
->d_ino
>= maxino
) {
539 Dprintf(stderr
, "corrupted directory: bad inum %d\n",
548 * Simulate the opening of a directory
551 rst_opendir(const char *name
)
557 if ((ino
= dirlookup(name
)) > 0 &&
558 (itp
= inotablookup(ino
)) != NULL
) {
559 dirp
= opendirfile(dirfile
);
560 rst_seekdir(dirp
, itp
->t_seekpt
, itp
->t_seekpt
);
567 * In our case, there is nothing to do when closing a directory.
570 rst_closedir(RST_DIR
*dirp
)
573 (void)close(dirp
->dd_fd
);
579 * Simulate finding the current offset in the directory.
582 rst_telldir(RST_DIR
*dirp
)
584 return ((long)lseek(dirp
->dd_fd
,
585 (off_t
)0, SEEK_CUR
) - dirp
->dd_size
+ dirp
->dd_loc
);
589 * Open a directory file.
592 opendirfile(const char *name
)
594 register RST_DIR
*dirp
;
597 if ((fd
= open(name
, O_RDONLY
)) == -1)
599 if ((dirp
= malloc(sizeof(RST_DIR
))) == NULL
) {
609 * Set the mode, owner, and times for all new or changed directories
612 setdirmodes(int flags
)
615 struct modeinfo node
;
619 Vprintf(stdout
, "Set directory mode, owner, and times.\n");
620 if (command
== 'r' || command
== 'R')
621 (void) snprintf(modefile
, sizeof(modefile
), "%s/rstmode%lu", tmpdir
, (long)dumpdate
);
622 if (modefile
[0] == '#') {
623 panic("modefile not defined\n");
624 fprintf(stderr
, "directory mode, owner, and times not set\n");
627 mf
= fopen(modefile
, "r");
630 fprintf(stderr
, "cannot open mode file %s\n", modefile
);
631 fprintf(stderr
, "directory mode, owner, and times not set\n");
636 (void) fread((char *)&node
, 1, sizeof(struct modeinfo
), mf
);
639 ep
= lookupino(node
.ino
);
640 if (command
== 'i' || command
== 'x') {
643 if ((flags
& FORCE
) == 0 && ep
->e_flags
& EXISTED
) {
647 if (node
.ino
== ROOTINO
&&
648 reply("set owner/mode for '.'") == FAIL
)
652 panic("cannot find directory inode %d\n", node
.ino
);
655 (void) chown(cp
, node
.uid
, node
.gid
);
656 (void) chmod(cp
, node
.mode
);
659 (void) fsetflags(cp
, node
.flags
);
661 (void) chflags(cp
, node
.flags
);
663 utimes(cp
, node
.timep
);
668 panic("error setting directory modes\n");
673 * Generate a literal copy of a directory.
676 genliteraldir(char *name
, ino_t ino
)
678 register struct inotab
*itp
;
679 int ofile
, dp
, i
, size
;
682 itp
= inotablookup(ino
);
684 panic("Cannot find directory inode %d named %s\n", ino
, name
);
685 if ((ofile
= open(name
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666)) < 0) {
686 warn("%s: cannot create file\n", name
);
689 rst_seekdir(dirp
, itp
->t_seekpt
, itp
->t_seekpt
);
690 dp
= dup(dirp
->dd_fd
);
691 for (i
= itp
->t_size
; i
> 0; i
-= BUFSIZ
) {
692 size
= i
< BUFSIZ
? i
: BUFSIZ
;
693 if (read(dp
, buf
, (int) size
) == -1) {
694 warnx("write error extracting inode %lu, name %s\n",
695 (unsigned long)curfile
.ino
, curfile
.name
);
698 if (!Nflag
&& write(ofile
, buf
, (int) size
) == -1) {
699 warnx("write error extracting inode %lu, name %s\n",
700 (unsigned long)curfile
.ino
, curfile
.name
);
710 * Determine the type of an inode
717 itp
= inotablookup(ino
);
724 * Allocate and initialize a directory inode entry.
725 * If requested, save its pertinent mode, owner, and time info.
727 static struct inotab
*
729 allocinotab(ino_t ino
, struct new_bsd_inode
*dip
, long seekpt
)
731 allocinotab(ino_t ino
, struct dinode
*dip
, long seekpt
)
734 register struct inotab
*itp
;
735 struct modeinfo node
;
737 itp
= calloc(1, sizeof(struct inotab
));
739 panic("no memory directory table\n");
740 itp
->t_next
= inotab
[INOHASH(ino
)];
741 inotab
[INOHASH(ino
)] = itp
;
743 itp
->t_seekpt
= seekpt
;
748 node
.timep
[0].tv_sec
= dip
->di_atime
.tv_sec
;
749 node
.timep
[0].tv_usec
= dip
->di_atime
.tv_usec
;
750 node
.timep
[1].tv_sec
= dip
->di_mtime
.tv_sec
;
751 node
.timep
[1].tv_usec
= dip
->di_mtime
.tv_usec
;
752 #else /* __linux__ */
753 node
.timep
[0].tv_sec
= dip
->di_atime
;
754 node
.timep
[0].tv_usec
= dip
->di_atimensec
/ 1000;
755 node
.timep
[1].tv_sec
= dip
->di_mtime
;
756 node
.timep
[1].tv_usec
= dip
->di_mtimensec
/ 1000;
757 #endif /* __linux__ */
758 node
.mode
= dip
->di_mode
;
759 node
.flags
= dip
->di_flags
;
760 node
.uid
= dip
->di_uid
;
761 node
.gid
= dip
->di_gid
;
762 (void) fwrite((char *)&node
, 1, sizeof(struct modeinfo
), mf
);
767 * Look up an inode in the table of directories
769 static struct inotab
*
770 inotablookup(ino_t ino
)
772 register struct inotab
*itp
;
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
);