hashindex: improve error messages

This commit is contained in:
Thomas Waldmann 2015-05-31 17:48:19 +02:00
parent 91e10fec5f
commit 776bb9fabc
1 changed files with 10 additions and 10 deletions

View File

@ -137,7 +137,7 @@ hashindex_read(const char *path)
HashIndex *index = NULL; HashIndex *index = NULL;
if((fd = fopen(path, "r")) == NULL) { if((fd = fopen(path, "r")) == NULL) {
EPRINTF_PATH(path, "fopen failed"); EPRINTF_PATH(path, "fopen for reading failed");
return NULL; return NULL;
} }
bytes_read = fread(&header, 1, sizeof(HashHeader), fd); bytes_read = fread(&header, 1, sizeof(HashHeader), fd);
@ -163,20 +163,20 @@ hashindex_read(const char *path)
goto fail; goto fail;
} }
if(memcmp(header.magic, MAGIC, 8)) { if(memcmp(header.magic, MAGIC, 8)) {
EPRINTF_MSG_PATH(path, "Unknown file header"); EPRINTF_MSG_PATH(path, "Unknown MAGIC in header");
goto fail; goto fail;
} }
buckets_length = (off_t)_le32toh(header.num_buckets) * (header.key_size + header.value_size); buckets_length = (off_t)_le32toh(header.num_buckets) * (header.key_size + header.value_size);
if(length != sizeof(HashHeader) + buckets_length) { if(length != sizeof(HashHeader) + buckets_length) {
EPRINTF_MSG_PATH(path, "Incorrect file length"); EPRINTF_MSG_PATH(path, "Incorrect file length (expected %ld, got %ld)", sizeof(HashHeader) + buckets_length, length);
goto fail; goto fail;
} }
if(!(index = malloc(sizeof(HashIndex)))) { if(!(index = malloc(sizeof(HashIndex)))) {
EPRINTF_PATH(path, "malloc failed"); EPRINTF_PATH(path, "malloc header failed");
goto fail; goto fail;
} }
if(!(index->buckets = malloc(buckets_length))) { if(!(index->buckets = malloc(buckets_length))) {
EPRINTF_PATH(path, "malloc failed"); EPRINTF_PATH(path, "malloc buckets failed");
free(index); free(index);
index = NULL; index = NULL;
goto fail; goto fail;
@ -217,12 +217,12 @@ hashindex_init(int capacity, int key_size, int value_size)
capacity = MAX(MIN_BUCKETS, capacity); capacity = MAX(MIN_BUCKETS, capacity);
if(!(index = malloc(sizeof(HashIndex)))) { if(!(index = malloc(sizeof(HashIndex)))) {
EPRINTF("malloc failed"); EPRINTF("malloc header failed");
return NULL; return NULL;
} }
buckets_length = (off_t)capacity * (key_size + value_size); buckets_length = (off_t)capacity * (key_size + value_size);
if(!(index->buckets = calloc(buckets_length, 1))) { if(!(index->buckets = calloc(buckets_length, 1))) {
EPRINTF("malloc failed"); EPRINTF("malloc buckets failed");
free(index); free(index);
return NULL; return NULL;
} }
@ -261,15 +261,15 @@ hashindex_write(HashIndex *index, const char *path)
int ret = 1; int ret = 1;
if((fd = fopen(path, "w")) == NULL) { if((fd = fopen(path, "w")) == NULL) {
EPRINTF_PATH(path, "open failed"); EPRINTF_PATH(path, "fopen for writing failed");
return 0; return 0;
} }
if(fwrite(&header, 1, sizeof(header), fd) != sizeof(header)) { if(fwrite(&header, 1, sizeof(header), fd) != sizeof(header)) {
EPRINTF_PATH(path, "fwrite failed"); EPRINTF_PATH(path, "fwrite header failed");
ret = 0; ret = 0;
} }
if(fwrite(index->buckets, 1, buckets_length, fd) != buckets_length) { if(fwrite(index->buckets, 1, buckets_length, fd) != buckets_length) {
EPRINTF_PATH(path, "fwrite failed"); EPRINTF_PATH(path, "fwrite buckets failed");
ret = 0; ret = 0;
} }
if(fclose(fd) < 0) { if(fclose(fd) < 0) {