From da0ef3e05e5dc20706d8eadfe0ec933ec83dea57 Mon Sep 17 00:00:00 2001 From: Castulo Martinez Date: Fri, 27 Mar 2020 09:58:59 -0700 Subject: [PATCH] Remove extra slash from paths This commit removes the extra slash '/' from the path from: - the update_boot function - the verifytime script Signed-off-by: Castulo Martinez --- src/scripts.c | 2 +- src/verifytime.c | 44 ++++++++++++++++++-- test/functional/os-install/install-json.bats | 2 +- test/functional/repair/repair-boot-file.bats | 2 +- test/functional/repair/repair-json.bats | 2 +- test/functional/update/update-boot-file.bats | 2 +- 6 files changed, 45 insertions(+), 9 deletions(-) diff --git a/src/scripts.c b/src/scripts.c index d6d4d300..4d719cc7 100644 --- a/src/scripts.c +++ b/src/scripts.c @@ -58,7 +58,7 @@ static void update_boot(void) if (strcmp("/", globals.path_prefix) == 0) { run_script_if_exists("/usr/bin/clr-boot-manager", "update", NULL); } else { - string_or_die(&scriptname, "%s/usr/bin/clr-boot-manager", globals.path_prefix); + scriptname = sys_path_join("%s/%s", globals.path_prefix, "/usr/bin/clr-boot-manager"); run_script_if_exists(scriptname, "update", "--path", globals.path_prefix, NULL); free_and_clear_pointer(&scriptname); } diff --git a/src/verifytime.c b/src/verifytime.c index 06952bd2..90d00f25 100644 --- a/src/verifytime.c +++ b/src/verifytime.c @@ -24,6 +24,7 @@ #include "lib/log.h" #include +#include #include #include #include @@ -31,6 +32,44 @@ #include #define DAY_SECONDS 86400 +#define PATH_SEPARATOR '/' + +/* this function is a copy from sys_path_join() from lib/sys.c */ +static char *path_join(const char *fmt, ...) +{ + char *path; + va_list ap; + int len; + int i, j; + + /* merge arguments into one path */ + va_start(ap, fmt); + if (vasprintf(&path, fmt, ap) < 0) { + abort(); + } + va_end(ap); + + len = strlen(path); + char *pretty_path = malloc(strlen(path) + 1); + if (!pretty_path) { + abort(); + } + + /* remove all duplicated PATH_SEPARATOR from the path */ + for (i = j = 0; i < len; i++) { + if (path[i] == PATH_SEPARATOR && path[i + 1] == PATH_SEPARATOR) { + /* duplicated PATH_SEPARATOR, throw it away */ + continue; + } + pretty_path[j] = path[i]; + j++; + } + pretty_path[j] = '\0'; + + free(path); + + return pretty_path; +} static unsigned long int get_versionstamp(char *path_prefix) { @@ -39,10 +78,7 @@ static unsigned long int get_versionstamp(char *path_prefix) char *filename; unsigned long int version_num; - if (asprintf(&filename, "%s/usr/share/clear/versionstamp", path_prefix ? path_prefix : "") < 0) { - error("Failed to get the versionstamp\n"); - return 0; - } + filename = path_join("%s/usr/share/clear/versionstamp", path_prefix ? path_prefix : ""); errno = 0; fp = fopen(filename, "r"); diff --git a/test/functional/os-install/install-json.bats b/test/functional/os-install/install-json.bats index ea65ff62..07f922da 100755 --- a/test/functional/os-install/install-json.bats +++ b/test/functional/os-install/install-json.bats @@ -61,7 +61,7 @@ test_setup() { { "type" : "info", "msg" : " 0 of 2 missing files were not installed" }, { "type" : "progress", "currentStep" : 9, "totalSteps" : 9, "stepCompletion" : -1, "stepDescription" : "run_postupdate_scripts" }, { "type" : "info", "msg" : "Calling post-update helper scripts" }, - { "type" : "warning", "msg" : "helper script ($TEST_DIRNAME/testfs/target-dir//usr/bin/clr-boot-manager) not found, it will be skipped" }, + { "type" : "warning", "msg" : "helper script ($TEST_DIRNAME/testfs/target-dir/usr/bin/clr-boot-manager) not found, it will be skipped" }, { "type" : "info", "msg" : " Installation successful" }, { "type" : "progress", "currentStep" : 9, "totalSteps" : 9, "stepCompletion" : 100, "stepDescription" : "run_postupdate_scripts" }, { "type" : "end", "section" : "os-install", "status" : 0 } diff --git a/test/functional/repair/repair-boot-file.bats b/test/functional/repair/repair-boot-file.bats index 67b2f3f7..9e5c7744 100755 --- a/test/functional/repair/repair-boot-file.bats +++ b/test/functional/repair/repair-boot-file.bats @@ -36,7 +36,7 @@ test_setup() { 1 of 1 files were repaired 0 of 1 files were not repaired Calling post-update helper scripts - Warning: helper script ($PATH_PREFIX//usr/bin/clr-boot-manager) not found, it will be skipped + Warning: helper script ($PATH_PREFIX/usr/bin/clr-boot-manager) not found, it will be skipped Repair successful EOM ) diff --git a/test/functional/repair/repair-json.bats b/test/functional/repair/repair-json.bats index 8a92a687..c6eae7b1 100755 --- a/test/functional/repair/repair-json.bats +++ b/test/functional/repair/repair-json.bats @@ -153,7 +153,7 @@ test_setup() { { "type" : "progress", "currentStep" : 9, "totalSteps" : 10, "stepCompletion" : 100, "stepDescription" : "remove_extra_files" }, { "type" : "progress", "currentStep" : 10, "totalSteps" : 10, "stepCompletion" : -1, "stepDescription" : "run_postupdate_scripts" }, { "type" : "info", "msg" : "Calling post-update helper scripts" }, - { "type" : "warning", "msg" : "helper script ($PATH_PREFIX//usr/bin/clr-boot-manager) not found, it will be skipped" }, + { "type" : "warning", "msg" : "helper script ($PATH_PREFIX/usr/bin/clr-boot-manager) not found, it will be skipped" }, { "type" : "info", "msg" : " Repair successful" }, { "type" : "progress", "currentStep" : 10, "totalSteps" : 10, "stepCompletion" : 100, "stepDescription" : "run_postupdate_scripts" }, { "type" : "end", "section" : "repair", "status" : 0 } diff --git a/test/functional/update/update-boot-file.bats b/test/functional/update/update-boot-file.bats index b56be4bc..7c904171 100755 --- a/test/functional/update/update-boot-file.bats +++ b/test/functional/update/update-boot-file.bats @@ -32,7 +32,7 @@ test_setup() { Installing files... Update was applied Calling post-update helper scripts - Warning: helper script ($TEST_DIRNAME/testfs/target-dir//usr/bin/clr-boot-manager) not found, it will be skipped + Warning: helper script ($TEST_DIRNAME/testfs/target-dir/usr/bin/clr-boot-manager) not found, it will be skipped Update successful - System updated from version 10 to version 100 EOM )