arg
[oweals/gnunet.git] / src / fs / fs_file_information.c
index d9acc959563b1a8f6c2916a0f440a7cd1ad9defa..b248fae44353d1440316ed52fba20714fa68fb31 100644 (file)
@@ -1,10 +1,10 @@
 /*
      This file is part of GNUnet.
-     (C) 2009 Christian Grothoff (and other contributing authors)
+     (C) 2009, 2011 Christian Grothoff (and other contributing authors)
 
      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
@@ -24,7 +24,6 @@
  * @author Christian Grothoff
  *
  * TODO:
- * - serialization/deserialization (& deserialization API)
  * - metadata filename clean up code
  * - metadata/ksk generation for directories from contained files
  */
 #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
@@ -65,11 +132,7 @@ GNUNET_FS_file_information_get_id (struct GNUNET_FS_FileInformation *s)
  * @param meta metadata for the file
  * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
  *                GNUNET_SYSERR for simulation
- * @param anonymity what is the desired anonymity level for sharing?
- * @param priority what is the priority for OUR node to
- *   keep this file available?  Use 0 for maximum anonymity and
- *   minimum reliability...
- * @param expirationTime when should this content expire?
+ * @param bo block options
  * @return publish structure entry for the file
  */
 struct GNUNET_FS_FileInformation *
@@ -79,9 +142,7 @@ GNUNET_FS_file_information_create_from_file (struct GNUNET_FS_Handle *h,
                                             const struct GNUNET_FS_Uri *keywords,
                                             const struct GNUNET_CONTAINER_MetaData *meta,
                                             int do_index,
-                                            uint32_t anonymity,
-                                            uint32_t priority,
-                                            struct GNUNET_TIME_Absolute expirationTime)
+                                            const struct GNUNET_FS_BlockOptions *bo)
 {
   struct FileInfo *fi;
   struct stat sbuf;
@@ -110,9 +171,9 @@ GNUNET_FS_file_information_create_from_file (struct GNUNET_FS_Handle *h,
                                                       keywords,
                                                       meta,
                                                       do_index,
-                                                      anonymity,
-                                                      priority,
-                                                      expirationTime);
+                                                      bo);
+  if (ret == NULL)
+    return NULL;
   ret->h = h;
   ret->filename = GNUNET_strdup (filename);
   fn = filename;
@@ -121,7 +182,7 @@ GNUNET_FS_file_information_create_from_file (struct GNUNET_FS_Handle *h,
     fn = ss + 1;
   GNUNET_CONTAINER_meta_data_insert (ret->meta,
                                     "<gnunet>",
-                                    EXTRACTOR_METATYPE_FILENAME,
+                                    EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME,
                                     EXTRACTOR_METAFORMAT_C_STRING,
                                     "text/plain",
                                     fn,
@@ -143,11 +204,7 @@ GNUNET_FS_file_information_create_from_file (struct GNUNET_FS_Handle *h,
  * @param meta metadata for the file
  * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
  *                GNUNET_SYSERR for simulation
- * @param anonymity what is the desired anonymity level for sharing?
- * @param priority what is the priority for OUR node to
- *   keep this file available?  Use 0 for maximum anonymity and
- *   minimum reliability...
- * @param expirationTime when should this content expire?
+ * @param bo block options
  * @return publish structure entry for the file
  */
 struct GNUNET_FS_FileInformation *
@@ -158,9 +215,7 @@ GNUNET_FS_file_information_create_from_data (struct GNUNET_FS_Handle *h,
                                             const struct GNUNET_FS_Uri *keywords,
                                             const struct GNUNET_CONTAINER_MetaData *meta,
                                             int do_index,
-                                            uint32_t anonymity,
-                                            uint32_t priority,
-                                            struct GNUNET_TIME_Absolute expirationTime)
+                                            const struct GNUNET_FS_BlockOptions *bo)
 {
   if (GNUNET_YES == do_index)        
     {
@@ -175,9 +230,7 @@ GNUNET_FS_file_information_create_from_data (struct GNUNET_FS_Handle *h,
                                                        keywords,
                                                        meta,
                                                        do_index,
-                                                       anonymity,
-                                                       priority,
-                                                       expirationTime);
+                                                       bo);
 }
 
 
@@ -194,11 +247,7 @@ GNUNET_FS_file_information_create_from_data (struct GNUNET_FS_Handle *h,
  * @param meta metadata for the file
  * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
  *                GNUNET_SYSERR for simulation
- * @param anonymity what is the desired anonymity level for sharing?
- * @param priority what is the priority for OUR node to
- *   keep this file available?  Use 0 for maximum anonymity and
- *   minimum reliability...
- * @param expirationTime when should this content expire?
+ * @param bo block options
  * @return publish structure entry for the file
  */
 struct GNUNET_FS_FileInformation *
@@ -210,9 +259,7 @@ GNUNET_FS_file_information_create_from_reader (struct GNUNET_FS_Handle *h,
                                               const struct GNUNET_FS_Uri *keywords,
                                               const struct GNUNET_CONTAINER_MetaData *meta,
                                               int do_index,
-                                              uint32_t anonymity,
-                                              uint32_t priority,
-                                              struct GNUNET_TIME_Absolute expirationTime)
+                                              const struct GNUNET_FS_BlockOptions *bo)
 {
   struct GNUNET_FS_FileInformation *ret;
 
@@ -229,13 +276,11 @@ GNUNET_FS_file_information_create_from_reader (struct GNUNET_FS_Handle *h,
   if (ret->meta == NULL)
     ret->meta = GNUNET_CONTAINER_meta_data_create ();
   ret->keywords = (keywords == NULL) ? NULL : GNUNET_FS_uri_dup (keywords);
-  ret->expirationTime = expirationTime;
   ret->data.file.reader = reader; 
   ret->data.file.reader_cls = reader_cls;
   ret->data.file.do_index = do_index;
   ret->data.file.file_size = length;
-  ret->anonymity = anonymity;
-  ret->priority = priority;
+  ret->bo = *bo;
   return ret;
 }
 
@@ -281,24 +326,15 @@ struct DirScanCls
   char *emsg; 
 
   /**
-   * Should files be indexed?
-   */ 
-  int do_index;
-
-  /**
-   * Desired anonymity level.
+   * Block options.
    */
-  uint32_t anonymity;
+  const struct GNUNET_FS_BlockOptions *bo;
 
   /**
-   * Desired publishing priority.
-   */
-  uint32_t priority;
+   * Should files be indexed?
+   */ 
+  int do_index;
 
-  /**
-   * Expiration time for publication.
-   */
-  struct GNUNET_TIME_Absolute expiration;
 };
 
 
@@ -337,9 +373,7 @@ dir_scan_cb (void *cls,
                                                             dsc->scanner,
                                                             dsc->scanner_cls,
                                                             dsc->do_index,
-                                                            dsc->anonymity,
-                                                            dsc->priority,
-                                                            dsc->expiration,
+                                                            dsc->bo,
                                                             &dsc->emsg);
       if (NULL == fi)
        {
@@ -350,10 +384,9 @@ dir_scan_cb (void *cls,
   else
     {
       meta = GNUNET_CONTAINER_meta_data_create ();
-      GNUNET_CONTAINER_meta_data_extract_from_file (meta,
-                                                   filename,
-                                                   dsc->extractors);
-      // FIXME: remove path from filename in metadata!
+      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,
@@ -362,9 +395,7 @@ dir_scan_cb (void *cls,
                                                        ksk_uri,
                                                        meta,
                                                        dsc->do_index,
-                                                       dsc->anonymity,
-                                                       dsc->priority,
-                                                       dsc->expiration);
+                                                       dsc->bo);
       GNUNET_CONTAINER_meta_data_destroy (meta);
       GNUNET_FS_uri_destroy (keywords);
       GNUNET_FS_uri_destroy (ksk_uri);
@@ -391,9 +422,7 @@ dir_scan_cb (void *cls,
  * @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 anonymity desired anonymity level
- * @param priority priority for publishing
- * @param expirationTime expiration for publication
+ * @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)
@@ -404,9 +433,7 @@ GNUNET_FS_directory_scanner_default (void *cls,
                                     struct GNUNET_FS_Handle *h,
                                     const char *dirname,
                                     int do_index,
-                                    uint32_t anonymity,
-                                    uint32_t priority,
-                                    struct GNUNET_TIME_Absolute expirationTime,
+                                    const struct GNUNET_FS_BlockOptions *bo,
                                     GNUNET_FS_FileProcessor proc,
                                     void *proc_cls,
                                     char **emsg)
@@ -421,9 +448,7 @@ GNUNET_FS_directory_scanner_default (void *cls,
   dsc.scanner = &GNUNET_FS_directory_scanner_default;
   dsc.scanner_cls = cls;
   dsc.do_index = do_index;
-  dsc.anonymity = anonymity;
-  dsc.priority = priority;
-  dsc.expiration = expirationTime;
+  dsc.bo = bo;
   if (-1 == GNUNET_DISK_directory_scan (dirname,
                                        &dir_scan_cb,
                                        &dsc))
@@ -487,11 +512,7 @@ dirproc (void *cls,
  * @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 anonymity what is the desired anonymity level for sharing?
- * @param priority what is the priority for OUR node to
- *   keep this file available?  Use 0 for maximum anonymity and
- *   minimum reliability...
- * @param expirationTime when should this content expire?
+ * @param bo block options
  * @param emsg where to store an error message
  * @return publish structure entry for the directory, NULL on error
  */
@@ -502,9 +523,7 @@ GNUNET_FS_file_information_create_from_directory (struct GNUNET_FS_Handle *h,
                                                  GNUNET_FS_DirectoryScanner scanner,
                                                  void *scanner_cls,
                                                  int do_index,
-                                                 uint32_t anonymity,
-                                                 uint32_t priority,
-                                                 struct GNUNET_TIME_Absolute expirationTime,
+                                                 const struct GNUNET_FS_BlockOptions *bo,
                                                  char **emsg)
 {
   struct GNUNET_FS_FileInformation *ret;
@@ -513,6 +532,7 @@ GNUNET_FS_file_information_create_from_directory (struct GNUNET_FS_Handle *h,
   struct GNUNET_CONTAINER_MetaData *meta;
   const char *fn;
   const char *ss;
+  char *dn;
 
   dc.entries = NULL;
   meta = GNUNET_CONTAINER_meta_data_create ();
@@ -521,9 +541,7 @@ GNUNET_FS_file_information_create_from_directory (struct GNUNET_FS_Handle *h,
           h,
           filename,
           do_index,
-          anonymity,
-          priority,
-          expirationTime,
+          bo,
           &dirproc,
           &dc,
           emsg);
@@ -533,9 +551,7 @@ GNUNET_FS_file_information_create_from_directory (struct GNUNET_FS_Handle *h,
                                                           client_info,
                                                           ksk,
                                                           meta,
-                                                          anonymity,
-                                                          priority,
-                                                          expirationTime);
+                                                          bo);
   GNUNET_CONTAINER_meta_data_destroy (meta);
   ret->data.dir.entries = dc.entries;
   while (dc.entries != NULL)
@@ -544,21 +560,39 @@ GNUNET_FS_file_information_create_from_directory (struct GNUNET_FS_Handle *h,
       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_asprintf (&dn,
+                  "%s/", 
+                  fn);
   GNUNET_CONTAINER_meta_data_insert (ret->meta,
                                     "<gnunet>",
-                                    EXTRACTOR_METATYPE_FILENAME,
+                                    EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME,
                                     EXTRACTOR_METAFORMAT_C_STRING,
                                     "text/plain",
-                                    fn,
-                                    strlen (fn) + 1);
+                                    dn,
+                                    strlen (dn) + 1);
+  GNUNET_free (dn);
   ret->filename = GNUNET_strdup (filename);
   return ret;
 }
 
 
+/**
+ * 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
@@ -570,11 +604,7 @@ GNUNET_FS_file_information_create_from_directory (struct GNUNET_FS_Handle *h,
  * @param meta metadata for the directory
  * @param keywords under which keywords should this directory be available
  *         directly; can be NULL
- * @param anonymity what is the desired anonymity level for sharing?
- * @param priority what is the priority for OUR node to
- *   keep this file available?  Use 0 for maximum anonymity and
- *   minimum reliability...
- * @param expirationTime when should this content expire?
+ * @param bo block options
  * @return publish structure entry for the directory , NULL on error
  */
 struct GNUNET_FS_FileInformation *
@@ -582,9 +612,7 @@ GNUNET_FS_file_information_create_empty_directory (struct GNUNET_FS_Handle *h,
                                                   void *client_info,
                                                   const struct GNUNET_FS_Uri *keywords,
                                                   const struct GNUNET_CONTAINER_MetaData *meta,
-                                                  uint32_t anonymity,
-                                                  uint32_t priority,
-                                                  struct GNUNET_TIME_Absolute expirationTime)
+                                                  const struct GNUNET_FS_BlockOptions *bo)
 {
   struct GNUNET_FS_FileInformation *ret;
 
@@ -593,10 +621,8 @@ GNUNET_FS_file_information_create_empty_directory (struct GNUNET_FS_Handle *h,
   ret->client_info = client_info;
   ret->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
   ret->keywords = GNUNET_FS_uri_dup (keywords);
-  ret->expirationTime = expirationTime;
+  ret->bo = *bo;
   ret->is_directory = GNUNET_YES;
-  ret->anonymity = anonymity;
-  ret->priority = priority;
   return ret;
 }
 
@@ -651,16 +677,17 @@ 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,
            (dir->is_directory) ? dir->data.dir.dir_size : dir->data.file.file_size,
            dir->meta,
            &dir->keywords,
-           &dir->anonymity,
-           &dir->priority,
-           &dir->expirationTime,
+           &dir->bo,
+           (dir->is_directory) ? &no : &dir->data.file.do_index,
            &dir->client_info))
     return;
   if (! dir->is_directory)
@@ -668,15 +695,15 @@ 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,
                (pos->is_directory) ? pos->data.dir.dir_size : pos->data.file.file_size,
                pos->meta,
                &pos->keywords,
-               &pos->anonymity,
-               &pos->priority,
-               &pos->expirationTime,
+               &pos->bo,
+               (dir->is_directory) ? &no : &dir->data.file.do_index,
                &pos->client_info))
        break;
       pos = pos->next;
@@ -700,7 +727,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 */
@@ -716,9 +745,8 @@ GNUNET_FS_file_information_destroy (struct GNUNET_FS_FileInformation *fi,
                 fi->data.dir.dir_size,
                 fi->meta,
                 &fi->keywords,
-                &fi->anonymity,
-                &fi->priority,
-                &fi->expirationTime,
+                &fi->bo,
+                &no,
                 &fi->client_info);
       GNUNET_free_non_null (fi->data.dir.dir_data);
     }
@@ -735,9 +763,8 @@ GNUNET_FS_file_information_destroy (struct GNUNET_FS_FileInformation *fi,
                 fi->data.file.file_size,
                 fi->meta,
                 &fi->keywords,
-                &fi->anonymity,
-                &fi->priority,
-                &fi->expirationTime,
+                &fi->bo,
+                &fi->data.file.do_index,
                 &fi->client_info);
     }
   GNUNET_free_non_null (fi->filename);