2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file fs/fs_file_information.c
23 * @brief Manage information for publishing directory hierarchies
24 * @author Christian Grothoff
27 * - metadata filename clean up code
28 * - metadata/ksk generation for directories from contained files
31 #include <extractor.h>
32 #include "gnunet_fs_service.h"
38 * Add meta data that libextractor finds to our meta data
41 * @param cls closure, our meta data container
42 * @param plugin_name name of the plugin that produced this value;
43 * special values can be used (i.e. '<zlib>' for zlib being
44 * used in the main libextractor library and yielding
46 * @param type libextractor-type describing the meta data
47 * @param format basic format information about data
48 * @param data_mime_type mime-type of data (not of the original file);
49 * can be NULL (if mime-type is not known)
50 * @param data actual meta-data found
51 * @param data_len number of bytes in data
52 * @return always 0 to continue extracting
56 const char *plugin_name,
57 enum EXTRACTOR_MetaType type,
58 enum EXTRACTOR_MetaFormat format,
59 const char *data_mime_type,
63 struct GNUNET_CONTAINER_MetaData *md = cls;
64 (void) GNUNET_CONTAINER_meta_data_insert (md,
76 * Extract meta-data from a file.
78 * @return GNUNET_SYSERR on error, otherwise the number
79 * of meta-data items obtained
82 GNUNET_FS_meta_data_extract_from_file (struct GNUNET_CONTAINER_MetaData
83 *md, const char *filename,
84 struct EXTRACTOR_PluginList *
91 if (extractors == NULL)
93 old = GNUNET_CONTAINER_meta_data_iterate (md, NULL, NULL);
94 GNUNET_assert (old >= 0);
95 EXTRACTOR_extract (extractors,
100 return (GNUNET_CONTAINER_meta_data_iterate (md, NULL, NULL) - old);
106 * Obtain the name under which this file information
107 * structure is stored on disk. Only works for top-level
108 * file information structures.
110 * @param s structure to get the filename for
111 * @return NULL on error, otherwise filename that
112 * can be passed to "GNUNET_FS_file_information_recover"
113 * to read this fi-struct from disk.
116 GNUNET_FS_file_information_get_id (struct GNUNET_FS_FileInformation *s)
120 return s->serialization;
125 * Create an entry for a file in a publish-structure.
127 * @param h handle to the file sharing subsystem
128 * @param client_info initial value for the client-info value for this entry
129 * @param filename name of the file or directory to publish
130 * @param keywords under which keywords should this file be available
131 * directly; can be NULL
132 * @param meta metadata for the file
133 * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
134 * GNUNET_SYSERR for simulation
135 * @param anonymity what is the desired anonymity level for sharing?
136 * @param priority what is the priority for OUR node to
137 * keep this file available? Use 0 for maximum anonymity and
138 * minimum reliability...
139 * @param expirationTime when should this content expire?
140 * @return publish structure entry for the file
142 struct GNUNET_FS_FileInformation *
143 GNUNET_FS_file_information_create_from_file (struct GNUNET_FS_Handle *h,
145 const char *filename,
146 const struct GNUNET_FS_Uri *keywords,
147 const struct GNUNET_CONTAINER_MetaData *meta,
151 struct GNUNET_TIME_Absolute expirationTime)
155 struct GNUNET_FS_FileInformation *ret;
159 if (0 != STAT (filename, &sbuf))
161 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
166 fi = GNUNET_FS_make_file_reader_context_ (filename);
172 ret = GNUNET_FS_file_information_create_from_reader (h,
175 &GNUNET_FS_data_reader_file_,
186 ret->filename = GNUNET_strdup (filename);
188 while (NULL != (ss = strstr (fn,
191 GNUNET_CONTAINER_meta_data_insert (ret->meta,
193 EXTRACTOR_METATYPE_FILENAME,
194 EXTRACTOR_METAFORMAT_C_STRING,
203 * Create an entry for a file in a publish-structure.
205 * @param h handle to the file sharing subsystem
206 * @param client_info initial value for the client-info value for this entry
207 * @param length length of the file
208 * @param data data for the file (should not be used afterwards by
209 * the caller; callee will "free")
210 * @param keywords under which keywords should this file be available
211 * directly; can be NULL
212 * @param meta metadata for the file
213 * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
214 * GNUNET_SYSERR for simulation
215 * @param anonymity what is the desired anonymity level for sharing?
216 * @param priority what is the priority for OUR node to
217 * keep this file available? Use 0 for maximum anonymity and
218 * minimum reliability...
219 * @param expirationTime when should this content expire?
220 * @return publish structure entry for the file
222 struct GNUNET_FS_FileInformation *
223 GNUNET_FS_file_information_create_from_data (struct GNUNET_FS_Handle *h,
227 const struct GNUNET_FS_Uri *keywords,
228 const struct GNUNET_CONTAINER_MetaData *meta,
232 struct GNUNET_TIME_Absolute expirationTime)
234 if (GNUNET_YES == do_index)
239 return GNUNET_FS_file_information_create_from_reader (h,
242 &GNUNET_FS_data_reader_copy_,
254 * Create an entry for a file in a publish-structure.
256 * @param h handle to the file sharing subsystem
257 * @param client_info initial value for the client-info value for this entry
258 * @param length length of the file
259 * @param reader function that can be used to obtain the data for the file
260 * @param reader_cls closure for "reader"
261 * @param keywords under which keywords should this file be available
262 * directly; can be NULL
263 * @param meta metadata for the file
264 * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
265 * GNUNET_SYSERR for simulation
266 * @param anonymity what is the desired anonymity level for sharing?
267 * @param priority what is the priority for OUR node to
268 * keep this file available? Use 0 for maximum anonymity and
269 * minimum reliability...
270 * @param expirationTime when should this content expire?
271 * @return publish structure entry for the file
273 struct GNUNET_FS_FileInformation *
274 GNUNET_FS_file_information_create_from_reader (struct GNUNET_FS_Handle *h,
277 GNUNET_FS_DataReader reader,
279 const struct GNUNET_FS_Uri *keywords,
280 const struct GNUNET_CONTAINER_MetaData *meta,
284 struct GNUNET_TIME_Absolute expirationTime)
286 struct GNUNET_FS_FileInformation *ret;
288 if ( (GNUNET_YES == do_index) &&
289 (reader != &GNUNET_FS_data_reader_file_) )
294 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_FileInformation));
296 ret->client_info = client_info;
297 ret->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
298 if (ret->meta == NULL)
299 ret->meta = GNUNET_CONTAINER_meta_data_create ();
300 ret->keywords = (keywords == NULL) ? NULL : GNUNET_FS_uri_dup (keywords);
301 ret->expirationTime = expirationTime;
302 ret->data.file.reader = reader;
303 ret->data.file.reader_cls = reader_cls;
304 ret->data.file.do_index = do_index;
305 ret->data.file.file_size = length;
306 ret->anonymity = anonymity;
307 ret->priority = priority;
313 * Closure for "dir_scan_cb".
318 * Metadata extractors to use.
320 struct EXTRACTOR_PluginList *extractors;
325 struct GNUNET_FS_Handle *h;
328 * Function to call on each directory entry.
330 GNUNET_FS_FileProcessor proc;
338 * Scanner to use for subdirectories.
340 GNUNET_FS_DirectoryScanner scanner;
343 * Closure for scanner.
348 * Set to an error message (if any).
353 * Should files be indexed?
358 * Desired anonymity level.
363 * Desired publishing priority.
368 * Expiration time for publication.
370 struct GNUNET_TIME_Absolute expiration;
375 * Function called on each entry in a file to
376 * cause default-publishing.
377 * @param cls closure (struct DirScanCls)
378 * @param filename name of the file to be published
379 * @return GNUNET_OK on success, GNUNET_SYSERR to abort
382 dir_scan_cb (void *cls,
383 const char *filename)
385 struct DirScanCls *dsc = cls;
387 struct GNUNET_FS_FileInformation *fi;
388 struct GNUNET_FS_Uri *ksk_uri;
389 struct GNUNET_FS_Uri *keywords;
390 struct GNUNET_CONTAINER_MetaData *meta;
392 if (0 != STAT (filename, &sbuf))
394 GNUNET_asprintf (&dsc->emsg,
395 _("`%s' failed on file `%s': %s"),
399 return GNUNET_SYSERR;
401 if (S_ISDIR (sbuf.st_mode))
403 fi = GNUNET_FS_file_information_create_from_directory (dsc->h,
415 GNUNET_assert (NULL != dsc->emsg);
416 return GNUNET_SYSERR;
421 meta = GNUNET_CONTAINER_meta_data_create ();
422 GNUNET_FS_meta_data_extract_from_file (meta,
425 // FIXME: remove path from filename in metadata!
426 keywords = GNUNET_FS_uri_ksk_create_from_meta_data (meta);
427 ksk_uri = GNUNET_FS_uri_ksk_canonicalize (keywords);
428 fi = GNUNET_FS_file_information_create_from_file (dsc->h,
437 GNUNET_CONTAINER_meta_data_destroy (meta);
438 GNUNET_FS_uri_destroy (keywords);
439 GNUNET_FS_uri_destroy (ksk_uri);
441 dsc->proc (dsc->proc_cls,
449 * Simple, useful default implementation of a directory scanner
450 * (GNUNET_FS_DirectoryScanner). This implementation expects to get a
451 * UNIX filename, will publish all files in the directory except hidden
452 * files (those starting with a "."). Metadata will be extracted
453 * using GNU libextractor; the specific list of plugins should be
454 * specified in "cls", passing NULL will disable (!) metadata
455 * extraction. Keywords will be derived from the metadata and be
456 * subject to default canonicalization. This is strictly a
457 * convenience function.
459 * @param cls must be of type "struct EXTRACTOR_Extractor*"
460 * @param h handle to the file sharing subsystem
461 * @param dirname name of the directory to scan
462 * @param do_index should files be indexed or inserted
463 * @param anonymity desired anonymity level
464 * @param priority priority for publishing
465 * @param expirationTime expiration for publication
466 * @param proc function called on each entry
467 * @param proc_cls closure for proc
468 * @param emsg where to store an error message (on errors)
469 * @return GNUNET_OK on success
472 GNUNET_FS_directory_scanner_default (void *cls,
473 struct GNUNET_FS_Handle *h,
478 struct GNUNET_TIME_Absolute expirationTime,
479 GNUNET_FS_FileProcessor proc,
483 struct EXTRACTOR_PluginList *ex = cls;
484 struct DirScanCls dsc;
489 dsc.proc_cls = proc_cls;
490 dsc.scanner = &GNUNET_FS_directory_scanner_default;
491 dsc.scanner_cls = cls;
492 dsc.do_index = do_index;
493 dsc.anonymity = anonymity;
494 dsc.priority = priority;
495 dsc.expiration = expirationTime;
496 if (-1 == GNUNET_DISK_directory_scan (dirname,
500 GNUNET_assert (NULL != dsc.emsg);
502 return GNUNET_SYSERR;
509 * Closure for dirproc function.
514 * Linked list of directory entries that is being
517 struct GNUNET_FS_FileInformation *entries;
523 * Function that processes a directory entry that
524 * was obtained from the scanner.
525 * @param cls our closure
526 * @param filename name of the file (unused, why there???)
527 * @param fi information for publishing the file
531 const char *filename,
532 struct GNUNET_FS_FileInformation *fi)
534 struct EntryProcCls *dc = cls;
536 GNUNET_assert (fi->next == NULL);
537 GNUNET_assert (fi->dir == NULL);
538 fi->next = dc->entries;
544 * Create a publish-structure from an existing file hierarchy, inferring
545 * and organizing keywords and metadata as much as possible. This
546 * function primarily performs the recursive build and re-organizes
547 * keywords and metadata; for automatically getting metadata
548 * extraction, scanning of directories and creation of the respective
549 * GNUNET_FS_FileInformation entries the default scanner should be
550 * passed (GNUNET_FS_directory_scanner_default). This is strictly a
551 * convenience function.
553 * @param h handle to the file sharing subsystem
554 * @param client_info initial value for the client-info value for this entry
555 * @param filename name of the top-level file or directory
556 * @param scanner function used to get a list of files in a directory
557 * @param scanner_cls closure for scanner
558 * @param do_index should files in the hierarchy be indexed?
559 * @param anonymity what is the desired anonymity level for sharing?
560 * @param priority what is the priority for OUR node to
561 * keep this file available? Use 0 for maximum anonymity and
562 * minimum reliability...
563 * @param expirationTime when should this content expire?
564 * @param emsg where to store an error message
565 * @return publish structure entry for the directory, NULL on error
567 struct GNUNET_FS_FileInformation *
568 GNUNET_FS_file_information_create_from_directory (struct GNUNET_FS_Handle *h,
570 const char *filename,
571 GNUNET_FS_DirectoryScanner scanner,
576 struct GNUNET_TIME_Absolute expirationTime,
579 struct GNUNET_FS_FileInformation *ret;
580 struct EntryProcCls dc;
581 struct GNUNET_FS_Uri *ksk;
582 struct GNUNET_CONTAINER_MetaData *meta;
587 meta = GNUNET_CONTAINER_meta_data_create ();
588 GNUNET_FS_meta_data_make_directory (meta);
589 scanner (scanner_cls,
599 ksk = NULL; // FIXME...
600 // FIXME: create meta!
601 ret = GNUNET_FS_file_information_create_empty_directory (h,
608 GNUNET_CONTAINER_meta_data_destroy (meta);
609 ret->data.dir.entries = dc.entries;
610 while (dc.entries != NULL)
612 dc.entries->dir = ret;
613 dc.entries = dc.entries->next;
616 while ( (NULL != (ss = strstr (fn,
617 DIR_SEPARATOR_STR))) &&
620 GNUNET_CONTAINER_meta_data_insert (ret->meta,
622 EXTRACTOR_METATYPE_FILENAME,
623 EXTRACTOR_METAFORMAT_C_STRING,
627 ret->filename = GNUNET_strdup (filename);
633 * Test if a given entry represents a directory.
635 * @param ent check if this FI represents a directory
636 * @return GNUNET_YES if so, GNUNET_NO if not
639 GNUNET_FS_file_information_is_directory (struct GNUNET_FS_FileInformation *ent)
641 return ent->is_directory;
646 * Create an entry for an empty directory in a publish-structure.
647 * This function should be used by applications for which the
648 * use of "GNUNET_FS_file_information_create_from_directory"
649 * is not appropriate.
651 * @param h handle to the file sharing subsystem
652 * @param client_info initial value for the client-info value for this entry
653 * @param meta metadata for the directory
654 * @param keywords under which keywords should this directory be available
655 * directly; can be NULL
656 * @param anonymity what is the desired anonymity level for sharing?
657 * @param priority what is the priority for OUR node to
658 * keep this file available? Use 0 for maximum anonymity and
659 * minimum reliability...
660 * @param expirationTime when should this content expire?
661 * @return publish structure entry for the directory , NULL on error
663 struct GNUNET_FS_FileInformation *
664 GNUNET_FS_file_information_create_empty_directory (struct GNUNET_FS_Handle *h,
666 const struct GNUNET_FS_Uri *keywords,
667 const struct GNUNET_CONTAINER_MetaData *meta,
670 struct GNUNET_TIME_Absolute expirationTime)
672 struct GNUNET_FS_FileInformation *ret;
674 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_FileInformation));
676 ret->client_info = client_info;
677 ret->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
678 ret->keywords = GNUNET_FS_uri_dup (keywords);
679 ret->expirationTime = expirationTime;
680 ret->is_directory = GNUNET_YES;
681 ret->anonymity = anonymity;
682 ret->priority = priority;
688 * Add an entry to a directory in a publish-structure. Clients
689 * should never modify publish structures that were passed to
690 * "GNUNET_FS_publish_start" already.
692 * @param dir the directory
693 * @param ent the entry to add; the entry must not have been
694 * added to any other directory at this point and
695 * must not include "dir" in its structure
696 * @return GNUNET_OK on success, GNUNET_SYSERR on error
699 GNUNET_FS_file_information_add (struct GNUNET_FS_FileInformation *dir,
700 struct GNUNET_FS_FileInformation *ent)
702 if ( (ent->dir != NULL) ||
703 (ent->next != NULL) ||
704 (! dir->is_directory) )
707 return GNUNET_SYSERR;
710 ent->next = dir->data.dir.entries;
711 dir->data.dir.entries = ent;
712 dir->data.dir.dir_size = 0;
718 * Inspect a file or directory in a publish-structure. Clients
719 * should never modify publish structures that were passed to
720 * "GNUNET_FS_publish_start" already. When called on a directory,
721 * this function will FIRST call "proc" with information about
722 * the directory itself and then for each of the files in the
723 * directory (but not for files in subdirectories). When called
724 * on a file, "proc" will be called exactly once (with information
725 * about the specific file).
727 * @param dir the directory
728 * @param proc function to call on each entry
729 * @param proc_cls closure for proc
732 GNUNET_FS_file_information_inspect (struct GNUNET_FS_FileInformation *dir,
733 GNUNET_FS_FileInformationProcessor proc,
736 struct GNUNET_FS_FileInformation *pos;
743 (dir->is_directory) ? dir->data.dir.dir_size : dir->data.file.file_size,
748 (dir->is_directory) ? &no : &dir->data.file.do_index,
749 &dir->expirationTime,
752 if (! dir->is_directory)
754 pos = dir->data.dir.entries;
761 (pos->is_directory) ? pos->data.dir.dir_size : pos->data.file.file_size,
766 (dir->is_directory) ? &no : &dir->data.file.do_index,
767 &pos->expirationTime,
776 * Destroy publish-structure. Clients should never destroy publish
777 * structures that were passed to "GNUNET_FS_publish_start" already.
779 * @param fi structure to destroy
780 * @param cleaner function to call on each entry in the structure
781 * (useful to clean up client_info); can be NULL; return
783 * @param cleaner_cls closure for cleaner
786 GNUNET_FS_file_information_destroy (struct GNUNET_FS_FileInformation *fi,
787 GNUNET_FS_FileInformationProcessor cleaner,
790 struct GNUNET_FS_FileInformation *pos;
794 if (fi->is_directory)
796 /* clean up directory */
797 while (NULL != (pos = fi->data.dir.entries))
799 fi->data.dir.entries = pos->next;
800 GNUNET_FS_file_information_destroy (pos, cleaner, cleaner_cls);
802 /* clean up client-info */
804 cleaner (cleaner_cls,
806 fi->data.dir.dir_size,
814 GNUNET_free_non_null (fi->data.dir.dir_data);
818 /* call clean-up function of the reader */
819 if (fi->data.file.reader != NULL)
820 fi->data.file.reader (fi->data.file.reader_cls, 0, 0,
822 /* clean up client-info */
824 cleaner (cleaner_cls,
826 fi->data.file.file_size,
831 &fi->data.file.do_index,
835 GNUNET_free_non_null (fi->filename);
836 GNUNET_free_non_null (fi->emsg);
837 GNUNET_free_non_null (fi->chk_uri);
838 /* clean up serialization */
839 if ( (NULL != fi->serialization) &&
840 (0 != UNLINK (fi->serialization)) )
841 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
844 if (NULL != fi->keywords)
845 GNUNET_FS_uri_destroy (fi->keywords);
846 if (NULL != fi->meta)
847 GNUNET_CONTAINER_meta_data_destroy (fi->meta);
848 GNUNET_free_non_null (fi->serialization);
851 GNUNET_FS_tree_encoder_finish (fi->te,
859 /* end of fs_file_information.c */