GNUnet 0.22.2
gnunet-datastore.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2010, 2013 GNUnet e.V.
4
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.
9
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.
14
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/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
26#include "platform.h"
27#include <inttypes.h>
28#include "gnunet_util_lib.h"
30
32
34{
39
44
49
54
59
64
69};
71
72
76static const size_t MAGIC_LEN = 16;
77
81static const uint8_t MAGIC_BYTES[16] = "GNUNETDATASTORE1";
82
86static int dump;
87
91static int insert;
92
96static char *file_name;
97
102
106static int ret;
107
112
117
121static uint64_t record_count;
122
123
124static void
125do_shutdown (void *cls)
126{
127 if (NULL != qe)
129 if (NULL != datastore)
131 if (NULL != file_handle)
133}
134
135
139static void
140start_dump (void);
141
142
146static void
147start_insert (void);
148
149
153static void
154do_get (const uint64_t next_uid);
155
156
172static void
173get_cb (void *cls,
174 const struct GNUNET_HashCode *key,
175 size_t size,
176 const void *data,
178 uint32_t priority,
179 uint32_t anonymity,
180 uint32_t replication,
182 uint64_t uid)
183{
184 struct DataRecord dr;
185 ssize_t len;
186 qe = NULL;
187 if (NULL == key)
188 {
189 fprintf (stderr, _ ("Dumped %" PRIu64 " records\n"), record_count);
191 file_handle = NULL;
192 if (insert)
193 start_insert ();
194 else
195 {
196 ret = 0;
198 }
199 return;
200 }
201
202 dr.size = htonl ((uint32_t) size);
203 dr.type = htonl (type);
204 dr.priority = htonl (priority);
205 dr.anonymity = htonl (anonymity);
206 dr.replication = htonl (replication);
208 dr.key = *key;
209
210 len = GNUNET_DISK_file_write (file_handle, &dr, sizeof(dr));
211 if (sizeof(dr) != len)
212 {
213 fprintf (stderr,
214 _ ("Short write to file: %zd bytes expecting %zd\n"),
215 len,
216 sizeof(dr));
217 ret = 1;
219 return;
220 }
221
223 if (size != len)
224 {
225 fprintf (stderr,
226 _ ("Short write to file: %zd bytes expecting %zd\n"),
227 len,
228 size);
229 ret = 1;
231 return;
232 }
233
234 record_count++;
235 do_get (uid + 1);
236}
237
238
242static void
243do_get (const uint64_t next_uid)
244{
245 GNUNET_assert (NULL == qe);
247 next_uid,
248 false /* random */,
249 NULL /* key */,
251 0 /* queue_priority */,
252 1 /* max_queue_size */,
253 &get_cb,
254 NULL /* proc_cls */);
255 if (NULL == qe)
256 {
257 fprintf (stderr, _ ("Error queueing datastore GET operation\n"));
258 ret = 1;
260 }
261}
262
263
267static void
269{
270 record_count = 0;
271
272 if (NULL != file_name)
273 {
280 if (NULL == file_handle)
281 {
282 fprintf (stderr, _ ("Unable to open dump file: %s\n"), file_name);
283 ret = 1;
285 return;
286 }
287 }
288 else
289 {
291 }
293 do_get (0);
294}
295
296
310static void
311put_cb (void *cls,
312 int32_t success,
314 const char *msg)
315{
316 struct DataRecord dr;
317 ssize_t len;
318 qe = NULL;
319 if (GNUNET_SYSERR == success)
320 {
321 fprintf (stderr, _ ("Failed to store item: %s, aborting\n"), msg);
322 ret = 1;
324 return;
325 }
326
327
328 len = GNUNET_DISK_file_read (file_handle, &dr, sizeof(dr));
329 if (0 == len)
330 {
331 fprintf (stderr, _ ("Inserted %" PRIu64 " records\n"), record_count);
332 ret = 0;
334 return;
335 }
336 else if (sizeof(dr) != len)
337 {
338 fprintf (stderr,
339 _ ("Short read from file: %zd bytes expecting %zd\n"),
340 len,
341 sizeof(dr));
342 ret = 1;
344 return;
345 }
346
347 {
348 const size_t size = ntohl (dr.size);
349 uint8_t data[size];
351 if (size != len)
352 {
353 fprintf (stderr,
354 _ ("Short read from file: %zd bytes expecting %zd\n"),
355 len,
356 size);
357 ret = 1;
359 return;
360 }
361
362 record_count++;
364 0,
365 &dr.key,
366 size,
367 data,
368 ntohl (dr.type),
369 ntohl (dr.priority),
370 ntohl (dr.anonymity),
371 ntohl (dr.replication),
373 0,
374 1,
375 &put_cb,
376 NULL);
377 if (NULL == qe)
378 {
379 fprintf (stderr, _ ("Error queueing datastore PUT operation\n"));
380 ret = 1;
382 }
383 }
384}
385
386
390static void
392{
393 uint8_t buf[MAGIC_LEN];
394 ssize_t len;
395 record_count = 0;
396
397 if (NULL != file_name)
398 {
402 if (NULL == file_handle)
403 {
404 fprintf (stderr, _ ("Unable to open dump file: %s\n"), file_name);
405 ret = 1;
407 return;
408 }
409 }
410 else
411 {
413 }
414
416 if ((len != MAGIC_LEN) || (0 != memcmp (buf, MAGIC_BYTES, MAGIC_LEN)))
417 {
418 fprintf (stderr, _ ("Input file is not of a supported format\n"));
419 return;
420 }
422}
423
424
433static void
434run (void *cls,
435 char *const *args,
436 const char *cfgfile,
437 const struct GNUNET_CONFIGURATION_Handle *cfg)
438{
441 if (NULL == datastore)
442 {
443 fprintf (stderr, _ ("Failed connecting to the datastore.\n"));
444 ret = 1;
446 return;
447 }
448 if (dump)
449 start_dump ();
450 else if (insert)
451 start_insert ();
452 else
453 {
454 fprintf (stderr,
455 _ ("Please choose at least one operation: %s, %s\n"),
456 "dump",
457 "insert");
458 ret = 1;
460 }
461}
462
463
471int
472main (int argc, char *const *argv)
473{
476 "dump",
478 "Dump all records from the datastore"),
479 &dump),
481 "insert",
483 "Insert records into the datastore"),
484 &insert),
486 "file",
487 "FILENAME",
488 gettext_noop ("File to dump or insert"),
489 &file_name),
491
492 if (GNUNET_OK !=
494 argc,
495 argv,
496 "gnunet-datastore",
497 gettext_noop ("Manipulate GNUnet datastore"),
498 options,
499 &run,
500 NULL))
501 ret = 1;
502 return ret;
503}
504
505
506/* end of gnunet-datastore.c */
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
#define gettext_noop(String)
Definition: gettext.h:74
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:108
static void do_get(const uint64_t next_uid)
Perform next GET operation.
static struct GNUNET_DISK_FileHandle * file_handle
Dump file handle.
static uint64_t record_count
Record count.
static const uint8_t MAGIC_BYTES[16]
Magic header bytes.
static int dump
Dump the database.
static struct GNUNET_DATASTORE_QueueEntry * qe
Current operation.
static void get_cb(void *cls, const struct GNUNET_HashCode *key, size_t size, const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority, uint32_t anonymity, uint32_t replication, struct GNUNET_TIME_Absolute expiration, uint64_t uid)
Process a datum that was stored in the datastore.
static int ret
Global return value.
static void do_shutdown(void *cls)
static void put_cb(void *cls, int32_t success, struct GNUNET_TIME_Absolute min_expiration, const char *msg)
Continuation called to notify client about result of the operation.
static void start_insert(void)
Begin inserting into the database.
static char * file_name
Dump file name.
static struct GNUNET_DATASTORE_Handle * datastore
Handle for datastore.
static void run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
Main function that will be run by the scheduler.
static int insert
Insert into the database.
static GNUNET_NETWORK_STRUCT_END const size_t MAGIC_LEN
Length of our magic header.
int main(int argc, char *const *argv)
The main function to manipulate datastores.
static void start_dump(void)
Begin dumping the database.
static unsigned int replication
Desired replication level.
static char * data
The data to insert into the dht.
struct GNUNET_HashCode key
The key used in the DHT.
static struct GNUNET_TIME_Relative expiration
User supplied expiration value.
static unsigned int anonymity
static uint32_t type
Type string converted to DNS type value.
static struct GNUNET_TIME_Absolute min_expiration
Minimum time that content should have to not be discarded instantly (time stamp of any content that w...
GNUNET_BLOCK_Type
WARNING: This header is generated! In order to add DHT block types, you must register them in GANA,...
@ GNUNET_BLOCK_TYPE_ANY
Identifier for any block.
void GNUNET_DATASTORE_disconnect(struct GNUNET_DATASTORE_Handle *h, int drop)
Disconnect from the datastore service (and free associated resources).
void GNUNET_DATASTORE_cancel(struct GNUNET_DATASTORE_QueueEntry *qe)
Cancel a datastore operation.
struct GNUNET_DATASTORE_QueueEntry * GNUNET_DATASTORE_put(struct GNUNET_DATASTORE_Handle *h, uint32_t rid, const struct GNUNET_HashCode *key, size_t size, const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority, uint32_t anonymity, uint32_t replication, struct GNUNET_TIME_Absolute expiration, unsigned int queue_priority, unsigned int max_queue_size, GNUNET_DATASTORE_ContinuationWithStatus cont, void *cont_cls)
Store an item in the datastore.
struct GNUNET_DATASTORE_Handle * GNUNET_DATASTORE_connect(const struct GNUNET_CONFIGURATION_Handle *cfg)
Connect to the datastore service.
struct GNUNET_DATASTORE_QueueEntry * GNUNET_DATASTORE_get_key(struct GNUNET_DATASTORE_Handle *h, uint64_t next_uid, bool random, const struct GNUNET_HashCode *key, enum GNUNET_BLOCK_Type type, unsigned int queue_priority, unsigned int max_queue_size, GNUNET_DATASTORE_DatumProcessor proc, void *proc_cls)
Get a result for a particular key from the datastore.
struct GNUNET_DISK_FileHandle * GNUNET_DISK_file_open(const char *fn, enum GNUNET_DISK_OpenFlags flags, enum GNUNET_DISK_AccessPermissions perm)
Open a file.
Definition: disk.c:1234
ssize_t GNUNET_DISK_file_write(const struct GNUNET_DISK_FileHandle *h, const void *buffer, size_t n)
Write a buffer to a file.
Definition: disk.c:697
enum GNUNET_GenericReturnValue GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1308
struct GNUNET_DISK_FileHandle * GNUNET_DISK_get_handle_from_int_fd(int fno)
Get a handle from a native integer FD.
Definition: disk.c:1330
ssize_t GNUNET_DISK_file_read(const struct GNUNET_DISK_FileHandle *h, void *result, size_t len)
Read the contents of a binary file into a buffer.
Definition: disk.c:660
@ GNUNET_DISK_OPEN_READ
Open the file for reading.
@ GNUNET_DISK_OPEN_WRITE
Open the file for writing.
@ GNUNET_DISK_OPEN_TRUNCATE
Truncate file if it exists.
@ GNUNET_DISK_OPEN_CREATE
Create file if it doesn't exist.
@ GNUNET_DISK_PERM_USER_READ
Owner can read.
@ GNUNET_DISK_PERM_NONE
Nobody is allowed to do anything to the file.
@ GNUNET_DISK_PERM_USER_WRITE
Owner can write.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_filename(char shortName, const char *name, const char *argumentHelp, const char *description, char **str)
Allow user to specify a filename (automatically path expanded).
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_flag(char shortName, const char *name, const char *description, int *val)
Allow user to specify a flag (which internally means setting an integer to 1/GNUNET_YES/GNUNET_OK.
#define GNUNET_NETWORK_STRUCT_BEGIN
Define as empty, GNUNET_PACKED should suffice, but this won't work on W32.
#define GNUNET_NETWORK_STRUCT_END
Define as empty, GNUNET_PACKED should suffice, but this won't work on W32;.
#define GNUNET_PACKED
gcc-ism to get packed structs.
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_NO
@ GNUNET_SYSERR
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
const struct GNUNET_OS_ProjectData * GNUNET_OS_project_data_gnunet(void)
Return default project data used by 'libgnunetutil' for GNUnet.
enum GNUNET_GenericReturnValue GNUNET_PROGRAM_run(const struct GNUNET_OS_ProjectData *pd, int argc, char *const *argv, const char *binaryName, const char *binaryHelp, const struct GNUNET_GETOPT_CommandLineOption *options, GNUNET_PROGRAM_Main task, void *task_cls)
Run a standard GNUnet command startup sequence (initialize loggers and configuration,...
Definition: program.c:407
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:567
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run on shutdown, that is when a CTRL-C signal is received,...
Definition: scheduler.c:1339
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_ntoh(struct GNUNET_TIME_AbsoluteNBO a)
Convert absolute time from network byte order.
Definition: time.c:741
#define GNUNET_TIME_UNIT_ZERO_ABS
Absolute time zero.
struct GNUNET_TIME_AbsoluteNBO GNUNET_TIME_absolute_hton(struct GNUNET_TIME_Absolute a)
Convert absolute time to network byte order.
Definition: time.c:640
static unsigned int size
Size of the "table".
Definition: peer.c:68
#define _(String)
GNU gettext support macro.
Definition: platform.h:179
uint32_t replication
Desired replication level (NBO).
struct GNUNET_TIME_AbsoluteNBO expiration
Expiration time (NBO).
uint32_t type
Type of the item (NBO) (actually an enum GNUNET_BLOCK_Type)
uint32_t anonymity
Desired anonymity level (NBO).
uint32_t size
Number of bytes in the item (NBO).
uint32_t priority
Priority of the item (NBO).
struct GNUNET_HashCode key
Key under which the item can be found.
Handle to the datastore service.
Entry in our priority queue.
unsigned int priority
Priority in the queue.
Handle used to access files (and pipes).
Definition of a command line option.
A 512-bit hashcode.
Time for absolute time used by GNUnet, in microseconds and in network byte order.
Time for absolute times used by GNUnet, in microseconds.