FlatImage
A configurable Linux containerization system
Loading...
Searching...
No Matches
bind.hpp
Go to the documentation of this file.
1
8
9#pragma once
10
11
12#include <string>
13
14#include "db.hpp"
15#include "../std/expected.hpp"
16
17#include "../std/enum.hpp"
18
28namespace ns_db::ns_bind
29{
30
31ENUM(Type, RO, RW, DEV);
32
42struct Bind
43{
50 size_t index;
51
58 fs::path path_src;
59
66 fs::path path_dst;
67
75 Type type;
76};
77
87struct Binds
88{
89 private:
96 std::vector<Bind> m_binds;
97 public:
98 Binds() = default;
99 std::vector<Bind> const& get() const;
100 void push_back(Bind const& bind);
101 void erase(size_t index);
102 bool empty() const noexcept;
103 friend Value<Binds> deserialize(std::string_view raw_json);
104};
105
115[[nodiscard]] inline std::vector<Bind> const& Binds::get() const
116{
117 return this->m_binds;
118}
119
130inline void Binds::push_back(Bind const& bind)
131{
132 m_binds.push_back(bind);
133}
134
145inline void Binds::erase(size_t index)
146{
147 if (std::erase_if(m_binds, [&](auto&& bind){ return bind.index == index; }) > 0)
148 {
149 logger("I::Erase element with index '{}'", index);
150 }
151 else
152 {
153 logger("I::No element with index '{}' found", index);
154 }
155 for(long i{}; auto& bind : m_binds) { bind.index = i++; }
156}
157
166inline bool Binds::empty() const noexcept
167{
168 return m_binds.empty();
169}
170
184[[nodiscard]] inline Value<Binds> deserialize(std::string_view raw_json)
185{
186 Binds binds;
187
188 auto db = Pop(ns_db::from_string(raw_json));
189
190 for(auto [key,value] : db.items())
191 {
192 auto type = Pop(value("type").template value<std::string>());
193 if (auto index_result = Catch(std::stoull(key)))
194 {
195 binds.m_binds.push_back(Bind
196 {
197 .index = index_result.value(),
198 .path_src = Pop(value("src").template value<std::string>()),
199 .path_dst = Pop(value("dst").template value<std::string>()),
200 .type = (type == "ro")? Type::RO
201 : (type == "rw")? Type::RW
202 : (type == "rw")? Type::DEV
203 : Type::NONE
204 });
205 }
206 else
207 {
208 logger("W::Failed to parse bind index '{}'", key);
209 continue;
210 }
211 }
212 return binds;
213}
214
226[[nodiscard]] inline Value<Binds> deserialize(std::ifstream& stream_raw_json) noexcept
227{
228 std::stringstream ss;
229 ss << stream_raw_json.rdbuf();
230 return deserialize(ss.str());
231}
232
244[[nodiscard]] inline Value<Db> serialize(Binds const& binds) noexcept
245{
246 ns_db::Db db;
247 for (auto&& bind : binds.get())
248 {
249 db(std::to_string(bind.index))("src") = bind.path_src;
250 db(std::to_string(bind.index))("dst") = bind.path_dst;
251 db(std::to_string(bind.index))("type") = (bind.type == Type::RO)? "ro" : (bind.type == Type::RW)? "rw" : "dev";
252 }
253 return db;
254}
255
256} // namespace ns_db::ns_bind
257
258/* vim: set expandtab fdm=marker ts=2 sw=2 tw=100 et :*/
A type-safe wrapper around nlohmann::json for database operations.
Definition db.hpp:64
A database that interfaces with nlohmann json.
Custom enumeration class.
Enhanced error handling framework built on std::expected.
#define logger(fmt,...)
Compile-time log level dispatch macro with automatic location capture.
Definition log.hpp:682
Bind mount configuration management.
Value< Binds > deserialize(std::string_view raw_json)
Deserializes JSON string into a Binds object.
Definition bind.hpp:184
Value< Db > serialize(Binds const &binds) noexcept
Serializes a Binds object into a JSON database.
Definition bind.hpp:244
Value< Db > from_string(S &&s)
Parses a JSON string and creates a Db object.
Definition db.hpp:496
Enhanced expected type with integrated logging capabilities.
Definition expected.hpp:44
Represents a single bind mount from host to guest.
Definition bind.hpp:43
fs::path path_dst
Destination path inside the sandbox.
Definition bind.hpp:66
size_t index
Sequential index of this bind mount in the collection.
Definition bind.hpp:50
Type type
Access type for this bind mount.
Definition bind.hpp:75
fs::path path_src
Source path on the host system.
Definition bind.hpp:58
Container for multiple bind mount configurations.
Definition bind.hpp:88
void erase(size_t index)
Removes a bind mount by its index.
Definition bind.hpp:145
bool empty() const noexcept
Checks whether the bind mount collection is empty.
Definition bind.hpp:166
friend Value< Binds > deserialize(std::string_view raw_json)
Deserializes JSON string into a Binds object.
Definition bind.hpp:184
std::vector< Bind > const & get() const
Retrieves the current list of bind mounts.
Definition bind.hpp:115
void push_back(Bind const &bind)
Appends a new bind mount to the collection.
Definition bind.hpp:130