fix
[oweals/gnunet.git] / src / fs / fs_file_information.c
index d9acc959563b1a8f6c2916a0f440a7cd1ad9defa..2e9b7b6678426e86ff9df19dc12f6b433ee0bae2 100644 (file)
 #include "fs_tree.h"
 
 
+/**
+ * Add meta data that libextractor finds to our meta data
+ * container.
+ *
+ * @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
+ */
+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)
+{
+  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;
+}
+
+
+/**
+ * Extract meta-data from a file.
+ *
+ * @return GNUNET_SYSERR on error, otherwise the number
+ *   of meta-data items obtained
+ */
+int
+GNUNET_FS_meta_data_extract_from_file (struct GNUNET_CONTAINER_MetaData
+                                      *md, const char *filename,
+                                      struct EXTRACTOR_PluginList *
+                                      extractors)
+{
+  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);
+}
+
+
+
 /**
  * Obtain the name under which this file information
  * structure is stored on disk.  Only works for top-level
@@ -350,9 +418,9 @@ dir_scan_cb (void *cls,
   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);
@@ -559,6 +627,19 @@ GNUNET_FS_file_information_create_from_directory (struct GNUNET_FS_Handle *h,
 }
 
 
+/**
+ * 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
@@ -651,7 +732,9 @@ GNUNET_FS_file_information_inspect (struct GNUNET_FS_FileInformation *dir,
                                    void *proc_cls)
 {
   struct GNUNET_FS_FileInformation *pos;
+  int no;
 
+  no = GNUNET_NO;
   if (GNUNET_OK !=
       proc (proc_cls, 
            dir,
@@ -660,6 +743,7 @@ GNUNET_FS_file_information_inspect (struct GNUNET_FS_FileInformation *dir,
            &dir->keywords,
            &dir->anonymity,
            &dir->priority,
+           (dir->is_directory) ? &no : &dir->data.file.do_index,
            &dir->expirationTime,
            &dir->client_info))
     return;
@@ -668,6 +752,7 @@ GNUNET_FS_file_information_inspect (struct GNUNET_FS_FileInformation *dir,
   pos = dir->data.dir.entries;
   while (pos != NULL)
     {
+      no = GNUNET_NO;
       if (GNUNET_OK != 
          proc (proc_cls, 
                pos,
@@ -676,6 +761,7 @@ GNUNET_FS_file_information_inspect (struct GNUNET_FS_FileInformation *dir,
                &pos->keywords,
                &pos->anonymity,
                &pos->priority,
+               (dir->is_directory) ? &no : &dir->data.file.do_index,
                &pos->expirationTime,
                &pos->client_info))
        break;
@@ -700,7 +786,9 @@ GNUNET_FS_file_information_destroy (struct GNUNET_FS_FileInformation *fi,
                                    void *cleaner_cls)
 {
   struct GNUNET_FS_FileInformation *pos;
+  int no;
 
+  no = GNUNET_NO;
   if (fi->is_directory)
     {
       /* clean up directory */
@@ -718,6 +806,7 @@ GNUNET_FS_file_information_destroy (struct GNUNET_FS_FileInformation *fi,
                 &fi->keywords,
                 &fi->anonymity,
                 &fi->priority,
+                &no,
                 &fi->expirationTime,
                 &fi->client_info);
       GNUNET_free_non_null (fi->data.dir.dir_data);
@@ -737,6 +826,7 @@ GNUNET_FS_file_information_destroy (struct GNUNET_FS_FileInformation *fi,
                 &fi->keywords,
                 &fi->anonymity,
                 &fi->priority,
+                &fi->data.file.do_index,
                 &fi->expirationTime,
                 &fi->client_info);
     }