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 ;
@@ -38,7 +49,7 @@ void CortexUpdCmd::Exec(std::string v) {
3849
3950bool CortexUpdCmd::GetStable (const std::string& v) {
4051 auto system_info = system_info_utils::GetSystemInfo ();
41- CTL_INF (" OS: " << system_info. os << " , Arch: " << system_info. arch );
52+ CTL_INF (" OS: " << system_info-> os << " , Arch: " << system_info-> arch );
4253
4354 // Download file
4455 auto github_host = GetHostName ();
@@ -56,7 +67,7 @@ bool CortexUpdCmd::GetStable(const std::string& v) {
5667 }
5768
5869 if (!HandleGithubRelease (json_data[" assets" ],
59- {system_info. os + " -" + system_info. arch })) {
70+ {system_info-> os + " -" + system_info-> arch })) {
6071 return false ;
6172 }
6273 } catch (const nlohmann::json::parse_error& e) {
@@ -75,15 +86,24 @@ 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
84104bool CortexUpdCmd::GetBeta (const std::string& v) {
85105 auto system_info = system_info_utils::GetSystemInfo ();
86- CTL_INF (" OS: " << system_info. os << " , Arch: " << system_info. arch );
106+ CTL_INF (" OS: " << system_info-> os << " , Arch: " << system_info-> arch );
87107
88108 // Download file
89109 auto github_host = GetHostName ();
@@ -113,7 +133,7 @@ bool CortexUpdCmd::GetBeta(const std::string& v) {
113133 }
114134
115135 if (!HandleGithubRelease (json_data[" assets" ],
116- {system_info. os + " -" + system_info. arch })) {
136+ {system_info-> os + " -" + system_info-> arch })) {
117137 return false ;
118138 }
119139 } catch (const nlohmann::json::parse_error& e) {
@@ -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
@@ -205,11 +234,11 @@ bool CortexUpdCmd::HandleGithubRelease(const nlohmann::json& assets,
205234
206235bool CortexUpdCmd::GetNightly (const std::string& v) {
207236 auto system_info = system_info_utils::GetSystemInfo ();
208- CTL_INF (" OS: " << system_info. os << " , Arch: " << system_info. arch );
237+ CTL_INF (" OS: " << system_info-> os << " , Arch: " << system_info-> arch );
209238
210239 // Download file
211240 std::string version = v.empty () ? " latest" : std::move (v);
212- std::string os_arch{system_info. os + " -" + system_info. arch };
241+ std::string os_arch{system_info-> os + " -" + system_info-> arch };
213242 const char * paths[] = {
214243 " cortex" ,
215244 version.c_str (),
@@ -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