forked from janhq/cortex.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset_permission_utils.h
More file actions
76 lines (66 loc) · 2.71 KB
/
set_permission_utils.h
File metadata and controls
76 lines (66 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#pragma once
#include <filesystem>
#include <iostream>
#include <system_error>
#include <vector>
#ifdef _WIN32
#include <windows.h>
#else
#include <sys/stat.h>
#endif
#include "utils/logging_utils.h"
namespace set_permission_utils {
// Cross-platform method to set execute permission for a single file
[[nodiscard]] inline bool SetExecutePermission(const std::filesystem::path& filePath,
bool ownerOnly = false) noexcept {
try {
std::filesystem::perms current_perms = std::filesystem::status(filePath).permissions();
std::filesystem::perms new_perms;
if (ownerOnly) {
new_perms = current_perms | std::filesystem::perms::owner_exec;
// Remove group and others execute permissions
new_perms &= ~(std::filesystem::perms::group_exec | std::filesystem::perms::others_exec);
} else {
new_perms = current_perms | std::filesystem::perms::owner_exec |
std::filesystem::perms::group_exec |
std::filesystem::perms::others_exec;
}
std::filesystem::permissions(filePath, new_perms,
std::filesystem::perm_options::replace);
return true;
} catch (const std::filesystem::filesystem_error& e) {
CTL_ERR("Permission error for file " << filePath.string()
<< ": " << e.what());
return false;
} catch (const std::exception& e) {
CTL_ERR("Unexpected error for file " << filePath.string()
<< ": " << e.what());
return false;
}
}
[[nodiscard]] inline std::vector<std::filesystem::path> SetExecutePermissionsRecursive(
const std::filesystem::path& directoryPath,
bool ownerOnly = false,
bool skipDirectories = true) {
std::vector<std::filesystem::path> modifiedFiles;
modifiedFiles.reserve(100); // Reserve space to prevent frequent reallocations
try {
const auto options = std::filesystem::directory_options::skip_permission_denied |
std::filesystem::directory_options::follow_directory_symlink;
for (const auto& entry :
std::filesystem::recursive_directory_iterator(directoryPath, options)) {
if (skipDirectories && entry.is_directory()) {
continue;
}
if (entry.is_regular_file()) {
if (SetExecutePermission(entry.path(), ownerOnly)) {
modifiedFiles.push_back(entry.path());
}
}
}
} catch (const std::filesystem::filesystem_error& e) {
CTL_ERR("Filesystem error: " << e.what());
}
return modifiedFiles;
}
} // namespace set_permission_utils