36namespace fs = std::filesystem;
44ENUM(Permission,ALL,HOME,MEDIA,AUDIO,WAYLAND,XORG,DBUS_USER,DBUS_SYSTEM,UDEV,USB,INPUT,GPU,NETWORK,DEV,SHM,OPTICAL);
47inline std::map<Permission,Bits>
const permission_mask =
49 {Permission::HOME, Bits{1} << 0},
50 {Permission::MEDIA, Bits{1} << 1},
51 {Permission::AUDIO, Bits{1} << 2},
52 {Permission::WAYLAND, Bits{1} << 3},
53 {Permission::XORG, Bits{1} << 4},
54 {Permission::DBUS_USER, Bits{1} << 5},
55 {Permission::DBUS_SYSTEM, Bits{1} << 6},
56 {Permission::UDEV, Bits{1} << 7},
57 {Permission::USB, Bits{1} << 8},
58 {Permission::INPUT, Bits{1} << 9},
59 {Permission::GPU, Bits{1} << 10},
60 {Permission::NETWORK, Bits{1} << 11},
61 {Permission::DEV, Bits{1} << 12},
62 {Permission::SHM, Bits{1} << 13},
63 {Permission::OPTICAL, Bits{1} << 14},
74[[nodiscard]]
inline Value<void> bit_set(Bits& bits, Permission
const& permission,
bool value)
noexcept
76 auto it = permission_mask.find(permission);
77 return_if(it == permission_mask.end(), Error(
"E::Permission '{}' not found", permission));
78 Bits mask = it->second;
79 if (value) { bits |= mask; }
80 else { bits &= ~mask; }
90[[nodiscard]]
inline std::set<std::string>
to_strings(Bits
const& bits)
noexcept
92 std::set<std::string> out;
93 for(
auto&& [permission,mask] : permission_mask)
97 std::string str = permission;
98 std::ranges::transform(str, str.begin(), ::tolower);
114 static_assert(
sizeof(Bits) ==
sizeof(uint64_t),
"Bits size must be 8 bytes");
115 uint64_t offset_begin = ns_reserved::FIM_RESERVED_OFFSET_PERMISSIONS_BEGIN;
116 uint64_t offset_end = ns_reserved::FIM_RESERVED_OFFSET_PERMISSIONS_END;
117 return ns_reserved::write(path_file_binary, offset_begin, offset_end,
reinterpret_cast<char const*
>(&bits),
sizeof(bits));
128 static_assert(
sizeof(Bits) ==
sizeof(uint64_t),
"Bits size must be 8 bytes");
129 uint64_t offset_begin = ns_reserved::FIM_RESERVED_OFFSET_PERMISSIONS_BEGIN;
130 uint64_t size = ns_reserved::FIM_RESERVED_OFFSET_PERMISSIONS_END - offset_begin;
131 constexpr size_t const size_bits =
sizeof(Bits);
132 return_if(size_bits != size, Error(
"E::Trying to read an exceeding number of bytes: {} vs {}", size_bits, size));
134 Pop(
ns_reserved::read(path_file_binary, offset_begin,
reinterpret_cast<char*
>(&bits), size_bits));
144 fs::path m_path_file_binary;
146 Value<void> set_permissions(Bits bits, std::set<Permission>
const& permissions,
bool value)
148 if(permissions.contains(Permission::NONE))
150 return Error(
"E::Invalid permission 'NONE'");
152 if(permissions.contains(Permission::ALL))
154 return_if(permissions.size() > 1, Error(
"E::Permission 'all' should not be used with others"));
157 for(Permission
const& permission : permissions)
159 Pop(
bit_set(bits, permission, value));
161 Pop(
write(m_path_file_binary, bits));
170 : m_path_file_binary(path_file_binary)
180 auto permissions = permission_mask
181 | std::views::transform([](
auto&& e){
return e.first; })
182 | std::views::filter([](
auto&& e){
return e != Permission::ALL; })
183 | std::ranges::to<std::set<Permission>>();
184 return set_permissions(Bits{}, permissions, value);
194 return set_permissions(Bits{}, permissions,
true);
204 return set_permissions(Pop(
read(m_path_file_binary)), permissions,
true);
214 return set_permissions(Pop(
read(m_path_file_binary)), permissions,
false);
222 [[nodiscard]]
inline bool contains(Permission
const& permission)
const noexcept
224 return_if(permission == Permission::NONE or permission == Permission::ALL,
false);
225 Bits bits =
read(m_path_file_binary).value_or(0);
226 auto it = permission_mask.find(permission);
227 return_if(it == permission_mask.end(),
false);
228 return (bits & it->second) != 0;
237 return ::ns_reserved::ns_permissions::to_strings(Pop(
read(m_path_file_binary)));
Value< std::set< std::string > > to_strings() const noexcept
Converts enabled permissions to string representations.
Value< void > add(std::set< Permission > const &permissions)
Adds permissions to existing configuration.
Value< void > set_all(bool value)
Sets all permissions to the specified value.
Value< void > del(std::set< Permission > const &permissions)
Removes permissions from existing configuration.
Permissions(fs::path const &path_file_binary)
Constructs a Permissions manager for the given binary.
bool contains(Permission const &permission) const noexcept
Checks if a specific permission is enabled.
Value< void > set(std::set< Permission > const &permissions)
Sets the specified permissions (replaces existing)
Custom enumeration class.
Enhanced error handling framework built on std::expected.
Permission bitfield management in reserved space.
std::set< std::string > to_strings(Bits const &bits) noexcept
Creates a set of lowercase string permission representations.
Value< void > write(fs::path const &path_file_binary, Bits const &bits) noexcept
Write the Bits struct to the given binary.
Value< Bits > read(fs::path const &path_file_binary) noexcept
Read the Bits struct from the given binary.
Value< void > bit_set(Bits &bits, Permission const &permission, bool value) noexcept
Sets a bit permission with the target value.
Value< void > write(fs::path const &path_file_binary, uint64_t offset_begin, uint64_t offset_end, const char *data, uint64_t length) noexcept
Writes data to a file in binary format.
Value< std::streamsize > read(fs::path const &path_file_binary, uint64_t offset, char *data, uint64_t length) noexcept
Reads data from a file in binary format.
Enhanced expected type with integrated logging capabilities.