summaryrefslogtreecommitdiff
path: root/src/persistence_client_library_backup_filelist.c
blob: eae44255341f7eb7114832991c5a491b2c27955e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/******************************************************************************
 * Project         Persistency
 * (c) copyright   2012
 * Company         XS Embedded GmbH
 *****************************************************************************/
/******************************************************************************
 * This Source Code Form is subject to the terms of the
 * Mozilla Public License, v. 2.0. If a  copy of the MPL was not distributed
 * with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
******************************************************************************/
 /**
 * @file           persistence_client_library_backup_filelist.c
 * @ingroup        Persistence client library
 * @author         Ingo Huerner
 * @brief          Implementation of persistence client library backup filelist
 * @see
 */

#include "persistence_client_library_backup_filelist.h"
#include "rbtree.h"
#include "../include_protected/crc32.h"


#include <fcntl.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>


/// structure definition for a key value item
typedef struct _key_value_s
{
   unsigned int key;
   char*        value;
}key_value_s;


void  key_val_rel(void *p);

void* key_val_dup(void *p);

int key_val_cmp(const void *p1, const void *p2 );



/// the size of the token array
enum configConstants
{
   TOKENARRAYSIZE = 255
};


const char gCharLookup[] =
{
   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)
   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 (?)
   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 (_)
   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 (~)

};


char* gpConfigFileMap = 0;
char* gpTokenArray[TOKENARRAYSIZE];
int gTokenCounter = 0;
unsigned int gConfigFileSize = 0;

/// the rb tree
static jsw_rbtree_t *gRb_tree_bl = NULL;

void fillCharTokenArray()
{
   unsigned int i=0;
   int blankCount=0;
   char* tmpPointer = gpConfigFileMap;

   // set the first pointer to the start of the file
   gpTokenArray[blankCount] = tmpPointer;
   blankCount++;

   while(i < gConfigFileSize)
   {
      if(1 != gCharLookup[(int)*tmpPointer])
      {
         *tmpPointer = 0;

         // check if we are at the end of the token array
         if(blankCount >= TOKENARRAYSIZE)
         {
            break;
         }
         gpTokenArray[blankCount] = tmpPointer+1;
         blankCount++;
         gTokenCounter++;

      }
      tmpPointer++;
      i++;
   }
}


void createAndStoreFileNames()
{
   int i= 0, j =0;
   char path[128];
   const char* gFilePostFix                = ".pers";
   const char* gKeyPathFormat              = "/%s/%s/%s/%s/%s%s";
   key_value_s* item;

   // creat new tree
   gRb_tree_bl = jsw_rbnew(key_val_cmp, key_val_dup, key_val_rel);

   if(gRb_tree_bl != NULL)
   {

      for(i=0; i<128; i++)
      {
         // assemble path
         snprintf(path, 128, gKeyPathFormat, gpTokenArray[j+2],      // storage type
                                             gpTokenArray[j+3],      // policy id
                                             gpTokenArray[j+4],      // profileID
                                             gpTokenArray[j],        // application id
                                             gpTokenArray[j+1],      // filename
                                             gFilePostFix);          // file postfix

         // asign key and value to the rbtree item
         item = malloc(sizeof(key_value_s));
         if(item != NULL)
         {
            item->key = crc32(0, (unsigned char*)path, strlen(path));
            // we don't need the path name here, we just need to know that this key is available in the tree
            item->value = "";
            jsw_rbinsert(gRb_tree_bl, item);
            free(item);
         }
         j+=5;
         if(gpTokenArray[j] == NULL)
         {
            break;
         }
      }
   }

}


int readBlacklistConfigFile(const char* filename)
{
   int fd = 0,
       status = 0;
   struct stat buffer;

   memset(&buffer, 0, sizeof(buffer));
   status = stat(filename, &buffer);
   if(status != -1)
   {
      gConfigFileSize = buffer.st_size;
   }

   fd = open(filename, O_RDONLY);
   if (fd == -1)
   {
      printf("configReader::readConfigFile ==> Error file open: %s | error: %s \n", filename, strerror(errno));

      return -1;
   }


   // check for empty file
   if(gConfigFileSize == 0)
   {
      printf("configReader::readConfigFile ==> Error file size is 0: %s | buffer.st_size: %d \n", filename, (int)buffer.st_size);
      close(fd);
      return -1;
   }

   // map the config file into memory
   gpConfigFileMap = (char*)mmap(0, gConfigFileSize, PROT_WRITE, MAP_PRIVATE, fd, 0);

   if (gpConfigFileMap == MAP_FAILED)
   {
      gpConfigFileMap = 0;
      close(fd);
      printf("configReader::readConfigFile ==> Error mapping the file: %s | error: %s \n", filename, strerror(errno));
      return -1;
   }

   // reset the token counter
   gTokenCounter = 0;

   fillCharTokenArray();

   // create filernames and store them in the tree
   createAndStoreFileNames();

   munmap(gpConfigFileMap, gConfigFileSize);

   close(fd);
   return 0;
}



int need_backup_path(const char* path)
{
   return need_backup_key(crc32(0, (const unsigned char*)path, strlen(path)));
}



int need_backup_key(unsigned int key)
{
   int rval = 1;
   key_value_s* item = NULL;
   key_value_s* foundItem = NULL;

   item = malloc(sizeof(key_value_s));
   if(item != NULL && gRb_tree_bl != NULL)
   {
      item->key = key;
      foundItem = (key_value_s*)jsw_rbfind(gRb_tree_bl, item);
      if(foundItem != NULL)
      {
         rval = 0;
      }
      free(item);
   }
   else
   {
      if(item!=NULL)
	     free(item);

      rval = -1;
      printf("need_backup_key ==> item or gRb_tree_bl is NULL\n");
   }

   return rval;
}


/// compare function for tree key_value_s item
int key_val_cmp(const void *p1, const void *p2 )
{
   int rval = -1;
   key_value_s* first;
   key_value_s* second;

   first  = (key_value_s*)p1;
   second = (key_value_s*)p2;

   if(second->key == first->key)
   {
      rval = 0;
   }
   else if(second->key < first->key)
   {
      rval = -1;
   }
   else
   {
      rval = 1;
   }

   return rval;
 }

/// duplicate function for key_value_s item
void* key_val_dup(void *p)
{
   int value_size = 0;
   key_value_s* src = NULL;
   key_value_s* dst = NULL;

   src = (key_value_s*)p;
   value_size = strlen(src->value)+1;

   // allocate memory for node
   dst = malloc(sizeof(key_value_s));
   if(dst != NULL)
   {
      // duplicate hash key
     dst->key = src->key;

     // duplicate value
     dst->value = malloc(value_size);
     if(dst->value != NULL)
        strncpy(dst->value, src->value, value_size);
   }


   return dst;
}

/// release function for key_value_s item
void  key_val_rel(void *p )
{
   key_value_s* rel = NULL;
   rel = (key_value_s*)p;

   if(rel->value != NULL)
      free(rel->value);

   if(rel != NULL)
      free(rel);
}