2#ifndef SAFEHERON_SGX_TRUSTED_FILESTREAM_H
3#define SAFEHERON_SGX_TRUSTED_FILESTREAM_H
7#include "sgx_tprotected_fs.h"
14namespace filesystem_t {
101#if defined(_LIBCPP_WIN32API)
102#error "We do not support Windows."
103 typedef wchar_t value_type;
104 static constexpr value_type preferred_separator = L
'\\';
106 typedef char value_type;
107 static constexpr value_type preferred_separator =
'/';
109 typedef std::basic_string<value_type> string_type;
133 Path(string_type&& source) : pn_(std::move(source)) {
155 pn_ = std::move(p.pn_);
164 pn_ = std::move(source);
173 pn_ = std::move(source);
213 pn_ += preferred_separator;
225 return lhs.String() == rhs.String();
235 return lhs.String() != rhs.String();
260 return !pn_.empty() && pn_.front() == preferred_separator;
268 return !pn_.empty() && pn_.back() != preferred_separator;
283 const char*
c_str() const noexcept {
550 const char*
what() const noexcept
override {
551 return err_msg_.c_str();
555 std::string err_msg_;
585 std::string file_path_;
615 std::string file_path_;
624 const char* file_name_;
648 size_t Read(
void* buffer,
size_t size)
const;
663 void Seek(int64_t offset,
int origin)
const;
679 const char* file_name_;
705 void Write(
const void* data,
size_t size)
const;
A class to store file type and permissions.
Definition ssgx_filesystem_t.h:18
void Type(FileType ft) noexcept
Modify the file type in FileStatus.
Definition ssgx_filesystem_t.h:80
FileStatus & operator=(FileStatus &&) noexcept=default
A move assignment operator.
void Permissions(Perms p) noexcept
Modify the permissions in FileStatus.
Definition ssgx_filesystem_t.h:88
FileStatus(FileStatus &&) noexcept=default
A move constructor.
FileStatus & operator=(const FileStatus &) noexcept=default
A copy assignment operator.
FileType Type() const noexcept
Return the file type of file.
Definition ssgx_filesystem_t.h:64
Perms Permissions() const noexcept
Return the permissions of file.
Definition ssgx_filesystem_t.h:72
FileStatus(const FileStatus &) noexcept=default
A copy constructor.
FileStatus(FileType ft, Perms prms=Perms::Unknown) noexcept
Construct a FileStatus and initialize it.
Definition ssgx_filesystem_t.h:29
FileStatus() noexcept
Construct a FileStatus and initialize it.
Definition ssgx_filesystem_t.h:23
Exception about filesystem.
Definition ssgx_filesystem_t.h:538
FileSystemException(std::string err_msg)
Construct a FileSystemException and initialized it.
Definition ssgx_filesystem_t.h:543
const char * what() const noexcept override
Output exception message.
Definition ssgx_filesystem_t.h:550
A class for storing path.
Definition ssgx_filesystem_t.h:100
Path(string_type &&source)
A move constructor.
Definition ssgx_filesystem_t.h:133
friend Path operator/(const Path &lhs, const Path &rhs)
Appends operator.
Definition ssgx_filesystem_t.h:249
std::string String() const
Return the string of Path.
Definition ssgx_filesystem_t.h:291
Path & operator=(string_type &&source) noexcept
A move assignment operator.
Definition ssgx_filesystem_t.h:163
Path & operator/=(const Path &p)
Appends operator.
Definition ssgx_filesystem_t.h:207
Path & assign(string_type &&source) noexcept
A move assignment operator.
Definition ssgx_filesystem_t.h:172
Path & operator=(const Path &p)
A copy assignment operator.
Definition ssgx_filesystem_t.h:145
Path & operator+=(const string_type &x)
Appends operator.
Definition ssgx_filesystem_t.h:192
~Path()=default
Destruction.
bool IsAbsolute() const noexcept
Check if the path is absolute.
Definition ssgx_filesystem_t.h:259
Path(Path &&p) noexcept
A move constructor.
Definition ssgx_filesystem_t.h:127
void Clear() noexcept
Clear the Path object.
Definition ssgx_filesystem_t.h:298
friend bool operator!=(const Path &lhs, const Path &rhs) noexcept
Compare whether the Paths are different.
Definition ssgx_filesystem_t.h:234
bool Empty() const noexcept
Check if the path is empty.
Definition ssgx_filesystem_t.h:275
Path & operator=(Path &&p) noexcept
A move assignment operator.
Definition ssgx_filesystem_t.h:154
Path & operator+=(const Path &x)
Appends operator.
Definition ssgx_filesystem_t.h:182
bool HasFilename() const noexcept
Check if the path has a file name.
Definition ssgx_filesystem_t.h:267
Path(const Path &p)
Construct a Path and initialize it.
Definition ssgx_filesystem_t.h:121
Path() noexcept
Construct a Path and initialize it.
Definition ssgx_filesystem_t.h:115
const char * c_str() const noexcept
Return the string of Path.
Definition ssgx_filesystem_t.h:283
friend bool operator==(const Path &lhs, const Path &rhs) noexcept
Compare whether the Paths are the same.
Definition ssgx_filesystem_t.h:224
Read a plaintext file (file size <= 100 KB)
Definition ssgx_filesystem_t.h:561
std::string ReadAllText() const
Read a plaintext text file, the max file size is 100KB.
std::vector< uint8_t > ReadAllBytes() const
Read a plaintext binary file, the max file size is 100KB.
PlainFileReader(std::string filepath)
Construct a PlainFileReader and initialized it.
Definition ssgx_filesystem_t.h:567
Write a plaintext file (file size <= 100 KB)
Definition ssgx_filesystem_t.h:591
void WriteAllBytes(const std::vector< uint8_t > &data)
Write data to a binary file in plaintext.
PlainFileWriter(std::string filepath)
Construct a PlainFileWriter and initialized it.
Definition ssgx_filesystem_t.h:597
void WriteAllText(const std::string &str)
Write data to a text file in plaintext.
A ProtectedFileReader for reading from the Intel SGX Protected File System.
Definition ssgx_filesystem_t.h:621
ProtectedFileReader(const ProtectedFileReader &)=delete
ProtectedFileReader & operator=(const ProtectedFileReader &)=delete
void Close()
Closes the file.
size_t Read(void *buffer, size_t size) const
Reads data from the file into the given buffer.
int64_t Tell() const
Returns the current file position.
ProtectedFileReader(const char *file_name)
Opens a file for reading.
void Seek(int64_t offset, int origin) const
Moves the file position.
A ProtectedFileWriter for writing to the Intel SGX Protected File System.
Definition ssgx_filesystem_t.h:676
ProtectedFileWriter & operator=(const ProtectedFileWriter &)=delete
void Flush() const
Flushes any buffered data to disk.
void Write(const void *data, size_t size) const
Writes data to the file.
void Close()
Closes the file.
ProtectedFileWriter(const ProtectedFileWriter &)=delete
ProtectedFileWriter(const char *file_name, FileMode file_mode=FileMode::CreateNew, uint16_t key_policy=SGX_KEYPOLICY_MRENCLAVE)
Opens a file for writing.
FileStatus SymlinkStatus(const Path &p)
Retrieve file type and permissions; This function can retrieve the type and permissions of the symbol...
bool CreateDirectory(const Path &p)
Create a directory (attribute default 0755)
bool IsEmpty(const Path &p)
If the path is a directory, check whether it is empty; If the path is a regular file,...
bool Remove(const Path &p)
Remove a file.
bool RemoveProtectedFile(const Path &file_name)
Remove a protected file.
bool IsRegularFile(const FileStatus &s) noexcept
Check if the file is a regular file.
FileMode
The mode for file writing operation.
Definition ssgx_filesystem_t_enum.h:60
@ CreateNew
Specifies that the operating system should create a new file. If the file already exists,...
FileType
File type enum.
Definition ssgx_filesystem_t_enum.h:10
FileStatus Status(const Path &p)
Retrieve file type and permissions; If the file is a symbolic link, the retrieved file type and permi...
Perms
File permissions (Octal)
Definition ssgx_filesystem_t_enum.h:26
@ Unknown
Invalid permission.
uintmax_t FileSize(const Path &p)
Check the size of the file.
bool IsDirectory(const FileStatus &s) noexcept
Check if the file is a directory.
bool Exists(const FileStatus &s) noexcept
Check if a file exists.
Definition ssgx_attestation_t.h:6