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 * Create a temporary file on disk to store the current
42 * @param fi file information to sync with disk
45 GNUNET_FS_file_information_sync (struct GNUNET_FS_FileInformation * fi)
47 if (NULL == fi->serialization)
49 fi->serialization = NULL; // FIXME -- need cfg!
56 * Load file information from the file to which
59 * @param fn name of the file to use
60 * @return NULL on error
62 struct GNUNET_FS_FileInformation *
63 GNUNET_FS_file_information_recover (const char *fn)
65 struct GNUNET_FS_FileInformation *ret;
73 * Obtain the name under which this file information
74 * structure is stored on disk. Only works for top-level
75 * file information structures.
77 * @param s structure to get the filename for
78 * @return NULL on error, otherwise filename that
79 * can be passed to "GNUNET_FS_file_information_recover"
80 * to read this fi-struct from disk.
83 GNUNET_FS_file_information_get_id (struct GNUNET_FS_FileInformation *s)
87 return s->serialization;
92 * Closure for "data_reader_file".
97 * Name of the file to read.
102 * File descriptor, NULL if it has not yet been opened.
104 struct GNUNET_DISK_FileHandle *fd;
109 * Function that provides data by reading from a file.
111 * @param cls closure (points to the file information)
112 * @param offset offset to read from; it is possible
113 * that the caller might need to go backwards
115 * @param max maximum number of bytes that should be
116 * copied to buf; readers are not allowed
117 * to provide less data unless there is an error;
118 * a value of "0" will be used at the end to allow
119 * the reader to clean up its internal state
120 * @param buf where the reader should write the data
121 * @param emsg location for the reader to store an error message
122 * @return number of bytes written, usually "max", 0 on error
125 data_reader_file(void *cls,
131 struct FileInfo *fi = cls;
137 GNUNET_DISK_file_close (fi->fd);
138 GNUNET_free (fi->filename);
144 fi->fd = GNUNET_DISK_file_open (fi->filename,
145 GNUNET_DISK_OPEN_READ,
146 GNUNET_DISK_PERM_NONE);
149 GNUNET_asprintf (emsg,
150 _("Could not open file `%s': %s"),
156 GNUNET_DISK_file_seek (fi->fd, offset, GNUNET_DISK_SEEK_SET);
157 ret = GNUNET_DISK_file_read (fi->fd, buf, max);
160 GNUNET_asprintf (emsg,
161 _("Could not read file `%s': %s"),
168 GNUNET_asprintf (emsg,
169 _("Short read reading from file `%s'!"),
178 * Create an entry for a file in a publish-structure.
180 * @param client_info initial value for the client-info value for this entry
181 * @param filename name of the file or directory to publish
182 * @param keywords under which keywords should this file be available
183 * directly; can be NULL
184 * @param meta metadata for the file
185 * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
186 * GNUNET_SYSERR for simulation
187 * @param anonymity what is the desired anonymity level for sharing?
188 * @param priority what is the priority for OUR node to
189 * keep this file available? Use 0 for maximum anonymity and
190 * minimum reliability...
191 * @param expirationTime when should this content expire?
192 * @return publish structure entry for the file
194 struct GNUNET_FS_FileInformation *
195 GNUNET_FS_file_information_create_from_file (void *client_info,
196 const char *filename,
197 const struct GNUNET_FS_Uri *keywords,
198 const struct GNUNET_CONTAINER_MetaData *meta,
202 struct GNUNET_TIME_Absolute expirationTime)
206 struct GNUNET_FS_FileInformation *ret;
208 if (0 != STAT (filename, &sbuf))
210 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
215 fi = GNUNET_malloc (sizeof(struct FileInfo));
216 fi->filename = GNUNET_strdup (filename);
217 ret = GNUNET_FS_file_information_create_from_reader (client_info,
227 ret->data.file.filename = GNUNET_strdup (filename);
233 * Function that provides data by copying from a buffer.
235 * @param cls closure (points to the buffer)
236 * @param offset offset to read from; it is possible
237 * that the caller might need to go backwards
239 * @param max maximum number of bytes that should be
240 * copied to buf; readers are not allowed
241 * to provide less data unless there is an error;
242 * a value of "0" will be used at the end to allow
243 * the reader to clean up its internal state
244 * @param buf where the reader should write the data
245 * @param emsg location for the reader to store an error message
246 * @return number of bytes written, usually "max", 0 on error
249 data_reader_copy(void *cls,
262 memcpy (buf, &data[offset], max);
268 * Create an entry for a file in a publish-structure.
270 * @param client_info initial value for the client-info value for this entry
271 * @param length length of the file
272 * @param data data for the file (should not be used afterwards by
273 * the caller; callee will "free")
274 * @param keywords under which keywords should this file be available
275 * directly; can be NULL
276 * @param meta metadata for the file
277 * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
278 * GNUNET_SYSERR for simulation
279 * @param anonymity what is the desired anonymity level for sharing?
280 * @param priority what is the priority for OUR node to
281 * keep this file available? Use 0 for maximum anonymity and
282 * minimum reliability...
283 * @param expirationTime when should this content expire?
284 * @return publish structure entry for the file
286 struct GNUNET_FS_FileInformation *
287 GNUNET_FS_file_information_create_from_data (void *client_info,
290 const struct GNUNET_FS_Uri *keywords,
291 const struct GNUNET_CONTAINER_MetaData *meta,
295 struct GNUNET_TIME_Absolute expirationTime)
297 return GNUNET_FS_file_information_create_from_reader (client_info,
311 * Create an entry for a file in a publish-structure.
313 * @param client_info initial value for the client-info value for this entry
314 * @param length length of the file
315 * @param reader function that can be used to obtain the data for the file
316 * @param reader_cls closure for "reader"
317 * @param keywords under which keywords should this file be available
318 * directly; can be NULL
319 * @param meta metadata for the file
320 * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
321 * GNUNET_SYSERR for simulation
322 * @param anonymity what is the desired anonymity level for sharing?
323 * @param priority what is the priority for OUR node to
324 * keep this file available? Use 0 for maximum anonymity and
325 * minimum reliability...
326 * @param expirationTime when should this content expire?
327 * @return publish structure entry for the file
329 struct GNUNET_FS_FileInformation *
330 GNUNET_FS_file_information_create_from_reader (void *client_info,
332 GNUNET_FS_DataReader reader,
334 const struct GNUNET_FS_Uri *keywords,
335 const struct GNUNET_CONTAINER_MetaData *meta,
339 struct GNUNET_TIME_Absolute expirationTime)
341 struct GNUNET_FS_FileInformation *ret;
343 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_FileInformation));
344 ret->client_info = client_info;
345 ret->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
346 if (ret->meta == NULL)
347 ret->meta = GNUNET_CONTAINER_meta_data_create ();
348 ret->keywords = (keywords == NULL) ? NULL : GNUNET_FS_uri_dup (keywords);
349 ret->expirationTime = expirationTime;
350 ret->data.file.reader = reader;
351 ret->data.file.reader_cls = reader_cls;
352 ret->data.file.do_index = do_index;
353 ret->data.file.file_size = length;
354 ret->anonymity = anonymity;
355 ret->priority = priority;
356 GNUNET_FS_file_information_sync (ret);
362 * Closure for "dir_scan_cb".
367 * Metadata extractors to use.
369 struct EXTRACTOR_PluginList *extractors;
372 * Function to call on each directory entry.
374 GNUNET_FS_FileProcessor proc;
382 * Scanner to use for subdirectories.
384 GNUNET_FS_DirectoryScanner scanner;
387 * Closure for scanner.
392 * Set to an error message (if any).
397 * Should files be indexed?
402 * Desired anonymity level.
407 * Desired publishing priority.
412 * Expiration time for publication.
414 struct GNUNET_TIME_Absolute expiration;
419 * Function called on each entry in a file to
420 * cause default-publishing.
421 * @param cls closure (struct DirScanCls)
422 * @param filename name of the file to be published
423 * @return GNUNET_OK on success, GNUNET_SYSERR to abort
426 dir_scan_cb (void *cls,
427 const char *filename)
429 struct DirScanCls *dsc = cls;
431 struct GNUNET_FS_FileInformation *fi;
432 struct GNUNET_FS_Uri *ksk_uri;
433 struct GNUNET_FS_Uri *keywords;
434 struct GNUNET_CONTAINER_MetaData *meta;
436 if (0 != STAT (filename, &sbuf))
438 GNUNET_asprintf (&dsc->emsg,
439 _("`%s' failed on file `%s': %s"),
443 return GNUNET_SYSERR;
445 if (S_ISDIR (sbuf.st_mode))
447 fi = GNUNET_FS_file_information_create_from_directory (NULL,
458 GNUNET_assert (NULL != dsc->emsg);
459 return GNUNET_SYSERR;
464 meta = GNUNET_CONTAINER_meta_data_create ();
465 GNUNET_CONTAINER_meta_data_extract_from_file (meta,
468 // FIXME: remove path from filename in metadata!
469 keywords = GNUNET_FS_uri_ksk_create_from_meta_data (meta);
470 ksk_uri = GNUNET_FS_uri_ksk_canonicalize (keywords);
471 fi = GNUNET_FS_file_information_create_from_file (NULL,
479 GNUNET_CONTAINER_meta_data_destroy (meta);
480 GNUNET_FS_uri_destroy (keywords);
481 GNUNET_FS_uri_destroy (ksk_uri);
483 dsc->proc (dsc->proc_cls,
491 * Simple, useful default implementation of a directory scanner
492 * (GNUNET_FS_DirectoryScanner). This implementation expects to get a
493 * UNIX filename, will publish all files in the directory except hidden
494 * files (those starting with a "."). Metadata will be extracted
495 * using GNU libextractor; the specific list of plugins should be
496 * specified in "cls", passing NULL will disable (!) metadata
497 * extraction. Keywords will be derived from the metadata and be
498 * subject to default canonicalization. This is strictly a
499 * convenience function.
501 * @param cls must be of type "struct EXTRACTOR_Extractor*"
502 * @param dirname name of the directory to scan
503 * @param do_index should files be indexed or inserted
504 * @param anonymity desired anonymity level
505 * @param priority priority for publishing
506 * @param expirationTime expiration for publication
507 * @param proc function called on each entry
508 * @param proc_cls closure for proc
509 * @param emsg where to store an error message (on errors)
510 * @return GNUNET_OK on success
513 GNUNET_FS_directory_scanner_default (void *cls,
518 struct GNUNET_TIME_Absolute expirationTime,
519 GNUNET_FS_FileProcessor proc,
523 struct EXTRACTOR_PluginList *ex = cls;
524 struct DirScanCls dsc;
528 dsc.proc_cls = proc_cls;
529 dsc.scanner = &GNUNET_FS_directory_scanner_default;
530 dsc.scanner_cls = cls;
531 dsc.do_index = do_index;
532 dsc.anonymity = anonymity;
533 dsc.priority = priority;
534 dsc.expiration = expirationTime;
535 if (-1 == GNUNET_DISK_directory_scan (dirname,
539 GNUNET_assert (NULL != dsc.emsg);
541 return GNUNET_SYSERR;
548 * Closure for dirproc function.
553 * Linked list of directory entries that is being
556 struct GNUNET_FS_FileInformation *entries;
562 * Function that processes a directory entry that
563 * was obtained from the scanner.
564 * @param cls our closure
565 * @param filename name of the file (unused, why there???)
566 * @param fi information for publishing the file
570 const char *filename,
571 struct GNUNET_FS_FileInformation *fi)
573 struct EntryProcCls *dc = cls;
575 GNUNET_assert (fi->next == NULL);
576 GNUNET_assert (fi->dir == NULL);
577 fi->next = dc->entries;
583 * Create a publish-structure from an existing file hierarchy, inferring
584 * and organizing keywords and metadata as much as possible. This
585 * function primarily performs the recursive build and re-organizes
586 * keywords and metadata; for automatically getting metadata
587 * extraction, scanning of directories and creation of the respective
588 * GNUNET_FS_FileInformation entries the default scanner should be
589 * passed (GNUNET_FS_directory_scanner_default). This is strictly a
590 * convenience function.
592 * @param client_info initial value for the client-info value for this entry
593 * @param filename name of the top-level file or directory
594 * @param scanner function used to get a list of files in a directory
595 * @param scanner_cls closure for scanner
596 * @param do_index should files in the hierarchy be indexed?
597 * @param anonymity what is the desired anonymity level for sharing?
598 * @param priority what is the priority for OUR node to
599 * keep this file available? Use 0 for maximum anonymity and
600 * minimum reliability...
601 * @param expirationTime when should this content expire?
602 * @param emsg where to store an error message
603 * @return publish structure entry for the directory, NULL on error
605 struct GNUNET_FS_FileInformation *
606 GNUNET_FS_file_information_create_from_directory (void *client_info,
607 const char *filename,
608 GNUNET_FS_DirectoryScanner scanner,
613 struct GNUNET_TIME_Absolute expirationTime,
616 struct GNUNET_FS_FileInformation *ret;
617 struct EntryProcCls dc;
618 struct GNUNET_FS_Uri *ksk;
619 struct GNUNET_CONTAINER_MetaData *meta;
624 meta = GNUNET_CONTAINER_meta_data_create ();
625 GNUNET_FS_meta_data_make_directory (meta);
627 scanner (scanner_cls,
636 ksk = NULL; // FIXME...
637 // FIXME: create meta!
638 ret = GNUNET_FS_file_information_create_empty_directory (client_info,
644 GNUNET_CONTAINER_meta_data_destroy (meta);
645 ret->data.dir.entries = dc.entries;
646 while (dc.entries != NULL)
648 dc.entries->dir = ret;
649 GNUNET_FS_file_information_sync (dc.entries);
650 dc.entries = dc.entries->next;
652 GNUNET_FS_file_information_sync (ret);
658 * Create an entry for an empty directory in a publish-structure.
659 * This function should be used by applications for which the
660 * use of "GNUNET_FS_file_information_create_from_directory"
661 * is not appropriate.
663 * @param client_info initial value for the client-info value for this entry
664 * @param meta metadata for the directory
665 * @param keywords under which keywords should this directory be available
666 * directly; can be NULL
667 * @param anonymity what is the desired anonymity level for sharing?
668 * @param priority what is the priority for OUR node to
669 * keep this file available? Use 0 for maximum anonymity and
670 * minimum reliability...
671 * @param expirationTime when should this content expire?
672 * @return publish structure entry for the directory , NULL on error
674 struct GNUNET_FS_FileInformation *
675 GNUNET_FS_file_information_create_empty_directory (void *client_info,
676 const struct GNUNET_FS_Uri *keywords,
677 const struct GNUNET_CONTAINER_MetaData *meta,
680 struct GNUNET_TIME_Absolute expirationTime)
682 struct GNUNET_FS_FileInformation *ret;
684 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_FileInformation));
685 ret->client_info = client_info;
686 ret->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
687 ret->keywords = GNUNET_FS_uri_dup (keywords);
688 ret->expirationTime = expirationTime;
689 ret->is_directory = GNUNET_YES;
690 ret->anonymity = anonymity;
691 ret->priority = priority;
692 GNUNET_FS_file_information_sync (ret);
698 * Add an entry to a directory in a publish-structure. Clients
699 * should never modify publish structures that were passed to
700 * "GNUNET_FS_publish_start" already.
702 * @param dir the directory
703 * @param ent the entry to add; the entry must not have been
704 * added to any other directory at this point and
705 * must not include "dir" in its structure
706 * @return GNUNET_OK on success, GNUNET_SYSERR on error
709 GNUNET_FS_file_information_add (struct GNUNET_FS_FileInformation *dir,
710 struct GNUNET_FS_FileInformation *ent)
712 if ( (ent->dir != NULL) ||
713 (ent->next != NULL) ||
714 (! dir->is_directory) )
717 return GNUNET_SYSERR;
720 ent->next = dir->data.dir.entries;
721 dir->data.dir.entries = ent;
722 dir->data.dir.dir_size = 0;
723 GNUNET_FS_file_information_sync (ent);
724 GNUNET_FS_file_information_sync (dir);
730 * Inspect a file or directory in a publish-structure. Clients
731 * should never modify publish structures that were passed to
732 * "GNUNET_FS_publish_start" already. When called on a directory,
733 * this function will FIRST call "proc" with information about
734 * the directory itself and then for each of the files in the
735 * directory (but not for files in subdirectories). When called
736 * on a file, "proc" will be called exactly once (with information
737 * about the specific file).
739 * @param dir the directory
740 * @param proc function to call on each entry
741 * @param proc_cls closure for proc
744 GNUNET_FS_file_information_inspect (struct GNUNET_FS_FileInformation *dir,
745 GNUNET_FS_FileInformationProcessor proc,
748 struct GNUNET_FS_FileInformation *pos;
750 if (dir->is_directory)
754 dir->data.dir.dir_size,
759 &dir->expirationTime,
761 pos = dir->data.dir.entries;
766 (pos->is_directory) ? pos->data.dir.dir_size : pos->data.file.file_size,
771 &pos->expirationTime,
780 dir->data.file.file_size,
785 &dir->expirationTime,
792 * Destroy publish-structure. Clients should never destroy publish
793 * structures that were passed to "GNUNET_FS_publish_start" already.
795 * @param fi structure to destroy
796 * @param cleaner function to call on each entry in the structure
797 * (useful to clean up client_info); can be NULL; return
799 * @param cleaner_cls closure for cleaner
802 GNUNET_FS_file_information_destroy (struct GNUNET_FS_FileInformation *fi,
803 GNUNET_FS_FileInformationProcessor cleaner,
806 struct GNUNET_FS_FileInformation *pos;
808 if (fi->is_directory)
810 /* clean up directory */
811 while (NULL != (pos = fi->data.dir.entries))
813 fi->data.dir.entries = pos->next;
814 GNUNET_FS_file_information_destroy (pos, cleaner, cleaner_cls);
816 /* clean up client-info */
818 cleaner (cleaner_cls,
820 fi->data.dir.dir_size,
827 GNUNET_free_non_null (fi->data.dir.dir_data);
828 GNUNET_free_non_null (fi->data.dir.dirname);
832 /* call clean-up function of the reader */
833 fi->data.file.reader (fi->data.file.reader_cls, 0, 0, NULL, NULL);
834 /* clean up client-info */
836 cleaner (cleaner_cls,
838 fi->data.file.file_size,
846 GNUNET_free_non_null (fi->emsg);
847 GNUNET_free_non_null (fi->chk_uri);
848 /* clean up serialization */
849 if ( (NULL != fi->serialization) &&
850 (0 != UNLINK (fi->serialization)) )
851 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
854 if (NULL != fi->keywords)
855 GNUNET_FS_uri_destroy (fi->keywords);
856 GNUNET_CONTAINER_meta_data_destroy (fi->meta);
857 GNUNET_free_non_null (fi->serialization);
860 GNUNET_FS_tree_encoder_finish (fi->te,
868 /* end of fs_file_information.c */