diff options
Diffstat (limited to 'src/peerstore/plugin_peerstore_flat.c')
-rw-r--r-- | src/peerstore/plugin_peerstore_flat.c | 520 |
1 files changed, 260 insertions, 260 deletions
diff --git a/src/peerstore/plugin_peerstore_flat.c b/src/peerstore/plugin_peerstore_flat.c index 0cd2522ec..749e270e4 100644 --- a/src/peerstore/plugin_peerstore_flat.c +++ b/src/peerstore/plugin_peerstore_flat.c | |||
@@ -32,9 +32,7 @@ | |||
32 | /** | 32 | /** |
33 | * Context for all functions in this plugin. | 33 | * Context for all functions in this plugin. |
34 | */ | 34 | */ |
35 | struct Plugin | 35 | struct Plugin { |
36 | { | ||
37 | |||
38 | /** | 36 | /** |
39 | * Configuration handle | 37 | * Configuration handle |
40 | */ | 38 | */ |
@@ -94,27 +92,27 @@ struct Plugin | |||
94 | * Result found bool | 92 | * Result found bool |
95 | */ | 93 | */ |
96 | int iter_result_found; | 94 | int iter_result_found; |
97 | |||
98 | }; | 95 | }; |
99 | 96 | ||
100 | 97 | ||
101 | static int | 98 | static int |
102 | delete_entries (void *cls, | 99 | delete_entries(void *cls, |
103 | const struct GNUNET_HashCode *key, | 100 | const struct GNUNET_HashCode *key, |
104 | void *value) | 101 | void *value) |
105 | { | 102 | { |
106 | struct Plugin *plugin = cls; | 103 | struct Plugin *plugin = cls; |
107 | struct GNUNET_PEERSTORE_Record *entry = value; | 104 | struct GNUNET_PEERSTORE_Record *entry = value; |
108 | if (0 != strcmp (plugin->iter_key, entry->key)) | 105 | |
106 | if (0 != strcmp(plugin->iter_key, entry->key)) | ||
109 | return GNUNET_YES; | 107 | return GNUNET_YES; |
110 | if (0 != memcmp (plugin->iter_peer, | 108 | if (0 != memcmp(plugin->iter_peer, |
111 | &entry->peer, | 109 | &entry->peer, |
112 | sizeof (struct GNUNET_PeerIdentity))) | 110 | sizeof(struct GNUNET_PeerIdentity))) |
113 | return GNUNET_YES; | 111 | return GNUNET_YES; |
114 | if (0 != strcmp (plugin->iter_sub_system, entry->sub_system)) | 112 | if (0 != strcmp(plugin->iter_sub_system, entry->sub_system)) |
115 | return GNUNET_YES; | 113 | return GNUNET_YES; |
116 | 114 | ||
117 | GNUNET_CONTAINER_multihashmap_remove (plugin->hm, key, value); | 115 | GNUNET_CONTAINER_multihashmap_remove(plugin->hm, key, value); |
118 | plugin->deleted_entries++; | 116 | plugin->deleted_entries++; |
119 | return GNUNET_YES; | 117 | return GNUNET_YES; |
120 | } | 118 | } |
@@ -130,9 +128,9 @@ delete_entries (void *cls, | |||
130 | * @return number of deleted records | 128 | * @return number of deleted records |
131 | */ | 129 | */ |
132 | static int | 130 | static int |
133 | peerstore_flat_delete_records (void *cls, const char *sub_system, | 131 | peerstore_flat_delete_records(void *cls, const char *sub_system, |
134 | const struct GNUNET_PeerIdentity *peer, | 132 | const struct GNUNET_PeerIdentity *peer, |
135 | const char *key) | 133 | const char *key) |
136 | { | 134 | { |
137 | struct Plugin *plugin = cls; | 135 | struct Plugin *plugin = cls; |
138 | 136 | ||
@@ -141,25 +139,25 @@ peerstore_flat_delete_records (void *cls, const char *sub_system, | |||
141 | plugin->iter_key = key; | 139 | plugin->iter_key = key; |
142 | plugin->deleted_entries = 0; | 140 | plugin->deleted_entries = 0; |
143 | 141 | ||
144 | GNUNET_CONTAINER_multihashmap_iterate (plugin->hm, | 142 | GNUNET_CONTAINER_multihashmap_iterate(plugin->hm, |
145 | &delete_entries, | 143 | &delete_entries, |
146 | plugin); | 144 | plugin); |
147 | return plugin->deleted_entries; | 145 | return plugin->deleted_entries; |
148 | } | 146 | } |
149 | 147 | ||
150 | static int | 148 | static int |
151 | expire_entries (void *cls, | 149 | expire_entries(void *cls, |
152 | const struct GNUNET_HashCode *key, | 150 | const struct GNUNET_HashCode *key, |
153 | void *value) | 151 | void *value) |
154 | { | 152 | { |
155 | struct Plugin *plugin = cls; | 153 | struct Plugin *plugin = cls; |
156 | struct GNUNET_PEERSTORE_Record *entry = value; | 154 | struct GNUNET_PEERSTORE_Record *entry = value; |
157 | 155 | ||
158 | if (entry->expiry.abs_value_us < plugin->iter_now.abs_value_us) | 156 | if (entry->expiry.abs_value_us < plugin->iter_now.abs_value_us) |
159 | { | 157 | { |
160 | GNUNET_CONTAINER_multihashmap_remove (plugin->hm, key, value); | 158 | GNUNET_CONTAINER_multihashmap_remove(plugin->hm, key, value); |
161 | plugin->exp_changes++; | 159 | plugin->exp_changes++; |
162 | } | 160 | } |
163 | return GNUNET_YES; | 161 | return GNUNET_YES; |
164 | } | 162 | } |
165 | 163 | ||
@@ -176,49 +174,49 @@ expire_entries (void *cls, | |||
176 | * called | 174 | * called |
177 | */ | 175 | */ |
178 | static int | 176 | static int |
179 | peerstore_flat_expire_records (void *cls, struct GNUNET_TIME_Absolute now, | 177 | peerstore_flat_expire_records(void *cls, struct GNUNET_TIME_Absolute now, |
180 | GNUNET_PEERSTORE_Continuation cont, | 178 | GNUNET_PEERSTORE_Continuation cont, |
181 | void *cont_cls) | 179 | void *cont_cls) |
182 | { | 180 | { |
183 | struct Plugin *plugin = cls; | 181 | struct Plugin *plugin = cls; |
182 | |||
184 | plugin->exp_changes = 0; | 183 | plugin->exp_changes = 0; |
185 | plugin->iter_now = now; | 184 | plugin->iter_now = now; |
186 | 185 | ||
187 | GNUNET_CONTAINER_multihashmap_iterate (plugin->hm, | 186 | GNUNET_CONTAINER_multihashmap_iterate(plugin->hm, |
188 | &expire_entries, | 187 | &expire_entries, |
189 | plugin); | 188 | plugin); |
190 | if (NULL != cont) | 189 | if (NULL != cont) |
191 | { | 190 | { |
192 | cont (cont_cls, plugin->exp_changes); | 191 | cont(cont_cls, plugin->exp_changes); |
193 | } | 192 | } |
194 | return GNUNET_OK; | 193 | return GNUNET_OK; |
195 | |||
196 | } | 194 | } |
197 | 195 | ||
198 | 196 | ||
199 | static int | 197 | static int |
200 | iterate_entries (void *cls, | 198 | iterate_entries(void *cls, |
201 | const struct GNUNET_HashCode *key, | 199 | const struct GNUNET_HashCode *key, |
202 | void *value) | 200 | void *value) |
203 | { | 201 | { |
204 | struct Plugin *plugin = cls; | 202 | struct Plugin *plugin = cls; |
205 | struct GNUNET_PEERSTORE_Record *entry = value; | 203 | struct GNUNET_PEERSTORE_Record *entry = value; |
206 | 204 | ||
207 | if ((NULL != plugin->iter_peer) && | 205 | if ((NULL != plugin->iter_peer) && |
208 | (0 != memcmp (plugin->iter_peer, | 206 | (0 != memcmp(plugin->iter_peer, |
209 | &entry->peer, | 207 | &entry->peer, |
210 | sizeof (struct GNUNET_PeerIdentity)))) | 208 | sizeof(struct GNUNET_PeerIdentity)))) |
211 | { | 209 | { |
212 | return GNUNET_YES; | 210 | return GNUNET_YES; |
213 | } | 211 | } |
214 | if ((NULL != plugin->iter_key) && | 212 | if ((NULL != plugin->iter_key) && |
215 | (0 != strcmp (plugin->iter_key, | 213 | (0 != strcmp(plugin->iter_key, |
216 | entry->key))) | 214 | entry->key))) |
217 | { | 215 | { |
218 | return GNUNET_YES; | 216 | return GNUNET_YES; |
219 | } | 217 | } |
220 | if (NULL != plugin->iter) | 218 | if (NULL != plugin->iter) |
221 | plugin->iter (plugin->iter_cls, entry, NULL); | 219 | plugin->iter(plugin->iter_cls, entry, NULL); |
222 | plugin->iter_result_found = GNUNET_YES; | 220 | plugin->iter_result_found = GNUNET_YES; |
223 | return GNUNET_YES; | 221 | return GNUNET_YES; |
224 | } | 222 | } |
@@ -238,24 +236,25 @@ iterate_entries (void *cls, | |||
238 | * called | 236 | * called |
239 | */ | 237 | */ |
240 | static int | 238 | static int |
241 | peerstore_flat_iterate_records (void *cls, const char *sub_system, | 239 | peerstore_flat_iterate_records(void *cls, const char *sub_system, |
242 | const struct GNUNET_PeerIdentity *peer, | 240 | const struct GNUNET_PeerIdentity *peer, |
243 | const char *key, | 241 | const char *key, |
244 | GNUNET_PEERSTORE_Processor iter, | 242 | GNUNET_PEERSTORE_Processor iter, |
245 | void *iter_cls) | 243 | void *iter_cls) |
246 | { | 244 | { |
247 | struct Plugin *plugin = cls; | 245 | struct Plugin *plugin = cls; |
246 | |||
248 | plugin->iter = iter; | 247 | plugin->iter = iter; |
249 | plugin->iter_cls = iter_cls; | 248 | plugin->iter_cls = iter_cls; |
250 | plugin->iter_peer = peer; | 249 | plugin->iter_peer = peer; |
251 | plugin->iter_sub_system = sub_system; | 250 | plugin->iter_sub_system = sub_system; |
252 | plugin->iter_key = key; | 251 | plugin->iter_key = key; |
253 | 252 | ||
254 | GNUNET_CONTAINER_multihashmap_iterate (plugin->hm, | 253 | GNUNET_CONTAINER_multihashmap_iterate(plugin->hm, |
255 | &iterate_entries, | 254 | &iterate_entries, |
256 | plugin); | 255 | plugin); |
257 | if (NULL != iter) | 256 | if (NULL != iter) |
258 | iter (iter_cls, NULL, NULL); | 257 | iter(iter_cls, NULL, NULL); |
259 | return GNUNET_OK; | 258 | return GNUNET_OK; |
260 | } | 259 | } |
261 | 260 | ||
@@ -278,13 +277,13 @@ peerstore_flat_iterate_records (void *cls, const char *sub_system, | |||
278 | * @return #GNUNET_OK on success, else #GNUNET_SYSERR and cont is not called | 277 | * @return #GNUNET_OK on success, else #GNUNET_SYSERR and cont is not called |
279 | */ | 278 | */ |
280 | static int | 279 | static int |
281 | peerstore_flat_store_record (void *cls, const char *sub_system, | 280 | peerstore_flat_store_record(void *cls, const char *sub_system, |
282 | const struct GNUNET_PeerIdentity *peer, | 281 | const struct GNUNET_PeerIdentity *peer, |
283 | const char *key, const void *value, size_t size, | 282 | const char *key, const void *value, size_t size, |
284 | struct GNUNET_TIME_Absolute expiry, | 283 | struct GNUNET_TIME_Absolute expiry, |
285 | enum GNUNET_PEERSTORE_StoreOption options, | 284 | enum GNUNET_PEERSTORE_StoreOption options, |
286 | GNUNET_PEERSTORE_Continuation cont, | 285 | GNUNET_PEERSTORE_Continuation cont, |
287 | void *cont_cls) | 286 | void *cont_cls) |
288 | { | 287 | { |
289 | struct Plugin *plugin = cls; | 288 | struct Plugin *plugin = cls; |
290 | struct GNUNET_HashCode hkey; | 289 | struct GNUNET_HashCode hkey; |
@@ -292,33 +291,33 @@ peerstore_flat_store_record (void *cls, const char *sub_system, | |||
292 | const char *peer_id; | 291 | const char *peer_id; |
293 | 292 | ||
294 | 293 | ||
295 | entry = GNUNET_new (struct GNUNET_PEERSTORE_Record); | 294 | entry = GNUNET_new(struct GNUNET_PEERSTORE_Record); |
296 | entry->sub_system = GNUNET_strdup (sub_system); | 295 | entry->sub_system = GNUNET_strdup(sub_system); |
297 | entry->key = GNUNET_strdup (key); | 296 | entry->key = GNUNET_strdup(key); |
298 | entry->value = GNUNET_malloc (size); | 297 | entry->value = GNUNET_malloc(size); |
299 | GNUNET_memcpy (entry->value, value, size); | 298 | GNUNET_memcpy(entry->value, value, size); |
300 | entry->value_size = size; | 299 | entry->value_size = size; |
301 | entry->peer = *peer; | 300 | entry->peer = *peer; |
302 | entry->expiry = expiry; | 301 | entry->expiry = expiry; |
303 | 302 | ||
304 | peer_id = GNUNET_i2s (peer); | 303 | peer_id = GNUNET_i2s(peer); |
305 | GNUNET_CRYPTO_hash (peer_id, | 304 | GNUNET_CRYPTO_hash(peer_id, |
306 | strlen (peer_id), | 305 | strlen(peer_id), |
307 | &hkey); | 306 | &hkey); |
308 | 307 | ||
309 | if (GNUNET_PEERSTORE_STOREOPTION_REPLACE == options) | 308 | if (GNUNET_PEERSTORE_STOREOPTION_REPLACE == options) |
310 | { | 309 | { |
311 | peerstore_flat_delete_records (cls, sub_system, peer, key); | 310 | peerstore_flat_delete_records(cls, sub_system, peer, key); |
312 | } | 311 | } |
313 | 312 | ||
314 | GNUNET_CONTAINER_multihashmap_put (plugin->hm, | 313 | GNUNET_CONTAINER_multihashmap_put(plugin->hm, |
315 | &hkey, | 314 | &hkey, |
316 | entry, | 315 | entry, |
317 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); | 316 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); |
318 | if (NULL != cont) | 317 | if (NULL != cont) |
319 | { | 318 | { |
320 | cont (cont_cls, GNUNET_OK); | 319 | cont(cont_cls, GNUNET_OK); |
321 | } | 320 | } |
322 | return GNUNET_OK; | 321 | return GNUNET_OK; |
323 | } | 322 | } |
324 | 323 | ||
@@ -332,7 +331,7 @@ peerstore_flat_store_record (void *cls, const char *sub_system, | |||
332 | * @return GNUNET_OK on success | 331 | * @return GNUNET_OK on success |
333 | */ | 332 | */ |
334 | static int | 333 | static int |
335 | database_setup (struct Plugin *plugin) | 334 | database_setup(struct Plugin *plugin) |
336 | { | 335 | { |
337 | char *afsdir; | 336 | char *afsdir; |
338 | char *key; | 337 | char *key; |
@@ -349,138 +348,139 @@ database_setup (struct Plugin *plugin) | |||
349 | char *line; | 348 | char *line; |
350 | 349 | ||
351 | if (GNUNET_OK != | 350 | if (GNUNET_OK != |
352 | GNUNET_CONFIGURATION_get_value_filename (plugin->cfg, "peerstore-flat", | 351 | GNUNET_CONFIGURATION_get_value_filename(plugin->cfg, "peerstore-flat", |
353 | "FILENAME", &afsdir)) | 352 | "FILENAME", &afsdir)) |
354 | { | ||
355 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "peerstore-flat", | ||
356 | "FILENAME"); | ||
357 | return GNUNET_SYSERR; | ||
358 | } | ||
359 | if (GNUNET_OK != GNUNET_DISK_file_test (afsdir)) | ||
360 | { | ||
361 | if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (afsdir)) | ||
362 | { | 353 | { |
363 | GNUNET_break (0); | 354 | GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR, "peerstore-flat", |
364 | GNUNET_free (afsdir); | 355 | "FILENAME"); |
365 | return GNUNET_SYSERR; | 356 | return GNUNET_SYSERR; |
366 | } | 357 | } |
367 | } | 358 | if (GNUNET_OK != GNUNET_DISK_file_test(afsdir)) |
359 | { | ||
360 | if (GNUNET_OK != GNUNET_DISK_directory_create_for_file(afsdir)) | ||
361 | { | ||
362 | GNUNET_break(0); | ||
363 | GNUNET_free(afsdir); | ||
364 | return GNUNET_SYSERR; | ||
365 | } | ||
366 | } | ||
368 | /* afsdir should be UTF-8-encoded. If it isn't, it's a bug */ | 367 | /* afsdir should be UTF-8-encoded. If it isn't, it's a bug */ |
369 | plugin->fn = afsdir; | 368 | plugin->fn = afsdir; |
370 | 369 | ||
371 | fh = GNUNET_DISK_file_open (afsdir, | 370 | fh = GNUNET_DISK_file_open(afsdir, |
372 | GNUNET_DISK_OPEN_CREATE | | 371 | GNUNET_DISK_OPEN_CREATE | |
373 | GNUNET_DISK_OPEN_READWRITE, | 372 | GNUNET_DISK_OPEN_READWRITE, |
374 | GNUNET_DISK_PERM_USER_WRITE | | 373 | GNUNET_DISK_PERM_USER_WRITE | |
375 | GNUNET_DISK_PERM_USER_READ); | 374 | GNUNET_DISK_PERM_USER_READ); |
376 | if (NULL == fh) | 375 | if (NULL == fh) |
377 | { | 376 | { |
378 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 377 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, |
379 | _("Unable to initialize file: %s.\n"), | 378 | _("Unable to initialize file: %s.\n"), |
380 | afsdir); | 379 | afsdir); |
381 | return GNUNET_SYSERR; | 380 | return GNUNET_SYSERR; |
382 | } | 381 | } |
383 | 382 | ||
384 | /* Load data from file into hashmap */ | 383 | /* Load data from file into hashmap */ |
385 | plugin->hm = GNUNET_CONTAINER_multihashmap_create (10, | 384 | plugin->hm = GNUNET_CONTAINER_multihashmap_create(10, |
386 | GNUNET_NO); | 385 | GNUNET_NO); |
387 | |||
388 | if (GNUNET_SYSERR == GNUNET_DISK_file_size (afsdir, | ||
389 | &size, | ||
390 | GNUNET_YES, | ||
391 | GNUNET_YES)) | ||
392 | { | ||
393 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
394 | _("Unable to get filesize: %s.\n"), | ||
395 | afsdir); | ||
396 | return GNUNET_SYSERR; | ||
397 | } | ||
398 | |||
399 | buffer = GNUNET_malloc (size + 1); | ||
400 | |||
401 | if (GNUNET_SYSERR == GNUNET_DISK_file_read (fh, | ||
402 | buffer, | ||
403 | size)) | ||
404 | { | ||
405 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
406 | _("Unable to read file: %s.\n"), | ||
407 | afsdir); | ||
408 | GNUNET_DISK_file_close (fh); | ||
409 | GNUNET_free (buffer); | ||
410 | return GNUNET_SYSERR; | ||
411 | } | ||
412 | 386 | ||
413 | buffer[size] = '\0'; | 387 | if (GNUNET_SYSERR == GNUNET_DISK_file_size(afsdir, |
414 | GNUNET_DISK_file_close (fh); | 388 | &size, |
415 | if (0 < size) { | 389 | GNUNET_YES, |
416 | line = strtok (buffer, "\n"); | 390 | GNUNET_YES)) |
417 | while (line != NULL) { | 391 | { |
418 | sub_system = strtok (line, ","); | 392 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, |
419 | if (NULL == sub_system) | 393 | _("Unable to get filesize: %s.\n"), |
420 | break; | 394 | afsdir); |
421 | peer = strtok (NULL, ","); | 395 | return GNUNET_SYSERR; |
422 | if (NULL == peer) | 396 | } |
423 | break; | ||
424 | key = strtok (NULL, ","); | ||
425 | if (NULL == key) | ||
426 | break; | ||
427 | value = strtok (NULL, ","); | ||
428 | if (NULL == value) | ||
429 | break; | ||
430 | expiry = strtok (NULL, ","); | ||
431 | if (NULL == expiry) | ||
432 | break; | ||
433 | entry = GNUNET_new (struct GNUNET_PEERSTORE_Record); | ||
434 | entry->sub_system = GNUNET_strdup (sub_system); | ||
435 | entry->key = GNUNET_strdup (key); | ||
436 | { | ||
437 | size_t s; | ||
438 | char *o; | ||
439 | |||
440 | o = NULL; | ||
441 | s = GNUNET_STRINGS_base64_decode (peer, | ||
442 | strlen (peer), | ||
443 | (void**)&o); | ||
444 | if (sizeof (struct GNUNET_PeerIdentity) == s) | ||
445 | GNUNET_memcpy (&entry->peer, | ||
446 | o, | ||
447 | s); | ||
448 | else | ||
449 | GNUNET_break (0); | ||
450 | GNUNET_free_non_null (o); | ||
451 | } | ||
452 | entry->value_size = GNUNET_STRINGS_base64_decode (value, | ||
453 | strlen (value), | ||
454 | (void**)&entry->value); | ||
455 | if (GNUNET_SYSERR == | ||
456 | GNUNET_STRINGS_fancy_time_to_absolute (expiry, | ||
457 | &entry->expiry)) | ||
458 | { | ||
459 | GNUNET_free (entry->sub_system); | ||
460 | GNUNET_free (entry->key); | ||
461 | GNUNET_free (entry); | ||
462 | break; | ||
463 | } | ||
464 | peer_id = GNUNET_i2s (&entry->peer); | ||
465 | GNUNET_CRYPTO_hash (peer_id, | ||
466 | strlen (peer_id), | ||
467 | &hkey); | ||
468 | |||
469 | GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (plugin->hm, | ||
470 | &hkey, | ||
471 | entry, | ||
472 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE)); | ||
473 | 397 | ||
398 | buffer = GNUNET_malloc(size + 1); | ||
399 | |||
400 | if (GNUNET_SYSERR == GNUNET_DISK_file_read(fh, | ||
401 | buffer, | ||
402 | size)) | ||
403 | { | ||
404 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, | ||
405 | _("Unable to read file: %s.\n"), | ||
406 | afsdir); | ||
407 | GNUNET_DISK_file_close(fh); | ||
408 | GNUNET_free(buffer); | ||
409 | return GNUNET_SYSERR; | ||
410 | } | ||
411 | |||
412 | buffer[size] = '\0'; | ||
413 | GNUNET_DISK_file_close(fh); | ||
414 | if (0 < size) | ||
415 | { | ||
416 | line = strtok(buffer, "\n"); | ||
417 | while (line != NULL) | ||
418 | { | ||
419 | sub_system = strtok(line, ","); | ||
420 | if (NULL == sub_system) | ||
421 | break; | ||
422 | peer = strtok(NULL, ","); | ||
423 | if (NULL == peer) | ||
424 | break; | ||
425 | key = strtok(NULL, ","); | ||
426 | if (NULL == key) | ||
427 | break; | ||
428 | value = strtok(NULL, ","); | ||
429 | if (NULL == value) | ||
430 | break; | ||
431 | expiry = strtok(NULL, ","); | ||
432 | if (NULL == expiry) | ||
433 | break; | ||
434 | entry = GNUNET_new(struct GNUNET_PEERSTORE_Record); | ||
435 | entry->sub_system = GNUNET_strdup(sub_system); | ||
436 | entry->key = GNUNET_strdup(key); | ||
437 | { | ||
438 | size_t s; | ||
439 | char *o; | ||
440 | |||
441 | o = NULL; | ||
442 | s = GNUNET_STRINGS_base64_decode(peer, | ||
443 | strlen(peer), | ||
444 | (void**)&o); | ||
445 | if (sizeof(struct GNUNET_PeerIdentity) == s) | ||
446 | GNUNET_memcpy(&entry->peer, | ||
447 | o, | ||
448 | s); | ||
449 | else | ||
450 | GNUNET_break(0); | ||
451 | GNUNET_free_non_null(o); | ||
452 | } | ||
453 | entry->value_size = GNUNET_STRINGS_base64_decode(value, | ||
454 | strlen(value), | ||
455 | (void**)&entry->value); | ||
456 | if (GNUNET_SYSERR == | ||
457 | GNUNET_STRINGS_fancy_time_to_absolute(expiry, | ||
458 | &entry->expiry)) | ||
459 | { | ||
460 | GNUNET_free(entry->sub_system); | ||
461 | GNUNET_free(entry->key); | ||
462 | GNUNET_free(entry); | ||
463 | break; | ||
464 | } | ||
465 | peer_id = GNUNET_i2s(&entry->peer); | ||
466 | GNUNET_CRYPTO_hash(peer_id, | ||
467 | strlen(peer_id), | ||
468 | &hkey); | ||
469 | |||
470 | GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(plugin->hm, | ||
471 | &hkey, | ||
472 | entry, | ||
473 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE)); | ||
474 | } | ||
474 | } | 475 | } |
475 | } | 476 | GNUNET_free(buffer); |
476 | GNUNET_free (buffer); | ||
477 | return GNUNET_OK; | 477 | return GNUNET_OK; |
478 | } | 478 | } |
479 | 479 | ||
480 | static int | 480 | static int |
481 | store_and_free_entries (void *cls, | 481 | store_and_free_entries(void *cls, |
482 | const struct GNUNET_HashCode *key, | 482 | const struct GNUNET_HashCode *key, |
483 | void *value) | 483 | void *value) |
484 | { | 484 | { |
485 | struct GNUNET_DISK_FileHandle *fh = cls; | 485 | struct GNUNET_DISK_FileHandle *fh = cls; |
486 | struct GNUNET_PEERSTORE_Record *entry = value; | 486 | struct GNUNET_PEERSTORE_Record *entry = value; |
@@ -489,32 +489,31 @@ store_and_free_entries (void *cls, | |||
489 | const char *expiry; | 489 | const char *expiry; |
490 | char *val; | 490 | char *val; |
491 | 491 | ||
492 | GNUNET_STRINGS_base64_encode (entry->value, | 492 | GNUNET_STRINGS_base64_encode(entry->value, |
493 | entry->value_size, | 493 | entry->value_size, |
494 | &val); | 494 | &val); |
495 | expiry = GNUNET_STRINGS_absolute_time_to_string (entry->expiry); | 495 | expiry = GNUNET_STRINGS_absolute_time_to_string(entry->expiry); |
496 | GNUNET_STRINGS_base64_encode ((char*)&entry->peer, | 496 | GNUNET_STRINGS_base64_encode((char*)&entry->peer, |
497 | sizeof (struct GNUNET_PeerIdentity), | 497 | sizeof(struct GNUNET_PeerIdentity), |
498 | &peer); | 498 | &peer); |
499 | GNUNET_asprintf (&line, | 499 | GNUNET_asprintf(&line, |
500 | "%s,%s,%s,%s,%s", | 500 | "%s,%s,%s,%s,%s", |
501 | entry->sub_system, | 501 | entry->sub_system, |
502 | peer, | 502 | peer, |
503 | entry->key, | 503 | entry->key, |
504 | val, | 504 | val, |
505 | expiry); | 505 | expiry); |
506 | GNUNET_free (val); | 506 | GNUNET_free(val); |
507 | GNUNET_free (peer); | 507 | GNUNET_free(peer); |
508 | GNUNET_DISK_file_write (fh, | 508 | GNUNET_DISK_file_write(fh, |
509 | line, | 509 | line, |
510 | strlen (line)); | 510 | strlen(line)); |
511 | GNUNET_free (entry->sub_system); | 511 | GNUNET_free(entry->sub_system); |
512 | GNUNET_free (entry->key); | 512 | GNUNET_free(entry->key); |
513 | GNUNET_free (entry->value); | 513 | GNUNET_free(entry->value); |
514 | GNUNET_free (entry); | 514 | GNUNET_free(entry); |
515 | GNUNET_free (line); | 515 | GNUNET_free(line); |
516 | return GNUNET_YES; | 516 | return GNUNET_YES; |
517 | |||
518 | } | 517 | } |
519 | 518 | ||
520 | /** | 519 | /** |
@@ -523,27 +522,28 @@ store_and_free_entries (void *cls, | |||
523 | * @param plugin the plugin context (state for this module) | 522 | * @param plugin the plugin context (state for this module) |
524 | */ | 523 | */ |
525 | static void | 524 | static void |
526 | database_shutdown (struct Plugin *plugin) | 525 | database_shutdown(struct Plugin *plugin) |
527 | { | 526 | { |
528 | struct GNUNET_DISK_FileHandle *fh; | 527 | struct GNUNET_DISK_FileHandle *fh; |
529 | fh = GNUNET_DISK_file_open (plugin->fn, | 528 | |
530 | GNUNET_DISK_OPEN_CREATE | | 529 | fh = GNUNET_DISK_file_open(plugin->fn, |
531 | GNUNET_DISK_OPEN_TRUNCATE | | 530 | GNUNET_DISK_OPEN_CREATE | |
532 | GNUNET_DISK_OPEN_READWRITE, | 531 | GNUNET_DISK_OPEN_TRUNCATE | |
533 | GNUNET_DISK_PERM_USER_WRITE | | 532 | GNUNET_DISK_OPEN_READWRITE, |
534 | GNUNET_DISK_PERM_USER_READ); | 533 | GNUNET_DISK_PERM_USER_WRITE | |
534 | GNUNET_DISK_PERM_USER_READ); | ||
535 | if (NULL == fh) | 535 | if (NULL == fh) |
536 | { | 536 | { |
537 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 537 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, |
538 | _("Unable to initialize file: %s.\n"), | 538 | _("Unable to initialize file: %s.\n"), |
539 | plugin->fn); | 539 | plugin->fn); |
540 | return; | 540 | return; |
541 | } | 541 | } |
542 | GNUNET_CONTAINER_multihashmap_iterate (plugin->hm, | 542 | GNUNET_CONTAINER_multihashmap_iterate(plugin->hm, |
543 | &store_and_free_entries, | 543 | &store_and_free_entries, |
544 | fh); | 544 | fh); |
545 | GNUNET_CONTAINER_multihashmap_destroy (plugin->hm); | 545 | GNUNET_CONTAINER_multihashmap_destroy(plugin->hm); |
546 | GNUNET_DISK_file_close (fh); | 546 | GNUNET_DISK_file_close(fh); |
547 | } | 547 | } |
548 | 548 | ||
549 | 549 | ||
@@ -554,7 +554,7 @@ database_shutdown (struct Plugin *plugin) | |||
554 | * @return NULL on error, otherwise the plugin context | 554 | * @return NULL on error, otherwise the plugin context |
555 | */ | 555 | */ |
556 | void * | 556 | void * |
557 | libgnunet_plugin_peerstore_flat_init (void *cls) | 557 | libgnunet_plugin_peerstore_flat_init(void *cls) |
558 | { | 558 | { |
559 | static struct Plugin plugin; | 559 | static struct Plugin plugin; |
560 | const struct GNUNET_CONFIGURATION_Handle *cfg = cls; | 560 | const struct GNUNET_CONFIGURATION_Handle *cfg = cls; |
@@ -562,19 +562,19 @@ libgnunet_plugin_peerstore_flat_init (void *cls) | |||
562 | 562 | ||
563 | if (NULL != plugin.cfg) | 563 | if (NULL != plugin.cfg) |
564 | return NULL; /* can only initialize once! */ | 564 | return NULL; /* can only initialize once! */ |
565 | memset (&plugin, 0, sizeof (struct Plugin)); | 565 | memset(&plugin, 0, sizeof(struct Plugin)); |
566 | plugin.cfg = cfg; | 566 | plugin.cfg = cfg; |
567 | if (GNUNET_OK != database_setup (&plugin)) | 567 | if (GNUNET_OK != database_setup(&plugin)) |
568 | { | 568 | { |
569 | database_shutdown (&plugin); | 569 | database_shutdown(&plugin); |
570 | return NULL; | 570 | return NULL; |
571 | } | 571 | } |
572 | api = GNUNET_new (struct GNUNET_PEERSTORE_PluginFunctions); | 572 | api = GNUNET_new(struct GNUNET_PEERSTORE_PluginFunctions); |
573 | api->cls = &plugin; | 573 | api->cls = &plugin; |
574 | api->store_record = &peerstore_flat_store_record; | 574 | api->store_record = &peerstore_flat_store_record; |
575 | api->iterate_records = &peerstore_flat_iterate_records; | 575 | api->iterate_records = &peerstore_flat_iterate_records; |
576 | api->expire_records = &peerstore_flat_expire_records; | 576 | api->expire_records = &peerstore_flat_expire_records; |
577 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Flat plugin is running\n"); | 577 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Flat plugin is running\n"); |
578 | return api; | 578 | return api; |
579 | } | 579 | } |
580 | 580 | ||
@@ -586,15 +586,15 @@ libgnunet_plugin_peerstore_flat_init (void *cls) | |||
586 | * @return Always NULL | 586 | * @return Always NULL |
587 | */ | 587 | */ |
588 | void * | 588 | void * |
589 | libgnunet_plugin_peerstore_flat_done (void *cls) | 589 | libgnunet_plugin_peerstore_flat_done(void *cls) |
590 | { | 590 | { |
591 | struct GNUNET_PEERSTORE_PluginFunctions *api = cls; | 591 | struct GNUNET_PEERSTORE_PluginFunctions *api = cls; |
592 | struct Plugin *plugin = api->cls; | 592 | struct Plugin *plugin = api->cls; |
593 | 593 | ||
594 | database_shutdown (plugin); | 594 | database_shutdown(plugin); |
595 | plugin->cfg = NULL; | 595 | plugin->cfg = NULL; |
596 | GNUNET_free (api); | 596 | GNUNET_free(api); |
597 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Flat plugin is finished\n"); | 597 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Flat plugin is finished\n"); |
598 | return NULL; | 598 | return NULL; |
599 | } | 599 | } |
600 | 600 | ||