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