Change db schema comparison logic 37/272837/7
authorJunghyun Yeon <jungh.yeon@samsung.com>
Fri, 25 Mar 2022 08:30:04 +0000 (17:30 +0900)
committerJunghyun Yeon <jungh.yeon@samsung.com>
Thu, 7 Apr 2022 01:02:19 +0000 (01:02 +0000)
DB schema couldn't be separated by newline when db has upgraded
using "alter table" sql statement.
To compare it with db schema separated by newline, parse retrieved
schema with "," and trim empty lines to separate schema with one
colum definition.

Change-Id: I14c0f58c67f668962aac9a574ff2161132e69702
Signed-off-by: Junghyun Yeon <jungh.yeon@samsung.com>
tool/pkg-db-recovery.c

index fc6da54..f48d2b4 100644 (file)
@@ -20,6 +20,7 @@
 
 #define _GNU_SOURCE
 
+#include <ctype.h>
 #include <dirent.h>
 #include <errno.h>
 #include <fcntl.h>
@@ -70,6 +71,27 @@ typedef struct user_info {
 
 static GList *user_info_list;
 
+char *string_trim_inplace(char *s) {
+       char *original = s;
+       size_t len = 0;
+
+       while (isspace((unsigned char) *s))
+               s++;
+
+       if (*s) {
+               char *p = s;
+               while (*p)
+                       p++;
+               while (isspace((unsigned char) *(--p)));
+               p[1] = '\0';
+               len = (size_t) (p - s + 1);
+       }
+
+       if (len > MAX_QUERY_LEN)
+               return NULL;
+       return (s == original) ? s : memmove(original, s, len + 1);
+}
+
 static char *__get_dbpath(uid_t uid)
 {
        const char *db_path;
@@ -367,6 +389,12 @@ static bool __check_db_schema(const char *db_path,
        static const char table_schema_query[] =
                        "SELECT sql from sqlite_master WHERE name=?";
        sqlite3 *db;
+       char *schema_lib_tmp;
+       char *schema_lib_ptr;
+       char *schema_db_tmp;
+       char *schema_db_ptr;
+       char *line_db;
+       char *line_library;
 
        ret = sqlite3_open_v2(db_path, &db,
                        SQLITE_OPEN_READONLY, NULL);
@@ -414,13 +442,46 @@ static bool __check_db_schema(const char *db_path,
                        LOGE("Failed to get initialization query from library");
                        goto err;
                }
+               schema_lib_tmp = strdup(schema_in_library);
+               schema_lib_ptr = schema_lib_tmp;
+               if (schema_lib_tmp == NULL) {
+                       LOGE("Out of memory");
+                       goto err;
+               }
+
                schema_in_db = strstr(check_result, db_tables[i]);
                if (schema_in_db == NULL) {
                        LOGE("Failed to get initialization query from db");
+                       free(schema_lib_ptr);
+                       goto err;
+               }
+               schema_db_tmp = strdup(schema_in_db);
+               schema_db_ptr = schema_db_tmp;
+               if (schema_db_tmp == NULL) {
+                       LOGE("Out of memory");
+                       free(schema_lib_ptr);
                        goto err;
                }
 
-               ret = strcmp(schema_in_library, schema_in_db);
+               while (true) {
+                       line_db = strsep(&schema_db_tmp, ",");
+                       line_library = strsep(&schema_lib_tmp, ",");
+                       if (line_db == NULL || line_library == NULL)
+                               break;
+
+                       if (string_trim_inplace(line_db) == NULL ||
+                                       string_trim_inplace(line_library) == NULL)
+                               break;
+
+                       ret = strcmp(string_trim_inplace(line_db),
+                                       string_trim_inplace(line_library));
+                       if (ret != 0)
+                               break;
+               }
+
+               free(schema_lib_ptr);
+               free(schema_db_ptr);
+
                if (ret != 0) {
                        LOGE("Broken schema detected in table[%s], query[%s]",
                                        db_tables[i], schema_in_db);