Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit 440856b

Browse files
authored
fix: remove cortex tmp after cortex update (#1295)
* fix: remove cortex tmp after updating * fix: use scope_exit * fix: add e2e test * fix: delete cortex tmp before cortex update
1 parent 8ac2615 commit 440856b

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

engine/commands/cortex_upd_cmd.cc

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "utils/archive_utils.h"
77
#include "utils/file_manager_utils.h"
88
#include "utils/logging_utils.h"
9+
#include "utils/scope_exit.h"
910
#include "utils/system_info_utils.h"
1011
#include "utils/url_parser.h"
1112

@@ -23,6 +24,16 @@ void CortexUpdCmd::Exec(std::string v) {
2324
ssc.Exec();
2425
}
2526
}
27+
28+
// Try to remove cortex temp folder if it exists first
29+
try {
30+
auto n = std::filesystem::remove_all(
31+
std::filesystem::temp_directory_path() / "cortex");
32+
CTL_INF("Deleted " << n << " files or directories");
33+
} catch (const std::exception& e) {
34+
CTL_WRN(e.what());
35+
}
36+
2637
if (CORTEX_VARIANT == file_manager_utils::kProdVariant) {
2738
if (!GetStable(v))
2839
return;
@@ -75,9 +86,18 @@ bool CortexUpdCmd::GetStable(const std::string& v) {
7586

7687
// Replace binary file
7788
auto executable_path = file_manager_utils::GetExecutableFolderContainerPath();
78-
auto src = std::filesystem::temp_directory_path() / "cortex" / kCortexBinary /
79-
GetCortexBinary();
89+
auto src =
90+
std::filesystem::temp_directory_path() / "cortex" / GetCortexBinary();
8091
auto dst = executable_path / GetCortexBinary();
92+
utils::ScopeExit se([]() {
93+
auto cortex_tmp = std::filesystem::temp_directory_path() / "cortex";
94+
try {
95+
auto n = std::filesystem::remove_all(cortex_tmp);
96+
CTL_INF("Deleted " << n << " files or directories");
97+
} catch (const std::exception& e) {
98+
CTL_WRN(e.what());
99+
}
100+
});
81101
return ReplaceBinaryInflight(src, dst);
82102
}
83103

@@ -135,6 +155,15 @@ bool CortexUpdCmd::GetBeta(const std::string& v) {
135155
auto src =
136156
std::filesystem::temp_directory_path() / "cortex" / GetCortexBinary();
137157
auto dst = executable_path / GetCortexBinary();
158+
utils::ScopeExit se([]() {
159+
auto cortex_tmp = std::filesystem::temp_directory_path() / "cortex";
160+
try {
161+
auto n = std::filesystem::remove_all(cortex_tmp);
162+
CTL_INF("Deleted " << n << " files or directories");
163+
} catch (const std::exception& e) {
164+
CTL_WRN(e.what());
165+
}
166+
});
138167
return ReplaceBinaryInflight(src, dst);
139168
}
140169

@@ -264,6 +293,15 @@ bool CortexUpdCmd::GetNightly(const std::string& v) {
264293
auto src =
265294
std::filesystem::temp_directory_path() / "cortex" / GetCortexBinary();
266295
auto dst = executable_path / GetCortexBinary();
296+
utils::ScopeExit se([]() {
297+
auto cortex_tmp = std::filesystem::temp_directory_path() / "cortex";
298+
try {
299+
auto n = std::filesystem::remove_all(cortex_tmp);
300+
CTL_INF("Deleted " << n << " files or directories");
301+
} catch (const std::exception& e) {
302+
CTL_WRN(e.what());
303+
}
304+
});
267305
return ReplaceBinaryInflight(src, dst);
268306
}
269307
} // namespace commands

engine/e2e-test/test_cortex_update.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import pytest
22
from test_runner import run
3+
import tempfile
4+
import os
35

46

57
class TestCortexUpdate:
@@ -10,3 +12,4 @@ def test_cortex_update(self):
1012
exit_code, output, error = run("Update cortex", ["update"])
1113
assert exit_code == 0, "Something went wrong"
1214
assert "Updated cortex sucessfully" in output
15+
assert os.path.exists(os.path.join(tempfile.gettempdir()), 'cortex') == False

engine/utils/scope_exit.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
3+
namespace utils {
4+
template <typename F>
5+
struct ScopeExit {
6+
ScopeExit(F&& f) : f_(std::forward<F>(f)) {}
7+
~ScopeExit() { f_(); }
8+
F f_;
9+
};
10+
11+
template <typename F>
12+
ScopeExit<F> makeScopeExit(F&& f) {
13+
return ScopeExit<F>(std::forward<F>(f));
14+
};
15+
} // namespace utils

0 commit comments

Comments
 (0)