FlatImage
A configurable Linux containerization system
Loading...
Searching...
No Matches
dwarfs.hpp
Go to the documentation of this file.
1
8
9#pragma once
10
11#include <filesystem>
12
13#include "../lib/subprocess.hpp"
14#include "../lib/env.hpp"
15#include "../macro.hpp"
16#include "filesystem.hpp"
17
28{
29
30namespace
31{
32
33namespace fs = std::filesystem;
34
35};
36
38{
39 private:
40 fs::path m_path_file_image;
41 uint64_t m_offset;
42 uint64_t m_size_image;
43 public:
44 Dwarfs(pid_t pid_to_die_for
45 , fs::path const& path_dir_mount
46 , fs::path const& path_file_image
47 , fs::path const& path_file_log
48 , uint64_t offset
49 , uint64_t size_image);
50 Value<void> mount() override;
51};
52
62inline Dwarfs::Dwarfs(pid_t pid_to_die_for
63 , fs::path const& path_dir_mount
64 , fs::path const& path_file_image
65 , fs::path const& path_file_log
66 , uint64_t offset
67 , uint64_t size_image
68)
69 : ns_filesystem::Filesystem(pid_to_die_for, path_dir_mount, path_file_log)
70 , m_path_file_image(path_file_image)
71 , m_offset(offset)
72 , m_size_image(size_image)
73{
74 this->mount().discard("E::Could not mount dwarfs filesystem '{}' to '{}'", path_file_image, path_dir_mount);
75}
76
83{
84 // Check if image exists and is a regular file
85 return_if(not Try(fs::is_regular_file(m_path_file_image))
86 , Error("E::'{}' does not exist or is not a regular file", m_path_file_image.string())
87 );
88 // Check if mountpoint exists and is directory
89 return_if(not Try(fs::is_directory(m_path_dir_mount))
90 , Error("E::'{}' does not exist or is not a directory", m_path_dir_mount.string())
91 );
92 // Find command in PATH
93 auto path_file_dwarfs = Pop(ns_env::search_path("dwarfs"), "E::Could not find dwarfs in PATH");
94 // Spawn command
95 m_child = ns_subprocess::Subprocess(path_file_dwarfs)
96 .with_args(m_path_file_image, m_path_dir_mount)
97 .with_args("-f", "-o", std::format("uid={},gid={},auto_unmount,offset={},imagesize={}", getuid(), getgid(), m_offset, m_size_image))
98 .with_die_on_pid(m_pid_to_die_for)
99 .with_stdio(ns_subprocess::Stream::Pipe)
100 .with_log_file(m_path_file_log)
101 .spawn();
102 // Wait for mount
103 ns_fuse::wait_fuse(m_path_dir_mount);
104 return {};
105}
106
114inline bool is_dwarfs(fs::path const& path_file_dwarfs, uint64_t offset = 0)
115{
116 // Open file
117 std::ifstream file_dwarfs(path_file_dwarfs, std::ios::binary | std::ios::in);
118 return_if(not file_dwarfs.is_open(), false, "E::Could not open file '{}'", path_file_dwarfs.string());
119 // Adjust offset
120 file_dwarfs.seekg(offset);
121 return_if(not file_dwarfs, false, "E::Failed to seek offset '{}' in file '{}'", offset, path_file_dwarfs.string());
122 // Read initial 'DWARFS' identifier
123 std::array<char,6> header;
124 return_if(not file_dwarfs.read(header.data(), header.size()), false, "E::Could not read bytes from file '{}'", path_file_dwarfs.string());
125 // Check for a successful read
126 return_if(file_dwarfs.gcount() != header.size(), false, "E::Short read for file '{}'", path_file_dwarfs.string());
127 // Check for match
128 return std::ranges::equal(header, std::string_view("DWARFS"));
129}
130
131} // namespace ns_filesystems::ns_dwarfs
132
133/* vim: set expandtab fdm=marker ts=2 sw=2 tw=100 et :*/
Filesystem(pid_t pid_to_die_for, std::filesystem::path const &path_dir_mount, std::filesystem::path const &path_file_log)
Construct a new Filesystem object.
Value< void > mount() override
Mounts the filesystem.
Definition dwarfs.hpp:82
Dwarfs(pid_t pid_to_die_for, fs::path const &path_dir_mount, fs::path const &path_file_image, fs::path const &path_file_log, uint64_t offset, uint64_t size_image)
Construct a new Dwarfs:: Dwarfs object.
Definition dwarfs.hpp:62
Subprocess & with_die_on_pid(pid_t pid)
Configures the child process to die when the specified PID dies.
std::unique_ptr< Child > spawn()
Spawns (forks) the child process and begins execution.
Subprocess & with_stdio(Stream mode)
Sets the stdio redirection mode for the child process.
Subprocess & with_log_file(std::filesystem::path const &path)
Configures logging output for child process stdout/stderr.
Subprocess & with_args(Args &&... args)
Arguments forwarded as the process' arguments.
The base class for filesystems.
A library for manipulating environment variables.
Simplified macros for common control flow patterns with optional logging.
Value< fs::path > search_path(std::string const &query)
Search the directories in the PATH variable for the given input file name.
Definition env.hpp:150
Base filesystem interface and abstraction.
DwarFS compressed read-only filesystem wrapper.
bool is_dwarfs(fs::path const &path_file_dwarfs, uint64_t offset=0)
Checks if the filesystem is a Dwarfs filesystem with a given offset.
Definition dwarfs.hpp:114
void wait_fuse(fs::path const &path_dir_filesystem)
Waits for the given directory to not be fuse.
Definition fuse.hpp:68
Enhanced expected type with integrated logging capabilities.
Definition expected.hpp:44
A library to spawn sub-processes in linux.