mod_musicindex  1.4.1
cache-file.c File Reference

Flat file cache management subsystem. More...

#include "playlist.h"
#include "cache-file.h"
#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <sys/file.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>

Go to the source code of this file.

Data Structures

struct  mi_data_buffer
 struct type used to reduce memory usage More...

Macros

#define CACHE_VERS   12
#define CACHE_NF   15
#define CA_OK   0
#define CA_FATAL   10
#define CA_MISSARG   20
#define CA_CREATE   30
#define CA_LOCKED   40
#define ISDOT(x)   ( (x)[0] == '.' && (!(x)[1] || ((x)[1] == '.' && !(x)[2])) )
 test for "." and ".." dir entries.
#define BLANKSTR(x)   ( !(x) ? "#" : (x) )
 if char *x is NULL, print a printable ascii char (white space would be trashed by fscanf alas)
#define ISBLANK(x)   ( ((x)[0] == '#') && ((x)[1] == '\0') )
 Detection of our "home made" encoding of NULL strings.

Functions

static void error_handler (request_rec *r, const char *const caller)
 Handles error for the flat file cache subsystem.
static short file_cache_make_dir (request_rec *r, const char *const dirpath)
 Creates cache subdirectories.
static void file_cache_remove_dir (request_rec *r, DIR *cachedir, const char *const curdir)
 Removes cache subdirectories.
static void * cache_file_opendir (request_rec *r, mu_pack *const pack, const char *const filename, const char *uri, unsigned long soptions)
 Checks if a directory already exists in the cache.
static mu_entfile_make_cache_entry (request_rec *r, apr_pool_t *pool, FILE *const in, const char *const filename)
 Fills in the information fields about a music file from the cache.
static void cache_file_write (request_rec *r, const mu_ent *const p, const char *const filename)
 Creates and writes cache file information.
int cache_file_setup (cmd_parms *cmd, const char *const setup_string, mu_config *const conf)

Variables

static const cache_backend cache_backend_file

Detailed Description

Flat file cache management subsystem.

Author
Thibaut Varene
Version
Revision:
1011
Date
2003-2010
Warning
'errno' seems not to be seen as volatile by the compiler, therefore we cannot do "if(mkdir(foo) && errno == EEXIST)" for instance.
Bug:
No flat file cache support for Solaris.
Warning
We do rely on success of chdir() calls.
Todo:
O P T I M I Z E ! (maybe)

Definition in file cache-file.c.

Macro Definition Documentation

#define BLANKSTR (   x)    ( !(x) ? "#" : (x) )

if char *x is NULL, print a printable ascii char (white space would be trashed by fscanf alas)

Definition at line 68 of file cache-file.c.

Referenced by cache_file_write().

#define CA_CREATE   30

Definition at line 62 of file cache-file.c.

#define CA_FATAL   10

Definition at line 60 of file cache-file.c.

Referenced by file_cache_make_dir().

#define CA_LOCKED   40

Definition at line 63 of file cache-file.c.

#define CA_MISSARG   20

Definition at line 61 of file cache-file.c.

#define CA_OK   0

Definition at line 59 of file cache-file.c.

Referenced by file_cache_make_dir().

#define CACHE_NF   15

Definition at line 57 of file cache-file.c.

Referenced by file_make_cache_entry().

#define CACHE_VERS   12

Definition at line 56 of file cache-file.c.

Referenced by cache_file_write(), and file_make_cache_entry().

#define ISBLANK (   x)    ( ((x)[0] == '#') && ((x)[1] == '\0') )

Detection of our "home made" encoding of NULL strings.

Definition at line 70 of file cache-file.c.

Referenced by file_make_cache_entry().

#define ISDOT (   x)    ( (x)[0] == '.' && (!(x)[1] || ((x)[1] == '.' && !(x)[2])) )

test for "." and ".." dir entries.

x is a char *

Definition at line 66 of file cache-file.c.

Referenced by file_cache_remove_dir().

Function Documentation

static void* cache_file_opendir ( request_rec *  r,
mu_pack *const  pack,
const char *const  filename,
const char *  uri,
unsigned long  soptions 
)
static

Checks if a directory already exists in the cache.

This function takes advantage of the standard behaviour of the cache backend in musicindex: this function is called when the caller wants to know whether the backend handles a given directory. We always return NULL, pretending we don't, as "handling" means being able to provide a full listing straight out of cache, which we don't in this particular incarnation, but we take the opportunity to do our own cooking with our cache data, creating the directory in cache if need be, and checking for cache data sanity.

Parameters
rApache request_rec struct.
packIgnored
filenamecurrent directory path
uriserver-side uri (unused)
soptionsFlags to use for created entries. Ignored.
Returns
NULL.

Definition at line 280 of file cache-file.c.

References mu_config::cache_setup, error_handler(), file_cache_make_dir(), file_cache_remove_dir(), musicindex_module, and unlikely.

int cache_file_setup ( cmd_parms *  cmd,
const char *const  setup_string,
mu_config *const  conf 
)
static void cache_file_write ( request_rec *  r,
const mu_ent *const  p,
const char *const  filename 
)
static

Creates and writes cache file information.

This function creates a new cache file (for #filename), and fills it with the data contained in the mu_ent p structure.

Parameters
rApache request_rec struct to handle log writings.
pA mu_ent struct containing the data to store.
filenamecurrent filename of the file to cache.

Definition at line 451 of file cache-file.c.

References mu_ent::album, mu_ent::artist, mu_ent::bitrate, BLANKSTR, mu_config::cache_setup, CACHE_VERS, mu_ent::date, EF_FLAGSTOSAVE, error_handler(), mu_ent::filetype, mu_ent::flags, mu_ent::freq, mu_ent::genre, mu_ent::length, mu_ent::mtime, musicindex_module, mu_ent::posn, mu_ent::size, mu_ent::title, and mu_ent::track.

static void error_handler ( request_rec *  r,
const char *const  caller 
)
static

Handles error for the flat file cache subsystem.

This function handles various errors depending on errno's value.

Parameters
rApache request_rec struct to handle log writings.
callerA string (eg. calling function name) used in messages sent.
Todo:
Many things.

Definition at line 82 of file cache-file.c.

References mi_rerror.

Referenced by cache_file_opendir(), cache_file_write(), file_cache_make_dir(), file_cache_remove_dir(), and file_make_cache_entry().

static short file_cache_make_dir ( request_rec *  r,
const char *const  dirpath 
)
static

Creates cache subdirectories.

This subroutine takes care of creating all requested directories and subdirectories if they don't already exist and if possible.

Warning
If dirpath begins with a "/" the function will return immediately.
Parameters
rApache request_rec struct to handle log writings.
dirpathA string representing a path to create.
Returns
CA_OK on succes, CA_FATAL otherwise.

Definition at line 167 of file cache-file.c.

References CA_FATAL, CA_OK, and error_handler().

Referenced by cache_file_opendir().

static void file_cache_remove_dir ( request_rec *  r,
DIR *  cachedir,
const char *const  curdir 
)
static

Removes cache subdirectories.

This subroutine takes care of removing any given directory and its content (recursively) if any, and if possible.

Parameters
rApache request_rec struct to handle log writings.
cachedirA DIR stream corresponding to the directory to remove.
curdirA string representing the absolute path of the corresponding parent directory on the "original" filesystem.

Definition at line 216 of file cache-file.c.

References apr_pstrcat, error_handler(), ISDOT, and unlikely.

Referenced by cache_file_opendir().

static mu_ent* file_make_cache_entry ( request_rec *  r,
apr_pool_t pool,
FILE *const  in,
const char *const  filename 
)
static

Fills in the information fields about a music file from the cache.

This function reads the tags from the cache file and fills in the struct mu_ent fields accordingly.

Warning
MUST close in file handler on success.
Parameters
rApache request_rec struct to handle log writings.
poolApache pool
inNot used (except for closing).
filenamecurrent filename.
Returns
When possible, struct mu_ent correctly set up, file stream closed, NULL otherwise.

Definition at line 345 of file cache-file.c.

References mu_ent::album, mi_data_buffer::album, apr_pstrdup, mu_ent::artist, mi_data_buffer::artist, mu_ent::bitrate, CACHE_NF, mu_config::cache_setup, CACHE_VERS, mu_ent::date, error_handler(), filetype, mu_ent::filetype, mu_ent::flags, mu_ent::freq, mu_ent::genre, mi_data_buffer::genre, ISBLANK, mu_ent::length, likely, mu_ent::mtime, musicindex_module, NEW_ENT, mu_ent::posn, mu_ent::size, mu_ent::title, mi_data_buffer::title, mu_ent::track, and unlikely.

Variable Documentation

const cache_backend cache_backend_file
static
Initial value:
{
.opendir = cache_file_opendir,
.readdir = NULL,
.closedir = NULL,
.make_entry = file_make_cache_entry,
.write = cache_file_write,
.prologue = NULL,
.epilogue = NULL,
}

Definition at line 503 of file cache-file.c.

Referenced by cache_file_setup().