Corrected error codes in doxygen function doc; corrected problem with return value...
[profile/ivi/persistence-client-library.git] / src / persistence_client_library_backup_filelist.c
1 /******************************************************************************
2  * Project         Persistency
3  * (c) copyright   2012
4  * Company         XS Embedded GmbH
5  *****************************************************************************/
6 /******************************************************************************
7  * This Source Code Form is subject to the terms of the
8  * Mozilla Public License, v. 2.0. If a  copy of the MPL was not distributed
9  * with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 ******************************************************************************/
11  /**
12  * @file           persistence_client_library_backup_filelist.c
13  * @ingroup        Persistence client library
14  * @author         Ingo Huerner
15  * @brief          Implementation of persistence client library backup filelist
16  * @see
17  */
18
19 #include "persistence_client_library_backup_filelist.h"
20 #include "rbtree.h"
21 #include "../include_protected/crc32.h"
22 #include "../include_protected/persistence_client_library_data_organization.h"
23
24
25 #include <fcntl.h>
26 #include <string.h>
27 #include <sys/mman.h>
28 #include <sys/stat.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33
34
35 /// structure definition for a key value item
36 typedef struct _key_value_s
37 {
38    unsigned int key;
39    char*        value;
40 }key_value_s;
41
42
43 void  key_val_rel(void *p);
44
45 void* key_val_dup(void *p);
46
47 int key_val_cmp(const void *p1, const void *p2 );
48
49
50
51 /// the size of the token array
52 enum configConstants
53 {
54    TOKENARRAYSIZE = 255
55 };
56
57
58 const char gCharLookup[] =
59 {
60    0,0,0,0,0,0,0,0,  0,0,0,0,0,0,0,0,  0,0,0,0,0,0,0,0,  0,0,0,0,0,0,0,0,  // from 0x0 (NULL)  to 0x1F (unit seperator)
61    0,1,1,1,1,1,1,1,  1,1,1,1,1,1,1,1,  1,1,1,1,1,1,1,1,  1,1,1,1,1,1,1,1,  // from 020 (space) to 0x2F (?)
62    1,1,1,1,1,1,1,1,  1,1,1,1,1,1,1,1,  1,1,1,1,1,1,1,1,  1,1,1,1,1,1,1,1,  // from 040 (@)     to 0x5F (_)
63    1,1,1,1,1,1,1,1,  1,1,1,1,1,1,1,1,  1,1,1,1,1,1,1,1,  1,1,1,1,1,1,1     // from 060 (')     to 0x7E (~)
64 };
65
66
67 char* gpConfigFileMap = 0;
68 char* gpTokenArray[TOKENARRAYSIZE] = {0};
69 int gTokenCounter = 0;
70 unsigned int gConfigFileSize = 0;
71
72
73 /// the rb tree
74 static jsw_rbtree_t *gRb_tree_bl = NULL;
75
76 void fillCharTokenArray()
77 {
78    unsigned int i=0;
79    int blankCount=0;
80    char* tmpPointer = gpConfigFileMap;
81
82    // set the first pointer to the start of the file
83    gpTokenArray[blankCount] = tmpPointer;
84    blankCount++;
85
86    while(i < gConfigFileSize)
87    {
88       if(   ((unsigned int)*tmpPointer < 127)
89          && ((unsigned int)*tmpPointer >= 0))
90            {
91                    if(1 != gCharLookup[(unsigned int)*tmpPointer])
92                    {
93                            *tmpPointer = 0;
94
95                            // check if we are at the end of the token array
96                            if(blankCount >= TOKENARRAYSIZE)
97                            {
98                                    break;
99                            }
100                            gpTokenArray[blankCount] = tmpPointer+1;
101                            blankCount++;
102                            gTokenCounter++;
103                    }
104            }
105       tmpPointer++;
106            i++;
107    }
108 }
109
110
111 void createAndStoreFileNames()
112 {
113    int i= 0, j =0;
114    char path[128];
115    const char* gFilePostFix                = ".pers";
116    const char* gKeyPathFormat              = "/%s/%s/%s/%s/%s%s";
117    key_value_s* item;
118
119    // creat new tree
120    gRb_tree_bl = jsw_rbnew(key_val_cmp, key_val_dup, key_val_rel);
121
122    if(gRb_tree_bl != NULL)
123    {
124
125       for(i=0; i<128; i++)
126       {
127          // assemble path
128          snprintf(path, 128, gKeyPathFormat, gpTokenArray[j+2],      // storage type
129                                              gpTokenArray[j+3],      // policy id
130                                              gpTokenArray[j+4],      // profileID
131                                              gpTokenArray[j],        // application id
132                                              gpTokenArray[j+1],      // filename
133                                              gFilePostFix);          // file postfix
134
135          // asign key and value to the rbtree item
136          item = malloc(sizeof(key_value_s));
137          if(item != NULL)
138          {
139             item->key = pclCrc32(0, (unsigned char*)path, strlen(path));
140             // we don't need the path name here, we just need to know that this key is available in the tree
141             item->value = "";
142             jsw_rbinsert(gRb_tree_bl, item);
143             free(item);
144          }
145          j+=5;
146          if(gpTokenArray[j] == NULL)
147          {
148             break;
149          }
150       }
151    }
152
153 }
154
155
156 int readBlacklistConfigFile(const char* filename)
157 {
158    int fd = 0,
159        status = 0,
160        rval = 0;
161
162    struct stat buffer;
163
164    if(filename != NULL)
165    {
166
167            memset(&buffer, 0, sizeof(buffer));
168            status = stat(filename, &buffer);
169            if(status != -1)
170            {
171                   gConfigFileSize = buffer.st_size;
172            }
173
174            fd = open(filename, O_RDONLY);
175            if (fd == -1)
176            {
177                   DLT_LOG(gDLTContext, DLT_LOG_WARN, DLT_STRING("configReader::readConfigFile ==> Error file open"), DLT_STRING(filename), DLT_STRING(strerror(errno)) );
178                   return -1;
179            }
180
181            // check for empty file
182            if(gConfigFileSize == 0)
183            {
184                   DLT_LOG(gDLTContext, DLT_LOG_WARN, DLT_STRING("configReader::readConfigFile ==> Error file size is 0:"), DLT_STRING(filename));
185                   close(fd);
186                   return -1;
187            }
188
189            // map the config file into memory
190            gpConfigFileMap = (char*)mmap(0, gConfigFileSize, PROT_WRITE, MAP_PRIVATE, fd, 0);
191
192            if (gpConfigFileMap == MAP_FAILED)
193            {
194                   gpConfigFileMap = 0;
195                   close(fd);
196                   DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("configReader::readConfigFile ==> Error mapping the file:"), DLT_STRING(filename), DLT_STRING(strerror(errno)) );
197
198                   return -1;
199            }
200
201            // reset the token counter
202            gTokenCounter = 0;
203
204            fillCharTokenArray();
205
206            // create filenames and store them in the tree
207            createAndStoreFileNames();
208
209            munmap(gpConfigFileMap, gConfigFileSize);
210
211            close(fd);
212    }
213    else
214    {
215            rval = -1;
216    }
217
218    return rval;
219 }
220
221
222
223 int need_backup_key(unsigned int key)
224 {
225    int rval = 1;
226    key_value_s* item = NULL;
227    key_value_s* foundItem = NULL;
228
229    item = malloc(sizeof(key_value_s));
230    if(item != NULL && gRb_tree_bl != NULL)
231    {
232       item->key = key;
233       foundItem = (key_value_s*)jsw_rbfind(gRb_tree_bl, item);
234       if(foundItem != NULL)
235       {
236          rval = 0;
237       }
238       free(item);
239    }
240    else
241    {
242       if(item!=NULL)
243              free(item);
244
245       rval = -1;
246       DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("need_backup_key ==> item or gRb_tree_bl is NULL"));
247    }
248
249    return rval;
250 }
251
252
253 /// compare function for tree key_value_s item
254 int key_val_cmp(const void *p1, const void *p2 )
255 {
256    int rval = -1;
257    key_value_s* first;
258    key_value_s* second;
259
260    first  = (key_value_s*)p1;
261    second = (key_value_s*)p2;
262
263    if(second->key == first->key)
264    {
265       rval = 0;
266    }
267    else if(second->key < first->key)
268    {
269       rval = -1;
270    }
271    else
272    {
273       rval = 1;
274    }
275
276    return rval;
277  }
278
279 /// duplicate function for key_value_s item
280 void* key_val_dup(void *p)
281 {
282    int value_size = 0;
283    key_value_s* src = NULL;
284    key_value_s* dst = NULL;
285
286    src = (key_value_s*)p;
287    value_size = strlen(src->value)+1;
288
289    // allocate memory for node
290    dst = malloc(sizeof(key_value_s));
291    if(dst != NULL)
292    {
293       // duplicate hash key
294      dst->key = src->key;
295
296      // duplicate value
297      dst->value = malloc(value_size);
298      if(dst->value != NULL)
299         strncpy(dst->value, src->value, value_size);
300    }
301
302
303    return dst;
304 }
305
306 /// release function for key_value_s item
307 void  key_val_rel(void *p )
308 {
309    key_value_s* rel = NULL;
310    rel = (key_value_s*)p;
311
312    if(rel->value != NULL)
313       free(rel->value);
314
315    if(rel != NULL)
316       free(rel);
317 }
318
319