/* Copyright (c) 1983 Regents of the University of California */
static char sccsid
[] = "@(#)dirs.c 3.8 (Berkeley) 83/04/19";
* 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 */
* Format of old style directories.
* Structure and routines associated with listing directories.
ino_t fnum
; /* inode number of file */
char *fname
; /* file name */
short fflags
; /* extraction flags, if any */
char ftype
; /* file type, e.g. LEAF or NODE */
* 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");
"restor: %s - cannot create directory temporary\n",
(void) sprintf(modefile
, "/tmp/rstmode%d", dumpdate
);
mf
= fopen(modefile
, "w");
"restor: %s - cannot create modefile \n",
(void) strncpy(nulldir
.d_name
, "/", (int)nulldir
.d_namlen
);
nulldir
.d_reclen
= DIRSIZ(&nulldir
);
curfile
.name
= "<directory file - name unknown>";
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
;
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
);
seekdir(dirp
, itp
->t_seekpt
, itp
->t_seekpt
);
dp
= readdir(dirp
); /* "." */
dp
= readdir(dirp
); /* ".." */
dp
= readdir(dirp
); /* first real entry */
* "/" signals end of directory
while (dp
!= NULL
&& !(dp
->d_namlen
== 1 && dp
->d_name
[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
);
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
);
seekdir(dirp
, itp
->t_seekpt
, itp
->t_seekpt
);
if (dp
->d_namlen
== 1 && dp
->d_name
[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
);
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 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.
if (loc
== telldir(dirp
))
fprintf(stderr
, "bad seek pointer to 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_loc
>= dirp
->dd_size
) {
dp
= (struct direct
*)(dirp
->dd_buf
+ dirp
->dd_loc
);
dp
->d_reclen
> DIRBLKSIZ
+ 1 - dirp
->dd_loc
)
dirp
->dd_loc
+= dp
->d_reclen
;
* Set the mode, owner, and times for all new or changed directories
vprintf(stdout
, "Set directory mode, owner, and times.\n");
mf
= fopen(modefile
, "r");
panic("cannot open mode file %s\n", modefile
);
(void) fread((char *)&node
, 1, sizeof(struct modeinfo
), mf
);
ep
= lookupino(node
.ino
);
if (ep
== NIL
|| (ep
->e_flags
& NEW
) == 0) {
if (command
!= 'r' && command
!= 'R')
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
= open(name
, FWRONLY
|FCREATE
, 0666)) < 0) {
fprintf(stderr
, "%s: cannot create file\n", name
);
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
);
* Do an "ls" style listing of a directory
register struct afile
*fp
;
register struct inotab
*itp
;
struct afile
*dfp0
, *dfplast
;
single
.fname
= savename(rindex(name
, '/') + 1);
seekdir(dirp
, itp
->t_seekpt
, itp
->t_seekpt
);
if (getdir(dirp
, &dfp0
, &dfplast
) == FAIL
)
qsort((char *)dfp0
, dfplast
- dfp0
, sizeof (struct afile
), fcmp
);
for (fp
= dfp0
; fp
< dfplast
; fp
++)
* Read the contents of a directory.
getdir(dirp
, pfp0
, pfplast
)
struct afile
**pfp0
, **pfplast
;
register struct afile
*fp
;
register struct direct
*dp
;
static struct afile
*basefp
= NULL
;
basefp
= (struct afile
*)calloc((unsigned)nent
,
while (dp
= readdir(dirp
)) {
if (dp
->d_ino
== 0 && strcmp(dp
->d_name
, "/") == 0)
if (BIT(dp
->d_ino
, dumpmap
) == 0)
(strcmp(dp
->d_name
, ".") == 0 ||
strcmp(dp
->d_name
, "..") == 0))
fp
->fname
= savename(dp
->d_name
);
basefp
= (struct afile
*)realloc((char *)basefp
,
(unsigned)(2 * nent
* sizeof (struct afile
)));
fprintf(stderr
, "ls: out of memory\n");
* Print out a pretty listing of a directory
struct afile
*fp0
, *fplast
;
register struct afile
*fp
;
int width
= 0, w
, nentry
= fplast
- fp0
;
int i
, j
, len
, columns
, lines
;
for (fp
= fp0
; fp
< fplast
; fp
++) {
fp
->ftype
= inodetype(fp
->fnum
);
np
= lookupino(fp
->fnum
);
fp
->fflags
= np
->e_flags
;
len
= strlen(fmtentry(fp
));
lines
= (nentry
+ columns
- 1) / columns
;
for (i
= 0; i
< lines
; i
++) {
for (j
= 0; j
< columns
; j
++) {
fp
= fp0
+ j
* lines
+ i
;
fprintf(stderr
, "%s", cp
);
if (fp
+ lines
>= fplast
) {
* Comparison routine for qsort.
register struct afile
*f1
, *f2
;
return (strcmp(f1
->fname
, f2
->fname
));
* Format a directory entry.
register struct afile
*fp
;
static char fmtres
[BUFSIZ
];
(void) sprintf(fmtres
, "%5d ", fp
->fnum
);
dp
= &fmtres
[strlen(fmtres
)];
if ((fp
->fflags
& NEW
) != 0)
for (cp
= fp
->fname
; *cp
; cp
++)
if (!vflag
&& (*cp
< ' ' || *cp
>= 0177))
* 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
));
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);