-}
-
-
-/**
- * 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 (i.e. invalid filename)
- */
-struct GNUNET_FS_Namespace *
-GNUNET_FS_namespace_create (struct GNUNET_FS_Handle *h, const char *name)
-{
- char *dn;
- char *fn;
- struct GNUNET_FS_Namespace *ret;
-
- dn = get_namespace_directory (h);
- if (NULL == dn)
- {
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- _("Can't determine where namespace directory is\n"));
- return NULL;
- }
- GNUNET_asprintf (&fn, "%s%s%s", dn, DIR_SEPARATOR_STR, name);
- GNUNET_free (dn);
- ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Namespace));
- ret->h = h;
- ret->rc = 1;
- ret->key = GNUNET_CRYPTO_rsa_key_create_from_file (fn);
- if (NULL == ret->key)
- {
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- _("Failed to create or read private key for namespace `%s'\n"),
- name);
- GNUNET_free (ret);
- GNUNET_free (fn);
- return NULL;
- }
- ret->name = GNUNET_strdup (name);
- ret->filename = fn;
- return ret;
-}
-
-
-/**
- * Function called upon completion of 'GNUNET_CRYPTO_rsa_key_create_start'.
- *
- * @param cls closure
- * @param pk NULL on error, otherwise the private key (which must be free'd by the callee)
- * @param emsg NULL on success, otherwise an error message
- */
-static void
-ns_key_created (void *cls, struct GNUNET_CRYPTO_RsaPrivateKey *pk,
- const char *emsg)
-{
- struct GNUNET_FS_NamespaceCreationContext *ncc = cls;
-
- ncc->keycreator = NULL;
-
- if (pk)
- {
- struct GNUNET_FS_Namespace *ret;
- ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Namespace));
- ret->rc = 1;
- ret->key = pk;
- ret->h = ncc->h;
- ret->name = ncc->name;
- ret->filename = ncc->filename;
- ncc->cont (ncc->cont_cls, ret, NULL);
- }
- else
- {
- GNUNET_free (ncc->filename);
- GNUNET_free (ncc->name);
- ncc->cont (ncc->cont_cls, NULL, emsg);
- }
- GNUNET_free (ncc);
-}
-
-
-/**
- * Create a namespace with the given name.
- * If one already exists, the continuation will be called with a handle to
- * the existing namespace.
- * Otherwise creates a new namespace.
- *
- * @param h handle to the file sharing subsystem
- * @param name name to use for the namespace
- * @return namespace creation context, NULL on error (i.e. invalid filename)
- */
-struct GNUNET_FS_NamespaceCreationContext *
-GNUNET_FS_namespace_create_start (struct GNUNET_FS_Handle *h, const char *name,
- GNUNET_FS_NamespaceCreationCallback cont, void *cont_cls)
-{
- char *dn;
- char *fn;
- struct GNUNET_FS_NamespaceCreationContext *ret;
-
- dn = get_namespace_directory (h);
- if (NULL == dn)
- {
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- _("Can't determine where namespace directory is\n"));
- return NULL;
- }
- GNUNET_asprintf (&fn, "%s%s%s", dn, DIR_SEPARATOR_STR, name);
- GNUNET_free (dn);
-
- ret = GNUNET_malloc (sizeof (struct GNUNET_FS_NamespaceCreationContext));
- ret->filename = fn;
- ret->h = h;
- ret->name = GNUNET_strdup (name);
- ret->cont = cont;
- ret->cont_cls = cont_cls;
-
- ret->keycreator = GNUNET_CRYPTO_rsa_key_create_start (fn,
- ns_key_created, ret);
-
- if (NULL == ret->keycreator)
- {
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- _("Failed to start creating or reading private key for namespace `%s'\n"),
- name);
- GNUNET_free (fn);
- GNUNET_free (ret->name);
- GNUNET_free (ret);
- return NULL;
- }
- return ret;
-}
-
-
-/**
- * Abort namespace creation.
- *
- * @param ncc namespace creation context to abort
- */
-void
-GNUNET_FS_namespace_create_stop (struct GNUNET_FS_NamespaceCreationContext *ncc)
-{
- if (NULL != ncc->keycreator)
- {
- GNUNET_CRYPTO_rsa_key_create_stop (ncc->keycreator);
- ncc->keycreator = NULL;
- }
- if (NULL != ncc->filename)
- {
- if (0 != UNLINK (ncc->filename))
- GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", ncc->filename);
- GNUNET_free (ncc->filename);
- }
- GNUNET_free_non_null (ncc->name);
- GNUNET_free (ncc);
-}
-
-
-/**
- * Duplicate a namespace handle.
- *
- * @param ns namespace handle
- * @return duplicated handle to the namespace
- */
-struct GNUNET_FS_Namespace *
-GNUNET_FS_namespace_dup (struct GNUNET_FS_Namespace *ns)
-{
- ns->rc++;
- return ns;
-}
-
-
-/**
- * Delete a namespace handle. Can be used for a clean shutdown (free
- * memory) or also to freeze the namespace to prevent further
- * insertions by anyone.
- *
- * @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 *ns, int freeze)
-{
- unsigned int i;
- struct NamespaceUpdateNode *nsn;
-
- ns->rc--;
- if (freeze)
- {
- if (0 != UNLINK (ns->filename))
- GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "unlink",
- ns->filename);
- }
- 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++)
- {
- 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;
-}
-
-
-/**
- * Context for the 'process_namespace' callback.
- * Specifies a function to call on each namespace.
- */
-struct ProcessNamespaceContext
-{
- /**
- * Function to call.
- */
- GNUNET_FS_NamespaceInfoProcessor cb;
-
- /**
- * Closure for 'cb'.
- */
- void *cb_cls;
-};
-
-
-/**
- * Function called with a filename of a namespace. Reads the key and
- * calls the callback.
- *
- * @param cls closure (struct ProcessNamespaceContext)
- * @param filename complete filename (absolute path)
- * @return GNUNET_OK to continue to iterate,
- * GNUNET_SYSERR to abort iteration with error!
- */
-static int
-process_namespace (void *cls, const char *filename)
-{
- struct ProcessNamespaceContext *pnc = cls;
- struct GNUNET_CRYPTO_RsaPrivateKey *key;
- struct GNUNET_HashCode id;
- const char *name;
- const char *t;
-
- key = GNUNET_CRYPTO_rsa_key_create_from_file (filename);
- if (NULL == key)
- {
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- _
- ("Failed to read namespace private key file `%s', deleting it!\n"),
- filename);
- if (0 != UNLINK (filename))
- GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
- return GNUNET_OK;
- }
- GNUNET_CRYPTO_rsa_get_public_key_hash (key, &id);
- GNUNET_CRYPTO_rsa_key_free (key);
- name = filename;
- while (NULL != (t = strstr (name, DIR_SEPARATOR_STR)))
- name = t + 1;
- pnc->cb (pnc->cb_cls, name, &id);
- return GNUNET_OK;
-}
-
-
-/**
- * Build a list of all available local (!) namespaces The returned
- * names are only the nicknames since we only iterate over the local
- * namespaces.
- *
- * @param h handle to the file sharing subsystem
- * @param cb function to call on each known namespace
- * @param cb_cls closure for cb
- */
-void
-GNUNET_FS_namespace_list (struct GNUNET_FS_Handle *h,
- GNUNET_FS_NamespaceInfoProcessor cb, void *cb_cls)
-{
- char *dn;
- struct ProcessNamespaceContext ctx;
-
- dn = get_namespace_directory (h);
- if (NULL == dn)
- return;
- ctx.cb = cb;
- ctx.cb_cls = cb_cls;
- GNUNET_DISK_directory_scan (dn, &process_namespace, &ctx);
- GNUNET_free (dn);