-fix #2598
[oweals/gnunet.git] / src / fs / fs_dirmetascan.c
index 5b7d2d0c1a811fb90cada8a17d4003748a9dc94d..0afec3886c18324ea72fc9e19ffe5f03cc187029 100644 (file)
@@ -92,7 +92,8 @@ struct GNUNET_FS_DirScanner
 
 
 /**
- * Abort the scan.
+ * Abort the scan.  Must not be called from within the progress_callback
+ * function.
  *
  * @param ds directory scanner structure
  */
@@ -151,7 +152,7 @@ advance (struct GNUNET_FS_ShareTreeItem *pos)
   
   GNUNET_assert (NULL != pos);
   moved = 0; /* must not terminate, even on file, otherwise "normal" */
-  while ( (pos->is_directory) ||
+  while ( (pos->is_directory == GNUNET_YES) ||
          (0 == moved) )
   {
     if ( (moved != -1) &&
@@ -202,7 +203,7 @@ expand_tree (struct GNUNET_FS_ShareTreeItem *parent,
   GNUNET_asprintf (&chld->short_filename,
                   "%s%s",
                   GNUNET_STRINGS_get_short_name (filename),
-                  is_directory ? "/" : "");
+                  is_directory == GNUNET_YES ? "/" : "");
   /* make sure we do not end with '//' */
   slen = strlen (chld->short_filename);
   if ( (slen >= 2) &&
@@ -247,7 +248,7 @@ finish_scan (void *cls,
  * @param client always NULL
  * @param msg message from the helper process
  */
-static void
+static int
 process_helper_msgs (void *cls, 
                     void *client,
                     const struct GNUNET_MessageHeader *msg)
@@ -256,6 +257,11 @@ process_helper_msgs (void *cls,
   const char *filename;
   size_t left;
 
+#if 0
+  fprintf (stderr, "DMS parses %u-byte message of type %u\n",
+          (unsigned int) ntohs (msg->size),
+          (unsigned int) ntohs (msg->type));
+#endif
   left = ntohs (msg->size) - sizeof (struct GNUNET_MessageHeader);
   filename = (const char*) &msg[1];
   switch (ntohs (msg->type))
@@ -275,7 +281,7 @@ process_helper_msgs (void *cls,
     else
       (void) expand_tree (ds->pos,
                          filename, GNUNET_NO);
-    return;
+    return GNUNET_OK;
   case GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_PROGRESS_DIRECTORY:
     if (filename[left-1] != '\0')
     {
@@ -290,7 +296,7 @@ process_helper_msgs (void *cls,
        break;
       }
       ds->pos = ds->pos->parent;
-      return;
+      return GNUNET_OK;
     }
     ds->progress_callback (ds->progress_callback_cls, 
                           filename, GNUNET_YES,
@@ -299,16 +305,16 @@ process_helper_msgs (void *cls,
                           filename, GNUNET_YES);
     if (NULL == ds->toplevel)
       ds->toplevel = ds->pos;
-    return;
+    return GNUNET_OK;
   case GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_ERROR:
     break;
   case GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_SKIP_FILE:
-    if (filename[left-1] != '\0')
+    if ('\0' != filename[left-1])
       break;
     ds->progress_callback (ds->progress_callback_cls, 
                           filename, GNUNET_SYSERR,
                           GNUNET_FS_DIRSCANNER_FILE_IGNORED);
-    return;
+    return GNUNET_OK;
   case GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_COUNTING_DONE:
     if (0 != left)
     {
@@ -324,9 +330,9 @@ process_helper_msgs (void *cls,
                           NULL, GNUNET_SYSERR,
                           GNUNET_FS_DIRSCANNER_ALL_COUNTED);
     ds->pos = ds->toplevel;
-    if (ds->pos->is_directory)
+    if (GNUNET_YES == ds->pos->is_directory)
       ds->pos = advance (ds->pos);
-    return;
+    return GNUNET_OK;
   case GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_META_DATA:
     {
       size_t nlen;
@@ -376,7 +382,7 @@ process_helper_msgs (void *cls,
       }
       ds->pos->ksk_uri = GNUNET_FS_uri_ksk_create_from_meta_data (ds->pos->meta);
       ds->pos = advance (ds->pos);      
-      return;
+      return GNUNET_OK;
     }
   case GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_FINISHED:
     if (NULL != ds->pos)
@@ -396,7 +402,7 @@ process_helper_msgs (void *cls,
     }
     ds->stop_task = GNUNET_SCHEDULER_add_now (&finish_scan,
                                              ds);
-    return;
+    return GNUNET_OK;
   default:
     GNUNET_break (0);
     break;
@@ -404,6 +410,24 @@ process_helper_msgs (void *cls,
   ds->progress_callback (ds->progress_callback_cls, 
                         NULL, GNUNET_SYSERR,
                         GNUNET_FS_DIRSCANNER_INTERNAL_ERROR);
+  return GNUNET_OK;
+}
+
+
+/**
+ * Function called if our helper process died.
+ *
+ * @param cls the 'struct GNUNET_FS_DirScanner' callback.
+ */
+static void
+helper_died_cb (void *cls)
+{
+  struct GNUNET_FS_DirScanner *ds = cls;
+
+  ds->helper = NULL;
+  ds->progress_callback (ds->progress_callback_cls, 
+                        NULL, GNUNET_SYSERR,
+                        GNUNET_FS_DIRSCANNER_INTERNAL_ERROR);  
 }
 
 
@@ -411,7 +435,7 @@ process_helper_msgs (void *cls,
  * Start a directory scanner thread.
  *
  * @param filename name of the directory to scan
- * @param GNUNET_YES to not to run libextractor on files (only build a tree)
+ * @param disable_extractor GNUNET_YES to not to run libextractor on files (only build a tree)
  * @param ex if not NULL, must be a list of extra plugins for extractor
  * @param cb the callback to call when there are scanning progress messages
  * @param cb_cls closure for 'cb'
@@ -447,12 +471,13 @@ GNUNET_FS_directory_scan_start (const char *filename,
   ds->args[1] = ds->filename_expanded;
   ds->args[2] = ds->ex_arg;
   ds->args[3] = NULL;
-  ds->helper = GNUNET_HELPER_start ("gnunet-helper-fs-publish",
+  ds->helper = GNUNET_HELPER_start (GNUNET_NO,
+                                   "gnunet-helper-fs-publish",
                                    ds->args,
                                    &process_helper_msgs,
-                                   ds);
+                                   &helper_died_cb, ds);
   if (NULL == ds->helper)
-  {
+    {
     GNUNET_free (filename_expanded);
     GNUNET_free (ds);
     return NULL;