source: subversion/applications/utils/mod_tile/dir_utils.c @ 6744

Last change on this file since 6744 was 6571, checked in by jonb, 12 years ago

mod_tile: Implement meta-tiles. Add speed test and tool to allow bulk rendering of tile list from stdin

File size: 2.2 KB
Line 
1#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <limits.h>
5#include <string.h>
6#include <sys/types.h>
7#include <sys/stat.h>
8
9
10#include "protocol.h"
11#include "render_config.h"
12#include "dir_utils.h"
13
14// Build parent directories for the specified file name
15// Note: the part following the trailing / is ignored
16// e.g. mkdirp("/a/b/foo.png") == shell mkdir -p /a/b
17int mkdirp(const char *path) {
18    struct stat s;
19    char tmp[PATH_MAX];
20    char *p;
21
22    strncpy(tmp, path, sizeof(tmp));
23
24    // Look for parent directory
25    p = strrchr(tmp, '/');
26    if (!p)
27        return 0;
28
29    *p = '\0';
30
31    if (!stat(tmp, &s))
32        return !S_ISDIR(s.st_mode);
33    *p = '/';
34    // Walk up the path making sure each element is a directory
35    p = tmp;
36    if (!*p)
37        return 0;
38    p++; // Ignore leading /
39    while (*p) {
40        if (*p == '/') {
41            *p = '\0';
42            if (!stat(tmp, &s)) {
43                if (!S_ISDIR(s.st_mode))
44                    return 1;
45            } else if (mkdir(tmp, 0777))
46                return 1;
47            *p = '/';
48        }
49        p++;
50    }
51    return 0;
52}
53
54
55
56/* File path hashing. Used by both mod_tile and render daemon
57 * The two must both agree on the file layout for meta-tiling
58 * to work
59 */
60
61const char *xyz_to_path(char *path, size_t len, int x, int y, int z)
62{
63#ifdef DIRECTORY_HASH
64    // Directory hashing is optimised for z18 max (2^18 tiles split into 2 levels of 2^9)
65    //snprintf(path, PATH_MAX, WWW_ROOT HASH_PATH "/%d/%03d/%03d/%03d/%03d.png", z,
66    //                 x/512 x%512, y/512, y%512);
67
68    // We attempt to cluseter the tiles so that a 16x16 square of tiles will be in a single directory
69    // Hash stores our 40 bit result of mixing the 20 bits of the x & y co-ordinates
70    // 4 bits of x & y are used per byte of output
71    unsigned char i, hash[5];
72
73    for (i=0; i<5; i++) {
74        hash[i] = ((x & 0x0f) << 4) | (y & 0x0f);
75        x >>= 4;
76        y >>= 4;
77    }
78    snprintf(path, PATH_MAX, WWW_ROOT HASH_PATH "/%d/%u/%u/%u/%u/%u.png", z, hash[4], hash[3], hash[2], hash[1], hash[0]);
79#else
80    snprintf(path, PATH_MAX, WWW_ROOT TILE_PATH "/%d/%d/%d.png", z, x, y);
81#endif
82    return path + strlen(WWW_ROOT);
83}
Note: See TracBrowser for help on using the repository browser.