-}
-
-
-/**
- * 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_FS_pseudonym_create (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;
-}
-
-
-/**
- * Open the namespace with the given name; if it does not exist,
- * or the key file is corrupted, the function fails.
- *
- * @param h handle to the file sharing subsystem
- * @param name name of the namespace
- * @return handle to the namespace,
- * NULL on error (i.e. invalid filename, non-existent filename)
- */
-struct GNUNET_FS_Namespace *
-GNUNET_FS_namespace_open_existing (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_FS_pseudonym_create_from_existing_file (fn);
- if (NULL == ret->key)
- {
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- _("Failed to 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;
-}
-
-
-/**
- * Rename a local namespace.
- *
- * @param h handle to the file sharing subsystem
- * @param old_name old name of the namespace
- * @param new_name new name of the namespace
- * @return GNUNET_OK on success, GNUNET_SYSERR on error (see errno for details)
- */
-int
-GNUNET_FS_namespace_rename (struct GNUNET_FS_Handle *h,
- const char *old_name,
- const char *new_name)
-{
- char *dn;
- char *fn_old;
- char *fn_new;
- int result;
- int save_errno;
-
- dn = get_namespace_directory (h);
- if (NULL == dn)
- {
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- _("Can't determine where namespace directory is\n"));
- return GNUNET_SYSERR;
- }
- GNUNET_asprintf (&fn_old, "%s%s%s", dn, DIR_SEPARATOR_STR, old_name);
- GNUNET_asprintf (&fn_new, "%s%s%s", dn, DIR_SEPARATOR_STR, new_name);
- GNUNET_free (dn);
- result = RENAME (fn_old, fn_new);
- save_errno = errno;
- GNUNET_free (fn_old);
- GNUNET_free (fn_new);
- errno = save_errno;
- if (result == 0)
- return GNUNET_OK;
- return GNUNET_SYSERR;
-}
-
-
-/**
- * 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_FS_pseudonym_destroy (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;
-};
-
-
-/**
- * Get hash of the public key of a namespace.
- *
- * @param ns namespace
- * @param id buffer to store the key in
- * @return GNUNET_OK on success
- * GNUNET_SYSERR on failure (contents of id remain intact)
- */
-int
-GNUNET_FS_namespace_get_public_identifier (struct GNUNET_FS_Namespace *ns,
- struct GNUNET_FS_PseudonymIdentifier *id)
-{
- if ((NULL == ns) || (NULL == id))
- return GNUNET_SYSERR;
- GNUNET_FS_pseudonym_get_identifier (ns->key, id);
- return GNUNET_OK;
-}
-
-
-/**
- * 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_FS_PseudonymHandle *ph;
- struct GNUNET_FS_PseudonymIdentifier id;
- const char *name;
- const char *t;
-
- if (NULL == (ph = GNUNET_FS_pseudonym_create (filename)))
- {
- 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_FS_pseudonym_get_identifier (ph, &id);
- GNUNET_FS_pseudonym_destroy (ph);
- 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);