Commit d07d5b99 authored by Per Cederqvist's avatar Per Cederqvist

Include smalloc.h. Adapted to the LysKOM environment. Fold some long lines.

parent e6b07461
...@@ -13,13 +13,10 @@ ...@@ -13,13 +13,10 @@
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
#include "cache-database.h" #include "cache-database.h"
#include "smalloc.h"
/* misc. defines *********************************************/ /* misc. defines *********************************************/
#define smalloc malloc
#define sfree free
#define srealloc realloc
#define TIME (time(NULL)) #define TIME (time(NULL))
#define SAFESTORAGETIME 30 /* seconds to regen blocks */ #define SAFESTORAGETIME 30 /* seconds to regen blocks */
#define cb_addsafestoragetime(x) ((x)+SAFESTORAGETIME) #define cb_addsafestoragetime(x) ((x)+SAFESTORAGETIME)
...@@ -80,41 +77,6 @@ static unsigned long ...@@ -80,41 +77,6 @@ static unsigned long
cb_getgeneration(struct cachebase *cb,unsigned long bn, cb_getgeneration(struct cachebase *cb,unsigned long bn,
unsigned long no); unsigned long no);
/* ugly we-don't-link-with-others-right-now functions ********/
void log(char *format,...)
{
va_list va;
time_t t;
struct tm *tm;
t=TIME;
tm=localtime(&t);
printf("%02d-%02d-%02d %2d:%02d:%02d ",
tm->tm_year,tm->tm_mon+1,tm->tm_mday,
tm->tm_hour,tm->tm_min,tm->tm_sec);
va_start(va,format);
vprintf(format,va);
va_end(va);
fflush(stdout);
}
void restart_kom(char *format,...)
{
va_list va;
time_t t;
struct tm*tm;
t=TIME;
tm=localtime(&t);
printf("%02d-%02d-%02d %2d:%02d:%02d ",
tm->tm_year,tm->tm_mon+1,tm->tm_mday,
tm->tm_hour,tm->tm_min,tm->tm_sec);
va_start(va,format);
vprintf(format,va);
va_end(va);
printf("(exit! exit! exit! abandon! alien intruder detected! self destructing!)\n");
exit(0);
}
/* internal functions ****************************************/ /* internal functions ****************************************/
static unsigned long cb_sizeoflist(struct cb_blocklist *cbb) static unsigned long cb_sizeoflist(struct cb_blocklist *cbb)
...@@ -145,7 +107,8 @@ static struct cachebase * ...@@ -145,7 +107,8 @@ static struct cachebase *
cb->fp=fopen(filename,"r+b"); cb->fp=fopen(filename,"r+b");
if (!cb->fp) { sfree(cb); return NULL; } if (!cb->fp) { sfree(cb); return NULL; }
log("cache: recovering database '%s' (recreating index '%s'):\n",filename,indexname); log("cache: recovering database '%s' (recreating index '%s'):\n",
filename,indexname);
fseek(cb->fp,0,0); fseek(cb->fp,0,0);
fread(&(cb->header),sizeof(cb->header),1,cb->fp); fread(&(cb->header),sizeof(cb->header),1,cb->fp);
...@@ -476,22 +439,40 @@ static unsigned long ...@@ -476,22 +439,40 @@ static unsigned long
rh.ts_created=TIME; rh.ts_created=TIME;
fseek(cb->fp,BLOCKPOSITION(cb,bn)+index,0); fseek(cb->fp,BLOCKPOSITION(cb,bn)+index,0);
if (!fwrite(&rh,sizeof(struct cb_recordheader),1,cb->fp)) if (!fwrite(&rh,sizeof(struct cb_recordheader),1,cb->fp))
{ restart_kom("cache: Can't write recordheader, r%ib%i, base='%s'\n",no,bn,cb->filename); return 0; } {
restart_kom("cache: Can't write recordheader, r%ib%i, base='%s'\n",
no,bn,cb->filename);
return 0;
}
if (size) if (size)
if (!fwrite(buffert,size,1,cb->fp)) if (!fwrite(buffert,size,1,cb->fp))
{ restart_kom("cache: Can't write record data, r%ib%i, base='%s'\n",no,bn,cb->filename); return 0; } {
restart_kom("cache: Can't write record data, r%ib%i, base='%s'\n",
no,bn,cb->filename);
return 0;
}
fseek(cb->fp,BLOCKPOSITION(cb,bn),0); fseek(cb->fp,BLOCKPOSITION(cb,bn),0);
bh.index[i]=no; bh.index[i]=no;
bh.ts_written=TIME; bh.ts_written=TIME;
if (!fwrite(&bh,sizeof(struct cb_blockheader),1,cb->fp)) if (!fwrite(&bh,sizeof(struct cb_blockheader),1,cb->fp))
{ restart_kom("cache: Can't write block header, r%i to b%i, base='%s'\n",no,bn,cb->filename); return 0; } {
restart_kom("cache: Can't write block header, r%i to b%i, base='%s'\n",
no,bn,cb->filename);
return 0;
}
fseek(cb->ifp,sizeof(unsigned long)*no,0); fseek(cb->ifp,sizeof(unsigned long)*no,0);
if (!fwrite(&bn,sizeof(unsigned long),1,cb->ifp)) if (!fwrite(&bn,sizeof(unsigned long),1,cb->ifp))
{ restart_kom("cache: Can't write record index, r%i to b%i, base='%s', index='%s'\n",no,bn,cb->filename,cb->indexname); return 0; } {
restart_kom("cache: Can't write record index, r%i to b%i, base='%s', "
"index='%s'\n",
no,bn,cb->filename,cb->indexname);
return 0;
}
log("debug: W: rec %lu as #%lu in bl %lu (pos %lu)\n",no,i,bn,BLOCKPOSITION(cb,bn)+sizeof(struct cb_recordheader)+index); log("debug: W: rec %lu as #%lu in bl %lu (pos %lu)\n",
no,i,bn,BLOCKPOSITION(cb,bn)+sizeof(struct cb_recordheader)+index);
if (no>=cb->header.highnumber) if (no>=cb->header.highnumber)
{ {
...@@ -501,7 +482,8 @@ log("debug: highnumber set to %lu\n",no); ...@@ -501,7 +482,8 @@ log("debug: highnumber set to %lu\n",no);
cb->header.ts_written=TIME; cb->header.ts_written=TIME;
if (!fwrite(&cb->header,sizeof(struct cachebaseheader),1,cb->fp)) if (!fwrite(&cb->header,sizeof(struct cachebaseheader),1,cb->fp))
{ {
restart_kom("cache: !write to base header failed ('%s')\n",cb->filename); restart_kom("cache: !write to base header failed ('%s')\n",
cb->filename);
return bn; return bn;
} }
} }
...@@ -526,7 +508,8 @@ static void ...@@ -526,7 +508,8 @@ static void
fseek(cb->fp,BLOCKPOSITION(cb,bn),0); fseek(cb->fp,BLOCKPOSITION(cb,bn),0);
if (!fwrite(&bh,sizeof(struct cb_blockheader),1,cb->fp)) if (!fwrite(&bh,sizeof(struct cb_blockheader),1,cb->fp))
{ {
restart_kom("cache: cb_abandon: failed to write blockheader (b%lu)\n",bn); restart_kom("cache: cb_abandon: failed to write blockheader (b%lu)\n",
bn);
return; return;
} }
...@@ -539,7 +522,9 @@ static void ...@@ -539,7 +522,9 @@ static void
fread(&rh,sizeof(struct cb_recordheader),1,cb->fp); fread(&rh,sizeof(struct cb_recordheader),1,cb->fp);
if ((bh.index[i]!=~0)&&(rh.no!=bh.index[i])) if ((bh.index[i]!=~0)&&(rh.no!=bh.index[i]))
{ {
restart_kom("cache: !record %lu in '%s', index(%lu)!=recordno(%lu) => corrupt\n",bn,cb->filename,bh.index[i],rh.no); restart_kom("cache: !record %lu in '%s', "
"index(%lu)!=recordno(%lu) => corrupt\n",
bn,cb->filename,bh.index[i],rh.no);
return; return;
} }
index+=rh.size+sizeof(struct cb_recordheader); index+=rh.size+sizeof(struct cb_recordheader);
...@@ -561,9 +546,15 @@ static unsigned long ...@@ -561,9 +546,15 @@ static unsigned long
unsigned long index; unsigned long index;
if (bn<1) if (bn<1)
{ log("cache: cb_getgeneration: warning; bn<1\n"); return 0; } {
log("cache: cb_getgeneration: warning; bn<1\n");
return 0;
}
if (no<1) if (no<1)
{ log("cache: cb_getgeneration: warning; no<1\n"); return 0; } {
log("cache: cb_getgeneration: warning; no<1\n");
return 0;
}
fseek(cb->fp,BLOCKPOSITION(cb,bn),0); fseek(cb->fp,BLOCKPOSITION(cb,bn),0);
fread(&bh,sizeof(struct cb_blockheader),1,cb->fp); fread(&bh,sizeof(struct cb_blockheader),1,cb->fp);
...@@ -576,22 +567,25 @@ static unsigned long ...@@ -576,22 +567,25 @@ static unsigned long
fread(&rh,sizeof(struct cb_recordheader),1,cb->fp); fread(&rh,sizeof(struct cb_recordheader),1,cb->fp);
if ((bh.index[i]!=~0)&&(rh.no!=bh.index[i])) if ((bh.index[i]!=~0)&&(rh.no!=bh.index[i]))
{ {
restart_kom("cache: !record %lu in '%s', index(%lu)!=recordno(%lu) => corrupt\n",bn,cb->filename,bh.index[i],rh.no); restart_kom("cache: !record %lu in '%s', "
"index(%lu)!=recordno(%lu) => corrupt\n",
bn,cb->filename,bh.index[i],rh.no);
return 0; return 0;
} }
if (rh.no==no) return rh.generation; if (rh.no==no) return rh.generation;
index+=rh.size+sizeof(struct cb_recordheader); index+=rh.size+sizeof(struct cb_recordheader);
} }
restart_kom("cache: !record %lu in '%s', missing record %lu => corrupt",bn,cb->filename,rh.no); restart_kom("cache: !record %lu in '%s', missing record %lu => corrupt",
bn,cb->filename,rh.no);
log("cache: cb_getgeneration: warning, missing record %ld in block %ld.\n", log("cache: cb_getgeneration: warning, missing record %ld in block %ld.\n",
no,bn); no,bn);
return 0; return 0;
} }
void * void *
cb_readrecord(struct cachebase *cb,unsigned long bn, cb_readrecord(struct cachebase *cb,unsigned long bn,
unsigned long no,unsigned long *size, unsigned long no,unsigned long *size,
unsigned long *gen) unsigned long *gen)
{ {
struct cb_blockheader bh; struct cb_blockheader bh;
struct cb_recordheader rh; struct cb_recordheader rh;
...@@ -600,7 +594,10 @@ void * ...@@ -600,7 +594,10 @@ void *
void *buffert; void *buffert;
if (no<1) if (no<1)
{ log("cache: cb_readrecord: warning; no<1\n"); return 0; } {
log("cache: cb_readrecord: warning; no<1\n");
return 0;
}
fseek(cb->fp,BLOCKPOSITION(cb,bn),0); fseek(cb->fp,BLOCKPOSITION(cb,bn),0);
fread(&bh,sizeof(struct cb_blockheader),1,cb->fp); fread(&bh,sizeof(struct cb_blockheader),1,cb->fp);
...@@ -613,24 +610,32 @@ void * ...@@ -613,24 +610,32 @@ void *
fread(&rh,sizeof(struct cb_recordheader),1,cb->fp); fread(&rh,sizeof(struct cb_recordheader),1,cb->fp);
if ((bh.index[i]!=~0)&&(rh.no!=bh.index[i])) if ((bh.index[i]!=~0)&&(rh.no!=bh.index[i]))
{ {
restart_kom("cache: !record %lu in '%s', index(%lu)!=recordno(%lu) => corrupt",bn,cb->filename,bh.index[i],rh.no); restart_kom("cache: !record %lu in '%s', "
"index(%lu)!=recordno(%lu) => corrupt"
,bn,cb->filename,bh.index[i],rh.no);
return NULL; return NULL;
} }
if (rh.no==no) if (rh.no==no)
{ {
*size=rh.size; *size=rh.size;
*gen=rh.generation; *gen=rh.generation;
if (!rh.size) { return NULL; } /* recorden raderad */ if (!rh.size)
return NULL; /* recorden raderad */
buffert=smalloc(rh.size); buffert=smalloc(rh.size);
if (!buffert) if (!buffert)
{ restart_kom("cache: cb_readrecord: out of memory (%lu bytes)\n",rh.size); {
return NULL; } restart_kom("cache: cb_readrecord: out of memory (%lu bytes)\n",
rh.size);
return NULL;
}
fread(buffert,rh.size,1,cb->fp); fread(buffert,rh.size,1,cb->fp);
return buffert; return buffert;
} }
index+=rh.size+sizeof(struct cb_recordheader); index+=rh.size+sizeof(struct cb_recordheader);
} }
restart_kom("cache: !record %lu in '%s', missing record %lu => corrupt",bn,cb->filename,rh.no); restart_kom("cache: !record %lu in '%s', missing record %lu => corrupt",
bn,cb->filename,rh.no);
log("cache: cb_readrecord: warning, missing record %ld in block %ld.\n", log("cache: cb_readrecord: warning, missing record %ld in block %ld.\n",
no,bn); no,bn);
return 0; return 0;
...@@ -726,9 +731,12 @@ void ...@@ -726,9 +731,12 @@ void
if (i) if (i)
n=cb_writenew(cb,no,buffert,size,cb_getgeneration(cb,i,no)); n=cb_writenew(cb,no,buffert,size,cb_getgeneration(cb,i,no));
else else
n=cb_writenew(cb,no,buffert,size,0), {
cb->active_records++; n=cb_writenew(cb,no,buffert,size,0);
if (i&&n) cb_abandon(cb,i,no); cb->active_records++;
}
if (i && n)
cb_abandon(cb,i,no);
fflush(cb->fp); fflush(cb->fp);
fflush(cb->ifp); fflush(cb->ifp);
} }
...@@ -741,7 +749,11 @@ void * ...@@ -741,7 +749,11 @@ void *
unsigned long t1,t2; unsigned long t1,t2;
i=cb_search(cb,no); i=cb_search(cb,no);
if (!i) if (!i)
{ log("cache: warning: try to read data from non-existing record (%lu)\n",no); return NULL; } {
log("cache: warning: try to read data from non-existing record (%lu)\n",
no);
return NULL;
}
return cb_readrecord(cb,i,no,&t1,&t2); return cb_readrecord(cb,i,no,&t1,&t2);
} }
...@@ -750,9 +762,11 @@ void ...@@ -750,9 +762,11 @@ void
unsigned long no) unsigned long no)
{ {
unsigned long n,i; unsigned long n,i;
log("debug: erase record %lu\n",no); log("debug: erase record %lu\n",no);
i=cb_search(cb,no); i=cb_search(cb,no);
if (!i) return; /* well, erased is erased is erased */ if (!i)
return; /* well, erased is erased is erased */
cb->active_records--; cb->active_records--;
cb_writenew(cb,no,NULL,0,cb_getgeneration(cb,i,no)); cb_writenew(cb,no,NULL,0,cb_getgeneration(cb,i,no));
cb_abandon(cb,i,no); cb_abandon(cb,i,no);
...@@ -771,7 +785,8 @@ void cb_refresh(struct cachebase *cb) ...@@ -771,7 +785,8 @@ void cb_refresh(struct cachebase *cb)
/* test main ********/ /* test main ********/
/*int main() #if 0
int main()
{ {
struct cachebase *cb; struct cachebase *cb;
char *foo; char *foo;
...@@ -796,4 +811,4 @@ void cb_refresh(struct cachebase *cb) ...@@ -796,4 +811,4 @@ void cb_refresh(struct cachebase *cb)
if (cb) cb_close(cb); if (cb) cb_close(cb);
return 0; return 0;
} }
*/ #endif
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