Fix db integrity check 76/211076/1
authorAbhay agarwal <ay.agarwal@samsung.com>
Mon, 29 Jul 2019 08:28:29 +0000 (13:58 +0530)
committersaerome.kim <saerome.kim@samsung.com>
Mon, 29 Jul 2019 23:58:09 +0000 (08:58 +0900)
Change-Id: I9227598ad860ea925dce54cd6912ce74086d0af6
Signed-off-by: Abhay agarwal <ay.agarwal@samsung.com>
ua-daemon/src/ua-manager-device-db.c
ua-daemon/src/ua-manager-device-service-db.c
ua-daemon/src/ua-manager-service-db.c
ua-daemon/src/ua-manager-user-db.c

index 5a97dc3..2dfa5c5 100644 (file)
@@ -166,6 +166,30 @@ static int __ua_device_db_busy(void *user, int attempts)
        return 1;
 }
 
+static int __uam_db_check_integrity_cb(void *err, int count, char **data, char **columns)
+{
+       FUNC_ENTRY;
+       int i;
+
+       for (i = 0; i < count; i++) {
+               UAM_DBG("%s = %s\n", columns[i], data[i] ? data[i] : "NULL");
+               if (!g_strcmp0(columns[i], "integrity_check") && !g_strcmp0(data[i], "ok")) return 0;
+       }
+       return 1;
+}
+
+static int __uam_db_check_table_cb(void *err, int count, char **data, char **columns)
+{
+       FUNC_ENTRY;
+       int i;
+
+       for (i = 0; i < count; i++) {
+               UAM_DBG("%s = %s\n", columns[i], data[i] ? data[i] : "NULL");
+               if (!g_strcmp0(columns[i], "count") && !g_strcmp0(data[i], "1")) return 0;
+       }
+       return 1;
+}
+
 static int __ua_device_db_initialize_once(void)
 {
        FUNC_ENTRY;
@@ -177,13 +201,11 @@ static int __ua_device_db_initialize_once(void)
        }
 
        res = sqlite3_open(DATABASE_FULL_PATH, &database);
-
        if (res != SQLITE_OK) {
                // TODO : Can add logic of retry if not able to open
                UAM_ERR("Error in opening database %s: %s", DATABASE_FULL_PATH, sqlite3_errmsg(database));
                return UAM_ERROR_DB_FAILED;
        }
-
        UAM_DBG("Successfully opened database");
 
        size_t db_size = _uam_getFilesize(DATABASE_FULL_PATH);
@@ -193,8 +215,7 @@ static int __ua_device_db_initialize_once(void)
        }
        UAM_DBG("Database size: %d", db_size);
 
-       res = sqlite3_exec(database, "PRAGMA integrity_check", 0, 0, 0);
-
+       res = sqlite3_exec(database, "PRAGMA integrity_check", __uam_db_check_integrity_cb, 0, 0);
        if (res != SQLITE_OK) {
                UAM_ERR("Can't verify database integrity %s",
                                sqlite3_errmsg(database));
@@ -205,8 +226,20 @@ static int __ua_device_db_initialize_once(void)
        }
        UAM_DBG("Successfully verified database integrity");
 
-       res = sqlite3_exec(database, "PRAGMA locking_mode = NORMAL", 0, 0, 0);
+       res = sqlite3_exec(database,
+               "SELECT count(*) as count FROM sqlite_master WHERE type='table' AND name='devices'",
+               __uam_db_check_table_cb, 0, 0);
+       if (res != SQLITE_OK) {
+               UAM_ERR("Can't verify table creation %s",
+                               sqlite3_errmsg(database));
+               sqlite3_close(database);
+               database = NULL;
+               FUNC_EXIT;
+               return UAM_ERROR_DB_FAILED;
+       }
+       UAM_DBG("Successfully verified table creation");
 
+       res = sqlite3_exec(database, "PRAGMA locking_mode = NORMAL", 0, 0, 0);
        if (res != SQLITE_OK) {
                UAM_ERR("Can't set locking mode %s, skip set busy handler.",
                                sqlite3_errmsg(database));
index 053709e..82d617a 100644 (file)
@@ -143,6 +143,30 @@ static int __ua_device_service_db_busy(void *user, int attempts)
        return 1;
 }
 
+static int __uam_db_check_integrity_cb(void *err, int count, char **data, char **columns)
+{
+       FUNC_ENTRY;
+       int i;
+
+       for (i = 0; i < count; i++) {
+               UAM_DBG("%s = %s\n", columns[i], data[i] ? data[i] : "NULL");
+               if (!g_strcmp0(columns[i], "integrity_check") && !g_strcmp0(data[i], "ok")) return 0;
+       }
+       return 1;
+}
+
+static int __uam_db_check_table_cb(void *err, int count, char **data, char **columns)
+{
+       FUNC_ENTRY;
+       int i;
+
+       for (i = 0; i < count; i++) {
+               UAM_DBG("%s = %s\n", columns[i], data[i] ? data[i] : "NULL");
+               if (!g_strcmp0(columns[i], "count") && !g_strcmp0(data[i], "1")) return 0;
+       }
+       return 1;
+}
+
 static int __ua_device_service_db_initialize_once(void)
 {
        FUNC_ENTRY;
@@ -154,13 +178,11 @@ static int __ua_device_service_db_initialize_once(void)
        }
 
        res = sqlite3_open(DATABASE_FULL_PATH, &database);
-
        if (res != SQLITE_OK) {
                // TODO : Can add logic of retry if not able to open
                UAM_ERR("Error in opening database %s: %s", DATABASE_FULL_PATH, sqlite3_errmsg(database));
                return UAM_ERROR_DB_FAILED;
        }
-
        UAM_DBG("Successfully opened database");
 
        size_t db_size = _uam_getFilesize(DATABASE_FULL_PATH);
@@ -170,8 +192,7 @@ static int __ua_device_service_db_initialize_once(void)
        }
        UAM_DBG("Database size: %d", db_size);
 
-       res = sqlite3_exec(database, "PRAGMA integrity_check", 0, 0, 0);
-
+       res = sqlite3_exec(database, "PRAGMA integrity_check", __uam_db_check_integrity_cb, 0, 0);
        if (res != SQLITE_OK) {
                UAM_ERR("Can't verify database integrity %s",
                                sqlite3_errmsg(database));
@@ -182,8 +203,20 @@ static int __ua_device_service_db_initialize_once(void)
        }
        UAM_DBG("Successfully verified database integrity");
 
-       res = sqlite3_exec(database, "PRAGMA locking_mode = NORMAL", 0, 0, 0);
+       res = sqlite3_exec(database,
+               "SELECT count(*) as count FROM sqlite_master WHERE type='table' AND name='device_services'",
+               __uam_db_check_table_cb, 0, 0);
+       if (res != SQLITE_OK) {
+               UAM_ERR("Can't verify table creation %s",
+                               sqlite3_errmsg(database));
+               sqlite3_close(database);
+               database = NULL;
+               FUNC_EXIT;
+               return UAM_ERROR_DB_FAILED;
+       }
+       UAM_DBG("Successfully verified table creation");
 
+       res = sqlite3_exec(database, "PRAGMA locking_mode = NORMAL", 0, 0, 0);
        if (res != SQLITE_OK) {
                UAM_ERR("Can't set locking mode %s, skip set busy handler.",
                                sqlite3_errmsg(database));
index 90f9be0..38530c1 100644 (file)
@@ -146,6 +146,30 @@ static int __ua_service_db_busy(void *user, int attempts)
        return 1;
 }
 
+static int __uam_db_check_integrity_cb(void *err, int count, char **data, char **columns)
+{
+       FUNC_ENTRY;
+       int i;
+
+       for (i = 0; i < count; i++) {
+               UAM_DBG("%s = %s\n", columns[i], data[i] ? data[i] : "NULL");
+               if (!g_strcmp0(columns[i], "integrity_check") && !g_strcmp0(data[i], "ok")) return 0;
+       }
+       return 1;
+}
+
+static int __uam_db_check_table_cb(void *err, int count, char **data, char **columns)
+{
+       FUNC_ENTRY;
+       int i;
+
+       for (i = 0; i < count; i++) {
+               UAM_DBG("%s = %s\n", columns[i], data[i] ? data[i] : "NULL");
+               if (!g_strcmp0(columns[i], "count") && !g_strcmp0(data[i], "1")) return 0;
+       }
+       return 1;
+}
+
 static int __ua_service_db_initialize_once(void)
 {
        FUNC_ENTRY;
@@ -157,24 +181,21 @@ static int __ua_service_db_initialize_once(void)
        }
 
        res = sqlite3_open(DATABASE_FULL_PATH, &database);
-
        if (res != SQLITE_OK) {
                // TODO : Can add logic of retry if not able to open
                UAM_ERR("Error in opening database %s: %s", DATABASE_FULL_PATH, sqlite3_errmsg(database));
                return UAM_ERROR_DB_FAILED;
        }
-
        UAM_DBG("Successfully opened database");
 
        size_t db_size = _uam_getFilesize(DATABASE_FULL_PATH);
        if (db_size == 0) {
-       UAM_ERR("Database size is 0");
-       return UAM_ERROR_DB_FAILED;
+               UAM_ERR("Database size is 0");
+               return UAM_ERROR_DB_FAILED;
        }
        UAM_DBG("Database size: %d", db_size);
 
-       res = sqlite3_exec(database, "PRAGMA integrity_check", 0, 0, 0);
-
+       res = sqlite3_exec(database, "PRAGMA integrity_check", __uam_db_check_integrity_cb, 0, 0);
        if (res != SQLITE_OK) {
                UAM_ERR("Can't verify database integrity %s",
                                sqlite3_errmsg(database));
@@ -185,8 +206,20 @@ static int __ua_service_db_initialize_once(void)
        }
        UAM_DBG("Successfully verified database integrity");
 
-       res = sqlite3_exec(database, "PRAGMA locking_mode = NORMAL", 0, 0, 0);
+       res = sqlite3_exec(database,
+               "SELECT count(*) as count FROM sqlite_master WHERE type='table' AND name='services'",
+               __uam_db_check_table_cb, 0, 0);
+       if (res != SQLITE_OK) {
+               UAM_ERR("Can't verify table creation %s",
+                               sqlite3_errmsg(database));
+               sqlite3_close(database);
+               database = NULL;
+               FUNC_EXIT;
+               return UAM_ERROR_DB_FAILED;
+       }
+       UAM_DBG("Successfully verified table creation");
 
+       res = sqlite3_exec(database, "PRAGMA locking_mode = NORMAL", 0, 0, 0);
        if (res != SQLITE_OK) {
                UAM_ERR("Can't set locking mode %s, skip set busy handler.",
                                sqlite3_errmsg(database));
index f6528ae..d3cc15e 100644 (file)
@@ -152,6 +152,30 @@ static int __ua_db_busy(void *user, int attempts)
        return 1;
 }
 
+static int __uam_db_check_integrity_cb(void *err, int count, char **data, char **columns)
+{
+       FUNC_ENTRY;
+       int i;
+
+       for (i = 0; i < count; i++) {
+               UAM_DBG("%s = %s\n", columns[i], data[i] ? data[i] : "NULL");
+               if (!g_strcmp0(columns[i], "integrity_check") && !g_strcmp0(data[i], "ok")) return 0;
+       }
+       return 1;
+}
+
+static int __uam_db_check_table_cb(void *err, int count, char **data, char **columns)
+{
+       FUNC_ENTRY;
+       int i;
+
+       for (i = 0; i < count; i++) {
+               UAM_DBG("%s = %s\n", columns[i], data[i] ? data[i] : "NULL");
+               if (!g_strcmp0(columns[i], "count") && !g_strcmp0(data[i], "1")) return 0;
+       }
+       return 1;
+}
+
 static int __ua_db_initialize_once(void)
 {
        FUNC_ENTRY;
@@ -163,13 +187,11 @@ static int __ua_db_initialize_once(void)
        }
 
        res = sqlite3_open(DATABASE_FULL_PATH, &database);
-
        if (res != SQLITE_OK) {
                // TODO : Can add logic of retry if not able to open
                UAM_ERR("Error in opening database %s: %s", DATABASE_FULL_PATH, sqlite3_errmsg(database));
                return UAM_ERROR_DB_FAILED;
        }
-
        UAM_DBG("Successfully opened database");
 
        size_t db_size = _uam_getFilesize(DATABASE_FULL_PATH);
@@ -179,8 +201,7 @@ static int __ua_db_initialize_once(void)
        }
        UAM_DBG("Database size: %d", db_size);
 
-       res = sqlite3_exec(database, "PRAGMA integrity_check", 0, 0, 0);
-
+       res = sqlite3_exec(database, "PRAGMA integrity_check", __uam_db_check_integrity_cb, 0, 0);
        if (res != SQLITE_OK) {
                UAM_ERR("Can't verify database integrity %s",
                                sqlite3_errmsg(database));
@@ -191,8 +212,20 @@ static int __ua_db_initialize_once(void)
        }
        UAM_DBG("Successfully verified database integrity");
 
-       res = sqlite3_exec(database, "PRAGMA locking_mode = NORMAL", 0, 0, 0);
+       res = sqlite3_exec(database,
+               "SELECT count(*) as count FROM sqlite_master WHERE type='table' AND name='userdata'",
+               __uam_db_check_table_cb, 0, 0);
+       if (res != SQLITE_OK) {
+               UAM_ERR("Can't verify table creation %s",
+                               sqlite3_errmsg(database));
+               sqlite3_close(database);
+               database = NULL;
+               FUNC_EXIT;
+               return UAM_ERROR_DB_FAILED;
+       }
+       UAM_DBG("Successfully verified table creation");
 
+       res = sqlite3_exec(database, "PRAGMA locking_mode = NORMAL", 0, 0, 0);
        if (res != SQLITE_OK) {
                UAM_ERR("Can't set locking mode %s, skip set busy handler.",
                                sqlite3_errmsg(database));