GNUnet  0.11.x
gnunet-service-fs_indexing.c
Go to the documentation of this file.
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 
26 #include "platform.h"
27 #include <float.h>
28 #include "gnunet_core_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"
36 #include "fs.h"
37 
42 struct IndexInfo
43 {
47  struct IndexInfo *next;
48 
52  struct IndexInfo *prev;
53 
58  const char *filename;
59 
64  struct GNUNET_SERVER_TransmitContext *tc;
65 
70 
75 };
76 
77 
83 
88 
95 
99 static const struct GNUNET_CONFIGURATION_Handle *cfg;
100 
106 
107 
111 static void
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  {
122  "fs",
123  "INDEXDB");
124  return;
125  }
126  wh = GNUNET_BIO_write_open (fn);
127  if (NULL == wh)
128  {
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  &pos->file_id,
138  sizeof(struct GNUNET_HashCode))) ||
140  break;
141  if (GNUNET_OK != GNUNET_BIO_write_close (wh))
142  {
144  _ ("Error writing `%s'.\n"),
145  fn);
146  GNUNET_free (fn);
147  return;
148  }
149  GNUNET_free (fn);
150 }
151 
152 
156 static void
158 {
159  struct GNUNET_BIO_ReadHandle *rh;
160  char *fn;
161  struct IndexInfo *pos;
162  char *fname;
163  struct GNUNET_HashCode hc;
164  size_t slen;
165  char *emsg;
166 
167  if (GNUNET_OK !=
168  GNUNET_CONFIGURATION_get_value_filename (cfg, "FS", "INDEXDB", &fn))
169  {
171  "fs",
172  "INDEXDB");
173  return;
174  }
175  if (GNUNET_NO == GNUNET_DISK_file_test (fn))
176  {
177  /* no index info yet */
178  GNUNET_free (fn);
179  return;
180  }
181  rh = GNUNET_BIO_read_open (fn);
182  if (NULL == rh)
183  {
185  _ ("Could not open `%s'.\n"),
186  fn);
187  GNUNET_free (fn);
188  return;
189  }
190  while (
191  (GNUNET_OK == GNUNET_BIO_read (rh,
192  "Hash of indexed file",
193  &hc,
194  sizeof(struct GNUNET_HashCode))) &&
195  (GNUNET_OK ==
196  GNUNET_BIO_read_string (rh, "Name of indexed file", &fname, 1024 * 16)) &&
197  (fname != NULL))
198  {
199  slen = strlen (fname) + 1;
200  pos = GNUNET_malloc (sizeof(struct IndexInfo) + slen);
201  pos->file_id = hc;
202  pos->filename = (const char *) &pos[1];
203  GNUNET_memcpy (&pos[1], fname, slen);
205  ifm,
206  &pos->file_id,
207  pos,
209  {
210  GNUNET_free (pos);
211  }
212  else
213  {
214  GNUNET_CONTAINER_DLL_insert (indexed_files_head, indexed_files_tail, pos);
215  }
216  GNUNET_free (fname);
217  }
218  if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
219  GNUNET_free (emsg);
220  GNUNET_free (fn);
221 }
222 
223 
233 static void
234 remove_cont (void *cls,
235  int success,
237  const char *msg)
238 {
239  if (GNUNET_OK != success)
241  _ ("Failed to delete bogus block: %s\n"),
242  msg);
243 }
244 
245 
266 int
268  uint32_t size,
269  const void *data,
270  enum GNUNET_BLOCK_Type type,
271  uint32_t priority,
272  uint32_t anonymity,
273  uint32_t replication,
275  uint64_t uid,
277  void *cont_cls)
278 {
279  const struct OnDemandBlock *odb;
280  struct GNUNET_HashCode nkey;
283  struct GNUNET_HashCode query;
284  ssize_t nsize;
285  char ndata[DBLOCK_SIZE];
286  char edata[DBLOCK_SIZE];
287  const char *fn;
288  struct GNUNET_DISK_FileHandle *fh;
289  uint64_t off;
290  struct IndexInfo *ii;
291 
292  if (size != sizeof(struct OnDemandBlock))
293  {
294  GNUNET_break (0);
295  GNUNET_DATASTORE_remove (dsh, key, size, data, -1, -1, &remove_cont, NULL);
296  return GNUNET_SYSERR;
297  }
298  odb = (const struct OnDemandBlock *) data;
299  off = GNUNET_ntohll (odb->offset);
300  ii = GNUNET_CONTAINER_multihashmap_get (ifm, &odb->file_id);
301  if (NULL == ii)
302  {
303  GNUNET_break (0);
305  "Failed to find index %s\n",
306  GNUNET_h2s (&odb->file_id));
307  return GNUNET_SYSERR;
308  }
309  fn = ii->filename;
310  if ((NULL == fn) || (0 != access (fn, R_OK)))
311  {
313  GSF_stats,
314  gettext_noop ("# index blocks removed: original file inaccessible"),
315  1,
316  GNUNET_YES);
317  GNUNET_DATASTORE_remove (dsh, key, size, data, -1, -1, &remove_cont, NULL);
318  return GNUNET_SYSERR;
319  }
320  if ((NULL == (fh = GNUNET_DISK_file_open (fn,
323  (off != GNUNET_DISK_file_seek (fh, off, GNUNET_DISK_SEEK_SET)) ||
324  (-1 == (nsize = GNUNET_DISK_file_read (fh, ndata, sizeof(ndata)))))
325  {
327  _ (
328  "Could not access indexed file `%s' (%s) at offset %llu: %s\n"),
329  GNUNET_h2s (&odb->file_id),
330  fn,
331  (unsigned long long) off,
332  (fn == NULL) ? _ ("not indexed") : strerror (errno));
333  if (fh != NULL)
335  GNUNET_DATASTORE_remove (dsh, key, size, data, -1, -1, &remove_cont, NULL);
336  return GNUNET_SYSERR;
337  }
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)))
344  {
346  _ ("Indexed file `%s' changed at offset %llu\n"),
347  fn,
348  (unsigned long long) off);
349  GNUNET_DATASTORE_remove (dsh, key, size, data, -1, -1, &remove_cont, NULL);
350  return GNUNET_SYSERR;
351  }
353  "On-demand encoded block for query `%s'\n",
354  GNUNET_h2s (key));
355  cont (cont_cls,
356  key,
357  nsize,
358  edata,
360  priority,
361  anonymity,
362  replication,
363  expiration,
364  uid);
365  return GNUNET_OK;
366 }
367 
368 
374 void
376 {
377  struct GNUNET_MQ_Envelope *env;
378  struct IndexInfoMessage *iim;
379  struct GNUNET_MessageHeader *iem;
380  size_t slen;
381  const char *fn;
382  struct IndexInfo *pos;
383 
384  for (pos = indexed_files_head; NULL != pos; pos = pos->next)
385  {
386  fn = pos->filename;
387  slen = strlen (fn) + 1;
388  if (slen + sizeof(struct IndexInfoMessage) >= GNUNET_MAX_MESSAGE_SIZE)
389  {
390  GNUNET_break (0);
391  break;
392  }
393  env =
395  iim->reserved = 0;
396  iim->file_id = pos->file_id;
397  GNUNET_memcpy (&iim[1], fn, slen);
398  GNUNET_MQ_send (mq, env);
399  }
401  GNUNET_MQ_send (mq, env);
402 }
403 
404 
411 int
413 {
414  struct IndexInfo *pos;
415 
416  for (pos = indexed_files_head; NULL != pos; pos = pos->next)
417  {
418  if (0 == memcmp (&pos->file_id, fid, sizeof(struct GNUNET_HashCode)))
419  {
420  GNUNET_CONTAINER_DLL_remove (indexed_files_head, indexed_files_tail, pos);
421  GNUNET_break (
422  GNUNET_OK ==
424  GNUNET_free (pos);
425  write_index_list ();
426  return GNUNET_YES;
427  }
428  }
429  return GNUNET_NO;
430 }
431 
432 
439 void
441  const struct GNUNET_HashCode *file_id)
442 {
443  struct IndexInfo *ii;
444  size_t slen;
445 
446  ii = GNUNET_CONTAINER_multihashmap_get (ifm, file_id);
447  if (NULL != ii)
448  {
449  GNUNET_log (
451  _ (
452  "Index request received for file `%s' is already indexed as `%s'. Permitting anyway.\n"),
453  filename,
454  ii->filename);
455  return;
456  }
458  "Adding file %s to index as %s\n",
459  filename,
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);
469  ifm,
470  &ii->file_id,
471  ii,
473  write_index_list ();
474 }
475 
476 
480 void
482 {
483  struct IndexInfo *pos;
484 
485  while (NULL != (pos = indexed_files_head))
486  {
487  GNUNET_CONTAINER_DLL_remove (indexed_files_head, indexed_files_tail, pos);
488  if (pos->fhc != NULL)
490  GNUNET_break (
491  GNUNET_OK ==
493  GNUNET_free (pos);
494  }
496  ifm = NULL;
497  cfg = NULL;
498 }
499 
500 
507 int
509  struct GNUNET_DATASTORE_Handle *d)
510 {
511  cfg = c;
512  dsh = d;
514  read_index_list ();
515  return GNUNET_OK;
516 }
517 
518 
519 /* end of gnunet-service-fs_indexing.c */
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
int GNUNET_DISK_file_test(const char *fil)
Check that fil corresponds to a filename (of a file that exists and that is not a directory)...
Definition: disk.c:544
static struct GNUNET_TIME_Absolute min_expiration
Minimum time that content should have to not be discarded instantly (time stamp of any content that w...
Open the file for reading.
int GNUNET_FS_indexing_do_unindex(const struct GNUNET_HashCode *fid)
Remove a file from the index.
struct IndexInfo * next
This is a doubly linked list.
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
struct GNUNET_HashCode file_id
Hash of the contents of the file.
int GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1345
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
struct GNUNET_SERVER_TransmitContext * tc
Context for transmitting confirmation to client, NULL if we&#39;ve done this already. ...
static void remove_cont(void *cls, int success, struct GNUNET_TIME_Absolute min_expiration, const char *msg)
Continuation called from datastore&#39;s remove function.
GNUNET_BLOCK_Type
Blocks in the datastore and the datacache must have a unique type.
ssize_t GNUNET_DISK_file_read(const struct GNUNET_DISK_FileHandle *h, void *result, size_t len)
Read the contents of a binary file into a buffer.
Definition: disk.c:732
void GNUNET_FS_indexing_done()
Shutdown the module.
static struct GNUNET_CONTAINER_MultiHashMap * ifm
Maps hash over content of indexed files to the respective &#39;struct IndexInfo&#39;.
int GNUNET_BIO_write_string(struct GNUNET_BIO_WriteHandle *h, const char *s)
Write a string to a file.
Definition: bio.c:523
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
const char * filename
Name of the indexed file.
static unsigned int replication
definitions for the entire fs module
Nobody is allowed to do anything to the file.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_MQ_msg(mvar, type)
Allocate a GNUNET_MQ_Envelope.
Definition: gnunet_mq_lib.h:67
struct GNUNET_STATISTICS_Handle * GSF_stats
Handle for reporting statistics.
#define GNUNET_NO
Definition: gnunet_common.h:78
shared data structures of gnunet-service-fs.c
const char * GNUNET_h2s(const struct GNUNET_HashCode *hc)
Convert a hash value to a string (for printing debug messages).
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
Message send by FS service in response to a request asking for a list of all indexed files...
Definition: fs.h:195
static struct IndexInfo * indexed_files_head
Head of linked list of indexed files.
off_t GNUNET_DISK_file_seek(const struct GNUNET_DISK_FileHandle *h, off_t offset, enum GNUNET_DISK_Seek whence)
Move the read/write pointer in a file.
Definition: disk.c:226
void GNUNET_STATISTICS_update(struct GNUNET_STATISTICS_Handle *handle, const char *name, int64_t delta, int make_persistent)
Set statistic value for the peer.
struct GNUNET_HashCode file_id
Hash of the indexed file.
Definition: fs.h:211
Internal representation of the hash map.
int GNUNET_BIO_read_close(struct GNUNET_BIO_ReadHandle *h, char **emsg)
Close an open file.
Definition: bio.c:119
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
int GNUNET_FS_indexing_init(const struct GNUNET_CONFIGURATION_Handle *c, struct GNUNET_DATASTORE_Handle *d)
Initialize the indexing submodule.
void GNUNET_FS_indexing_send_list(struct GNUNET_MQ_Handle *mq)
Transmit information about indexed files to mq.
void * GNUNET_CONTAINER_multihashmap_get(const struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key)
Given a key find a value in the map matching the key.
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
struct GNUNET_BIO_ReadHandle * GNUNET_BIO_read_open(const char *fn)
Open a file for reading.
Definition: bio.c:94
#define GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_ENTRY
Reply to client with an indexed file name.
int GNUNET_BIO_read(struct GNUNET_BIO_ReadHandle *h, const char *what, void *result, size_t len)
Read the contents of a binary file into a buffer.
Definition: bio.c:144
#define GNUNET_MQ_msg_extra(mvar, esize, type)
Allocate an envelope, with extra space allocated after the space needed by the message struct...
Definition: gnunet_mq_lib.h:52
void GNUNET_log_config_missing(enum GNUNET_ErrorType kind, const char *section, const char *option)
Log error message about missing configuration option.
static char * fn
Filename of the unique file.
static void write_index_list()
Write the current index information list to disk.
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:48
ssize_t GNUNET_CRYPTO_symmetric_encrypt(const void *block, size_t size, const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey, const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv, void *result)
Encrypt a block using a symmetric sessionkey.
void GNUNET_CONTAINER_multihashmap_destroy(struct GNUNET_CONTAINER_MultiHashMap *map)
Destroy a hash map.
void(* GNUNET_DATASTORE_DatumProcessor)(void *cls, const struct GNUNET_HashCode *key, size_t size, const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority, uint32_t anonymity, uint32_t replication, struct GNUNET_TIME_Absolute expiration, uint64_t uid)
Process a datum that was stored in the datastore.
int GNUNET_CONTAINER_multihashmap_remove(struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key, const void *value)
Remove the given key-value pair from the map.
static int fh
Handle to the unique file.
Handle for buffered writing.
Definition: bio.c:379
struct GNUNET_DATASTORE_QueueEntry * GNUNET_DATASTORE_remove(struct GNUNET_DATASTORE_Handle *h, const struct GNUNET_HashCode *key, size_t size, const void *data, unsigned int queue_priority, unsigned int max_queue_size, GNUNET_DATASTORE_ContinuationWithStatus cont, void *cont_cls)
Explicitly remove some content from the database.
void GNUNET_CRYPTO_hash_file_cancel(struct GNUNET_CRYPTO_FileHashContext *fhc)
Cancel a file hashing operation.
A 512-bit hashcode.
static char * expiration
Credential TTL.
Definition: gnunet-abd.c:96
In-memory information about indexed files (also available on-disk).
int GNUNET_BIO_read_string(struct GNUNET_BIO_ReadHandle *h, const char *what, char **result, size_t max_length)
Read 0-terminated string from a file.
Definition: bio.c:233
void GNUNET_CRYPTO_hash_to_aes_key(const struct GNUNET_HashCode *hc, struct GNUNET_CRYPTO_SymmetricSessionKey *skey, struct GNUNET_CRYPTO_SymmetricInitializationVector *iv)
Convert a hashcode into a key.
Definition: crypto_hash.c:222
#define DBLOCK_SIZE
Size of the individual blocks used for file-sharing.
Definition: fs.h:40
There must only be one value per key; storing a value should fail if a value under the same key alrea...
struct GNUNET_HashCode key
The key used in the DHT.
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
static unsigned int size
Size of the "table".
Definition: peer.c:67
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
struct GNUNET_HashCode file_id
Hash code of the entire content of the file that was indexed (used to uniquely identify the plaintext...
Definition: block_fs.h:88
indexing for the file-sharing service
static struct GNUNET_TESTBED_BarrierWaitHandle * wh
Our barrier wait handle.
int GNUNET_CONTAINER_multihashmap_put(struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key, void *value, enum GNUNET_CONTAINER_MultiHashMapOption opt)
Store a key-value pair in the map.
Context used when hashing a file.
int GNUNET_BIO_write_close(struct GNUNET_BIO_WriteHandle *h)
Close an open file for writing.
Definition: bio.c:438
#define GNUNET_MAX_MESSAGE_SIZE
Largest supported message (to be precise, one byte more than the largest possible message...
Handle to a message queue.
Definition: mq.c:85
#define GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_END
Reply to client indicating end of list.
static void read_index_list()
Read index information from disk.
Seek an absolute position (from the start of the file).
configuration data
Definition: configuration.c:85
struct GNUNET_BIO_WriteHandle * GNUNET_BIO_write_open(const char *fn)
Open a file for writing.
Definition: bio.c:410
Handle to the datastore service.
static struct GNUNET_DATASTORE_Handle * dsh
Datastore handle.
struct GNUNET_CRYPTO_FileHashContext * fhc
Context for hashing of the file.
struct GNUNET_MQ_Handle * mq
Definition: 003.c:5
#define GNUNET_log(kind,...)
uint64_t offset
At which offset should we be able to find this on-demand encoded block? (in NBO)
Definition: block_fs.h:94
int GNUNET_CONFIGURATION_get_value_filename(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be the name of a file or directory.
Data block (leaf) in the CHK tree.
struct GNUNET_CONTAINER_MultiHashMap * GNUNET_CONTAINER_multihashmap_create(unsigned int len, int do_not_copy_keys)
Create a multi hash map.
struct IndexInfo * prev
This is a doubly linked list.
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
Header for all communications.
Time for absolute times used by GNUnet, in microseconds.
#define GNUNET_YES
Definition: gnunet_common.h:77
static unsigned int anonymity
void GNUNET_MQ_send(struct GNUNET_MQ_Handle *mq, struct GNUNET_MQ_Envelope *ev)
Send a message with the given message queue.
Definition: mq.c:353
int GNUNET_BIO_write(struct GNUNET_BIO_WriteHandle *h, const void *buffer, size_t n)
Write a buffer to a file.
Definition: bio.c:483
static const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
int GNUNET_FS_handle_on_demand_block(const struct GNUNET_HashCode *key, uint32_t size, const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority, uint32_t anonymity, uint32_t replication, struct GNUNET_TIME_Absolute expiration, uint64_t uid, GNUNET_DATASTORE_DatumProcessor cont, void *cont_cls)
We&#39;ve received an on-demand encoded block from the datastore.
uint32_t reserved
Always zero.
Definition: fs.h:206
uint32_t data
The data value.
struct GNUNET_DISK_FileHandle * GNUNET_DISK_file_open(const char *fn, enum GNUNET_DISK_OpenFlags flags, enum GNUNET_DISK_AccessPermissions perm)
Open a file.
Definition: disk.c:1268
Handle used to access files (and pipes).
#define GNUNET_malloc(size)
Wrapper around malloc.
Handle for buffered reading.
Definition: bio.c:53
void GNUNET_FS_add_to_index(const char *filename, const struct GNUNET_HashCode *file_id)
Add the given file to the list of indexed files.
uint64_t GNUNET_ntohll(uint64_t n)
Convert unsigned 64-bit integer to host byte order.
Definition: common_endian.c:48
index block (indexing a DBlock that can be obtained directly from reading the plaintext file) ...
Definition: block_fs.h:81
#define GNUNET_free(ptr)
Wrapper around free.
#define gettext_noop(String)
Definition: gettext.h:69
static struct IndexInfo * indexed_files_tail
Tail of linked list of indexed files.