mod_tile 0.9.1
Renders map tiles with Mapnik and serves them using Apache HTTP Server
Loading...
Searching...
No Matches
store_file.c File Reference
#include <errno.h>
#include <fcntl.h>
#include <glib.h>
#include <limits.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include <utime.h>
#include "g_logger.h"
#include "metatile.h"
#include "render_config.h"
#include "store.h"
#include "store_file.h"
#include "store_file_utils.h"

Functions

static int file_tile_read (struct storage_backend *store, const char *xmlconfig, const char *options, int x, int y, int z, char *buf, size_t sz, int *compressed, char *log_msg)
 Read a single sub-tile payload from an on-disk .meta archive file.
static struct stat_info file_tile_stat (struct storage_backend *store, const char *xmlconfig, const char *options, int x, int y, int z)
 Retrieve filesystem metadata and expiry status for the metatile containing (x, y, z).
static char * file_tile_storage_id (struct storage_backend *store, const char *xmlconfig, const char *options, int x, int y, int z, char *string)
 Format a human-readable file:// storage URI identifying the metatile for (x, y, z).
static int file_metatile_write (struct storage_backend *store, const char *xmlconfig, const char *options, int x, int y, int z, const char *buf, int sz)
 Atomically write a serialized metatile buffer to disk via a thread-specific temporary file.
static int file_metatile_delete (struct storage_backend *store, const char *xmlconfig, int x, int y, int z)
 Delete the on-disk .meta file containing (x, y, z).
static int file_metatile_expire (struct storage_backend *store, const char *xmlconfig, int x, int y, int z)
 Mark an on-disk .meta file as expired by backdating its modification timestamp (mtime).
static int file_close_storage (struct storage_backend *store)
 Release resources associated with a filesystem storage_backend instance.
struct storage_backend * init_storage_file (const char *tile_dir)
 Allocate and initialize a local filesystem metatile storage_backend.

Function Documentation

◆ file_close_storage()

int file_close_storage ( struct storage_backend * store)
static

Release resources associated with a filesystem storage_backend instance.

Frees the duplicated tile_dir string stored in store->storage_ctx.

Parameters
storeStorage backend instance to close.
Returns
Always returns 0.

◆ file_metatile_delete()

int file_metatile_delete ( struct storage_backend * store,
const char * xmlconfig,
int x,
int y,
int z )
static

Delete the on-disk .meta file containing (x, y, z).

Parameters
storeInitialized file storage_backend.
xmlconfigLogical map style name.
xTile X coordinate.
yTile Y coordinate.
zTile zoom level.
Returns
0 on success, or -1 on unlink() failure (with errno set).

◆ file_metatile_expire()

int file_metatile_expire ( struct storage_backend * store,
const char * xmlconfig,
int x,
int y,
int z )
static

Mark an on-disk .meta file as expired by backdating its modification timestamp (mtime).

If the .meta file exists and its mtime is after the year 2015 (tm_year > 115), subtracts 20 years from mtime (so it predates any planet import timestamp while preserving relative age information) and leaves atime untouched so LRU cache-purging tools still see accurate last-access times. Falls back to epoch 315558000 (1980-01-01) if gmtime_r() fails.

Parameters
storeInitialized file storage_backend.
xmlconfigLogical map style name.
xTile X coordinate.
yTile Y coordinate.
zTile zoom level.
Returns
Always returns 0.

◆ file_metatile_write()

int file_metatile_write ( struct storage_backend * store,
const char * xmlconfig,
const char * options,
int x,
int y,
int z,
const char * buf,
int sz )
static

Atomically write a serialized metatile buffer to disk via a thread-specific temporary file.

Creates any missing parent directories via mkdirp(), writes buf to <meta_path>.<pthread_self()>, closes the file, and atomically replaces meta_path using rename() so concurrent readers never observe partially written .meta files.

Parameters
storeInitialized file storage_backend.
xmlconfigLogical map style name.
optionsStyle parameterization options string.
xTop-left tile X coordinate of the metatile.
yTop-left tile Y coordinate of the metatile.
zTile zoom level.
bufSerialized metatile buffer (meta_layout + index table + tile payloads).
szTotal byte length of buf.
Returns
sz on success, or -1 on allocation, directory, I/O, or rename failure.

◆ file_tile_read()

int file_tile_read ( struct storage_backend * store,
const char * xmlconfig,
const char * options,
int x,
int y,
int z,
char * buf,
size_t sz,
int * compressed,
char * log_msg )
static

Read a single sub-tile payload from an on-disk .meta archive file.

Resolves (xmlconfig, options, x, y, z) to a .meta path and sub-tile slot index, reads and validates the meta_layout header and magic bytes (META_MAGIC or META_MAGIC_COMPRESSED), seeks to the sub-tile's offset, and reads its payload into buf.

Parameters
storeInitialized file storage_backend (storage_ctx holds base tile directory).
xmlconfigLogical map style name (e.g., "default").
optionsStyle parameterization options string (empty if unused).
xTile X coordinate.
yTile Y coordinate.
zTile zoom level.
bufCaller-supplied output buffer receiving the tile image bytes.
szMaximum capacity of buf in bytes.
compressedOutput flag set to 1 if the metatile uses META_MAGIC_COMPRESSED, 0 otherwise.
log_msgCaller-supplied buffer (PATH_MAX bytes) populated with an error description on failure.
Returns
Number of tile bytes read into buf (>= 0) on success, or a negative error code:
  • -1: Header allocation failure or .meta file could not be opened.
  • -2: read() error while reading the metatile header.
  • -3: File truncated / smaller than header_len.
  • -4: Invalid header magic bytes.
  • -5: Unexpected tile count (m->count != METATILE * METATILE).
  • -6: Stored tile size exceeds output buffer capacity sz.
  • -7: lseek() failure seeking to sub-tile payload offset.
  • -8: read() error while reading sub-tile payload data.

◆ file_tile_stat()

struct stat_info file_tile_stat ( struct storage_backend * store,
const char * xmlconfig,
const char * options,
int x,
int y,
int z )
static

Retrieve filesystem metadata and expiry status for the metatile containing (x, y, z).

Stats the corresponding .meta file on disk (setting size = -1 if absent) and marks expired = 1 if the file's modification time is older than the planet import timestamp returned by get_planet_time().

Parameters
storeInitialized file storage_backend.
xmlconfigLogical map style name.
optionsStyle parameterization options string.
xTile X coordinate.
yTile Y coordinate.
zTile zoom level.
Returns
Populated struct stat_info (size < 0 indicates the metatile does not exist).

◆ file_tile_storage_id()

char * file_tile_storage_id ( struct storage_backend * store,
const char * xmlconfig,
const char * options,
int x,
int y,
int z,
char * string )
static

Format a human-readable file:// storage URI identifying the metatile for (x, y, z).

Parameters
storeInitialized file storage_backend.
xmlconfigLogical map style name.
optionsStyle parameterization options string.
xTile X coordinate.
yTile Y coordinate.
zTile zoom level.
stringCaller-supplied output buffer (PATH_MAX bytes) receiving the formatted URI.
Returns
Pointer to string.

◆ init_storage_file()

struct storage_backend * init_storage_file ( const char * tile_dir)

Allocate and initialize a local filesystem metatile storage_backend.

Duplicates tile_dir into store->storage_ctx and wires all backend operation function pointers (tile_read, tile_stat, metatile_write, metatile_delete, metatile_expire, tile_storage_id, and close_storage).

Parameters
tile_dirBase directory path under which <xmlname>/<z>/.../<x>/<y>.meta files are stored.
Returns
Newly allocated struct storage_backend pointer, or NULL on allocation failure.