Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_disk_cache.c File Reference

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>
Include dependency graph for n_disk_cache.c:

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.

Detailed Description

A bounded cache of files in one directory, keyed by content.

Author
Castagnier Mickael
Version
1.0
Date
24/09/2026

Definition in file n_disk_cache.c.


Data Structure Documentation

◆ _N_DC_ENTRY

struct _N_DC_ENTRY

one entry found by a walk of the directory

Definition at line 60 of file n_disk_cache.c.

Collaboration diagram for _N_DC_ENTRY:
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

◆ _N_DC_GROUP_ENTRY

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.

Collaboration diagram for _N_DC_GROUP_ENTRY:
Data Fields
_N_DC_ENTRY entry the entry
int owner which of the caches

◆ N_DISK_CACHE

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().

Examples
ex_disk_cache.c.

Definition at line 54 of file n_disk_cache.c.

Collaboration diagram for N_DISK_CACHE:
Data Fields
char * dir the directory, without a trailing separator

Macro Definition Documentation

◆ N_DISK_CACHE_PATH_MAX

#define N_DISK_CACHE_PATH_MAX   4096

◆ N_DISK_CACHE_SEED

#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().

Function Documentation

◆ _n_dc_fopen()

FILE * _n_dc_fopen ( const char * path,
const char * mode )
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().

Here is the caller graph for this function:

◆ _n_dc_group_older()

int _n_dc_group_older ( const void * a,
const void * b )
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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _n_dc_is_dir()

int _n_dc_is_dir ( const char * path)
static

helper: is path a directory

Definition at line 140 of file n_disk_cache.c.

Referenced by n_disk_cache_open().

Here is the caller graph for this function:

◆ _n_dc_join()

int _n_dc_join ( const N_DISK_CACHE * cache,
const char * name,
char * out,
size_t out_len )
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().

Here is the caller graph for this function:

◆ _n_dc_older()

int _n_dc_older ( const void * a,
const void * b )
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().

Here is the caller graph for this function:

◆ _n_dc_owned()

int _n_dc_owned ( const char * name)
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().

Here is the caller graph for this function:

◆ _n_dc_remove()

int _n_dc_remove ( const char * path)
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().

Here is the caller graph for this function:

◆ _n_dc_replace()

int _n_dc_replace ( const char * from,
const char * to )
static

helper: rename over an existing target

Definition at line 125 of file n_disk_cache.c.

Referenced by n_disk_cache_put().

Here is the caller graph for this function:

◆ _n_dc_walk()

int _n_dc_walk ( const N_DISK_CACHE * cache,
_N_DC_ENTRY ** out )
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().

Here is the call graph for this function:
Here is the caller graph for this function: