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#include <filesystem>
12#include <algorithm>
13
14#include "../../db/db.hpp"
15#include "../../db/bind.hpp"
17
26namespace ns_cmd::ns_bind
27{
28
29namespace
30{
31
32namespace fs = std::filesystem;
33
40[[nodiscard]] inline uint64_t get_highest_index(ns_db::ns_bind::Binds const& binds)
41{
42 auto it = std::ranges::max_element(binds.get(), {}, [](ns_db::ns_bind::Bind const& bind)
43 {
44 return bind.index;
45 });
46 return (it == std::ranges::end(binds.get()))? -1 : it->index;
47}
48
49} // namespace
50
57inline Value<ns_db::ns_bind::Binds> db_read(fs::path const& path_file_binary)
58{
59 auto reserved_data = Pop(ns_reserved::ns_bind::read(path_file_binary));
61 reserved_data.empty()? "{}" : reserved_data
62 ).value_or(ns_db::ns_bind::Binds{});
63}
64
71inline Value<void> db_write(fs::path const& path_file_binary, ns_db::ns_bind::Binds const& binds)
72{
73 Pop(ns_reserved::ns_bind::write(path_file_binary
74 , Pop(Pop(ns_db::ns_bind::serialize(binds)).dump())
75 ));
76 return {};
77}
78
86[[nodiscard]] inline Value<void> add(fs::path const& path_file_binary
87 , ns_db::ns_bind::Type bind_type
88 , fs::path const& path_src
89 , fs::path const& path_dst
90)
91{
92 // Deserialize bindings
93 ns_db::ns_bind::Binds binds = Pop(db_read(path_file_binary));
94 // Find out the highest bind index
96 {
97 .index = get_highest_index(binds) + 1,
98 .path_src = path_src,
99 .path_dst = path_dst,
100 .type = bind_type,
101 };
102 binds.push_back(bind);
103 logger("I::Binding index is '{}'", bind.index);
104 // Write database
105 Pop(db_write(path_file_binary, binds));
106 return {};
107}
108
116[[nodiscard]] inline Value<void> del(fs::path const& path_file_binary, uint64_t index)
117{
118 std::error_code ec;
119 // Deserialize bindings
120 ns_db::ns_bind::Binds binds = Pop(db_read(path_file_binary));
121 // Erase index if exists
122 binds.erase(index);
123 // Write database
124 Pop(db_write(path_file_binary, binds));
125 return {};
126}
127
133[[nodiscard]] inline Value<void> list(fs::path const& path_file_binary)
134{
135 // Deserialize bindings
136 ns_db::ns_bind::Binds binds = Pop(db_read(path_file_binary));
137 if(not binds.empty())
138 {
139 std::println("{}", Pop(Pop(ns_db::ns_bind::serialize(binds)).dump()));
140 }
141 return {};
142}
143
144} // namespace ns_cmd::ns_bind
145
146/* vim: set expandtab fdm=marker ts=2 sw=2 tw=100 et :*/
Used to manage the bindings from the host to the sandbox.
A database that interfaces with nlohmann json.
#define logger(fmt,...)
Compile-time log level dispatch macro with automatic location capture.
Definition log.hpp:682
uint64_t get_highest_index(ns_db::ns_bind::Binds const &binds)
Get the highest index object.
Definition bind.hpp:40
Bind mount command implementation.
Value< void > list(fs::path const &path_file_binary)
List bindings from the given bindings database.
Definition bind.hpp:133
Value< void > del(fs::path const &path_file_binary, uint64_t index)
Deletes a binding from the database.
Definition bind.hpp:116
Value< void > add(fs::path const &path_file_binary, ns_db::ns_bind::Type bind_type, fs::path const &path_src, fs::path const &path_dst)
Adds a binding instruction to the binding database.
Definition bind.hpp:86
Value< ns_db::ns_bind::Binds > db_read(fs::path const &path_file_binary)
Reads data from the reserved space.
Definition bind.hpp:57
Value< void > db_write(fs::path const &path_file_binary, ns_db::ns_bind::Binds const &binds)
Writes data to the reserved space.
Definition bind.hpp:71
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< void > write(fs::path const &path_file_binary, std::string_view const &json)
Writes the bindings json string to the target binary.
Definition bind.hpp:45
Value< std::string > read(fs::path const &path_file_binary)
Reads the bindings json string from the target binary.
Definition bind.hpp:65
Manages the bindings reserved space.
Enhanced expected type with integrated logging capabilities.
Definition expected.hpp:44
Represents a single bind mount from host to guest.
Definition bind.hpp:43
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
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