aboutsummaryrefslogtreecommitdiff
path: root/src/peerstore/plugin_peerstore_sqlite.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2019-10-05 15:09:28 +0200
committerChristian Grothoff <christian@grothoff.org>2019-10-05 15:09:28 +0200
commitc4e9ba925ffd758aaa3feee2ccfc0b76f26fe207 (patch)
treecac3ce030d77b4cbe7c7dc62ed58cfe6d24f73e1 /src/peerstore/plugin_peerstore_sqlite.c
parentfbb71d527c7d6babf269a8fefce1db291b9f7068 (diff)
downloadgnunet-c4e9ba925ffd758aaa3feee2ccfc0b76f26fe207.tar.gz
gnunet-c4e9ba925ffd758aaa3feee2ccfc0b76f26fe207.zip
global reindent, now with uncrustify hook enabled
Diffstat (limited to 'src/peerstore/plugin_peerstore_sqlite.c')
-rw-r--r--src/peerstore/plugin_peerstore_sqlite.c704
1 files changed, 356 insertions, 348 deletions
diff --git a/src/peerstore/plugin_peerstore_sqlite.c b/src/peerstore/plugin_peerstore_sqlite.c
index 5f6617ebc..51cc651a2 100644
--- a/src/peerstore/plugin_peerstore_sqlite.c
+++ b/src/peerstore/plugin_peerstore_sqlite.c
@@ -49,14 +49,22 @@
49 * a failure of the command 'cmd' on file 'filename' 49 * a failure of the command 'cmd' on file 'filename'
50 * with the message given by strerror(errno). 50 * with the message given by strerror(errno).
51 */ 51 */
52#define LOG_SQLITE(db, level, cmd) do { GNUNET_log_from(level, "peerstore-sqlite", _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, sqlite3_errmsg(db->dbh)); } while (0) 52#define LOG_SQLITE(db, level, cmd) do { GNUNET_log_from (level, \
53 "peerstore-sqlite", _ ( \
54 "`%s' failed at %s:%d with error: %s\n"), \
55 cmd, \
56 __FILE__, __LINE__, \
57 sqlite3_errmsg ( \
58 db->dbh)); \
59} while (0)
53 60
54#define LOG(kind, ...) GNUNET_log_from(kind, "peerstore-sqlite", __VA_ARGS__) 61#define LOG(kind, ...) GNUNET_log_from (kind, "peerstore-sqlite", __VA_ARGS__)
55 62
56/** 63/**
57 * Context for all functions in this plugin. 64 * Context for all functions in this plugin.
58 */ 65 */
59struct Plugin { 66struct Plugin
67{
60 /** 68 /**
61 * Configuration handle 69 * Configuration handle
62 */ 70 */
@@ -121,46 +129,46 @@ struct Plugin {
121 * @return number of deleted records, #GNUNE_SYSERR on error 129 * @return number of deleted records, #GNUNE_SYSERR on error
122 */ 130 */
123static int 131static int
124peerstore_sqlite_delete_records(void *cls, 132peerstore_sqlite_delete_records (void *cls,
125 const char *sub_system, 133 const char *sub_system,
126 const struct GNUNET_PeerIdentity *peer, 134 const struct GNUNET_PeerIdentity *peer,
127 const char *key) 135 const char *key)
128{ 136{
129 struct Plugin *plugin = cls; 137 struct Plugin *plugin = cls;
130 sqlite3_stmt *stmt = plugin->delete_peerstoredata; 138 sqlite3_stmt *stmt = plugin->delete_peerstoredata;
131 struct GNUNET_SQ_QueryParam params[] = { 139 struct GNUNET_SQ_QueryParam params[] = {
132 GNUNET_SQ_query_param_string(sub_system), 140 GNUNET_SQ_query_param_string (sub_system),
133 GNUNET_SQ_query_param_auto_from_type(peer), 141 GNUNET_SQ_query_param_auto_from_type (peer),
134 GNUNET_SQ_query_param_string(key), 142 GNUNET_SQ_query_param_string (key),
135 GNUNET_SQ_query_param_end 143 GNUNET_SQ_query_param_end
136 }; 144 };
137 int ret; 145 int ret;
138 146
139 if (GNUNET_OK != 147 if (GNUNET_OK !=
140 GNUNET_SQ_bind(stmt, 148 GNUNET_SQ_bind (stmt,
141 params)) 149 params))
142 { 150 {
143 LOG_SQLITE(plugin, 151 LOG_SQLITE (plugin,
144 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 152 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
145 "sqlite3_bind"); 153 "sqlite3_bind");
146 GNUNET_SQ_reset(plugin->dbh, 154 GNUNET_SQ_reset (plugin->dbh,
147 stmt); 155 stmt);
148 return GNUNET_SYSERR; 156 return GNUNET_SYSERR;
149 } 157 }
150 if (SQLITE_DONE != 158 if (SQLITE_DONE !=
151 sqlite3_step(stmt)) 159 sqlite3_step (stmt))
152 { 160 {
153 LOG_SQLITE(plugin, 161 LOG_SQLITE (plugin,
154 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 162 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
155 "sqlite3_step"); 163 "sqlite3_step");
156 ret = GNUNET_SYSERR; 164 ret = GNUNET_SYSERR;
157 } 165 }
158 else 166 else
159 { 167 {
160 ret = sqlite3_changes(plugin->dbh); 168 ret = sqlite3_changes (plugin->dbh);
161 } 169 }
162 GNUNET_SQ_reset(plugin->dbh, 170 GNUNET_SQ_reset (plugin->dbh,
163 stmt); 171 stmt);
164 return ret; 172 return ret;
165} 173}
166 174
@@ -176,42 +184,42 @@ peerstore_sqlite_delete_records(void *cls,
176 * called 184 * called
177 */ 185 */
178static int 186static int
179peerstore_sqlite_expire_records(void *cls, struct GNUNET_TIME_Absolute now, 187peerstore_sqlite_expire_records (void *cls, struct GNUNET_TIME_Absolute now,
180 GNUNET_PEERSTORE_Continuation cont, 188 GNUNET_PEERSTORE_Continuation cont,
181 void *cont_cls) 189 void *cont_cls)
182{ 190{
183 struct Plugin *plugin = cls; 191 struct Plugin *plugin = cls;
184 sqlite3_stmt *stmt = plugin->expire_peerstoredata; 192 sqlite3_stmt *stmt = plugin->expire_peerstoredata;
185 struct GNUNET_SQ_QueryParam params[] = { 193 struct GNUNET_SQ_QueryParam params[] = {
186 GNUNET_SQ_query_param_absolute_time(&now), 194 GNUNET_SQ_query_param_absolute_time (&now),
187 GNUNET_SQ_query_param_end 195 GNUNET_SQ_query_param_end
188 }; 196 };
189 197
190 if (GNUNET_OK != 198 if (GNUNET_OK !=
191 GNUNET_SQ_bind(stmt, 199 GNUNET_SQ_bind (stmt,
192 params)) 200 params))
193 { 201 {
194 LOG_SQLITE(plugin, 202 LOG_SQLITE (plugin,
195 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 203 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
196 "sqlite3_bind"); 204 "sqlite3_bind");
197 GNUNET_SQ_reset(plugin->dbh, 205 GNUNET_SQ_reset (plugin->dbh,
198 stmt); 206 stmt);
199 return GNUNET_SYSERR; 207 return GNUNET_SYSERR;
200 } 208 }
201 if (SQLITE_DONE != sqlite3_step(stmt)) 209 if (SQLITE_DONE != sqlite3_step (stmt))
202 { 210 {
203 LOG_SQLITE(plugin, 211 LOG_SQLITE (plugin,
204 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 212 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
205 "sqlite3_step"); 213 "sqlite3_step");
206 GNUNET_SQ_reset(plugin->dbh, 214 GNUNET_SQ_reset (plugin->dbh,
207 stmt); 215 stmt);
208 return GNUNET_SYSERR; 216 return GNUNET_SYSERR;
209 } 217 }
210 if (NULL != cont) 218 if (NULL != cont)
211 cont(cont_cls, 219 cont (cont_cls,
212 sqlite3_changes(plugin->dbh)); 220 sqlite3_changes (plugin->dbh));
213 GNUNET_SQ_reset(plugin->dbh, 221 GNUNET_SQ_reset (plugin->dbh,
214 stmt); 222 stmt);
215 return GNUNET_OK; 223 return GNUNET_OK;
216} 224}
217 225
@@ -231,12 +239,12 @@ peerstore_sqlite_expire_records(void *cls, struct GNUNET_TIME_Absolute now,
231 * called 239 * called
232 */ 240 */
233static int 241static int
234peerstore_sqlite_iterate_records(void *cls, 242peerstore_sqlite_iterate_records (void *cls,
235 const char *sub_system, 243 const char *sub_system,
236 const struct GNUNET_PeerIdentity *peer, 244 const struct GNUNET_PeerIdentity *peer,
237 const char *key, 245 const char *key,
238 GNUNET_PEERSTORE_Processor iter, 246 GNUNET_PEERSTORE_Processor iter,
239 void *iter_cls) 247 void *iter_cls)
240{ 248{
241 struct Plugin *plugin = cls; 249 struct Plugin *plugin = cls;
242 sqlite3_stmt *stmt; 250 sqlite3_stmt *stmt;
@@ -244,113 +252,113 @@ peerstore_sqlite_iterate_records(void *cls,
244 int sret; 252 int sret;
245 struct GNUNET_PEERSTORE_Record rec; 253 struct GNUNET_PEERSTORE_Record rec;
246 254
247 LOG(GNUNET_ERROR_TYPE_DEBUG, 255 LOG (GNUNET_ERROR_TYPE_DEBUG,
248 "Executing iterate request on sqlite db.\n"); 256 "Executing iterate request on sqlite db.\n");
249 if (NULL == peer) 257 if (NULL == peer)
258 {
259 if (NULL == key)
250 { 260 {
251 if (NULL == key) 261 struct GNUNET_SQ_QueryParam params[] = {
252 { 262 GNUNET_SQ_query_param_string (sub_system),
253 struct GNUNET_SQ_QueryParam params[] = { 263 GNUNET_SQ_query_param_end
254 GNUNET_SQ_query_param_string(sub_system), 264 };
255 GNUNET_SQ_query_param_end 265
256 }; 266 stmt = plugin->select_peerstoredata;
257 267 err = GNUNET_SQ_bind (stmt,
258 stmt = plugin->select_peerstoredata; 268 params);
259 err = GNUNET_SQ_bind(stmt,
260 params);
261 }
262 else
263 {
264 struct GNUNET_SQ_QueryParam params[] = {
265 GNUNET_SQ_query_param_string(sub_system),
266 GNUNET_SQ_query_param_string(key),
267 GNUNET_SQ_query_param_end
268 };
269
270 stmt = plugin->select_peerstoredata_by_key;
271 err = GNUNET_SQ_bind(stmt,
272 params);
273 }
274 } 269 }
275 else 270 else
276 { 271 {
277 if (NULL == key) 272 struct GNUNET_SQ_QueryParam params[] = {
278 { 273 GNUNET_SQ_query_param_string (sub_system),
279 struct GNUNET_SQ_QueryParam params[] = { 274 GNUNET_SQ_query_param_string (key),
280 GNUNET_SQ_query_param_string(sub_system), 275 GNUNET_SQ_query_param_end
281 GNUNET_SQ_query_param_auto_from_type(peer), 276 };
282 GNUNET_SQ_query_param_end
283 };
284
285 stmt = plugin->select_peerstoredata_by_pid;
286 err = GNUNET_SQ_bind(stmt,
287 params);
288 }
289 else
290 {
291 struct GNUNET_SQ_QueryParam params[] = {
292 GNUNET_SQ_query_param_string(sub_system),
293 GNUNET_SQ_query_param_auto_from_type(peer),
294 GNUNET_SQ_query_param_string(key),
295 GNUNET_SQ_query_param_end
296 };
297
298 stmt = plugin->select_peerstoredata_by_all;
299 err = GNUNET_SQ_bind(stmt,
300 params);
301 }
302 }
303 277
304 if (GNUNET_OK != err) 278 stmt = plugin->select_peerstoredata_by_key;
305 { 279 err = GNUNET_SQ_bind (stmt,
306 LOG_SQLITE(plugin, 280 params);
307 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
308 "sqlite3_bind_XXXX");
309 GNUNET_SQ_reset(plugin->dbh,
310 stmt);
311 return GNUNET_SYSERR;
312 } 281 }
282 }
283 else
284 {
285 if (NULL == key)
286 {
287 struct GNUNET_SQ_QueryParam params[] = {
288 GNUNET_SQ_query_param_string (sub_system),
289 GNUNET_SQ_query_param_auto_from_type (peer),
290 GNUNET_SQ_query_param_end
291 };
313 292
314 err = 0; 293 stmt = plugin->select_peerstoredata_by_pid;
315 while (SQLITE_ROW == (sret = sqlite3_step(stmt))) 294 err = GNUNET_SQ_bind (stmt,
295 params);
296 }
297 else
316 { 298 {
317 LOG(GNUNET_ERROR_TYPE_DEBUG, 299 struct GNUNET_SQ_QueryParam params[] = {
318 "Returning a matched record.\n"); 300 GNUNET_SQ_query_param_string (sub_system),
319 struct GNUNET_SQ_ResultSpec rs[] = { 301 GNUNET_SQ_query_param_auto_from_type (peer),
320 GNUNET_SQ_result_spec_string(&rec.sub_system), 302 GNUNET_SQ_query_param_string (key),
321 GNUNET_SQ_result_spec_auto_from_type(&rec.peer), 303 GNUNET_SQ_query_param_end
322 GNUNET_SQ_result_spec_string(&rec.key),
323 GNUNET_SQ_result_spec_variable_size(&rec.value, &rec.value_size),
324 GNUNET_SQ_result_spec_absolute_time(&rec.expiry),
325 GNUNET_SQ_result_spec_end
326 }; 304 };
327 305
328 if (GNUNET_OK != 306 stmt = plugin->select_peerstoredata_by_all;
329 GNUNET_SQ_extract_result(stmt, 307 err = GNUNET_SQ_bind (stmt,
330 rs)) 308 params);
331 {
332 GNUNET_break(0);
333 break;
334 }
335 if (NULL != iter)
336 iter(iter_cls,
337 &rec,
338 NULL);
339 GNUNET_SQ_cleanup_result(rs);
340 } 309 }
341 if (SQLITE_DONE != sret) 310 }
311
312 if (GNUNET_OK != err)
313 {
314 LOG_SQLITE (plugin,
315 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
316 "sqlite3_bind_XXXX");
317 GNUNET_SQ_reset (plugin->dbh,
318 stmt);
319 return GNUNET_SYSERR;
320 }
321
322 err = 0;
323 while (SQLITE_ROW == (sret = sqlite3_step (stmt)))
324 {
325 LOG (GNUNET_ERROR_TYPE_DEBUG,
326 "Returning a matched record.\n");
327 struct GNUNET_SQ_ResultSpec rs[] = {
328 GNUNET_SQ_result_spec_string (&rec.sub_system),
329 GNUNET_SQ_result_spec_auto_from_type (&rec.peer),
330 GNUNET_SQ_result_spec_string (&rec.key),
331 GNUNET_SQ_result_spec_variable_size (&rec.value, &rec.value_size),
332 GNUNET_SQ_result_spec_absolute_time (&rec.expiry),
333 GNUNET_SQ_result_spec_end
334 };
335
336 if (GNUNET_OK !=
337 GNUNET_SQ_extract_result (stmt,
338 rs))
342 { 339 {
343 LOG_SQLITE(plugin, 340 GNUNET_break (0);
344 GNUNET_ERROR_TYPE_ERROR, 341 break;
345 "sqlite_step");
346 err = 1;
347 } 342 }
348 GNUNET_SQ_reset(plugin->dbh, 343 if (NULL != iter)
349 stmt); 344 iter (iter_cls,
345 &rec,
346 NULL);
347 GNUNET_SQ_cleanup_result (rs);
348 }
349 if (SQLITE_DONE != sret)
350 {
351 LOG_SQLITE (plugin,
352 GNUNET_ERROR_TYPE_ERROR,
353 "sqlite_step");
354 err = 1;
355 }
356 GNUNET_SQ_reset (plugin->dbh,
357 stmt);
350 if (NULL != iter) 358 if (NULL != iter)
351 iter(iter_cls, 359 iter (iter_cls,
352 NULL, 360 NULL,
353 err ? "sqlite error" : NULL); 361 err ? "sqlite error" : NULL);
354 return GNUNET_OK; 362 return GNUNET_OK;
355} 363}
356 364
@@ -373,52 +381,52 @@ peerstore_sqlite_iterate_records(void *cls,
373 * @return #GNUNET_OK on success, else #GNUNET_SYSERR and cont is not called 381 * @return #GNUNET_OK on success, else #GNUNET_SYSERR and cont is not called
374 */ 382 */
375static int 383static int
376peerstore_sqlite_store_record(void *cls, 384peerstore_sqlite_store_record (void *cls,
377 const char *sub_system, 385 const char *sub_system,
378 const struct GNUNET_PeerIdentity *peer, 386 const struct GNUNET_PeerIdentity *peer,
379 const char *key, 387 const char *key,
380 const void *value, 388 const void *value,
381 size_t size, 389 size_t size,
382 struct GNUNET_TIME_Absolute expiry, 390 struct GNUNET_TIME_Absolute expiry,
383 enum GNUNET_PEERSTORE_StoreOption options, 391 enum GNUNET_PEERSTORE_StoreOption options,
384 GNUNET_PEERSTORE_Continuation cont, 392 GNUNET_PEERSTORE_Continuation cont,
385 void *cont_cls) 393 void *cont_cls)
386{ 394{
387 struct Plugin *plugin = cls; 395 struct Plugin *plugin = cls;
388 sqlite3_stmt *stmt = plugin->insert_peerstoredata; 396 sqlite3_stmt *stmt = plugin->insert_peerstoredata;
389 struct GNUNET_SQ_QueryParam params[] = { 397 struct GNUNET_SQ_QueryParam params[] = {
390 GNUNET_SQ_query_param_string(sub_system), 398 GNUNET_SQ_query_param_string (sub_system),
391 GNUNET_SQ_query_param_auto_from_type(peer), 399 GNUNET_SQ_query_param_auto_from_type (peer),
392 GNUNET_SQ_query_param_string(key), 400 GNUNET_SQ_query_param_string (key),
393 GNUNET_SQ_query_param_fixed_size(value, size), 401 GNUNET_SQ_query_param_fixed_size (value, size),
394 GNUNET_SQ_query_param_absolute_time(&expiry), 402 GNUNET_SQ_query_param_absolute_time (&expiry),
395 GNUNET_SQ_query_param_end 403 GNUNET_SQ_query_param_end
396 }; 404 };
397 405
398 if (GNUNET_PEERSTORE_STOREOPTION_REPLACE == options) 406 if (GNUNET_PEERSTORE_STOREOPTION_REPLACE == options)
399 { 407 {
400 peerstore_sqlite_delete_records(cls, 408 peerstore_sqlite_delete_records (cls,
401 sub_system, 409 sub_system,
402 peer, 410 peer,
403 key); 411 key);
404 } 412 }
405 if (GNUNET_OK != 413 if (GNUNET_OK !=
406 GNUNET_SQ_bind(stmt, 414 GNUNET_SQ_bind (stmt,
407 params)) 415 params))
408 LOG_SQLITE(plugin, 416 LOG_SQLITE (plugin,
409 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 417 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
410 "sqlite3_bind"); 418 "sqlite3_bind");
411 else if (SQLITE_DONE != sqlite3_step(stmt)) 419 else if (SQLITE_DONE != sqlite3_step (stmt))
412 { 420 {
413 LOG_SQLITE(plugin, 421 LOG_SQLITE (plugin,
414 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 422 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
415 "sqlite3_step"); 423 "sqlite3_step");
416 } 424 }
417 GNUNET_SQ_reset(plugin->dbh, 425 GNUNET_SQ_reset (plugin->dbh,
418 stmt); 426 stmt);
419 if (NULL != cont) 427 if (NULL != cont)
420 cont(cont_cls, 428 cont (cont_cls,
421 GNUNET_OK); 429 GNUNET_OK);
422 return GNUNET_OK; 430 return GNUNET_OK;
423} 431}
424 432
@@ -431,25 +439,25 @@ peerstore_sqlite_store_record(void *cls,
431 * @return 0 on success 439 * @return 0 on success
432 */ 440 */
433static int 441static int
434sql_exec(sqlite3 *dbh, 442sql_exec (sqlite3 *dbh,
435 const char *sql) 443 const char *sql)
436{ 444{
437 int result; 445 int result;
438 446
439 result = sqlite3_exec(dbh, 447 result = sqlite3_exec (dbh,
440 sql, 448 sql,
441 NULL, 449 NULL,
442 NULL, 450 NULL,
443 NULL); 451 NULL);
444 LOG(GNUNET_ERROR_TYPE_DEBUG, 452 LOG (GNUNET_ERROR_TYPE_DEBUG,
445 "Executed `%s' / %d\n", 453 "Executed `%s' / %d\n",
446 sql, 454 sql,
447 result); 455 result);
448 if (SQLITE_OK != result) 456 if (SQLITE_OK != result)
449 LOG(GNUNET_ERROR_TYPE_ERROR, 457 LOG (GNUNET_ERROR_TYPE_ERROR,
450 _("Error executing SQL query: %s\n %s\n"), 458 _ ("Error executing SQL query: %s\n %s\n"),
451 sqlite3_errmsg(dbh), 459 sqlite3_errmsg (dbh),
452 sql); 460 sql);
453 return result; 461 return result;
454} 462}
455 463
@@ -463,28 +471,28 @@ sql_exec(sqlite3 *dbh,
463 * @return 0 on success 471 * @return 0 on success
464 */ 472 */
465static int 473static int
466sql_prepare(sqlite3 *dbh, 474sql_prepare (sqlite3 *dbh,
467 const char *sql, 475 const char *sql,
468 sqlite3_stmt ** stmt) 476 sqlite3_stmt **stmt)
469{ 477{
470 char *tail; 478 char *tail;
471 int result; 479 int result;
472 480
473 result = sqlite3_prepare_v2(dbh, 481 result = sqlite3_prepare_v2 (dbh,
474 sql, 482 sql,
475 strlen(sql), 483 strlen (sql),
476 stmt, 484 stmt,
477 (const char **)&tail); 485 (const char **) &tail);
478 LOG(GNUNET_ERROR_TYPE_DEBUG, 486 LOG (GNUNET_ERROR_TYPE_DEBUG,
479 "Prepared `%s' / %p: %d\n", 487 "Prepared `%s' / %p: %d\n",
480 sql, 488 sql,
481 *stmt, 489 *stmt,
482 result); 490 result);
483 if (SQLITE_OK != result) 491 if (SQLITE_OK != result)
484 LOG(GNUNET_ERROR_TYPE_ERROR, 492 LOG (GNUNET_ERROR_TYPE_ERROR,
485 _("Error preparing SQL query: %s\n %s\n"), 493 _ ("Error preparing SQL query: %s\n %s\n"),
486 sqlite3_errmsg(dbh), 494 sqlite3_errmsg (dbh),
487 sql); 495 sql);
488 return result; 496 return result;
489} 497}
490 498
@@ -498,110 +506,110 @@ sql_prepare(sqlite3 *dbh,
498 * @return #GNUNET_OK on success 506 * @return #GNUNET_OK on success
499 */ 507 */
500static int 508static int
501database_setup(struct Plugin *plugin) 509database_setup (struct Plugin *plugin)
502{ 510{
503 char *filename; 511 char *filename;
504 512
505 if (GNUNET_OK != 513 if (GNUNET_OK !=
506 GNUNET_CONFIGURATION_get_value_filename(plugin->cfg, 514 GNUNET_CONFIGURATION_get_value_filename (plugin->cfg,
507 "peerstore-sqlite", 515 "peerstore-sqlite",
508 "FILENAME", 516 "FILENAME",
509 &filename)) 517 &filename))
518 {
519 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
520 "peerstore-sqlite",
521 "FILENAME");
522 return GNUNET_SYSERR;
523 }
524 if (GNUNET_OK != GNUNET_DISK_file_test (filename))
525 {
526 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (filename))
510 { 527 {
511 GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR, 528 GNUNET_break (0);
512 "peerstore-sqlite", 529 GNUNET_free (filename);
513 "FILENAME");
514 return GNUNET_SYSERR; 530 return GNUNET_SYSERR;
515 } 531 }
516 if (GNUNET_OK != GNUNET_DISK_file_test(filename)) 532 }
517 {
518 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file(filename))
519 {
520 GNUNET_break(0);
521 GNUNET_free(filename);
522 return GNUNET_SYSERR;
523 }
524 }
525 /* filename should be UTF-8-encoded. If it isn't, it's a bug */ 533 /* filename should be UTF-8-encoded. If it isn't, it's a bug */
526 plugin->fn = filename; 534 plugin->fn = filename;
527 /* Open database and precompile statements */ 535 /* Open database and precompile statements */
528 if (SQLITE_OK != sqlite3_open(plugin->fn, 536 if (SQLITE_OK != sqlite3_open (plugin->fn,
529 &plugin->dbh)) 537 &plugin->dbh))
530 { 538 {
531 LOG(GNUNET_ERROR_TYPE_ERROR, 539 LOG (GNUNET_ERROR_TYPE_ERROR,
532 _("Unable to initialize SQLite: %s.\n"), 540 _ ("Unable to initialize SQLite: %s.\n"),
533 sqlite3_errmsg(plugin->dbh)); 541 sqlite3_errmsg (plugin->dbh));
534 return GNUNET_SYSERR; 542 return GNUNET_SYSERR;
535 } 543 }
536 sql_exec(plugin->dbh, 544 sql_exec (plugin->dbh,
537 "PRAGMA temp_store=MEMORY"); 545 "PRAGMA temp_store=MEMORY");
538 sql_exec(plugin->dbh, 546 sql_exec (plugin->dbh,
539 "PRAGMA synchronous=OFF"); 547 "PRAGMA synchronous=OFF");
540 sql_exec(plugin->dbh, 548 sql_exec (plugin->dbh,
541 "PRAGMA legacy_file_format=OFF"); 549 "PRAGMA legacy_file_format=OFF");
542 sql_exec(plugin->dbh, 550 sql_exec (plugin->dbh,
543 "PRAGMA auto_vacuum=INCREMENTAL"); 551 "PRAGMA auto_vacuum=INCREMENTAL");
544 sql_exec(plugin->dbh, 552 sql_exec (plugin->dbh,
545 "PRAGMA encoding=\"UTF-8\""); 553 "PRAGMA encoding=\"UTF-8\"");
546 sql_exec(plugin->dbh, 554 sql_exec (plugin->dbh,
547 "PRAGMA page_size=4096"); 555 "PRAGMA page_size=4096");
548 sqlite3_busy_timeout(plugin->dbh, 556 sqlite3_busy_timeout (plugin->dbh,
549 BUSY_TIMEOUT_MS); 557 BUSY_TIMEOUT_MS);
550 /* Create tables */ 558 /* Create tables */
551 sql_exec(plugin->dbh, 559 sql_exec (plugin->dbh,
552 "CREATE TABLE IF NOT EXISTS peerstoredata (\n" 560 "CREATE TABLE IF NOT EXISTS peerstoredata (\n"
553 " sub_system TEXT NOT NULL,\n" 561 " sub_system TEXT NOT NULL,\n"
554 " peer_id BLOB NOT NULL,\n" 562 " peer_id BLOB NOT NULL,\n"
555 " key TEXT NOT NULL,\n" 563 " key TEXT NOT NULL,\n"
556 " value BLOB NULL,\n" 564 " value BLOB NULL,\n"
557 " expiry INT8 NOT NULL" ");"); 565 " expiry INT8 NOT NULL" ");");
558 /* Create Indices */ 566 /* Create Indices */
559 if (SQLITE_OK != 567 if (SQLITE_OK !=
560 sqlite3_exec(plugin->dbh, 568 sqlite3_exec (plugin->dbh,
561 "CREATE INDEX IF NOT EXISTS peerstoredata_key_index ON peerstoredata (sub_system, peer_id, key)", 569 "CREATE INDEX IF NOT EXISTS peerstoredata_key_index ON peerstoredata (sub_system, peer_id, key)",
562 NULL, 570 NULL,
563 NULL, 571 NULL,
564 NULL)) 572 NULL))
565 { 573 {
566 LOG(GNUNET_ERROR_TYPE_ERROR, 574 LOG (GNUNET_ERROR_TYPE_ERROR,
567 _("Unable to create indices: %s.\n"), 575 _ ("Unable to create indices: %s.\n"),
568 sqlite3_errmsg(plugin->dbh)); 576 sqlite3_errmsg (plugin->dbh));
569 return GNUNET_SYSERR; 577 return GNUNET_SYSERR;
570 } 578 }
571 /* Prepare statements */ 579 /* Prepare statements */
572 580
573 sql_prepare(plugin->dbh, 581 sql_prepare (plugin->dbh,
574 "INSERT INTO peerstoredata (sub_system, peer_id, key, value, expiry)" 582 "INSERT INTO peerstoredata (sub_system, peer_id, key, value, expiry)"
575 " VALUES (?,?,?,?,?);", 583 " VALUES (?,?,?,?,?);",
576 &plugin->insert_peerstoredata); 584 &plugin->insert_peerstoredata);
577 sql_prepare(plugin->dbh, 585 sql_prepare (plugin->dbh,
578 "SELECT sub_system,peer_id,key,value,expiry FROM peerstoredata" 586 "SELECT sub_system,peer_id,key,value,expiry FROM peerstoredata"
579 " WHERE sub_system = ?", 587 " WHERE sub_system = ?",
580 &plugin->select_peerstoredata); 588 &plugin->select_peerstoredata);
581 sql_prepare(plugin->dbh, 589 sql_prepare (plugin->dbh,
582 "SELECT sub_system,peer_id,key,value,expiry FROM peerstoredata" 590 "SELECT sub_system,peer_id,key,value,expiry FROM peerstoredata"
583 " WHERE sub_system = ?" 591 " WHERE sub_system = ?"
584 " AND peer_id = ?", 592 " AND peer_id = ?",
585 &plugin->select_peerstoredata_by_pid); 593 &plugin->select_peerstoredata_by_pid);
586 sql_prepare(plugin->dbh, 594 sql_prepare (plugin->dbh,
587 "SELECT sub_system,peer_id,key,value,expiry FROM peerstoredata" 595 "SELECT sub_system,peer_id,key,value,expiry FROM peerstoredata"
588 " WHERE sub_system = ?" 596 " WHERE sub_system = ?"
589 " AND key = ?", 597 " AND key = ?",
590 &plugin->select_peerstoredata_by_key); 598 &plugin->select_peerstoredata_by_key);
591 sql_prepare(plugin->dbh, 599 sql_prepare (plugin->dbh,
592 "SELECT sub_system,peer_id,key,value,expiry FROM peerstoredata" 600 "SELECT sub_system,peer_id,key,value,expiry FROM peerstoredata"
593 " WHERE sub_system = ?" 601 " WHERE sub_system = ?"
594 " AND peer_id = ?" " AND key = ?", 602 " AND peer_id = ?" " AND key = ?",
595 &plugin->select_peerstoredata_by_all); 603 &plugin->select_peerstoredata_by_all);
596 sql_prepare(plugin->dbh, 604 sql_prepare (plugin->dbh,
597 "DELETE FROM peerstoredata" 605 "DELETE FROM peerstoredata"
598 " WHERE expiry < ?", 606 " WHERE expiry < ?",
599 &plugin->expire_peerstoredata); 607 &plugin->expire_peerstoredata);
600 sql_prepare(plugin->dbh, 608 sql_prepare (plugin->dbh,
601 "DELETE FROM peerstoredata" 609 "DELETE FROM peerstoredata"
602 " WHERE sub_system = ?" 610 " WHERE sub_system = ?"
603 " AND peer_id = ?" " AND key = ?", 611 " AND peer_id = ?" " AND key = ?",
604 &plugin->delete_peerstoredata); 612 &plugin->delete_peerstoredata);
605 return GNUNET_OK; 613 return GNUNET_OK;
606} 614}
607 615
@@ -612,26 +620,26 @@ database_setup(struct Plugin *plugin)
612 * @param plugin the plugin context (state for this module) 620 * @param plugin the plugin context (state for this module)
613 */ 621 */
614static void 622static void
615database_shutdown(struct Plugin *plugin) 623database_shutdown (struct Plugin *plugin)
616{ 624{
617 int result; 625 int result;
618 sqlite3_stmt *stmt; 626 sqlite3_stmt *stmt;
619 627
620 while (NULL != (stmt = sqlite3_next_stmt(plugin->dbh, 628 while (NULL != (stmt = sqlite3_next_stmt (plugin->dbh,
621 NULL))) 629 NULL)))
622 { 630 {
623 result = sqlite3_finalize(stmt); 631 result = sqlite3_finalize (stmt);
624 if (SQLITE_OK != result) 632 if (SQLITE_OK != result)
625 LOG(GNUNET_ERROR_TYPE_WARNING, 633 LOG (GNUNET_ERROR_TYPE_WARNING,
626 "Failed to close statement %p: %d\n", 634 "Failed to close statement %p: %d\n",
627 stmt, 635 stmt,
628 result); 636 result);
629 } 637 }
630 if (SQLITE_OK != sqlite3_close(plugin->dbh)) 638 if (SQLITE_OK != sqlite3_close (plugin->dbh))
631 LOG_SQLITE(plugin, 639 LOG_SQLITE (plugin,
632 GNUNET_ERROR_TYPE_ERROR, 640 GNUNET_ERROR_TYPE_ERROR,
633 "sqlite3_close"); 641 "sqlite3_close");
634 GNUNET_free_non_null(plugin->fn); 642 GNUNET_free_non_null (plugin->fn);
635} 643}
636 644
637 645
@@ -642,7 +650,7 @@ database_shutdown(struct Plugin *plugin)
642 * @return NULL on error, otherwise the plugin context 650 * @return NULL on error, otherwise the plugin context
643 */ 651 */
644void * 652void *
645libgnunet_plugin_peerstore_sqlite_init(void *cls) 653libgnunet_plugin_peerstore_sqlite_init (void *cls)
646{ 654{
647 static struct Plugin plugin; 655 static struct Plugin plugin;
648 const struct GNUNET_CONFIGURATION_Handle *cfg = cls; 656 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
@@ -650,22 +658,22 @@ libgnunet_plugin_peerstore_sqlite_init(void *cls)
650 658
651 if (NULL != plugin.cfg) 659 if (NULL != plugin.cfg)
652 return NULL; /* can only initialize once! */ 660 return NULL; /* can only initialize once! */
653 memset(&plugin, 661 memset (&plugin,
654 0, 662 0,
655 sizeof(struct Plugin)); 663 sizeof(struct Plugin));
656 plugin.cfg = cfg; 664 plugin.cfg = cfg;
657 if (GNUNET_OK != database_setup(&plugin)) 665 if (GNUNET_OK != database_setup (&plugin))
658 { 666 {
659 database_shutdown(&plugin); 667 database_shutdown (&plugin);
660 return NULL; 668 return NULL;
661 } 669 }
662 api = GNUNET_new(struct GNUNET_PEERSTORE_PluginFunctions); 670 api = GNUNET_new (struct GNUNET_PEERSTORE_PluginFunctions);
663 api->cls = &plugin; 671 api->cls = &plugin;
664 api->store_record = &peerstore_sqlite_store_record; 672 api->store_record = &peerstore_sqlite_store_record;
665 api->iterate_records = &peerstore_sqlite_iterate_records; 673 api->iterate_records = &peerstore_sqlite_iterate_records;
666 api->expire_records = &peerstore_sqlite_expire_records; 674 api->expire_records = &peerstore_sqlite_expire_records;
667 LOG(GNUNET_ERROR_TYPE_DEBUG, 675 LOG (GNUNET_ERROR_TYPE_DEBUG,
668 "Sqlite plugin is running\n"); 676 "Sqlite plugin is running\n");
669 return api; 677 return api;
670} 678}
671 679
@@ -677,16 +685,16 @@ libgnunet_plugin_peerstore_sqlite_init(void *cls)
677 * @return Always NULL 685 * @return Always NULL
678 */ 686 */
679void * 687void *
680libgnunet_plugin_peerstore_sqlite_done(void *cls) 688libgnunet_plugin_peerstore_sqlite_done (void *cls)
681{ 689{
682 struct GNUNET_PEERSTORE_PluginFunctions *api = cls; 690 struct GNUNET_PEERSTORE_PluginFunctions *api = cls;
683 struct Plugin *plugin = api->cls; 691 struct Plugin *plugin = api->cls;
684 692
685 database_shutdown(plugin); 693 database_shutdown (plugin);
686 plugin->cfg = NULL; 694 plugin->cfg = NULL;
687 GNUNET_free(api); 695 GNUNET_free (api);
688 LOG(GNUNET_ERROR_TYPE_DEBUG, 696 LOG (GNUNET_ERROR_TYPE_DEBUG,
689 "Sqlite plugin is finished\n"); 697 "Sqlite plugin is finished\n");
690 return NULL; 698 return NULL;
691} 699}
692 700