Fix broken instance state after table read failure

Also fix memory leak in the same case
master
Yury Kurlykov 2019-10-30 01:34:58 +10:00
parent ebedf5619c
commit 31c5b08f6a
1 changed files with 5 additions and 5 deletions

View File

@ -135,15 +135,12 @@ YDB_Error ydb_load_table(YDB_Engine *instance, const char *path) {
THROW_IF_NULL(instance, YDB_ERR_INSTANCE_NOT_INITIALIZED); THROW_IF_NULL(instance, YDB_ERR_INSTANCE_NOT_INITIALIZED);
THROW_IF_NULL(!instance->in_use, YDB_ERR_INSTANCE_IN_USE); THROW_IF_NULL(!instance->in_use, YDB_ERR_INSTANCE_IN_USE);
instance->in_use = -1; // unsigned value overflow to fill all the bits if (access(path, F_OK) == -1) {
instance->filename = strdup(path);
if (access(instance->filename, F_OK) == -1) {
// TODO: Windows does not check W_OK correctly, use other methods. // TODO: Windows does not check W_OK correctly, use other methods.
// TODO: if can't read/write, throw other error // TODO: if can't read/write, throw other error
return YDB_ERR_TABLE_NOT_EXIST; return YDB_ERR_TABLE_NOT_EXIST;
} }
instance->fd = fopen(instance->filename, "rb+"); instance->fd = fopen(path, "rb+");
THROW_IF_NULL(instance->fd, YDB_ERR_UNKNOWN); // TODO file open error THROW_IF_NULL(instance->fd, YDB_ERR_UNKNOWN); // TODO file open error
char signature[4]; char signature[4];
@ -165,6 +162,9 @@ YDB_Error ydb_load_table(YDB_Engine *instance, const char *path) {
fread(&(instance->last_free_page_offset), sizeof(YDB_Offset), 1, instance->fd); fread(&(instance->last_free_page_offset), sizeof(YDB_Offset), 1, instance->fd);
// TODO check offsets // TODO check offsets
instance->in_use = -1; // unsigned value overflow to fill all the bits
instance->filename = strdup(path);
instance->curr_page_offset = instance->first_page_offset; instance->curr_page_offset = instance->first_page_offset;
return __ydb_read_page(instance); return __ydb_read_page(instance);
} }