-LRN: calculate file size for single files when needed and use GNUNET_DISK_file_size...
authorChristian Grothoff <christian@grothoff.org>
Mon, 19 Mar 2012 10:48:51 +0000 (10:48 +0000)
committerChristian Grothoff <christian@grothoff.org>
Mon, 19 Mar 2012 10:48:51 +0000 (10:48 +0000)
22 files changed:
src/fs/fs_download.c
src/fs/fs_file_information.c
src/fs/fs_unindex.c
src/fs/gnunet-directory.c
src/fs/gnunet-helper-fs-publish.c
src/fs/test_fs_download.c
src/fs/test_fs_download_indexed.c
src/fs/test_fs_download_persistence.c
src/hello/gnunet-hello.c
src/include/gnunet_disk_lib.h
src/statistics/gnunet-service-statistics.c
src/testing/gnunet-testing.c
src/testing/testing_group.c
src/topology/gnunet-daemon-topology.c
src/transport/gnunet-service-transport_blacklist.c
src/transport/plugin_transport_http_server.c
src/transport/transport-testing.c
src/util/crypto_hash.c
src/util/crypto_rsa.c
src/util/disk.c
src/util/pseudonym.c
src/util/test_disk.c

index 0596d85e3f5cdc3c3cbe3e476998472f044f64b3..490beef5e9bcca468bed7fd33c2a5cd57953a477 100644 (file)
@@ -2068,7 +2068,7 @@ GNUNET_FS_download_start (struct GNUNET_FS_Handle *h,
   {
     dc->filename = GNUNET_strdup (filename);
     if (GNUNET_YES == GNUNET_DISK_file_test (filename))
-      GNUNET_break (GNUNET_OK == GNUNET_DISK_file_size (filename, &dc->old_file_size, GNUNET_YES));
+      GNUNET_break (GNUNET_OK == GNUNET_DISK_file_size (filename, &dc->old_file_size, GNUNET_YES, GNUNET_YES));
   }
   if (GNUNET_FS_uri_test_loc (dc->uri))
     GNUNET_assert (GNUNET_OK ==
@@ -2178,7 +2178,7 @@ GNUNET_FS_download_start_from_search (struct GNUNET_FS_Handle *h,
   {
     dc->filename = GNUNET_strdup (filename);
     if (GNUNET_YES == GNUNET_DISK_file_test (filename))
-      GNUNET_break (GNUNET_OK == GNUNET_DISK_file_size (filename, &dc->old_file_size, GNUNET_YES));
+      GNUNET_break (GNUNET_OK == GNUNET_DISK_file_size (filename, &dc->old_file_size, GNUNET_YES, GNUNET_YES));
   }
   if (GNUNET_FS_uri_test_loc (dc->uri))
     GNUNET_assert (GNUNET_OK ==
index 85a076fee24f886ba9c508f0a37c2d3638ba7c7f..806592784bfc4ae9e06280cdfac828417669f09a 100644 (file)
@@ -107,7 +107,7 @@ GNUNET_FS_file_information_create_from_file (struct GNUNET_FS_Handle *h,
                                              *bo)
 {
   struct FileInfo *fi;
-  struct stat sbuf;
+  uint64_t fsize;
   struct GNUNET_FS_FileInformation *ret;
   const char *fn;
   const char *ss;
@@ -116,7 +116,8 @@ GNUNET_FS_file_information_create_from_file (struct GNUNET_FS_Handle *h,
   char fn_conv[MAX_PATH];
 #endif
 
-  if (0 != STAT (filename, &sbuf))
+  /* FIXME: should includeSymLinks be GNUNET_NO or GNUNET_YES here? */
+  if (GNUNET_OK != GNUNET_DISK_file_size (filename, &fsize, GNUNET_NO, GNUNET_YES))
   {
     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", filename);
     return NULL;
@@ -129,7 +130,7 @@ GNUNET_FS_file_information_create_from_file (struct GNUNET_FS_Handle *h,
   }
   ret =
       GNUNET_FS_file_information_create_from_reader (h, client_info,
-                                                     sbuf.st_size,
+                                                     fsize,
                                                      &GNUNET_FS_data_reader_file_,
                                                      fi, keywords, meta,
                                                      do_index, bo);
@@ -145,6 +146,9 @@ GNUNET_FS_file_information_create_from_file (struct GNUNET_FS_Handle *h,
 #endif
   while (NULL != (ss = strstr (fn, DIR_SEPARATOR_STR)))
     fn = ss + 1;
+/* FIXME: If we assume that on other platforms CRT is UTF-8-aware, then
+ * this should be changed to EXTRACTOR_METAFORMAT_UTF8
+ */
 #if !WINDOWS
   GNUNET_CONTAINER_meta_data_insert (ret->meta, "<gnunet>",
                                      EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME,
index 3c2ef8f6fe73cf2fa58d4c6ae94d7351773a409f..bf619b75acf127c48e66969231b7b984044d99ea 100644 (file)
@@ -755,7 +755,7 @@ GNUNET_FS_unindex_start (struct GNUNET_FS_Handle *h, const char *filename,
   struct GNUNET_FS_ProgressInfo pi;
   uint64_t size;
 
-  if (GNUNET_OK != GNUNET_DISK_file_size (filename, &size, GNUNET_YES))
+  if (GNUNET_OK != GNUNET_DISK_file_size (filename, &size, GNUNET_YES, GNUNET_YES))
     return NULL;
   ret = GNUNET_malloc (sizeof (struct GNUNET_FS_UnindexContext));
   ret->h = h;
index 0721ea90e8af956796f1da15fe92d44f1d0b9fa6..c722f57ff232c581719194460a51449799b19d57 100644 (file)
@@ -136,7 +136,7 @@ run (void *cls, char *const *args, const char *cfgfile,
   i = 0;
   while (NULL != (filename = args[i++]))
   {
-    if ((GNUNET_OK != GNUNET_DISK_file_size (filename, &size, GNUNET_YES)) ||
+    if ((GNUNET_OK != GNUNET_DISK_file_size (filename, &size, GNUNET_YES, GNUNET_YES)) ||
         (NULL ==
          (h =
           GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ,
index 4f704640818e46a31fc741770a6dee576fd14267..4bb77fa225eb7ac3cb27622af0423c9d30b05d76 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;
 
@@ -275,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. */
@@ -297,7 +301,7 @@ 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;
+  item->file_size = fsize;
   if (item->is_directory == GNUNET_YES)
   {
     struct RecursionContext rc;
index 570eab909c9976950d967e2eee888664f7f0da6d..198a8154e8ad25cadda944ccbb060cd6d786d9bd 100644 (file)
@@ -118,7 +118,7 @@ abort_download_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     GNUNET_FS_download_stop (download, GNUNET_YES);
     download = NULL;
   }
-  GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_size (fn, &size, GNUNET_YES));
+  GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_size (fn, &size, GNUNET_YES, GNUNET_NO));
   GNUNET_assert (size == FILESIZE);
   GNUNET_DISK_directory_remove (fn);
   GNUNET_free (fn);
index e8504f17596752b618dc4ad04738238b76d2157c..c00b9e7175df34481a326c086c69def76e19e2cf 100644 (file)
@@ -119,7 +119,7 @@ abort_download_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     GNUNET_FS_download_stop (download, GNUNET_YES);
     download = NULL;
   }
-  GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_size (fn, &size, GNUNET_YES));
+  GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_size (fn, &size, GNUNET_YES, GNUNET_NO));
   GNUNET_assert (size == FILESIZE);
   GNUNET_DISK_directory_remove (fn);
   GNUNET_free (fn);
index bcb1c54b66d587ac8bbeb6d79db6a30f59fa9665..3a6d9553e330ba30abe754acf5f94c241112f3e8 100644 (file)
@@ -113,7 +113,7 @@ abort_download_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     GNUNET_FS_download_stop (download, GNUNET_YES);
     download = NULL;
   }
-  GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_size (fn, &size, GNUNET_YES));
+  GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_size (fn, &size, GNUNET_YES, GNUNET_NO));
   GNUNET_assert (size == FILESIZE);
   GNUNET_DISK_directory_remove (fn);
   GNUNET_free (fn);
index d64833b4280d6fe5edd235439226add0f4ed1da6..cbbdce3da285ccaaed246a0469ee94f7affcc2da 100644 (file)
@@ -122,7 +122,7 @@ main (int argc, char *argv[])
             _("Call with name of HELLO file to modify.\n"));
     return 1;
   }
-  if (GNUNET_OK != GNUNET_DISK_file_size (argv[1], &fsize, GNUNET_YES))
+  if (GNUNET_OK != GNUNET_DISK_file_size (argv[1], &fsize, GNUNET_YES, GNUNET_YES))
   {
     FPRINTF (stderr,
             _("Error accessing file `%s': %s\n"),
index 18f553576d86595e19b4a572ddc3cfd2ee44d25c..d6ec0fe80e9d488878b24f817a53e153fb33fc66 100644 (file)
@@ -302,8 +302,8 @@ GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle *h, OFF_T offset,
 
 
 /**
- * Get the size of the file (or directory)
- * of the given file (in bytes).
+ * Get the size of the file (or directory) of the given file (in
+ * bytes).
  *
  * @param filename name of the file or directory
  * @param size set to the size of the file (or,
@@ -311,11 +311,13 @@ GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle *h, OFF_T offset,
  *             of all sizes of files in the directory)
  * @param includeSymLinks should symbolic links be
  *        included?
- * @return GNUNET_OK on success, GNUNET_SYSERR on error
+ * @param singleFileMode GNUNET_YES to only get size of one file
+ *        and return GNUNET_SYSERR for directories.
+ * @return GNUNET_SYSERR on error, GNUNET_OK on success
  */
 int
 GNUNET_DISK_file_size (const char *filename, uint64_t * size,
-                       int includeSymLinks);
+                       int includeSymLinks, int singleFileMode);
 
 
 /**
index 58ef763e3ad0ce8141b0dc2ce3c225c1a306d05e..1ec07ad5dc40eb0ee649ead935ea5df36b155f6e 100644 (file)
@@ -224,6 +224,7 @@ load (struct GNUNET_SERVER_Handle *server)
   char *fn;
   struct GNUNET_BIO_ReadHandle *rh;
   struct stat sb;
+  uint64_t fsize;
   char *buf;
   struct GNUNET_SERVER_MessageStreamTokenizer *mst;
   char *emsg;
@@ -232,12 +233,12 @@ load (struct GNUNET_SERVER_Handle *server)
                                       NULL);
   if (fn == NULL)
     return;
-  if ((0 != stat (fn, &sb)) || (sb.st_size == 0))
+  if ((GNUNET_OK != GNUNET_DISK_file_size (fn, &fsize, GNUNET_NO, GNUNET_YES)) || (fsize == 0))
   {
     GNUNET_free (fn);
     return;
   }
-  buf = GNUNET_malloc (sb.st_size);
+  buf = GNUNET_malloc (fsize);
   rh = GNUNET_BIO_read_open (fn);
   if (!rh)
   {
@@ -245,7 +246,7 @@ load (struct GNUNET_SERVER_Handle *server)
     GNUNET_free (fn);
     return;
   }
-  if (GNUNET_OK != GNUNET_BIO_read (rh, fn, buf, sb.st_size))
+  if (GNUNET_OK != GNUNET_BIO_read (rh, fn, buf, fsize))
   {
     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "read", fn);
     GNUNET_break (GNUNET_OK == GNUNET_BIO_read_close (rh, &emsg));
@@ -256,10 +257,10 @@ load (struct GNUNET_SERVER_Handle *server)
   }
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
               _("Loading %llu bytes of statistics from `%s'\n"),
-              (unsigned long long) sb.st_size, fn);
+              fsize, fn);
   mst = GNUNET_SERVER_mst_create (&inject_message, server);
   GNUNET_break (GNUNET_OK ==
-                GNUNET_SERVER_mst_receive (mst, NULL, buf, sb.st_size,
+                GNUNET_SERVER_mst_receive (mst, NULL, buf, fsize,
                                            GNUNET_YES, GNUNET_NO));
   GNUNET_SERVER_mst_destroy (mst);
   GNUNET_free (buf);
index 0caa28e255900b276b482d78e096e2d320ca89cd..bdbb5e8c5e4acf02c31f08e542022c743d228923 100644 (file)
@@ -170,7 +170,7 @@ create_hostkeys (const unsigned int no)
       return 1;
     }
 
-    if (GNUNET_YES != GNUNET_DISK_file_size (hostkey_src_file, &fs, GNUNET_YES))
+    if (GNUNET_OK != GNUNET_DISK_file_size (hostkey_src_file, &fs, GNUNET_YES, GNUNET_YES))
       fs = 0;
 
     if (0 != (fs % HOSTKEYFILESIZE))
index e8db98a91add24e7312017a5ec0dce62c4a3b839..49c0c1a33f9c193b3eb5784a2311742f72bc249d 100644 (file)
@@ -6078,7 +6078,7 @@ GNUNET_TESTING_daemons_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
         return NULL;
       }
 
-      if (GNUNET_YES != GNUNET_DISK_file_size (hostkeys_file, &fs, GNUNET_YES))
+      if (GNUNET_OK != GNUNET_DISK_file_size (hostkeys_file, &fs, GNUNET_YES, GNUNET_YES))
         fs = 0;
 #if DEBUG_TESTING
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
index 38a648afc03bce3d24f19f91857e42d951375427..4e332496c7469500d87da5a3637f4538d3d6a332 100644 (file)
@@ -970,7 +970,7 @@ read_friends_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
   char *data;
   size_t pos;
   struct GNUNET_PeerIdentity pid;
-  struct stat frstat;
+  uint64_t fsize;
   struct GNUNET_CRYPTO_HashAsciiEncoded enc;
   unsigned int entries_found;
   struct Peer *fl;
@@ -987,7 +987,8 @@ read_friends_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
     GNUNET_DISK_fn_write (fn, NULL, 0,
                           GNUNET_DISK_PERM_USER_READ |
                           GNUNET_DISK_PERM_USER_WRITE);
-  if (0 != STAT (fn, &frstat))
+  if (GNUNET_OK != GNUNET_DISK_file_size (fn,
+      &fsize, GNUNET_NO, GNUNET_YES))
   {
     if ((friends_only) || (minimum_friend_count > 0))
       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
@@ -995,14 +996,14 @@ read_friends_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
     GNUNET_free (fn);
     return;
   }
-  if (frstat.st_size == 0)
+  if (fsize == 0)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("Friends file `%s' is empty.\n"),
                 fn);
     GNUNET_free (fn);
     return;
   }
-  data = GNUNET_malloc_large (frstat.st_size);
+  data = GNUNET_malloc_large (fsize);
   if (data == NULL)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
@@ -1011,7 +1012,7 @@ read_friends_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
     GNUNET_free (fn);
     return;
   }
-  if (frstat.st_size != GNUNET_DISK_fn_read (fn, data, frstat.st_size))
+  if (fsize != GNUNET_DISK_fn_read (fn, data, fsize))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 _("Failed to read friends list from `%s'\n"), fn);
@@ -1021,11 +1022,11 @@ read_friends_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
   }
   entries_found = 0;
   pos = 0;
-  while ((pos < frstat.st_size) && isspace ((unsigned char) data[pos]))
+  while ((pos < fsize) && isspace ((unsigned char) data[pos]))
     pos++;
-  while ((frstat.st_size >= sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) &&
+  while ((fsize >= sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) &&
          (pos <=
-          frstat.st_size - sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)))
+          fsize - sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)))
   {
     memcpy (&enc, &data[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
     if (!isspace
@@ -1037,7 +1038,7 @@ read_friends_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
                   ("Syntax error in topology specification at offset %llu, skipping bytes.\n"),
                   (unsigned long long) pos);
       pos++;
-      while ((pos < frstat.st_size) && (!isspace ((unsigned char) data[pos])))
+      while ((pos < fsize) && (!isspace ((unsigned char) data[pos])))
         pos++;
       continue;
     }
@@ -1068,7 +1069,7 @@ read_friends_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
       }
     }
     pos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded);
-    while ((pos < frstat.st_size) && isspace ((unsigned char) data[pos]))
+    while ((pos < fsize) && isspace ((unsigned char) data[pos]))
       pos++;
   }
   GNUNET_free (data);
index 2089949f742f0c903b884a2c69fc63cc2b5d8a82..10362681ca183f59f2b9485d1394d281b76729d9 100644 (file)
@@ -221,7 +221,7 @@ read_blacklist_file ()
   size_t colon_pos;
   int tsize;
   struct GNUNET_PeerIdentity pid;
-  struct stat frstat;
+  uint64_t fsize;
   struct GNUNET_CRYPTO_HashAsciiEncoded enc;
   unsigned int entries_found;
   char *transport_name;
@@ -241,14 +241,15 @@ read_blacklist_file ()
     GNUNET_DISK_fn_write (fn, NULL, 0,
                           GNUNET_DISK_PERM_USER_READ |
                           GNUNET_DISK_PERM_USER_WRITE);
-  if (0 != STAT (fn, &frstat))
+  if (GNUNET_OK != GNUNET_DISK_file_size (fn,
+      &fsize, GNUNET_NO, GNUNET_YES))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 _("Could not read blacklist file `%s'\n"), fn);
     GNUNET_free (fn);
     return;
   }
-  if (frstat.st_size == 0)
+  if (fsize == 0)
   {
 #if DEBUG_TRANSPORT
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Blacklist file `%s' is empty.\n"),
@@ -258,9 +259,9 @@ read_blacklist_file ()
     return;
   }
   /* FIXME: use mmap */
-  data = GNUNET_malloc_large (frstat.st_size);
+  data = GNUNET_malloc_large (fsize);
   GNUNET_assert (data != NULL);
-  if (frstat.st_size != GNUNET_DISK_fn_read (fn, data, frstat.st_size))
+  if (fsize != GNUNET_DISK_fn_read (fn, data, fsize))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 _("Failed to read blacklist from `%s'\n"), fn);
@@ -270,17 +271,17 @@ read_blacklist_file ()
   }
   entries_found = 0;
   pos = 0;
-  while ((pos < frstat.st_size) && isspace ((unsigned char) data[pos]))
+  while ((pos < fsize) && isspace ((unsigned char) data[pos]))
     pos++;
-  while ((frstat.st_size >= sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) &&
+  while ((fsize >= sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) &&
          (pos <=
-          frstat.st_size - sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)))
+          fsize - sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)))
   {
     colon_pos = pos;
-    while ((colon_pos < frstat.st_size) && (data[colon_pos] != ':') &&
+    while ((colon_pos < fsize) && (data[colon_pos] != ':') &&
            (!isspace ((unsigned char) data[colon_pos])))
       colon_pos++;
-    if (colon_pos >= frstat.st_size)
+    if (colon_pos >= fsize)
     {
       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                   _
@@ -298,12 +299,12 @@ read_blacklist_file ()
                   ("Syntax error in blacklist file at offset %llu, skipping bytes.\n"),
                   (unsigned long long) colon_pos);
       pos = colon_pos;
-      while ((pos < frstat.st_size) && isspace ((unsigned char) data[pos]))
+      while ((pos < fsize) && isspace ((unsigned char) data[pos]))
         pos++;
       continue;
     }
     tsize = colon_pos - pos;
-    if ((pos >= frstat.st_size) || (pos + tsize >= frstat.st_size) ||
+    if ((pos >= fsize) || (pos + tsize >= fsize) ||
         (tsize == 0))
     {
       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
@@ -336,7 +337,7 @@ read_blacklist_file ()
                   ("Syntax error in blacklist file at offset %llu, skipping bytes.\n"),
                   (unsigned long long) pos);
       pos++;
-      while ((pos < frstat.st_size) && (!isspace ((unsigned char) data[pos])))
+      while ((pos < fsize) && (!isspace ((unsigned char) data[pos])))
         pos++;
       GNUNET_free_non_null (transport_name);
       continue;
@@ -367,7 +368,7 @@ read_blacklist_file ()
     }
     pos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded);
     GNUNET_free_non_null (transport_name);
-    while ((pos < frstat.st_size) && isspace ((unsigned char) data[pos]))
+    while ((pos < fsize) && isspace ((unsigned char) data[pos]))
       pos++;
   }
   GNUNET_STATISTICS_update (GST_stats, "# Transport entries blacklisted",
index 8ec5a5e43c1bc03a11b221918518a21e7934b16a..68b6a2a4f36851418401cb039595149bfcb792db 100644 (file)
@@ -95,12 +95,13 @@ static char *
 server_load_file (const char *file)
 {
   struct GNUNET_DISK_FileHandle *gn_file;
-  struct stat fstat;
+  uint64_t fsize;
   char *text = NULL;
 
-  if (0 != STAT (file, &fstat))
+  if (GNUNET_OK != GNUNET_DISK_file_size (file,
+      &fsize, GNUNET_NO, GNUNET_YES))
     return NULL;
-  text = GNUNET_malloc (fstat.st_size + 1);
+  text = GNUNET_malloc (fsize + 1);
   gn_file =
       GNUNET_DISK_file_open (file, GNUNET_DISK_OPEN_READ,
                              GNUNET_DISK_PERM_USER_READ);
@@ -109,13 +110,13 @@ server_load_file (const char *file)
     GNUNET_free (text);
     return NULL;
   }
-  if (GNUNET_SYSERR == GNUNET_DISK_file_read (gn_file, text, fstat.st_size))
+  if (GNUNET_SYSERR == GNUNET_DISK_file_read (gn_file, text, fsize))
   {
     GNUNET_free (text);
     GNUNET_DISK_file_close (gn_file);
     return NULL;
   }
-  text[fstat.st_size] = '\0';
+  text[fsize] = '\0';
   GNUNET_DISK_file_close (gn_file);
   return text;
 }
index 752106bfe9ed26bedb8d4d45411fd237928525a4..fd5cc4701d21786f8e2676f48924668f0864f10d 100644 (file)
@@ -661,7 +661,7 @@ GNUNET_TRANSPORT_TESTING_init ()
       return NULL;
     }
 
-    if (GNUNET_YES != GNUNET_DISK_file_size (hostkeys_file, &fs, GNUNET_YES))
+    if (GNUNET_OK != GNUNET_DISK_file_size (hostkeys_file, &fs, GNUNET_YES, GNUNET_YES))
       fs = 0;
 
     if (0 != (fs % HOSTKEYFILESIZE))
index aaf5175d7db556a5712472f1c3e56e6699ea510e..57e37f3fd225fdccfe8b876609b6e02f5e7e9982 100644 (file)
@@ -220,7 +220,7 @@ GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority,
     return NULL;
   }
   fhc->bsize = blocksize;
-  if (GNUNET_OK != GNUNET_DISK_file_size (filename, &fhc->fsize, GNUNET_NO))
+  if (GNUNET_OK != GNUNET_DISK_file_size (filename, &fhc->fsize, GNUNET_NO, GNUNET_YES))
   {
     GNUNET_free (fhc->filename);
     GNUNET_free (fhc);
index ee7e5e9f8238acdac54b7836feba200367dacfe2..0106f43be22049fd85ad460c8d54cff79bc615e3 100644 (file)
@@ -726,7 +726,7 @@ GNUNET_CRYPTO_rsa_key_create_from_file (const char *filename)
 
       return NULL;
     }
-    if (GNUNET_YES != GNUNET_DISK_file_size (filename, &fs, GNUNET_YES))
+    if (GNUNET_OK != GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES))
       fs = 0;
     if (fs < sizeof (struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded))
     {
index b6b458f151e17105f589127575b0c8d099efadf1..ae3ac4c413f25c22f602335e494e1a36e4e88a50 100644 (file)
@@ -112,6 +112,11 @@ struct GetFileSizeData
    * GNUNET_YES if symbolic links should be included.
    */
   int include_sym_links;
+
+  /**
+   * GNUNET_YES if mode is file-only (return total == -1 for directories).
+   */
+  int single_file_mode;
 };
 
 
@@ -176,6 +181,11 @@ getSizeRec (void *cls, const char *fn)
     return GNUNET_SYSERR;
   }
 #endif
+  if ((S_ISDIR (buf.st_mode)) && (gfsd->single_file_mode == GNUNET_YES))
+  {
+    errno = EISDIR;
+    return GNUNET_SYSERR;
+  }
   if ((!S_ISLNK (buf.st_mode)) || (gfsd->include_sym_links == GNUNET_YES))
     gfsd->total += buf.st_size;
   if ((S_ISDIR (buf.st_mode)) && (0 == ACCESS (fn, X_OK)) &&
@@ -290,11 +300,13 @@ GNUNET_DISK_file_seek (const struct GNUNET_DISK_FileHandle * h, OFF_T offset,
  *             of all sizes of files in the directory)
  * @param includeSymLinks should symbolic links be
  *        included?
+ * @param singleFileMode GNUNET_YES to only get size of one file
+ *        and return GNUNET_SYSERR for directories.
  * @return GNUNET_SYSERR on error, GNUNET_OK on success
  */
 int
 GNUNET_DISK_file_size (const char *filename, uint64_t * size,
-                       int includeSymLinks)
+                       int includeSymLinks, int singleFileMode)
 {
   struct GetFileSizeData gfsd;
   int ret;
@@ -302,6 +314,7 @@ GNUNET_DISK_file_size (const char *filename, uint64_t * size,
   GNUNET_assert (size != NULL);
   gfsd.total = 0;
   gfsd.include_sym_links = includeSymLinks;
+  gfsd.single_file_mode = singleFileMode;
   ret = getSizeRec (&gfsd, filename);
   *size = gfsd.total;
   return ret;
@@ -1350,7 +1363,7 @@ GNUNET_DISK_file_copy (const char *src, const char *dst)
   struct GNUNET_DISK_FileHandle *in;
   struct GNUNET_DISK_FileHandle *out;
 
-  if (GNUNET_OK != GNUNET_DISK_file_size (src, &size, GNUNET_YES))
+  if (GNUNET_OK != GNUNET_DISK_file_size (src, &size, GNUNET_YES, GNUNET_YES))
     return GNUNET_SYSERR;
   pos = 0;
   in = GNUNET_DISK_file_open (src, GNUNET_DISK_OPEN_READ,
index c2b95832ed12518b8fae02ae141c9faa24c1dc3e..d2efdc98f8fa58dfb4752d59f9c3e6f2042f24b4 100644 (file)
@@ -320,7 +320,7 @@ GNUNET_PSEUDONYM_name_uniquify (const struct GNUNET_CONFIGURATION_Handle *cfg,
 
   len = 0;
   if (0 == STAT (fn, &sbuf))
-    GNUNET_break (GNUNET_OK == GNUNET_DISK_file_size (fn, &len, GNUNET_YES));
+    GNUNET_break (GNUNET_OK == GNUNET_DISK_file_size (fn, &len, GNUNET_YES, GNUNET_YES));
   fh = GNUNET_DISK_file_open (fn,
                               GNUNET_DISK_OPEN_CREATE |
                               GNUNET_DISK_OPEN_READWRITE,
@@ -475,7 +475,7 @@ GNUNET_PSEUDONYM_name_to_id (const struct GNUNET_CONFIGURATION_Handle *cfg,
   GNUNET_assert (fn != NULL);
 
   if ((GNUNET_OK != GNUNET_DISK_file_test (fn) ||
-       (GNUNET_OK != GNUNET_DISK_file_size (fn, &len, GNUNET_YES))) ||
+       (GNUNET_OK != GNUNET_DISK_file_size (fn, &len, GNUNET_YES, GNUNET_YES))) ||
       ((idx + 1) * sizeof (GNUNET_HashCode) > len))
   {
     GNUNET_free (fn);
index 5462772567bfec06798c6251e3d4797093943fb5..149cec0b24ed2eb8c43418c7963cd46ccbe06283 100644 (file)
@@ -97,7 +97,7 @@ testOpenClose ()
   GNUNET_break (5 == GNUNET_DISK_file_write (fh, "Hello", 5));
   GNUNET_DISK_file_close (fh);
   GNUNET_break (GNUNET_OK ==
-                GNUNET_DISK_file_size (".testfile", &size, GNUNET_NO));
+                GNUNET_DISK_file_size (".testfile", &size, GNUNET_NO, GNUNET_YES));
   if (size != 5)
     return 1;
   GNUNET_break (0 == UNLINK (".testfile"));