donau

Donation authority for GNU Taler (experimental)
Log | Files | Refs | Submodules | README | LICENSE

donau-httpd_db.c (3924B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2024 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 donau-httpd_db.c
     18  * @brief Generic database operations for the donau.
     19  * @author Johannes Casaburi
     20  */
     21 #include <donau_config.h>
     22 #include <pthread.h>
     23 #include <jansson.h>
     24 #include <gnunet/gnunet_json_lib.h>
     25 #include <taler/taler_json_lib.h>
     26 #include <taler/taler_mhd_lib.h>
     27 #include "donaudb_lib.h"
     28 #include "donau-httpd_db.h"
     29 #include "donau-httpd.h"
     30 
     31 
     32 enum GNUNET_GenericReturnValue
     33 DH_DB_run_transaction (struct MHD_Connection *connection,
     34                        const char *name,
     35                        MHD_RESULT *mhd_ret,
     36                        DH_DB_TransactionCallback cb,
     37                        void *cb_cls)
     38 {
     39   if (NULL != mhd_ret)
     40     *mhd_ret = -1; /* set to invalid value, to help detect bugs */
     41   if (GNUNET_OK !=
     42       DH_plugin->preflight (DH_plugin->cls))
     43   {
     44     GNUNET_break (0);
     45     if (NULL != mhd_ret)
     46       *mhd_ret = TALER_MHD_reply_with_error (connection,
     47                                              MHD_HTTP_INTERNAL_SERVER_ERROR,
     48                                              TALER_EC_GENERIC_DB_SETUP_FAILED,
     49                                              NULL);
     50     return GNUNET_SYSERR;
     51   }
     52   for (unsigned int retries = 0;
     53        retries < MAX_TRANSACTION_COMMIT_RETRIES;
     54        retries++)
     55   {
     56     enum GNUNET_DB_QueryStatus qs;
     57 
     58     if (GNUNET_OK !=
     59         DH_plugin->start (DH_plugin->cls,
     60                           name))
     61     {
     62       GNUNET_break (0);
     63       if (NULL != mhd_ret)
     64         *mhd_ret = TALER_MHD_reply_with_error (connection,
     65                                                MHD_HTTP_INTERNAL_SERVER_ERROR,
     66                                                TALER_EC_GENERIC_DB_START_FAILED,
     67                                                NULL);
     68       return GNUNET_SYSERR;
     69     }
     70     qs = cb (cb_cls,
     71              connection,
     72              mhd_ret);
     73     if (0 > qs)
     74     {
     75       DH_plugin->rollback (DH_plugin->cls);
     76       if (GNUNET_DB_STATUS_HARD_ERROR == qs)
     77         return GNUNET_SYSERR;
     78     }
     79     else
     80     {
     81       qs = DH_plugin->commit (DH_plugin->cls);
     82       if (GNUNET_DB_STATUS_HARD_ERROR == qs)
     83       {
     84         DH_plugin->rollback (DH_plugin->cls);
     85         if (NULL != mhd_ret)
     86           *mhd_ret = TALER_MHD_reply_with_error (connection,
     87                                                  MHD_HTTP_INTERNAL_SERVER_ERROR,
     88                                                  TALER_EC_GENERIC_DB_COMMIT_FAILED,
     89                                                  NULL);
     90         return GNUNET_SYSERR;
     91       }
     92       if (0 > qs)
     93         DH_plugin->rollback (DH_plugin->cls);
     94     }
     95     /* make sure callback did not violate invariants! */
     96     GNUNET_assert ( (NULL == mhd_ret) ||
     97                     (-1 == (int) *mhd_ret) );
     98     if (0 <= qs)
     99       return GNUNET_OK;
    100   }
    101   DH_plugin->rollback (DH_plugin->cls);
    102   TALER_LOG_ERROR ("Transaction `%s' commit failed %u times\n",
    103                    name,
    104                    MAX_TRANSACTION_COMMIT_RETRIES);
    105   if (NULL != mhd_ret)
    106     *mhd_ret = TALER_MHD_reply_with_error (connection,
    107                                            MHD_HTTP_INTERNAL_SERVER_ERROR,
    108                                            TALER_EC_GENERIC_DB_SOFT_FAILURE,
    109                                            NULL);
    110   return GNUNET_SYSERR;
    111 }
    112 
    113 
    114 /* end of donau-httpd_db.c */