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 $
38 #include <sys/param.h>
42 #include <vfs/ufs/dinode.h>
43 #include <vfs/ufs/dir.h>
44 #include <protocols/dumprestore.h>
59 * Symbol table of directories read from tape.
62 #define INOHASH(val) (val % HASHSIZE)
64 struct inotab
*t_next
;
69 static struct inotab
*inotab
[HASHSIZE
];
72 * Information retained about directories.
76 struct timeval timep
[2];
84 * Definitions for library routines operating on directories.
87 #define DIRBLKSIZ 1024
92 char dd_buf
[DIRBLKSIZ
];
96 * Global variables for this file.
99 static FILE *df
, *mfg
;
100 static RST_DIR
*dirpg
;
101 static char dirfile
[MAXPATHLEN
] = "#"; /* No file */
102 static char modefile
[MAXPATHLEN
] = "#"; /* No file */
103 static char dot
[2] = "."; /* So it can be modified */
106 * Format of old style directories.
111 char d_name
[ODIRSIZ
];
114 static struct inotab
*allocinotab(ufs1_ino_t
, struct ufs1_dinode
*, long);
115 static void dcvt(struct odirect
*, struct direct
*);
116 static void flushent(void);
117 static struct inotab
*inotablookup(ufs1_ino_t
);
118 static RST_DIR
*opendirfile(const char *);
119 static void putdir(char *, size_t);
120 static void putent(struct direct
*);
121 static void rst_seekdir(RST_DIR
*, long, long);
122 static long rst_telldir(RST_DIR
*);
123 static struct direct
*searchdir(ufs1_ino_t
, char *);
126 * Extract directory contents, building up a directory structure
127 * on disk for extraction by name.
128 * If genmode is requested, save mode, owner, and times for all
129 * directories on the tape.
132 extractdirs(int genmode
)
135 struct ufs1_dinode
*ip
;
137 struct direct nulldir
;
141 vprintf(stdout
, "Extract directories from tape\n");
142 if ((tmpdir
= getenv("TMPDIR")) == NULL
|| tmpdir
[0] == '\0')
144 sprintf(dirfile
, "%s/rstdir%ld", tmpdir
, (long)dumpdate
);
145 if (command
!= 'r' && command
!= 'R') {
146 strcat(dirfile
, "-XXXXXX");
147 fd
= mkstemp(dirfile
);
149 fd
= open(dirfile
, O_RDWR
|O_CREAT
|O_EXCL
, 0666);
150 if (fd
== -1 || (df
= fdopen(fd
, "w")) == NULL
) {
153 warn("%s - cannot create directory temporary\nfopen", dirfile
);
157 sprintf(modefile
, "%s/rstmode%ld", tmpdir
, (long)dumpdate
);
158 if (command
!= 'r' && command
!= 'R') {
159 strcat(modefile
, "-XXXXXX");
160 fd
= mkstemp(modefile
);
162 fd
= open(modefile
, O_RDWR
|O_CREAT
|O_EXCL
, 0666);
163 if (fd
== -1 || (mfg
= fdopen(fd
, "w")) == NULL
) {
166 warn("%s - cannot create modefile\nfopen", modefile
);
171 nulldir
.d_type
= DT_DIR
;
172 nulldir
.d_namlen
= 1;
173 strcpy(nulldir
.d_name
, "/");
174 nulldir
.d_reclen
= DIRSIZ(0, &nulldir
);
176 curfile
.name
= "<directory file - name unknown>";
177 curfile
.action
= USING
;
179 if (ip
== NULL
|| (ip
->di_mode
& IFMT
) != IFDIR
) {
181 dirpg
= opendirfile(dirfile
);
183 fprintf(stderr
, "opendirfile: %s\n",
189 panic("Root directory is not on tape\n");
192 itp
= allocinotab(curfile
.ino
, ip
, seekptg
);
193 getfile(putdir
, xtrnull
);
196 itp
->t_size
= seekptg
- itp
->t_seekpt
;
201 * skip over all the directories on the tape
207 while (curfile
.dip
&& (curfile
.dip
->di_mode
& IFMT
) == IFDIR
) {
213 * Recursively find names and inumbers of all files in subtree
214 * pname and pass them off to be processed.
217 treescan(char *pname
, ufs1_ino_t ino
, long (*todo
)(char *, ufs1_ino_t
, int))
223 char locname
[MAXPATHLEN
+ 1];
225 itp
= inotablookup(ino
);
228 * Pname is name of a simple file or an unchanged directory.
230 (*todo
)(pname
, ino
, LEAF
);
234 * Pname is a dumped directory name.
236 if ((*todo
)(pname
, ino
, NODE
) == FAIL
)
239 * begin search through the directory
240 * skipping over "." and ".."
242 strncpy(locname
, pname
, sizeof(locname
) - 1);
243 locname
[sizeof(locname
) - 1] = '\0';
244 strncat(locname
, "/", sizeof(locname
) - strlen(locname
));
245 namelen
= strlen(locname
);
246 rst_seekdir(dirpg
, itp
->t_seekpt
, itp
->t_seekpt
);
247 dp
= rst_readdir(dirpg
); /* "." */
248 if (dp
!= NULL
&& strcmp(dp
->d_name
, ".") == 0)
249 dp
= rst_readdir(dirpg
); /* ".." */
251 fprintf(stderr
, "Warning: `.' missing from directory %s\n",
253 if (dp
!= NULL
&& strcmp(dp
->d_name
, "..") == 0)
254 dp
= rst_readdir(dirpg
); /* first real entry */
256 fprintf(stderr
, "Warning: `..' missing from directory %s\n",
258 bpt
= rst_telldir(dirpg
);
260 * a zero inode signals end of directory
263 locname
[namelen
] = '\0';
264 if ((size_t)(namelen
+ dp
->d_namlen
) >= sizeof(locname
)) {
265 fprintf(stderr
, "%s%s: name exceeds %zu char\n",
266 locname
, dp
->d_name
, sizeof(locname
) - 1);
268 strncat(locname
, dp
->d_name
, (int)dp
->d_namlen
);
269 treescan(locname
, dp
->d_ino
, todo
);
270 rst_seekdir(dirpg
, bpt
, itp
->t_seekpt
);
272 dp
= rst_readdir(dirpg
);
273 bpt
= rst_telldir(dirpg
);
278 * Lookup a pathname which is always assumed to start from the UFS_ROOTINO.
281 pathsearch(const char *pathname
)
285 char *path
, *name
, buffer
[MAXPATHLEN
];
287 strcpy(buffer
, pathname
);
293 while ((name
= strsep(&path
, "/")) != NULL
&& *name
!= '\0') {
294 if ((dp
= searchdir(ino
, name
)) == NULL
)
302 * Lookup the requested name in directory inum.
303 * Return its inode number if found, zero if it does not exist.
305 static struct direct
*
306 searchdir(ufs1_ino_t inum
, char *name
)
312 itp
= inotablookup(inum
);
315 rst_seekdir(dirpg
, itp
->t_seekpt
, itp
->t_seekpt
);
318 dp
= rst_readdir(dirpg
);
321 } while (dp
->d_namlen
!= len
|| strncmp(dp
->d_name
, name
, len
) != 0);
326 * Put the directory entries in the directory file
329 putdir(char *buf
, size_t size
)
331 struct direct cvtbuf
;
333 struct odirect
*eodp
;
338 eodp
= (struct odirect
*)&buf
[size
];
339 for (odp
= (struct odirect
*)buf
; odp
< eodp
; odp
++)
340 if (odp
->d_ino
!= 0) {
345 for (loc
= 0; loc
< size
; ) {
346 dp
= (struct direct
*)(buf
+ loc
);
348 swabst("ls", (u_char
*) dp
);
349 if (oldinofmt
&& dp
->d_ino
!= 0) {
350 # if BYTE_ORDER == BIG_ENDIAN
352 dp
->d_namlen
= dp
->d_type
;
355 dp
->d_namlen
= dp
->d_type
;
357 dp
->d_type
= DT_UNKNOWN
;
359 i
= DIRBLKSIZ
- (loc
& (DIRBLKSIZ
- 1));
360 if ((dp
->d_reclen
& 0x3) != 0 ||
362 dp
->d_reclen
< DIRSIZ(0, dp
)
364 || dp
->d_namlen
> NAME_MAX
367 vprintf(stdout
, "Mangled directory: ");
368 if ((dp
->d_reclen
& 0x3) != 0)
370 "reclen not multiple of 4 ");
371 if (dp
->d_reclen
< DIRSIZ(0, dp
))
373 "reclen less than DIRSIZ (%u < %zu) ",
374 dp
->d_reclen
, DIRSIZ(0, dp
));
376 if (dp
->d_namlen
> NAME_MAX
)
378 "reclen name too big (%u > %u) ",
379 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
*dirpl
, long loc
, long base
)
453 if (loc
== rst_telldir(dirpl
))
457 fprintf(stderr
, "bad seek pointer to rst_seekdir %ld\n", loc
);
458 lseek(dirpl
->dd_fd
, base
+ rounddown2(loc
, DIRBLKSIZ
), SEEK_SET
);
459 dirpl
->dd_loc
= loc
& (DIRBLKSIZ
- 1);
460 if (dirpl
->dd_loc
!= 0)
461 dirpl
->dd_size
= read(dirpl
->dd_fd
, dirpl
->dd_buf
, DIRBLKSIZ
);
465 * get next entry in a directory.
468 rst_readdir(RST_DIR
*dirpl
)
473 if (dirpl
->dd_loc
== 0) {
474 dirpl
->dd_size
= read(dirpl
->dd_fd
, dirpl
->dd_buf
,
476 if (dirpl
->dd_size
<= 0) {
477 dprintf(stderr
, "error reading directory\n");
481 if (dirpl
->dd_loc
>= dirpl
->dd_size
) {
485 dp
= (struct direct
*)(dirpl
->dd_buf
+ dirpl
->dd_loc
);
486 if (dp
->d_reclen
== 0 ||
487 dp
->d_reclen
> DIRBLKSIZ
+ 1 - dirpl
->dd_loc
) {
488 dprintf(stderr
, "corrupted directory: bad reclen %d\n",
492 dirpl
->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 dirpl
= opendirfile(dirfile
);
517 rst_seekdir(dirpl
, itp
->t_seekpt
, itp
->t_seekpt
);
524 * In our case, there is nothing to do when closing a directory.
527 rst_closedir(RST_DIR
*dirpl
)
536 * Simulate finding the current offset in the directory.
539 rst_telldir(RST_DIR
*dirpl
)
541 return ((long)lseek(dirpl
->dd_fd
,
542 (off_t
)0, SEEK_CUR
) - dirpl
->dd_size
+ dirpl
->dd_loc
);
546 * Open a directory file.
549 opendirfile(const char *name
)
554 if ((fd
= open(name
, O_RDONLY
)) == -1)
556 if ((dirpl
= 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 mfl
= 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
), mfl
);
599 ep
= lookupino(node
.ino
);
600 if (command
== 'i' || command
== 'x') {
603 if ((flags
& FORCE
) == 0 && ep
->e_flags
& EXISTED
) {
607 if (node
.ino
== UFS_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(dirpg
, itp
->t_seekpt
, itp
->t_seekpt
);
649 dp
= dup(dirpg
->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 seekptl
)
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
= seekptl
;
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
), mfg
);
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] != '#')