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_STRINGS_filename_expand (filename);
217 if (fi->filename == NULL)
222 ret = GNUNET_FS_file_information_create_from_reader (client_info,
232 ret->data.file.filename = GNUNET_strdup (filename);
238 * Function that provides data by copying from a buffer.
240 * @param cls closure (points to the buffer)
241 * @param offset offset to read from; it is possible
242 * that the caller might need to go backwards
244 * @param max maximum number of bytes that should be
245 * copied to buf; readers are not allowed
246 * to provide less data unless there is an error;
247 * a value of "0" will be used at the end to allow
248 * the reader to clean up its internal state
249 * @param buf where the reader should write the data
250 * @param emsg location for the reader to store an error message
251 * @return number of bytes written, usually "max", 0 on error
254 data_reader_copy(void *cls,
267 memcpy (buf, &data[offset], max);
273 * Create an entry for a file in a publish-structure.
275 * @param client_info initial value for the client-info value for this entry
276 * @param length length of the file
277 * @param data data for the file (should not be used afterwards by
278 * the caller; callee will "free")
279 * @param keywords under which keywords should this file be available
280 * directly; can be NULL
281 * @param meta metadata for the file
282 * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
283 * GNUNET_SYSERR for simulation
284 * @param anonymity what is the desired anonymity level for sharing?
285 * @param priority what is the priority for OUR node to
286 * keep this file available? Use 0 for maximum anonymity and
287 * minimum reliability...
288 * @param expirationTime when should this content expire?
289 * @return publish structure entry for the file
291 struct GNUNET_FS_FileInformation *
292 GNUNET_FS_file_information_create_from_data (void *client_info,
295 const struct GNUNET_FS_Uri *keywords,
296 const struct GNUNET_CONTAINER_MetaData *meta,
300 struct GNUNET_TIME_Absolute expirationTime)
302 return GNUNET_FS_file_information_create_from_reader (client_info,
316 * Create an entry for a file in a publish-structure.
318 * @param client_info initial value for the client-info value for this entry
319 * @param length length of the file
320 * @param reader function that can be used to obtain the data for the file
321 * @param reader_cls closure for "reader"
322 * @param keywords under which keywords should this file be available
323 * directly; can be NULL
324 * @param meta metadata for the file
325 * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
326 * GNUNET_SYSERR for simulation
327 * @param anonymity what is the desired anonymity level for sharing?
328 * @param priority what is the priority for OUR node to
329 * keep this file available? Use 0 for maximum anonymity and
330 * minimum reliability...
331 * @param expirationTime when should this content expire?
332 * @return publish structure entry for the file
334 struct GNUNET_FS_FileInformation *
335 GNUNET_FS_file_information_create_from_reader (void *client_info,
337 GNUNET_FS_DataReader reader,
339 const struct GNUNET_FS_Uri *keywords,
340 const struct GNUNET_CONTAINER_MetaData *meta,
344 struct GNUNET_TIME_Absolute expirationTime)
346 struct GNUNET_FS_FileInformation *ret;
348 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_FileInformation));
349 ret->client_info = client_info;
350 ret->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
351 if (ret->meta == NULL)
352 ret->meta = GNUNET_CONTAINER_meta_data_create ();
353 ret->keywords = (keywords == NULL) ? NULL : GNUNET_FS_uri_dup (keywords);
354 ret->expirationTime = expirationTime;
355 ret->data.file.reader = reader;
356 ret->data.file.reader_cls = reader_cls;
357 ret->data.file.do_index = do_index;
358 ret->data.file.file_size = length;
359 ret->anonymity = anonymity;
360 ret->priority = priority;
361 GNUNET_FS_file_information_sync (ret);
367 * Closure for "dir_scan_cb".
372 * Metadata extractors to use.
374 struct EXTRACTOR_PluginList *extractors;
377 * Function to call on each directory entry.
379 GNUNET_FS_FileProcessor proc;
387 * Scanner to use for subdirectories.
389 GNUNET_FS_DirectoryScanner scanner;
392 * Closure for scanner.
397 * Set to an error message (if any).
402 * Should files be indexed?
407 * Desired anonymity level.
412 * Desired publishing priority.
417 * Expiration time for publication.
419 struct GNUNET_TIME_Absolute expiration;
424 * Function called on each entry in a file to
425 * cause default-publishing.
426 * @param cls closure (struct DirScanCls)
427 * @param filename name of the file to be published
428 * @return GNUNET_OK on success, GNUNET_SYSERR to abort
431 dir_scan_cb (void *cls,
432 const char *filename)
434 struct DirScanCls *dsc = cls;
436 struct GNUNET_FS_FileInformation *fi;
437 struct GNUNET_FS_Uri *ksk_uri;
438 struct GNUNET_FS_Uri *keywords;
439 struct GNUNET_CONTAINER_MetaData *meta;
441 if (0 != STAT (filename, &sbuf))
443 GNUNET_asprintf (&dsc->emsg,
444 _("`%s' failed on file `%s': %s"),
448 return GNUNET_SYSERR;
450 if (S_ISDIR (sbuf.st_mode))
452 fi = GNUNET_FS_file_information_create_from_directory (NULL,
463 GNUNET_assert (NULL != dsc->emsg);
464 return GNUNET_SYSERR;
469 meta = GNUNET_CONTAINER_meta_data_create ();
470 GNUNET_CONTAINER_meta_data_extract_from_file (meta,
473 // FIXME: remove path from filename in metadata!
474 keywords = GNUNET_FS_uri_ksk_create_from_meta_data (meta);
475 ksk_uri = GNUNET_FS_uri_ksk_canonicalize (keywords);
476 fi = GNUNET_FS_file_information_create_from_file (NULL,
484 GNUNET_CONTAINER_meta_data_destroy (meta);
485 GNUNET_FS_uri_destroy (keywords);
486 GNUNET_FS_uri_destroy (ksk_uri);
488 dsc->proc (dsc->proc_cls,
496 * Simple, useful default implementation of a directory scanner
497 * (GNUNET_FS_DirectoryScanner). This implementation expects to get a
498 * UNIX filename, will publish all files in the directory except hidden
499 * files (those starting with a "."). Metadata will be extracted
500 * using GNU libextractor; the specific list of plugins should be
501 * specified in "cls", passing NULL will disable (!) metadata
502 * extraction. Keywords will be derived from the metadata and be
503 * subject to default canonicalization. This is strictly a
504 * convenience function.
506 * @param cls must be of type "struct EXTRACTOR_Extractor*"
507 * @param dirname name of the directory to scan
508 * @param do_index should files be indexed or inserted
509 * @param anonymity desired anonymity level
510 * @param priority priority for publishing
511 * @param expirationTime expiration for publication
512 * @param proc function called on each entry
513 * @param proc_cls closure for proc
514 * @param emsg where to store an error message (on errors)
515 * @return GNUNET_OK on success
518 GNUNET_FS_directory_scanner_default (void *cls,
523 struct GNUNET_TIME_Absolute expirationTime,
524 GNUNET_FS_FileProcessor proc,
528 struct EXTRACTOR_PluginList *ex = cls;
529 struct DirScanCls dsc;
533 dsc.proc_cls = proc_cls;
534 dsc.scanner = &GNUNET_FS_directory_scanner_default;
535 dsc.scanner_cls = cls;
536 dsc.do_index = do_index;
537 dsc.anonymity = anonymity;
538 dsc.priority = priority;
539 dsc.expiration = expirationTime;
540 if (-1 == GNUNET_DISK_directory_scan (dirname,
544 GNUNET_assert (NULL != dsc.emsg);
546 return GNUNET_SYSERR;
553 * Closure for dirproc function.
558 * Linked list of directory entries that is being
561 struct GNUNET_FS_FileInformation *entries;
567 * Function that processes a directory entry that
568 * was obtained from the scanner.
569 * @param cls our closure
570 * @param filename name of the file (unused, why there???)
571 * @param fi information for publishing the file
575 const char *filename,
576 struct GNUNET_FS_FileInformation *fi)
578 struct EntryProcCls *dc = cls;
580 GNUNET_assert (fi->next == NULL);
581 GNUNET_assert (fi->dir == NULL);
582 fi->next = dc->entries;
588 * Create a publish-structure from an existing file hierarchy, inferring
589 * and organizing keywords and metadata as much as possible. This
590 * function primarily performs the recursive build and re-organizes
591 * keywords and metadata; for automatically getting metadata
592 * extraction, scanning of directories and creation of the respective
593 * GNUNET_FS_FileInformation entries the default scanner should be
594 * passed (GNUNET_FS_directory_scanner_default). This is strictly a
595 * convenience function.
597 * @param client_info initial value for the client-info value for this entry
598 * @param filename name of the top-level file or directory
599 * @param scanner function used to get a list of files in a directory
600 * @param scanner_cls closure for scanner
601 * @param do_index should files in the hierarchy be indexed?
602 * @param anonymity what is the desired anonymity level for sharing?
603 * @param priority what is the priority for OUR node to
604 * keep this file available? Use 0 for maximum anonymity and
605 * minimum reliability...
606 * @param expirationTime when should this content expire?
607 * @param emsg where to store an error message
608 * @return publish structure entry for the directory, NULL on error
610 struct GNUNET_FS_FileInformation *
611 GNUNET_FS_file_information_create_from_directory (void *client_info,
612 const char *filename,
613 GNUNET_FS_DirectoryScanner scanner,
618 struct GNUNET_TIME_Absolute expirationTime,
621 struct GNUNET_FS_FileInformation *ret;
622 struct EntryProcCls dc;
623 struct GNUNET_FS_Uri *ksk;
624 struct GNUNET_CONTAINER_MetaData *meta;
629 meta = GNUNET_CONTAINER_meta_data_create ();
630 GNUNET_FS_meta_data_make_directory (meta);
632 scanner (scanner_cls,
641 ksk = NULL; // FIXME...
642 // FIXME: create meta!
643 ret = GNUNET_FS_file_information_create_empty_directory (client_info,
649 GNUNET_CONTAINER_meta_data_destroy (meta);
650 ret->data.dir.entries = dc.entries;
651 while (dc.entries != NULL)
653 dc.entries->dir = ret;
654 GNUNET_FS_file_information_sync (dc.entries);
655 dc.entries = dc.entries->next;
657 GNUNET_FS_file_information_sync (ret);
663 * Create an entry for an empty directory in a publish-structure.
664 * This function should be used by applications for which the
665 * use of "GNUNET_FS_file_information_create_from_directory"
666 * is not appropriate.
668 * @param client_info initial value for the client-info value for this entry
669 * @param meta metadata for the directory
670 * @param keywords under which keywords should this directory be available
671 * directly; can be NULL
672 * @param anonymity what is the desired anonymity level for sharing?
673 * @param priority what is the priority for OUR node to
674 * keep this file available? Use 0 for maximum anonymity and
675 * minimum reliability...
676 * @param expirationTime when should this content expire?
677 * @return publish structure entry for the directory , NULL on error
679 struct GNUNET_FS_FileInformation *
680 GNUNET_FS_file_information_create_empty_directory (void *client_info,
681 const struct GNUNET_FS_Uri *keywords,
682 const struct GNUNET_CONTAINER_MetaData *meta,
685 struct GNUNET_TIME_Absolute expirationTime)
687 struct GNUNET_FS_FileInformation *ret;
689 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_FileInformation));
690 ret->client_info = client_info;
691 ret->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
692 ret->keywords = GNUNET_FS_uri_dup (keywords);
693 ret->expirationTime = expirationTime;
694 ret->is_directory = GNUNET_YES;
695 ret->anonymity = anonymity;
696 ret->priority = priority;
697 GNUNET_FS_file_information_sync (ret);
703 * Add an entry to a directory in a publish-structure. Clients
704 * should never modify publish structures that were passed to
705 * "GNUNET_FS_publish_start" already.
707 * @param dir the directory
708 * @param ent the entry to add; the entry must not have been
709 * added to any other directory at this point and
710 * must not include "dir" in its structure
711 * @return GNUNET_OK on success, GNUNET_SYSERR on error
714 GNUNET_FS_file_information_add (struct GNUNET_FS_FileInformation *dir,
715 struct GNUNET_FS_FileInformation *ent)
717 if ( (ent->dir != NULL) ||
718 (ent->next != NULL) ||
719 (! dir->is_directory) )
722 return GNUNET_SYSERR;
725 ent->next = dir->data.dir.entries;
726 dir->data.dir.entries = ent;
727 dir->data.dir.dir_size = 0;
728 GNUNET_FS_file_information_sync (ent);
729 GNUNET_FS_file_information_sync (dir);
735 * Inspect a file or directory in a publish-structure. Clients
736 * should never modify publish structures that were passed to
737 * "GNUNET_FS_publish_start" already. When called on a directory,
738 * this function will FIRST call "proc" with information about
739 * the directory itself and then for each of the files in the
740 * directory (but not for files in subdirectories). When called
741 * on a file, "proc" will be called exactly once (with information
742 * about the specific file).
744 * @param dir the directory
745 * @param proc function to call on each entry
746 * @param proc_cls closure for proc
749 GNUNET_FS_file_information_inspect (struct GNUNET_FS_FileInformation *dir,
750 GNUNET_FS_FileInformationProcessor proc,
753 struct GNUNET_FS_FileInformation *pos;
755 if (dir->is_directory)
759 dir->data.dir.dir_size,
764 &dir->expirationTime,
766 pos = dir->data.dir.entries;
771 (pos->is_directory) ? pos->data.dir.dir_size : pos->data.file.file_size,
776 &pos->expirationTime,
785 dir->data.file.file_size,
790 &dir->expirationTime,
797 * Destroy publish-structure. Clients should never destroy publish
798 * structures that were passed to "GNUNET_FS_publish_start" already.
800 * @param fi structure to destroy
801 * @param cleaner function to call on each entry in the structure
802 * (useful to clean up client_info); can be NULL; return
804 * @param cleaner_cls closure for cleaner
807 GNUNET_FS_file_information_destroy (struct GNUNET_FS_FileInformation *fi,
808 GNUNET_FS_FileInformationProcessor cleaner,
811 struct GNUNET_FS_FileInformation *pos;
813 if (fi->is_directory)
815 /* clean up directory */
816 while (NULL != (pos = fi->data.dir.entries))
818 fi->data.dir.entries = pos->next;
819 GNUNET_FS_file_information_destroy (pos, cleaner, cleaner_cls);
821 /* clean up client-info */
823 cleaner (cleaner_cls,
825 fi->data.dir.dir_size,
832 GNUNET_free_non_null (fi->data.dir.dir_data);
833 GNUNET_free_non_null (fi->data.dir.dirname);
837 /* call clean-up function of the reader */
838 fi->data.file.reader (fi->data.file.reader_cls, 0, 0, NULL, NULL);
839 /* clean up client-info */
841 cleaner (cleaner_cls,
843 fi->data.file.file_size,
851 GNUNET_free_non_null (fi->emsg);
852 GNUNET_free_non_null (fi->chk_uri);
853 /* clean up serialization */
854 if ( (NULL != fi->serialization) &&
855 (0 != UNLINK (fi->serialization)) )
856 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
859 if (NULL != fi->keywords)
860 GNUNET_FS_uri_destroy (fi->keywords);
861 GNUNET_CONTAINER_meta_data_destroy (fi->meta);
862 GNUNET_free_non_null (fi->serialization);
865 GNUNET_FS_tree_encoder_finish (fi->te,
873 /* end of fs_file_information.c */