shithub: mapfs

ref: 5390510d0ae960fae9a0a9039bb9da3d1f4f969c
dir: /cache.c/

View raw version
#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 $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)
		;
}