1 /* sqlite.c
2 *
3 * ====================================================================
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 * ====================================================================
21 */
22
23 #include <apr_pools.h>
24
25 #include "svn_types.h"
26 #include "svn_error.h"
27 #include "svn_pools.h"
28 #include "svn_io.h"
29 #include "svn_dirent_uri.h"
30 #include "svn_checksum.h"
31
32 #include "internal_statements.h"
33
34 #include "private/svn_sqlite.h"
35 #include "svn_private_config.h"
36 #include "private/svn_dep_compat.h"
37 #include "private/svn_atomic.h"
38 #include "private/svn_skel.h"
39 #include "private/svn_token.h"
40 #ifdef WIN32
41 #include "private/svn_io_private.h"
42 #include "private/svn_utf_private.h"
43 #endif
44
45 #ifdef SVN_UNICODE_NORMALIZATION_FIXES
46 #include "private/svn_utf_private.h"
47 #include "private/svn_string_private.h"
48 #endif /* SVN_UNICODE_NORMALIZATION_FIXES */
49
50 #ifdef SQLITE3_DEBUG
51 #include "private/svn_debug.h"
52 #endif
53
54 #ifdef SVN_SQLITE_INLINE
55 /* Import the sqlite3 API vtable from sqlite3wrapper.c */
56 # define SQLITE_OMIT_DEPRECATED
57 # include <sqlite3ext.h>
58 extern const sqlite3_api_routines *const svn_sqlite3__api_funcs;
59 extern int (*const svn_sqlite3__api_initialize)(void);
60 extern int (*const svn_sqlite3__api_config)(int, ...);
61 # define sqlite3_api svn_sqlite3__api_funcs
62 # define sqlite3_initialize svn_sqlite3__api_initialize
63 # define sqlite3_config svn_sqlite3__api_config
64 #else
65 # include <sqlite3.h>
66 #endif
67
68 #if !SQLITE_VERSION_AT_LEAST(3,7,12)
69 #error SQLite is too old -- version 3.7.12 is the minimum required version
70 #endif
71
72 #ifndef SQLITE_DETERMINISTIC
73 #define SQLITE_DETERMINISTIC 0
74 #endif
75
76 #ifdef SVN_UNICODE_NORMALIZATION_FIXES
77 /* Limit the length of a GLOB or LIKE pattern. */
78 #ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH
79 # define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000
80 #endif
81 #endif /* SVN_UNICODE_NORMALIZATION_FIXES */
82
83 const char *
svn_sqlite__compiled_version(void)84 svn_sqlite__compiled_version(void)
85 {
86 static const char sqlite_version[] = SQLITE_VERSION;
87 return sqlite_version;
88 }
89
90 const char *
svn_sqlite__runtime_version(void)91 svn_sqlite__runtime_version(void)
92 {
93 return sqlite3_libversion();
94 }
95
96
97 INTERNAL_STATEMENTS_SQL_DECLARE_STATEMENTS(internal_statements);
98
99
100 #ifdef SQLITE3_DEBUG
101 /* An sqlite query execution callback. */
102 static void
sqlite_tracer(void * data,const char * sql)103 sqlite_tracer(void *data, const char *sql)
104 {
105 /* sqlite3 *db3 = data; */
106 SVN_DBG(("sql=\"%s\"\n", sql));
107 }
108 #endif
109
110 #ifdef SQLITE3_PROFILE
111 /* An sqlite execution timing callback. */
112 static void
sqlite_profiler(void * data,const char * sql,sqlite3_uint64 duration)113 sqlite_profiler(void *data, const char *sql, sqlite3_uint64 duration)
114 {
115 /* sqlite3 *db3 = data; */
116 SVN_DBG(("[%.3f] sql=\"%s\"\n", 1e-9 * duration, sql));
117 }
118 #endif
119
120 #if defined(SVN_DEBUG) && defined(SQLITE_CONFIG_LOG)
121 static void
sqlite_error_log(void * baton,int err,const char * msg)122 sqlite_error_log(void* baton, int err, const char* msg)
123 {
124 fprintf(SVN_DBG_OUTPUT, "DBG: sqlite[S%d]: %s\n", err, msg);
125 }
126 #endif
127
128 void
svn_sqlite__dbg_enable_errorlog(void)129 svn_sqlite__dbg_enable_errorlog(void)
130 {
131 #if defined(SVN_DEBUG) && defined(SQLITE_CONFIG_LOG)
132 sqlite3_config(SQLITE_CONFIG_LOG, sqlite_error_log, (void*)NULL /* baton */);
133 #endif
134 }
135
136
137 struct svn_sqlite__db_t
138 {
139 sqlite3 *db3;
140 const char * const *statement_strings;
141 int nbr_statements;
142 svn_sqlite__stmt_t **prepared_stmts;
143 apr_pool_t *state_pool;
144
145 #ifdef SVN_UNICODE_NORMALIZATION_FIXES
146 /* Buffers for SQLite extensoins. */
147 svn_membuf_t sqlext_buf1;
148 svn_membuf_t sqlext_buf2;
149 svn_membuf_t sqlext_buf3;
150 #endif /* SVN_UNICODE_NORMALIZATION_FIXES */
151 };
152
153 struct svn_sqlite__stmt_t
154 {
155 sqlite3_stmt *s3stmt;
156 svn_sqlite__db_t *db;
157 svn_boolean_t needs_reset;
158 };
159
160 struct svn_sqlite__context_t
161 {
162 sqlite3_context *context;
163 };
164
165 struct svn_sqlite__value_t
166 {
167 sqlite3_value *value;
168 };
169
170
171 /* Convert SQLite error codes to SVN. Evaluates X multiple times */
172 #define SQLITE_ERROR_CODE(x) ((x) == SQLITE_READONLY \
173 ? SVN_ERR_SQLITE_READONLY \
174 : ((x) == SQLITE_BUSY \
175 ? SVN_ERR_SQLITE_BUSY \
176 : ((x) == SQLITE_CONSTRAINT \
177 ? SVN_ERR_SQLITE_CONSTRAINT \
178 : SVN_ERR_SQLITE_ERROR)))
179
180
181 /* SQLITE->SVN quick error wrap, much like SVN_ERR. */
182 #define SQLITE_ERR(x, db) do \
183 { \
184 int sqlite_err__temp = (x); \
185 if (sqlite_err__temp != SQLITE_OK) \
186 return svn_error_createf(SQLITE_ERROR_CODE(sqlite_err__temp), \
187 NULL, "sqlite[S%d]: %s", \
188 sqlite_err__temp, \
189 sqlite3_errmsg((db)->db3)); \
190 } while (0)
191
192 #define SQLITE_ERR_CLOSE(x, db, pool) do \
193 { \
194 int sqlite_err__temp = (x); \
195 if (sqlite_err__temp != SQLITE_OK) \
196 { \
197 const char *sqlite_err__msg \
198 = apr_pstrdup(pool, sqlite3_errmsg((db)->db3)); \
199 return svn_error_compose_create( \
200 svn_error_createf(SQLITE_ERROR_CODE(sqlite_err__temp), \
201 NULL, "sqlite[S%d]: %s", \
202 sqlite_err__temp, sqlite_err__msg), \
203 svn_sqlite__close(db)); \
204 } \
205 } while (0)
206
207 #define SQLITE_ERR_MSG(x, msg) do \
208 { \
209 int sqlite_err__temp = (x); \
210 if (sqlite_err__temp != SQLITE_OK) \
211 return svn_error_createf(SQLITE_ERROR_CODE(sqlite_err__temp), \
212 NULL, "sqlite[S%d]: %s", \
213 sqlite_err__temp, msg); \
214 } while (0)
215
216 #define SVN_ERR_CLOSE(x, db) do \
217 { \
218 svn_error_t *svn__err = (x); \
219 if (svn__err) \
220 return svn_error_compose_create(svn__err, svn_sqlite__close(db)); \
221 } while (0)
222
223
224 /* Time (in milliseconds) to wait for sqlite locks before giving up. */
225 #define BUSY_TIMEOUT 10000
226
227
228 /* Convenience wrapper around exec_sql2(). */
229 #define exec_sql(db, sql) exec_sql2((db), (sql), SQLITE_OK)
230
231 /* Run the statement SQL on DB, ignoring SQLITE_OK and IGNORED_ERR.
232 (Note: the IGNORED_ERR parameter itself is not ignored.) */
233 static svn_error_t *
exec_sql2(svn_sqlite__db_t * db,const char * sql,int ignored_err)234 exec_sql2(svn_sqlite__db_t *db, const char *sql, int ignored_err)
235 {
236 char *err_msg;
237 int sqlite_err = sqlite3_exec(db->db3, sql, NULL, NULL, &err_msg);
238
239 if (sqlite_err != SQLITE_OK && sqlite_err != ignored_err)
240 {
241 svn_error_t *err = svn_error_createf(SQLITE_ERROR_CODE(sqlite_err), NULL,
242 _("sqlite[S%d]: %s,"
243 " executing statement '%s'"),
244 sqlite_err, err_msg, sql);
245 sqlite3_free(err_msg);
246 return err;
247 }
248
249 return SVN_NO_ERROR;
250 }
251
252
253 static svn_error_t *
prepare_statement(svn_sqlite__stmt_t ** stmt,svn_sqlite__db_t * db,const char * text,apr_pool_t * result_pool)254 prepare_statement(svn_sqlite__stmt_t **stmt, svn_sqlite__db_t *db,
255 const char *text, apr_pool_t *result_pool)
256 {
257 *stmt = apr_palloc(result_pool, sizeof(**stmt));
258 (*stmt)->db = db;
259 (*stmt)->needs_reset = FALSE;
260
261 SQLITE_ERR(sqlite3_prepare_v2(db->db3, text, -1, &(*stmt)->s3stmt, NULL), db);
262
263 return SVN_NO_ERROR;
264 }
265
266
267 svn_error_t *
svn_sqlite__exec_statements(svn_sqlite__db_t * db,int stmt_idx)268 svn_sqlite__exec_statements(svn_sqlite__db_t *db, int stmt_idx)
269 {
270 SVN_ERR_ASSERT(stmt_idx < db->nbr_statements);
271
272 return svn_error_trace(exec_sql(db, db->statement_strings[stmt_idx]));
273 }
274
275
276 svn_error_t *
svn_sqlite__get_statement(svn_sqlite__stmt_t ** stmt,svn_sqlite__db_t * db,int stmt_idx)277 svn_sqlite__get_statement(svn_sqlite__stmt_t **stmt, svn_sqlite__db_t *db,
278 int stmt_idx)
279 {
280 SVN_ERR_ASSERT(stmt_idx < db->nbr_statements);
281
282 if (db->prepared_stmts[stmt_idx] == NULL)
283 SVN_ERR(prepare_statement(&db->prepared_stmts[stmt_idx], db,
284 db->statement_strings[stmt_idx],
285 db->state_pool));
286
287 *stmt = db->prepared_stmts[stmt_idx];
288
289 if ((*stmt)->needs_reset)
290 return svn_error_trace(svn_sqlite__reset(*stmt));
291
292 return SVN_NO_ERROR;
293 }
294
295 /* Like svn_sqlite__get_statement but gets an internal statement.
296
297 All internal statements that use this api are executed with step_done(),
298 so we don't need the fallback reset handling here or in the pool cleanup */
299 static svn_error_t *
get_internal_statement(svn_sqlite__stmt_t ** stmt,svn_sqlite__db_t * db,int stmt_idx)300 get_internal_statement(svn_sqlite__stmt_t **stmt, svn_sqlite__db_t *db,
301 int stmt_idx)
302 {
303 /* The internal statements are stored after the registered statements */
304 int prep_idx = db->nbr_statements + stmt_idx;
305 SVN_ERR_ASSERT(stmt_idx < STMT_INTERNAL_LAST);
306
307 if (db->prepared_stmts[prep_idx] == NULL)
308 SVN_ERR(prepare_statement(&db->prepared_stmts[prep_idx], db,
309 internal_statements[stmt_idx],
310 db->state_pool));
311
312 *stmt = db->prepared_stmts[prep_idx];
313
314 return SVN_NO_ERROR;
315 }
316
317
318 static svn_error_t *
step_with_expectation(svn_sqlite__stmt_t * stmt,svn_boolean_t expecting_row)319 step_with_expectation(svn_sqlite__stmt_t* stmt,
320 svn_boolean_t expecting_row)
321 {
322 svn_boolean_t got_row;
323
324 SVN_ERR(svn_sqlite__step(&got_row, stmt));
325 if ((got_row && !expecting_row)
326 ||
327 (!got_row && expecting_row))
328 return svn_error_create(SVN_ERR_SQLITE_ERROR,
329 svn_sqlite__reset(stmt),
330 expecting_row
331 ? _("sqlite: Expected database row missing")
332 : _("sqlite: Extra database row found"));
333
334 return SVN_NO_ERROR;
335 }
336
337 svn_error_t *
svn_sqlite__step_done(svn_sqlite__stmt_t * stmt)338 svn_sqlite__step_done(svn_sqlite__stmt_t *stmt)
339 {
340 SVN_ERR(step_with_expectation(stmt, FALSE));
341 return svn_error_trace(svn_sqlite__reset(stmt));
342 }
343
344 svn_error_t *
svn_sqlite__step_row(svn_sqlite__stmt_t * stmt)345 svn_sqlite__step_row(svn_sqlite__stmt_t *stmt)
346 {
347 return svn_error_trace(step_with_expectation(stmt, TRUE));
348 }
349
350
351 svn_error_t *
svn_sqlite__step(svn_boolean_t * got_row,svn_sqlite__stmt_t * stmt)352 svn_sqlite__step(svn_boolean_t *got_row, svn_sqlite__stmt_t *stmt)
353 {
354 int sqlite_result = sqlite3_step(stmt->s3stmt);
355
356 if (sqlite_result != SQLITE_DONE && sqlite_result != SQLITE_ROW)
357 {
358 svn_error_t *err1, *err2;
359
360 err1 = svn_error_createf(SQLITE_ERROR_CODE(sqlite_result), NULL,
361 "sqlite[S%d]: %s",
362 sqlite_result, sqlite3_errmsg(stmt->db->db3));
363 err2 = svn_sqlite__reset(stmt);
364 return svn_error_compose_create(err1, err2);
365 }
366
367 *got_row = (sqlite_result == SQLITE_ROW);
368 stmt->needs_reset = TRUE;
369
370 return SVN_NO_ERROR;
371 }
372
373 svn_error_t *
svn_sqlite__insert(apr_int64_t * row_id,svn_sqlite__stmt_t * stmt)374 svn_sqlite__insert(apr_int64_t *row_id, svn_sqlite__stmt_t *stmt)
375 {
376 svn_boolean_t got_row;
377
378 SVN_ERR(svn_sqlite__step(&got_row, stmt));
379 if (row_id)
380 *row_id = sqlite3_last_insert_rowid(stmt->db->db3);
381
382 return svn_error_trace(svn_sqlite__reset(stmt));
383 }
384
385 svn_error_t *
svn_sqlite__update(int * affected_rows,svn_sqlite__stmt_t * stmt)386 svn_sqlite__update(int *affected_rows, svn_sqlite__stmt_t *stmt)
387 {
388 SVN_ERR(step_with_expectation(stmt, FALSE));
389
390 if (affected_rows)
391 *affected_rows = sqlite3_changes(stmt->db->db3);
392
393 return svn_error_trace(svn_sqlite__reset(stmt));
394 }
395
396
397 static svn_error_t *
vbindf(svn_sqlite__stmt_t * stmt,const char * fmt,va_list ap)398 vbindf(svn_sqlite__stmt_t *stmt, const char *fmt, va_list ap)
399 {
400 int count;
401
402 for (count = 1; *fmt; fmt++, count++)
403 {
404 const void *blob;
405 apr_size_t blob_size;
406 const svn_token_map_t *map;
407
408 switch (*fmt)
409 {
410 case 's':
411 SVN_ERR(svn_sqlite__bind_text(stmt, count,
412 va_arg(ap, const char *)));
413 break;
414
415 case 'd':
416 SVN_ERR(svn_sqlite__bind_int(stmt, count,
417 va_arg(ap, int)));
418 break;
419
420 case 'i':
421 case 'L':
422 SVN_ERR(svn_sqlite__bind_int64(stmt, count,
423 va_arg(ap, apr_int64_t)));
424 break;
425
426 case 'b':
427 blob = va_arg(ap, const void *);
428 blob_size = va_arg(ap, apr_size_t);
429 SVN_ERR(svn_sqlite__bind_blob(stmt, count, blob, blob_size));
430 break;
431
432 case 'r':
433 SVN_ERR(svn_sqlite__bind_revnum(stmt, count,
434 va_arg(ap, svn_revnum_t)));
435 break;
436
437 case 't':
438 map = va_arg(ap, const svn_token_map_t *);
439 SVN_ERR(svn_sqlite__bind_token(stmt, count, map, va_arg(ap, int)));
440 break;
441
442 case 'n':
443 /* Skip this column: no binding */
444 break;
445
446 default:
447 SVN_ERR_MALFUNCTION();
448 }
449 }
450
451 return SVN_NO_ERROR;
452 }
453
454 svn_error_t *
svn_sqlite__bindf(svn_sqlite__stmt_t * stmt,const char * fmt,...)455 svn_sqlite__bindf(svn_sqlite__stmt_t *stmt, const char *fmt, ...)
456 {
457 svn_error_t *err;
458 va_list ap;
459
460 va_start(ap, fmt);
461 err = vbindf(stmt, fmt, ap);
462 va_end(ap);
463 return svn_error_trace(err);
464 }
465
466 svn_error_t *
svn_sqlite__bind_int(svn_sqlite__stmt_t * stmt,int slot,int val)467 svn_sqlite__bind_int(svn_sqlite__stmt_t *stmt,
468 int slot,
469 int val)
470 {
471 SQLITE_ERR(sqlite3_bind_int(stmt->s3stmt, slot, val), stmt->db);
472 return SVN_NO_ERROR;
473 }
474
475 svn_error_t *
svn_sqlite__bind_int64(svn_sqlite__stmt_t * stmt,int slot,apr_int64_t val)476 svn_sqlite__bind_int64(svn_sqlite__stmt_t *stmt,
477 int slot,
478 apr_int64_t val)
479 {
480 SQLITE_ERR(sqlite3_bind_int64(stmt->s3stmt, slot, val), stmt->db);
481 return SVN_NO_ERROR;
482 }
483
484 svn_error_t *
svn_sqlite__bind_text(svn_sqlite__stmt_t * stmt,int slot,const char * val)485 svn_sqlite__bind_text(svn_sqlite__stmt_t *stmt,
486 int slot,
487 const char *val)
488 {
489 SQLITE_ERR(sqlite3_bind_text(stmt->s3stmt, slot, val, -1, SQLITE_TRANSIENT),
490 stmt->db);
491 return SVN_NO_ERROR;
492 }
493
494 svn_error_t *
svn_sqlite__bind_blob(svn_sqlite__stmt_t * stmt,int slot,const void * val,apr_size_t len)495 svn_sqlite__bind_blob(svn_sqlite__stmt_t *stmt,
496 int slot,
497 const void *val,
498 apr_size_t len)
499 {
500 SQLITE_ERR(sqlite3_bind_blob(stmt->s3stmt, slot, val, (int) len,
501 SQLITE_TRANSIENT),
502 stmt->db);
503 return SVN_NO_ERROR;
504 }
505
506 svn_error_t *
svn_sqlite__bind_token(svn_sqlite__stmt_t * stmt,int slot,const svn_token_map_t * map,int value)507 svn_sqlite__bind_token(svn_sqlite__stmt_t *stmt,
508 int slot,
509 const svn_token_map_t *map,
510 int value)
511 {
512 const char *word = svn_token__to_word(map, value);
513
514 SQLITE_ERR(sqlite3_bind_text(stmt->s3stmt, slot, word, -1, SQLITE_STATIC),
515 stmt->db);
516 return SVN_NO_ERROR;
517 }
518
519 svn_error_t *
svn_sqlite__bind_revnum(svn_sqlite__stmt_t * stmt,int slot,svn_revnum_t value)520 svn_sqlite__bind_revnum(svn_sqlite__stmt_t *stmt,
521 int slot,
522 svn_revnum_t value)
523 {
524 if (SVN_IS_VALID_REVNUM(value))
525 SQLITE_ERR(sqlite3_bind_int64(stmt->s3stmt, slot,
526 (sqlite_int64)value), stmt->db);
527 else
528 SQLITE_ERR(sqlite3_bind_null(stmt->s3stmt, slot), stmt->db);
529
530 return SVN_NO_ERROR;
531 }
532
533 svn_error_t *
svn_sqlite__bind_properties(svn_sqlite__stmt_t * stmt,int slot,const apr_hash_t * props,apr_pool_t * scratch_pool)534 svn_sqlite__bind_properties(svn_sqlite__stmt_t *stmt,
535 int slot,
536 const apr_hash_t *props,
537 apr_pool_t *scratch_pool)
538 {
539 svn_skel_t *skel;
540 svn_stringbuf_t *properties;
541
542 if (props == NULL)
543 return svn_error_trace(svn_sqlite__bind_blob(stmt, slot, NULL, 0));
544
545 SVN_ERR(svn_skel__unparse_proplist(&skel, props, scratch_pool));
546 properties = svn_skel__unparse(skel, scratch_pool);
547 return svn_error_trace(svn_sqlite__bind_blob(stmt,
548 slot,
549 properties->data,
550 properties->len));
551 }
552
553 svn_error_t *
svn_sqlite__bind_iprops(svn_sqlite__stmt_t * stmt,int slot,const apr_array_header_t * inherited_props,apr_pool_t * scratch_pool)554 svn_sqlite__bind_iprops(svn_sqlite__stmt_t *stmt,
555 int slot,
556 const apr_array_header_t *inherited_props,
557 apr_pool_t *scratch_pool)
558 {
559 svn_skel_t *skel;
560 svn_stringbuf_t *properties;
561
562 if (inherited_props == NULL)
563 return svn_error_trace(svn_sqlite__bind_blob(stmt, slot, NULL, 0));
564
565 SVN_ERR(svn_skel__unparse_iproplist(&skel, inherited_props,
566 scratch_pool, scratch_pool));
567 properties = svn_skel__unparse(skel, scratch_pool);
568 return svn_error_trace(svn_sqlite__bind_blob(stmt,
569 slot,
570 properties->data,
571 properties->len));
572 }
573
574 svn_error_t *
svn_sqlite__bind_checksum(svn_sqlite__stmt_t * stmt,int slot,const svn_checksum_t * checksum,apr_pool_t * scratch_pool)575 svn_sqlite__bind_checksum(svn_sqlite__stmt_t *stmt,
576 int slot,
577 const svn_checksum_t *checksum,
578 apr_pool_t *scratch_pool)
579 {
580 const char *csum_str;
581
582 if (checksum == NULL)
583 csum_str = NULL;
584 else
585 csum_str = svn_checksum_serialize(checksum, scratch_pool, scratch_pool);
586
587 return svn_error_trace(svn_sqlite__bind_text(stmt, slot, csum_str));
588 }
589
590
591 const void *
svn_sqlite__column_blob(svn_sqlite__stmt_t * stmt,int column,apr_size_t * len,apr_pool_t * result_pool)592 svn_sqlite__column_blob(svn_sqlite__stmt_t *stmt, int column,
593 apr_size_t *len, apr_pool_t *result_pool)
594 {
595 const void *val = sqlite3_column_blob(stmt->s3stmt, column);
596 *len = sqlite3_column_bytes(stmt->s3stmt, column);
597
598 if (result_pool && val != NULL)
599 val = apr_pmemdup(result_pool, val, *len);
600
601 return val;
602 }
603
604 const char *
svn_sqlite__column_text(svn_sqlite__stmt_t * stmt,int column,apr_pool_t * result_pool)605 svn_sqlite__column_text(svn_sqlite__stmt_t *stmt, int column,
606 apr_pool_t *result_pool)
607 {
608 /* cast from 'unsigned char' to regular 'char' */
609 const char *result = (const char *)sqlite3_column_text(stmt->s3stmt, column);
610
611 if (result_pool && result != NULL)
612 result = apr_pstrdup(result_pool, result);
613
614 return result;
615 }
616
617 svn_revnum_t
svn_sqlite__column_revnum(svn_sqlite__stmt_t * stmt,int column)618 svn_sqlite__column_revnum(svn_sqlite__stmt_t *stmt, int column)
619 {
620 if (svn_sqlite__column_is_null(stmt, column))
621 return SVN_INVALID_REVNUM;
622 return (svn_revnum_t) sqlite3_column_int64(stmt->s3stmt, column);
623 }
624
625 svn_boolean_t
svn_sqlite__column_boolean(svn_sqlite__stmt_t * stmt,int column)626 svn_sqlite__column_boolean(svn_sqlite__stmt_t *stmt, int column)
627 {
628 return sqlite3_column_int64(stmt->s3stmt, column) != 0;
629 }
630
631 int
svn_sqlite__column_int(svn_sqlite__stmt_t * stmt,int column)632 svn_sqlite__column_int(svn_sqlite__stmt_t *stmt, int column)
633 {
634 return sqlite3_column_int(stmt->s3stmt, column);
635 }
636
637 apr_int64_t
svn_sqlite__column_int64(svn_sqlite__stmt_t * stmt,int column)638 svn_sqlite__column_int64(svn_sqlite__stmt_t *stmt, int column)
639 {
640 return sqlite3_column_int64(stmt->s3stmt, column);
641 }
642
643 int
svn_sqlite__column_token(svn_sqlite__stmt_t * stmt,int column,const svn_token_map_t * map)644 svn_sqlite__column_token(svn_sqlite__stmt_t *stmt,
645 int column,
646 const svn_token_map_t *map)
647 {
648 /* cast from 'unsigned char' to regular 'char' */
649 const char *word = (const char *)sqlite3_column_text(stmt->s3stmt, column);
650
651 return svn_token__from_word_strict(map, word);
652 }
653
654 int
svn_sqlite__column_token_null(svn_sqlite__stmt_t * stmt,int column,const svn_token_map_t * map,int null_val)655 svn_sqlite__column_token_null(svn_sqlite__stmt_t *stmt,
656 int column,
657 const svn_token_map_t *map,
658 int null_val)
659 {
660 /* cast from 'unsigned char' to regular 'char' */
661 const char *word = (const char *)sqlite3_column_text(stmt->s3stmt, column);
662
663 if (!word)
664 return null_val;
665
666 return svn_token__from_word_strict(map, word);
667 }
668
669 svn_error_t *
svn_sqlite__column_properties(apr_hash_t ** props,svn_sqlite__stmt_t * stmt,int column,apr_pool_t * result_pool,apr_pool_t * scratch_pool)670 svn_sqlite__column_properties(apr_hash_t **props,
671 svn_sqlite__stmt_t *stmt,
672 int column,
673 apr_pool_t *result_pool,
674 apr_pool_t *scratch_pool)
675 {
676 apr_size_t len;
677 const void *val;
678
679 /* svn_skel__parse_proplist copies everything needed to result_pool */
680 val = svn_sqlite__column_blob(stmt, column, &len, NULL);
681 if (val == NULL)
682 {
683 *props = NULL;
684 return SVN_NO_ERROR;
685 }
686
687 SVN_ERR(svn_skel__parse_proplist(props,
688 svn_skel__parse(val, len, scratch_pool),
689 result_pool));
690
691 return SVN_NO_ERROR;
692 }
693
694 svn_error_t *
svn_sqlite__column_iprops(apr_array_header_t ** iprops,svn_sqlite__stmt_t * stmt,int column,apr_pool_t * result_pool,apr_pool_t * scratch_pool)695 svn_sqlite__column_iprops(apr_array_header_t **iprops,
696 svn_sqlite__stmt_t *stmt,
697 int column,
698 apr_pool_t *result_pool,
699 apr_pool_t *scratch_pool)
700 {
701 apr_size_t len;
702 const void *val;
703
704 /* svn_skel__parse_iprops copies everything needed to result_pool */
705 val = svn_sqlite__column_blob(stmt, column, &len, NULL);
706 if (val == NULL)
707 {
708 *iprops = NULL;
709 return SVN_NO_ERROR;
710 }
711
712 SVN_ERR(svn_skel__parse_iprops(iprops,
713 svn_skel__parse(val, len, scratch_pool),
714 result_pool));
715
716 return SVN_NO_ERROR;
717 }
718
719 svn_error_t *
svn_sqlite__column_checksum(const svn_checksum_t ** checksum,svn_sqlite__stmt_t * stmt,int column,apr_pool_t * result_pool)720 svn_sqlite__column_checksum(const svn_checksum_t **checksum,
721 svn_sqlite__stmt_t *stmt, int column,
722 apr_pool_t *result_pool)
723 {
724 const char *digest = svn_sqlite__column_text(stmt, column, NULL);
725
726 if (digest == NULL)
727 *checksum = NULL;
728 else
729 SVN_ERR(svn_checksum_deserialize(checksum, digest,
730 result_pool, result_pool));
731
732 return SVN_NO_ERROR;
733 }
734
735 svn_boolean_t
svn_sqlite__column_is_null(svn_sqlite__stmt_t * stmt,int column)736 svn_sqlite__column_is_null(svn_sqlite__stmt_t *stmt, int column)
737 {
738 return sqlite3_column_type(stmt->s3stmt, column) == SQLITE_NULL;
739 }
740
741 int
svn_sqlite__column_bytes(svn_sqlite__stmt_t * stmt,int column)742 svn_sqlite__column_bytes(svn_sqlite__stmt_t *stmt, int column)
743 {
744 return sqlite3_column_bytes(stmt->s3stmt, column);
745 }
746
747 svn_error_t *
svn_sqlite__finalize(svn_sqlite__stmt_t * stmt)748 svn_sqlite__finalize(svn_sqlite__stmt_t *stmt)
749 {
750 SQLITE_ERR(sqlite3_finalize(stmt->s3stmt), stmt->db);
751 return SVN_NO_ERROR;
752 }
753
754 svn_error_t *
svn_sqlite__reset(svn_sqlite__stmt_t * stmt)755 svn_sqlite__reset(svn_sqlite__stmt_t *stmt)
756 {
757 /* No need to reset again after a first attempt */
758 stmt->needs_reset = FALSE;
759
760 /* Clear bindings first, as there are no documented reasons
761 why this would ever fail, but keeping variable bindings
762 when reset is not what we expect. */
763 SQLITE_ERR(sqlite3_clear_bindings(stmt->s3stmt), stmt->db);
764
765 /* Reset last, as this *will* fail if the statement failed since
766 the last time it was reset, while reporting just the same failure.
767 (In this case the statement is also properly reset).
768
769 See the sqlite3_reset() documentation for more details. */
770 SQLITE_ERR(sqlite3_reset(stmt->s3stmt), stmt->db);
771 return SVN_NO_ERROR;
772 }
773
774
775 svn_error_t *
svn_sqlite__read_schema_version(int * version,svn_sqlite__db_t * db,apr_pool_t * scratch_pool)776 svn_sqlite__read_schema_version(int *version,
777 svn_sqlite__db_t *db,
778 apr_pool_t *scratch_pool)
779 {
780 svn_sqlite__stmt_t *stmt;
781
782 SVN_ERR(prepare_statement(&stmt, db, "PRAGMA user_version;", scratch_pool));
783 SVN_ERR(svn_sqlite__step_row(stmt));
784
785 *version = svn_sqlite__column_int(stmt, 0);
786
787 return svn_error_trace(svn_sqlite__finalize(stmt));
788 }
789
790
791 static volatile svn_atomic_t sqlite_init_state = 0;
792
793 /* If possible, verify that SQLite was compiled in a thread-safe
794 manner. */
795 /* Don't call this function directly! Use svn_atomic__init_once(). */
796 static svn_error_t *
init_sqlite(void * baton,apr_pool_t * pool)797 init_sqlite(void *baton, apr_pool_t *pool)
798 {
799 if (sqlite3_libversion_number() < SVN_SQLITE_MIN_VERSION_NUMBER)
800 {
801 return svn_error_createf(
802 SVN_ERR_SQLITE_ERROR, NULL,
803 _("SQLite compiled for %s, but running with %s"),
804 SVN_SQLITE_MIN_VERSION, sqlite3_libversion());
805 }
806
807 #if APR_HAS_THREADS
808
809 /* SQLite 3.5 allows verification of its thread-safety at runtime.
810 Older versions are simply expected to have been configured with
811 --enable-threadsafe, which compiles with -DSQLITE_THREADSAFE=1
812 (or -DTHREADSAFE, for older versions). */
813 if (! sqlite3_threadsafe())
814 return svn_error_create(SVN_ERR_SQLITE_ERROR, NULL,
815 _("SQLite is required to be compiled and run in "
816 "thread-safe mode"));
817
818 /* If SQLite has been already initialized, sqlite3_config() returns
819 SQLITE_MISUSE. */
820 {
821 int err = sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
822 if (err != SQLITE_OK && err != SQLITE_MISUSE)
823 return svn_error_createf(SQLITE_ERROR_CODE(err), NULL,
824 _("Could not configure SQLite [S%d]"), err);
825 }
826 SQLITE_ERR_MSG(sqlite3_initialize(), _("Could not initialize SQLite"));
827
828 #endif /* APR_HAS_THRADS */
829
830 return SVN_NO_ERROR;
831 }
832
833 static svn_error_t *
internal_open(svn_sqlite__db_t * db,const char * path,svn_sqlite__mode_t mode,apr_int32_t timeout,apr_pool_t * scratch_pool)834 internal_open(svn_sqlite__db_t *db, const char *path, svn_sqlite__mode_t mode,
835 apr_int32_t timeout, apr_pool_t *scratch_pool)
836 {
837 {
838 int flags;
839
840 if (mode == svn_sqlite__mode_readonly)
841 flags = SQLITE_OPEN_READONLY;
842 else if (mode == svn_sqlite__mode_readwrite)
843 flags = SQLITE_OPEN_READWRITE;
844 else if (mode == svn_sqlite__mode_rwcreate)
845 flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
846 else
847 SVN_ERR_MALFUNCTION();
848
849 /* Turn off SQLite's mutexes. All svn objects are single-threaded,
850 so we can already guarantee that our use of the SQLite handle
851 will be serialized properly.
852
853 Note: in 3.6.x, we've already config'd SQLite into MULTITHREAD mode,
854 so this is probably redundant, but if we are running in a process where
855 somebody initialized SQLite before us it is needed anyway. */
856 flags |= SQLITE_OPEN_NOMUTEX;
857
858 #if !defined(WIN32) && !defined(SVN_SQLITE_INLINE)
859 if (mode == svn_sqlite__mode_rwcreate)
860 {
861 svn_node_kind_t kind;
862
863 /* Create the file before SQLite to avoid any permissions
864 problems with an SQLite build that uses the default
865 SQLITE_DEFAULT_FILE_PERMISSIONS of 644 modified by umask.
866 We simply want umask permissions. */
867 SVN_ERR(svn_io_check_path(path, &kind, scratch_pool));
868 if (kind == svn_node_none)
869 {
870 /* Another thread may have created the file, that's OK. */
871 svn_error_t *err = svn_io_file_create_empty(path, scratch_pool);
872 if (err && !APR_STATUS_IS_EEXIST(err->apr_err))
873 return svn_error_trace(err);
874 svn_error_clear(err);
875 }
876 }
877 #endif
878
879 /* Open the database. Note that a handle is returned, even when an error
880 occurs (except for out-of-memory); thus, we can safely use it to
881 extract an error message and construct an svn_error_t. SQLite always
882 requires sqlite3_close() after sqlite3_open_v2() while Subversion
883 typically does not require close() after an open() that returns an
884 error. So we must ensure we close the handle if this function, or
885 the caller svn_sqlite__open, returns an error to the application. */
886 {
887 const char *vFs = NULL;
888
889 #if defined(WIN32) && SQLITE_VERSION_AT_LEAST(3, 8, 1)
890 if (strlen(path) > 248)
891 {
892 WCHAR *win_path;
893 vFs = "win32-longpath"; /* Enable long paths in sqlite */
894
895 /* Long paths must be absolute */
896 if (!svn_dirent_is_absolute(path))
897 SVN_ERR(svn_dirent_get_absolute(&path, path, scratch_pool));
898
899 /* Convert the path to a properly canonicalized \\?\C:\long\path */
900 SVN_ERR(svn_io__utf8_to_unicode_longpath(&win_path, path,
901 scratch_pool));
902
903 /* And convert it back to UTF-8 because there is no
904 sqlite3_open16_v2() yet */
905 SVN_ERR(svn_utf__win32_utf16_to_utf8(&path, win_path, NULL,
906 scratch_pool));
907 }
908 #endif
909
910 /* ### SQLITE_CANTOPEN */
911 SQLITE_ERR_CLOSE(sqlite3_open_v2(path, &db->db3, flags, vFs),
912 db, scratch_pool);
913 }
914 }
915
916 if (timeout <= 0)
917 timeout = BUSY_TIMEOUT;
918
919 /* Retry until timeout when database is busy. */
920 SQLITE_ERR_CLOSE(sqlite3_busy_timeout(db->db3, timeout),
921 db, scratch_pool);
922
923 return SVN_NO_ERROR;
924 }
925
926
927 /* APR cleanup function used to close the database when its pool is destroyed.
928 DATA should be the svn_sqlite__db_t handle for the database. */
929 static apr_status_t
close_apr(void * data)930 close_apr(void *data)
931 {
932 svn_sqlite__db_t *db = data;
933 svn_error_t *err = SVN_NO_ERROR;
934 apr_status_t result;
935 int i;
936
937 /* Check to see if we've already closed this database. */
938 if (db->db3 == NULL)
939 return APR_SUCCESS;
940
941 /* Finalize any prepared statements. */
942 if (db->prepared_stmts)
943 {
944 for (i = 0; i < db->nbr_statements + STMT_INTERNAL_LAST; i++)
945 {
946 if (db->prepared_stmts[i])
947 {
948 if (i < db->nbr_statements
949 && db->prepared_stmts[i]->needs_reset)
950 {
951 #ifdef SVN_DEBUG
952 const char *stmt_text = db->statement_strings[i];
953 SVN_UNUSED(stmt_text);
954
955 SVN_ERR_MALFUNCTION_NO_RETURN();
956 #else
957 err = svn_error_compose_create(err,
958 svn_sqlite__reset(db->prepared_stmts[i]));
959 #endif
960 }
961 err = svn_error_compose_create(
962 svn_sqlite__finalize(db->prepared_stmts[i]), err);
963 }
964 }
965 }
966
967 result = sqlite3_close(db->db3);
968
969 /* If there's a pre-existing error, return it. */
970 if (err)
971 {
972 result = err->apr_err;
973 svn_error_clear(err);
974 return result;
975 }
976
977 if (result != SQLITE_OK)
978 return SQLITE_ERROR_CODE(result); /* ### lossy */
979
980 db->db3 = NULL;
981
982 return APR_SUCCESS;
983 }
984
985 #ifdef SVN_UNICODE_NORMALIZATION_FIXES
986 /* Unicode normalizing collation for WC paths */
987 static int
collate_ucs_nfd(void * baton,int len1,const void * key1,int len2,const void * key2)988 collate_ucs_nfd(void *baton,
989 int len1, const void *key1,
990 int len2, const void *key2)
991 {
992 svn_sqlite__db_t *db = baton;
993 int result;
994
995 if (svn_utf__normcmp(key1, len1, key2, len2,
996 &db->sqlext_buf1, &db->sqlext_buf2, &result))
997 {
998 /* There is really nothing we can do here if an error occurs
999 during Unicode normalizetion, and attempting to recover could
1000 result in the wc.db index being corrupted. Presumably this
1001 can only happen if the index already contains invalid UTF-8
1002 strings, which should never happen in any case ... */
1003 SVN_ERR_MALFUNCTION_NO_RETURN();
1004 }
1005
1006 return result;
1007 }
1008
1009 static void
glob_like_ucs_nfd_common(sqlite3_context * context,int argc,sqlite3_value ** argv,svn_boolean_t sql_like)1010 glob_like_ucs_nfd_common(sqlite3_context *context,
1011 int argc, sqlite3_value **argv,
1012 svn_boolean_t sql_like)
1013 {
1014 svn_sqlite__db_t *const db = sqlite3_user_data(context);
1015
1016 const char *const pattern = (void*)sqlite3_value_text(argv[0]);
1017 const apr_size_t pattern_len = sqlite3_value_bytes(argv[0]);
1018 const char *const string = (void*)sqlite3_value_text(argv[1]);
1019 const apr_size_t string_len = sqlite3_value_bytes(argv[1]);
1020
1021 const char *escape = NULL;
1022 apr_size_t escape_len = 0;
1023
1024 svn_boolean_t match;
1025 svn_error_t *err;
1026
1027 if (pattern_len > SQLITE_MAX_LIKE_PATTERN_LENGTH)
1028 {
1029 sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
1030 return;
1031 }
1032
1033 if (argc == 3 && sql_like)
1034 {
1035 escape = (void*)sqlite3_value_text(argv[2]);
1036 escape_len = sqlite3_value_bytes(argv[2]);
1037 }
1038
1039 if (pattern && string)
1040 {
1041 err = svn_utf__glob(pattern, pattern_len, string, string_len,
1042 escape, escape_len, sql_like,
1043 &db->sqlext_buf1, &db->sqlext_buf2, &db->sqlext_buf3,
1044 &match);
1045
1046 if (err)
1047 {
1048 const char *errmsg;
1049 svn_membuf__ensure(&db->sqlext_buf1, 512);
1050 errmsg = svn_err_best_message(err,
1051 db->sqlext_buf1.data,
1052 db->sqlext_buf1.size - 1);
1053 svn_error_clear(err);
1054 sqlite3_result_error(context, errmsg, -1);
1055 return;
1056 }
1057
1058 sqlite3_result_int(context, match);
1059 }
1060 }
1061
1062 /* Unicode normalizing implementation of GLOB */
1063 static void
glob_ucs_nfd(sqlite3_context * context,int argc,sqlite3_value ** argv)1064 glob_ucs_nfd(sqlite3_context *context,
1065 int argc, sqlite3_value **argv)
1066 {
1067 glob_like_ucs_nfd_common(context, argc, argv, FALSE);
1068 }
1069
1070 /* Unicode normalizing implementation of LIKE */
1071 static void
like_ucs_nfd(sqlite3_context * context,int argc,sqlite3_value ** argv)1072 like_ucs_nfd(sqlite3_context *context,
1073 int argc, sqlite3_value **argv)
1074 {
1075 glob_like_ucs_nfd_common(context, argc, argv, TRUE);
1076 }
1077 #endif /* SVN_UNICODE_NORMALIZATION_FIXES */
1078
1079 svn_error_t *
svn_sqlite__open(svn_sqlite__db_t ** db,const char * path,svn_sqlite__mode_t mode,const char * const statements[],int unused1,const char * const * unused2,apr_int32_t timeout,apr_pool_t * result_pool,apr_pool_t * scratch_pool)1080 svn_sqlite__open(svn_sqlite__db_t **db, const char *path,
1081 svn_sqlite__mode_t mode, const char * const statements[],
1082 int unused1, const char * const *unused2,
1083 apr_int32_t timeout,
1084 apr_pool_t *result_pool, apr_pool_t *scratch_pool)
1085 {
1086 SVN_ERR(svn_atomic__init_once(&sqlite_init_state,
1087 init_sqlite, NULL, scratch_pool));
1088
1089 *db = apr_pcalloc(result_pool, sizeof(**db));
1090
1091 SVN_ERR(internal_open(*db, path, mode, timeout, scratch_pool));
1092
1093 #if SQLITE_VERSION_NUMBER >= 3008000 && SQLITE_VERSION_NUMBER < 3009000
1094 /* disable SQLITE_ENABLE_STAT3/4 from 3.8.1 - 3.8.3 (but not 3.8.3.1+)
1095 * to prevent using it when it's buggy.
1096 * See: https://www.sqlite.org/src/info/4c86b126f2 */
1097 if (sqlite3_libversion_number() > 3008000 &&
1098 sqlite3_libversion_number() < 3008004 &&
1099 strcmp(sqlite3_sourceid(),"2014-02-11")<0)
1100 {
1101 sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, (*db)->db3, 0x800);
1102 }
1103 #endif
1104
1105 #ifdef SVN_UNICODE_NORMALIZATION_FIXES
1106 /* Create extension buffers with space for 200 UCS-4 characters. */
1107 svn_membuf__create(&(*db)->sqlext_buf1, 800, result_pool);
1108 svn_membuf__create(&(*db)->sqlext_buf2, 800, result_pool);
1109 svn_membuf__create(&(*db)->sqlext_buf3, 800, result_pool);
1110
1111 /* Register collation and LIKE and GLOB operator replacements. */
1112 SQLITE_ERR_CLOSE(sqlite3_create_collation((*db)->db3,
1113 "svn-ucs-nfd", SQLITE_UTF8,
1114 *db, collate_ucs_nfd),
1115 db, scratch_pool);
1116 /* ### Is it really necessary to override these functions?
1117 I would assume the default implementation to be collation agnostic?
1118 And otherwise our implementation should be...
1119
1120 The default implementation is in some cases index backed, while our
1121 implementation can't be. With an index based on the collation it could
1122 be. */
1123 SQLITE_ERR_CLOSE(sqlite3_create_function((*db)->db3, "glob", 2,
1124 SQLITE_UTF8 | SQLITE_DETERMINISTIC,
1125 *db, glob_ucs_nfd, NULL, NULL),
1126 db, scratch_pool);
1127 SQLITE_ERR_CLOSE(sqlite3_create_function((*db)->db3, "like", 2,
1128 SQLITE_UTF8 | SQLITE_DETERMINISTIC,
1129 *db, like_ucs_nfd, NULL, NULL),
1130 db, scratch_pool);
1131 SQLITE_ERR_CLOSE(sqlite3_create_function((*db)->db3, "like", 3,
1132 SQLITE_UTF8 | SQLITE_DETERMINISTIC,
1133 *db, like_ucs_nfd, NULL, NULL),
1134 db, scratch_pool);
1135 #endif /* SVN_UNICODE_NORMALIZATION_FIXES */
1136
1137 #ifdef SQLITE3_DEBUG
1138 sqlite3_trace((*db)->db3, sqlite_tracer, (*db)->db3);
1139 #endif
1140 #ifdef SQLITE3_PROFILE
1141 sqlite3_profile((*db)->db3, sqlite_profiler, (*db)->db3);
1142 #endif
1143
1144 SVN_ERR_CLOSE(exec_sql(*db,
1145 /* The default behavior of the LIKE operator is to ignore case
1146 for ASCII characters. Hence, by default 'a' LIKE 'A' is true.
1147 The case_sensitive_like pragma installs a new application-
1148 defined LIKE function that is either case sensitive or
1149 insensitive depending on the value of the case_sensitive_like
1150 pragma. */
1151 "PRAGMA case_sensitive_like=1;"
1152 /* Disable synchronization to disable the explicit disk flushes
1153 that make Sqlite up to 50 times slower; especially on small
1154 transactions.
1155
1156 This removes some stability guarantees on specific hardware
1157 and power failures, but still guarantees atomic commits on
1158 application crashes. With our dependency on external data
1159 like pristine files (Wc) and revision files (repository),
1160 we can't keep up these additional guarantees anyway.
1161
1162 ### Maybe switch to NORMAL(1) when we use larger transaction
1163 scopes */
1164 "PRAGMA synchronous=OFF;"
1165 /* Enable recursive triggers so that a user trigger will fire
1166 in the deletion phase of an INSERT OR REPLACE statement.
1167 Requires SQLite >= 3.6.18 */
1168 "PRAGMA recursive_triggers=ON;"
1169 /* Enforce current Sqlite default behavior. Some distributions
1170 might change the Sqlite defaults without realizing how this
1171 affects application(read: Subversion) performance/behavior. */
1172 "PRAGMA foreign_keys=OFF;" /* SQLITE_DEFAULT_FOREIGN_KEYS*/
1173 "PRAGMA locking_mode = NORMAL;" /* SQLITE_DEFAULT_LOCKING_MODE */
1174 "PRAGMA journal_mode = TRUNCATE;"
1175 ),
1176 *db);
1177
1178 #if defined(SVN_DEBUG)
1179 /* When running in debug mode, enable the checking of foreign key
1180 constraints. This has possible performance implications, so we don't
1181 bother to do it for production...for now. */
1182 SVN_ERR_CLOSE(exec_sql(*db, "PRAGMA foreign_keys=ON;"),
1183 *db);
1184 #endif
1185
1186 #ifdef SVN_SQLITE_REVERSE_UNORDERED_SELECTS
1187 /* When enabled, this PRAGMA causes SELECT statements without an ORDER BY
1188 clause to emit their results in the reverse order of what they normally
1189 would. This can help detecting invalid assumptions about the result
1190 order.*/
1191 SVN_ERR_CLOSE(exec_sql(*db, "PRAGMA reverse_unordered_selects=ON;"),
1192 *db);
1193 #endif
1194
1195 /* Store temporary tables in RAM instead of in temporary files, but don't
1196 fail on this if this option is disabled in the sqlite compilation by
1197 setting SQLITE_TEMP_STORE to 0 (always to disk) */
1198 svn_error_clear(exec_sql(*db, "PRAGMA temp_store = MEMORY;"));
1199
1200 /* Store the provided statements. */
1201 if (statements)
1202 {
1203 (*db)->statement_strings = statements;
1204 (*db)->nbr_statements = 0;
1205 while (*statements != NULL)
1206 {
1207 statements++;
1208 (*db)->nbr_statements++;
1209 }
1210
1211 (*db)->prepared_stmts = apr_pcalloc(
1212 result_pool,
1213 ((*db)->nbr_statements + STMT_INTERNAL_LAST)
1214 * sizeof(svn_sqlite__stmt_t *));
1215 }
1216 else
1217 {
1218 (*db)->nbr_statements = 0;
1219 (*db)->prepared_stmts = apr_pcalloc(result_pool,
1220 (0 + STMT_INTERNAL_LAST)
1221 * sizeof(svn_sqlite__stmt_t *));
1222 }
1223
1224 (*db)->state_pool = result_pool;
1225 apr_pool_cleanup_register(result_pool, *db, close_apr, apr_pool_cleanup_null);
1226
1227 return SVN_NO_ERROR;
1228 }
1229
1230 svn_error_t *
svn_sqlite__close(svn_sqlite__db_t * db)1231 svn_sqlite__close(svn_sqlite__db_t *db)
1232 {
1233 apr_status_t result = apr_pool_cleanup_run(db->state_pool, db, close_apr);
1234
1235 if (result == APR_SUCCESS)
1236 return SVN_NO_ERROR;
1237
1238 return svn_error_wrap_apr(result, NULL);
1239 }
1240
1241 static svn_error_t *
reset_all_statements(svn_sqlite__db_t * db,svn_error_t * error_to_wrap)1242 reset_all_statements(svn_sqlite__db_t *db,
1243 svn_error_t *error_to_wrap)
1244 {
1245 int i;
1246 svn_error_t *err;
1247
1248 /* ### Should we reorder the errors in this specific case
1249 ### to avoid returning the normal error as top level error? */
1250
1251 err = svn_error_compose_create(error_to_wrap,
1252 svn_error_create(SVN_ERR_SQLITE_RESETTING_FOR_ROLLBACK,
1253 NULL, NULL));
1254
1255 for (i = 0; i < db->nbr_statements; i++)
1256 if (db->prepared_stmts[i] && db->prepared_stmts[i]->needs_reset)
1257 err = svn_error_compose_create(err,
1258 svn_sqlite__reset(db->prepared_stmts[i]));
1259
1260 return err;
1261 }
1262
1263 svn_error_t *
svn_sqlite__begin_transaction(svn_sqlite__db_t * db)1264 svn_sqlite__begin_transaction(svn_sqlite__db_t *db)
1265 {
1266 svn_sqlite__stmt_t *stmt;
1267
1268 SVN_ERR(get_internal_statement(&stmt, db,
1269 STMT_INTERNAL_BEGIN_TRANSACTION));
1270 SVN_ERR(svn_sqlite__step_done(stmt));
1271 return SVN_NO_ERROR;
1272 }
1273
1274 svn_error_t *
svn_sqlite__begin_immediate_transaction(svn_sqlite__db_t * db)1275 svn_sqlite__begin_immediate_transaction(svn_sqlite__db_t *db)
1276 {
1277 svn_sqlite__stmt_t *stmt;
1278
1279 SVN_ERR(get_internal_statement(&stmt, db,
1280 STMT_INTERNAL_BEGIN_IMMEDIATE_TRANSACTION));
1281 SVN_ERR(svn_sqlite__step_done(stmt));
1282 return SVN_NO_ERROR;
1283 }
1284
1285 svn_error_t *
svn_sqlite__begin_savepoint(svn_sqlite__db_t * db)1286 svn_sqlite__begin_savepoint(svn_sqlite__db_t *db)
1287 {
1288 svn_sqlite__stmt_t *stmt;
1289
1290 SVN_ERR(get_internal_statement(&stmt, db,
1291 STMT_INTERNAL_SAVEPOINT_SVN));
1292 SVN_ERR(svn_sqlite__step_done(stmt));
1293 return SVN_NO_ERROR;
1294 }
1295
1296 svn_error_t *
svn_sqlite__finish_transaction(svn_sqlite__db_t * db,svn_error_t * err)1297 svn_sqlite__finish_transaction(svn_sqlite__db_t *db,
1298 svn_error_t *err)
1299 {
1300 svn_sqlite__stmt_t *stmt;
1301
1302 /* Commit or rollback the sqlite transaction. */
1303 if (err)
1304 {
1305 svn_error_t *err2;
1306
1307 err2 = get_internal_statement(&stmt, db,
1308 STMT_INTERNAL_ROLLBACK_TRANSACTION);
1309 if (!err2)
1310 err2 = svn_sqlite__step_done(stmt);
1311
1312 if (err2 && err2->apr_err == SVN_ERR_SQLITE_BUSY)
1313 {
1314 /* ### Houston, we have a problem!
1315
1316 We are trying to rollback but we can't because some
1317 statements are still busy. This leaves the database
1318 unusable for future transactions as the current transaction
1319 is still open.
1320
1321 As we are returning the actual error as the most relevant
1322 error in the chain, our caller might assume that it can
1323 retry/compensate on this error (e.g. SVN_WC_LOCKED), while
1324 in fact the SQLite database is unusable until the statements
1325 started within this transaction are reset and the transaction
1326 aborted.
1327
1328 We try to compensate by resetting all prepared but unreset
1329 statements; but we leave the busy error in the chain anyway to
1330 help diagnosing the original error and help in finding where
1331 a reset statement is missing. */
1332
1333 err2 = reset_all_statements(db, err2);
1334 err2 = svn_error_compose_create(
1335 svn_sqlite__step_done(stmt),
1336 err2);
1337 }
1338
1339 return svn_error_compose_create(err,
1340 err2);
1341 }
1342
1343 SVN_ERR(get_internal_statement(&stmt, db, STMT_INTERNAL_COMMIT_TRANSACTION));
1344 return svn_error_trace(svn_sqlite__step_done(stmt));
1345 }
1346
1347 svn_error_t *
svn_sqlite__finish_savepoint(svn_sqlite__db_t * db,svn_error_t * err)1348 svn_sqlite__finish_savepoint(svn_sqlite__db_t *db,
1349 svn_error_t *err)
1350 {
1351 svn_sqlite__stmt_t *stmt;
1352
1353 if (err)
1354 {
1355 svn_error_t *err2;
1356
1357 err2 = get_internal_statement(&stmt, db,
1358 STMT_INTERNAL_ROLLBACK_TO_SAVEPOINT_SVN);
1359
1360 if (!err2)
1361 err2 = svn_sqlite__step_done(stmt);
1362
1363 if (err2 && err2->apr_err == SVN_ERR_SQLITE_BUSY)
1364 {
1365 /* Ok, we have a major problem. Some statement is still open, which
1366 makes it impossible to release this savepoint.
1367
1368 ### See huge comment in svn_sqlite__finish_transaction for
1369 further details */
1370
1371 err2 = reset_all_statements(db, err2);
1372 err2 = svn_error_compose_create(svn_sqlite__step_done(stmt), err2);
1373 }
1374
1375 err = svn_error_compose_create(err, err2);
1376 err2 = get_internal_statement(&stmt, db,
1377 STMT_INTERNAL_RELEASE_SAVEPOINT_SVN);
1378
1379 if (!err2)
1380 err2 = svn_sqlite__step_done(stmt);
1381
1382 return svn_error_trace(svn_error_compose_create(err, err2));
1383 }
1384
1385 SVN_ERR(get_internal_statement(&stmt, db,
1386 STMT_INTERNAL_RELEASE_SAVEPOINT_SVN));
1387
1388 return svn_error_trace(svn_sqlite__step_done(stmt));
1389 }
1390
1391 svn_error_t *
svn_sqlite__with_transaction(svn_sqlite__db_t * db,svn_sqlite__transaction_callback_t cb_func,void * cb_baton,apr_pool_t * scratch_pool)1392 svn_sqlite__with_transaction(svn_sqlite__db_t *db,
1393 svn_sqlite__transaction_callback_t cb_func,
1394 void *cb_baton,
1395 apr_pool_t *scratch_pool /* NULL allowed */)
1396 {
1397 SVN_SQLITE__WITH_TXN(cb_func(cb_baton, db, scratch_pool), db);
1398 return SVN_NO_ERROR;
1399 }
1400
1401 svn_error_t *
svn_sqlite__with_immediate_transaction(svn_sqlite__db_t * db,svn_sqlite__transaction_callback_t cb_func,void * cb_baton,apr_pool_t * scratch_pool)1402 svn_sqlite__with_immediate_transaction(
1403 svn_sqlite__db_t *db,
1404 svn_sqlite__transaction_callback_t cb_func,
1405 void *cb_baton,
1406 apr_pool_t *scratch_pool /* NULL allowed */)
1407 {
1408 SVN_SQLITE__WITH_IMMEDIATE_TXN(cb_func(cb_baton, db, scratch_pool), db);
1409 return SVN_NO_ERROR;
1410 }
1411
1412 svn_error_t *
svn_sqlite__with_lock(svn_sqlite__db_t * db,svn_sqlite__transaction_callback_t cb_func,void * cb_baton,apr_pool_t * scratch_pool)1413 svn_sqlite__with_lock(svn_sqlite__db_t *db,
1414 svn_sqlite__transaction_callback_t cb_func,
1415 void *cb_baton,
1416 apr_pool_t *scratch_pool /* NULL allowed */)
1417 {
1418 SVN_SQLITE__WITH_LOCK(cb_func(cb_baton, db, scratch_pool), db);
1419 return SVN_NO_ERROR;
1420 }
1421
1422 svn_error_t *
svn_sqlite__hotcopy(const char * src_path,const char * dst_path,apr_pool_t * scratch_pool)1423 svn_sqlite__hotcopy(const char *src_path,
1424 const char *dst_path,
1425 apr_pool_t *scratch_pool)
1426 {
1427 svn_sqlite__db_t *src_db;
1428
1429 SVN_ERR(svn_sqlite__open(&src_db, src_path, svn_sqlite__mode_readonly,
1430 NULL, 0, NULL, 0,
1431 scratch_pool, scratch_pool));
1432
1433 {
1434 svn_sqlite__db_t *dst_db;
1435 sqlite3_backup *backup;
1436 int rc1, rc2;
1437
1438 SVN_ERR(svn_sqlite__open(&dst_db, dst_path, svn_sqlite__mode_rwcreate,
1439 NULL, 0, NULL, 0, scratch_pool, scratch_pool));
1440 backup = sqlite3_backup_init(dst_db->db3, "main", src_db->db3, "main");
1441 if (!backup)
1442 return svn_error_createf(SVN_ERR_SQLITE_ERROR, NULL,
1443 _("SQLite hotcopy failed for %s"), src_path);
1444 do
1445 {
1446 /* Pages are usually 1024 byte (SQLite docs). On my laptop
1447 copying gets faster as the number of pages is increased up
1448 to about 64, beyond that speed levels off. Lets put the
1449 number of pages an order of magnitude higher, this is still
1450 likely to be a fraction of large databases. */
1451 rc1 = sqlite3_backup_step(backup, 1024);
1452
1453 /* Should we sleep on SQLITE_OK? That would make copying a
1454 large database take much longer. When we do sleep how,
1455 long should we sleep? Should the sleep get longer if we
1456 keep getting BUSY/LOCKED? I have no real reason for
1457 choosing 25. */
1458 if (rc1 == SQLITE_BUSY || rc1 == SQLITE_LOCKED)
1459 sqlite3_sleep(25);
1460 }
1461 while (rc1 == SQLITE_OK || rc1 == SQLITE_BUSY || rc1 == SQLITE_LOCKED);
1462 rc2 = sqlite3_backup_finish(backup);
1463 if (rc1 != SQLITE_DONE)
1464 SQLITE_ERR(rc1, dst_db);
1465 SQLITE_ERR(rc2, dst_db);
1466 SVN_ERR(svn_sqlite__close(dst_db));
1467 }
1468
1469 SVN_ERR(svn_sqlite__close(src_db));
1470
1471 SVN_ERR(svn_io_copy_perms(src_path, dst_path, scratch_pool));
1472
1473 return SVN_NO_ERROR;
1474 }
1475
1476 struct function_wrapper_baton_t
1477 {
1478 svn_sqlite__func_t func;
1479 void *baton;
1480 };
1481
1482 static void
wrapped_func(sqlite3_context * context,int argc,sqlite3_value * values[])1483 wrapped_func(sqlite3_context *context,
1484 int argc,
1485 sqlite3_value *values[])
1486 {
1487 struct function_wrapper_baton_t *fwb = sqlite3_user_data(context);
1488 svn_sqlite__context_t sctx;
1489 svn_error_t *err;
1490 void *void_values = values;
1491
1492 sctx.context = context;
1493
1494 err = fwb->func(&sctx, argc, void_values, fwb->baton);
1495
1496 if (err)
1497 {
1498 char buf[256];
1499 sqlite3_result_error(context,
1500 svn_err_best_message(err, buf, sizeof(buf)),
1501 -1);
1502 svn_error_clear(err);
1503 }
1504 }
1505
1506
1507 svn_error_t *
svn_sqlite__create_scalar_function(svn_sqlite__db_t * db,const char * func_name,int argc,svn_boolean_t deterministic,svn_sqlite__func_t func,void * baton)1508 svn_sqlite__create_scalar_function(svn_sqlite__db_t *db,
1509 const char *func_name,
1510 int argc,
1511 svn_boolean_t deterministic,
1512 svn_sqlite__func_t func,
1513 void *baton)
1514 {
1515 int eTextRep;
1516 struct function_wrapper_baton_t *fwb = apr_pcalloc(db->state_pool,
1517 sizeof(*fwb));
1518
1519 fwb->func = func;
1520 fwb->baton = baton;
1521
1522 eTextRep = SQLITE_ANY;
1523 if (deterministic)
1524 eTextRep |= SQLITE_DETERMINISTIC;
1525
1526 SQLITE_ERR(sqlite3_create_function(db->db3, func_name, argc, eTextRep,
1527 fwb, wrapped_func, NULL, NULL),
1528 db);
1529
1530 return SVN_NO_ERROR;
1531 }
1532
1533 int
svn_sqlite__value_type(svn_sqlite__value_t * val)1534 svn_sqlite__value_type(svn_sqlite__value_t *val)
1535 {
1536 void *v = val;
1537 return sqlite3_value_type(v);
1538 }
1539
1540 const char *
svn_sqlite__value_text(svn_sqlite__value_t * val)1541 svn_sqlite__value_text(svn_sqlite__value_t *val)
1542 {
1543 void *v = val;
1544 return (const char *) sqlite3_value_text(v);
1545 }
1546
1547 void
svn_sqlite__result_null(svn_sqlite__context_t * sctx)1548 svn_sqlite__result_null(svn_sqlite__context_t *sctx)
1549 {
1550 sqlite3_result_null(sctx->context);
1551 }
1552
1553 void
svn_sqlite__result_int64(svn_sqlite__context_t * sctx,apr_int64_t val)1554 svn_sqlite__result_int64(svn_sqlite__context_t *sctx, apr_int64_t val)
1555 {
1556 sqlite3_result_int64(sctx->context, val);
1557 }
1558
1559 void
svn_sqlite__result_error(svn_sqlite__context_t * sctx,const char * msg,int num)1560 svn_sqlite__result_error(svn_sqlite__context_t *sctx, const char *msg, int num)
1561 {
1562 sqlite3_result_error(sctx->context, msg, num);
1563 }
1564