2019-04-09 13:51:15 +02:00
|
|
|
#ifndef CACHE_H
|
|
|
|
#define CACHE_H
|
|
|
|
|
2019-04-20 03:04:29 +02:00
|
|
|
#include <stdint.h>
|
2019-04-09 13:51:15 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \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 03:43:48 +02:00
|
|
|
char *p_url; /**< the filename from the http server */
|
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-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 12:08:42 +02:00
|
|
|
* \note Called by parse_arg_list()
|
2019-04-22 03:33:26 +02:00
|
|
|
*/
|
|
|
|
void CacheSystem_init(const char *dir);
|
|
|
|
|
2019-04-09 13:51:15 +02:00
|
|
|
/**
|
2019-04-22 12:08:42 +02:00
|
|
|
* \brief open a cache file set
|
|
|
|
* \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
|
|
|
|
* \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 03:59:53 +02:00
|
|
|
/***************************** Work in Progress ******************************/
|
|
|
|
|
2019-04-22 03:04:19 +02:00
|
|
|
/**
|
|
|
|
* \brief Check if a segment exists.
|
2019-04-22 03:33:26 +02:00
|
|
|
* \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
|
2019-04-22 03:33:26 +02:00
|
|
|
* \note Call this after downloading a segment.
|
2019-04-22 03:04:19 +02:00
|
|
|
*/
|
|
|
|
void Seg_set(Cache *cf, long start, int i);
|
2019-04-18 10:48:40 +02:00
|
|
|
|
2019-04-22 12:08:42 +02:00
|
|
|
/**
|
|
|
|
* \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
|
|
|
|
2019-04-22 03:33:26 +02:00
|
|
|
/**
|
2019-04-22 12:08:42 +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 Call this when creating a new LinkTable
|
2019-04-22 03:33:26 +02:00
|
|
|
*/
|
2019-04-22 12:08:42 +02:00
|
|
|
int CacheDir_create(const char *fn);
|
|
|
|
|
|
|
|
/***************************** To be completed ******************************/
|
|
|
|
|
|
|
|
|
2019-04-22 03:33:26 +02:00
|
|
|
|
2019-04-21 11:57:00 +02:00
|
|
|
/**
|
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
|
2019-04-22 03:33:26 +02:00
|
|
|
* \note Call this when it is not necessary to download a segment
|
2019-04-21 11:57:00 +02:00
|
|
|
*/
|
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
|
2019-04-22 03:33:26 +02:00
|
|
|
* \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);
|
|
|
|
|
2019-04-09 13:51:15 +02:00
|
|
|
#endif
|