8 /---------------------------------------------------------------------------
9 |header|current_position|total_count|item_header|item_body(512kib)|item...|
10 --------------------------------------------------------------------------/
14 #define STORAGE_FILEPATH "/opt/var/.savecbh"
15 #define STORAGE_MAX_ITEMS HISTORY_QUEUE_MAX_ITEMS
16 #define HEADER_ITEM_SIZE (sizeof(int))
17 #define BODY_ITEM_SIZE HISTORY_QUEUE_ITEM_SIZE
18 #define STORAGE_HEADER_SIZE (STORAGE_MAX_ITEMS * HEADER_ITEM_SIZE)
19 #define STORAGE_BODY_SIZE (STORAGE_MAX_ITEMS * BODY_ITEM_SIZE)
20 #define TOTAL_STORAGE_SIZE (STORAGE_HEADER_SIZE+STORAGE_BODY_SIZE)
22 #define GET_HEADER_ADDR_BY_POSITION(pos) (STORAGE_HEADER_SIZE+pos*(HEADER_ITEM_SIZE+BODY_ITEM_SIZE))
23 #define GET_BODY_ADDR_BY_POSITION(pos) (GET_HEADER_ADDR_BY_POSITION(pos)+HEADER_ITEM_SIZE)
25 static FILE *g_storage_file = NULL;
26 static unsigned int g_storage_serial_number = 0;
28 int init_storage(void *data)
30 struct appdata *ad = data;
38 g_storage_file = fopen(STORAGE_FILEPATH, "r+");
40 { // making data savefile
41 g_storage_file = fopen(STORAGE_FILEPATH, "w+");
46 DTRACE("Error : failed openning file for writing\n");
50 result = fseek(g_storage_file, TOTAL_STORAGE_SIZE-1, SEEK_SET);
54 DTRACE("Error : failed moving file position to file's end\n");
58 result = fputc(0, g_storage_file);
61 DTRACE("Error : failed writing to file's end\n");
66 DTRACE("Success : storage init is done\n");
68 g_storage_serial_number = 0;
73 int sync_storage(void *data)
75 // struct appdata *ad = data;
79 DTRACE("g_storage_file is null\n");
82 fsync(g_storage_file);
87 unsigned int get_storage_serial_code(void *data)
89 // struct appdata *ad = data;
91 return g_storage_serial_number;
94 int adding_item_to_storage(void *data, int pos, char *idata)
96 // struct appdata *ad = data;
99 DTRACE("g_storage_file is null\n");
104 result = fseek(g_storage_file, GET_HEADER_ADDR_BY_POSITION(pos), SEEK_SET);
105 // FIXME : replace from fprintf to fwrite
106 fprintf(g_storage_file, "%d", strlen(idata));
107 fprintf(g_storage_file, "%s", idata);
109 g_storage_serial_number++;
113 int get_item_counts(void *data)
115 struct appdata *ad = data;
120 int close_storage(void *data)
122 struct appdata *ad = data;
125 fclose(g_storage_file);
126 g_storage_file = NULL;
127 g_storage_serial_number = 0;
132 int check_regular_file(char *path)
135 if (stat(path, &fattr))
137 DTRACE("Cannot get file at path = %s\n", path);
141 return S_ISREG(fattr.st_mode);