-fix
[oweals/gnunet.git] / src / fs / gnunet-helper-fs-publish.c
index 9594c19c2b2588f05dabe65ed2f37b0da7a6280a..b96559cd14d62fdede6e84b0c8aa63d256eccecb 100644 (file)
@@ -69,7 +69,8 @@ struct ScanTreeNode
   char *filename;
 
   /**
-   * Size of the file (if it is a file), in bytes
+   * Size of the file (if it is a file), in bytes.
+   * At the moment it is set to 0 for directories.
    */
   uint64_t file_size;
 
@@ -253,10 +254,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;
 }
 
@@ -276,8 +276,11 @@ preprocess_file (const char *filename,
 {
   struct ScanTreeNode *item;
   struct stat sbuf;
+  uint64_t fsize = 0;
 
-  if (0 != STAT (filename, &sbuf))
+  if ((0 != STAT (filename, &sbuf)) ||
+      ((!S_ISDIR (sbuf.st_mode)) && (GNUNET_OK != GNUNET_DISK_file_size (
+      filename, &fsize, GNUNET_NO, GNUNET_YES))))
   {
     /* If the file doesn't exist (or is not stat-able for any other reason)
        skip it (but report it), but do continue. */
@@ -298,8 +301,8 @@ preprocess_file (const char *filename,
   item = GNUNET_malloc (sizeof (struct ScanTreeNode));
   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)
+  item->file_size = fsize;
+  if (item->is_directory == GNUNET_YES)
   {
     struct RecursionContext rc;
 
@@ -335,7 +338,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 */
@@ -354,8 +357,7 @@ extract_files (struct ScanTreeNode *item)
     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);
@@ -372,7 +374,12 @@ 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);
+                                                GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
+    if (size < 0)
+    {
+      GNUNET_break (0);
+      size = 0;
+    }
     GNUNET_CONTAINER_meta_data_destroy (meta);
     if (GNUNET_OK !=
        write_message (GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_META_DATA,
@@ -401,6 +408,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) )
   {