#define APP_BG_CATEGORY_IOTCOMM_STR "iot-communication"
#define APP_BG_CATEGORY_SYSTEM "system"
+#define DB_POSTFIX_SHM "-shm"
+#define DB_POSTFIX_WAL "-wal"
+
#define REGULAR_USER 5000
static inline uid_t __getuid(void)
{
static int __set_db_permission(const char *path, uid_t uid)
{
int fd;
- const char *files[2];
- char journal_file[BUFSIZE];
+ const char *files[3];
+ char shm_file[BUFSIZE];
+ char wal_file[BUFSIZE];
struct stat sb;
mode_t mode;
struct passwd pwd;
uid = pwd.pw_uid;
}
- snprintf(journal_file, sizeof(journal_file), "%s-journal", path);
+ snprintf(shm_file, sizeof(shm_file), "%s%s", path, DB_POSTFIX_SHM);
+ snprintf(wal_file, sizeof(wal_file), "%s%s", path, DB_POSTFIX_WAL);
files[0] = path;
- files[1] = journal_file;
+ files[1] = shm_file;
+ files[2] = wal_file;
ret = getpwuid_r(uid, &pwd, buf, sizeof(buf), &result);
if (result == NULL) {
return -1;
}
- for (i = 0; i < 2; i++) {
+ for (i = 0; i < 3; i++) {
fd = open(files[i], O_RDONLY);
if (fd == -1) {
_LOGE("open %s failed: %d", files[i], errno);
static int __initialize_db(sqlite3 *db, const char *dbpath, uid_t uid)
{
const char **queries;
-
+ int persist_wal = 1;
if (__set_db_version(db))
return -1;
+ int ret = sqlite3_exec(db, "PRAGMA journal_mode=WAL", NULL, NULL, NULL);
+ if (ret != SQLITE_OK) {
+ _LOGE("failed to set wal mode: %s",
+ sqlite3_errmsg(db));
+ sqlite3_close_v2(db);
+ return ret;
+ }
+
+ ret = sqlite3_file_control(db, NULL,
+ SQLITE_FCNTL_PERSIST_WAL, &persist_wal);
+ if (ret != SQLITE_OK) {
+ _LOGE("failed to execute sqlite3_file_control: %s",
+ sqlite3_errmsg(db));
+ sqlite3_close_v2(db);
+ return ret;
+ }
+
if (strstr(dbpath, ".pkgmgr_parser.db")) {
queries = parser_init_queries;
} else if (strstr(dbpath, ".pkgmgr_cert.db")) {
return PM_PARSER_R_OK;
}
+static int _internal_callback(void *userdata, sqlite3 *db,
+ const char *db_name, int page_num)
+{
+ int ret = sqlite3_wal_checkpoint_v2(db, NULL, SQLITE_CHECKPOINT_PASSIVE,
+ NULL, NULL);
+ if (ret != SQLITE_OK) {
+ _LOGE("failed to checkpoint: %s",
+ sqlite3_errmsg(db));
+ return SQLITE_ERROR;
+ }
+ return SQLITE_OK;
+}
+
static int __open_db(uid_t uid, const char *path, sqlite3 **db, int flags)
{
int ret;
-
- /* FIXME: always open with OPEN_CREATE flag for keeping previous
- * implementation
- */
- if (flags & SQLITE_OPEN_READWRITE)
- flags = flags | SQLITE_OPEN_CREATE;
+ int persist_wal = 1;
ret = sqlite3_open_v2(path, db, flags, NULL);
if (ret != SQLITE_OK)
}
}
+ sqlite3_wal_hook(*db, _internal_callback, NULL);
+
+ ret = sqlite3_file_control(*db, NULL,
+ SQLITE_FCNTL_PERSIST_WAL, &persist_wal);
+ if (ret != SQLITE_OK) {
+ _LOGE("failed to execute sqlite3_file_control: %s",
+ sqlite3_errmsg(*db));
+ sqlite3_close_v2(*db);
+ return ret;
+ }
+
ret = sqlite3_exec(*db, "PRAGMA foreign_keys=ON", NULL, NULL, NULL);
if (ret != SQLITE_OK) {
_LOGE("failed to enable foreign key support: %s",
int __open_db(const char *path, sqlite3 **db, int flags)
{
int ret;
+ int persist_wal = 1;
+ int no_checkpoint = 1;
ret = sqlite3_open_v2(path, db, flags, NULL);
if (ret != SQLITE_OK)
return ret;
}
+ ret = sqlite3_file_control(*db, NULL,
+ SQLITE_FCNTL_PERSIST_WAL, &persist_wal);
+ if (ret != SQLITE_OK) {
+ _LOGE("failed to sqlite3_file_control: %s",
+ sqlite3_errmsg(*db));
+ sqlite3_close_v2(*db);
+ return ret;
+ }
+
+ ret = sqlite3_wal_autocheckpoint(*db, 0);
+ if (ret != SQLITE_OK) {
+ _LOGE("failed to sqlite3_wal_autocheckpoint: %s",
+ sqlite3_errmsg(*db));
+ sqlite3_close_v2(*db);
+ return ret;
+ }
+
+ ret = sqlite3_db_config(*db, SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE,
+ &no_checkpoint);
+ if (ret != SQLITE_OK) {
+ _LOGE("failed to execute sqlite3_db_config: %s",
+ sqlite3_errmsg(*db));
+ sqlite3_close_v2(*db);
+ return ret;
+ }
+
return ret;
}