aboutsummaryrefslogtreecommitdiff
path: root/src/fs/gnunet-service-fs_indexing.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/fs/gnunet-service-fs_indexing.c')
-rw-r--r--src/fs/gnunet-service-fs_indexing.c522
1 files changed, 0 insertions, 522 deletions
diff --git a/src/fs/gnunet-service-fs_indexing.c b/src/fs/gnunet-service-fs_indexing.c
deleted file mode 100644
index f4d560176..000000000
--- a/src/fs/gnunet-service-fs_indexing.c
+++ /dev/null
@@ -1,522 +0,0 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010 GNUnet e.V.
4
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.
9
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.
14
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/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
22 * @file fs/gnunet-service-fs_indexing.c
23 * @brief program that provides indexing functions of the file-sharing service
24 * @author Christian Grothoff
25 */
26#include "platform.h"
27#include <float.h>
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"
36#include "fs.h"
37
38/**
39 * In-memory information about indexed files (also available
40 * on-disk).
41 */
42struct IndexInfo
43{
44 /**
45 * This is a doubly linked list.
46 */
47 struct IndexInfo *next;
48
49 /**
50 * This is a doubly linked list.
51 */
52 struct IndexInfo *prev;
53
54 /**
55 * Name of the indexed file. Memory allocated
56 * at the end of this struct (do not free).
57 */
58 const char *filename;
59
60 /**
61 * Context for transmitting confirmation to client,
62 * NULL if we've done this already.
63 */
64 struct GNUNET_SERVER_TransmitContext *tc;
65
66 /**
67 * Context for hashing of the file.
68 */
69 struct GNUNET_CRYPTO_FileHashContext *fhc;
70
71 /**
72 * Hash of the contents of the file.
73 */
74 struct GNUNET_HashCode file_id;
75};
76
77
78/**
79 * Head of linked list of indexed files.
80 * FIXME: we don't need both a DLL and a hashmap here!
81 */
82static struct IndexInfo *indexed_files_head;
83
84/**
85 * Tail of linked list of indexed files.
86 */
87static struct IndexInfo *indexed_files_tail;
88
89/**
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.
93 */
94static struct GNUNET_CONTAINER_MultiHashMap *ifm;
95
96/**
97 * Our configuration.
98 */
99static const struct GNUNET_CONFIGURATION_Handle *cfg;
100
101/**
102 * Datastore handle. Created and destroyed by code in
103 * gnunet-service-fs (this is an alias).
104 */
105static struct GNUNET_DATASTORE_Handle *dsh;
106
107
108/**
109 * Write the current index information list to disk.
110 */
111static void
112write_index_list ()
113{
114 struct GNUNET_BIO_WriteHandle *wh;
115 char *fn;
116 struct IndexInfo *pos;
117
118 if (GNUNET_OK !=
119 GNUNET_CONFIGURATION_get_value_filename (cfg, "FS", "INDEXDB", &fn))
120 {
121 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
122 "fs",
123 "INDEXDB");
124 return;
125 }
126 wh = GNUNET_BIO_write_open_file (fn);
127 if (NULL == wh)
128 {
129 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
130 _ ("Could not open `%s'.\n"),
131 fn);
132 GNUNET_free (fn);
133 return;
134 }
135 for (pos = indexed_files_head; NULL != pos; pos = pos->next)
136 if ((GNUNET_OK != GNUNET_BIO_write (wh,
137 "fs-indexing-file-id",
138 &pos->file_id,
139 sizeof(struct GNUNET_HashCode))) ||
140 (GNUNET_OK != GNUNET_BIO_write_string (wh,
141 "fs-indexing-filename",
142 pos->filename)))
143 break;
144 if (GNUNET_OK != GNUNET_BIO_write_close (wh, NULL))
145 {
146 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
147 _ ("Error writing `%s'.\n"),
148 fn);
149 GNUNET_free (fn);
150 return;
151 }
152 GNUNET_free (fn);
153}
154
155
156/**
157 * Read index information from disk.
158 */
159static void
160read_index_list ()
161{
162 struct GNUNET_BIO_ReadHandle *rh;
163 char *fn;
164 struct IndexInfo *pos;
165 char *fname;
166 struct GNUNET_HashCode hc;
167 size_t slen;
168 char *emsg;
169
170 if (GNUNET_OK !=
171 GNUNET_CONFIGURATION_get_value_filename (cfg, "FS", "INDEXDB", &fn))
172 {
173 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
174 "fs",
175 "INDEXDB");
176 return;
177 }
178 if (GNUNET_NO == GNUNET_DISK_file_test (fn))
179 {
180 /* no index info yet */
181 GNUNET_free (fn);
182 return;
183 }
184 rh = GNUNET_BIO_read_open_file (fn);
185 if (NULL == rh)
186 {
187 GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
188 _ ("Could not open `%s'.\n"),
189 fn);
190 GNUNET_free (fn);
191 return;
192 }
193 while (
194 (GNUNET_OK == GNUNET_BIO_read (rh,
195 "Hash of indexed file",
196 &hc,
197 sizeof(struct GNUNET_HashCode))) &&
198 (GNUNET_OK ==
199 GNUNET_BIO_read_string (rh, "Name of indexed file", &fname, 1024 * 16)) &&
200 (fname != NULL))
201 {
202 slen = strlen (fname) + 1;
203 pos = GNUNET_malloc (sizeof(struct IndexInfo) + slen);
204 pos->file_id = hc;
205 pos->filename = (const char *) &pos[1];
206 GNUNET_memcpy (&pos[1], fname, slen);
207 if (GNUNET_SYSERR == GNUNET_CONTAINER_multihashmap_put (
208 ifm,
209 &pos->file_id,
210 pos,
211 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
212 {
213 GNUNET_free (pos);
214 }
215 else
216 {
217 GNUNET_CONTAINER_DLL_insert (indexed_files_head, indexed_files_tail, pos);
218 }
219 GNUNET_free (fname);
220 }
221 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
222 GNUNET_free (emsg);
223 GNUNET_free (fn);
224}
225
226
227/**
228 * Continuation called from datastore's remove
229 * function.
230 *
231 * @param cls unused
232 * @param success did the deletion work?
233 * @param min_expiration minimum expiration time required for content to be stored
234 * @param msg error message
235 */
236static void
237remove_cont (void *cls,
238 int success,
239 struct GNUNET_TIME_Absolute min_expiration,
240 const char *msg)
241{
242 if (GNUNET_OK != success)
243 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
244 _ ("Failed to delete bogus block: %s\n"),
245 msg);
246}
247
248
249/**
250 * We've received an on-demand encoded block from the datastore.
251 * Attempt to do on-demand encoding and (if successful), call the
252 * continuation with the resulting block. On error, clean up and ask
253 * the datastore for more results.
254 *
255 * @param key key for the content
256 * @param size number of bytes in data
257 * @param data content stored
258 * @param type type of the content
259 * @param priority priority of the content
260 * @param anonymity anonymity-level for the content
261 * @param replication replication-level for the content
262 * @param expiration expiration time for the content
263 * @param uid unique identifier for the datum;
264 * maybe 0 if no unique identifier is available
265 * @param cont function to call with the actual block (at most once, on success)
266 * @param cont_cls closure for cont
267 * @return GNUNET_OK on success
268 */
269int
270GNUNET_FS_handle_on_demand_block (const struct GNUNET_HashCode *key,
271 uint32_t size,
272 const void *data,
273 enum GNUNET_BLOCK_Type type,
274 uint32_t priority,
275 uint32_t anonymity,
276 uint32_t replication,
277 struct GNUNET_TIME_Absolute expiration,
278 uint64_t uid,
279 GNUNET_DATASTORE_DatumProcessor cont,
280 void *cont_cls)
281{
282 const struct OnDemandBlock *odb;
283 struct GNUNET_HashCode nkey;
284 struct GNUNET_CRYPTO_SymmetricSessionKey skey;
285 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
286 struct GNUNET_HashCode query;
287 ssize_t nsize;
288 char ndata[DBLOCK_SIZE];
289 char edata[DBLOCK_SIZE];
290 const char *fn;
291 struct GNUNET_DISK_FileHandle *fh;
292 uint64_t off;
293 struct IndexInfo *ii;
294
295 if (size != sizeof(struct OnDemandBlock))
296 {
297 GNUNET_break (0);
298 GNUNET_DATASTORE_remove (dsh, key, size, data, -1, -1, &remove_cont, NULL);
299 return GNUNET_SYSERR;
300 }
301 odb = (const struct OnDemandBlock *) data;
302 off = GNUNET_ntohll (odb->offset);
303 ii = GNUNET_CONTAINER_multihashmap_get (ifm, &odb->file_id);
304 if (NULL == ii)
305 {
306 GNUNET_break (0);
307 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
308 "Failed to find index %s\n",
309 GNUNET_h2s (&odb->file_id));
310 return GNUNET_SYSERR;
311 }
312 fn = ii->filename;
313 if ((NULL == fn) || (0 != access (fn, R_OK)))
314 {
315 GNUNET_STATISTICS_update (
316 GSF_stats,
317 gettext_noop ("# index blocks removed: original file inaccessible"),
318 1,
319 GNUNET_YES);
320 GNUNET_DATASTORE_remove (dsh, key, size, data, -1, -1, &remove_cont, NULL);
321 return GNUNET_SYSERR;
322 }
323 if ((NULL == (fh = GNUNET_DISK_file_open (fn,
324 GNUNET_DISK_OPEN_READ,
325 GNUNET_DISK_PERM_NONE))) ||
326 (off != GNUNET_DISK_file_seek (fh, off, GNUNET_DISK_SEEK_SET)) ||
327 (-1 == (nsize = GNUNET_DISK_file_read (fh, ndata, sizeof(ndata)))))
328 {
329 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
330 _ (
331 "Could not access indexed file `%s' (%s) at offset %llu: %s\n"),
332 GNUNET_h2s (&odb->file_id),
333 fn,
334 (unsigned long long) off,
335 (fn == NULL) ? _ ("not indexed") : strerror (errno));
336 if (fh != NULL)
337 GNUNET_DISK_file_close (fh);
338 GNUNET_DATASTORE_remove (dsh, key, size, data, -1, -1, &remove_cont, NULL);
339 return GNUNET_SYSERR;
340 }
341 GNUNET_DISK_file_close (fh);
342 GNUNET_CRYPTO_hash (ndata, nsize, &nkey);
343 GNUNET_CRYPTO_hash_to_aes_key (&nkey, &skey, &iv);
344 GNUNET_CRYPTO_symmetric_encrypt (ndata, nsize, &skey, &iv, edata);
345 GNUNET_CRYPTO_hash (edata, nsize, &query);
346 if (0 != memcmp (&query, key, sizeof(struct GNUNET_HashCode)))
347 {
348 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
349 _ ("Indexed file `%s' changed at offset %llu\n"),
350 fn,
351 (unsigned long long) off);
352 GNUNET_DATASTORE_remove (dsh, key, size, data, -1, -1, &remove_cont, NULL);
353 return GNUNET_SYSERR;
354 }
355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
356 "On-demand encoded block for query `%s'\n",
357 GNUNET_h2s (key));
358 cont (cont_cls,
359 key,
360 nsize,
361 edata,
362 GNUNET_BLOCK_TYPE_FS_DBLOCK,
363 priority,
364 anonymity,
365 replication,
366 expiration,
367 uid);
368 return GNUNET_OK;
369}
370
371
372/**
373 * Transmit information about indexed files to @a mq.
374 *
375 * @param mq message queue to send information to
376 */
377void
378GNUNET_FS_indexing_send_list (struct GNUNET_MQ_Handle *mq)
379{
380 struct GNUNET_MQ_Envelope *env;
381 struct IndexInfoMessage *iim;
382 struct GNUNET_MessageHeader *iem;
383 size_t slen;
384 const char *fn;
385 struct IndexInfo *pos;
386
387 for (pos = indexed_files_head; NULL != pos; pos = pos->next)
388 {
389 fn = pos->filename;
390 slen = strlen (fn) + 1;
391 if (slen + sizeof(struct IndexInfoMessage) >= GNUNET_MAX_MESSAGE_SIZE)
392 {
393 GNUNET_break (0);
394 break;
395 }
396 env =
397 GNUNET_MQ_msg_extra (iim, slen, GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_ENTRY);
398 iim->reserved = 0;
399 iim->file_id = pos->file_id;
400 GNUNET_memcpy (&iim[1], fn, slen);
401 GNUNET_MQ_send (mq, env);
402 }
403 env = GNUNET_MQ_msg (iem, GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_END);
404 GNUNET_MQ_send (mq, env);
405}
406
407
408/**
409 * Remove a file from the index.
410 *
411 * @param fid identifier of the file to remove
412 * @return #GNUNET_YES if the @a fid was found
413 */
414int
415GNUNET_FS_indexing_do_unindex (const struct GNUNET_HashCode *fid)
416{
417 struct IndexInfo *pos;
418
419 for (pos = indexed_files_head; NULL != pos; pos = pos->next)
420 {
421 if (0 == memcmp (&pos->file_id, fid, sizeof(struct GNUNET_HashCode)))
422 {
423 GNUNET_CONTAINER_DLL_remove (indexed_files_head, indexed_files_tail, pos);
424 GNUNET_break (
425 GNUNET_OK ==
426 GNUNET_CONTAINER_multihashmap_remove (ifm, &pos->file_id, pos));
427 GNUNET_free (pos);
428 write_index_list ();
429 return GNUNET_YES;
430 }
431 }
432 return GNUNET_NO;
433}
434
435
436/**
437 * Add the given file to the list of indexed files.
438 *
439 * @param filename name of the file
440 * @param file_id hash identifier for @a filename
441 */
442void
443GNUNET_FS_add_to_index (const char *filename,
444 const struct GNUNET_HashCode *file_id)
445{
446 struct IndexInfo *ii;
447 size_t slen;
448
449 ii = GNUNET_CONTAINER_multihashmap_get (ifm, file_id);
450 if (NULL != ii)
451 {
452 GNUNET_log (
453 GNUNET_ERROR_TYPE_INFO,
454 _ (
455 "Index request received for file `%s' is already indexed as `%s'. Permitting anyway.\n"),
456 filename,
457 ii->filename);
458 return;
459 }
460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
461 "Adding file %s to index as %s\n",
462 filename,
463 GNUNET_h2s (file_id));
464 slen = strlen (filename) + 1;
465 ii = GNUNET_malloc (sizeof(struct IndexInfo) + slen);
466 ii->file_id = *file_id;
467 ii->filename = (const char *) &ii[1];
468 GNUNET_memcpy (&ii[1], filename, slen);
469 GNUNET_CONTAINER_DLL_insert (indexed_files_head, indexed_files_tail, ii);
470 GNUNET_assert (GNUNET_OK ==
471 GNUNET_CONTAINER_multihashmap_put (
472 ifm,
473 &ii->file_id,
474 ii,
475 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
476 write_index_list ();
477}
478
479
480/**
481 * Shutdown the module.
482 */
483void
484GNUNET_FS_indexing_done ()
485{
486 struct IndexInfo *pos;
487
488 while (NULL != (pos = indexed_files_head))
489 {
490 GNUNET_CONTAINER_DLL_remove (indexed_files_head, indexed_files_tail, pos);
491 if (pos->fhc != NULL)
492 GNUNET_CRYPTO_hash_file_cancel (pos->fhc);
493 GNUNET_break (
494 GNUNET_OK ==
495 GNUNET_CONTAINER_multihashmap_remove (ifm, &pos->file_id, pos));
496 GNUNET_free (pos);
497 }
498 GNUNET_CONTAINER_multihashmap_destroy (ifm);
499 ifm = NULL;
500 cfg = NULL;
501}
502
503
504/**
505 * Initialize the indexing submodule.
506 *
507 * @param c configuration to use
508 * @param d datastore to use
509 */
510int
511GNUNET_FS_indexing_init (const struct GNUNET_CONFIGURATION_Handle *c,
512 struct GNUNET_DATASTORE_Handle *d)
513{
514 cfg = c;
515 dsh = d;
516 ifm = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_YES);
517 read_index_list ();
518 return GNUNET_OK;
519}
520
521
522/* end of gnunet-service-fs_indexing.c */