-
Notifications
You must be signed in to change notification settings - Fork 39
/
db_mysql.c
369 lines (315 loc) · 10 KB
/
db_mysql.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
/*
* Copyright (C) 2010 Dennis Schafroth <[email protected]>>
* Copyright (C) 2010 Johannes Thoma <[email protected]>
* 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_mysql.h"
#include "dl.h"
#ifdef HAVE_MYSQL
#include <mysql.h>
#include <mysqld_error.h>
static struct db_mysql_fns {
MYSQL *(*mysql_init_fn) (MYSQL *);
MYSQL *(*mysql_real_connect_fn) (MYSQL *, const char *, const char *, const char *, const char *,
unsigned int, const char *, unsigned long);
int (*mysql_errno_fn) (MYSQL *);
int (*mysql_query_fn) (MYSQL *, const char *);
void (*mysql_close_fn) (MYSQL *);
const char *(*mysql_error_fn) (MYSQL *);
MYSQL_RES *(*mysql_store_result_fn) (MYSQL *);
unsigned int (*mysql_num_fields_fn) (MYSQL_RES *);
MYSQL_ROW(*mysql_fetch_row_fn) (MYSQL_RES *);
void (*mysql_free_result_fn) (MYSQL_RES *);
unsigned int (*mysql_warning_count_fn) (MYSQL *);
} f;
static void *dl_handle;
static void db_mysql_dlopen(void)
{
csync_debug(2, "Opening shared library " LIBMYSQLCLIENT_SO "\n");
dl_handle = dlopen(LIBMYSQLCLIENT_SO, RTLD_LAZY);
if (dl_handle == NULL) {
csync_fatal
("Could not open " LIBMYSQLCLIENT_SO ": %s\n"
"Please install Mysql client library (libmysqlclient) or use other database (sqlite, postgres)\n",
dlerror());
}
csync_debug(2, "Reading symbols from shared library " LIBMYSQLCLIENT_SO "\n");
LOOKUP_SYMBOL(dl_handle, mysql_init);
LOOKUP_SYMBOL(dl_handle, mysql_real_connect);
LOOKUP_SYMBOL(dl_handle, mysql_errno);
LOOKUP_SYMBOL(dl_handle, mysql_query);
LOOKUP_SYMBOL(dl_handle, mysql_close);
LOOKUP_SYMBOL(dl_handle, mysql_error);
LOOKUP_SYMBOL(dl_handle, mysql_store_result);
LOOKUP_SYMBOL(dl_handle, mysql_num_fields);
LOOKUP_SYMBOL(dl_handle, mysql_fetch_row);
LOOKUP_SYMBOL(dl_handle, mysql_free_result);
LOOKUP_SYMBOL(dl_handle, mysql_warning_count);
}
int db_mysql_open(const char *file, db_conn_p * conn_p)
{
db_mysql_dlopen();
MYSQL *db = f.mysql_init_fn(0);
char *host, *user, *pass, *database;
unsigned int port = 0;
char *db_url = malloc(strlen(file) + 1);
char *create_database_statement;
if (db_url == NULL)
csync_fatal("No memory for db_url\n");
strcpy(db_url, file);
csync_parse_url(db_url, &host, &user, &pass, &database, &port);
if (f.mysql_real_connect_fn(db, host, user, pass, database, port, NULL, 0) == NULL) {
if (f.mysql_errno_fn(db) != ER_BAD_DB_ERROR)
goto fatal;
if (f.mysql_real_connect_fn(db, host, user, pass, NULL, port, NULL, 0) == NULL)
goto fatal;
ASPRINTF(&create_database_statement, "create database %s", database);
csync_debug(2, "creating database %s\n", database);
if (f.mysql_query_fn(db, create_database_statement) != 0)
csync_fatal("Cannot create database %s: Error: %s\n", database, f.mysql_error_fn(db));
free(create_database_statement);
f.mysql_close_fn(db);
db = f.mysql_init_fn(0);
if (f.mysql_real_connect_fn(db, host, user, pass, database, port, NULL, 0) == NULL)
goto fatal;
}
db_conn_p conn = calloc(1, sizeof(*conn));
if (conn == NULL)
return DB_ERROR;
*conn_p = conn;
conn->private = db;
conn->close = db_mysql_close;
conn->exec = db_mysql_exec;
conn->prepare = db_mysql_prepare;
conn->errmsg = db_mysql_errmsg;
conn->upgrade_to_schema = db_mysql_upgrade_to_schema;
return DB_OK;
fatal:
csync_fatal("Failed to connect to database: Error: %s\n", f.mysql_error_fn(db));
return DB_ERROR;
}
void db_mysql_close(db_conn_p conn)
{
if (!conn)
return;
if (!conn->private)
return;
f.mysql_close_fn(conn->private);
conn->private = 0;
}
const char *db_mysql_errmsg(db_conn_p conn)
{
if (!conn)
return "(no connection)";
if (!conn->private)
return "(no private data in conn)";
return f.mysql_error_fn(conn->private);
}
static void print_warnings(int level, MYSQL * m)
{
int rc;
MYSQL_RES *res;
int fields;
MYSQL_ROW row;
if (m == NULL)
csync_fatal("print_warnings: m is NULL");
rc = f.mysql_query_fn(m, "SHOW WARNINGS");
if (rc != 0)
csync_fatal("print_warnings: Failed to get warning messages");
res = f.mysql_store_result_fn(m);
if (res == NULL)
csync_fatal("print_warnings: Failed to get result set for warning messages");
fields = f.mysql_num_fields_fn(res);
if (fields < 2)
csync_fatal("print_warnings: Strange: show warnings result set has less than 2 rows");
row = f.mysql_fetch_row_fn(res);
while (row) {
csync_debug(level, "MySql Warning: %s\n", row[2]);
row = f.mysql_fetch_row_fn(res);
}
f.mysql_free_result_fn(res);
}
int db_mysql_exec(db_conn_p conn, const char *sql)
{
int rc = DB_ERROR;
if (!conn)
return DB_NO_CONNECTION;
if (!conn->private) {
/* added error element */
return DB_NO_CONNECTION_REAL;
}
rc = f.mysql_query_fn(conn->private, sql);
/* Treat warnings as errors.
* For example when a column is too short this should be an error. */
if (f.mysql_warning_count_fn(conn->private) > 0) {
print_warnings(1, conn->private);
return DB_ERROR;
}
/* On error parse, create DB ERROR element */
return rc;
}
int db_mysql_prepare(db_conn_p conn, const char *sql, db_stmt_p * stmt_p, char **pptail)
{
*stmt_p = NULL;
if (!conn)
return DB_NO_CONNECTION;
if (!conn->private) {
/* added error element */
return DB_NO_CONNECTION_REAL;
}
db_stmt_p stmt = malloc(sizeof(*stmt));
/* TODO avoid strlen, use configurable limit? */
f.mysql_query_fn(conn->private, sql);
if (f.mysql_warning_count_fn(conn->private) > 0) {
print_warnings(1, conn->private);
return DB_ERROR;
}
MYSQL_RES *mysql_stmt = f.mysql_store_result_fn(conn->private);
if (mysql_stmt == NULL) {
csync_debug(2, "Error in mysql_store_result: %s\n", f.mysql_error_fn(conn->private));
return DB_ERROR;
}
if (f.mysql_warning_count_fn(conn->private) > 0) {
print_warnings(1, conn->private);
return DB_ERROR;
}
stmt->private = mysql_stmt;
/* TODO error mapping / handling */
*stmt_p = stmt;
stmt->get_column_text = db_mysql_stmt_get_column_text;
stmt->get_column_blob = db_mysql_stmt_get_column_blob;
stmt->get_column_int = db_mysql_stmt_get_column_int;
stmt->next = db_mysql_stmt_next;
stmt->close = db_mysql_stmt_close;
stmt->db = conn;
return DB_OK;
}
const void *db_mysql_stmt_get_column_blob(db_stmt_p stmt, int column)
{
if (!stmt || !stmt->private2) {
return 0;
}
MYSQL_ROW row = stmt->private2;
return row[column];
}
const char *db_mysql_stmt_get_column_text(db_stmt_p stmt, int column)
{
if (!stmt || !stmt->private2) {
return 0;
}
MYSQL_ROW row = stmt->private2;
return row[column];
}
int db_mysql_stmt_get_column_int(db_stmt_p stmt, int column)
{
const char *value = db_mysql_stmt_get_column_text(stmt, column);
if (value)
return atoi(value);
/* error mapping */
return 0;
}
int db_mysql_stmt_next(db_stmt_p stmt)
{
MYSQL_RES *mysql_stmt = stmt->private;
stmt->private2 = f.mysql_fetch_row_fn(mysql_stmt);
/* error mapping */
if (stmt->private2)
return DB_ROW;
return DB_DONE;
}
int db_mysql_stmt_close(db_stmt_p stmt)
{
MYSQL_RES *mysql_stmt = stmt->private;
f.mysql_free_result_fn(mysql_stmt);
free(stmt);
return DB_OK;
}
/* NOTE:
* NI_MAXHOST is 1025. That should be plenty for typical hostnames.
*
* Use TEXT fields:
* PATH_MAX is typically 4096, and that is assumed elsewhere in the code as
* well. But as all filenames (and other interesting fields) are stored as
* transformed by url_encode(), they can be three times as long.
* checktxt may be very long, if it stores the target of a very long symlink.
*
* The INTEGER are actually used as boolean flags.
*
* prefix limit MyISAM: 1000 bytes, InnoDB: 767 bytes.
* Which is also the max key length.
* utf8: up to 3 bytes per "character" --> sum of key prefix lengths: 767/3 = 255
*
* We must not define UNIQUE keys on prefixes,
* or we would not be able to handle long path names.
* We should be able to get away with just "key",
* typically the code does "delete from" before "insert into" anyways.
* */
int db_mysql_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,"
" KEY filename (filename(255)),"
" KEY command (command(255))"
");");
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,"
" KEY filename (filename(255)),"
" KEY dirty_host (peername(255))"
");");
csync_db_sql("Creating file table",
"CREATE TABLE file ("
" filename TEXT NOT NULL,"
" checktxt TEXT NOT NULL,"
" KEY filename (filename(255))"
");");
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,"
" KEY peername (peername(255))"
");");
/* *INDENT-ON* */
return DB_OK;
}
#endif