-fix #2598
[oweals/gnunet.git] / src / fs / fs_namespace.c
index 9cb8cd5a45d103cd667efe9255ffe3482a66d287..eaff8a6d2bf82cfd26f1008cf4d1b0d23123f3eb 100644 (file)
 #include "gnunet_fs_service.h"
 #include "fs_api.h"
 
-#define DEBUG_NAMESPACE GNUNET_EXTRA_LOGGING
-
 
 /**
  * Maximum legal size for an sblock.
  */
 #define MAX_SBLOCK_SIZE (60 * 1024)
 
-/**
- * Maximum legal size for an nblock.
- */
-#define MAX_NBLOCK_SIZE (60 * 1024)
-
 
 /**
  * Return the name of the directory in which we store
@@ -60,9 +53,8 @@ get_namespace_directory (struct GNUNET_FS_Handle *h)
       GNUNET_CONFIGURATION_get_value_filename (h->cfg, "FS", "IDENTITY_DIR",
                                                &dn))
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                _("Configuration fails to specify `%s' in section `%s'\n"),
-                "IDENTITY_DIR", "fs");
+    GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
+                              "fs", "IDENTITY_DIR");
     return NULL;
   }
   return dn;
@@ -86,9 +78,8 @@ get_update_information_directory (struct GNUNET_FS_Namespace *ns)
       GNUNET_CONFIGURATION_get_value_filename (ns->h->cfg, "FS", "UPDATE_DIR",
                                                &dn))
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                _("Configuration fails to specify `%s' in section `%s'\n"),
-                "UPDATE_DIR", "fs");
+    GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
+                              "fs", "UPDATE_DIR");
     return NULL;
   }
   GNUNET_asprintf (&ret, "%s%s%s", dn, DIR_SEPARATOR_STR, ns->name);
@@ -113,7 +104,7 @@ write_update_information_graph (struct GNUNET_FS_Namespace *ns)
 
   fn = get_update_information_directory (ns);
   wh = GNUNET_BIO_write_open (fn);
-  if (wh == NULL)
+  if (NULL == wh)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 _("Failed to open `%s' for writing: %s\n"), STRERROR (errno));
@@ -167,7 +158,7 @@ read_update_information_graph (struct GNUNET_FS_Namespace *ns)
     return;
   }
   rh = GNUNET_BIO_read_open (fn);
-  if (rh == NULL)
+  if (NULL == rh)
   {
     GNUNET_free (fn);
     return;
@@ -182,7 +173,7 @@ read_update_information_graph (struct GNUNET_FS_Namespace *ns)
     GNUNET_break (0);
     goto END;
   }
-  if (count == 0)
+  if (0 == count)
   {
     GNUNET_break (GNUNET_OK == GNUNET_BIO_read_close (rh, NULL));
     GNUNET_free (fn);
@@ -233,262 +224,13 @@ END:
 }
 
 
-/**
- * Context for advertising a namespace.
- */
-struct AdvertisementContext
-{
-  /**
-   * Function to call with the result.
-   */
-  GNUNET_FS_PublishContinuation cont;
-
-  /**
-   * Closure for cont.
-   */
-  void *cont_cls;
-
-  /**
-   * Datastore handle.
-   */
-  struct GNUNET_DATASTORE_Handle *dsh;
-
-  /**
-   * Our KSK URI.
-   */
-  struct GNUNET_FS_Uri *ksk_uri;
-
-  /**
-   * Plaintext.
-   */
-  char *pt;
-
-  /**
-   * NBlock to sign and store.
-   */
-  struct NBlock *nb;
-
-  /**
-   * The namespace.
-   */
-  struct GNUNET_FS_Namespace *ns;
-
-  /**
-   * Block options.
-   */
-  struct GNUNET_FS_BlockOptions bo;
-
-  /**
-   * Number of bytes of plaintext.
-   */
-  size_t pt_size;
-
-  /**
-   * Current keyword offset.
-   */
-  unsigned int pos;
-};
-
-
-/**
- * Disconnect from the datastore.
- *
- * @param cls datastore handle
- * @param tc scheduler context
- */
-static void
-do_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
-  struct GNUNET_DATASTORE_Handle *dsh = cls;
-
-  GNUNET_DATASTORE_disconnect (dsh, GNUNET_NO);
-}
-
-
-/**
- * Continuation called to notify client about result of the
- * operation.
- *
- * @param cls closure (our struct AdvertismentContext)
- * @param success GNUNET_SYSERR on failure
- * @param min_expiration minimum expiration time required for content to be stored
- * @param msg NULL on success, otherwise an error message
- */
-static void
-advertisement_cont (void *cls, int success, 
-                   struct GNUNET_TIME_Absolute min_expiration,
-                   const char *msg)
-{
-  struct AdvertisementContext *ac = cls;
-  const char *keyword;
-  GNUNET_HashCode key;
-  GNUNET_HashCode query;
-  struct GNUNET_CRYPTO_AesSessionKey skey;
-  struct GNUNET_CRYPTO_AesInitializationVector iv;
-  struct GNUNET_CRYPTO_RsaPrivateKey *pk;
-
-  if (GNUNET_OK != success)
-  {
-    /* error! */
-    GNUNET_SCHEDULER_add_continuation (&do_disconnect, ac->dsh,
-                                       GNUNET_SCHEDULER_REASON_PREREQ_DONE);
-    if (msg == NULL)
-    {
-      GNUNET_break (0);
-      msg = _("Unknown error");
-    }
-    if (ac->cont != NULL)
-      ac->cont (ac->cont_cls, NULL, msg);
-    GNUNET_FS_uri_destroy (ac->ksk_uri);
-    GNUNET_free (ac->pt);
-    GNUNET_free (ac->nb);
-    GNUNET_FS_namespace_delete (ac->ns, GNUNET_NO);
-    GNUNET_free (ac);
-    return;
-  }
-  if (ac->pos == ac->ksk_uri->data.ksk.keywordCount)
-  {
-    /* done! */
-    GNUNET_SCHEDULER_add_continuation (&do_disconnect, ac->dsh,
-                                       GNUNET_SCHEDULER_REASON_PREREQ_DONE);
-    if (ac->cont != NULL)
-      ac->cont (ac->cont_cls, ac->ksk_uri, NULL);
-    GNUNET_FS_uri_destroy (ac->ksk_uri);
-    GNUNET_free (ac->pt);
-    GNUNET_free (ac->nb);
-    GNUNET_FS_namespace_delete (ac->ns, GNUNET_NO);
-    GNUNET_free (ac);
-    return;
-  }
-  keyword = ac->ksk_uri->data.ksk.keywords[ac->pos++];
-  /* first character of keyword indicates if it is
-   * mandatory or not -- ignore for hashing */
-  GNUNET_CRYPTO_hash (&keyword[1], strlen (&keyword[1]), &key);
-  GNUNET_CRYPTO_hash_to_aes_key (&key, &skey, &iv);
-  GNUNET_CRYPTO_aes_encrypt (ac->pt, ac->pt_size, &skey, &iv, &ac->nb[1]);
-  GNUNET_break (GNUNET_OK ==
-                GNUNET_CRYPTO_rsa_sign (ac->ns->key, &ac->nb->ns_purpose,
-                                        &ac->nb->ns_signature));
-  pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&key);
-  GNUNET_assert (pk != NULL);
-  GNUNET_CRYPTO_rsa_key_get_public (pk, &ac->nb->keyspace);
-  GNUNET_CRYPTO_hash (&ac->nb->keyspace,
-                      sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
-                      &query);
-  GNUNET_break (GNUNET_OK ==
-                GNUNET_CRYPTO_rsa_sign (pk, &ac->nb->ksk_purpose,
-                                        &ac->nb->ksk_signature));
-  GNUNET_CRYPTO_rsa_key_free (pk);
-  GNUNET_DATASTORE_put (ac->dsh, 0 /* no reservation */ ,
-                        &query, ac->pt_size + sizeof (struct NBlock), ac->nb,
-                        GNUNET_BLOCK_TYPE_FS_NBLOCK, ac->bo.content_priority,
-                        ac->bo.anonymity_level, ac->bo.replication_level,
-                        ac->bo.expiration_time, -2, 1,
-                        GNUNET_CONSTANTS_SERVICE_TIMEOUT, &advertisement_cont,
-                        ac);
-}
-
-
-/**
- * Publish an advertismement for a namespace.
- *
- * @param h handle to the file sharing subsystem
- * @param ksk_uri keywords to use for advertisment
- * @param namespace handle for the namespace that should be advertised
- * @param meta meta-data for the namespace advertisement
- * @param bo block options
- * @param rootEntry name of the root of the namespace
- * @param cont continuation
- * @param cont_cls closure for cont
- */
-void
-GNUNET_FS_namespace_advertise (struct GNUNET_FS_Handle *h,
-                               struct GNUNET_FS_Uri *ksk_uri,
-                               struct GNUNET_FS_Namespace *namespace,
-                               const struct GNUNET_CONTAINER_MetaData *meta,
-                               const struct GNUNET_FS_BlockOptions *bo,
-                               const char *rootEntry,
-                               GNUNET_FS_PublishContinuation cont,
-                               void *cont_cls)
-{
-  size_t reslen;
-  size_t size;
-  ssize_t mdsize;
-  struct NBlock *nb;
-  char *mdst;
-  struct GNUNET_DATASTORE_Handle *dsh;
-  struct AdvertisementContext *ctx;
-  char *pt;
-
-  /* create advertisements */
-  mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
-  if (-1 == mdsize)
-  {
-    cont (cont_cls, NULL, _("Failed to serialize meta data"));
-    return;
-  }
-  reslen = strlen (rootEntry) + 1;
-  size = mdsize + sizeof (struct NBlock) + reslen;
-  if (size > MAX_NBLOCK_SIZE)
-  {
-    size = MAX_NBLOCK_SIZE;
-    mdsize = size - sizeof (struct NBlock) - reslen;
-  }
-
-  pt = GNUNET_malloc (mdsize + reslen);
-  memcpy (pt, rootEntry, reslen);
-  mdst = &pt[reslen];
-  mdsize =
-      GNUNET_CONTAINER_meta_data_serialize (meta, &mdst, mdsize,
-                                            GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
-  if (mdsize == -1)
-  {
-    GNUNET_break (0);
-    GNUNET_free (pt);
-    cont (cont_cls, NULL, _("Failed to serialize meta data"));
-    return;
-  }
-  size = mdsize + sizeof (struct NBlock) + reslen;
-  nb = GNUNET_malloc (size);
-  GNUNET_CRYPTO_rsa_key_get_public (namespace->key, &nb->subspace);
-  nb->ns_purpose.size =
-      htonl (mdsize + reslen +
-             sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
-             sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
-  nb->ns_purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK);
-  nb->ksk_purpose.size =
-      htonl (size - sizeof (struct GNUNET_CRYPTO_RsaSignature));
-  nb->ksk_purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK_KSIG);
-  dsh = GNUNET_DATASTORE_connect (h->cfg);
-  if (NULL == dsh)
-  {
-    GNUNET_free (nb);
-    GNUNET_free (pt);
-    cont (cont_cls, NULL, _("Failed to connect to datastore service"));
-    return;
-  }
-  ctx = GNUNET_malloc (sizeof (struct AdvertisementContext));
-  ctx->cont = cont;
-  ctx->cont_cls = cont_cls;
-  ctx->dsh = dsh;
-  ctx->ksk_uri = GNUNET_FS_uri_dup (ksk_uri);
-  ctx->nb = nb;
-  ctx->pt = pt;
-  ctx->pt_size = mdsize + reslen;
-  ctx->ns = namespace;
-  ctx->ns->rc++;
-  ctx->bo = *bo;
-  advertisement_cont (ctx, GNUNET_OK, GNUNET_TIME_UNIT_ZERO_ABS, NULL);
-}
-
-
 /**
  * Create a namespace with the given name; if one already
  * exists, return a handle to the existing namespace.
  *
  * @param h handle to the file sharing subsystem
  * @param name name to use for the namespace
- * @return handle to the namespace, NULL on error
+ * @return handle to the namespace, NULL on error (i.e. invalid filename)
  */
 struct GNUNET_FS_Namespace *
 GNUNET_FS_namespace_create (struct GNUNET_FS_Handle *h, const char *name)
@@ -504,7 +246,7 @@ GNUNET_FS_namespace_create (struct GNUNET_FS_Handle *h, const char *name)
   ret->h = h;
   ret->rc = 1;
   ret->key = GNUNET_CRYPTO_rsa_key_create_from_file (fn);
-  if (ret->key == NULL)
+  if (NULL == ret->key)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 _("Failed to create or read private key for namespace `%s'\n"),
@@ -538,45 +280,44 @@ GNUNET_FS_namespace_dup (struct GNUNET_FS_Namespace *ns)
  * memory) or also to freeze the namespace to prevent further
  * insertions by anyone.
  *
- * @param namespace handle to the namespace that should be deleted / freed
+ * @param ns handle to the namespace that should be deleted / freed
  * @param freeze prevents future insertions; creating a namespace
  *        with the same name again will create a fresh namespace instead
  *
  * @return GNUNET_OK on success, GNUNET_SYSERR on error
  */
 int
-GNUNET_FS_namespace_delete (struct GNUNET_FS_Namespace *namespace, int freeze)
+GNUNET_FS_namespace_delete (struct GNUNET_FS_Namespace *ns, int freeze)
 {
   unsigned int i;
   struct NamespaceUpdateNode *nsn;
 
-  namespace->rc--;
+  ns->rc--;
   if (freeze)
   {
-    if (0 != UNLINK (namespace->filename))
+    if (0 != UNLINK (ns->filename))
       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "unlink",
-                                namespace->filename);
+                                ns->filename);
   }
-  if (0 == namespace->rc)
+  if (0 != ns->rc)
+    return GNUNET_OK;
+  GNUNET_CRYPTO_rsa_key_free (ns->key);
+  GNUNET_free (ns->filename);
+  GNUNET_free (ns->name);
+  for (i = 0; i < ns->update_node_count; i++)
   {
-    GNUNET_CRYPTO_rsa_key_free (namespace->key);
-    GNUNET_free (namespace->filename);
-    GNUNET_free (namespace->name);
-    for (i = 0; i < namespace->update_node_count; i++)
-    {
-      nsn = namespace->update_nodes[i];
-      GNUNET_CONTAINER_meta_data_destroy (nsn->md);
-      GNUNET_FS_uri_destroy (nsn->uri);
-      GNUNET_free (nsn->id);
-      GNUNET_free (nsn->update);
-      GNUNET_free (nsn);
-    }
-    GNUNET_array_grow (namespace->update_nodes, namespace->update_node_count,
-                       0);
-    if (namespace->update_map != NULL)
-      GNUNET_CONTAINER_multihashmap_destroy (namespace->update_map);
-    GNUNET_free (namespace);
-  }
+    nsn = ns->update_nodes[i];
+    GNUNET_CONTAINER_meta_data_destroy (nsn->md);
+    GNUNET_FS_uri_destroy (nsn->uri);
+    GNUNET_free (nsn->id);
+    GNUNET_free (nsn->update);
+    GNUNET_free (nsn);
+  }
+  GNUNET_array_grow (ns->update_nodes, ns->update_node_count,
+                    0);
+  if (ns->update_map != NULL)
+    GNUNET_CONTAINER_multihashmap_destroy (ns->update_map);
+  GNUNET_free (ns);
   return GNUNET_OK;
 }
 
@@ -614,12 +355,12 @@ process_namespace (void *cls, const char *filename)
   struct ProcessNamespaceContext *pnc = cls;
   struct GNUNET_CRYPTO_RsaPrivateKey *key;
   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
-  GNUNET_HashCode id;
+  struct GNUNET_HashCode id;
   const char *name;
   const char *t;
 
   key = GNUNET_CRYPTO_rsa_key_create_from_file (filename);
-  if (key == NULL)
+  if (NULL == key)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 _
@@ -657,7 +398,7 @@ GNUNET_FS_namespace_list (struct GNUNET_FS_Handle *h,
   struct ProcessNamespaceContext ctx;
 
   dn = get_namespace_directory (h);
-  if (dn == NULL)
+  if (NULL == dn)
     return;
   ctx.cb = cb;
   ctx.cb_cls = cb_cls;
@@ -666,12 +407,10 @@ GNUNET_FS_namespace_list (struct GNUNET_FS_Handle *h,
 }
 
 
-
-
 /**
  * Context for the SKS publication.
  */
-struct PublishSksContext
+struct GNUNET_FS_PublishSksContext
 {
 
   /**
@@ -688,7 +427,7 @@ struct PublishSksContext
   /**
    * Namespace we're publishing to.
    */
-  struct GNUNET_FS_Namespace *namespace;
+  struct GNUNET_FS_Namespace *ns;
 
   /**
    * Handle to the datastore.
@@ -705,6 +444,10 @@ struct PublishSksContext
    */
   void *cont_cls;
 
+  /**
+   * Handle for our datastore request.
+   */
+  struct GNUNET_DATASTORE_QueueEntry *dqe;
 };
 
 
@@ -712,7 +455,7 @@ struct PublishSksContext
  * Function called by the datastore API with
  * the result from the PUT (SBlock) request.
  *
- * @param cls closure of type "struct PublishSksContext*"
+ * @param cls closure of type "struct GNUNET_FS_PublishSksContext*"
  * @param success GNUNET_OK on success
  * @param min_expiration minimum expiration time required for content to be stored
  * @param msg error message (or NULL)
@@ -722,54 +465,39 @@ sb_put_cont (void *cls, int success,
             struct GNUNET_TIME_Absolute min_expiration,
             const char *msg)
 {
-  struct PublishSksContext *psc = cls;
-  GNUNET_HashCode hc;
+  struct GNUNET_FS_PublishSksContext *psc = cls;
+  struct GNUNET_HashCode hc;
 
-  if (NULL != psc->dsh)
-  {
-    GNUNET_DATASTORE_disconnect (psc->dsh, GNUNET_NO);
-    psc->dsh = NULL;
-  }
+  psc->dqe = NULL;
   if (GNUNET_OK != success)
   {
-    if (psc->cont != NULL)
+    if (NULL != psc->cont)
       psc->cont (psc->cont_cls, NULL, msg);
+    GNUNET_FS_publish_sks_cancel (psc);
+    return;
   }
-  else
+  if (NULL != psc->nsn)
   {
-    if (psc->nsn != NULL)
+    /* FIXME: this can be done much more
+     * efficiently by simply appending to the
+     * file and overwriting the 4-byte header */
+    if (psc->ns->update_nodes == NULL)
+      read_update_information_graph (psc->ns);
+    GNUNET_array_append (psc->ns->update_nodes,
+                        psc->ns->update_node_count, psc->nsn);
+    if (psc->ns->update_map != NULL)
     {
-      /* FIXME: this can be done much more
-       * efficiently by simply appending to the
-       * file and overwriting the 4-byte header */
-      if (psc->namespace->update_nodes == NULL)
-        read_update_information_graph (psc->namespace);
-      GNUNET_array_append (psc->namespace->update_nodes,
-                           psc->namespace->update_node_count, psc->nsn);
-      if (psc->namespace->update_map != NULL)
-      {
-        GNUNET_CRYPTO_hash (psc->nsn->id, strlen (psc->nsn->id), &hc);
-        GNUNET_CONTAINER_multihashmap_put (psc->namespace->update_map, &hc,
-                                           psc->nsn,
-                                           GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
-      }
-      psc->nsn = NULL;
-      write_update_information_graph (psc->namespace);
+      GNUNET_CRYPTO_hash (psc->nsn->id, strlen (psc->nsn->id), &hc);
+      GNUNET_CONTAINER_multihashmap_put (psc->ns->update_map, &hc,
+                                        psc->nsn,
+                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
     }
-    if (psc->cont != NULL)
-      psc->cont (psc->cont_cls, psc->uri, NULL);
+    psc->nsn = NULL;
+    write_update_information_graph (psc->ns);
   }
-  GNUNET_FS_namespace_delete (psc->namespace, GNUNET_NO);
-  GNUNET_FS_uri_destroy (psc->uri);
-  if (psc->nsn != NULL)
-  {
-    GNUNET_CONTAINER_meta_data_destroy (psc->nsn->md);
-    GNUNET_FS_uri_destroy (psc->nsn->uri);
-    GNUNET_free (psc->nsn->id);
-    GNUNET_free (psc->nsn->update);
-    GNUNET_free (psc->nsn);
-  }
-  GNUNET_free (psc);
+  if (NULL != psc->cont)
+    psc->cont (psc->cont_cls, psc->uri, NULL);
+  GNUNET_FS_publish_sks_cancel (psc);
 }
 
 
@@ -777,7 +505,7 @@ sb_put_cont (void *cls, int success,
  * Publish an SBlock on GNUnet.
  *
  * @param h handle to the file sharing subsystem
- * @param namespace namespace to publish in
+ * @param ns namespace to publish in
  * @param identifier identifier to use
  * @param update update identifier to use
  * @param meta metadata to use
@@ -786,10 +514,11 @@ sb_put_cont (void *cls, int success,
  * @param options publication options
  * @param cont continuation
  * @param cont_cls closure for cont
+ * @return NULL on error ('cont' will still be called)
  */
-void
+struct GNUNET_FS_PublishSksContext *
 GNUNET_FS_publish_sks (struct GNUNET_FS_Handle *h,
-                       struct GNUNET_FS_Namespace *namespace,
+                       struct GNUNET_FS_Namespace *ns,
                        const char *identifier, const char *update,
                        const struct GNUNET_CONTAINER_MetaData *meta,
                        const struct GNUNET_FS_Uri *uri,
@@ -797,7 +526,7 @@ GNUNET_FS_publish_sks (struct GNUNET_FS_Handle *h,
                        enum GNUNET_FS_PublishOptions options,
                        GNUNET_FS_PublishContinuation cont, void *cont_cls)
 {
-  struct PublishSksContext *psc;
+  struct GNUNET_FS_PublishSksContext *psc;
   struct GNUNET_CRYPTO_AesSessionKey sk;
   struct GNUNET_CRYPTO_AesInitializationVector iv;
   struct GNUNET_FS_Uri *sks_uri;
@@ -811,9 +540,9 @@ GNUNET_FS_publish_sks (struct GNUNET_FS_Handle *h,
   struct SBlock *sb_enc;
   char *dest;
   struct GNUNET_CONTAINER_MetaData *mmeta;
-  GNUNET_HashCode key;          /* hash of thisId = key */
-  GNUNET_HashCode id;           /* hash of hc = identifier */
-  GNUNET_HashCode query;        /* id ^ nsid = DB query */
+  struct GNUNET_HashCode key;          /* hash of thisId = key */
+  struct GNUNET_HashCode id;           /* hash of hc = identifier */
+  struct GNUNET_HashCode query;        /* id ^ nsid = DB query */
 
   if (NULL == meta)
     mmeta = GNUNET_CONTAINER_meta_data_create ();
@@ -822,7 +551,7 @@ GNUNET_FS_publish_sks (struct GNUNET_FS_Handle *h,
   uris = GNUNET_FS_uri_to_string (uri);
   slen = strlen (uris) + 1;
   idlen = strlen (identifier);
-  if (update != NULL)
+  if (NULL != update)
     nidlen = strlen (update) + 1;
   else
     nidlen = 1;
@@ -847,25 +576,26 @@ GNUNET_FS_publish_sks (struct GNUNET_FS_Handle *h,
       GNUNET_CONTAINER_meta_data_serialize (mmeta, &dest, mdsize,
                                             GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
   GNUNET_CONTAINER_meta_data_destroy (mmeta);
-  if (mdsize == -1)
+  if (-1 == mdsize)
   {
     GNUNET_break (0);
     GNUNET_free (sb);
-    cont (cont_cls, NULL, _("Internal error."));
-    return;
+    if (NULL != cont)
+      cont (cont_cls, NULL, _("Internal error."));
+    return NULL;
   }
   size = sizeof (struct SBlock) + mdsize + slen + nidlen;
   sb_enc = GNUNET_malloc (size);
   GNUNET_CRYPTO_hash (identifier, idlen, &key);
-  GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &id);
+  GNUNET_CRYPTO_hash (&key, sizeof (struct GNUNET_HashCode), &id);
   sks_uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
   sks_uri->type = sks;
-  GNUNET_CRYPTO_rsa_key_get_public (namespace->key, &sb_enc->subspace);
+  GNUNET_CRYPTO_rsa_key_get_public (ns->key, &sb_enc->subspace);
   GNUNET_CRYPTO_hash (&sb_enc->subspace,
                       sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
-                      &sks_uri->data.sks.namespace);
+                      &sks_uri->data.sks.ns);
   sks_uri->data.sks.identifier = GNUNET_strdup (identifier);
-  GNUNET_CRYPTO_hash_xor (&id, &sks_uri->data.sks.namespace,
+  GNUNET_CRYPTO_hash_xor (&id, &sks_uri->data.sks.ns,
                           &sb_enc->identifier);
   GNUNET_CRYPTO_hash_to_aes_key (&key, &sk, &iv);
   GNUNET_CRYPTO_aes_encrypt (&sb[1], size - sizeof (struct SBlock), &sk, &iv,
@@ -875,20 +605,19 @@ GNUNET_FS_publish_sks (struct GNUNET_FS_Handle *h,
       htonl (slen + mdsize + nidlen + sizeof (struct SBlock) -
              sizeof (struct GNUNET_CRYPTO_RsaSignature));
   GNUNET_assert (GNUNET_OK ==
-                 GNUNET_CRYPTO_rsa_sign (namespace->key, &sb_enc->purpose,
+                 GNUNET_CRYPTO_rsa_sign (ns->key, &sb_enc->purpose,
                                          &sb_enc->signature));
-  psc = GNUNET_malloc (sizeof (struct PublishSksContext));
+  psc = GNUNET_malloc (sizeof (struct GNUNET_FS_PublishSksContext));
   psc->uri = sks_uri;
   psc->cont = cont;
-  psc->namespace = namespace;
-  namespace->rc++;
+  psc->ns = GNUNET_FS_namespace_dup (ns);
   psc->cont_cls = cont_cls;
   if (0 != (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
   {
     GNUNET_free (sb_enc);
     GNUNET_free (sb);
     sb_put_cont (psc, GNUNET_OK, GNUNET_TIME_UNIT_ZERO_ABS, NULL);
-    return;
+    return NULL;
   }
   psc->dsh = GNUNET_DATASTORE_connect (h->cfg);
   if (NULL == psc->dsh)
@@ -896,9 +625,9 @@ GNUNET_FS_publish_sks (struct GNUNET_FS_Handle *h,
     GNUNET_free (sb_enc);
     GNUNET_free (sb);
     sb_put_cont (psc, GNUNET_NO, GNUNET_TIME_UNIT_ZERO_ABS, _("Failed to connect to datastore."));
-    return;
+    return NULL;
   }
-  GNUNET_CRYPTO_hash_xor (&sks_uri->data.sks.namespace, &id, &query);
+  GNUNET_CRYPTO_hash_xor (&sks_uri->data.sks.ns, &id, &query);
   if (NULL != update)
   {
     psc->nsn = GNUNET_malloc (sizeof (struct NamespaceUpdateNode));
@@ -907,13 +636,46 @@ GNUNET_FS_publish_sks (struct GNUNET_FS_Handle *h,
     psc->nsn->md = GNUNET_CONTAINER_meta_data_duplicate (meta);
     psc->nsn->uri = GNUNET_FS_uri_dup (uri);
   }
-  GNUNET_DATASTORE_put (psc->dsh, 0, &sb_enc->identifier, size, sb_enc,
-                        GNUNET_BLOCK_TYPE_FS_SBLOCK, bo->content_priority,
-                        bo->anonymity_level, bo->replication_level,
-                        bo->expiration_time, -2, 1,
-                        GNUNET_CONSTANTS_SERVICE_TIMEOUT, &sb_put_cont, psc);
+  psc->dqe = GNUNET_DATASTORE_put (psc->dsh, 0, &sb_enc->identifier, size, sb_enc,
+                                  GNUNET_BLOCK_TYPE_FS_SBLOCK, bo->content_priority,
+                                  bo->anonymity_level, bo->replication_level,
+                                  bo->expiration_time, -2, 1,
+                                  GNUNET_CONSTANTS_SERVICE_TIMEOUT, &sb_put_cont, psc);
   GNUNET_free (sb);
   GNUNET_free (sb_enc);
+  return psc;
+}
+
+
+/**
+ * Abort the SKS publishing operation.
+ *
+ * @param psc context of the operation to abort.
+ */
+void
+GNUNET_FS_publish_sks_cancel (struct GNUNET_FS_PublishSksContext *psc)
+{
+  if (NULL != psc->dqe)
+  {
+    GNUNET_DATASTORE_cancel (psc->dqe);
+    psc->dqe = NULL;
+  }
+  if (NULL != psc->dsh)
+  {
+    GNUNET_DATASTORE_disconnect (psc->dsh, GNUNET_NO);
+    psc->dsh = NULL;
+  }
+  GNUNET_FS_namespace_delete (psc->ns, GNUNET_NO);
+  GNUNET_FS_uri_destroy (psc->uri);
+  if (NULL != psc->nsn)
+  {
+    GNUNET_CONTAINER_meta_data_destroy (psc->nsn->md);
+    GNUNET_FS_uri_destroy (psc->nsn->uri);
+    GNUNET_free (psc->nsn->id);
+    GNUNET_free (psc->nsn->update);
+    GNUNET_free (psc->nsn);
+  }
+  GNUNET_free (psc);
 }
 
 
@@ -945,7 +707,7 @@ struct ProcessUpdateClosure
  *         GNUNET_NO if not.
  */
 static int
-process_update_node (void *cls, const GNUNET_HashCode * key, void *value)
+process_update_node (void *cls, const struct GNUNET_HashCode * key, void *value)
 {
   struct ProcessUpdateClosure *pc = cls;
   struct NamespaceUpdateNode *nsn = value;
@@ -963,7 +725,7 @@ struct FindTreeClosure
   /**
    * Namespace we are operating on.
    */
-  struct GNUNET_FS_Namespace *namespace;
+  struct GNUNET_FS_Namespace *ns;
 
   /**
    * Array with 'head's of TREEs.
@@ -1008,15 +770,15 @@ struct FindTreeClosure
  *         GNUNET_NO if not.
  */
 static int
-find_trees (void *cls, const GNUNET_HashCode * key, void *value)
+find_trees (void *cls, const struct GNUNET_HashCode * key, void *value)
 {
   struct FindTreeClosure *fc = cls;
   struct NamespaceUpdateNode *nsn = value;
-  GNUNET_HashCode hc;
+  struct GNUNET_HashCode hc;
 
   if (nsn->nug == fc->nug)
   {
-    if (nsn->tree_id == UINT_MAX)
+    if (UINT_MAX == nsn->tree_id)
       return GNUNET_YES;        /* circular */
     GNUNET_assert (nsn->tree_id < fc->tree_array_size);
     if (fc->tree_array[nsn->tree_id] != nsn)
@@ -1026,7 +788,7 @@ find_trees (void *cls, const GNUNET_HashCode * key, void *value)
       return GNUNET_YES;        /* that's our own root (can this be?) */
     /* merge existing TREE, we have a root for both */
     fc->tree_array[nsn->tree_id] = NULL;
-    if (fc->id == UINT_MAX)
+    if (UINT_MAX == fc->id)
       fc->id = nsn->tree_id;    /* take over ID */
   }
   else
@@ -1035,7 +797,7 @@ find_trees (void *cls, const GNUNET_HashCode * key, void *value)
     nsn->tree_id = UINT_MAX;    /* mark as undef */
     /* trace */
     GNUNET_CRYPTO_hash (nsn->update, strlen (nsn->update), &hc);
-    GNUNET_CONTAINER_multihashmap_get_multiple (fc->namespace->update_map, &hc,
+    GNUNET_CONTAINER_multihashmap_get_multiple (fc->ns->update_map, &hc,
                                                 &find_trees, fc);
   }
   return GNUNET_YES;
@@ -1059,76 +821,71 @@ find_trees (void *cls, const GNUNET_HashCode * key, void *value)
  * I know, odd definition of a tree, but the GUI will display an actual
  * tree (GtkTreeView), so that's what counts for the term here.
  *
- * @param namespace namespace to inspect for updateable content
+ * @param ns namespace to inspect for updateable content
  * @param next_id ID to look for; use NULL to look for tree roots
  * @param ip function to call on each updateable identifier
  * @param ip_cls closure for ip
  */
 void
-GNUNET_FS_namespace_list_updateable (struct GNUNET_FS_Namespace *namespace,
+GNUNET_FS_namespace_list_updateable (struct GNUNET_FS_Namespace *ns,
                                      const char *next_id,
                                      GNUNET_FS_IdentifierProcessor ip,
                                      void *ip_cls)
 {
   unsigned int i;
   unsigned int nug;
-  GNUNET_HashCode hc;
+  struct GNUNET_HashCode hc;
   struct NamespaceUpdateNode *nsn;
   struct ProcessUpdateClosure pc;
   struct FindTreeClosure fc;
 
-  if (namespace->update_nodes == NULL)
-    read_update_information_graph (namespace);
-  if (namespace->update_nodes == NULL)
+  if (NULL == ns->update_nodes)
+    read_update_information_graph (ns);
+  if (NULL == ns->update_nodes)
   {
-#if DEBUG_NAMESPACE
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "No updateable nodes found for ID `%s'\n", next_id);
-#endif
     return;                     /* no nodes */
   }
-  if (namespace->update_map == NULL)
+  if (NULL == ns->update_map)
   {
     /* need to construct */
-    namespace->update_map =
+    ns->update_map =
         GNUNET_CONTAINER_multihashmap_create (2 +
-                                              3 * namespace->update_node_count /
-                                              4);
-    for (i = 0; i < namespace->update_node_count; i++)
+                                              3 * ns->update_node_count /
+                                              4,
+                                             GNUNET_NO);
+    for (i = 0; i < ns->update_node_count; i++)
     {
-      nsn = namespace->update_nodes[i];
+      nsn = ns->update_nodes[i];
       GNUNET_CRYPTO_hash (nsn->id, strlen (nsn->id), &hc);
-      GNUNET_CONTAINER_multihashmap_put (namespace->update_map, &hc, nsn,
+      GNUNET_CONTAINER_multihashmap_put (ns->update_map, &hc, nsn,
                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
     }
   }
-  if (next_id != NULL)
+  if (NULL != next_id)
   {
     GNUNET_CRYPTO_hash (next_id, strlen (next_id), &hc);
     pc.ip = ip;
     pc.ip_cls = ip_cls;
-    GNUNET_CONTAINER_multihashmap_get_multiple (namespace->update_map, &hc,
+    GNUNET_CONTAINER_multihashmap_get_multiple (ns->update_map, &hc,
                                                 &process_update_node, &pc);
     return;
   }
-#if DEBUG_NAMESPACE
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Calculating TREEs to find roots of update trees\n");
-#endif
   /* Find heads of TREEs in update graph */
-  nug = ++namespace->nug_gen;
+  nug = ++ns->nug_gen;
   fc.tree_array = NULL;
   fc.tree_array_size = 0;
 
-  for (i = 0; i < namespace->update_node_count; i++)
+  for (i = 0; i < ns->update_node_count; i++)
   {
-    nsn = namespace->update_nodes[i];
+    nsn = ns->update_nodes[i];
     if (nsn->nug == nug)
     {
-#if DEBUG_NAMESPACE
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TREE of node `%s' is %u\n", nsn->id,
                   nsn->nug);
-#endif
       continue;                 /* already placed in TREE */
     }
     GNUNET_CRYPTO_hash (nsn->update, strlen (nsn->update), &hc);
@@ -1136,10 +893,10 @@ GNUNET_FS_namespace_list_updateable (struct GNUNET_FS_Namespace *namespace,
     nsn->tree_id = UINT_MAX;
     fc.id = UINT_MAX;
     fc.nug = nug;
-    fc.namespace = namespace;
-    GNUNET_CONTAINER_multihashmap_get_multiple (namespace->update_map, &hc,
+    fc.ns = ns;
+    GNUNET_CONTAINER_multihashmap_get_multiple (ns->update_map, &hc,
                                                 &find_trees, &fc);
-    if (fc.id == UINT_MAX)
+    if (UINT_MAX == fc.id)
     {
       /* start new TREE */
       for (fc.id = 0; fc.id < fc.tree_array_size; fc.id++)
@@ -1156,17 +913,15 @@ GNUNET_FS_namespace_list_updateable (struct GNUNET_FS_Namespace *namespace,
         GNUNET_array_append (fc.tree_array, fc.tree_array_size, nsn);
         nsn->tree_id = fc.id;
       }
-#if DEBUG_NAMESPACE
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                   "Starting new TREE %u with node `%s'\n", nsn->tree_id,
                   nsn->id);
-#endif
       /* put all nodes with same identifier into this TREE */
       GNUNET_CRYPTO_hash (nsn->id, strlen (nsn->id), &hc);
       fc.id = nsn->tree_id;
       fc.nug = nug;
-      fc.namespace = namespace;
-      GNUNET_CONTAINER_multihashmap_get_multiple (namespace->update_map, &hc,
+      fc.ns = ns;
+      GNUNET_CONTAINER_multihashmap_get_multiple (ns->update_map, &hc,
                                                   &find_trees, &fc);
     }
     else
@@ -1175,28 +930,21 @@ GNUNET_FS_namespace_list_updateable (struct GNUNET_FS_Namespace *namespace,
       fc.tree_array[fc.id] = nsn;
       nsn->tree_id = fc.id;
     }
-#if DEBUG_NAMESPACE
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TREE of node `%s' is %u\n", nsn->id,
                 fc.id);
-#endif
   }
   for (i = 0; i < fc.tree_array_size; i++)
   {
     nsn = fc.tree_array[i];
     if (NULL != nsn)
     {
-#if DEBUG_NAMESPACE
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Root of TREE %u is node `%s'\n", i,
                   nsn->id);
-#endif
-
       ip (ip_cls, nsn->id, nsn->uri, nsn->md, nsn->update);
     }
   }
   GNUNET_array_grow (fc.tree_array, fc.tree_array_size, 0);
-#if DEBUG_NAMESPACE
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Done processing TREEs\n");
-#endif
 }