FlatImage
A configurable Linux containerization system
Loading...
Searching...
No Matches
overlay.hpp
Go to the documentation of this file.
1
8
9#pragma once
10
11#include <cstdint>
12#include <filesystem>
13
14#include "../std/expected.hpp"
15#include "../macro.hpp"
16#include "../std/enum.hpp"
17#include "reserved.hpp"
18
30{
31
32namespace
33{
34
35namespace fs = std::filesystem;
36
37}
38
39ENUM(OverlayType, BWRAP, OVERLAYFS, UNIONFS)
40
41
48inline Value<void> write(fs::path const& path_file_binary, OverlayType const& overlay)
49{
50 uint8_t mask{};
51 switch(overlay)
52 {
53 case OverlayType::BWRAP: mask = 1 << 1; break;
54 case OverlayType::OVERLAYFS: mask = 1 << 2; break;
55 case OverlayType::UNIONFS: mask = 1 << 3; break;
56 case OverlayType::NONE:
57 default: return Error("E::Invalid overlay option");
58 }
59 uint64_t offset_begin = ns_reserved::FIM_RESERVED_OFFSET_OVERLAY_BEGIN;
60 uint64_t offset_end = ns_reserved::FIM_RESERVED_OFFSET_OVERLAY_END;
61 uint64_t size = offset_end - offset_begin;
62 return_if(size != sizeof(uint8_t), Error("E::Incorrect number of bytes to write overlay mask: {} vs {}", size, sizeof(uint8_t)));
63 return ns_reserved::write(path_file_binary, offset_begin, offset_end, reinterpret_cast<char*>(&mask), sizeof(uint8_t));
64}
65
72inline Value<OverlayType> read(fs::path const& path_file_binary)
73{
74 uint64_t offset_begin = ns_reserved::FIM_RESERVED_OFFSET_OVERLAY_BEGIN;
75 uint8_t mask;
76 ssize_t bytes = Pop(ns_reserved::read(path_file_binary, offset_begin, reinterpret_cast<char*>(&mask), sizeof(uint8_t)));
77 log_if(bytes != 1, "E::Possible error to read overlay byte, count is {}", bytes);
78 switch(mask)
79 {
80 case 1 << 1: return OverlayType::BWRAP;
81 case 1 << 2: return OverlayType::OVERLAYFS;
82 case 1 << 3: return OverlayType::UNIONFS;
83 default: return std::unexpected("Invalid overlay option");
84 }
85}
86
87} // namespace ns_reserved::ns_overlay
88
89/* vim: set expandtab fdm=marker ts=2 sw=2 tw=100 et :*/
Custom enumeration class.
Enhanced error handling framework built on std::expected.
Simplified macros for common control flow patterns with optional logging.
Overlay filesystem type selection in reserved space.
Value< void > write(fs::path const &path_file_binary, OverlayType const &overlay)
Writes a overlay mask to the flatimage binary.
Definition overlay.hpp:48
Value< OverlayType > read(fs::path const &path_file_binary)
Read the overlay mask from the flatimage binary.
Definition overlay.hpp:72
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
Manages reserved space.
Enhanced expected type with integrated logging capabilities.
Definition expected.hpp:44