-better time conversion code
[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   fileR = GNUNET_BIO_read_open (fn);
252   if (fileR == NULL)
253   {
254     GNUNET_free (fn);
255     return GNUNET_SYSERR;
256   }
257   emsg = NULL;
258   *ns_name = NULL;
259   if ((GNUNET_OK != GNUNET_BIO_read_int32 (fileR, ranking)) ||
260       (GNUNET_OK !=
261        GNUNET_BIO_read_string (fileR, "Read string error!", ns_name, 200)) ||
262       (GNUNET_OK !=
263        GNUNET_BIO_read_meta_data (fileR, "Read meta data error!", meta)))
264   {
265     (void) GNUNET_BIO_read_close (fileR, &emsg);
266     GNUNET_free_non_null (emsg);
267     GNUNET_free_non_null (*ns_name);
268     *ns_name = NULL;
269     GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
270     GNUNET_free (fn);
271     return GNUNET_SYSERR;
272   }
273   if (GNUNET_OK != GNUNET_BIO_read_close (fileR, &emsg))
274   {
275     LOG (GNUNET_ERROR_TYPE_WARNING,
276          _("Failed to parse metadata about pseudonym from file `%s': %s\n"), fn,
277          emsg);
278     GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_remove (fn));
279     GNUNET_CONTAINER_meta_data_destroy (*meta);
280     *meta = NULL;
281     GNUNET_free_non_null (*ns_name);
282     *ns_name = NULL;
283     GNUNET_free_non_null (emsg);
284     GNUNET_free (fn);
285     return GNUNET_SYSERR;
286   }
287   GNUNET_free (fn);
288   return GNUNET_OK;
289 }
290
291
292 /**
293  * Return unique variant of the namespace name.
294  * Use it after GNUNET_PSEUDONYM_get_info() to make sure
295  * that name is unique.
296  *
297  * @param cfg configuration
298  * @param nsid cryptographic ID of the namespace
299  * @param name name to uniquify
300  * @param suffix if not NULL, filled with the suffix value
301  * @return NULL on failure (should never happen), name on success.
302  *         Free the name with GNUNET_free().
303  */
304 char *
305 GNUNET_PSEUDONYM_name_uniquify (const struct GNUNET_CONFIGURATION_Handle *cfg,
306     const struct GNUNET_HashCode * nsid, const char *name, unsigned int *suffix)
307 {
308   struct GNUNET_HashCode nh;
309   uint64_t len;
310   char *fn;
311   struct GNUNET_DISK_FileHandle *fh;
312   unsigned int i;
313   unsigned int idx;
314   char *ret;
315   struct stat sbuf;
316
317   GNUNET_CRYPTO_hash (name, strlen (name), &nh);
318   fn = get_data_filename (cfg, PS_NAMES_DIR, &nh);
319   GNUNET_assert (fn != NULL);
320
321   len = 0;
322   if (0 == STAT (fn, &sbuf))
323     GNUNET_break (GNUNET_OK == GNUNET_DISK_file_size (fn, &len, GNUNET_YES, GNUNET_YES));
324   fh = GNUNET_DISK_file_open (fn,
325                               GNUNET_DISK_OPEN_CREATE |
326                               GNUNET_DISK_OPEN_READWRITE,
327                               GNUNET_DISK_PERM_USER_READ |
328                               GNUNET_DISK_PERM_USER_WRITE);
329   i = 0;
330   idx = -1;
331   while ((len >= sizeof (struct GNUNET_HashCode)) &&
332          (sizeof (struct GNUNET_HashCode) ==
333           GNUNET_DISK_file_read (fh, &nh, sizeof (struct GNUNET_HashCode))))
334   {
335     if (0 == memcmp (&nh, nsid, sizeof (struct GNUNET_HashCode)))
336     {
337       idx = i;
338       break;
339     }
340     i++;
341     len -= sizeof (struct GNUNET_HashCode);
342   }
343   if (idx == -1)
344   {
345     idx = i;
346     if (sizeof (struct GNUNET_HashCode) !=
347         GNUNET_DISK_file_write (fh, nsid, sizeof (struct GNUNET_HashCode)))
348       LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "write", fn);
349   }
350   GNUNET_DISK_file_close (fh);
351   ret = GNUNET_malloc (strlen (name) + 32);
352   GNUNET_snprintf (ret, strlen (name) + 32, "%s-%u", name, idx);
353   if (suffix != NULL)
354     *suffix = idx;
355   GNUNET_free (fn);
356   return ret;
357 }
358
359 /**
360  * Get namespace name, metadata and rank
361  * This is a wrapper around internal read_info() call, and ensures that
362  * returned data is not invalid (not NULL).
363  *
364  * @param cfg configuration
365  * @param nsid cryptographic ID of the namespace
366  * @param ret_meta a location to store metadata pointer. NULL, if metadata
367  *        is not needed. Destroy with GNUNET_CONTAINER_meta_data_destroy().
368  * @param ret_rank a location to store rank. NULL, if rank not needed.
369  * @param ret_name a location to store human-readable name. Name is not unique.
370  *        NULL, if name is not needed. Free with GNUNET_free().
371  * @param name_is_a_dup is set to GNUNET_YES, if ret_name was filled with
372  *        a duplicate of a "no-name" placeholder
373  * @return GNUNET_OK on success. GNUENT_SYSERR if the data was
374  *         unobtainable (in that case ret_* are filled with placeholders - 
375  *         empty metadata container, rank -1 and a "no-name" name).
376  */
377 int
378 GNUNET_PSEUDONYM_get_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
379     const struct GNUNET_HashCode * nsid, struct GNUNET_CONTAINER_MetaData **ret_meta,
380     int32_t *ret_rank, char **ret_name, int *name_is_a_dup)
381 {
382   struct GNUNET_CONTAINER_MetaData *meta;
383   char *name;
384   int32_t rank = -1;
385
386   meta = NULL;
387   name = NULL;
388   if (GNUNET_OK == read_info (cfg, nsid, &meta, &rank, &name))
389   {
390     if ((meta != NULL) && (name == NULL))
391       name =
392           GNUNET_CONTAINER_meta_data_get_first_by_types (meta,
393                                                          EXTRACTOR_METATYPE_TITLE,
394                                                          EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME,
395                                                          EXTRACTOR_METATYPE_FILENAME,
396                                                          EXTRACTOR_METATYPE_DESCRIPTION,
397                                                          EXTRACTOR_METATYPE_SUBJECT,
398                                                          EXTRACTOR_METATYPE_PUBLISHER,
399                                                          EXTRACTOR_METATYPE_AUTHOR_NAME,
400                                                          EXTRACTOR_METATYPE_COMMENT,
401                                                          EXTRACTOR_METATYPE_SUMMARY,
402                                                          -1);
403     if (ret_name != NULL)
404     {
405       if (name == NULL)
406       {
407         name = GNUNET_strdup (_("no-name"));
408         if (name_is_a_dup != NULL)
409           *name_is_a_dup = GNUNET_YES;
410       }
411       else if (name_is_a_dup != NULL)
412         *name_is_a_dup = GNUNET_NO;
413       *ret_name = name;
414     }
415     else if (name != NULL)
416       GNUNET_free (name);
417
418     if (ret_meta != NULL)
419     {
420       if (meta == NULL)
421         meta = GNUNET_CONTAINER_meta_data_create ();
422       *ret_meta = meta;
423     }
424     else if (meta != NULL)
425       GNUNET_CONTAINER_meta_data_destroy (meta);
426
427     if (ret_rank != NULL)
428       *ret_rank = rank;
429
430     return GNUNET_OK;
431   }
432   if (ret_name != NULL)
433     *ret_name = GNUNET_strdup (_("no-name"));
434   if (ret_meta != NULL)
435     *ret_meta = GNUNET_CONTAINER_meta_data_create ();
436   if (ret_rank != NULL)
437     *ret_rank = -1;
438   if (name_is_a_dup != NULL)
439     *name_is_a_dup = GNUNET_YES;
440   return GNUNET_SYSERR;
441 }
442
443 /**
444  * Get the namespace ID belonging to the given namespace name.
445  *
446  * @param cfg configuration to use
447  * @param ns_uname unique (!) human-readable name for the namespace
448  * @param nsid set to namespace ID based on 'ns_uname'
449  * @return GNUNET_OK on success, GNUNET_SYSERR on failure
450  */
451 int
452 GNUNET_PSEUDONYM_name_to_id (const struct GNUNET_CONFIGURATION_Handle *cfg,
453     const char *ns_uname, struct GNUNET_HashCode * nsid)
454 {
455   size_t slen;
456   uint64_t len;
457   unsigned int idx;
458   char *name;
459   struct GNUNET_HashCode nh;
460   char *fn;
461   struct GNUNET_DISK_FileHandle *fh;
462
463   idx = -1;
464   slen = strlen (ns_uname);
465   while ((slen > 0) && (1 != SSCANF (&ns_uname[slen - 1], "-%u", &idx)))
466     slen--;
467   if (slen == 0)
468     return GNUNET_SYSERR;
469   name = GNUNET_strdup (ns_uname);
470   name[slen - 1] = '\0';
471
472   GNUNET_CRYPTO_hash (name, strlen (name), &nh);
473   GNUNET_free (name);
474   fn = get_data_filename (cfg, PS_NAMES_DIR, &nh);
475   GNUNET_assert (fn != NULL);
476
477   if ((GNUNET_OK != GNUNET_DISK_file_test (fn) ||
478        (GNUNET_OK != GNUNET_DISK_file_size (fn, &len, GNUNET_YES, GNUNET_YES))) ||
479       ((idx + 1) * sizeof (struct GNUNET_HashCode) > len))
480   {
481     GNUNET_free (fn);
482     return GNUNET_SYSERR;
483   }
484   fh = GNUNET_DISK_file_open (fn,
485                               GNUNET_DISK_OPEN_CREATE |
486                               GNUNET_DISK_OPEN_READWRITE,
487                               GNUNET_DISK_PERM_USER_READ |
488                               GNUNET_DISK_PERM_USER_WRITE);
489   GNUNET_free (fn);
490   GNUNET_DISK_file_seek (fh, idx * sizeof (struct GNUNET_HashCode),
491                          GNUNET_DISK_SEEK_SET);
492   if (sizeof (struct GNUNET_HashCode) !=
493       GNUNET_DISK_file_read (fh, nsid, sizeof (struct GNUNET_HashCode)))
494   {
495     GNUNET_DISK_file_close (fh);
496     return GNUNET_SYSERR;
497   }
498   GNUNET_DISK_file_close (fh);
499   return GNUNET_OK;
500 }
501
502
503
504 /**
505  * struct used to list the pseudonym
506  */
507 struct ListPseudonymClosure
508 {
509
510   /**
511    * iterator over pseudonym
512    */
513   GNUNET_PSEUDONYM_Iterator iterator;
514
515   /**
516    * Closure for iterator.
517    */
518   void *closure;
519
520   /**
521    * Configuration to use.
522    */
523   const struct GNUNET_CONFIGURATION_Handle *cfg;
524 };
525
526
527
528 /**
529  * the help function to list all available pseudonyms
530  * @param cls point to a struct ListPseudonymClosure
531  * @param fullname name of pseudonym
532  */
533 static int
534 list_pseudonym_helper (void *cls, const char *fullname)
535 {
536   struct ListPseudonymClosure *c = cls;
537   int ret;
538   struct GNUNET_HashCode id;
539   int32_t rating;
540   struct GNUNET_CONTAINER_MetaData *meta;
541   const char *fn;
542   char *str;
543   char *name_unique;
544
545   if (strlen (fullname) < sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded))
546     return GNUNET_OK;
547   fn = &fullname[strlen (fullname) + 1 -
548                  sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)];
549   if (fn[-1] != DIR_SEPARATOR)
550     return GNUNET_OK;
551   ret = GNUNET_OK;
552   if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (fn, &id))
553     return GNUNET_OK;           /* invalid name */
554   str = NULL;
555   if (GNUNET_OK != GNUNET_PSEUDONYM_get_info (c->cfg, &id, &meta, &rating,
556       &str, NULL))
557   {
558     /* ignore entry. FIXME: Why? Lack of data about a pseudonym is not a reason
559      * to ignore it... So yeah, it will have placeholders instead of name,
560      * empty metadata container and a default rank == -1, so what? We know
561      * its nsid - that's all we really need. Right? */
562     GNUNET_free (str);
563     GNUNET_CONTAINER_meta_data_destroy (meta);
564     return GNUNET_OK;
565   }
566   name_unique = GNUNET_PSEUDONYM_name_uniquify (c->cfg, &id, str, NULL);
567   if (c->iterator != NULL)
568     ret = c->iterator (c->closure, &id, str, name_unique, meta, rating);
569   GNUNET_free_non_null (str);
570   GNUNET_free_non_null (name_unique);
571   GNUNET_CONTAINER_meta_data_destroy (meta);
572   return ret;
573 }
574
575
576 /**
577  * List all available pseudonyms.
578  *
579  * @param cfg overall configuration
580  * @param iterator function to call for each pseudonym
581  * @param closure closure for iterator
582  * @return number of pseudonyms found
583  */
584 int
585 GNUNET_PSEUDONYM_list_all (const struct GNUNET_CONFIGURATION_Handle *cfg,
586                            GNUNET_PSEUDONYM_Iterator iterator, void *closure)
587 {
588   struct ListPseudonymClosure cls;
589   char *fn;
590   int ret;
591
592   cls.iterator = iterator;
593   cls.closure = closure;
594   cls.cfg = cfg;
595   fn = get_data_filename (cfg, PS_METADATA_DIR, NULL);
596   GNUNET_assert (fn != NULL);
597   GNUNET_DISK_directory_create (fn);
598   ret = GNUNET_DISK_directory_scan (fn, &list_pseudonym_helper, &cls);
599   GNUNET_free (fn);
600   return ret;
601 }
602
603
604 /**
605  * Change the ranking of a pseudonym.
606  *
607  * @param cfg overall configuration
608  * @param nsid id of the pseudonym
609  * @param delta by how much should the rating be
610  *  changed?
611  * @return new rating of the pseudonym
612  */
613 int
614 GNUNET_PSEUDONYM_rank (const struct GNUNET_CONFIGURATION_Handle *cfg,
615                        const struct GNUNET_HashCode * nsid, int delta)
616 {
617   struct GNUNET_CONTAINER_MetaData *meta;
618   int ret;
619   int32_t ranking;
620   char *name;
621
622   name = NULL;
623   ret = read_info (cfg, nsid, &meta, &ranking, &name);
624   if (ret == GNUNET_SYSERR)
625   {
626     ranking = 0;
627     meta = GNUNET_CONTAINER_meta_data_create ();
628   }
629   ranking += delta;
630   write_pseudonym_info (cfg, nsid, meta, ranking, name);
631   GNUNET_CONTAINER_meta_data_destroy (meta);
632   GNUNET_free_non_null (name);
633   return ranking;
634 }
635
636
637 /**
638  * Set the pseudonym metadata, rank and name.
639  *
640  * @param cfg overall configuration
641  * @param nsid id of the pseudonym
642  * @param name name to set. Must be the non-unique version of it.
643  *        May be NULL, in which case it erases pseudonym's name!
644  * @param md metadata to set
645  *        May be NULL, in which case it erases pseudonym's metadata!
646  * @param rank rank to assign
647  * @return GNUNET_OK on success, GNUNET_SYSERR on failure
648  */
649 int
650 GNUNET_PSEUDONYM_set_info (const struct GNUNET_CONFIGURATION_Handle *cfg,
651     const struct GNUNET_HashCode * nsid, const char *name,
652     const struct GNUNET_CONTAINER_MetaData *md, int rank)
653 {
654   GNUNET_assert (cfg != NULL);
655   GNUNET_assert (nsid != NULL);
656
657   write_pseudonym_info (cfg, nsid, md, rank, name);
658   return GNUNET_OK;
659 }
660
661
662 /**
663  * Add a pseudonym to the set of known pseudonyms.
664  * For all pseudonym advertisements that we discover
665  * FS should automatically call this function.
666  *
667  * @param cfg overall configuration
668  * @param id the pseudonym identifier
669  * @param meta metadata for the pseudonym
670  */
671 void
672 GNUNET_PSEUDONYM_add (const struct GNUNET_CONFIGURATION_Handle *cfg,
673                       const struct GNUNET_HashCode * id,
674                       const struct GNUNET_CONTAINER_MetaData *meta)
675 {
676   char *name;
677   int32_t ranking;
678   struct GNUNET_CONTAINER_MetaData *old;
679   char *fn;
680   struct stat sbuf;
681
682   ranking = 0;
683   fn = get_data_filename (cfg, PS_METADATA_DIR, id);
684   GNUNET_assert (fn != NULL);
685
686   if ((0 == STAT (fn, &sbuf)) &&
687       (GNUNET_OK == read_info (cfg, id, &old, &ranking, &name)))
688   {
689     GNUNET_CONTAINER_meta_data_merge (old, meta);
690     write_pseudonym_info (cfg, id, old, ranking, name);
691     GNUNET_CONTAINER_meta_data_destroy (old);
692     GNUNET_free_non_null (name);
693   }
694   else
695   {
696     write_pseudonym_info (cfg, id, meta, ranking, NULL);
697   }
698   GNUNET_free (fn);
699   internal_notify (id, meta, ranking);
700 }
701
702
703 /* end of pseudonym.c */