~foura/midifs

midifs/main.c -rw-r--r-- 2.5 KiB
2db95d1fjames palmer start overhaul of midi parsing a month ago
                                                                                
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
#include <u.h>
#include <libc.h>
#include <thread.h>
#include <fcall.h>
#include <9p.h>

#include "midi.h"

enum {
	Qroot,
		Qctl,
		Qmidi,
		Qraw,
};

typedef struct Dirtab Dirtab;
struct Dirtab {
	char *name;
	Qid qid;
	ulong mode;
};

Dirtab root = { "/", {Qroot, 0, QTDIR}, 0555|DMDIR };
Dirtab roottab[] = {
	"ctl",  {Qctl,  0, QTFILE}, 0444,
	"midi", {Qmidi, 0, QTFILE}, 0444,
	"raw",  {Qraw,  0, QTFILE}, 0444,
};

Dirtab*
filebypath(uvlong path)
{
	int i;

	if(path == Qroot)
		return &root;
	for(i = 0; i < nelem(roottab); i++)
		if(path == roottab[i].qid.path)
			return &roottab[i];

	return nil;
}

void
fillstat(Dir *d, uvlong path, char *user)
{
	Dirtab *f;

	f = filebypath(path);

	d->name = estrdup9p(f->name);
	d->qid = f->qid;
	d->mode = f->mode;
	d->uid = estrdup9p(user);
	d->gid = estrdup9p(user);
	d->muid = estrdup9p(user);
	d->length = 0;
	d->atime = time(0);
	d->mtime = time(0);
}

int
rootgen(int n, Dir *d, void *aux)
{
	Req *r = aux;
	
	if(n >= nelem(roottab))
		return -1;
	fillstat(d, roottab[n].qid.path, r->fid->uid);
	return 0;
}

void
fsattach(Req *r)
{
	r->fid->qid = filebypath(Qroot)->qid;
	r->ofcall.qid = r->fid->qid;
	respond(r, nil);
}

void
fsread(Req *r)
{
	uvlong path = r->fid->qid.path;
	
	switch(path) {
	case Qroot:
		dirread9p(r, rootgen, r);
		respond(r, nil);
		return;
	}

	respond(r, "not implemented");
}

void
fswrite(Req *r)
{
	uvlong path = r->fid->qid.path;

	switch(path) {
	};

	respond(r, "not implemented");
}

void
fsstat(Req *r)
{
	fillstat(&r->d, r->fid->qid.path, r->fid->uid);
	respond(r, nil);
	return;
}

char *
fswalk1(Fid *fid, char *name, Qid *qid)
{
	int i;

	switch(fid->qid.path) {
	case Qroot:
		for(i = 0; i <= nelem(roottab); i++) {
			if(strcmp(roottab[i].name, name) == 0) {
				*qid = roottab[i].qid;
				fid->qid = *qid;
				return nil;
			}
		}

		if(strcmp("..", name) == 0) {
			*qid = root.qid;
			fid->qid = *qid;
			return nil;
		}

		break;
	}

	return "not found";
}

Srv fs = {
	.attach = fsattach,
	.read = fsread,
	.write = fswrite,
	.stat = fsstat,
	.walk1 = fswalk1,
};

void
usage(void)
{
	fprint(2, "usage: %s [-Dp] [-m mtpt] [-s srvn] device\n", argv0);	
	exits("usage");
}


void
main(int argc, char *argv[])
{
	int pipe = 0;
	char *mtpt = "/mnt/midi";
	char *srvn = nil;

	ARGBEGIN {
	case 'p':
		pipe = 1;
		break;
	case 'm':
		mtpt = EARGF(usage());
		break;
	case 's':
		srvn = EARGF(usage());
		break;
	case 'D':
		chatty9p++;
		break;
	default:
		usage();
	} ARGEND

	if(argc < 1)
		usage();

	USED(pipe);
	postmountsrv(&fs, srvn, mtpt, MREPL);
	exits(nil);
}