From: Julius Bünger Date: Tue, 27 Nov 2018 23:00:05 +0000 (+0100) Subject: RPS tests/profiler: Keep files for dumping internals open X-Git-Tag: v0.11.0~206 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=3cb06d8896196df2238c2b6dda59ba871af8b276;p=oweals%2Fgnunet.git RPS tests/profiler: Keep files for dumping internals open --- diff --git a/src/rps/gnunet-rps-profiler.c b/src/rps/gnunet-rps-profiler.c index 5ccf1017e..35a77e1bb 100644 --- a/src/rps/gnunet-rps-profiler.c +++ b/src/rps/gnunet-rps-profiler.c @@ -1102,6 +1102,7 @@ shutdown_op (void *cls) { clean_peer (i); } + close_all_files(); } static void diff --git a/src/rps/gnunet-service-rps.c b/src/rps/gnunet-service-rps.c index 20b314db3..f121db5b8 100644 --- a/src/rps/gnunet-service-rps.c +++ b/src/rps/gnunet-service-rps.c @@ -4556,6 +4556,7 @@ shutdown_task (void *cls) GNUNET_free (tmp_att_peer); } #endif /* ENABLE_MALICIOUS */ + close_all_files(); } diff --git a/src/rps/rps-test_util.c b/src/rps/rps-test_util.c index a6ea033cd..d0d195229 100644 --- a/src/rps/rps-test_util.c +++ b/src/rps/rps-test_util.c @@ -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) diff --git a/src/rps/rps-test_util.h b/src/rps/rps-test_util.h index a806f11cd..5dfcc5f6a 100644 --- a/src/rps/rps-test_util.h +++ b/src/rps/rps-test_util.h @@ -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, ...) diff --git a/src/rps/test_rps.c b/src/rps/test_rps.c index 63a6007ae..72dc90b17 100644 --- a/src/rps/test_rps.c +++ b/src/rps/test_rps.c @@ -806,6 +806,7 @@ shutdown_op (void *cls) GNUNET_TESTBED_operation_done (rps_peers[i].op); } } + close_all_files(); }