* Copyright (c) 1983 Regents of the University of California.
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
static char sccsid
[] = "@(#)dirs.c 5.4 (Berkeley) %G%";
#include <protocols/dumprestore.h>
* Symbol table of directories read from tape.
#define INOHASH(val) (val % HASHSIZE)
static struct inotab
*inotab
[HASHSIZE
];
extern struct inotab
*inotablookup();
extern struct inotab
*allocinotab();
* Information retained about directories.
* Global variables for this file.
static char dirfile
[32] = "#"; /* No file */
static char modefile
[32] = "#"; /* No file */
struct direct
*rst_readdir();
extern void rst_seekdir();
* Format of old style directories.
* Extract directory contents, building up a directory structure
* on disk for extraction by name.
* If genmode is requested, save mode, owner, and times for all
* directories on the tape.
register struct dinode
*ip
;
vprintf(stdout
, "Extract directories from tape\n");
(void) sprintf(dirfile
, "/tmp/rstdir%d", dumpdate
);
df
= fopen(dirfile
, "w");
"restore: %s - cannot create directory temporary\n",
(void) sprintf(modefile
, "/tmp/rstmode%d", dumpdate
);
mf
= fopen(modefile
, "w");
"restore: %s - cannot create modefile \n",
(void) strcpy(nulldir
.d_name
, "/");
nulldir
.d_reclen
= DIRSIZ(&nulldir
);
curfile
.name
= "<directory file - name unknown>";
if (ip
== NULL
|| (ip
->di_mode
& IFMT
) != IFDIR
) {
panic("Root directory is not on tape\n");
itp
= allocinotab(curfile
.ino
, ip
, seekpt
);
itp
->t_size
= seekpt
- itp
->t_seekpt
;
* skip over all the directories on the tape
while ((curfile
.dip
->di_mode
& IFMT
) == IFDIR
) {
* Recursively find names and inumbers of all files in subtree
* pname and pass them off to be processed.
treescan(pname
, ino
, todo
)
register struct inotab
*itp
;
register struct direct
*dp
;
register struct entry
*np
;
char locname
[MAXPATHLEN
+ 1];
* Pname is name of a simple file or an unchanged directory.
(void) (*todo
)(pname
, ino
, LEAF
);
* Pname is a dumped directory name.
if ((*todo
)(pname
, ino
, NODE
) == FAIL
)
* begin search through the directory
* skipping over "." and ".."
(void) strncpy(locname
, pname
, MAXPATHLEN
);
(void) strncat(locname
, "/", MAXPATHLEN
);
namelen
= strlen(locname
);
rst_seekdir(dirp
, itp
->t_seekpt
, itp
->t_seekpt
);
dp
= rst_readdir(dirp
); /* "." */
if (dp
!= NULL
&& strcmp(dp
->d_name
, ".") == 0)
dp
= rst_readdir(dirp
); /* ".." */
fprintf(stderr
, "Warning: `.' missing from directory %s\n",
if (dp
!= NULL
&& strcmp(dp
->d_name
, "..") == 0)
dp
= rst_readdir(dirp
); /* first real entry */
fprintf(stderr
, "Warning: `..' missing from directory %s\n",
* a zero inode signals end of directory
while (dp
!= NULL
&& dp
->d_ino
!= 0) {
if (namelen
+ dp
->d_namlen
>= MAXPATHLEN
) {
fprintf(stderr
, "%s%s: name exceeds %d char\n",
locname
, dp
->d_name
, MAXPATHLEN
);
(void) strncat(locname
, dp
->d_name
, (int)dp
->d_namlen
);
treescan(locname
, dp
->d_ino
, todo
);
rst_seekdir(dirp
, bpt
, itp
->t_seekpt
);
fprintf(stderr
, "corrupted directory: %s.\n", locname
);
* Search the directory tree rooted at inode ROOTINO
* for the path pointed at by n
while (*cp1
!= '/' && *cp1
)
* search the directory inode ino
register struct direct
*dp
;
register struct inotab
*itp
;
itp
= inotablookup(inum
);
rst_seekdir(dirp
, itp
->t_seekpt
, itp
->t_seekpt
);
if (dp
== NULL
|| dp
->d_ino
== 0)
} while (dp
->d_namlen
!= len
|| strncmp(dp
->d_name
, cp
, len
) != 0);
* Put the directory entries in the directory file
register struct odirect
*odp
;
register struct direct
*dp
;
eodp
= (struct odirect
*)&buf
[size
];
for (odp
= (struct odirect
*)buf
; odp
< eodp
; odp
++)
for (loc
= 0; loc
< size
; ) {
dp
= (struct direct
*)(buf
+ loc
);
swabst("l2s", (char *) dp
);
i
= DIRBLKSIZ
- (loc
& (DIRBLKSIZ
- 1));
if (dp
->d_reclen
== 0 || dp
->d_reclen
> i
) {
* These variables are "local" to the following two functions.
* add a new directory entry to a file.
dp
->d_reclen
= DIRSIZ(dp
);
if (dirloc
+ dp
->d_reclen
> DIRBLKSIZ
) {
((struct direct
*)(dirbuf
+ prev
))->d_reclen
=
(void) fwrite(dirbuf
, 1, DIRBLKSIZ
, df
);
bcopy((char *)dp
, dirbuf
+ dirloc
, (long)dp
->d_reclen
);
* flush out a directory that is finished.
((struct direct
*)(dirbuf
+ prev
))->d_reclen
= DIRBLKSIZ
- prev
;
(void) fwrite(dirbuf
, (int)dirloc
, 1, df
);
register struct odirect
*odp
;
register struct direct
*ndp
;
bzero((char *)ndp
, (long)(sizeof *ndp
));
(void) strncpy(ndp
->d_name
, odp
->d_name
, ODIRSIZ
);
ndp
->d_namlen
= strlen(ndp
->d_name
);
ndp
->d_reclen
= DIRSIZ(ndp
);
* Seek to an entry in a directory.
* Only values returned by ``telldir'' should be passed to rst_seekdir.
* This routine handles many directories in a single file.
* It takes the base of the directory in the file, plus
* the desired seek offset into it.
rst_seekdir(dirp
, loc
, base
)
if (loc
== telldir(dirp
))
fprintf(stderr
, "bad seek pointer to rst_seekdir %d\n", loc
);
(void) lseek(dirp
->dd_fd
, base
+ (loc
& ~(DIRBLKSIZ
- 1)), 0);
dirp
->dd_loc
= loc
& (DIRBLKSIZ
- 1);
dirp
->dd_size
= read(dirp
->dd_fd
, dirp
->dd_buf
, DIRBLKSIZ
);
* get next entry in a directory.
register struct direct
*dp
;
dirp
->dd_size
= read(dirp
->dd_fd
, dirp
->dd_buf
,
if (dirp
->dd_size
<= 0) {
dprintf(stderr
, "error reading directory\n");
if (dirp
->dd_loc
>= dirp
->dd_size
) {
dp
= (struct direct
*)(dirp
->dd_buf
+ dirp
->dd_loc
);
dp
->d_reclen
> DIRBLKSIZ
+ 1 - dirp
->dd_loc
) {
dprintf(stderr
, "corrupted directory: bad reclen %d\n",
dirp
->dd_loc
+= dp
->d_reclen
;
if (dp
->d_ino
== 0 && strcmp(dp
->d_name
, "/") != 0)
if (dp
->d_ino
>= maxino
) {
dprintf(stderr
, "corrupted directory: bad inum %d\n",
* Simulate the opening of a directory
if ((ino
= dirlookup(name
)) > 0 &&
(itp
= inotablookup(ino
)) != NULL
) {
rst_seekdir(dirp
, itp
->t_seekpt
, itp
->t_seekpt
);
* Set the mode, owner, and times for all new or changed directories
vprintf(stdout
, "Set directory mode, owner, and times.\n");
(void) sprintf(modefile
, "/tmp/rstmode%d", dumpdate
);
mf
= fopen(modefile
, "r");
fprintf(stderr
, "cannot open mode file %s\n", modefile
);
fprintf(stderr
, "directory mode, owner, and times not set\n");
(void) fread((char *)&node
, 1, sizeof(struct modeinfo
), mf
);
ep
= lookupino(node
.ino
);
if (command
== 'i' || command
== 'x') {
if (ep
->e_flags
& EXISTED
) {
if (node
.ino
== ROOTINO
&&
reply("set owner/mode for '.'") == FAIL
)
panic("cannot find directory inode %d\n", node
.ino
);
(void) chown(cp
, node
.uid
, node
.gid
);
(void) chmod(cp
, node
.mode
);
panic("error setting directory modes\n");
* Generate a literal copy of a directory.
register struct inotab
*itp
;
panic("Cannot find directory inode %d named %s\n", ino
, name
);
if ((ofile
= creat(name
, 0666)) < 0) {
fprintf(stderr
, "%s: ", name
);
perror("cannot create file");
rst_seekdir(dirp
, itp
->t_seekpt
, itp
->t_seekpt
);
for (i
= itp
->t_size
; i
> 0; i
-= BUFSIZ
) {
size
= i
< BUFSIZ
? i
: BUFSIZ
;
if (read(dp
, buf
, (int) size
) == -1) {
"write error extracting inode %d, name %s\n",
curfile
.ino
, curfile
.name
);
if (write(ofile
, buf
, (int) size
) == -1) {
"write error extracting inode %d, name %s\n",
curfile
.ino
, curfile
.name
);
* Determine the type of an inode
* Allocate and initialize a directory inode entry.
* If requested, save its pertinent mode, owner, and time info.
allocinotab(ino
, dip
, seekpt
)
register struct inotab
*itp
;
itp
= (struct inotab
*)calloc(1, sizeof(struct inotab
));
panic("no memory directory table\n");
itp
->t_next
= inotab
[INOHASH(ino
)];
inotab
[INOHASH(ino
)] = itp
;
node
.timep
[0] = dip
->di_atime
;
node
.timep
[1] = dip
->di_mtime
;
node
.mode
= dip
->di_mode
;
(void) fwrite((char *)&node
, 1, sizeof(struct modeinfo
), mf
);
* Look up an inode in the table of directories
register struct inotab
*itp
;
for (itp
= inotab
[INOHASH(ino
)]; itp
!= NULL
; itp
= itp
->t_next
)
return ((struct inotab
*)0);