blob: 80866177dae27cee4a30edd8c5ffb9d6a1aaffde [file] [log] [blame]
rsc39405062005-01-13 04:56:07 +00001.TH 9P-FILE 3
2.SH NAME
3Tree, alloctree, freetree,
4File, createfile, closefile, removefile, walkfile,
5opendirfile, readdirfile, closedirfile, hasperm \- in-memory file hierarchy
6.SH SYNOPSIS
7.ft L
8.nf
9#include <u.h>
10#include <libc.h>
11#include <fcall.h>
12#include <thread.h>
13#include <9p.h>
14.fi
15.PP
16.ft L
17.nf
18.ta \w'\fLFile 'u
19typedef struct File
20{
21 Ref;
22 Dir;
23 void *aux;
24 \fI...\fP
25} File;
26.fi
27.PP
28.ft L
29.nf
30.ta \w'\fLTree 'u
31typedef struct Tree
32{
33 File *root;
34 \fI...\fP
35} Tree;
36.fi
37.PP
38.ft L
39.nf
40.ta \w'\fLReaddir* 'u +4n +4n
41Tree* alloctree(char *uid, char *gid, ulong mode,
42 void (*destroy)(File*))
43void freetree(Tree *tree)
44File* createfile(File *dir, char *name, char *uid,
45 ulong mode, void *aux)
46int removefile(File *file)
47void closefile(File *file)
48File* walkfile(File *dir, char *path)
49Readdir* opendirfile(File *dir)
50long readdirfile(Readdir *rdir, char *buf, long n)
51void closedirfile(Readdir *rdir)
52int hasperm(File *file, char *uid, int p)
53.fi
54.SH DESCRIPTION
55.BR File s
56and
57.BR Tree s
58provide an in-memory file hierarchy
59intended for use in 9P file servers.
60.PP
61.I Alloctree
62creates a new tree of files, and
63.I freetree
64destroys it.
65The root of the tree
66(also the
67.B root
68element in the structure)
69will have mode
70.I mode
71and be owned by user
72.I uid
73and group
74.IR gid .
75.I Destroy
76is used when freeing
77.B File
78structures and is described later.
79.PP
80.BR File s
81(including directories)
82other than the root are created using
83.IR createfile ,
84which attempts to create a file named
85.I name
86in the directory
87.IR dir .
88If created, the file will have owner
89.I uid
90and have a group inherited from
91the directory.
92.I Mode
93and the permissions of
94.I dir
95are used to calculate the permission bits for
96the file as described in
97.IR open (9p).
98It is permissible for
99.I name
100to be a slash-separated path rather than a single element.
101.PP
102.I Removefile
103removes a file from the file tree.
104The file will not be freed until the last
105reference to it has been removed.
106Directories may only be removed when empty.
107.I Removefile
108returns zero on success, \-1 on error.
109It is correct to consider
110.I removefile
111to be
112.I closefile
113with the side effect of removing the file
114when possible.
115.PP
116.I Walkfile
117evaluates
118.I path
119relative to the directory
120.IR dir ,
121returning the resulting file,
122or zero if the named file or any intermediate element
123does not exist.
124.PP
125The
126.B File
127structure's
128.B aux
129pointer may be used by the client
130for
131.RB per- File
132storage.
133.BR File s
134are reference-counted: if not zero,
135.I destroy
136(specified in the call to
137.IR alloctree )
138will be called for each file when its
139last reference is removed or when the tree is freed.
140.I Destroy
141should take care of any necessary cleanup related to
142.BR aux .
143When creating new file references by copying pointers,
144call
145.I incref
146(see
147.IR lock (3))
148to update the reference count.
149To note the removal of a reference to a file, call
150.IR closefile .
151.I Createfile
152and
153.I walkfile
154return new references.
155.IR Removefile ,
156.IR closefile ,
157and
158.I walkfile
159(but not
160.IR createfile )
161consume the passed reference.
162.PP
163Directories may be read, yielding a directory entry structure
164(see
165.IR stat (9p))
166for each file in the directory.
167In order to allow concurrent reading of directories,
168clients must obtain a
169.B Readdir
170structure by calling
171.I opendirfile
172on a directory.
173Subsequent calls to
174.I readdirfile
175will each yield an integral number of machine-independent
176stat buffers, until end of directory.
177When finished, call
178.I closedirfile
179to free the
180.BR Readdir .
181.PP
182.I Hasperm
183does simplistic permission checking; it assumes only
184one-user groups named by uid and returns non-zero if
185.I uid
186has permission
187.I p
188(a bitwise-or of
189.BR AREAD ,
190.BR AWRITE
191and
192.BR AEXEC )
193according to
194.IB file ->mode \fR.
1959P servers written using
196.B File
197trees will do standard permission checks automatically;
198.I hasperm
199may be called explicitly to do additional checks.
200A 9P server may link against a different
201.I hasperm
202implementation to provide more complex groups.
203.SH EXAMPLE
204The following code correctly handles references
205when elementwise walking a path and creating a file.
206.IP
207.EX
208f = tree->root;
209incref(f);
210for(i=0; i<n && f!=nil; i++)
211 f = walkfile(f, elem[i]);
212if(f == nil)
213 return nil;
214nf = createfile(f, "foo", "nls", 0666, nil);
215closefile(f);
216return nf;
217.EE
218.SH SOURCE
219.B \*9/src/lib9p/file.c
220.SH SEE ALSO
221.IR 9p (3)
222.SH BUGS
223The reference counting is cumbersome.