improved stability / error handling

This commit is contained in:
Fufu Fang 2019-04-23 12:51:10 +01:00
parent 1ad2238aff
commit fe079f6266

View File

@ -20,6 +20,13 @@
*/ */
#define DATA_BLK_SZ 1048576 #define DATA_BLK_SZ 1048576
/**
* \brief Maximum segment block count
* \details This is set to 1024*1024*1024 = 1 GiB, which allows the user to
* access a 1TB file.
*/
#define MAX_SEGBC 1073741824
/** /**
* \brief the maximum length of a path * \brief the maximum length of a path
* \details This corresponds the maximum path length under Ext4. * \details This corresponds the maximum path length under Ext4.
@ -30,10 +37,11 @@
* \brief error associated with metadata * \brief error associated with metadata
*/ */
typedef enum { typedef enum {
SUCCESS = 0, /**< Metadata read successful */ SUCCESS = 0, /**< Metadata read successful */
EFREAD = -1, /**< Fread failed */ EFREAD = -1, /**< Fread failed */
EINCON = -2, /**< Inconsistency in metadata */ EINCONSIST = -2, /**< Inconsistency in metadata */
EZEROS = -3 /**< Unexpected zeros in metadata */ EZERO = -3, /**< Unexpected zeros in metadata */
EMEM = -4, /**< Memory allocation failure */
} MetaError; } MetaError;
int CACHE_SYSTEM_INIT = 0; int CACHE_SYSTEM_INIT = 0;
@ -132,16 +140,21 @@ static int Meta_read(Cache *cf)
fprintf(stderr, fprintf(stderr,
"Meta_read:() Warning corrupt metadata: content_length: %ld, \ "Meta_read:() Warning corrupt metadata: content_length: %ld, \
blksz: %d, segbc: %ld\n", cf->content_length, cf->blksz, cf->segbc); blksz: %d, segbc: %ld\n", cf->content_length, cf->blksz, cf->segbc);
res = EZEROS; res = EZERO;
goto end; goto end;
} }
/* Allocate some memory for the segment */ /* Allocate some memory for the segment */
if (cf->segbc > MAX_SEGBC) {
fprintf(stderr, "Meta_read(): Error: segbc: %ld\n", cf->segbc);
res = EMEM;
goto end;
}
cf->seg = calloc(cf->segbc, sizeof(Seg)); cf->seg = calloc(cf->segbc, sizeof(Seg));
if (!cf->seg) { if (!cf->seg) {
fprintf(stderr, "Meta_read(): segbc: %ld, calloc failure: %s\n", fprintf(stderr, "Meta_read(): calloc failure: %s\n", strerror(errno));
cf->segbc, strerror(errno)); res = EMEM;
exit(EXIT_FAILURE); goto end;
} }
/* Read all the segment */ /* Read all the segment */
nmemb = fread(cf->seg, sizeof(Seg), cf->segbc, fp); nmemb = fread(cf->seg, sizeof(Seg), cf->segbc, fp);
@ -157,7 +170,7 @@ blksz: %d, segbc: %ld\n", cf->content_length, cf->blksz, cf->segbc);
if (nmemb != cf-> segbc) { if (nmemb != cf-> segbc) {
fprintf(stderr, fprintf(stderr,
"Meta_read(): corrupted metadata!\n"); "Meta_read(): corrupted metadata!\n");
res = EINCON; res = EINCONSIST;
} }
end: end:
@ -588,7 +601,7 @@ Cache *Cache_open(const char *fn)
* Internally inconsistent or corrupt metadata * Internally inconsistent or corrupt metadata
*/ */
int rtn = Meta_read(cf); int rtn = Meta_read(cf);
if ((rtn == EINCON) || rtn == EZEROS) { if ((rtn == EINCONSIST) || (rtn == EZERO) || (rtn == EMEM)) {
Cache_free(cf); Cache_free(cf);
fprintf(stderr, "Failure!\nMetadata inconsistent or corrupt!\n"); fprintf(stderr, "Failure!\nMetadata inconsistent or corrupt!\n");
return NULL; return NULL;