GNUnet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
- by the Free Software Foundation; either version 2, or (at your
+ by the Free Software Foundation; either version 3, or (at your
option) any later version.
GNUnet is distributed in the hope that it will be useful, but
* @author Christian Grothoff
*
* TODO:
- * - serialization/deserialization (& deserialization API)
* - metadata filename clean up code
* - metadata/ksk generation for directories from contained files
*/
/**
- * Obtain the name under which this file information
- * structure is stored on disk. Only works for top-level
- * file information structures.
+ * Add meta data that libextractor finds to our meta data
+ * container.
*
- * @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 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
*/
-const char *
-GNUNET_FS_file_information_get_id (struct GNUNET_FS_FileInformation *s)
+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)
{
- if (NULL != s->dir)
- return NULL;
- return s->serialization;
+ 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;
}
/**
- * Closure for "data_reader_file".
+ * Extract meta-data from a file.
+ *
+ * @return GNUNET_SYSERR on error, otherwise the number
+ * of meta-data items obtained
*/
-struct FileInfo
+int
+GNUNET_FS_meta_data_extract_from_file (struct GNUNET_CONTAINER_MetaData
+ *md, const char *filename,
+ struct EXTRACTOR_PluginList *
+ extractors)
{
- /**
- * Name of the file to read.
- */
- char *filename;
+ 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);
+}
- /**
- * File descriptor, NULL if it has not yet been opened.
- */
- struct GNUNET_DISK_FileHandle *fd;
-};
/**
- * Function that provides data by reading from a file.
+ * Obtain the name under which this file information
+ * structure is stored on disk. Only works for top-level
+ * file information structures.
*
- * @param cls closure (points to the file information)
- * @param offset offset to read from; it is possible
- * that the caller might need to go backwards
- * a bit at times
- * @param max maximum number of bytes that should be
- * copied to buf; readers are not allowed
- * to provide less data unless there is an error;
- * a value of "0" will be used at the end to allow
- * the reader to clean up its internal state
- * @param buf where the reader should write the data
- * @param emsg location for the reader to store an error message
- * @return number of bytes written, usually "max", 0 on error
+ * @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 size_t
-data_reader_file(void *cls,
- uint64_t offset,
- size_t max,
- void *buf,
- char **emsg)
+const char *
+GNUNET_FS_file_information_get_id (struct GNUNET_FS_FileInformation *s)
{
- struct FileInfo *fi = cls;
- ssize_t ret;
-
- if (max == 0)
- {
- if (fi->fd != NULL)
- GNUNET_DISK_file_close (fi->fd);
- GNUNET_free (fi->filename);
- GNUNET_free (fi);
- return 0;
- }
- if (fi->fd == NULL)
- {
- fi->fd = GNUNET_DISK_file_open (fi->filename,
- GNUNET_DISK_OPEN_READ,
- GNUNET_DISK_PERM_NONE);
- if (fi->fd == NULL)
- {
- GNUNET_asprintf (emsg,
- _("Could not open file `%s': %s"),
- fi->filename,
- STRERROR (errno));
- return 0;
- }
- }
- GNUNET_DISK_file_seek (fi->fd, offset, GNUNET_DISK_SEEK_SET);
- ret = GNUNET_DISK_file_read (fi->fd, buf, max);
- if (ret == -1)
- {
- GNUNET_asprintf (emsg,
- _("Could not read file `%s': %s"),
- fi->filename,
- STRERROR (errno));
- return 0;
- }
- if (ret != max)
- {
- GNUNET_asprintf (emsg,
- _("Short read reading from file `%s'!"),
- fi->filename);
- return 0;
- }
- return max;
+ if (NULL != s->dir)
+ return NULL;
+ return s->serialization;
}
filename);
return NULL;
}
- fi = GNUNET_malloc (sizeof(struct FileInfo));
- fi->filename = GNUNET_STRINGS_filename_expand (filename);
- if (fi->filename == NULL)
+ fi = GNUNET_FS_make_file_reader_context_ (filename);
+ if (fi == NULL)
{
- GNUNET_free (fi);
+ GNUNET_break (0);
return NULL;
}
ret = GNUNET_FS_file_information_create_from_reader (h,
client_info,
sbuf.st_size,
- &data_reader_file,
+ &GNUNET_FS_data_reader_file_,
fi,
keywords,
meta,
anonymity,
priority,
expirationTime);
+ if (ret == NULL)
+ return NULL;
ret->h = h;
ret->filename = GNUNET_strdup (filename);
fn = filename;
}
-/**
- * Function that provides data by copying from a buffer.
- *
- * @param cls closure (points to the buffer)
- * @param offset offset to read from; it is possible
- * that the caller might need to go backwards
- * a bit at times
- * @param max maximum number of bytes that should be
- * copied to buf; readers are not allowed
- * to provide less data unless there is an error;
- * a value of "0" will be used at the end to allow
- * the reader to clean up its internal state
- * @param buf where the reader should write the data
- * @param emsg location for the reader to store an error message
- * @return number of bytes written, usually "max", 0 on error
- */
-static size_t
-data_reader_copy(void *cls,
- uint64_t offset,
- size_t max,
- void *buf,
- char **emsg)
-{
- char *data = cls;
-
- if (max == 0)
- {
- GNUNET_free (data);
- return 0;
- }
- memcpy (buf, &data[offset], max);
- return max;
-}
-
-
/**
* Create an entry for a file in a publish-structure.
*
uint32_t priority,
struct GNUNET_TIME_Absolute expirationTime)
{
+ if (GNUNET_YES == do_index)
+ {
+ GNUNET_break (0);
+ return NULL;
+ }
return GNUNET_FS_file_information_create_from_reader (h,
client_info,
length,
- &data_reader_copy,
+ &GNUNET_FS_data_reader_copy_,
data,
keywords,
meta,
{
struct GNUNET_FS_FileInformation *ret;
+ if ( (GNUNET_YES == do_index) &&
+ (reader != &GNUNET_FS_data_reader_file_) )
+ {
+ GNUNET_break (0);
+ return NULL;
+ }
ret = GNUNET_malloc (sizeof (struct GNUNET_FS_FileInformation));
ret->h = h;
ret->client_info = client_info;
else
{
meta = GNUNET_CONTAINER_meta_data_create ();
- GNUNET_CONTAINER_meta_data_extract_from_file (meta,
- filename,
- dsc->extractors);
+ GNUNET_FS_meta_data_extract_from_file (meta,
+ filename,
+ dsc->extractors);
// FIXME: remove path from filename in metadata!
keywords = GNUNET_FS_uri_ksk_create_from_meta_data (meta);
ksk_uri = GNUNET_FS_uri_ksk_canonicalize (keywords);
dc.entries = dc.entries->next;
}
fn = filename;
- while (NULL != (ss = strstr (fn,
- DIR_SEPARATOR_STR)))
+ while ( (NULL != (ss = strstr (fn,
+ DIR_SEPARATOR_STR))) &&
+ (strlen (ss) > 1) )
fn = ss + 1;
GNUNET_CONTAINER_meta_data_insert (ret->meta,
"<gnunet>",
}
+/**
+ * Test if a given entry represents a directory.
+ *
+ * @param ent check if this FI represents a directory
+ * @return GNUNET_YES if so, GNUNET_NO if not
+ */
+int
+GNUNET_FS_file_information_is_directory (struct GNUNET_FS_FileInformation *ent)
+{
+ return ent->is_directory;
+}
+
+
/**
* Create an entry for an empty directory in a publish-structure.
* This function should be used by applications for which the
void *proc_cls)
{
struct GNUNET_FS_FileInformation *pos;
+ int no;
+ no = GNUNET_NO;
if (GNUNET_OK !=
proc (proc_cls,
dir,
&dir->keywords,
&dir->anonymity,
&dir->priority,
+ (dir->is_directory) ? &no : &dir->data.file.do_index,
&dir->expirationTime,
&dir->client_info))
return;
pos = dir->data.dir.entries;
while (pos != NULL)
{
+ no = GNUNET_NO;
if (GNUNET_OK !=
proc (proc_cls,
pos,
&pos->keywords,
&pos->anonymity,
&pos->priority,
+ (dir->is_directory) ? &no : &dir->data.file.do_index,
&pos->expirationTime,
&pos->client_info))
break;
void *cleaner_cls)
{
struct GNUNET_FS_FileInformation *pos;
+ int no;
+ no = GNUNET_NO;
if (fi->is_directory)
{
/* clean up directory */
&fi->keywords,
&fi->anonymity,
&fi->priority,
+ &no,
&fi->expirationTime,
&fi->client_info);
GNUNET_free_non_null (fi->data.dir.dir_data);
&fi->keywords,
&fi->anonymity,
&fi->priority,
+ &fi->data.file.do_index,
&fi->expirationTime,
&fi->client_info);
}
GNUNET_free_non_null (fi->filename);
- GNUNET_free_non_null (fi->serialization);
GNUNET_free_non_null (fi->emsg);
GNUNET_free_non_null (fi->chk_uri);
/* clean up serialization */