-towards block plugin for mesh
[oweals/gnunet.git] / src / fs / fs_unindex.c
index 07b9cccd3f688c3ad95b6398a7987bf098bbea6f..71a1b9e0ba78c4195904693d9c3a113801e19091 100644 (file)
@@ -223,7 +223,7 @@ process_fs_response (void *cls, const struct GNUNET_MessageHeader *msg)
 
   if (uc->client != NULL)
   {
-    GNUNET_CLIENT_disconnect (uc->client, GNUNET_NO);
+    GNUNET_CLIENT_disconnect (uc->client);
     uc->client = NULL;
   }
   if (uc->state != UNINDEX_STATE_FS_NOTIFY)
@@ -341,7 +341,7 @@ unindex_directory_scan_cb (void *cls,
     {
       uc->ksk_uri = GNUNET_FS_uri_dup (directory_scan_result->ksk_uri);
       uc->state = UNINDEX_STATE_DS_REMOVE_KBLOCKS;
-      uc->emsg = GNUNET_strdup (_("Failed to connect to `datastore' service."));
+      uc->emsg = GNUNET_strdup (_("Failed to get KSKs from directory scan."));
       GNUNET_FS_unindex_sync_ (uc);
       GNUNET_FS_unindex_do_remove_kblocks_ (uc);
     }
@@ -405,6 +405,7 @@ continue_after_remove (void *cls,
 {
   struct GNUNET_FS_UnindexContext *uc = cls;
 
+  uc->dqe = NULL;
   if (success != GNUNET_YES)  
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                _("Failed to remove KBlock: %s\n"),
@@ -436,7 +437,7 @@ continue_after_remove (void *cls,
  */
 static void
 process_kblock_for_unindex (void *cls,
-                           const GNUNET_HashCode * key,
+                           const struct GNUNET_HashCode * key,
                            size_t size, const void *data,
                            enum GNUNET_BLOCK_Type type,
                            uint32_t priority,
@@ -446,7 +447,6 @@ process_kblock_for_unindex (void *cls,
 {
   struct GNUNET_FS_UnindexContext *uc = cls;
   const struct KBlock *kb;
-  const char *uris;
   struct GNUNET_FS_Uri *chk_uri;
 
   uc->dqe = NULL;
@@ -476,17 +476,33 @@ process_kblock_for_unindex (void *cls,
     goto get_next;
   }
   kb = data;
-  uris = (const char*) &kb[1];
-  if (NULL == memchr (uris, 0, size - sizeof (struct KBlock)))
-  {
-    GNUNET_break (0);
-    goto get_next;
-  }
-  chk_uri = GNUNET_FS_uri_parse (uris, NULL);
-  if (NULL == chk_uri)
   {
-    GNUNET_break (0);
-    goto get_next;
+    char pt[size - sizeof (struct KBlock)];  
+    struct GNUNET_CRYPTO_AesSessionKey skey;
+    struct GNUNET_CRYPTO_AesInitializationVector iv;
+    GNUNET_CRYPTO_hash_to_aes_key (&uc->key, &skey, &iv);
+    if (-1 ==
+       GNUNET_CRYPTO_aes_decrypt (&kb[1], size - sizeof (struct KBlock), &skey,
+                                  &iv, pt))
+    {
+      GNUNET_break (0);
+      goto get_next;
+    }       
+    if (NULL == memchr (pt, 0, sizeof (pt)))
+    {
+      GNUNET_break (0);
+      goto get_next;
+    }
+    chk_uri = GNUNET_FS_uri_parse (pt, NULL);
+    if (NULL == chk_uri)
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                 _("Failed to parse URI `%s' from KBlock!\n"),
+                 pt);
+      GNUNET_break (0);
+      goto get_next;
+    }
   }
   if (0 != memcmp (&uc->chk,
                   &chk_uri->data.chk.chk,
@@ -496,6 +512,7 @@ process_kblock_for_unindex (void *cls,
     GNUNET_FS_uri_destroy (chk_uri);
     goto get_next;
   }
+  GNUNET_FS_uri_destroy (chk_uri);
   /* matches! */
   uc->dqe = GNUNET_DATASTORE_remove (uc->dsh,
                                     key, size, data,
@@ -507,7 +524,7 @@ process_kblock_for_unindex (void *cls,
  get_next:
   uc->dqe = GNUNET_DATASTORE_get_key (uc->dsh,
                                      uc->roff++,
-                                     &uc->key,
+                                     &uc->query,
                                      GNUNET_BLOCK_TYPE_FS_KBLOCK,
                                      0 /* priority */, 1 /* queue size */,
                                      GNUNET_TIME_UNIT_FOREVER_REL,
@@ -525,11 +542,10 @@ void
 GNUNET_FS_unindex_do_remove_kblocks_ (struct GNUNET_FS_UnindexContext *uc)
 {
   const char *keyword;
-  GNUNET_HashCode hc;
   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
   struct GNUNET_CRYPTO_RsaPrivateKey *pk;
 
-  if (NULL != uc->dsh)
+  if (NULL == uc->dsh)
     uc->dsh = GNUNET_DATASTORE_connect (uc->h->cfg);
   if (NULL == uc->dsh)
   {
@@ -547,19 +563,19 @@ GNUNET_FS_unindex_do_remove_kblocks_ (struct GNUNET_FS_UnindexContext *uc)
   }
   /* FIXME: code duplication with fs_search.c here... */
   keyword = &uc->ksk_uri->data.ksk.keywords[uc->ksk_offset][1];
-  GNUNET_CRYPTO_hash (keyword, strlen (keyword), &hc);
-  pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&hc);
+  GNUNET_CRYPTO_hash (keyword, strlen (keyword), &uc->key);
+  pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&uc->key);
   GNUNET_assert (pk != NULL);
   GNUNET_CRYPTO_rsa_key_get_public (pk, &pub);
   GNUNET_CRYPTO_rsa_key_free (pk);
   GNUNET_CRYPTO_hash (&pub,
                      sizeof (struct
                              GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
-                     &uc->key);
+                     &uc->query);
   uc->first_uid = 0;
   uc->dqe = GNUNET_DATASTORE_get_key (uc->dsh,
                                      uc->roff++,
-                                     &uc->key,
+                                     &uc->query,
                                      GNUNET_BLOCK_TYPE_FS_KBLOCK,
                                      0 /* priority */, 1 /* queue size */,
                                      GNUNET_TIME_UNIT_FOREVER_REL,
@@ -594,7 +610,8 @@ unindex_extract_keywords (void *cls, const struct GNUNET_SCHEDULER_TaskContext *
 void
 GNUNET_FS_unindex_do_remove_ (struct GNUNET_FS_UnindexContext *uc)
 {
-  uc->dsh = GNUNET_DATASTORE_connect (uc->h->cfg);
+  if (NULL == uc->dsh)
+    uc->dsh = GNUNET_DATASTORE_connect (uc->h->cfg);
   if (NULL == uc->dsh)
   {
     uc->state = UNINDEX_STATE_ERROR;
@@ -632,7 +649,7 @@ GNUNET_FS_unindex_do_remove_ (struct GNUNET_FS_UnindexContext *uc)
  * @param file_id computed hash, NULL on error
  */
 void
-GNUNET_FS_unindex_process_hash_ (void *cls, const GNUNET_HashCode * file_id)
+GNUNET_FS_unindex_process_hash_ (void *cls, const struct GNUNET_HashCode * file_id)
 {
   struct GNUNET_FS_UnindexContext *uc = cls;
 
@@ -692,7 +709,7 @@ GNUNET_FS_unindex_signal_suspend_ (void *cls)
   }
   if (uc->client != NULL)
   {
-    GNUNET_CLIENT_disconnect (uc->client, GNUNET_NO);
+    GNUNET_CLIENT_disconnect (uc->client);
     uc->client = NULL;
   }
   if (NULL != uc->dsh)
@@ -739,7 +756,7 @@ GNUNET_FS_unindex_start (struct GNUNET_FS_Handle *h, const char *filename,
   struct GNUNET_FS_ProgressInfo pi;
   uint64_t size;
 
-  if (GNUNET_OK != GNUNET_DISK_file_size (filename, &size, GNUNET_YES))
+  if (GNUNET_OK != GNUNET_DISK_file_size (filename, &size, GNUNET_YES, GNUNET_YES))
     return NULL;
   ret = GNUNET_malloc (sizeof (struct GNUNET_FS_UnindexContext));
   ret->h = h;
@@ -787,7 +804,7 @@ GNUNET_FS_unindex_stop (struct GNUNET_FS_UnindexContext *uc)
   }
   if (uc->client != NULL)
   {
-    GNUNET_CLIENT_disconnect (uc->client, GNUNET_NO);
+    GNUNET_CLIENT_disconnect (uc->client);
     uc->client = NULL;
   }
   if (NULL != uc->dsh)
@@ -824,6 +841,7 @@ GNUNET_FS_unindex_stop (struct GNUNET_FS_UnindexContext *uc)
                                   (uc->state ==
                                    UNINDEX_STATE_COMPLETE) ? uc->file_size : 0);
   GNUNET_break (NULL == uc->client_info);
+  GNUNET_free_non_null (uc->emsg);
   GNUNET_free (uc->filename);
   GNUNET_free (uc);
 }