aboutsummaryrefslogtreecommitdiff
path: root/src/rps/rps-test_util.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/rps/rps-test_util.c')
-rw-r--r--src/rps/rps-test_util.c99
1 files changed, 99 insertions, 0 deletions
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;
56 */ 56 */
57static unsigned num_bits_buf_unaligned; 57static unsigned num_bits_buf_unaligned;
58 58
59static struct GNUNET_CONTAINER_MultiHashMap *open_files;
60
61
62
63/**
64 * @brief Get file handle
65 *
66 * If necessary, create file handle and store it with the other file handles.
67 *
68 * @param name Name of the file
69 *
70 * @return File handle
71 */
72struct GNUNET_DISK_FileHandle *
73get_file_handle (const char *name)
74{
75 struct GNUNET_HashCode hash;
76 struct GNUNET_DISK_FileHandle *fh;
77
78 if (NULL == open_files)
79 {
80 open_files = GNUNET_CONTAINER_multihashmap_create (16,
81 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
82 }
83 GNUNET_CRYPTO_hash (name,
84 strnlen (name,
85 512),
86 &hash);
87 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (open_files,
88 &hash))
89 {
90 fh = GNUNET_DISK_file_open (name,
91 GNUNET_DISK_OPEN_APPEND,
92 GNUNET_DISK_PERM_USER_READ |
93 GNUNET_DISK_PERM_USER_WRITE |
94 GNUNET_DISK_PERM_GROUP_READ);
95 GNUNET_CONTAINER_multihashmap_put (open_files,
96 &hash,
97 fh,
98 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
99 return fh;
100 }
101 else
102 {
103 fh = GNUNET_CONTAINER_multihashmap_get (open_files,
104 &hash);
105 return fh;
106 }
107}
108
109
110/**
111 * @brief Closes the file of the current entry
112 *
113 * Implements #GNUNET_CONTAINER_HashMapIterator
114 *
115 * @param cls unused
116 * @param key unused
117 * @param value the file handle
118 *
119 * @return #GNUNET_YES if we should continue to
120 * iterate,
121 * #GNUNET_NO if not.
122 */
123int
124close_files_iter (void *cls,
125 const struct GNUNET_HashCode *key,
126 void *value)
127{
128 (void) cls;
129 (void) key;
130 struct GNUNET_DISK_FileHandle *fh = value;
131
132 if (NULL != fh)
133 {
134 GNUNET_DISK_file_close (fh);
135 }
136 return GNUNET_YES;
137}
138
139
140/**
141 * @brief Close all files that were opened with #get_file_handle
142 *
143 * @return Success of iterating over files
144 */
145int
146close_all_files ()
147{
148 int ret;
149
150 ret = GNUNET_CONTAINER_multihashmap_iterate (open_files,
151 close_files_iter,
152 NULL);
153 GNUNET_CONTAINER_multihashmap_destroy (open_files);
154 return ret;
155}
156
157
59 158
60void 159void
61to_file_raw (const char *file_name, const char *buf, size_t size_buf) 160to_file_raw (const char *file_name, const char *buf, size_t size_buf)