aboutsummaryrefslogtreecommitdiff
path: root/src/mysql
diff options
context:
space:
mode:
authorng0 <ng0@n0.is>2019-05-14 17:40:58 +0000
committerng0 <ng0@n0.is>2019-05-14 17:40:58 +0000
commit1a9adcbf6a1983837a746f8a49e26adcbb905a11 (patch)
treeb1c18ed1e41ce5e270670515079a8b9c2b688fb2 /src/mysql
parentb7f868185f05ab2a8a8fee77a354a766c81755b2 (diff)
downloadgnunet-1a9adcbf6a1983837a746f8a49e26adcbb905a11.tar.gz
gnunet-1a9adcbf6a1983837a746f8a49e26adcbb905a11.zip
mysql.c: clang-format indent
Diffstat (limited to 'src/mysql')
-rw-r--r--src/mysql/mysql.c96
1 files changed, 64 insertions, 32 deletions
diff --git a/src/mysql/mysql.c b/src/mysql/mysql.c
index 5abc7bdf0..652d7e7d8 100644
--- a/src/mysql/mysql.c
+++ b/src/mysql/mysql.c
@@ -39,14 +39,35 @@
39 * a failure of the command 'cmd' with the message given 39 * a failure of the command 'cmd' with the message given
40 * by strerror(errno). 40 * by strerror(errno).
41 */ 41 */
42#define DIE_MYSQL(cmd, dbh) do { GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "mysql", _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, mysql_error((dbh)->dbf)); GNUNET_assert (0); } while(0); 42#define DIE_MYSQL(cmd, dbh) \
43 do \
44 { \
45 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, \
46 "mysql", \
47 _ ("`%s' failed at %s:%d with error: %s\n"), \
48 cmd, \
49 __FILE__, \
50 __LINE__, \
51 mysql_error ((dbh)->dbf)); \
52 GNUNET_assert (0); \
53 } while (0);
43 54
44/** 55/**
45 * Log an error message at log-level 'level' that indicates 56 * Log an error message at log-level 'level' that indicates
46 * a failure of the command 'cmd' on file 'filename' 57 * a failure of the command 'cmd' on file 'filename'
47 * with the message given by strerror(errno). 58 * with the message given by strerror(errno).
48 */ 59 */
49#define LOG_MYSQL(level, cmd, dbh) do { GNUNET_log_from (level, "mysql", _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, mysql_error((dbh)->dbf)); } while(0); 60#define LOG_MYSQL(level, cmd, dbh) \
61 do \
62 { \
63 GNUNET_log_from (level, \
64 "mysql", \
65 _ ("`%s' failed at %s:%d with error: %s\n"), \
66 cmd, \
67 __FILE__, \
68 __LINE__, \
69 mysql_error ((dbh)->dbf)); \
70 } while (0);
50 71
51 72
52/** 73/**
@@ -84,7 +105,6 @@ struct GNUNET_MYSQL_Context
84 * Filename of "my.cnf" (msyql configuration). 105 * Filename of "my.cnf" (msyql configuration).
85 */ 106 */
86 char *cnffile; 107 char *cnffile;
87
88}; 108};
89 109
90 110
@@ -123,7 +143,6 @@ struct GNUNET_MYSQL_StatementHandle
123 * Is the MySQL prepared statement valid, or do we need to re-initialize it? 143 * Is the MySQL prepared statement valid, or do we need to re-initialize it?
124 */ 144 */
125 int valid; 145 int valid;
126
127}; 146};
128 147
129 148
@@ -149,7 +168,7 @@ get_my_cnf_path (const struct GNUNET_CONFIGURATION_Handle *cfg,
149 168
150#ifndef WINDOWS 169#ifndef WINDOWS
151 pw = getpwuid (getuid ()); 170 pw = getpwuid (getuid ());
152 if (!pw) 171 if (! pw)
153 { 172 {
154 GNUNET_log_from_strerror (GNUNET_ERROR_TYPE_ERROR, "mysql", "getpwuid"); 173 GNUNET_log_from_strerror (GNUNET_ERROR_TYPE_ERROR, "mysql", "getpwuid");
155 return NULL; 174 return NULL;
@@ -157,7 +176,8 @@ get_my_cnf_path (const struct GNUNET_CONFIGURATION_Handle *cfg,
157 if (GNUNET_YES == GNUNET_CONFIGURATION_have_value (cfg, section, "CONFIG")) 176 if (GNUNET_YES == GNUNET_CONFIGURATION_have_value (cfg, section, "CONFIG"))
158 { 177 {
159 GNUNET_assert (GNUNET_OK == 178 GNUNET_assert (GNUNET_OK ==
160 GNUNET_CONFIGURATION_get_value_filename (cfg, section, 179 GNUNET_CONFIGURATION_get_value_filename (cfg,
180 section,
161 "CONFIG", 181 "CONFIG",
162 &cnffile)); 182 &cnffile));
163 configured = GNUNET_YES; 183 configured = GNUNET_YES;
@@ -176,15 +196,18 @@ get_my_cnf_path (const struct GNUNET_CONFIGURATION_Handle *cfg,
176 GNUNET_free (home_dir); 196 GNUNET_free (home_dir);
177 configured = GNUNET_NO; 197 configured = GNUNET_NO;
178#endif 198#endif
179 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "mysql", 199 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
180 _("Trying to use file `%s' for MySQL configuration.\n"), 200 "mysql",
201 _ ("Trying to use file `%s' for MySQL configuration.\n"),
181 cnffile); 202 cnffile);
182 if ((0 != STAT (cnffile, &st)) || (0 != ACCESS (cnffile, R_OK)) || 203 if ((0 != STAT (cnffile, &st)) || (0 != ACCESS (cnffile, R_OK)) ||
183 (!S_ISREG (st.st_mode))) 204 (! S_ISREG (st.st_mode)))
184 { 205 {
185 if (configured == GNUNET_YES) 206 if (configured == GNUNET_YES)
186 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "mysql", 207 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
187 _("Could not access file `%s': %s\n"), cnffile, 208 "mysql",
209 _ ("Could not access file `%s': %s\n"),
210 cnffile,
188 STRERROR (errno)); 211 STRERROR (errno));
189 GNUNET_free (cnffile); 212 GNUNET_free (cnffile);
190 return NULL; 213 return NULL;
@@ -221,14 +244,15 @@ iopen (struct GNUNET_MYSQL_Context *mc)
221 mysql_options (mc->dbf, MYSQL_OPT_RECONNECT, &reconnect); 244 mysql_options (mc->dbf, MYSQL_OPT_RECONNECT, &reconnect);
222 mysql_options (mc->dbf, MYSQL_OPT_CONNECT_TIMEOUT, (const void *) &timeout); 245 mysql_options (mc->dbf, MYSQL_OPT_CONNECT_TIMEOUT, (const void *) &timeout);
223 mysql_options (mc->dbf, MYSQL_SET_CHARSET_NAME, "UTF8"); 246 mysql_options (mc->dbf, MYSQL_SET_CHARSET_NAME, "UTF8");
224 timeout = 60; /* in seconds */ 247 timeout = 60; /* in seconds */
225 mysql_options (mc->dbf, MYSQL_OPT_READ_TIMEOUT, (const void *) &timeout); 248 mysql_options (mc->dbf, MYSQL_OPT_READ_TIMEOUT, (const void *) &timeout);
226 mysql_options (mc->dbf, MYSQL_OPT_WRITE_TIMEOUT, (const void *) &timeout); 249 mysql_options (mc->dbf, MYSQL_OPT_WRITE_TIMEOUT, (const void *) &timeout);
227 mysql_dbname = NULL; 250 mysql_dbname = NULL;
228 if (GNUNET_YES == 251 if (GNUNET_YES ==
229 GNUNET_CONFIGURATION_have_value (mc->cfg, mc->section, "DATABASE")) 252 GNUNET_CONFIGURATION_have_value (mc->cfg, mc->section, "DATABASE"))
230 GNUNET_assert (GNUNET_OK == 253 GNUNET_assert (GNUNET_OK ==
231 GNUNET_CONFIGURATION_get_value_string (mc->cfg, mc->section, 254 GNUNET_CONFIGURATION_get_value_string (mc->cfg,
255 mc->section,
232 "DATABASE", 256 "DATABASE",
233 &mysql_dbname)); 257 &mysql_dbname));
234 else 258 else
@@ -238,15 +262,18 @@ iopen (struct GNUNET_MYSQL_Context *mc)
238 GNUNET_CONFIGURATION_have_value (mc->cfg, mc->section, "USER")) 262 GNUNET_CONFIGURATION_have_value (mc->cfg, mc->section, "USER"))
239 { 263 {
240 GNUNET_assert (GNUNET_OK == 264 GNUNET_assert (GNUNET_OK ==
241 GNUNET_CONFIGURATION_get_value_string (mc->cfg, mc->section, 265 GNUNET_CONFIGURATION_get_value_string (mc->cfg,
242 "USER", &mysql_user)); 266 mc->section,
267 "USER",
268 &mysql_user));
243 } 269 }
244 mysql_password = NULL; 270 mysql_password = NULL;
245 if (GNUNET_YES == 271 if (GNUNET_YES ==
246 GNUNET_CONFIGURATION_have_value (mc->cfg, mc->section, "PASSWORD")) 272 GNUNET_CONFIGURATION_have_value (mc->cfg, mc->section, "PASSWORD"))
247 { 273 {
248 GNUNET_assert (GNUNET_OK == 274 GNUNET_assert (GNUNET_OK ==
249 GNUNET_CONFIGURATION_get_value_string (mc->cfg, mc->section, 275 GNUNET_CONFIGURATION_get_value_string (mc->cfg,
276 mc->section,
250 "PASSWORD", 277 "PASSWORD",
251 &mysql_password)); 278 &mysql_password));
252 } 279 }
@@ -255,7 +282,8 @@ iopen (struct GNUNET_MYSQL_Context *mc)
255 GNUNET_CONFIGURATION_have_value (mc->cfg, mc->section, "HOST")) 282 GNUNET_CONFIGURATION_have_value (mc->cfg, mc->section, "HOST"))
256 { 283 {
257 GNUNET_assert (GNUNET_OK == 284 GNUNET_assert (GNUNET_OK ==
258 GNUNET_CONFIGURATION_get_value_string (mc->cfg, mc->section, 285 GNUNET_CONFIGURATION_get_value_string (mc->cfg,
286 mc->section,
259 "HOST", 287 "HOST",
260 &mysql_server)); 288 &mysql_server));
261 } 289 }
@@ -264,13 +292,20 @@ iopen (struct GNUNET_MYSQL_Context *mc)
264 GNUNET_CONFIGURATION_have_value (mc->cfg, mc->section, "PORT")) 292 GNUNET_CONFIGURATION_have_value (mc->cfg, mc->section, "PORT"))
265 { 293 {
266 GNUNET_assert (GNUNET_OK == 294 GNUNET_assert (GNUNET_OK ==
267 GNUNET_CONFIGURATION_get_value_number (mc->cfg, mc->section, 295 GNUNET_CONFIGURATION_get_value_number (mc->cfg,
268 "PORT", &mysql_port)); 296 mc->section,
297 "PORT",
298 &mysql_port));
269 } 299 }
270 300
271 GNUNET_assert (mysql_dbname != NULL); 301 GNUNET_assert (mysql_dbname != NULL);
272 mysql_real_connect (mc->dbf, mysql_server, mysql_user, mysql_password, 302 mysql_real_connect (mc->dbf,
273 mysql_dbname, (unsigned int) mysql_port, NULL, 303 mysql_server,
304 mysql_user,
305 mysql_password,
306 mysql_dbname,
307 (unsigned int) mysql_port,
308 NULL,
274 CLIENT_IGNORE_SIGPIPE); 309 CLIENT_IGNORE_SIGPIPE);
275 GNUNET_free_non_null (mysql_server); 310 GNUNET_free_non_null (mysql_server);
276 GNUNET_free_non_null (mysql_user); 311 GNUNET_free_non_null (mysql_user);
@@ -301,8 +336,7 @@ GNUNET_MYSQL_context_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
301 mc = GNUNET_new (struct GNUNET_MYSQL_Context); 336 mc = GNUNET_new (struct GNUNET_MYSQL_Context);
302 mc->cfg = cfg; 337 mc->cfg = cfg;
303 mc->section = section; 338 mc->section = section;
304 mc->cnffile = get_my_cnf_path (cfg, 339 mc->cnffile = get_my_cnf_path (cfg, section);
305 section);
306 340
307 return mc; 341 return mc;
308} 342}
@@ -389,18 +423,14 @@ GNUNET_MYSQL_statement_prepare (struct GNUNET_MYSQL_Context *mc,
389 * #GNUNET_SYSERR if there was a problem 423 * #GNUNET_SYSERR if there was a problem
390 */ 424 */
391int 425int
392GNUNET_MYSQL_statement_run (struct GNUNET_MYSQL_Context *mc, 426GNUNET_MYSQL_statement_run (struct GNUNET_MYSQL_Context *mc, const char *sql)
393 const char *sql)
394{ 427{
395 if ( (NULL == mc->dbf) && 428 if ((NULL == mc->dbf) && (GNUNET_OK != iopen (mc)))
396 (GNUNET_OK != iopen (mc)) )
397 return GNUNET_SYSERR; 429 return GNUNET_SYSERR;
398 mysql_query (mc->dbf, sql); 430 mysql_query (mc->dbf, sql);
399 if (mysql_error (mc->dbf)[0]) 431 if (mysql_error (mc->dbf)[0])
400 { 432 {
401 LOG_MYSQL (GNUNET_ERROR_TYPE_ERROR, 433 LOG_MYSQL (GNUNET_ERROR_TYPE_ERROR, "mysql_query", mc);
402 "mysql_query",
403 mc);
404 GNUNET_MYSQL_statements_invalidate (mc); 434 GNUNET_MYSQL_statements_invalidate (mc);
405 return GNUNET_SYSERR; 435 return GNUNET_SYSERR;
406 } 436 }
@@ -432,8 +462,10 @@ prepare_statement (struct GNUNET_MYSQL_StatementHandle *sh)
432 } 462 }
433 if (0 != mysql_stmt_prepare (sh->statement, sh->query, strlen (sh->query))) 463 if (0 != mysql_stmt_prepare (sh->statement, sh->query, strlen (sh->query)))
434 { 464 {
435 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "mysql", 465 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
436 "prepare_statement: %s\n", sh->query); 466 "mysql",
467 "prepare_statement: %s\n",
468 sh->query);
437 LOG_MYSQL (GNUNET_ERROR_TYPE_ERROR, "mysql_stmt_prepare", mc); 469 LOG_MYSQL (GNUNET_ERROR_TYPE_ERROR, "mysql_stmt_prepare", mc);
438 mysql_stmt_close (sh->statement); 470 mysql_stmt_close (sh->statement);
439 sh->statement = NULL; 471 sh->statement = NULL;