1 /******************************************************************************
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 ******************************************************************************/
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
19 #include "persistence_client_library_backup_filelist.h"
21 #include "../include_protected/crc32.h"
22 #include "../include_protected/persistence_client_library_data_organization.h"
35 /// structure definition for a key value item
36 typedef struct _key_value_s
43 void key_val_rel(void *p);
45 void* key_val_dup(void *p);
47 int key_val_cmp(const void *p1, const void *p2 );
51 /// the size of the token array
58 const char gCharLookup[] =
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 (~)
67 char* gpConfigFileMap = 0;
68 char* gpTokenArray[TOKENARRAYSIZE] = {0};
69 int gTokenCounter = 0;
70 unsigned int gConfigFileSize = 0;
74 static jsw_rbtree_t *gRb_tree_bl = NULL;
76 void fillCharTokenArray()
80 char* tmpPointer = gpConfigFileMap;
82 // set the first pointer to the start of the file
83 gpTokenArray[blankCount] = tmpPointer;
86 while(i < gConfigFileSize)
88 if( ((unsigned int)*tmpPointer < 127)
89 && ((unsigned int)*tmpPointer >= 0))
91 if(1 != gCharLookup[(unsigned int)*tmpPointer])
95 // check if we are at the end of the token array
96 if(blankCount >= TOKENARRAYSIZE)
100 gpTokenArray[blankCount] = tmpPointer+1;
111 void createAndStoreFileNames()
115 const char* gFilePostFix = ".pers";
116 const char* gKeyPathFormat = "/%s/%s/%s/%s/%s%s";
120 gRb_tree_bl = jsw_rbnew(key_val_cmp, key_val_dup, key_val_rel);
122 if(gRb_tree_bl != NULL)
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
135 // asign key and value to the rbtree item
136 item = malloc(sizeof(key_value_s));
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
142 jsw_rbinsert(gRb_tree_bl, item);
146 if(gpTokenArray[j] == NULL)
156 int readBlacklistConfigFile(const char* filename)
167 memset(&buffer, 0, sizeof(buffer));
168 status = stat(filename, &buffer);
171 gConfigFileSize = buffer.st_size;
174 fd = open(filename, O_RDONLY);
177 DLT_LOG(gDLTContext, DLT_LOG_WARN, DLT_STRING("configReader::readConfigFile ==> Error file open"), DLT_STRING(filename), DLT_STRING(strerror(errno)) );
181 // check for empty file
182 if(gConfigFileSize == 0)
184 DLT_LOG(gDLTContext, DLT_LOG_WARN, DLT_STRING("configReader::readConfigFile ==> Error file size is 0:"), DLT_STRING(filename));
189 // map the config file into memory
190 gpConfigFileMap = (char*)mmap(0, gConfigFileSize, PROT_WRITE, MAP_PRIVATE, fd, 0);
192 if (gpConfigFileMap == MAP_FAILED)
196 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("configReader::readConfigFile ==> Error mapping the file:"), DLT_STRING(filename), DLT_STRING(strerror(errno)) );
201 // reset the token counter
204 fillCharTokenArray();
206 // create filenames and store them in the tree
207 createAndStoreFileNames();
209 munmap(gpConfigFileMap, gConfigFileSize);
223 int need_backup_key(unsigned int key)
226 key_value_s* item = NULL;
227 key_value_s* foundItem = NULL;
229 item = malloc(sizeof(key_value_s));
230 if(item != NULL && gRb_tree_bl != NULL)
233 foundItem = (key_value_s*)jsw_rbfind(gRb_tree_bl, item);
234 if(foundItem != NULL)
246 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("need_backup_key ==> item or gRb_tree_bl is NULL"));
253 /// compare function for tree key_value_s item
254 int key_val_cmp(const void *p1, const void *p2 )
260 first = (key_value_s*)p1;
261 second = (key_value_s*)p2;
263 if(second->key == first->key)
267 else if(second->key < first->key)
279 /// duplicate function for key_value_s item
280 void* key_val_dup(void *p)
283 key_value_s* src = NULL;
284 key_value_s* dst = NULL;
286 src = (key_value_s*)p;
287 value_size = strlen(src->value)+1;
289 // allocate memory for node
290 dst = malloc(sizeof(key_value_s));
293 // duplicate hash key
297 dst->value = malloc(value_size);
298 if(dst->value != NULL)
299 strncpy(dst->value, src->value, value_size);
306 /// release function for key_value_s item
307 void key_val_rel(void *p )
309 key_value_s* rel = NULL;
310 rel = (key_value_s*)p;
312 if(rel->value != NULL)