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

Commit ad61259

Browse files
authored
fix: correct permissions owner and group (#1267)
* fix: correct permissions owner and group * fix: remove cortex_tmp on linux * fix: unit tests * fix: ifdef * fix: more checks on unit tests * fix: remove unused
1 parent 22467b7 commit ad61259

File tree

2 files changed

+73
-9
lines changed

2 files changed

+73
-9
lines changed

engine/commands/cortex_upd_cmd.h

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
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";
1620
constexpr const auto kBetaComp = "-rc";
1721
constexpr 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+
1931
inline 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("\nA 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

engine/test/components/test_cortex_upd_cmd.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,24 @@ TEST_F(CortexUpdCmdTest, return_true_if_self_replace) {
4242
TEST_F(CortexUpdCmdTest, replace_binary_successfully) {
4343
std::filesystem::path new_binary(kNewReleaseFile);
4444
std::filesystem::path cur_binary(kCurReleaseFile);
45+
#if !defined(_WIN32)
46+
struct stat cur_file_stat;
47+
EXPECT_TRUE(stat(cur_binary.string().c_str(), &cur_file_stat) == 0);
48+
#endif
49+
4550
EXPECT_TRUE(commands::ReplaceBinaryInflight(new_binary, cur_binary));
51+
52+
#if !defined(_WIN32)
53+
EXPECT_FALSE(std::filesystem::exists(kCortexTemp));
54+
55+
struct stat new_file_stat;
56+
EXPECT_TRUE(stat(cur_binary.string().c_str(), &new_file_stat) == 0);
57+
EXPECT_EQ(cur_file_stat.st_uid, new_file_stat.st_uid);
58+
EXPECT_EQ(cur_file_stat.st_gid, new_file_stat.st_gid);
59+
EXPECT_EQ(cur_file_stat.st_mode, new_file_stat.st_mode);
60+
#else
4661
EXPECT_TRUE(std::filesystem::exists(kCortexTemp));
62+
#endif
4763
}
4864

4965
TEST_F(CortexUpdCmdTest, should_restore_old_binary_if_has_error) {

0 commit comments

Comments
 (0)