Commit cc2ed3f9 authored by Ivan Donchevskiy's avatar Ivan Donchevskiy

Closing files in function open in case they have wrong heading or don't exist…

Closing files in function open in case they have wrong heading or don't exist (CycleStorage.cc and TableBlockStorage.cc)
parent 070c4032
......@@ -147,7 +147,12 @@ bool CycleStorage::open(const char* name, int inf_sz, int inf_count, int seek)
file = fopen(name, "r+");
if(file==NULL) return false;
if(fseek(file,seek,0)==-1) return false;
if(fseek(file,seek,0)==-1)
{
fclose(file);
file=NULL;
return false;
}
/*! */
CycleStorageAttr csa;
......@@ -155,7 +160,11 @@ bool CycleStorage::open(const char* name, int inf_sz, int inf_count, int seek)
/*! */
if((csa.size!=inf_count)||(csa.inf_size!=inf_sz)||(csa.seekpos!=seek))
{
fclose(file);
file=NULL;
return false;
}
inf_size=inf_sz;
full_size=sizeof(CycleStorageElem)+inf_size;
size=inf_count;
......@@ -163,6 +172,8 @@ bool CycleStorage::open(const char* name, int inf_sz, int inf_count, int seek)
if(!findHead())
{
fclose(file);
file=NULL;
return false;
}
return true;
......
......@@ -123,11 +123,16 @@ bool TableBlockStorage::open(const char* name, int key_sz, int inf_sz, int sz, i
if(file==NULL) return false;
seekpos=seek;
if(fseek(file,seekpos,0)==-1) return false;
if(fseek(file,seekpos,0)==-1)
{
fclose(file);
file=NULL;
return false;
}
/*! */
StorageAttr *sa = new StorageAttr();
fread(sa,sizeof(StorageAttr),1,file);
StorageAttr sa;
fread(&sa,sizeof(StorageAttr),1,file);
full_size = sizeof(TableBlockStorageElem)+key_sz+inf_sz;
......@@ -136,12 +141,12 @@ bool TableBlockStorage::open(const char* name, int key_sz, int inf_sz, int sz, i
tmpsize=tmpblock*block_num;
/*! */
if((sa->k_size!=key_sz)||(sa->inf_size!=inf_sz)||(sa->size!=tmpsize)||(sa->block_number!=block_num)||(sa->lim!=block_lim)||(sa->seekpos!=seek))
if((sa.k_size!=key_sz)||(sa.inf_size!=inf_sz)||(sa.size!=tmpsize)||(sa.block_number!=block_num)||(sa.lim!=block_lim)||(sa.seekpos!=seek))
{
delete sa;
fclose(file);
file=NULL;
return false;
}
delete sa;
k_size=key_sz;
inf_size=inf_sz;
......@@ -216,20 +221,19 @@ bool TableBlockStorage::create(const char* name, int key_sz, int inf_sz, int sz,
mem[i]=(TableBlockStorageElem*)new char[full_size];
}
StorageAttr *sa = new StorageAttr();
sa->k_size=k_size;
sa->inf_size=inf_size;
sa->size=size;
sa->block_number=block_number;
sa->lim=lim;
sa->seekpos=seekpos;
StorageAttr sa;
sa.k_size=k_size;
sa.inf_size=inf_size;
sa.size=size;
sa.block_number=block_number;
sa.lim=lim;
sa.seekpos=seekpos;
/*! */
cur_block=0;
fwrite(sa,sizeof(StorageAttr),1,file);
fwrite(&sa,sizeof(StorageAttr),1,file);
fflush(file);
seekpos+=sizeof(StorageAttr);
delete sa;
/*! :
EMPTY_BLOCK=(-5) - , , , EMPTY_ELEM=(-1) -
......@@ -255,6 +259,7 @@ bool TableBlockStorage::create(const char* name, int key_sz, int inf_sz, int sz,
char* empty=new char[emp];
fwrite(empty,emp,1,file);
fflush(file);
delete empty;
}
return true;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment