diff --git a/engine/commands/cortex_upd_cmd.cc b/engine/commands/cortex_upd_cmd.cc index 3c892f6fc..c6526df66 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" @@ -23,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; @@ -75,9 +86,18 @@ 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(); + utils::ScopeExit se([]() { + auto cortex_tmp = std::filesystem::temp_directory_path() / "cortex"; + try { + 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 ReplaceBinaryInflight(src, dst); } @@ -135,6 +155,15 @@ bool CortexUpdCmd::GetBeta(const std::string& v) { auto src = std::filesystem::temp_directory_path() / "cortex" / GetCortexBinary(); auto dst = executable_path / GetCortexBinary(); + utils::ScopeExit se([]() { + auto cortex_tmp = std::filesystem::temp_directory_path() / "cortex"; + try { + 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 ReplaceBinaryInflight(src, dst); } @@ -264,6 +293,15 @@ bool CortexUpdCmd::GetNightly(const std::string& v) { auto src = std::filesystem::temp_directory_path() / "cortex" / GetCortexBinary(); auto dst = executable_path / GetCortexBinary(); + utils::ScopeExit se([]() { + auto cortex_tmp = std::filesystem::temp_directory_path() / "cortex"; + try { + 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 ReplaceBinaryInflight(src, dst); } } // namespace commands 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 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