sync

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

sync-dbinit.c (3894B)


      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/sync_util.h"
     24 #include "sync/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   if (GNUNET_OK !=
     58       SYNCDB_init_admin (cfg))
     59   {
     60     fprintf (stderr,
     61              "Failed to initialize database.\n");
     62     global_ret = EXIT_NOTINSTALLED;
     63     return;
     64   }
     65   if (reset_db)
     66   {
     67     if (GNUNET_OK != SYNCDB_drop_tables ())
     68     {
     69       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     70                   "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");
     71     }
     72   }
     73   if (GNUNET_OK !=
     74       SYNCDB_create_tables ())
     75   {
     76     global_ret = EXIT_FAILURE;
     77     SYNCDB_fini ();
     78     return;
     79   }
     80   if (gc_db)
     81   {
     82     struct GNUNET_TIME_Absolute now;
     83     struct GNUNET_TIME_Absolute ancient;
     84 
     85     now = GNUNET_TIME_absolute_get ();
     86     ancient = GNUNET_TIME_absolute_subtract (now,
     87                                              GNUNET_TIME_relative_multiply (
     88                                                GNUNET_TIME_UNIT_YEARS,
     89                                                6));
     90     if (0 >
     91         SYNCDB_gc (now,
     92                    ancient))
     93     {
     94       fprintf (stderr,
     95                "Garbage collection failed!\n");
     96       global_ret = EXIT_FAILURE;
     97     }
     98   }
     99   SYNCDB_fini ();
    100 }
    101 
    102 
    103 /**
    104  * The main function of the database initialization tool.
    105  * Used to initialize the Sync' database.
    106  *
    107  * @param argc number of arguments from the command line
    108  * @param argv command line arguments
    109  * @return 0 ok, non-zero on error
    110  */
    111 int
    112 main (int argc,
    113       char *const *argv)
    114 {
    115   struct GNUNET_GETOPT_CommandLineOption options[] = {
    116     GNUNET_GETOPT_option_flag ('r',
    117                                "reset",
    118                                "reset database (DANGEROUS: all existing data is lost!)",
    119                                &reset_db),
    120     GNUNET_GETOPT_option_flag ('g',
    121                                "garbagecollect",
    122                                "remove state data from database",
    123                                &gc_db),
    124     GNUNET_GETOPT_OPTION_END
    125   };
    126   enum GNUNET_GenericReturnValue ret;
    127 
    128   ret = GNUNET_PROGRAM_run (SYNC_project_data (),
    129                             argc, argv,
    130                             "sync-dbinit",
    131                             "Initialize sync database",
    132                             options,
    133                             &run, NULL);
    134   if (GNUNET_SYSERR == ret)
    135     return EXIT_INVALIDARGUMENT;
    136   if (GNUNET_NO == ret)
    137     return EXIT_SUCCESS;
    138   return global_ret;
    139 }
    140 
    141 
    142 /* end of sync-dbinit.c */