fix
[oweals/gnunet.git] / src / fs / gnunet-service-fs_indexing.c
index 3095092b885b5b696e96638ea6c3c0d3c98bbb29..d6654bcdcf21055e6b44e3f6b182c0e2747df66d 100644 (file)
  * @file fs/gnunet-service-fs_indexing.c
  * @brief program that provides indexing functions of the file-sharing service
  * @author Christian Grothoff
+ *
+ * TODO:
+ * - consider doing GNUNET_CRYPTO_hash_file_cancel on active indexing
+ *   jobs during shutdown (currently, shutdown will only happen after
+ *   all of those are done, not sure if this is good or bad)
  */
 #include "platform.h"
 #include <float.h>
@@ -35,7 +40,7 @@
 #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
@@ -60,6 +65,11 @@ struct IndexInfo
    * NULL if we've done this already.
    */
   struct GNUNET_SERVER_TransmitContext *tc;
+
+  /**
+   * Context for hashing of the file.
+   */
+  struct GNUNET_CRYPTO_FileHashContext *fhc;
   
   /**
    * Hash of the contents of the file.
@@ -91,6 +101,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.
@@ -190,7 +206,6 @@ read_index_list ()
       GNUNET_free (fn);
       return;
     }
-
   while ( (GNUNET_OK ==
           GNUNET_BIO_read (rh,
                            "Hash of indexed file",
@@ -282,7 +297,8 @@ hash_for_index_val (void *cls,
                    res)
 {
   struct IndexInfo *ii = cls;
-  
+
+  ii->fhc = NULL;
   if ( (res == NULL) ||
        (0 != memcmp (res,
                     &ii->file_id,
@@ -342,6 +358,12 @@ GNUNET_FS_handle_index_start (void *cls,
     }
   ism = (const struct IndexStartMessage*) message;
   fn = GNUNET_STRINGS_filename_expand ((const char*) &ism[1]);
+  if (fn == NULL)
+    {
+      GNUNET_SERVER_receive_done (client,
+                                 GNUNET_SYSERR);
+      return;
+    }
   dev = ntohl (ism->device);
   ino = GNUNET_ntohll (ism->inode);
   ism = (const struct IndexStartMessage*) message;
@@ -350,6 +372,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;
@@ -375,12 +404,14 @@ GNUNET_FS_handle_index_start (void *cls,
              (unsigned int) mydev);
 #endif
   /* slow validation, need to hash full file (again) */
-  GNUNET_CRYPTO_hash_file (sched,
-                          GNUNET_SCHEDULER_PRIORITY_IDLE,
-                          fn,
-                          HASHING_BLOCKSIZE,
-                          &hash_for_index_val,
-                          ii);
+  ii->fhc = GNUNET_CRYPTO_hash_file (sched,
+                                    GNUNET_SCHEDULER_PRIORITY_IDLE,
+                                    fn,
+                                    HASHING_BLOCKSIZE,
+                                    &hash_for_index_val,
+                                    ii);
+  if (ii->fhc == NULL)    
+    hash_for_index_val (ii, NULL);    
   GNUNET_free (fn);
 }
 
@@ -452,7 +483,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;
@@ -468,6 +499,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;
        }
@@ -539,7 +574,7 @@ int
 GNUNET_FS_handle_on_demand_block (const GNUNET_HashCode * key,
                                  uint32_t size,
                                  const void *data,
-                                 uint32_t type,
+                                 enum GNUNET_BLOCK_Type type,
                                  uint32_t priority,
                                  uint32_t anonymity,
                                  struct GNUNET_TIME_Absolute
@@ -562,12 +597,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;
@@ -589,18 +626,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);
@@ -624,12 +664,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
@@ -641,7 +683,7 @@ GNUNET_FS_handle_on_demand_block (const GNUNET_HashCode * key,
        key,
        nsize,
        edata,
-       GNUNET_DATASTORE_BLOCKTYPE_DBLOCK,
+       GNUNET_BLOCK_TYPE_DBLOCK,
        priority,
        anonymity,
        expiration,
@@ -679,13 +721,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,