Check delta more strictly 07/239907/17
authorJeon Sang-Heon <sh95.jeon@samsung.com>
Thu, 30 Jul 2020 20:04:24 +0000 (20:04 +0000)
committerJeon Sang-Heon <sh95.jeon@samsung.com>
Fri, 7 Aug 2020 17:25:32 +0000 (17:25 +0000)
- Current delta verify only check target's build string
- Check that new version build string matches with delta additionally
- Devide fota delta verify into fota-delta-verifier file
- Minor changes for log

Change-Id: Idfdf4c5b268f060dee0fea9e873242e3cf1d6587
Signed-off-by: Jeon Sang-Heon <sh95.jeon@samsung.com>
update-manager/common/common-boot-status-checker.c
update-manager/fota-controller.c
update-manager/fota/fota-delta-verifier.c [new file with mode: 0644]
update-manager/fota/fota-manager.h
update-manager/fota/fota-status-checker.c
update-manager/fota/fota-storage-checker.c
update-manager/recovery/recovery-storage-checker.c
update-manager/update-manager.h
update-manager/util.c

index 13f8e36bbd79f33f879089ca3fc35849002b01af..aa604230442794c8b0ec6c2f81c9b4c0b6ee4a9b 100644 (file)
@@ -23,10 +23,10 @@ void common_boot_status_checker_callback(keynode_t *node, void *user_data)
                        else
                                _W("vconf_ignore_key_changed for %s failed : %d", VCONFKEY_SYSMAN_BOOTINGSTATUS, ret);
 
-                       _I("Success to get bootstatus success, try process fota event");
+                       _FLOGI("Success to get bootstatus success, try process fota event");
                        ret = fota_client_controller_process_event();
                        if (ret < 0)
-                               _E("Failed to process fota client event : %d", ret);
+                               _FLOGE("Failed to process fota client event : %d", ret);
 
                        _RLOGI("Success to get bootstatus success, try process recovery event");
                        ret = recovery_client_controller_process_event();
index 4cea23add9bcf2b7a3a40d4375ccccaadfd7c0ee..ef8143736ef6f056cf1a053ffa68590fbf26e206 100644 (file)
@@ -1,60 +1,10 @@
 #include "fota/fota-manager.h"
 #include "update-manager.h"
 
-int fota_controller_verify_delta(const char *delta_path)
-{
-       int ret = 0, status = 0;
-       char buf[MAX_BUFFER_SIZE] = {0, };
-       char *build_id = NULL;
-
-       if (g_file_test(delta_path, G_FILE_TEST_EXISTS)) {
-               _I("Found delta in %s, start process to verify", delta_path);
-
-               ret = util_file_untar(delta_path, BUILD_ID_DIR, BUILD_ID_FILE);
-               if (ret < 0) {
-                       _E("Failed to fetch %s from delta : %d", BUILD_ID_FILE, ret);
-                       status = -1;
-                       goto verify_destroy;
-               }
-
-               build_id = fota_info_get_build_string();
-               if (build_id == NULL) {
-                       _E("Failed to get current build id");
-                       status = -1;
-                       goto verify_destroy;
-               }
-
-               ret = util_file_read_line(BUILD_ID_PATH, buf);
-               if (ret < 0) {
-                       _E("Failed to read delta build id : %d", ret);
-                       status = -1;
-                       goto verify_destroy;
-               }
-
-               if (g_strrstr(buf, build_id) != NULL) {
-                       _I("Delta build id matched : %s", build_id);
-               } else {
-                       _I("Delta build id unmatched, current : %s, delta : %s",
-                          build_id, buf);
-                       status = 1;
-               }
-       } else {
-               _I("Not found delta in %s", delta_path);
-               status = 2;
-       }
-
-verify_destroy:
-       ret = remove(BUILD_ID_PATH);
-       if (ret < 0)
-               _W("Failed to remove %s : %m", BUILD_ID_PATH);
-
-       return status;
-}
-
 int fota_controller_setup_delta()
 {
        int ret = 0, status = 0;
-       char *appid = NULL;
+       char *appid = NULL, *build_string = NULL;
        gchar *shared_path = NULL;
 
        ret = util_file_mkdir(FOTA_DIR);
@@ -70,12 +20,19 @@ int fota_controller_setup_delta()
                goto delta_destroy;
        }
 
+       build_string = fota_info_get_build_string();
+       if (build_string == NULL) {
+               _E("Failed to get build string");
+               status = -1;
+               goto delta_destroy;
+       }
+
        ret = remove(FOTA_DELTA_PATH);
        if (ret < 0 && errno != ENOENT)
                _W("Failed to remove exist link : %m");
 
        shared_path = g_strjoin("/", SHARED_DIR, appid, SHARED_DATA_DIR, FOTA_DELTA_FILE, NULL);
-       ret = fota_controller_verify_delta(shared_path);
+       ret = fota_delta_verify(shared_path, build_string, NULL);
        if (ret != 0) {
                _E("Failed to verify delta : %d", ret);
                status = -1;
diff --git a/update-manager/fota/fota-delta-verifier.c b/update-manager/fota/fota-delta-verifier.c
new file mode 100644 (file)
index 0000000..a02af04
--- /dev/null
@@ -0,0 +1,74 @@
+#include "fota-manager.h"
+#include "../update-manager.h"
+
+int fota_delta_verify(const char *path, const char *old_build_string, const char *new_build_string)
+{
+       int ret = 0, status = 0;
+       char buf[MAX_BUFFER_SIZE] = {0, };
+       FILE *fp = NULL;
+
+       if (!g_file_test(path, G_FILE_TEST_EXISTS)) {
+               _FLOGI("Not found delta in %s", path);
+               status = 1;
+               goto verify_destroy;
+       }
+
+       if (old_build_string == NULL) {
+               _FLOGE("Failed to try verify, old build string must be passed");
+               status = -1;
+               goto verify_destroy;
+       }
+
+       _FLOGI("Found delta in %s, start process to verify", path);
+       ret = util_file_untar(path, BUILD_ID_DIR, BUILD_ID_FILE);
+       if (ret < 0) {
+               _FLOGE("Failed to fetch %s from delta : %d", BUILD_ID_FILE, ret);
+               status = -1;
+               goto verify_destroy;
+       }
+
+       fp = fopen(BUILD_ID_PATH, "r");
+       if (fp == NULL) {
+               _FLOGE("Failed to open : %s", BUILD_ID_PATH);
+               status = -1;
+               goto verify_destroy;
+       }
+
+       if (fgets(buf, MAX_BUFFER_SIZE, fp) == NULL) {
+               _FLOGE("Failed to read : %s", BUILD_ID_PATH);
+               status = -1;
+               goto verify_destroy;
+       }
+
+       if (g_strrstr(buf, old_build_string) == NULL) {
+               _FLOGI("Unmatched occur, old_build_string : %s, delta : %s", old_build_string, buf);
+               status = 2;
+               goto verify_destroy;
+       }
+
+       if (new_build_string != NULL) {
+               if (fgets(buf, MAX_BUFFER_SIZE, fp) == NULL) {
+                       _FLOGE("Failed to read : %s", BUILD_ID_PATH);
+                       status = -1;
+                       goto verify_destroy;
+               }
+
+               if (g_strrstr(buf, new_build_string) == NULL) {
+                       _FLOGI("Unmatched occur, new_build_string : %s, delta : %s", new_build_string, buf);
+                       status = 3;
+                       goto verify_destroy;
+               }
+       }
+
+       _FLOGI("Success to verify %s, old : %s, new : %s", path, old_build_string, new_build_string);
+
+verify_destroy:
+       ret = remove(BUILD_ID_PATH);
+       if (ret < 0)
+               _FLOGW("Failed to remove %s : %m", BUILD_ID_PATH);
+
+       if (!fp)
+               fclose(fp);
+
+       return status;
+}
\ No newline at end of file
index a279fbd38f5f45b692317442196d81b9861b345b..2940c3e36ddf15578bae071c364ec59b7c34ce30 100644 (file)
@@ -24,6 +24,8 @@
 
 #define FOTA_EVENT_SIZE 3
 
+#define FOTA_LOCAL_FOLDER_TOKEN "@"
+
 /* Enum */
 typedef enum {
        FOTA_EVENT_PLUG = 0,
@@ -39,6 +41,8 @@ int fota_client_info_checker_init(void);
 int fota_client_info_checker_fini(void);
 char *fota_client_info_get_appid(void);
 
+int fota_delta_verify(const char *, const char *, const char *);
+
 int fota_info_checker_init(void);
 int fota_info_checker_fini(void);
 char *fota_info_get_build_string(void);
index c4e8409183df51c57ca4a04575fc6bcbb3688f1f..a23af6d3588da0275153a5ca962c8bbda954d26c 100644 (file)
@@ -18,7 +18,7 @@ int fota_status_checker_init()
                return 0;
        }
 
-       ret = util_file_read_line(STATUS_RESULT_PATH, buf);
+       ret = util_file_read_single_line(STATUS_RESULT_PATH, buf);
        if (ret < 0) {
                _FLOGE("Failed to read fota result : %d", ret);
                return -1;
index ed04d7d776ef090be1c4e872ef6002098245a072..529337ea40c2eda862fa12c7980b125c19b54879 100644 (file)
@@ -3,10 +3,59 @@
 
 static int fota_storage_id = -1;
 
+int fota_storage_verify_delta(const char* mount_path, const char *folder_name, gchar **delta_path)
+{
+       int ret = 0, status = 0;
+       char *old_build_string = NULL;
+       gchar *new_build_string = NULL, *path = NULL;
+
+       old_build_string = fota_info_get_build_string();
+       if (old_build_string == NULL) {
+               _FLOGE("Failed to get build string");
+               status = -1;
+               goto verify_destroy;
+       }
+
+       if (g_str_has_prefix(folder_name, old_build_string) != TRUE) {
+               _FLOGI("Folder %s is not start with %s, pass", folder_name, old_build_string);
+               goto verify_destroy;
+       }
+
+       new_build_string = strstr(folder_name, FOTA_LOCAL_FOLDER_TOKEN);
+       if (new_build_string == NULL) {
+               _FLOGI("Folder %s doesn't have token : %s", folder_name, FOTA_LOCAL_FOLDER_TOKEN);
+               goto verify_destroy;
+       }
+       new_build_string = new_build_string + 1;
+
+       path = g_strjoin("/", mount_path, folder_name, FOTA_DELTA_FILE, NULL);
+       if (!g_file_test(path, G_FILE_TEST_EXISTS)) {
+               _FLOGI("Folder %s doesn't have delta.tar", folder_name);
+               goto verify_destroy;
+       }
+
+       ret = fota_delta_verify(path, old_build_string, new_build_string);
+       if (ret < 0) {
+               status = -1;
+               goto verify_destroy;
+       }
+       else if (ret > 0) {
+               goto verify_destroy;
+       }
+
+       *delta_path = path;
+
+verify_destroy:
+       if (status != 0) {
+               g_free(path);
+       }
+
+       return status;
+}
+
 int fota_storage_search_delta_path(const char *mount_path, gchar **delta_path)
 {
-       int status = 0;
-       char *build_id = NULL;
+       int ret = 0, status = 0;
        const gchar *folder_name = NULL;
        GFile *mount = NULL;
        GFileEnumerator *enumerator = NULL;
@@ -22,33 +71,22 @@ int fota_storage_search_delta_path(const char *mount_path, gchar **delta_path)
                goto search_destroy;
        }
 
-       build_id = fota_info_get_build_string();
-       if (build_id == NULL) {
-               _FLOGE("Failed to get build id");
-               status = -1;
-               goto search_destroy;
-       }
-
        while ((info = g_file_enumerator_next_file(enumerator, NULL, NULL)) != NULL) {
-               if (g_file_info_get_file_type(info) != G_FILE_TYPE_DIRECTORY)
+               if (g_file_info_get_file_type(info) != G_FILE_TYPE_DIRECTORY) {
+                       g_object_unref(info);
                        continue;
+               }
 
                folder_name = g_file_info_get_name(info);
-               if (g_str_has_prefix(folder_name, build_id) == TRUE) {
-                       *delta_path = g_strjoin("/", mount_path, folder_name, FOTA_DELTA_FILE, NULL);
-                       break;
+               ret = fota_storage_verify_delta(mount_path, folder_name, delta_path);
+               if (ret < 0) {
+                       _FLOGE("Failed to verify delta : %d, folder : %s", ret, folder_name);
+                       status = -1;
                }
 
                g_object_unref(info);
        }
 
-       if (*delta_path != NULL) {
-               _FLOGI("Success to find delta path : %s", *delta_path);
-       } else {
-               _FLOGI("Failed to find delta path with prefix %s", build_id);
-               status = 1;
-       }
-
 search_destroy:
        if (enumerator)
                g_object_unref(enumerator);
@@ -68,14 +106,13 @@ void fota_storage_checker_plug(int storage_id, const char *mount_path)
 
        _FLOGI("Storage mounted with %s, start process to check local delta", mount_path);
        ret = fota_storage_search_delta_path(mount_path, &delta_path);
-       if (ret != 0) {
+       if (ret < 0) {
                _FLOGE("Failed to find delta path : %d", ret);
                goto plug_destroy;
        }
 
-       ret = fota_controller_verify_delta(delta_path);
-       if (ret != 0) {
-               _FLOGE("Failed to verify delta : %d", ret);
+       if (delta_path == NULL) {
+               _FLOGI("Not found matched delta in %s", mount_path);
                goto plug_destroy;
        }
 
index 8aef617ce2355953279195e9746765477f7df969..95cf95774756fa0debadff45e35a8a73a6bf7527 100644 (file)
@@ -23,7 +23,7 @@ void recovery_storage_checker_plug(int storage_id, const char *mount_path)
        image_path = g_strjoin("/", mount_path, RECOVERY_IMAGE_FILE, NULL);
 
        ret = recovery_storage_search_image(image_path);
-       if (ret != 0) {
+       if (ret < 0) {
                _RLOGE("Failed to find delta path : %d", ret);
                goto plug_destroy;
        }
index 88e21107ae95c00a21125897f771b3ec7e399588..3ad5cc7927379b629f799afc78b8467b48aabe97 100644 (file)
@@ -78,12 +78,11 @@ int common_client_info_checker_filter(const char*, const char*, pkgmgrinfo_app_l
 int dbus_manager_init(void);
 int dbus_manager_fini(void);
 
-int fota_controller_verify_delta(const char *);
 int fota_controller_install(void);
 int fota_controller_result(void);
 
 int util_file_mkdir(const char *);
-int util_file_read_line(const char *, char []);
+int util_file_read_single_line(const char *, char []);
 int util_file_write_line(const char *, const char *);
 int util_file_untar(const char *, const char *, const char *);
 
index 54d6355f66cf9d3e79ecbe6abfc7e803e6528c8f..7667b10fd1b26a72066fa246a0417736a67ee791 100644 (file)
@@ -17,7 +17,7 @@ int util_file_mkdir(const char *path)
        return 0;
 }
 
-int util_file_read_line(const char *path, char buf[])
+int util_file_read_single_line(const char *path, char buf[])
 {
        int status = 0;
        FILE *fp = NULL;