ref: 1885f38f890e6e85d0dd53f07b9c4f4b02c25073
dir: /mapfs.c/
#include <u.h>
#include <libc.h>
#include <auth.h>
#include <fcall.h>
#include <thread.h>
#include <9p.h>
#include "dat.h"
#include "fns.h"
void
usage(void)
{
fprint(2, "usage: %s [-s srvname] [-m mtpt] [-c cachedir] [-z maxzoom]\n", argv0);
exits("usage");
}
char *mtpt = "/mnt/map";
char *cache = "/tmp/mapcache";
char *uid;
int maxzoom = 8;
void
fsread(Req *r)
{
Bundle *b;
char *f;
int fd;
if (!r->fid->file->aux) {
respond(r, "invalid file");
return;
}
b = (Bundle*)r->fid->file->aux;
f = smprint("%s/%d/%d/%d", cache, b->z, b->x, b->y);
if (!f)
sysfatal("%r");
if (access(f, AEXIST) < 0)
requestfile(r->fid->file, b, f);
fd = open(f, OREAD);
free(f);
if (fd < 0) {
responderror(r);
return;
}
r->ofcall.count = pread(fd, r->ofcall.data, r->ifcall.count, r->ifcall.offset);
close(fd);
respond(r, nil);
}
void
fsremove(Req *r)
{
Bundle *b;
char *f;
if (!r->fid->file->aux) {
r->fid->file = nil;
respond(r, nil);
return;
}
b = (Bundle*)r->fid->file->aux;
f = smprint("%s/%d/%d/%d", cache, b->z, b->x, b->y);
if (!f)
sysfatal("%r");
if (access(f, AEXIST) < 0) {
respond(r, nil);
return;
}
remove(f);
r->fid->file->length = 0;
r->fid->file = nil;
respond(r, nil);
return;
}
Srv fs = {
.read = fsread,
.remove = fsremove,
};
void
main(int argc, char **argv)
{
char *srv = nil;
ARGBEGIN{
case 's':
srv = EARGF(usage());
break;
case 'm':
mtpt = EARGF(usage());
break;
case 'c':
cache = EARGF(usage());
break;
case 'z':
maxzoom = atoi(EARGF(usage()));
break;
}ARGEND;
uid = getuser();
fs.tree = alloctree(nil, nil, DMDIR|0777, nil);
inittree(fs.tree->root);
postmountsrv(&fs, srv, mtpt, MREPL|MCREATE);
}