Skip to content

Commit

Permalink
Avoid SQLite3 database corruption.
Browse files Browse the repository at this point in the history
Use journal_mode = TRUNCATE, synchronous = FULL.
  • Loading branch information
cgull authored and bapt committed Nov 8, 2022
1 parent 187d1b7 commit d94168d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
8 changes: 3 additions & 5 deletions libpkg/pkgdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,8 @@ static int
pkgdb_init(sqlite3 *sdb)
{
const char sql[] = ""
"PRAGMA journal_mode = TRUNCATE;"
"PRAGMA synchronous = FULL;"
"BEGIN;"
"CREATE TABLE packages ("
"id INTEGER PRIMARY KEY,"
Expand Down Expand Up @@ -2925,8 +2927,6 @@ int
pkgdb_begin_solver(struct pkgdb *db)
{
const char solver_sql[] = ""
"PRAGMA synchronous = OFF;"
"PRAGMA journal_mode = MEMORY;"
"BEGIN TRANSACTION;";
const char update_digests_sql[] = ""
"DROP INDEX IF EXISTS pkg_digest_id;"
Expand Down Expand Up @@ -2992,9 +2992,7 @@ int
pkgdb_end_solver(struct pkgdb *db)
{
const char solver_sql[] = ""
"END TRANSACTION;"
"PRAGMA synchronous = NORMAL;"
"PRAGMA journal_mode = DELETE;";
"END TRANSACTION;";

return (sql_exec(db->sqlite, solver_sql));
}
Expand Down
7 changes: 6 additions & 1 deletion libpkg/repo/binary/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,12 @@ pkg_repo_binary_init(struct pkg_repo *repo)

sqlite3_create_function(sqlite, "file_exists", 2, SQLITE_ANY, NULL,
sqlite_file_exists, NULL, NULL);
retcode = sql_exec(sqlite, "PRAGMA synchronous=default");

retcode = sql_exec(sqlite, "PRAGMA journal_mode=TRUNCATE;");
if (retcode != EPKG_OK)
return (retcode);

retcode = sql_exec(sqlite, "PRAGMA synchronous=FULL");
if (retcode != EPKG_OK)
return (retcode);

Expand Down
3 changes: 2 additions & 1 deletion libpkg/repo/binary/update.c
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,8 @@ pkg_repo_binary_update_proceed(const char *name, struct pkg_repo *repo,
sql_exec(sqlite, "PRAGMA mmap_size = 209715200;");
sql_exec(sqlite, "PRAGMA page_size = %d;", getpagesize());
sql_exec(sqlite, "PRAGMA foreign_keys = OFF;");
sql_exec(sqlite, "PRAGMA synchronous = OFF;");
sql_exec(sqlite, "PRAGMA journal_mode = TRUNCATE;");
sql_exec(sqlite, "PRAGMA synchronous = FULL;");

rc = pkgdb_transaction_begin_sqlite(sqlite, "REPO");
if (rc != EPKG_OK)
Expand Down

0 comments on commit d94168d

Please sign in to comment.