fixing common off-by-one error with respect to maximum message size
[oweals/gnunet.git] / src / fs / fs_file_information.c
index 2d01947417cbf2c62519818e3441945d89a67a2d..2e9b7b6678426e86ff9df19dc12f6b433ee0bae2 100644 (file)
 
 
 /**
- * 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;
 }
 
 
@@ -182,17 +164,16 @@ GNUNET_FS_file_information_create_from_file (struct GNUNET_FS_Handle *h,
                                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,
@@ -217,41 +198,6 @@ GNUNET_FS_file_information_create_from_file (struct GNUNET_FS_Handle *h,
 }
 
 
-/**
- * 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.
  *
@@ -284,10 +230,15 @@ GNUNET_FS_file_information_create_from_data (struct GNUNET_FS_Handle *h,
                                             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,
@@ -333,6 +284,12 @@ GNUNET_FS_file_information_create_from_reader (struct GNUNET_FS_Handle *h,
 {
   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;  
@@ -461,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);
@@ -670,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
@@ -762,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,
@@ -771,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;
@@ -779,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,
@@ -787,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;
@@ -811,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 */
@@ -829,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);
@@ -848,11 +826,11 @@ 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);
     }
   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 */