RPS tests/profiler: Keep files for dumping internals open
authorJulius Bünger <buenger@mytum.de>
Tue, 27 Nov 2018 23:00:05 +0000 (00:00 +0100)
committerJulius Bünger <buenger@mytum.de>
Tue, 27 Nov 2018 23:00:05 +0000 (00:00 +0100)
src/rps/gnunet-rps-profiler.c
src/rps/gnunet-service-rps.c
src/rps/rps-test_util.c
src/rps/rps-test_util.h
src/rps/test_rps.c

index 5ccf1017e3a31b09bbee662625db1e21bbefdbd4..35a77e1bb925f69094147a38ef7df7e64ff69b61 100644 (file)
@@ -1102,6 +1102,7 @@ shutdown_op (void *cls)
   {
     clean_peer (i);
   }
+  close_all_files();
 }
 
 static void
index 20b314db337c6e3cc738fcc5d5d7c66c0f1ce836..f121db5b8157cae2a6613631239b74e1ab7f921d 100644 (file)
@@ -4556,6 +4556,7 @@ shutdown_task (void *cls)
     GNUNET_free (tmp_att_peer);
   }
 #endif /* ENABLE_MALICIOUS */
+  close_all_files();
 }
 
 
index a6ea033cd23420b52df20bfdd5ab7009af655045..d0d195229cef2c01da79159e7818a4e9cd708792 100644 (file)
@@ -56,6 +56,105 @@ static char buf_unaligned;
  */
 static unsigned num_bits_buf_unaligned;
 
+static struct GNUNET_CONTAINER_MultiHashMap *open_files;
+
+
+
+/**
+ * @brief Get file handle
+ *
+ * If necessary, create file handle and store it with the other file handles.
+ *
+ * @param name Name of the file
+ *
+ * @return File handle
+ */
+struct GNUNET_DISK_FileHandle *
+get_file_handle (const char *name)
+{
+  struct GNUNET_HashCode hash;
+  struct GNUNET_DISK_FileHandle *fh;
+
+  if (NULL == open_files)
+  {
+    open_files = GNUNET_CONTAINER_multihashmap_create (16,
+        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
+  }
+  GNUNET_CRYPTO_hash (name,
+                      strnlen (name,
+                               512),
+                      &hash);
+  if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (open_files,
+                                                           &hash))
+  {
+    fh = GNUNET_DISK_file_open (name,
+                                GNUNET_DISK_OPEN_APPEND,
+                                GNUNET_DISK_PERM_USER_READ |
+                                GNUNET_DISK_PERM_USER_WRITE |
+                                GNUNET_DISK_PERM_GROUP_READ);
+    GNUNET_CONTAINER_multihashmap_put (open_files,
+                                       &hash,
+                                       fh,
+                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
+    return fh;
+  }
+  else
+  {
+    fh = GNUNET_CONTAINER_multihashmap_get (open_files,
+                                            &hash);
+    return fh;
+  }
+}
+
+
+/**
+ * @brief Closes the file of the current entry
+ *
+ * Implements #GNUNET_CONTAINER_HashMapIterator
+ *
+ * @param cls unused
+ * @param key unused
+ * @param value the file handle
+ *
+ * @return #GNUNET_YES if we should continue to
+ *         iterate,
+ *         #GNUNET_NO if not.
+ */
+int
+close_files_iter (void *cls,
+                  const struct GNUNET_HashCode *key,
+                  void *value)
+{
+  (void) cls;
+  (void) key;
+  struct GNUNET_DISK_FileHandle *fh = value;
+
+  if (NULL != fh)
+  {
+    GNUNET_DISK_file_close (fh);
+  }
+  return GNUNET_YES;
+}
+
+
+/**
+ * @brief Close all files that were opened with #get_file_handle
+ *
+ * @return Success of iterating over files
+ */
+int
+close_all_files ()
+{
+  int ret;
+
+  ret = GNUNET_CONTAINER_multihashmap_iterate (open_files,
+                                               close_files_iter,
+                                               NULL);
+  GNUNET_CONTAINER_multihashmap_destroy (open_files);
+  return ret;
+}
+
+
 
 void
 to_file_raw (const char *file_name, const char *buf, size_t size_buf)
index a806f11cdc2c84de720d6434f834da6df0ed6b1c..5dfcc5f6abff55bda47547377478ace00d659df9 100644 (file)
@@ -38,24 +38,44 @@ string_to_auth_key (const char *str);
 char *
 create_file (const char *name);
 
+/**
+ * @brief Get file handle
+ *
+ * If necessary, create file handle and store it with the other file handles.
+ *
+ * @param name Name of the file
+ *
+ * @return File handle
+ */
+struct GNUNET_DISK_FileHandle *
+get_file_handle (const char *name);
+
+/**
+ * @brief Close all files that were opened with #get_file_handle
+ *
+ * @return Success of iterating over files
+ */
+int
+close_all_files ();
+
 /**
  * This function is used to facilitate writing important information to disk
  */
 #ifdef TO_FILE
-#  define to_file(file_name, ...) do {char tmp_buf[512] = "";\
+#define to_file(file_name, ...) do {GNUNET_assert (NULL != file_name);\
+    char tmp_buf[512] = "";\
     int size;\
     size = GNUNET_snprintf(tmp_buf,sizeof(tmp_buf),__VA_ARGS__);\
     if (0 > size)\
       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,\
            "Failed to create tmp_buf\n");\
     else\
-      GNUNET_DISK_fn_write(file_name, tmp_buf, strnlen(tmp_buf, 512),\
-                            GNUNET_DISK_PERM_USER_READ |\
-                            GNUNET_DISK_PERM_USER_WRITE |\
-                            GNUNET_DISK_PERM_GROUP_READ |\
-                            GNUNET_DISK_PERM_OTHER_READ);\
+      GNUNET_DISK_file_write (get_file_handle (file_name),\
+                              tmp_buf,\
+                              strnlen (tmp_buf, 512));\
   } while (0);
 
+
 #define to_file_w_len(file_name, len, ...) do {char tmp_buf[len];\
     int size;\
     memset (tmp_buf, 0, len);\
@@ -64,11 +84,9 @@ create_file (const char *name);
       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,\
            "Failed to create tmp_buf\n");\
     else\
-      GNUNET_DISK_fn_write(file_name, tmp_buf, strnlen(tmp_buf, len), \
-                            GNUNET_DISK_PERM_USER_READ |\
-                            GNUNET_DISK_PERM_USER_WRITE |\
-                            GNUNET_DISK_PERM_GROUP_READ |\
-                            GNUNET_DISK_PERM_OTHER_READ);\
+      GNUNET_DISK_file_write (get_file_handle (file_name),\
+                              tmp_buf,\
+                              strnlen (tmp_buf, 512));\
   } while (0);
 #else /* TO_FILE */
 #  define to_file(file_name, ...)
index 63a6007aed3724184714fc4433123dbd41c72acb..72dc90b17b4cda27eb1b4333cdd0d03fbc4a0402 100644 (file)
@@ -806,6 +806,7 @@ shutdown_op (void *cls)
       GNUNET_TESTBED_operation_done (rps_peers[i].op);
     }
   }
+  close_all_files();
 }