1 /* $NetBSD: dirs.c,v 1.47 2009/02/18 13:14:12 yamt Exp $ */
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include <sys/cdefs.h>
40 static char sccsid
[] = "@(#)dirs.c 8.7 (Berkeley) 5/1/95";
42 __RCSID("$NetBSD: dirs.c,v 1.47 2009/02/18 13:14:12 yamt Exp $");
46 #include <sys/param.h>
51 #include <ufs/ufs/dinode.h>
52 #include <ufs/ufs/dir.h>
53 #include <ufs/ffs/fs.h>
54 #include <protocols/dumprestore.h>
64 #include <machine/endian.h>
70 * Symbol table of directories read from tape.
73 #define INOHASH(val) (val % HASHSIZE)
75 struct inotab
*t_next
;
80 static struct inotab
*inotab
[HASHSIZE
];
83 * Information retained about directories.
87 struct timeval ctimep
[2];
88 struct timeval mtimep
[2];
96 * Definitions for library routines operating on directories.
99 #define DIRBLKSIZ 1024
104 char dd_buf
[DIRBLKSIZ
];
108 * Global variables for this file.
112 static RST_DIR
*dirp
;
113 static char dirfile
[MAXPATHLEN
] = "#"; /* No file */
114 static char modefile
[MAXPATHLEN
] = "#"; /* No file */
115 static char dot
[2] = "."; /* So it can be modified */
118 * Format of old style directories.
123 char d_name
[ODIRSIZ
];
126 static struct inotab
*allocinotab(FILE *, struct context
*, long);
127 static void dcvt(struct odirect
*, struct direct
*);
128 static void flushent(void);
129 static struct inotab
*inotablookup(ino_t
);
130 static RST_DIR
*opendirfile(const char *);
131 static void putdir(char *, long);
132 static void putent(struct direct
*);
133 static void rst_seekdir(RST_DIR
*, long, long);
134 static long rst_telldir(RST_DIR
*);
135 static struct direct
*searchdir(ino_t
, char *);
138 * Extract directory contents, building up a directory structure
139 * on disk for extraction by name.
140 * If genmode is requested, save mode, owner, and times for all
141 * directories on the tape.
144 extractdirs(int genmode
)
149 struct direct nulldir
;
152 vprintf(stdout
, "Extract directories from tape\n");
153 (void) snprintf(dirfile
, sizeof(dirfile
), "%s/rstdir%d",
154 tmpdir
, (int)dumpdate
);
155 if (command
!= 'r' && command
!= 'R') {
156 (void) snprintf(dirfile
, sizeof(dirfile
), "%s/rstdir%d-XXXXXX",
157 tmpdir
, (int)dumpdate
);
158 if ((dfd
= mkstemp(dirfile
)) == -1)
159 err(1, "cannot mkstemp temporary file %s", dirfile
);
160 df
= fdopen(dfd
, "w");
163 df
= fopen(dirfile
, "w");
165 err(1, "cannot open temporary file %s", dirfile
);
168 (void) snprintf(modefile
, sizeof(modefile
), "%s/rstmode%d",
169 tmpdir
, (int)dumpdate
);
170 if (command
!= 'r' && command
!= 'R') {
171 (void) snprintf(modefile
, sizeof(modefile
),
172 "%s/rstmode%d-XXXXXX", tmpdir
, (int)dumpdate
);
173 if ((mfd
= mkstemp(modefile
)) == -1)
174 err(1, "cannot mkstemp temporary file %s",
176 mf
= fdopen(mfd
, "w");
179 mf
= fopen(modefile
, "w");
181 err(1, "cannot open temporary file %s", modefile
);
184 nulldir
.d_type
= DT_DIR
;
185 nulldir
.d_namlen
= 1;
186 (void) strcpy(nulldir
.d_name
, "/");
187 nulldir
.d_reclen
= DIRSIZ(0, &nulldir
, 0);
189 curfile
.name
= "<directory file - name unknown>";
190 curfile
.action
= USING
;
191 if (curfile
.mode
== 0 || (curfile
.mode
& IFMT
) != IFDIR
) {
193 dirp
= opendirfile(dirfile
);
195 fprintf(stderr
, "opendirfile: %s\n",
201 panic("Root directory is not on tape\n");
204 itp
= allocinotab(mf
, &curfile
, seekpt
);
205 getfile(putdir
, xtrnull
);
208 itp
->t_size
= seekpt
- itp
->t_seekpt
;
213 * skip over all the directories on the tape
219 while (curfile
.ino
&& (curfile
.mode
& IFMT
) == IFDIR
) {
225 * Recursively find names and inumbers of all files in subtree
226 * pname and pass them off to be processed.
229 treescan(const char *pname
, ino_t ino
, long (*todo
)(const char *, ino_t
, int))
235 char locname
[MAXPATHLEN
+ 1];
237 itp
= inotablookup(ino
);
240 * Pname is name of a simple file or an unchanged directory.
242 (void) (*todo
)(pname
, ino
, LEAF
);
246 * Pname is a dumped directory name.
248 if ((*todo
)(pname
, ino
, NODE
) == FAIL
)
251 * begin search through the directory
252 * skipping over "." and ".."
254 (void) snprintf(locname
, sizeof(locname
), "%s/", pname
);
255 namelen
= strlen(locname
);
256 rst_seekdir(dirp
, itp
->t_seekpt
, itp
->t_seekpt
);
257 dp
= rst_readdir(dirp
); /* "." */
258 if (dp
!= NULL
&& strcmp(dp
->d_name
, ".") == 0)
259 dp
= rst_readdir(dirp
); /* ".." */
261 fprintf(stderr
, "Warning: `.' missing from directory %s\n",
263 if (dp
!= NULL
&& strcmp(dp
->d_name
, "..") == 0)
264 dp
= rst_readdir(dirp
); /* first real entry */
266 fprintf(stderr
, "Warning: `..' missing from directory %s\n",
268 bpt
= rst_telldir(dirp
);
270 * a zero inode signals end of directory
273 locname
[namelen
] = '\0';
274 if (namelen
+ dp
->d_namlen
>= sizeof(locname
)) {
275 fprintf(stderr
, "%s%s: name exceeds %lu char\n",
276 locname
, dp
->d_name
, (u_long
)(sizeof(locname
) - 1));
278 (void) strncat(locname
, dp
->d_name
, (int)dp
->d_namlen
);
279 locname
[namelen
+ dp
->d_namlen
] = '\0';
280 treescan(locname
, dp
->d_ino
, todo
);
281 rst_seekdir(dirp
, bpt
, itp
->t_seekpt
);
283 dp
= rst_readdir(dirp
);
284 bpt
= rst_telldir(dirp
);
289 * Lookup a pathname which is always assumed to start from the ROOTINO.
292 pathsearch(const char *pathname
)
296 char *path
, *name
, buffer
[MAXPATHLEN
];
298 strcpy(buffer
, pathname
);
304 while ((name
= strsep(&path
, "/")) != NULL
&& *name
!= '\0') {
305 if ((dp
= searchdir(ino
, name
)) == NULL
)
313 * Lookup the requested name in directory inum.
314 * Return its inode number if found, zero if it does not exist.
316 static struct direct
*
317 searchdir(ino_t inum
, char *name
)
323 itp
= inotablookup(inum
);
326 rst_seekdir(dirp
, itp
->t_seekpt
, itp
->t_seekpt
);
329 dp
= rst_readdir(dirp
);
332 } while (dp
->d_namlen
!= len
|| strncmp(dp
->d_name
, name
, len
) != 0);
337 * Put the directory entries in the directory file
340 putdir(char *buf
, long size
)
342 struct direct cvtbuf
;
344 struct odirect
*eodp
;
349 eodp
= (struct odirect
*)&buf
[size
];
350 for (odp
= (struct odirect
*)buf
; odp
< eodp
; odp
++)
351 if (odp
->d_ino
!= 0) {
356 for (loc
= 0; loc
< size
; ) {
357 dp
= (struct direct
*)(buf
+ loc
);
359 dp
->d_ino
= bswap32(dp
->d_ino
);
360 dp
->d_reclen
= bswap16(dp
->d_reclen
);
362 if (oldinofmt
&& dp
->d_ino
!= 0) {
363 # if BYTE_ORDER == BIG_ENDIAN
365 dp
->d_namlen
= dp
->d_type
;
368 dp
->d_namlen
= dp
->d_type
;
370 dp
->d_type
= DT_UNKNOWN
;
372 i
= DIRBLKSIZ
- (loc
& (DIRBLKSIZ
- 1));
373 if ((dp
->d_reclen
& 0x3) != 0 ||
375 dp
->d_reclen
< DIRSIZ(0, dp
, 0) /* ||
376 dp->d_namlen > NAME_MAX */) {
377 vprintf(stdout
, "Mangled directory: ");
378 if ((dp
->d_reclen
& 0x3) != 0)
380 "reclen not multiple of 4 ");
381 if (dp
->d_reclen
< DIRSIZ(0, dp
, 0))
383 "reclen less than DIRSIZ (%d < %lu) ",
384 dp
->d_reclen
, (u_long
)DIRSIZ(0, dp
, 0));
385 #if 0 /* dp->d_namlen is a uint8_t, always < NAME_MAX */
386 if (dp
->d_namlen
> NAME_MAX
)
388 "reclen name too big (%d > %d) ",
389 dp
->d_namlen
, NAME_MAX
);
391 vprintf(stdout
, "\n");
396 if (dp
->d_ino
!= 0) {
404 * These variables are "local" to the following two functions.
406 char dirbuf
[DIRBLKSIZ
];
411 * add a new directory entry to a file.
414 putent(struct direct
*dp
)
416 dp
->d_reclen
= DIRSIZ(0, dp
, 0);
417 if (dirloc
+ dp
->d_reclen
> DIRBLKSIZ
) {
418 ((struct direct
*)(dirbuf
+ prev
))->d_reclen
=
420 (void) fwrite(dirbuf
, 1, DIRBLKSIZ
, df
);
423 memmove(dirbuf
+ dirloc
, dp
, (long)dp
->d_reclen
);
425 dirloc
+= dp
->d_reclen
;
429 * flush out a directory that is finished.
434 ((struct direct
*)(dirbuf
+ prev
))->d_reclen
= DIRBLKSIZ
- prev
;
435 (void) fwrite(dirbuf
, (int)dirloc
, 1, df
);
441 dcvt(struct odirect
*odp
, struct direct
*ndp
)
444 memset(ndp
, 0, sizeof(*ndp
));
446 ndp
->d_ino
= bswap16(odp
->d_ino
);
448 ndp
->d_ino
= odp
->d_ino
;
449 ndp
->d_type
= DT_UNKNOWN
;
450 (void) strncpy(ndp
->d_name
, odp
->d_name
, ODIRSIZ
);
451 ndp
->d_namlen
= strlen(ndp
->d_name
);
452 ndp
->d_reclen
= DIRSIZ(0, ndp
, 0);
456 * Seek to an entry in a directory.
457 * Only values returned by rst_telldir should be passed to rst_seekdir.
458 * This routine handles many directories in a single file.
459 * It takes the base of the directory in the file, plus
460 * the desired seek offset into it.
463 rst_seekdir(RST_DIR
*rdirp
, long loc
, long base
)
466 if (loc
== rst_telldir(rdirp
))
470 fprintf(stderr
, "bad seek pointer to rst_seekdir %d\n",
472 (void) lseek(rdirp
->dd_fd
, base
+ (loc
& ~(DIRBLKSIZ
- 1)), SEEK_SET
);
473 rdirp
->dd_loc
= loc
& (DIRBLKSIZ
- 1);
474 if (rdirp
->dd_loc
!= 0)
475 rdirp
->dd_size
= read(rdirp
->dd_fd
, rdirp
->dd_buf
, DIRBLKSIZ
);
479 * get next entry in a directory.
482 rst_readdir(RST_DIR
*rdirp
)
487 if (rdirp
->dd_loc
== 0) {
488 rdirp
->dd_size
= read(rdirp
->dd_fd
, rdirp
->dd_buf
,
490 if (rdirp
->dd_size
<= 0) {
491 dprintf(stderr
, "error reading directory\n");
495 if (rdirp
->dd_loc
>= rdirp
->dd_size
) {
499 dp
= (struct direct
*)(rdirp
->dd_buf
+ rdirp
->dd_loc
);
500 if (dp
->d_reclen
== 0 ||
501 dp
->d_reclen
> DIRBLKSIZ
+ 1 - rdirp
->dd_loc
) {
502 dprintf(stderr
, "corrupted directory: bad reclen %d\n",
506 rdirp
->dd_loc
+= dp
->d_reclen
;
507 if (dp
->d_ino
== 0 && strcmp(dp
->d_name
, "/") == 0)
509 if (dp
->d_ino
>= maxino
) {
510 dprintf(stderr
, "corrupted directory: bad inum %d\n",
519 * Simulate the opening of a directory
522 rst_opendir(const char *name
)
528 if ((ino
= dirlookup(name
)) > 0 &&
529 (itp
= inotablookup(ino
)) != NULL
) {
530 rdirp
= opendirfile(dirfile
);
531 rst_seekdir(rdirp
, itp
->t_seekpt
, itp
->t_seekpt
);
538 * In our case, there is nothing to do when closing a directory.
541 rst_closedir(RST_DIR
*rdirp
)
544 (void)close(rdirp
->dd_fd
);
550 * Simulate finding the current offset in the directory.
553 rst_telldir(RST_DIR
*rdirp
)
555 return ((long)lseek(rdirp
->dd_fd
,
556 (off_t
)0, SEEK_CUR
) - rdirp
->dd_size
+ rdirp
->dd_loc
);
560 * Open a directory file.
563 opendirfile(const char *name
)
568 if ((fd
= open(name
, O_RDONLY
)) == -1)
570 if ((rdirp
= malloc(sizeof(RST_DIR
))) == NULL
) {
580 * Set the mode, owner, and times for all new or changed directories
583 setdirmodes(int flags
)
586 struct modeinfo node
;
590 vprintf(stdout
, "Set directory mode, owner, and times.\n");
591 if (command
== 'r' || command
== 'R')
592 (void) snprintf(modefile
, sizeof(modefile
), "%s/rstmode%d",
593 tmpdir
, (int)dumpdate
);
594 if (modefile
[0] == '#') {
595 panic("modefile not defined\n");
596 fprintf(stderr
, "directory mode, owner, and times not set\n");
599 mf
= fopen(modefile
, "r");
601 fprintf(stderr
, "fopen: %s\n", strerror(errno
));
602 fprintf(stderr
, "cannot open mode file %s\n", modefile
);
603 fprintf(stderr
, "directory mode, owner, and times not set\n");
608 (void) fread((char *)&node
, 1, sizeof(struct modeinfo
), mf
);
611 ep
= lookupino(node
.ino
);
612 if (command
== 'i' || command
== 'x') {
615 if ((flags
& FORCE
) == 0 && ep
->e_flags
& EXISTED
) {
619 if (node
.ino
== ROOTINO
&& dotflag
== 0)
623 panic("cannot find directory inode %llu\n",
624 (unsigned long long)node
.ino
);
628 (void) utimes(cp
, node
.ctimep
);
629 (void) utimes(cp
, node
.mtimep
);
630 (void) chown(cp
, node
.uid
, node
.gid
);
631 (void) chmod(cp
, node
.mode
);
633 writemtree(cp
, "dir",
634 node
.uid
, node
.gid
, node
.mode
,
637 (void) chflags(cp
, node
.flags
);
643 panic("error setting directory modes\n");
648 * Generate a literal copy of a directory.
651 genliteraldir(const char *name
, ino_t ino
)
654 int ofile
, dp
, i
, size
;
657 itp
= inotablookup(ino
);
659 panic("Cannot find directory inode %llu named %s\n",
660 (unsigned long long)ino
, name
);
661 if ((ofile
= open(name
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666)) < 0) {
662 fprintf(stderr
, "%s: ", name
);
663 (void) fflush(stderr
);
664 fprintf(stderr
, "cannot create file: %s\n", strerror(errno
));
667 rst_seekdir(dirp
, itp
->t_seekpt
, itp
->t_seekpt
);
668 dp
= dup(dirp
->dd_fd
);
669 for (i
= itp
->t_size
; i
> 0; i
-= BUFSIZ
) {
670 size
= i
< BUFSIZ
? i
: BUFSIZ
;
671 if (read(dp
, buf
, (int) size
) == -1) {
673 "write error extracting inode %llu, name %s\n",
674 (unsigned long long)curfile
.ino
, curfile
.name
);
675 fprintf(stderr
, "read: %s\n", strerror(errno
));
678 if (!Nflag
&& write(ofile
, buf
, (int) size
) == -1) {
680 "write error extracting inode %llu, name %s\n",
681 (unsigned long long)curfile
.ino
, curfile
.name
);
682 fprintf(stderr
, "write: %s\n", strerror(errno
));
692 * Determine the type of an inode
699 itp
= inotablookup(ino
);
706 * Allocate and initialize a directory inode entry.
707 * If requested, save its pertinent mode, owner, and time info.
709 static struct inotab
*
710 allocinotab(FILE *mf
, struct context
*ctxp
, long aseekpt
)
713 struct modeinfo node
;
715 itp
= calloc(1, sizeof(struct inotab
));
717 panic("no memory directory table\n");
718 itp
->t_next
= inotab
[INOHASH(ctxp
->ino
)];
719 inotab
[INOHASH(ctxp
->ino
)] = itp
;
720 itp
->t_ino
= ctxp
->ino
;
721 itp
->t_seekpt
= aseekpt
;
724 node
.ino
= ctxp
->ino
;
725 node
.mtimep
[0].tv_sec
= ctxp
->atime_sec
;
726 node
.mtimep
[0].tv_usec
= ctxp
->atime_nsec
/ 1000;
727 node
.mtimep
[1].tv_sec
= ctxp
->mtime_sec
;
728 node
.mtimep
[1].tv_usec
= ctxp
->mtime_nsec
/ 1000;
729 node
.ctimep
[0].tv_sec
= ctxp
->atime_sec
;
730 node
.ctimep
[0].tv_usec
= ctxp
->atime_nsec
/ 1000;
731 node
.ctimep
[1].tv_sec
= ctxp
->birthtime_sec
;
732 node
.ctimep
[1].tv_usec
= ctxp
->birthtime_nsec
/ 1000;
733 node
.mode
= ctxp
->mode
;
734 node
.flags
= ctxp
->file_flags
;
735 node
.uid
= ctxp
->uid
;
736 node
.gid
= ctxp
->gid
;
737 (void) fwrite((char *)&node
, 1, sizeof(struct modeinfo
), mf
);
742 * Look up an inode in the table of directories
744 static struct inotab
*
745 inotablookup(ino_t ino
)
749 for (itp
= inotab
[INOHASH(ino
)]; itp
!= NULL
; itp
= itp
->t_next
)
750 if (itp
->t_ino
== ino
)
763 if (modefile
[0] != '#')
764 (void) unlink(modefile
);
765 if (dirfile
[0] != '#')
766 (void) unlink(dirfile
);