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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)dirs.c 8.7 (Berkeley) 5/1/95
35 * $FreeBSD: src/sbin/restore/dirs.c,v 1.14.2.5 2001/10/15 13:44:45 dd Exp $
36 * $DragonFly: src/sbin/restore/dirs.c,v 1.10 2006/04/03 01:58:49 dillon Exp $
39 #include <sys/param.h>
44 #include <vfs/ufs/dinode.h>
45 #include <vfs/ufs/dir.h>
46 #include <protocols/dumprestore.h>
60 * Symbol table of directories read from tape.
63 #define INOHASH(val) (val % HASHSIZE)
65 struct inotab
*t_next
;
70 static struct inotab
*inotab
[HASHSIZE
];
73 * Information retained about directories.
77 struct timeval timep
[2];
85 * Definitions for library routines operating on directories.
88 #define DIRBLKSIZ 1024
93 char dd_buf
[DIRBLKSIZ
];
97 * Global variables for this file.
100 static FILE *df
, *mf
;
101 static RST_DIR
*dirp
;
102 static char dirfile
[MAXPATHLEN
] = "#"; /* No file */
103 static char modefile
[MAXPATHLEN
] = "#"; /* No file */
104 static char dot
[2] = "."; /* So it can be modified */
107 * Format of old style directories.
112 char d_name
[ODIRSIZ
];
115 static struct inotab
*allocinotab(ufs1_ino_t
, struct ufs1_dinode
*, long);
116 static void dcvt(struct odirect
*, struct direct
*);
117 static void flushent(void);
118 static struct inotab
*inotablookup(ufs1_ino_t
);
119 static RST_DIR
*opendirfile(const char *);
120 static void putdir(char *, long);
121 static void putent(struct direct
*);
122 static void rst_seekdir(RST_DIR
*, long, long);
123 static long rst_telldir(RST_DIR
*);
124 static struct direct
*searchdir(ufs1_ino_t
, char *);
127 * Extract directory contents, building up a directory structure
128 * on disk for extraction by name.
129 * If genmode is requested, save mode, owner, and times for all
130 * directories on the tape.
133 extractdirs(int genmode
)
136 struct ufs1_dinode
*ip
;
138 struct direct nulldir
;
142 vprintf(stdout
, "Extract directories from tape\n");
143 if ((tmpdir
= getenv("TMPDIR")) == NULL
|| tmpdir
[0] == '\0')
145 sprintf(dirfile
, "%s/rstdir%ld", tmpdir
, (long)dumpdate
);
146 if (command
!= 'r' && command
!= 'R') {
147 (void *) strcat(dirfile
, "-XXXXXX");
148 fd
= mkstemp(dirfile
);
150 fd
= open(dirfile
, O_RDWR
|O_CREAT
|O_EXCL
, 0666);
151 if (fd
== -1 || (df
= fdopen(fd
, "w")) == NULL
) {
154 warn("%s - cannot create directory temporary\nfopen", dirfile
);
158 sprintf(modefile
, "%s/rstmode%ld", tmpdir
, (long)dumpdate
);
159 if (command
!= 'r' && command
!= 'R') {
160 (void *) strcat(modefile
, "-XXXXXX");
161 fd
= mkstemp(modefile
);
163 fd
= open(modefile
, O_RDWR
|O_CREAT
|O_EXCL
, 0666);
164 if (fd
== -1 || (mf
= fdopen(fd
, "w")) == NULL
) {
167 warn("%s - cannot create modefile\nfopen", modefile
);
172 nulldir
.d_type
= DT_DIR
;
173 nulldir
.d_namlen
= 1;
174 strcpy(nulldir
.d_name
, "/");
175 nulldir
.d_reclen
= DIRSIZ(0, &nulldir
);
177 curfile
.name
= "<directory file - name unknown>";
178 curfile
.action
= USING
;
180 if (ip
== NULL
|| (ip
->di_mode
& IFMT
) != IFDIR
) {
182 dirp
= opendirfile(dirfile
);
184 fprintf(stderr
, "opendirfile: %s\n",
190 panic("Root directory is not on tape\n");
193 itp
= allocinotab(curfile
.ino
, ip
, seekpt
);
194 getfile(putdir
, xtrnull
);
197 itp
->t_size
= seekpt
- itp
->t_seekpt
;
202 * skip over all the directories on the tape
208 while (curfile
.dip
&& (curfile
.dip
->di_mode
& IFMT
) == IFDIR
) {
214 * Recursively find names and inumbers of all files in subtree
215 * pname and pass them off to be processed.
218 treescan(char *pname
, ufs1_ino_t ino
, long (*todo
) (char *, ufs1_ino_t
, int))
224 char locname
[MAXPATHLEN
+ 1];
226 itp
= inotablookup(ino
);
229 * Pname is name of a simple file or an unchanged directory.
231 (*todo
)(pname
, ino
, LEAF
);
235 * Pname is a dumped directory name.
237 if ((*todo
)(pname
, ino
, NODE
) == FAIL
)
240 * begin search through the directory
241 * skipping over "." and ".."
243 strncpy(locname
, pname
, sizeof(locname
) - 1);
244 locname
[sizeof(locname
) - 1] = '\0';
245 strncat(locname
, "/", sizeof(locname
) - strlen(locname
));
246 namelen
= strlen(locname
);
247 rst_seekdir(dirp
, itp
->t_seekpt
, itp
->t_seekpt
);
248 dp
= rst_readdir(dirp
); /* "." */
249 if (dp
!= NULL
&& strcmp(dp
->d_name
, ".") == 0)
250 dp
= rst_readdir(dirp
); /* ".." */
252 fprintf(stderr
, "Warning: `.' missing from directory %s\n",
254 if (dp
!= NULL
&& strcmp(dp
->d_name
, "..") == 0)
255 dp
= rst_readdir(dirp
); /* first real entry */
257 fprintf(stderr
, "Warning: `..' missing from directory %s\n",
259 bpt
= rst_telldir(dirp
);
261 * a zero inode signals end of directory
264 locname
[namelen
] = '\0';
265 if (namelen
+ dp
->d_namlen
>= sizeof(locname
)) {
266 fprintf(stderr
, "%s%s: name exceeds %d char\n",
267 locname
, dp
->d_name
, sizeof(locname
) - 1);
269 strncat(locname
, dp
->d_name
, (int)dp
->d_namlen
);
270 treescan(locname
, dp
->d_ino
, todo
);
271 rst_seekdir(dirp
, bpt
, itp
->t_seekpt
);
273 dp
= rst_readdir(dirp
);
274 bpt
= rst_telldir(dirp
);
279 * Lookup a pathname which is always assumed to start from the ROOTINO.
282 pathsearch(const char *pathname
)
286 char *path
, *name
, buffer
[MAXPATHLEN
];
288 strcpy(buffer
, pathname
);
294 while ((name
= strsep(&path
, "/")) != NULL
&& *name
!= '\0') {
295 if ((dp
= searchdir(ino
, name
)) == NULL
)
303 * Lookup the requested name in directory inum.
304 * Return its inode number if found, zero if it does not exist.
306 static struct direct
*
307 searchdir(ufs1_ino_t inum
, char *name
)
313 itp
= inotablookup(inum
);
316 rst_seekdir(dirp
, itp
->t_seekpt
, itp
->t_seekpt
);
319 dp
= rst_readdir(dirp
);
322 } while (dp
->d_namlen
!= len
|| strncmp(dp
->d_name
, name
, len
) != 0);
327 * Put the directory entries in the directory file
330 putdir(char *buf
, long size
)
332 struct direct cvtbuf
;
334 struct odirect
*eodp
;
339 eodp
= (struct odirect
*)&buf
[size
];
340 for (odp
= (struct odirect
*)buf
; odp
< eodp
; odp
++)
341 if (odp
->d_ino
!= 0) {
346 for (loc
= 0; loc
< size
; ) {
347 dp
= (struct direct
*)(buf
+ loc
);
349 swabst((u_char
*)"ls", (u_char
*) dp
);
350 if (oldinofmt
&& dp
->d_ino
!= 0) {
351 # if BYTE_ORDER == BIG_ENDIAN
353 dp
->d_namlen
= dp
->d_type
;
356 dp
->d_namlen
= dp
->d_type
;
358 dp
->d_type
= DT_UNKNOWN
;
360 i
= DIRBLKSIZ
- (loc
& (DIRBLKSIZ
- 1));
361 if ((dp
->d_reclen
& 0x3) != 0 ||
363 dp
->d_reclen
< DIRSIZ(0, dp
) ||
364 dp
->d_namlen
> NAME_MAX
) {
365 vprintf(stdout
, "Mangled directory: ");
366 if ((dp
->d_reclen
& 0x3) != 0)
368 "reclen not multiple of 4 ");
369 if (dp
->d_reclen
< DIRSIZ(0, dp
))
371 "reclen less than DIRSIZ (%d < %d) ",
372 dp
->d_reclen
, DIRSIZ(0, dp
));
373 if (dp
->d_namlen
> NAME_MAX
)
375 "reclen name too big (%d > %d) ",
376 dp
->d_namlen
, NAME_MAX
);
377 vprintf(stdout
, "\n");
382 if (dp
->d_ino
!= 0) {
390 * These variables are "local" to the following two functions.
392 char dirbuf
[DIRBLKSIZ
];
397 * add a new directory entry to a file.
400 putent(struct direct
*dp
)
402 dp
->d_reclen
= DIRSIZ(0, dp
);
403 if (dirloc
+ dp
->d_reclen
> DIRBLKSIZ
) {
404 ((struct direct
*)(dirbuf
+ prev
))->d_reclen
=
406 fwrite(dirbuf
, 1, DIRBLKSIZ
, df
);
409 memmove(dirbuf
+ dirloc
, dp
, (long)dp
->d_reclen
);
411 dirloc
+= dp
->d_reclen
;
415 * flush out a directory that is finished.
420 ((struct direct
*)(dirbuf
+ prev
))->d_reclen
= DIRBLKSIZ
- prev
;
421 fwrite(dirbuf
, (int)dirloc
, 1, df
);
427 dcvt(struct odirect
*odp
, struct direct
*ndp
)
430 memset(ndp
, 0, (long)(sizeof *ndp
));
431 ndp
->d_ino
= odp
->d_ino
;
432 ndp
->d_type
= DT_UNKNOWN
;
433 strncpy(ndp
->d_name
, odp
->d_name
, ODIRSIZ
);
434 ndp
->d_namlen
= strlen(ndp
->d_name
);
435 ndp
->d_reclen
= DIRSIZ(0, ndp
);
439 * Seek to an entry in a directory.
440 * Only values returned by rst_telldir should be passed to rst_seekdir.
441 * This routine handles many directories in a single file.
442 * It takes the base of the directory in the file, plus
443 * the desired seek offset into it.
446 rst_seekdir(RST_DIR
*dirp
, long loc
, long base
)
449 if (loc
== rst_telldir(dirp
))
453 fprintf(stderr
, "bad seek pointer to rst_seekdir %ld\n", loc
);
454 lseek(dirp
->dd_fd
, base
+ (loc
& ~(DIRBLKSIZ
- 1)), SEEK_SET
);
455 dirp
->dd_loc
= loc
& (DIRBLKSIZ
- 1);
456 if (dirp
->dd_loc
!= 0)
457 dirp
->dd_size
= read(dirp
->dd_fd
, dirp
->dd_buf
, DIRBLKSIZ
);
461 * get next entry in a directory.
464 rst_readdir(RST_DIR
*dirp
)
469 if (dirp
->dd_loc
== 0) {
470 dirp
->dd_size
= read(dirp
->dd_fd
, dirp
->dd_buf
,
472 if (dirp
->dd_size
<= 0) {
473 dprintf(stderr
, "error reading directory\n");
477 if (dirp
->dd_loc
>= dirp
->dd_size
) {
481 dp
= (struct direct
*)(dirp
->dd_buf
+ dirp
->dd_loc
);
482 if (dp
->d_reclen
== 0 ||
483 dp
->d_reclen
> DIRBLKSIZ
+ 1 - dirp
->dd_loc
) {
484 dprintf(stderr
, "corrupted directory: bad reclen %d\n",
488 dirp
->dd_loc
+= dp
->d_reclen
;
489 if (dp
->d_ino
== 0 && strcmp(dp
->d_name
, "/") == 0)
491 if (dp
->d_ino
>= maxino
) {
492 dprintf(stderr
, "corrupted directory: bad inum %d\n",
501 * Simulate the opening of a directory
504 rst_opendir(const char *name
)
510 if ((ino
= dirlookup(name
)) > 0 &&
511 (itp
= inotablookup(ino
)) != NULL
) {
512 dirp
= opendirfile(dirfile
);
513 rst_seekdir(dirp
, itp
->t_seekpt
, itp
->t_seekpt
);
520 * In our case, there is nothing to do when closing a directory.
523 rst_closedir(RST_DIR
*dirp
)
532 * Simulate finding the current offset in the directory.
535 rst_telldir(RST_DIR
*dirp
)
537 return ((long)lseek(dirp
->dd_fd
,
538 (off_t
)0, SEEK_CUR
) - dirp
->dd_size
+ dirp
->dd_loc
);
542 * Open a directory file.
545 opendirfile(const char *name
)
550 if ((fd
= open(name
, O_RDONLY
)) == -1)
552 if ((dirp
= malloc(sizeof(RST_DIR
))) == NULL
) {
562 * Set the mode, owner, and times for all new or changed directories
565 setdirmodes(int flags
)
568 struct modeinfo node
;
573 vprintf(stdout
, "Set directory mode, owner, and times.\n");
574 if ((tmpdir
= getenv("TMPDIR")) == NULL
|| tmpdir
[0] == '\0')
576 if (command
== 'r' || command
== 'R')
577 sprintf(modefile
, "%s/rstmode%ld", tmpdir
, (long)dumpdate
);
578 if (modefile
[0] == '#') {
579 panic("modefile not defined\n");
580 fprintf(stderr
, "directory mode, owner, and times not set\n");
583 mf
= fopen(modefile
, "r");
585 fprintf(stderr
, "fopen: %s\n", strerror(errno
));
586 fprintf(stderr
, "cannot open mode file %s\n", modefile
);
587 fprintf(stderr
, "directory mode, owner, and times not set\n");
592 fread((char *)&node
, 1, sizeof(struct modeinfo
), mf
);
595 ep
= lookupino(node
.ino
);
596 if (command
== 'i' || command
== 'x') {
599 if ((flags
& FORCE
) == 0 && ep
->e_flags
& EXISTED
) {
603 if (node
.ino
== ROOTINO
&&
604 reply("set owner/mode for '.'") == FAIL
)
608 panic("cannot find directory inode %d\n", node
.ino
);
612 chown(cp
, node
.uid
, node
.gid
);
613 chmod(cp
, node
.mode
);
614 utimes(cp
, node
.timep
);
615 chflags(cp
, node
.flags
);
621 panic("error setting directory modes\n");
626 * Generate a literal copy of a directory.
629 genliteraldir(char *name
, ufs1_ino_t ino
)
632 int ofile
, dp
, i
, size
;
635 itp
= inotablookup(ino
);
637 panic("Cannot find directory inode %d named %s\n", ino
, name
);
638 if ((ofile
= open(name
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666)) < 0) {
639 fprintf(stderr
, "%s: ", name
);
641 fprintf(stderr
, "cannot create file: %s\n", strerror(errno
));
644 rst_seekdir(dirp
, itp
->t_seekpt
, itp
->t_seekpt
);
645 dp
= dup(dirp
->dd_fd
);
646 for (i
= itp
->t_size
; i
> 0; i
-= BUFSIZ
) {
647 size
= i
< BUFSIZ
? i
: BUFSIZ
;
648 if (read(dp
, buf
, (int) size
) == -1) {
650 "write error extracting inode %d, name %s\n",
651 curfile
.ino
, curfile
.name
);
652 fprintf(stderr
, "read: %s\n", strerror(errno
));
655 if (!Nflag
&& write(ofile
, buf
, (int) size
) == -1) {
657 "write error extracting inode %d, name %s\n",
658 curfile
.ino
, curfile
.name
);
659 fprintf(stderr
, "write: %s\n", strerror(errno
));
669 * Determine the type of an inode
672 inodetype(ufs1_ino_t ino
)
676 itp
= inotablookup(ino
);
683 * Allocate and initialize a directory inode entry.
684 * If requested, save its pertinent mode, owner, and time info.
686 static struct inotab
*
687 allocinotab(ufs1_ino_t ino
, struct ufs1_dinode
*dip
, long seekpt
)
690 struct modeinfo node
;
692 itp
= calloc(1, sizeof(struct inotab
));
694 panic("no memory directory table\n");
695 itp
->t_next
= inotab
[INOHASH(ino
)];
696 inotab
[INOHASH(ino
)] = itp
;
698 itp
->t_seekpt
= seekpt
;
702 node
.timep
[0].tv_sec
= dip
->di_atime
;
703 node
.timep
[0].tv_usec
= dip
->di_atimensec
/ 1000;
704 node
.timep
[1].tv_sec
= dip
->di_mtime
;
705 node
.timep
[1].tv_usec
= dip
->di_mtimensec
/ 1000;
706 node
.mode
= dip
->di_mode
;
707 node
.flags
= dip
->di_flags
;
708 node
.uid
= dip
->di_uid
;
709 node
.gid
= dip
->di_gid
;
710 fwrite((char *)&node
, 1, sizeof(struct modeinfo
), mf
);
715 * Look up an inode in the table of directories
717 static struct inotab
*
718 inotablookup(ufs1_ino_t ino
)
722 for (itp
= inotab
[INOHASH(ino
)]; itp
!= NULL
; itp
= itp
->t_next
)
723 if (itp
->t_ino
== ino
)
736 if (modefile
[0] != '#')
738 if (dirfile
[0] != '#')