httpdirfs/src/cache.h

120 lines
3.1 KiB
C
Raw Normal View History

#ifndef CACHE_H
#define CACHE_H
#include <stdint.h>
#include <unistd.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 {
2019-04-22 14:32:15 +02:00
char *p_url; /**< the partial URL -- this is the link relative
to the root link */
long time; /**<the modified time of the file */
off_t content_length; /**<the size of the file */
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 fs_readdir(), verified to be working
*/
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);
2019-04-22 03:59:53 +02:00
/***************************** Work in Progress ******************************/
2019-04-22 03:04:19 +02:00
/**
* \brief Check if a segment exists.
* \note Call this when deciding whether to download a file
2019-04-22 03:04:19 +02:00
*/
int Seg_exist(Cache *cf, long start);
2019-04-20 09:31:16 +02:00
2019-04-22 03:04:19 +02:00
/**
* \brief Set the existence of a segment
* \param[in] start the starting position of the segment.
* \param[in] i 1 for exist, 0 for doesn't exist
* \note Call this after downloading a segment.
2019-04-22 03:04:19 +02:00
*/
void Seg_set(Cache *cf, long start, int i);
/**
* \brief create a cache file set
* * \return
* - 0, if the cache file was created succesfully
* - -1, otherwise
* \note Call this when creating a new LinkTable
*/
int Cache_create(const char *fn, long len, long time);
2019-04-21 01:42:32 +02:00
/***************************** To be completed ******************************/
/**
2019-04-22 03:04:19 +02:00
* \brief Read from a cache file set
* \details This function performs the following two things:
* - check again the metafile to see which segments are available
* - return the available segments
* \note Call this when it is not necessary to download a segment
*/
2019-04-22 03:04:19 +02:00
long Cache_read(const char *fn, long offset, long len, uint8_t *buf);
/**
* \brief Write to a cache file set
* \details This function performs the following two things:
* - Write to the data file
* - Update the metadata file
* \note Call this after downloading a segment.
2019-04-22 03:04:19 +02:00
*/
long Cache_write(const char *fn, long offset, long len, const uint8_t *buf);
#endif