plugify 1.2.8
Loading...
Searching...
No Matches
file_system.hpp
1#pragma once
2
3#include <filesystem>
4#include <fstream>
5#include <functional>
6#include <regex>
7#include <span>
8#include <string>
9#include <vector>
10
11#include "plugify/types.hpp"
12
13namespace plugify {
17 struct FileInfo {
18 std::filesystem::path path;
19 std::uintmax_t size;
20 std::filesystem::file_time_type last_write_time;
21 std::filesystem::file_type type;
22 std::filesystem::perms permissions;
23
24 bool is_directory() const {
25 return type == std::filesystem::file_type::directory;
26 }
27
28 bool is_regular_file() const {
29 return type == std::filesystem::file_type::regular;
30 }
31
32 bool is_symlink() const {
33 return type == std::filesystem::file_type::symlink;
34 }
35 };
36
41 bool recursive = false;
42 bool follow_symlinks = false;
43 bool skip_permission_denied = true;
44 std::optional<std::regex> filter_pattern;
45 std::function<bool(const FileInfo&)> filter_predicate;
46 };
47
52 public:
53 virtual ~IFileSystem() = default;
54
55 // File Operations
59 virtual Result<std::string> ReadTextFile(const std::filesystem::path& path) = 0;
60
64 virtual Result<std::vector<uint8_t>> ReadBinaryFile(const std::filesystem::path& path) = 0;
65
69 virtual Result<void>
70 WriteTextFile(const std::filesystem::path& path, std::string_view content) = 0;
71
75 virtual Result<void>
76 WriteBinaryFile(const std::filesystem::path& path, std::span<const uint8_t> data) = 0;
77
78 // Directory Operations
82 virtual bool IsExists(const std::filesystem::path& path) = 0;
83
87 virtual bool IsDirectory(const std::filesystem::path& path) = 0;
88
92 virtual bool IsRegularFile(const std::filesystem::path& path) = 0;
93
97 virtual Result<FileInfo> GetFileInfo(const std::filesystem::path& path) = 0;
98
103 ListDirectory(const std::filesystem::path& directory) = 0;
104
109 const std::filesystem::path& directory,
110 const DirectoryIterationOptions& options = {}
111 ) = 0;
112
117 const std::filesystem::path& directory,
118 std::span<const std::string_view> patterns, // e.g., {"*.json", "manifest.*"}
119 bool recursive = true
120 ) = 0;
121
122 // Path Operations
126 virtual Result<void> CreateDirectories(const std::filesystem::path& path) = 0;
127
131 virtual Result<void> Remove(const std::filesystem::path& path) = 0;
132
136 virtual Result<void> RemoveAll(const std::filesystem::path& path) = 0;
137
141 virtual Result<void>
142 Copy(const std::filesystem::path& from, const std::filesystem::path& to) = 0;
143
147 virtual Result<void>
148 Move(const std::filesystem::path& from, const std::filesystem::path& to) = 0;
149
153 virtual Result<std::filesystem::path> GetAbsolutePath(const std::filesystem::path& path) = 0;
154
158 virtual Result<std::filesystem::path> GetCanonicalPath(const std::filesystem::path& path) = 0;
159
164 GetRelativePath(const std::filesystem::path& path, const std::filesystem::path& base) = 0;
165 };
166} // namespace plugify
Simple filesystem interface for reading files and iterating directories.
virtual bool IsDirectory(const std::filesystem::path &path)=0
Check if path is a directory.
virtual Result< std::vector< std::filesystem::path > > FindFiles(const std::filesystem::path &directory, std::span< const std::string_view > patterns, bool recursive=true)=0
Find files matching pattern.
virtual Result< void > WriteTextFile(const std::filesystem::path &path, std::string_view content)=0
Write text to file.
virtual Result< void > Remove(const std::filesystem::path &path)=0
Remove file or empty directory.
virtual bool IsRegularFile(const std::filesystem::path &path)=0
Check if path is a regular file.
virtual Result< void > WriteBinaryFile(const std::filesystem::path &path, std::span< const uint8_t > data)=0
Write binary data to file.
virtual Result< FileInfo > GetFileInfo(const std::filesystem::path &path)=0
Get file information.
virtual Result< std::string > ReadTextFile(const std::filesystem::path &path)=0
Read entire file as text.
virtual Result< std::vector< FileInfo > > ListDirectory(const std::filesystem::path &directory)=0
List files in directory (non-recursive)
virtual Result< std::vector< FileInfo > > IterateDirectory(const std::filesystem::path &directory, const DirectoryIterationOptions &options={})=0
Iterate over files in directory with options.
virtual Result< void > Move(const std::filesystem::path &from, const std::filesystem::path &to)=0
Move/rename file or directory.
virtual Result< std::vector< uint8_t > > ReadBinaryFile(const std::filesystem::path &path)=0
Read entire file as binary.
virtual bool IsExists(const std::filesystem::path &path)=0
Check if path exists.
virtual Result< void > CreateDirectories(const std::filesystem::path &path)=0
Create directory (including parents)
virtual Result< void > RemoveAll(const std::filesystem::path &path)=0
Remove directory and all contents.
virtual Result< void > Copy(const std::filesystem::path &from, const std::filesystem::path &to)=0
Copy file or directory.
Directory iteration options.
File information.