Arion 1.0.2-alpha
A high-performance C++ framework for emulating executable binaries.
 
Loading...
Searching...
No Matches
file_system_manager.hpp
Go to the documentation of this file.
1#ifndef ARION_FS_MANAGER_HPP
2#define ARION_FS_MANAGER_HPP
3
5#include <map>
6#include <memory>
7#include <regex>
8#include <string>
9#include <vector>
10
11namespace arion
12{
13
14class Arion;
15
18{
20 int fd;
22 std::string path;
24 int flags;
26 mode_t mode;
28 bool blocking = true;
30 off_t saved_off = 0;
31 /*
32 * Builder for ARION_FILE instances.
33 */
35 /*
36 * Builder for ARION_FILE instances.
37 * @param[in] fd The UNIX file descriptor.
38 * @param[in] path Path to the file.
39 * @param[in] flags Flags which were used to open the file (like in "open" syscall).
40 * @param[in] mode Mode which was used to open the file (like in "open" syscall).
41 */
42 ARION_FILE(int fd, std::string path, int flags, mode_t mode) : fd(fd), path(path), flags(flags), mode(mode) {};
43 /*
44 * Builder for ARION_FILE instances. Used to clone an ARION_FILE instance.
45 * @param[in] arion_f The ARION_FILE to be cloned.
46 */
47 ARION_FILE(std::shared_ptr<ARION_FILE> arion_f)
48 : fd(arion_f->fd), path(arion_f->path), flags(arion_f->flags), mode(arion_f->mode), blocking(arion_f->blocking),
49 saved_off(arion_f->saved_off) {};
50 /*
51 * Builder for ARION_FILE instances. Used to clone an ARION_FILE instance.
52 * @param[in] arion_f The ARION_FILE to be cloned.
53 */
55 : fd(arion_f->fd), path(arion_f->path), flags(arion_f->flags), mode(arion_f->mode), blocking(arion_f->blocking),
56 saved_off(arion_f->saved_off) {};
57};
58/*
59 * Serializes an ARION_FILE instance into a vector of bytes.
60 * @param[in] arion_f The ARION_FILE to be serialized.
61 * @return The serialized vector of bytes.
62 */
63std::vector<BYTE> serialize_arion_file(ARION_FILE *arion_f);
64/*
65 * Deserializes an ARION_FILE instance from a vector of bytes.
66 * @param[in] srz_file The serialized vector of bytes.
67 * @return The deserialized ARION_FILE.
68 */
69ARION_FILE *deserialize_arion_file(std::vector<BYTE> srz_file);
70
73{
74 private:
76 static const inline std::regex PROCFS_REGEX = std::regex(R"(^/proc/([0-9]+|self)/([^/]+)$)");
78 std::weak_ptr<Arion> arion;
79
80 public:
81 /*
82 * Instanciates and initializes new ProcFSManager objects with some parameters.
83 * @param[in] arion The Arion instance used for emulation.
84 * @return A new ProcFSManager instance.
85 */
86 static std::unique_ptr<ProcFSManager> initialize(std::weak_ptr<Arion> arion);
91 ProcFSManager(std::weak_ptr<Arion> arion) : arion(arion) {};
97 bool is_procfs_path(std::string path);
103 std::string convert(std::string path);
104};
105
108{
109 private:
111 std::weak_ptr<Arion> arion;
113 std::string fs_path;
115 std::string cwd_path;
117 std::unique_ptr<ProcFSManager> procfs;
123 bool can_access_file(std::string path);
124
125 public:
127 std::map<int, std::shared_ptr<ARION_FILE>> files;
135 static std::unique_ptr<FileSystemManager> initialize(std::weak_ptr<Arion> arion, std::string fs_path,
136 std::string cwd_path);
142 static std::string find_fd_path(int fd);
149 FileSystemManager(std::weak_ptr<Arion> arion, std::string fs_path, std::string cwd_path);
164 void ARION_EXPORT set_cwd_path(std::string cwd_path);
171 void ARION_EXPORT add_file_entry(int target_fd, std::shared_ptr<ARION_FILE> file, bool safe = true);
177 bool ARION_EXPORT has_file_entry(int target_fd);
182 void ARION_EXPORT rm_file_entry(int target_fd);
188 std::shared_ptr<ARION_FILE> ARION_EXPORT get_arion_file(int target_fd);
194 bool ARION_EXPORT is_in_fs(std::string path);
201 std::string ARION_EXPORT to_fs_path(std::string path);
202};
203
204}; // namespace arion
205
206#endif // ARION_FS_MANAGER_HPP
This class is used to emulate interactions with the rootfs specified by user.
Definition file_system_manager.hpp:108
std::map< int, std::shared_ptr< ARION_FILE > > files
A map identifying an open file given its UNIX file descriptor.
Definition file_system_manager.hpp:127
bool is_in_fs(std::string path)
std::string cwd_path
Path to the user specifed current working directory.
Definition file_system_manager.hpp:115
bool has_file_entry(int target_fd)
std::weak_ptr< Arion > arion
The Arion instance used for emulation.
Definition file_system_manager.hpp:111
static std::string find_fd_path(int fd)
std::string fs_path
Path to the user specified rootfs.
Definition file_system_manager.hpp:113
std::unique_ptr< ProcFSManager > procfs
Used to emulate the behavior of the procfs filesystem.
Definition file_system_manager.hpp:117
static std::unique_ptr< FileSystemManager > initialize(std::weak_ptr< Arion > arion, std::string fs_path, std::string cwd_path)
void set_cwd_path(std::string cwd_path)
std::string get_cwd_path()
void add_file_entry(int target_fd, std::shared_ptr< ARION_FILE > file, bool safe=true)
std::string to_fs_path(std::string path)
bool can_access_file(std::string path)
std::shared_ptr< ARION_FILE > get_arion_file(int target_fd)
FileSystemManager(std::weak_ptr< Arion > arion, std::string fs_path, std::string cwd_path)
std::string get_fs_path()
void rm_file_entry(int target_fd)
This class is used to emulate the behavior of the procfs filesystem.
Definition file_system_manager.hpp:73
ProcFSManager(std::weak_ptr< Arion > arion)
Definition file_system_manager.hpp:91
std::string convert(std::string path)
std::weak_ptr< Arion > arion
The Arion instance used for emulation.
Definition file_system_manager.hpp:78
static const std::regex PROCFS_REGEX
This regex is used to check whether a file path is located inside the system procfs.
Definition file_system_manager.hpp:76
bool is_procfs_path(std::string path)
static std::unique_ptr< ProcFSManager > initialize(std::weak_ptr< Arion > arion)
#define ARION_EXPORT
Defines which symbols should be exported from the library.
Definition global_defs.hpp:13
Definition arch_x86-64.hpp:11
std::vector< BYTE > serialize_arion_file(ARION_FILE *arion_f)
ARION_FILE * deserialize_arion_file(std::vector< BYTE > srz_file)
Stores attributes related to a file which was opened during emulation.
Definition file_system_manager.hpp:18
int flags
Flags which were used to open the file (like in "open" syscall).
Definition file_system_manager.hpp:24
ARION_FILE(int fd, std::string path, int flags, mode_t mode)
Definition file_system_manager.hpp:42
ARION_FILE(std::shared_ptr< ARION_FILE > arion_f)
Definition file_system_manager.hpp:47
ARION_FILE(ARION_FILE *arion_f)
Definition file_system_manager.hpp:54
mode_t mode
Mode which was used to open the file (like in "open" syscall).
Definition file_system_manager.hpp:26
std::string path
Path to the file.
Definition file_system_manager.hpp:22
int fd
The UNIX file descriptor.
Definition file_system_manager.hpp:20
ARION_FILE()
Definition file_system_manager.hpp:34