-fixes
[oweals/gnunet.git] / src / fs / gnunet-helper-fs-publish.c
index a1a2c1a808d11bb1bc88a9851a43b79795458b0d..4f704640818e46a31fc741770a6dee576fd14267 100644 (file)
@@ -87,6 +87,36 @@ struct ScanTreeNode
 static struct EXTRACTOR_PluginList *plugins;
 
 
+/**
+ * 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;
+}
+
+
 /**
  * Free memory of the 'tree' structure
  *
@@ -223,10 +253,9 @@ scan_callback (void *cls,
     return GNUNET_SYSERR;
   }
   chld->parent = rc->parent;
-  if (NULL != rc->parent)
-    GNUNET_CONTAINER_DLL_insert (rc->parent->children_head,
-                                rc->parent->children_tail,
-                                chld);
+  GNUNET_CONTAINER_DLL_insert (rc->parent->children_head,
+                              rc->parent->children_tail,
+                              chld);
   return GNUNET_OK;
 }
 
@@ -269,7 +298,7 @@ preprocess_file (const char *filename,
   item->filename = GNUNET_strdup (filename);
   item->is_directory = (S_ISDIR (sbuf.st_mode)) ? GNUNET_YES : GNUNET_NO;
   item->file_size = (uint64_t) sbuf.st_size;
-  if (item->is_directory)
+  if (item->is_directory == GNUNET_YES)
   {
     struct RecursionContext rc;
 
@@ -278,15 +307,14 @@ preprocess_file (const char *filename,
     GNUNET_DISK_directory_scan (filename, 
                                &scan_callback, 
                                &rc);    
-    if (rc.stop == GNUNET_YES) 
+    if ( (rc.stop == GNUNET_YES) ||
+        (GNUNET_OK !=
+         write_message (GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_PROGRESS_DIRECTORY,
+                        "..", 3)) )
     {
       free_tree (item);
       return GNUNET_SYSERR;
     }
-    if (GNUNET_OK !=
-       write_message (GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_PROGRESS_DIRECTORY,
-                      "..", 3))
-      return GNUNET_SYSERR;
   }
   *dst = item;
   return GNUNET_OK;
@@ -296,7 +324,6 @@ preprocess_file (const char *filename,
 /**
  * Extract metadata from files.
  *
- * @param ds directory scanner context
  * @param item entry we are processing
  * @return GNUNET_OK on success, GNUNET_SYSERR on fatal errors
  */
@@ -307,7 +334,7 @@ extract_files (struct ScanTreeNode *item)
   ssize_t size;
   size_t slen;
 
-  if (item->is_directory)
+  if (item->is_directory == GNUNET_YES)
   {
     /* for directories, we simply only descent, no extraction, no
        progress reporting */
@@ -322,13 +349,11 @@ extract_files (struct ScanTreeNode *item)
   
   /* this is the expensive operation, *afterwards* we'll check for aborts */
   meta = GNUNET_CONTAINER_meta_data_create ();
-  GNUNET_FS_meta_data_extract_from_file (meta, 
-                                        item->filename,
-                                        plugins);
+  if (NULL != plugins)
+    EXTRACTOR_extract (plugins, item->filename, NULL, 0, &add_to_md, meta);
   slen = strlen (item->filename) + 1;
   size = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
-  if ( (-1 == size) ||
-       (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE - slen) )
+  if (-1 == size)
   {
     /* no meta data */
     GNUNET_CONTAINER_meta_data_destroy (meta);
@@ -344,8 +369,8 @@ extract_files (struct ScanTreeNode *item)
     
     memcpy (buf, item->filename, slen);
     size = GNUNET_CONTAINER_meta_data_serialize (meta,
-                                                &dst, size,
-                                                GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL);
+                                                &dst, size - slen,
+                                                GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
     GNUNET_CONTAINER_meta_data_destroy (meta);
     if (GNUNET_OK !=
        write_message (GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_META_DATA,
@@ -374,6 +399,13 @@ int main(int argc,
   const char *ex;
   struct ScanTreeNode *root;
 
+#if WINDOWS
+  /* We're using stdout to communicate binary data back to the parent; use
+   * binary mode.
+   */
+  _setmode (1, _O_BINARY);
+#endif
+
   /* parse command line */
   if ( (argc != 3) && (argc != 2) )
   {