200
1 1
U S E R - L E V E L A P I
Generating the Attributes
The next phase of the program is to iterate over all the files in the directory
referenced by the path. The program does this work in a separate function,
generate keywords()
, that
main()
calls. The
main()
function passes its
BPath
object to
generate keywords()
to indicate which directory to iterate over.
void
generate_keywords(BPath *path)
{
BDirectory dir;
entry_ref
ref;
dir.SetTo(path->Path());
if (dir.InitCheck() != 0)
/* hmmm, dir doesn't exist? */
return;
while(dir.GetNextRef(&ref) == B_NO_ERROR) {
char *keywords;
BFile file;
file.SetTo(&ref, O_RDWR);
keywords = synthesize_keywords(&file);
file.WriteAttr(INDEX_NAME,
B_STRING_TYPE, 0,
keywords, strlen(keywords)+1);
free(keywords);
}
}
The first part of the routine initializes the
BDirectory
object and checks
that it refers to a valid directory. The main loop of
generate keywords()
iter-
ates on the call to
GetNextRef()
. Each call to
GetNextRef()
returns a reference
to the next entry in the directory until there are no more entries. The
en-
try ref
object returned by
GetNextRef()
is used to initialize the
BFile
object
so that the contents of the file can be read.
Next,
generate keywords()
calls
synthesize keywords()
. Although we omit
the details, presumably
synthesize keywords()
would read the contents of the
file and generate a list of keywords as a string.
After synthesizing the list of keywords, our example program writes those
keywords as an attribute of the file using the
WriteAttr()
function. Writing
the keyword attribute also automatically indexes the keywords because the
keyword index exists.
Practical File System Design:The Be File System
, Dominic Giampaolo
page 200