make clang static analysis happy
[oweals/gnunet.git] / src / fs / gnunet-service-fs_indexing.c
index ebd7114d30a51782c7d82eb83f084dab9b3b9f3c..4041997423d89df7f1b0efd4c1554ca0337aff30 100644 (file)
@@ -22,9 +22,6 @@
  * @file fs/gnunet-service-fs_indexing.c
  * @brief program that provides indexing functions of the file-sharing service
  * @author Christian Grothoff
- *
- * TODO:
- * - indexed files/blocks not removed on errors
  */
 #include "platform.h"
 #include <float.h>
 #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_NO
-
+#define DEBUG_FS GNUNET_YES
 
 /**
  * In-memory information about indexed files (also available
@@ -92,7 +89,7 @@ static struct GNUNET_SCHEDULER_Handle *sched;
 /**
  * Our configuration.
  */
-const struct GNUNET_CONFIGURATION_Handle *cfg;
+static const struct GNUNET_CONFIGURATION_Handle *cfg;
 
 
 /**
@@ -180,7 +177,7 @@ read_index_list ()
     }
   if (GNUNET_NO == GNUNET_DISK_file_test (fn))
     {
-      /* no index info  yet */
+      /* no index info yet */
       GNUNET_free (fn);
       return;
     }
@@ -248,7 +245,7 @@ signal_index_ok (struct IndexInfo *ii)
                                         GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
     {
       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                 _("Index request received for file `%s' is indexed as `%s'.  Permitting anyway.\n"),
+                 _("Index request received for file `%s' is already indexed as `%s'.  Permitting anyway.\n"),
                  ii->filename,
                  (const char*) GNUNET_CONTAINER_multihashmap_get (ifm,
                                                                   &ii->file_id));
@@ -325,7 +322,7 @@ GNUNET_FS_handle_index_start (void *cls,
                              const struct GNUNET_MessageHeader *message)
 {
   const struct IndexStartMessage *ism;
-  const char *fn;
+  char *fn;
   uint16_t msize;
   struct IndexInfo *ii;
   size_t slen;
@@ -344,7 +341,7 @@ GNUNET_FS_handle_index_start (void *cls,
       return;
     }
   ism = (const struct IndexStartMessage*) message;
-  fn = (const char*) &ism[1];
+  fn = GNUNET_STRINGS_filename_expand ((const char*) &ism[1]);
   dev = ntohl (ism->device);
   ino = GNUNET_ntohll (ism->inode);
   ism = (const struct IndexStartMessage*) message;
@@ -354,6 +351,8 @@ GNUNET_FS_handle_index_start (void *cls,
   memcpy (&ii[1], fn, slen);
   ii->file_id = ism->file_id;  
   ii->tc = GNUNET_SERVER_transmit_context_create (client);
+  mydev = 0;
+  myino = 0;
   if ( ( (dev != 0) ||
         (ino != 0) ) &&
        (GNUNET_OK == GNUNET_DISK_file_get_identifiers (fn,
@@ -364,6 +363,7 @@ GNUNET_FS_handle_index_start (void *cls,
     {      
       /* fast validation OK! */
       signal_index_ok (ii);
+      GNUNET_free (fn);
       return;
     }
 #if DEBUG_FS
@@ -381,6 +381,7 @@ GNUNET_FS_handle_index_start (void *cls,
                           HASHING_BLOCKSIZE,
                           &hash_for_index_val,
                           ii);
+  GNUNET_free (fn);
 }
 
 
@@ -508,13 +509,10 @@ remove_cont (void *cls,
             int success,
             const char *msg)
 {
-  struct GNUNET_DATASTORE_Handle *dsh = cls;
-
   if (GNUNET_OK != success)
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                _("Failed to delete bogus block: %s\n"),
                msg);
-  GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
 }
 
 
@@ -533,10 +531,11 @@ remove_cont (void *cls,
  * @param expiration expiration time for the content
  * @param uid unique identifier for the datum;
  *        maybe 0 if no unique identifier is available
- * @param cont function to call with the actual block
+ * @param cont function to call with the actual block (at most once, on success)
  * @param cont_cls closure for cont
+ * @return GNUNET_OK on success
  */
-void
+int
 GNUNET_FS_handle_on_demand_block (const GNUNET_HashCode * key,
                                  uint32_t size,
                                  const void *data,
@@ -545,7 +544,6 @@ GNUNET_FS_handle_on_demand_block (const GNUNET_HashCode * key,
                                  uint32_t anonymity,
                                  struct GNUNET_TIME_Absolute
                                  expiration, uint64_t uid,
-                                 struct GNUNET_DATASTORE_Handle *dsh,
                                  GNUNET_DATASTORE_Iterator cont,
                                  void *cont_cls)
 {
@@ -564,14 +562,13 @@ GNUNET_FS_handle_on_demand_block (const GNUNET_HashCode * key,
   if (size != sizeof (struct OnDemandBlock))
     {
       GNUNET_break (0);
-      GNUNET_DATASTORE_remove (dsh, 
-                              key,
-                              size,
-                              data,
-                              &remove_cont,
-                              dsh,
-                              GNUNET_TIME_UNIT_FOREVER_REL);     
-      return;
+      GNUNET_FS_drq_remove (key,
+                           size,
+                           data,
+                           &remove_cont,
+                           NULL,
+                           GNUNET_TIME_UNIT_FOREVER_REL);
+      return GNUNET_SYSERR;
     }
   odb = (const struct OnDemandBlock*) data;
   off = GNUNET_ntohll (odb->offset);
@@ -598,10 +595,13 @@ GNUNET_FS_handle_on_demand_block (const GNUNET_HashCode * key,
                  STRERROR (errno));
       if (fh != NULL)
        GNUNET_DISK_file_close (fh);
-      /* FIXME: if this happens often, we need
-        to remove the OnDemand block from the DS! */
-      GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);       
-      return;
+      GNUNET_FS_drq_remove (key,
+                           size,
+                           data,
+                           &remove_cont,
+                           NULL,
+                           GNUNET_TIME_UNIT_FOREVER_REL);
+      return GNUNET_SYSERR;
     }
   GNUNET_DISK_file_close (fh);
   GNUNET_CRYPTO_hash (ndata,
@@ -624,11 +624,19 @@ GNUNET_FS_handle_on_demand_block (const GNUNET_HashCode * key,
                  _("Indexed file `%s' changed at offset %llu\n"),
                  fn,
                  (unsigned long long) off);
-      /* FIXME: if this happens often, we need
-        to remove the OnDemand block from the DS! */
-      GNUNET_DATASTORE_get_next (dsh, GNUNET_YES);
-      return;
+      GNUNET_FS_drq_remove (key,
+                           size,
+                           data,
+                           &remove_cont,
+                           NULL,
+                           GNUNET_TIME_UNIT_FOREVER_REL);
+      return GNUNET_SYSERR;
     }
+#if DEBUG_FS
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                 "On-demand encoded block for query `%s'\n",
+                 GNUNET_h2s (key));
+#endif  
   cont (cont_cls,
        key,
        nsize,
@@ -638,6 +646,7 @@ GNUNET_FS_handle_on_demand_block (const GNUNET_HashCode * key,
        anonymity,
        expiration,
        uid);
+  return GNUNET_OK;
 }
 
 
@@ -671,8 +680,8 @@ shutdown_task (void *cls,
  * @param s scheduler to use
  * @param c configuration to use
  */
-void
-GNUNET_FS_init_indexing (struct GNUNET_SCHEDULER_Handle *s,
+int
+GNUNET_FS_indexing_init (struct GNUNET_SCHEDULER_Handle *s,
                         const struct GNUNET_CONFIGURATION_Handle *c)
 {
   sched = s;
@@ -683,6 +692,7 @@ GNUNET_FS_init_indexing (struct GNUNET_SCHEDULER_Handle *s,
                                &shutdown_task,
                                NULL);
   read_index_list ();
+  return GNUNET_OK;
 }
 
 /* end of gnunet-service-fs_indexing.c */