~amavect/whiteboardfs

whiteboardfs/whiteboardfs.c -rw-r--r-- 11.1 KiB
2d38717aamavect dryerase: fix arg parsing (thanks umbraticus) 11 months 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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
/* Amavect! */
#include <u.h>
#include <libc.h>
#include <fcall.h>
#include <thread.h>
#include <9p.h>
#include <draw.h>
#include <memdraw.h>

extern Memimage *hdrallocmemimage(char *);
extern int blockloadmemimage(Memimage *i, uchar *buf, int n, int *miny, int comp);

#define DEBUG 0

enum {
	Qroot,
	Qcanvas,
	Qupdate,
};

typedef struct F {
	char *name;
	Qid qid;
	ulong perm;
} F;

typedef struct Readq Readq;
struct Readq {
	Req *this;
	Readq *last; /* doubly linked list */
	Readq *link;
};

typedef struct Writeq Writeq;
struct Writeq {
	Rectangle r;
	uchar comp; /* is compressed */
	int miny; /* last y row written */
	Memimage *mi;
	ulong chan;
	uvlong s; /* residual write buf size */
	uvlong i; /* current buffer index */
	uchar *buf; /* residual write buffer, size bytesperline */
};

typedef struct Updateq Updateq;
struct Updateq {
	Updateq *last; /* doubly linked list */
	Updateq *link;
	u32int fid;
	uchar hasread;
	Rectangle dirty;
};

char *user;
Memimage *canvas, *diff;
char imhdr[5*12+1];
uchar *imdata;
Updateq *fqhead, *fqtail;
Readq *rqhead, *rqtail;

F root = {"/", {Qroot, 0, QTDIR}, 0555|DMDIR};
F canvasf = {"canvas.bit", {Qcanvas, 0, QTFILE}, 0666};
F updatef = {"update", {Qupdate, 0, QTFILE}, 0666};

void
usage(void)
{
	fprint(2, "%s [-D] [-m mtpt] [-s srv] [-x width] [-y height] \n", argv0);
	exits("usage");
}

ulong
memimagebytelen(Memimage *i)
{
	return Dy(i->r)*bytesperline(i->r, i->depth);
}

void
imdataupdate(void)
{
	unloadmemimage(canvas, canvas->r, imdata, memimagebytelen(canvas));
}

/* update the imhdr, reallocate imdata, and update imdata */
void
imhdrupdate(void)
{
	char ch[11];
	snprint(imhdr, sizeof(imhdr), "%11s %11d %11d %11d %11d ",
		chantostr(ch, canvas->chan), canvas->r.min.x, 
		canvas->r.min.y, canvas->r.max.x, canvas->r.max.y);
	if(imdata != nil)
		imdata = realloc(imdata, memimagebytelen(canvas));
	else
		imdata = malloc(memimagebytelen(canvas));
	imdataupdate();
}

F *
filebypath(uvlong path)
{
	if(path == Qroot)
		return &root;
	else if(path == Qcanvas)
		return &canvasf;
	else if(path == Qupdate)
		return &updatef;
	else
		return nil;
}

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

char *
fswalk1(Fid *fid, char *name, Qid *qid)
{
	if(fid->qid.path == Qroot){
		if(strcmp(name, canvasf.name) == 0){
			*qid = canvasf.qid;
			fid->qid = *qid;
			return nil;
		}else if(strcmp(name, updatef.name) == 0){
			*qid = updatef.qid;
			fid->qid = *qid;
			return nil;
		}else if(strcmp(name, "..") == 0){
			*qid = root.qid;
			fid->qid = *qid;
			return nil;
		}	
	}
	return "not found";
}

void
fillstat(Dir *d, uvlong path)
{
	F *f;
	
	f = filebypath(path);
	d->qid = f->qid;	/* unique id from server */
	d->mode = f->perm;	/* permissions */
	d->atime = time(0);	/* last read time */
	d->mtime = time(0);	/* last write time */
	d->length = 0;	/* file length */
	d->name = estrdup9p(f->name);	/* last element of path */
	d->uid = estrdup9p(user);	/* owner name */
	d->gid = estrdup9p(user);	/* group name */
	d->muid = estrdup9p(user);	/* last modifier name */
}

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

int
rootgen(int n, Dir *d, void *)
{
	switch(n){
	case 0:
		fillstat(d, canvasf.qid.path);
		break;
	case 1:
		fillstat(d, updatef.qid.path);
		break;
	default:
		return -1;
	}
	return 0;
}

void
fsopen(Req *r)
{
	Updateq *fq;
	
	if(r->fid->qid.path == Qupdate){
		fq = emalloc9p(sizeof(Updateq));
		fq->fid = r->ifcall.fid;
		fq->last = fqtail;
		if(fqtail != nil)
			fqtail->link = fq;
		else
			fqhead = fq; /* assert fqhead == nil */
		fqtail = fq;
		fq->hasread = 1;
		r->fid->aux = fq;
	}
	
	respond(r, nil);
}


void
readspond(Req *r)
{
	Updateq *fq;
	char s[1+12*4+1];
	ulong n;
	
	fq = r->fid->aux;
	
	if(r->ifcall.count < 1){
		respond(r, "buffer too small");
	}else{
		r->ofcall.data[0] = 'y'; /* draw(3) y, but not really */
		n = snprint(s, sizeof(s), " %11d %11d %11d %11d\n", 
			fq->dirty.min.x, fq->dirty.min.y, 
			fq->dirty.max.x, fq->dirty.max.y);
		if(r->ifcall.count - 1 < n)
			n = r->ifcall.count - 1;
		memcpy(r->ofcall.data + 1, s, n);
		r->ofcall.count = n+1;
		fq->hasread = 1;
		respond(r,nil);
	}
}

void
fsread(Req *r)
{
	uvlong path;
	Updateq *fq;
	Readq *rq;
	ulong c, o, n, maxoff;
	
	path = r->fid->qid.path;
	if(path == Qroot){
		dirread9p(r, rootgen, nil);
		respond(r, nil);
	}else if(path == Qcanvas){
		c = r->ifcall.count;
		o = r->ifcall.offset;
		n = 0;
		if(o < 5*12){
			n += 5*12 - o;
			if(c < n)
				n = c;
			memcpy(r->ofcall.data, imhdr + o, n);
			c -= n;
			o += n;
		}
		o -= 5*12;
		maxoff = memimagebytelen(canvas);
		if(o < maxoff && c > 0){
			if(c + o > maxoff)
				c = maxoff - o;
			memcpy(r->ofcall.data + n, imdata + o, c);
			n += c;
		}
		r->ofcall.count = n;
		respond(r, nil);
	}else if(path == Qupdate){
		/* if buffer ptr is behind, catchup, otherwise add to queue */
		fq = r->fid->aux;
		if(fq->hasread){
			rq = emalloc9p(sizeof(Readq)); /* potential cache thrasher */
			rq->this = r;
			rq->last = rqtail;
			if(rqtail != nil)
				rqtail->link = rq;
			else
				rqhead = rq; /* assert rqhead == nil */
			rqtail = rq;
			r->aux = rq;
		}else{
			readspond(r);
		}
	}else{
		respond(r, "invalid Qid path");
	}
}

void
fswrite(Req *r)
{
	Readq *rq, *next;
	int n, e, s;
	Writeq *wq;
	Updateq *uq;
	Rectangle rekt;
	
	if(r->fid->qid.path != Qcanvas){
		respond(r, "Cannot write to there!");
		return;
	}
	n = 0;
	/* basic sanitization until I add support for general image types and compression */
	wq = r->fid->aux;
	if(wq == nil){
		if(r->ifcall.count < 60){
			respond(r, "write buffer too small, cannot recover header");
			return;
		}
		wq = malloc(sizeof(Writeq));
		if(wq == nil){
			respond(r, "out of memory #1");
			return;
		}
		if(memcmp(r->ifcall.data, "compressed\n", 11) == 0){
			wq->comp = 1;
			n += 11;
		}else{
			wq->comp = 0;
		}
		wq->mi = hdrallocmemimage(r->ifcall.data + n);
		if(wq->mi == nil){
			free(wq);
			respond(r, "bad header");
			return;
		}
		if(!rectinrect(wq->mi->r, canvas->r)){
			freememimage(wq->mi);
			free(wq);
			respond(r, "image size not within canvas rectangle");
			return;
		}
		n += 60;
		wq->i = 0;
		if(wq->comp)
			wq->s = 6025;
		else
			wq->s = bytesperline(wq->mi->r, wq->mi->depth); /* residual buffer size */
		wq->buf = malloc(wq->s); /* residual buffer for uncompressed writes */
		if(wq->buf == nil){
			freememimage(wq->mi);
			free(wq);
			respond(r, "out of memory #2");
			return;
		}
		wq->miny = wq->mi->r.min.y;
		r->fid->aux = wq;
	}
	
	e = 1;
	while(n < r->ifcall.count && e > 0){
		if(DEBUG) fprint(2, "start: n=%d e=%d wqi=%ulld\n", n, e, wq->i);
		if(wq->i > 0){
			s = r->ifcall.count - n < wq->s - wq->i ? r->ifcall.count - n : wq->s - wq->i;
			memcpy(wq->buf + wq->i, r->ifcall.data, s);
			wq->i += s;
			e = blockloadmemimage(wq->mi, wq->buf, wq->i, &wq->miny, wq->comp);
			wq->i = 0;
		}else{
			e = blockloadmemimage(wq->mi, (uchar*)r->ifcall.data+n, r->ifcall.count, &wq->miny, wq->comp);
		}
		if(e < 0){
			freememimage(wq->mi);
			free(wq->buf);
			free(wq);
			r->fid->aux = nil;
			fprint(2, "%r\n");
			respond(r, "error loading image. deleting write request");
			return;
		}
		n += e;
		if(DEBUG) fprint(2, "end: n=%d e=%d wqi=%ulld\n", n, e, wq->i);
	}
	if(n < r->ifcall.count){
		wq->i = r->ifcall.count - n;
		if(wq->i > wq->s)
			sysfatal("Why would that be bigger? %ulld", wq->i);
		memcpy(wq->buf, r->ifcall.data+n, wq->i);
	}
	if(wq->miny == wq->mi->r.max.y && wq->i != 0){
		freememimage(wq->mi);
		free(wq->buf);
		free(wq);
		r->fid->aux = nil;
		respond(r, "too much data sent. deleting write request");
		return;
	}
	r->ofcall.count = r->ifcall.count;
	if(wq->miny < wq->mi->r.max.y){
		respond(r, nil);
		return;
	}else if(wq->miny > wq->mi->r.max.y){
		/* assert cannot happen */
		fprint(2, "write size error: %d > %d", wq->miny, wq->mi->r.max.y);
		freememimage(wq->mi);
		free(wq->buf);
		free(wq);
		r->fid->aux = nil;
		respond(r, "image write too large, somehow. deleting write request");
		return;
	}
	if(DEBUG) fprint(2, "successful image write\n");
	/* Successful image write. composite, update, and notify. */
	/*
	 * Currently: wq->chan == diff->chan && wq->r == diff->r
	 * To support different image write sizes, a new Memimage will
	 * need to be allocated.
	 */
	memimagedraw(canvas, canvas->r, wq->mi, ZP, nil, ZP, SoverD);
	rekt = wq->mi->r;
	freememimage(wq->mi);
	free(wq->buf);
	free(wq);
	r->fid->aux = nil;
	respond(r,nil);
	imdataupdate();
	/* 
	 * invalidate all updateqs
	 * expand dirty rectangle
	 */
	for(uq = fqhead; uq != nil; uq = uq->link){
		if(uq->hasread == 1){
			uq->dirty = rekt;
			uq->hasread = 0;
		}else{
			combinerect(&uq->dirty, rekt);
		}
	}
	/* respond to all waiting reads */
	for(rq = rqhead; rq != nil; rq = next){
		/* respond() calls destroyreq, which frees this rq */
		next = rq->link;
		readspond(rq->this);
	}
	rqhead = nil;
	rqtail = nil;
}

void
fsflush(Req *r)
{
	Readq *rq;
	for(rq = rqhead; rq != nil; rq = rq->link){
		if(r->ifcall.oldtag == rq->this->tag){
			respond(rq->this, "interrupted");
			if(rq->last != nil)
				rq->last->link = rq->link;
			else
				rqhead = rq->link;
			if(rq->link != nil)
				rq->link->last = rq->last;
			else
				rqtail = rq->last;
			respond(r, nil);
			return;
		}
	}
	respond(r, "invalid tag");
}

void
fsdestroyfid(Fid *fid)
{
	Updateq *fq;
	Writeq *wq;
	
	if(fid->aux == nil)
		return;
	if(fid->qid.path == Qupdate){
		fq = fid->aux;
		/* remove self from DLL */
		if(fq->last == nil){
			fqhead = fq->link;
		}else{
			fq->last->link = fq->link;
		}
		if(fq->link == nil){
			fqtail = fq->last;
		}else{
			fq->link->last = fq->last;
		}
		
		free(fq);
		fid->aux = nil;
	}else if(fid->qid.path == Qcanvas){
		wq = fid->aux;
		freememimage(wq->mi);
		free(wq->buf);
		free(wq);
		fid->aux = nil;
	}else{ /* assert this branch cannot happen */
		sysfatal("unknown dangling fid->aux, qid=%ulld", fid->qid.path);
	}
}

void
fsdestroyreq(Req *r)
{
	Readq *rq;
	
	if(r->aux == nil)
		return;
	rq = r->aux;
	/* remove self from DLL */
	if(rq->last == nil){
		rqhead = rq->link;
	}else{
		rq->last->link = rq->link;
	}
	if(rq->link == nil){
		rqtail = rq->last;
	}else{
		rq->link->last = rq->last;
	}
	
	free(rq);
	r->aux = nil;
}

Srv fs = {
	.attach = fsattach,
	.walk1 = fswalk1,
	.stat = fsstat,
	.open = fsopen,
	.read = fsread,
	.write = fswrite,
	.flush = fsflush,
	.destroyfid = fsdestroyfid,
	.destroyreq = fsdestroyreq,
};

void
main(int argc, char *argv[])
{
	char *mtpt, *srv;
	int x, y;
	
	mtpt = "/mnt/whiteboard";
	srv = nil;
	x = 512;
	y = 512;
	ARGBEGIN{
	case 'm':
		mtpt = EARGF(usage());
		break;
	case 's':
		srv = EARGF(usage());
		break;
	case 'x':
		x = strtol(EARGF(usage()), nil, 0);
		break;
	case 'y':
		y = strtol(EARGF(usage()), nil, 0);
		break;
	case 'D':
		chatty9p++;
		break;
	default:
		usage();
	}ARGEND;
	
	if(x <= 1)
		exits("x too small");
	if(x >= 1024) /* don't break the network */
		exits("x too big");
	if(y <= 1)
		exits("y too small");
	if(y >= 1024)
		exits("y too big");
	
	if(memimageinit() < 0)
		sysfatal("%r");
	canvas = allocmemimage(Rect(0,0,x,y), RGB24);
	memfillcolor(canvas, 0xA0A0A0FF);
	imhdrupdate();
	diff = allocmemimage(Rect(0,0,x,y), RGBA32);
	
	user = getuser();
	postmountsrv(&fs, srv, mtpt, MREPL);
	
	exits(nil);
}