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 2, 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 * - serialization/deserialization (& deserialization API)
28 * - metadata filename clean up code
29 * - metadata/ksk generation for directories from contained files
32 #include <extractor.h>
33 #include "gnunet_fs_service.h"
39 * Add meta data that libextractor finds to our meta data
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. '<zlib>' for zlib being
45 * used in the main libextractor library and yielding
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
57 const char *plugin_name,
58 enum EXTRACTOR_MetaType type,
59 enum EXTRACTOR_MetaFormat format,
60 const char *data_mime_type,
64 struct GNUNET_CONTAINER_MetaData *md = cls;
65 (void) GNUNET_CONTAINER_meta_data_insert (md,
77 * Extract meta-data from a file.
79 * @return GNUNET_SYSERR on error, otherwise the number
80 * of meta-data items obtained
83 GNUNET_FS_meta_data_extract_from_file (struct GNUNET_CONTAINER_MetaData
84 *md, const char *filename,
85 struct EXTRACTOR_PluginList *
92 if (extractors == NULL)
94 old = GNUNET_CONTAINER_meta_data_iterate (md, NULL, NULL);
95 GNUNET_assert (old >= 0);
96 EXTRACTOR_extract (extractors,
101 return (GNUNET_CONTAINER_meta_data_iterate (md, NULL, NULL) - old);
107 * Obtain the name under which this file information
108 * structure is stored on disk. Only works for top-level
109 * file information structures.
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.
117 GNUNET_FS_file_information_get_id (struct GNUNET_FS_FileInformation *s)
121 return s->serialization;
126 * Create an entry for a file in a publish-structure.
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
143 struct GNUNET_FS_FileInformation *
144 GNUNET_FS_file_information_create_from_file (struct GNUNET_FS_Handle *h,
146 const char *filename,
147 const struct GNUNET_FS_Uri *keywords,
148 const struct GNUNET_CONTAINER_MetaData *meta,
152 struct GNUNET_TIME_Absolute expirationTime)
156 struct GNUNET_FS_FileInformation *ret;
160 if (0 != STAT (filename, &sbuf))
162 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
167 fi = GNUNET_FS_make_file_reader_context_ (filename);
173 ret = GNUNET_FS_file_information_create_from_reader (h,
176 &GNUNET_FS_data_reader_file_,
185 ret->filename = GNUNET_strdup (filename);
187 while (NULL != (ss = strstr (fn,
190 GNUNET_CONTAINER_meta_data_insert (ret->meta,
192 EXTRACTOR_METATYPE_FILENAME,
193 EXTRACTOR_METAFORMAT_C_STRING,
202 * Create an entry for a file in a publish-structure.
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
221 struct GNUNET_FS_FileInformation *
222 GNUNET_FS_file_information_create_from_data (struct GNUNET_FS_Handle *h,
226 const struct GNUNET_FS_Uri *keywords,
227 const struct GNUNET_CONTAINER_MetaData *meta,
231 struct GNUNET_TIME_Absolute expirationTime)
233 if (GNUNET_YES == do_index)
238 return GNUNET_FS_file_information_create_from_reader (h,
241 &GNUNET_FS_data_reader_copy_,
253 * Create an entry for a file in a publish-structure.
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
272 struct GNUNET_FS_FileInformation *
273 GNUNET_FS_file_information_create_from_reader (struct GNUNET_FS_Handle *h,
276 GNUNET_FS_DataReader reader,
278 const struct GNUNET_FS_Uri *keywords,
279 const struct GNUNET_CONTAINER_MetaData *meta,
283 struct GNUNET_TIME_Absolute expirationTime)
285 struct GNUNET_FS_FileInformation *ret;
287 if ( (GNUNET_YES == do_index) &&
288 (reader != &GNUNET_FS_data_reader_file_) )
293 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_FileInformation));
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;
312 * Closure for "dir_scan_cb".
317 * Metadata extractors to use.
319 struct EXTRACTOR_PluginList *extractors;
324 struct GNUNET_FS_Handle *h;
327 * Function to call on each directory entry.
329 GNUNET_FS_FileProcessor proc;
337 * Scanner to use for subdirectories.
339 GNUNET_FS_DirectoryScanner scanner;
342 * Closure for scanner.
347 * Set to an error message (if any).
352 * Should files be indexed?
357 * Desired anonymity level.
362 * Desired publishing priority.
367 * Expiration time for publication.
369 struct GNUNET_TIME_Absolute expiration;
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
381 dir_scan_cb (void *cls,
382 const char *filename)
384 struct DirScanCls *dsc = cls;
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;
391 if (0 != STAT (filename, &sbuf))
393 GNUNET_asprintf (&dsc->emsg,
394 _("`%s' failed on file `%s': %s"),
398 return GNUNET_SYSERR;
400 if (S_ISDIR (sbuf.st_mode))
402 fi = GNUNET_FS_file_information_create_from_directory (dsc->h,
414 GNUNET_assert (NULL != dsc->emsg);
415 return GNUNET_SYSERR;
420 meta = GNUNET_CONTAINER_meta_data_create ();
421 GNUNET_FS_meta_data_extract_from_file (meta,
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,
436 GNUNET_CONTAINER_meta_data_destroy (meta);
437 GNUNET_FS_uri_destroy (keywords);
438 GNUNET_FS_uri_destroy (ksk_uri);
440 dsc->proc (dsc->proc_cls,
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.
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
471 GNUNET_FS_directory_scanner_default (void *cls,
472 struct GNUNET_FS_Handle *h,
477 struct GNUNET_TIME_Absolute expirationTime,
478 GNUNET_FS_FileProcessor proc,
482 struct EXTRACTOR_PluginList *ex = cls;
483 struct DirScanCls dsc;
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,
499 GNUNET_assert (NULL != dsc.emsg);
501 return GNUNET_SYSERR;
508 * Closure for dirproc function.
513 * Linked list of directory entries that is being
516 struct GNUNET_FS_FileInformation *entries;
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
530 const char *filename,
531 struct GNUNET_FS_FileInformation *fi)
533 struct EntryProcCls *dc = cls;
535 GNUNET_assert (fi->next == NULL);
536 GNUNET_assert (fi->dir == NULL);
537 fi->next = dc->entries;
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.
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
566 struct GNUNET_FS_FileInformation *
567 GNUNET_FS_file_information_create_from_directory (struct GNUNET_FS_Handle *h,
569 const char *filename,
570 GNUNET_FS_DirectoryScanner scanner,
575 struct GNUNET_TIME_Absolute expirationTime,
578 struct GNUNET_FS_FileInformation *ret;
579 struct EntryProcCls dc;
580 struct GNUNET_FS_Uri *ksk;
581 struct GNUNET_CONTAINER_MetaData *meta;
586 meta = GNUNET_CONTAINER_meta_data_create ();
587 GNUNET_FS_meta_data_make_directory (meta);
588 scanner (scanner_cls,
598 ksk = NULL; // FIXME...
599 // FIXME: create meta!
600 ret = GNUNET_FS_file_information_create_empty_directory (h,
607 GNUNET_CONTAINER_meta_data_destroy (meta);
608 ret->data.dir.entries = dc.entries;
609 while (dc.entries != NULL)
611 dc.entries->dir = ret;
612 dc.entries = dc.entries->next;
615 while (NULL != (ss = strstr (fn,
618 GNUNET_CONTAINER_meta_data_insert (ret->meta,
620 EXTRACTOR_METATYPE_FILENAME,
621 EXTRACTOR_METAFORMAT_C_STRING,
625 ret->filename = GNUNET_strdup (filename);
631 * Test if a given entry represents a directory.
633 * @param ent check if this FI represents a directory
634 * @return GNUNET_YES if so, GNUNET_NO if not
637 GNUNET_FS_file_information_is_directory (struct GNUNET_FS_FileInformation *ent)
639 return ent->is_directory;
644 * Create an entry for an empty directory in a publish-structure.
645 * This function should be used by applications for which the
646 * use of "GNUNET_FS_file_information_create_from_directory"
647 * is not appropriate.
649 * @param h handle to the file sharing subsystem
650 * @param client_info initial value for the client-info value for this entry
651 * @param meta metadata for the directory
652 * @param keywords under which keywords should this directory be available
653 * directly; can be NULL
654 * @param anonymity what is the desired anonymity level for sharing?
655 * @param priority what is the priority for OUR node to
656 * keep this file available? Use 0 for maximum anonymity and
657 * minimum reliability...
658 * @param expirationTime when should this content expire?
659 * @return publish structure entry for the directory , NULL on error
661 struct GNUNET_FS_FileInformation *
662 GNUNET_FS_file_information_create_empty_directory (struct GNUNET_FS_Handle *h,
664 const struct GNUNET_FS_Uri *keywords,
665 const struct GNUNET_CONTAINER_MetaData *meta,
668 struct GNUNET_TIME_Absolute expirationTime)
670 struct GNUNET_FS_FileInformation *ret;
672 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_FileInformation));
674 ret->client_info = client_info;
675 ret->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
676 ret->keywords = GNUNET_FS_uri_dup (keywords);
677 ret->expirationTime = expirationTime;
678 ret->is_directory = GNUNET_YES;
679 ret->anonymity = anonymity;
680 ret->priority = priority;
686 * Add an entry to a directory in a publish-structure. Clients
687 * should never modify publish structures that were passed to
688 * "GNUNET_FS_publish_start" already.
690 * @param dir the directory
691 * @param ent the entry to add; the entry must not have been
692 * added to any other directory at this point and
693 * must not include "dir" in its structure
694 * @return GNUNET_OK on success, GNUNET_SYSERR on error
697 GNUNET_FS_file_information_add (struct GNUNET_FS_FileInformation *dir,
698 struct GNUNET_FS_FileInformation *ent)
700 if ( (ent->dir != NULL) ||
701 (ent->next != NULL) ||
702 (! dir->is_directory) )
705 return GNUNET_SYSERR;
708 ent->next = dir->data.dir.entries;
709 dir->data.dir.entries = ent;
710 dir->data.dir.dir_size = 0;
716 * Inspect a file or directory in a publish-structure. Clients
717 * should never modify publish structures that were passed to
718 * "GNUNET_FS_publish_start" already. When called on a directory,
719 * this function will FIRST call "proc" with information about
720 * the directory itself and then for each of the files in the
721 * directory (but not for files in subdirectories). When called
722 * on a file, "proc" will be called exactly once (with information
723 * about the specific file).
725 * @param dir the directory
726 * @param proc function to call on each entry
727 * @param proc_cls closure for proc
730 GNUNET_FS_file_information_inspect (struct GNUNET_FS_FileInformation *dir,
731 GNUNET_FS_FileInformationProcessor proc,
734 struct GNUNET_FS_FileInformation *pos;
741 (dir->is_directory) ? dir->data.dir.dir_size : dir->data.file.file_size,
746 (dir->is_directory) ? &no : &dir->data.file.do_index,
747 &dir->expirationTime,
750 if (! dir->is_directory)
752 pos = dir->data.dir.entries;
759 (pos->is_directory) ? pos->data.dir.dir_size : pos->data.file.file_size,
764 (dir->is_directory) ? &no : &dir->data.file.do_index,
765 &pos->expirationTime,
774 * Destroy publish-structure. Clients should never destroy publish
775 * structures that were passed to "GNUNET_FS_publish_start" already.
777 * @param fi structure to destroy
778 * @param cleaner function to call on each entry in the structure
779 * (useful to clean up client_info); can be NULL; return
781 * @param cleaner_cls closure for cleaner
784 GNUNET_FS_file_information_destroy (struct GNUNET_FS_FileInformation *fi,
785 GNUNET_FS_FileInformationProcessor cleaner,
788 struct GNUNET_FS_FileInformation *pos;
792 if (fi->is_directory)
794 /* clean up directory */
795 while (NULL != (pos = fi->data.dir.entries))
797 fi->data.dir.entries = pos->next;
798 GNUNET_FS_file_information_destroy (pos, cleaner, cleaner_cls);
800 /* clean up client-info */
802 cleaner (cleaner_cls,
804 fi->data.dir.dir_size,
812 GNUNET_free_non_null (fi->data.dir.dir_data);
816 /* call clean-up function of the reader */
817 if (fi->data.file.reader != NULL)
818 fi->data.file.reader (fi->data.file.reader_cls, 0, 0,
820 /* clean up client-info */
822 cleaner (cleaner_cls,
824 fi->data.file.file_size,
829 &fi->data.file.do_index,
833 GNUNET_free_non_null (fi->filename);
834 GNUNET_free_non_null (fi->emsg);
835 GNUNET_free_non_null (fi->chk_uri);
836 /* clean up serialization */
837 if ( (NULL != fi->serialization) &&
838 (0 != UNLINK (fi->serialization)) )
839 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
842 if (NULL != fi->keywords)
843 GNUNET_FS_uri_destroy (fi->keywords);
844 if (NULL != fi->meta)
845 GNUNET_CONTAINER_meta_data_destroy (fi->meta);
846 GNUNET_free_non_null (fi->serialization);
849 GNUNET_FS_tree_encoder_finish (fi->te,
857 /* end of fs_file_information.c */