summaryrefslogtreecommitdiff
path: root/src/datastore/gnunet-datastore.c
blob: 5901cce5482ab046cc65bcff323ba91a8c2ccb9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/*
     This file is part of GNUnet.
     Copyright (C) 2010, 2013 GNUnet e.V.

     GNUnet is free software: you can redistribute it and/or modify it
     under the terms of the GNU Affero General Public License as published
     by the Free Software Foundation, either version 3 of the License,
     or (at your option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     Affero General Public License for more details.

     You should have received a copy of the GNU Affero General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.

     SPDX-License-Identifier: AGPL3.0-or-later
 */

/**
 * @file datastore/gnunet-datastore.c
 * @brief tool to manipulate datastores
 * @author Christian Grothoff
 */
#include <inttypes.h>
#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_datastore_service.h"

GNUNET_NETWORK_STRUCT_BEGIN

struct DataRecord
{
  /**
   * Number of bytes in the item (NBO).
   */
  uint32_t size GNUNET_PACKED;

  /**
   * Type of the item (NBO) (actually an enum GNUNET_BLOCK_Type)
   */
  uint32_t type GNUNET_PACKED;

  /**
   * Priority of the item (NBO).
   */
  uint32_t priority GNUNET_PACKED;

  /**
   * Desired anonymity level (NBO).
   */
  uint32_t anonymity GNUNET_PACKED;

  /**
   * Desired replication level (NBO).
   */
  uint32_t replication GNUNET_PACKED;

  /**
   * Expiration time (NBO).
   */
  struct GNUNET_TIME_AbsoluteNBO expiration;

  /**
   * Key under which the item can be found.
   */
  struct GNUNET_HashCode key;
};
GNUNET_NETWORK_STRUCT_END


/**
 * Length of our magic header.
 */
static const size_t MAGIC_LEN = 16;

/**
 * Magic header bytes.
 */
static const uint8_t MAGIC_BYTES[16] = "GNUNETDATASTORE1";

/**
 * Dump the database.
 */
static int dump;

/**
 * Insert into the database.
 */
static int insert;

/**
 * Dump file name.
 */
static char *file_name;

/**
 * Dump file handle.
 */
static struct GNUNET_DISK_FileHandle *file_handle;

/**
 * Global return value.
 */
static int ret;

/**
 * Handle for datastore.
 */
static struct GNUNET_DATASTORE_Handle *datastore;

/**
 * Current operation.
 */
static struct GNUNET_DATASTORE_QueueEntry *qe;

/**
 * Record count.
 */
static uint64_t record_count;


static void
do_shutdown (void *cls)
{
  if (NULL != qe)
    GNUNET_DATASTORE_cancel (qe);
  if (NULL != datastore)
    GNUNET_DATASTORE_disconnect (datastore, GNUNET_NO);
  if (NULL != file_handle)
    GNUNET_DISK_file_close (file_handle);
}


/**
 * Begin dumping the database.
 */
static void
start_dump (void);


/**
 * Begin inserting into the database.
 */
static void
start_insert (void);


/**
 * Perform next GET operation.
 */
static void
do_get (const uint64_t next_uid);


/**
 * Process a datum that was stored in the datastore.
 *
 * @param cls closure
 * @param key key for the content
 * @param size number of bytes in data
 * @param data content stored
 * @param type type of the content
 * @param priority priority of the content
 * @param anonymity anonymity-level for the content
 * @param replication replication-level for the content
 * @param expiration expiration time for the content
 * @param uid unique identifier for the datum;
 *        maybe 0 if no unique identifier is available
 */
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)
{
  qe = NULL;
  if (NULL == key)
  {
    fprintf (stderr, _ ("Dumped %" PRIu64 " records\n"), record_count);
    GNUNET_DISK_file_close (file_handle);
    file_handle = NULL;
    if (insert)
      start_insert ();
    else
    {
      ret = 0;
      GNUNET_SCHEDULER_shutdown ();
    }
    return;
  }

  struct DataRecord dr;
  dr.size = htonl ((uint32_t) size);
  dr.type = htonl (type);
  dr.priority = htonl (priority);
  dr.anonymity = htonl (anonymity);
  dr.replication = htonl (replication);
  dr.expiration = GNUNET_TIME_absolute_hton (expiration);
  dr.key = *key;

  ssize_t len;
  len = GNUNET_DISK_file_write (file_handle, &dr, sizeof(dr));
  if (sizeof(dr) != len)
  {
    fprintf (stderr,
             _ ("Short write to file: %zd bytes expecting %zd\n"),
             len,
             sizeof(dr));
    ret = 1;
    GNUNET_SCHEDULER_shutdown ();
    return;
  }

  len = GNUNET_DISK_file_write (file_handle, data, size);
  if (size != len)
  {
    fprintf (stderr,
             _ ("Short write to file: %zd bytes expecting %zd\n"),
             len,
             size);
    ret = 1;
    GNUNET_SCHEDULER_shutdown ();
    return;
  }

  record_count++;
  do_get (uid + 1);
}


/**
 * Perform next GET operation.
 */
static void
do_get (const uint64_t next_uid)
{
  GNUNET_assert (NULL == qe);
  qe = GNUNET_DATASTORE_get_key (datastore,
                                 next_uid,
                                 false /* random */,
                                 NULL /* key */,
                                 GNUNET_BLOCK_TYPE_ANY,
                                 0 /* queue_priority */,
                                 1 /* max_queue_size */,
                                 &get_cb,
                                 NULL /* proc_cls */);
  if (NULL == qe)
  {
    fprintf (stderr, _ ("Error queueing datastore GET operation\n"));
    ret = 1;
    GNUNET_SCHEDULER_shutdown ();
  }
}


/**
 * Begin dumping the database.
 */
static void
start_dump ()
{
  record_count = 0;

  if (NULL != file_name)
  {
    file_handle = GNUNET_DISK_file_open (file_name,
                                         GNUNET_DISK_OPEN_WRITE
                                         | GNUNET_DISK_OPEN_TRUNCATE
                                         | GNUNET_DISK_OPEN_CREATE,
                                         GNUNET_DISK_PERM_USER_READ
                                         | GNUNET_DISK_PERM_USER_WRITE);
    if (NULL == file_handle)
    {
      fprintf (stderr, _ ("Unable to open dump file: %s\n"), file_name);
      ret = 1;
      GNUNET_SCHEDULER_shutdown ();
      return;
    }
  }
  else
  {
    file_handle = GNUNET_DISK_get_handle_from_int_fd (STDOUT_FILENO);
  }
  GNUNET_DISK_file_write (file_handle, MAGIC_BYTES, MAGIC_LEN);
  do_get (0);
}


/**
 * Continuation called to notify client about result of the
 * operation.
 *
 * @param cls closure
 * @param success GNUNET_SYSERR on failure (including timeout/queue drop)
 *                GNUNET_NO if content was already there
 *                GNUNET_YES (or other positive value) on success
 * @param min_expiration minimum expiration time required for 0-priority content to be stored
 *                by the datacache at this time, zero for unknown, forever if we have no
 *                space for 0-priority content
 * @param msg NULL on success, otherwise an error message
 */
static void
put_cb (void *cls,
        int32_t success,
        struct GNUNET_TIME_Absolute min_expiration,
        const char *msg)
{
  qe = NULL;
  if (GNUNET_SYSERR == success)
  {
    fprintf (stderr, _ ("Failed to store item: %s, aborting\n"), msg);
    ret = 1;
    GNUNET_SCHEDULER_shutdown ();
    return;
  }

  struct DataRecord dr;
  ssize_t len;

  len = GNUNET_DISK_file_read (file_handle, &dr, sizeof(dr));
  if (0 == len)
  {
    fprintf (stderr, _ ("Inserted %" PRIu64 " records\n"), record_count);
    ret = 0;
    GNUNET_SCHEDULER_shutdown ();
    return;
  }
  else if (sizeof(dr) != len)
  {
    fprintf (stderr,
             _ ("Short read from file: %zd bytes expecting %zd\n"),
             len,
             sizeof(dr));
    ret = 1;
    GNUNET_SCHEDULER_shutdown ();
    return;
  }

  const size_t size = ntohl (dr.size);
  uint8_t data[size];
  len = GNUNET_DISK_file_read (file_handle, data, size);
  if (size != len)
  {
    fprintf (stderr,
             _ ("Short read from file: %zd bytes expecting %zd\n"),
             len,
             size);
    ret = 1;
    GNUNET_SCHEDULER_shutdown ();
    return;
  }

  record_count++;
  qe = GNUNET_DATASTORE_put (datastore,
                             0,
                             &dr.key,
                             size,
                             data,
                             ntohl (dr.type),
                             ntohl (dr.priority),
                             ntohl (dr.anonymity),
                             ntohl (dr.replication),
                             GNUNET_TIME_absolute_ntoh (dr.expiration),
                             0,
                             1,
                             &put_cb,
                             NULL);
  if (NULL == qe)
  {
    fprintf (stderr, _ ("Error queueing datastore PUT operation\n"));
    ret = 1;
    GNUNET_SCHEDULER_shutdown ();
  }
}


/**
 * Begin inserting into the database.
 */
static void
start_insert ()
{
  record_count = 0;

  if (NULL != file_name)
  {
    file_handle = GNUNET_DISK_file_open (file_name,
                                         GNUNET_DISK_OPEN_READ,
                                         GNUNET_DISK_PERM_NONE);
    if (NULL == file_handle)
    {
      fprintf (stderr, _ ("Unable to open dump file: %s\n"), file_name);
      ret = 1;
      GNUNET_SCHEDULER_shutdown ();
      return;
    }
  }
  else
  {
    file_handle = GNUNET_DISK_get_handle_from_int_fd (STDIN_FILENO);
  }

  uint8_t buf[MAGIC_LEN];
  ssize_t len;

  len = GNUNET_DISK_file_read (file_handle, buf, MAGIC_LEN);
  if ((len != MAGIC_LEN) || (0 != memcmp (buf, MAGIC_BYTES, MAGIC_LEN)))
  {
    fprintf (stderr, _ ("Input file is not of a supported format\n"));
    return;
  }
  put_cb (NULL, GNUNET_YES, GNUNET_TIME_UNIT_ZERO_ABS, NULL);
}


/**
 * Main function that will be run by the scheduler.
 *
 * @param cls closure
 * @param args remaining command-line arguments
 * @param cfgfile name of the configuration file used
 * @param cfg configuration
 */
static void
run (void *cls,
     char *const *args,
     const char *cfgfile,
     const struct GNUNET_CONFIGURATION_Handle *cfg)
{
  GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
  datastore = GNUNET_DATASTORE_connect (cfg);
  if (NULL == datastore)
  {
    fprintf (stderr, _ ("Failed connecting to the datastore.\n"));
    ret = 1;
    GNUNET_SCHEDULER_shutdown ();
    return;
  }
  if (dump)
    start_dump ();
  else if (insert)
    start_insert ();
  else
  {
    fprintf (stderr,
             _ ("Please choose at least one operation: %s, %s\n"),
             "dump",
             "insert");
    ret = 1;
    GNUNET_SCHEDULER_shutdown ();
  }
}


/**
 * The main function to manipulate datastores.
 *
 * @param argc number of arguments from the command line
 * @param argv command line arguments
 * @return 0 ok, 1 on error
 */
int
main (int argc, char *const *argv)
{
  struct GNUNET_GETOPT_CommandLineOption options[] =
  { GNUNET_GETOPT_option_flag ('d',
                               "dump",
                               gettext_noop (
                                 "Dump all records from the datastore"),
                               &dump),
    GNUNET_GETOPT_option_flag ('i',
                               "insert",
                               gettext_noop (
                                 "Insert records into the datastore"),
                               &insert),
    GNUNET_GETOPT_option_filename ('f',
                                   "file",
                                   "FILENAME",
                                   gettext_noop ("File to dump or insert"),
                                   &file_name),
    GNUNET_GETOPT_OPTION_END };

  if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
    return 2;

  if (GNUNET_OK !=
      GNUNET_PROGRAM_run (argc,
                          argv,
                          "gnunet-datastore",
                          gettext_noop ("Manipulate GNUnet datastore"),
                          options,
                          &run,
                          NULL))
    ret = 1;
  GNUNET_free_nz ((void *) argv);
  return ret;
}


/* end of gnunet-datastore.c */