#include "gnunet_pseudonym_lib.h"
#include "gnunet_bio_lib.h"
-/**
+#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
+
+#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
+
+/**
* Name of the directory which stores meta data for pseudonym
*/
#define PS_METADATA_DIR DIR_SEPARATOR_STR "data" DIR_SEPARATOR_STR "pseudonyms" DIR_SEPARATOR_STR "metadata" DIR_SEPARATOR_STR
-/**
+/**
* Name of the directory which stores names for pseudonyms
*/
#define PS_NAMES_DIR DIR_SEPARATOR_STR "data" DIR_SEPARATOR_STR "pseudonyms" DIR_SEPARATOR_STR "names" DIR_SEPARATOR_STR
-/**
+/**
+ * Configuration section we use.
+ */
+#define GNUNET_CLIENT_SERVICE_NAME "client"
+
+
+
+/**
* Registered callbacks for discovery of pseudonyms.
*/
struct DiscoveryCallback
{
- /**
+ /**
* This is a linked list.
*/
struct DiscoveryCallback *next;
- /**
+ /**
* Function to call each time a pseudonym is discovered.
*/
GNUNET_PSEUDONYM_Iterator callback;
- /**
+ /**
* Closure for callback.
*/
void *closure;
};
-/**
- * Head of the linked list of functions to call when
+/**
+ * Head of the linked list of functions to call when
* new pseudonyms are added.
*/
static struct DiscoveryCallback *head;
pos = head;
while (pos != NULL)
- {
- pos->callback (pos->closure, id, md, rating);
- pos = pos->next;
- }
+ {
+ pos->callback (pos->closure, id, md, rating);
+ pos = pos->next;
+ }
}
/**
*/
int
GNUNET_PSEUDONYM_discovery_callback_register (const struct
- GNUNET_CONFIGURATION_Handle
- *cfg,
+ GNUNET_CONFIGURATION_Handle *cfg,
GNUNET_PSEUDONYM_Iterator
iterator, void *closure)
{
pos = head;
while ((pos != NULL) &&
((pos->callback != iterator) || (pos->closure != closure)))
- {
- prev = pos;
- pos = pos->next;
- }
+ {
+ prev = pos;
+ pos = pos->next;
+ }
if (pos == NULL)
return GNUNET_SYSERR;
if (prev == NULL)
* @return filename of the pseudonym (if psid != NULL) or directory with the data (if psid == NULL)
*/
static char *
-get_data_filename (const struct GNUNET_CONFIGURATION_Handle
- *cfg, const char *prefix, const GNUNET_HashCode * psid)
+get_data_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
+ const char *prefix, const GNUNET_HashCode * psid)
{
struct GNUNET_CRYPTO_HashAsciiEncoded enc;
if (psid != NULL)
GNUNET_CRYPTO_hash_to_enc (psid, &enc);
- return GNUNET_DISK_get_home_filename (cfg,
- GNUNET_CLIENT_SERVICE_NAME,
- prefix,
+ return GNUNET_DISK_get_home_filename (cfg, GNUNET_CLIENT_SERVICE_NAME, prefix,
(psid ==
NULL) ? NULL : (const char *) &enc,
NULL);
fn = get_data_filename (cfg, PS_METADATA_DIR, nsid);
GNUNET_assert (fn != NULL);
- fileW = GNUNET_BIO_write_open(fn);
- if (NULL != fileW)
+ fileW = GNUNET_BIO_write_open (fn);
+ if (NULL != fileW)
+ {
+ if ((GNUNET_OK != GNUNET_BIO_write_int32 (fileW, ranking)) ||
+ (GNUNET_OK != GNUNET_BIO_write_string (fileW, ns_name)) ||
+ (GNUNET_OK != GNUNET_BIO_write_meta_data (fileW, meta)))
+ {
+ (void) GNUNET_BIO_write_close (fileW);
+ GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
+ GNUNET_free (fn);
+ return;
+ }
+ if (GNUNET_OK != GNUNET_BIO_write_close (fileW))
{
- if ( (GNUNET_OK != GNUNET_BIO_write_int32(fileW, ranking)) ||
- (GNUNET_OK != GNUNET_BIO_write_string(fileW, ns_name)) ||
- (GNUNET_OK != GNUNET_BIO_write_meta_data(fileW, meta)) )
- {
- (void) GNUNET_BIO_write_close(fileW);
- GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
- GNUNET_free (fn);
- return;
- }
- if (GNUNET_OK != GNUNET_BIO_write_close(fileW))
- {
- GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
- GNUNET_free (fn);
- return;
- }
+ GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
+ GNUNET_free (fn);
+ return;
}
+ }
GNUNET_free (fn);
/* create entry for pseudonym name in names */
/* FIXME: 90% of what this call does is not needed
- here => refactor code to only create the entry! */
+ * here => refactor code to only create the entry! */
GNUNET_free_non_null (GNUNET_PSEUDONYM_id_to_name (cfg, nsid));
}
static int
read_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
const GNUNET_HashCode * nsid,
- struct GNUNET_CONTAINER_MetaData **meta,
- int32_t * ranking, char **ns_name)
+ struct GNUNET_CONTAINER_MetaData **meta, int32_t * ranking,
+ char **ns_name)
{
char *fn;
char *emsg;
fn = get_data_filename (cfg, PS_METADATA_DIR, nsid);
GNUNET_assert (fn != NULL);
- fileR = GNUNET_BIO_read_open(fn);
+ fileR = GNUNET_BIO_read_open (fn);
if (fileR == NULL)
- {
- GNUNET_free (fn);
- return GNUNET_SYSERR;
- }
+ {
+ GNUNET_free (fn);
+ return GNUNET_SYSERR;
+ }
emsg = NULL;
*ns_name = NULL;
- if ( (GNUNET_OK != GNUNET_BIO_read_int32 (fileR, ranking)) ||
- (GNUNET_OK != GNUNET_BIO_read_string(fileR, "Read string error!", ns_name, 200)) ||
- (NULL == *ns_name) ||
- (GNUNET_OK != GNUNET_BIO_read_meta_data(fileR, "Read meta data error!", meta)) )
- {
- GNUNET_BIO_read_close(fileR, &emsg);
- GNUNET_free_non_null (emsg);
- GNUNET_free_non_null (*ns_name);
- *ns_name = NULL;
- GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
- GNUNET_free (fn);
- return GNUNET_SYSERR;
- }
- if (GNUNET_OK != GNUNET_BIO_read_close(fileR, &emsg))
- {
- GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
- _("Failed to parse metadata about pseudonym from file `%s': %s\n"),
- fn,
- emsg);
- GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
- GNUNET_CONTAINER_meta_data_destroy (*meta);
- *meta = NULL;
- GNUNET_free_non_null (*ns_name);
- *ns_name = NULL;
- GNUNET_free_non_null (emsg);
- GNUNET_free (fn);
- return GNUNET_SYSERR;
- }
+ if ((GNUNET_OK != GNUNET_BIO_read_int32 (fileR, ranking)) ||
+ (GNUNET_OK !=
+ GNUNET_BIO_read_string (fileR, "Read string error!", ns_name, 200)) ||
+ (GNUNET_OK !=
+ GNUNET_BIO_read_meta_data (fileR, "Read meta data error!", meta)))
+ {
+ (void) GNUNET_BIO_read_close (fileR, &emsg);
+ GNUNET_free_non_null (emsg);
+ GNUNET_free_non_null (*ns_name);
+ *ns_name = NULL;
+ GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
+ GNUNET_free (fn);
+ return GNUNET_SYSERR;
+ }
+ if (GNUNET_OK != GNUNET_BIO_read_close (fileR, &emsg))
+ {
+ LOG (GNUNET_ERROR_TYPE_WARNING,
+ _("Failed to parse metadata about pseudonym from file `%s': %s\n"), fn,
+ emsg);
+ GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
+ GNUNET_CONTAINER_meta_data_destroy (*meta);
+ *meta = NULL;
+ GNUNET_free_non_null (*ns_name);
+ *ns_name = NULL;
+ GNUNET_free_non_null (emsg);
+ GNUNET_free (fn);
+ return GNUNET_SYSERR;
+ }
GNUNET_free (fn);
return GNUNET_OK;
}
/**
* Return the unique, human readable name for the given namespace.
*
- * @param cfg configuration
+ * @param cfg configuration
* @param nsid cryptographic ID of the namespace
* @return NULL on failure (should never happen)
*/
meta = NULL;
name = NULL;
if (GNUNET_OK == read_info (cfg, nsid, &meta, rank, &name))
+ {
+ if ((meta != NULL) && (name == NULL))
+ name =
+ GNUNET_CONTAINER_meta_data_get_first_by_types (meta,
+ EXTRACTOR_METATYPE_TITLE,
+ EXTRACTOR_METATYPE_FILENAME,
+ EXTRACTOR_METATYPE_DESCRIPTION,
+ EXTRACTOR_METATYPE_SUBJECT,
+ EXTRACTOR_METATYPE_PUBLISHER,
+ EXTRACTOR_METATYPE_AUTHOR_NAME,
+ EXTRACTOR_METATYPE_COMMENT,
+ EXTRACTOR_METATYPE_SUMMARY,
+ -1);
+ if (meta != NULL)
{
- if ((meta != NULL) && (name == NULL))
- name = GNUNET_CONTAINER_meta_data_get_first_by_types (meta,
- EXTRACTOR_METATYPE_TITLE,
- EXTRACTOR_METATYPE_FILENAME,
- EXTRACTOR_METATYPE_DESCRIPTION,
- EXTRACTOR_METATYPE_SUBJECT,
- EXTRACTOR_METATYPE_PUBLISHER,
- EXTRACTOR_METATYPE_AUTHOR_NAME,
- EXTRACTOR_METATYPE_COMMENT,
- EXTRACTOR_METATYPE_SUMMARY,
- -1);
- if (meta != NULL)
- {
- GNUNET_CONTAINER_meta_data_destroy (meta);
- meta = NULL;
- }
+ GNUNET_CONTAINER_meta_data_destroy (meta);
+ meta = NULL;
}
+ }
if (name == NULL)
name = GNUNET_strdup (_("no-name"));
GNUNET_CRYPTO_hash (name, strlen (name), &nh);
len = 0;
if (0 == STAT (fn, &sbuf))
GNUNET_DISK_file_size (fn, &len, GNUNET_YES);
- fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_CREATE
- | GNUNET_DISK_OPEN_READWRITE,
+ fh = GNUNET_DISK_file_open (fn,
+ GNUNET_DISK_OPEN_CREATE |
+ GNUNET_DISK_OPEN_READWRITE,
GNUNET_DISK_PERM_USER_READ |
GNUNET_DISK_PERM_USER_WRITE);
i = 0;
idx = -1;
while ((len >= sizeof (GNUNET_HashCode)) &&
- (sizeof (GNUNET_HashCode)
- == GNUNET_DISK_file_read (fh, &nh, sizeof (GNUNET_HashCode))))
- {
- if (0 == memcmp (&nh, nsid, sizeof (GNUNET_HashCode)))
- {
- idx = i;
- break;
- }
- i++;
- len -= sizeof (GNUNET_HashCode);
- }
- if (idx == -1)
+ (sizeof (GNUNET_HashCode) ==
+ GNUNET_DISK_file_read (fh, &nh, sizeof (GNUNET_HashCode))))
+ {
+ if (0 == memcmp (&nh, nsid, sizeof (GNUNET_HashCode)))
{
idx = i;
- if (sizeof (GNUNET_HashCode) !=
- GNUNET_DISK_file_write (fh, nsid, sizeof (GNUNET_HashCode)))
- GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn);
+ break;
}
+ i++;
+ len -= sizeof (GNUNET_HashCode);
+ }
+ if (idx == -1)
+ {
+ idx = i;
+ if (sizeof (GNUNET_HashCode) !=
+ GNUNET_DISK_file_write (fh, nsid, sizeof (GNUNET_HashCode)))
+ LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "write", fn);
+ }
GNUNET_DISK_file_close (fh);
ret = GNUNET_malloc (strlen (name) + 32);
GNUNET_snprintf (ret, strlen (name) + 32, "%s-%u", name, idx);
if ((GNUNET_OK != GNUNET_DISK_file_test (fn) ||
(GNUNET_OK != GNUNET_DISK_file_size (fn, &len, GNUNET_YES))) ||
((idx + 1) * sizeof (GNUNET_HashCode) > len))
- {
- GNUNET_free (fn);
- return GNUNET_SYSERR;
- }
- fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_CREATE
- | GNUNET_DISK_OPEN_READWRITE,
+ {
+ GNUNET_free (fn);
+ return GNUNET_SYSERR;
+ }
+ fh = GNUNET_DISK_file_open (fn,
+ GNUNET_DISK_OPEN_CREATE |
+ GNUNET_DISK_OPEN_READWRITE,
GNUNET_DISK_PERM_USER_READ |
GNUNET_DISK_PERM_USER_WRITE);
GNUNET_free (fn);
GNUNET_DISK_SEEK_SET);
if (sizeof (GNUNET_HashCode) !=
GNUNET_DISK_file_read (fh, nsid, sizeof (GNUNET_HashCode)))
- {
- GNUNET_DISK_file_close (fh);
- return GNUNET_SYSERR;
- }
+ {
+ GNUNET_DISK_file_close (fh);
+ return GNUNET_SYSERR;
+ }
GNUNET_DISK_file_close (fh);
return GNUNET_OK;
}
if (strlen (fullname) < sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded))
return GNUNET_OK;
- fn =
- &fullname[strlen (fullname) + 1 -
- sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)];
+ fn = &fullname[strlen (fullname) + 1 -
+ sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)];
if (fn[-1] != DIR_SEPARATOR)
return GNUNET_OK;
ret = GNUNET_OK;
/**
* List all available pseudonyms.
*
- * @param cfg overall configuration
+ * @param cfg overall configuration
* @param iterator function to call for each pseudonym
* @param closure closure for iterator
* @return number of pseudonyms found
name = NULL;
ret = read_info (cfg, nsid, &meta, &ranking, &name);
if (ret == GNUNET_SYSERR)
- {
- ranking = 0;
- meta = GNUNET_CONTAINER_meta_data_create ();
- }
+ {
+ ranking = 0;
+ meta = GNUNET_CONTAINER_meta_data_create ();
+ }
ranking += delta;
write_pseudonym_info (cfg, nsid, meta, ranking, name);
GNUNET_CONTAINER_meta_data_destroy (meta);
if ((0 == STAT (fn, &sbuf)) &&
(GNUNET_OK == read_info (cfg, id, &old, &ranking, &name)))
- {
- GNUNET_CONTAINER_meta_data_merge (old, meta);
- write_pseudonym_info (cfg, id, old, ranking, name);
- GNUNET_CONTAINER_meta_data_destroy (old);
- GNUNET_free_non_null (name);
- }
+ {
+ GNUNET_CONTAINER_meta_data_merge (old, meta);
+ write_pseudonym_info (cfg, id, old, ranking, name);
+ GNUNET_CONTAINER_meta_data_destroy (old);
+ GNUNET_free_non_null (name);
+ }
else
- {
- write_pseudonym_info (cfg, id, meta, ranking, NULL);
- }
+ {
+ write_pseudonym_info (cfg, id, meta, ranking, NULL);
+ }
GNUNET_free (fn);
internal_notify (id, meta, ranking);
}