2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)dirs.c 8.7 (Berkeley) 5/1/95
39 * $FreeBSD: src/sbin/restore/dirs.c,v 1.14.2.5 2001/10/15 13:44:45 dd Exp $
40 * $DragonFly: src/sbin/restore/dirs.c,v 1.10 2006/04/03 01:58:49 dillon Exp $
43 #include <sys/param.h>
48 #include <vfs/ufs/dinode.h>
49 #include <vfs/ufs/dir.h>
50 #include <protocols/dumprestore.h>
64 * Symbol table of directories read from tape.
67 #define INOHASH(val) (val % HASHSIZE)
69 struct inotab
*t_next
;
74 static struct inotab
*inotab
[HASHSIZE
];
77 * Information retained about directories.
81 struct timeval timep
[2];
89 * Definitions for library routines operating on directories.
92 #define DIRBLKSIZ 1024
97 char dd_buf
[DIRBLKSIZ
];
101 * Global variables for this file.
104 static FILE *df
, *mf
;
105 static RST_DIR
*dirp
;
106 static char dirfile
[MAXPATHLEN
] = "#"; /* No file */
107 static char modefile
[MAXPATHLEN
] = "#"; /* No file */
108 static char dot
[2] = "."; /* So it can be modified */
111 * Format of old style directories.
116 char d_name
[ODIRSIZ
];
119 static struct inotab
*allocinotab(ufs1_ino_t
, struct ufs1_dinode
*, long);
120 static void dcvt(struct odirect
*, struct direct
*);
121 static void flushent(void);
122 static struct inotab
*inotablookup(ufs1_ino_t
);
123 static RST_DIR
*opendirfile(const char *);
124 static void putdir(char *, long);
125 static void putent(struct direct
*);
126 static void rst_seekdir(RST_DIR
*, long, long);
127 static long rst_telldir(RST_DIR
*);
128 static struct direct
*searchdir(ufs1_ino_t
, char *);
131 * Extract directory contents, building up a directory structure
132 * on disk for extraction by name.
133 * If genmode is requested, save mode, owner, and times for all
134 * directories on the tape.
137 extractdirs(int genmode
)
140 struct ufs1_dinode
*ip
;
142 struct direct nulldir
;
146 vprintf(stdout
, "Extract directories from tape\n");
147 if ((tmpdir
= getenv("TMPDIR")) == NULL
|| tmpdir
[0] == '\0')
149 sprintf(dirfile
, "%s/rstdir%ld", tmpdir
, (long)dumpdate
);
150 if (command
!= 'r' && command
!= 'R') {
151 (void *) strcat(dirfile
, "-XXXXXX");
152 fd
= mkstemp(dirfile
);
154 fd
= open(dirfile
, O_RDWR
|O_CREAT
|O_EXCL
, 0666);
155 if (fd
== -1 || (df
= fdopen(fd
, "w")) == NULL
) {
158 warn("%s - cannot create directory temporary\nfopen", dirfile
);
162 sprintf(modefile
, "%s/rstmode%ld", tmpdir
, (long)dumpdate
);
163 if (command
!= 'r' && command
!= 'R') {
164 (void *) strcat(modefile
, "-XXXXXX");
165 fd
= mkstemp(modefile
);
167 fd
= open(modefile
, O_RDWR
|O_CREAT
|O_EXCL
, 0666);
168 if (fd
== -1 || (mf
= fdopen(fd
, "w")) == NULL
) {
171 warn("%s - cannot create modefile\nfopen", modefile
);
176 nulldir
.d_type
= DT_DIR
;
177 nulldir
.d_namlen
= 1;
178 strcpy(nulldir
.d_name
, "/");
179 nulldir
.d_reclen
= DIRSIZ(0, &nulldir
);
181 curfile
.name
= "<directory file - name unknown>";
182 curfile
.action
= USING
;
184 if (ip
== NULL
|| (ip
->di_mode
& IFMT
) != IFDIR
) {
186 dirp
= opendirfile(dirfile
);
188 fprintf(stderr
, "opendirfile: %s\n",
194 panic("Root directory is not on tape\n");
197 itp
= allocinotab(curfile
.ino
, ip
, seekpt
);
198 getfile(putdir
, xtrnull
);
201 itp
->t_size
= seekpt
- itp
->t_seekpt
;
206 * skip over all the directories on the tape
212 while (curfile
.dip
&& (curfile
.dip
->di_mode
& IFMT
) == IFDIR
) {
218 * Recursively find names and inumbers of all files in subtree
219 * pname and pass them off to be processed.
222 treescan(char *pname
, ufs1_ino_t ino
, long (*todo
) (char *, ufs1_ino_t
, int))
228 char locname
[MAXPATHLEN
+ 1];
230 itp
= inotablookup(ino
);
233 * Pname is name of a simple file or an unchanged directory.
235 (*todo
)(pname
, ino
, LEAF
);
239 * Pname is a dumped directory name.
241 if ((*todo
)(pname
, ino
, NODE
) == FAIL
)
244 * begin search through the directory
245 * skipping over "." and ".."
247 strncpy(locname
, pname
, sizeof(locname
) - 1);
248 locname
[sizeof(locname
) - 1] = '\0';
249 strncat(locname
, "/", sizeof(locname
) - strlen(locname
));
250 namelen
= strlen(locname
);
251 rst_seekdir(dirp
, itp
->t_seekpt
, itp
->t_seekpt
);
252 dp
= rst_readdir(dirp
); /* "." */
253 if (dp
!= NULL
&& strcmp(dp
->d_name
, ".") == 0)
254 dp
= rst_readdir(dirp
); /* ".." */
256 fprintf(stderr
, "Warning: `.' missing from directory %s\n",
258 if (dp
!= NULL
&& strcmp(dp
->d_name
, "..") == 0)
259 dp
= rst_readdir(dirp
); /* first real entry */
261 fprintf(stderr
, "Warning: `..' missing from directory %s\n",
263 bpt
= rst_telldir(dirp
);
265 * a zero inode signals end of directory
268 locname
[namelen
] = '\0';
269 if (namelen
+ dp
->d_namlen
>= sizeof(locname
)) {
270 fprintf(stderr
, "%s%s: name exceeds %d char\n",
271 locname
, dp
->d_name
, sizeof(locname
) - 1);
273 strncat(locname
, dp
->d_name
, (int)dp
->d_namlen
);
274 treescan(locname
, dp
->d_ino
, todo
);
275 rst_seekdir(dirp
, bpt
, itp
->t_seekpt
);
277 dp
= rst_readdir(dirp
);
278 bpt
= rst_telldir(dirp
);
283 * Lookup a pathname which is always assumed to start from the ROOTINO.
286 pathsearch(const char *pathname
)
290 char *path
, *name
, buffer
[MAXPATHLEN
];
292 strcpy(buffer
, pathname
);
298 while ((name
= strsep(&path
, "/")) != NULL
&& *name
!= '\0') {
299 if ((dp
= searchdir(ino
, name
)) == NULL
)
307 * Lookup the requested name in directory inum.
308 * Return its inode number if found, zero if it does not exist.
310 static struct direct
*
311 searchdir(ufs1_ino_t inum
, char *name
)
317 itp
= inotablookup(inum
);
320 rst_seekdir(dirp
, itp
->t_seekpt
, itp
->t_seekpt
);
323 dp
= rst_readdir(dirp
);
326 } while (dp
->d_namlen
!= len
|| strncmp(dp
->d_name
, name
, len
) != 0);
331 * Put the directory entries in the directory file
334 putdir(char *buf
, long size
)
336 struct direct cvtbuf
;
338 struct odirect
*eodp
;
343 eodp
= (struct odirect
*)&buf
[size
];
344 for (odp
= (struct odirect
*)buf
; odp
< eodp
; odp
++)
345 if (odp
->d_ino
!= 0) {
350 for (loc
= 0; loc
< size
; ) {
351 dp
= (struct direct
*)(buf
+ loc
);
353 swabst((u_char
*)"ls", (u_char
*) dp
);
354 if (oldinofmt
&& dp
->d_ino
!= 0) {
355 # if BYTE_ORDER == BIG_ENDIAN
357 dp
->d_namlen
= dp
->d_type
;
360 dp
->d_namlen
= dp
->d_type
;
362 dp
->d_type
= DT_UNKNOWN
;
364 i
= DIRBLKSIZ
- (loc
& (DIRBLKSIZ
- 1));
365 if ((dp
->d_reclen
& 0x3) != 0 ||
367 dp
->d_reclen
< DIRSIZ(0, dp
) ||
368 dp
->d_namlen
> NAME_MAX
) {
369 vprintf(stdout
, "Mangled directory: ");
370 if ((dp
->d_reclen
& 0x3) != 0)
372 "reclen not multiple of 4 ");
373 if (dp
->d_reclen
< DIRSIZ(0, dp
))
375 "reclen less than DIRSIZ (%d < %d) ",
376 dp
->d_reclen
, DIRSIZ(0, dp
));
377 if (dp
->d_namlen
> NAME_MAX
)
379 "reclen name too big (%d > %d) ",
380 dp
->d_namlen
, NAME_MAX
);
381 vprintf(stdout
, "\n");
386 if (dp
->d_ino
!= 0) {
394 * These variables are "local" to the following two functions.
396 char dirbuf
[DIRBLKSIZ
];
401 * add a new directory entry to a file.
404 putent(struct direct
*dp
)
406 dp
->d_reclen
= DIRSIZ(0, dp
);
407 if (dirloc
+ dp
->d_reclen
> DIRBLKSIZ
) {
408 ((struct direct
*)(dirbuf
+ prev
))->d_reclen
=
410 fwrite(dirbuf
, 1, DIRBLKSIZ
, df
);
413 memmove(dirbuf
+ dirloc
, dp
, (long)dp
->d_reclen
);
415 dirloc
+= dp
->d_reclen
;
419 * flush out a directory that is finished.
424 ((struct direct
*)(dirbuf
+ prev
))->d_reclen
= DIRBLKSIZ
- prev
;
425 fwrite(dirbuf
, (int)dirloc
, 1, df
);
431 dcvt(struct odirect
*odp
, struct direct
*ndp
)
434 memset(ndp
, 0, (long)(sizeof *ndp
));
435 ndp
->d_ino
= odp
->d_ino
;
436 ndp
->d_type
= DT_UNKNOWN
;
437 strncpy(ndp
->d_name
, odp
->d_name
, ODIRSIZ
);
438 ndp
->d_namlen
= strlen(ndp
->d_name
);
439 ndp
->d_reclen
= DIRSIZ(0, ndp
);
443 * Seek to an entry in a directory.
444 * Only values returned by rst_telldir should be passed to rst_seekdir.
445 * This routine handles many directories in a single file.
446 * It takes the base of the directory in the file, plus
447 * the desired seek offset into it.
450 rst_seekdir(RST_DIR
*dirp
, long loc
, long base
)
453 if (loc
== rst_telldir(dirp
))
457 fprintf(stderr
, "bad seek pointer to rst_seekdir %ld\n", loc
);
458 lseek(dirp
->dd_fd
, base
+ (loc
& ~(DIRBLKSIZ
- 1)), SEEK_SET
);
459 dirp
->dd_loc
= loc
& (DIRBLKSIZ
- 1);
460 if (dirp
->dd_loc
!= 0)
461 dirp
->dd_size
= read(dirp
->dd_fd
, dirp
->dd_buf
, DIRBLKSIZ
);
465 * get next entry in a directory.
468 rst_readdir(RST_DIR
*dirp
)
473 if (dirp
->dd_loc
== 0) {
474 dirp
->dd_size
= read(dirp
->dd_fd
, dirp
->dd_buf
,
476 if (dirp
->dd_size
<= 0) {
477 dprintf(stderr
, "error reading directory\n");
481 if (dirp
->dd_loc
>= dirp
->dd_size
) {
485 dp
= (struct direct
*)(dirp
->dd_buf
+ dirp
->dd_loc
);
486 if (dp
->d_reclen
== 0 ||
487 dp
->d_reclen
> DIRBLKSIZ
+ 1 - dirp
->dd_loc
) {
488 dprintf(stderr
, "corrupted directory: bad reclen %d\n",
492 dirp
->dd_loc
+= dp
->d_reclen
;
493 if (dp
->d_ino
== 0 && strcmp(dp
->d_name
, "/") == 0)
495 if (dp
->d_ino
>= maxino
) {
496 dprintf(stderr
, "corrupted directory: bad inum %d\n",
505 * Simulate the opening of a directory
508 rst_opendir(const char *name
)
514 if ((ino
= dirlookup(name
)) > 0 &&
515 (itp
= inotablookup(ino
)) != NULL
) {
516 dirp
= opendirfile(dirfile
);
517 rst_seekdir(dirp
, itp
->t_seekpt
, itp
->t_seekpt
);
524 * In our case, there is nothing to do when closing a directory.
527 rst_closedir(RST_DIR
*dirp
)
536 * Simulate finding the current offset in the directory.
539 rst_telldir(RST_DIR
*dirp
)
541 return ((long)lseek(dirp
->dd_fd
,
542 (off_t
)0, SEEK_CUR
) - dirp
->dd_size
+ dirp
->dd_loc
);
546 * Open a directory file.
549 opendirfile(const char *name
)
554 if ((fd
= open(name
, O_RDONLY
)) == -1)
556 if ((dirp
= malloc(sizeof(RST_DIR
))) == NULL
) {
566 * Set the mode, owner, and times for all new or changed directories
569 setdirmodes(int flags
)
572 struct modeinfo node
;
577 vprintf(stdout
, "Set directory mode, owner, and times.\n");
578 if ((tmpdir
= getenv("TMPDIR")) == NULL
|| tmpdir
[0] == '\0')
580 if (command
== 'r' || command
== 'R')
581 sprintf(modefile
, "%s/rstmode%ld", tmpdir
, (long)dumpdate
);
582 if (modefile
[0] == '#') {
583 panic("modefile not defined\n");
584 fprintf(stderr
, "directory mode, owner, and times not set\n");
587 mf
= fopen(modefile
, "r");
589 fprintf(stderr
, "fopen: %s\n", strerror(errno
));
590 fprintf(stderr
, "cannot open mode file %s\n", modefile
);
591 fprintf(stderr
, "directory mode, owner, and times not set\n");
596 fread((char *)&node
, 1, sizeof(struct modeinfo
), mf
);
599 ep
= lookupino(node
.ino
);
600 if (command
== 'i' || command
== 'x') {
603 if ((flags
& FORCE
) == 0 && ep
->e_flags
& EXISTED
) {
607 if (node
.ino
== ROOTINO
&&
608 reply("set owner/mode for '.'") == FAIL
)
612 panic("cannot find directory inode %d\n", node
.ino
);
616 chown(cp
, node
.uid
, node
.gid
);
617 chmod(cp
, node
.mode
);
618 utimes(cp
, node
.timep
);
619 chflags(cp
, node
.flags
);
625 panic("error setting directory modes\n");
630 * Generate a literal copy of a directory.
633 genliteraldir(char *name
, ufs1_ino_t ino
)
636 int ofile
, dp
, i
, size
;
639 itp
= inotablookup(ino
);
641 panic("Cannot find directory inode %d named %s\n", ino
, name
);
642 if ((ofile
= open(name
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666)) < 0) {
643 fprintf(stderr
, "%s: ", name
);
645 fprintf(stderr
, "cannot create file: %s\n", strerror(errno
));
648 rst_seekdir(dirp
, itp
->t_seekpt
, itp
->t_seekpt
);
649 dp
= dup(dirp
->dd_fd
);
650 for (i
= itp
->t_size
; i
> 0; i
-= BUFSIZ
) {
651 size
= i
< BUFSIZ
? i
: BUFSIZ
;
652 if (read(dp
, buf
, (int) size
) == -1) {
654 "write error extracting inode %d, name %s\n",
655 curfile
.ino
, curfile
.name
);
656 fprintf(stderr
, "read: %s\n", strerror(errno
));
659 if (!Nflag
&& write(ofile
, buf
, (int) size
) == -1) {
661 "write error extracting inode %d, name %s\n",
662 curfile
.ino
, curfile
.name
);
663 fprintf(stderr
, "write: %s\n", strerror(errno
));
673 * Determine the type of an inode
676 inodetype(ufs1_ino_t ino
)
680 itp
= inotablookup(ino
);
687 * Allocate and initialize a directory inode entry.
688 * If requested, save its pertinent mode, owner, and time info.
690 static struct inotab
*
691 allocinotab(ufs1_ino_t ino
, struct ufs1_dinode
*dip
, long seekpt
)
694 struct modeinfo node
;
696 itp
= calloc(1, sizeof(struct inotab
));
698 panic("no memory directory table\n");
699 itp
->t_next
= inotab
[INOHASH(ino
)];
700 inotab
[INOHASH(ino
)] = itp
;
702 itp
->t_seekpt
= seekpt
;
706 node
.timep
[0].tv_sec
= dip
->di_atime
;
707 node
.timep
[0].tv_usec
= dip
->di_atimensec
/ 1000;
708 node
.timep
[1].tv_sec
= dip
->di_mtime
;
709 node
.timep
[1].tv_usec
= dip
->di_mtimensec
/ 1000;
710 node
.mode
= dip
->di_mode
;
711 node
.flags
= dip
->di_flags
;
712 node
.uid
= dip
->di_uid
;
713 node
.gid
= dip
->di_gid
;
714 fwrite((char *)&node
, 1, sizeof(struct modeinfo
), mf
);
719 * Look up an inode in the table of directories
721 static struct inotab
*
722 inotablookup(ufs1_ino_t ino
)
726 for (itp
= inotab
[INOHASH(ino
)]; itp
!= NULL
; itp
= itp
->t_next
)
727 if (itp
->t_ino
== ino
)
740 if (modefile
[0] != '#')
742 if (dirfile
[0] != '#')