2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
17 * @file fs/gnunet-service-fs_indexing.c
18 * @brief program that provides indexing functions of the file-sharing service
19 * @author Christian Grothoff
23 #include "gnunet_core_service.h"
24 #include "gnunet_datastore_service.h"
25 #include "gnunet_peer_lib.h"
26 #include "gnunet_protocols.h"
27 #include "gnunet_signatures.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet-service-fs.h"
30 #include "gnunet-service-fs_indexing.h"
34 * In-memory information about indexed files (also available
41 * This is a doubly linked list.
43 struct IndexInfo *next;
46 * This is a doubly linked list.
48 struct IndexInfo *prev;
51 * Name of the indexed file. Memory allocated
52 * at the end of this struct (do not free).
57 * Context for transmitting confirmation to client,
58 * NULL if we've done this already.
60 struct GNUNET_SERVER_TransmitContext *tc;
63 * Context for hashing of the file.
65 struct GNUNET_CRYPTO_FileHashContext *fhc;
68 * Hash of the contents of the file.
70 struct GNUNET_HashCode file_id;
76 * Head of linked list of indexed files.
77 * FIXME: we don't need both a DLL and a hashmap here!
79 static struct IndexInfo *indexed_files_head;
82 * Tail of linked list of indexed files.
84 static struct IndexInfo *indexed_files_tail;
87 * Maps hash over content of indexed files to the respective 'struct IndexInfo'.
88 * The filenames are pointers into the indexed_files linked list and
89 * do not need to be freed.
91 static struct GNUNET_CONTAINER_MultiHashMap *ifm;
96 static const struct GNUNET_CONFIGURATION_Handle *cfg;
99 * Datastore handle. Created and destroyed by code in
100 * gnunet-service-fs (this is an alias).
102 static struct GNUNET_DATASTORE_Handle *dsh;
106 * Write the current index information list to disk.
111 struct GNUNET_BIO_WriteHandle *wh;
113 struct IndexInfo *pos;
116 GNUNET_CONFIGURATION_get_value_filename (cfg, "FS",
120 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
125 wh = GNUNET_BIO_write_open (fn);
128 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
129 _("Could not open `%s'.\n"),
134 for (pos = indexed_files_head; NULL != pos; pos = pos->next)
136 GNUNET_BIO_write (wh,
138 sizeof (struct GNUNET_HashCode))) ||
140 GNUNET_BIO_write_string (wh,
143 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
145 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
146 _("Error writing `%s'.\n"),
156 * Read index information from disk.
161 struct GNUNET_BIO_ReadHandle *rh;
163 struct IndexInfo *pos;
165 struct GNUNET_HashCode hc;
170 GNUNET_CONFIGURATION_get_value_filename (cfg,
175 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
180 if (GNUNET_NO == GNUNET_DISK_file_test (fn))
182 /* no index info yet */
186 rh = GNUNET_BIO_read_open (fn);
189 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
190 _("Could not open `%s'.\n"),
195 while ( (GNUNET_OK ==
197 "Hash of indexed file",
199 sizeof (struct GNUNET_HashCode))) &&
201 GNUNET_BIO_read_string (rh,
202 "Name of indexed file",
207 slen = strlen (fname) + 1;
208 pos = GNUNET_malloc (sizeof (struct IndexInfo) + slen);
210 pos->filename = (const char *) &pos[1];
211 GNUNET_memcpy (&pos[1], fname, slen);
213 GNUNET_CONTAINER_multihashmap_put (ifm, &pos->file_id, pos,
214 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
220 GNUNET_CONTAINER_DLL_insert (indexed_files_head,
226 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
233 * Continuation called from datastore's remove
237 * @param success did the deletion work?
238 * @param min_expiration minimum expiration time required for content to be stored
239 * @param msg error message
242 remove_cont (void *cls, int success,
243 struct GNUNET_TIME_Absolute min_expiration,
246 if (GNUNET_OK != success)
247 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
248 _("Failed to delete bogus block: %s\n"), msg);
253 * We've received an on-demand encoded block from the datastore.
254 * Attempt to do on-demand encoding and (if successful), call the
255 * continuation with the resulting block. On error, clean up and ask
256 * the datastore for more results.
258 * @param key key for the content
259 * @param size number of bytes in data
260 * @param data content stored
261 * @param type type of the content
262 * @param priority priority of the content
263 * @param anonymity anonymity-level for the content
264 * @param replication replication-level for the content
265 * @param expiration expiration time for the content
266 * @param uid unique identifier for the datum;
267 * maybe 0 if no unique identifier is available
268 * @param cont function to call with the actual block (at most once, on success)
269 * @param cont_cls closure for cont
270 * @return GNUNET_OK on success
273 GNUNET_FS_handle_on_demand_block (const struct GNUNET_HashCode * key,
276 enum GNUNET_BLOCK_Type type,
279 uint32_t replication,
280 struct GNUNET_TIME_Absolute expiration,
282 GNUNET_DATASTORE_DatumProcessor cont,
285 const struct OnDemandBlock *odb;
286 struct GNUNET_HashCode nkey;
287 struct GNUNET_CRYPTO_SymmetricSessionKey skey;
288 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
289 struct GNUNET_HashCode query;
291 char ndata[DBLOCK_SIZE];
292 char edata[DBLOCK_SIZE];
294 struct GNUNET_DISK_FileHandle *fh;
296 struct IndexInfo *ii;
298 if (size != sizeof (struct OnDemandBlock))
301 GNUNET_DATASTORE_remove (dsh,
308 return GNUNET_SYSERR;
310 odb = (const struct OnDemandBlock *) data;
311 off = GNUNET_ntohll (odb->offset);
312 ii = GNUNET_CONTAINER_multihashmap_get (ifm,
317 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
318 "Failed to find index %s\n",
319 GNUNET_h2s (&odb->file_id));
320 return GNUNET_SYSERR;
323 if ((NULL == fn) || (0 != ACCESS (fn, R_OK)))
325 GNUNET_STATISTICS_update (GSF_stats,
326 gettext_noop ("# index blocks removed: original file inaccessible"),
329 GNUNET_DATASTORE_remove (dsh,
337 return GNUNET_SYSERR;
341 GNUNET_DISK_file_open (fn,
342 GNUNET_DISK_OPEN_READ,
343 GNUNET_DISK_PERM_NONE))) ||
344 (off != GNUNET_DISK_file_seek (fh,
346 GNUNET_DISK_SEEK_SET)) ||
347 (-1 == (nsize = GNUNET_DISK_file_read (fh,
351 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
352 _("Could not access indexed file `%s' (%s) at offset %llu: %s\n"),
353 GNUNET_h2s (&odb->file_id),
355 (unsigned long long) off,
356 (fn == NULL) ? _("not indexed") : STRERROR (errno));
358 GNUNET_DISK_file_close (fh);
359 GNUNET_DATASTORE_remove (dsh,
367 return GNUNET_SYSERR;
369 GNUNET_DISK_file_close (fh);
370 GNUNET_CRYPTO_hash (ndata,
373 GNUNET_CRYPTO_hash_to_aes_key (&nkey,
376 GNUNET_CRYPTO_symmetric_encrypt (ndata,
381 GNUNET_CRYPTO_hash (edata,
384 if (0 != memcmp (&query,
386 sizeof (struct GNUNET_HashCode)))
388 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
389 _("Indexed file `%s' changed at offset %llu\n"),
391 (unsigned long long) off);
392 GNUNET_DATASTORE_remove (dsh,
400 return GNUNET_SYSERR;
402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
403 "On-demand encoded block for query `%s'\n",
409 GNUNET_BLOCK_TYPE_FS_DBLOCK,
420 * Transmit information about indexed files to @a mq.
422 * @param mq message queue to send information to
425 GNUNET_FS_indexing_send_list (struct GNUNET_MQ_Handle *mq)
427 struct GNUNET_MQ_Envelope *env;
428 struct IndexInfoMessage *iim;
429 struct GNUNET_MessageHeader *iem;
432 struct IndexInfo *pos;
434 for (pos = indexed_files_head; NULL != pos; pos = pos->next)
437 slen = strlen (fn) + 1;
438 if (slen + sizeof (struct IndexInfoMessage) >=
439 GNUNET_MAX_MESSAGE_SIZE)
444 env = GNUNET_MQ_msg_extra (iim,
446 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_ENTRY);
448 iim->file_id = pos->file_id;
449 GNUNET_memcpy (&iim[1],
455 env = GNUNET_MQ_msg (iem,
456 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_END);
463 * Remove a file from the index.
465 * @param fid identifier of the file to remove
466 * @return #GNUNET_YES if the @a fid was found
469 GNUNET_FS_indexing_do_unindex (const struct GNUNET_HashCode *fid)
471 struct IndexInfo *pos;
473 for (pos = indexed_files_head; NULL != pos; pos = pos->next)
475 if (0 == memcmp (&pos->file_id,
477 sizeof (struct GNUNET_HashCode)))
479 GNUNET_CONTAINER_DLL_remove (indexed_files_head,
482 GNUNET_break (GNUNET_OK ==
483 GNUNET_CONTAINER_multihashmap_remove (ifm,
496 * Add the given file to the list of indexed files.
498 * @param filename name of the file
499 * @param file_id hash identifier for @a filename
502 GNUNET_FS_add_to_index (const char *filename,
503 const struct GNUNET_HashCode *file_id)
505 struct IndexInfo *ii;
508 ii = GNUNET_CONTAINER_multihashmap_get (ifm,
512 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
513 _("Index request received for file `%s' is already indexed as `%s'. Permitting anyway.\n"),
518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
519 "Adding file %s to index as %s\n",
521 GNUNET_h2s (file_id));
522 slen = strlen (filename) + 1;
523 ii = GNUNET_malloc (sizeof (struct IndexInfo) + slen);
524 ii->file_id = *file_id;
525 ii->filename = (const char *) &ii[1];
526 GNUNET_memcpy (&ii[1],
529 GNUNET_CONTAINER_DLL_insert (indexed_files_head,
532 GNUNET_assert (GNUNET_OK ==
533 GNUNET_CONTAINER_multihashmap_put (ifm,
536 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
542 * Shutdown the module.
545 GNUNET_FS_indexing_done ()
547 struct IndexInfo *pos;
549 while (NULL != (pos = indexed_files_head))
551 GNUNET_CONTAINER_DLL_remove (indexed_files_head,
554 if (pos->fhc != NULL)
555 GNUNET_CRYPTO_hash_file_cancel (pos->fhc);
556 GNUNET_break (GNUNET_OK ==
557 GNUNET_CONTAINER_multihashmap_remove (ifm,
562 GNUNET_CONTAINER_multihashmap_destroy (ifm);
569 * Initialize the indexing submodule.
571 * @param c configuration to use
572 * @param d datastore to use
575 GNUNET_FS_indexing_init (const struct GNUNET_CONFIGURATION_Handle *c,
576 struct GNUNET_DATASTORE_Handle *d)
580 ifm = GNUNET_CONTAINER_multihashmap_create (128,
586 /* end of gnunet-service-fs_indexing.c */