check integrity in media server 36/178836/1 submit/tizen_4.0/20180514.082511
authoryujie.cheng <yujie.cheng@samsung.com>
Mon, 14 May 2018 02:53:55 +0000 (10:53 +0800)
committercheng yujie <yujie.cheng@samsung.com>
Mon, 14 May 2018 07:50:15 +0000 (07:50 +0000)
Change-Id: I69a49a40bb9a98b46df6006a885dbbbf99006e86
(cherry picked from commit 5854e5ccf09c6b7e5f01101478270d7acaaea63d)

lib/include/media-util-db.h
lib/media-util-db.c
src/server/media-server-db-manage.c

index 8e85fe2..e05b2f2 100755 (executable)
@@ -45,6 +45,9 @@ int media_db_request_update_db_batch_end(const char *query_str, uid_t uid);
 
 int media_db_request_update_db_batch_clear(void);
 
+int media_db_check_integrity(MediaDBHandle *handle);
+
+
 /**
 * @}
 */
index 25c7d39..4854548 100755 (executable)
@@ -373,6 +373,40 @@ EXEC_RETRY:
        return ret;
 }
 
+int media_db_check_integrity(MediaDBHandle *handle)
+{
+       sqlite3 * db_handle = (sqlite3 *)handle;
+
+       MSAPI_RETVM_IF(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
+
+       int ret = 0;
+       sqlite3_stmt *stmt = NULL;
+       const char integrity_check_query[] = "PRAGMA integrity_check";
+
+       ret = sqlite3_prepare_v2(db_handle, integrity_check_query, strlen(integrity_check_query), &stmt, NULL);
+       if (ret != SQLITE_OK) {
+               MSAPI_DBG_ERR("prepare error, ret = %d, error = %s", ret, sqlite3_errmsg(db_handle));
+               return MS_MEDIA_ERR_DB_CORRUPT;
+       }
+
+       ret = sqlite3_step(stmt);
+       if (ret != SQLITE_ROW) {
+               MSAPI_DBG_ERR("integrity_check failed in step");
+               sqlite3_finalize(stmt);
+               return MS_MEDIA_ERR_DB_CORRUPT;
+       }
+
+       char* check_result = (char*) sqlite3_column_text(stmt, 0);
+       if (check_result == NULL || strncmp(check_result, "ok", strlen(check_result))) {
+               MSAPI_DBG_ERR("integrity_check failed - result(%s)\n", check_result);
+               sqlite3_finalize(stmt);
+               return MS_MEDIA_ERR_DB_CORRUPT;
+       }
+       sqlite3_finalize(stmt);
+
+       return MS_MEDIA_ERR_NONE;
+}
+
 int media_db_connect(MediaDBHandle **handle, uid_t uid, bool need_write)
 {
        int ret = MS_MEDIA_ERR_NONE;
index 2d944d7..0c9a852 100755 (executable)
@@ -140,6 +140,12 @@ int ms_check_corrupt_mediadb(void)
                return MS_MEDIA_ERR_DB_CONNECT_FAIL;
        }
 
+       if (media_db_check_integrity(db_handle) != MS_MEDIA_ERR_NONE) {
+               MS_DBG_ERR("check interity failed");
+               media_db_disconnect(db_handle);
+               return MS_MEDIA_ERR_DB_CORRUPT;
+       }
+
        /* Disconnect DB*/
        media_db_disconnect(db_handle);