sync

Backup service to store encrypted wallet databases (experimental)
Log | Files | Refs | Submodules | README | LICENSE

sync-dbinit.c (4072B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2019 Taler Systems SA
      4 
      5   TALER is free software; you can redistribute it and/or modify it under the
      6   terms of the GNU General Public License as published by the Free Software
      7   Foundation; either version 3, or (at your option) any later version.
      8 
      9   TALER is distributed in the hope that it will be useful, but WITHOUT ANY
     10   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11   A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
     12 
     13   You should have received a copy of the GNU General Public License along with
     14   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15 */
     16 /**
     17  * @file syncdb/sync-dbinit.c
     18  * @brief Create tables for the sync database.
     19  * @author Christian Grothoff
     20  */
     21 #include "platform.h"
     22 #include <gnunet/gnunet_util_lib.h>
     23 #include "sync_util.h"
     24 #include "sync_database_lib.h"
     25 
     26 
     27 /**
     28  * Return value from main().
     29  */
     30 static int global_ret;
     31 
     32 /**
     33  * -r option: do full DB reset
     34  */
     35 static int reset_db;
     36 
     37 /**
     38  * -g option: do GC reset
     39  */
     40 static int gc_db;
     41 
     42 
     43 /**
     44  * Main function that will be run.
     45  *
     46  * @param cls closure
     47  * @param args remaining command-line arguments
     48  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
     49  * @param cfg configuration
     50  */
     51 static void
     52 run (void *cls,
     53      char *const *args,
     54      const char *cfgfile,
     55      const struct GNUNET_CONFIGURATION_Handle *cfg)
     56 {
     57   struct SYNC_DatabasePlugin *plugin;
     58 
     59   if (NULL ==
     60       (plugin = SYNC_DB_plugin_load (cfg,
     61                                      true)))
     62   {
     63     fprintf (stderr,
     64              "Failed to initialize database plugin.\n");
     65     global_ret = EXIT_NOTINSTALLED;
     66     return;
     67   }
     68   if (reset_db)
     69   {
     70     if (GNUNET_OK != plugin->drop_tables (plugin->cls))
     71     {
     72       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     73                   "Could not drop tables as requested. Either database was not yet initialized, or permission denied. Consult the logs. Will still try to create new tables.\n");
     74     }
     75   }
     76   if (GNUNET_OK !=
     77       plugin->create_tables (plugin->cls))
     78   {
     79     global_ret = EXIT_FAILURE;
     80     SYNC_DB_plugin_unload (plugin);
     81     return;
     82   }
     83   if (gc_db)
     84   {
     85     struct GNUNET_TIME_Absolute now;
     86     struct GNUNET_TIME_Absolute ancient;
     87 
     88     now = GNUNET_TIME_absolute_get ();
     89     ancient = GNUNET_TIME_absolute_subtract (now,
     90                                              GNUNET_TIME_relative_multiply (
     91                                                GNUNET_TIME_UNIT_YEARS,
     92                                                6));
     93     if (0 >
     94         plugin->gc (plugin->cls,
     95                     now,
     96                     ancient))
     97     {
     98       fprintf (stderr,
     99                "Garbage collection failed!\n");
    100       global_ret = EXIT_FAILURE;
    101     }
    102   }
    103   SYNC_DB_plugin_unload (plugin);
    104 }
    105 
    106 
    107 /**
    108  * The main function of the database initialization tool.
    109  * Used to initialize the Sync' database.
    110  *
    111  * @param argc number of arguments from the command line
    112  * @param argv command line arguments
    113  * @return 0 ok, non-zero on error
    114  */
    115 int
    116 main (int argc,
    117       char *const *argv)
    118 {
    119   struct GNUNET_GETOPT_CommandLineOption options[] = {
    120     GNUNET_GETOPT_option_flag ('r',
    121                                "reset",
    122                                "reset database (DANGEROUS: all existing data is lost!)",
    123                                &reset_db),
    124     GNUNET_GETOPT_option_flag ('g',
    125                                "garbagecollect",
    126                                "remove state data from database",
    127                                &gc_db),
    128     GNUNET_GETOPT_OPTION_END
    129   };
    130   enum GNUNET_GenericReturnValue ret;
    131 
    132   ret = GNUNET_PROGRAM_run (SYNC_project_data (),
    133                             argc, argv,
    134                             "sync-dbinit",
    135                             "Initialize sync database",
    136                             options,
    137                             &run, NULL);
    138   if (GNUNET_SYSERR == ret)
    139     return EXIT_INVALIDARGUMENT;
    140   if (GNUNET_NO == ret)
    141     return EXIT_SUCCESS;
    142   return global_ret;
    143 }
    144 
    145 
    146 /* end of sync-dbinit.c */