Modified to create the DB tables when sticker daemon was not launched 74/223874/1
authorInHong Han <inhong1.han@samsung.com>
Tue, 4 Feb 2020 11:24:54 +0000 (20:24 +0900)
committerInHong Han <inhong1.han@samsung.com>
Tue, 4 Feb 2020 11:24:54 +0000 (20:24 +0900)
Change-Id: Ib828fde1922bd96d7664b3fd1fc13a7d4e4a5984

sticker-parser/sticker-parser.c

index d0437c1..f8fba16 100644 (file)
 #define LOG_TAG "STICKER_PARSER"
 
 #define STICKER_DIRECTORY tzplatform_mkpath(TZ_SYS_SHARE, "sticker-data")
+#define STICKER_INFO_CREATE_TABLE "CREATE TABLE IF NOT EXISTS sticker_info(sticker_info_id INTEGER PRIMARY KEY AUTOINCREMENT, app_id TEXT NOT NULL, type INTEGER NOT NULL, uri TEXT NOT NULL, thumbnail TEXT, description TEXT, group_name TEXT NOT NULL, date TEXT NOT NULL);"
+#define STICKER_KEYWORD_INFO_CREATE_TABLE "CREATE TABLE IF NOT EXISTS sticker_keyword_info(keyword_id INTEGER PRIMARY KEY AUTOINCREMENT, sticker_info_id INTEGER, keyword TEXT NOT NULL, FOREIGN KEY (sticker_info_id) REFERENCES sticker_info(sticker_info_id) ON DELETE CASCADE)"
 #define UIFW_ID           502
 #define APPFW_ID          301
 #define MAX_ERROR_BUFFER  256
 
 static char error_buffer[MAX_ERROR_BUFFER];
+static gboolean is_corrupted = FALSE;
 
 typedef struct metadata {
     const char *key;
@@ -74,6 +77,105 @@ static int __get_int_from_object(JsonObject *object, const char *key)
     return type;
 }
 
+static void __recover_db()
+{
+    int ret;
+    sqlite3 *db = NULL;
+    char *err = NULL;
+    const char *db_path = tzplatform_mkpath(TZ_SYS_DB, ".sticker_info.db");
+
+    LOGD("Start to recover sticker db");
+    if (unlink(db_path) == -1)
+        LOGE("Failed to remove db file");
+
+    ret = sqlite3_open_v2(db_path, &db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, NULL);
+    if (ret != SQLITE_OK) {
+        LOGE("Failed to open db : %s", sqlite3_errmsg(db));
+        if (unlink(db_path) == -1)
+            LOGE("Failed to remove db file");
+        goto cleanup;
+    }
+
+    ret = sqlite3_exec(db, STICKER_INFO_CREATE_TABLE, NULL, NULL, &err);
+    if (ret != SQLITE_OK) {
+        LOGE("Failed to create sticker_info table : %s" , err);
+        goto cleanup;
+    }
+
+    ret = sqlite3_exec(db, STICKER_KEYWORD_INFO_CREATE_TABLE, NULL, NULL, &err);
+    if (ret != SQLITE_OK) {
+        LOGE("Failed to create sticker_keyword_info table : %s", err);
+        goto cleanup;
+    }
+
+    is_corrupted = FALSE;
+
+cleanup:
+    if (err)
+        sqlite3_free(err);
+
+    if (db)
+        sqlite3_close(db);
+}
+
+static int __integrity_check_cb(void *pid, int argc, char **argv, char **notUsed)
+{
+    if (strcmp(argv[0], "ok") != 0) {
+        LOGE("DB integrity check failed : %s", argv[0]);
+        is_corrupted = TRUE;
+        return -1;
+    }
+
+    LOGD("Result integrity : %s", argv[0]);
+    return 0;
+}
+
+static void __db_init()
+{
+    int ret;
+    sqlite3 *db = NULL;
+    char *err = NULL;
+    const char *db_path = tzplatform_mkpath(TZ_SYS_DB, ".sticker_info.db");
+
+    ret = sqlite3_open_v2(db_path, &db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, NULL);
+    if (ret != SQLITE_OK) {
+        LOGE("Failed to open db : %s", sqlite3_errmsg(db));
+        goto cleanup;
+    }
+
+    ret = sqlite3_exec(db, STICKER_INFO_CREATE_TABLE, NULL, NULL, &err);
+    if (ret != SQLITE_OK) {
+        LOGE("Failed to create sticker_info table : %s" , err);
+        goto cleanup;
+    }
+
+    ret = sqlite3_exec(db, STICKER_KEYWORD_INFO_CREATE_TABLE, NULL, NULL, &err);
+    if (ret != SQLITE_OK) {
+        LOGE("Failed to create sticker_keyword_info table : %s", err);
+        goto cleanup;
+    }
+
+    ret = sqlite3_exec(db, "PRAGMA journal_mode = WAL", NULL, NULL, &err);
+    if (ret != SQLITE_OK) {
+        LOGE("Failed to set journal_mode : %s", err);
+        goto cleanup;
+    }
+
+    ret = sqlite3_exec(db, "PRAGMA integrity_check", __integrity_check_cb, NULL, &err);
+    if (ret != SQLITE_OK)
+        LOGE("Failed to check integrity : %s", err);
+
+cleanup:
+    if (err)
+        sqlite3_free(err);
+
+    if (db)
+        sqlite3_close(db);
+
+    if (ret == SQLITE_CORRUPT || ret == SQLITE_NOTADB || is_corrupted)
+        __recover_db();
+}
+
 static sqlite3 *__db_open(const char *path)
 {
     int ret;
@@ -627,6 +729,8 @@ int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgid, const char *appid, GList *
     char *file_path = NULL;
     int ret = 0;
 
+    __db_init();
+
     ret = package_info_create(pkgid, &package_info);
     if (ret != PACKAGE_MANAGER_ERROR_NONE || package_info == NULL) {
         LOGE("failed to create package_info. ret: %d", ret);