a110822d12517817fd586bdb22ac8f2d26b417bf
[oweals/gnunet.git] / src / fs / fs_file_information.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 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 fs/fs_file_information.c
23  * @brief  Manage information for publishing directory hierarchies
24  * @author Christian Grothoff
25  *
26  * TODO:
27  * - serialization/deserialization (& deserialization API)
28  * - metadata filename clean up code
29  * - metadata/ksk generation for directories from contained files
30  */
31 #include "platform.h"
32 #include <extractor.h>
33 #include "gnunet_fs_service.h"
34 #include "fs.h"
35 #include "fs_tree.h"
36
37
38 /**
39  * Create a temporary file on disk to store the current
40  * state of "fi" in.
41  *
42  * @param fi file information to sync with disk
43  */
44 void
45 GNUNET_FS_file_information_sync (struct GNUNET_FS_FileInformation * fi)
46 {
47   if (NULL == fi->serialization)
48     {
49       fi->serialization = NULL; // FIXME -- need cfg!
50     }
51   // FIXME...
52 }
53
54
55 /**
56  * Load file information from the file to which
57  * it was sync'ed.
58  *
59  * @param fn name of the file to use
60  * @return NULL on error
61  */
62 struct GNUNET_FS_FileInformation *
63 GNUNET_FS_file_information_recover (const char *fn)
64 {
65   struct GNUNET_FS_FileInformation *ret;
66   ret = NULL;
67   // FIXME!
68   return ret;
69 }
70
71
72 /**
73  * Obtain the name under which this file information
74  * structure is stored on disk.  Only works for top-level
75  * file information structures.
76  *
77  * @param s structure to get the filename for
78  * @return NULL on error, otherwise filename that
79  *         can be passed to "GNUNET_FS_file_information_recover"
80  *         to read this fi-struct from disk.
81  */
82 const char *
83 GNUNET_FS_file_information_get_id (struct GNUNET_FS_FileInformation *s)
84 {
85   if (NULL != s->dir)
86     return NULL;
87   return s->serialization;
88 }
89
90
91 /**
92  * Closure for "data_reader_file".
93  */
94 struct FileInfo
95 {
96   /**
97    * Name of the file to read.
98    */
99   char *filename;
100
101   /**
102    * File descriptor, NULL if it has not yet been opened.
103    */
104   struct GNUNET_DISK_FileHandle *fd;
105 };
106
107
108 /**
109  * Function that provides data by reading from a file.
110  *
111  * @param cls closure (points to the file information)
112  * @param offset offset to read from; it is possible
113  *            that the caller might need to go backwards
114  *            a bit at times
115  * @param max maximum number of bytes that should be 
116  *            copied to buf; readers are not allowed
117  *            to provide less data unless there is an error;
118  *            a value of "0" will be used at the end to allow
119  *            the reader to clean up its internal state
120  * @param buf where the reader should write the data
121  * @param emsg location for the reader to store an error message
122  * @return number of bytes written, usually "max", 0 on error
123  */
124 static size_t
125 data_reader_file(void *cls, 
126                  uint64_t offset,
127                  size_t max, 
128                  void *buf,
129                  char **emsg)
130 {
131   struct FileInfo *fi = cls;
132   ssize_t ret;
133
134   if (max == 0)
135     {
136       if (fi->fd != NULL)
137         GNUNET_DISK_file_close (fi->fd);
138       GNUNET_free (fi->filename);
139       GNUNET_free (fi);
140       return 0;
141     }  
142   if (fi->fd == NULL)
143     {
144       fi->fd = GNUNET_DISK_file_open (fi->filename,
145                                       GNUNET_DISK_OPEN_READ,
146                                       GNUNET_DISK_PERM_NONE);
147       if (fi->fd == NULL)
148         {
149           GNUNET_asprintf (emsg, 
150                            _("Could not open file `%s': %s"),
151                            fi->filename,
152                            STRERROR (errno));
153           return 0;
154         }
155     }
156   GNUNET_DISK_file_seek (fi->fd, offset, GNUNET_DISK_SEEK_SET);
157   ret = GNUNET_DISK_file_read (fi->fd, buf, max);
158   if (ret == -1)
159     {
160       GNUNET_asprintf (emsg, 
161                        _("Could not read file `%s': %s"),
162                        fi->filename,
163                        STRERROR (errno));
164       return 0;
165     }
166   if (ret != max)
167     {
168       GNUNET_asprintf (emsg, 
169                        _("Short read reading from file `%s'!"),
170                        fi->filename);
171       return 0;
172     }
173   return max;
174 }
175
176
177 /**
178  * Create an entry for a file in a publish-structure.
179  *
180  * @param client_info initial value for the client-info value for this entry
181  * @param filename name of the file or directory to publish
182  * @param keywords under which keywords should this file be available
183  *         directly; can be NULL
184  * @param meta metadata for the file
185  * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
186  *                GNUNET_SYSERR for simulation
187  * @param anonymity what is the desired anonymity level for sharing?
188  * @param priority what is the priority for OUR node to
189  *   keep this file available?  Use 0 for maximum anonymity and
190  *   minimum reliability...
191  * @param expirationTime when should this content expire?
192  * @return publish structure entry for the file
193  */
194 struct GNUNET_FS_FileInformation *
195 GNUNET_FS_file_information_create_from_file (void *client_info,
196                                              const char *filename,
197                                              const struct GNUNET_FS_Uri *keywords,
198                                              const struct GNUNET_CONTAINER_MetaData *meta,
199                                              int do_index,
200                                              uint32_t anonymity,
201                                              uint32_t priority,
202                                              struct GNUNET_TIME_Absolute expirationTime)
203 {
204   struct FileInfo *fi;
205   struct stat sbuf;
206   struct GNUNET_FS_FileInformation *ret;
207
208   if (0 != STAT (filename, &sbuf))
209     {
210       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
211                                 "stat",
212                                 filename);
213       return NULL;
214     }
215   fi = GNUNET_malloc (sizeof(struct FileInfo));
216   fi->filename = GNUNET_STRINGS_filename_expand (filename);
217   if (fi->filename == NULL)
218     {
219       GNUNET_free (fi);
220       return NULL;
221     }
222   ret = GNUNET_FS_file_information_create_from_reader (client_info,
223                                                        sbuf.st_size,
224                                                        &data_reader_file,
225                                                        fi,
226                                                        keywords,
227                                                        meta,
228                                                        do_index,
229                                                        anonymity,
230                                                        priority,
231                                                        expirationTime);
232   ret->data.file.filename = GNUNET_strdup (filename);
233   return ret;
234 }
235
236
237 /**
238  * Function that provides data by copying from a buffer.
239  *
240  * @param cls closure (points to the buffer)
241  * @param offset offset to read from; it is possible
242  *            that the caller might need to go backwards
243  *            a bit at times
244  * @param max maximum number of bytes that should be 
245  *            copied to buf; readers are not allowed
246  *            to provide less data unless there is an error;
247  *            a value of "0" will be used at the end to allow
248  *            the reader to clean up its internal state
249  * @param buf where the reader should write the data
250  * @param emsg location for the reader to store an error message
251  * @return number of bytes written, usually "max", 0 on error
252  */
253 static size_t
254 data_reader_copy(void *cls, 
255                  uint64_t offset,
256                  size_t max, 
257                  void *buf,
258                  char **emsg)
259 {
260   char *data = cls;
261
262   if (max == 0)
263     {
264       GNUNET_free (data);
265       return 0;
266     }  
267   memcpy (buf, &data[offset], max);
268   return max;
269 }
270
271
272 /**
273  * Create an entry for a file in a publish-structure.
274  *
275  * @param client_info initial value for the client-info value for this entry
276  * @param length length of the file
277  * @param data data for the file (should not be used afterwards by
278  *        the caller; callee will "free")
279  * @param keywords under which keywords should this file be available
280  *         directly; can be NULL
281  * @param meta metadata for the file
282  * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
283  *                GNUNET_SYSERR for simulation
284  * @param anonymity what is the desired anonymity level for sharing?
285  * @param priority what is the priority for OUR node to
286  *   keep this file available?  Use 0 for maximum anonymity and
287  *   minimum reliability...
288  * @param expirationTime when should this content expire?
289  * @return publish structure entry for the file
290  */
291 struct GNUNET_FS_FileInformation *
292 GNUNET_FS_file_information_create_from_data (void *client_info,
293                                              uint64_t length,
294                                              void *data,
295                                              const struct GNUNET_FS_Uri *keywords,
296                                              const struct GNUNET_CONTAINER_MetaData *meta,
297                                              int do_index,
298                                              uint32_t anonymity,
299                                              uint32_t priority,
300                                              struct GNUNET_TIME_Absolute expirationTime)
301 {
302   return GNUNET_FS_file_information_create_from_reader (client_info,
303                                                         length,
304                                                         &data_reader_copy,
305                                                         data,
306                                                         keywords,
307                                                         meta,
308                                                         do_index,
309                                                         anonymity,
310                                                         priority,
311                                                         expirationTime);
312 }
313
314
315 /**
316  * Create an entry for a file in a publish-structure.
317  *
318  * @param client_info initial value for the client-info value for this entry
319  * @param length length of the file
320  * @param reader function that can be used to obtain the data for the file 
321  * @param reader_cls closure for "reader"
322  * @param keywords under which keywords should this file be available
323  *         directly; can be NULL
324  * @param meta metadata for the file
325  * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
326  *                GNUNET_SYSERR for simulation
327  * @param anonymity what is the desired anonymity level for sharing?
328  * @param priority what is the priority for OUR node to
329  *   keep this file available?  Use 0 for maximum anonymity and
330  *   minimum reliability...
331  * @param expirationTime when should this content expire?
332  * @return publish structure entry for the file
333  */
334 struct GNUNET_FS_FileInformation *
335 GNUNET_FS_file_information_create_from_reader (void *client_info,
336                                                uint64_t length,
337                                                GNUNET_FS_DataReader reader,
338                                                void *reader_cls,
339                                                const struct GNUNET_FS_Uri *keywords,
340                                                const struct GNUNET_CONTAINER_MetaData *meta,
341                                                int do_index,
342                                                uint32_t anonymity,
343                                                uint32_t priority,
344                                                struct GNUNET_TIME_Absolute expirationTime)
345 {
346   struct GNUNET_FS_FileInformation *ret;
347
348   ret = GNUNET_malloc (sizeof (struct GNUNET_FS_FileInformation));
349   ret->client_info = client_info;  
350   ret->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
351   if (ret->meta == NULL)
352     ret->meta = GNUNET_CONTAINER_meta_data_create ();
353   ret->keywords = (keywords == NULL) ? NULL : GNUNET_FS_uri_dup (keywords);
354   ret->expirationTime = expirationTime;
355   ret->data.file.reader = reader; 
356   ret->data.file.reader_cls = reader_cls;
357   ret->data.file.do_index = do_index;
358   ret->data.file.file_size = length;
359   ret->anonymity = anonymity;
360   ret->priority = priority;
361   GNUNET_FS_file_information_sync (ret);
362   return ret;
363 }
364
365
366 /**
367  * Closure for "dir_scan_cb".
368  */
369 struct DirScanCls 
370 {
371   /**
372    * Metadata extractors to use.
373    */
374   struct EXTRACTOR_PluginList *extractors;
375
376   /**
377    * Function to call on each directory entry.
378    */
379   GNUNET_FS_FileProcessor proc;
380   
381   /**
382    * Closure for proc.
383    */
384   void *proc_cls;
385
386   /**
387    * Scanner to use for subdirectories.
388    */
389   GNUNET_FS_DirectoryScanner scanner;
390
391   /**
392    * Closure for scanner.
393    */
394   void *scanner_cls;
395
396   /**
397    * Set to an error message (if any).
398    */
399   char *emsg; 
400
401   /**
402    * Should files be indexed?
403    */ 
404   int do_index;
405
406   /**
407    * Desired anonymity level.
408    */
409   uint32_t anonymity;
410
411   /**
412    * Desired publishing priority.
413    */
414   uint32_t priority;
415
416   /**
417    * Expiration time for publication.
418    */
419   struct GNUNET_TIME_Absolute expiration;
420 };
421
422
423 /**
424  * Function called on each entry in a file to
425  * cause default-publishing.
426  * @param cls closure (struct DirScanCls)
427  * @param filename name of the file to be published
428  * @return GNUNET_OK on success, GNUNET_SYSERR to abort
429  */
430 static int
431 dir_scan_cb (void *cls,
432              const char *filename)
433 {
434   struct DirScanCls *dsc = cls;  
435   struct stat sbuf;
436   struct GNUNET_FS_FileInformation *fi;
437   struct GNUNET_FS_Uri *ksk_uri;
438   struct GNUNET_FS_Uri *keywords;
439   struct GNUNET_CONTAINER_MetaData *meta;
440
441   if (0 != STAT (filename, &sbuf))
442     {
443       GNUNET_asprintf (&dsc->emsg,
444                        _("`%s' failed on file `%s': %s"),
445                        "stat",
446                        filename,
447                        STRERROR (errno));
448       return GNUNET_SYSERR;
449     }
450   if (S_ISDIR (sbuf.st_mode))
451     {
452       fi = GNUNET_FS_file_information_create_from_directory (NULL,
453                                                              filename,
454                                                              dsc->scanner,
455                                                              dsc->scanner_cls,
456                                                              dsc->do_index,
457                                                              dsc->anonymity,
458                                                              dsc->priority,
459                                                              dsc->expiration,
460                                                              &dsc->emsg);
461       if (NULL == fi)
462         {
463           GNUNET_assert (NULL != dsc->emsg);
464           return GNUNET_SYSERR;
465         }
466     }
467   else
468     {
469       meta = GNUNET_CONTAINER_meta_data_create ();
470       GNUNET_CONTAINER_meta_data_extract_from_file (meta,
471                                                     filename,
472                                                     dsc->extractors);
473       // FIXME: remove path from filename in metadata!
474       keywords = GNUNET_FS_uri_ksk_create_from_meta_data (meta);
475       ksk_uri = GNUNET_FS_uri_ksk_canonicalize (keywords);
476       fi = GNUNET_FS_file_information_create_from_file (NULL,
477                                                         filename,
478                                                         ksk_uri,
479                                                         meta,
480                                                         dsc->do_index,
481                                                         dsc->anonymity,
482                                                         dsc->priority,
483                                                         dsc->expiration);
484       GNUNET_CONTAINER_meta_data_destroy (meta);
485       GNUNET_FS_uri_destroy (keywords);
486       GNUNET_FS_uri_destroy (ksk_uri);
487     }
488   dsc->proc (dsc->proc_cls,
489              filename,
490              fi);
491   return GNUNET_OK;
492 }
493
494
495 /**
496  * Simple, useful default implementation of a directory scanner
497  * (GNUNET_FS_DirectoryScanner).  This implementation expects to get a
498  * UNIX filename, will publish all files in the directory except hidden
499  * files (those starting with a ".").  Metadata will be extracted
500  * using GNU libextractor; the specific list of plugins should be
501  * specified in "cls", passing NULL will disable (!)  metadata
502  * extraction.  Keywords will be derived from the metadata and be
503  * subject to default canonicalization.  This is strictly a
504  * convenience function.
505  *
506  * @param cls must be of type "struct EXTRACTOR_Extractor*"
507  * @param dirname name of the directory to scan
508  * @param do_index should files be indexed or inserted
509  * @param anonymity desired anonymity level
510  * @param priority priority for publishing
511  * @param expirationTime expiration for publication
512  * @param proc function called on each entry
513  * @param proc_cls closure for proc
514  * @param emsg where to store an error message (on errors)
515  * @return GNUNET_OK on success
516  */
517 int
518 GNUNET_FS_directory_scanner_default (void *cls,
519                                      const char *dirname,
520                                      int do_index,
521                                      uint32_t anonymity,
522                                      uint32_t priority,
523                                      struct GNUNET_TIME_Absolute expirationTime,
524                                      GNUNET_FS_FileProcessor proc,
525                                      void *proc_cls,
526                                      char **emsg)
527 {
528   struct EXTRACTOR_PluginList *ex = cls;
529   struct DirScanCls dsc;
530
531   dsc.extractors = ex;
532   dsc.proc = proc;
533   dsc.proc_cls = proc_cls;
534   dsc.scanner = &GNUNET_FS_directory_scanner_default;
535   dsc.scanner_cls = cls;
536   dsc.do_index = do_index;
537   dsc.anonymity = anonymity;
538   dsc.priority = priority;
539   dsc.expiration = expirationTime;
540   if (-1 == GNUNET_DISK_directory_scan (dirname,
541                                         &dir_scan_cb,
542                                         &dsc))
543     {
544       GNUNET_assert (NULL != dsc.emsg);
545       *emsg = dsc.emsg;
546       return GNUNET_SYSERR;
547     }
548   return GNUNET_OK;
549 }
550
551
552 /**
553  * Closure for dirproc function.
554  */
555 struct EntryProcCls
556 {
557   /**
558    * Linked list of directory entries that is being
559    * created.
560    */
561   struct GNUNET_FS_FileInformation *entries;
562
563 };
564
565
566 /**
567  * Function that processes a directory entry that
568  * was obtained from the scanner.
569  * @param cls our closure
570  * @param filename name of the file (unused, why there???)
571  * @param fi information for publishing the file
572  */
573 static void
574 dirproc (void *cls,
575          const char *filename,
576          struct GNUNET_FS_FileInformation *fi)
577 {
578   struct EntryProcCls *dc = cls;
579
580   GNUNET_assert (fi->next == NULL);
581   GNUNET_assert (fi->dir == NULL);
582   fi->next = dc->entries;
583   dc->entries = fi;
584 }
585
586
587 /**
588  * Create a publish-structure from an existing file hierarchy, inferring
589  * and organizing keywords and metadata as much as possible.  This
590  * function primarily performs the recursive build and re-organizes
591  * keywords and metadata; for automatically getting metadata
592  * extraction, scanning of directories and creation of the respective
593  * GNUNET_FS_FileInformation entries the default scanner should be
594  * passed (GNUNET_FS_directory_scanner_default).  This is strictly a
595  * convenience function.
596  *
597  * @param client_info initial value for the client-info value for this entry
598  * @param filename name of the top-level file or directory
599  * @param scanner function used to get a list of files in a directory
600  * @param scanner_cls closure for scanner
601  * @param do_index should files in the hierarchy be indexed?
602  * @param anonymity what is the desired anonymity level for sharing?
603  * @param priority what is the priority for OUR node to
604  *   keep this file available?  Use 0 for maximum anonymity and
605  *   minimum reliability...
606  * @param expirationTime when should this content expire?
607  * @param emsg where to store an error message
608  * @return publish structure entry for the directory, NULL on error
609  */
610 struct GNUNET_FS_FileInformation *
611 GNUNET_FS_file_information_create_from_directory (void *client_info,
612                                                   const char *filename,
613                                                   GNUNET_FS_DirectoryScanner scanner,
614                                                   void *scanner_cls,
615                                                   int do_index,
616                                                   uint32_t anonymity,
617                                                   uint32_t priority,
618                                                   struct GNUNET_TIME_Absolute expirationTime,
619                                                   char **emsg)
620 {
621   struct GNUNET_FS_FileInformation *ret;
622   struct EntryProcCls dc;
623   struct GNUNET_FS_Uri *ksk;
624   struct GNUNET_CONTAINER_MetaData *meta;
625
626   
627
628   dc.entries = NULL;
629   meta = GNUNET_CONTAINER_meta_data_create ();
630   GNUNET_FS_meta_data_make_directory (meta);
631   
632   scanner (scanner_cls,
633            filename,
634            do_index,
635            anonymity,
636            priority,
637            expirationTime,
638            &dirproc,
639            &dc,
640            emsg);
641   ksk = NULL; // FIXME...
642   // FIXME: create meta!
643   ret = GNUNET_FS_file_information_create_empty_directory (client_info,
644                                                            ksk,
645                                                            meta,
646                                                            anonymity,
647                                                            priority,
648                                                            expirationTime);
649   GNUNET_CONTAINER_meta_data_destroy (meta);
650   ret->data.dir.entries = dc.entries;
651   while (dc.entries != NULL)
652     {
653       dc.entries->dir = ret;
654       GNUNET_FS_file_information_sync (dc.entries);
655       dc.entries = dc.entries->next;
656     }
657   GNUNET_FS_file_information_sync (ret);
658   return ret;
659 }
660
661
662 /**
663  * Create an entry for an empty directory in a publish-structure.
664  * This function should be used by applications for which the
665  * use of "GNUNET_FS_file_information_create_from_directory"
666  * is not appropriate.
667  *
668  * @param client_info initial value for the client-info value for this entry
669  * @param meta metadata for the directory
670  * @param keywords under which keywords should this directory be available
671  *         directly; can be NULL
672  * @param anonymity what is the desired anonymity level for sharing?
673  * @param priority what is the priority for OUR node to
674  *   keep this file available?  Use 0 for maximum anonymity and
675  *   minimum reliability...
676  * @param expirationTime when should this content expire?
677  * @return publish structure entry for the directory , NULL on error
678  */
679 struct GNUNET_FS_FileInformation *
680 GNUNET_FS_file_information_create_empty_directory (void *client_info,
681                                                    const struct GNUNET_FS_Uri *keywords,
682                                                    const struct GNUNET_CONTAINER_MetaData *meta,
683                                                    uint32_t anonymity,
684                                                    uint32_t priority,
685                                                    struct GNUNET_TIME_Absolute expirationTime)
686 {
687   struct GNUNET_FS_FileInformation *ret;
688
689   ret = GNUNET_malloc (sizeof (struct GNUNET_FS_FileInformation));
690   ret->client_info = client_info;
691   ret->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
692   ret->keywords = GNUNET_FS_uri_dup (keywords);
693   ret->expirationTime = expirationTime;
694   ret->is_directory = GNUNET_YES;
695   ret->anonymity = anonymity;
696   ret->priority = priority;
697   GNUNET_FS_file_information_sync (ret);
698   return ret;
699 }
700
701
702 /**
703  * Add an entry to a directory in a publish-structure.  Clients
704  * should never modify publish structures that were passed to
705  * "GNUNET_FS_publish_start" already.
706  *
707  * @param dir the directory
708  * @param ent the entry to add; the entry must not have been
709  *            added to any other directory at this point and 
710  *            must not include "dir" in its structure
711  * @return GNUNET_OK on success, GNUNET_SYSERR on error
712  */
713 int
714 GNUNET_FS_file_information_add (struct GNUNET_FS_FileInformation *dir,
715                                 struct GNUNET_FS_FileInformation *ent)
716 {
717   if ( (ent->dir != NULL) ||
718        (ent->next != NULL) ||
719        (! dir->is_directory) )
720     {
721       GNUNET_break (0);
722       return GNUNET_SYSERR;
723     }
724   ent->dir = dir;
725   ent->next = dir->data.dir.entries;
726   dir->data.dir.entries = ent;
727   dir->data.dir.dir_size = 0;
728   GNUNET_FS_file_information_sync (ent);
729   GNUNET_FS_file_information_sync (dir);
730   return GNUNET_OK;
731 }
732
733
734 /**
735  * Inspect a file or directory in a publish-structure.  Clients
736  * should never modify publish structures that were passed to
737  * "GNUNET_FS_publish_start" already.  When called on a directory,
738  * this function will FIRST call "proc" with information about
739  * the directory itself and then for each of the files in the
740  * directory (but not for files in subdirectories).  When called
741  * on a file, "proc" will be called exactly once (with information
742  * about the specific file).
743  *
744  * @param dir the directory
745  * @param proc function to call on each entry
746  * @param proc_cls closure for proc
747  */
748 void
749 GNUNET_FS_file_information_inspect (struct GNUNET_FS_FileInformation *dir,
750                                     GNUNET_FS_FileInformationProcessor proc,
751                                     void *proc_cls)
752 {
753   struct GNUNET_FS_FileInformation *pos;
754
755   if (dir->is_directory)
756     {
757       proc (proc_cls, 
758             dir,
759             dir->data.dir.dir_size,
760             dir->meta,
761             &dir->keywords,
762             &dir->anonymity,
763             &dir->priority,
764             &dir->expirationTime,
765             &dir->client_info);
766       pos = dir->data.dir.entries;
767       while (pos != NULL)
768         {
769           proc (proc_cls, 
770                 pos,
771                 (pos->is_directory) ? pos->data.dir.dir_size : pos->data.file.file_size,
772                 pos->meta,
773                 &pos->keywords,
774                 &pos->anonymity,
775                 &pos->priority,
776                 &pos->expirationTime,
777                 &pos->client_info);
778           pos = pos->next;
779         }
780     }
781   else
782     {
783       proc (proc_cls, 
784             dir,
785             dir->data.file.file_size,
786             dir->meta,
787             &dir->keywords,
788             &dir->anonymity,
789             &dir->priority,
790             &dir->expirationTime,
791             &dir->client_info);
792     }
793 }
794
795
796 /**
797  * Destroy publish-structure.  Clients should never destroy publish
798  * structures that were passed to "GNUNET_FS_publish_start" already.
799  *
800  * @param fi structure to destroy
801  * @param cleaner function to call on each entry in the structure
802  *        (useful to clean up client_info); can be NULL; return
803  *        values are ignored
804  * @param cleaner_cls closure for cleaner
805  */
806 void
807 GNUNET_FS_file_information_destroy (struct GNUNET_FS_FileInformation *fi,
808                                     GNUNET_FS_FileInformationProcessor cleaner,
809                                     void *cleaner_cls)
810 {
811   struct GNUNET_FS_FileInformation *pos;
812
813   if (fi->is_directory)
814     {
815       /* clean up directory */
816       while (NULL != (pos = fi->data.dir.entries))
817         {
818           fi->data.dir.entries = pos->next;
819           GNUNET_FS_file_information_destroy (pos, cleaner, cleaner_cls);
820         }
821       /* clean up client-info */
822       if (NULL != cleaner)
823         cleaner (cleaner_cls, 
824                  fi,
825                  fi->data.dir.dir_size,
826                  fi->meta,
827                  &fi->keywords,
828                  &fi->anonymity,
829                  &fi->priority,
830                  &fi->expirationTime,
831                  &fi->client_info);
832       GNUNET_free_non_null (fi->data.dir.dir_data);
833       GNUNET_free_non_null (fi->data.dir.dirname);
834     }
835   else
836     {
837       /* call clean-up function of the reader */
838       fi->data.file.reader (fi->data.file.reader_cls, 0, 0, NULL, NULL);
839       /* clean up client-info */
840       if (NULL != cleaner)
841         cleaner (cleaner_cls, 
842                  fi,
843                  fi->data.file.file_size,
844                  fi->meta,
845                  &fi->keywords,
846                  &fi->anonymity,
847                  &fi->priority,
848                  &fi->expirationTime,
849                  &fi->client_info);
850     }
851   GNUNET_free_non_null (fi->emsg);
852   GNUNET_free_non_null (fi->chk_uri);
853   /* clean up serialization */
854   if ( (NULL != fi->serialization) &&
855        (0 != UNLINK (fi->serialization)) )
856     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
857                               "unlink",
858                               fi->serialization);
859   if (NULL != fi->keywords)
860     GNUNET_FS_uri_destroy (fi->keywords);
861   GNUNET_CONTAINER_meta_data_destroy (fi->meta);
862   GNUNET_free_non_null (fi->serialization);
863   if (fi->te != NULL)
864     {
865       GNUNET_FS_tree_encoder_finish (fi->te,
866                                      NULL, NULL);
867       fi->te = NULL;
868     }
869   GNUNET_free (fi);
870 }
871
872
873 /* end of fs_file_information.c */