sync

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

sync-dbinit.c (4262B)


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