FlatImage
A configurable Linux containerization system
Loading...
Searching...
No Matches
filesystem.hpp
Go to the documentation of this file.
1
8
9#pragma once
10
11#include <cstring>
12#include <expected>
13#include <vector>
14#include <ranges>
15#include <string>
16#include <filesystem>
17#include <linux/limits.h>
18
19#include "expected.hpp"
20#include "string.hpp"
21
29namespace ns_fs
30{
31
32namespace fs = std::filesystem;
33
34using path = fs::path;
35using perms = fs::perms;
36using copy_options = fs::copy_options;
37using perm_options = fs::perm_options;
38
45[[nodiscard]] inline std::expected<fs::path, std::string> realpath(fs::path const& path_file_src)
46{
47 char str_path_file_resolved[PATH_MAX];
48 if ( ::realpath(path_file_src.c_str(), str_path_file_resolved) == nullptr )
49 {
50 return std::unexpected<std::string>(strerror(errno));
51 }
52 return fs::path{str_path_file_resolved};
53}
54
61[[nodiscard]] inline Value<std::vector<fs::path>> regular_files(fs::path const& path_dir_src)
62{
63 std::vector<fs::path> files = Try(fs::directory_iterator(path_dir_src))
64 | std::views::transform([](auto&& e){ return e.path(); })
65 | std::views::filter([](auto&& e){ return fs::is_regular_file(e); })
66 | std::ranges::to<std::vector<fs::path>>();
67 return files;
68}
69
76[[nodiscard]] inline Value<fs::path> create_directories(fs::path const& p)
77{
78 if(Try(fs::exists(p)))
79 {
80 return p;
81 }
82 if (not Try(fs::create_directories(p)))
83 {
84 return std::unexpected(std::format("Could not create directory {}", p.string()));
85 }
86 return p;
87}
88
100template<typename... Args>
101[[nodiscard]] inline fs::path placeholders_replace(fs::path const& path, Args&&... args)
102{
103 return ns_string::placeholders_replace(path, std::forward<Args>(args)...);
104}
105
106} // namespace: ns_fs
Enhanced error handling framework built on std::expected.
Enhanced filesystem utilities wrapping std::filesystem.
Value< std::vector< fs::path > > regular_files(fs::path const &path_dir_src)
List the files in a directory.
fs::path placeholders_replace(fs::path const &path, Args &&... args)
Replace placeholders in a path by traversing components.
std::expected< fs::path, std::string > realpath(fs::path const &path_file_src)
Resolves an input path using realpath(3)
Value< fs::path > create_directories(fs::path const &p)
Creates directories recursively.
std::string placeholders_replace(std::string str, Args &&... args)
Replace all occurrences of "{}" placeholders in a string with provided values.
Definition string.hpp:178
String helpers.
Enhanced expected type with integrated logging capabilities.
Definition expected.hpp:44