2 This file is part of GNUnet.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @file rps/rps-test_util.c
21 * @brief Some utils faciliating the view into the internals for the sampler
22 * needed for evaluation
24 * @author Julius Bünger
28 #include "gnunet_util_lib.h"
32 #define LOG(kind, ...) GNUNET_log_from(kind,"rps-sampler",__VA_ARGS__)
40 to_file_ (const char *file_name, char *line)
42 struct GNUNET_DISK_FileHandle *f;
43 char output_buffer[512];
44 size_t output_buffer_size = 512;
45 char *output_buffer_p;
51 if (NULL == (f = GNUNET_DISK_file_open (file_name,
52 GNUNET_DISK_OPEN_APPEND |
53 GNUNET_DISK_OPEN_WRITE |
54 GNUNET_DISK_OPEN_CREATE,
55 GNUNET_DISK_PERM_USER_READ |
56 GNUNET_DISK_PERM_USER_WRITE |
57 GNUNET_DISK_PERM_GROUP_READ |
58 GNUNET_DISK_PERM_OTHER_READ)))
60 LOG (GNUNET_ERROR_TYPE_WARNING,
61 "Not able to open file %s\n",
65 output_buffer_size = strlen (line) + 18;
66 if (512 < output_buffer_size)
68 output_buffer_p = GNUNET_malloc ((output_buffer_size) * sizeof (char));
70 output_buffer_p = &output_buffer[0];
72 size = GNUNET_snprintf (output_buffer_p,
75 (GNUNET_TIME_absolute_get ().abs_value_us) / 1000000, // microsec -> sec
79 LOG (GNUNET_ERROR_TYPE_WARNING,
80 "Failed to write string to buffer (size: %i)\n",
85 size2 = GNUNET_DISK_file_write (f, output_buffer_p, size);
88 LOG (GNUNET_ERROR_TYPE_WARNING,
89 "Unable to write to file! (Size: %u, size2: %u)\n",
93 if (GNUNET_YES != GNUNET_DISK_file_close (f))
94 LOG (GNUNET_ERROR_TYPE_WARNING,
95 "Unable to close file\n");
100 if (512 < output_buffer_size)
102 GNUNET_free (output_buffer_p);
105 if (GNUNET_YES != GNUNET_DISK_file_close (f))
106 LOG (GNUNET_ERROR_TYPE_WARNING,
107 "Unable to close file\n");
111 auth_key_to_string (struct GNUNET_CRYPTO_AuthKey auth_key)
114 size_t name_buf_size;
118 size_t keylen = (sizeof (struct GNUNET_CRYPTO_AuthKey)) * 8;
120 name_buf_size = 512 * sizeof (char);
121 name_buf = GNUNET_malloc (name_buf_size);
124 keylen += 5 - keylen % 5;
126 buf = GNUNET_malloc (keylen + 1);
128 end = GNUNET_STRINGS_data_to_string (&(auth_key.key),
129 sizeof (struct GNUNET_CRYPTO_AuthKey),
143 size = GNUNET_snprintf (name_buf, name_buf_size, "sampler_el-%s", buf);
145 LOG (GNUNET_ERROR_TYPE_WARNING, "Failed to create name_buf\n");
153 struct GNUNET_CRYPTO_AuthKey
154 string_to_auth_key (const char *str)
156 struct GNUNET_CRYPTO_AuthKey auth_key;
159 GNUNET_STRINGS_string_to_data (str,
162 sizeof (struct GNUNET_CRYPTO_AuthKey)))
164 LOG (GNUNET_ERROR_TYPE_WARNING, "Failed to convert string to data\n");
172 create_file (const char *name)
175 size_t name_buf_size;
180 prefix = "/tmp/rps/";
181 name_buf_size = (strlen (prefix) + strlen (name) + 2) * sizeof (char);
182 name_buf = GNUNET_malloc (name_buf_size);
184 size = GNUNET_snprintf (name_buf, name_buf_size, "%s%s", prefix, name);
186 LOG (GNUNET_ERROR_TYPE_WARNING, "Failed to create name_buf\n");
188 if (GNUNET_YES != GNUNET_DISK_directory_create (prefix))
190 LOG (GNUNET_ERROR_TYPE_WARNING,
191 "Could not create directory %s.\n",
195 if (NULL == strstr (name, "sampler_el"))
196 {/* only append random string to sampler */
197 if (NULL == (file_name = GNUNET_DISK_mktemp (name_buf)))
198 LOG (GNUNET_ERROR_TYPE_WARNING, "Could not create file\n");
200 GNUNET_free (name_buf);
210 * @brief Try to ensure that `/tmp/rps` exists.
212 * @return #GNUNET_YES on success
213 * #GNUNET_SYSERR on failure
215 static int ensure_folder_exist (void)
217 if (GNUNET_NO == GNUNET_DISK_directory_test ("/tmp/rps/", GNUNET_NO))
219 GNUNET_DISK_directory_create ("/tmp/rps");
221 if (GNUNET_YES != GNUNET_DISK_directory_test ("/tmp/rps/", GNUNET_NO))
223 return GNUNET_SYSERR;
229 store_prefix_file_name (const struct GNUNET_PeerIdentity *peer,
232 unsigned int len_file_name;
233 unsigned int out_size;
235 const char *pid_long;
237 if (GNUNET_SYSERR == ensure_folder_exist()) return NULL;
238 pid_long = GNUNET_i2s_full (peer);
239 len_file_name = (strlen (prefix) +
243 file_name = GNUNET_malloc (len_file_name);
244 out_size = GNUNET_snprintf (file_name,
249 if (len_file_name < out_size ||
252 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
253 "Failed to write string to buffer (size: %i, out_size: %i)\n",
260 /* end of gnunet-service-rps.c */