ref: 03be0170dc8eb63834dd43db946a7012dd3fa286
dir: /cache.c/
#include <u.h>
#include <libc.h>
#include <fcall.h>
#include <thread.h>
#include <9p.h>
#include "dat.h"
#include "fns.h"
void
download(Bundle b, char *file)
{
char *url;
url = smprint("https://tile.openstreetmap.org/%d/%d/%d.png", b.z, b.x, b.y);
putenv("tileurl", url);
free(url);
putenv("tilefile", file);
char *cmd =
"mkdir -p `{basename -d $tilefile} && "
"hget -r 'User-Agent: mapfs (Plan 9)' $tileurl > /tmp/tmptile.png && "
"png -t9 /tmp/tmptile.png > $tilefile && "
"rm /tmp/tmptile.png";
execl("/bin/rc", "rc", "-c", cmd, nil);
}
void
requestfile(Bundle b, char *dest)
{
int p[2];
int pid;
int mod;
mod = 1;
for (int i = 0; i < b.z; i++)
mod *= 2;
b.x = b.x % mod;
b.y = b.y % mod;
switch (pid = fork()) {
case -1:
sysfatal("fork: %r");
default:
close(p[1]);
break;
case 0:
download(b, dest);
sysfatal("download: %r");
}
while (waitpid() != pid)
;
}