-fix
[oweals/gnunet.git] / src / fs / gnunet-helper-fs-publish.c
index b96559cd14d62fdede6e84b0c8aa63d256eccecb..45f6d588d6a48f4de18bbfa9125e7aadbce10f7e 100644 (file)
@@ -186,6 +186,11 @@ write_message (uint16_t message_type,
 {
   struct GNUNET_MessageHeader hdr;
 
+#if 0
+  fprintf (stderr, "Helper sends %u-byte message of type %u\n",
+          (unsigned int) (sizeof (struct GNUNET_MessageHeader) + data_length),
+          (unsigned int) message_type);
+#endif
   hdr.type = htons (message_type);
   hdr.size = htons (sizeof (struct GNUNET_MessageHeader) + data_length);
   if ( (GNUNET_OK !=
@@ -205,7 +210,8 @@ write_message (uint16_t message_type,
  * the scan.  Does NOT yet add any metadata.
  *
  * @param filename file or directory to scan
- * @param dst where to store the resulting share tree item
+ * @param dst where to store the resulting share tree item;
+ *         NULL is stored in 'dst' upon recoverable errors (GNUNET_OK is returned)
  * @return GNUNET_OK on success, GNUNET_SYSERR on error
  */
 static int
@@ -253,6 +259,8 @@ scan_callback (void *cls,
     rc->stop = GNUNET_YES;
     return GNUNET_SYSERR;
   }
+  if (NULL == chld)
+    return GNUNET_OK;
   chld->parent = rc->parent;
   GNUNET_CONTAINER_DLL_insert (rc->parent->children_head,
                               rc->parent->children_tail,
@@ -267,7 +275,8 @@ scan_callback (void *cls,
  * the scan.  Does NOT yet add any metadata.
  *
  * @param filename file or directory to scan
- * @param dst where to store the resulting share tree item
+ * @param dst where to store the resulting share tree item;
+ *         NULL is stored in 'dst' upon recoverable errors (GNUNET_OK is returned) 
  * @return GNUNET_OK on success, GNUNET_SYSERR on error
  */
 static int
@@ -288,6 +297,8 @@ preprocess_file (const char *filename,
        write_message (GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_SKIP_FILE,
                       filename, strlen (filename) + 1))
       return GNUNET_SYSERR;
+    /* recoverable error, store 'NULL' in *dst */
+    *dst = NULL;
     return GNUNET_OK;
   }
 
@@ -302,7 +313,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 = fsize;
-  if (item->is_directory == GNUNET_YES)
+  if (GNUNET_YES == item->is_directory)
   {
     struct RecursionContext rc;
 
@@ -311,7 +322,7 @@ preprocess_file (const char *filename,
     GNUNET_DISK_directory_scan (filename, 
                                &scan_callback, 
                                &rc);    
-    if ( (rc.stop == GNUNET_YES) ||
+    if ( (GNUNET_YES == rc.stop) ||
         (GNUNET_OK !=
          write_message (GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_PROGRESS_DIRECTORY,
                         "..", 3)) )
@@ -338,7 +349,7 @@ extract_files (struct ScanTreeNode *item)
   ssize_t size;
   size_t slen;
 
-  if (item->is_directory == GNUNET_YES)
+  if (GNUNET_YES == item->is_directory)
   {
     /* for directories, we simply only descent, no extraction, no
        progress reporting */
@@ -402,7 +413,7 @@ extract_files (struct ScanTreeNode *item)
  * @return 0 on success
  */
 int main(int argc,
-        char **argv)
+        char *const *argv)
 {
   const char *filename_expanded;
   const char *ex;
@@ -413,10 +424,13 @@ int main(int argc,
    * binary mode.
    */
   _setmode (1, _O_BINARY);
+  /* Get utf-8-encoded arguments */
+  if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
+    return 5;
 #endif
 
   /* parse command line */
-  if ( (argc != 3) && (argc != 2) )
+  if ( (3 != argc) && (2 != argc) )
   {
     FPRINTF (stderr, 
             "%s",
@@ -425,7 +439,7 @@ int main(int argc,
   }
   filename_expanded = argv[1];
   ex = argv[2];
-  if ( (ex == NULL) ||
+  if ( (NULL == ex) ||
        (0 != strcmp (ex, "-")) )
   {
     plugins = EXTRACTOR_plugin_add_defaults (EXTRACTOR_OPTION_DEFAULT_POLICY);
@@ -446,14 +460,17 @@ int main(int argc,
   if (GNUNET_OK !=
       write_message (GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_COUNTING_DONE, NULL, 0))
     return 3;  
-  if (GNUNET_OK !=
-      extract_files (root))
+  if (NULL != root)
   {
-    (void) write_message (GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_ERROR, NULL, 0);
+    if (GNUNET_OK !=
+       extract_files (root))
+    {
+      (void) write_message (GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_ERROR, NULL, 0);
+      free_tree (root);
+      return 4;
+    }
     free_tree (root);
-    return 4;
   }
-  free_tree (root);
   /* enable "clean" shutdown by telling parent that we are done */
   (void) write_message (GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_FINISHED, NULL, 0);
   if (NULL != plugins)