[cbhm] copy&paste error in webkit
[framework/uifw/cbhm.git] / src / storage.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  */
17
18 #include <Ecore_File.h>
19
20 #include "storage.h"
21 #define STORAGE_FILEPATH "/opt/var/.savecbh"
22 #define STORAGE_KEY_INDEX "index"
23 #define STORAGE_KEY_FORMAT "data%02d"
24 #define STORAGE_INDEX_ITEM_NONE 0.0
25
26 static void storage_item_index_wrote(StorageData *sd);
27 static Eina_Bool item_write(Eet_File *ef, int index, CNP_ITEM *item);
28 static void storage_rewrite_all_items(StorageData *sd);
29 static Eina_Bool storage_index_write(StorageData *sd);
30 static Eina_Bool storage_item_write(AppData *ad, CNP_ITEM *item);
31 static Eina_Bool storage_item_delete(AppData *ad, CNP_ITEM *item);
32 static CNP_ITEM *storage_item_load(StorageData *sd, int index);
33 #ifdef DEBUG
34 static void dump_items(StorageData *sd);
35 #else
36 #define dunp_items(a)
37 #endif
38
39 static int getMinIndex(indexType *indexTable, int len)
40 {
41         int i = 0;
42         int minIndex;
43         indexType min;
44         min = indexTable[i];
45         minIndex = i;
46
47         for (i = 1; i < len; i++)
48         {
49                 if ((min > indexTable[i]))
50                 {
51                         min = indexTable[i];
52                         minIndex = i;
53                 }
54         }
55         return minIndex;
56 }
57
58 static int getMaxIndex(indexType *indexTable, int len)
59 {
60         int i = 0;
61         indexType max = indexTable[i];
62         int maxIndex = i;
63         for (i = 1; i < len; i++)
64         {
65                 if (max < indexTable[i])
66                 {
67                         max = indexTable[i];
68                         maxIndex = i;
69                 }
70         }
71         return maxIndex;
72 }
73
74 StorageData *init_storage(AppData *ad)
75 {
76         CALLED();
77         StorageData *sd = CALLOC(1, sizeof(StorageData));
78         eet_init();
79         ecore_file_init();
80
81         sd->ef = eet_open(STORAGE_FILEPATH, EET_FILE_MODE_READ_WRITE);
82         /*
83         if (sd->ef)
84         {
85                 int read_size;
86                 indexType *read_data;
87                 read_data = eet_read(sd->ef, STORAGE_KEY_INDEX, &read_size);
88
89                 int storage_size = sizeof(indexType) * STORAGE_ITEM_CNT;
90
91                 int copy_size = storage_size < read_size ? storage_size : read_size;
92
93                 if (read_data)
94                 {
95                         indexType *temp = MALLOC(read_size);
96                         if (!temp)
97                                 return sd;
98                         memcpy(temp, read_data, read_size);
99
100                         int i;
101                         int copy_cnt = copy_size/sizeof(indexType);
102                         for (i = 0; i < copy_cnt; i++)
103                         {
104                                 int maxIndex = getMaxIndex(temp, copy_cnt);
105                                 if (temp[maxIndex] == STORAGE_INDEX_ITEM_NONE)
106                                         break;
107                                 sd->itemTable[i] = storage_item_load(sd, maxIndex);
108                                 if (sd->itemTable[i])
109                                         sd->indexTable[i] = temp[maxIndex];
110                                 temp[maxIndex] = STORAGE_INDEX_ITEM_NONE;
111
112                                 DMSG("load storage item index %d\n", i);
113                         }
114                         for (i = copy_cnt - 1; i >= 0; i--)
115                         {
116                                 if (sd->itemTable[i])
117                                         item_add_by_CNP_ITEM(ad, sd->itemTable[i]);
118                         }
119                 }
120                 else
121                 {
122                         DMSG("load storage index failed\n");
123                 }
124         }
125         else
126                 DMSG("storage ef is NULLd\n");
127         */
128         dump_items(sd);
129
130         ad->storage_item_add = storage_item_write;
131         ad->storage_item_del = storage_item_delete;
132 //      ad->storage_item_load = storage_item_load;
133
134         return sd;
135 }
136
137 void depose_storage(StorageData *sd)
138 {
139         CALLED();
140         storage_rewrite_all_items(sd);
141         dump_items(sd);
142         if (sd->ef)
143                 eet_close(sd->ef);
144         sd->ef = NULL;
145         eet_shutdown();
146         ecore_file_shutdown();
147 }
148
149 #ifdef DEBUG
150 static void dump_items(StorageData *sd)
151 {
152         CALLED();
153         int i;
154         for (i = 0; i < STORAGE_ITEM_CNT; i++)
155         {
156                 CNP_ITEM *item = storage_item_load(sd, i);
157                 if (item)
158                         printf("item #%d type: 0x%x, data: %s\n, len: %d\n", i, item->type_index, item->data, item->len);
159         }
160 }
161 #endif
162
163 static Eina_Bool item_write(Eet_File *ef, int index, CNP_ITEM *item)
164 {
165         if (!ef)
166         {
167                 DMSG("eet_file is NULL\n");
168                 return EINA_FALSE;
169         }
170         char datakey[10];
171         snprintf(datakey, 10, STORAGE_KEY_FORMAT, index);
172         int buf_size = item->len + sizeof(int);
173         char *buf = MALLOC(buf_size);
174         if (!buf)
175                 return EINA_FALSE;
176         ((int *)buf)[0] = item->type_index;
177         char *data = buf + sizeof(int);
178         memcpy(data, item->data, item->len);
179
180         int ret = eet_write(ef, datakey, buf, buf_size, 1);
181         DMSG("write result: %d, datakey: %s, buf_size: %d, item_len: %d\n", ret, datakey, buf_size, item->len);
182 /*      if (ret)
183                 eet_sync(ef);*/
184         return ret != 0;
185 }
186
187 static void storage_rewrite_all_items(StorageData *sd)
188 {
189         CALLED();
190         if (sd->ef)
191                 eet_close(sd->ef);
192         ecore_file_remove(STORAGE_FILEPATH);
193         sd->ef = eet_open(STORAGE_FILEPATH, EET_FILE_MODE_READ_WRITE);
194
195         int i;
196         for (i = 0; i < STORAGE_ITEM_CNT; i++)
197         {
198                 if ((sd->indexTable[i] != STORAGE_INDEX_ITEM_NONE) && (sd->itemTable[i]))
199                         item_write(sd->ef, i, sd->itemTable[i]);
200         }
201         storage_index_write(sd);
202 }
203
204 static Eina_Bool storage_item_write(AppData *ad, CNP_ITEM *item)
205 {
206         CALLED();
207         StorageData *sd = ad->storage;
208         int index = getMinIndex(sd->indexTable, STORAGE_ITEM_CNT);
209         sd->indexTable[index] = ecore_time_unix_get();
210         sd->itemTable[index] = item;
211
212         item_write(sd->ef, index, item);
213         storage_index_write(sd);
214         dump_items(sd);
215         return EINA_TRUE;
216 }
217
218 static Eina_Bool storage_item_delete(AppData *ad, CNP_ITEM *item)
219 {
220         CALLED();
221         StorageData *sd = ad->storage;
222         int index;
223         for (index = 0; index < STORAGE_ITEM_CNT; index++)
224         {
225                 if (sd->itemTable[index] == item)
226                         break;
227         }
228
229         if (index < STORAGE_ITEM_CNT)
230         {
231                 sd->indexTable[index] = STORAGE_INDEX_ITEM_NONE;
232                 storage_index_write(sd);
233         }
234         return EINA_TRUE;
235 }
236
237 static CNP_ITEM *storage_item_load(StorageData *sd, int index)
238 {
239         if (!sd->ef)
240         {
241                 DMSG("eet_file is NULL\n");
242                 return EINA_FALSE;
243         }
244         if (index >= STORAGE_ITEM_CNT)
245                 return NULL;
246
247         indexType copyTable[STORAGE_ITEM_CNT];
248         memcpy(copyTable, sd->indexTable, sizeof(copyTable));
249         int i;
250         for (i = 0; i < index; i++)
251         {
252                 int maxIndex = getMaxIndex(copyTable, STORAGE_ITEM_CNT);
253                 if (maxIndex == -1)
254                         maxIndex = 0;
255                 copyTable[maxIndex] = 0;
256         }
257
258         char datakey[10];
259         snprintf(datakey, 10, STORAGE_KEY_FORMAT, i);
260
261         int read_size;
262         char *read_data = eet_read(sd->ef, datakey, &read_size);
263
264         if (!read_data)
265         {
266                 DMSG("read failed index: %d\n", index);
267                 return NULL;
268         }
269         CNP_ITEM *item = CALLOC(1, sizeof(CNP_ITEM));
270         if (item)
271         {
272                 char *data = read_data + sizeof(int);
273                 int data_size = read_size - sizeof(int);
274                 char *buf = CALLOC(1, data_size);
275                 if (!buf)
276                 {
277                         FREE(item);
278                         return NULL;
279                 }
280                 item->type_index = ((int *)read_data)[0];
281                 memcpy(buf, data, data_size);
282                 item->data = buf;
283                 item->len = data_size;
284         }
285         return item;
286 }
287
288 static Eina_Bool storage_index_write(StorageData *sd)
289 {
290         CALLED();
291         int ret;
292         if (!sd->ef)
293         {
294                 DMSG("eet_file is NULL\n");
295                 return EINA_FALSE;
296         }
297 #ifdef DEBUG
298         for (ret = 0; ret < STORAGE_ITEM_CNT; ret++)
299                 printf(", index %d: %lf", ret, sd->indexTable[ret]);
300         printf("\n");
301 #endif
302         ret = eet_write(sd->ef, STORAGE_KEY_INDEX, sd->indexTable, sizeof(indexType) * STORAGE_ITEM_CNT, 1);
303         if (ret)
304                 eet_sync(sd->ef);
305         return ret != 0;
306 }