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