aboutsummaryrefslogtreecommitdiff
path: root/src/messenger/gnunet-service-messenger_operation_store.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/messenger/gnunet-service-messenger_operation_store.c')
-rw-r--r--src/messenger/gnunet-service-messenger_operation_store.c104
1 files changed, 62 insertions, 42 deletions
diff --git a/src/messenger/gnunet-service-messenger_operation_store.c b/src/messenger/gnunet-service-messenger_operation_store.c
index 6a639d69e..670d5cdf5 100644
--- a/src/messenger/gnunet-service-messenger_operation_store.c
+++ b/src/messenger/gnunet-service-messenger_operation_store.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 Copyright (C) 2021 GNUnet e.V. 3 Copyright (C) 2021, 2023 GNUnet e.V.
4 4
5 GNUnet is free software: you can redistribute it and/or modify it 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 6 under the terms of the GNU Affero General Public License as published
@@ -33,12 +33,13 @@ void
33init_operation_store (struct GNUNET_MESSENGER_OperationStore *store, 33init_operation_store (struct GNUNET_MESSENGER_OperationStore *store,
34 struct GNUNET_MESSENGER_SrvRoom *room) 34 struct GNUNET_MESSENGER_SrvRoom *room)
35{ 35{
36 GNUNET_assert((store) && (room)); 36 GNUNET_assert ((store) && (room));
37 37
38 store->room = room; 38 store->room = room;
39 store->operations = GNUNET_CONTAINER_multihashmap_create(8, GNUNET_NO); 39 store->operations = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO);
40} 40}
41 41
42
42static int 43static int
43iterate_destroy_operations (void *cls, 44iterate_destroy_operations (void *cls,
44 const struct GNUNET_HashCode *key, 45 const struct GNUNET_HashCode *key,
@@ -46,20 +47,23 @@ iterate_destroy_operations (void *cls,
46{ 47{
47 struct GNUNET_MESSENGER_Operation *op = value; 48 struct GNUNET_MESSENGER_Operation *op = value;
48 49
49 destroy_operation(op); 50 destroy_operation (op);
50 51
51 return GNUNET_YES; 52 return GNUNET_YES;
52} 53}
53 54
55
54void 56void
55clear_operation_store (struct GNUNET_MESSENGER_OperationStore *store) 57clear_operation_store (struct GNUNET_MESSENGER_OperationStore *store)
56{ 58{
57 GNUNET_assert(store); 59 GNUNET_assert (store);
58 60
59 GNUNET_CONTAINER_multihashmap_iterate (store->operations, iterate_destroy_operations, NULL); 61 GNUNET_CONTAINER_multihashmap_iterate (store->operations,
60 GNUNET_CONTAINER_multihashmap_destroy(store->operations); 62 iterate_destroy_operations, NULL);
63 GNUNET_CONTAINER_multihashmap_destroy (store->operations);
61} 64}
62 65
66
63static int 67static int
64callback_scan_for_operations (void *cls, 68callback_scan_for_operations (void *cls,
65 const char *filename) 69 const char *filename)
@@ -69,37 +73,40 @@ callback_scan_for_operations (void *cls,
69 if (GNUNET_YES != GNUNET_DISK_file_test (filename)) 73 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
70 return GNUNET_OK; 74 return GNUNET_OK;
71 75
72 if ((strlen(filename) <= 4) || (0 != strcmp(filename + strlen(filename) - 4, ".cfg"))) 76 if ((strlen (filename) <= 4) || (0 != strcmp (filename + strlen (filename)
77 - 4, ".cfg")))
73 return GNUNET_OK; 78 return GNUNET_OK;
74 79
75 struct GNUNET_MESSENGER_Operation *op = load_operation(store, filename); 80 struct GNUNET_MESSENGER_Operation *op = load_operation (store, filename);
76 81
77 if ((op) && (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put( 82 if ((op) && (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put (
78 store->operations, 83 store->operations,
79 &(op->hash), op, 84 &(op->hash), op,
80 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))) 85 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)))
81 { 86 {
82 destroy_operation(op); 87 destroy_operation (op);
83 } 88 }
84 89
85 return GNUNET_OK; 90 return GNUNET_OK;
86} 91}
87 92
93
88void 94void
89load_operation_store (struct GNUNET_MESSENGER_OperationStore *store, 95load_operation_store (struct GNUNET_MESSENGER_OperationStore *store,
90 const char *directory) 96 const char *directory)
91{ 97{
92 GNUNET_assert ((store) && (directory)); 98 GNUNET_assert ((store) && (directory));
93 99
94 char* load_dir; 100 char *load_dir;
95 GNUNET_asprintf (&load_dir, "%s%s%c", directory, "operations", DIR_SEPARATOR); 101 GNUNET_asprintf (&load_dir, "%s%s%c", directory, "operations", DIR_SEPARATOR);
96 102
97 if (GNUNET_OK == GNUNET_DISK_directory_test (load_dir, GNUNET_YES)) 103 if (GNUNET_OK == GNUNET_DISK_directory_test (load_dir, GNUNET_YES))
98 GNUNET_DISK_directory_scan (load_dir, callback_scan_for_operations, store); 104 GNUNET_DISK_directory_scan (load_dir, callback_scan_for_operations, store);
99 105
100 GNUNET_free(load_dir); 106 GNUNET_free (load_dir);
101} 107}
102 108
109
103static int 110static int
104iterate_save_operations (void *cls, 111iterate_save_operations (void *cls,
105 const struct GNUNET_HashCode *key, 112 const struct GNUNET_HashCode *key,
@@ -109,65 +116,73 @@ iterate_save_operations (void *cls,
109 116
110 struct GNUNET_MESSENGER_Operation *op = value; 117 struct GNUNET_MESSENGER_Operation *op = value;
111 118
112 if (!op) 119 if (! op)
113 return GNUNET_YES; 120 return GNUNET_YES;
114 121
115 char *op_dir; 122 char *op_dir;
116 GNUNET_asprintf (&op_dir, "%s%s.cfg", save_dir, GNUNET_h2s(key)); 123 GNUNET_asprintf (&op_dir, "%s%s.cfg", save_dir, GNUNET_h2s (key));
117 save_operation(op, op_dir); 124 save_operation (op, op_dir);
118 125
119 GNUNET_free(op_dir); 126 GNUNET_free (op_dir);
120 return GNUNET_YES; 127 return GNUNET_YES;
121} 128}
122 129
130
123void 131void
124save_operation_store (const struct GNUNET_MESSENGER_OperationStore *store, 132save_operation_store (const struct GNUNET_MESSENGER_OperationStore *store,
125 const char *directory) 133 const char *directory)
126{ 134{
127 GNUNET_assert ((store) && (directory)); 135 GNUNET_assert ((store) && (directory));
128 136
129 char* save_dir; 137 char *save_dir;
130 GNUNET_asprintf (&save_dir, "%s%s%c", directory, "operations", DIR_SEPARATOR); 138 GNUNET_asprintf (&save_dir, "%s%s%c", directory, "operations", DIR_SEPARATOR);
131 139
132 if ((GNUNET_YES == GNUNET_DISK_directory_test (save_dir, GNUNET_NO)) || 140 if ((GNUNET_YES == GNUNET_DISK_directory_test (save_dir, GNUNET_NO)) ||
133 (GNUNET_OK == GNUNET_DISK_directory_create (save_dir))) 141 (GNUNET_OK == GNUNET_DISK_directory_create (save_dir)))
134 GNUNET_CONTAINER_multihashmap_iterate (store->operations, iterate_save_operations, save_dir); 142 GNUNET_CONTAINER_multihashmap_iterate (store->operations,
143 iterate_save_operations, save_dir);
135 144
136 GNUNET_free(save_dir); 145 GNUNET_free (save_dir);
137} 146}
138 147
148
139enum GNUNET_MESSENGER_OperationType 149enum GNUNET_MESSENGER_OperationType
140get_store_operation_type (const struct GNUNET_MESSENGER_OperationStore *store, 150get_store_operation_type (const struct GNUNET_MESSENGER_OperationStore *store,
141 const struct GNUNET_HashCode *hash) 151 const struct GNUNET_HashCode *hash)
142{ 152{
143 GNUNET_assert((store) && (hash)); 153 GNUNET_assert ((store) && (hash));
144 154
145 struct GNUNET_MESSENGER_Operation *op = GNUNET_CONTAINER_multihashmap_get(store->operations, hash); 155 struct GNUNET_MESSENGER_Operation *op = GNUNET_CONTAINER_multihashmap_get (
156 store->operations, hash);
146 157
147 if (!op) 158 if (! op)
148 return GNUNET_MESSENGER_OP_UNKNOWN; 159 return GNUNET_MESSENGER_OP_UNKNOWN;
149 160
150 return op->type; 161 return op->type;
151} 162}
152 163
164
153int 165int
154use_store_operation (struct GNUNET_MESSENGER_OperationStore *store, 166use_store_operation (struct GNUNET_MESSENGER_OperationStore *store,
155 const struct GNUNET_HashCode *hash, 167 const struct GNUNET_HashCode *hash,
156 enum GNUNET_MESSENGER_OperationType type, 168 enum GNUNET_MESSENGER_OperationType type,
157 struct GNUNET_TIME_Relative delay) 169 struct GNUNET_TIME_Relative delay)
158{ 170{
159 GNUNET_assert((store) && (hash)); 171 GNUNET_assert ((store) && (hash));
160 172
161 struct GNUNET_MESSENGER_Operation *op = GNUNET_CONTAINER_multihashmap_get(store->operations, hash); 173 struct GNUNET_MESSENGER_Operation *op = GNUNET_CONTAINER_multihashmap_get (
174 store->operations, hash);
162 175
163 if (op) 176 if (op)
164 goto use_op; 177 goto use_op;
165 178
166 op = create_operation(hash); 179 op = create_operation (hash);
167 180
168 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put(store->operations, hash, op, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)) 181 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put (store->operations, hash,
182 op,
183 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
169 { 184 {
170 destroy_operation(op); 185 destroy_operation (op);
171 186
172 return GNUNET_SYSERR; 187 return GNUNET_SYSERR;
173 } 188 }
@@ -177,29 +192,34 @@ use_op:
177 (type == GNUNET_MESSENGER_OP_DELETE)) 192 (type == GNUNET_MESSENGER_OP_DELETE))
178 stop_operation (op); 193 stop_operation (op);
179 194
180 return start_operation(op, type, store, delay); 195 return start_operation (op, type, store, delay);
181} 196}
182 197
198
183void 199void
184cancel_store_operation (struct GNUNET_MESSENGER_OperationStore *store, 200cancel_store_operation (struct GNUNET_MESSENGER_OperationStore *store,
185 const struct GNUNET_HashCode *hash) 201 const struct GNUNET_HashCode *hash)
186{ 202{
187 GNUNET_assert((store) && (hash)); 203 GNUNET_assert ((store) && (hash));
188 204
189 struct GNUNET_MESSENGER_Operation *op = GNUNET_CONTAINER_multihashmap_get(store->operations, hash); 205 struct GNUNET_MESSENGER_Operation *op = GNUNET_CONTAINER_multihashmap_get (
206 store->operations, hash);
190 207
191 if (!op) 208 if (! op)
192 return; 209 return;
193 210
194 stop_operation(op); 211 stop_operation (op);
195 212
196 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove(store->operations, hash, op)) 213 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (store->operations,
197 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Canceled operation could not be removed: %s\n", 214 hash, op))
198 GNUNET_h2s(hash)); 215 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
216 "Canceled operation could not be removed: %s\n",
217 GNUNET_h2s (hash));
199 218
200 destroy_operation(op); 219 destroy_operation (op);
201} 220}
202 221
222
203extern void 223extern void
204callback_room_deletion (struct GNUNET_MESSENGER_SrvRoom *room, 224callback_room_deletion (struct GNUNET_MESSENGER_SrvRoom *room,
205 const struct GNUNET_HashCode *hash); 225 const struct GNUNET_HashCode *hash);
@@ -213,10 +233,10 @@ callback_store_operation (struct GNUNET_MESSENGER_OperationStore *store,
213 enum GNUNET_MESSENGER_OperationType type, 233 enum GNUNET_MESSENGER_OperationType type,
214 const struct GNUNET_HashCode *hash) 234 const struct GNUNET_HashCode *hash)
215{ 235{
216 GNUNET_assert((store) && (hash)); 236 GNUNET_assert ((store) && (hash));
217 237
218 struct GNUNET_HashCode op_hash; 238 struct GNUNET_HashCode op_hash;
219 GNUNET_memcpy(&op_hash, hash, sizeof(op_hash)); 239 GNUNET_memcpy (&op_hash, hash, sizeof(op_hash));
220 cancel_store_operation (store, &op_hash); 240 cancel_store_operation (store, &op_hash);
221 241
222 struct GNUNET_MESSENGER_SrvRoom *room = store->room; 242 struct GNUNET_MESSENGER_SrvRoom *room = store->room;