adding single location for no_forcestart configuration list
[oweals/gnunet.git] / src / fs / fs_file_information.c
index 01ce6f54bc860f35d207d6a36aa6b673b5b492f4..f004e4b295dc5f02a004dfc2f46e34819f3a8eef 100644 (file)
  * @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 "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. '&lt;zlib&gt;' 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
@@ -96,8 +37,7 @@ GNUNET_FS_meta_data_extract_from_file (struct GNUNET_CONTAINER_MetaData *md,
  *
  * @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.
+ *         can be used to read this fi-struct from disk.
  */
 const char *
 GNUNET_FS_file_information_get_id (struct GNUNET_FS_FileInformation *s)
@@ -139,6 +79,7 @@ GNUNET_FS_file_information_set_filename (struct GNUNET_FS_FileInformation *s,
     s->filename = NULL;
 }
 
+
 /**
  * Create an entry for a file in a publish-structure.
  *
@@ -148,8 +89,8 @@ GNUNET_FS_file_information_set_filename (struct GNUNET_FS_FileInformation *s,
  * @param keywords under which keywords should this file be available
  *         directly; can be NULL
  * @param meta metadata for the file
- * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
- *                GNUNET_SYSERR for simulation
+ * @param do_index #GNUNET_YES for index, #GNUNET_NO for insertion,
+ *                #GNUNET_SYSERR for simulation
  * @param bo block options
  * @return publish structure entry for the file
  */
@@ -166,7 +107,7 @@ GNUNET_FS_file_information_create_from_file (struct GNUNET_FS_Handle *h,
                                              *bo)
 {
   struct FileInfo *fi;
-  struct stat sbuf;
+  uint64_t fsize;
   struct GNUNET_FS_FileInformation *ret;
   const char *fn;
   const char *ss;
@@ -175,20 +116,21 @@ GNUNET_FS_file_information_create_from_file (struct GNUNET_FS_Handle *h,
   char fn_conv[MAX_PATH];
 #endif
 
-  if (0 != STAT (filename, &sbuf))
+  /* FIXME: should include_symbolic_links 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;
   }
   fi = GNUNET_FS_make_file_reader_context_ (filename);
-  if (fi == NULL)
+  if (NULL == fi)
   {
     GNUNET_break (0);
     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);
@@ -204,6 +146,9 @@ GNUNET_FS_file_information_create_from_file (struct GNUNET_FS_Handle *h,
 #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,
@@ -270,8 +215,8 @@ GNUNET_FS_file_information_create_from_data (struct GNUNET_FS_Handle *h,
  * @param keywords under which keywords should this file be available
  *         directly; can be NULL
  * @param meta metadata for the file
- * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
- *                GNUNET_SYSERR for simulation
+ * @param do_index #GNUNET_YES for index, #GNUNET_NO for insertion,
+ *                #GNUNET_SYSERR for simulation
  * @param bo block options
  * @return publish structure entry for the file
  */
@@ -296,7 +241,7 @@ GNUNET_FS_file_information_create_from_reader (struct GNUNET_FS_Handle *h,
     GNUNET_break (0);
     return NULL;
   }
-  ret = GNUNET_malloc (sizeof (struct GNUNET_FS_FileInformation));
+  ret = GNUNET_new (struct GNUNET_FS_FileInformation);
   ret->h = h;
   ret->client_info = client_info;
   ret->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
@@ -316,7 +261,7 @@ GNUNET_FS_file_information_create_from_reader (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
+ * @return #GNUNET_YES if so, #GNUNET_NO if not
  */
 int
 GNUNET_FS_file_information_is_directory (const struct GNUNET_FS_FileInformation
@@ -328,9 +273,6 @@ GNUNET_FS_file_information_is_directory (const struct GNUNET_FS_FileInformation
 
 /**
  * Create an entry for an empty directory in a publish-structure.
- * This function should be used by applications for which the
- * use of "GNUNET_FS_file_information_create_from_directory"
- * is not appropriate.
  *
  * @param h handle to the file sharing subsystem
  * @param client_info initial value for the client-info value for this entry
@@ -355,7 +297,7 @@ GNUNET_FS_file_information_create_empty_directory (struct GNUNET_FS_Handle *h,
 {
   struct GNUNET_FS_FileInformation *ret;
 
-  ret = GNUNET_malloc (sizeof (struct GNUNET_FS_FileInformation));
+  ret = GNUNET_new (struct GNUNET_FS_FileInformation);
   ret->h = h;
   ret->client_info = client_info;
   ret->meta = GNUNET_CONTAINER_meta_data_duplicate (meta);
@@ -371,19 +313,19 @@ GNUNET_FS_file_information_create_empty_directory (struct GNUNET_FS_Handle *h,
 /**
  * Add an entry to a directory in a publish-structure.  Clients
  * should never modify publish structures that were passed to
- * "GNUNET_FS_publish_start" already.
+ * #GNUNET_FS_publish_start already.
  *
  * @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
- *            must not include "dir" in its structure
- * @return GNUNET_OK on success, GNUNET_SYSERR on error
+ *            must not include @a dir in its structure
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  */
 int
 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;
@@ -399,16 +341,16 @@ GNUNET_FS_file_information_add (struct GNUNET_FS_FileInformation *dir,
 /**
  * Inspect a file or directory in a publish-structure.  Clients
  * should never modify publish structures that were passed to
- * "GNUNET_FS_publish_start" already.  When called on a directory,
- * this function will FIRST call "proc" with information about
+ * #GNUNET_FS_publish_start already.  When called on a directory,
+ * this function will FIRST call @a proc with information about
  * the directory itself and then for each of the files in the
  * directory (but not for files in subdirectories).  When called
- * on a file, "proc" will be called exactly once (with information
+ * on a file, @a proc will be called exactly once (with information
  * about the specific file).
  *
  * @param dir the directory
  * @param proc function to call on each entry
- * @param proc_cls closure for proc
+ * @param proc_cls closure for @a proc
  */
 void
 GNUNET_FS_file_information_inspect (struct GNUNET_FS_FileInformation *dir,
@@ -421,12 +363,13 @@ GNUNET_FS_file_information_inspect (struct GNUNET_FS_FileInformation *dir,
   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)
@@ -434,9 +377,9 @@ GNUNET_FS_file_information_inspect (struct GNUNET_FS_FileInformation *dir,
     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,
-              (pos->is_directory) ? &no : &pos->data.file.do_index,
+              (pos->is_directory == GNUNET_YES) ? &no : &pos->data.file.do_index,
               &pos->client_info))
       break;
     pos = pos->next;
@@ -446,13 +389,13 @@ GNUNET_FS_file_information_inspect (struct GNUNET_FS_FileInformation *dir,
 
 /**
  * Destroy publish-structure.  Clients should never destroy publish
- * structures that were passed to "GNUNET_FS_publish_start" already.
+ * structures that were passed to #GNUNET_FS_publish_start already.
  *
  * @param fi structure to destroy
  * @param cleaner function to call on each entry in the structure
  *        (useful to clean up client_info); can be NULL; return
  *        values are ignored
- * @param cleaner_cls closure for cleaner
+ * @param cleaner_cls closure for @a cleaner
  */
 void
 GNUNET_FS_file_information_destroy (struct GNUNET_FS_FileInformation *fi,
@@ -463,7 +406,7 @@ GNUNET_FS_file_information_destroy (struct GNUNET_FS_FileInformation *fi,
   int no;
 
   no = GNUNET_NO;
-  if (fi->is_directory)
+  if (GNUNET_YES == fi->is_directory)
   {
     /* clean up directory */
     while (NULL != (pos = fi->data.dir.entries))
@@ -480,8 +423,11 @@ GNUNET_FS_file_information_destroy (struct GNUNET_FS_FileInformation *fi,
   else
   {
     /* call clean-up function of the reader */
-    if (fi->data.file.reader != NULL)
-      fi->data.file.reader (fi->data.file.reader_cls, 0, 0, NULL, NULL);
+    if (NULL != fi->data.file.reader)
+    {
+      (void) fi->data.file.reader (fi->data.file.reader_cls, 0, 0, NULL, NULL);
+      fi->data.file.reader = NULL;
+    }
     /* clean up client-info */
     if (NULL != cleaner)
       cleaner (cleaner_cls, fi, fi->data.file.file_size, fi->meta,
@@ -490,7 +436,10 @@ GNUNET_FS_file_information_destroy (struct GNUNET_FS_FileInformation *fi,
   }
   GNUNET_free_non_null (fi->filename);
   GNUNET_free_non_null (fi->emsg);
-  GNUNET_free_non_null (fi->chk_uri);
+  if (NULL != fi->sks_uri)
+      GNUNET_FS_uri_destroy (fi->sks_uri);
+  if (NULL != fi->chk_uri)
+      GNUNET_FS_uri_destroy (fi->chk_uri);
   /* clean up serialization */
   if ((NULL != fi->serialization) && (0 != UNLINK (fi->serialization)))
     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
@@ -500,9 +449,9 @@ GNUNET_FS_file_information_destroy (struct GNUNET_FS_FileInformation *fi,
   if (NULL != fi->meta)
     GNUNET_CONTAINER_meta_data_destroy (fi->meta);
   GNUNET_free_non_null (fi->serialization);
-  if (fi->te != NULL)
+  if (NULL != fi->te)
   {
-    GNUNET_FS_tree_encoder_finish (fi->te, NULL, NULL);
+    GNUNET_FS_tree_encoder_finish (fi->te, NULL);
     fi->te = NULL;
   }
   GNUNET_free (fi);