Safeheron-SGX-Native-Development-Framework v1.2.0
Loading...
Searching...
No Matches
ssgx_filesystem_t.h
Go to the documentation of this file.
1
2#ifndef SAFEHERON_SGX_TRUSTED_FILESTREAM_H
3#define SAFEHERON_SGX_TRUSTED_FILESTREAM_H
4#include <string>
5
6#include "sgx_key.h"
7#include "sgx_tprotected_fs.h"
8
10namespace ssgx {
14namespace filesystem_t {
19 public:
24 }
25
29 explicit FileStatus(FileType ft, Perms prms = Perms::Unknown) noexcept : ft_(ft), prms_(prms) {
30 }
31
35 FileStatus(const FileStatus&) noexcept = default;
36
40 FileStatus(FileStatus&&) noexcept = default;
41
46 }
47
52 FileStatus& operator=(const FileStatus&) noexcept = default;
53
58 FileStatus& operator=(FileStatus&&) noexcept = default;
59
64 FileType Type() const noexcept {
65 return ft_;
66 }
67
72 Perms Permissions() const noexcept {
73 return prms_;
74 }
75
80 void Type(FileType ft) noexcept {
81 ft_ = ft;
82 }
83
88 void Permissions(Perms p) noexcept {
89 prms_ = p;
90 }
91
92 private:
93 FileType ft_;
94 Perms prms_;
95};
96
100class Path {
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'\\';
105#else
106 typedef char value_type;
107 static constexpr value_type preferred_separator = '/';
108#endif
109 typedef std::basic_string<value_type> string_type;
110
111 public:
115 Path() noexcept {
116 }
117
121 Path(const Path& p) : pn_(p.pn_) {
122 }
123
127 Path(Path&& p) noexcept : pn_(std::move(p.pn_)) {
128 }
129
133 Path(string_type&& source) : pn_(std::move(source)) {
134 }
135
139 ~Path() = default;
140
145 Path& operator=(const Path& p) {
146 pn_ = p.pn_;
147 return *this;
148 }
149
154 Path& operator=(Path&& p) noexcept {
155 pn_ = std::move(p.pn_);
156 return *this;
157 }
158
163 Path& operator=(string_type&& source) noexcept {
164 pn_ = std::move(source);
165 return *this;
166 }
167
172 Path& assign(string_type&& source) noexcept {
173 pn_ = std::move(source);
174 return *this;
175 }
176
182 Path& operator+=(const Path& x) {
183 pn_ += x.pn_;
184 return *this;
185 }
186
192 Path& operator+=(const string_type& x) {
193 pn_ += x;
194 return *this;
195 }
196
207 Path& operator/=(const Path& p) {
208 if (p.IsAbsolute()) {
209 pn_ = p.pn_;
210 return *this;
211 }
212 if (HasFilename())
213 pn_ += preferred_separator;
214 pn_ += p.pn_;
215 return *this;
216 }
217
224 friend bool operator==(const Path& lhs, const Path& rhs) noexcept {
225 return lhs.String() == rhs.String();
226 }
227
234 friend bool operator!=(const Path& lhs, const Path& rhs) noexcept {
235 return lhs.String() != rhs.String();
236 }
237
249 friend Path operator/(const Path& lhs, const Path& rhs) {
250 Path result(lhs);
251 result /= rhs;
252 return result;
253 }
254
259 bool IsAbsolute() const noexcept {
260 return !pn_.empty() && pn_.front() == preferred_separator;
261 }
262
267 bool HasFilename() const noexcept {
268 return !pn_.empty() && pn_.back() != preferred_separator;
269 }
270
275 bool Empty() const noexcept {
276 return pn_.empty();
277 }
278
283 const char* c_str() const noexcept {
284 return pn_.c_str();
285 }
286
291 std::string String() const {
292 return pn_;
293 }
294
298 void Clear() noexcept {
299 pn_.clear();
300 }
301
302 private:
303 string_type pn_;
304};
305
321bool CreateDirectory(const Path& p);
322
340
358
375bool Exists(const FileStatus& s) noexcept;
376
392bool Exists(const Path& p);
393
409bool Remove(const Path& p);
410
426bool RemoveProtectedFile(const Path& file_name);
427
443uintmax_t FileSize(const Path& p);
444
461bool IsDirectory(const FileStatus& s) noexcept;
462
478bool IsDirectory(const Path& p);
479
497bool IsEmpty(const Path& p);
498
516bool IsRegularFile(const FileStatus& s) noexcept;
517
533bool IsRegularFile(const Path& p);
534
538class FileSystemException : public std::exception {
539 public:
543 explicit FileSystemException(std::string err_msg) : err_msg_(std::move(err_msg)) {
544 }
545
550 const char* what() const noexcept override {
551 return err_msg_.c_str();
552 }
553
554 private:
555 std::string err_msg_;
556};
557
562 public:
567 explicit PlainFileReader(std::string filepath) : file_path_(std::move(filepath)) {
568 }
569
575 std::vector<uint8_t> ReadAllBytes() const;
576
582 std::string ReadAllText() const;
583
584 private:
585 std::string file_path_;
586};
587
592 public:
597 explicit PlainFileWriter(std::string filepath) : file_path_(std::move(filepath)) {
598 }
599
605 void WriteAllBytes(const std::vector<uint8_t>& data);
606
612 void WriteAllText(const std::string& str);
613
614 private:
615 std::string file_path_;
616};
617
622 private:
623 SGX_FILE* file_;
624 const char* file_name_;
625
626 public:
629
635 explicit ProtectedFileReader(const char* file_name);
637
648 size_t Read(void* buffer, size_t size) const;
649
655 int64_t Tell() const;
656
663 void Seek(int64_t offset, int origin) const;
664
669 void Close();
670};
671
677 private:
678 SGX_FILE* file_;
679 const char* file_name_;
680
681 public:
684
692 explicit ProtectedFileWriter(const char* file_name, FileMode file_mode = FileMode::CreateNew, uint16_t key_policy = SGX_KEYPOLICY_MRENCLAVE);
693
695
705 void Write(const void* data, size_t size) const;
706
711 void Flush() const;
712
717 void Close();
718};
719
720}; // namespace filesystem_t
721}; // namespace ssgx
722
723#endif // SAFEHERON_SGX_TRUSTED_FILESTREAM_H
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
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.
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