httpdirfs/src/cache.h

147 lines
3.8 KiB
C
Raw Permalink Normal View History

#ifndef CACHE_H
#define CACHE_H
/**
* \file cache.h
* \brief cache related structures and functions
* \details
* - We store the metadata and the actual data separately in two
* separate folders.
*/
typedef struct Cache Cache;
#include "link.h"
2021-09-03 22:39:31 +02:00
#include "network.h"
#include <stdio.h>
#include <stdint.h>
#include <pthread.h>
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
/**
* \brief cache data type in-memory data structure
*/
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;
/** \brief the Link associated with this cache data set */
2021-08-31 12:18:39 +02:00
Link *link;
/** \brief the modified time of the file */
2021-08-31 12:18:39 +02:00
long time;
/** \brief the size of the file */
2021-08-31 12:18:39 +02:00
off_t content_length;
/** \brief the block size of the data file */
2021-08-31 12:18:39 +02:00
int blksz;
/** \brief segment array byte count */
2021-08-31 12:18:39 +02:00
long segbc;
/** \brief the detail of each segment */
2021-08-31 12:18:39 +02:00
Seg *seg;
/** \brief mutex lock for seek operation */
2021-08-31 12:18:39 +02:00
pthread_mutex_t seek_lock;
/** \brief mutex lock for write operation */
2021-08-31 12:18:39 +02:00
pthread_mutex_t w_lock;
/** \brief background download pthread */
2021-08-31 12:18:39 +02:00
pthread_t bgt;
/**
* \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;
/** \brief mutex attributes for bgt_lock */
2021-08-31 12:18:39 +02:00
pthread_mutexattr_t bgt_lock_attr;
/** \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;
};
/**
* \brief whether the cache system is enabled
*/
extern int CACHE_SYSTEM_INIT;
/**
* \brief The metadata directory
*/
extern char *META_DIR;
/**
* \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-09-04 18:41:34 +02:00
void CacheSystem_init(const char *path, int url_supplied);
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()
*/
2021-09-03 15:56:11 +02:00
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 successfully.
* - -1, otherwise
* \note Called by fs_open()
*/
2019-10-24 03:15:05 +02:00
int Cache_create(const char *path);
/**
* \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
2019-04-24 01:33:20 +02:00
* \param[out] output_buf the output buffer
* \param[in] len the requested segment size
* \param[in] offset_start 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
*/
2021-09-03 15:56:11 +02:00
long Cache_read(Cache *cf, char *const output_buf, const off_t len,
const off_t offset_start);
#endif