blob: 80866177dae27cee4a30edd8c5ffb9d6a1aaffde [file] [log] [blame]
Tree, alloctree, freetree,
File, createfile, closefile, removefile, walkfile,
opendirfile, readdirfile, closedirfile, hasperm \- in-memory file hierarchy
.ft L
#include <u.h>
#include <libc.h>
#include <fcall.h>
#include <thread.h>
#include <9p.h>
.ft L
.ta \w'\fLFile 'u
typedef struct File
void *aux;
} File;
.ft L
.ta \w'\fLTree 'u
typedef struct Tree
File *root;
} Tree;
.ft L
.ta \w'\fLReaddir* 'u +4n +4n
Tree* alloctree(char *uid, char *gid, ulong mode,
void (*destroy)(File*))
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, char *buf, long n)
void closedirfile(Readdir *rdir)
int hasperm(File *file, char *uid, int p)
.BR File s
.BR Tree s
provide an in-memory file hierarchy
intended for use in 9P file servers.
.I Alloctree
creates a new tree of files, and
.I freetree
destroys it.
The root of the tree
(also the
.B root
element in the structure)
will have mode
.I mode
and be owned by user
.I uid
and group
.IR gid .
.I Destroy
is used when freeing
.B File
structures and is described later.
.BR File s
(including directories)
other than the root are created using
.IR createfile ,
which attempts to create a file named
.I name
in the directory
.IR dir .
If created, the file will have owner
.I uid
and have a group inherited from
the directory.
.I Mode
and the permissions of
.I dir
are used to calculate the permission bits for
the file as described in
.IR open (9p).
It is permissible for
.I name
to be a slash-separated path rather than a single element.
.I Removefile
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.
.I Removefile
returns zero on success, \-1 on error.
It is correct to consider
.I removefile
to be
.I closefile
with the side effect of removing the file
when possible.
.I Walkfile
.I path
relative to the directory
.IR dir ,
returning the resulting file,
or zero if the named file or any intermediate element
does not exist.
.B File
.B aux
pointer may be used by the client
.RB per- File
.BR File s
are reference-counted: if not zero,
.I destroy
(specified in the call to
.IR alloctree )
will be called for each file when its
last reference is removed or when the tree is freed.
.I Destroy
should take care of any necessary cleanup related to
.BR aux .
When creating new file references by copying pointers,
.I incref
.IR lock (3))
to update the reference count.
To note the removal of a reference to a file, call
.IR closefile .
.I Createfile
.I walkfile
return new references.
.IR Removefile ,
.IR closefile ,
.I walkfile
(but not
.IR createfile )
consume the passed reference.
Directories may be read, yielding a directory entry structure
.IR stat (9p))
for each file in the directory.
In order to allow concurrent reading of directories,
clients must obtain a
.B Readdir
structure by calling
.I opendirfile
on a directory.
Subsequent calls to
.I readdirfile
will each yield an integral number of machine-independent
stat buffers, until end of directory.
When finished, call
.I closedirfile
to free the
.BR Readdir .
.I Hasperm
does simplistic permission checking; it assumes only
one-user groups named by uid and returns non-zero if
.I uid
has permission
.I p
(a bitwise-or of
according to
.IB file ->mode \fR.
9P servers written using
.B File
trees will do standard permission checks automatically;
.I hasperm
may be called explicitly to do additional checks.
A 9P server may link against a different
.I hasperm
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)
return nil;
nf = createfile(f, "foo", "nls", 0666, nil);
return nf;
.B \*9/src/lib9p/file.c
.IR 9p (3)
The reference counting is cumbersome.