2019-04-09 13:51:15 +02:00
|
|
|
#ifndef CACHE_H
|
|
|
|
#define CACHE_H
|
|
|
|
|
2019-09-01 09:52:18 +02:00
|
|
|
/**
|
|
|
|
* \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-22 22:20:41 +02:00
|
|
|
#include <pthread.h>
|
2019-04-09 13:51:15 +02:00
|
|
|
|
2019-09-01 01:43:50 +02:00
|
|
|
/**
|
|
|
|
* \brief cache data type
|
|
|
|
*/
|
|
|
|
typedef struct Cache Cache;
|
|
|
|
|
|
|
|
#include "link.h"
|
|
|
|
|
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-09-01 01:43:50 +02:00
|
|
|
* \brief cache data type in-memory data structure
|
2019-04-09 13:51:15 +02:00
|
|
|
*/
|
2019-09-01 01:43:50 +02:00
|
|
|
struct Cache {
|
2021-09-01 11:38:45 +02:00
|
|
|
/** \brief How many times the cache has been opened */
|
|
|
|
int cache_opened;
|
|
|
|
|
2019-10-24 03:15:05 +02:00
|
|
|
/** \brief the FILE pointer for the data file*/
|
2021-08-31 12:18:39 +02:00
|
|
|
FILE *dfp;
|
2019-10-24 03:15:05 +02:00
|
|
|
/** \brief the FILE pointer for the metadata */
|
2021-08-31 12:18:39 +02:00
|
|
|
FILE *mfp;
|
2019-10-24 03:15:05 +02:00
|
|
|
/** \brief the path to the local cache file */
|
2021-08-31 12:18:39 +02:00
|
|
|
char *path;
|
2019-09-01 12:39:47 +02:00
|
|
|
/** \brief the Link associated with this cache data set */
|
2021-08-31 12:18:39 +02:00
|
|
|
Link *link;
|
2019-09-01 12:39:47 +02:00
|
|
|
/** \brief the modified time of the file */
|
2021-08-31 12:18:39 +02:00
|
|
|
long time;
|
2019-09-01 12:39:47 +02:00
|
|
|
/** \brief the size of the file */
|
2021-08-31 12:18:39 +02:00
|
|
|
off_t content_length;
|
2019-09-01 12:39:47 +02:00
|
|
|
/** \brief the block size of the data file */
|
2021-08-31 12:18:39 +02:00
|
|
|
int blksz;
|
2019-09-01 12:39:47 +02:00
|
|
|
/** \brief segment array byte count */
|
2021-08-31 12:18:39 +02:00
|
|
|
long segbc;
|
2019-09-01 12:39:47 +02:00
|
|
|
/** \brief the detail of each segment */
|
2021-08-31 12:18:39 +02:00
|
|
|
Seg *seg;
|
2019-09-01 12:39:47 +02:00
|
|
|
|
|
|
|
/** \brief mutex lock for seek operation */
|
2021-08-31 12:18:39 +02:00
|
|
|
pthread_mutex_t seek_lock;
|
2019-09-01 12:39:47 +02:00
|
|
|
/** \brief mutex lock for write operation */
|
2021-08-31 12:18:39 +02:00
|
|
|
pthread_mutex_t w_lock;
|
2019-09-01 12:39:47 +02:00
|
|
|
|
|
|
|
/** \brief background download pthread */
|
2021-08-31 12:18:39 +02:00
|
|
|
pthread_t bgt;
|
2019-09-01 12:39:47 +02:00
|
|
|
/**
|
|
|
|
* \brief mutex lock for the background download thread
|
|
|
|
* \note This lock is locked by the foreground thread, but unlocked by the
|
|
|
|
* background thread!
|
|
|
|
*/
|
2021-08-31 12:18:39 +02:00
|
|
|
pthread_mutex_t bgt_lock;
|
2019-09-01 12:39:47 +02:00
|
|
|
/** \brief mutex attributes for bgt_lock */
|
2021-08-31 12:18:39 +02:00
|
|
|
pthread_mutexattr_t bgt_lock_attr;
|
2019-09-01 12:39:47 +02:00
|
|
|
/** \brief the offset of the next segment to be downloaded in background*/
|
2021-08-31 12:18:39 +02:00
|
|
|
off_t next_dl_offset;
|
2019-10-24 03:15:05 +02:00
|
|
|
|
|
|
|
/** \brief the FUSE filesystem path to the remote file*/
|
2021-08-31 12:18:39 +02:00
|
|
|
char *fs_path;
|
2019-09-01 01:43:50 +02:00
|
|
|
};
|
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-09-04 20:53:11 +02:00
|
|
|
/**
|
|
|
|
* \brief The metadata directory
|
|
|
|
*/
|
|
|
|
extern char *META_DIR;
|
|
|
|
|
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
|
|
|
*/
|
2019-09-04 18:41:34 +02:00
|
|
|
void CacheSystem_init(const char *path, int url_supplied);
|
2019-04-22 03:33:26 +02:00
|
|
|
|
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
|
|
|
*/
|
2021-08-31 12:15:00 +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
|
2019-09-01 09:52:18 +02:00
|
|
|
* - 0, if the cache file already exists, or was created successfully.
|
2019-04-22 16:26:25 +02:00
|
|
|
* - -1, otherwise
|
2019-04-23 12:38:08 +02:00
|
|
|
* \note Called by fs_open()
|
|
|
|
*/
|
2019-10-24 03:15:05 +02:00
|
|
|
int Cache_create(const char *path);
|
2019-04-23 12:38:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \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-09-02 16:19:41 +02:00
|
|
|
* \param[in] offset_start the start of the segment
|
2019-04-22 22:20:41 +02:00
|
|
|
* \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
|
|
|
*/
|
2021-08-31 12:15:00 +02:00
|
|
|
long Cache_read(Cache * cf, char *const output_buf, const off_t len,
|
2019-09-02 16:19:41 +02:00
|
|
|
const off_t offset_start);
|
2019-04-09 13:51:15 +02:00
|
|
|
#endif
|