Calendar-Service DataBase Deleted when TV is Facory reset 82/195582/4
authorkamaljeet <kamal.jc@samsung.com>
Fri, 14 Dec 2018 12:12:57 +0000 (17:42 +0530)
committerkamaljeet <kamal.jc@samsung.com>
Thu, 20 Dec 2018 10:03:46 +0000 (15:33 +0530)
Change-Id: I77da4af8039a3cc5f0eda9c699d94c498a36ea7a
Signed-off-by: kamaljeet <kamal.jc@samsung.com>
server/cal_server.c
server/cal_server_schema.c
server/cal_server_schema.h

index 97b29d2..49f99ee 100644 (file)
  *
  */
 
-#include <stdio.h>
 #include <stdlib.h>
+#include <system_info.h>
+#include <system_info_type.h>
+#include <tizen.h>
+#include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <sys/param.h>
 #include <unistd.h>
+#include <vconf.h>
+#include <sqlite3.h>
+#include <db-util.h>
 
+#include "schema.h"
+#include "cal_server_profile.h"
 #include "calendar.h"
 #include "cal_typedef.h"
 #include "cal_internal.h"
@@ -50,6 +58,8 @@
 
 #define CAL_TIMEOUT_FOR_DECLARE 1
 #define CAL_TIMEOUT_FOR_DEFAULT 0
+#define CAL_RESET "db/calendar_service/reset_status"
+
 
 GMainLoop* main_loop = NULL;
 static int cal_timeout = 0;
@@ -63,6 +73,8 @@ void cal_server_quit_loop(void)
 
 static int _cal_server_init(void)
 {
+
+
        int ret = 0;
 #if !GLIB_CHECK_VERSION(2, 35, 0)
        g_type_init();
@@ -89,6 +101,134 @@ static void _cal_server_deinit(void)
        cal_disconnect();
 }
 
+
+static bool bNeedToRemove = false;
+int dbIntegrityCheckCallback(void *pid, int argc, char **argv, char **notUsed2)
+{
+       if (!strncmp(argv[0], "ok", strlen("ok"))) {
+               DBG("db integrity check done - no error");
+       } else {
+               ERR("db integrity check failed : %d , %s", argc, argv[0]);
+               bNeedToRemove = true;
+       }
+       return 0;
+}
+
+
+int checkDb(void)
+{
+       INFO("---------------------[checkDb START]------------------------");
+       if (open_file() == 0)
+       {
+               int r = 0;
+               sqlite3 *db = NULL;
+
+               r = sqlite3_open(CAL_DB_FILE, &db);
+               if (r != SQLITE_OK) {
+                       ERR("DB open: [%s], remove", sqlite3_errmsg(db));
+                       sqlite3_close(db);
+                       if(remove(CAL_DB_FILE)!=0)
+                               ERR("Fail to remove the FIle");
+                       return -1;
+               }
+
+               r = sqlite3_exec(db, "pragma integrity_check", dbIntegrityCheckCallback, NULL, NULL);
+               if (r == SQLITE_CORRUPT || r == SQLITE_NOTADB) {
+                       ERR("SQLITE_ERROR [%d]", r);
+                       ERR("SQLITE_ERROR [%d]", r);
+                       bNeedToRemove = true;
+               }
+
+               if (bNeedToRemove == true) {
+                       sqlite3_close(db);
+                       if(remove(CAL_DB_FILE)!=0)
+                               ERR("Fail to remove the FIle");
+                       else
+                               ERR("db deleted");
+
+                       bNeedToRemove = false;
+                       return -1;
+               }
+               sqlite3_close(db);
+               DBG("DBFILE [%s] pragma intergrity check OK", CAL_DB_FILE);
+               return 0;
+       }
+       INFO("---------------------[checkDb END]------------------------");
+       return -1;
+}
+
+
+
+int deleteDBFile()
+{
+       INFO("---------------------[deleteDBFile START]------------------------");
+       int ret = 0;
+
+       if(checkDb() == 0){
+               ret = remove(CAL_DB_FILE);
+               if (ret != 0)
+               {
+                       ERR("Not able to delete DB file");
+                       return -1;
+               }
+
+       }
+       else{
+               ERR("deleteDBFile is not progressed : No DB file to remove");
+               return -1;
+       }
+
+
+       INFO("Successfully deleted!!");
+       return 0;
+}
+
+
+
+void checkFactoryReset()
+{
+       INFO("---------------------[service_app_create START]------------------------");
+       int value = 0;
+       if (vconf_get_int(CAL_RESET, &value) == 0)
+       {
+               if (value == 1 || value == 2)
+               {
+                       INFO("need to cleanup db %d", value);
+                       int ret = 0;
+                       ret = deleteDBFile();
+                       if(ret != 0)
+                       {
+                               ERR("Calendar DB delete fail! What should I do?");
+                       }
+
+                       if(remake_db()!=0)
+                       {
+                               ERR("Remake of Calendar DB failed! ");
+                       }
+
+                       ret = vconf_set_int(CAL_RESET, 0);
+                       if (ret != 0) {
+                               ERR("retry vconf_set_int %d", ret);
+                               ret = vconf_set_int(CAL_RESET, 0);
+                               if (ret != 0) {
+                                       ERR("vconf_set_int failed %d", ret);
+                               }
+                       }
+               }
+               else
+               {
+                       ERR("don't need to cleanup db %d", value);
+               }
+       }
+       else
+       {
+               ERR("vconf_get_int failed");
+       }
+       INFO("---------------------[service_app_create END]------------------------");
+}
+
+
+
 static int _cal_server_main(void)
 {
        main_loop = g_main_loop_new(NULL, FALSE);
@@ -154,6 +294,12 @@ int main(int argc, char *argv[])
        cal_server_schema_check();
        cal_server_update();
 
+#ifdef TIZEN_PROFILE_TV
+        checkFactoryReset();
+#endif
+
+
+
        _cal_server_init();
        cal_server_account_init();
 
@@ -180,4 +326,3 @@ int main(int argc, char *argv[])
 
        return 0;
 }
-
index 4168f5e..1a259db 100644 (file)
@@ -59,6 +59,17 @@ static int __remake_db(void)
        return 0;
 }
 
+
+int remake_db(void)
+{
+       CAL_FN_CALL();
+        int ret = __remake_db();
+        if(ret == -1)
+               ERR("Not able to remake the database");
+        return ret;
+}
+
+
 static bool need_to_remove = false;
 static int __integrity_callback(void *pid, int argc, char **argv, char **notUsed2)
 {
@@ -98,6 +109,24 @@ static int __open_file(void)
        return 0;
 }
 
+
+int open_file(void)
+{
+       CAL_FN_CALL();
+       if(access(CAL_DB_FILE, F_OK) == 0 )
+       {
+               int ret = __open_file();
+               if(ret == -1)
+                       ERR("Not able to open the file");
+               return ret;
+       }
+       else
+       {
+               ERR("File is not accessible");
+               return -1;
+       }
+}
+
 static int __check_integrity(void)
 {
        CAL_FN_CALL();
index aa9c959..f9ec65b 100644 (file)
 #ifndef __CAL_SERVER_SCHEMA_H__
 #define __CAL_SERVER_SCHEMA_H__
 
+int remake_db(void);
+
+int open_file(void);
+
 int cal_server_schema_check(void);
 
 #endif /* __CAL_SERVER_SCHEMA_H__ */