fixing 1584
[oweals/gnunet.git] / src / fs / gnunet-service-fs_indexing.c
index f8593ffd45e846ed1a9c632b27a56e280a1bc359..833169792f5ef8c0ba7ef2e706384d114bdede66 100644 (file)
@@ -4,7 +4,7 @@
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
-     by the Free Software Foundation; either version 2, or (at your
+     by the Free Software Foundation; either version 3, or (at your
      option) any later version.
 
      GNUnet is distributed in the hope that it will be useful, but
 #include "gnunet_protocols.h"
 #include "gnunet_signatures.h"
 #include "gnunet_util_lib.h"
-#include "gnunet-service-fs_drq.h"
 #include "gnunet-service-fs_indexing.h"
 #include "fs.h"
 
-#define DEBUG_FS GNUNET_YES
+#define DEBUG_FS GNUNET_NO
 
 /**
  * In-memory information about indexed files (also available
@@ -101,6 +100,12 @@ static struct GNUNET_SCHEDULER_Handle *sched;
  */
 static const struct GNUNET_CONFIGURATION_Handle *cfg;
 
+/**
+ * Datastore handle.  Created and destroyed by code in
+ * gnunet-service-fs (this is an alias).
+ */
+static struct GNUNET_DATASTORE_Handle *dsh;
+
 
 /**
  * Write the current index information list to disk.
@@ -200,7 +205,6 @@ read_index_list ()
       GNUNET_free (fn);
       return;
     }
-
   while ( (GNUNET_OK ==
           GNUNET_BIO_read (rh,
                            "Hash of indexed file",
@@ -337,9 +341,9 @@ GNUNET_FS_handle_index_start (void *cls,
   uint16_t msize;
   struct IndexInfo *ii;
   size_t slen;
-  uint32_t dev;
+  uint64_t dev;
   uint64_t ino;
-  uint32_t mydev;
+  uint64_t mydev;
   uint64_t myino;
 
   msize = ntohs(message->size);
@@ -353,7 +357,13 @@ GNUNET_FS_handle_index_start (void *cls,
     }
   ism = (const struct IndexStartMessage*) message;
   fn = GNUNET_STRINGS_filename_expand ((const char*) &ism[1]);
-  dev = ntohl (ism->device);
+  if (fn == NULL)
+    {
+      GNUNET_SERVER_receive_done (client,
+                                 GNUNET_SYSERR);
+      return;
+    }
+  dev = GNUNET_ntohll (ism->device);
   ino = GNUNET_ntohll (ism->inode);
   ism = (const struct IndexStartMessage*) message;
   slen = strlen (fn) + 1;
@@ -361,6 +371,13 @@ GNUNET_FS_handle_index_start (void *cls,
   ii->filename = (const char*) &ii[1];
   memcpy (&ii[1], fn, slen);
   ii->file_id = ism->file_id;  
+#if DEBUG_FS
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Received `%s' message for file `%s'\n",
+             "START_INDEX",
+             ii->filename);
+#endif
+
   ii->tc = GNUNET_SERVER_transmit_context_create (client);
   mydev = 0;
   myino = 0;
@@ -412,7 +429,7 @@ GNUNET_FS_handle_index_list_get (void *cls,
 {
   struct GNUNET_SERVER_TransmitContext *tc;
   struct IndexInfoMessage *iim;
-  char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
+  char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
   size_t slen;
   const char *fn;
   struct IndexInfo *pos;
@@ -424,8 +441,7 @@ GNUNET_FS_handle_index_list_get (void *cls,
     {
       fn = pos->filename;
       slen = strlen (fn) + 1;
-      if (slen + sizeof (struct IndexInfoMessage) > 
-         GNUNET_SERVER_MAX_MESSAGE_SIZE)
+      if (slen + sizeof (struct IndexInfoMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
        {
          GNUNET_break (0);
          break;
@@ -465,7 +481,7 @@ GNUNET_FS_handle_unindex (void *cls,
   struct IndexInfo *next;
   struct GNUNET_SERVER_TransmitContext *tc;
   int found;
-  
+
   um = (const struct UnindexMessage*) message;
   found = GNUNET_NO;
   prev = NULL;
@@ -481,6 +497,10 @@ GNUNET_FS_handle_unindex (void *cls,
            indexed_files = next;
          else
            prev->next = next;
+         GNUNET_break (GNUNET_OK ==
+                       GNUNET_CONTAINER_multihashmap_remove (ifm,
+                                                             &pos->file_id,
+                                                             (void*) pos->filename));
          GNUNET_free (pos);
          found = GNUNET_YES;
        }
@@ -575,12 +595,14 @@ GNUNET_FS_handle_on_demand_block (const GNUNET_HashCode * key,
   if (size != sizeof (struct OnDemandBlock))
     {
       GNUNET_break (0);
-      GNUNET_FS_drq_remove (key,
-                           size,
-                           data,
-                           &remove_cont,
-                           NULL,
-                           GNUNET_TIME_UNIT_FOREVER_REL);
+      GNUNET_DATASTORE_remove (dsh,
+                              key,
+                              size,
+                              data,
+                              -1, -1,
+                              GNUNET_TIME_UNIT_FOREVER_REL,
+                              &remove_cont,
+                              NULL);
       return GNUNET_SYSERR;
     }
   odb = (const struct OnDemandBlock*) data;
@@ -602,18 +624,21 @@ GNUNET_FS_handle_on_demand_block (const GNUNET_HashCode * key,
                                        sizeof (ndata)))) )
     {
       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                 _("Could not access indexed file `%s' at offset %llu: %s\n"),
+                 _("Could not access indexed file `%s' (%s) at offset %llu: %s\n"),
                  GNUNET_h2s (&odb->file_id),
+                 fn,
                  (unsigned long long) off,
-                 STRERROR (errno));
+                 (fn == NULL) ? _("not indexed") : STRERROR (errno));
       if (fh != NULL)
        GNUNET_DISK_file_close (fh);
-      GNUNET_FS_drq_remove (key,
-                           size,
-                           data,
-                           &remove_cont,
-                           NULL,
-                           GNUNET_TIME_UNIT_FOREVER_REL);
+      GNUNET_DATASTORE_remove (dsh,
+                              key,
+                              size,
+                              data,
+                              -1, -1,
+                              GNUNET_TIME_UNIT_FOREVER_REL,
+                              &remove_cont,
+                              NULL);
       return GNUNET_SYSERR;
     }
   GNUNET_DISK_file_close (fh);
@@ -637,12 +662,14 @@ GNUNET_FS_handle_on_demand_block (const GNUNET_HashCode * key,
                  _("Indexed file `%s' changed at offset %llu\n"),
                  fn,
                  (unsigned long long) off);
-      GNUNET_FS_drq_remove (key,
-                           size,
-                           data,
-                           &remove_cont,
-                           NULL,
-                           GNUNET_TIME_UNIT_FOREVER_REL);
+      GNUNET_DATASTORE_remove (dsh,
+                              key,
+                              size,
+                              data,
+                              -1, -1,
+                              GNUNET_TIME_UNIT_FOREVER_REL,
+                              &remove_cont,
+                              NULL);
       return GNUNET_SYSERR;
     }
 #if DEBUG_FS
@@ -692,13 +719,16 @@ shutdown_task (void *cls,
  *
  * @param s scheduler to use
  * @param c configuration to use
+ * @param d datastore to use
  */
 int
 GNUNET_FS_indexing_init (struct GNUNET_SCHEDULER_Handle *s,
-                        const struct GNUNET_CONFIGURATION_Handle *c)
+                        const struct GNUNET_CONFIGURATION_Handle *c,
+                        struct GNUNET_DATASTORE_Handle *d)
 {
   sched = s;
   cfg = c;
+  dsh = d;
   ifm = GNUNET_CONTAINER_multihashmap_create (128);
   GNUNET_SCHEDULER_add_delayed (sched,
                                GNUNET_TIME_UNIT_FOREVER_REL,