httpdirfs/src/cache.h

106 lines
2.8 KiB
C
Raw Normal View History

#ifndef CACHE_H
#define CACHE_H
#include "link.h"
#include <pthread.h>
#include <stdint.h>
/**
* \file cache.h
* \brief cache related structures and functions
* \details
* - We store the metadata and the actual data separately in two
* separate folders.
*/
2019-04-12 13:41:07 +02:00
/**
* \brief Type definition for a cache segment
2019-04-12 13:41:07 +02:00
*/
typedef uint8_t Seg;
2019-04-12 13:41:07 +02:00
/**
2019-04-20 09:31:16 +02:00
* \brief cache in-memory data structure
*/
2019-04-12 13:41:07 +02:00
typedef struct {
char *path; /**< the path to the file on the web server */
long time; /**<the modified time of the file */
off_t content_length; /**<the size of the file */
pthread_mutex_t rw_lock; /**< mutex for disk operation */
pthread_mutex_t meta_lock; /**< mutex for metadata operation */
FILE *dfp; /**< The FILE pointer for the cache data file*/
Link *link; /**< The Link associated with this cache data set */
int blksz; /**<the block size of the data file */
long segbc; /**<segment array byte count */
2019-04-12 13:41:07 +02:00
Seg *seg; /**< the detail of each segment */
} Cache;
/**
* \brief whether the cache system is enabled
*/
extern int CACHE_SYSTEM_INIT;
/**
* \brief initialise the cache system directories
* \details This function basically sets up the following variables:
* - META_DIR
* - DATA_DIR
*
* If these directories do not exist, they will be created.
2019-04-22 14:32:15 +02:00
* \note Called by parse_arg_list(), verified to be working
*/
void CacheSystem_init(const char *dir);
2019-04-22 14:32:15 +02:00
/**
* \brief Create directories under the cache directory structure, if they do
* not already exist
* \return
* - -1 failed to create metadata directory.
* - -2 failed to create data directory.
* - -3 failed to create both metadata and data directory.
* \note Called by LinkTable_new()
2019-04-22 14:32:15 +02:00
*/
int CacheDir_create(const char *fn);
/**
* \brief open a cache file set
* \note This function is called by fs_open()
*/
Cache *Cache_open(const char *fn);
2019-04-12 13:41:07 +02:00
/**
* \brief Close a cache data structure
* \note This function is called by fs_release()
*/
void Cache_close(Cache *cf);
/**
* \brief create a cache file set if it doesn't exist already
* \return
* - 0, if the cache file already exists, or was created succesfully.
* - -1, otherwise
* \note Called by fs_open()
*/
int Cache_create(Link *this_link);
/**
* \brief delete a cache file set
* \note Called by fs_open()
*/
void Cache_delete(const char *fn);
/**
* \brief Intelligently read from the cache system
* \details If the segment does not exist on the local hard disk, download from
* the Internet
* \param[in] cf the cache in-memory data structure
* \param[out] buf the output buffer
* \param[in] size the requested segment size
* \param[in] offset the start of the segment
* \return the length of the segment the cache system managed to obtain.
* \note Called by fs_read(), verified to be working
2019-04-22 03:04:19 +02:00
*/
long Cache_read(Cache *cf, char *output_buf, off_t size, off_t offset);
2019-04-22 03:04:19 +02:00
#endif