2 This file is part of GNUnet.
3 (C) 2009, 2011 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 bo block options
136 * @return publish structure entry for the file
138 struct GNUNET_FS_FileInformation *
139 GNUNET_FS_file_information_create_from_file (struct GNUNET_FS_Handle *h,
141 const char *filename,
142 const struct GNUNET_FS_Uri *keywords,
143 const struct GNUNET_CONTAINER_MetaData *meta,
145 const struct GNUNET_FS_BlockOptions *bo)
149 struct GNUNET_FS_FileInformation *ret;
153 if (0 != STAT (filename, &sbuf))
155 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
160 fi = GNUNET_FS_make_file_reader_context_ (filename);
166 ret = GNUNET_FS_file_information_create_from_reader (h,
169 &GNUNET_FS_data_reader_file_,
178 ret->filename = GNUNET_strdup (filename);
180 while (NULL != (ss = strstr (fn,
183 GNUNET_CONTAINER_meta_data_insert (ret->meta,
185 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME,
186 EXTRACTOR_METAFORMAT_C_STRING,
195 * Create an entry for a file in a publish-structure.
197 * @param h handle to the file sharing subsystem
198 * @param client_info initial value for the client-info value for this entry
199 * @param length length of the file
200 * @param data data for the file (should not be used afterwards by
201 * the caller; callee will "free")
202 * @param keywords under which keywords should this file be available
203 * directly; can be NULL
204 * @param meta metadata for the file
205 * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
206 * GNUNET_SYSERR for simulation
207 * @param bo block options
208 * @return publish structure entry for the file
210 struct GNUNET_FS_FileInformation *
211 GNUNET_FS_file_information_create_from_data (struct GNUNET_FS_Handle *h,
215 const struct GNUNET_FS_Uri *keywords,
216 const struct GNUNET_CONTAINER_MetaData *meta,
218 const struct GNUNET_FS_BlockOptions *bo)
220 if (GNUNET_YES == do_index)
225 return GNUNET_FS_file_information_create_from_reader (h,
228 &GNUNET_FS_data_reader_copy_,
238 * Create an entry for a file in a publish-structure.
240 * @param h handle to the file sharing subsystem
241 * @param client_info initial value for the client-info value for this entry
242 * @param length length of the file
243 * @param reader function that can be used to obtain the data for the file
244 * @param reader_cls closure for "reader"
245 * @param keywords under which keywords should this file be available
246 * directly; can be NULL
247 * @param meta metadata for the file
248 * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
249 * GNUNET_SYSERR for simulation
250 * @param bo block options
251 * @return publish structure entry for the file
253 struct GNUNET_FS_FileInformation *
254 GNUNET_FS_file_information_create_from_reader (struct GNUNET_FS_Handle *h,
257 GNUNET_FS_DataReader reader,
259 const struct GNUNET_FS_Uri *keywords,
260 const struct GNUNET_CONTAINER_MetaData *meta,
262 const struct GNUNET_FS_BlockOptions *bo)
264 struct GNUNET_FS_FileInformation *ret;
266 if ( (GNUNET_YES == do_index) &&
267 (reader != &GNUNET_FS_data_reader_file_) )
272 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_FileInformation));
274 ret->client_info = client_info;
275 ret->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
276 if (ret->meta == NULL)
277 ret->meta = GNUNET_CONTAINER_meta_data_create ();
278 ret->keywords = (keywords == NULL) ? NULL : GNUNET_FS_uri_dup (keywords);
279 ret->data.file.reader = reader;
280 ret->data.file.reader_cls = reader_cls;
281 ret->data.file.do_index = do_index;
282 ret->data.file.file_size = length;
289 * Closure for "dir_scan_cb".
294 * Metadata extractors to use.
296 struct EXTRACTOR_PluginList *extractors;
301 struct GNUNET_FS_Handle *h;
304 * Function to call on each directory entry.
306 GNUNET_FS_FileProcessor proc;
314 * Scanner to use for subdirectories.
316 GNUNET_FS_DirectoryScanner scanner;
319 * Closure for scanner.
324 * Set to an error message (if any).
331 const struct GNUNET_FS_BlockOptions *bo;
334 * Should files be indexed?
342 * Function called on each entry in a file to
343 * cause default-publishing.
344 * @param cls closure (struct DirScanCls)
345 * @param filename name of the file to be published
346 * @return GNUNET_OK on success, GNUNET_SYSERR to abort
349 dir_scan_cb (void *cls,
350 const char *filename)
352 struct DirScanCls *dsc = cls;
354 struct GNUNET_FS_FileInformation *fi;
355 struct GNUNET_FS_Uri *ksk_uri;
356 struct GNUNET_FS_Uri *keywords;
357 struct GNUNET_CONTAINER_MetaData *meta;
359 if (0 != STAT (filename, &sbuf))
361 GNUNET_asprintf (&dsc->emsg,
362 _("`%s' failed on file `%s': %s"),
366 return GNUNET_SYSERR;
368 if (S_ISDIR (sbuf.st_mode))
370 fi = GNUNET_FS_file_information_create_from_directory (dsc->h,
380 GNUNET_assert (NULL != dsc->emsg);
381 return GNUNET_SYSERR;
386 meta = GNUNET_CONTAINER_meta_data_create ();
387 GNUNET_FS_meta_data_extract_from_file (meta,
390 keywords = GNUNET_FS_uri_ksk_create_from_meta_data (meta);
391 ksk_uri = GNUNET_FS_uri_ksk_canonicalize (keywords);
392 fi = GNUNET_FS_file_information_create_from_file (dsc->h,
399 GNUNET_CONTAINER_meta_data_destroy (meta);
400 GNUNET_FS_uri_destroy (keywords);
401 GNUNET_FS_uri_destroy (ksk_uri);
403 dsc->proc (dsc->proc_cls,
411 * Simple, useful default implementation of a directory scanner
412 * (GNUNET_FS_DirectoryScanner). This implementation expects to get a
413 * UNIX filename, will publish all files in the directory except hidden
414 * files (those starting with a "."). Metadata will be extracted
415 * using GNU libextractor; the specific list of plugins should be
416 * specified in "cls", passing NULL will disable (!) metadata
417 * extraction. Keywords will be derived from the metadata and be
418 * subject to default canonicalization. This is strictly a
419 * convenience function.
421 * @param cls must be of type "struct EXTRACTOR_Extractor*"
422 * @param h handle to the file sharing subsystem
423 * @param dirname name of the directory to scan
424 * @param do_index should files be indexed or inserted
425 * @param bo block options
426 * @param proc function called on each entry
427 * @param proc_cls closure for proc
428 * @param emsg where to store an error message (on errors)
429 * @return GNUNET_OK on success
432 GNUNET_FS_directory_scanner_default (void *cls,
433 struct GNUNET_FS_Handle *h,
436 const struct GNUNET_FS_BlockOptions *bo,
437 GNUNET_FS_FileProcessor proc,
441 struct EXTRACTOR_PluginList *ex = cls;
442 struct DirScanCls dsc;
447 dsc.proc_cls = proc_cls;
448 dsc.scanner = &GNUNET_FS_directory_scanner_default;
449 dsc.scanner_cls = cls;
450 dsc.do_index = do_index;
452 if (-1 == GNUNET_DISK_directory_scan (dirname,
456 GNUNET_assert (NULL != dsc.emsg);
458 return GNUNET_SYSERR;
465 * Closure for dirproc function.
470 * Linked list of directory entries that is being
473 struct GNUNET_FS_FileInformation *entries;
479 * Function that processes a directory entry that
480 * was obtained from the scanner.
481 * @param cls our closure
482 * @param filename name of the file (unused, why there???)
483 * @param fi information for publishing the file
487 const char *filename,
488 struct GNUNET_FS_FileInformation *fi)
490 struct EntryProcCls *dc = cls;
492 GNUNET_assert (fi->next == NULL);
493 GNUNET_assert (fi->dir == NULL);
494 fi->next = dc->entries;
500 * Create a publish-structure from an existing file hierarchy, inferring
501 * and organizing keywords and metadata as much as possible. This
502 * function primarily performs the recursive build and re-organizes
503 * keywords and metadata; for automatically getting metadata
504 * extraction, scanning of directories and creation of the respective
505 * GNUNET_FS_FileInformation entries the default scanner should be
506 * passed (GNUNET_FS_directory_scanner_default). This is strictly a
507 * convenience function.
509 * @param h handle to the file sharing subsystem
510 * @param client_info initial value for the client-info value for this entry
511 * @param filename name of the top-level file or directory
512 * @param scanner function used to get a list of files in a directory
513 * @param scanner_cls closure for scanner
514 * @param do_index should files in the hierarchy be indexed?
515 * @param bo block options
516 * @param emsg where to store an error message
517 * @return publish structure entry for the directory, NULL on error
519 struct GNUNET_FS_FileInformation *
520 GNUNET_FS_file_information_create_from_directory (struct GNUNET_FS_Handle *h,
522 const char *filename,
523 GNUNET_FS_DirectoryScanner scanner,
526 const struct GNUNET_FS_BlockOptions *bo,
529 struct GNUNET_FS_FileInformation *ret;
530 struct EntryProcCls dc;
531 struct GNUNET_FS_Uri *ksk;
532 struct GNUNET_CONTAINER_MetaData *meta;
538 meta = GNUNET_CONTAINER_meta_data_create ();
539 GNUNET_FS_meta_data_make_directory (meta);
540 scanner (scanner_cls,
548 ksk = NULL; // FIXME...
549 // FIXME: create meta!
550 ret = GNUNET_FS_file_information_create_empty_directory (h,
555 GNUNET_CONTAINER_meta_data_destroy (meta);
556 ret->data.dir.entries = dc.entries;
557 while (dc.entries != NULL)
559 dc.entries->dir = ret;
560 dc.entries = dc.entries->next;
563 while ( (NULL != (ss = strstr (fn,
564 DIR_SEPARATOR_STR))) &&
567 GNUNET_asprintf (&dn,
570 GNUNET_CONTAINER_meta_data_insert (ret->meta,
572 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME,
573 EXTRACTOR_METAFORMAT_C_STRING,
578 ret->filename = GNUNET_strdup (filename);
584 * Test if a given entry represents a directory.
586 * @param ent check if this FI represents a directory
587 * @return GNUNET_YES if so, GNUNET_NO if not
590 GNUNET_FS_file_information_is_directory (struct GNUNET_FS_FileInformation *ent)
592 return ent->is_directory;
597 * Create an entry for an empty directory in a publish-structure.
598 * This function should be used by applications for which the
599 * use of "GNUNET_FS_file_information_create_from_directory"
600 * is not appropriate.
602 * @param h handle to the file sharing subsystem
603 * @param client_info initial value for the client-info value for this entry
604 * @param meta metadata for the directory
605 * @param keywords under which keywords should this directory be available
606 * directly; can be NULL
607 * @param bo block options
608 * @return publish structure entry for the directory , NULL on error
610 struct GNUNET_FS_FileInformation *
611 GNUNET_FS_file_information_create_empty_directory (struct GNUNET_FS_Handle *h,
613 const struct GNUNET_FS_Uri *keywords,
614 const struct GNUNET_CONTAINER_MetaData *meta,
615 const struct GNUNET_FS_BlockOptions *bo)
617 struct GNUNET_FS_FileInformation *ret;
619 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_FileInformation));
621 ret->client_info = client_info;
622 ret->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
623 ret->keywords = GNUNET_FS_uri_dup (keywords);
625 ret->is_directory = GNUNET_YES;
631 * Add an entry to a directory in a publish-structure. Clients
632 * should never modify publish structures that were passed to
633 * "GNUNET_FS_publish_start" already.
635 * @param dir the directory
636 * @param ent the entry to add; the entry must not have been
637 * added to any other directory at this point and
638 * must not include "dir" in its structure
639 * @return GNUNET_OK on success, GNUNET_SYSERR on error
642 GNUNET_FS_file_information_add (struct GNUNET_FS_FileInformation *dir,
643 struct GNUNET_FS_FileInformation *ent)
645 if ( (ent->dir != NULL) ||
646 (ent->next != NULL) ||
647 (! dir->is_directory) )
650 return GNUNET_SYSERR;
653 ent->next = dir->data.dir.entries;
654 dir->data.dir.entries = ent;
655 dir->data.dir.dir_size = 0;
661 * Inspect a file or directory in a publish-structure. Clients
662 * should never modify publish structures that were passed to
663 * "GNUNET_FS_publish_start" already. When called on a directory,
664 * this function will FIRST call "proc" with information about
665 * the directory itself and then for each of the files in the
666 * directory (but not for files in subdirectories). When called
667 * on a file, "proc" will be called exactly once (with information
668 * about the specific file).
670 * @param dir the directory
671 * @param proc function to call on each entry
672 * @param proc_cls closure for proc
675 GNUNET_FS_file_information_inspect (struct GNUNET_FS_FileInformation *dir,
676 GNUNET_FS_FileInformationProcessor proc,
679 struct GNUNET_FS_FileInformation *pos;
686 (dir->is_directory) ? dir->data.dir.dir_size : dir->data.file.file_size,
690 (dir->is_directory) ? &no : &dir->data.file.do_index,
693 if (! dir->is_directory)
695 pos = dir->data.dir.entries;
702 (pos->is_directory) ? pos->data.dir.dir_size : pos->data.file.file_size,
706 (dir->is_directory) ? &no : &dir->data.file.do_index,
715 * Destroy publish-structure. Clients should never destroy publish
716 * structures that were passed to "GNUNET_FS_publish_start" already.
718 * @param fi structure to destroy
719 * @param cleaner function to call on each entry in the structure
720 * (useful to clean up client_info); can be NULL; return
722 * @param cleaner_cls closure for cleaner
725 GNUNET_FS_file_information_destroy (struct GNUNET_FS_FileInformation *fi,
726 GNUNET_FS_FileInformationProcessor cleaner,
729 struct GNUNET_FS_FileInformation *pos;
733 if (fi->is_directory)
735 /* clean up directory */
736 while (NULL != (pos = fi->data.dir.entries))
738 fi->data.dir.entries = pos->next;
739 GNUNET_FS_file_information_destroy (pos, cleaner, cleaner_cls);
741 /* clean up client-info */
743 cleaner (cleaner_cls,
745 fi->data.dir.dir_size,
751 GNUNET_free_non_null (fi->data.dir.dir_data);
755 /* call clean-up function of the reader */
756 if (fi->data.file.reader != NULL)
757 fi->data.file.reader (fi->data.file.reader_cls, 0, 0,
759 /* clean up client-info */
761 cleaner (cleaner_cls,
763 fi->data.file.file_size,
767 &fi->data.file.do_index,
770 GNUNET_free_non_null (fi->filename);
771 GNUNET_free_non_null (fi->emsg);
772 GNUNET_free_non_null (fi->chk_uri);
773 /* clean up serialization */
774 if ( (NULL != fi->serialization) &&
775 (0 != UNLINK (fi->serialization)) )
776 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
779 if (NULL != fi->keywords)
780 GNUNET_FS_uri_destroy (fi->keywords);
781 if (NULL != fi->meta)
782 GNUNET_CONTAINER_meta_data_destroy (fi->meta);
783 GNUNET_free_non_null (fi->serialization);
786 GNUNET_FS_tree_encoder_finish (fi->te,
794 /* end of fs_file_information.c */