diff options
Diffstat (limited to 'src/common/utils.c')
-rw-r--r-- | src/common/utils.c | 57 |
1 files changed, 35 insertions, 22 deletions
diff --git a/src/common/utils.c b/src/common/utils.c index c168bd74e..ad68706ca 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -354,40 +354,51 @@ const char* timestamp2string(char* str, size_t size, time_t timestamp, const cha /* [Ind/Hercules] Caching */ -bool HCache_check(const char *file) { +bool HCache_check(const char *file) +{ struct stat bufa, bufb; FILE *first, *second; char s_path[255], dT[1]; time_t rtime; - if( !(first = fopen(file,"rb")) ) + if (!(first = fopen(file,"rb"))) return false; - if( file[0] == '.' && file[1] == '/' ) + if (file[0] == '.' && file[1] == '/') file += 2; - else if( file[0] == '.' ) + else if (file[0] == '.') file++; snprintf(s_path, 255, "./cache/%s", file); - if( !(second = fopen(s_path,"rb")) ) { + if (!(second = fopen(s_path,"rb"))) { fclose(first); return false; } - if( fread(dT,sizeof(dT),1,second) != 1 || fread(&rtime,sizeof(rtime),1,second) != 1 || dT[0] != HCACHE_KEY || HCache->recompile_time > rtime ) { + if (fread(dT,sizeof(dT),1,second) != 1 + || fread(&rtime,sizeof(rtime),1,second) != 1 + || dT[0] != HCACHE_KEY + || HCache->recompile_time > rtime) { fclose(first); fclose(second); return false; } - fstat(fileno(first), &bufa); - fstat(fileno(second), &bufb); - + if (fstat(fileno(first), &bufa) != 0) { + fclose(first); + fclose(second); + return false; + } fclose(first); + + if (fstat(fileno(second), &bufb) != 0) { + fclose(second); + return false; + } fclose(second); - if( bufa.st_mtime > bufb.st_mtime ) + if (bufa.st_mtime > bufb.st_mtime) return false; return true; @@ -414,24 +425,26 @@ FILE *HCache_open(const char *file, const char *opt) { hwrite(dT,sizeof(dT),1,first); hwrite(&HCache->recompile_time,sizeof(HCache->recompile_time),1,first); } - fseek(first, 20, SEEK_SET);/* skip first 20, might wanna store something else later */ + if (fseek(first, 20, SEEK_SET) != 0) { // skip first 20, might wanna store something else later + fclose(first); + return NULL; + } return first; } -void HCache_init(void) { - FILE *server; - - if( (server = fopen(SERVER_NAME,"rb")) ) { - struct stat buf; - - fstat(fileno(server), &buf); - HCache->recompile_time = buf.st_mtime; - fclose(server); - HCache->enabled = true; - } else +void HCache_init(void) +{ + struct stat buf; + if (stat(SERVER_NAME, &buf) != 0) { ShowWarning("Unable to open '%s', caching capabilities have been disabled!\n",SERVER_NAME); + return; + } + + HCache->recompile_time = buf.st_mtime; + HCache->enabled = true; } + /* transit to fread, shields vs warn_unused_result */ size_t hread(void * ptr, size_t size, size_t count, FILE * stream) { return fread(ptr, size, count, stream); |