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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
20 * ====================================================================
23 #include <apr_pools.h>
25 #include "svn_types.h"
26 #include "svn_error.h"
27 #include "svn_pools.h"
29 #include "svn_dirent_uri.h"
30 #include "svn_checksum.h"
32 #include "internal_statements.h"
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"
41 #include "private/svn_io_private.h"
42 #include "private/svn_utf_private.h"
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 */
51 #include "private/svn_debug.h"
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
68 #if !SQLITE_VERSION_AT_LEAST(3,8,2)
69 #error SQLite is too old -- version 3.8.2 is the minimum required version
72 #ifndef SQLITE_DETERMINISTIC
73 #define SQLITE_DETERMINISTIC 0
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
81 #endif /* SVN_UNICODE_NORMALIZATION_FIXES */
84 svn_sqlite__compiled_version(void)
86 static const char sqlite_version[] = SQLITE_VERSION;
87 return sqlite_version;
91 svn_sqlite__runtime_version(void)
93 return sqlite3_libversion();
97 INTERNAL_STATEMENTS_SQL_DECLARE_STATEMENTS(internal_statements);
101 /* An sqlite query execution callback. */
103 sqlite_tracer(void *data, const char *sql)
105 /* sqlite3 *db3 = data; */
106 SVN_DBG(("sql=\"%s\"\n", sql));
110 #ifdef SQLITE3_PROFILE
111 /* An sqlite execution timing callback. */
113 sqlite_profiler(void *data, const char *sql, sqlite3_uint64 duration)
115 /* sqlite3 *db3 = data; */
116 SVN_DBG(("[%.3f] sql=\"%s\"\n", 1e-9 * duration, sql));
120 #if defined(SVN_DEBUG) && defined(SQLITE_CONFIG_LOG)
122 sqlite_error_log(void* baton, int err, const char* msg)
124 fprintf(SVN_DBG_OUTPUT, "DBG: sqlite[S%d]: %s\n", err, msg);
129 svn_sqlite__dbg_enable_errorlog(void)
131 #if defined(SVN_DEBUG) && defined(SQLITE_CONFIG_LOG)
132 sqlite3_config(SQLITE_CONFIG_LOG, sqlite_error_log, (void*)NULL /* baton */);
137 struct svn_sqlite__db_t
140 const char * const *statement_strings;
142 svn_sqlite__stmt_t **prepared_stmts;
143 apr_pool_t *state_pool;
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 */
153 struct svn_sqlite__stmt_t
155 sqlite3_stmt *s3stmt;
156 svn_sqlite__db_t *db;
157 svn_boolean_t needs_reset;
160 struct svn_sqlite__context_t
162 sqlite3_context *context;
165 struct svn_sqlite__value_t
167 sqlite3_value *value;
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)))
181 /* SQLITE->SVN quick error wrap, much like SVN_ERR. */
182 #define SQLITE_ERR(x, db) do \
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", \
189 sqlite3_errmsg((db)->db3)); \
192 #define SQLITE_ERR_CLOSE(x, db, pool) do \
194 int sqlite_err__temp = (x); \
195 if (sqlite_err__temp != SQLITE_OK) \
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)); \
207 #define SQLITE_ERR_MSG(x, msg) do \
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); \
217 /* Time (in milliseconds) to wait for sqlite locks before giving up. */
218 #define BUSY_TIMEOUT 10000
221 /* Convenience wrapper around exec_sql2(). */
222 #define exec_sql(db, sql) exec_sql2((db), (sql), SQLITE_OK)
224 /* Run the statement SQL on DB, ignoring SQLITE_OK and IGNORED_ERR.
225 (Note: the IGNORED_ERR parameter itself is not ignored.) */
227 exec_sql2(svn_sqlite__db_t *db, const char *sql, int ignored_err)
230 int sqlite_err = sqlite3_exec(db->db3, sql, NULL, NULL, &err_msg);
232 if (sqlite_err != SQLITE_OK && sqlite_err != ignored_err)
234 svn_error_t *err = svn_error_createf(SQLITE_ERROR_CODE(sqlite_err), NULL,
236 " executing statement '%s'"),
237 sqlite_err, err_msg, sql);
238 sqlite3_free(err_msg);
247 prepare_statement(svn_sqlite__stmt_t **stmt, svn_sqlite__db_t *db,
248 const char *text, apr_pool_t *result_pool)
250 *stmt = apr_palloc(result_pool, sizeof(**stmt));
252 (*stmt)->needs_reset = FALSE;
254 SQLITE_ERR(sqlite3_prepare_v2(db->db3, text, -1, &(*stmt)->s3stmt, NULL), db);
261 svn_sqlite__exec_statements(svn_sqlite__db_t *db, int stmt_idx)
263 SVN_ERR_ASSERT(stmt_idx < db->nbr_statements);
265 return svn_error_trace(exec_sql(db, db->statement_strings[stmt_idx]));
270 svn_sqlite__get_statement(svn_sqlite__stmt_t **stmt, svn_sqlite__db_t *db,
273 SVN_ERR_ASSERT(stmt_idx < db->nbr_statements);
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],
280 *stmt = db->prepared_stmts[stmt_idx];
282 if ((*stmt)->needs_reset)
283 return svn_error_trace(svn_sqlite__reset(*stmt));
288 /* Like svn_sqlite__get_statement but gets an internal statement.
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 */
293 get_internal_statement(svn_sqlite__stmt_t **stmt, svn_sqlite__db_t *db,
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);
300 if (db->prepared_stmts[prep_idx] == NULL)
301 SVN_ERR(prepare_statement(&db->prepared_stmts[prep_idx], db,
302 internal_statements[stmt_idx],
305 *stmt = db->prepared_stmts[prep_idx];
312 step_with_expectation(svn_sqlite__stmt_t* stmt,
313 svn_boolean_t expecting_row)
315 svn_boolean_t got_row;
317 SVN_ERR(svn_sqlite__step(&got_row, stmt));
318 if ((got_row && !expecting_row)
320 (!got_row && expecting_row))
321 return svn_error_create(SVN_ERR_SQLITE_ERROR,
322 svn_sqlite__reset(stmt),
324 ? _("sqlite: Expected database row missing")
325 : _("sqlite: Extra database row found"));
331 svn_sqlite__step_done(svn_sqlite__stmt_t *stmt)
333 SVN_ERR(step_with_expectation(stmt, FALSE));
334 return svn_error_trace(svn_sqlite__reset(stmt));
338 svn_sqlite__step_row(svn_sqlite__stmt_t *stmt)
340 return svn_error_trace(step_with_expectation(stmt, TRUE));
345 svn_sqlite__step(svn_boolean_t *got_row, svn_sqlite__stmt_t *stmt)
347 int sqlite_result = sqlite3_step(stmt->s3stmt);
349 if (sqlite_result != SQLITE_DONE && sqlite_result != SQLITE_ROW)
351 svn_error_t *err1, *err2;
353 err1 = svn_error_createf(SQLITE_ERROR_CODE(sqlite_result), NULL,
355 sqlite_result, sqlite3_errmsg(stmt->db->db3));
356 err2 = svn_sqlite__reset(stmt);
357 return svn_error_compose_create(err1, err2);
360 *got_row = (sqlite_result == SQLITE_ROW);
361 stmt->needs_reset = TRUE;
367 svn_sqlite__insert(apr_int64_t *row_id, svn_sqlite__stmt_t *stmt)
369 svn_boolean_t got_row;
371 SVN_ERR(svn_sqlite__step(&got_row, stmt));
373 *row_id = sqlite3_last_insert_rowid(stmt->db->db3);
375 return svn_error_trace(svn_sqlite__reset(stmt));
379 svn_sqlite__update(int *affected_rows, svn_sqlite__stmt_t *stmt)
381 SVN_ERR(step_with_expectation(stmt, FALSE));
384 *affected_rows = sqlite3_changes(stmt->db->db3);
386 return svn_error_trace(svn_sqlite__reset(stmt));
391 vbindf(svn_sqlite__stmt_t *stmt, const char *fmt, va_list ap)
395 for (count = 1; *fmt; fmt++, count++)
398 apr_size_t blob_size;
399 const svn_token_map_t *map;
404 SVN_ERR(svn_sqlite__bind_text(stmt, count,
405 va_arg(ap, const char *)));
409 SVN_ERR(svn_sqlite__bind_int(stmt, count,
415 SVN_ERR(svn_sqlite__bind_int64(stmt, count,
416 va_arg(ap, apr_int64_t)));
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));
426 SVN_ERR(svn_sqlite__bind_revnum(stmt, count,
427 va_arg(ap, svn_revnum_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)));
436 /* Skip this column: no binding */
440 SVN_ERR_MALFUNCTION();
448 svn_sqlite__bindf(svn_sqlite__stmt_t *stmt, const char *fmt, ...)
454 err = vbindf(stmt, fmt, ap);
456 return svn_error_trace(err);
460 svn_sqlite__bind_int(svn_sqlite__stmt_t *stmt,
464 SQLITE_ERR(sqlite3_bind_int(stmt->s3stmt, slot, val), stmt->db);
469 svn_sqlite__bind_int64(svn_sqlite__stmt_t *stmt,
473 SQLITE_ERR(sqlite3_bind_int64(stmt->s3stmt, slot, val), stmt->db);
478 svn_sqlite__bind_text(svn_sqlite__stmt_t *stmt,
482 SQLITE_ERR(sqlite3_bind_text(stmt->s3stmt, slot, val, -1, SQLITE_TRANSIENT),
488 svn_sqlite__bind_blob(svn_sqlite__stmt_t *stmt,
493 SQLITE_ERR(sqlite3_bind_blob(stmt->s3stmt, slot, val, (int) len,
500 svn_sqlite__bind_token(svn_sqlite__stmt_t *stmt,
502 const svn_token_map_t *map,
505 const char *word = svn_token__to_word(map, value);
507 SQLITE_ERR(sqlite3_bind_text(stmt->s3stmt, slot, word, -1, SQLITE_STATIC),
513 svn_sqlite__bind_revnum(svn_sqlite__stmt_t *stmt,
517 if (SVN_IS_VALID_REVNUM(value))
518 SQLITE_ERR(sqlite3_bind_int64(stmt->s3stmt, slot,
519 (sqlite_int64)value), stmt->db);
521 SQLITE_ERR(sqlite3_bind_null(stmt->s3stmt, slot), stmt->db);
527 svn_sqlite__bind_properties(svn_sqlite__stmt_t *stmt,
529 const apr_hash_t *props,
530 apr_pool_t *scratch_pool)
533 svn_stringbuf_t *properties;
536 return svn_error_trace(svn_sqlite__bind_blob(stmt, slot, NULL, 0));
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,
547 svn_sqlite__bind_iprops(svn_sqlite__stmt_t *stmt,
549 const apr_array_header_t *inherited_props,
550 apr_pool_t *scratch_pool)
553 svn_stringbuf_t *properties;
555 if (inherited_props == NULL)
556 return svn_error_trace(svn_sqlite__bind_blob(stmt, slot, NULL, 0));
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,
568 svn_sqlite__bind_checksum(svn_sqlite__stmt_t *stmt,
570 const svn_checksum_t *checksum,
571 apr_pool_t *scratch_pool)
573 const char *csum_str;
575 if (checksum == NULL)
578 csum_str = svn_checksum_serialize(checksum, scratch_pool, scratch_pool);
580 return svn_error_trace(svn_sqlite__bind_text(stmt, slot, csum_str));
585 svn_sqlite__column_blob(svn_sqlite__stmt_t *stmt, int column,
586 apr_size_t *len, apr_pool_t *result_pool)
588 const void *val = sqlite3_column_blob(stmt->s3stmt, column);
589 *len = sqlite3_column_bytes(stmt->s3stmt, column);
591 if (result_pool && val != NULL)
592 val = apr_pmemdup(result_pool, val, *len);
598 svn_sqlite__column_text(svn_sqlite__stmt_t *stmt, int column,
599 apr_pool_t *result_pool)
601 /* cast from 'unsigned char' to regular 'char' */
602 const char *result = (const char *)sqlite3_column_text(stmt->s3stmt, column);
604 if (result_pool && result != NULL)
605 result = apr_pstrdup(result_pool, result);
611 svn_sqlite__column_revnum(svn_sqlite__stmt_t *stmt, int column)
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);
619 svn_sqlite__column_boolean(svn_sqlite__stmt_t *stmt, int column)
621 return sqlite3_column_int64(stmt->s3stmt, column) != 0;
625 svn_sqlite__column_int(svn_sqlite__stmt_t *stmt, int column)
627 return sqlite3_column_int(stmt->s3stmt, column);
631 svn_sqlite__column_int64(svn_sqlite__stmt_t *stmt, int column)
633 return sqlite3_column_int64(stmt->s3stmt, column);
637 svn_sqlite__column_token(svn_sqlite__stmt_t *stmt,
639 const svn_token_map_t *map)
641 /* cast from 'unsigned char' to regular 'char' */
642 const char *word = (const char *)sqlite3_column_text(stmt->s3stmt, column);
644 return svn_token__from_word_strict(map, word);
648 svn_sqlite__column_token_null(svn_sqlite__stmt_t *stmt,
650 const svn_token_map_t *map,
653 /* cast from 'unsigned char' to regular 'char' */
654 const char *word = (const char *)sqlite3_column_text(stmt->s3stmt, column);
659 return svn_token__from_word_strict(map, word);
663 svn_sqlite__column_properties(apr_hash_t **props,
664 svn_sqlite__stmt_t *stmt,
666 apr_pool_t *result_pool,
667 apr_pool_t *scratch_pool)
672 /* svn_skel__parse_proplist copies everything needed to result_pool */
673 val = svn_sqlite__column_blob(stmt, column, &len, NULL);
680 SVN_ERR(svn_skel__parse_proplist(props,
681 svn_skel__parse(val, len, scratch_pool),
688 svn_sqlite__column_iprops(apr_array_header_t **iprops,
689 svn_sqlite__stmt_t *stmt,
691 apr_pool_t *result_pool,
692 apr_pool_t *scratch_pool)
697 /* svn_skel__parse_iprops copies everything needed to result_pool */
698 val = svn_sqlite__column_blob(stmt, column, &len, NULL);
705 SVN_ERR(svn_skel__parse_iprops(iprops,
706 svn_skel__parse(val, len, scratch_pool),
713 svn_sqlite__column_checksum(const svn_checksum_t **checksum,
714 svn_sqlite__stmt_t *stmt, int column,
715 apr_pool_t *result_pool)
717 const char *digest = svn_sqlite__column_text(stmt, column, NULL);
722 SVN_ERR(svn_checksum_deserialize(checksum, digest,
723 result_pool, result_pool));
729 svn_sqlite__column_is_null(svn_sqlite__stmt_t *stmt, int column)
731 return sqlite3_column_type(stmt->s3stmt, column) == SQLITE_NULL;
735 svn_sqlite__column_bytes(svn_sqlite__stmt_t *stmt, int column)
737 return sqlite3_column_bytes(stmt->s3stmt, column);
741 svn_sqlite__finalize(svn_sqlite__stmt_t *stmt)
743 SQLITE_ERR(sqlite3_finalize(stmt->s3stmt), stmt->db);
748 svn_sqlite__reset(svn_sqlite__stmt_t *stmt)
750 /* No need to reset again after a first attempt */
751 stmt->needs_reset = FALSE;
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);
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).
762 See the sqlite3_reset() documentation for more details. */
763 SQLITE_ERR(sqlite3_reset(stmt->s3stmt), stmt->db);
769 svn_sqlite__read_schema_version(int *version,
770 svn_sqlite__db_t *db,
771 apr_pool_t *scratch_pool)
773 svn_sqlite__stmt_t *stmt;
775 SVN_ERR(prepare_statement(&stmt, db, "PRAGMA user_version;", scratch_pool));
776 SVN_ERR(svn_sqlite__step_row(stmt));
778 *version = svn_sqlite__column_int(stmt, 0);
780 return svn_error_trace(svn_sqlite__finalize(stmt));
784 static volatile svn_atomic_t sqlite_init_state = 0;
786 /* If possible, verify that SQLite was compiled in a thread-safe
788 /* Don't call this function directly! Use svn_atomic__init_once(). */
790 init_sqlite(void *baton, apr_pool_t *pool)
792 if (sqlite3_libversion_number() < SVN_SQLITE_MIN_VERSION_NUMBER)
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());
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"));
811 /* If SQLite has been already initialized, sqlite3_config() returns
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);
819 SQLITE_ERR_MSG(sqlite3_initialize(), _("Could not initialize SQLite"));
821 #endif /* APR_HAS_THRADS */
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)
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;
840 SVN_ERR_MALFUNCTION();
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.
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;
851 #if !defined(WIN32) && !defined(SVN_SQLITE_INLINE)
852 if (mode == svn_sqlite__mode_rwcreate)
854 svn_node_kind_t kind;
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)
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);
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. */
880 const char *vFs = NULL;
882 #if defined(WIN32) && SQLITE_VERSION_AT_LEAST(3, 8, 1)
883 if (strlen(path) > 248)
886 vFs = "win32-longpath"; /* Enable long paths in sqlite */
888 /* Long paths must be absolute */
889 if (!svn_dirent_is_absolute(path))
890 SVN_ERR(svn_dirent_get_absolute(&path, path, scratch_pool));
892 /* Convert the path to a properly canonicalized \\?\C:\long\path */
893 SVN_ERR(svn_io__utf8_to_unicode_longpath(&win_path, path,
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,
903 /* ### SQLITE_CANTOPEN */
904 SQLITE_ERR_CLOSE(sqlite3_open_v2(path, &db->db3, flags, vFs),
910 timeout = BUSY_TIMEOUT;
912 /* Retry until timeout when database is busy. */
913 SQLITE_ERR_CLOSE(sqlite3_busy_timeout(db->db3, timeout),
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. */
923 close_apr(void *data)
925 svn_sqlite__db_t *db = data;
926 svn_error_t *err = SVN_NO_ERROR;
930 /* Check to see if we've already closed this database. */
934 /* Finalize any prepared statements. */
935 if (db->prepared_stmts)
937 for (i = 0; i < db->nbr_statements + STMT_INTERNAL_LAST; i++)
939 if (db->prepared_stmts[i])
941 if (i < db->nbr_statements
942 && db->prepared_stmts[i]->needs_reset)
945 const char *stmt_text = db->statement_strings[i];
946 SVN_UNUSED(stmt_text);
948 SVN_ERR_MALFUNCTION_NO_RETURN();
950 err = svn_error_compose_create(err,
951 svn_sqlite__reset(db->prepared_stmts[i]));
954 err = svn_error_compose_create(
955 svn_sqlite__finalize(db->prepared_stmts[i]), err);
960 result = sqlite3_close(db->db3);
962 /* If there's a pre-existing error, return it. */
965 result = err->apr_err;
966 svn_error_clear(err);
970 if (result != SQLITE_OK)
971 return SQLITE_ERROR_CODE(result); /* ### lossy */
978 #ifdef SVN_UNICODE_NORMALIZATION_FIXES
979 /* Unicode normalizing collation for WC paths */
981 collate_ucs_nfd(void *baton,
982 int len1, const void *key1,
983 int len2, const void *key2)
985 svn_sqlite__db_t *db = baton;
988 if (svn_utf__normcmp(key1, len1, key2, len2,
989 &db->sqlext_buf1, &db->sqlext_buf2, &result))
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();
1003 glob_like_ucs_nfd_common(sqlite3_context *context,
1004 int argc, sqlite3_value **argv,
1005 svn_boolean_t sql_like)
1007 svn_sqlite__db_t *const db = sqlite3_user_data(context);
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]);
1014 const char *escape = NULL;
1015 apr_size_t escape_len = 0;
1017 svn_boolean_t match;
1020 if (pattern_len > SQLITE_MAX_LIKE_PATTERN_LENGTH)
1022 sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
1026 if (argc == 3 && sql_like)
1028 escape = (void*)sqlite3_value_text(argv[2]);
1029 escape_len = sqlite3_value_bytes(argv[2]);
1032 if (pattern && string)
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,
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);
1051 sqlite3_result_int(context, match);
1055 /* Unicode normalizing implementation of GLOB */
1057 glob_ucs_nfd(sqlite3_context *context,
1058 int argc, sqlite3_value **argv)
1060 glob_like_ucs_nfd_common(context, argc, argv, FALSE);
1063 /* Unicode normalizing implementation of LIKE */
1065 like_ucs_nfd(sqlite3_context *context,
1066 int argc, sqlite3_value **argv)
1068 glob_like_ucs_nfd_common(context, argc, argv, TRUE);
1070 #endif /* SVN_UNICODE_NORMALIZATION_FIXES */
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)
1079 SVN_ERR(svn_atomic__init_once(&sqlite_init_state,
1080 init_sqlite, NULL, scratch_pool));
1082 *db = apr_pcalloc(result_pool, sizeof(**db));
1084 SVN_ERR(internal_open(*db, path, mode, timeout, scratch_pool));
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)
1094 sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, (*db)->db3, 0x800);
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);
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),
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...
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
1116 SQLITE_ERR_CLOSE(sqlite3_create_function((*db)->db3, "glob", 2,
1117 SQLITE_UTF8 | SQLITE_DETERMINISTIC,
1118 *db, glob_ucs_nfd, NULL, NULL),
1120 SQLITE_ERR_CLOSE(sqlite3_create_function((*db)->db3, "like", 2,
1121 SQLITE_UTF8 | SQLITE_DETERMINISTIC,
1122 *db, like_ucs_nfd, NULL, NULL),
1124 SQLITE_ERR_CLOSE(sqlite3_create_function((*db)->db3, "like", 3,
1125 SQLITE_UTF8 | SQLITE_DETERMINISTIC,
1126 *db, like_ucs_nfd, NULL, NULL),
1128 #endif /* SVN_UNICODE_NORMALIZATION_FIXES */
1130 #ifdef SQLITE3_DEBUG
1131 sqlite3_trace((*db)->db3, sqlite_tracer, (*db)->db3);
1133 #ifdef SQLITE3_PROFILE
1134 sqlite3_profile((*db)->db3, sqlite_profiler, (*db)->db3);
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
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
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.
1155 ### Maybe switch to NORMAL(1) when we use larger transaction
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;"
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;"),
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
1185 SVN_SQLITE__ERR_CLOSE(exec_sql(*db, "PRAGMA reverse_unordered_selects=ON;"),
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;"));
1194 /* Store the provided statements. */
1197 (*db)->statement_strings = statements;
1198 (*db)->nbr_statements = 0;
1199 while (*statements != NULL)
1202 (*db)->nbr_statements++;
1205 (*db)->prepared_stmts = apr_pcalloc(
1207 ((*db)->nbr_statements + STMT_INTERNAL_LAST)
1208 * sizeof(svn_sqlite__stmt_t *));
1212 (*db)->nbr_statements = 0;
1213 (*db)->prepared_stmts = apr_pcalloc(result_pool,
1214 (0 + STMT_INTERNAL_LAST)
1215 * sizeof(svn_sqlite__stmt_t *));
1218 (*db)->state_pool = result_pool;
1219 apr_pool_cleanup_register(result_pool, *db, close_apr, apr_pool_cleanup_null);
1221 return SVN_NO_ERROR;
1225 svn_sqlite__close(svn_sqlite__db_t *db)
1227 apr_status_t result = apr_pool_cleanup_run(db->state_pool, db, close_apr);
1229 if (result == APR_SUCCESS)
1230 return SVN_NO_ERROR;
1232 return svn_error_wrap_apr(result, NULL);
1235 static svn_error_t *
1236 reset_all_statements(svn_sqlite__db_t *db,
1237 svn_error_t *error_to_wrap)
1242 /* ### Should we reorder the errors in this specific case
1243 ### to avoid returning the normal error as top level error? */
1245 err = svn_error_compose_create(error_to_wrap,
1246 svn_error_create(SVN_ERR_SQLITE_RESETTING_FOR_ROLLBACK,
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]));
1257 static svn_error_t *
1258 rollback_transaction(svn_sqlite__db_t *db,
1259 svn_error_t *error_to_wrap)
1261 svn_sqlite__stmt_t *stmt;
1264 err = get_internal_statement(&stmt, db, STMT_INTERNAL_ROLLBACK_TRANSACTION);
1267 err = svn_error_trace(svn_sqlite__step_done(stmt));
1269 if (err && err->apr_err == SVN_ERR_SQLITE_BUSY)
1271 /* ### Houston, we have a problem!
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
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
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)),
1298 /* Rollback failed, use a specific error code. */
1299 err = svn_error_create(SVN_ERR_SQLITE_ROLLBACK_FAILED, err, NULL);
1302 return svn_error_compose_create(error_to_wrap, err);
1306 svn_sqlite__begin_transaction(svn_sqlite__db_t *db)
1308 svn_sqlite__stmt_t *stmt;
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;
1317 svn_sqlite__begin_immediate_transaction(svn_sqlite__db_t *db)
1319 svn_sqlite__stmt_t *stmt;
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;
1328 svn_sqlite__begin_savepoint(svn_sqlite__db_t *db)
1330 svn_sqlite__stmt_t *stmt;
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;
1339 svn_sqlite__finish_transaction(svn_sqlite__db_t *db,
1342 svn_sqlite__stmt_t *stmt;
1344 /* Commit or rollback the sqlite transaction. */
1347 return svn_error_trace(rollback_transaction(db, err));
1351 err = get_internal_statement(&stmt, db,
1352 STMT_INTERNAL_COMMIT_TRANSACTION);
1354 err = svn_error_trace(svn_sqlite__step_done(stmt));
1356 /* Need to rollback if the commit fails as well, because otherwise the
1357 db connection will be left in an unusable state.
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.
1366 See https://www.sqlite.org/lang_transaction.html
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. */
1374 return svn_error_trace(rollback_transaction(db, err));
1377 return SVN_NO_ERROR;
1381 svn_sqlite__finish_savepoint(svn_sqlite__db_t *db,
1384 svn_sqlite__stmt_t *stmt;
1390 err2 = get_internal_statement(&stmt, db,
1391 STMT_INTERNAL_ROLLBACK_TO_SAVEPOINT_SVN);
1395 err2 = svn_error_trace(svn_sqlite__step_done(stmt));
1397 if (err2 && err2->apr_err == SVN_ERR_SQLITE_BUSY)
1399 /* Ok, we have a major problem. Some statement is still open,
1400 which makes it impossible to release this savepoint.
1402 ### See huge comment in rollback_transaction() for
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)),
1412 err = svn_error_compose_create(err, err2);
1413 err2 = get_internal_statement(&stmt, db,
1414 STMT_INTERNAL_RELEASE_SAVEPOINT_SVN);
1417 err2 = svn_error_trace(svn_sqlite__step_done(stmt));
1419 return svn_error_compose_create(err, err2);
1422 SVN_ERR(get_internal_statement(&stmt, db,
1423 STMT_INTERNAL_RELEASE_SAVEPOINT_SVN));
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));
1431 svn_sqlite__with_transaction(svn_sqlite__db_t *db,
1432 svn_sqlite__transaction_callback_t cb_func,
1434 apr_pool_t *scratch_pool /* NULL allowed */)
1436 SVN_SQLITE__WITH_TXN(cb_func(cb_baton, db, scratch_pool), db);
1437 return SVN_NO_ERROR;
1441 svn_sqlite__with_immediate_transaction(
1442 svn_sqlite__db_t *db,
1443 svn_sqlite__transaction_callback_t cb_func,
1445 apr_pool_t *scratch_pool /* NULL allowed */)
1447 SVN_SQLITE__WITH_IMMEDIATE_TXN(cb_func(cb_baton, db, scratch_pool), db);
1448 return SVN_NO_ERROR;
1452 svn_sqlite__with_lock(svn_sqlite__db_t *db,
1453 svn_sqlite__transaction_callback_t cb_func,
1455 apr_pool_t *scratch_pool /* NULL allowed */)
1457 SVN_SQLITE__WITH_LOCK(cb_func(cb_baton, db, scratch_pool), db);
1458 return SVN_NO_ERROR;
1462 svn_sqlite__hotcopy(const char *src_path,
1463 const char *dst_path,
1464 apr_pool_t *scratch_pool)
1466 svn_sqlite__db_t *src_db;
1468 SVN_ERR(svn_sqlite__open(&src_db, src_path, svn_sqlite__mode_readonly,
1470 scratch_pool, scratch_pool));
1473 svn_sqlite__db_t *dst_db;
1474 sqlite3_backup *backup;
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");
1481 return svn_error_createf(SVN_ERR_SQLITE_ERROR, NULL,
1482 _("SQLite hotcopy failed for %s"), src_path);
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);
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
1497 if (rc1 == SQLITE_BUSY || rc1 == SQLITE_LOCKED)
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));
1508 SVN_ERR(svn_sqlite__close(src_db));
1510 SVN_ERR(svn_io_copy_perms(src_path, dst_path, scratch_pool));
1512 return SVN_NO_ERROR;
1515 struct function_wrapper_baton_t
1517 svn_sqlite__func_t func;
1522 wrapped_func(sqlite3_context *context,
1524 sqlite3_value *values[])
1526 struct function_wrapper_baton_t *fwb = sqlite3_user_data(context);
1527 svn_sqlite__context_t sctx;
1529 void *void_values = values;
1531 sctx.context = context;
1533 err = fwb->func(&sctx, argc, void_values, fwb->baton);
1538 sqlite3_result_error(context,
1539 svn_err_best_message(err, buf, sizeof(buf)),
1541 svn_error_clear(err);
1547 svn_sqlite__create_scalar_function(svn_sqlite__db_t *db,
1548 const char *func_name,
1550 svn_boolean_t deterministic,
1551 svn_sqlite__func_t func,
1555 struct function_wrapper_baton_t *fwb = apr_pcalloc(db->state_pool,
1561 eTextRep = SQLITE_ANY;
1563 eTextRep |= SQLITE_DETERMINISTIC;
1565 SQLITE_ERR(sqlite3_create_function(db->db3, func_name, argc, eTextRep,
1566 fwb, wrapped_func, NULL, NULL),
1569 return SVN_NO_ERROR;
1573 svn_sqlite__value_type(svn_sqlite__value_t *val)
1576 return sqlite3_value_type(v);
1580 svn_sqlite__value_text(svn_sqlite__value_t *val)
1583 return (const char *) sqlite3_value_text(v);
1587 svn_sqlite__result_null(svn_sqlite__context_t *sctx)
1589 sqlite3_result_null(sctx->context);
1593 svn_sqlite__result_int64(svn_sqlite__context_t *sctx, apr_int64_t val)
1595 sqlite3_result_int64(sctx->context, val);
1599 svn_sqlite__result_error(svn_sqlite__context_t *sctx, const char *msg, int num)
1601 sqlite3_result_error(sctx->context, msg, num);