From 4d9ee16555794beaa64c773b229573c3e5b91e2e Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Mon, 23 Sep 2024 10:52:34 +0700 Subject: [PATCH 1/4] fix: remove cortex tmp after updating --- engine/commands/cortex_upd_cmd.cc | 37 ++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/engine/commands/cortex_upd_cmd.cc b/engine/commands/cortex_upd_cmd.cc index 3c892f6fc..af13f6243 100644 --- a/engine/commands/cortex_upd_cmd.cc +++ b/engine/commands/cortex_upd_cmd.cc @@ -75,10 +75,19 @@ bool CortexUpdCmd::GetStable(const std::string& v) { // Replace binary file auto executable_path = file_manager_utils::GetExecutableFolderContainerPath(); - auto src = std::filesystem::temp_directory_path() / "cortex" / kCortexBinary / - GetCortexBinary(); + auto src = + std::filesystem::temp_directory_path() / "cortex" / GetCortexBinary(); auto dst = executable_path / GetCortexBinary(); - return ReplaceBinaryInflight(src, dst); + auto res = ReplaceBinaryInflight(src, dst); + // Remove cortex tmp folder + if (std::filesystem::exists(src.parent_path())) { + try { + std::filesystem::remove_all(src.parent_path()); + } catch (const std::exception& e) { + CTL_WRN(e.what()); + } + } + return res; } bool CortexUpdCmd::GetBeta(const std::string& v) { @@ -135,7 +144,16 @@ bool CortexUpdCmd::GetBeta(const std::string& v) { auto src = std::filesystem::temp_directory_path() / "cortex" / GetCortexBinary(); auto dst = executable_path / GetCortexBinary(); - return ReplaceBinaryInflight(src, dst); + auto res = ReplaceBinaryInflight(src, dst); + // Remove cortex tmp folder + if (std::filesystem::exists(src.parent_path())) { + try { + std::filesystem::remove_all(src.parent_path()); + } catch (const std::exception& e) { + CTL_WRN(e.what()); + } + } + return res; } bool CortexUpdCmd::HandleGithubRelease(const nlohmann::json& assets, @@ -264,6 +282,15 @@ bool CortexUpdCmd::GetNightly(const std::string& v) { auto src = std::filesystem::temp_directory_path() / "cortex" / GetCortexBinary(); auto dst = executable_path / GetCortexBinary(); - return ReplaceBinaryInflight(src, dst); + auto res = ReplaceBinaryInflight(src, dst); + // Remove cortex tmp folder + if (std::filesystem::exists(src.parent_path())) { + try { + std::filesystem::remove_all(src.parent_path()); + } catch (const std::exception& e) { + CTL_WRN(e.what()); + } + } + return res; } } // namespace commands From 2ef6c952252e2c194678d784bd4063d9389d6951 Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Mon, 23 Sep 2024 11:31:30 +0700 Subject: [PATCH 2/4] fix: use scope_exit --- engine/commands/cortex_upd_cmd.cc | 37 ++++++++++++++++--------------- engine/utils/scope_exit.h | 15 +++++++++++++ 2 files changed, 34 insertions(+), 18 deletions(-) create mode 100644 engine/utils/scope_exit.h diff --git a/engine/commands/cortex_upd_cmd.cc b/engine/commands/cortex_upd_cmd.cc index af13f6243..924241ac8 100644 --- a/engine/commands/cortex_upd_cmd.cc +++ b/engine/commands/cortex_upd_cmd.cc @@ -6,6 +6,7 @@ #include "utils/archive_utils.h" #include "utils/file_manager_utils.h" #include "utils/logging_utils.h" +#include "utils/scope_exit.h" #include "utils/system_info_utils.h" #include "utils/url_parser.h" @@ -78,16 +79,16 @@ bool CortexUpdCmd::GetStable(const std::string& v) { auto src = std::filesystem::temp_directory_path() / "cortex" / GetCortexBinary(); auto dst = executable_path / GetCortexBinary(); - auto res = ReplaceBinaryInflight(src, dst); - // Remove cortex tmp folder - if (std::filesystem::exists(src.parent_path())) { + utils::ScopeExit se([]() { + auto cortex_tmp = std::filesystem::temp_directory_path() / "cortex"; try { - std::filesystem::remove_all(src.parent_path()); + auto n = std::filesystem::remove_all(cortex_tmp); + CTL_INF("Deleted " << n << " files or directories"); } catch (const std::exception& e) { CTL_WRN(e.what()); } - } - return res; + }); + return ReplaceBinaryInflight(src, dst); } bool CortexUpdCmd::GetBeta(const std::string& v) { @@ -144,16 +145,16 @@ bool CortexUpdCmd::GetBeta(const std::string& v) { auto src = std::filesystem::temp_directory_path() / "cortex" / GetCortexBinary(); auto dst = executable_path / GetCortexBinary(); - auto res = ReplaceBinaryInflight(src, dst); - // Remove cortex tmp folder - if (std::filesystem::exists(src.parent_path())) { + utils::ScopeExit se([]() { + auto cortex_tmp = std::filesystem::temp_directory_path() / "cortex"; try { - std::filesystem::remove_all(src.parent_path()); + auto n = std::filesystem::remove_all(cortex_tmp); + CTL_INF("Deleted " << n << " files or directories"); } catch (const std::exception& e) { CTL_WRN(e.what()); } - } - return res; + }); + return ReplaceBinaryInflight(src, dst); } bool CortexUpdCmd::HandleGithubRelease(const nlohmann::json& assets, @@ -282,15 +283,15 @@ bool CortexUpdCmd::GetNightly(const std::string& v) { auto src = std::filesystem::temp_directory_path() / "cortex" / GetCortexBinary(); auto dst = executable_path / GetCortexBinary(); - auto res = ReplaceBinaryInflight(src, dst); - // Remove cortex tmp folder - if (std::filesystem::exists(src.parent_path())) { + utils::ScopeExit se([]() { + auto cortex_tmp = std::filesystem::temp_directory_path() / "cortex"; try { - std::filesystem::remove_all(src.parent_path()); + auto n = std::filesystem::remove_all(cortex_tmp); + CTL_INF("Deleted " << n << " files or directories"); } catch (const std::exception& e) { CTL_WRN(e.what()); } - } - return res; + }); + return ReplaceBinaryInflight(src, dst); } } // namespace commands diff --git a/engine/utils/scope_exit.h b/engine/utils/scope_exit.h new file mode 100644 index 000000000..d79d0951f --- /dev/null +++ b/engine/utils/scope_exit.h @@ -0,0 +1,15 @@ +#pragma once + +namespace utils { +template +struct ScopeExit { + ScopeExit(F&& f) : f_(std::forward(f)) {} + ~ScopeExit() { f_(); } + F f_; +}; + +template +ScopeExit makeScopeExit(F&& f) { + return ScopeExit(std::forward(f)); +}; +} // namespace utils \ No newline at end of file From 97e7711398a7890991c2899d8073be7308e6317f Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Mon, 23 Sep 2024 11:45:49 +0700 Subject: [PATCH 3/4] fix: add e2e test --- engine/e2e-test/test_cortex_update.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/engine/e2e-test/test_cortex_update.py b/engine/e2e-test/test_cortex_update.py index 0c4d3a774..2d7d652ec 100644 --- a/engine/e2e-test/test_cortex_update.py +++ b/engine/e2e-test/test_cortex_update.py @@ -1,5 +1,7 @@ import pytest from test_runner import run +import tempfile +import os class TestCortexUpdate: @@ -10,3 +12,4 @@ def test_cortex_update(self): exit_code, output, error = run("Update cortex", ["update"]) assert exit_code == 0, "Something went wrong" assert "Updated cortex sucessfully" in output + assert os.path.exists(os.path.join(tempfile.gettempdir()), 'cortex') == False From d2429912c61a20b37e16ae1f48c10d814846cf8d Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Mon, 23 Sep 2024 11:51:54 +0700 Subject: [PATCH 4/4] fix: delete cortex tmp before cortex update --- engine/commands/cortex_upd_cmd.cc | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/engine/commands/cortex_upd_cmd.cc b/engine/commands/cortex_upd_cmd.cc index 924241ac8..c6526df66 100644 --- a/engine/commands/cortex_upd_cmd.cc +++ b/engine/commands/cortex_upd_cmd.cc @@ -24,6 +24,16 @@ void CortexUpdCmd::Exec(std::string v) { ssc.Exec(); } } + + // Try to remove cortex temp folder if it exists first + try { + auto n = std::filesystem::remove_all( + std::filesystem::temp_directory_path() / "cortex"); + CTL_INF("Deleted " << n << " files or directories"); + } catch (const std::exception& e) { + CTL_WRN(e.what()); + } + if (CORTEX_VARIANT == file_manager_utils::kProdVariant) { if (!GetStable(v)) return;