FlatImage
A configurable Linux containerization system
Loading...
Searching...
No Matches
reserved.hpp
Go to the documentation of this file.
1
8
9#pragma once
10
11#include <expected>
12#include <fcntl.h>
13#include <filesystem>
14#include <cstdint>
15#include <fstream>
16
17#include "../common.hpp"
18#include "../macro.hpp"
19#include "../std/expected.hpp"
20
32namespace ns_reserved
33{
34
35extern "C" uint32_t FIM_RESERVED_OFFSET;
36
37namespace
38{
39namespace fs = std::filesystem;
40
49{
50 // Permissions
51 constexpr static uint64_t const fim_reserved_offset_permissions_begin = 0;
52 constexpr static uint64_t const fim_reserved_offset_permissions_end = fim_reserved_offset_permissions_begin + 8;
53 // notify
54 constexpr static uint64_t const fim_reserved_offset_notify_begin = fim_reserved_offset_permissions_end;
55 constexpr static uint64_t const fim_reserved_offset_notify_end = fim_reserved_offset_notify_begin + 1;
56 // overlay
57 constexpr static uint64_t const fim_reserved_offset_overlay_begin = fim_reserved_offset_notify_end;
58 constexpr static uint64_t const fim_reserved_offset_overlay_end = fim_reserved_offset_overlay_begin + 1;
59 // casefold
60 constexpr static uint64_t const fim_reserved_offset_casefold_begin = fim_reserved_offset_overlay_end;
61 constexpr static uint64_t const fim_reserved_offset_casefold_end = fim_reserved_offset_casefold_begin + 1;
62 // desktop
63 constexpr static uint64_t const fim_reserved_offset_desktop_begin = fim_reserved_offset_casefold_end;
64 constexpr static uint64_t const fim_reserved_offset_desktop_end = fim_reserved_offset_desktop_begin + 4_kib;
65 // boot
66 constexpr static uint64_t const fim_reserved_offset_boot_begin = fim_reserved_offset_desktop_end;
67 constexpr static uint64_t const fim_reserved_offset_boot_end = fim_reserved_offset_boot_begin + 8_kib;
68 // icon
69 constexpr static uint64_t const fim_reserved_offset_icon_begin = fim_reserved_offset_boot_end;
70 constexpr static uint64_t const fim_reserved_offset_icon_end = fim_reserved_offset_icon_begin + 1_mib;
71 // environment
72 constexpr static uint64_t const fim_reserved_offset_environment_begin = fim_reserved_offset_icon_end;
73 constexpr static uint64_t const fim_reserved_offset_environment_end = fim_reserved_offset_environment_begin + 1_mib;
74 // bindings
75 constexpr static uint64_t const fim_reserved_offset_bindings_begin = fim_reserved_offset_environment_end;
76 constexpr static uint64_t const fim_reserved_offset_bindings_end = fim_reserved_offset_bindings_begin + 1_mib;
77 // remote
78 constexpr static uint64_t const fim_reserved_offset_remote_begin = fim_reserved_offset_bindings_end;
79 constexpr static uint64_t const fim_reserved_offset_remote_end = fim_reserved_offset_remote_begin + 4_kib;
80 // unshare
81 constexpr static uint64_t const fim_reserved_offset_unshare_begin = fim_reserved_offset_remote_end;
82 constexpr static uint64_t const fim_reserved_offset_unshare_end = fim_reserved_offset_unshare_begin + 2;
83
87 constexpr Reserved()
88 {
89 static_assert(fim_reserved_offset_unshare_end < FIM_RESERVED_SIZE, "Insufficient reserved space");
90 }
91};
92
93constexpr Reserved reserved;
94}
95
96// Permissions
97uint64_t const FIM_RESERVED_OFFSET_PERMISSIONS_BEGIN = FIM_RESERVED_OFFSET + reserved.fim_reserved_offset_permissions_begin;
98uint64_t const FIM_RESERVED_OFFSET_PERMISSIONS_END = FIM_RESERVED_OFFSET + reserved.fim_reserved_offset_permissions_end;
99// Notify
100uint64_t const FIM_RESERVED_OFFSET_NOTIFY_BEGIN = FIM_RESERVED_OFFSET + reserved.fim_reserved_offset_notify_begin;
101uint64_t const FIM_RESERVED_OFFSET_NOTIFY_END = FIM_RESERVED_OFFSET + reserved.fim_reserved_offset_notify_end;
102// Overlay
103uint64_t const FIM_RESERVED_OFFSET_OVERLAY_BEGIN = FIM_RESERVED_OFFSET + reserved.fim_reserved_offset_overlay_begin;
104uint64_t const FIM_RESERVED_OFFSET_OVERLAY_END = FIM_RESERVED_OFFSET + reserved.fim_reserved_offset_overlay_end;
105// Casefold
106uint64_t const FIM_RESERVED_OFFSET_CASEFOLD_BEGIN = FIM_RESERVED_OFFSET + reserved.fim_reserved_offset_casefold_begin;
107uint64_t const FIM_RESERVED_OFFSET_CASEFOLD_END = FIM_RESERVED_OFFSET + reserved.fim_reserved_offset_casefold_end;
108// Desktop
109uint64_t const FIM_RESERVED_OFFSET_DESKTOP_BEGIN = FIM_RESERVED_OFFSET + reserved.fim_reserved_offset_desktop_begin;
110uint64_t const FIM_RESERVED_OFFSET_DESKTOP_END = FIM_RESERVED_OFFSET + reserved.fim_reserved_offset_desktop_end;
111// Boot
112uint64_t const FIM_RESERVED_OFFSET_BOOT_BEGIN = FIM_RESERVED_OFFSET + reserved.fim_reserved_offset_boot_begin;
113uint64_t const FIM_RESERVED_OFFSET_BOOT_END = FIM_RESERVED_OFFSET + reserved.fim_reserved_offset_boot_end;
114// Icon
115uint64_t const FIM_RESERVED_OFFSET_ICON_BEGIN = FIM_RESERVED_OFFSET + reserved.fim_reserved_offset_icon_begin;
116uint64_t const FIM_RESERVED_OFFSET_ICON_END = FIM_RESERVED_OFFSET + reserved.fim_reserved_offset_icon_end;
117// Environment
118uint64_t const FIM_RESERVED_OFFSET_ENVIRONMENT_BEGIN = FIM_RESERVED_OFFSET + reserved.fim_reserved_offset_environment_begin;
119uint64_t const FIM_RESERVED_OFFSET_ENVIRONMENT_END = FIM_RESERVED_OFFSET + reserved.fim_reserved_offset_environment_end;
120// Bindings
121uint64_t const FIM_RESERVED_OFFSET_BINDINGS_BEGIN = FIM_RESERVED_OFFSET + reserved.fim_reserved_offset_bindings_begin;
122uint64_t const FIM_RESERVED_OFFSET_BINDINGS_END = FIM_RESERVED_OFFSET + reserved.fim_reserved_offset_bindings_end;
123// Remote
124uint64_t const FIM_RESERVED_OFFSET_REMOTE_BEGIN = FIM_RESERVED_OFFSET + reserved.fim_reserved_offset_remote_begin;
125uint64_t const FIM_RESERVED_OFFSET_REMOTE_END = FIM_RESERVED_OFFSET + reserved.fim_reserved_offset_remote_end;
126// Unshare
127uint64_t const FIM_RESERVED_OFFSET_UNSHARE_BEGIN = FIM_RESERVED_OFFSET + reserved.fim_reserved_offset_unshare_begin;
128uint64_t const FIM_RESERVED_OFFSET_UNSHARE_END = FIM_RESERVED_OFFSET + reserved.fim_reserved_offset_unshare_end;
129
130
131
142[[nodiscard]] inline Value<void> write(fs::path const& path_file_binary
143 , uint64_t offset_begin
144 , uint64_t offset_end
145 , const char* data
146 , uint64_t length) noexcept
147{
148 uint64_t size = offset_end - offset_begin;
149 return_if(length > size, Error("E::Size of data exceeds available space"));
150 // Open output binary file
151 std::fstream file_binary(path_file_binary, std::ios::binary | std::ios::in | std::ios::out);
152 return_if(not file_binary.is_open(), Error("E::Failed to open input file"));
153 // Write blank data
154 std::vector<char> blank(size, 0);
155 return_if(not file_binary.seekp(offset_begin), Error("E::Failed to seek offset to blank"));
156 return_if(not file_binary.write(blank.data(), size), Error("E::Failed to write blank data"));
157 // Write data with length
158 return_if(not file_binary.seekp(offset_begin), Error("E::Failed to seek offset to write data"));
159 return_if(not file_binary.write(data, length), Error("E::Failed to write data"));
160 return {};
161}
162
172[[nodiscard]] inline Value<std::streamsize> read(fs::path const& path_file_binary
173 , uint64_t offset
174 , char* data
175 , uint64_t length) noexcept
176{
177 // Open binary file
178 std::ifstream file_binary(path_file_binary, std::ios::binary | std::ios::in);
179 return_if(not file_binary.is_open(), Error("E::Failed to open input file"));
180 // Advance towards data
181 return_if(not file_binary.seekg(offset), Error("E::Failed to seek to file offset for read"));
182 // Read data
183 return_if(not file_binary.read(data, length), Error("E::Failed to read data from binary file"));
184 // Return number of read bytes
185 return file_binary.gcount();
186}
187
188} // namespace ns_reserved
189
190/* vim: set expandtab fdm=marker ts=2 sw=2 tw=100 et :*/
Common utility functions and helpers used throughout FlatImage.
Enhanced error handling framework built on std::expected.
Simplified macros for common control flow patterns with optional logging.
Management of ELF binary reserved space for configuration storage.
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.
Definition reserved.hpp:142
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.
Definition reserved.hpp:172
Enhanced expected type with integrated logging capabilities.
Definition expected.hpp:44
constexpr Reserved()
Validates reserved space layout at compile-time.
Definition reserved.hpp:87