- doc
[oweals/gnunet.git] / src / util / pseudonym.c
1 /*
2      This file is part of GNUnet
3      (C) 2003, 2004, 2005, 2006, 2007, 2008 Christian Grothoff (and other contributing authors)
4
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 2, or (at your
8      option) any later version.
9
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.
14
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.
19 */
20
21 /**
22  * @file util/pseudonym.c
23  * @brief helper functions
24  * @author Christian Grothoff
25  */
26
27 #include "platform.h"
28 #include "gnunet_common.h"
29 #include "gnunet_container_lib.h"
30 #include "gnunet_disk_lib.h"
31 #include "gnunet_pseudonym_lib.h"
32 #include "gnunet_bio_lib.h"
33
34 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
35
36 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
37
38 /**
39  * Name of the directory which stores meta data for pseudonym
40  */
41 #define PS_METADATA_DIR DIR_SEPARATOR_STR "data" DIR_SEPARATOR_STR "pseudonyms" DIR_SEPARATOR_STR "metadata" DIR_SEPARATOR_STR
42
43 /**
44  * Name of the directory which stores names for pseudonyms
45  */
46 #define PS_NAMES_DIR    DIR_SEPARATOR_STR "data" DIR_SEPARATOR_STR "pseudonyms" DIR_SEPARATOR_STR "names"    DIR_SEPARATOR_STR
47
48
49 /**
50  * Configuration section we use.
51  */
52 #define GNUNET_CLIENT_SERVICE_NAME "client"
53
54
55
56 /**
57  * Registered callbacks for discovery of pseudonyms.
58  */
59 struct DiscoveryCallback
60 {
61   /**
62    * This is a linked list.
63    */
64   struct DiscoveryCallback *next;
65
66   /**
67    * Function to call each time a pseudonym is discovered.
68    */
69   GNUNET_PSEUDONYM_Iterator callback;
70
71   /**
72    * Closure for callback.
73    */
74   void *closure;
75 };
76
77
78 /**
79  * Head of the linked list of functions to call when
80  * new pseudonyms are added.
81  */
82 static struct DiscoveryCallback *head;
83
84 /**
85  * Internal notification about new tracked URI.
86  * @param id a point to the hash code of pseudonym
87  * @param md meta data to be written
88  * @param rating rating of pseudonym
89  */
90 static void
91 internal_notify (const struct GNUNET_HashCode * id,
92                  const struct GNUNET_CONTAINER_MetaData *md, int rating)
93 {
94   struct DiscoveryCallback *pos;
95
96   pos = head;
97   while (pos != NULL)
98   {
99     pos->callback (pos->closure, id, NULL, NULL, md, rating);
100     pos = pos->next;
101   }
102 }
103
104 /**
105  * Register callback to be invoked whenever we discover
106  * a new pseudonym.
107  * Will immediately call provided iterator callback for all
108  * already discovered pseudonyms.
109  *
110  * @param cfg configuration to use
111  * @param iterator iterator over pseudonym
112  * @param closure point to a closure
113  */
114 int
115 GNUNET_PSEUDONYM_discovery_callback_register (const struct
116                                               GNUNET_CONFIGURATION_Handle *cfg,
117                                               GNUNET_PSEUDONYM_Iterator
118                                               iterator, void *closure)
119 {
120   struct DiscoveryCallback *list;
121
122   list = GNUNET_malloc (sizeof (struct DiscoveryCallback));
123   list->callback = iterator;
124   list->closure = closure;
125   list->next = head;
126   head = list;
127   GNUNET_PSEUDONYM_list_all (cfg, iterator, closure);
128   return GNUNET_OK;
129 }
130
131 /**
132  * Unregister pseudonym discovery callback.
133  * @param iterator iterator over pseudonym
134  * @param closure point to a closure
135  */
136 int
137 GNUNET_PSEUDONYM_discovery_callback_unregister (GNUNET_PSEUDONYM_Iterator
138                                                 iterator, void *closure)
139 {
140   struct DiscoveryCallback *prev;
141   struct DiscoveryCallback *pos;
142
143   prev = NULL;
144   pos = head;
145   while ((pos != NULL) &&
146          ((pos->callback != iterator) || (pos->closure != closure)))
147   {
148     prev = pos;
149     pos = pos->next;
150   }
151   if (pos == NULL)
152     return GNUNET_SYSERR;
153   if (prev == NULL)
154     head = pos->next;
155   else
156     prev->next = pos->next;
157   GNUNET_free (pos);
158   return GNUNET_OK;
159 }
160
161
162 /**
163  * Get the filename (or directory name) for the given
164  * pseudonym identifier and directory prefix.
165  * @param cfg configuration to use
166  * @param prefix path components to append to the private directory name
167  * @param psid hash code of pseudonym, can be NULL
168  * @return filename of the pseudonym (if psid != NULL) or directory with the data (if psid == NULL)
169  */
170 static char *
171 get_data_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
172                    const char *prefix, const struct GNUNET_HashCode * psid)
173 {
174   struct GNUNET_CRYPTO_HashAsciiEncoded enc;
175
176   if (psid != NULL)
177     GNUNET_CRYPTO_hash_to_enc (psid, &enc);
178   return GNUNET_DISK_get_home_filename (cfg, GNUNET_CLIENT_SERVICE_NAME, prefix,
179                                         (psid ==
180                                          NULL) ? NULL : (const char *) &enc,
181                                         NULL);
182 }
183
184
185 /**
186  * Write the pseudonym infomation into a file
187  * @param cfg configuration to use
188  * @param nsid hash code of a pseudonym
189  * @param meta meta data to be written into a file
190  * @param ranking ranking of a pseudonym
191  * @param ns_name non-unique name of a pseudonym
192  */
193 static void
194 write_pseudonym_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
195                       const struct GNUNET_HashCode * nsid,
196                       const struct GNUNET_CONTAINER_MetaData *meta,
197                       int32_t ranking, const char *ns_name)
198 {
199   char *fn;
200   struct GNUNET_BIO_WriteHandle *fileW;
201
202   fn = get_data_filename (cfg, PS_METADATA_DIR, nsid);
203   GNUNET_assert (fn != NULL);
204   fileW = GNUNET_BIO_write_open (fn);
205   if (NULL != fileW)
206   {
207     if ((GNUNET_OK != GNUNET_BIO_write_int32 (fileW, ranking)) ||
208         (GNUNET_OK != GNUNET_BIO_write_string (fileW, ns_name)) ||
209         (GNUNET_OK != GNUNET_BIO_write_meta_data (fileW, meta)))
210     {
211       (void) GNUNET_BIO_write_close (fileW);
212       GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
213       GNUNET_free (fn);
214       return;
215     }
216     if (GNUNET_OK != GNUNET_BIO_write_close (fileW))
217     {
218       GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
219       GNUNET_free (fn);
220       return;
221     }
222   }
223   GNUNET_free (fn);
224   /* create entry for pseudonym name in names */
225   if (ns_name != NULL)
226     GNUNET_free_non_null (GNUNET_PSEUDONYM_name_uniquify (cfg, nsid, ns_name,
227         NULL));
228 }
229
230
231 /**
232  * read the pseudonym infomation from a file
233  * @param cfg configuration to use
234  * @param nsid hash code of a pseudonym
235  * @param meta meta data to be read from a file
236  * @param ranking ranking of a pseudonym
237  * @param ns_name name of a pseudonym
238  */
239 static int
240 read_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
241            const struct GNUNET_HashCode * nsid,
242            struct GNUNET_CONTAINER_MetaData **meta, int32_t * ranking,
243            char **ns_name)
244 {
245   char *fn;
246   char *emsg;
247   struct GNUNET_BIO_ReadHandle *fileR;
248
249   fn = get_data_filename (cfg, PS_METADATA_DIR, nsid);
250   GNUNET_assert (fn != NULL);
251   if (GNUNET_YES !=
252       GNUNET_DISK_file_test (fn))
253   {
254     GNUNET_free (fn);
255     return GNUNET_SYSERR;
256   }
257   fileR = GNUNET_BIO_read_open (fn);
258   if (fileR == NULL)
259   {
260     GNUNET_free (fn);
261     return GNUNET_SYSERR;
262   }
263   emsg = NULL;
264   *ns_name = NULL;
265   if ((GNUNET_OK != GNUNET_BIO_read_int32 (fileR, ranking)) ||
266       (GNUNET_OK !=
267        GNUNET_BIO_read_string (fileR, "Read string error!", ns_name, 200)) ||
268       (GNUNET_OK !=
269        GNUNET_BIO_read_meta_data (fileR, "Read meta data error!", meta)))
270   {
271     (void) GNUNET_BIO_read_close (fileR, &emsg);
272     GNUNET_free_non_null (emsg);
273     GNUNET_free_non_null (*ns_name);
274     *ns_name = NULL;
275     GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
276     GNUNET_free (fn);
277     return GNUNET_SYSERR;
278   }
279   if (GNUNET_OK != GNUNET_BIO_read_close (fileR, &emsg))
280   {
281     LOG (GNUNET_ERROR_TYPE_WARNING,
282          _("Failed to parse metadata about pseudonym from file `%s': %s\n"), fn,
283          emsg);
284     GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
285     GNUNET_CONTAINER_meta_data_destroy (*meta);
286     *meta = NULL;
287     GNUNET_free_non_null (*ns_name);
288     *ns_name = NULL;
289     GNUNET_free_non_null (emsg);
290     GNUNET_free (fn);
291     return GNUNET_SYSERR;
292   }
293   GNUNET_free (fn);
294   return GNUNET_OK;
295 }
296
297
298 /**
299  * Return unique variant of the namespace name.
300  * Use it after GNUNET_PSEUDONYM_get_info() to make sure
301  * that name is unique.
302  *
303  * @param cfg configuration
304  * @param nsid cryptographic ID of the namespace
305  * @param name name to uniquify
306  * @param suffix if not NULL, filled with the suffix value
307  * @return NULL on failure (should never happen), name on success.
308  *         Free the name with GNUNET_free().
309  */
310 char *
311 GNUNET_PSEUDONYM_name_uniquify (const struct GNUNET_CONFIGURATION_Handle *cfg,
312     const struct GNUNET_HashCode * nsid, const char *name, unsigned int *suffix)
313 {
314   struct GNUNET_HashCode nh;
315   uint64_t len;
316   char *fn;
317   struct GNUNET_DISK_FileHandle *fh;
318   unsigned int i;
319   unsigned int idx;
320   char *ret;
321   struct stat sbuf;
322
323   GNUNET_CRYPTO_hash (name, strlen (name), &nh);
324   fn = get_data_filename (cfg, PS_NAMES_DIR, &nh);
325   GNUNET_assert (fn != NULL);
326
327   len = 0;
328   if (0 == STAT (fn, &sbuf))
329     GNUNET_break (GNUNET_OK == GNUNET_DISK_file_size (fn, &len, GNUNET_YES, GNUNET_YES));
330   fh = GNUNET_DISK_file_open (fn,
331                               GNUNET_DISK_OPEN_CREATE |
332                               GNUNET_DISK_OPEN_READWRITE,
333                               GNUNET_DISK_PERM_USER_READ |
334                               GNUNET_DISK_PERM_USER_WRITE);
335   i = 0;
336   idx = -1;
337   while ((len >= sizeof (struct GNUNET_HashCode)) &&
338          (sizeof (struct GNUNET_HashCode) ==
339           GNUNET_DISK_file_read (fh, &nh, sizeof (struct GNUNET_HashCode))))
340   {
341     if (0 == memcmp (&nh, nsid, sizeof (struct GNUNET_HashCode)))
342     {
343       idx = i;
344       break;
345     }
346     i++;
347     len -= sizeof (struct GNUNET_HashCode);
348   }
349   if (idx == -1)
350   {
351     idx = i;
352     if (sizeof (struct GNUNET_HashCode) !=
353         GNUNET_DISK_file_write (fh, nsid, sizeof (struct GNUNET_HashCode)))
354       LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "write", fn);
355   }
356   GNUNET_DISK_file_close (fh);
357   ret = GNUNET_malloc (strlen (name) + 32);
358   GNUNET_snprintf (ret, strlen (name) + 32, "%s-%u", name, idx);
359   if (suffix != NULL)
360     *suffix = idx;
361   GNUNET_free (fn);
362   return ret;
363 }
364
365 /**
366  * Get namespace name, metadata and rank
367  * This is a wrapper around internal read_info() call, and ensures that
368  * returned data is not invalid (not NULL).
369  *
370  * @param cfg configuration
371  * @param nsid cryptographic ID of the namespace
372  * @param ret_meta a location to store metadata pointer. NULL, if metadata
373  *        is not needed. Destroy with GNUNET_CONTAINER_meta_data_destroy().
374  * @param ret_rank a location to store rank. NULL, if rank not needed.
375  * @param ret_name a location to store human-readable name. Name is not unique.
376  *        NULL, if name is not needed. Free with GNUNET_free().
377  * @param name_is_a_dup is set to GNUNET_YES, if ret_name was filled with
378  *        a duplicate of a "no-name" placeholder
379  * @return GNUNET_OK on success. GNUENT_SYSERR if the data was
380  *         unobtainable (in that case ret_* are filled with placeholders - 
381  *         empty metadata container, rank -1 and a "no-name" name).
382  */
383 int
384 GNUNET_PSEUDONYM_get_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
385     const struct GNUNET_HashCode * nsid, struct GNUNET_CONTAINER_MetaData **ret_meta,
386     int32_t *ret_rank, char **ret_name, int *name_is_a_dup)
387 {
388   struct GNUNET_CONTAINER_MetaData *meta;
389   char *name;
390   int32_t rank = -1;
391
392   meta = NULL;
393   name = NULL;
394   if (GNUNET_OK == read_info (cfg, nsid, &meta, &rank, &name))
395   {
396     if ((meta != NULL) && (name == NULL))
397       name =
398           GNUNET_CONTAINER_meta_data_get_first_by_types (meta,
399                                                          EXTRACTOR_METATYPE_TITLE,
400                                                          EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME,
401                                                          EXTRACTOR_METATYPE_FILENAME,
402                                                          EXTRACTOR_METATYPE_DESCRIPTION,
403                                                          EXTRACTOR_METATYPE_SUBJECT,
404                                                          EXTRACTOR_METATYPE_PUBLISHER,
405                                                          EXTRACTOR_METATYPE_AUTHOR_NAME,
406                                                          EXTRACTOR_METATYPE_COMMENT,
407                                                          EXTRACTOR_METATYPE_SUMMARY,
408                                                          -1);
409     if (ret_name != NULL)
410     {
411       if (name == NULL)
412       {
413         name = GNUNET_strdup (_("no-name"));
414         if (name_is_a_dup != NULL)
415           *name_is_a_dup = GNUNET_YES;
416       }
417       else if (name_is_a_dup != NULL)
418         *name_is_a_dup = GNUNET_NO;
419       *ret_name = name;
420     }
421     else if (name != NULL)
422       GNUNET_free (name);
423
424     if (ret_meta != NULL)
425     {
426       if (meta == NULL)
427         meta = GNUNET_CONTAINER_meta_data_create ();
428       *ret_meta = meta;
429     }
430     else if (meta != NULL)
431       GNUNET_CONTAINER_meta_data_destroy (meta);
432
433     if (ret_rank != NULL)
434       *ret_rank = rank;
435
436     return GNUNET_OK;
437   }
438   if (ret_name != NULL)
439     *ret_name = GNUNET_strdup (_("no-name"));
440   if (ret_meta != NULL)
441     *ret_meta = GNUNET_CONTAINER_meta_data_create ();
442   if (ret_rank != NULL)
443     *ret_rank = -1;
444   if (name_is_a_dup != NULL)
445     *name_is_a_dup = GNUNET_YES;
446   return GNUNET_SYSERR;
447 }
448
449 /**
450  * Get the namespace ID belonging to the given namespace name.
451  *
452  * @param cfg configuration to use
453  * @param ns_uname unique (!) human-readable name for the namespace
454  * @param nsid set to namespace ID based on 'ns_uname'
455  * @return GNUNET_OK on success, GNUNET_SYSERR on failure
456  */
457 int
458 GNUNET_PSEUDONYM_name_to_id (const struct GNUNET_CONFIGURATION_Handle *cfg,
459     const char *ns_uname, struct GNUNET_HashCode * nsid)
460 {
461   size_t slen;
462   uint64_t len;
463   unsigned int idx;
464   char *name;
465   struct GNUNET_HashCode nh;
466   char *fn;
467   struct GNUNET_DISK_FileHandle *fh;
468
469   idx = -1;
470   slen = strlen (ns_uname);
471   while ((slen > 0) && (1 != SSCANF (&ns_uname[slen - 1], "-%u", &idx)))
472     slen--;
473   if (slen == 0)
474     return GNUNET_SYSERR;
475   name = GNUNET_strdup (ns_uname);
476   name[slen - 1] = '\0';
477
478   GNUNET_CRYPTO_hash (name, strlen (name), &nh);
479   GNUNET_free (name);
480   fn = get_data_filename (cfg, PS_NAMES_DIR, &nh);
481   GNUNET_assert (fn != NULL);
482
483   if ((GNUNET_OK != GNUNET_DISK_file_test (fn) ||
484        (GNUNET_OK != GNUNET_DISK_file_size (fn, &len, GNUNET_YES, GNUNET_YES))) ||
485       ((idx + 1) * sizeof (struct GNUNET_HashCode) > len))
486   {
487     GNUNET_free (fn);
488     return GNUNET_SYSERR;
489   }
490   fh = GNUNET_DISK_file_open (fn,
491                               GNUNET_DISK_OPEN_CREATE |
492                               GNUNET_DISK_OPEN_READWRITE,
493                               GNUNET_DISK_PERM_USER_READ |
494                               GNUNET_DISK_PERM_USER_WRITE);
495   GNUNET_free (fn);
496   if (GNUNET_SYSERR ==
497       GNUNET_DISK_file_seek (fh, idx * sizeof (struct GNUNET_HashCode),
498                              GNUNET_DISK_SEEK_SET))
499   {
500     GNUNET_DISK_file_close (fh);
501     return GNUNET_SYSERR;
502   }
503   if (sizeof (struct GNUNET_HashCode) !=
504       GNUNET_DISK_file_read (fh, nsid, sizeof (struct GNUNET_HashCode)))
505   {
506     GNUNET_DISK_file_close (fh);
507     return GNUNET_SYSERR;
508   }
509   GNUNET_DISK_file_close (fh);
510   return GNUNET_OK;
511 }
512
513
514
515 /**
516  * struct used to list the pseudonym
517  */
518 struct ListPseudonymClosure
519 {
520
521   /**
522    * iterator over pseudonym
523    */
524   GNUNET_PSEUDONYM_Iterator iterator;
525
526   /**
527    * Closure for iterator.
528    */
529   void *closure;
530
531   /**
532    * Configuration to use.
533    */
534   const struct GNUNET_CONFIGURATION_Handle *cfg;
535 };
536
537
538
539 /**
540  * the help function to list all available pseudonyms
541  * @param cls point to a struct ListPseudonymClosure
542  * @param fullname name of pseudonym
543  */
544 static int
545 list_pseudonym_helper (void *cls, const char *fullname)
546 {
547   struct ListPseudonymClosure *c = cls;
548   int ret;
549   struct GNUNET_HashCode id;
550   int32_t rating;
551   struct GNUNET_CONTAINER_MetaData *meta;
552   const char *fn;
553   char *str;
554   char *name_unique;
555
556   if (strlen (fullname) < sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded))
557     return GNUNET_OK;
558   fn = &fullname[strlen (fullname) + 1 -
559                  sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)];
560   if (fn[-1] != DIR_SEPARATOR)
561     return GNUNET_OK;
562   ret = GNUNET_OK;
563   if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (fn, &id))
564     return GNUNET_OK;           /* invalid name */
565   str = NULL;
566   if (GNUNET_OK != GNUNET_PSEUDONYM_get_info (c->cfg, &id, &meta, &rating,
567       &str, NULL))
568   {
569     /* ignore entry. FIXME: Why? Lack of data about a pseudonym is not a reason
570      * to ignore it... So yeah, it will have placeholders instead of name,
571      * empty metadata container and a default rank == -1, so what? We know
572      * its nsid - that's all we really need. Right? */
573     GNUNET_free (str);
574     GNUNET_CONTAINER_meta_data_destroy (meta);
575     return GNUNET_OK;
576   }
577   name_unique = GNUNET_PSEUDONYM_name_uniquify (c->cfg, &id, str, NULL);
578   if (c->iterator != NULL)
579     ret = c->iterator (c->closure, &id, str, name_unique, meta, rating);
580   GNUNET_free_non_null (str);
581   GNUNET_free_non_null (name_unique);
582   GNUNET_CONTAINER_meta_data_destroy (meta);
583   return ret;
584 }
585
586
587 /**
588  * List all available pseudonyms.
589  *
590  * @param cfg overall configuration
591  * @param iterator function to call for each pseudonym
592  * @param closure closure for iterator
593  * @return number of pseudonyms found
594  */
595 int
596 GNUNET_PSEUDONYM_list_all (const struct GNUNET_CONFIGURATION_Handle *cfg,
597                            GNUNET_PSEUDONYM_Iterator iterator, void *closure)
598 {
599   struct ListPseudonymClosure cls;
600   char *fn;
601   int ret;
602
603   cls.iterator = iterator;
604   cls.closure = closure;
605   cls.cfg = cfg;
606   fn = get_data_filename (cfg, PS_METADATA_DIR, NULL);
607   GNUNET_assert (fn != NULL);
608   GNUNET_DISK_directory_create (fn);
609   ret = GNUNET_DISK_directory_scan (fn, &list_pseudonym_helper, &cls);
610   GNUNET_free (fn);
611   return ret;
612 }
613
614
615 /**
616  * Change the ranking of a pseudonym.
617  *
618  * @param cfg overall configuration
619  * @param nsid id of the pseudonym
620  * @param delta by how much should the rating be
621  *  changed?
622  * @return new rating of the pseudonym
623  */
624 int
625 GNUNET_PSEUDONYM_rank (const struct GNUNET_CONFIGURATION_Handle *cfg,
626                        const struct GNUNET_HashCode * nsid, int delta)
627 {
628   struct GNUNET_CONTAINER_MetaData *meta;
629   int ret;
630   int32_t ranking;
631   char *name;
632
633   name = NULL;
634   ret = read_info (cfg, nsid, &meta, &ranking, &name);
635   if (ret == GNUNET_SYSERR)
636   {
637     ranking = 0;
638     meta = GNUNET_CONTAINER_meta_data_create ();
639   }
640   ranking += delta;
641   write_pseudonym_info (cfg, nsid, meta, ranking, name);
642   GNUNET_CONTAINER_meta_data_destroy (meta);
643   GNUNET_free_non_null (name);
644   return ranking;
645 }
646
647
648 /**
649  * Set the pseudonym metadata, rank and name.
650  *
651  * @param cfg overall configuration
652  * @param nsid id of the pseudonym
653  * @param name name to set. Must be the non-unique version of it.
654  *        May be NULL, in which case it erases pseudonym's name!
655  * @param md metadata to set
656  *        May be NULL, in which case it erases pseudonym's metadata!
657  * @param rank rank to assign
658  * @return GNUNET_OK on success, GNUNET_SYSERR on failure
659  */
660 int
661 GNUNET_PSEUDONYM_set_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
662     const struct GNUNET_HashCode * nsid, const char *name,
663     const struct GNUNET_CONTAINER_MetaData *md, int rank)
664 {
665   GNUNET_assert (cfg != NULL);
666   GNUNET_assert (nsid != NULL);
667
668   write_pseudonym_info (cfg, nsid, md, rank, name);
669   return GNUNET_OK;
670 }
671
672
673 /**
674  * Add a pseudonym to the set of known pseudonyms.
675  * For all pseudonym advertisements that we discover
676  * FS should automatically call this function.
677  *
678  * @param cfg overall configuration
679  * @param id the pseudonym identifier
680  * @param meta metadata for the pseudonym
681  */
682 void
683 GNUNET_PSEUDONYM_add (const struct GNUNET_CONFIGURATION_Handle *cfg,
684                       const struct GNUNET_HashCode * id,
685                       const struct GNUNET_CONTAINER_MetaData *meta)
686 {
687   char *name;
688   int32_t ranking;
689   struct GNUNET_CONTAINER_MetaData *old;
690   char *fn;
691   struct stat sbuf;
692
693   ranking = 0;
694   fn = get_data_filename (cfg, PS_METADATA_DIR, id);
695   GNUNET_assert (fn != NULL);
696
697   if ((0 == STAT (fn, &sbuf)) &&
698       (GNUNET_OK == read_info (cfg, id, &old, &ranking, &name)))
699   {
700     GNUNET_CONTAINER_meta_data_merge (old, meta);
701     write_pseudonym_info (cfg, id, old, ranking, name);
702     GNUNET_CONTAINER_meta_data_destroy (old);
703     GNUNET_free_non_null (name);
704   }
705   else
706   {
707     write_pseudonym_info (cfg, id, meta, ranking, NULL);
708   }
709   GNUNET_free (fn);
710   internal_notify (id, meta, ranking);
711 }
712
713
714 /* end of pseudonym.c */