2 This file is part of GNUnet
3 (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file fs/fs_namespace.c
23 * @brief create and destroy namespaces
24 * @author Christian Grothoff
27 #include "gnunet_constants.h"
28 #include "gnunet_signatures.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_fs_service.h"
35 * Maximum legal size for an sblock.
37 #define MAX_SBLOCK_SIZE (60 * 1024)
41 * Context for creating a namespace asynchronously.
43 struct GNUNET_FS_NamespaceCreationContext
46 * Context for asynchronous key creation.
48 struct GNUNET_CRYPTO_RsaKeyGenerationContext *keycreator;
51 * Name of the file to store key in / read key from.
56 * Name of the namespace.
63 struct GNUNET_FS_Handle *h;
66 * Function to call when generation ends (successfully or not)
68 GNUNET_FS_NamespaceCreationCallback cont;
71 * Client value to pass to continuation function.
78 * Return the name of the directory in which we store
79 * our local namespaces (or rather, their public keys).
81 * @param h global fs handle
82 * @return NULL on error, otherwise the name of the directory
85 get_namespace_directory (struct GNUNET_FS_Handle *h)
90 GNUNET_CONFIGURATION_get_value_filename (h->cfg, "FS", "IDENTITY_DIR",
93 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
94 "fs", "IDENTITY_DIR");
102 * Return the name of the directory in which we store
103 * the update information graph for the given local namespace.
105 * @param ns namespace handle
106 * @return NULL on error, otherwise the name of the directory
109 get_update_information_directory (struct GNUNET_FS_Namespace *ns)
115 GNUNET_CONFIGURATION_get_value_filename (ns->h->cfg, "FS", "UPDATE_DIR",
118 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
122 GNUNET_asprintf (&ret, "%s%s%s", dn, DIR_SEPARATOR_STR, ns->name);
129 * Write the namespace update node graph to a file.
131 * @param ns namespace to dump
134 write_update_information_graph (struct GNUNET_FS_Namespace *ns)
137 struct GNUNET_BIO_WriteHandle *wh;
139 struct NamespaceUpdateNode *n;
142 fn = get_update_information_directory (ns);
143 wh = GNUNET_BIO_write_open (fn);
146 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
147 _("Failed to open `%s' for writing: %s\n"), STRERROR (errno));
151 if (GNUNET_OK != GNUNET_BIO_write_int32 (wh, ns->update_node_count))
153 for (i = 0; i < ns->update_node_count; i++)
155 n = ns->update_nodes[i];
156 uris = GNUNET_FS_uri_to_string (n->uri);
157 if ((GNUNET_OK != GNUNET_BIO_write_string (wh, n->id)) ||
158 (GNUNET_OK != GNUNET_BIO_write_meta_data (wh, n->md)) ||
159 (GNUNET_OK != GNUNET_BIO_write_string (wh, n->update)) ||
160 (GNUNET_OK != GNUNET_BIO_write_string (wh, uris)))
168 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
169 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to write `%s': %s\n"),
176 * Read the namespace update node graph from a file.
178 * @param ns namespace to read
181 read_update_information_graph (struct GNUNET_FS_Namespace *ns)
184 struct GNUNET_BIO_ReadHandle *rh;
186 struct NamespaceUpdateNode *n;
191 fn = get_update_information_directory (ns);
192 if (GNUNET_YES != GNUNET_DISK_file_test (fn))
197 rh = GNUNET_BIO_read_open (fn);
203 if (GNUNET_OK != GNUNET_BIO_read_int32 (rh, &count))
208 if (count > 1024 * 1024)
215 GNUNET_break (GNUNET_OK == GNUNET_BIO_read_close (rh, NULL));
220 GNUNET_malloc (count * sizeof (struct NamespaceUpdateNode *));
222 for (i = 0; i < count; i++)
224 n = GNUNET_malloc (sizeof (struct NamespaceUpdateNode));
225 if ((GNUNET_OK != GNUNET_BIO_read_string (rh, "identifier", &n->id, 1024))
226 || (GNUNET_OK != GNUNET_BIO_read_meta_data (rh, "meta", &n->md)) ||
228 GNUNET_BIO_read_string (rh, "update-id", &n->update, 1024)) ||
229 (GNUNET_OK != GNUNET_BIO_read_string (rh, "uri", &uris, 1024 * 2)))
232 GNUNET_free_non_null (n->id);
233 GNUNET_free_non_null (n->update);
235 GNUNET_CONTAINER_meta_data_destroy (n->md);
239 n->uri = GNUNET_FS_uri_parse (uris, &emsg);
246 GNUNET_free_non_null (n->update);
247 GNUNET_CONTAINER_meta_data_destroy (n->md);
251 ns->update_nodes[i] = n;
253 ns->update_node_count = i;
255 if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
257 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to write `%s': %s\n"), emsg);
265 * Create a namespace with the given name; if one already
266 * exists, return a handle to the existing namespace.
268 * @param h handle to the file sharing subsystem
269 * @param name name to use for the namespace
270 * @return handle to the namespace, NULL on error (i.e. invalid filename)
272 struct GNUNET_FS_Namespace *
273 GNUNET_FS_namespace_create (struct GNUNET_FS_Handle *h, const char *name)
277 struct GNUNET_FS_Namespace *ret;
279 dn = get_namespace_directory (h);
282 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
283 _("Can't determine where namespace directory is\n"));
286 GNUNET_asprintf (&fn, "%s%s%s", dn, DIR_SEPARATOR_STR, name);
288 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Namespace));
291 ret->key = GNUNET_CRYPTO_rsa_key_create_from_file (fn);
292 if (NULL == ret->key)
294 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
295 _("Failed to create or read private key for namespace `%s'\n"),
301 ret->name = GNUNET_strdup (name);
308 * Open the namespace with the given name; if it does not exist,
309 * or the key file is corrupted, the function fails.
311 * @param h handle to the file sharing subsystem
312 * @param name name of the namespace
313 * @return handle to the namespace,
314 * NULL on error (i.e. invalid filename, non-existent filename)
316 struct GNUNET_FS_Namespace *
317 GNUNET_FS_namespace_open_existing (struct GNUNET_FS_Handle *h, const char *name)
321 struct GNUNET_FS_Namespace *ret;
323 dn = get_namespace_directory (h);
326 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
327 _("Can't determine where namespace directory is\n"));
330 GNUNET_asprintf (&fn, "%s%s%s", dn, DIR_SEPARATOR_STR, name);
332 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Namespace));
335 ret->key = GNUNET_CRYPTO_rsa_key_create_from_existing_file (fn);
336 if (NULL == ret->key)
338 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
339 _("Failed to read private key for namespace `%s'\n"), name);
344 ret->name = GNUNET_strdup (name);
351 * Function called upon completion of 'GNUNET_CRYPTO_rsa_key_create_start'.
354 * @param pk NULL on error, otherwise the private key (which must be free'd by the callee)
355 * @param emsg NULL on success, otherwise an error message
358 ns_key_created (void *cls, struct GNUNET_CRYPTO_RsaPrivateKey *pk,
361 struct GNUNET_FS_NamespaceCreationContext *ncc = cls;
363 ncc->keycreator = NULL;
367 struct GNUNET_FS_Namespace *ret;
368 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Namespace));
372 ret->name = ncc->name;
373 ret->filename = ncc->filename;
374 ncc->cont (ncc->cont_cls, ret, NULL);
378 GNUNET_free (ncc->filename);
379 GNUNET_free (ncc->name);
380 ncc->cont (ncc->cont_cls, NULL, emsg);
387 * Create a namespace with the given name.
388 * If one already exists, the continuation will be called with a handle to
389 * the existing namespace.
390 * Otherwise creates a new namespace.
392 * @param h handle to the file sharing subsystem
393 * @param name name to use for the namespace
394 * @return namespace creation context, NULL on error (i.e. invalid filename)
396 struct GNUNET_FS_NamespaceCreationContext *
397 GNUNET_FS_namespace_create_start (struct GNUNET_FS_Handle *h, const char *name,
398 GNUNET_FS_NamespaceCreationCallback cont, void *cont_cls)
402 struct GNUNET_FS_NamespaceCreationContext *ret;
404 dn = get_namespace_directory (h);
407 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
408 _("Can't determine where namespace directory is\n"));
411 GNUNET_asprintf (&fn, "%s%s%s", dn, DIR_SEPARATOR_STR, name);
414 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_NamespaceCreationContext));
417 ret->name = GNUNET_strdup (name);
419 ret->cont_cls = cont_cls;
421 ret->keycreator = GNUNET_CRYPTO_rsa_key_create_start (fn,
422 ns_key_created, ret);
424 if (NULL == ret->keycreator)
426 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
427 _("Failed to start creating or reading private key for namespace `%s'\n"),
430 GNUNET_free (ret->name);
439 * Abort namespace creation.
441 * @param ncc namespace creation context to abort
444 GNUNET_FS_namespace_create_stop (struct GNUNET_FS_NamespaceCreationContext *ncc)
446 if (NULL != ncc->keycreator)
448 GNUNET_CRYPTO_rsa_key_create_stop (ncc->keycreator);
449 ncc->keycreator = NULL;
451 if (NULL != ncc->filename)
453 if (0 != UNLINK (ncc->filename))
454 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", ncc->filename);
455 GNUNET_free (ncc->filename);
457 GNUNET_free_non_null (ncc->name);
463 * Duplicate a namespace handle.
465 * @param ns namespace handle
466 * @return duplicated handle to the namespace
468 struct GNUNET_FS_Namespace *
469 GNUNET_FS_namespace_dup (struct GNUNET_FS_Namespace *ns)
477 * Delete a namespace handle. Can be used for a clean shutdown (free
478 * memory) or also to freeze the namespace to prevent further
479 * insertions by anyone.
481 * @param ns handle to the namespace that should be deleted / freed
482 * @param freeze prevents future insertions; creating a namespace
483 * with the same name again will create a fresh namespace instead
485 * @return GNUNET_OK on success, GNUNET_SYSERR on error
488 GNUNET_FS_namespace_delete (struct GNUNET_FS_Namespace *ns, int freeze)
491 struct NamespaceUpdateNode *nsn;
496 if (0 != UNLINK (ns->filename))
497 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "unlink",
502 GNUNET_CRYPTO_rsa_key_free (ns->key);
503 GNUNET_free (ns->filename);
504 GNUNET_free (ns->name);
505 for (i = 0; i < ns->update_node_count; i++)
507 nsn = ns->update_nodes[i];
508 GNUNET_CONTAINER_meta_data_destroy (nsn->md);
509 GNUNET_FS_uri_destroy (nsn->uri);
510 GNUNET_free (nsn->id);
511 GNUNET_free (nsn->update);
514 GNUNET_array_grow (ns->update_nodes, ns->update_node_count,
516 if (ns->update_map != NULL)
517 GNUNET_CONTAINER_multihashmap_destroy (ns->update_map);
524 * Context for the 'process_namespace' callback.
525 * Specifies a function to call on each namespace.
527 struct ProcessNamespaceContext
532 GNUNET_FS_NamespaceInfoProcessor cb;
542 * Get hash of the public key of a namespace.
544 * @param ns namespace
545 * @param id buffer to store the key in
546 * @return GNUNET_OK on success
547 * GNUNET_SYSERR on failure (contents of id remain intact)
550 GNUNET_FS_namespace_get_public_key_hash (struct GNUNET_FS_Namespace *ns,
551 struct GNUNET_HashCode *id)
553 if ((NULL == ns) || (NULL == id))
554 return GNUNET_SYSERR;
555 GNUNET_CRYPTO_rsa_get_public_key_hash (ns->key, id);
561 * Function called with a filename of a namespace. Reads the key and
562 * calls the callback.
564 * @param cls closure (struct ProcessNamespaceContext)
565 * @param filename complete filename (absolute path)
566 * @return GNUNET_OK to continue to iterate,
567 * GNUNET_SYSERR to abort iteration with error!
570 process_namespace (void *cls, const char *filename)
572 struct ProcessNamespaceContext *pnc = cls;
573 struct GNUNET_CRYPTO_RsaPrivateKey *key;
574 struct GNUNET_HashCode id;
578 key = GNUNET_CRYPTO_rsa_key_create_from_file (filename);
581 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
583 ("Failed to read namespace private key file `%s', deleting it!\n"),
585 if (0 != UNLINK (filename))
586 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
589 GNUNET_CRYPTO_rsa_get_public_key_hash (key, &id);
590 GNUNET_CRYPTO_rsa_key_free (key);
592 while (NULL != (t = strstr (name, DIR_SEPARATOR_STR)))
594 pnc->cb (pnc->cb_cls, name, &id);
600 * Build a list of all available local (!) namespaces The returned
601 * names are only the nicknames since we only iterate over the local
604 * @param h handle to the file sharing subsystem
605 * @param cb function to call on each known namespace
606 * @param cb_cls closure for cb
609 GNUNET_FS_namespace_list (struct GNUNET_FS_Handle *h,
610 GNUNET_FS_NamespaceInfoProcessor cb, void *cb_cls)
613 struct ProcessNamespaceContext ctx;
615 dn = get_namespace_directory (h);
620 GNUNET_DISK_directory_scan (dn, &process_namespace, &ctx);
626 * Context for the SKS publication.
628 struct GNUNET_FS_PublishSksContext
632 * URI of the new entry in the namespace.
634 struct GNUNET_FS_Uri *uri;
637 * Namespace update node to add to namespace on success (or to be
638 * deleted if publishing failed).
640 struct NamespaceUpdateNode *nsn;
643 * Namespace we're publishing to.
645 struct GNUNET_FS_Namespace *ns;
648 * Handle to the datastore.
650 struct GNUNET_DATASTORE_Handle *dsh;
653 * Function to call once we're done.
655 GNUNET_FS_PublishContinuation cont;
663 * Handle for our datastore request.
665 struct GNUNET_DATASTORE_QueueEntry *dqe;
670 * Function called by the datastore API with
671 * the result from the PUT (SBlock) request.
673 * @param cls closure of type "struct GNUNET_FS_PublishSksContext*"
674 * @param success GNUNET_OK on success
675 * @param min_expiration minimum expiration time required for content to be stored
676 * @param msg error message (or NULL)
679 sb_put_cont (void *cls, int success,
680 struct GNUNET_TIME_Absolute min_expiration,
683 struct GNUNET_FS_PublishSksContext *psc = cls;
684 struct GNUNET_HashCode hc;
687 if (GNUNET_OK != success)
689 if (NULL != psc->cont)
690 psc->cont (psc->cont_cls, NULL, msg);
691 GNUNET_FS_publish_sks_cancel (psc);
694 if (NULL != psc->nsn)
696 /* FIXME: this can be done much more
697 * efficiently by simply appending to the
698 * file and overwriting the 4-byte header */
699 if (psc->ns->update_nodes == NULL)
700 read_update_information_graph (psc->ns);
701 GNUNET_array_append (psc->ns->update_nodes,
702 psc->ns->update_node_count, psc->nsn);
703 if (psc->ns->update_map != NULL)
705 GNUNET_CRYPTO_hash (psc->nsn->id, strlen (psc->nsn->id), &hc);
706 GNUNET_CONTAINER_multihashmap_put (psc->ns->update_map, &hc,
708 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
711 write_update_information_graph (psc->ns);
713 if (NULL != psc->cont)
714 psc->cont (psc->cont_cls, psc->uri, NULL);
715 GNUNET_FS_publish_sks_cancel (psc);
720 * Publish an SBlock on GNUnet.
722 * @param h handle to the file sharing subsystem
723 * @param ns namespace to publish in
724 * @param identifier identifier to use
725 * @param update update identifier to use
726 * @param meta metadata to use
727 * @param uri URI to refer to in the SBlock
728 * @param bo block options
729 * @param options publication options
730 * @param cont continuation
731 * @param cont_cls closure for cont
732 * @return NULL on error ('cont' will still be called)
734 struct GNUNET_FS_PublishSksContext *
735 GNUNET_FS_publish_sks (struct GNUNET_FS_Handle *h,
736 struct GNUNET_FS_Namespace *ns,
737 const char *identifier, const char *update,
738 const struct GNUNET_CONTAINER_MetaData *meta,
739 const struct GNUNET_FS_Uri *uri,
740 const struct GNUNET_FS_BlockOptions *bo,
741 enum GNUNET_FS_PublishOptions options,
742 GNUNET_FS_PublishContinuation cont, void *cont_cls)
744 struct GNUNET_FS_PublishSksContext *psc;
745 struct GNUNET_CRYPTO_AesSessionKey sk;
746 struct GNUNET_CRYPTO_AesInitializationVector iv;
747 struct GNUNET_FS_Uri *sks_uri;
755 struct SBlock *sb_enc;
757 struct GNUNET_CONTAINER_MetaData *mmeta;
758 struct GNUNET_HashCode key; /* hash of thisId = key */
759 struct GNUNET_HashCode id; /* hash of hc = identifier */
760 struct GNUNET_HashCode query; /* id ^ nsid = DB query */
762 idlen = strlen (identifier);
764 nidlen = strlen (update) + 1;
767 uris = GNUNET_FS_uri_to_string (uri);
768 slen = strlen (uris) + 1;
769 if ( (slen >= MAX_SBLOCK_SIZE - sizeof (struct SBlock)) ||
770 (nidlen >= MAX_SBLOCK_SIZE - sizeof (struct SBlock) - slen) )
772 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
773 _("Identifiers or URI too long to create SBlock"));
778 mmeta = GNUNET_CONTAINER_meta_data_create ();
780 mmeta = GNUNET_CONTAINER_meta_data_duplicate (meta);
781 mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (mmeta);
782 size = sizeof (struct SBlock) + slen + nidlen + mdsize;
783 if ( (size > MAX_SBLOCK_SIZE) ||
784 (size < sizeof (struct SBlock) + slen + nidlen) )
786 size = MAX_SBLOCK_SIZE;
787 mdsize = MAX_SBLOCK_SIZE - (sizeof (struct SBlock) + slen + nidlen);
789 sb = GNUNET_malloc (sizeof (struct SBlock) + size);
790 dest = (char *) &sb[1];
792 memcpy (dest, update, nidlen);
796 memcpy (dest, uris, slen);
800 GNUNET_CONTAINER_meta_data_serialize (mmeta, &dest, mdsize,
801 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
802 GNUNET_CONTAINER_meta_data_destroy (mmeta);
808 cont (cont_cls, NULL, _("Internal error."));
811 size = sizeof (struct SBlock) + mdsize + slen + nidlen;
812 sb_enc = GNUNET_malloc (size);
813 GNUNET_CRYPTO_hash (identifier, idlen, &key);
814 GNUNET_CRYPTO_hash (&key, sizeof (struct GNUNET_HashCode), &id);
815 sks_uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
816 sks_uri->type = GNUNET_FS_URI_SKS;
817 GNUNET_CRYPTO_rsa_key_get_public (ns->key, &sb_enc->subspace);
818 GNUNET_CRYPTO_hash (&sb_enc->subspace,
819 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
820 &sks_uri->data.sks.ns);
821 sks_uri->data.sks.identifier = GNUNET_strdup (identifier);
822 GNUNET_CRYPTO_hash_xor (&id, &sks_uri->data.sks.ns,
823 &sb_enc->identifier);
824 GNUNET_CRYPTO_hash_to_aes_key (&key, &sk, &iv);
825 GNUNET_CRYPTO_aes_encrypt (&sb[1], size - sizeof (struct SBlock), &sk, &iv,
827 sb_enc->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_SBLOCK);
828 sb_enc->purpose.size =
829 htonl (slen + mdsize + nidlen + sizeof (struct SBlock) -
830 sizeof (struct GNUNET_CRYPTO_RsaSignature));
831 GNUNET_assert (GNUNET_OK ==
832 GNUNET_CRYPTO_rsa_sign (ns->key, &sb_enc->purpose,
833 &sb_enc->signature));
834 psc = GNUNET_malloc (sizeof (struct GNUNET_FS_PublishSksContext));
837 psc->ns = GNUNET_FS_namespace_dup (ns);
838 psc->cont_cls = cont_cls;
839 if (0 != (options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY))
841 GNUNET_free (sb_enc);
843 sb_put_cont (psc, GNUNET_OK, GNUNET_TIME_UNIT_ZERO_ABS, NULL);
846 psc->dsh = GNUNET_DATASTORE_connect (h->cfg);
847 if (NULL == psc->dsh)
849 GNUNET_free (sb_enc);
851 sb_put_cont (psc, GNUNET_NO, GNUNET_TIME_UNIT_ZERO_ABS, _("Failed to connect to datastore."));
854 GNUNET_CRYPTO_hash_xor (&sks_uri->data.sks.ns, &id, &query);
857 psc->nsn = GNUNET_malloc (sizeof (struct NamespaceUpdateNode));
858 psc->nsn->id = GNUNET_strdup (identifier);
859 psc->nsn->update = GNUNET_strdup (update);
860 psc->nsn->md = GNUNET_CONTAINER_meta_data_duplicate (meta);
861 psc->nsn->uri = GNUNET_FS_uri_dup (uri);
863 psc->dqe = GNUNET_DATASTORE_put (psc->dsh, 0, &sb_enc->identifier, size, sb_enc,
864 GNUNET_BLOCK_TYPE_FS_SBLOCK, bo->content_priority,
865 bo->anonymity_level, bo->replication_level,
866 bo->expiration_time, -2, 1,
867 GNUNET_CONSTANTS_SERVICE_TIMEOUT, &sb_put_cont, psc);
869 GNUNET_free (sb_enc);
875 * Abort the SKS publishing operation.
877 * @param psc context of the operation to abort.
880 GNUNET_FS_publish_sks_cancel (struct GNUNET_FS_PublishSksContext *psc)
882 if (NULL != psc->dqe)
884 GNUNET_DATASTORE_cancel (psc->dqe);
887 if (NULL != psc->dsh)
889 GNUNET_DATASTORE_disconnect (psc->dsh, GNUNET_NO);
892 GNUNET_FS_namespace_delete (psc->ns, GNUNET_NO);
893 GNUNET_FS_uri_destroy (psc->uri);
894 if (NULL != psc->nsn)
896 GNUNET_CONTAINER_meta_data_destroy (psc->nsn->md);
897 GNUNET_FS_uri_destroy (psc->nsn->uri);
898 GNUNET_free (psc->nsn->id);
899 GNUNET_free (psc->nsn->update);
900 GNUNET_free (psc->nsn);
907 * Closure for 'process_update_node'.
909 struct ProcessUpdateClosure
912 * Function to call for each node.
914 GNUNET_FS_IdentifierProcessor ip;
924 * Call the iterator in the closure for each node.
926 * @param cls closure (of type 'struct ProcessUpdateClosure *')
927 * @param key current key code
928 * @param value value in the hash map (of type 'struct NamespaceUpdateNode *')
929 * @return GNUNET_YES if we should continue to
934 process_update_node (void *cls, const struct GNUNET_HashCode * key, void *value)
936 struct ProcessUpdateClosure *pc = cls;
937 struct NamespaceUpdateNode *nsn = value;
939 pc->ip (pc->ip_cls, nsn->id, nsn->uri, nsn->md, nsn->update);
945 * Closure for 'find_trees'.
947 struct FindTreeClosure
950 * Namespace we are operating on.
952 struct GNUNET_FS_Namespace *ns;
955 * Array with 'head's of TREEs.
957 struct NamespaceUpdateNode **tree_array;
960 * Size of 'tree_array'
962 unsigned int tree_array_size;
965 * Current generational ID used.
970 * Identifier for the current TREE, or UINT_MAX for none yet.
977 * Find all nodes reachable from the current node (including the
978 * current node itself). If they are in no tree, add them to the
979 * current one. If they are the head of another tree, merge the
980 * trees. If they are in the middle of another tree, let them be.
981 * We can tell that a node is already in an tree by checking if
982 * its 'nug' field is set to the current 'nug' value. It is the
983 * head of an tree if it is in the 'tree_array' under its respective
986 * In short, we're trying to find the smallest number of tree to
987 * cover a directed graph.
989 * @param cls closure (of type 'struct FindTreeClosure')
990 * @param key current key code
991 * @param value value in the hash map
992 * @return GNUNET_YES if we should continue to
997 find_trees (void *cls, const struct GNUNET_HashCode * key, void *value)
999 struct FindTreeClosure *fc = cls;
1000 struct NamespaceUpdateNode *nsn = value;
1001 struct GNUNET_HashCode hc;
1003 if (nsn->nug == fc->nug)
1005 if (UINT_MAX == nsn->tree_id)
1006 return GNUNET_YES; /* circular */
1007 GNUNET_assert (nsn->tree_id < fc->tree_array_size);
1008 if (fc->tree_array[nsn->tree_id] != nsn)
1009 return GNUNET_YES; /* part of "another" (directed) TREE,
1010 * and not root of it, end trace */
1011 if (nsn->tree_id == fc->id)
1012 return GNUNET_YES; /* that's our own root (can this be?) */
1013 /* merge existing TREE, we have a root for both */
1014 fc->tree_array[nsn->tree_id] = NULL;
1015 if (UINT_MAX == fc->id)
1016 fc->id = nsn->tree_id; /* take over ID */
1021 nsn->tree_id = UINT_MAX; /* mark as undef */
1023 GNUNET_CRYPTO_hash (nsn->update, strlen (nsn->update), &hc);
1024 GNUNET_CONTAINER_multihashmap_get_multiple (fc->ns->update_map, &hc,
1032 * List all of the identifiers in the namespace for which we could
1033 * produce an update. Namespace updates form a graph where each node
1034 * has a name. Each node can have any number of URI/meta-data entries
1035 * which can each be linked to other nodes. Cycles are possible.
1037 * Calling this function with "next_id" NULL will cause the library to
1038 * call "ip" with a root for each strongly connected component of the
1039 * graph (a root being a node from which all other nodes in the Tree
1042 * Calling this function with "next_id" being the name of a node will
1043 * cause the library to call "ip" with all children of the node. Note
1044 * that cycles within the final tree are possible (including self-loops).
1045 * I know, odd definition of a tree, but the GUI will display an actual
1046 * tree (GtkTreeView), so that's what counts for the term here.
1048 * @param ns namespace to inspect for updateable content
1049 * @param next_id ID to look for; use NULL to look for tree roots
1050 * @param ip function to call on each updateable identifier
1051 * @param ip_cls closure for ip
1054 GNUNET_FS_namespace_list_updateable (struct GNUNET_FS_Namespace *ns,
1055 const char *next_id,
1056 GNUNET_FS_IdentifierProcessor ip,
1061 struct GNUNET_HashCode hc;
1062 struct NamespaceUpdateNode *nsn;
1063 struct ProcessUpdateClosure pc;
1064 struct FindTreeClosure fc;
1066 if (NULL == ns->update_nodes)
1067 read_update_information_graph (ns);
1068 if (NULL == ns->update_nodes)
1070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1071 "No updateable nodes found for ID `%s'\n", next_id);
1072 return; /* no nodes */
1074 if (NULL == ns->update_map)
1076 /* need to construct */
1078 GNUNET_CONTAINER_multihashmap_create (2 +
1079 3 * ns->update_node_count /
1082 for (i = 0; i < ns->update_node_count; i++)
1084 nsn = ns->update_nodes[i];
1085 GNUNET_CRYPTO_hash (nsn->id, strlen (nsn->id), &hc);
1086 GNUNET_CONTAINER_multihashmap_put (ns->update_map, &hc, nsn,
1087 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1090 if (NULL != next_id)
1092 GNUNET_CRYPTO_hash (next_id, strlen (next_id), &hc);
1095 GNUNET_CONTAINER_multihashmap_get_multiple (ns->update_map, &hc,
1096 &process_update_node, &pc);
1099 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1100 "Calculating TREEs to find roots of update trees\n");
1101 /* Find heads of TREEs in update graph */
1102 nug = ++ns->nug_gen;
1103 fc.tree_array = NULL;
1104 fc.tree_array_size = 0;
1106 for (i = 0; i < ns->update_node_count; i++)
1108 nsn = ns->update_nodes[i];
1109 if (nsn->nug == nug)
1111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TREE of node `%s' is %u\n", nsn->id,
1113 continue; /* already placed in TREE */
1115 GNUNET_CRYPTO_hash (nsn->update, strlen (nsn->update), &hc);
1117 nsn->tree_id = UINT_MAX;
1121 GNUNET_CONTAINER_multihashmap_get_multiple (ns->update_map, &hc,
1123 if (UINT_MAX == fc.id)
1125 /* start new TREE */
1126 for (fc.id = 0; fc.id < fc.tree_array_size; fc.id++)
1128 if (fc.tree_array[fc.id] == NULL)
1130 fc.tree_array[fc.id] = nsn;
1131 nsn->tree_id = fc.id;
1135 if (fc.id == fc.tree_array_size)
1137 GNUNET_array_append (fc.tree_array, fc.tree_array_size, nsn);
1138 nsn->tree_id = fc.id;
1140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1141 "Starting new TREE %u with node `%s'\n", nsn->tree_id,
1143 /* put all nodes with same identifier into this TREE */
1144 GNUNET_CRYPTO_hash (nsn->id, strlen (nsn->id), &hc);
1145 fc.id = nsn->tree_id;
1148 GNUNET_CONTAINER_multihashmap_get_multiple (ns->update_map, &hc,
1153 /* make head of TREE "id" */
1154 fc.tree_array[fc.id] = nsn;
1155 nsn->tree_id = fc.id;
1157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TREE of node `%s' is %u\n", nsn->id,
1160 for (i = 0; i < fc.tree_array_size; i++)
1162 nsn = fc.tree_array[i];
1165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Root of TREE %u is node `%s'\n", i,
1167 ip (ip_cls, nsn->id, nsn->uri, nsn->md, nsn->update);
1170 GNUNET_array_grow (fc.tree_array, fc.tree_array_size, 0);
1171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Done processing TREEs\n");
1175 /* end of fs_namespace.c */