sync

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

sync-dbinit.c (3913B)


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