-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvarfs.c
More file actions
246 lines (218 loc) · 3.83 KB
/
Copy pathvarfs.c
File metadata and controls
246 lines (218 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include <u.h>
#include <libc.h>
#include <fcall.h>
#include <thread.h>
#include <9p.h>
#include "err.h"
typedef struct {
char *name;
ulong mode;
} FsFile;
typedef struct {
char *data;
char *name;
u32int ndata;
} Var;
static void delvar(Req *);
static void destroy(File *);
static void fsread(Req *);
static void fswrite(Req *);
static void newvar(Req *);
static void readvar(Req *);
static void usage(void);
static void writevar(Req *);
enum {
Qnew,
Qdel,
Qend,
};
static Srv fs = {
/*
* Variable (file) creation is achieved by writing to
* /new, hence, fscreate doesn't have any implementation
* and file variable can't be created this way.
*
* See 9p(3), for file tree handled functions.
*/
.read = fsread,
.write = fswrite,
};
static FsFile fsfile[Qend] = {
[Qnew] = { "new", 0222 },
[Qdel] = { "del", 0222 },
};
static char *mtpt;
static char *fsname;
void
threadmain(int argc, char *argv[])
{
Err *e;
File *f;
Var *v;
int i;
ARGBEGIN {
case 'm':
mtpt = ARGF();
if (mtpt == nil)
goto Usage;
break;
case 'n':
fsname = ARGF();
if (fsname == nil)
goto Usage;
break;
default:
Usage:
usage();
} ARGEND
if (fsname == nil)
fsname = "varfs";
fs.tree = alloctree(nil, nil, DMDIR|0777, destroy);
for (i = Qnew; i < Qend; i++) {
createfile(fs.tree->root, fsfile[i].name, nil, fsfile[i].mode, nil);
}
/* create command line passed variables */
e = err();
for (i = 0; i < argc; i++) {
f = createfile(fs.tree->root, argv[i], nil, 0666, nil);
if (f == nil) {
errcat(e, "could not create variable: %s\n", argv[i]);
continue;
}
v = emalloc9p(sizeof(Var));
v->name = estrdup9p(argv[i]);
v->data = nil;
f->aux = v;
}
if (e->ndata)
fprint(2, e->data);
fs.foreground = 1;
threadpostmountsrv(&fs, fsname, mtpt, MREPL | MCREATE);
}
static void
delvar(Req *r)
{
Err *e;
File *f;
char *name;
e = err();
for (name = strtok(r->ifcall.data, " \n"); name != nil;
name = strtok(NULL, " \n"))
{
f = fs.tree->root;
incref((Ref *)f);
f = walkfile(f, name);
if (f == nil) {
errcat(e, "\nno such variable: %s", name);
continue;
}
closefile(f);
if (removefile(f)) {
errcat(e, "could not delete variable: %s\n", name);
continue;
}
}
if (e->ndata) {
respond(r, e->data);
} else {
r->ofcall.count = r->ifcall.count;
respond(r, nil);
}
}
static void
destroy(File *f)
{
Var *v;
v = f->aux;
if (v != nil) {
free(v->name);
if (v->ndata)
free(v->data);
free(v);
}
}
static void
fsread(Req *r)
{
ulong path;
path = r->fid->qid.path;
switch (path) {
case Qnew:
case Qdel:
respond(r, "this file isn't supposed to be read");
break;
default:
readvar(r);
break;
}
}
static void
fswrite(Req *r)
{
ulong path;
path = r->fid->qid.path;
switch (path) {
case Qnew:
newvar(r);
break;
case Qdel:
delvar(r);
break;
default:
writevar(r);
break;
}
}
static void
newvar(Req *r)
{
Err *e;
File *f;
Var *v;
char *name;
e = err();
for (name = strtok(r->ifcall.data, " \n"); name != nil;
name = strtok(NULL, " \n"))
{
f = createfile(fs.tree->root, name, r->fid->uid, 0666, nil);
if (f == nil) {
errcat(e, "\ncould not create variable: %s", name);
continue;
}
v = emalloc9p(sizeof(Var));
v->name = estrdup9p(name);
v->data = nil;
f->aux = v;
}
if (e->ndata) {
respond(r, e->data);
} else {
r->ofcall.count = r->ifcall.count;
respond(r, nil);
}
}
static void
readvar(Req *r)
{
Var *v;
v = r->fid->file->aux;
readstr(r, v->data);
respond(r, nil);
}
static void
usage(void)
{
fprint(2, "usage: varfs [-n fsname] [-m mtpt] [var ...]\n");
threadexitsall("usage");
}
static void
writevar(Req *r)
{
Var *v;
v = r->fid->file->aux;
v->data = erealloc9p(v->data, r->ifcall.count + 1);
strcpy(v->data, r->ifcall.data);
v->ndata = r->ifcall.count;
r->ofcall.count = r->ifcall.count;
respond(r, nil);
}