Changed the way segments work, added more functions

Now segments are fixed size block of 128KiB
This commit is contained in:
Fufu Fang 2019-04-21 10:57:00 +01:00
parent 3d0269e90f
commit e5c77f3680
2 changed files with 102 additions and 45 deletions

View File

@ -10,7 +10,18 @@
#include <sys/stat.h>
#include <sys/types.h>
#define MAX_PATH_LEN 4096
/**
* \brief Data file block size
* \details The data file block size is set to 128KiB, for convenience. This is
* because the maximum requested block size by FUSE seems to be 128KiB under
* Debian Stretch. Note that the minimum requested block size appears to be
* 4KiB.
*
* More information regarding block size can be found at:
* https://wiki.vuze.com/w/Torrent_Piece_Size
*/
#define DATA_BLK_SZ 131072
#define MAX_PATH_LEN 4096
/**
* \brief The metadata directory
@ -22,6 +33,7 @@ char *META_DIR;
*/
char *DATA_DIR;
void Cache_init(const char *dir)
{
META_DIR = strndupcat(dir, "meta/", MAX_PATH_LEN);
@ -30,16 +42,11 @@ void Cache_init(const char *dir)
Cache *Cache_alloc()
{
Cache *cf = malloc(sizeof(Cache));
Cache *cf = calloc(1, sizeof(Cache));
if (!cf) {
fprintf(stderr, "Cache_new(): malloc failure!\n");
fprintf(stderr, "Cache_new(): calloc failure!\n");
exit(EXIT_FAILURE);
}
cf->filename = NULL;
cf->len = 0;
cf->time = 0;
cf->nseg = 0;
cf->seg = NULL;
return cf;
}
@ -89,7 +96,7 @@ int Cache_exist(const char *fn)
free(metafn);
free(datafn);
return 1;
return !(meta_exists & data_exists);
}
Cache *Cache_create(const char *fn, long len, long time)
@ -97,8 +104,8 @@ Cache *Cache_create(const char *fn, long len, long time)
Cache *cf = Cache_alloc();
cf->filename = strndup(fn, MAX_PATH_LEN);
cf->len = len;
cf->time = time;
cf->len = len;
if (Data_create(cf)) {
Cache_free(cf);
@ -114,8 +121,34 @@ Cache *Cache_create(const char *fn, long len, long time)
return cf;
}
void Cache_delete(const char *fn)
{
char *metafn = strndupcat(META_DIR, fn, MAX_PATH_LEN);
char *datafn = strndupcat(DATA_DIR, fn, MAX_PATH_LEN);
if (!access(metafn, F_OK)) {
if(unlink(metafn)) {
fprintf(stderr, "Cache_delete(): unlink(): %s\n",
strerror(errno));
}
}
if (!access(datafn, F_OK)) {
if(unlink(datafn)) {
fprintf(stderr, "Cache_delete(): unlink(): %s\n",
strerror(errno));
}
}
free(metafn);
free(datafn);
}
Cache *Cache_open(const char *fn)
{
/* Check if both metadata and data file exist */
if (Cache_validate(fn)) {
return NULL;
}
/* Create the cache in-memory data structure */
Cache *cf = Cache_alloc();
cf->filename = strndup(fn, MAX_PATH_LEN);
@ -125,17 +158,27 @@ Cache *Cache_open(const char *fn)
return NULL;
}
/* In consistent metadata / data file */
if (cf->len != Data_size(fn)) {
fprintf(stderr,
"Cache_open(): metadata is inconsistent with the data file!\n");
Cache_delete(fn);
Cache_free(cf);
return NULL;
}
return cf;
}
int Meta_create(const Cache *cf)
{
cf->blksz = DATA_BLK_SZ;
cf->nseg = cf->len / cf->blksz + 1;
cf->seg = calloc(nseg, sizeof(Seg));
if (!(cf->seg)) {
fprintf(stderr, "Meta_create(): calloc failure!\n");
exit(EXIT_FAILURE);
}
return Meta_write(cf);
}
@ -154,20 +197,19 @@ int Meta_read(Cache *cf)
return -1;
}
fread(&(cf->len), sizeof(long), 1, fp);
fread(&(cf->time), sizeof(long), 1, fp);
fread(&(cf->len), sizeof(long), 1, fp);
fread(&(cf->len), sizeof(int), 1, fp);
fread(&(cf->nseg), sizeof(int), 1, fp);
if (cf->nseg) {
/* Allocate some memory for the segment */
cf->seg = malloc(cf->nseg * sizeof(Seg));
if (!(cf->seg)) {
fprintf(stderr, "Meta_read(): malloc failure!\n");
exit(EXIT_FAILURE);
}
/* Read all the segment */
nmemb = fread(cf->seg, sizeof(Seg), cf->nseg, fp);
/* Allocate some memory for the segment */
cf->seg = malloc(cf->nseg * sizeof(Seg));
if (!(cf->seg)) {
fprintf(stderr, "Meta_read(): malloc failure!\n");
exit(EXIT_FAILURE);
}
/* Read all the segment */
nmemb = fread(cf->seg, sizeof(Seg), cf->nseg, fp);
/* Error checking for fread */
if (ferror(fp)) {
@ -204,14 +246,11 @@ int Meta_write(const Cache *cf)
return -1;
}
fwrite(&(cf->len), sizeof(long), 1, fp);
fwrite(&(cf->time), sizeof(long), 1, fp);
fwrite(&(cf->len), sizeof(long), 1, fp);
fwrite(&(cf->blksz), sizeof(int), 1, fp);
fwrite(&(cf->nseg), sizeof(int), 1, fp);
/* Finally write segments to the file */
if (cf->nseg) {
fwrite(cf->seg, sizeof(Seg), cf->nseg, fp);
}
fwrite(cf->seg, sizeof(Seg), cf->nseg, fp);
/* Error checking for fwrite */
if (ferror(fp)) {
@ -239,7 +278,7 @@ int Data_create(Cache *cf)
fprintf(stderr, "Data_create(): open(): %s\n", strerror(errno));
return -1;
}
if (ftruncate(fd, cf->len) == -1) {
if (ftruncate(fd, cf->len)) {
fprintf(stderr, "Data_create(): ftruncate(): %s\n", strerror(errno));
}
if (close(fd)) {
@ -252,11 +291,11 @@ long Data_size(const char *fn)
{
char *datafn = strndupcat(DATA_DIR, fn, MAX_PATH_LEN);
struct stat st;
if (stat(datafn, &st) == 0) {
free(datafn);
int s = stat(datafn, &st);
free(datafn);
if (!s) {
return st.st_blksize;
}
free(datafn);
fprintf(stderr, "Data_size(): stat(): %s\n", strerror(errno));
return -1;
}
@ -281,7 +320,7 @@ long Data_read(const Cache *cf, long offset, long len,
return -1;
}
if (fseek(fp, offset, SEEK_SET) == -1) {
if (fseek(fp, offset, SEEK_SET)) {
/* fseek failed */
fprintf(stderr, "Data_read(): fseek(): %s\n", strerror(errno));
goto cleanup;
@ -331,7 +370,7 @@ long Data_write(const Cache *cf, long offset, long len,
return -1;
}
if (fseek(fp, offset, SEEK_SET) == -1) {
if (fseek(fp, offset, SEEK_SET)) {
/* fseek failed */
fprintf(stderr, "Data_write(): fseek(): %s\n", strerror(errno));
goto cleanup;

View File

@ -21,26 +21,22 @@
* - We are using 'long' to store file size, because the offset in fseek() is
* in long, because the way we use the cache system, you cannot seek past
* long. So the biggest file size has to be able to be stored in long. This
* makes this program architecturally dependent, but this is due to the
* dependency to fseek().
* makes this program architecturally dependent, i.e. i386 vs amd64
*/
/**
* \brief a cache segment
* \brief Type definition for a cache segment
*/
typedef struct {
long start;
long end;
} Seg;
typedef uint8_t Seg;
/**
* \brief cache in-memory data structure
* \note fanf2@cam.ac.uk told me to use an array rather than linked list!
*/
typedef struct {
char *filename; /**< the filename from the http server */
long len; /**<the size of the file */
long time; /**<the modified time of the file */
long len; /**<the size of the file */
int blksz; /**<the block size of the data file */
int nseg; /**<the number of segments */
Seg *seg; /**< the detail of each segment */
} Cache;
@ -63,6 +59,17 @@ long Cache_read(const char *fn, long offset, long len, uint8_t *buf);
*/
long Cache_write(const char *fn, long offset, long len,
const uint8_t *buf);
/**
* \brief Initialise cache directory structure
*/
int Cache_DirInit(const char *fn);
/**
* \brief Create a directory within the cache structure
*/
int Cache_DirCreate(const char *fn);
/****************************** Work in progress *****************************/
@ -88,14 +95,14 @@ Cache *Cache_alloc();
void Cache_free(Cache *cf);
/**
* \brief Check if both metadata and the data file exist, and perform cleanup.
* \brief Check if both metadata and data file exist, otherwise perform cleanup.
* \details
* This function checks if both metadata file and the data file exist. If that
* is not the case, clean up is performed - the existing unpaired metadata file
* or data file is deleted.
* \return
* - 1, if both metadata and cache file exist
* - 0, otherwise
* - 0, if both metadata and cache file exist
* - -1, otherwise
*/
int Cache_exist(const char *fn);
@ -104,14 +111,25 @@ int Cache_exist(const char *fn);
*/
Cache *Cache_create(const char *fn, long len, long time);
/**
* \brief delete a cache file set
*/
void Cache_delete(const char *fn);
/**
* \brief open a cache file set
* \warning We assume that the metadata file and the data file both exist
*/
Cache *Cache_open(const char *fn);
/**
* \brief create a metadata file
* \details We set the followings here:
* - block size
* - the number of segments
*
* The number of segments depends on the block size. The block size is set to
* 128KiB for now. In future support for different block size may be
* implemented.
*/
int Meta_create(const Cache *cf);