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 replication replication-level for the content
270 * @param expiration expiration time for the content
271 * @param uid unique identifier for the datum;
272 * maybe 0 if no unique identifier is available
273 * @param cont function to call with the actual block (at most once, on success)
274 * @param cont_cls closure for cont
275 * @return GNUNET_OK on success
278 GNUNET_FS_handle_on_demand_block (const struct GNUNET_HashCode * key,
281 enum GNUNET_BLOCK_Type type,
284 uint32_t replication,
285 struct GNUNET_TIME_Absolute expiration,
287 GNUNET_DATASTORE_DatumProcessor cont,
290 const struct OnDemandBlock *odb;
291 struct GNUNET_HashCode nkey;
292 struct GNUNET_CRYPTO_SymmetricSessionKey skey;
293 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
294 struct GNUNET_HashCode query;
296 char ndata[DBLOCK_SIZE];
297 char edata[DBLOCK_SIZE];
299 struct GNUNET_DISK_FileHandle *fh;
301 struct IndexInfo *ii;
303 if (size != sizeof (struct OnDemandBlock))
306 GNUNET_DATASTORE_remove (dsh,
313 return GNUNET_SYSERR;
315 odb = (const struct OnDemandBlock *) data;
316 off = GNUNET_ntohll (odb->offset);
317 ii = GNUNET_CONTAINER_multihashmap_get (ifm,
322 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
323 "Failed to find index %s\n",
324 GNUNET_h2s (&odb->file_id));
325 return GNUNET_SYSERR;
328 if ((NULL == fn) || (0 != ACCESS (fn, R_OK)))
330 GNUNET_STATISTICS_update (GSF_stats,
331 gettext_noop ("# index blocks removed: original file inaccessible"),
334 GNUNET_DATASTORE_remove (dsh,
342 return GNUNET_SYSERR;
346 GNUNET_DISK_file_open (fn,
347 GNUNET_DISK_OPEN_READ,
348 GNUNET_DISK_PERM_NONE))) ||
349 (off != GNUNET_DISK_file_seek (fh,
351 GNUNET_DISK_SEEK_SET)) ||
352 (-1 == (nsize = GNUNET_DISK_file_read (fh,
356 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
357 _("Could not access indexed file `%s' (%s) at offset %llu: %s\n"),
358 GNUNET_h2s (&odb->file_id),
360 (unsigned long long) off,
361 (fn == NULL) ? _("not indexed") : STRERROR (errno));
363 GNUNET_DISK_file_close (fh);
364 GNUNET_DATASTORE_remove (dsh,
372 return GNUNET_SYSERR;
374 GNUNET_DISK_file_close (fh);
375 GNUNET_CRYPTO_hash (ndata,
378 GNUNET_CRYPTO_hash_to_aes_key (&nkey,
381 GNUNET_CRYPTO_symmetric_encrypt (ndata,
386 GNUNET_CRYPTO_hash (edata,
389 if (0 != memcmp (&query,
391 sizeof (struct GNUNET_HashCode)))
393 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
394 _("Indexed file `%s' changed at offset %llu\n"),
396 (unsigned long long) off);
397 GNUNET_DATASTORE_remove (dsh,
405 return GNUNET_SYSERR;
407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
408 "On-demand encoded block for query `%s'\n",
414 GNUNET_BLOCK_TYPE_FS_DBLOCK,
425 * Transmit information about indexed files to @a mq.
427 * @param mq message queue to send information to
430 GNUNET_FS_indexing_send_list (struct GNUNET_MQ_Handle *mq)
432 struct GNUNET_MQ_Envelope *env;
433 struct IndexInfoMessage *iim;
434 struct GNUNET_MessageHeader *iem;
437 struct IndexInfo *pos;
439 for (pos = indexed_files_head; NULL != pos; pos = pos->next)
442 slen = strlen (fn) + 1;
443 if (slen + sizeof (struct IndexInfoMessage) >=
444 GNUNET_MAX_MESSAGE_SIZE)
449 env = GNUNET_MQ_msg_extra (iim,
451 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_ENTRY);
453 iim->file_id = pos->file_id;
454 GNUNET_memcpy (&iim[1],
460 env = GNUNET_MQ_msg (iem,
461 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_END);
468 * Remove a file from the index.
470 * @param fid identifier of the file to remove
471 * @return #GNUNET_YES if the @a fid was found
474 GNUNET_FS_indexing_do_unindex (const struct GNUNET_HashCode *fid)
476 struct IndexInfo *pos;
478 for (pos = indexed_files_head; NULL != pos; pos = pos->next)
480 if (0 == memcmp (&pos->file_id,
482 sizeof (struct GNUNET_HashCode)))
484 GNUNET_CONTAINER_DLL_remove (indexed_files_head,
487 GNUNET_break (GNUNET_OK ==
488 GNUNET_CONTAINER_multihashmap_remove (ifm,
501 * Add the given file to the list of indexed files.
503 * @param filename name of the file
504 * @param file_id hash identifier for @a filename
507 GNUNET_FS_add_to_index (const char *filename,
508 const struct GNUNET_HashCode *file_id)
510 struct IndexInfo *ii;
513 ii = GNUNET_CONTAINER_multihashmap_get (ifm,
517 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
518 _("Index request received for file `%s' is already indexed as `%s'. Permitting anyway.\n"),
523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
524 "Adding file %s to index as %s\n",
526 GNUNET_h2s (file_id));
527 slen = strlen (filename) + 1;
528 ii = GNUNET_malloc (sizeof (struct IndexInfo) + slen);
529 ii->file_id = *file_id;
530 ii->filename = (const char *) &ii[1];
534 GNUNET_CONTAINER_DLL_insert (indexed_files_head,
537 GNUNET_assert (GNUNET_OK ==
538 GNUNET_CONTAINER_multihashmap_put (ifm,
541 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
547 * Shutdown the module.
550 GNUNET_FS_indexing_done ()
552 struct IndexInfo *pos;
554 while (NULL != (pos = indexed_files_head))
556 GNUNET_CONTAINER_DLL_remove (indexed_files_head,
559 if (pos->fhc != NULL)
560 GNUNET_CRYPTO_hash_file_cancel (pos->fhc);
561 GNUNET_break (GNUNET_OK ==
562 GNUNET_CONTAINER_multihashmap_remove (ifm,
567 GNUNET_CONTAINER_multihashmap_destroy (ifm);
574 * Initialize the indexing submodule.
576 * @param c configuration to use
577 * @param d datastore to use
580 GNUNET_FS_indexing_init (const struct GNUNET_CONFIGURATION_Handle *c,
581 struct GNUNET_DATASTORE_Handle *d)
585 ifm = GNUNET_CONTAINER_multihashmap_create (128,
591 /* end of gnunet-service-fs_indexing.c */