sync

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

syncdb_pg.c (4092B)


      1 /*
      2   This file is part of TALER
      3   (C) 2014--2022 Taler Systems SA
      4 
      5   TALER is free software; you can redistribute it and/or modify it under the
      6   terms of the GNU Lesser 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/syncdb_pg.c
     18  * @brief shared database state and helpers for sync postgres
     19  * @author Christian Grothoff
     20  */
     21 struct PostgresClosure;
     22 #define GNUNET_PQ_RECONNECT_CALLBACK_CLOSURE \
     23         struct PostgresClosure
     24 #include "syncdb_pg.h"
     25 #include "sync/sync_util.h"
     26 
     27 
     28 /**
     29  * Global database closure.
     30  */
     31 struct PostgresClosure *pg;
     32 
     33 
     34 /**
     35  * Function called each time we connect or reconnect to the
     36  * database.
     37  *
     38  * @param pg_ database context
     39  * @param pq database connection handle
     40  */
     41 static void
     42 reconnect_cb (struct PostgresClosure *pg_,
     43               struct GNUNET_PQ_Context *pq)
     44 {
     45   struct GNUNET_PQ_ExecuteStatement es[] = {
     46     GNUNET_PQ_make_execute ("SET search_path TO sync;"),
     47     GNUNET_PQ_EXECUTE_STATEMENT_END
     48   };
     49 
     50   if (GNUNET_OK !=
     51       GNUNET_PQ_exec_statements (pq,
     52                                  es))
     53   {
     54     GNUNET_break (0);
     55     return;
     56   }
     57   pg_->prep_gen++;
     58 }
     59 
     60 
     61 /**
     62  * Initialize the global database closure #pg from @a cfg.
     63  *
     64  * @param cfg configuration to use
     65  * @param check_current true to refuse to start unless the
     66  *        database schema is up-to-date
     67  * @return #GNUNET_OK on success
     68  */
     69 static enum GNUNET_GenericReturnValue
     70 do_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
     71          bool check_current)
     72 {
     73   pg = GNUNET_new (struct PostgresClosure);
     74   pg->cfg = cfg;
     75   if (GNUNET_OK !=
     76       GNUNET_CONFIGURATION_get_value_filename (cfg,
     77                                                "syncdb-postgres",
     78                                                "SQL_DIR",
     79                                                &pg->sql_dir))
     80   {
     81     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
     82                                "syncdb-postgres",
     83                                "SQL_DIR");
     84     GNUNET_free (pg);
     85     pg = NULL;
     86     return GNUNET_SYSERR;
     87   }
     88   if (GNUNET_OK !=
     89       GNUNET_CONFIGURATION_get_value_string (cfg,
     90                                              "sync",
     91                                              "CURRENCY",
     92                                              &pg->currency))
     93   {
     94     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
     95                                "sync",
     96                                "CURRENCY");
     97     GNUNET_free (pg->sql_dir);
     98     GNUNET_free (pg);
     99     pg = NULL;
    100     return GNUNET_SYSERR;
    101   }
    102   pg->conn = GNUNET_PQ_init (cfg,
    103                              "syncdb-postgres",
    104                              &reconnect_cb,
    105                              pg);
    106   if (NULL == pg->conn)
    107   {
    108     SYNCDB_fini ();
    109     return GNUNET_NO;
    110   }
    111   if (check_current &&
    112       (GNUNET_OK !=
    113        GNUNET_PQ_check_current (pg->conn,
    114                                 "sync-")))
    115   {
    116     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    117                 "Database schema is not up-to-date."
    118                 " Try running sync-dbinit!\n");
    119     SYNCDB_fini ();
    120     return GNUNET_NO;
    121   }
    122   return GNUNET_OK;
    123 }
    124 
    125 
    126 enum GNUNET_GenericReturnValue
    127 SYNCDB_init (const struct GNUNET_CONFIGURATION_Handle *cfg)
    128 {
    129   return do_init (cfg,
    130                   true);
    131 }
    132 
    133 
    134 enum GNUNET_GenericReturnValue
    135 SYNCDB_init_admin (const struct GNUNET_CONFIGURATION_Handle *cfg)
    136 {
    137   return do_init (cfg,
    138                   false);
    139 }
    140 
    141 
    142 void
    143 SYNCDB_fini (void)
    144 {
    145   if (NULL == pg)
    146     return;
    147   if (NULL != pg->conn)
    148     GNUNET_PQ_disconnect (pg->conn);
    149   GNUNET_free (pg->currency);
    150   GNUNET_free (pg->sql_dir);
    151   GNUNET_free (pg);
    152   pg = NULL;
    153 }
    154 
    155 
    156 /* end of syncdb_pg.c */