diff --git a/src/server/cache-database.c b/src/server/cache-database.c index 638f4827203b9439ad41413ac87c84cabae6d238..7146d6e5c970d9cfd983e88c3229bd6ec62b4463 100644 --- a/src/server/cache-database.c +++ b/src/server/cache-database.c @@ -13,13 +13,10 @@ #include <stdlib.h> #include <time.h> #include "cache-database.h" +#include "smalloc.h" /* misc. defines *********************************************/ -#define smalloc malloc -#define sfree free -#define srealloc realloc - #define TIME (time(NULL)) #define SAFESTORAGETIME 30 /* seconds to regen blocks */ #define cb_addsafestoragetime(x) ((x)+SAFESTORAGETIME) @@ -80,41 +77,6 @@ static unsigned long cb_getgeneration(struct cachebase *cb,unsigned long bn, 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 ****************************************/ static unsigned long cb_sizeoflist(struct cb_blocklist *cbb) @@ -145,7 +107,8 @@ static struct cachebase * cb->fp=fopen(filename,"r+b"); 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); fread(&(cb->header),sizeof(cb->header),1,cb->fp); @@ -476,22 +439,40 @@ static unsigned long rh.ts_created=TIME; fseek(cb->fp,BLOCKPOSITION(cb,bn)+index,0); 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 (!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); bh.index[i]=no; bh.ts_written=TIME; 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); 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) { @@ -501,7 +482,8 @@ log("debug: highnumber set to %lu\n",no); cb->header.ts_written=TIME; 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; } } @@ -526,7 +508,8 @@ static void fseek(cb->fp,BLOCKPOSITION(cb,bn),0); 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; } @@ -539,7 +522,9 @@ static void fread(&rh,sizeof(struct cb_recordheader),1,cb->fp); 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; } index+=rh.size+sizeof(struct cb_recordheader); @@ -561,9 +546,15 @@ static unsigned long unsigned long index; 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) - { 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); fread(&bh,sizeof(struct cb_blockheader),1,cb->fp); @@ -576,22 +567,25 @@ static unsigned long fread(&rh,sizeof(struct cb_recordheader),1,cb->fp); 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; } if (rh.no==no) return rh.generation; 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", no,bn); return 0; } void * - cb_readrecord(struct cachebase *cb,unsigned long bn, - unsigned long no,unsigned long *size, - unsigned long *gen) +cb_readrecord(struct cachebase *cb,unsigned long bn, + unsigned long no,unsigned long *size, + unsigned long *gen) { struct cb_blockheader bh; struct cb_recordheader rh; @@ -600,7 +594,10 @@ void * void *buffert; 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); fread(&bh,sizeof(struct cb_blockheader),1,cb->fp); @@ -613,24 +610,32 @@ void * fread(&rh,sizeof(struct cb_recordheader),1,cb->fp); 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; } if (rh.no==no) { *size=rh.size; *gen=rh.generation; - if (!rh.size) { return NULL; } /* recorden raderad */ + if (!rh.size) + return NULL; /* recorden raderad */ + buffert=smalloc(rh.size); 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); return buffert; } 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", no,bn); return 0; @@ -726,9 +731,12 @@ void if (i) n=cb_writenew(cb,no,buffert,size,cb_getgeneration(cb,i,no)); else - n=cb_writenew(cb,no,buffert,size,0), - cb->active_records++; - if (i&&n) cb_abandon(cb,i,no); + { + n=cb_writenew(cb,no,buffert,size,0); + cb->active_records++; + } + if (i && n) + cb_abandon(cb,i,no); fflush(cb->fp); fflush(cb->ifp); } @@ -741,7 +749,11 @@ void * unsigned long t1,t2; i=cb_search(cb,no); 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); } @@ -750,9 +762,11 @@ void unsigned long no) { unsigned long n,i; + log("debug: erase record %lu\n",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_writenew(cb,no,NULL,0,cb_getgeneration(cb,i,no)); cb_abandon(cb,i,no); @@ -771,7 +785,8 @@ void cb_refresh(struct cachebase *cb) /* test main ********/ -/*int main() +#if 0 +int main() { struct cachebase *cb; char *foo; @@ -796,4 +811,4 @@ void cb_refresh(struct cachebase *cb) if (cb) cb_close(cb); return 0; } -*/ +#endif