diff options
author | Christian Grothoff <christian@grothoff.org> | 2020-01-24 16:51:29 +0100 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2020-01-24 16:51:29 +0100 |
commit | f4a172abf3afabf677c59020c6fabc76b87b3f22 (patch) | |
tree | 19adfe6019fa9d364d54c19c8e9e50b82e06168a /src/pq/pq_connect.c | |
parent | 2fd320bc7263b1dadd8fbead1004e9ee8b822f45 (diff) | |
download | gnunet-f4a172abf3afabf677c59020c6fabc76b87b3f22.tar.gz gnunet-f4a172abf3afabf677c59020c6fabc76b87b3f22.zip |
export GNUNET_PQ_run_sql() functonality
Diffstat (limited to 'src/pq/pq_connect.c')
-rw-r--r-- | src/pq/pq_connect.c | 135 |
1 files changed, 77 insertions, 58 deletions
diff --git a/src/pq/pq_connect.c b/src/pq/pq_connect.c index 7cd7d8787..31ee59b51 100644 --- a/src/pq/pq_connect.c +++ b/src/pq/pq_connect.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet | 2 | This file is part of GNUnet |
3 | Copyright (C) 2017, 2019 GNUnet e.V. | 3 | Copyright (C) 2017, 2019, 2020 GNUnet e.V. |
4 | 4 | ||
5 | GNUnet is free software: you can redistribute it and/or modify it | 5 | GNUnet is free software: you can redistribute it and/or modify it |
6 | under the terms of the GNU Affero General Public License as published | 6 | under the terms of the GNU Affero General Public License as published |
@@ -135,6 +135,74 @@ GNUNET_PQ_connect (const char *config_str, | |||
135 | 135 | ||
136 | 136 | ||
137 | /** | 137 | /** |
138 | * Within the @a db context, run all the SQL files | ||
139 | * from the @a load_path from 0000-9999.sql (as long | ||
140 | * as the files exist contiguously). | ||
141 | * | ||
142 | * @param db database context to use | ||
143 | * @param load_path where to find the XXXX.sql files | ||
144 | * @return #GNUNET_OK on success | ||
145 | */ | ||
146 | int | ||
147 | GNUNET_PQ_run_sql (struct GNUNET_PQ_Context *db, | ||
148 | const char *load_path) | ||
149 | { | ||
150 | size_t slen = strlen (db->load_path) + 10; | ||
151 | |||
152 | for (unsigned int i = 0; i<10000; i++) | ||
153 | { | ||
154 | char buf[slen]; | ||
155 | struct GNUNET_OS_Process *psql; | ||
156 | enum GNUNET_OS_ProcessStatusType type; | ||
157 | unsigned long code; | ||
158 | |||
159 | GNUNET_snprintf (buf, | ||
160 | sizeof (buf), | ||
161 | "%s%04u.sql", | ||
162 | db->load_path, | ||
163 | i); | ||
164 | if (GNUNET_YES != | ||
165 | GNUNET_DISK_file_test (buf)) | ||
166 | break; /* We are done */ | ||
167 | psql = GNUNET_OS_start_process (GNUNET_NO, | ||
168 | GNUNET_OS_INHERIT_STD_NONE, | ||
169 | NULL, | ||
170 | NULL, | ||
171 | NULL, | ||
172 | "psql", | ||
173 | "psql", | ||
174 | db->config_str, | ||
175 | "-f", | ||
176 | buf, | ||
177 | "-q", | ||
178 | NULL); | ||
179 | if (NULL == psql) | ||
180 | { | ||
181 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, | ||
182 | "exec", | ||
183 | "psql"); | ||
184 | return GNUNET_SYSERR; | ||
185 | } | ||
186 | GNUNET_assert (GNUNET_OK == | ||
187 | GNUNET_OS_process_wait_status (psql, | ||
188 | &type, | ||
189 | &code)); | ||
190 | GNUNET_OS_process_destroy (psql); | ||
191 | if ( (GNUNET_OS_PROCESS_EXITED != type) || | ||
192 | (0 != code) ) | ||
193 | { | ||
194 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
195 | "Could not run PSQL on file %s: %d", | ||
196 | buf, | ||
197 | (int) code); | ||
198 | return GNUNET_SYSERR; | ||
199 | } | ||
200 | } | ||
201 | return GNUNET_OK; | ||
202 | } | ||
203 | |||
204 | |||
205 | /** | ||
138 | * Reinitialize the database @a db if the connection is down. | 206 | * Reinitialize the database @a db if the connection is down. |
139 | * | 207 | * |
140 | * @param db database connection to reinitialize | 208 | * @param db database connection to reinitialize |
@@ -182,63 +250,14 @@ GNUNET_PQ_reconnect (struct GNUNET_PQ_Context *db) | |||
182 | PQsetNoticeProcessor (db->conn, | 250 | PQsetNoticeProcessor (db->conn, |
183 | &pq_notice_processor_cb, | 251 | &pq_notice_processor_cb, |
184 | db); | 252 | db); |
185 | if (NULL != db->load_path) | 253 | if ( (NULL != db->load_path) && |
186 | { | 254 | (GNUNET_OK != |
187 | size_t slen = strlen (db->load_path) + 10; | 255 | GNUNET_PQ_run_sql (db, |
188 | 256 | db->load_path)) ) | |
189 | for (unsigned int i = 0; i<10000; i++) | 257 | { |
190 | { | 258 | PQfinish (db->conn); |
191 | char buf[slen]; | 259 | db->conn = NULL; |
192 | struct GNUNET_OS_Process *psql; | 260 | return; |
193 | enum GNUNET_OS_ProcessStatusType type; | ||
194 | unsigned long code; | ||
195 | |||
196 | GNUNET_snprintf (buf, | ||
197 | sizeof (buf), | ||
198 | "%s%04u.sql", | ||
199 | db->load_path, | ||
200 | i); | ||
201 | if (GNUNET_YES != | ||
202 | GNUNET_DISK_file_test (buf)) | ||
203 | break; /* We are done */ | ||
204 | psql = GNUNET_OS_start_process (GNUNET_NO, | ||
205 | GNUNET_OS_INHERIT_STD_NONE, | ||
206 | NULL, | ||
207 | NULL, | ||
208 | NULL, | ||
209 | "psql", | ||
210 | "psql", | ||
211 | db->config_str, | ||
212 | "-f", | ||
213 | buf, | ||
214 | "-q", | ||
215 | NULL); | ||
216 | if (NULL == psql) | ||
217 | { | ||
218 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, | ||
219 | "exec", | ||
220 | "psql"); | ||
221 | PQfinish (db->conn); | ||
222 | db->conn = NULL; | ||
223 | return; | ||
224 | } | ||
225 | GNUNET_assert (GNUNET_OK == | ||
226 | GNUNET_OS_process_wait_status (psql, | ||
227 | &type, | ||
228 | &code)); | ||
229 | GNUNET_OS_process_destroy (psql); | ||
230 | if ( (GNUNET_OS_PROCESS_EXITED != type) || | ||
231 | (0 != code) ) | ||
232 | { | ||
233 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
234 | "Could not run PSQL on file %s: %d", | ||
235 | buf, | ||
236 | (int) code); | ||
237 | PQfinish (db->conn); | ||
238 | db->conn = NULL; | ||
239 | return; | ||
240 | } | ||
241 | } | ||
242 | } | 261 | } |
243 | if ( (NULL != db->es) && | 262 | if ( (NULL != db->es) && |
244 | (GNUNET_OK != | 263 | (GNUNET_OK != |