[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[GNUnet-SVN] r6826 - GNUnet/src/applications/fs/namespace
From: |
gnunet |
Subject: |
[GNUnet-SVN] r6826 - GNUnet/src/applications/fs/namespace |
Date: |
Wed, 14 May 2008 19:29:23 -0600 (MDT) |
Author: grothoff
Date: 2008-05-14 19:29:15 -0600 (Wed, 14 May 2008)
New Revision: 6826
Added:
GNUnet/src/applications/fs/namespace/common.c
GNUnet/src/applications/fs/namespace/common.h
GNUnet/src/applications/fs/namespace/names.c
GNUnet/src/applications/fs/namespace/namespace_info.h
GNUnet/src/applications/fs/namespace/namespace_notification.c
GNUnet/src/applications/fs/namespace/namespace_notification.h
GNUnet/src/applications/fs/namespace/root_info.c
GNUnet/src/applications/fs/namespace/update_info.c
GNUnet/src/applications/fs/namespace/uri.c
Log:
missing
Added: GNUnet/src/applications/fs/namespace/common.c
===================================================================
--- GNUnet/src/applications/fs/namespace/common.c
(rev 0)
+++ GNUnet/src/applications/fs/namespace/common.c 2008-05-15 01:29:15 UTC
(rev 6826)
@@ -0,0 +1,82 @@
+/*
+ This file is part of GNUnet
+ (C) 2003, 2004, 2005, 2006, 2007, 2008 Christian Grothoff (and other
contributing authors)
+
+ GNUnet is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published
+ by the Free Software Foundation; either version 2, or (at your
+ option) any later version.
+
+ GNUnet is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNUnet; see the file COPYING. If not, write to the
+ Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA.
+*/
+
+/**
+ * @file applications/fs/namespace/common.c
+ * @brief helper functions
+ * @author Christian Grothoff
+ */
+
+#include "platform.h"
+#include "gnunet_util.h"
+#include "gnunet_directories.h"
+#include "common.h"
+
+/**
+ * Get the filename (or directory name) for the given
+ * namespace and content identifier and directory
+ * prefix.
+ *
+ * @param prefix NS_DIR, NS_UPDATE_DIR or NS_ROOTS
+ * @param nsid maybe NULL
+ * @param lastId maybe NULL
+ */
+char *
+GNUNET_NS_internal_get_data_filename_ (struct GNUNET_GE_Context *ectx,
+ struct GNUNET_GC_Configuration *cfg,
+ const char * prefix,
+ const GNUNET_HashCode *nsid,
+ const GNUNET_HashCode * lastId)
+{
+ char *tmp;
+ char *ret;
+ GNUNET_EncName enc;
+ GNUNET_EncName idenc;
+
+ GNUNET_GC_get_configuration_value_filename (cfg,
+ "GNUNET",
+ "GNUNET_HOME",
+ GNUNET_DEFAULT_HOME_DIRECTORY,
+ &tmp);
+ ret =
+ GNUNET_malloc (strlen (tmp) + strlen (prefix) +
+ sizeof (GNUNET_EncName)*2 + 20);
+ strcpy (ret, tmp);
+ GNUNET_free (tmp);
+ if (ret[strlen(ret)-1] != DIR_SEPARATOR)
+ strcat (ret, DIR_SEPARATOR_STR);
+ strcat (ret, prefix);
+ GNUNET_disk_directory_create (ectx, ret);
+ if (nsid != NULL)
+ {
+ GNUNET_hash_to_enc(nsid, &enc);
+ strcat (ret, (const char*)&enc);
+ }
+ if (lastId != NULL)
+ {
+ strcat (ret, DIR_SEPARATOR_STR);
+ GNUNET_disk_directory_create (ectx, ret);
+ GNUNET_hash_to_enc (lastId, &idenc);
+ strcat (ret, (const char *) &idenc);
+ }
+ return ret;
+}
+
+/* end of common.c */
Added: GNUnet/src/applications/fs/namespace/common.h
===================================================================
--- GNUnet/src/applications/fs/namespace/common.h
(rev 0)
+++ GNUnet/src/applications/fs/namespace/common.h 2008-05-15 01:29:15 UTC
(rev 6826)
@@ -0,0 +1,54 @@
+/*
+ This file is part of GNUnet
+ (C) 2003, 2004, 2005, 2006, 2007, 2008 Christian Grothoff (and other
contributing authors)
+
+ GNUnet is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published
+ by the Free Software Foundation; either version 2, or (at your
+ option) any later version.
+
+ GNUnet is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNUnet; see the file COPYING. If not, write to the
+ Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA.
+*/
+
+/**
+ * @file applications/fs/namespace/common.h
+ * @brief helper functions
+ * @author Christian Grothoff
+ */
+
+
+#include "gnunet_namespace_lib.h"
+
+#ifndef NS_COMMON_H
+#define NS_COMMON_H
+
+#define NS_METADATA_DIR "data" DIR_SEPARATOR_STR "namespace/metadata"
DIR_SEPARATOR_STR
+#define NS_UPDATE_DIR "data" DIR_SEPARATOR_STR "namespace/updates"
DIR_SEPARATOR_STR
+#define NS_ROOTS_DIR "data" DIR_SEPARATOR_STR "namespace/roots"
DIR_SEPARATOR_STR
+#define NS_NAMES_DIR "data" DIR_SEPARATOR_STR "namespace/names"
DIR_SEPARATOR_STR
+
+/**
+ * Get the filename (or directory name) for the given
+ * namespace and content identifier and directory
+ * prefix.
+ *
+ * @param prefix NS_DIR, NS_UPDATE_DIR or NS_ROOTS
+ * @param nsid maybe NULL
+ * @param lastId maybe NULL
+ */
+char *
+GNUNET_NS_internal_get_data_filename_ (struct GNUNET_GE_Context *ectx,
+ struct GNUNET_GC_Configuration *cfg,
+ const char * prefix,
+ const GNUNET_HashCode *nsid,
+ const GNUNET_HashCode * lastId);
+
+#endif
Added: GNUnet/src/applications/fs/namespace/names.c
===================================================================
--- GNUnet/src/applications/fs/namespace/names.c
(rev 0)
+++ GNUnet/src/applications/fs/namespace/names.c 2008-05-15 01:29:15 UTC
(rev 6826)
@@ -0,0 +1,191 @@
+/*
+ This file is part of GNUnet
+ (C) 2003, 2004, 2005, 2006, 2007, 2008 Christian Grothoff (and other
contributing authors)
+
+ GNUnet is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published
+ by the Free Software Foundation; either version 2, or (at your
+ option) any later version.
+
+ GNUnet is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNUnet; see the file COPYING. If not, write to the
+ Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA.
+*/
+
+/**
+ * @file applications/fs/namespace/names.c
+ * @brief create unique, human-readable names for namespaces
+ * @author Christian Grothoff
+ */
+
+#include "platform.h"
+#include <extractor.h>
+#include "gnunet_directories.h"
+#include "gnunet_namespace_lib.h"
+#include "gnunet_util.h"
+#include "namespace_info.h"
+#include "common.h"
+
+
+/**
+ * Return the unique, human readable name for the given namespace.
+ *
+ * @return NULL on failure (should never happen)
+ */
+char *
+GNUNET_NS_nsid_to_name(struct GNUNET_GE_Context *ectx,
+ struct GNUNET_GC_Configuration *cfg,
+ const GNUNET_HashCode * nsid)
+{
+ struct GNUNET_ECRS_MetaData * meta;
+ char * name;
+ GNUNET_HashCode nh;
+ char * fn;
+ unsigned long long len;
+ int fd;
+ unsigned int i;
+ unsigned int idx;
+ char * ret;
+
+ meta = NULL;
+ name = NULL;
+ GNUNET_NS_internal_read_namespace_info_ (ectx, cfg, nsid, &meta, NULL,
&name);
+ if ( (meta != NULL) &&
+ (name == NULL) )
+ name = GNUNET_ECRS_meta_data_get_first_by_types(meta,
+ EXTRACTOR_TITLE,
+ EXTRACTOR_FILENAME,
+ EXTRACTOR_DESCRIPTION,
+ EXTRACTOR_SUBJECT,
+ EXTRACTOR_PUBLISHER,
+ EXTRACTOR_AUTHOR,
+ EXTRACTOR_COMMENT,
+ EXTRACTOR_SUMMARY,
+ EXTRACTOR_OWNER,
+ -1);
+ if (meta != NULL)
+ GNUNET_ECRS_meta_data_destroy(meta);
+ if (name == NULL)
+ name = GNUNET_strdup(_("no-name"));
+ GNUNET_hash(name,
+ strlen(name),
+ &nh);
+ fn = GNUNET_NS_internal_get_data_filename_(ectx,
+ cfg,
+ NS_NAMES_DIR,
+ &nh,
+ NULL);
+ if ((GNUNET_OK != GNUNET_disk_file_test (ectx,
+ fn) ||
+ (GNUNET_OK != GNUNET_disk_file_size (ectx, fn, &len, GNUNET_YES))))
+ {
+ GNUNET_free (fn);
+ return NULL;
+ }
+ fd = GNUNET_disk_file_open(ectx,
+ fn,
+ O_CREAT | O_RDWR,
+ S_IRUSR | S_IWUSR);
+ i = 0;
+ idx = -1;
+ while ( (len >= sizeof(GNUNET_HashCode)) &&
+ (sizeof(GNUNET_HashCode)
+ == READ(fd, &nh, sizeof(GNUNET_HashCode))))
+ {
+ if (0 == memcmp(&nh,
+ nsid,
+ sizeof(GNUNET_HashCode)))
+ {
+ idx = i;
+ break;
+ }
+ i++;
+ len -= sizeof(GNUNET_HashCode);
+ }
+ if (idx == -1)
+ {
+ idx = i;
+ WRITE(fd, nsid, sizeof(GNUNET_HashCode));
+ }
+ CLOSE(fd);
+ ret = GNUNET_malloc(strlen(name) + 32);
+ GNUNET_snprintf(ret,
+ strlen(name) + 32,
+ "%s-%u",
+ name,
+ idx);
+ GNUNET_free(name);
+ GNUNET_free (fn);
+ return ret;
+}
+
+/**
+ * Get the namespace ID belonging to the given namespace name.
+ *
+ * @return GNUNET_OK on success
+ */
+int GNUNET_NS_name_to_nsid(struct GNUNET_GE_Context *ectx,
+ struct GNUNET_GC_Configuration *cfg,
+ const char * ns_uname,
+ GNUNET_HashCode * nsid)
+{
+ size_t slen;
+ unsigned long long len;
+ unsigned int idx;
+ char * name;
+ GNUNET_HashCode nh;
+ char * fn;
+ int fd;
+
+ idx = -1;
+ slen = strlen(ns_uname);
+ while ( (slen > 0) &&
+ (1 != sscanf(&ns_uname[slen-1],
+ "-%u",
+ &idx)) )
+ slen--;
+ if (slen == 0)
+ return GNUNET_SYSERR;
+ name = GNUNET_strdup(ns_uname);
+ name[slen] = '\0';
+ GNUNET_hash(name,
+ strlen(name),
+ &nh);
+ GNUNET_free(name);
+ fn = GNUNET_NS_internal_get_data_filename_(ectx,
+ cfg,
+ NS_NAMES_DIR,
+ &nh,
+ NULL);
+ if ( (GNUNET_OK != GNUNET_disk_file_test (ectx,
+ fn) ||
+ (GNUNET_OK != GNUNET_disk_file_size (ectx, fn, &len, GNUNET_YES))) ||
+ ( (idx+1) * sizeof(GNUNET_HashCode) > len) )
+ {
+ GNUNET_free (fn);
+ return GNUNET_SYSERR;
+ }
+ fd = GNUNET_disk_file_open(ectx,
+ fn,
+ O_CREAT | O_RDWR,
+ S_IRUSR | S_IWUSR);
+ GNUNET_free (fn);
+ LSEEK(fd, idx * sizeof(GNUNET_HashCode), SEEK_SET);
+ if (sizeof(GNUNET_HashCode) !=
+ READ(fd, nsid, sizeof(GNUNET_HashCode)))
+ {
+ CLOSE(fd);
+ return GNUNET_SYSERR;
+ }
+ CLOSE(fd);
+ return GNUNET_OK;
+}
+
+
+/* end of names.c */
Added: GNUnet/src/applications/fs/namespace/namespace_info.h
===================================================================
--- GNUnet/src/applications/fs/namespace/namespace_info.h
(rev 0)
+++ GNUnet/src/applications/fs/namespace/namespace_info.h 2008-05-15
01:29:15 UTC (rev 6826)
@@ -0,0 +1,41 @@
+/*
+ This file is part of GNUnet
+ (C) 2003, 2004, 2005, 2006, 2007, 2008 Christian Grothoff (and other
contributing authors)
+
+ GNUnet is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published
+ by the Free Software Foundation; either version 2, or (at your
+ option) any later version.
+
+ GNUnet is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNUnet; see the file COPYING. If not, write to the
+ Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA.
+*/
+
+/**
+ * @file applications/fs/namespace/namespace_info.h
+ * @brief keeping track of namespaces and metadata about them
+ * @author Christian Grothoff
+ */
+
+#ifndef NAMESPACE_INFO_H
+#define NAMESPACE_INFO_H
+
+#include "gnunet_namespace_lib.h"
+#include "gnunet_util.h"
+
+int
+GNUNET_NS_internal_read_namespace_info_ (struct GNUNET_GE_Context *ectx,
+ struct GNUNET_GC_Configuration *cfg,
+ const GNUNET_HashCode * nsid,
+ struct GNUNET_ECRS_MetaData **meta,
+ int *ranking,
+ char ** ns_name);
+
+#endif
Added: GNUnet/src/applications/fs/namespace/namespace_notification.c
===================================================================
--- GNUnet/src/applications/fs/namespace/namespace_notification.c
(rev 0)
+++ GNUnet/src/applications/fs/namespace/namespace_notification.c
2008-05-15 01:29:15 UTC (rev 6826)
@@ -0,0 +1,136 @@
+/*
+ This file is part of GNUnet
+ (C) 2003, 2004, 2005, 2006, 2007, 2008 Christian Grothoff (and other
contributing authors)
+
+ GNUnet is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published
+ by the Free Software Foundation; either version 2, or (at your
+ option) any later version.
+
+ GNUnet is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNUnet; see the file COPYING. If not, write to the
+ Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA.
+*/
+
+/**
+ * @file applications/fs/namespace/namespace_notification.c
+ * @brief implementation of the notification mechanism
+ * @author Christian Grothoff
+ */
+
+
+#include "platform.h"
+#include "gnunet_directories.h"
+#include "gnunet_namespace_lib.h"
+#include "gnunet_util.h"
+
+struct DiscoveryCallback
+{
+ struct DiscoveryCallback *next;
+ GNUNET_NS_NamespaceIterator callback;
+ void *closure;
+};
+
+static struct DiscoveryCallback *head;
+
+static struct GNUNET_Mutex *lock;
+
+/**
+ * Internal notification about new tracked URI.
+ */
+void
+GNUNET_NS_internal_notify_ (const char *name,
+ const GNUNET_HashCode * id,
+ const struct GNUNET_ECRS_MetaData *md, int rating)
+{
+ struct DiscoveryCallback *pos;
+
+ GNUNET_mutex_lock (lock);
+ pos = head;
+ while (pos != NULL)
+ {
+ pos->callback (pos->closure, name, id, md, rating);
+ pos = pos->next;
+ }
+ GNUNET_mutex_unlock (lock);
+}
+
+
+
+/**
+ * Register callback to be invoked whenever we discover
+ * a new namespace.
+ */
+int
+GNUNET_NS_register_discovery_callback (struct GNUNET_GE_Context *ectx,
+ struct GNUNET_GC_Configuration *cfg,
+ GNUNET_NS_NamespaceIterator iterator,
+ void *closure)
+{
+ struct DiscoveryCallback *list;
+
+ list = GNUNET_malloc (sizeof (struct DiscoveryCallback));
+ list->callback = iterator;
+ list->closure = closure;
+ GNUNET_mutex_lock (lock);
+ list->next = head;
+ head = list;
+ GNUNET_NS_namespace_list_all (ectx, cfg, iterator, closure);
+ GNUNET_mutex_unlock (lock);
+ return GNUNET_OK;
+}
+
+/**
+ * Unregister namespace discovery callback.
+ */
+int
+GNUNET_NS_unregister_discovery_callback (GNUNET_NS_NamespaceIterator iterator,
+ void *closure)
+{
+ struct DiscoveryCallback *prev;
+ struct DiscoveryCallback *pos;
+
+ prev = NULL;
+ GNUNET_mutex_lock (lock);
+ pos = head;
+ while ((pos != NULL) &&
+ ((pos->callback != iterator) || (pos->closure != closure)))
+ {
+ prev = pos;
+ pos = pos->next;
+ }
+ if (pos == NULL)
+ {
+ GNUNET_mutex_unlock (lock);
+ return GNUNET_SYSERR;
+ }
+ if (prev == NULL)
+ head = pos->next;
+ else
+ prev->next = pos->next;
+ GNUNET_free (pos);
+ GNUNET_mutex_unlock (lock);
+ return GNUNET_OK;
+}
+
+
+
+void __attribute__ ((constructor)) GNUNET_NS_ltdl_init ()
+{
+ lock = GNUNET_mutex_create (GNUNET_NO);
+}
+
+void __attribute__ ((destructor)) GNUNET_NS_ltdl_fini ()
+{
+ GNUNET_mutex_destroy (lock);
+ lock = NULL;
+}
+
+
+/* end of namespace_notification.c */
Added: GNUnet/src/applications/fs/namespace/namespace_notification.h
===================================================================
--- GNUnet/src/applications/fs/namespace/namespace_notification.h
(rev 0)
+++ GNUnet/src/applications/fs/namespace/namespace_notification.h
2008-05-15 01:29:15 UTC (rev 6826)
@@ -0,0 +1,41 @@
+/*
+ This file is part of GNUnet
+ (C) 2003, 2004, 2005, 2006, 2007, 2008 Christian Grothoff (and other
contributing authors)
+
+ GNUnet is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published
+ by the Free Software Foundation; either version 2, or (at your
+ option) any later version.
+
+ GNUnet is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNUnet; see the file COPYING. If not, write to the
+ Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA.
+*/
+
+/**
+ * @file applications/fs/namespace/namespace_notification.h
+ * @brief implementation of the notification mechanism
+ * @author Christian Grothoff
+ */
+
+
+#include "gnunet_namespace_lib.h"
+
+#ifndef NAMESPACE_NOTIFICATON_H
+#define NAMESPACE_NOTIFICATON_H
+
+/**
+ * Internal notification about new tracked URI.
+ */
+void
+GNUNET_NS_internal_notify_ (const char *name,
+ const GNUNET_HashCode * id,
+ const struct GNUNET_ECRS_MetaData *md, int rating);
+
+#endif
Added: GNUnet/src/applications/fs/namespace/root_info.c
===================================================================
--- GNUnet/src/applications/fs/namespace/root_info.c
(rev 0)
+++ GNUnet/src/applications/fs/namespace/root_info.c 2008-05-15 01:29:15 UTC
(rev 6826)
@@ -0,0 +1,85 @@
+/*
+ This file is part of GNUnet
+ (C) 2003, 2004, 2005, 2006, 2007, 2008 Christian Grothoff (and other
contributing authors)
+
+ GNUnet is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published
+ by the Free Software Foundation; either version 2, or (at your
+ option) any later version.
+
+ GNUnet is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNUnet; see the file COPYING. If not, write to the
+ Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA.
+*/
+
+/**
+ * @file applications/fs/namespace/namespace_info.c
+ * @brief keeping track of namespaces roots
+ * @author Christian Grothoff
+ */
+
+#include "platform.h"
+#include "gnunet_directories.h"
+#include "gnunet_namespace_lib.h"
+#include "gnunet_util.h"
+#include "common.h"
+
+/**
+ * Get the root of the namespace (if we have one).
+ * @return GNUNET_SYSERR on error, GNUNET_OK on success
+ */
+int
+GNUNET_NS_namespace_get_root (struct GNUNET_GE_Context *ectx,
+ struct GNUNET_GC_Configuration *cfg,
+ const GNUNET_HashCode * ns_id,
+ GNUNET_HashCode * root)
+{
+ char *fn;
+ int ret;
+
+ fn = GNUNET_NS_internal_get_data_filename_(ectx,
+ cfg,
+ NS_ROOTS_DIR,
+ ns_id,
+ NULL);
+ if (sizeof (GNUNET_HashCode)
+ == GNUNET_disk_file_read (ectx, fn, sizeof (GNUNET_HashCode), root))
+ ret = GNUNET_OK;
+ else
+ ret = GNUNET_SYSERR;
+ GNUNET_free (fn);
+ return ret;
+}
+
+void
+GNUNET_NS_namespace_set_root (struct GNUNET_GE_Context *ectx,
+ struct GNUNET_GC_Configuration *cfg,
+ const struct GNUNET_ECRS_URI *uri)
+{
+ char *fn;
+ GNUNET_HashCode ns;
+ GNUNET_HashCode rt;
+
+ if (GNUNET_OK != GNUNET_ECRS_uri_get_namespace_from_sks (uri, &ns))
+ {
+ GNUNET_GE_BREAK (ectx, 0);
+ return;
+ }
+ fn = GNUNET_NS_internal_get_data_filename_(ectx,
+ cfg,
+ NS_ROOTS_DIR,
+ &ns,
+ NULL);
+ if (GNUNET_OK == GNUNET_ECRS_uri_get_content_hash_from_sks (uri, &rt))
+ GNUNET_disk_file_write (ectx, fn, &rt, sizeof (GNUNET_HashCode), "644");
+ GNUNET_free (fn);
+}
+
+
+/* end of root_info.c */
Added: GNUnet/src/applications/fs/namespace/update_info.c
===================================================================
--- GNUnet/src/applications/fs/namespace/update_info.c
(rev 0)
+++ GNUnet/src/applications/fs/namespace/update_info.c 2008-05-15 01:29:15 UTC
(rev 6826)
@@ -0,0 +1,525 @@
+/*
+ This file is part of GNUnet
+ (C) 2003, 2004, 2005, 2006, 2007, 2008 Christian Grothoff (and other
contributing authors)
+
+ GNUnet is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published
+ by the Free Software Foundation; either version 2, or (at your
+ option) any later version.
+
+ GNUnet is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNUnet; see the file COPYING. If not, write to the
+ Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA.
+*/
+
+/**
+ * @file applications/fs/namespace/update_info.c
+ * @brief support for content updates
+ * @author Christian Grothoff
+ */
+
+#include "platform.h"
+#include "gnunet_directories.h"
+#include "gnunet_namespace_lib.h"
+#include "gnunet_util.h"
+#include "namespace_info.h"
+#include "namespace_notification.h"
+#include "common.h"
+
+
+struct UpdateData
+{
+ GNUNET_Int32Time updateInterval;
+ GNUNET_Int32Time lastPubTime;
+ GNUNET_HashCode nextId;
+ GNUNET_HashCode thisId;
+};
+
+/**
+ * Read content update information about content
+ * published in the given namespace under 'lastId'.
+ *
+ * @param fi maybe NULL
+ * @return GNUNET_OK if update data was found, GNUNET_SYSERR if not.
+ */
+static int
+read_update_data (struct GNUNET_GE_Context *ectx,
+ struct GNUNET_GC_Configuration *cfg,
+ const GNUNET_HashCode * nsid,
+ const GNUNET_HashCode * lastId,
+ GNUNET_HashCode * nextId,
+ GNUNET_ECRS_FileInfo * fi,
+ GNUNET_Int32Time * updateInterval,
+ GNUNET_Int32Time * lastPubTime)
+{
+ char *fn;
+ struct UpdateData *buf;
+ char *uri;
+ unsigned long long size;
+ size_t pos;
+
+ fn = GNUNET_NS_internal_get_data_filename_(ectx,
+ cfg,
+ NS_UPDATE_DIR,
+ nsid,
+ lastId);
+ if (GNUNET_OK != GNUNET_disk_file_size (ectx, fn, &size, GNUNET_YES))
+ {
+ GNUNET_free (fn);
+ return GNUNET_SYSERR;
+ }
+ if ((size == 0) ||
+ (size <= sizeof (struct UpdateData)) || (size > 1024 * 1024 * 16))
+ {
+ GNUNET_free (fn);
+ return GNUNET_SYSERR;
+ }
+ buf = GNUNET_malloc (size);
+ if (size != GNUNET_disk_file_read (ectx, fn, size, buf))
+ {
+ GNUNET_free (buf);
+ GNUNET_free (fn);
+ return GNUNET_SYSERR;
+ }
+ GNUNET_free (fn);
+ if (0 != memcmp (lastId, &buf->thisId, sizeof (GNUNET_HashCode)))
+ {
+ GNUNET_free (buf);
+ return GNUNET_SYSERR;
+ }
+ uri = (char *) &buf[1];
+ size -= sizeof (struct UpdateData);
+ pos = 0;
+ while ((pos < size) && (uri[pos] != '\0'))
+ pos++;
+ pos++;
+ size -= pos;
+ if (size == 0)
+ {
+ GNUNET_free (buf);
+ GNUNET_GE_BREAK (ectx, 0);
+ return GNUNET_SYSERR;
+ }
+ if (fi != NULL)
+ {
+ fi->meta = GNUNET_ECRS_meta_data_deserialize (ectx, &uri[pos], size);
+ if (fi->meta == NULL)
+ {
+ GNUNET_free (buf);
+ GNUNET_GE_BREAK (ectx, 0);
+ return GNUNET_SYSERR;
+ }
+ fi->uri = GNUNET_ECRS_string_to_uri (ectx, uri);
+ if (fi->uri == NULL)
+ {
+ GNUNET_ECRS_meta_data_destroy (fi->meta);
+ fi->meta = NULL;
+ GNUNET_free (buf);
+ GNUNET_GE_BREAK (ectx, 0);
+ return GNUNET_SYSERR;
+ }
+ }
+ if (updateInterval != NULL)
+ *updateInterval = ntohl (buf->updateInterval);
+ if (lastPubTime != NULL)
+ *lastPubTime = ntohl (buf->lastPubTime);
+ if (nextId != NULL)
+ *nextId = buf->nextId;
+ GNUNET_free (buf);
+ return GNUNET_OK;
+}
+
+/**
+ * Write content update information.
+ */
+static int
+write_update_data (struct GNUNET_GE_Context *ectx,
+ struct GNUNET_GC_Configuration *cfg,
+ const GNUNET_HashCode * nsid,
+ const GNUNET_HashCode * thisId,
+ const GNUNET_HashCode * nextId,
+ const GNUNET_ECRS_FileInfo * fi,
+ const GNUNET_Int32Time updateInterval,
+ const GNUNET_Int32Time lastPubTime)
+{
+ char *fn;
+ char *uri;
+ size_t metaSize;
+ size_t size;
+ struct UpdateData *buf;
+
+ uri = GNUNET_ECRS_uri_to_string (fi->uri);
+ metaSize =
+ GNUNET_ECRS_meta_data_get_serialized_size (fi->meta,
+ GNUNET_ECRS_SERIALIZE_FULL);
+ size = sizeof (struct UpdateData) + metaSize + strlen (uri) + 1;
+ buf = GNUNET_malloc (size);
+ buf->nextId = *nextId;
+ buf->thisId = *thisId;
+ buf->updateInterval = htonl (updateInterval);
+ buf->lastPubTime = htonl (lastPubTime);
+ memcpy (&buf[1], uri, strlen (uri) + 1);
+ GNUNET_GE_ASSERT (ectx,
+ metaSize ==
+ GNUNET_ECRS_meta_data_serialize (ectx,
+ fi->meta,
+ &((char *)
+ &buf[1])[strlen (uri) +
+ 1], metaSize,
+
GNUNET_ECRS_SERIALIZE_FULL));
+ GNUNET_free (uri);
+ fn = GNUNET_NS_internal_get_data_filename_(ectx,
+ cfg,
+ NS_UPDATE_DIR,
+ nsid,
+ thisId);
+ GNUNET_disk_file_write (ectx, fn, buf, size, "400"); /* no editing, just
deletion */
+ GNUNET_free (fn);
+ GNUNET_free (buf);
+ return GNUNET_OK;
+}
+
+
+/**
+ * Compute the next ID for peridodically updated content.
+ * @param updateInterval MUST be a peridic interval (not NONE or SPORADIC)
+ * @param thisId MUST be known to NAMESPACE
+ * @return GNUNET_OK on success, GNUNET_SYSERR on error
+ */
+int
+GNUNET_NS_compute_next_identifier (struct GNUNET_GE_Context *ectx,
+ struct GNUNET_GC_Configuration *cfg,
+ const GNUNET_HashCode * nsid,
+ const GNUNET_HashCode * lastId,
+ const GNUNET_HashCode * thisId,
+ GNUNET_Int32Time updateInterval,
+ GNUNET_HashCode * nextId)
+{
+ GNUNET_HashCode delta;
+ GNUNET_CronTime now;
+ GNUNET_Int32Time tnow;
+ GNUNET_Int32Time lastTime;
+ GNUNET_Int32Time ui;
+
+ if ((updateInterval == GNUNET_ECRS_SBLOCK_UPDATE_SPORADIC) ||
+ (updateInterval == GNUNET_ECRS_SBLOCK_UPDATE_NONE))
+ return GNUNET_SYSERR;
+
+ if (GNUNET_OK != read_update_data (ectx,
+ cfg, nsid, lastId, NULL, NULL, &ui,
+ &lastTime))
+ return GNUNET_SYSERR;
+ GNUNET_hash_difference (lastId, thisId, &delta);
+ now = GNUNET_get_time ();
+ GNUNET_get_time_int32 (&tnow);
+ *nextId = *thisId;
+ while (lastTime < tnow + updateInterval / 2)
+ {
+ lastTime += updateInterval;
+ GNUNET_hash_sum (nextId, &delta, nextId);
+ }
+ return GNUNET_OK;
+}
+
+
+/**
+ * Add an entry into a namespace (also for publishing
+ * updates).
+ *
+ * @param name in which namespace to publish
+ * @param updateInterval the desired frequency for updates
+ * @param lastId the ID of the last value (maybe NULL)
+ * @param thisId the ID of the update (maybe NULL)
+ * @param nextId the ID of the next update (maybe NULL)
+ * @param dst to which URI should the namespace entry refer?
+ * @param md what meta-data should be associated with the
+ * entry?
+ * @param uri set to the resulting URI
+ */
+struct GNUNET_ECRS_URI *
+GNUNET_NS_add_to_namespace (struct GNUNET_GE_Context *ectx,
+ struct GNUNET_GC_Configuration *cfg,
+ unsigned int anonymityLevel,
+ unsigned int insertPriority,
+ GNUNET_CronTime insertExpiration,
+ const GNUNET_HashCode * nsid,
+ GNUNET_Int32Time updateInterval,
+ const GNUNET_HashCode * lastId,
+ const GNUNET_HashCode * thisId,
+ const GNUNET_HashCode * nextId,
+ const struct GNUNET_ECRS_URI *dst,
+ const struct GNUNET_ECRS_MetaData *md)
+{
+ GNUNET_Int32Time creationTime;
+ GNUNET_HashCode nid;
+ GNUNET_HashCode tid;
+ GNUNET_HashCode delta;
+ GNUNET_Int32Time now;
+ GNUNET_Int32Time lastTime;
+ GNUNET_Int32Time lastInterval;
+ GNUNET_ECRS_FileInfo fi;
+ char *old;
+ struct GNUNET_ECRS_URI *uri;
+ char * name;
+ GNUNET_EncName enc;
+
+ if ( (GNUNET_OK !=
+ GNUNET_NS_internal_read_namespace_info_(ectx,
+ cfg,
+ nsid,
+ NULL,
+ NULL,
+ &name)) ||
+ (name == NULL) )
+ {
+ GNUNET_hash_to_enc(nsid, &enc);
+ GNUNET_GE_LOG (ectx,
+ GNUNET_GE_WARNING | GNUNET_GE_BULK |
+ GNUNET_GE_USER,
+ _
+ ("Could not determine namespace name for `%s'."),
+ &enc);
+ return NULL;
+ }
+
+ /* computation of IDs of update(s). Not as terrible as
+ it looks, just enumerating all of the possible cases
+ of periodic/sporadic updates and how IDs are computed. */
+ creationTime = GNUNET_get_time_int32 (&now);
+ if (updateInterval != GNUNET_ECRS_SBLOCK_UPDATE_NONE)
+ {
+ if ((lastId != NULL) &&
+ (GNUNET_OK == read_update_data (ectx,
+ cfg,
+ nsid,
+ lastId,
+ &tid, NULL, &lastInterval,
+ &lastTime)))
+ {
+ if (lastInterval != updateInterval)
+ {
+ GNUNET_GE_LOG (ectx,
+ GNUNET_GE_WARNING | GNUNET_GE_BULK |
+ GNUNET_GE_USER,
+ _
+ ("Publication interval for periodic publication
changed."));
+ }
+ /* try to compute tid and/or
+ nid based on information read from lastId */
+
+ if (updateInterval != GNUNET_ECRS_SBLOCK_UPDATE_SPORADIC)
+ {
+ GNUNET_hash_difference (lastId, &tid, &delta);
+
+ creationTime = lastTime + updateInterval;
+ while (creationTime < now - updateInterval)
+ {
+ creationTime += updateInterval;
+ GNUNET_hash_sum (&tid, &delta, &tid);
+ }
+ if (creationTime > GNUNET_get_time () + 7 * GNUNET_CRON_DAYS)
+ {
+ GNUNET_GE_LOG (ectx,
+ GNUNET_GE_WARNING | GNUNET_GE_BULK |
+ GNUNET_GE_USER,
+ _
+ ("Publishing update for periodically updated "
+ "content more than a week ahead of
schedule.\n"));
+ }
+ if (thisId != NULL)
+ tid = *thisId; /* allow override! */
+ GNUNET_hash_sum (&tid, &delta, &nid);
+ if (nextId != NULL)
+ nid = *nextId; /* again, allow override */
+ }
+ else
+ {
+ /* sporadic ones are unpredictable,
+ tid has been obtained from IO, pick random nid if
+ not specified */
+ if (thisId != NULL)
+ tid = *thisId; /* allow user override */
+ if (nextId == NULL)
+ {
+ GNUNET_create_random_hash (&nid);
+ }
+ else
+ {
+ nid = *nextId;
+ }
+ }
+ }
+ else
+ { /* no previous ID found or given */
+ if (nextId == NULL)
+ {
+ /* no previous block found and nextId not specified;
+ pick random nid */
+ GNUNET_create_random_hash (&nid);
+ }
+ else
+ {
+ nid = *nextId;
+ }
+ if (thisId != NULL)
+ {
+ tid = *thisId;
+ }
+ else
+ {
+ GNUNET_create_random_hash (&tid);
+ }
+ }
+ }
+ else
+ {
+ if (thisId != NULL)
+ {
+ nid = tid = *thisId;
+ }
+ else
+ {
+ GNUNET_create_random_hash (&tid);
+ nid = tid;
+ }
+ }
+ uri = GNUNET_ECRS_namespace_add_content (ectx,
+ cfg,
+ name,
+ anonymityLevel,
+ insertPriority,
+ insertExpiration,
+ creationTime,
+ updateInterval, &tid, &nid, dst,
+ md);
+ if ((uri != NULL) && (dst != NULL))
+ {
+ fi.uri = (struct GNUNET_ECRS_URI*) dst;
+ fi.meta = (struct GNUNET_ECRS_MetaData *) md;
+ write_update_data (ectx,
+ cfg,
+ nsid, &tid, &nid, &fi, updateInterval,
+ creationTime);
+ if (lastId != NULL)
+ {
+ old = GNUNET_NS_internal_get_data_filename_(ectx,
+ cfg,
+ NS_UPDATE_DIR,
+ nsid,
+ lastId);
+ UNLINK (old);
+ GNUNET_free (old);
+ }
+ }
+ return uri;
+}
+
+struct ListNamespaceContentsClosure
+{
+ GNUNET_HashCode nsid;
+ GNUNET_NS_UpdateIterator it;
+ void *closure;
+ int cnt;
+ struct GNUNET_GE_Context *ectx;
+ struct GNUNET_GC_Configuration *cfg;
+};
+
+static int
+list_namespace_contents_helper (const char *fil, const char *dir, void *ptr)
+{
+ struct ListNamespaceContentsClosure *cls = ptr;
+ GNUNET_ECRS_FileInfo fi;
+ GNUNET_HashCode lastId;
+ GNUNET_HashCode nextId;
+ GNUNET_Int32Time pubFreq;
+ GNUNET_Int32Time lastTime;
+ GNUNET_Int32Time nextTime;
+ GNUNET_Int32Time now;
+
+ if (GNUNET_OK != GNUNET_enc_to_hash (fil, &lastId))
+ {
+ GNUNET_GE_BREAK (cls->ectx, 0);
+ return GNUNET_OK;
+ }
+ fi.uri = NULL;
+ fi.meta = NULL;
+ if (GNUNET_OK != read_update_data (cls->ectx,
+ cls->cfg,
+ &cls->nsid,
+ &lastId,
+ &nextId, &fi, &pubFreq,
+ &lastTime))
+ {
+ GNUNET_GE_BREAK (cls->ectx, 0);
+ return GNUNET_OK;
+ }
+ cls->cnt++;
+ if (pubFreq == GNUNET_ECRS_SBLOCK_UPDATE_SPORADIC)
+ {
+ nextTime = 0;
+ }
+ else
+ {
+ GNUNET_get_time_int32 (&now);
+ nextTime = lastTime;
+ if ((nextTime + pubFreq < now) && (nextTime + pubFreq > nextTime))
+ nextTime += pubFreq * ((now - nextTime) / pubFreq);
+ }
+ if (cls->it != NULL)
+ {
+ if (GNUNET_OK != cls->it (cls->closure,
+ &fi, &lastId, &nextId, pubFreq, nextTime))
+ {
+ GNUNET_ECRS_uri_destroy (fi.uri);
+ GNUNET_ECRS_meta_data_destroy (fi.meta);
+ return GNUNET_SYSERR;
+ }
+ }
+ GNUNET_ECRS_uri_destroy (fi.uri);
+ GNUNET_ECRS_meta_data_destroy (fi.meta);
+ return GNUNET_OK;
+}
+
+/**
+ * List all updateable content in a given namespace.
+ */
+int
+GNUNET_NS_namespace_list_contents (struct GNUNET_GE_Context *ectx,
+ struct GNUNET_GC_Configuration *cfg,
+ const GNUNET_HashCode *nsid,
+ GNUNET_NS_UpdateIterator iterator,
+ void *closure)
+{
+ struct ListNamespaceContentsClosure cls;
+ char *dirName;
+
+ cls.nsid = *nsid;
+ cls.it = iterator;
+ cls.closure = closure;
+ cls.cnt = 0;
+ cls.ectx = ectx;
+ cls.cfg = cfg;
+ dirName = GNUNET_NS_internal_get_data_filename_(ectx,
+ cfg,
+ NS_UPDATE_DIR,
+ nsid,
+ NULL);
+ GNUNET_disk_directory_create (ectx, dirName);
+ if (GNUNET_SYSERR ==
+ GNUNET_disk_directory_scan (ectx, dirName,
&list_namespace_contents_helper, &cls))
+ {
+ GNUNET_free (dirName);
+ return GNUNET_SYSERR;
+ }
+ GNUNET_free (dirName);
+ return cls.cnt;
+}
+
+
+/* end of update_info.c */
Added: GNUnet/src/applications/fs/namespace/uri.c
===================================================================
--- GNUnet/src/applications/fs/namespace/uri.c (rev 0)
+++ GNUnet/src/applications/fs/namespace/uri.c 2008-05-15 01:29:15 UTC (rev
6826)
@@ -0,0 +1,66 @@
+/*
+ This file is part of GNUnet
+ (C) 2003, 2004, 2005, 2006, 2007, 2008 Christian Grothoff (and other
contributing authors)
+
+ GNUnet is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published
+ by the Free Software Foundation; either version 2, or (at your
+ option) any later version.
+
+ GNUnet is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNUnet; see the file COPYING. If not, write to the
+ Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA.
+*/
+
+/**
+ * @file applications/fs/namespace/uri.c
+ * @brief uri support
+ * @author Christian Grothoff
+ *
+ * TODO:
+ * - consider remembering the char*-form of the
+ * namespace identifier (optionally?)
+ * => generate better names when possible!
+ * (this would require changes in ECRS!)
+ */
+
+#include "platform.h"
+#include "gnunet_ecrs_lib.h"
+#include "gnunet_namespace_lib.h"
+
+/**
+ * Convert namespace URI to a human readable format
+ * (using the namespace description, if available).
+ */
+char * GNUNET_NS_sks_uri_to_human_readable_string (struct GNUNET_GE_Context
*ectx,
+ struct
GNUNET_GC_Configuration *cfg,
+ const struct GNUNET_ECRS_URI
*uri)
+{
+ GNUNET_EncName enc;
+ char * ret;
+ char *name;
+ GNUNET_HashCode nsid;
+ GNUNET_HashCode chk;
+
+ if (! GNUNET_ECRS_uri_test_sks(uri))
+ return NULL;
+ GNUNET_ECRS_uri_get_namespace_from_sks (uri, &nsid);
+ name = GNUNET_NS_nsid_to_name(ectx, cfg, &nsid);
+ if (name == NULL)
+ return GNUNET_ECRS_uri_to_string(uri);
+ GNUNET_ECRS_uri_get_content_hash_from_sks(uri, &chk);
+ GNUNET_hash_to_enc(&chk, &enc);
+ ret = GNUNET_malloc(strlen(name) + 4 + sizeof(GNUNET_EncName));
+ strcpy(ret, name);
+ strcat(ret, ": ");
+ strcat(ret, (const char*)&enc);
+ return ret;
+}
+
+/* end of uri.c */
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [GNUnet-SVN] r6826 - GNUnet/src/applications/fs/namespace,
gnunet <=