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
41 * @param fi file information to sync with disk
44 GNUNET_FS_file_information_sync (struct GNUNET_FS_FileInformation * fi)
46 if (NULL == fi->serialization)
48 fi->serialization = NULL; // FIXME -- need cfg!
55 * Load file information from the file to which
58 * @param fn name of the file to use
59 * @return NULL on error
61 struct GNUNET_FS_FileInformation *
62 GNUNET_FS_file_information_recover (const char *fn)
64 struct GNUNET_FS_FileInformation *ret;
72 * Obtain the name under which this file information
73 * structure is stored on disk. Only works for top-level
74 * file information structures.
76 * @param s structure to get the filename for
77 * @return NULL on error, otherwise filename that
78 * can be passed to "GNUNET_FS_file_information_recover"
79 * to read this fi-struct from disk.
82 GNUNET_FS_file_information_get_id (struct GNUNET_FS_FileInformation *s)
86 return s->serialization;
91 * Closure for "data_reader_file".
96 * Name of the file to read.
101 * File descriptor, NULL if it has not yet been opened.
103 struct GNUNET_DISK_FileHandle *fd;
108 * Function that provides data by reading from a file.
110 * @param cls closure (points to the file information)
111 * @param offset offset to read from; it is possible
112 * that the caller might need to go backwards
114 * @param max maximum number of bytes that should be
115 * copied to buf; readers are not allowed
116 * to provide less data unless there is an error;
117 * a value of "0" will be used at the end to allow
118 * the reader to clean up its internal state
119 * @param buf where the reader should write the data
120 * @param emsg location for the reader to store an error message
121 * @return number of bytes written, usually "max", 0 on error
124 data_reader_file(void *cls,
130 struct FileInfo *fi = cls;
136 GNUNET_DISK_file_close (fi->fd);
137 GNUNET_free (fi->filename);
143 fi->fd = GNUNET_DISK_file_open (fi->filename,
144 GNUNET_DISK_OPEN_READ,
145 GNUNET_DISK_PERM_NONE);
148 GNUNET_asprintf (emsg,
149 _("Could not open file `%s': %s"),
155 GNUNET_DISK_file_seek (fi->fd, offset, GNUNET_DISK_SEEK_SET);
156 ret = GNUNET_DISK_file_read (fi->fd, buf, max);
159 GNUNET_asprintf (emsg,
160 _("Could not read file `%s': %s"),
167 GNUNET_asprintf (emsg,
168 _("Short read reading from file `%s'!"),
177 * Create an entry for a file in a publish-structure.
179 * @param client_info initial value for the client-info value for this entry
180 * @param filename name of the file or directory to publish
181 * @param keywords under which keywords should this file be available
182 * directly; can be NULL
183 * @param meta metadata for the file
184 * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
185 * GNUNET_SYSERR for simulation
186 * @param anonymity what is the desired anonymity level for sharing?
187 * @param priority what is the priority for OUR node to
188 * keep this file available? Use 0 for maximum anonymity and
189 * minimum reliability...
190 * @param expirationTime when should this content expire?
191 * @return publish structure entry for the file
193 struct GNUNET_FS_FileInformation *
194 GNUNET_FS_file_information_create_from_file (void *client_info,
195 const char *filename,
196 const struct GNUNET_FS_Uri *keywords,
197 const struct GNUNET_CONTAINER_MetaData *meta,
201 struct GNUNET_TIME_Absolute expirationTime)
206 if (0 != STAT (filename, &sbuf))
208 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
213 fi = GNUNET_malloc (sizeof(struct FileInfo));
214 fi->filename = GNUNET_strdup (filename);
215 return GNUNET_FS_file_information_create_from_reader (client_info,
229 * Function that provides data by copying from a buffer.
231 * @param cls closure (points to the buffer)
232 * @param offset offset to read from; it is possible
233 * that the caller might need to go backwards
235 * @param max maximum number of bytes that should be
236 * copied to buf; readers are not allowed
237 * to provide less data unless there is an error;
238 * a value of "0" will be used at the end to allow
239 * the reader to clean up its internal state
240 * @param buf where the reader should write the data
241 * @param emsg location for the reader to store an error message
242 * @return number of bytes written, usually "max", 0 on error
245 data_reader_copy(void *cls,
257 memcpy (buf, &data[offset], max);
263 * Create an entry for a file in a publish-structure.
265 * @param client_info initial value for the client-info value for this entry
266 * @param length length of the file
267 * @param data data for the file (should not be used afterwards by
268 * the caller; callee will "free")
269 * @param keywords under which keywords should this file be available
270 * directly; can be NULL
271 * @param meta metadata for the file
272 * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
273 * GNUNET_SYSERR for simulation
274 * @param anonymity what is the desired anonymity level for sharing?
275 * @param priority what is the priority for OUR node to
276 * keep this file available? Use 0 for maximum anonymity and
277 * minimum reliability...
278 * @param expirationTime when should this content expire?
279 * @return publish structure entry for the file
281 struct GNUNET_FS_FileInformation *
282 GNUNET_FS_file_information_create_from_data (void *client_info,
285 const struct GNUNET_FS_Uri *keywords,
286 const struct GNUNET_CONTAINER_MetaData *meta,
290 struct GNUNET_TIME_Absolute expirationTime)
292 return GNUNET_FS_file_information_create_from_reader (client_info,
306 * Create an entry for a file in a publish-structure.
308 * @param client_info initial value for the client-info value for this entry
309 * @param length length of the file
310 * @param reader function that can be used to obtain the data for the file
311 * @param reader_cls closure for "reader"
312 * @param keywords under which keywords should this file be available
313 * directly; can be NULL
314 * @param meta metadata for the file
315 * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
316 * GNUNET_SYSERR for simulation
317 * @param anonymity what is the desired anonymity level for sharing?
318 * @param priority what is the priority for OUR node to
319 * keep this file available? Use 0 for maximum anonymity and
320 * minimum reliability...
321 * @param expirationTime when should this content expire?
322 * @return publish structure entry for the file
324 struct GNUNET_FS_FileInformation *
325 GNUNET_FS_file_information_create_from_reader (void *client_info,
327 GNUNET_FS_DataReader reader,
329 const struct GNUNET_FS_Uri *keywords,
330 const struct GNUNET_CONTAINER_MetaData *meta,
334 struct GNUNET_TIME_Absolute expirationTime)
336 struct GNUNET_FS_FileInformation *ret;
338 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_FileInformation));
339 ret->client_info = client_info;
340 ret->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
341 ret->keywords = (keywords == NULL) ? NULL : GNUNET_FS_uri_dup (keywords);
342 ret->expirationTime = expirationTime;
343 ret->data.file.reader = reader;
344 ret->data.file.reader_cls = reader_cls;
345 ret->data.file.do_index = do_index;
346 ret->anonymity = anonymity;
347 ret->priority = priority;
348 GNUNET_FS_file_information_sync (ret);
354 * Closure for "dir_scan_cb".
359 * Metadata extractors to use.
361 struct EXTRACTOR_Extractor *extractors;
364 * Function to call on each directory entry.
366 GNUNET_FS_FileProcessor proc;
374 * Scanner to use for subdirectories.
376 GNUNET_FS_DirectoryScanner scanner;
379 * Closure for scanner.
384 * Set to an error message (if any).
389 * Should files be indexed?
394 * Desired anonymity level.
399 * Desired publishing priority.
404 * Expiration time for publication.
406 struct GNUNET_TIME_Absolute expiration;
411 * Function called on each entry in a file to
412 * cause default-publishing.
413 * @param cls closure (struct DirScanCls)
414 * @param filename name of the file to be published
415 * @return GNUNET_OK on success, GNUNET_SYSERR to abort
418 dir_scan_cb (void *cls,
419 const char *filename)
421 struct DirScanCls *dsc = cls;
423 struct GNUNET_FS_FileInformation *fi;
424 struct GNUNET_FS_Uri *ksk_uri;
425 struct GNUNET_FS_Uri *keywords;
426 struct GNUNET_CONTAINER_MetaData *meta;
428 if (0 != STAT (filename, &sbuf))
430 GNUNET_asprintf (&dsc->emsg,
431 _("`%s' failed on file `%s': %s"),
435 return GNUNET_SYSERR;
437 if (S_ISDIR (sbuf.st_mode))
439 fi = GNUNET_FS_file_information_create_from_directory (NULL,
450 GNUNET_assert (NULL != dsc->emsg);
451 return GNUNET_SYSERR;
456 meta = GNUNET_CONTAINER_meta_data_create ();
457 GNUNET_CONTAINER_meta_data_extract_from_file (meta,
460 // FIXME: remove path from filename in metadata!
461 keywords = GNUNET_FS_uri_ksk_create_from_meta_data (meta);
462 ksk_uri = GNUNET_FS_uri_ksk_canonicalize (keywords);
463 fi = GNUNET_FS_file_information_create_from_file (NULL,
471 GNUNET_CONTAINER_meta_data_destroy (meta);
472 GNUNET_FS_uri_destroy (keywords);
473 GNUNET_FS_uri_destroy (ksk_uri);
475 dsc->proc (dsc->proc_cls,
483 * Simple, useful default implementation of a directory scanner
484 * (GNUNET_FS_DirectoryScanner). This implementation expects to get a
485 * UNIX filename, will publish all files in the directory except hidden
486 * files (those starting with a "."). Metadata will be extracted
487 * using GNU libextractor; the specific list of plugins should be
488 * specified in "cls", passing NULL will disable (!) metadata
489 * extraction. Keywords will be derived from the metadata and be
490 * subject to default canonicalization. This is strictly a
491 * convenience function.
493 * @param cls must be of type "struct EXTRACTOR_Extractor*"
494 * @param dirname name of the directory to scan
495 * @param do_index should files be indexed or inserted
496 * @param anonymity desired anonymity level
497 * @param priority priority for publishing
498 * @param expirationTime expiration for publication
499 * @param proc function called on each entry
500 * @param proc_cls closure for proc
501 * @param emsg where to store an error message (on errors)
502 * @return GNUNET_OK on success
505 GNUNET_FS_directory_scanner_default (void *cls,
510 struct GNUNET_TIME_Absolute expirationTime,
511 GNUNET_FS_FileProcessor proc,
515 struct EXTRACTOR_Extractor *ex = cls;
516 struct DirScanCls dsc;
520 dsc.proc_cls = proc_cls;
521 dsc.scanner = &GNUNET_FS_directory_scanner_default;
522 dsc.scanner_cls = cls;
523 dsc.do_index = do_index;
524 dsc.anonymity = anonymity;
525 dsc.priority = priority;
526 dsc.expiration = expirationTime;
527 if (-1 == GNUNET_DISK_directory_scan (dirname,
531 GNUNET_assert (NULL != dsc.emsg);
533 return GNUNET_SYSERR;
540 * Closure for dirproc function.
545 * Linked list of directory entries that is being
548 struct GNUNET_FS_FileInformation *entries;
554 * Function that processes a directory entry that
555 * was obtained from the scanner.
556 * @param cls our closure
557 * @param filename name of the file (unused, why there???)
558 * @param fi information for publishing the file
562 const char *filename,
563 struct GNUNET_FS_FileInformation *fi)
565 struct EntryProcCls *dc = cls;
567 GNUNET_assert (fi->next == NULL);
568 GNUNET_assert (fi->dir == NULL);
569 fi->next = dc->entries;
575 * Create a publish-structure from an existing file hierarchy, inferring
576 * and organizing keywords and metadata as much as possible. This
577 * function primarily performs the recursive build and re-organizes
578 * keywords and metadata; for automatically getting metadata
579 * extraction, scanning of directories and creation of the respective
580 * GNUNET_FS_FileInformation entries the default scanner should be
581 * passed (GNUNET_FS_directory_scanner_default). This is strictly a
582 * convenience function.
584 * @param client_info initial value for the client-info value for this entry
585 * @param filename name of the top-level file or directory
586 * @param scanner function used to get a list of files in a directory
587 * @param scanner_cls closure for scanner
588 * @param do_index should files in the hierarchy be indexed?
589 * @param anonymity what is the desired anonymity level for sharing?
590 * @param priority what is the priority for OUR node to
591 * keep this file available? Use 0 for maximum anonymity and
592 * minimum reliability...
593 * @param expirationTime when should this content expire?
594 * @param emsg where to store an error message
595 * @return publish structure entry for the directory, NULL on error
597 struct GNUNET_FS_FileInformation *
598 GNUNET_FS_file_information_create_from_directory (void *client_info,
599 const char *filename,
600 GNUNET_FS_DirectoryScanner scanner,
605 struct GNUNET_TIME_Absolute expirationTime,
608 struct GNUNET_FS_FileInformation *ret;
609 struct EntryProcCls dc;
610 struct GNUNET_FS_Uri *ksk;
611 struct GNUNET_CONTAINER_MetaData *meta;
614 meta = GNUNET_CONTAINER_meta_data_create ();
615 GNUNET_FS_meta_data_make_directory (meta);
617 scanner (scanner_cls,
626 ksk = NULL; // FIXME...
627 // FIXME: create meta!
628 ret = GNUNET_FS_file_information_create_empty_directory (client_info,
634 ret->data.dir.entries = dc.entries;
635 while (dc.entries != NULL)
637 dc.entries->dir = ret;
638 GNUNET_FS_file_information_sync (dc.entries);
639 dc.entries = dc.entries->next;
641 GNUNET_FS_file_information_sync (ret);
647 * Create an entry for an empty directory in a publish-structure.
648 * This function should be used by applications for which the
649 * use of "GNUNET_FS_file_information_create_from_directory"
650 * is not appropriate.
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 (void *client_info,
665 const struct GNUNET_CONTAINER_MetaData *meta,
666 const struct GNUNET_FS_Uri *keywords,
669 struct GNUNET_TIME_Absolute expirationTime)
671 struct GNUNET_FS_FileInformation *ret;
673 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;
681 GNUNET_FS_file_information_sync (ret);
687 * Add an entry to a directory in a publish-structure. Clients
688 * should never modify publish structures that were passed to
689 * "GNUNET_FS_publish_start" already.
691 * @param dir the directory
692 * @param ent the entry to add; the entry must not have been
693 * added to any other directory at this point and
694 * must not include "dir" in its structure
695 * @return GNUNET_OK on success, GNUNET_SYSERR on error
698 GNUNET_FS_file_information_add (struct GNUNET_FS_FileInformation *dir,
699 struct GNUNET_FS_FileInformation *ent)
701 if ( (ent->dir != NULL) ||
702 (ent->next != NULL) ||
703 (! dir->is_directory) )
706 return GNUNET_SYSERR;
709 ent->next = dir->data.dir.entries;
710 dir->data.dir.entries = ent;
711 dir->data.dir.dir_size = 0;
712 GNUNET_FS_file_information_sync (ent);
713 GNUNET_FS_file_information_sync (dir);
719 * Inspect a file or directory in a publish-structure. Clients
720 * should never modify publish structures that were passed to
721 * "GNUNET_FS_publish_start" already. When called on a directory,
722 * this function will FIRST call "proc" with information about
723 * the directory itself and then for each of the files in the
724 * directory (but not for files in subdirectories). When called
725 * on a file, "proc" will be called exactly once (with information
726 * about the specific file).
728 * @param dir the directory
729 * @param proc function to call on each entry
730 * @param proc_cls closure for proc
733 GNUNET_FS_file_information_inspect (struct GNUNET_FS_FileInformation *dir,
734 GNUNET_FS_FileInformationProcessor proc,
737 struct GNUNET_FS_FileInformation *pos;
739 if (dir->is_directory)
743 dir->data.dir.dir_size,
748 &dir->expirationTime,
750 pos = dir->data.dir.entries;
755 pos->data.dir.dir_size,
760 &pos->expirationTime,
769 dir->data.file.file_size,
774 &dir->expirationTime,
781 * Destroy publish-structure. Clients should never destroy publish
782 * structures that were passed to "GNUNET_FS_publish_start" already.
784 * @param fi structure to destroy
785 * @param cleaner function to call on each entry in the structure
786 * (useful to clean up client_info); can be NULL; return
788 * @param cleaner_cls closure for cleaner
791 GNUNET_FS_file_information_destroy (struct GNUNET_FS_FileInformation *fi,
792 GNUNET_FS_FileInformationProcessor cleaner,
795 struct GNUNET_FS_FileInformation *pos;
797 if (fi->is_directory)
799 /* clean up directory */
800 while (NULL != (pos = fi->data.dir.entries))
802 fi->data.dir.entries = pos->next;
803 GNUNET_FS_file_information_destroy (pos, cleaner, cleaner_cls);
805 /* clean up client-info */
806 cleaner (cleaner_cls,
808 fi->data.dir.dir_size,
815 GNUNET_free_non_null (fi->data.dir.dir_data);
816 GNUNET_free (fi->data.dir.dirname);
820 /* call clean-up function of the reader */
821 fi->data.file.reader (fi->data.file.reader_cls, 0, 0, NULL, NULL);
822 /* clean up client-info */
823 cleaner (cleaner_cls,
825 fi->data.file.file_size,
833 GNUNET_free_non_null (fi->emsg);
834 /* clean up serialization */
835 if (0 != UNLINK (fi->serialization))
836 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
839 GNUNET_FS_uri_destroy (fi->keywords);
840 GNUNET_CONTAINER_meta_data_destroy (fi->meta);
841 GNUNET_free (fi->serialization);
846 /* end of fs_file_information.c */