FlatImage
A configurable Linux containerization system
Loading...
Searching...
No Matches
unionfs.hpp
Go to the documentation of this file.
1
8
9#pragma once
10
11#include <filesystem>
12#include <unistd.h>
13
14#include "../lib/subprocess.hpp"
15#include "../lib/env.hpp"
16#include "../lib/fuse.hpp"
17#include "../std/filesystem.hpp"
18#include "filesystem.hpp"
19
20namespace
21{
22
23namespace fs = std::filesystem;
24
25} // namespace
26
37{
38
40{
41 private:
42 fs::path m_path_dir_upper;
43 std::vector<fs::path> m_vec_path_dir_layer;
44 public:
45 UnionFs(pid_t pid_to_die_for
46 , fs::path const& path_dir_mount
47 , fs::path const& path_dir_upper
48 , fs::path const& path_file_log
49 , std::vector<fs::path> const& vec_path_dir_layer
50 );
51 Value<void> mount() override;
52};
53
62inline UnionFs::UnionFs(pid_t pid_to_die_for
63 , fs::path const& path_dir_mount
64 , fs::path const& path_dir_upper
65 , fs::path const& path_file_log
66 , std::vector<fs::path> const& vec_path_dir_layer
67 )
68 : ns_filesystem::Filesystem(pid_to_die_for, path_dir_mount, path_file_log)
69 , m_path_dir_upper(path_dir_upper)
70 , m_vec_path_dir_layer(vec_path_dir_layer)
71{
72 this->mount().discard("E::Could not mount unionfs filesystem to '{}'", path_dir_mount);
73}
74
81{
82 // Validate directories
83 Pop(ns_fs::create_directories(m_path_dir_upper), "E::Could not create upper directory");
84 Pop(ns_fs::create_directories(m_path_dir_mount), "E::Could not create lower directory");
85 // Find unionfs
86 auto path_file_unionfs = Pop(ns_env::search_path("unionfs"), "E::Could not find unionfs in PATH");
87 // Create string to represent layers argumnet
88 // First layer is the writteable one
89 // Layers are overlayed as top_branch:lower_branch:...:lowest_branch
90 std::string arg_layers=std::format("{}=RW", m_path_dir_upper.string());
91 for (auto&& path_dir_layer : m_vec_path_dir_layer | std::views::reverse)
92 {
93 arg_layers += std::format(":{}=RO", path_dir_layer.string());
94 } // for
95 // Include arguments and spawn process
96 using enum ns_subprocess::Stream;
97 m_child = ns_subprocess::Subprocess(path_file_unionfs)
98 .with_args("-f")
99 .with_args("-o", "cow")
100 .with_args(arg_layers)
101 .with_args(m_path_dir_mount)
102 .with_die_on_pid(m_pid_to_die_for)
103 .with_stdio(ns_subprocess::Stream::Pipe)
104 .with_log_file(m_path_file_log)
105 .spawn();
106 // Wait for mount
107 ns_fuse::wait_fuse(m_path_dir_mount);
108 return {};
109}
110
111
112} // namespace ns_filesystems::ns_unionfs
113
114/* 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.
UnionFs(pid_t pid_to_die_for, fs::path const &path_dir_mount, fs::path const &path_dir_upper, fs::path const &path_file_log, std::vector< fs::path > const &vec_path_dir_layer)
Construct a new Union Fs:: Union Fs object.
Definition unionfs.hpp:62
Value< void > mount() override
Mounts the filesystem.
Definition unionfs.hpp:80
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 operations on fuse filesystems.
A library for manipulating environment variables.
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.
UnionFS-FUSE overlay filesystem implementation.
Value< fs::path > create_directories(fs::path const &p)
Creates directories recursively.
void wait_fuse(fs::path const &path_dir_filesystem)
Waits for the given directory to not be fuse.
Definition fuse.hpp:68
Stream
Stream redirection modes for child process stdio.
Filesystem helpers.
Enhanced expected type with integrated logging capabilities.
Definition expected.hpp:44
A library to spawn sub-processes in linux.