* @file fs/fs_file_information.c
* @brief Manage information for publishing directory hierarchies
* @author Christian Grothoff
- *
- * TODO:
- * - metadata filename clean up code
- * - metadata/ksk generation for directories from contained files
*/
#include "platform.h"
#include <extractor.h>
#include "gnunet_fs_service.h"
-#include "fs.h"
+#include "fs_api.h"
#include "fs_tree.h"
/**
- * Add meta data that libextractor finds to our meta data
- * container.
+ * Obtain the name under which this file information
+ * structure is stored on disk. Only works for top-level
+ * file information structures.
*
- * @param cls closure, our meta data container
- * @param plugin_name name of the plugin that produced this value;
- * special values can be used (i.e. '<zlib>' for zlib being
- * used in the main libextractor library and yielding
- * meta data).
- * @param type libextractor-type describing the meta data
- * @param format basic format information about data
- * @param data_mime_type mime-type of data (not of the original file);
- * can be NULL (if mime-type is not known)
- * @param data actual meta-data found
- * @param data_len number of bytes in data
- * @return always 0 to continue extracting
+ * @param s structure to get the filename for
+ * @return NULL on error, otherwise filename that
+ * can be passed to "GNUNET_FS_file_information_recover"
+ * to read this fi-struct from disk.
*/
-static int
-add_to_md (void *cls, const char *plugin_name, enum EXTRACTOR_MetaType type,
- enum EXTRACTOR_MetaFormat format, const char *data_mime_type,
- const char *data, size_t data_len)
+const char *
+GNUNET_FS_file_information_get_id (struct GNUNET_FS_FileInformation *s)
{
- struct GNUNET_CONTAINER_MetaData *md = cls;
-
- (void) GNUNET_CONTAINER_meta_data_insert (md, plugin_name, type, format,
- data_mime_type, data, data_len);
- return 0;
+ if (NULL != s->dir)
+ return NULL;
+ return s->serialization;
}
-
/**
- * Extract meta-data from a file.
+ * Obtain the filename from the file information structure.
*
- * @return GNUNET_SYSERR on error, otherwise the number
- * of meta-data items obtained
+ * @param s structure to get the filename for
+ * @return "filename" field of the structure (can be NULL)
*/
-int
-GNUNET_FS_meta_data_extract_from_file (struct GNUNET_CONTAINER_MetaData *md,
- const char *filename,
- struct EXTRACTOR_PluginList *extractors)
+const char *
+GNUNET_FS_file_information_get_filename (struct GNUNET_FS_FileInformation *s)
{
- int old;
-
- if (filename == NULL)
- return GNUNET_SYSERR;
- if (extractors == NULL)
- return 0;
- old = GNUNET_CONTAINER_meta_data_iterate (md, NULL, NULL);
- GNUNET_assert (old >= 0);
- EXTRACTOR_extract (extractors, filename, NULL, 0, &add_to_md, md);
- return (GNUNET_CONTAINER_meta_data_iterate (md, NULL, NULL) - old);
+ return s->filename;
}
-
/**
- * Obtain the name under which this file information
- * structure is stored on disk. Only works for top-level
- * file information structures.
+ * Set the filename in the file information structure.
+ * If filename was already set, frees it before setting the new one.
+ * Makes a copy of the argument.
*
* @param s structure to get the filename for
- * @return NULL on error, otherwise filename that
- * can be passed to "GNUNET_FS_file_information_recover"
- * to read this fi-struct from disk.
+ * @param filename filename to set
*/
-const char *
-GNUNET_FS_file_information_get_id (struct GNUNET_FS_FileInformation *s)
+void
+GNUNET_FS_file_information_set_filename (struct GNUNET_FS_FileInformation *s,
+ const char *filename)
{
- if (NULL != s->dir)
- return NULL;
- return s->serialization;
+ GNUNET_free_non_null (s->filename);
+ if (filename)
+ s->filename = GNUNET_strdup (filename);
+ else
+ s->filename = NULL;
}
-
/**
* Create an entry for a file in a publish-structure.
*
*bo)
{
struct FileInfo *fi;
- struct stat sbuf;
+ uint64_t fsize;
struct GNUNET_FS_FileInformation *ret;
const char *fn;
const char *ss;
char fn_conv[MAX_PATH];
#endif
- if (0 != STAT (filename, &sbuf))
+ /* FIXME: should includeSymLinks be GNUNET_NO or GNUNET_YES here? */
+ if (GNUNET_OK != GNUNET_DISK_file_size (filename, &fsize, GNUNET_NO, GNUNET_YES))
{
GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", filename);
return NULL;
}
ret =
GNUNET_FS_file_information_create_from_reader (h, client_info,
- sbuf.st_size,
+ fsize,
&GNUNET_FS_data_reader_file_,
fi, keywords, meta,
do_index, bo);
#endif
while (NULL != (ss = strstr (fn, DIR_SEPARATOR_STR)))
fn = ss + 1;
+/* FIXME: If we assume that on other platforms CRT is UTF-8-aware, then
+ * this should be changed to EXTRACTOR_METAFORMAT_UTF8
+ */
+#if !WINDOWS
GNUNET_CONTAINER_meta_data_insert (ret->meta, "<gnunet>",
EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME,
EXTRACTOR_METAFORMAT_C_STRING,
"text/plain", fn, strlen (fn) + 1);
+#else
+ GNUNET_CONTAINER_meta_data_insert (ret->meta, "<gnunet>",
+ EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME,
+ EXTRACTOR_METAFORMAT_UTF8,
+ "text/plain", fn, strlen (fn) + 1);
+#endif
return ret;
}
* @param h handle to the file sharing subsystem
* @param client_info initial value for the client-info value for this entry
* @param length length of the file
- * @param reader function that can be used to obtain the data for the file
+ * @param reader function that can be used to obtain the data for the file
* @param reader_cls closure for "reader"
* @param keywords under which keywords should this file be available
* directly; can be NULL
}
-/**
- * Closure for "dir_scan_cb".
- */
-struct DirScanCls
-{
- /**
- * Metadata extractors to use.
- */
- struct EXTRACTOR_PluginList *extractors;
-
- /**
- * Master context.
- */
- struct GNUNET_FS_Handle *h;
-
- /**
- * Function to call on each directory entry.
- */
- GNUNET_FS_FileProcessor proc;
-
- /**
- * Closure for proc.
- */
- void *proc_cls;
-
- /**
- * Scanner to use for subdirectories.
- */
- GNUNET_FS_DirectoryScanner scanner;
-
- /**
- * Closure for scanner.
- */
- void *scanner_cls;
-
- /**
- * Set to an error message (if any).
- */
- char *emsg;
-
- /**
- * Block options.
- */
- const struct GNUNET_FS_BlockOptions *bo;
-
- /**
- * Should files be indexed?
- */
- int do_index;
-
-};
-
-
-/**
- * Function called on each entry in a file to
- * cause default-publishing.
- * @param cls closure (struct DirScanCls)
- * @param filename name of the file to be published
- * @return GNUNET_OK on success, GNUNET_SYSERR to abort
- */
-static int
-dir_scan_cb (void *cls, const char *filename)
-{
- struct DirScanCls *dsc = cls;
- struct stat sbuf;
- struct GNUNET_FS_FileInformation *fi;
- struct GNUNET_FS_Uri *ksk_uri;
- struct GNUNET_FS_Uri *keywords;
- struct GNUNET_CONTAINER_MetaData *meta;
-
- if (0 != STAT (filename, &sbuf))
- {
- GNUNET_asprintf (&dsc->emsg, _("`%s' failed on file `%s': %s"), "stat",
- filename, STRERROR (errno));
- return GNUNET_SYSERR;
- }
- if (S_ISDIR (sbuf.st_mode))
- {
- fi = GNUNET_FS_file_information_create_from_directory (dsc->h, NULL,
- filename,
- dsc->scanner,
- dsc->scanner_cls,
- dsc->do_index,
- dsc->bo, &dsc->emsg);
- if (NULL == fi)
- {
- GNUNET_assert (NULL != dsc->emsg);
- return GNUNET_SYSERR;
- }
- }
- else
- {
- meta = GNUNET_CONTAINER_meta_data_create ();
- GNUNET_FS_meta_data_extract_from_file (meta, filename, dsc->extractors);
- keywords = GNUNET_FS_uri_ksk_create_from_meta_data (meta);
- ksk_uri = GNUNET_FS_uri_ksk_canonicalize (keywords);
- fi = GNUNET_FS_file_information_create_from_file (dsc->h, NULL, filename,
- ksk_uri, meta,
- dsc->do_index, dsc->bo);
- GNUNET_CONTAINER_meta_data_destroy (meta);
- GNUNET_FS_uri_destroy (keywords);
- GNUNET_FS_uri_destroy (ksk_uri);
- }
- dsc->proc (dsc->proc_cls, filename, fi);
- return GNUNET_OK;
-}
-
-
-/**
- * Simple, useful default implementation of a directory scanner
- * (GNUNET_FS_DirectoryScanner). This implementation expects to get a
- * UNIX filename, will publish all files in the directory except hidden
- * files (those starting with a "."). Metadata will be extracted
- * using GNU libextractor; the specific list of plugins should be
- * specified in "cls", passing NULL will disable (!) metadata
- * extraction. Keywords will be derived from the metadata and be
- * subject to default canonicalization. This is strictly a
- * convenience function.
- *
- * @param cls must be of type "struct EXTRACTOR_Extractor*"
- * @param h handle to the file sharing subsystem
- * @param dirname name of the directory to scan
- * @param do_index should files be indexed or inserted
- * @param bo block options
- * @param proc function called on each entry
- * @param proc_cls closure for proc
- * @param emsg where to store an error message (on errors)
- * @return GNUNET_OK on success
- */
-int
-GNUNET_FS_directory_scanner_default (void *cls, struct GNUNET_FS_Handle *h,
- const char *dirname, int do_index,
- const struct GNUNET_FS_BlockOptions *bo,
- GNUNET_FS_FileProcessor proc,
- void *proc_cls, char **emsg)
-{
- struct EXTRACTOR_PluginList *ex = cls;
- struct DirScanCls dsc;
-
- dsc.h = h;
- dsc.extractors = ex;
- dsc.proc = proc;
- dsc.proc_cls = proc_cls;
- dsc.scanner = &GNUNET_FS_directory_scanner_default;
- dsc.scanner_cls = cls;
- dsc.do_index = do_index;
- dsc.bo = bo;
- if (-1 == GNUNET_DISK_directory_scan (dirname, &dir_scan_cb, &dsc))
- {
- GNUNET_assert (NULL != dsc.emsg);
- *emsg = dsc.emsg;
- return GNUNET_SYSERR;
- }
- return GNUNET_OK;
-}
-
-
-/**
- * Closure for dirproc function.
- */
-struct EntryProcCls
-{
- /**
- * Linked list of directory entries that is being
- * created.
- */
- struct GNUNET_FS_FileInformation *entries;
-
-};
-
-
-/**
- * Function that processes a directory entry that
- * was obtained from the scanner.
- * @param cls our closure
- * @param filename name of the file (unused, why there???)
- * @param fi information for publishing the file
- */
-static void
-dirproc (void *cls, const char *filename, struct GNUNET_FS_FileInformation *fi)
-{
- struct EntryProcCls *dc = cls;
-
- GNUNET_assert (fi->next == NULL);
- GNUNET_assert (fi->dir == NULL);
- fi->next = dc->entries;
- dc->entries = fi;
-}
-
-
-/**
- * Create a publish-structure from an existing file hierarchy, inferring
- * and organizing keywords and metadata as much as possible. This
- * function primarily performs the recursive build and re-organizes
- * keywords and metadata; for automatically getting metadata
- * extraction, scanning of directories and creation of the respective
- * GNUNET_FS_FileInformation entries the default scanner should be
- * passed (GNUNET_FS_directory_scanner_default). This is strictly a
- * convenience function.
- *
- * @param h handle to the file sharing subsystem
- * @param client_info initial value for the client-info value for this entry
- * @param filename name of the top-level file or directory
- * @param scanner function used to get a list of files in a directory
- * @param scanner_cls closure for scanner
- * @param do_index should files in the hierarchy be indexed?
- * @param bo block options
- * @param emsg where to store an error message
- * @return publish structure entry for the directory, NULL on error
- */
-struct GNUNET_FS_FileInformation *
-GNUNET_FS_file_information_create_from_directory (struct GNUNET_FS_Handle *h,
- void *client_info,
- const char *filename,
- GNUNET_FS_DirectoryScanner
- scanner, void *scanner_cls,
- int do_index,
- const struct
- GNUNET_FS_BlockOptions *bo,
- char **emsg)
-{
- struct GNUNET_FS_FileInformation *ret;
- struct EntryProcCls dc;
- struct GNUNET_FS_Uri *ksk;
- struct GNUNET_CONTAINER_MetaData *meta;
- const char *fn;
- const char *ss;
- char *dn;
-
- dc.entries = NULL;
- meta = GNUNET_CONTAINER_meta_data_create ();
- GNUNET_FS_meta_data_make_directory (meta);
- scanner (scanner_cls, h, filename, do_index, bo, &dirproc, &dc, emsg);
- ksk = NULL; // FIXME...
- // FIXME: create meta!
- ret =
- GNUNET_FS_file_information_create_empty_directory (h, client_info, ksk,
- meta, bo);
- GNUNET_CONTAINER_meta_data_destroy (meta);
- ret->data.dir.entries = dc.entries;
- while (dc.entries != NULL)
- {
- dc.entries->dir = ret;
- dc.entries = dc.entries->next;
- }
- fn = filename;
- while ((NULL != (ss = strstr (fn, DIR_SEPARATOR_STR))) && (strlen (ss) > 1))
- fn = ss + 1;
- GNUNET_asprintf (&dn, "%s/", fn);
- GNUNET_CONTAINER_meta_data_insert (ret->meta, "<gnunet>",
- EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME,
- EXTRACTOR_METAFORMAT_C_STRING,
- "text/plain", dn, strlen (dn) + 1);
- GNUNET_free (dn);
- ret->filename = GNUNET_strdup (filename);
- return ret;
-}
-
-
/**
* Test if a given entry represents a directory.
*
* @param keywords under which keywords should this directory be available
* directly; can be NULL
* @param bo block options
+ * @param filename name of the directory; can be NULL
* @return publish structure entry for the directory , NULL on error
*/
struct GNUNET_FS_FileInformation *
GNUNET_CONTAINER_MetaData
*meta,
const struct
- GNUNET_FS_BlockOptions *bo)
+ GNUNET_FS_BlockOptions *bo,
+ const char *filename)
{
struct GNUNET_FS_FileInformation *ret;
ret->keywords = GNUNET_FS_uri_dup (keywords);
ret->bo = *bo;
ret->is_directory = GNUNET_YES;
+ if (filename != NULL)
+ ret->filename = GNUNET_strdup (filename);
return ret;
}
*
* @param dir the directory
* @param ent the entry to add; the entry must not have been
- * added to any other directory at this point and
+ * added to any other directory at this point and
* must not include "dir" in its structure
* @return GNUNET_OK on success, GNUNET_SYSERR on error
*/
GNUNET_FS_file_information_add (struct GNUNET_FS_FileInformation *dir,
struct GNUNET_FS_FileInformation *ent)
{
- if ((ent->dir != NULL) || (ent->next != NULL) || (!dir->is_directory))
+ if ((ent->dir != NULL) || (ent->next != NULL) || (dir->is_directory != GNUNET_YES))
{
GNUNET_break (0);
return GNUNET_SYSERR;
no = GNUNET_NO;
if (GNUNET_OK !=
proc (proc_cls, dir,
- (dir->is_directory) ? dir->data.dir.dir_size : dir->data.
- file.file_size, dir->meta, &dir->keywords, &dir->bo,
- (dir->is_directory) ? &no : &dir->data.file.do_index,
+ (dir->is_directory == GNUNET_YES) ? dir->data.dir.dir_size : dir->data.
+ file.file_size,
+ dir->meta, &dir->keywords, &dir->bo,
+ (dir->is_directory == GNUNET_YES) ? &no : &dir->data.file.do_index,
&dir->client_info))
return;
- if (!dir->is_directory)
+ if (dir->is_directory != GNUNET_YES)
return;
pos = dir->data.dir.entries;
while (pos != NULL)
no = GNUNET_NO;
if (GNUNET_OK !=
proc (proc_cls, pos,
- (pos->is_directory) ? pos->data.dir.dir_size : pos->data.
+ (pos->is_directory == GNUNET_YES) ? pos->data.dir.dir_size : pos->data.
file.file_size, pos->meta, &pos->keywords, &pos->bo,
- (dir->is_directory) ? &no : &dir->data.file.do_index,
+ (pos->is_directory == GNUNET_YES) ? &no : &pos->data.file.do_index,
&pos->client_info))
break;
pos = pos->next;
int no;
no = GNUNET_NO;
- if (fi->is_directory)
+ if (fi->is_directory == GNUNET_YES)
{
/* clean up directory */
while (NULL != (pos = fi->data.dir.entries))