Tree, alloctree, freetree,
File, createfile, closefile, removefile, walkfile,
opendirfile, readdirfile, closedirfile, hasperm – in-memory file hierarchy
typedef struct File
typedef struct Tree
Tree* alloctree(char *uid, char *gid, ulong mode,
void freetree(Tree *tree)
File* createfile(File *dir, char *name, char *uid,
ulong mode, void *aux)
int removefile(File *file)
void closefile(File *file)
File* walkfile(File *dir, char *path)
Readdir* opendirfile(File *dir)
long readdirfile(Readdir *rdir, uchar *buf, long n, long o)
void closedirfile(Readdir *rdir)
int hasperm(File *file, char *uid, int p)
provide an in-memory file hierarchy
intended for use in 9P file servers.
creates a new tree of files, and
The root of the tree
element in the structure)
will have mode
and be owned by user
is used when freeing
structures and is described later.
other than the root are created using
which attempts to create a file named
in the directory
If created, the file will have owner
and have a group inherited from
and the permissions of
are used to calculate the permission bits for
the file as described in
It is permissible for
to be a slash-separated path rather than a single element.
removes a file from the file tree.
The file will not be freed until the last
reference to it has been removed.
Directories may only be removed when empty.
returns zero on success, –1 on error.
It is correct to consider
with the side effect of removing the file
relative to the directory
returning the resulting file,
or zero if the named file or any intermediate element
does not exist.
pointer may be used by the client
are reference-counted: if not zero,
(specified in the call to
will be called for each file when its
last reference is removed or when the tree is freed.
should take care of any necessary cleanup related to
When creating new file references by copying pointers,
to update the reference count.
To note the removal of a reference to a file, call
return new references.
consume the passed reference.
Directories may be read, yielding a directory entry structure
for each file in the directory.
In order to allow concurrent reading of directories,
clients must obtain a
structure by calling
on a directory.
Subsequent calls to
will each yield an integral number of machine-independent
stat buffers, until end of directory.
When finished, call
to free the
does simplistic permission checking; it assumes only
one-user groups named by uid and returns non-zero if
(a bitwise-or of
9P servers written using
trees will do standard permission checks automatically;
may be called explicitly to do additional checks.
A 9P server may link against a different
implementation to provide more complex groups.
The following code correctly handles references
when elementwise walking a path and creating a file.
f = tree->root;
for(i=0; i<n && f!=nil; i++)
f = walkfile(f, elem[i]);
if(f == nil)
nf = createfile(f, "foo", "nls", 0666, nil);
The reference counting is cumbersome.