From fe8bbade55c8c5b59d61d51df827345c7fb5aa3a Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Thu, 19 Sep 2024 18:43:11 +0700 Subject: [PATCH 1/6] fix: correct permissions owner and group --- engine/commands/cortex_upd_cmd.h | 58 +++++++++++++++++++++++++++----- 1 file changed, 49 insertions(+), 9 deletions(-) diff --git a/engine/commands/cortex_upd_cmd.h b/engine/commands/cortex_upd_cmd.h index 4a94ba01a..2bf57e32b 100644 --- a/engine/commands/cortex_upd_cmd.h +++ b/engine/commands/cortex_upd_cmd.h @@ -1,5 +1,9 @@ #pragma once #include +#if defined(__linux__) +#include +#include +#endif #include "httplib.h" #include "nlohmann/json.hpp" @@ -16,6 +20,14 @@ const std::string kCortexBinary = "cortex"; constexpr const auto kBetaComp = "-rc"; constexpr const auto kReleaseFormat = ".tar.gz"; +inline std::string GetRole() { +#if defined(_WIN32) + return ""; +#else + return "sudo "; +#endif +} + inline std::string GetCortexBinary() { #if defined(_WIN32) constexpr const bool has_exe = true; @@ -88,7 +100,8 @@ inline void CheckNewUpdate() { if (current_version != latest_version) { CLI_LOG("\nA new release of cortex is available: " << current_version << " -> " << latest_version); - CLI_LOG("To upgrade, run: " << GetCortexBinary() << " update"); + CLI_LOG("To upgrade, run: " << GetRole() << GetCortexBinary() + << " update"); if (CORTEX_VARIANT == file_manager_utils::kProdVariant) { CLI_LOG(json_res["html_url"].get()); } @@ -113,25 +126,52 @@ inline bool ReplaceBinaryInflight(const std::filesystem::path& src, } std::filesystem::path temp = dst.parent_path() / "cortex_temp"; + auto restore_binary = [&temp, &dst]() { + if (std::filesystem::exists(temp)) { + std::rename(temp.string().c_str(), dst.string().c_str()); + CLI_LOG("Restored binary file"); + } + }; try { if (std::filesystem::exists(temp)) { std::filesystem::remove(temp); } +#if defined(__linux__) + // Get permissions of the executable file + struct stat dst_file_stat; + if (stat(dst.string().c_str(), &dst_file_stat) != 0) { + CTL_ERR("Error getting permissions of executable file: " << dst.string()); + return false; + } + + // Get owner and group of the executable file + uid_t dst_file_owner = dst_file_stat.st_uid; + gid_t dst_file_group = dst_file_stat.st_gid; +#endif std::rename(dst.string().c_str(), temp.string().c_str()); std::filesystem::copy_file( src, dst, std::filesystem::copy_options::overwrite_existing); - std::filesystem::permissions(dst, std::filesystem::perms::owner_all | - std::filesystem::perms::group_all | - std::filesystem::perms::others_read | - std::filesystem::perms::others_exec); + +#if defined(__linux__) + // Set permissions of the executable file + if (chmod(dst.string().c_str(), dst_file_stat.st_mode) != 0) { + CTL_ERR("Error setting permissions of executable file: " << dst.string()); + restore_binary(); + return false; + } + + // Set owner and group of the executable file + if (chown(dst.string().c_str(), dst_file_owner, dst_file_group) != 0) { + CTL_ERR("Error setting owner and group of executable file: " << dst.string()); + restore_binary(); + return false; + } +#endif } catch (const std::exception& e) { CTL_ERR("Something went wrong: " << e.what()); - if (std::filesystem::exists(temp)) { - std::rename(temp.string().c_str(), dst.string().c_str()); - CLI_LOG("Restored binary file"); - } + restore_binary(); return false; } From 6ef4851b31e4b8d29a44dc182db2fa377f17bd39 Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Thu, 19 Sep 2024 19:38:22 +0700 Subject: [PATCH 2/6] fix: remove cortex_tmp on linux --- engine/commands/cortex_upd_cmd.h | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/engine/commands/cortex_upd_cmd.h b/engine/commands/cortex_upd_cmd.h index 2bf57e32b..d54a5642d 100644 --- a/engine/commands/cortex_upd_cmd.h +++ b/engine/commands/cortex_upd_cmd.h @@ -164,7 +164,15 @@ inline bool ReplaceBinaryInflight(const std::filesystem::path& src, // Set owner and group of the executable file if (chown(dst.string().c_str(), dst_file_owner, dst_file_group) != 0) { - CTL_ERR("Error setting owner and group of executable file: " << dst.string()); + CTL_ERR( + "Error setting owner and group of executable file: " << dst.string()); + restore_binary(); + return false; + } + + // Remove cortex_temp + if (unlink(temp.string().c_str()) != 0) { + CTL_ERR("Error deleting self: " << strerror(errno)); restore_binary(); return false; } From e67c1d22a80a8426f29b35c9631635a38204368f Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Thu, 19 Sep 2024 19:48:58 +0700 Subject: [PATCH 3/6] fix: unit tests --- engine/test/components/test_cortex_upd_cmd.cc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/engine/test/components/test_cortex_upd_cmd.cc b/engine/test/components/test_cortex_upd_cmd.cc index 93f555535..d394628c8 100644 --- a/engine/test/components/test_cortex_upd_cmd.cc +++ b/engine/test/components/test_cortex_upd_cmd.cc @@ -43,7 +43,11 @@ TEST_F(CortexUpdCmdTest, replace_binary_successfully) { std::filesystem::path new_binary(kNewReleaseFile); std::filesystem::path cur_binary(kCurReleaseFile); EXPECT_TRUE(commands::ReplaceBinaryInflight(new_binary, cur_binary)); +#if defined(__linux__) + EXPECT_FALSE(std::filesystem::exists(kCortexTemp)); +#else EXPECT_TRUE(std::filesystem::exists(kCortexTemp)); +#endif } TEST_F(CortexUpdCmdTest, should_restore_old_binary_if_has_error) { From 4c160fe05f9e643bfa3fa6518a686e8800b0386c Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Thu, 19 Sep 2024 19:53:23 +0700 Subject: [PATCH 4/6] fix: ifdef --- engine/commands/cortex_upd_cmd.h | 8 ++++---- engine/test/components/test_cortex_upd_cmd.cc | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/engine/commands/cortex_upd_cmd.h b/engine/commands/cortex_upd_cmd.h index d54a5642d..4d2899a36 100644 --- a/engine/commands/cortex_upd_cmd.h +++ b/engine/commands/cortex_upd_cmd.h @@ -1,6 +1,6 @@ #pragma once #include -#if defined(__linux__) +#if !defined(_WIN32) #include #include #endif @@ -137,7 +137,7 @@ inline bool ReplaceBinaryInflight(const std::filesystem::path& src, if (std::filesystem::exists(temp)) { std::filesystem::remove(temp); } -#if defined(__linux__) +#if !defined(_WIN32) // Get permissions of the executable file struct stat dst_file_stat; if (stat(dst.string().c_str(), &dst_file_stat) != 0) { @@ -154,7 +154,7 @@ inline bool ReplaceBinaryInflight(const std::filesystem::path& src, std::filesystem::copy_file( src, dst, std::filesystem::copy_options::overwrite_existing); -#if defined(__linux__) +#if !defined(_WIN32) // Set permissions of the executable file if (chmod(dst.string().c_str(), dst_file_stat.st_mode) != 0) { CTL_ERR("Error setting permissions of executable file: " << dst.string()); @@ -169,7 +169,7 @@ inline bool ReplaceBinaryInflight(const std::filesystem::path& src, restore_binary(); return false; } - + // Remove cortex_temp if (unlink(temp.string().c_str()) != 0) { CTL_ERR("Error deleting self: " << strerror(errno)); diff --git a/engine/test/components/test_cortex_upd_cmd.cc b/engine/test/components/test_cortex_upd_cmd.cc index d394628c8..879a4c854 100644 --- a/engine/test/components/test_cortex_upd_cmd.cc +++ b/engine/test/components/test_cortex_upd_cmd.cc @@ -43,7 +43,7 @@ TEST_F(CortexUpdCmdTest, replace_binary_successfully) { std::filesystem::path new_binary(kNewReleaseFile); std::filesystem::path cur_binary(kCurReleaseFile); EXPECT_TRUE(commands::ReplaceBinaryInflight(new_binary, cur_binary)); -#if defined(__linux__) +#if !defined(_WIN32) EXPECT_FALSE(std::filesystem::exists(kCortexTemp)); #else EXPECT_TRUE(std::filesystem::exists(kCortexTemp)); From d9837945bfb7c36e0d6670b85d10e8906dfb2179 Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Thu, 19 Sep 2024 21:02:02 +0700 Subject: [PATCH 5/6] fix: more checks on unit tests --- engine/test/components/test_cortex_upd_cmd.cc | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/engine/test/components/test_cortex_upd_cmd.cc b/engine/test/components/test_cortex_upd_cmd.cc index 879a4c854..601677b6a 100644 --- a/engine/test/components/test_cortex_upd_cmd.cc +++ b/engine/test/components/test_cortex_upd_cmd.cc @@ -42,9 +42,23 @@ TEST_F(CortexUpdCmdTest, return_true_if_self_replace) { TEST_F(CortexUpdCmdTest, replace_binary_successfully) { std::filesystem::path new_binary(kNewReleaseFile); std::filesystem::path cur_binary(kCurReleaseFile); +#if !defined(_WIN32) + struct stat cur_file_stat; + EXPECT_TRUE(stat(cur_binary.string().c_str(), &cur_file_stat) == 0); +#endif + EXPECT_TRUE(commands::ReplaceBinaryInflight(new_binary, cur_binary)); + #if !defined(_WIN32) EXPECT_FALSE(std::filesystem::exists(kCortexTemp)); + + struct stat new_file_stat; + EXPECT_TRUE(stat(cur_binary.string().c_str(), &new_file_stat) == 0); + uid_t new_file_owner = new_file_stat.st_uid; + gid_t new_file_group = new_file_stat.st_gid; + EXPECT_EQ(cur_file_stat.st_uid, new_file_stat.st_uid); + EXPECT_EQ(cur_file_stat.st_gid, new_file_stat.st_gid); + EXPECT_EQ(cur_file_stat.st_mode, new_file_stat.st_mode); #else EXPECT_TRUE(std::filesystem::exists(kCortexTemp)); #endif From fb85d142926fa898c347961141ecac3b069a67fc Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Thu, 19 Sep 2024 21:04:32 +0700 Subject: [PATCH 6/6] fix: remove unused --- engine/test/components/test_cortex_upd_cmd.cc | 2 -- 1 file changed, 2 deletions(-) diff --git a/engine/test/components/test_cortex_upd_cmd.cc b/engine/test/components/test_cortex_upd_cmd.cc index 601677b6a..ff70c814d 100644 --- a/engine/test/components/test_cortex_upd_cmd.cc +++ b/engine/test/components/test_cortex_upd_cmd.cc @@ -54,8 +54,6 @@ TEST_F(CortexUpdCmdTest, replace_binary_successfully) { struct stat new_file_stat; EXPECT_TRUE(stat(cur_binary.string().c_str(), &new_file_stat) == 0); - uid_t new_file_owner = new_file_stat.st_uid; - gid_t new_file_group = new_file_stat.st_gid; EXPECT_EQ(cur_file_stat.st_uid, new_file_stat.st_uid); EXPECT_EQ(cur_file_stat.st_gid, new_file_stat.st_gid); EXPECT_EQ(cur_file_stat.st_mode, new_file_stat.st_mode);