From 3d0269e90f499a6bfd420903a41c81efebd0ce9e Mon Sep 17 00:00:00 2001 From: Fufu Fang Date: Sun, 21 Apr 2019 00:42:32 +0100 Subject: [PATCH] added Cache_create --- src/cache.c | 55 +++++++++++++++++++++++++++++++++++++++++------------ src/cache.h | 19 +++++++++++------- 2 files changed, 55 insertions(+), 19 deletions(-) diff --git a/src/cache.c b/src/cache.c index fccb8e9..2b6596f 100644 --- a/src/cache.c +++ b/src/cache.c @@ -92,6 +92,28 @@ int Cache_exist(const char *fn) return 1; } +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; + + if (Data_create(cf)) { + Cache_free(cf); + fprintf(stderr, "Cache_create(): Data_create() failed!\n"); + return NULL; + } + + if (Meta_create(cf)) { + Cache_free(cf); + fprintf(stderr, "Cache_create(): Meta_create() failed!\n"); + return NULL; + } + return cf; +} + Cache *Cache_open(const char *fn) { /* Create the cache in-memory data structure */ @@ -112,6 +134,11 @@ Cache *Cache_open(const char *fn) return cf; } +int Meta_create(const Cache *cf) +{ + return Meta_write(cf); +} + int Meta_read(Cache *cf) { FILE *fp; @@ -119,6 +146,7 @@ int Meta_read(Cache *cf) fp = fopen(metafn, "r"); free(metafn); int res = 0; + int nmemb = 0; if (!fp) { /* The metadata file does not exist */ @@ -130,16 +158,17 @@ int Meta_read(Cache *cf) fread(&(cf->time), sizeof(long), 1, fp); fread(&(cf->nseg), sizeof(int), 1, 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); + 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); } - /* Read all the segment */ - int nmemb = fread(cf->seg, sizeof(Seg), cf->nseg, fp); - /* Error checking for fread */ if (ferror(fp)) { fprintf(stderr, @@ -180,7 +209,9 @@ int Meta_write(const Cache *cf) fwrite(&(cf->nseg), sizeof(int), 1, fp); /* Finally write segments to the file */ - fwrite(cf->seg, sizeof(Seg), cf->nseg, fp); + if (cf->nseg) { + fwrite(cf->seg, sizeof(Seg), cf->nseg, fp); + } /* Error checking for fwrite */ if (ferror(fp)) { @@ -206,15 +237,15 @@ int Data_create(Cache *cf) free(datafn); if (fd == -1) { fprintf(stderr, "Data_create(): open(): %s\n", strerror(errno)); - return 0; + return -1; } if (ftruncate(fd, cf->len) == -1) { fprintf(stderr, "Data_create(): ftruncate(): %s\n", strerror(errno)); } - if (close(fd) == -1) { + if (close(fd)) { fprintf(stderr, "Data_create(): close:(): %s\n", strerror(errno)); } - return 1; + return 0; } long Data_size(const char *fn) diff --git a/src/cache.h b/src/cache.h index 6e4a27f..23b62a0 100644 --- a/src/cache.h +++ b/src/cache.h @@ -47,11 +47,6 @@ typedef struct { /***************************** To be completed ******************************/ -/** - * \brief create a cache file set - */ -Cache *Cache_create(const char *fn, long len, long time); - /** * \brief Read from a cache file set * \details This function performs the following two things: @@ -104,12 +99,22 @@ void Cache_free(Cache *cf); */ int Cache_exist(const char *fn); +/** + * \brief create a cache file set + */ +Cache *Cache_create(const char *fn, long len, long time); + /** * \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 + */ +int Meta_create(const Cache *cf); + /** * \brief write a metadata file * \return @@ -130,9 +135,9 @@ int Meta_read(Cache *cf); * \brief create a data file * \details We use sparse creation here * \return - * - 1 on successful creation of the data file, note that the result of + * - 0 on successful creation of the data file, note that the result of * the ftruncate() is ignored. - * - 0 on failure to create the data file. + * - -1 on failure to create the data file. */ int Data_create(Cache *cf);