aboutsummaryrefslogtreecommitdiff
path: root/src/datastore/gnunet-datastore.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/datastore/gnunet-datastore.c')
-rw-r--r--src/datastore/gnunet-datastore.c442
1 files changed, 221 insertions, 221 deletions
diff --git a/src/datastore/gnunet-datastore.c b/src/datastore/gnunet-datastore.c
index 4cf318777..46a0b2358 100644
--- a/src/datastore/gnunet-datastore.c
+++ b/src/datastore/gnunet-datastore.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 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/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file datastore/gnunet-datastore.c 22 * @file datastore/gnunet-datastore.c
@@ -30,8 +30,7 @@
30 30
31GNUNET_NETWORK_STRUCT_BEGIN 31GNUNET_NETWORK_STRUCT_BEGIN
32 32
33struct DataRecord 33struct DataRecord {
34{
35 /** 34 /**
36 * Number of bytes in the item (NBO). 35 * Number of bytes in the item (NBO).
37 */ 36 */
@@ -122,14 +121,14 @@ static uint64_t record_count;
122 121
123 122
124static void 123static void
125do_shutdown (void *cls) 124do_shutdown(void *cls)
126{ 125{
127 if (NULL != qe) 126 if (NULL != qe)
128 GNUNET_DATASTORE_cancel (qe); 127 GNUNET_DATASTORE_cancel(qe);
129 if (NULL != datastore) 128 if (NULL != datastore)
130 GNUNET_DATASTORE_disconnect (datastore, GNUNET_NO); 129 GNUNET_DATASTORE_disconnect(datastore, GNUNET_NO);
131 if (NULL != file_handle) 130 if (NULL != file_handle)
132 GNUNET_DISK_file_close (file_handle); 131 GNUNET_DISK_file_close(file_handle);
133} 132}
134 133
135 134
@@ -137,21 +136,21 @@ do_shutdown (void *cls)
137 * Begin dumping the database. 136 * Begin dumping the database.
138 */ 137 */
139static void 138static void
140start_dump (void); 139start_dump(void);
141 140
142 141
143/** 142/**
144 * Begin inserting into the database. 143 * Begin inserting into the database.
145 */ 144 */
146static void 145static void
147start_insert (void); 146start_insert(void);
148 147
149 148
150/** 149/**
151 * Perform next GET operation. 150 * Perform next GET operation.
152 */ 151 */
153static void 152static void
154do_get (const uint64_t next_uid); 153do_get(const uint64_t next_uid);
155 154
156 155
157/** 156/**
@@ -170,69 +169,69 @@ do_get (const uint64_t next_uid);
170 * maybe 0 if no unique identifier is available 169 * maybe 0 if no unique identifier is available
171 */ 170 */
172static void 171static void
173get_cb (void *cls, 172get_cb(void *cls,
174 const struct GNUNET_HashCode *key, 173 const struct GNUNET_HashCode *key,
175 size_t size, 174 size_t size,
176 const void *data, 175 const void *data,
177 enum GNUNET_BLOCK_Type type, 176 enum GNUNET_BLOCK_Type type,
178 uint32_t priority, 177 uint32_t priority,
179 uint32_t anonymity, 178 uint32_t anonymity,
180 uint32_t replication, 179 uint32_t replication,
181 struct GNUNET_TIME_Absolute expiration, 180 struct GNUNET_TIME_Absolute expiration,
182 uint64_t uid) 181 uint64_t uid)
183{ 182{
184 qe = NULL; 183 qe = NULL;
185 if (NULL == key) 184 if (NULL == key)
186 {
187 fprintf (stderr, _ ("Dumped %" PRIu64 " records\n"), record_count);
188 GNUNET_DISK_file_close (file_handle);
189 file_handle = NULL;
190 if (insert)
191 start_insert ();
192 else
193 { 185 {
194 ret = 0; 186 fprintf(stderr, _("Dumped %" PRIu64 " records\n"), record_count);
195 GNUNET_SCHEDULER_shutdown (); 187 GNUNET_DISK_file_close(file_handle);
188 file_handle = NULL;
189 if (insert)
190 start_insert();
191 else
192 {
193 ret = 0;
194 GNUNET_SCHEDULER_shutdown();
195 }
196 return;
196 } 197 }
197 return;
198 }
199 198
200 struct DataRecord dr; 199 struct DataRecord dr;
201 dr.size = htonl ((uint32_t) size); 200 dr.size = htonl((uint32_t)size);
202 dr.type = htonl (type); 201 dr.type = htonl(type);
203 dr.priority = htonl (priority); 202 dr.priority = htonl(priority);
204 dr.anonymity = htonl (anonymity); 203 dr.anonymity = htonl(anonymity);
205 dr.replication = htonl (replication); 204 dr.replication = htonl(replication);
206 dr.expiration = GNUNET_TIME_absolute_hton (expiration); 205 dr.expiration = GNUNET_TIME_absolute_hton(expiration);
207 dr.key = *key; 206 dr.key = *key;
208 207
209 ssize_t len; 208 ssize_t len;
210 len = GNUNET_DISK_file_write (file_handle, &dr, sizeof (dr)); 209 len = GNUNET_DISK_file_write(file_handle, &dr, sizeof(dr));
211 if (sizeof (dr) != len) 210 if (sizeof(dr) != len)
212 { 211 {
213 fprintf (stderr, 212 fprintf(stderr,
214 _ ("Short write to file: %zd bytes expecting %zd\n"), 213 _("Short write to file: %zd bytes expecting %zd\n"),
215 len, 214 len,
216 sizeof (dr)); 215 sizeof(dr));
217 ret = 1; 216 ret = 1;
218 GNUNET_SCHEDULER_shutdown (); 217 GNUNET_SCHEDULER_shutdown();
219 return; 218 return;
220 } 219 }
221 220
222 len = GNUNET_DISK_file_write (file_handle, data, size); 221 len = GNUNET_DISK_file_write(file_handle, data, size);
223 if (size != len) 222 if (size != len)
224 { 223 {
225 fprintf (stderr, 224 fprintf(stderr,
226 _ ("Short write to file: %zd bytes expecting %zd\n"), 225 _("Short write to file: %zd bytes expecting %zd\n"),
227 len, 226 len,
228 size); 227 size);
229 ret = 1; 228 ret = 1;
230 GNUNET_SCHEDULER_shutdown (); 229 GNUNET_SCHEDULER_shutdown();
231 return; 230 return;
232 } 231 }
233 232
234 record_count++; 233 record_count++;
235 do_get (uid + 1); 234 do_get(uid + 1);
236} 235}
237 236
238 237
@@ -240,24 +239,24 @@ get_cb (void *cls,
240 * Perform next GET operation. 239 * Perform next GET operation.
241 */ 240 */
242static void 241static void
243do_get (const uint64_t next_uid) 242do_get(const uint64_t next_uid)
244{ 243{
245 GNUNET_assert (NULL == qe); 244 GNUNET_assert(NULL == qe);
246 qe = GNUNET_DATASTORE_get_key (datastore, 245 qe = GNUNET_DATASTORE_get_key(datastore,
247 next_uid, 246 next_uid,
248 false /* random */, 247 false /* random */,
249 NULL /* key */, 248 NULL /* key */,
250 GNUNET_BLOCK_TYPE_ANY, 249 GNUNET_BLOCK_TYPE_ANY,
251 0 /* queue_priority */, 250 0 /* queue_priority */,
252 1 /* max_queue_size */, 251 1 /* max_queue_size */,
253 &get_cb, 252 &get_cb,
254 NULL /* proc_cls */); 253 NULL /* proc_cls */);
255 if (NULL == qe) 254 if (NULL == qe)
256 { 255 {
257 fprintf (stderr, _ ("Error queueing datastore GET operation\n")); 256 fprintf(stderr, _("Error queueing datastore GET operation\n"));
258 ret = 1; 257 ret = 1;
259 GNUNET_SCHEDULER_shutdown (); 258 GNUNET_SCHEDULER_shutdown();
260 } 259 }
261} 260}
262 261
263 262
@@ -265,32 +264,32 @@ do_get (const uint64_t next_uid)
265 * Begin dumping the database. 264 * Begin dumping the database.
266 */ 265 */
267static void 266static void
268start_dump () 267start_dump()
269{ 268{
270 record_count = 0; 269 record_count = 0;
271 270
272 if (NULL != file_name) 271 if (NULL != file_name)
273 {
274 file_handle = GNUNET_DISK_file_open (file_name,
275 GNUNET_DISK_OPEN_WRITE |
276 GNUNET_DISK_OPEN_TRUNCATE |
277 GNUNET_DISK_OPEN_CREATE,
278 GNUNET_DISK_PERM_USER_READ |
279 GNUNET_DISK_PERM_USER_WRITE);
280 if (NULL == file_handle)
281 { 272 {
282 fprintf (stderr, _ ("Unable to open dump file: %s\n"), file_name); 273 file_handle = GNUNET_DISK_file_open(file_name,
283 ret = 1; 274 GNUNET_DISK_OPEN_WRITE |
284 GNUNET_SCHEDULER_shutdown (); 275 GNUNET_DISK_OPEN_TRUNCATE |
285 return; 276 GNUNET_DISK_OPEN_CREATE,
277 GNUNET_DISK_PERM_USER_READ |
278 GNUNET_DISK_PERM_USER_WRITE);
279 if (NULL == file_handle)
280 {
281 fprintf(stderr, _("Unable to open dump file: %s\n"), file_name);
282 ret = 1;
283 GNUNET_SCHEDULER_shutdown();
284 return;
285 }
286 } 286 }
287 }
288 else 287 else
289 { 288 {
290 file_handle = GNUNET_DISK_get_handle_from_int_fd (STDOUT_FILENO); 289 file_handle = GNUNET_DISK_get_handle_from_int_fd(STDOUT_FILENO);
291 } 290 }
292 GNUNET_DISK_file_write (file_handle, MAGIC_BYTES, MAGIC_LEN); 291 GNUNET_DISK_file_write(file_handle, MAGIC_BYTES, MAGIC_LEN);
293 do_get (0); 292 do_get(0);
294} 293}
295 294
296 295
@@ -308,77 +307,77 @@ start_dump ()
308 * @param msg NULL on success, otherwise an error message 307 * @param msg NULL on success, otherwise an error message
309 */ 308 */
310static void 309static void
311put_cb (void *cls, 310put_cb(void *cls,
312 int32_t success, 311 int32_t success,
313 struct GNUNET_TIME_Absolute min_expiration, 312 struct GNUNET_TIME_Absolute min_expiration,
314 const char *msg) 313 const char *msg)
315{ 314{
316 qe = NULL; 315 qe = NULL;
317 if (GNUNET_SYSERR == success) 316 if (GNUNET_SYSERR == success)
318 { 317 {
319 fprintf (stderr, _ ("Failed to store item: %s, aborting\n"), msg); 318 fprintf(stderr, _("Failed to store item: %s, aborting\n"), msg);
320 ret = 1; 319 ret = 1;
321 GNUNET_SCHEDULER_shutdown (); 320 GNUNET_SCHEDULER_shutdown();
322 return; 321 return;
323 } 322 }
324 323
325 struct DataRecord dr; 324 struct DataRecord dr;
326 ssize_t len; 325 ssize_t len;
327 326
328 len = GNUNET_DISK_file_read (file_handle, &dr, sizeof (dr)); 327 len = GNUNET_DISK_file_read(file_handle, &dr, sizeof(dr));
329 if (0 == len) 328 if (0 == len)
330 { 329 {
331 fprintf (stderr, _ ("Inserted %" PRIu64 " records\n"), record_count); 330 fprintf(stderr, _("Inserted %" PRIu64 " records\n"), record_count);
332 ret = 0; 331 ret = 0;
333 GNUNET_SCHEDULER_shutdown (); 332 GNUNET_SCHEDULER_shutdown();
334 return; 333 return;
335 } 334 }
336 else if (sizeof (dr) != len) 335 else if (sizeof(dr) != len)
337 { 336 {
338 fprintf (stderr, 337 fprintf(stderr,
339 _ ("Short read from file: %zd bytes expecting %zd\n"), 338 _("Short read from file: %zd bytes expecting %zd\n"),
340 len, 339 len,
341 sizeof (dr)); 340 sizeof(dr));
342 ret = 1; 341 ret = 1;
343 GNUNET_SCHEDULER_shutdown (); 342 GNUNET_SCHEDULER_shutdown();
344 return; 343 return;
345 } 344 }
346 345
347 const size_t size = ntohl (dr.size); 346 const size_t size = ntohl(dr.size);
348 uint8_t data[size]; 347 uint8_t data[size];
349 len = GNUNET_DISK_file_read (file_handle, data, size); 348 len = GNUNET_DISK_file_read(file_handle, data, size);
350 if (size != len) 349 if (size != len)
351 { 350 {
352 fprintf (stderr, 351 fprintf(stderr,
353 _ ("Short read from file: %zd bytes expecting %zd\n"), 352 _("Short read from file: %zd bytes expecting %zd\n"),
354 len, 353 len,
355 size); 354 size);
356 ret = 1; 355 ret = 1;
357 GNUNET_SCHEDULER_shutdown (); 356 GNUNET_SCHEDULER_shutdown();
358 return; 357 return;
359 } 358 }
360 359
361 record_count++; 360 record_count++;
362 qe = GNUNET_DATASTORE_put (datastore, 361 qe = GNUNET_DATASTORE_put(datastore,
363 0, 362 0,
364 &dr.key, 363 &dr.key,
365 size, 364 size,
366 data, 365 data,
367 ntohl (dr.type), 366 ntohl(dr.type),
368 ntohl (dr.priority), 367 ntohl(dr.priority),
369 ntohl (dr.anonymity), 368 ntohl(dr.anonymity),
370 ntohl (dr.replication), 369 ntohl(dr.replication),
371 GNUNET_TIME_absolute_ntoh (dr.expiration), 370 GNUNET_TIME_absolute_ntoh(dr.expiration),
372 0, 371 0,
373 1, 372 1,
374 &put_cb, 373 &put_cb,
375 NULL); 374 NULL);
376 if (NULL == qe) 375 if (NULL == qe)
377 { 376 {
378 fprintf (stderr, _ ("Error queueing datastore PUT operation\n")); 377 fprintf(stderr, _("Error queueing datastore PUT operation\n"));
379 ret = 1; 378 ret = 1;
380 GNUNET_SCHEDULER_shutdown (); 379 GNUNET_SCHEDULER_shutdown();
381 } 380 }
382} 381}
383 382
384 383
@@ -386,38 +385,38 @@ put_cb (void *cls,
386 * Begin inserting into the database. 385 * Begin inserting into the database.
387 */ 386 */
388static void 387static void
389start_insert () 388start_insert()
390{ 389{
391 record_count = 0; 390 record_count = 0;
392 391
393 if (NULL != file_name) 392 if (NULL != file_name)
394 {
395 file_handle = GNUNET_DISK_file_open (file_name,
396 GNUNET_DISK_OPEN_READ,
397 GNUNET_DISK_PERM_NONE);
398 if (NULL == file_handle)
399 { 393 {
400 fprintf (stderr, _ ("Unable to open dump file: %s\n"), file_name); 394 file_handle = GNUNET_DISK_file_open(file_name,
401 ret = 1; 395 GNUNET_DISK_OPEN_READ,
402 GNUNET_SCHEDULER_shutdown (); 396 GNUNET_DISK_PERM_NONE);
403 return; 397 if (NULL == file_handle)
398 {
399 fprintf(stderr, _("Unable to open dump file: %s\n"), file_name);
400 ret = 1;
401 GNUNET_SCHEDULER_shutdown();
402 return;
403 }
404 } 404 }
405 }
406 else 405 else
407 { 406 {
408 file_handle = GNUNET_DISK_get_handle_from_int_fd (STDIN_FILENO); 407 file_handle = GNUNET_DISK_get_handle_from_int_fd(STDIN_FILENO);
409 } 408 }
410 409
411 uint8_t buf[MAGIC_LEN]; 410 uint8_t buf[MAGIC_LEN];
412 ssize_t len; 411 ssize_t len;
413 412
414 len = GNUNET_DISK_file_read (file_handle, buf, MAGIC_LEN); 413 len = GNUNET_DISK_file_read(file_handle, buf, MAGIC_LEN);
415 if (len != MAGIC_LEN || 0 != memcmp (buf, MAGIC_BYTES, MAGIC_LEN)) 414 if (len != MAGIC_LEN || 0 != memcmp(buf, MAGIC_BYTES, MAGIC_LEN))
416 { 415 {
417 fprintf (stderr, _ ("Input file is not of a supported format\n")); 416 fprintf(stderr, _("Input file is not of a supported format\n"));
418 return; 417 return;
419 } 418 }
420 put_cb (NULL, GNUNET_YES, GNUNET_TIME_UNIT_ZERO_ABS, NULL); 419 put_cb(NULL, GNUNET_YES, GNUNET_TIME_UNIT_ZERO_ABS, NULL);
421} 420}
422 421
423 422
@@ -430,33 +429,33 @@ start_insert ()
430 * @param cfg configuration 429 * @param cfg configuration
431 */ 430 */
432static void 431static void
433run (void *cls, 432run(void *cls,
434 char *const *args, 433 char *const *args,
435 const char *cfgfile, 434 const char *cfgfile,
436 const struct GNUNET_CONFIGURATION_Handle *cfg) 435 const struct GNUNET_CONFIGURATION_Handle *cfg)
437{ 436{
438 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL); 437 GNUNET_SCHEDULER_add_shutdown(&do_shutdown, NULL);
439 datastore = GNUNET_DATASTORE_connect (cfg); 438 datastore = GNUNET_DATASTORE_connect(cfg);
440 if (NULL == datastore) 439 if (NULL == datastore)
441 { 440 {
442 fprintf (stderr, _ ("Failed connecting to the datastore.\n")); 441 fprintf(stderr, _("Failed connecting to the datastore.\n"));
443 ret = 1; 442 ret = 1;
444 GNUNET_SCHEDULER_shutdown (); 443 GNUNET_SCHEDULER_shutdown();
445 return; 444 return;
446 } 445 }
447 if (dump) 446 if (dump)
448 start_dump (); 447 start_dump();
449 else if (insert) 448 else if (insert)
450 start_insert (); 449 start_insert();
451 else 450 else
452 { 451 {
453 fprintf (stderr, 452 fprintf(stderr,
454 _ ("Please choose at least one operation: %s, %s\n"), 453 _("Please choose at least one operation: %s, %s\n"),
455 "dump", 454 "dump",
456 "insert"); 455 "insert");
457 ret = 1; 456 ret = 1;
458 GNUNET_SCHEDULER_shutdown (); 457 GNUNET_SCHEDULER_shutdown();
459 } 458 }
460} 459}
461 460
462 461
@@ -468,38 +467,39 @@ run (void *cls,
468 * @return 0 ok, 1 on error 467 * @return 0 ok, 1 on error
469 */ 468 */
470int 469int
471main (int argc, char *const *argv) 470main(int argc, char *const *argv)
472{ 471{
473 struct GNUNET_GETOPT_CommandLineOption options[] = 472 struct GNUNET_GETOPT_CommandLineOption options[] =
474 {GNUNET_GETOPT_option_flag ('d', 473 { GNUNET_GETOPT_option_flag('d',
475 "dump", 474 "dump",
476 gettext_noop ( 475 gettext_noop(
477 "Dump all records from the datastore"), 476 "Dump all records from the datastore"),
478 &dump), 477 &dump),
479 GNUNET_GETOPT_option_flag ('i', 478 GNUNET_GETOPT_option_flag('i',
480 "insert", 479 "insert",
481 gettext_noop ( 480 gettext_noop(
482 "Insert records into the datastore"), 481 "Insert records into the datastore"),
483 &insert), 482 &insert),
484 GNUNET_GETOPT_option_filename ('f', 483 GNUNET_GETOPT_option_filename('f',
485 "file", 484 "file",
486 "FILENAME", 485 "FILENAME",
487 gettext_noop ("File to dump or insert"), 486 gettext_noop("File to dump or insert"),
488 &file_name), 487 &file_name),
489 GNUNET_GETOPT_OPTION_END}; 488 GNUNET_GETOPT_OPTION_END };
490 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) 489
490 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv))
491 return 2; 491 return 2;
492 492
493 if (GNUNET_OK != 493 if (GNUNET_OK !=
494 GNUNET_PROGRAM_run (argc, 494 GNUNET_PROGRAM_run(argc,
495 argv, 495 argv,
496 "gnunet-datastore", 496 "gnunet-datastore",
497 gettext_noop ("Manipulate GNUnet datastore"), 497 gettext_noop("Manipulate GNUnet datastore"),
498 options, 498 options,
499 &run, 499 &run,
500 NULL)) 500 NULL))
501 ret = 1; 501 ret = 1;
502 GNUNET_free ((void *) argv); 502 GNUNET_free((void *)argv);
503 return ret; 503 return ret;
504} 504}
505 505