commit cef88c8da511cdb97cdd37250514bf63bdc8c942
parent a486e2eaaabcd0dfc7a41346af4be1a5522629f9
Author: Tomas Hlavaty <tom@logand.com>
Date: Fri, 13 Sep 2019 00:50:30 +0200
introduce osmtile-download
Diffstat:
2 files changed, 21 insertions(+), 28 deletions(-)
diff --git a/osmtile-download b/osmtile-download
@@ -0,0 +1,7 @@
+#!/usr/bin/env bash
+set -euo pipefail
+zxy=$1
+dir=$2
+url=https://tile.openstreetmap.org/
+mkdir -p $dir/$(dirname $zxy)
+exec flock -n -F $dir/$zxy.png.lock curl -s -o $dir/$zxy.png $url/$zxy.png
diff --git a/osmtile.c b/osmtile.c
@@ -106,43 +106,28 @@ static int probe(char *path) {
return 0;
}
-static void run(char *args[]) {
- int z = fork();
- if(z < 0) fail(1, "fork failed\n");
- if(z) { /* parent */
- int stat;
- wait(&stat);
- if(stat) fail(1, "%s failed %d\n", *args, stat);
- return;
- }
- /* child */
- execvp(*args, args);
- fail(1, "exec %s failed\n", *args);
-}
-
static int cached_file(int z, int x, int y, char *dir, char file[4096]) {
int max = (1 << z) - 1;
if(x < 0) x = max;
if(y < 0) y = max;
if(max < x) x = 0;
if(max < y) y = 0;
- char d[4096];
- int n = snprintf(d, sizeof(d), "%s/%d/%d", dir, z, x);
- if(n < 0 || sizeof(d) < n) fail(1, "directory path error\n");
- n = snprintf(file, 4096, "%s/%d.png", d, y);
+ char zxy[4096];
+ int n = snprintf(zxy, sizeof(zxy), "%d/%d/%d", z, x, y);
+ if(n < 0 || sizeof(zxy) < n) fail(1, "zxy error\n");
+ n = snprintf(file, 4096, "%s/%s.png", dir, zxy);
if(n < 0 || 4096 < n) fail(1, "file path error\n");
if(probe(file)) return 1;
- {
- char url[4096];
- int n = snprintf(url, sizeof(url), "https://tile.openstreetmap.org/%d/%d/%d.png", z, x, y);
- if(n < 0 || sizeof(url) < n) fail(1, "url error\n");
- char *args1[] = {"mkdir", "-p", d, NULL};
- run(args1);
- char *args2[] = {"curl", "-s", "-o", file, url, NULL};
- run(args2);
- if(!probe(file)) fail(1, "cached file '%s' not found\n", file);
+ /* download tile in background */
+ int pid = fork();
+ if(pid < 0) fail(1, "fork failed\n");
+ if(pid) { /* parent */
+ return 0;
}
- return 1;
+ /* child */
+ char *args[] = {"osmtile-download", zxy, dir, NULL};
+ execvp(*args, args);
+ fail(1, "exec %s failed\n", *args);
}
static void screen_size(int *w, int *h) {
@@ -190,6 +175,7 @@ static int main_tty(int argc, char *argv[]) {
double a = a0, o = o0;
int x = long2tilex(o, z);
int y = lat2tiley(a, z);
+ signal(SIGCHLD, SIG_IGN);
for(;;) {
FILE *p = popen("w3mimgdisplay", "w");
if(!p) fail(1, "popen w3mimgdisplay failed\n");