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
0 commit comments