![]() |
Nilorea Library
C utilities for networking, threading, graphics
|
A bounded cache of files in one directory, keyed by content. More...
#include <errno.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <time.h>#include "nilorea/n_disk_cache.h"#include "nilorea/n_hash.h"#include "nilorea/n_log.h"#include <dirent.h>#include <sys/stat.h>#include <unistd.h>Go to the source code of this file.
Data Structures | |
| struct | _N_DC_ENTRY |
| one entry found by a walk of the directory More... | |
| struct | _N_DC_GROUP_ENTRY |
| one entry of a group prune: the entry and the cache it is in More... | |
| struct | N_DISK_CACHE |
| a cache: only where it is More... | |
Macros | |
| #define | N_DISK_CACHE_PATH_MAX 4096 |
| longest path this module builds | |
| #define | N_DISK_CACHE_SEED 0x4E444331u |
| seed of the key hash. | |
Functions | |
| static FILE * | _n_dc_fopen (const char *path, const char *mode) |
| helper: fopen a UTF-8 path | |
| static int | _n_dc_group_older (const void *a, const void *b) |
| qsort order for a group: least recently used first | |
| static int | _n_dc_is_dir (const char *path) |
helper: is path a directory | |
| static int | _n_dc_join (const N_DISK_CACHE *cache, const char *name, char *out, size_t out_len) |
| helper: join the directory and a file name | |
| static int | _n_dc_older (const void *a, const void *b) |
| qsort order: least recently used first | |
| static int | _n_dc_owned (const char *name) |
helper: is name one this module could have made: 32 lowercase hex digits, then the end or a '. | |
| static int | _n_dc_remove (const char *path) |
| helper: remove a UTF-8 path. | |
| static int | _n_dc_replace (const char *from, const char *to) |
| helper: rename over an existing target | |
| static int | _n_dc_walk (const N_DISK_CACHE *cache, _N_DC_ENTRY **out) |
| helper: every entry of the cache, in a new array the caller frees. | |
| void | n_disk_cache_close (N_DISK_CACHE **cache) |
| Forget a cache. | |
| const char * | n_disk_cache_dir (const N_DISK_CACHE *cache) |
| The cache's directory, as it was opened. | |
| int | n_disk_cache_expire (const N_DISK_CACHE *cache, int64_t max_age_seconds) |
| Remove every entry unused for longer than an age. | |
| int | n_disk_cache_get (const N_DISK_CACHE *cache, const void *key, size_t key_len, const char *ext, size_t max_len, unsigned char **data, size_t *len) |
| Read an entry. | |
| N_DISK_CACHE * | n_disk_cache_open (const char *dir) |
| Use a directory as a cache. | |
| int | n_disk_cache_path (const N_DISK_CACHE *cache, const void *key, size_t key_len, const char *ext, char *out, size_t out_len) |
| The file an entry lives in, whether or not it exists. | |
| int | n_disk_cache_prune (const N_DISK_CACHE *cache, uint64_t max_bytes) |
| Remove least recently used entries until the rest fit a budget. | |
| int | n_disk_cache_prune_group (const N_DISK_CACHE *const *caches, int count, uint64_t max_bytes) |
| n_disk_cache_prune() over several caches that share one budget. | |
| int | n_disk_cache_put (const N_DISK_CACHE *cache, const void *key, size_t key_len, const char *ext, const void *data, size_t len) |
| Store an entry, replacing any under the same key and extension. | |
| int | n_disk_cache_remove (const N_DISK_CACHE *cache, const void *key, size_t key_len, const char *ext) |
| Remove one entry, for a caller that found it unreadable. | |
| int | n_disk_cache_size (const N_DISK_CACHE *cache, uint64_t *total) |
| How many bytes the cache's entries take. | |
A bounded cache of files in one directory, keyed by content.
Definition in file n_disk_cache.c.
| struct _N_DC_ENTRY |
one entry found by a walk of the directory
Definition at line 60 of file n_disk_cache.c.
| Data Fields | ||
|---|---|---|
| char | name[32+64] | file name, an entry's by construction |
| uint64_t | size | size in bytes |
| int64_t | used | last used: access time, else write time |
| struct _N_DC_GROUP_ENTRY |
one entry of a group prune: the entry and the cache it is in
Definition at line 440 of file n_disk_cache.c.
| Data Fields | ||
|---|---|---|
| _N_DC_ENTRY | entry | the entry |
| int | owner | which of the caches |
| struct N_DISK_CACHE |
a cache: only where it is
a cache directory.
Opaque: make it with n_disk_cache_open(), release it with n_disk_cache_close().
Definition at line 54 of file n_disk_cache.c.
| Data Fields | ||
|---|---|---|
| char * | dir | the directory, without a trailing separator |
| #define N_DISK_CACHE_PATH_MAX 4096 |
longest path this module builds
Definition at line 51 of file n_disk_cache.c.
Referenced by _n_dc_walk(), n_disk_cache_expire(), n_disk_cache_get(), n_disk_cache_open(), n_disk_cache_prune(), n_disk_cache_prune_group(), n_disk_cache_put(), and n_disk_cache_remove().
| #define N_DISK_CACHE_SEED 0x4E444331u |
seed of the key hash.
Fixed, so a cache written by one build is read by the next.
Definition at line 48 of file n_disk_cache.c.
Referenced by n_disk_cache_path().
|
static |
helper: fopen a UTF-8 path
Definition at line 96 of file n_disk_cache.c.
References mode.
Referenced by n_disk_cache_get(), and n_disk_cache_put().
|
static |
qsort order for a group: least recently used first
Definition at line 448 of file n_disk_cache.c.
References _n_dc_older().
Referenced by n_disk_cache_prune_group().
|
static |
helper: is path a directory
Definition at line 140 of file n_disk_cache.c.
Referenced by n_disk_cache_open().
|
static |
helper: join the directory and a file name
Definition at line 168 of file n_disk_cache.c.
References N_DISK_CACHE::dir.
Referenced by _n_dc_walk(), n_disk_cache_expire(), n_disk_cache_path(), n_disk_cache_prune(), and n_disk_cache_prune_group().
|
static |
qsort order: least recently used first
Definition at line 265 of file n_disk_cache.c.
Referenced by _n_dc_group_older(), and n_disk_cache_prune().
|
static |
helper: is name one this module could have made: 32 lowercase hex digits, then the end or a '.
'
Definition at line 156 of file n_disk_cache.c.
References N_DISK_CACHE_NAME_LEN.
Referenced by _n_dc_walk().
|
static |
helper: remove a UTF-8 path.
TRUE when it is gone or was never there.
Definition at line 111 of file n_disk_cache.c.
Referenced by n_disk_cache_expire(), n_disk_cache_prune(), n_disk_cache_prune_group(), n_disk_cache_put(), and n_disk_cache_remove().
|
static |
helper: rename over an existing target
Definition at line 125 of file n_disk_cache.c.
Referenced by n_disk_cache_put().
|
static |
helper: every entry of the cache, in a new array the caller frees.
Returns the count, or -1 when the directory cannot be read.
Definition at line 176 of file n_disk_cache.c.
References _n_dc_join(), _n_dc_owned(), N_DISK_CACHE::dir, N_DISK_CACHE_NAME_LEN, N_DISK_CACHE_PATH_MAX, _N_DC_ENTRY::size, and _N_DC_ENTRY::used.
Referenced by n_disk_cache_expire(), n_disk_cache_prune(), n_disk_cache_prune_group(), and n_disk_cache_size().