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, const char *data, size_t data_len)
61 struct GNUNET_CONTAINER_MetaData *md = cls;
63 (void) GNUNET_CONTAINER_meta_data_insert (md,
67 data_mime_type, data, data_len);
73 * Extract meta-data from a file.
75 * @return GNUNET_SYSERR on error, otherwise the number
76 * of meta-data items obtained
79 GNUNET_FS_meta_data_extract_from_file (struct GNUNET_CONTAINER_MetaData
80 *md, const char *filename,
81 struct EXTRACTOR_PluginList *extractors)
87 if (extractors == NULL)
89 old = GNUNET_CONTAINER_meta_data_iterate (md, NULL, NULL);
90 GNUNET_assert (old >= 0);
91 EXTRACTOR_extract (extractors, filename, NULL, 0, &add_to_md, md);
92 return (GNUNET_CONTAINER_meta_data_iterate (md, NULL, NULL) - old);
98 * Obtain the name under which this file information
99 * structure is stored on disk. Only works for top-level
100 * file information structures.
102 * @param s structure to get the filename for
103 * @return NULL on error, otherwise filename that
104 * can be passed to "GNUNET_FS_file_information_recover"
105 * to read this fi-struct from disk.
108 GNUNET_FS_file_information_get_id (struct GNUNET_FS_FileInformation *s)
112 return s->serialization;
117 * Create an entry for a file in a publish-structure.
119 * @param h handle to the file sharing subsystem
120 * @param client_info initial value for the client-info value for this entry
121 * @param filename name of the file or directory to publish
122 * @param keywords under which keywords should this file be available
123 * directly; can be NULL
124 * @param meta metadata for the file
125 * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
126 * GNUNET_SYSERR for simulation
127 * @param bo block options
128 * @return publish structure entry for the file
130 struct GNUNET_FS_FileInformation *
131 GNUNET_FS_file_information_create_from_file (struct GNUNET_FS_Handle *h,
133 const char *filename,
134 const struct GNUNET_FS_Uri
137 GNUNET_CONTAINER_MetaData *meta,
139 const struct GNUNET_FS_BlockOptions
144 struct GNUNET_FS_FileInformation *ret;
149 char fn_conv[MAX_PATH];
152 if (0 != STAT (filename, &sbuf))
154 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", filename);
157 fi = GNUNET_FS_make_file_reader_context_ (filename);
163 ret = GNUNET_FS_file_information_create_from_reader (h,
166 &GNUNET_FS_data_reader_file_,
173 ret->filename = GNUNET_strdup (filename);
177 plibc_conv_to_win_path (filename, fn_conv);
180 while (NULL != (ss = strstr (fn, DIR_SEPARATOR_STR)))
182 GNUNET_CONTAINER_meta_data_insert (ret->meta,
184 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME,
185 EXTRACTOR_METAFORMAT_C_STRING,
186 "text/plain", fn, strlen (fn) + 1);
192 * Create an entry for a file in a publish-structure.
194 * @param h handle to the file sharing subsystem
195 * @param client_info initial value for the client-info value for this entry
196 * @param length length of the file
197 * @param data data for the file (should not be used afterwards by
198 * the caller; callee will "free")
199 * @param keywords under which keywords should this file be available
200 * directly; can be NULL
201 * @param meta metadata for the file
202 * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
203 * GNUNET_SYSERR for simulation
204 * @param bo block options
205 * @return publish structure entry for the file
207 struct GNUNET_FS_FileInformation *
208 GNUNET_FS_file_information_create_from_data (struct GNUNET_FS_Handle *h,
212 const struct GNUNET_FS_Uri
215 GNUNET_CONTAINER_MetaData *meta,
217 const struct GNUNET_FS_BlockOptions
220 if (GNUNET_YES == do_index)
225 return GNUNET_FS_file_information_create_from_reader (h,
228 &GNUNET_FS_data_reader_copy_,
236 * Create an entry for a file in a publish-structure.
238 * @param h handle to the file sharing subsystem
239 * @param client_info initial value for the client-info value for this entry
240 * @param length length of the file
241 * @param reader function that can be used to obtain the data for the file
242 * @param reader_cls closure for "reader"
243 * @param keywords under which keywords should this file be available
244 * directly; can be NULL
245 * @param meta metadata for the file
246 * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
247 * GNUNET_SYSERR for simulation
248 * @param bo block options
249 * @return publish structure entry for the file
251 struct GNUNET_FS_FileInformation *
252 GNUNET_FS_file_information_create_from_reader (struct GNUNET_FS_Handle *h,
255 GNUNET_FS_DataReader reader,
257 const struct GNUNET_FS_Uri
260 GNUNET_CONTAINER_MetaData *meta,
263 GNUNET_FS_BlockOptions *bo)
265 struct GNUNET_FS_FileInformation *ret;
267 if ((GNUNET_YES == do_index) && (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, const char *filename)
351 struct DirScanCls *dsc = cls;
353 struct GNUNET_FS_FileInformation *fi;
354 struct GNUNET_FS_Uri *ksk_uri;
355 struct GNUNET_FS_Uri *keywords;
356 struct GNUNET_CONTAINER_MetaData *meta;
358 if (0 != STAT (filename, &sbuf))
360 GNUNET_asprintf (&dsc->emsg,
361 _("`%s' failed on file `%s': %s"),
362 "stat", filename, STRERROR (errno));
363 return GNUNET_SYSERR;
365 if (S_ISDIR (sbuf.st_mode))
367 fi = GNUNET_FS_file_information_create_from_directory (dsc->h,
373 dsc->bo, &dsc->emsg);
376 GNUNET_assert (NULL != dsc->emsg);
377 return GNUNET_SYSERR;
382 meta = GNUNET_CONTAINER_meta_data_create ();
383 GNUNET_FS_meta_data_extract_from_file (meta, filename, dsc->extractors);
384 keywords = GNUNET_FS_uri_ksk_create_from_meta_data (meta);
385 ksk_uri = GNUNET_FS_uri_ksk_canonicalize (keywords);
386 fi = GNUNET_FS_file_information_create_from_file (dsc->h,
391 dsc->do_index, dsc->bo);
392 GNUNET_CONTAINER_meta_data_destroy (meta);
393 GNUNET_FS_uri_destroy (keywords);
394 GNUNET_FS_uri_destroy (ksk_uri);
396 dsc->proc (dsc->proc_cls, filename, fi);
402 * Simple, useful default implementation of a directory scanner
403 * (GNUNET_FS_DirectoryScanner). This implementation expects to get a
404 * UNIX filename, will publish all files in the directory except hidden
405 * files (those starting with a "."). Metadata will be extracted
406 * using GNU libextractor; the specific list of plugins should be
407 * specified in "cls", passing NULL will disable (!) metadata
408 * extraction. Keywords will be derived from the metadata and be
409 * subject to default canonicalization. This is strictly a
410 * convenience function.
412 * @param cls must be of type "struct EXTRACTOR_Extractor*"
413 * @param h handle to the file sharing subsystem
414 * @param dirname name of the directory to scan
415 * @param do_index should files be indexed or inserted
416 * @param bo block options
417 * @param proc function called on each entry
418 * @param proc_cls closure for proc
419 * @param emsg where to store an error message (on errors)
420 * @return GNUNET_OK on success
423 GNUNET_FS_directory_scanner_default (void *cls,
424 struct GNUNET_FS_Handle *h,
427 const struct GNUNET_FS_BlockOptions *bo,
428 GNUNET_FS_FileProcessor proc,
429 void *proc_cls, char **emsg)
431 struct EXTRACTOR_PluginList *ex = cls;
432 struct DirScanCls dsc;
437 dsc.proc_cls = proc_cls;
438 dsc.scanner = &GNUNET_FS_directory_scanner_default;
439 dsc.scanner_cls = cls;
440 dsc.do_index = do_index;
442 if (-1 == GNUNET_DISK_directory_scan (dirname, &dir_scan_cb, &dsc))
444 GNUNET_assert (NULL != dsc.emsg);
446 return GNUNET_SYSERR;
453 * Closure for dirproc function.
458 * Linked list of directory entries that is being
461 struct GNUNET_FS_FileInformation *entries;
467 * Function that processes a directory entry that
468 * was obtained from the scanner.
469 * @param cls our closure
470 * @param filename name of the file (unused, why there???)
471 * @param fi information for publishing the file
474 dirproc (void *cls, const char *filename, struct GNUNET_FS_FileInformation *fi)
476 struct EntryProcCls *dc = cls;
478 GNUNET_assert (fi->next == NULL);
479 GNUNET_assert (fi->dir == NULL);
480 fi->next = dc->entries;
486 * Create a publish-structure from an existing file hierarchy, inferring
487 * and organizing keywords and metadata as much as possible. This
488 * function primarily performs the recursive build and re-organizes
489 * keywords and metadata; for automatically getting metadata
490 * extraction, scanning of directories and creation of the respective
491 * GNUNET_FS_FileInformation entries the default scanner should be
492 * passed (GNUNET_FS_directory_scanner_default). This is strictly a
493 * convenience function.
495 * @param h handle to the file sharing subsystem
496 * @param client_info initial value for the client-info value for this entry
497 * @param filename name of the top-level file or directory
498 * @param scanner function used to get a list of files in a directory
499 * @param scanner_cls closure for scanner
500 * @param do_index should files in the hierarchy be indexed?
501 * @param bo block options
502 * @param emsg where to store an error message
503 * @return publish structure entry for the directory, NULL on error
505 struct GNUNET_FS_FileInformation *
506 GNUNET_FS_file_information_create_from_directory (struct GNUNET_FS_Handle *h,
508 const char *filename,
509 GNUNET_FS_DirectoryScanner
510 scanner, void *scanner_cls,
513 GNUNET_FS_BlockOptions *bo,
516 struct GNUNET_FS_FileInformation *ret;
517 struct EntryProcCls dc;
518 struct GNUNET_FS_Uri *ksk;
519 struct GNUNET_CONTAINER_MetaData *meta;
525 meta = GNUNET_CONTAINER_meta_data_create ();
526 GNUNET_FS_meta_data_make_directory (meta);
527 scanner (scanner_cls, h, filename, do_index, bo, &dirproc, &dc, emsg);
528 ksk = NULL; // FIXME...
529 // FIXME: create meta!
530 ret = GNUNET_FS_file_information_create_empty_directory (h,
533 GNUNET_CONTAINER_meta_data_destroy (meta);
534 ret->data.dir.entries = dc.entries;
535 while (dc.entries != NULL)
537 dc.entries->dir = ret;
538 dc.entries = dc.entries->next;
541 while ((NULL != (ss = strstr (fn, DIR_SEPARATOR_STR))) && (strlen (ss) > 1))
543 GNUNET_asprintf (&dn, "%s/", fn);
544 GNUNET_CONTAINER_meta_data_insert (ret->meta,
546 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME,
547 EXTRACTOR_METAFORMAT_C_STRING,
548 "text/plain", dn, strlen (dn) + 1);
550 ret->filename = GNUNET_strdup (filename);
556 * Test if a given entry represents a directory.
558 * @param ent check if this FI represents a directory
559 * @return GNUNET_YES if so, GNUNET_NO if not
562 GNUNET_FS_file_information_is_directory (const struct GNUNET_FS_FileInformation
565 return ent->is_directory;
570 * Create an entry for an empty directory in a publish-structure.
571 * This function should be used by applications for which the
572 * use of "GNUNET_FS_file_information_create_from_directory"
573 * is not appropriate.
575 * @param h handle to the file sharing subsystem
576 * @param client_info initial value for the client-info value for this entry
577 * @param meta metadata for the directory
578 * @param keywords under which keywords should this directory be available
579 * directly; can be NULL
580 * @param bo block options
581 * @return publish structure entry for the directory , NULL on error
583 struct GNUNET_FS_FileInformation *
584 GNUNET_FS_file_information_create_empty_directory (struct GNUNET_FS_Handle *h,
586 const struct GNUNET_FS_Uri
589 GNUNET_CONTAINER_MetaData
592 GNUNET_FS_BlockOptions *bo)
594 struct GNUNET_FS_FileInformation *ret;
596 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_FileInformation));
598 ret->client_info = client_info;
599 ret->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
600 ret->keywords = GNUNET_FS_uri_dup (keywords);
602 ret->is_directory = GNUNET_YES;
608 * Add an entry to a directory in a publish-structure. Clients
609 * should never modify publish structures that were passed to
610 * "GNUNET_FS_publish_start" already.
612 * @param dir the directory
613 * @param ent the entry to add; the entry must not have been
614 * added to any other directory at this point and
615 * must not include "dir" in its structure
616 * @return GNUNET_OK on success, GNUNET_SYSERR on error
619 GNUNET_FS_file_information_add (struct GNUNET_FS_FileInformation *dir,
620 struct GNUNET_FS_FileInformation *ent)
622 if ((ent->dir != NULL) || (ent->next != NULL) || (!dir->is_directory))
625 return GNUNET_SYSERR;
628 ent->next = dir->data.dir.entries;
629 dir->data.dir.entries = ent;
630 dir->data.dir.dir_size = 0;
636 * Inspect a file or directory in a publish-structure. Clients
637 * should never modify publish structures that were passed to
638 * "GNUNET_FS_publish_start" already. When called on a directory,
639 * this function will FIRST call "proc" with information about
640 * the directory itself and then for each of the files in the
641 * directory (but not for files in subdirectories). When called
642 * on a file, "proc" will be called exactly once (with information
643 * about the specific file).
645 * @param dir the directory
646 * @param proc function to call on each entry
647 * @param proc_cls closure for proc
650 GNUNET_FS_file_information_inspect (struct GNUNET_FS_FileInformation *dir,
651 GNUNET_FS_FileInformationProcessor proc,
654 struct GNUNET_FS_FileInformation *pos;
661 (dir->is_directory) ? dir->data.dir.dir_size : dir->data.
662 file.file_size, dir->meta, &dir->keywords, &dir->bo,
663 (dir->is_directory) ? &no : &dir->data.file.do_index,
666 if (!dir->is_directory)
668 pos = dir->data.dir.entries;
675 (pos->is_directory) ? pos->data.dir.dir_size : pos->data.
676 file.file_size, pos->meta, &pos->keywords, &pos->bo,
677 (dir->is_directory) ? &no : &dir->data.file.do_index,
686 * Destroy publish-structure. Clients should never destroy publish
687 * structures that were passed to "GNUNET_FS_publish_start" already.
689 * @param fi structure to destroy
690 * @param cleaner function to call on each entry in the structure
691 * (useful to clean up client_info); can be NULL; return
693 * @param cleaner_cls closure for cleaner
696 GNUNET_FS_file_information_destroy (struct GNUNET_FS_FileInformation *fi,
697 GNUNET_FS_FileInformationProcessor cleaner,
700 struct GNUNET_FS_FileInformation *pos;
704 if (fi->is_directory)
706 /* clean up directory */
707 while (NULL != (pos = fi->data.dir.entries))
709 fi->data.dir.entries = pos->next;
710 GNUNET_FS_file_information_destroy (pos, cleaner, cleaner_cls);
712 /* clean up client-info */
714 cleaner (cleaner_cls,
716 fi->data.dir.dir_size,
717 fi->meta, &fi->keywords, &fi->bo, &no, &fi->client_info);
718 GNUNET_free_non_null (fi->data.dir.dir_data);
722 /* call clean-up function of the reader */
723 if (fi->data.file.reader != NULL)
724 fi->data.file.reader (fi->data.file.reader_cls, 0, 0, NULL, NULL);
725 /* clean up client-info */
727 cleaner (cleaner_cls,
729 fi->data.file.file_size,
732 &fi->bo, &fi->data.file.do_index, &fi->client_info);
734 GNUNET_free_non_null (fi->filename);
735 GNUNET_free_non_null (fi->emsg);
736 GNUNET_free_non_null (fi->chk_uri);
737 /* clean up serialization */
738 if ((NULL != fi->serialization) && (0 != UNLINK (fi->serialization)))
739 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
740 "unlink", fi->serialization);
741 if (NULL != fi->keywords)
742 GNUNET_FS_uri_destroy (fi->keywords);
743 if (NULL != fi->meta)
744 GNUNET_CONTAINER_meta_data_destroy (fi->meta);
745 GNUNET_free_non_null (fi->serialization);
748 GNUNET_FS_tree_encoder_finish (fi->te, NULL, NULL);
755 /* end of fs_file_information.c */