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.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @file fs/gnunet-service-fs_indexing.c
21 * @brief program that provides indexing functions of the file-sharing service
22 * @author Christian Grothoff
26 #include "gnunet_core_service.h"
27 #include "gnunet_datastore_service.h"
28 #include "gnunet_peer_lib.h"
29 #include "gnunet_protocols.h"
30 #include "gnunet_signatures.h"
31 #include "gnunet_util_lib.h"
32 #include "gnunet-service-fs.h"
33 #include "gnunet-service-fs_indexing.h"
37 * In-memory information about indexed files (also available
44 * This is a doubly linked list.
46 struct IndexInfo *next;
49 * This is a doubly linked list.
51 struct IndexInfo *prev;
54 * Name of the indexed file. Memory allocated
55 * at the end of this struct (do not free).
60 * Context for transmitting confirmation to client,
61 * NULL if we've done this already.
63 struct GNUNET_SERVER_TransmitContext *tc;
66 * Context for hashing of the file.
68 struct GNUNET_CRYPTO_FileHashContext *fhc;
71 * Hash of the contents of the file.
73 struct GNUNET_HashCode file_id;
79 * Head of linked list of indexed files.
80 * FIXME: we don't need both a DLL and a hashmap here!
82 static struct IndexInfo *indexed_files_head;
85 * Tail of linked list of indexed files.
87 static struct IndexInfo *indexed_files_tail;
90 * Maps hash over content of indexed files to the respective 'struct IndexInfo'.
91 * The filenames are pointers into the indexed_files linked list and
92 * do not need to be freed.
94 static struct GNUNET_CONTAINER_MultiHashMap *ifm;
99 static const struct GNUNET_CONFIGURATION_Handle *cfg;
102 * Datastore handle. Created and destroyed by code in
103 * gnunet-service-fs (this is an alias).
105 static struct GNUNET_DATASTORE_Handle *dsh;
109 * Write the current index information list to disk.
114 struct GNUNET_BIO_WriteHandle *wh;
116 struct IndexInfo *pos;
119 GNUNET_CONFIGURATION_get_value_filename (cfg, "FS",
123 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
128 wh = GNUNET_BIO_write_open (fn);
131 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
132 _("Could not open `%s'.\n"),
137 for (pos = indexed_files_head; NULL != pos; pos = pos->next)
139 GNUNET_BIO_write (wh,
141 sizeof (struct GNUNET_HashCode))) ||
143 GNUNET_BIO_write_string (wh,
146 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
148 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
149 _("Error writing `%s'.\n"),
159 * Read index information from disk.
164 struct GNUNET_BIO_ReadHandle *rh;
166 struct IndexInfo *pos;
168 struct GNUNET_HashCode hc;
173 GNUNET_CONFIGURATION_get_value_filename (cfg,
178 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
183 if (GNUNET_NO == GNUNET_DISK_file_test (fn))
185 /* no index info yet */
189 rh = GNUNET_BIO_read_open (fn);
192 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
193 _("Could not open `%s'.\n"),
198 while ( (GNUNET_OK ==
200 "Hash of indexed file",
202 sizeof (struct GNUNET_HashCode))) &&
204 GNUNET_BIO_read_string (rh,
205 "Name of indexed file",
210 slen = strlen (fname) + 1;
211 pos = GNUNET_malloc (sizeof (struct IndexInfo) + slen);
213 pos->filename = (const char *) &pos[1];
214 GNUNET_memcpy (&pos[1], fname, slen);
216 GNUNET_CONTAINER_multihashmap_put (ifm, &pos->file_id, pos,
217 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
223 GNUNET_CONTAINER_DLL_insert (indexed_files_head,
229 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
236 * Continuation called from datastore's remove
240 * @param success did the deletion work?
241 * @param min_expiration minimum expiration time required for content to be stored
242 * @param msg error message
245 remove_cont (void *cls, int success,
246 struct GNUNET_TIME_Absolute min_expiration,
249 if (GNUNET_OK != success)
250 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
251 _("Failed to delete bogus block: %s\n"), msg);
256 * We've received an on-demand encoded block from the datastore.
257 * Attempt to do on-demand encoding and (if successful), call the
258 * continuation with the resulting block. On error, clean up and ask
259 * the datastore for more results.
261 * @param key key for the content
262 * @param size number of bytes in data
263 * @param data content stored
264 * @param type type of the content
265 * @param priority priority of the content
266 * @param anonymity anonymity-level for the content
267 * @param replication replication-level for the content
268 * @param expiration expiration time for the content
269 * @param uid unique identifier for the datum;
270 * maybe 0 if no unique identifier is available
271 * @param cont function to call with the actual block (at most once, on success)
272 * @param cont_cls closure for cont
273 * @return GNUNET_OK on success
276 GNUNET_FS_handle_on_demand_block (const struct GNUNET_HashCode * key,
279 enum GNUNET_BLOCK_Type type,
282 uint32_t replication,
283 struct GNUNET_TIME_Absolute expiration,
285 GNUNET_DATASTORE_DatumProcessor cont,
288 const struct OnDemandBlock *odb;
289 struct GNUNET_HashCode nkey;
290 struct GNUNET_CRYPTO_SymmetricSessionKey skey;
291 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
292 struct GNUNET_HashCode query;
294 char ndata[DBLOCK_SIZE];
295 char edata[DBLOCK_SIZE];
297 struct GNUNET_DISK_FileHandle *fh;
299 struct IndexInfo *ii;
301 if (size != sizeof (struct OnDemandBlock))
304 GNUNET_DATASTORE_remove (dsh,
311 return GNUNET_SYSERR;
313 odb = (const struct OnDemandBlock *) data;
314 off = GNUNET_ntohll (odb->offset);
315 ii = GNUNET_CONTAINER_multihashmap_get (ifm,
320 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
321 "Failed to find index %s\n",
322 GNUNET_h2s (&odb->file_id));
323 return GNUNET_SYSERR;
326 if ((NULL == fn) || (0 != ACCESS (fn, R_OK)))
328 GNUNET_STATISTICS_update (GSF_stats,
329 gettext_noop ("# index blocks removed: original file inaccessible"),
332 GNUNET_DATASTORE_remove (dsh,
340 return GNUNET_SYSERR;
344 GNUNET_DISK_file_open (fn,
345 GNUNET_DISK_OPEN_READ,
346 GNUNET_DISK_PERM_NONE))) ||
347 (off != GNUNET_DISK_file_seek (fh,
349 GNUNET_DISK_SEEK_SET)) ||
350 (-1 == (nsize = GNUNET_DISK_file_read (fh,
354 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
355 _("Could not access indexed file `%s' (%s) at offset %llu: %s\n"),
356 GNUNET_h2s (&odb->file_id),
358 (unsigned long long) off,
359 (fn == NULL) ? _("not indexed") : STRERROR (errno));
361 GNUNET_DISK_file_close (fh);
362 GNUNET_DATASTORE_remove (dsh,
370 return GNUNET_SYSERR;
372 GNUNET_DISK_file_close (fh);
373 GNUNET_CRYPTO_hash (ndata,
376 GNUNET_CRYPTO_hash_to_aes_key (&nkey,
379 GNUNET_CRYPTO_symmetric_encrypt (ndata,
384 GNUNET_CRYPTO_hash (edata,
387 if (0 != memcmp (&query,
389 sizeof (struct GNUNET_HashCode)))
391 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
392 _("Indexed file `%s' changed at offset %llu\n"),
394 (unsigned long long) off);
395 GNUNET_DATASTORE_remove (dsh,
403 return GNUNET_SYSERR;
405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
406 "On-demand encoded block for query `%s'\n",
412 GNUNET_BLOCK_TYPE_FS_DBLOCK,
423 * Transmit information about indexed files to @a mq.
425 * @param mq message queue to send information to
428 GNUNET_FS_indexing_send_list (struct GNUNET_MQ_Handle *mq)
430 struct GNUNET_MQ_Envelope *env;
431 struct IndexInfoMessage *iim;
432 struct GNUNET_MessageHeader *iem;
435 struct IndexInfo *pos;
437 for (pos = indexed_files_head; NULL != pos; pos = pos->next)
440 slen = strlen (fn) + 1;
441 if (slen + sizeof (struct IndexInfoMessage) >=
442 GNUNET_MAX_MESSAGE_SIZE)
447 env = GNUNET_MQ_msg_extra (iim,
449 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_ENTRY);
451 iim->file_id = pos->file_id;
452 GNUNET_memcpy (&iim[1],
458 env = GNUNET_MQ_msg (iem,
459 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_END);
466 * Remove a file from the index.
468 * @param fid identifier of the file to remove
469 * @return #GNUNET_YES if the @a fid was found
472 GNUNET_FS_indexing_do_unindex (const struct GNUNET_HashCode *fid)
474 struct IndexInfo *pos;
476 for (pos = indexed_files_head; NULL != pos; pos = pos->next)
478 if (0 == memcmp (&pos->file_id,
480 sizeof (struct GNUNET_HashCode)))
482 GNUNET_CONTAINER_DLL_remove (indexed_files_head,
485 GNUNET_break (GNUNET_OK ==
486 GNUNET_CONTAINER_multihashmap_remove (ifm,
499 * Add the given file to the list of indexed files.
501 * @param filename name of the file
502 * @param file_id hash identifier for @a filename
505 GNUNET_FS_add_to_index (const char *filename,
506 const struct GNUNET_HashCode *file_id)
508 struct IndexInfo *ii;
511 ii = GNUNET_CONTAINER_multihashmap_get (ifm,
515 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
516 _("Index request received for file `%s' is already indexed as `%s'. Permitting anyway.\n"),
521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
522 "Adding file %s to index as %s\n",
524 GNUNET_h2s (file_id));
525 slen = strlen (filename) + 1;
526 ii = GNUNET_malloc (sizeof (struct IndexInfo) + slen);
527 ii->file_id = *file_id;
528 ii->filename = (const char *) &ii[1];
529 GNUNET_memcpy (&ii[1],
532 GNUNET_CONTAINER_DLL_insert (indexed_files_head,
535 GNUNET_assert (GNUNET_OK ==
536 GNUNET_CONTAINER_multihashmap_put (ifm,
539 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
545 * Shutdown the module.
548 GNUNET_FS_indexing_done ()
550 struct IndexInfo *pos;
552 while (NULL != (pos = indexed_files_head))
554 GNUNET_CONTAINER_DLL_remove (indexed_files_head,
557 if (pos->fhc != NULL)
558 GNUNET_CRYPTO_hash_file_cancel (pos->fhc);
559 GNUNET_break (GNUNET_OK ==
560 GNUNET_CONTAINER_multihashmap_remove (ifm,
565 GNUNET_CONTAINER_multihashmap_destroy (ifm);
572 * Initialize the indexing submodule.
574 * @param c configuration to use
575 * @param d datastore to use
578 GNUNET_FS_indexing_init (const struct GNUNET_CONFIGURATION_Handle *c,
579 struct GNUNET_DATASTORE_Handle *d)
583 ifm = GNUNET_CONTAINER_multihashmap_create (128,
589 /* end of gnunet-service-fs_indexing.c */