[VD]Add db curruption check routine 16/166316/1
authorJeesun Kim <iamjs.kim@samsung.com>
Mon, 8 Jan 2018 09:42:20 +0000 (18:42 +0900)
committerjeesun kim <iamjs.kim@samsung.com>
Tue, 9 Jan 2018 09:57:09 +0000 (09:57 +0000)
Change-Id: Iaf69be1c7f5fc8bd0ec3d1ec574dfa57015e07c3

server/cal_server_schema.c
server/db/cal_db.h

index 609e909..7e539e1 100644 (file)
@@ -57,6 +57,55 @@ static inline int __remake_db_file(void)
        return 0;
 }
 
+#ifdef TIZEN_PROFILE_TV
+static bool need_to_remove = false;
+static int __integrity_callback(void *pid, int argc, char **argv, char **notUsed2)
+{
+       CAL_FN_CALL();
+
+       if (NULL == argv || NULL == argv[0]) {
+               ERR("Invalid parameter");
+               need_to_remove = true;
+               return -1;
+       }
+
+       if (0 != strcmp(argv[0], "ok")) {
+               ERR("integrity_check() Fail(argv[%s])", argv[0]);
+               need_to_remove = true;
+               return -1;
+       }
+
+       DBG("integrity_check done");
+       return 0;
+}
+
+void __integrity_check(void)
+{
+       CAL_FN_CALL();
+
+       int ret = 0;
+       sqlite3 *db = NULL;
+
+       ret = db_util_open(CAL_DB_FILE, &db, 0);
+       if (SQLITE_CORRUPT == ret) {
+               ERR("db_util_open() Fail(%d)", ret);
+               need_to_remove = true;
+       }
+
+       ret = sqlite3_exec(db, "pragma integrity_check", __integrity_callback, NULL, NULL);
+       db_util_close(db);
+
+       if (true == need_to_remove || SQLITE_CORRUPT == ret || SQLITE_NOTADB == ret) {
+               ERR("need_to_remove(%d)", ret);
+               remove(CAL_DB_SHM_FILE);
+               remove(CAL_DB_WAL_FILE);
+               remove(CAL_DB_FILE);
+               need_to_remove = false;
+       }
+}
+
+#endif
+
 static inline int __check_db_file(void)
 {
        int fd = -1;
@@ -73,6 +122,10 @@ static inline int __check_db_file(void)
 
 int cal_server_schema_check(void)
 {
+#ifdef TIZEN_PROFILE_TV
+       __integrity_check();
+#endif
+
        if (__check_db_file())
                __remake_db_file();
        return 0;
index 2f7759c..cd9aa15 100644 (file)
@@ -27,6 +27,8 @@
 #define TZ_DATA_PATH  tzplatform_getenv(TZ_USER_DATA)
 #define CAL_DATA_PATH tzplatform_mkpath(TZ_USER_DATA, "calendar-service")
 #define CAL_DB_FILE tzplatform_mkpath(TZ_USER_DB, "privacy/.calendar-service.db")
+#define CAL_DB_SHM_FILE tzplatform_mkpath(TZ_USER_DB, "privacy/.calendar-service.db-shm")
+#define CAL_DB_WAL_FILE tzplatform_mkpath(TZ_USER_DB, "privacy/.calendar-service.db-wal")
 
 /* For Security */
 #define CAL_SECURITY_FILE_GROUP 5000