11#pragma once
22#include < string>
3+ #if !defined(_WIN32)
4+ #include < sys/stat.h>
5+ #include < unistd.h>
6+ #endif
37
48#include " httplib.h"
59#include " nlohmann/json.hpp"
@@ -16,6 +20,14 @@ const std::string kCortexBinary = "cortex";
1620constexpr const auto kBetaComp = " -rc" ;
1721constexpr const auto kReleaseFormat = " .tar.gz" ;
1822
23+ inline std::string GetRole () {
24+ #if defined(_WIN32)
25+ return " " ;
26+ #else
27+ return " sudo " ;
28+ #endif
29+ }
30+
1931inline std::string GetCortexBinary () {
2032#if defined(_WIN32)
2133 constexpr const bool has_exe = true ;
@@ -88,7 +100,8 @@ inline void CheckNewUpdate() {
88100 if (current_version != latest_version) {
89101 CLI_LOG (" \n A new release of cortex is available: "
90102 << current_version << " -> " << latest_version);
91- CLI_LOG (" To upgrade, run: " << GetCortexBinary () << " update" );
103+ CLI_LOG (" To upgrade, run: " << GetRole () << GetCortexBinary ()
104+ << " update" );
92105 if (CORTEX_VARIANT == file_manager_utils::kProdVariant ) {
93106 CLI_LOG (json_res[" html_url" ].get <std::string>());
94107 }
@@ -113,25 +126,60 @@ inline bool ReplaceBinaryInflight(const std::filesystem::path& src,
113126 }
114127
115128 std::filesystem::path temp = dst.parent_path () / " cortex_temp" ;
129+ auto restore_binary = [&temp, &dst]() {
130+ if (std::filesystem::exists (temp)) {
131+ std::rename (temp.string ().c_str (), dst.string ().c_str ());
132+ CLI_LOG (" Restored binary file" );
133+ }
134+ };
116135
117136 try {
118137 if (std::filesystem::exists (temp)) {
119138 std::filesystem::remove (temp);
120139 }
140+ #if !defined(_WIN32)
141+ // Get permissions of the executable file
142+ struct stat dst_file_stat;
143+ if (stat (dst.string ().c_str (), &dst_file_stat) != 0 ) {
144+ CTL_ERR (" Error getting permissions of executable file: " << dst.string ());
145+ return false ;
146+ }
147+
148+ // Get owner and group of the executable file
149+ uid_t dst_file_owner = dst_file_stat.st_uid ;
150+ gid_t dst_file_group = dst_file_stat.st_gid ;
151+ #endif
121152
122153 std::rename (dst.string ().c_str (), temp.string ().c_str ());
123154 std::filesystem::copy_file (
124155 src, dst, std::filesystem::copy_options::overwrite_existing);
125- std::filesystem::permissions (dst, std::filesystem::perms::owner_all |
126- std::filesystem::perms::group_all |
127- std::filesystem::perms::others_read |
128- std::filesystem::perms::others_exec);
156+
157+ #if !defined(_WIN32)
158+ // Set permissions of the executable file
159+ if (chmod (dst.string ().c_str (), dst_file_stat.st_mode ) != 0 ) {
160+ CTL_ERR (" Error setting permissions of executable file: " << dst.string ());
161+ restore_binary ();
162+ return false ;
163+ }
164+
165+ // Set owner and group of the executable file
166+ if (chown (dst.string ().c_str (), dst_file_owner, dst_file_group) != 0 ) {
167+ CTL_ERR (
168+ " Error setting owner and group of executable file: " << dst.string ());
169+ restore_binary ();
170+ return false ;
171+ }
172+
173+ // Remove cortex_temp
174+ if (unlink (temp.string ().c_str ()) != 0 ) {
175+ CTL_ERR (" Error deleting self: " << strerror (errno));
176+ restore_binary ();
177+ return false ;
178+ }
179+ #endif
129180 } catch (const std::exception& e) {
130181 CTL_ERR (" Something went wrong: " << e.what ());
131- if (std::filesystem::exists (temp)) {
132- std::rename (temp.string ().c_str (), dst.string ().c_str ());
133- CLI_LOG (" Restored binary file" );
134- }
182+ restore_binary ();
135183 return false ;
136184 }
137185
0 commit comments