00001 00007 #include <stdbool.h> 00008 #include <stdint.h> 00009 #include <stdint.h> 00010 #include <stdio.h> 00011 #include <stdlib.h> 00012 #include <string.h> 00013 00014 #include "check.h" 00015 #include "db5_hdr.h" 00016 #include "db5_types.h" 00017 #include "file.h" 00018 #include "logger.h" 00019 00021 #define DB5_HDR_COUNT_OFFSET 1040 00022 00024 static FILE *db5_hdr; 00025 00027 static uint32_t count; 00028 00029 bool db5_hdr_init() 00030 { 00031 db5_hdr = file_fcaseopen(CONFIG_DB5_DATA_DIR, CONFIG_DB5_HDR_FILE, "rb+"); 00032 00033 if (db5_hdr == NULL) 00034 { 00035 add_log(ADDLOG_CRITICAL, "[db5/hdr]init", "unable to init database\n"); 00036 return false; 00037 } 00038 00039 if (fseek(db5_hdr, DB5_HDR_COUNT_OFFSET, SEEK_SET) != 0) 00040 { 00041 add_log(ADDLOG_CRITICAL, "[db5/hdr]init", "unable to find count value\n"); 00042 return false; 00043 } 00044 if (fread(&count, sizeof(count), 1, db5_hdr) != 1) 00045 { 00046 add_log(ADDLOG_CRITICAL, "[db5/hdr]init", "unable to read count value\n"); 00047 return false; 00048 } 00049 00050 return true; 00051 } 00052 00053 bool db5_hdr_free() 00054 { 00055 fclose(db5_hdr); 00056 00057 return true; 00058 } 00059 00060 uint32_t db5_hdr_count() 00061 { 00062 if (count == 0) 00063 { 00064 add_log(ADDLOG_NOTICE, "[db5/hdr]get", "the value count is zero\n"); 00065 } 00066 return count; 00067 } 00068 00069 bool db5_hdr_grow(const int delta) 00070 { 00071 count += delta; 00072 00073 if (fseek(db5_hdr, DB5_HDR_COUNT_OFFSET, SEEK_SET) != 0) 00074 { 00075 add_log(ADDLOG_FAIL, "[db5/hdr]add", "unable to find count value\n"); 00076 return false; 00077 } 00078 if (fwrite(&count, sizeof(count), 1, db5_hdr) != 1) 00079 { 00080 add_log(ADDLOG_FAIL, "[db5/hdr]add", "unable to write count value\n"); 00081 return false; 00082 } 00083 if (fflush(db5_hdr) != 0) 00084 { 00085 add_log(ADDLOG_FAIL, "[db5/hdr]add", "unable to write count value (flush)\n"); 00086 return false; 00087 } 00088 00089 return true; 00090 } 00091