update-manager: Separation of delta checking into a separate function 98/287098/3
authorMateusz Moscicki <m.moscicki2@partner.samsung.com>
Thu, 19 Jan 2023 14:43:33 +0000 (15:43 +0100)
committerMateusz Moscicki <m.moscicki2@partner.samsung.com>
Thu, 19 Jan 2023 16:35:39 +0000 (17:35 +0100)
Change-Id: I18db976595d920e1f4ab325e65791aa2e3b8762e

update-manager/CMakeLists.txt
update-manager/fota/fota-common.c [new file with mode: 0644]
update-manager/fota/fota-installer.c
update-manager/fota/fota-manager.h
update-manager/fota/fota-storage-checker.c

index a76e12e81b663486ba73c07acc1f9507fbfc5f46..2d9d750a56d8bcd718b14eb50db5f8123ec3a1fd 100644 (file)
@@ -32,7 +32,7 @@ SET(CMAKE_C_FLAGS_DEBUG "-O0 -g")
 SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -pie")
 
 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS_BASE} -Wall -fPIE")
-INCLUDE_DIRECTORIES(${INCLUDE_DIR})
+INCLUDE_DIRECTORIES(${INCLUDE_DIR} ${SOURCE_DIR})
 FILE(GLOB SOURCE_FILES
                "${SOURCE_DIR}/*.h" "${SOURCE_DIR}/*.c"
                "${SOURCE_DIR}/*/*.h" "${SOURCE_DIR}/*/*.c"
diff --git a/update-manager/fota/fota-common.c b/update-manager/fota/fota-common.c
new file mode 100644 (file)
index 0000000..25208b3
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ * Copyright (c) 2023 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "common/common.h"
+#include "fota-manager.h"
+
+int check_is_delta_appropriate(const char *delta_path, bool *checksum_available)
+{
+       int ret;
+       bool do_checksum = false;
+       char buf[MAX_BUFFER_SIZE] = {0, };
+
+       ret = util_file_untar(delta_path, FOTA_DIR, FOTA_DELTA_UPDATE_INFO_FILENAME);
+       if (ret < 0) {
+               ret = -1;
+               goto exit;
+       }
+
+       ret = util_file_untar(delta_path, FOTA_DIR, DELTA_VERIFIER_BIN);
+       if (ret < 0) {
+               ret = -1;
+               goto exit;
+       }
+
+       ret = util_file_untar(delta_path, FOTA_DIR, FOTA_CHECKSUM_FILE);
+       if (ret < 0) {
+               _FLOGW("There is no %s in delta", FOTA_CHECKSUM_FILE);
+       } else {
+               do_checksum = true;
+       }
+
+       if (checksum_available != NULL)
+               *checksum_available = do_checksum;
+
+       if (do_checksum) {
+               if (verify_checksum(FOTA_CHECKSUM_PATH, DELTA_VERIFIER_PATH) == 0) {
+                       _FLOGI("Checksum of %s is correct", DELTA_VERIFIER_PATH);
+               } else {
+                       ret = -1;
+                       _FLOGE("Failed checksum verification of %s", DELTA_VERIFIER_PATH);
+                       goto exit;
+               }
+       }
+
+       snprintf(buf, MAX_BUFFER_SIZE, "%s %s %s", DELTA_VERIFIER_BIN_PATH, DELTA_VERIFIER_BIN_LONG_ARG, FOTA_DELTA_UPDATE_INFO_PATH);
+       ret = system(buf);
+       if (ret < 0) {
+               _FLOGE("Delta verification failed due to an error [return code: %d].", ret);
+       } else if (ret > 0) {
+               _FLOGI("The delta at [%s] is not compatible with this device [return code: %d].", delta_path, ret);
+       }
+
+exit:
+       return ret;
+}
index 246cd0fccde5bf8167e44603fb140b922deec5e5..b0d7953d000cb55301a0683280ad54fc15715920 100644 (file)
@@ -74,16 +74,15 @@ static char *find_delta_dir(const char *appid)
 int fota_installer_execute(pid_t sender_pid)
 {
        int ret = 0, status = 0, exec_status = 0;
-       char buf[MAX_BUFFER_SIZE] = {0, };
        char *appid = NULL;
        gchar *client_delta_path = NULL;
        pid_t pid;
        bool check_sha = false;
 
-       /* 1. Check client have delta.tar */
+       /* Check client have delta.tar */
        appid = get_sender_appid(sender_pid);
 
-       /* 2. Find the delta file path */
+       /* Find the delta file path */
        client_delta_path = find_delta_dir(appid);
        if (appid != NULL) {
                free(appid);
@@ -97,55 +96,20 @@ int fota_installer_execute(pid_t sender_pid)
        }
        _FLOGI("Delta found: %s", client_delta_path);
 
-       /* 3. Prepare fota dir */
+       /* Prepare fota dir */
        ret = util_file_mkdir(FOTA_DIR);
        if (ret < 0) {
                status = -1;
                goto execute_destroy;
        }
 
-       /* 4. Check client have appropriate delta  */
-       ret = util_file_untar(client_delta_path, FOTA_DIR, FOTA_DELTA_UPDATE_INFO_FILENAME);
-       if (ret < 0) {
-               status = -1;
-               goto execute_destroy;
-       }
-       ret = util_file_untar(client_delta_path, FOTA_DIR, DELTA_VERIFIER_BIN);
-       if (ret < 0) {
+       /* Check client have appropriate delta  */
+       if (check_is_delta_appropriate(client_delta_path, &check_sha) != 0) {
                status = -1;
                goto execute_destroy;
        }
 
-       ret = util_file_untar(client_delta_path, FOTA_DIR, FOTA_CHECKSUM_FILE);
-       if (ret < 0) {
-               _FLOGW("There is no %s in delta", FOTA_CHECKSUM_FILE);
-       } else {
-               check_sha = true;
-       }
-
-       if (check_sha) {
-               if (verify_checksum(FOTA_CHECKSUM_PATH, DELTA_VERIFIER_PATH) == 0) {
-                       _FLOGI("Checksum of %s is correct", DELTA_VERIFIER_PATH);
-               } else {
-                       status = -1;
-                       _FLOGE("Failed checksum verification of %s", DELTA_VERIFIER_PATH);
-                       goto execute_destroy;
-               }
-       }
-
-       snprintf(buf, MAX_BUFFER_SIZE, "%s %s %s", DELTA_VERIFIER_BIN_PATH, DELTA_VERIFIER_BIN_LONG_ARG, FOTA_DELTA_UPDATE_INFO_PATH);
-       ret = system(buf);
-       if (ret < 0) {
-               _FLOGE("Delta verification failed due to an error [return code: %d].", ret);
-               status = -1;
-               goto execute_destroy;
-       } else if (ret > 0) {
-               _FLOGI("The delta of the caller is not compatible with this device [return code: %d].", ret);
-               status = -3;
-               goto execute_destroy;
-       }
-
-       /* 5. Setup local update flag */
+       /* Setup local update flag */
        ret = util_file_mkdir(FOTA_STATUS_DIR);
        if (ret < 0) {
                status = -1;
@@ -158,7 +122,7 @@ int fota_installer_execute(pid_t sender_pid)
                goto execute_destroy;
        }
 
-       /* 6. Trigger update */
+       /* Trigger update */
        ret = util_file_untar(client_delta_path, FOTA_DIR, FOTA_TRIGGER_FILE);
        if (ret < 0) {
                status = -1;
index acb6a2014bb19f551465ee4faa3e2312fc9ed92c..9f271e15ae9acaf74b7a36ae51723ab386190ba8 100644 (file)
@@ -52,5 +52,6 @@ int fota_installer_execute(pid_t pid);
 
 void fota_storage_checker_plug(int, const char *);
 void fota_storage_checker_unplug(int, const char *);
+int check_is_delta_appropriate(const char *delta_path, bool *checksum_available);
 
 #endif /* __FOTA_MANAGER_H__ */
index 168d593e4a63e607c719dd93f1832c3f199695ce..d5c8fc71ed6f2bcfb99715e9f75993f8a43885ba 100644 (file)
@@ -8,10 +8,9 @@ gchar *fota_storage_find_delta(const char* mount_path, const char *folder_name)
 {
        int ret = 0;
        bool is_appropriate = false;
-       char buf[MAX_BUFFER_SIZE] = {0, };
        gchar *storage_delta_path = NULL;
 
-       /* 1. Check storage have delta.tar */
+       /* Check storage have delta.tar */
        storage_delta_path = g_strjoin("/", mount_path, folder_name, FOTA_DELTA_FILENAME, NULL);
        if (!g_file_test(storage_delta_path, G_FILE_TEST_EXISTS)) {
                gchar *tmp_storage_delta_path = storage_delta_path;
@@ -24,39 +23,15 @@ gchar *fota_storage_find_delta(const char* mount_path, const char *folder_name)
                }
        }
 
-       /* 2. Prepare fota dir */
+       /* Prepare fota dir */
        ret = util_file_mkdir(FOTA_DIR);
        if (ret < 0) {
                goto verify_destroy;
        }
 
-       /* 2. Check storage have appropriate delta */
-       ret = util_file_untar(storage_delta_path, FOTA_DIR, FOTA_DELTA_UPDATE_INFO_FILENAME);
-       if (ret < 0) {
-               goto verify_destroy;
-       }
-
-       ret = util_file_untar(storage_delta_path, FOTA_DIR, FOTA_CHECKSUM_FILE);
-       if (ret < 0) {
-               _FLOGW("There is no %s in delta", FOTA_CHECKSUM_FILE);
-       } else {
-               if (verify_checksum(FOTA_CHECKSUM_PATH, DELTA_VERIFIER_PATH) == 0) {
-                       _FLOGI("Checksum of %s is correct", DELTA_VERIFIER_PATH);
-               } else {
-                       ret = -1;
-                       _FLOGE("Failed checksum verification of %s", DELTA_VERIFIER_PATH);
-                       goto verify_destroy;
-               }
-       }
-
-       snprintf(buf, MAX_BUFFER_SIZE, "%s %s %s", DELTA_VERIFIER_BIN_PATH, DELTA_VERIFIER_BIN_LONG_ARG, FOTA_DELTA_UPDATE_INFO_PATH);
-       ret = system(buf);
-
-       if (ret < 0) {
-               _FLOGE("Delta verification failed due to an error [return code: %d].", ret);
-               goto verify_destroy;
-       } else if (ret > 0) {
-               _FLOGI("The delta at [%s] is not compatible with this device [return code: %d].", storage_delta_path, ret);
+       /* Check storage have appropriate delta */
+       if (check_is_delta_appropriate(storage_delta_path, NULL) != 0) {
+               ret = -1;
                goto verify_destroy;
        }