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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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
45 * This is a doubly linked list.
47 struct IndexInfo *next;
50 * This is a doubly linked list.
52 struct IndexInfo *prev;
55 * Name of the indexed file. Memory allocated
56 * at the end of this struct (do not free).
61 * Context for transmitting confirmation to client,
62 * NULL if we've done this already.
64 struct GNUNET_SERVER_TransmitContext *tc;
67 * Context for hashing of the file.
69 struct GNUNET_CRYPTO_FileHashContext *fhc;
72 * Hash of the contents of the file.
74 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", "INDEXDB", &fn))
121 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
126 wh = GNUNET_BIO_write_open (fn);
129 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
130 _ ("Could not open `%s'.\n"),
135 for (pos = indexed_files_head; NULL != pos; pos = pos->next)
136 if ((GNUNET_OK != GNUNET_BIO_write (wh,
138 sizeof(struct GNUNET_HashCode))) ||
139 (GNUNET_OK != GNUNET_BIO_write_string (wh, pos->filename)))
141 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
143 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
144 _ ("Error writing `%s'.\n"),
154 * Read index information from disk.
159 struct GNUNET_BIO_ReadHandle *rh;
161 struct IndexInfo *pos;
163 struct GNUNET_HashCode hc;
168 GNUNET_CONFIGURATION_get_value_filename (cfg, "FS", "INDEXDB", &fn))
170 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
175 if (GNUNET_NO == GNUNET_DISK_file_test (fn))
177 /* no index info yet */
181 rh = GNUNET_BIO_read_open (fn);
184 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
185 _ ("Could not open `%s'.\n"),
191 (GNUNET_OK == GNUNET_BIO_read (rh,
192 "Hash of indexed file",
194 sizeof(struct GNUNET_HashCode))) &&
196 GNUNET_BIO_read_string (rh, "Name of indexed file", &fname, 1024 * 16)) &&
199 slen = strlen (fname) + 1;
200 pos = GNUNET_malloc (sizeof(struct IndexInfo) + slen);
202 pos->filename = (const char *) &pos[1];
203 GNUNET_memcpy (&pos[1], fname, slen);
204 if (GNUNET_SYSERR == GNUNET_CONTAINER_multihashmap_put (
208 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
214 GNUNET_CONTAINER_DLL_insert (indexed_files_head, indexed_files_tail, pos);
218 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
225 * Continuation called from datastore's remove
229 * @param success did the deletion work?
230 * @param min_expiration minimum expiration time required for content to be stored
231 * @param msg error message
234 remove_cont (void *cls,
236 struct GNUNET_TIME_Absolute min_expiration,
239 if (GNUNET_OK != success)
240 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
241 _ ("Failed to delete bogus block: %s\n"),
247 * We've received an on-demand encoded block from the datastore.
248 * Attempt to do on-demand encoding and (if successful), call the
249 * continuation with the resulting block. On error, clean up and ask
250 * the datastore for more results.
252 * @param key key for the content
253 * @param size number of bytes in data
254 * @param data content stored
255 * @param type type of the content
256 * @param priority priority of the content
257 * @param anonymity anonymity-level for the content
258 * @param replication replication-level for the content
259 * @param expiration expiration time for the content
260 * @param uid unique identifier for the datum;
261 * maybe 0 if no unique identifier is available
262 * @param cont function to call with the actual block (at most once, on success)
263 * @param cont_cls closure for cont
264 * @return GNUNET_OK on success
267 GNUNET_FS_handle_on_demand_block (const struct GNUNET_HashCode *key,
270 enum GNUNET_BLOCK_Type type,
273 uint32_t replication,
274 struct GNUNET_TIME_Absolute expiration,
276 GNUNET_DATASTORE_DatumProcessor cont,
279 const struct OnDemandBlock *odb;
280 struct GNUNET_HashCode nkey;
281 struct GNUNET_CRYPTO_SymmetricSessionKey skey;
282 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
283 struct GNUNET_HashCode query;
285 char ndata[DBLOCK_SIZE];
286 char edata[DBLOCK_SIZE];
288 struct GNUNET_DISK_FileHandle *fh;
290 struct IndexInfo *ii;
292 if (size != sizeof(struct OnDemandBlock))
295 GNUNET_DATASTORE_remove (dsh, key, size, data, -1, -1, &remove_cont, NULL);
296 return GNUNET_SYSERR;
298 odb = (const struct OnDemandBlock *) data;
299 off = GNUNET_ntohll (odb->offset);
300 ii = GNUNET_CONTAINER_multihashmap_get (ifm, &odb->file_id);
304 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
305 "Failed to find index %s\n",
306 GNUNET_h2s (&odb->file_id));
307 return GNUNET_SYSERR;
310 if ((NULL == fn) || (0 != access (fn, R_OK)))
312 GNUNET_STATISTICS_update (
314 gettext_noop ("# index blocks removed: original file inaccessible"),
317 GNUNET_DATASTORE_remove (dsh, key, size, data, -1, -1, &remove_cont, NULL);
318 return GNUNET_SYSERR;
320 if ((NULL == (fh = GNUNET_DISK_file_open (fn,
321 GNUNET_DISK_OPEN_READ,
322 GNUNET_DISK_PERM_NONE))) ||
323 (off != GNUNET_DISK_file_seek (fh, off, GNUNET_DISK_SEEK_SET)) ||
324 (-1 == (nsize = GNUNET_DISK_file_read (fh, ndata, sizeof(ndata)))))
326 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
328 "Could not access indexed file `%s' (%s) at offset %llu: %s\n"),
329 GNUNET_h2s (&odb->file_id),
331 (unsigned long long) off,
332 (fn == NULL) ? _ ("not indexed") : strerror (errno));
334 GNUNET_DISK_file_close (fh);
335 GNUNET_DATASTORE_remove (dsh, key, size, data, -1, -1, &remove_cont, NULL);
336 return GNUNET_SYSERR;
338 GNUNET_DISK_file_close (fh);
339 GNUNET_CRYPTO_hash (ndata, nsize, &nkey);
340 GNUNET_CRYPTO_hash_to_aes_key (&nkey, &skey, &iv);
341 GNUNET_CRYPTO_symmetric_encrypt (ndata, nsize, &skey, &iv, edata);
342 GNUNET_CRYPTO_hash (edata, nsize, &query);
343 if (0 != memcmp (&query, key, sizeof(struct GNUNET_HashCode)))
345 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
346 _ ("Indexed file `%s' changed at offset %llu\n"),
348 (unsigned long long) off);
349 GNUNET_DATASTORE_remove (dsh, key, size, data, -1, -1, &remove_cont, NULL);
350 return GNUNET_SYSERR;
352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
353 "On-demand encoded block for query `%s'\n",
359 GNUNET_BLOCK_TYPE_FS_DBLOCK,
370 * Transmit information about indexed files to @a mq.
372 * @param mq message queue to send information to
375 GNUNET_FS_indexing_send_list (struct GNUNET_MQ_Handle *mq)
377 struct GNUNET_MQ_Envelope *env;
378 struct IndexInfoMessage *iim;
379 struct GNUNET_MessageHeader *iem;
382 struct IndexInfo *pos;
384 for (pos = indexed_files_head; NULL != pos; pos = pos->next)
387 slen = strlen (fn) + 1;
388 if (slen + sizeof(struct IndexInfoMessage) >= GNUNET_MAX_MESSAGE_SIZE)
394 GNUNET_MQ_msg_extra (iim, slen, GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_ENTRY);
396 iim->file_id = pos->file_id;
397 GNUNET_memcpy (&iim[1], fn, slen);
398 GNUNET_MQ_send (mq, env);
400 env = GNUNET_MQ_msg (iem, GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_END);
401 GNUNET_MQ_send (mq, env);
406 * Remove a file from the index.
408 * @param fid identifier of the file to remove
409 * @return #GNUNET_YES if the @a fid was found
412 GNUNET_FS_indexing_do_unindex (const struct GNUNET_HashCode *fid)
414 struct IndexInfo *pos;
416 for (pos = indexed_files_head; NULL != pos; pos = pos->next)
418 if (0 == memcmp (&pos->file_id, fid, sizeof(struct GNUNET_HashCode)))
420 GNUNET_CONTAINER_DLL_remove (indexed_files_head, indexed_files_tail, pos);
423 GNUNET_CONTAINER_multihashmap_remove (ifm, &pos->file_id, pos));
434 * Add the given file to the list of indexed files.
436 * @param filename name of the file
437 * @param file_id hash identifier for @a filename
440 GNUNET_FS_add_to_index (const char *filename,
441 const struct GNUNET_HashCode *file_id)
443 struct IndexInfo *ii;
446 ii = GNUNET_CONTAINER_multihashmap_get (ifm, file_id);
450 GNUNET_ERROR_TYPE_INFO,
452 "Index request received for file `%s' is already indexed as `%s'. Permitting anyway.\n"),
457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
458 "Adding file %s to index as %s\n",
460 GNUNET_h2s (file_id));
461 slen = strlen (filename) + 1;
462 ii = GNUNET_malloc (sizeof(struct IndexInfo) + slen);
463 ii->file_id = *file_id;
464 ii->filename = (const char *) &ii[1];
465 GNUNET_memcpy (&ii[1], filename, slen);
466 GNUNET_CONTAINER_DLL_insert (indexed_files_head, indexed_files_tail, ii);
467 GNUNET_assert (GNUNET_OK ==
468 GNUNET_CONTAINER_multihashmap_put (
472 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
478 * Shutdown the module.
481 GNUNET_FS_indexing_done ()
483 struct IndexInfo *pos;
485 while (NULL != (pos = indexed_files_head))
487 GNUNET_CONTAINER_DLL_remove (indexed_files_head, indexed_files_tail, pos);
488 if (pos->fhc != NULL)
489 GNUNET_CRYPTO_hash_file_cancel (pos->fhc);
492 GNUNET_CONTAINER_multihashmap_remove (ifm, &pos->file_id, pos));
495 GNUNET_CONTAINER_multihashmap_destroy (ifm);
502 * Initialize the indexing submodule.
504 * @param c configuration to use
505 * @param d datastore to use
508 GNUNET_FS_indexing_init (const struct GNUNET_CONFIGURATION_Handle *c,
509 struct GNUNET_DATASTORE_Handle *d)
513 ifm = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_YES);
519 /* end of gnunet-service-fs_indexing.c */