Commit 8dc14337 authored by Ivan Donchevskiy's avatar Ivan Donchevskiy

Added check for byte size of TableBlockStorage

parent 833a4b73
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
#include "UniXML.h" #include "UniXML.h"
int seek=0; int seek=0;
int b_size=100000;
void testTable1(void) void testTable1(void)
{ {
...@@ -79,7 +80,7 @@ bool testTable2(void) ...@@ -79,7 +80,7 @@ bool testTable2(void)
char *val=new char[40]; char *val=new char[40];
TableBlockStorage t; TableBlockStorage t;
//t = new TableBlockStorage(); //t = new TableBlockStorage();
t.create("big_file.test", 4, 40, 100, 5,28,0); t.create("big_file.test", b_size, 4, 40, 100, 5,28,0);
seek=t.getByteSize(); seek=t.getByteSize();
printf("Table size in bytes = %d\n",seek); printf("Table size in bytes = %d\n",seek);
int i; int i;
...@@ -191,7 +192,7 @@ bool testTable2(void) ...@@ -191,7 +192,7 @@ bool testTable2(void)
return false; return false;
} }
printf("after reopen:\n"); printf("after reopen:\n");
t.open("big_file.test", 4, 40, 100, 5,28,0); t.open("big_file.test", b_size, 4, 40, 100, 5,28,0);
for(i=1;i<20;i++) for(i=1;i<20;i++)
{ {
if(t.findKeyValue(&i,val)!=0) printf("%s, ",val); if(t.findKeyValue(&i,val)!=0) printf("%s, ",val);
...@@ -259,7 +260,7 @@ bool testJournal1(void) ...@@ -259,7 +260,7 @@ bool testJournal1(void)
k = 0; k = 0;
printf("size changed to 33000 rows (increased)\n"); printf("size changed to 33000 rows (increased)\n");
j.setSize(33000); j.setSize(33000);
TableBlockStorage t("big_file.test", 4, 40, 100, 5,28,0); TableBlockStorage t("big_file.test", b_size, 4, 40, 100, 5,28,0);
printf("test of 2 classes working in 1 file together\n"); printf("test of 2 classes working in 1 file together\n");
char *val = new char[40]; char *val = new char[40];
for(i=1;i<20;i++) for(i=1;i<20;i++)
......
...@@ -92,7 +92,7 @@ class TableBlockStorage ...@@ -92,7 +92,7 @@ class TableBlockStorage
/*! Open, create=true /*! Open, create=true
*/ */
TableBlockStorage(const char* name, int key_sz, int inf_sz, int inf_count, int block_num, int block_lim, int seek, bool create=false); TableBlockStorage(const char* name, int byte_sz, int key_sz, int inf_sz, int inf_count, int block_num, int block_lim, int seek, bool create=false);
~TableBlockStorage(); ~TableBlockStorage();
...@@ -109,8 +109,8 @@ class TableBlockStorage ...@@ -109,8 +109,8 @@ class TableBlockStorage
sizeof(StorageAttr), - , sizeof(StorageAttr), - ,
, getByteSize() , getByteSize()
*/ */
bool open(const char* name, int inf_sz, int key_sz, int inf_count, int block_num, int block_lim, int seek); bool open(const char* name, int byte_sz, int inf_sz, int key_sz, int inf_count, int block_num, int block_lim, int seek);
bool create(const char* name, int inf_sz, int key_sz, int inf_count, int block_num, int block_lim, int seek); bool create(const char* name, int byte_sz, int inf_sz, int key_sz, int inf_count, int block_num, int block_lim, int seek);
/*! , */ /*! , */
bool addRow(void* key, void* val); bool addRow(void* key, void* val);
......
...@@ -35,13 +35,13 @@ TableBlockStorage::TableBlockStorage() ...@@ -35,13 +35,13 @@ TableBlockStorage::TableBlockStorage()
file=NULL; file=NULL;
} }
TableBlockStorage::TableBlockStorage(const char* name, int key_sz, int inf_sz, int inf_count, int block_num, int block_lim, int seek, bool cr) TableBlockStorage::TableBlockStorage(const char* name, int byte_sz, int key_sz, int inf_sz, int inf_count, int block_num, int block_lim, int seek, bool cr)
{ {
file=NULL; file=NULL;
if(!open(name, key_sz, inf_sz, inf_count, block_num, block_lim, seek)) if(!open(name, byte_sz, key_sz, inf_sz, inf_count, block_num, block_lim, seek))
{ {
if(cr) if(cr)
create(name, key_sz, inf_sz, inf_count, block_num, block_lim, seek); create(name, byte_sz, key_sz, inf_sz, inf_count, block_num, block_lim, seek);
else else
file=NULL; file=NULL;
} }
...@@ -115,7 +115,7 @@ bool TableBlockStorage::copyToNextBlock(void) ...@@ -115,7 +115,7 @@ bool TableBlockStorage::copyToNextBlock(void)
return true; return true;
} }
bool TableBlockStorage::open(const char* name, int key_sz, int inf_sz, int inf_count, int block_num, int block_lim, int seek) bool TableBlockStorage::open(const char* name, int byte_sz, int key_sz, int inf_sz, int inf_count, int block_num, int block_lim, int seek)
{ {
/*! , */ /*! , */
if(file!=NULL) fclose(file); if(file!=NULL) fclose(file);
...@@ -154,6 +154,13 @@ bool TableBlockStorage::open(const char* name, int key_sz, int inf_sz, int inf_c ...@@ -154,6 +154,13 @@ bool TableBlockStorage::open(const char* name, int key_sz, int inf_sz, int inf_c
block_size=inf_count; block_size=inf_count;
size=block_size*block_num; size=block_size*block_num;
if( byte_sz<getByteSize() )
{
fclose(file);
file=NULL;
return false;
}
max=-1; max=-1;
/*! */ /*! */
...@@ -182,16 +189,11 @@ bool TableBlockStorage::open(const char* name, int key_sz, int inf_sz, int inf_c ...@@ -182,16 +189,11 @@ bool TableBlockStorage::open(const char* name, int key_sz, int inf_sz, int inf_c
return true; return true;
} }
bool TableBlockStorage::create(const char* name, int key_sz, int inf_sz, int inf_count, int block_num, int block_lim, int seek) bool TableBlockStorage::create(const char* name, int byte_sz, int key_sz, int inf_sz, int inf_count, int block_num, int block_lim, int seek)
{ {
if(file!=NULL) fclose(file); if(file!=NULL) fclose(file);
file = fopen(name, "r+"); file = fopen(name, "r+");
if(file==NULL)
{
FILE*f=fopen(name,"w");
fclose(f);
file = fopen(name, "r+");
}
k_size=key_sz; k_size=key_sz;
inf_size=inf_sz; inf_size=inf_sz;
seekpos=seek; seekpos=seek;
...@@ -205,6 +207,20 @@ bool TableBlockStorage::create(const char* name, int key_sz, int inf_sz, int inf ...@@ -205,6 +207,20 @@ bool TableBlockStorage::create(const char* name, int key_sz, int inf_sz, int inf
size=block_size*block_num; size=block_size*block_num;
max=-1; max=-1;
if( byte_sz<getByteSize() )
{
if( file!=NULL ) fclose(file);
file=NULL;
return false;
}
if(file==NULL)
{
FILE*f=fopen(name,"w");
fclose(f);
file = fopen(name, "r+");
}
if(fseek(file,seekpos,0)==-1) return false; if(fseek(file,seekpos,0)==-1) return false;
/*! */ /*! */
......
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