export GNUNET_PQ_run_sql() functonality
authorChristian Grothoff <christian@grothoff.org>
Fri, 24 Jan 2020 15:51:29 +0000 (16:51 +0100)
committerChristian Grothoff <christian@grothoff.org>
Fri, 24 Jan 2020 15:51:29 +0000 (16:51 +0100)
src/include/gnunet_pq_lib.h
src/pq/pq_connect.c

index 53d2549c2978040e4299e29bc83df0a190edccf1..090e81331bb0f1df11c02f564b5630c059cf66ce 100644 (file)
@@ -779,6 +779,20 @@ void
 GNUNET_PQ_reconnect (struct GNUNET_PQ_Context *db);
 
 
+/**
+ * Within the @a db context, run all the SQL files
+ * from the @a load_path from 0000-9999.sql (as long
+ * as the files exist contiguously).
+ *
+ * @param db database context to use
+ * @param load_path where to find the XXXX.sql files
+ * @return #GNUNET_OK on success
+ */
+int
+GNUNET_PQ_run_sql (struct GNUNET_PQ_Context *db,
+                   const char *load_path);
+
+
 /**
  * Disconnect from the database, destroying the prepared statements
  * and releasing other associated resources.
index 7cd7d8787b5120deb3a47fb0c1beb46d87cd82d9..31ee59b514b9b501567f3b076f01a29869ffc4f1 100644 (file)
@@ -1,6 +1,6 @@
 /*
    This file is part of GNUnet
-   Copyright (C) 2017, 2019 GNUnet e.V.
+   Copyright (C) 2017, 2019, 2020 GNUnet e.V.
 
    GNUnet is free software: you can redistribute it and/or modify it
    under the terms of the GNU Affero General Public License as published
@@ -134,6 +134,74 @@ GNUNET_PQ_connect (const char *config_str,
 }
 
 
+/**
+ * Within the @a db context, run all the SQL files
+ * from the @a load_path from 0000-9999.sql (as long
+ * as the files exist contiguously).
+ *
+ * @param db database context to use
+ * @param load_path where to find the XXXX.sql files
+ * @return #GNUNET_OK on success
+ */
+int
+GNUNET_PQ_run_sql (struct GNUNET_PQ_Context *db,
+                   const char *load_path)
+{
+  size_t slen = strlen (db->load_path) + 10;
+
+  for (unsigned int i = 0; i<10000; i++)
+  {
+    char buf[slen];
+    struct GNUNET_OS_Process *psql;
+    enum GNUNET_OS_ProcessStatusType type;
+    unsigned long code;
+    
+    GNUNET_snprintf (buf,
+                     sizeof (buf),
+                     "%s%04u.sql",
+                     db->load_path,
+                     i);
+    if (GNUNET_YES !=
+        GNUNET_DISK_file_test (buf))
+      break; /* We are done */
+    psql = GNUNET_OS_start_process (GNUNET_NO,
+                                    GNUNET_OS_INHERIT_STD_NONE,
+                                    NULL,
+                                    NULL,
+                                    NULL,
+                                    "psql",
+                                    "psql",
+                                    db->config_str,
+                                    "-f",
+                                    buf,
+                                    "-q",
+                                    NULL);
+    if (NULL == psql)
+    {
+      GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
+                                "exec",
+                                "psql");
+      return GNUNET_SYSERR;
+    }
+    GNUNET_assert (GNUNET_OK ==
+                   GNUNET_OS_process_wait_status (psql,
+                                                  &type,
+                                                  &code));
+    GNUNET_OS_process_destroy (psql);
+    if ( (GNUNET_OS_PROCESS_EXITED != type) ||
+         (0 != code) )
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                  "Could not run PSQL on file %s: %d",
+                  buf,
+                  (int) code);
+      return GNUNET_SYSERR;
+    }
+  }
+  return GNUNET_OK;
+}
+
+
 /**
  * Reinitialize the database @a db if the connection is down.
  *
@@ -182,63 +250,14 @@ GNUNET_PQ_reconnect (struct GNUNET_PQ_Context *db)
   PQsetNoticeProcessor (db->conn,
                         &pq_notice_processor_cb,
                         db);
-  if (NULL != db->load_path)
-  {
-    size_t slen = strlen (db->load_path) + 10;
-
-    for (unsigned int i = 0; i<10000; i++)
-    {
-      char buf[slen];
-      struct GNUNET_OS_Process *psql;
-      enum GNUNET_OS_ProcessStatusType type;
-      unsigned long code;
-
-      GNUNET_snprintf (buf,
-                       sizeof (buf),
-                       "%s%04u.sql",
-                       db->load_path,
-                       i);
-      if (GNUNET_YES !=
-          GNUNET_DISK_file_test (buf))
-        break; /* We are done */
-      psql = GNUNET_OS_start_process (GNUNET_NO,
-                                      GNUNET_OS_INHERIT_STD_NONE,
-                                      NULL,
-                                      NULL,
-                                      NULL,
-                                      "psql",
-                                      "psql",
-                                      db->config_str,
-                                      "-f",
-                                      buf,
-                                      "-q",
-                                      NULL);
-      if (NULL == psql)
-      {
-        GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
-                                  "exec",
-                                  "psql");
-        PQfinish (db->conn);
-        db->conn = NULL;
-        return;
-      }
-      GNUNET_assert (GNUNET_OK ==
-                     GNUNET_OS_process_wait_status (psql,
-                                                    &type,
-                                                    &code));
-      GNUNET_OS_process_destroy (psql);
-      if ( (GNUNET_OS_PROCESS_EXITED != type) ||
-           (0 != code) )
-      {
-        GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                    "Could not run PSQL on file %s: %d",
-                    buf,
-                    (int) code);
-        PQfinish (db->conn);
-        db->conn = NULL;
-        return;
-      }
-    }
+  if ( (NULL != db->load_path) &&
+     (GNUNET_OK !=
+      GNUNET_PQ_run_sql (db,
+                         db->load_path)) )
+  { 
+    PQfinish (db->conn);
+    db->conn = NULL;
+    return;
   }
   if ( (NULL != db->es) &&
        (GNUNET_OK !=