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
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file fs/gnunet-service-fs_indexing.c
23 * @brief program that provides indexing functions of the file-sharing service
24 * @author Christian Grothoff
28 #include "gnunet_core_service.h"
29 #include "gnunet_datastore_service.h"
30 #include "gnunet_peer_lib.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet_signatures.h"
33 #include "gnunet_util_lib.h"
34 #include "gnunet-service-fs.h"
35 #include "gnunet-service-fs_indexing.h"
39 * In-memory information about indexed files (also available
46 * This is a doubly linked list.
48 struct IndexInfo *next;
51 * This is a doubly linked list.
53 struct IndexInfo *prev;
56 * Name of the indexed file. Memory allocated
57 * at the end of this struct (do not free).
62 * Context for transmitting confirmation to client,
63 * NULL if we've done this already.
65 struct GNUNET_SERVER_TransmitContext *tc;
68 * Context for hashing of the file.
70 struct GNUNET_CRYPTO_FileHashContext *fhc;
73 * Hash of the contents of the file.
75 struct GNUNET_HashCode file_id;
81 * Head of linked list of indexed files.
82 * FIXME: we don't need both a DLL and a hashmap here!
84 static struct IndexInfo *indexed_files_head;
87 * Tail of linked list of indexed files.
89 static struct IndexInfo *indexed_files_tail;
92 * Maps hash over content of indexed files to the respective 'struct IndexInfo'.
93 * The filenames are pointers into the indexed_files linked list and
94 * do not need to be freed.
96 static struct GNUNET_CONTAINER_MultiHashMap *ifm;
101 static const struct GNUNET_CONFIGURATION_Handle *cfg;
104 * Datastore handle. Created and destroyed by code in
105 * gnunet-service-fs (this is an alias).
107 static struct GNUNET_DATASTORE_Handle *dsh;
111 * Write the current index information list to disk.
116 struct GNUNET_BIO_WriteHandle *wh;
118 struct IndexInfo *pos;
121 GNUNET_CONFIGURATION_get_value_filename (cfg, "FS",
125 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
130 wh = GNUNET_BIO_write_open (fn);
133 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
134 _("Could not open `%s'.\n"),
139 for (pos = indexed_files_head; NULL != pos; pos = pos->next)
141 GNUNET_BIO_write (wh,
143 sizeof (struct GNUNET_HashCode))) ||
145 GNUNET_BIO_write_string (wh,
148 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
150 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
151 _("Error writing `%s'.\n"),
161 * Read index information from disk.
166 struct GNUNET_BIO_ReadHandle *rh;
168 struct IndexInfo *pos;
170 struct GNUNET_HashCode hc;
175 GNUNET_CONFIGURATION_get_value_filename (cfg,
180 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
185 if (GNUNET_NO == GNUNET_DISK_file_test (fn))
187 /* no index info yet */
191 rh = GNUNET_BIO_read_open (fn);
194 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
195 _("Could not open `%s'.\n"),
200 while ( (GNUNET_OK ==
202 "Hash of indexed file",
204 sizeof (struct GNUNET_HashCode))) &&
206 GNUNET_BIO_read_string (rh,
207 "Name of indexed file",
212 slen = strlen (fname) + 1;
213 pos = GNUNET_malloc (sizeof (struct IndexInfo) + slen);
215 pos->filename = (const char *) &pos[1];
216 GNUNET_memcpy (&pos[1], fname, slen);
218 GNUNET_CONTAINER_multihashmap_put (ifm, &pos->file_id, pos,
219 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
225 GNUNET_CONTAINER_DLL_insert (indexed_files_head,
231 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
238 * Continuation called from datastore's remove
242 * @param success did the deletion work?
243 * @param min_expiration minimum expiration time required for content to be stored
244 * @param msg error message
247 remove_cont (void *cls, int success,
248 struct GNUNET_TIME_Absolute min_expiration,
251 if (GNUNET_OK != success)
252 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
253 _("Failed to delete bogus block: %s\n"), msg);
258 * We've received an on-demand encoded block from the datastore.
259 * Attempt to do on-demand encoding and (if successful), call the
260 * continuation with the resulting block. On error, clean up and ask
261 * the datastore for more results.
263 * @param key key for the content
264 * @param size number of bytes in data
265 * @param data content stored
266 * @param type type of the content
267 * @param priority priority of the content
268 * @param anonymity anonymity-level for the content
269 * @param expiration expiration time for the content
270 * @param uid unique identifier for the datum;
271 * maybe 0 if no unique identifier is available
272 * @param cont function to call with the actual block (at most once, on success)
273 * @param cont_cls closure for cont
274 * @return GNUNET_OK on success
277 GNUNET_FS_handle_on_demand_block (const struct GNUNET_HashCode * key,
280 enum GNUNET_BLOCK_Type type,
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,
422 * Transmit information about indexed files to @a mq.
424 * @param mq message queue to send information to
427 GNUNET_FS_indexing_send_list (struct GNUNET_MQ_Handle *mq)
429 struct GNUNET_MQ_Envelope *env;
430 struct IndexInfoMessage *iim;
431 struct GNUNET_MessageHeader *iem;
434 struct IndexInfo *pos;
436 for (pos = indexed_files_head; NULL != pos; pos = pos->next)
439 slen = strlen (fn) + 1;
440 if (slen + sizeof (struct IndexInfoMessage) >=
441 GNUNET_SERVER_MAX_MESSAGE_SIZE)
446 env = GNUNET_MQ_msg_extra (iim,
448 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_ENTRY);
450 iim->file_id = pos->file_id;
451 GNUNET_memcpy (&iim[1],
457 env = GNUNET_MQ_msg (iem,
458 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_END);
465 * Remove a file from the index.
467 * @param fid identifier of the file to remove
468 * @return #GNUNET_YES if the @a fid was found
471 GNUNET_FS_indexing_do_unindex (const struct GNUNET_HashCode *fid)
473 struct IndexInfo *pos;
475 for (pos = indexed_files_head; NULL != pos; pos = pos->next)
477 if (0 == memcmp (&pos->file_id,
479 sizeof (struct GNUNET_HashCode)))
481 GNUNET_CONTAINER_DLL_remove (indexed_files_head,
484 GNUNET_break (GNUNET_OK ==
485 GNUNET_CONTAINER_multihashmap_remove (ifm,
498 * Add the given file to the list of indexed files.
500 * @param filename name of the file
501 * @param file_id hash identifier for @a filename
504 GNUNET_FS_add_to_index (const char *filename,
505 const struct GNUNET_HashCode *file_id)
507 struct IndexInfo *ii;
510 ii = GNUNET_CONTAINER_multihashmap_get (ifm,
514 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
515 _("Index request received for file `%s' is already indexed as `%s'. Permitting anyway.\n"),
520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
521 "Adding file %s to index as %s\n",
523 GNUNET_h2s (file_id));
524 slen = strlen (filename) + 1;
525 ii = GNUNET_malloc (sizeof (struct IndexInfo) + slen);
526 ii->file_id = *file_id;
527 ii->filename = (const char *) &ii[1];
531 GNUNET_CONTAINER_DLL_insert (indexed_files_head,
534 GNUNET_assert (GNUNET_OK ==
535 GNUNET_CONTAINER_multihashmap_put (ifm,
538 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
544 * Shutdown the module.
547 GNUNET_FS_indexing_done ()
549 struct IndexInfo *pos;
551 while (NULL != (pos = indexed_files_head))
553 GNUNET_CONTAINER_DLL_remove (indexed_files_head,
556 if (pos->fhc != NULL)
557 GNUNET_CRYPTO_hash_file_cancel (pos->fhc);
558 GNUNET_break (GNUNET_OK ==
559 GNUNET_CONTAINER_multihashmap_remove (ifm,
564 GNUNET_CONTAINER_multihashmap_destroy (ifm);
571 * Initialize the indexing submodule.
573 * @param c configuration to use
574 * @param d datastore to use
577 GNUNET_FS_indexing_init (const struct GNUNET_CONFIGURATION_Handle *c,
578 struct GNUNET_DATASTORE_Handle *d)
582 ifm = GNUNET_CONTAINER_multihashmap_create (128,
588 /* end of gnunet-service-fs_indexing.c */