change global variable position
[framework/uifw/cbhm.git] / src / storage.c
1 #include "common.h"
2 #include "cbhm_main.h"
3 #include "storage.h"
4
5 /*
6    file structure 
7
8    /---------------------------------------------------------------------------
9     |header|current_position|total_count|item_header|item_body(512kib)|item...|
10     --------------------------------------------------------------------------/
11
12 */
13
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)
21
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)
24
25 static FILE *g_storage_file = NULL;
26 static unsigned int g_storage_serial_number = 0;
27
28 int init_storage(void *data)
29 {
30         struct appdata *ad = data;
31
32         int i;
33         int result = 0;
34
35         if (g_storage_file)
36                 return 1;
37
38         g_storage_file = fopen(STORAGE_FILEPATH, "r+");
39         if (!g_storage_file)
40         {  // making data savefile
41                 g_storage_file = fopen(STORAGE_FILEPATH, "w+");
42
43                 if (!g_storage_file)
44                 {
45                         close_storage(ad);
46                         DTRACE("Error : failed openning file for writing\n");
47                         return -1;
48                 }
49
50                 result = fseek(g_storage_file, TOTAL_STORAGE_SIZE-1, SEEK_SET);
51                 if (!result) 
52                 {
53                         close_storage(ad);
54                         DTRACE("Error : failed moving file position to file's end\n");
55                         return -1;
56                 }
57
58                 result = fputc(0, g_storage_file);
59                 if (result == EOF)
60                 {
61                 DTRACE("Error : failed writing to file's end\n");
62                 return -1;
63                 }
64         }
65
66         DTRACE("Success : storage init is done\n");
67
68         g_storage_serial_number = 0;
69
70         return 0;
71 }
72
73 int sync_storage(void *data)
74 {
75 //      struct appdata *ad = data;
76
77         if (!g_storage_file)
78         {
79                 DTRACE("g_storage_file is null\n");
80                 return -1;
81         }
82         fsync(g_storage_file);
83
84         return 0;
85 }
86
87 unsigned int get_storage_serial_code(void *data)
88 {
89 //      struct appdata *ad = data;
90
91         return g_storage_serial_number;
92 }
93
94 int adding_item_to_storage(void *data, int pos, char *idata)
95 {
96 //      struct appdata *ad = data;
97         if (!g_storage_file)
98         {
99                 DTRACE("g_storage_file is null\n");
100                 return -1;
101         }
102
103         int result;
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);
108         
109         g_storage_serial_number++;
110         return 0;
111 }
112
113 int get_item_counts(void *data)
114 {
115         struct appdata *ad = data;
116
117         return ad->hicount;
118 }
119
120 int close_storage(void *data)
121 {
122         struct appdata *ad = data;
123
124         if (g_storage_file)
125                 fclose(g_storage_file);
126         g_storage_file = NULL;
127         g_storage_serial_number = 0;
128
129         return 0;
130 }
131
132 int check_regular_file(char *path)
133 {
134         struct stat fattr;
135         if (stat(path, &fattr))
136         {
137                 DTRACE("Cannot get file at path = %s\n", path);
138                 return FALSE;
139         }
140         
141         return S_ISREG(fattr.st_mode);
142 }
143