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