From ab6d5164cb7c536481d572c79ace03e5a3abba1f Mon Sep 17 00:00:00 2001 From: Mateusz Moscicki Date: Thu, 10 Jun 2021 15:45:13 +0200 Subject: [PATCH] update-manager: Change the search order for the delta file New search order: 1. /opt/usr/home/owner/app_rw//shared/data/delta.tar.gz 2. /opt/usr/home/owner/app_rw//shared/data/delta.tar 3. /opt/usr/data/fota/delta.tar.gz 4. /opt/usr/data/fota/delta.tar If the caller does not have app_id, only the last two directories will be searched. Change-Id: I88e69f3de0349a95ac74a4f7a9577e348bc84f57 --- update-manager/fota/fota-installer.c | 85 ++++++++++++++++++---------- update-manager/fota/fota-manager.h | 1 + 2 files changed, 57 insertions(+), 29 deletions(-) diff --git a/update-manager/fota/fota-installer.c b/update-manager/fota/fota-installer.c index 953bdf6..be1f156 100644 --- a/update-manager/fota/fota-installer.c +++ b/update-manager/fota/fota-installer.c @@ -1,3 +1,4 @@ +#include #include #include "../common/common.h" #include "fota-manager.h" @@ -11,7 +12,7 @@ #define FOTA_INSTALL_REBOOT_REASON "fota" -static char *get_sender_or_client_appid(pid_t pid) +static char *get_sender_appid(pid_t pid) { char *result = NULL; if (pid > 0) { @@ -24,18 +25,51 @@ static char *get_sender_or_client_appid(pid_t pid) return NULL; } } else { - _FLOGE("Failed to get sender app id: %d", res); + _FLOGE("Failed to get sender app id: %d. The caller is not an application.", res); } } + return result; +} + +static char *check_delta_exist(char *base_dir) +{ + assert(base_dir); + char *client_delta_path = NULL; + + const char *delta_files[] = {FOTA_DELTA_COMPRESSED_FILENAME, + FOTA_DELTA_FILENAME}; - // Fallback to appid retrieval based on application metadata if the - // caller appid cannot be determined - if (result == NULL) { - result = fota_client_info_get_appid(); - if (result == NULL) - _FLOGE("Failed to get client app id"); + for (size_t n = 0; n < G_N_ELEMENTS(delta_files); n++) { + gchar *tmp_file_name = g_strconcat(base_dir, "/", delta_files[n], NULL); + if (tmp_file_name == NULL) { + _FLOGE("Out of memory error"); + break; + } + + _FLOGD("Checking: %s", tmp_file_name); + if (g_file_test(tmp_file_name, G_FILE_TEST_EXISTS)) { + client_delta_path = tmp_file_name; + break; + } + free(tmp_file_name); } - return result; + + return client_delta_path; +} + +static char *find_delta_dir(const char *appid) +{ + char *client_delta_path = NULL; + if (appid != NULL) { + char *dir_path = g_strjoin("/", APP_SHARED_DIR, appid, APP_SHARED_DATA, NULL); + client_delta_path = check_delta_exist(dir_path); + free(dir_path); + } + + if (client_delta_path == NULL) + client_delta_path = check_delta_exist(FOTA_DIR); + + return client_delta_path; } int fota_installer_execute(pid_t sender_pid) @@ -47,26 +81,19 @@ int fota_installer_execute(pid_t sender_pid) pid_t pid; /* 1. Check client have delta.tar */ - appid = get_sender_or_client_appid(sender_pid); - if (appid == NULL) { - status = -1; - goto execute_destroy; - } + appid = get_sender_appid(sender_pid); - client_delta_path = g_strjoin("/", APP_SHARED_DIR, appid, APP_SHARED_DATA, FOTA_DELTA_FILENAME, NULL); - if (!g_file_test(client_delta_path, G_FILE_TEST_EXISTS)) { - gchar *tmp_client_delta_path = client_delta_path; - client_delta_path = g_strconcat(tmp_client_delta_path, ".gz", NULL); - free(tmp_client_delta_path); + /* 2. Find the delta file path */ + client_delta_path = find_delta_dir(appid); - if (!g_file_test(client_delta_path, G_FILE_TEST_EXISTS)) { - _FLOGI("%s doesn't have delta.tar.gz", appid); - status = -2; - goto execute_destroy; - } + if (client_delta_path == NULL) { + _FLOGE("Delta file not found"); + status = -2; + goto execute_destroy; } + _FLOGI("Delta found: %s", client_delta_path); - /* 2. Check client have appropriate delta */ + /* 3. Check client have appropriate delta */ ret = util_file_untar(client_delta_path, FOTA_DELTA_BUILD_STRING_DIR, FOTA_DELTA_BUILD_STRING_FILENAME); if (ret < 0) { status = -1; @@ -87,13 +114,13 @@ int fota_installer_execute(pid_t sender_pid) } if (g_str_has_prefix(buf, old_build_string) != TRUE) { - _FLOGI("%s doesn't have appropriate delta(%s), delta build string(%s)", - appid, old_build_string, buf); + _FLOGI("The caller doesn't have appropriate delta(%s), delta build string(%s)", + old_build_string, buf); status = -3; goto execute_destroy; } - /* 3. Setup local update flag */ + /* 4. Setup local update flag */ ret = util_file_mkdir(FOTA_STATUS_DIR); if (ret < 0) { status = -1; @@ -106,7 +133,7 @@ int fota_installer_execute(pid_t sender_pid) goto execute_destroy; } - /* 4. Trigger update */ + /* 5. Trigger update */ ret = util_file_mkdir(FOTA_DIR); if (ret < 0) { status = -1; diff --git a/update-manager/fota/fota-manager.h b/update-manager/fota/fota-manager.h index bad0e8f..0d9608e 100644 --- a/update-manager/fota/fota-manager.h +++ b/update-manager/fota/fota-manager.h @@ -16,6 +16,7 @@ /* Constant */ #define FOTA_DELTA_FILENAME "delta.tar" +#define FOTA_DELTA_COMPRESSED_FILENAME "delta.tar.gz" #define FOTA_DELTA_BUILD_STRING_DIR "/tmp" #define FOTA_DELTA_BUILD_STRING_FILENAME "build_string.txt" -- 2.34.1