2019-04-09 13:51:15 +02:00
|
|
|
#ifndef CACHE_H
|
|
|
|
#define CACHE_H
|
|
|
|
|
2019-04-23 12:38:08 +02:00
|
|
|
#include "link.h"
|
|
|
|
|
2019-04-22 22:20:41 +02:00
|
|
|
#include <pthread.h>
|
2019-04-20 03:04:29 +02:00
|
|
|
#include <stdint.h>
|
2019-04-09 13:51:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \file cache.h
|
|
|
|
* \brief cache related structures and functions
|
2019-04-18 10:48:40 +02:00
|
|
|
* \details
|
|
|
|
* - We store the metadata and the actual data separately in two
|
2019-04-22 10:50:53 +02:00
|
|
|
* separate folders.
|
2019-04-09 13:51:15 +02:00
|
|
|
*/
|
|
|
|
|
2019-04-12 13:41:07 +02:00
|
|
|
/**
|
2019-04-21 11:57:00 +02:00
|
|
|
* \brief Type definition for a cache segment
|
2019-04-12 13:41:07 +02:00
|
|
|
*/
|
2019-04-21 11:57:00 +02:00
|
|
|
typedef uint8_t Seg;
|
2019-04-12 13:41:07 +02:00
|
|
|
|
2019-04-09 13:51:15 +02:00
|
|
|
/**
|
2019-04-20 09:31:16 +02:00
|
|
|
* \brief cache in-memory data structure
|
2019-04-09 13:51:15 +02:00
|
|
|
*/
|
2019-04-12 13:41:07 +02:00
|
|
|
typedef struct {
|
2019-04-22 22:20:41 +02:00
|
|
|
char *path; /**< the path to the file on the web server */
|
2019-04-25 00:58:26 +02:00
|
|
|
Link *link; /**< The Link associated with this cache data set */
|
2019-04-20 03:04:29 +02:00
|
|
|
long time; /**<the modified time of the file */
|
2019-04-22 10:50:53 +02:00
|
|
|
off_t content_length; /**<the size of the file */
|
2019-04-25 00:58:26 +02:00
|
|
|
|
|
|
|
pthread_t bgt; /**< background pthread */
|
|
|
|
pthread_mutex_t bgt_lock; /**< mutex for spawning a background thread */
|
|
|
|
pthread_mutexattr_t bgt_lock_attr;
|
|
|
|
off_t next_offset; /**<the offset of the next segment to be
|
|
|
|
downloaded in background*/
|
|
|
|
|
|
|
|
pthread_mutex_t rw_lock; /**< mutex for read/write operation */
|
2019-04-24 04:00:47 +02:00
|
|
|
FILE *dfp; /**< The FILE pointer for the data file*/
|
|
|
|
FILE *mfp; /**< The FILE pointer for the metadata */
|
2019-04-21 11:57:00 +02:00
|
|
|
int blksz; /**<the block size of the data file */
|
2019-04-21 21:48:44 +02:00
|
|
|
long segbc; /**<segment array byte count */
|
2019-04-12 13:41:07 +02:00
|
|
|
Seg *seg; /**< the detail of each segment */
|
2019-04-20 03:04:29 +02:00
|
|
|
} Cache;
|
2019-04-09 13:51:15 +02:00
|
|
|
|
2019-04-22 13:06:34 +02:00
|
|
|
/**
|
|
|
|
* \brief whether the cache system is enabled
|
|
|
|
*/
|
|
|
|
extern int CACHE_SYSTEM_INIT;
|
|
|
|
|
2019-04-22 03:33:26 +02:00
|
|
|
/**
|
|
|
|
* \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
|
2019-04-22 03:33:26 +02:00
|
|
|
*/
|
|
|
|
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.
|
2019-04-23 12:38:08 +02:00
|
|
|
* \note Called by LinkTable_new()
|
2019-04-22 14:32:15 +02:00
|
|
|
*/
|
|
|
|
int CacheDir_create(const char *fn);
|
|
|
|
|
2019-04-09 13:51:15 +02:00
|
|
|
/**
|
2019-04-22 12:08:42 +02:00
|
|
|
* \brief open a cache file set
|
2019-04-23 12:38:08 +02:00
|
|
|
* \note This function is called by fs_open()
|
2019-04-09 13:51:15 +02:00
|
|
|
*/
|
2019-04-22 12:08:42 +02:00
|
|
|
Cache *Cache_open(const char *fn);
|
2019-04-12 13:41:07 +02:00
|
|
|
|
2019-04-22 03:33:26 +02:00
|
|
|
/**
|
2019-04-22 12:08:42 +02:00
|
|
|
* \brief Close a cache data structure
|
2019-04-23 12:38:08 +02:00
|
|
|
* \note This function is called by fs_release()
|
2019-04-22 03:33:26 +02:00
|
|
|
*/
|
2019-04-22 12:08:42 +02:00
|
|
|
void Cache_close(Cache *cf);
|
2019-04-22 03:33:26 +02:00
|
|
|
|
2019-04-22 16:26:25 +02:00
|
|
|
/**
|
|
|
|
* \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
|
2019-04-23 12:38:08 +02:00
|
|
|
* \note Called by fs_open()
|
|
|
|
*/
|
|
|
|
int Cache_create(Link *this_link);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief delete a cache file set
|
|
|
|
* \note Called by fs_open()
|
2019-04-22 16:26:25 +02:00
|
|
|
*/
|
2019-04-23 12:38:08 +02:00
|
|
|
void Cache_delete(const char *fn);
|
2019-04-22 16:26:25 +02:00
|
|
|
|
2019-04-21 11:57:00 +02:00
|
|
|
/**
|
2019-04-22 22:20:41 +02:00
|
|
|
* \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
|
2019-04-24 01:33:20 +02:00
|
|
|
* \param[out] output_buf the output buffer
|
|
|
|
* \param[in] len the requested segment size
|
2019-04-22 22:20:41 +02:00
|
|
|
* \param[in] offset the start of the segment
|
|
|
|
* \return the length of the segment the cache system managed to obtain.
|
2019-04-23 03:35:17 +02:00
|
|
|
* \note Called by fs_read(), verified to be working
|
2019-04-22 03:04:19 +02:00
|
|
|
*/
|
2019-04-24 01:33:20 +02:00
|
|
|
long Cache_read(Cache *cf, char *output_buf, off_t len, off_t offset);
|
2019-04-22 03:04:19 +02:00
|
|
|
|
2019-04-09 13:51:15 +02:00
|
|
|
#endif
|