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"
38 * Create a temporary file on disk to store the current
42 GNUNET_FS_file_information_sync (struct GNUNET_FS_FileInformation * fi)
44 if (NULL == fi->serialization)
46 fi->serialization = NULL; // FIXME -- need cfg!
53 * Load file information from the file to which
56 * @param filename name of the file to use
57 * @return NULL on error
59 struct GNUNET_FS_FileInformation *
60 GNUNET_FS_file_information_recover (const char *name)
62 struct GNUNET_FS_FileInformation *ret;
69 * Obtain the name under which this file information
70 * structure is stored on disk. Only works for top-level
71 * file information structures.
73 * @param s structure to get the filename for
74 * @return NULL on error, otherwise filename that
75 * can be passed to "GNUNET_FS_file_information_recover"
76 * to read this fi-struct from disk.
79 GNUNET_FS_file_information_get_id (struct GNUNET_FS_FileInformation *s)
83 return s->serialization;
88 * Closure for "data_reader_file".
93 * Name of the file to read.
98 * File descriptor, NULL if it has not yet been opened.
100 struct GNUNET_DISK_FileHandle *fd;
105 * Function that provides data by reading from a file.
107 * @param cls closure (points to the file information)
108 * @param offset offset to read from; it is possible
109 * that the caller might need to go backwards
111 * @param max maximum number of bytes that should be
112 * copied to buf; readers are not allowed
113 * to provide less data unless there is an error;
114 * a value of "0" will be used at the end to allow
115 * the reader to clean up its internal state
116 * @param buf where the reader should write the data
117 * @param emsg location for the reader to store an error message
118 * @return number of bytes written, usually "max", 0 on error
121 data_reader_file(void *cls,
127 struct FileInfo *fi = cls;
133 GNUNET_DISK_file_close (fi->fd);
134 GNUNET_free (fi->filename);
140 fi->fd = GNUNET_DISK_file_open (fi->filename,
141 GNUNET_DISK_OPEN_READ);
144 GNUNET_asprintf (emsg,
145 _("Could not open file `%s': %s"),
151 GNUNET_DISK_file_seek (fi->fd, offset, GNUNET_DISK_SEEK_SET);
152 ret = GNUNET_DISK_file_read (fi->fd, buf, max);
155 GNUNET_asprintf (emsg,
156 _("Could not read file `%s': %s"),
163 GNUNET_asprintf (emsg,
164 _("Short read reading from file `%s'!"),
173 * Create an entry for a file in a publish-structure.
175 * @param filename name of the file or directory to publish
176 * @param keywords under which keywords should this file be available
177 * directly; can be NULL
178 * @param meta metadata for the file
179 * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
180 * GNUNET_SYSERR for simulation
181 * @param anonymity what is the desired anonymity level for sharing?
182 * @param priority what is the priority for OUR node to
183 * keep this file available? Use 0 for maximum anonymity and
184 * minimum reliability...
185 * @param expirationTime when should this content expire?
186 * @return publish structure entry for the file
188 struct GNUNET_FS_FileInformation *
189 GNUNET_FS_file_information_create_from_file (void *client_info,
190 const char *filename,
191 const struct GNUNET_FS_Uri *keywords,
192 const struct GNUNET_CONTAINER_MetaData *meta,
194 unsigned int anonymity,
195 unsigned int priority,
196 struct GNUNET_TIME_Absolute expirationTime)
201 if (0 != STAT (filename, &sbuf))
203 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
208 fi = GNUNET_malloc (sizeof(struct FileInfo));
209 fi->filename = GNUNET_strdup (filename);
210 return GNUNET_FS_file_information_create_from_reader (client_info,
224 * Function that provides data by copying from a buffer.
226 * @param cls closure (points to the buffer)
227 * @param offset offset to read from; it is possible
228 * that the caller might need to go backwards
230 * @param max maximum number of bytes that should be
231 * copied to buf; readers are not allowed
232 * to provide less data unless there is an error;
233 * a value of "0" will be used at the end to allow
234 * the reader to clean up its internal state
235 * @param buf where the reader should write the data
236 * @param emsg location for the reader to store an error message
237 * @return number of bytes written, usually "max", 0 on error
240 data_reader_copy(void *cls,
252 memcpy (buf, &data[offset], max);
258 * Create an entry for a file in a publish-structure.
260 * @param length length of the file
261 * @param data data for the file (should not be used afterwards by
262 * the caller; caller will "free")
263 * @param keywords under which keywords should this file be available
264 * directly; can be NULL
265 * @param meta metadata for the file
266 * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
267 * GNUNET_SYSERR for simulation
268 * @param anonymity what is the desired anonymity level for sharing?
269 * @param priority what is the priority for OUR node to
270 * keep this file available? Use 0 for maximum anonymity and
271 * minimum reliability...
272 * @param expirationTime when should this content expire?
273 * @return publish structure entry for the file
275 struct GNUNET_FS_FileInformation *
276 GNUNET_FS_file_information_create_from_data (void *client_info,
279 const struct GNUNET_FS_Uri *keywords,
280 const struct GNUNET_CONTAINER_MetaData *meta,
282 unsigned int anonymity,
283 unsigned int priority,
284 struct GNUNET_TIME_Absolute expirationTime)
286 return GNUNET_FS_file_information_create_from_reader (client_info,
300 * Create an entry for a file in a publish-structure.
302 * @param length length of the file
303 * @param reader function that can be used to obtain the data for the file
304 * @param reader_cls closure for "reader"
305 * @param keywords under which keywords should this file be available
306 * directly; can be NULL
307 * @param meta metadata for the file
308 * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
309 * GNUNET_SYSERR for simulation
310 * @param anonymity what is the desired anonymity level for sharing?
311 * @param priority what is the priority for OUR node to
312 * keep this file available? Use 0 for maximum anonymity and
313 * minimum reliability...
314 * @param expirationTime when should this content expire?
315 * @return publish structure entry for the file
317 struct GNUNET_FS_FileInformation *
318 GNUNET_FS_file_information_create_from_reader (void *client_info,
320 GNUNET_FS_DataReader reader,
322 const struct GNUNET_FS_Uri *keywords,
323 const struct GNUNET_CONTAINER_MetaData *meta,
325 unsigned int anonymity,
326 unsigned int priority,
327 struct GNUNET_TIME_Absolute expirationTime)
329 struct GNUNET_FS_FileInformation *ret;
331 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_FileInformation));
332 ret->client_info = client_info;
333 ret->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
334 ret->keywords = (keywords == NULL) ? NULL : GNUNET_FS_uri_dup (keywords);
335 ret->expirationTime = expirationTime;
336 ret->data.file.reader = reader;
337 ret->data.file.reader_cls = reader_cls;
338 ret->data.file.do_index = do_index;
339 ret->anonymity = anonymity;
340 ret->priority = priority;
341 GNUNET_FS_file_information_sync (ret);
347 * Closure for "dir_scan_cb".
352 * Metadata extractors to use.
354 struct EXTRACTOR_Extractor *extractors;
357 * Function to call on each directory entry.
359 GNUNET_FS_FileProcessor proc;
367 * Scanner to use for subdirectories.
369 GNUNET_FS_DirectoryScanner scanner;
372 * Closure for scanner.
377 * Set to an error message (if any).
382 * Should files be indexed?
387 * Desired anonymity level.
389 unsigned int anonymity;
392 * Desired publishing priority.
394 unsigned int priority;
397 * Expiration time for publication.
399 struct GNUNET_TIME_Absolute expiration;
404 * Function called on each entry in a file to
405 * cause default-publishing.
406 * @param cls closure (struct DirScanCls)
407 * @param filename name of the file to be published
408 * @return GNUNET_OK on success, GNUNET_SYSERR to abort
411 dir_scan_cb (void *cls,
412 const char *filename)
414 struct DirScanCls *dsc = cls;
416 struct GNUNET_FS_FileInformation *fi;
417 struct GNUNET_FS_Uri *ksk_uri;
418 struct GNUNET_FS_Uri *keywords;
419 struct GNUNET_CONTAINER_MetaData *meta;
421 if (0 != STAT (filename, &sbuf))
423 GNUNET_asprintf (&dsc->emsg,
424 _("`%s' failed on file `%s': %s"),
428 return GNUNET_SYSERR;
430 if (S_ISDIR (sbuf.st_mode))
432 fi = GNUNET_FS_file_information_create_from_directory (NULL,
443 GNUNET_assert (NULL != dsc->emsg);
444 return GNUNET_SYSERR;
449 meta = GNUNET_CONTAINER_meta_data_create ();
450 GNUNET_CONTAINER_meta_data_extract_from_file (meta,
453 // FIXME: remove path from filename in metadata!
454 keywords = GNUNET_FS_uri_ksk_create_from_meta_data (meta);
455 ksk_uri = GNUNET_FS_uri_ksk_canonicalize (keywords);
456 fi = GNUNET_FS_file_information_create_from_file (NULL,
464 GNUNET_CONTAINER_meta_data_destroy (meta);
465 GNUNET_FS_uri_destroy (keywords);
466 GNUNET_FS_uri_destroy (ksk_uri);
468 dsc->proc (dsc->proc_cls,
476 * Simple, useful default implementation of a directory scanner
477 * (GNUNET_FS_DirectoryScanner). This implementation expects to get a
478 * UNIX filename, will publish all files in the directory except hidden
479 * files (those starting with a "."). Metadata will be extracted
480 * using GNU libextractor; the specific list of plugins should be
481 * specified in "cls", passing NULL will disable (!) metadata
482 * extraction. Keywords will be derived from the metadata and be
483 * subject to default canonicalization. This is strictly a
484 * convenience function.
486 * @param cls must be of type "struct EXTRACTOR_Extractor*"
487 * @param dirname name of the directory to scan
488 * @param do_index should files be indexed or inserted
489 * @param anonymity desired anonymity level
490 * @param priority priority for publishing
491 * @param expirationTime expiration for publication
492 * @param proc function called on each entry
493 * @param proc_cls closure for proc
494 * @param emsg where to store an error message (on errors)
495 * @return GNUNET_OK on success
498 GNUNET_FS_directory_scanner_default (void *cls,
501 unsigned int anonymity,
502 unsigned int priority,
503 struct GNUNET_TIME_Absolute expirationTime,
504 GNUNET_FS_FileProcessor proc,
508 struct EXTRACTOR_Extractor *ex = cls;
509 struct DirScanCls dsc;
513 dsc.proc_cls = proc_cls;
514 dsc.scanner = &GNUNET_FS_directory_scanner_default;
515 dsc.scanner_cls = cls;
516 dsc.do_index = do_index;
517 dsc.anonymity = anonymity;
518 dsc.priority = priority;
519 dsc.expiration = expirationTime;
520 if (-1 == GNUNET_DISK_directory_scan (dirname,
524 GNUNET_assert (NULL != dsc.emsg);
526 return GNUNET_SYSERR;
533 * Closure for dirproc function.
538 * Linked list of directory entries that is being
541 struct GNUNET_FS_FileInformation *entries;
547 * Function that processes a directory entry that
548 * was obtained from the scanner.
549 * @param cls our closure
550 * @param filename name of the file (unused, why there???)
551 * @param fi information for publishing the file
555 const char *filename,
556 struct GNUNET_FS_FileInformation *fi)
558 struct EntryProcCls *dc = cls;
560 GNUNET_assert (fi->next == NULL);
561 GNUNET_assert (fi->dir == NULL);
562 fi->next = dc->entries;
568 * Create a publish-structure from an existing file hierarchy, inferring
569 * and organizing keywords and metadata as much as possible. This
570 * function primarily performs the recursive build and re-organizes
571 * keywords and metadata; for automatically getting metadata
572 * extraction, scanning of directories and creation of the respective
573 * GNUNET_FS_FileInformation entries the default scanner should be
574 * passed (GNUNET_FS_directory_scanner_default). This is strictly a
575 * convenience function.
577 * @param filename name of the top-level file or directory
578 * @param scanner function used to get a list of files in a directory
579 * @param scanner_cls closure for scanner
580 * @param do_index should files in the hierarchy be indexed?
581 * @param anonymity what is the desired anonymity level for sharing?
582 * @param priority what is the priority for OUR node to
583 * keep this file available? Use 0 for maximum anonymity and
584 * minimum reliability...
585 * @param expirationTime when should this content expire?
586 * @param emsg where to store an error message
587 * @return publish structure entry for the directory, NULL on error
589 struct GNUNET_FS_FileInformation *
590 GNUNET_FS_file_information_create_from_directory (void *client_info,
591 const char *filename,
592 GNUNET_FS_DirectoryScanner scanner,
595 unsigned int anonymity,
596 unsigned int priority,
597 struct GNUNET_TIME_Absolute expirationTime,
600 struct GNUNET_FS_FileInformation *ret;
601 struct EntryProcCls dc;
602 struct GNUNET_FS_Uri *ksk;
603 struct GNUNET_CONTAINER_MetaData *meta;
606 meta = GNUNET_CONTAINER_meta_data_create ();
607 GNUNET_FS_meta_data_make_directory (meta);
609 scanner (scanner_cls,
618 ksk = NULL; // FIXME...
619 // FIXME: create meta!
620 ret = GNUNET_FS_file_information_create_empty_directory (client_info,
626 ret->data.dir.entries = dc.entries;
627 while (dc.entries != NULL)
629 dc.entries->dir = ret;
630 GNUNET_FS_file_information_sync (dc.entries);
631 dc.entries = dc.entries->next;
633 GNUNET_FS_file_information_sync (ret);
639 * Create an entry for an empty directory in a publish-structure.
640 * This function should be used by applications for which the
641 * use of "GNUNET_FS_file_information_create_from_directory"
642 * is not appropriate.
644 * @param meta metadata for the directory
645 * @param keywords under which keywords should this directory be available
646 * directly; can be NULL
647 * @param anonymity what is the desired anonymity level for sharing?
648 * @param priority what is the priority for OUR node to
649 * keep this file available? Use 0 for maximum anonymity and
650 * minimum reliability...
651 * @param expirationTime when should this content expire?
652 * @return publish structure entry for the directory , NULL on error
654 struct GNUNET_FS_FileInformation *
655 GNUNET_FS_file_information_create_empty_directory (void *client_info,
656 const struct GNUNET_CONTAINER_MetaData *meta,
657 const struct GNUNET_FS_Uri *keywords,
658 unsigned int anonymity,
659 unsigned int priority,
660 struct GNUNET_TIME_Absolute expirationTime)
662 struct GNUNET_FS_FileInformation *ret;
664 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_FileInformation));
665 ret->client_info = client_info;
666 ret->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
667 ret->keywords = GNUNET_FS_uri_dup (keywords);
668 ret->expirationTime = expirationTime;
669 ret->is_directory = GNUNET_YES;
670 ret->anonymity = anonymity;
671 ret->priority = priority;
672 GNUNET_FS_file_information_sync (ret);
678 * Add an entry to a directory in a publish-structure. Clients
679 * should never modify publish structures that were passed to
680 * "GNUNET_FS_publish_start" already.
682 * @param dir the directory
683 * @param ent the entry to add; the entry must not have been
684 * added to any other directory at this point and
685 * must not include "dir" in its structure
686 * @return GNUNET_OK on success, GNUNET_SYSERR on error
689 GNUNET_FS_file_information_add (struct GNUNET_FS_FileInformation *dir,
690 struct GNUNET_FS_FileInformation *ent)
692 if ( (ent->dir != NULL) ||
693 (ent->next != NULL) ||
694 (! dir->is_directory) )
697 return GNUNET_SYSERR;
700 ent->next = dir->data.dir.entries;
701 dir->data.dir.entries = ent;
702 dir->data.dir.dir_size = 0;
703 dir->publish_offset = 0;
704 GNUNET_FS_file_information_sync (ent);
705 GNUNET_FS_file_information_sync (dir);
711 * Inspect a file or directory in a publish-structure. Clients
712 * should never modify publish structures that were passed to
713 * "GNUNET_FS_publish_start" already. When called on a directory,
714 * this function will FIRST call "proc" with information about
715 * the directory itself and then for each of the files in the
716 * directory (but not for files in subdirectories). When called
717 * on a file, "proc" will be called exactly once (with information
718 * about the specific file).
720 * @param dir the directory
721 * @param proc function to call on each entry
722 * @param proc_cls closure for proc
725 GNUNET_FS_file_information_inspect (struct GNUNET_FS_FileInformation *dir,
726 GNUNET_FS_FileInformationProcessor proc,
729 struct GNUNET_FS_FileInformation *pos;
731 if (dir->is_directory)
735 dir->data.dir.dir_size,
740 &dir->expirationTime,
742 pos = dir->data.dir.entries;
747 pos->data.dir.dir_size,
752 &pos->expirationTime,
761 dir->data.file.file_size,
766 &dir->expirationTime,
773 * Destroy publish-structure. Clients should never destroy publish
774 * structures that were passed to "GNUNET_FS_publish_start" already.
776 * @param fi structure to destroy
777 * @param cleaner function to call on each entry in the structure
778 * (useful to clean up client_info); can be NULL; return
780 * @param cleaner_cls closure for cleaner
783 GNUNET_FS_file_information_destroy (struct GNUNET_FS_FileInformation *fi,
784 GNUNET_FS_FileInformationProcessor cleaner,
787 struct GNUNET_FS_FileInformation *pos;
789 if (fi->is_directory)
791 /* clean up directory */
792 while (NULL != (pos = fi->data.dir.entries))
794 fi->data.dir.entries = pos->next;
795 GNUNET_FS_file_information_destroy (pos, cleaner, cleaner_cls);
797 /* clean up client-info */
798 cleaner (cleaner_cls,
800 fi->data.dir.dir_size,
807 GNUNET_free_non_null (fi->data.dir.dir_data);
808 GNUNET_free (fi->data.dir.dirname);
812 /* call clean-up function of the reader */
813 fi->data.file.reader (fi->data.file.reader_cls, 0, 0, NULL, NULL);
814 /* clean up client-info */
815 cleaner (cleaner_cls,
817 fi->data.file.file_size,
825 GNUNET_free_non_null (fi->chk_tree);
826 /* clean up serialization */
827 if (0 != UNLINK (fi->serialization))
828 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
831 GNUNET_FS_uri_destroy (fi->keywords);
832 GNUNET_CONTAINER_meta_data_destroy (fi->meta);
833 GNUNET_free (fi->serialization);
838 /* end of fs_file_information.c */