summaryrefslogtreecommitdiff
path: root/src/fs/gnunet-service-fs_indexing.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/fs/gnunet-service-fs_indexing.c')
-rw-r--r--src/fs/gnunet-service-fs_indexing.c248
1 files changed, 88 insertions, 160 deletions
diff --git a/src/fs/gnunet-service-fs_indexing.c b/src/fs/gnunet-service-fs_indexing.c
index 0e8e62fc7..df5f70b3d 100644
--- a/src/fs/gnunet-service-fs_indexing.c
+++ b/src/fs/gnunet-service-fs_indexing.c
@@ -73,7 +73,6 @@ struct IndexInfo
73 * Hash of the contents of the file. 73 * Hash of the contents of the file.
74 */ 74 */
75 struct GNUNET_HashCode file_id; 75 struct GNUNET_HashCode file_id;
76
77}; 76};
78 77
79 78
@@ -118,12 +117,10 @@ write_index_list ()
118 struct IndexInfo *pos; 117 struct IndexInfo *pos;
119 118
120 if (GNUNET_OK != 119 if (GNUNET_OK !=
121 GNUNET_CONFIGURATION_get_value_filename (cfg, "FS", 120 GNUNET_CONFIGURATION_get_value_filename (cfg, "FS", "INDEXDB", &fn))
122 "INDEXDB",
123 &fn))
124 { 121 {
125 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 122 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
126 "fs", 123 "fs",
127 "INDEXDB"); 124 "INDEXDB");
128 return; 125 return;
129 } 126 }
@@ -131,24 +128,21 @@ write_index_list ()
131 if (NULL == wh) 128 if (NULL == wh)
132 { 129 {
133 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 130 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
134 _("Could not open `%s'.\n"), 131 _ ("Could not open `%s'.\n"),
135 fn); 132 fn);
136 GNUNET_free (fn); 133 GNUNET_free (fn);
137 return; 134 return;
138 } 135 }
139 for (pos = indexed_files_head; NULL != pos; pos = pos->next) 136 for (pos = indexed_files_head; NULL != pos; pos = pos->next)
140 if ((GNUNET_OK != 137 if ((GNUNET_OK != GNUNET_BIO_write (wh,
141 GNUNET_BIO_write (wh, 138 &pos->file_id,
142 &pos->file_id, 139 sizeof (struct GNUNET_HashCode))) ||
143 sizeof (struct GNUNET_HashCode))) || 140 (GNUNET_OK != GNUNET_BIO_write_string (wh, pos->filename)))
144 (GNUNET_OK !=
145 GNUNET_BIO_write_string (wh,
146 pos->filename)))
147 break; 141 break;
148 if (GNUNET_OK != GNUNET_BIO_write_close (wh)) 142 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
149 { 143 {
150 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 144 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
151 _("Error writing `%s'.\n"), 145 _ ("Error writing `%s'.\n"),
152 fn); 146 fn);
153 GNUNET_free (fn); 147 GNUNET_free (fn);
154 return; 148 return;
@@ -172,13 +166,10 @@ read_index_list ()
172 char *emsg; 166 char *emsg;
173 167
174 if (GNUNET_OK != 168 if (GNUNET_OK !=
175 GNUNET_CONFIGURATION_get_value_filename (cfg, 169 GNUNET_CONFIGURATION_get_value_filename (cfg, "FS", "INDEXDB", &fn))
176 "FS",
177 "INDEXDB",
178 &fn))
179 { 170 {
180 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 171 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
181 "fs", 172 "fs",
182 "INDEXDB"); 173 "INDEXDB");
183 return; 174 return;
184 } 175 }
@@ -192,39 +183,36 @@ read_index_list ()
192 if (NULL == rh) 183 if (NULL == rh)
193 { 184 {
194 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 185 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
195 _("Could not open `%s'.\n"), 186 _ ("Could not open `%s'.\n"),
196 fn); 187 fn);
197 GNUNET_free (fn); 188 GNUNET_free (fn);
198 return; 189 return;
199 } 190 }
200 while ( (GNUNET_OK == 191 while (
201 GNUNET_BIO_read (rh, 192 (GNUNET_OK == GNUNET_BIO_read (rh,
202 "Hash of indexed file", 193 "Hash of indexed file",
203 &hc, 194 &hc,
204 sizeof (struct GNUNET_HashCode))) && 195 sizeof (struct GNUNET_HashCode))) &&
205 (GNUNET_OK == 196 (GNUNET_OK ==
206 GNUNET_BIO_read_string (rh, 197 GNUNET_BIO_read_string (rh, "Name of indexed file", &fname, 1024 * 16)) &&
207 "Name of indexed file", 198 (fname != NULL))
208 &fname,
209 1024 * 16)) &&
210 (fname != NULL) )
211 { 199 {
212 slen = strlen (fname) + 1; 200 slen = strlen (fname) + 1;
213 pos = GNUNET_malloc (sizeof (struct IndexInfo) + slen); 201 pos = GNUNET_malloc (sizeof (struct IndexInfo) + slen);
214 pos->file_id = hc; 202 pos->file_id = hc;
215 pos->filename = (const char *) &pos[1]; 203 pos->filename = (const char *) &pos[1];
216 GNUNET_memcpy (&pos[1], fname, slen); 204 GNUNET_memcpy (&pos[1], fname, slen);
217 if (GNUNET_SYSERR == 205 if (GNUNET_SYSERR == GNUNET_CONTAINER_multihashmap_put (
218 GNUNET_CONTAINER_multihashmap_put (ifm, &pos->file_id, pos, 206 ifm,
219 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)) 207 &pos->file_id,
208 pos,
209 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
220 { 210 {
221 GNUNET_free (pos); 211 GNUNET_free (pos);
222 } 212 }
223 else 213 else
224 { 214 {
225 GNUNET_CONTAINER_DLL_insert (indexed_files_head, 215 GNUNET_CONTAINER_DLL_insert (indexed_files_head, indexed_files_tail, pos);
226 indexed_files_tail,
227 pos);
228 } 216 }
229 GNUNET_free (fname); 217 GNUNET_free (fname);
230 } 218 }
@@ -244,13 +232,15 @@ read_index_list ()
244 * @param msg error message 232 * @param msg error message
245 */ 233 */
246static void 234static void
247remove_cont (void *cls, int success, 235remove_cont (void *cls,
248 struct GNUNET_TIME_Absolute min_expiration, 236 int success,
249 const char *msg) 237 struct GNUNET_TIME_Absolute min_expiration,
238 const char *msg)
250{ 239{
251 if (GNUNET_OK != success) 240 if (GNUNET_OK != success)
252 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 241 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
253 _("Failed to delete bogus block: %s\n"), msg); 242 _ ("Failed to delete bogus block: %s\n"),
243 msg);
254} 244}
255 245
256 246
@@ -275,7 +265,7 @@ remove_cont (void *cls, int success,
275 * @return GNUNET_OK on success 265 * @return GNUNET_OK on success
276 */ 266 */
277int 267int
278GNUNET_FS_handle_on_demand_block (const struct GNUNET_HashCode * key, 268GNUNET_FS_handle_on_demand_block (const struct GNUNET_HashCode *key,
279 uint32_t size, 269 uint32_t size,
280 const void *data, 270 const void *data,
281 enum GNUNET_BLOCK_Type type, 271 enum GNUNET_BLOCK_Type type,
@@ -303,19 +293,12 @@ GNUNET_FS_handle_on_demand_block (const struct GNUNET_HashCode * key,
303 if (size != sizeof (struct OnDemandBlock)) 293 if (size != sizeof (struct OnDemandBlock))
304 { 294 {
305 GNUNET_break (0); 295 GNUNET_break (0);
306 GNUNET_DATASTORE_remove (dsh, 296 GNUNET_DATASTORE_remove (dsh, key, size, data, -1, -1, &remove_cont, NULL);
307 key,
308 size,
309 data,
310 -1,
311 -1,
312 &remove_cont, NULL);
313 return GNUNET_SYSERR; 297 return GNUNET_SYSERR;
314 } 298 }
315 odb = (const struct OnDemandBlock *) data; 299 odb = (const struct OnDemandBlock *) data;
316 off = GNUNET_ntohll (odb->offset); 300 off = GNUNET_ntohll (odb->offset);
317 ii = GNUNET_CONTAINER_multihashmap_get (ifm, 301 ii = GNUNET_CONTAINER_multihashmap_get (ifm, &odb->file_id);
318 &odb->file_id);
319 if (NULL == ii) 302 if (NULL == ii)
320 { 303 {
321 GNUNET_break (0); 304 GNUNET_break (0);
@@ -325,83 +308,46 @@ GNUNET_FS_handle_on_demand_block (const struct GNUNET_HashCode * key,
325 return GNUNET_SYSERR; 308 return GNUNET_SYSERR;
326 } 309 }
327 fn = ii->filename; 310 fn = ii->filename;
328 if ((NULL == fn) || (0 != ACCESS (fn, R_OK))) 311 if ((NULL == fn) || (0 != access (fn, R_OK)))
329 { 312 {
330 GNUNET_STATISTICS_update (GSF_stats, 313 GNUNET_STATISTICS_update (
331 gettext_noop ("# index blocks removed: original file inaccessible"), 314 GSF_stats,
332 1, 315 gettext_noop ("# index blocks removed: original file inaccessible"),
333 GNUNET_YES); 316 1,
334 GNUNET_DATASTORE_remove (dsh, 317 GNUNET_YES);
335 key, 318 GNUNET_DATASTORE_remove (dsh, key, size, data, -1, -1, &remove_cont, NULL);
336 size,
337 data,
338 -1,
339 -1,
340 &remove_cont,
341 NULL);
342 return GNUNET_SYSERR; 319 return GNUNET_SYSERR;
343 } 320 }
344 if ( (NULL == 321 if ((NULL == (fh = GNUNET_DISK_file_open (fn,
345 (fh = 322 GNUNET_DISK_OPEN_READ,
346 GNUNET_DISK_file_open (fn, 323 GNUNET_DISK_PERM_NONE))) ||
347 GNUNET_DISK_OPEN_READ, 324 (off != GNUNET_DISK_file_seek (fh, off, GNUNET_DISK_SEEK_SET)) ||
348 GNUNET_DISK_PERM_NONE))) || 325 (-1 == (nsize = GNUNET_DISK_file_read (fh, ndata, sizeof (ndata)))))
349 (off != GNUNET_DISK_file_seek (fh,
350 off,
351 GNUNET_DISK_SEEK_SET)) ||
352 (-1 == (nsize = GNUNET_DISK_file_read (fh,
353 ndata,
354 sizeof (ndata)))) )
355 { 326 {
356 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 327 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
357 _("Could not access indexed file `%s' (%s) at offset %llu: %s\n"), 328 _ (
329 "Could not access indexed file `%s' (%s) at offset %llu: %s\n"),
358 GNUNET_h2s (&odb->file_id), 330 GNUNET_h2s (&odb->file_id),
359 fn, 331 fn,
360 (unsigned long long) off, 332 (unsigned long long) off,
361 (fn == NULL) ? _("not indexed") : STRERROR (errno)); 333 (fn == NULL) ? _ ("not indexed") : strerror (errno));
362 if (fh != NULL) 334 if (fh != NULL)
363 GNUNET_DISK_file_close (fh); 335 GNUNET_DISK_file_close (fh);
364 GNUNET_DATASTORE_remove (dsh, 336 GNUNET_DATASTORE_remove (dsh, key, size, data, -1, -1, &remove_cont, NULL);
365 key,
366 size,
367 data,
368 -1,
369 -1,
370 &remove_cont,
371 NULL);
372 return GNUNET_SYSERR; 337 return GNUNET_SYSERR;
373 } 338 }
374 GNUNET_DISK_file_close (fh); 339 GNUNET_DISK_file_close (fh);
375 GNUNET_CRYPTO_hash (ndata, 340 GNUNET_CRYPTO_hash (ndata, nsize, &nkey);
376 nsize, 341 GNUNET_CRYPTO_hash_to_aes_key (&nkey, &skey, &iv);
377 &nkey); 342 GNUNET_CRYPTO_symmetric_encrypt (ndata, nsize, &skey, &iv, edata);
378 GNUNET_CRYPTO_hash_to_aes_key (&nkey, 343 GNUNET_CRYPTO_hash (edata, nsize, &query);
379 &skey, 344 if (0 != memcmp (&query, key, sizeof (struct GNUNET_HashCode)))
380 &iv);
381 GNUNET_CRYPTO_symmetric_encrypt (ndata,
382 nsize,
383 &skey,
384 &iv,
385 edata);
386 GNUNET_CRYPTO_hash (edata,
387 nsize,
388 &query);
389 if (0 != memcmp (&query,
390 key,
391 sizeof (struct GNUNET_HashCode)))
392 { 345 {
393 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 346 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
394 _("Indexed file `%s' changed at offset %llu\n"), 347 _ ("Indexed file `%s' changed at offset %llu\n"),
395 fn, 348 fn,
396 (unsigned long long) off); 349 (unsigned long long) off);
397 GNUNET_DATASTORE_remove (dsh, 350 GNUNET_DATASTORE_remove (dsh, key, size, data, -1, -1, &remove_cont, NULL);
398 key,
399 size,
400 data,
401 -1,
402 -1,
403 &remove_cont,
404 NULL);
405 return GNUNET_SYSERR; 351 return GNUNET_SYSERR;
406 } 352 }
407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
@@ -440,27 +386,20 @@ GNUNET_FS_indexing_send_list (struct GNUNET_MQ_Handle *mq)
440 { 386 {
441 fn = pos->filename; 387 fn = pos->filename;
442 slen = strlen (fn) + 1; 388 slen = strlen (fn) + 1;
443 if (slen + sizeof (struct IndexInfoMessage) >= 389 if (slen + sizeof (struct IndexInfoMessage) >= GNUNET_MAX_MESSAGE_SIZE)
444 GNUNET_MAX_MESSAGE_SIZE)
445 { 390 {
446 GNUNET_break (0); 391 GNUNET_break (0);
447 break; 392 break;
448 } 393 }
449 env = GNUNET_MQ_msg_extra (iim, 394 env =
450 slen, 395 GNUNET_MQ_msg_extra (iim, slen, GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_ENTRY);
451 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_ENTRY);
452 iim->reserved = 0; 396 iim->reserved = 0;
453 iim->file_id = pos->file_id; 397 iim->file_id = pos->file_id;
454 GNUNET_memcpy (&iim[1], 398 GNUNET_memcpy (&iim[1], fn, slen);
455 fn, 399 GNUNET_MQ_send (mq, env);
456 slen);
457 GNUNET_MQ_send (mq,
458 env);
459 } 400 }
460 env = GNUNET_MQ_msg (iem, 401 env = GNUNET_MQ_msg (iem, GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_END);
461 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_END); 402 GNUNET_MQ_send (mq, env);
462 GNUNET_MQ_send (mq,
463 env);
464} 403}
465 404
466 405
@@ -477,17 +416,12 @@ GNUNET_FS_indexing_do_unindex (const struct GNUNET_HashCode *fid)
477 416
478 for (pos = indexed_files_head; NULL != pos; pos = pos->next) 417 for (pos = indexed_files_head; NULL != pos; pos = pos->next)
479 { 418 {
480 if (0 == memcmp (&pos->file_id, 419 if (0 == memcmp (&pos->file_id, fid, sizeof (struct GNUNET_HashCode)))
481 fid,
482 sizeof (struct GNUNET_HashCode)))
483 { 420 {
484 GNUNET_CONTAINER_DLL_remove (indexed_files_head, 421 GNUNET_CONTAINER_DLL_remove (indexed_files_head, indexed_files_tail, pos);
485 indexed_files_tail, 422 GNUNET_break (
486 pos); 423 GNUNET_OK ==
487 GNUNET_break (GNUNET_OK == 424 GNUNET_CONTAINER_multihashmap_remove (ifm, &pos->file_id, pos));
488 GNUNET_CONTAINER_multihashmap_remove (ifm,
489 &pos->file_id,
490 pos));
491 GNUNET_free (pos); 425 GNUNET_free (pos);
492 write_index_list (); 426 write_index_list ();
493 return GNUNET_YES; 427 return GNUNET_YES;
@@ -510,14 +444,15 @@ GNUNET_FS_add_to_index (const char *filename,
510 struct IndexInfo *ii; 444 struct IndexInfo *ii;
511 size_t slen; 445 size_t slen;
512 446
513 ii = GNUNET_CONTAINER_multihashmap_get (ifm, 447 ii = GNUNET_CONTAINER_multihashmap_get (ifm, file_id);
514 file_id);
515 if (NULL != ii) 448 if (NULL != ii)
516 { 449 {
517 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 450 GNUNET_log (
518 _("Index request received for file `%s' is already indexed as `%s'. Permitting anyway.\n"), 451 GNUNET_ERROR_TYPE_INFO,
519 filename, 452 _ (
520 ii->filename); 453 "Index request received for file `%s' is already indexed as `%s'. Permitting anyway.\n"),
454 filename,
455 ii->filename);
521 return; 456 return;
522 } 457 }
523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
@@ -528,17 +463,14 @@ GNUNET_FS_add_to_index (const char *filename,
528 ii = GNUNET_malloc (sizeof (struct IndexInfo) + slen); 463 ii = GNUNET_malloc (sizeof (struct IndexInfo) + slen);
529 ii->file_id = *file_id; 464 ii->file_id = *file_id;
530 ii->filename = (const char *) &ii[1]; 465 ii->filename = (const char *) &ii[1];
531 GNUNET_memcpy (&ii[1], 466 GNUNET_memcpy (&ii[1], filename, slen);
532 filename, 467 GNUNET_CONTAINER_DLL_insert (indexed_files_head, indexed_files_tail, ii);
533 slen);
534 GNUNET_CONTAINER_DLL_insert (indexed_files_head,
535 indexed_files_tail,
536 ii);
537 GNUNET_assert (GNUNET_OK == 468 GNUNET_assert (GNUNET_OK ==
538 GNUNET_CONTAINER_multihashmap_put (ifm, 469 GNUNET_CONTAINER_multihashmap_put (
539 &ii->file_id, 470 ifm,
540 ii, 471 &ii->file_id,
541 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); 472 ii,
473 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
542 write_index_list (); 474 write_index_list ();
543} 475}
544 476
@@ -553,15 +485,12 @@ GNUNET_FS_indexing_done ()
553 485
554 while (NULL != (pos = indexed_files_head)) 486 while (NULL != (pos = indexed_files_head))
555 { 487 {
556 GNUNET_CONTAINER_DLL_remove (indexed_files_head, 488 GNUNET_CONTAINER_DLL_remove (indexed_files_head, indexed_files_tail, pos);
557 indexed_files_tail,
558 pos);
559 if (pos->fhc != NULL) 489 if (pos->fhc != NULL)
560 GNUNET_CRYPTO_hash_file_cancel (pos->fhc); 490 GNUNET_CRYPTO_hash_file_cancel (pos->fhc);
561 GNUNET_break (GNUNET_OK == 491 GNUNET_break (
562 GNUNET_CONTAINER_multihashmap_remove (ifm, 492 GNUNET_OK ==
563 &pos->file_id, 493 GNUNET_CONTAINER_multihashmap_remove (ifm, &pos->file_id, pos));
564 pos));
565 GNUNET_free (pos); 494 GNUNET_free (pos);
566 } 495 }
567 GNUNET_CONTAINER_multihashmap_destroy (ifm); 496 GNUNET_CONTAINER_multihashmap_destroy (ifm);
@@ -582,8 +511,7 @@ GNUNET_FS_indexing_init (const struct GNUNET_CONFIGURATION_Handle *c,
582{ 511{
583 cfg = c; 512 cfg = c;
584 dsh = d; 513 dsh = d;
585 ifm = GNUNET_CONTAINER_multihashmap_create (128, 514 ifm = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_YES);
586 GNUNET_YES);
587 read_index_list (); 515 read_index_list ();
588 return GNUNET_OK; 516 return GNUNET_OK;
589} 517}