]> git.lizzy.rs Git - plan9front.git/blob - sys/src/lib9p/ramfs.c
audiohda: fix syntax error
[plan9front.git] / sys / src / lib9p / ramfs.c
1 #include <u.h>
2 #include <libc.h>
3 #include <fcall.h>
4 #include <thread.h>
5 #include <9p.h>
6
7 static char Ebad[] = "something bad happened";
8 static char Enomem[] = "no memory";
9
10 typedef struct Ramfile  Ramfile;
11 struct Ramfile {
12         char *data;
13         int ndata;
14 };
15
16 void
17 fsread(Req *r)
18 {
19         Ramfile *rf;
20         vlong offset;
21         long count;
22
23         rf = r->fid->file->aux;
24         offset = r->ifcall.offset;
25         count = r->ifcall.count;
26
27 //print("read %ld %lld\n", *count, offset);
28         if(offset >= rf->ndata){
29                 r->ofcall.count = 0;
30                 respond(r, nil);
31                 return;
32         }
33
34         if(offset+count >= rf->ndata)
35                 count = rf->ndata - offset;
36
37         memmove(r->ofcall.data, rf->data+offset, count);
38         r->ofcall.count = count;
39         respond(r, nil);
40 }
41
42 void
43 fswrite(Req *r)
44 {
45         void *v;
46         Ramfile *rf;
47         vlong offset;
48         long count;
49
50         rf = r->fid->file->aux;
51         offset = r->ifcall.offset;
52         count = r->ifcall.count;
53
54         if(offset+count >= rf->ndata){
55                 v = realloc(rf->data, offset+count);
56                 if(v == nil){
57                         respond(r, Enomem);
58                         return;
59                 }
60                 rf->data = v;
61                 rf->ndata = offset+count;
62                 r->fid->file->length = rf->ndata;
63         }
64         memmove(rf->data+offset, r->ifcall.data, count);
65         r->ofcall.count = count;
66         respond(r, nil);
67 }
68
69 void
70 fscreate(Req *r)
71 {
72         Ramfile *rf;
73         File *f;
74
75         if(f = createfile(r->fid->file, r->ifcall.name, r->fid->uid, r->ifcall.perm, nil)){
76                 rf = emalloc9p(sizeof *rf);
77                 f->aux = rf;
78                 r->fid->file = f;
79                 r->ofcall.qid = f->qid;
80                 respond(r, nil);
81                 return;
82         }
83         respond(r, Ebad);
84 }
85
86 void
87 fsopen(Req *r)
88 {
89         Ramfile *rf;
90
91         rf = r->fid->file->aux;
92
93         if(rf && (r->ifcall.mode&OTRUNC)){
94                 rf->ndata = 0;
95                 r->fid->file->length = 0;
96         }
97
98         respond(r, nil);
99 }
100
101 void
102 fsdestroyfile(File *f)
103 {
104         Ramfile *rf;
105
106 //fprint(2, "clunk\n");
107         rf = f->aux;
108         if(rf){
109                 free(rf->data);
110                 free(rf);
111         }
112 }
113
114 Srv fs = {
115         .open=  fsopen,
116         .read=  fsread,
117         .write= fswrite,
118         .create=        fscreate,
119 };
120
121 void
122 usage(void)
123 {
124         fprint(2, "usage: ramfs [-D] [-s srvname] [-m mtpt]\n");
125         exits("usage");
126 }
127
128 void
129 main(int argc, char **argv)
130 {
131         char *addr = nil;
132         char *srvname = nil;
133         char *mtpt = nil;
134         Qid q;
135
136         fs.tree = alloctree(nil, nil, DMDIR|0777, fsdestroyfile);
137         q = fs.tree->root->qid;
138
139         ARGBEGIN{
140         case 'D':
141                 chatty9p++;
142                 break;
143         case 'a':
144                 addr = EARGF(usage());
145                 break;
146         case 's':
147                 srvname = EARGF(usage());
148                 break;
149         case 'm':
150                 mtpt = EARGF(usage());
151                 break;
152         default:
153                 usage();
154         }ARGEND;
155
156         if(argc)
157                 usage();
158
159         if(chatty9p)
160                 fprint(2, "srvname %s mtpt %s\n", srvname, mtpt);
161         if(addr == nil && srvname == nil && mtpt == nil)
162                 sysfatal("must specify -a, -s, or -m option");
163         if(addr)
164                 listensrv(&fs, addr);
165
166         if(srvname || mtpt)
167                 postmountsrv(&fs, srvname, mtpt, MREPL|MCREATE);
168         exits(0);
169 }