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