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 * 4. 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
37 static char sccsid
[] = "@(#)dirs.c 8.7 (Berkeley) 5/1/95";
39 static const char rcsid
[] =
43 #include <sys/param.h>
48 #include <ufs/ufs/dinode.h>
49 #include <ufs/ufs/dir.h>
50 #include <protocols/dumprestore.h>
65 * Symbol table of directories read from tape.
68 #define INOHASH(val) (val % HASHSIZE)
70 struct inotab
*t_next
;
75 static struct inotab
*inotab
[HASHSIZE
];
78 * Information retained about directories.
82 struct timeval ctimep
[2];
83 struct timeval mtimep
[2];
92 * Definitions for library routines operating on directories.
95 #define DIRBLKSIZ 1024
100 char dd_buf
[DIRBLKSIZ
];
104 * Global variables for this file.
107 static FILE *df
, *mf
;
108 static RST_DIR
*dirp
;
109 static char dirfile
[MAXPATHLEN
] = "#"; /* No file */
110 static char modefile
[MAXPATHLEN
] = "#"; /* No file */
111 static char dot
[2] = "."; /* So it can be modified */
113 static struct inotab
*allocinotab(struct context
*, long);
114 static void flushent(void);
115 static struct inotab
*inotablookup(ino_t
);
116 static RST_DIR
*opendirfile(const char *);
117 static void putdir(char *, long);
118 static void putdirattrs(char *, long);
119 static void putent(struct direct
*);
120 static void rst_seekdir(RST_DIR
*, long, long);
121 static long rst_telldir(RST_DIR
*);
122 static struct direct
*searchdir(ino_t
, char *);
123 static void fail_dirtmp(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 direct nulldir
;
139 vprintf(stdout
, "Extract directories from tape\n");
140 if ((tmpdir
= getenv("TMPDIR")) == NULL
|| tmpdir
[0] == '\0')
142 (void) sprintf(dirfile
, "%s/rstdir%ld", tmpdir
, dumpdate
);
143 if (command
!= 'r' && command
!= 'R') {
144 (void *) strcat(dirfile
, "-XXXXXX");
145 fd
= mkstemp(dirfile
);
147 fd
= open(dirfile
, O_RDWR
|O_CREAT
|O_EXCL
, 0666);
148 if (fd
== -1 || (df
= fdopen(fd
, "w")) == NULL
) {
151 warn("%s: cannot create directory database", dirfile
);
155 (void) sprintf(modefile
, "%s/rstmode%ld", tmpdir
, dumpdate
);
156 if (command
!= 'r' && command
!= 'R') {
157 (void *) strcat(modefile
, "-XXXXXX");
158 fd
= mkstemp(modefile
);
160 fd
= open(modefile
, O_RDWR
|O_CREAT
|O_EXCL
, 0666);
161 if (fd
== -1 || (mf
= fdopen(fd
, "w")) == NULL
) {
164 warn("%s: cannot create modefile", modefile
);
169 nulldir
.d_type
= DT_DIR
;
170 nulldir
.d_namlen
= 1;
171 (void) strcpy(nulldir
.d_name
, "/");
172 nulldir
.d_reclen
= DIRSIZ(0, &nulldir
);
174 curfile
.name
= "<directory file - name unknown>";
175 curfile
.action
= USING
;
176 if (curfile
.mode
== 0 || (curfile
.mode
& IFMT
) != IFDIR
)
178 itp
= allocinotab(&curfile
, seekpt
);
179 getfile(putdir
, putdirattrs
, xtrnull
);
182 itp
->t_size
= seekpt
- itp
->t_seekpt
;
185 fail_dirtmp(dirfile
);
186 dirp
= opendirfile(dirfile
);
188 fprintf(stderr
, "opendirfile: %s\n", strerror(errno
));
189 if (mf
!= NULL
&& fclose(mf
) != 0)
190 fail_dirtmp(modefile
);
193 panic("Root directory is not on tape\n");
197 * skip over all the directories on the tape
203 while (curfile
.ino
&& (curfile
.mode
& IFMT
) == IFDIR
) {
209 * Recursively find names and inumbers of all files in subtree
210 * pname and pass them off to be processed.
213 treescan(char *pname
, ino_t ino
, long (*todo
)(char *, ino_t
, int))
219 char locname
[MAXPATHLEN
];
221 itp
= inotablookup(ino
);
224 * Pname is name of a simple file or an unchanged directory.
226 (void) (*todo
)(pname
, ino
, LEAF
);
230 * Pname is a dumped directory name.
232 if ((*todo
)(pname
, ino
, NODE
) == FAIL
)
235 * begin search through the directory
236 * skipping over "." and ".."
238 (void) strlcpy(locname
, pname
, sizeof(locname
));
239 (void) strlcat(locname
, "/", sizeof(locname
));
240 namelen
= strlen(locname
);
241 rst_seekdir(dirp
, itp
->t_seekpt
, itp
->t_seekpt
);
242 dp
= rst_readdir(dirp
); /* "." */
243 if (dp
!= NULL
&& strcmp(dp
->d_name
, ".") == 0)
244 dp
= rst_readdir(dirp
); /* ".." */
246 fprintf(stderr
, "Warning: `.' missing from directory %s\n",
248 if (dp
!= NULL
&& strcmp(dp
->d_name
, "..") == 0)
249 dp
= rst_readdir(dirp
); /* first real entry */
251 fprintf(stderr
, "Warning: `..' missing from directory %s\n",
253 bpt
= rst_telldir(dirp
);
255 * a zero inode signals end of directory
258 locname
[namelen
] = '\0';
259 if (namelen
+ dp
->d_namlen
>= sizeof(locname
)) {
260 fprintf(stderr
, "%s%s: name exceeds %d char\n",
261 locname
, dp
->d_name
, sizeof(locname
) - 1);
263 (void)strlcat(locname
, dp
->d_name
, sizeof(locname
));
264 treescan(locname
, dp
->d_ino
, todo
);
265 rst_seekdir(dirp
, bpt
, itp
->t_seekpt
);
267 dp
= rst_readdir(dirp
);
268 bpt
= rst_telldir(dirp
);
273 * Lookup a pathname which is always assumed to start from the ROOTINO.
276 pathsearch(const char *pathname
)
280 char *path
, *name
, buffer
[MAXPATHLEN
];
282 strcpy(buffer
, pathname
);
288 while ((name
= strsep(&path
, "/")) != NULL
&& *name
!= '\0') {
289 if ((dp
= searchdir(ino
, name
)) == NULL
)
297 * Lookup the requested name in directory inum.
298 * Return its inode number if found, zero if it does not exist.
300 static struct direct
*
301 searchdir(ino_t inum
, char *name
)
307 itp
= inotablookup(inum
);
310 rst_seekdir(dirp
, itp
->t_seekpt
, itp
->t_seekpt
);
313 dp
= rst_readdir(dirp
);
316 } while (dp
->d_namlen
!= len
|| strncmp(dp
->d_name
, name
, len
) != 0);
321 * Put the directory entries in the directory file
324 putdir(char *buf
, long size
)
329 for (loc
= 0; loc
< size
; ) {
330 dp
= (struct direct
*)(buf
+ loc
);
332 swabst((u_char
*)"ls", (u_char
*) dp
);
333 if (oldinofmt
&& dp
->d_ino
!= 0) {
334 #if BYTE_ORDER == BIG_ENDIAN
336 dp
->d_namlen
= dp
->d_type
;
338 if (!Bcvt
&& dp
->d_namlen
== 0)
339 dp
->d_namlen
= dp
->d_type
;
341 dp
->d_type
= DT_UNKNOWN
;
343 i
= DIRBLKSIZ
- (loc
& (DIRBLKSIZ
- 1));
344 if ((dp
->d_reclen
& 0x3) != 0 ||
346 dp
->d_reclen
< DIRSIZ(0, dp
)
348 || dp
->d_namlen
> NAME_MAX
351 vprintf(stdout
, "Mangled directory: ");
352 if ((dp
->d_reclen
& 0x3) != 0)
354 "reclen not multiple of 4 ");
355 if (dp
->d_reclen
< DIRSIZ(0, dp
))
357 "reclen less than DIRSIZ (%d < %d) ",
358 dp
->d_reclen
, DIRSIZ(0, dp
));
360 if (dp
->d_namlen
> NAME_MAX
)
362 "reclen name too big (%d > %d) ",
363 dp
->d_namlen
, NAME_MAX
);
365 vprintf(stdout
, "\n");
370 if (dp
->d_ino
!= 0) {
377 * These variables are "local" to the following two functions.
379 char dirbuf
[DIRBLKSIZ
];
384 * add a new directory entry to a file.
387 putent(struct direct
*dp
)
389 dp
->d_reclen
= DIRSIZ(0, dp
);
390 if (dirloc
+ dp
->d_reclen
> DIRBLKSIZ
) {
391 ((struct direct
*)(dirbuf
+ prev
))->d_reclen
=
393 if (fwrite(dirbuf
, DIRBLKSIZ
, 1, df
) != 1)
394 fail_dirtmp(dirfile
);
397 memmove(dirbuf
+ dirloc
, dp
, (long)dp
->d_reclen
);
399 dirloc
+= dp
->d_reclen
;
403 * flush out a directory that is finished.
408 ((struct direct
*)(dirbuf
+ prev
))->d_reclen
= DIRBLKSIZ
- prev
;
409 if (fwrite(dirbuf
, (int)dirloc
, 1, df
) != 1)
410 fail_dirtmp(dirfile
);
416 * Save extended attributes for a directory entry to a file.
419 putdirattrs(char *buf
, long size
)
422 if (mf
!= NULL
&& fwrite(buf
, size
, 1, mf
) != 1)
423 fail_dirtmp(modefile
);
427 * Seek to an entry in a directory.
428 * Only values returned by rst_telldir should be passed to rst_seekdir.
429 * This routine handles many directories in a single file.
430 * It takes the base of the directory in the file, plus
431 * the desired seek offset into it.
434 rst_seekdir(RST_DIR
*dirp
, long loc
, long base
)
437 if (loc
== rst_telldir(dirp
))
441 fprintf(stderr
, "bad seek pointer to rst_seekdir %ld\n", loc
);
442 (void) lseek(dirp
->dd_fd
, base
+ (loc
& ~(DIRBLKSIZ
- 1)), SEEK_SET
);
443 dirp
->dd_loc
= loc
& (DIRBLKSIZ
- 1);
444 if (dirp
->dd_loc
!= 0)
445 dirp
->dd_size
= read(dirp
->dd_fd
, dirp
->dd_buf
, DIRBLKSIZ
);
449 * get next entry in a directory.
452 rst_readdir(RST_DIR
*dirp
)
457 if (dirp
->dd_loc
== 0) {
458 dirp
->dd_size
= read(dirp
->dd_fd
, dirp
->dd_buf
,
460 if (dirp
->dd_size
<= 0) {
461 dprintf(stderr
, "error reading directory\n");
465 if (dirp
->dd_loc
>= dirp
->dd_size
) {
469 dp
= (struct direct
*)(dirp
->dd_buf
+ dirp
->dd_loc
);
470 if (dp
->d_reclen
== 0 ||
471 dp
->d_reclen
> DIRBLKSIZ
+ 1 - dirp
->dd_loc
) {
472 dprintf(stderr
, "corrupted directory: bad reclen %d\n",
476 dirp
->dd_loc
+= dp
->d_reclen
;
477 if (dp
->d_ino
== 0 && strcmp(dp
->d_name
, "/") == 0)
479 if (dp
->d_ino
>= maxino
) {
480 dprintf(stderr
, "corrupted directory: bad inum %d\n",
489 * Simulate the opening of a directory
492 rst_opendir(const char *name
)
498 if ((ino
= dirlookup(name
)) > 0 &&
499 (itp
= inotablookup(ino
)) != NULL
) {
500 dirp
= opendirfile(dirfile
);
501 rst_seekdir(dirp
, itp
->t_seekpt
, itp
->t_seekpt
);
508 * In our case, there is nothing to do when closing a directory.
511 rst_closedir(void *arg
)
516 (void)close(dirp
->dd_fd
);
522 * Simulate finding the current offset in the directory.
525 rst_telldir(RST_DIR
*dirp
)
527 return ((long)lseek(dirp
->dd_fd
,
528 (off_t
)0, SEEK_CUR
) - dirp
->dd_size
+ dirp
->dd_loc
);
532 * Open a directory file.
535 opendirfile(const char *name
)
540 if ((fd
= open(name
, O_RDONLY
)) == -1)
542 if ((dirp
= malloc(sizeof(RST_DIR
))) == NULL
) {
552 * Set the mode, owner, and times for all new or changed directories
555 setdirmodes(int flags
)
558 struct modeinfo node
;
565 vprintf(stdout
, "Set directory mode, owner, and times.\n");
566 if ((tmpdir
= getenv("TMPDIR")) == NULL
|| tmpdir
[0] == '\0')
568 if (command
== 'r' || command
== 'R')
569 (void) sprintf(modefile
, "%s/rstmode%ld", tmpdir
, dumpdate
);
570 if (modefile
[0] == '#') {
571 panic("modefile not defined\n");
572 fprintf(stderr
, "directory mode, owner, and times not set\n");
575 mf
= fopen(modefile
, "r");
577 fprintf(stderr
, "fopen: %s\n", strerror(errno
));
578 fprintf(stderr
, "cannot open mode file %s\n", modefile
);
579 fprintf(stderr
, "directory mode, owner, and times not set\n");
586 (void) fread((char *)&node
, 1, sizeof(struct modeinfo
), mf
);
588 warn("%s: cannot read modefile.", modefile
);
589 fprintf(stderr
, "Mode, owner, and times not set.\n");
594 if (node
.extsize
> 0) {
595 if (bufsize
< node
.extsize
) {
598 if ((buf
= malloc(node
.extsize
)) != 0) {
599 bufsize
= node
.extsize
;
604 if (bufsize
>= node
.extsize
) {
605 (void) fread(buf
, 1, node
.extsize
, mf
);
607 warn("%s: cannot read modefile.",
609 fprintf(stderr
, "Not all external ");
610 fprintf(stderr
, "attributes set.\n");
614 (void) fseek(mf
, node
.extsize
, SEEK_CUR
);
616 warn("%s: cannot seek in modefile.",
618 fprintf(stderr
, "Not all directory ");
619 fprintf(stderr
, "attributes set.\n");
624 ep
= lookupino(node
.ino
);
625 if (command
== 'i' || command
== 'x') {
628 if ((flags
& FORCE
) == 0 && ep
->e_flags
& EXISTED
) {
632 if (node
.ino
== ROOTINO
&&
633 reply("set owner/mode for '.'") == FAIL
)
637 panic("cannot find directory inode %d\n", node
.ino
);
642 if (node
.extsize
> 0) {
643 if (bufsize
>= node
.extsize
) {
644 set_extattr_file(cp
, buf
, node
.extsize
);
646 fprintf(stderr
, "Cannot restore %s%s\n",
647 "extended attributes for ", cp
);
651 (void) chown(cp
, myuid
, node
.gid
);
653 (void) chown(cp
, node
.uid
, node
.gid
);
654 (void) chmod(cp
, node
.mode
);
655 utimes(cp
, node
.ctimep
);
656 utimes(cp
, node
.mtimep
);
657 (void) chflags(cp
, node
.flags
);
667 * Generate a literal copy of a directory.
670 genliteraldir(char *name
, ino_t ino
)
673 int ofile
, dp
, i
, size
;
676 itp
= inotablookup(ino
);
678 panic("Cannot find directory inode %d named %s\n", ino
, name
);
679 if ((ofile
= open(name
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666)) < 0) {
680 fprintf(stderr
, "%s: ", name
);
681 (void) fflush(stderr
);
682 fprintf(stderr
, "cannot create file: %s\n", strerror(errno
));
685 rst_seekdir(dirp
, itp
->t_seekpt
, itp
->t_seekpt
);
686 dp
= dup(dirp
->dd_fd
);
687 for (i
= itp
->t_size
; i
> 0; i
-= BUFSIZ
) {
688 size
= i
< BUFSIZ
? i
: BUFSIZ
;
689 if (read(dp
, buf
, (int) size
) == -1) {
691 "write error extracting inode %d, name %s\n",
692 curfile
.ino
, curfile
.name
);
693 fprintf(stderr
, "read: %s\n", strerror(errno
));
696 if (!Nflag
&& write(ofile
, buf
, (int) size
) == -1) {
698 "write error extracting inode %d, name %s\n",
699 curfile
.ino
, curfile
.name
);
700 fprintf(stderr
, "write: %s\n", strerror(errno
));
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
*
728 allocinotab(struct context
*ctxp
, long seekpt
)
731 struct modeinfo node
;
733 itp
= calloc(1, sizeof(struct inotab
));
735 panic("no memory for directory table\n");
736 itp
->t_next
= inotab
[INOHASH(ctxp
->ino
)];
737 inotab
[INOHASH(ctxp
->ino
)] = itp
;
738 itp
->t_ino
= ctxp
->ino
;
739 itp
->t_seekpt
= seekpt
;
742 node
.ino
= ctxp
->ino
;
743 node
.mtimep
[0].tv_sec
= ctxp
->atime_sec
;
744 node
.mtimep
[0].tv_usec
= ctxp
->atime_nsec
/ 1000;
745 node
.mtimep
[1].tv_sec
= ctxp
->mtime_sec
;
746 node
.mtimep
[1].tv_usec
= ctxp
->mtime_nsec
/ 1000;
747 node
.ctimep
[0].tv_sec
= ctxp
->atime_sec
;
748 node
.ctimep
[0].tv_usec
= ctxp
->atime_nsec
/ 1000;
749 node
.ctimep
[1].tv_sec
= ctxp
->birthtime_sec
;
750 node
.ctimep
[1].tv_usec
= ctxp
->birthtime_nsec
/ 1000;
751 node
.extsize
= ctxp
->extsize
;
752 node
.mode
= ctxp
->mode
;
753 node
.flags
= ctxp
->file_flags
;
754 node
.uid
= ctxp
->uid
;
755 node
.gid
= ctxp
->gid
;
756 if (fwrite((char *)&node
, sizeof(struct modeinfo
), 1, mf
) != 1)
757 fail_dirtmp(modefile
);
762 * Look up an inode in the table of directories
764 static struct inotab
*
765 inotablookup(ino_t ino
)
769 for (itp
= inotab
[INOHASH(ino
)]; itp
!= NULL
; itp
= itp
->t_next
)
770 if (itp
->t_ino
== ino
)
783 if (modefile
[0] != '#') {
784 (void) truncate(modefile
, 0);
785 (void) unlink(modefile
);
787 if (dirfile
[0] != '#') {
788 (void) truncate(dirfile
, 0);
789 (void) unlink(dirfile
);
795 * Print out information about the failure to save directory,
796 * extended attribute, and mode information.
799 fail_dirtmp(char *filename
)
803 warn("%s: cannot write directory database", filename
);
804 if (errno
== ENOSPC
) {
805 if ((tmpdir
= getenv("TMPDIR")) == NULL
|| tmpdir
[0] == '\0')
807 fprintf(stderr
, "Try making space in %s, %s\n%s\n", tmpdir
,
808 "or set environment variable TMPDIR",
809 "to an alternate location with more disk space.");