forked from LINBIT/csync2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_postgres.c
410 lines (336 loc) · 10.3 KB
/
db_postgres.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
* Copyright (C) 2010 Dennis Schafroth <dennis@schafroth.com>
* Copyright (C) 2010 Johannes Thoma <johannes.thoma@gmx.at>
* Copyright (C) 2010 - 2013 LINBIT Information Technologies GmbH
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "csync2.h"
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
#include <string.h>
#include "db_api.h"
#include "db_postgres.h"
#include "dl.h"
#ifdef HAVE_POSTGRES
#include <libpq-fe.h>
#endif
#if (!defined HAVE_POSTGRES)
int db_postgres_open(const char *file, db_conn_p * conn_p)
{
return DB_ERROR;
}
#else
static struct db_postgres_fns {
PGconn *(*PQconnectdb_fn) (char *);
ConnStatusType(*PQstatus_fn) (const PGconn *);
char *(*PQerrorMessage_fn) (const PGconn *);
void (*PQfinish_fn) (PGconn *);
PGresult *(*PQexec_fn) (PGconn *, const char *);
ExecStatusType(*PQresultStatus_fn) (const PGresult *);
char *(*PQresultErrorMessage_fn) (const PGresult *);
void (*PQclear_fn) (PGresult *);
int (*PQntuples_fn) (const PGresult *);
char *(*PQgetvalue_fn) (const PGresult *, int, int);
} f;
static void *dl_handle;
static void db_postgres_dlopen(void)
{
csync_debug(2, "Opening shared library libpq.so\n");
dl_handle = dlopen("libpq.so", RTLD_LAZY);
if (dl_handle == NULL) {
csync_fatal
("Could not open libpq.so: %s\n"
"Please install postgres client library (libpg) or use other database (sqlite, mysql)\n",
dlerror());
}
csync_debug(2, "Reading symbols from shared library libpq.so\n");
LOOKUP_SYMBOL(dl_handle, PQconnectdb);
LOOKUP_SYMBOL(dl_handle, PQstatus);
LOOKUP_SYMBOL(dl_handle, PQerrorMessage);
LOOKUP_SYMBOL(dl_handle, PQfinish);
LOOKUP_SYMBOL(dl_handle, PQexec);
LOOKUP_SYMBOL(dl_handle, PQresultStatus);
LOOKUP_SYMBOL(dl_handle, PQresultErrorMessage);
LOOKUP_SYMBOL(dl_handle, PQclear);
LOOKUP_SYMBOL(dl_handle, PQntuples);
LOOKUP_SYMBOL(dl_handle, PQgetvalue);
}
/* Thi function parses a URL string like pgsql://[user[:passwd]@]hostname[:port]/database.
and returns the result in the given parameters.
If an optional keyword is not given, the value of the parameter is not changed.
*/
int db_postgres_open(const char *file, db_conn_p * conn_p)
{
PGconn *pg_conn;
char *host, *user, *pass, *database;
unsigned int port = 5432; /* default postgres port */
char *db_url = strdup(file);
char *pg_conn_info;
db_postgres_dlopen();
if (db_url == NULL)
csync_fatal("No memory for db_url\n");
csync_parse_url(db_url, &host, &user, &pass, &database, &port);
ASPRINTF(&pg_conn_info, "host='%s' user='%s' password='%s' dbname='%s' port=%d",
host ?: "localhost", user ?: "csync2", pass ?: "", database, port);
pg_conn = f.PQconnectdb_fn(pg_conn_info);
if (pg_conn == NULL)
csync_fatal("No memory for postgress connection handle\n");
if (f.PQstatus_fn(pg_conn) != CONNECTION_OK) {
f.PQfinish_fn(pg_conn);
free(pg_conn_info);
ASPRINTF(&pg_conn_info, "host='%s' user='%s' password='%s' dbname='postgres' port=%d", host, user, pass, port);
pg_conn = f.PQconnectdb_fn(pg_conn_info);
if (pg_conn == NULL)
csync_fatal("No memory for postgress connection handle\n");
if (f.PQstatus_fn(pg_conn) != CONNECTION_OK) {
csync_debug(0, "Connection failed: %s", f.PQerrorMessage_fn(pg_conn));
f.PQfinish_fn(pg_conn);
free(pg_conn_info);
return DB_ERROR;
} else {
char *create_database_statement;
PGresult *res;
csync_debug(1, "Database %s not found, trying to create it ...", database);
ASPRINTF(&create_database_statement, "create database %s", database);
res = f.PQexec_fn(pg_conn, create_database_statement);
free(create_database_statement);
switch (f.PQresultStatus_fn(res)) {
case PGRES_COMMAND_OK:
case PGRES_TUPLES_OK:
break;
default:
csync_debug(0, "Could not create database %s: %s", database, f.PQerrorMessage_fn(pg_conn));
return DB_ERROR;
}
f.PQfinish_fn(pg_conn);
free(pg_conn_info);
ASPRINTF(&pg_conn_info, "host='%s' user='%s' password='%s' dbname='%s' port=%d",
host, user, pass, database, port);
pg_conn = f.PQconnectdb_fn(pg_conn_info);
if (pg_conn == NULL)
csync_fatal("No memory for postgress connection handle\n");
if (f.PQstatus_fn(pg_conn) != CONNECTION_OK) {
csync_debug(0, "Connection failed: %s", f.PQerrorMessage_fn(pg_conn));
f.PQfinish_fn(pg_conn);
free(pg_conn_info);
return DB_ERROR;
}
}
}
db_conn_p conn = calloc(1, sizeof(*conn));
if (conn == NULL)
csync_fatal("No memory for conn\n");
*conn_p = conn;
conn->private = pg_conn;
conn->close = db_postgres_close;
conn->exec = db_postgres_exec;
conn->errmsg = db_postgres_errmsg;
conn->prepare = db_postgres_prepare;
conn->upgrade_to_schema = db_postgres_upgrade_to_schema;
free(pg_conn_info);
return DB_OK;
}
void db_postgres_close(db_conn_p conn)
{
if (!conn)
return;
if (!conn->private)
return;
f.PQfinish_fn(conn->private);
conn->private = 0;
}
const char *db_postgres_errmsg(db_conn_p conn)
{
if (!conn)
return "(no connection)";
if (!conn->private)
return "(no private data in conn)";
return f.PQerrorMessage_fn(conn->private);
}
int db_postgres_exec(db_conn_p conn, const char *sql)
{
PGresult *res;
if (!conn)
return DB_NO_CONNECTION;
if (!conn->private) {
/* added error element */
return DB_NO_CONNECTION_REAL;
}
res = f.PQexec_fn(conn->private, sql);
switch (f.PQresultStatus_fn(res)) {
case PGRES_COMMAND_OK:
case PGRES_TUPLES_OK:
return DB_OK;
default:
return DB_ERROR;
}
}
int db_postgres_prepare(db_conn_p conn, const char *sql, db_stmt_p * stmt_p, char **pptail)
{
PGresult *result;
int *row_p;
*stmt_p = NULL;
if (!conn)
return DB_NO_CONNECTION;
if (!conn->private) {
/* added error element */
return DB_NO_CONNECTION_REAL;
}
result = f.PQexec_fn(conn->private, sql);
if (result == NULL)
csync_fatal("No memory for result\n");
switch (f.PQresultStatus_fn(result)) {
case PGRES_COMMAND_OK:
case PGRES_TUPLES_OK:
break;
default:
csync_debug(1, "Error in PQexec: %s", f.PQresultErrorMessage_fn(result));
f.PQclear_fn(result);
return DB_ERROR;
}
row_p = malloc(sizeof(*row_p));
if (row_p == NULL)
csync_fatal("No memory for row\n");
*row_p = -1;
db_stmt_p stmt = malloc(sizeof(*stmt));
if (stmt == NULL)
csync_fatal("No memory for stmt\n");
stmt->private = result;
stmt->private2 = row_p;
*stmt_p = stmt;
stmt->get_column_text = db_postgres_stmt_get_column_text;
stmt->get_column_blob = db_postgres_stmt_get_column_blob;
stmt->get_column_int = db_postgres_stmt_get_column_int;
stmt->next = db_postgres_stmt_next;
stmt->close = db_postgres_stmt_close;
stmt->db = conn;
return DB_OK;
}
const void *db_postgres_stmt_get_column_blob(db_stmt_p stmt, int column)
{
PGresult *result;
int *row_p;
if (!stmt || !stmt->private || !stmt->private2) {
return 0;
}
result = (PGresult *) stmt->private;
row_p = (int *)stmt->private2;
if (*row_p >= f.PQntuples_fn(result) || *row_p < 0) {
csync_debug(1, "row index out of range (should be between 0 and %d, is %d)\n", *row_p, f.PQntuples_fn(result));
return NULL;
}
return f.PQgetvalue_fn(result, *row_p, column);
}
const char *db_postgres_stmt_get_column_text(db_stmt_p stmt, int column)
{
PGresult *result;
int *row_p;
if (!stmt || !stmt->private || !stmt->private2) {
return 0;
}
result = (PGresult *) stmt->private;
row_p = (int *)stmt->private2;
if (*row_p >= f.PQntuples_fn(result) || *row_p < 0) {
csync_debug(1, "row index out of range (should be between 0 and %d, is %d)\n", *row_p, f.PQntuples_fn(result));
return NULL;
}
return f.PQgetvalue_fn(result, *row_p, column);
}
int db_postgres_stmt_get_column_int(db_stmt_p stmt, int column)
{
PGresult *result;
int *row_p;
if (!stmt || !stmt->private || !stmt->private2) {
return 0;
}
result = (PGresult *) stmt->private;
row_p = (int *)stmt->private2;
if (*row_p >= f.PQntuples_fn(result) || *row_p < 0) {
csync_debug(1, "row index out of range (should be between 0 and %d, is %d)\n", *row_p, f.PQntuples_fn(result));
return 0;
}
return atoi(f.PQgetvalue_fn(result, *row_p, column));
}
int db_postgres_stmt_next(db_stmt_p stmt)
{
PGresult *result;
int *row_p;
if (!stmt || !stmt->private || !stmt->private2) {
return 0;
}
result = (PGresult *) stmt->private;
row_p = (int *)stmt->private2;
(*row_p)++;
if (*row_p >= f.PQntuples_fn(result))
return DB_DONE;
return DB_ROW;
}
int db_postgres_stmt_close(db_stmt_p stmt)
{
PGresult *res = stmt->private;
f.PQclear_fn(res);
free(stmt->private2);
free(stmt);
return DB_OK;
}
int db_postgres_upgrade_to_schema(int version)
{
if (version < 0)
return DB_OK;
if (version > 0)
return DB_ERROR;
csync_debug(2, "Upgrading database schema to version %d.\n", version);
/* *INDENT-OFF* */
csync_db_sql("Creating action table",
"CREATE TABLE action ("
" filename TEXT NOT NULL,"
" command TEXT NOT NULL,"
" logfile TEXT NOT NULL,"
" UNIQUE (filename,command)"
");");
csync_db_sql("Creating dirty table",
"CREATE TABLE dirty ("
" filename TEXT NOT NULL,"
" forced INTEGER NOT NULL,"
" myname TEXT NOT NULL,"
" peername TEXT NOT NULL,"
" UNIQUE (filename,peername)"
");");
csync_db_sql("Creating file table",
"CREATE TABLE file ("
" filename TEXT NOT NULL,"
" checktxt TEXT NOT NULL,"
" UNIQUE (filename)"
");");
csync_db_sql("Creating hint table",
"CREATE TABLE hint ("
" filename TEXT NOT NULL,"
" recursive INTEGER NOT NULL"
");");
csync_db_sql("Creating x509_cert table",
"CREATE TABLE x509_cert ("
" peername TEXT NOT NULL,"
" certdata TEXT NOT NULL,"
" UNIQUE (peername)"
");");
/* *INDENT-ON* */
return DB_OK;
}
#endif /* HAVE_POSTGRES */