-only trigger check config if we actually need it
[oweals/gnunet.git] / src / fs / gnunet-helper-fs-publish.c
index 36af59cc84fea53cdd3ff5e3d3cf52087452c985..58f76a0d817d236fa040653183caa1bc44d5a5f9 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2012 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2012 GNUnet e.V.
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -14,8 +14,8 @@
 
      You should have received a copy of the GNU General Public License
      along with GNUnet; see the file COPYING.  If not, write to the
-     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-     Boston, MA 02111-1307, USA.
+     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+     Boston, MA 02110-1301, USA.
 */
 
 /**
@@ -82,10 +82,12 @@ struct ScanTreeNode
 };
 
 
+#if HAVE_LIBEXTRACTOR
 /**
  * List of libextractor plugins to use for extracting.
  */
 static struct EXTRACTOR_PluginList *plugins;
+#endif
 
 /**
  * File descriptor we use for IPC with the parent.
@@ -93,6 +95,7 @@ static struct EXTRACTOR_PluginList *plugins;
 static int output_stream;
 
 
+#if HAVE_LIBEXTRACTOR
 /**
  * Add meta data that libextractor finds to our meta data
  * container.
@@ -111,22 +114,26 @@ static int output_stream;
  * @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)
+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;
 
   if ( ((EXTRACTOR_METAFORMAT_UTF8 == format) ||
        (EXTRACTOR_METAFORMAT_C_STRING == format)) &&
-       ('\0' != data[data_size - 1]) )
+       ('\0' != data[data_len - 1]) )
   {
     char zdata[data_len + 1];
     memcpy (zdata, data, data_len);
-    zdata[data_size] = '\0';
+    zdata[data_len] = '\0';
     (void) GNUNET_CONTAINER_meta_data_insert (md, plugin_name, type, format,
-                                             data_mime_type, zdata, data_len + 1);    
-  } 
+                                             data_mime_type, zdata, data_len + 1);
+  }
   else
   {
     (void) GNUNET_CONTAINER_meta_data_insert (md, plugin_name, type, format,
@@ -134,6 +141,7 @@ add_to_md (void *cls, const char *plugin_name, enum EXTRACTOR_MetaType type,
   }
   return 0;
 }
+#endif
 
 
 /**
@@ -141,7 +149,7 @@ add_to_md (void *cls, const char *plugin_name, enum EXTRACTOR_MetaType type,
  *
  * @param tree tree to free
  */
-static void 
+static void
 free_tree (struct ScanTreeNode *tree)
 {
   struct ScanTreeNode *pos;
@@ -151,7 +159,7 @@ free_tree (struct ScanTreeNode *tree)
   if (NULL != tree->parent)
     GNUNET_CONTAINER_DLL_remove (tree->parent->children_head,
                                 tree->parent->children_tail,
-                                tree);                          
+                                tree);
   GNUNET_free (tree->filename);
   GNUNET_free (tree);
 }
@@ -205,7 +213,7 @@ write_message (uint16_t message_type,
   struct GNUNET_MessageHeader hdr;
 
 #if 0
-  fprintf (stderr, 
+  fprintf (stderr,
           "Helper sends %u-byte message of type %u\n",
           (unsigned int) (sizeof (struct GNUNET_MessageHeader) + data_length),
           (unsigned int) message_type);
@@ -295,7 +303,7 @@ scan_callback (void *cls,
  *
  * @param filename file or directory to scan
  * @param dst where to store the resulting share tree item;
- *         NULL is stored in @a dst upon recoverable errors (#GNUNET_OK is returned) 
+ *         NULL is stored in @a dst upon recoverable errors (#GNUNET_OK is returned)
  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  */
 static int
@@ -323,12 +331,12 @@ preprocess_file (const char *filename,
 
   /* Report the progress */
   if (GNUNET_OK !=
-      write_message (S_ISDIR (sbuf.st_mode) 
+      write_message (S_ISDIR (sbuf.st_mode)
                     ? GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_PROGRESS_DIRECTORY
                     : GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_PROGRESS_FILE,
                     filename, strlen (filename) + 1))
     return GNUNET_SYSERR;
-  item = GNUNET_malloc (sizeof (struct ScanTreeNode));
+  item = GNUNET_new (struct ScanTreeNode);
   item->filename = GNUNET_strdup (filename);
   item->is_directory = (S_ISDIR (sbuf.st_mode)) ? GNUNET_YES : GNUNET_NO;
   item->file_size = fsize;
@@ -338,9 +346,9 @@ preprocess_file (const char *filename,
 
     rc.parent = item;
     rc.stop = GNUNET_NO;
-    GNUNET_DISK_directory_scan (filename, 
-                               &scan_callback, 
-                               &rc);    
+    GNUNET_DISK_directory_scan (filename,
+                               &scan_callback,
+                               &rc);
     if ( (GNUNET_YES == rc.stop) ||
         (GNUNET_OK !=
          write_message (GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_PROGRESS_DIRECTORY,
@@ -363,7 +371,7 @@ preprocess_file (const char *filename,
  */
 static int
 extract_files (struct ScanTreeNode *item)
-{  
+{
   struct GNUNET_CONTAINER_MetaData *meta;
   ssize_t size;
   size_t slen;
@@ -380,10 +388,16 @@ extract_files (struct ScanTreeNode *item)
        return GNUNET_SYSERR;
     return GNUNET_OK;
   }
-  
+
   /* this is the expensive operation, *afterwards* we'll check for aborts */
   meta = GNUNET_CONTAINER_meta_data_create ();
-  EXTRACTOR_extract (plugins, item->filename, NULL, 0, &add_to_md, meta);
+#if HAVE_LIBEXTRACTOR
+  EXTRACTOR_extract (plugins,
+                     item->filename,
+                     NULL, 0,
+                     &add_to_md,
+                     meta);
+#endif
   slen = strlen (item->filename) + 1;
   size = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
   if (-1 == size)
@@ -393,7 +407,7 @@ extract_files (struct ScanTreeNode *item)
     if (GNUNET_OK !=
        write_message (GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_META_DATA,
                       item->filename, slen))
-      return GNUNET_SYSERR;    
+      return GNUNET_SYSERR;
     return GNUNET_OK;
   }
   else if (size > (UINT16_MAX - sizeof (struct GNUNET_MessageHeader) - slen))
@@ -404,7 +418,7 @@ extract_files (struct ScanTreeNode *item)
   {
     char buf[size + slen];
     char *dst = &buf[slen];
-    
+
     memcpy (buf, item->filename, slen);
     size = GNUNET_CONTAINER_meta_data_serialize (meta,
                                                 &dst, size,
@@ -417,7 +431,7 @@ extract_files (struct ScanTreeNode *item)
     GNUNET_CONTAINER_meta_data_destroy (meta);
     if (GNUNET_OK !=
        write_message (GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_META_DATA,
-                      buf, 
+                      buf,
                       slen + size))
       return GNUNET_SYSERR;
   }
@@ -456,7 +470,7 @@ ignore_sigpipe ()
  * @param flags flags to use (O_RDONLY or O_WRONLY)
  */
 static void
-make_dev_zero (int fd, 
+make_dev_zero (int fd,
               int flags)
 {
   int z;
@@ -483,7 +497,7 @@ make_dev_zero (int fd,
  *                 otherwise custom plugins to load from LE
  * @return 0 on success
  */
-int 
+int
 main (int argc,
       char *const *argv)
 {
@@ -502,7 +516,7 @@ main (int argc,
   output_stream = 1; /* stdout */
 #else
   ignore_sigpipe ();
-  /* move stdout to some other FD for IPC, bind 
+  /* move stdout to some other FD for IPC, bind
      stdout/stderr to /dev/null */
   output_stream = dup (1);
   make_dev_zero (1, O_WRONLY);
@@ -512,7 +526,7 @@ main (int argc,
   /* parse command line */
   if ( (3 != argc) && (2 != argc) )
   {
-    FPRINTF (stderr, 
+    FPRINTF (stderr,
             "%s",
             "gnunet-helper-fs-publish needs exactly one or two arguments\n");
 #if WINDOWS
@@ -525,33 +539,39 @@ main (int argc,
   if ( (NULL == ex) ||
        (0 != strcmp (ex, "-")) )
   {
+#if HAVE_LIBEXTRACTOR
     plugins = EXTRACTOR_plugin_add_defaults (EXTRACTOR_OPTION_DEFAULT_POLICY);
     if (NULL != ex)
       plugins = EXTRACTOR_plugin_add_config (plugins, ex,
                                             EXTRACTOR_OPTION_DEFAULT_POLICY);
+#endif
   }
 
   /* scan tree to find out how much work there is to be done */
-  if (GNUNET_OK != preprocess_file (filename_expanded, 
+  if (GNUNET_OK != preprocess_file (filename_expanded,
                                    &root))
   {
     (void) write_message (GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_ERROR, NULL, 0);
+#if HAVE_LIBEXTRACTOR
     EXTRACTOR_plugin_remove_all (plugins);
+#endif
 #if WINDOWS
     GNUNET_free ((void*) argv);
 #endif
     return 2;
   }
-  /* signal that we're done counting files, so that a percentage of 
+  /* signal that we're done counting files, so that a percentage of
      progress can now be calculated */
   if (GNUNET_OK !=
       write_message (GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_COUNTING_DONE, NULL, 0))
   {
+#if HAVE_LIBEXTRACTOR
     EXTRACTOR_plugin_remove_all (plugins);
+#endif
 #if WINDOWS
     GNUNET_free ((void*) argv);
 #endif
-    return 3;  
+    return 3;
   }
   if (NULL != root)
   {
@@ -560,7 +580,9 @@ main (int argc,
     {
       (void) write_message (GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_ERROR, NULL, 0);
       free_tree (root);
+#if HAVE_LIBEXTRACTOR
       EXTRACTOR_plugin_remove_all (plugins);
+#endif
 #if WINDOWS
       GNUNET_free ((void*) argv);
 #endif
@@ -570,12 +592,13 @@ main (int argc,
   }
   /* enable "clean" shutdown by telling parent that we are done */
   (void) write_message (GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_FINISHED, NULL, 0);
+#if HAVE_LIBEXTRACTOR
   EXTRACTOR_plugin_remove_all (plugins);
+#endif
 #if WINDOWS
-  GNUNET_free ((void*) argv);  
+  GNUNET_free ((void*) argv);
 #endif
   return 0;
 }
 
 /* end of gnunet-helper-fs-publish.c */
-