2 This file is part of GNUnet.
3 (C) 2010, 2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file datastore/gnunet-datastore.c
23 * @brief tool to manipulate datastores
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_datastore_service.h"
32 * Name of the second configuration file.
34 static char *alternative_cfg;
37 * Global return value.
42 * Our offset on 'get'.
44 static uint64_t offset;
47 * First UID ever returned.
49 static uint64_t first_uid;
52 * Configuration for the source database.
54 static struct GNUNET_CONFIGURATION_Handle *scfg;
57 * Handle for database source.
59 static struct GNUNET_DATASTORE_Handle *db_src;
62 * Handle for database destination.
64 static struct GNUNET_DATASTORE_Handle *db_dst;
69 static struct GNUNET_DATASTORE_QueueEntry *qe;
73 do_shutdown (void *cls,
74 const struct GNUNET_SCHEDULER_TaskContext *tc)
77 GNUNET_DATASTORE_cancel (qe);
78 GNUNET_DATASTORE_disconnect (db_src, GNUNET_NO);
79 GNUNET_DATASTORE_disconnect (db_dst, GNUNET_NO);
80 GNUNET_CONFIGURATION_destroy (scfg);
85 * Perform next GET operation.
92 * Continuation called to notify client about result of the
96 * @param success GNUNET_SYSERR on failure (including timeout/queue drop)
97 * GNUNET_NO if content was already there
98 * GNUNET_YES (or other positive value) on success
99 * @param min_expiration minimum expiration time required for 0-priority content to be stored
100 * by the datacache at this time, zero for unknown, forever if we have no
101 * space for 0-priority content
102 * @param msg NULL on success, otherwise an error message
105 do_finish (void *cls,
107 struct GNUNET_TIME_Absolute min_expiration,
111 if (GNUNET_SYSERR == success)
114 _("Failed to store item: %s, aborting\n"),
117 GNUNET_SCHEDULER_shutdown ();
125 * Process a datum that was stored in the datastore.
128 * @param key key for the content
129 * @param size number of bytes in data
130 * @param data content stored
131 * @param type type of the content
132 * @param priority priority of the content
133 * @param anonymity anonymity-level for the content
134 * @param expiration expiration time for the content
135 * @param uid unique identifier for the datum;
136 * maybe 0 if no unique identifier is available
140 const struct GNUNET_HashCode *key,
141 size_t size, const void *data,
142 enum GNUNET_BLOCK_Type type,
145 struct GNUNET_TIME_Absolute
146 expiration, uint64_t uid)
149 if ( (0 != offset) &&
152 GNUNET_SCHEDULER_shutdown ();
157 qe = GNUNET_DATASTORE_put (db_dst, 0,
158 key, size, data, type,
160 0 /* FIXME: replication is lost... */,
162 0, 1, GNUNET_TIME_UNIT_FOREVER_REL,
168 * Perform next GET operation.
173 qe = GNUNET_DATASTORE_get_key (db_src,
175 NULL, GNUNET_BLOCK_TYPE_ANY,
177 GNUNET_TIME_UNIT_FOREVER_REL,
184 * Main function that will be run by the scheduler.
187 * @param args remaining command-line arguments
188 * @param cfgfile name of the configuration file used
189 * @param cfg configuration -- for destination datastore
192 run (void *cls, char *const *args, const char *cfgfile,
193 const struct GNUNET_CONFIGURATION_Handle *cfg)
195 if (NULL == alternative_cfg)
196 return; /* nothing to be done */
197 if (0 == strcmp (cfgfile, alternative_cfg))
200 _("Cannot use the same configuration for source and destination\n"));
204 scfg = GNUNET_CONFIGURATION_create ();
206 GNUNET_CONFIGURATION_load (scfg,
209 GNUNET_CONFIGURATION_destroy (scfg);
213 db_src = GNUNET_DATASTORE_connect (scfg);
216 GNUNET_CONFIGURATION_destroy (scfg);
220 db_dst = GNUNET_DATASTORE_connect (cfg);
223 GNUNET_DATASTORE_disconnect (db_src, GNUNET_NO);
224 GNUNET_CONFIGURATION_destroy (scfg);
228 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
235 * The main function to manipulate datastores.
237 * @param argc number of arguments from the command line
238 * @param argv command line arguments
239 * @return 0 ok, 1 on error
242 main (int argc, char *const *argv)
244 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
245 { 's', "sourcecfg", "FILENAME",
246 gettext_noop ("specifies the configuration to use to access an alternative datastore; will merge that datastore into our current datastore"),
247 1, &GNUNET_GETOPT_set_filename, &alternative_cfg },
248 GNUNET_GETOPT_OPTION_END
250 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
254 GNUNET_PROGRAM_run (argc, argv, "gnunet-datastore",
255 gettext_noop ("Manipulate GNUnet datastore"),
256 options, &run, NULL))
258 GNUNET_free ((void*) argv);
262 /* end of gnunet-datastore.c */