7 #include "snapshot.hpp"
18 typedef std::shared_ptr<PhysicalFile> physical_file_t;
36 physical_file_t physical_file;
49 unsigned int read_buffer(std::vector<Expr>& buffer,
unsigned int nb_elems,
unsigned int elem_size);
68 std::list<std::pair<std::shared_ptr<PhysicalFile>,
SavedMemState>> saved_file_contents;
70 std::list<std::pair<std::string, FileSystemAction>> fs_actions;
71 std::list<env::FileAccessor> file_accessors;
77 void add_saved_file_content(std::shared_ptr<PhysicalFile> file,
SavedMemState&& content);
83 typedef unsigned node_status_t;
86 static constexpr node_status_t none = 0;
88 static constexpr node_status_t exists = 1 << 0;
89 static constexpr node_status_t is_file = 1 << 1;
90 static constexpr node_status_t is_symlink = 1 << 2;
91 static constexpr node_status_t is_dir = 1 << 3;
93 bool check_is_file(node_status_t s);
94 bool check_is_symlink(node_status_t s);
95 bool check_is_dir(node_status_t s);
115 std::shared_ptr<MemSegment> data;
122 addr_t istream_read_offset;
137 unsigned int read_buffer(std::vector<Expr>& buffer,
addr_t& offset,
unsigned int nb_elems,
unsigned int elem_size);
157 void _adjust_read_offset(
addr_t& offset);
158 void _adjust_write_offset(
addr_t& offset);
162 void record_write(
addr_t offset,
int nb_bytes);
165 void _set_symlink(
const std::string& target);
171 typedef std::shared_ptr<Directory> directory_t;
179 std::map<std::string, physical_file_t> files;
180 std::map<std::string, directory_t> subdirs;
181 std::map<std::string, fspath_t> symlinks;
186 bool _contains_name(
const std::string& name);
207 void print(std::ostream& os,
const std::string& indent)
const;
219 std::string path_separator;
220 std::string rootdir_prefix;
221 char orphan_file_wildcard;
227 std::list<FileAccessor> fa_list;
228 std::vector<filehandle_t> reserved_handles;
242 physical_file_t
get_file(
const std::string& path,
bool follow_symlink=
true);
250 bool create_file(
const std::string& path,
bool create_path=
false);
267 bool create_symlink(
const std::string& link,
const std::string& pointed_file,
bool create_path=
false);
297 std::string path_from_fspath(
const fspath_t& path);
298 fspath_t fspath_from_path(
const std::string& path);
299 fspath_t fspath_from_path_relative_to(std::string rel_path,
fspath_t path_base);
300 std::string path_from_relative_path(std::string rel_path, std::string path_base);
301 std::string pointed_path_from_symlink(std::string symlink_file);
302 bool is_relative_path(
const std::string& path);
304 node_status_t get_node_status(
const std::string& path);
321 void _new_fa(
const std::string& path,
filehandle_t handle);
324 using snapshot_t = int;
unsigned int read_buffer(std::vector< Expr > &buffer, unsigned int nb_elems, unsigned int elem_size)
Read abstract buffer from the file. Return the number of bytes read.
unsigned int write_buffer(uint8_t *buffer, addr_t &offset, int len)
Write concrete buffer to the file. Return the number of bytes written.
Definition: filesystem.hpp:103
Main class emulating a process environment.
Definition: env.hpp:31
Definition: filesystem.hpp:66
FileSystemAction
Actions modifying the filesystem layout.
Definition: filesystem.hpp:57
bool is_deleted()
Return 'True' if the file has been deleted by the emulated program.
Directory(SnapshotManager< env::Snapshot > *snapshots)
directory_t get_dir(fspath_t path)
Get directory object. Throws exception if the directory doesn't exist.
uint64_t addr_t
Concrete memory address.
Definition: types.hpp:21
FileAccessor(physical_file_t physical_file, filehandle_t handle)
If file accessor was deleted by the emulated program.
void print(std::ostream &os, const std::string &indent) const
Print directory to output stream.
unsigned int write_buffer(uint8_t *buffer, int len)
Write concrete buffer to the file. Return the number of bytes written.
Struct used by snapshots to record previous contents of an overwritten memory area.
Definition: snapshot.hpp:25
@ REGULAR
Regular file supporting arbitrary read write.
bool create_dir(const std::string &path)
Create a directory in the filesystem. Returns 'true' on success, 'false' on failure.
directory_t get_dir(const std::string &path)
Get directory by absolute path. This function doesn't check wheter the dir was deleted or not!
int filehandle_t
Opaque handle to a file (equivalent of file descriptors on Linux)
Definition: filesystem.hpp:21
Definition: filesystem.hpp:174
std::string get_stdout_for_pid(int pid)
Return the stdout file for process with PID 'pid'.
std::optional< std::reference_wrapper< std::ostream > > flush_stream
If this field is set, flush every write to the file in the stream as well. This is mostly used for st...
Definition: filesystem.hpp:128
bool file_exists(const std::string &path)
Check if a file exists in the filesystem.
Specialisation of 'EnvEmulator' for the Linux operating system.
Definition: env.hpp:68
unsigned int write_buffer(const std::vector< Expr > &buffer)
Write abstract buffer to the file. Return the number of bytes written.
Wrapper class to manage a list of snapshots.
Definition: snapshot.hpp:81
bool delete_file(fspath_t path, bool weak=true)
Delete physical file.
physical_file_t get_file(fspath_t path)
Get physical file object. Throws exception if the file doesn't exist.
Definition: filesystem.hpp:25
OS
Emulated operating system.
Definition: os.hpp:12
PhysicalFile(SnapshotManager< env::Snapshot > *snapshots, Type type=Type::REGULAR)
Create a new physical file.
bool delete_file(const std::string &path, bool weak=true)
Delete a file Returns 'true' on success and 'false' on failure.
unsigned int read_buffer(std::vector< Expr > &buffer, addr_t &offset, unsigned int nb_elems, unsigned int elem_size)
Read abstract buffer from file. Return the number of elements read.
Type
Types of files.
Definition: filesystem.hpp:108
void delete_fa(filehandle_t handle, bool weak=true)
Delete a file accessor.
std::string get_stdin_for_pid(int pid)
Return the stdin file for process with PID 'pid'.
unsigned int copy_real_file(const std::string &filename)
Fill the emulated file with concrete content from a real file. Return the size of 'filename'.
std::vector< std::string > fspath_t
Absolute path to a file or directory node in the virtual file system.
Definition: filesystem.hpp:99
std::ostream & operator<<(std::ostream &os, const Constraint &constr)
Print a constraint to an out stream.
unsigned int size()
Return the total size of the physical file content in bytes.
@ CREATE_FILE
Creating a new physical file.
node_status_t status()
Return the file status.
physical_file_t get_file_by_handle(filehandle_t handle)
Convenience function to get a physical file by handle.
Main namespace for Maat's API.
Definition: arch.hpp:11
std::string get_stderr_for_pid(int pid)
Return the stderr file for process with PID 'pid'.
physical_file_t get_file(const std::string &path, bool follow_symlink=true)
Get a file in the file system.
void delete_self(bool recursive=true, bool weak=true)
Delete the directory.
FileAccessor & get_fa_by_handle(filehandle_t handle)
Get a file accessor by handle.
bool is_symlink()
Return 'True' if the file is a symbolic link.
FileSystem(OS system)
Create a new filesystem for OS 'system'.
unsigned int write_buffer(const std::vector< Expr > &buffer, addr_t &offset)
Write abstract buffer to the file. Return the number of bytes written.
snapshot_t take_snapshot()
Take a snapshot of the filesystem.
node_status_t get_node_status(fspath_t path)
Get status of node.
addr_t _size
Size in bytes.
Definition: filesystem.hpp:118
std::string _symlink
Path if this file is a symlink.
Definition: filesystem.hpp:119
bool create_dir(fspath_t path)
Create new sub-directory.
Definition: filesystem.hpp:213
bool create_file(fspath_t path, bool create_path=false)
Create new file.
const std::string & symlink()
If symlink, returns the file it points to.
void set_deleted(bool deleted)
Set the deleted status of the file.
bool delete_dir(fspath_t path, bool weak=true)
Delete sub directory.
bool create_symlink(const std::string &link, const std::string &pointed_file, bool create_path=false)
Create a symbolic link.
@ IOSTREAM
Stream (reads consume data from the beginning, writes append data at the end)
Definition: filesystem.hpp:29
void restore_snapshot(snapshot_t snapshot, bool remove=false)
Restore a snapshot of the filesystem.
@ SYMLINK
Symbolic link to another file.
filehandle_t new_fa(const std::string &path)
Create a new file accessor.
addr_t write_ptr
Current read offset in file.
Definition: filesystem.hpp:31
void restore_last_snapshot(bool remove=false)
Restore latests snapshot.
bool delete_dir(const std::string &path, bool weak=true)
Delete a directory.
bool create_file(const std::string &path, bool create_path=false)
Create a file specified by its absolute path. Returns 'true' on success and 'false' on failure.
bool deleted
'True' if the file was deleted by the emulated program
Definition: filesystem.hpp:117