KittyMemoryEx 1.0.0
Dedicated library for runtime code patching, memory scanning, tracing, injection and some more useful memory utilities. works for both Android and Linux
Loading...
Searching...
No Matches
KittyIOFile.hpp
1#pragma once
2
3#include "KittyUtils.hpp"
4
9{
10private:
11 int _fd;
12 std::string _filePath;
13 int _flags;
14 mode_t _mode;
15 int _error;
16
17public:
21 KittyIOFile() : _fd(-1), _flags(0), _mode(0), _error(0)
22 {
23 }
24
32 KittyIOFile(const std::string &filePath, int flags, mode_t mode = 0)
33 : _fd(-1), _filePath(filePath), _flags(flags), _mode(mode), _error(0)
34 {
35 }
36
38 {
39 if (_fd >= 0)
40 {
41 close();
42 }
43 }
44
45 KittyIOFile(const KittyIOFile &) = delete;
46 KittyIOFile &operator=(const KittyIOFile &) = delete;
47
51 KittyIOFile(KittyIOFile &&other) noexcept
52 : _fd(other._fd), _filePath(std::move(other._filePath)), _flags(other._flags), _mode(other._mode),
53 _error(other._error)
54 {
55 other._fd = -1;
56 other._error = 0;
57 }
58
63 {
64 if (this != &other)
65 {
66 close();
67
68 _fd = other._fd;
69 _filePath = std::move(other._filePath);
70 _flags = other._flags;
71 _mode = other._mode;
72 _error = other._error;
73
74 other._fd = -1;
75 other._error = 0;
76 }
77 return *this;
78 }
79
85 bool open();
86
92 bool close();
93
99 inline int lastError() const
100 {
101 return _error;
102 }
103
109 inline std::string lastStrError() const
110 {
111 return _error ? strerror(_error) : "";
112 }
113
119 inline int fd() const
120 {
121 return _fd;
122 }
123
129 inline std::string path() const
130 {
131 return _filePath;
132 }
133
139 inline int flags() const
140 {
141 return _flags;
142 }
143
149 inline mode_t mode() const
150 {
151 return _mode;
152 }
153
161 ssize_t read(void *buffer, size_t len);
162
170 ssize_t write(const void *buffer, size_t len);
171
180 ssize_t pread(kt_off64_t offset, void *buffer, size_t len);
181
190 ssize_t pwrite(kt_off64_t offset, const void *buffer, size_t len);
191
197 inline bool exists() const
198 {
199 return access(_filePath.c_str(), F_OK) != -1;
200 }
201
207 inline bool canRead() const
208 {
209 return access(_filePath.c_str(), R_OK) != -1;
210 }
211
217 inline bool canWrite() const
218 {
219 return access(_filePath.c_str(), W_OK) != -1;
220 }
221
227 inline bool canExecute() const
228 {
229 return access(_filePath.c_str(), X_OK) != -1;
230 }
231
237 inline bool remove()
238 {
239 _error = (unlink(_filePath.c_str()) == -1) ? errno : 0;
240 return _error == 0;
241 }
242
248 inline kt_stat64_t info()
249 {
250 kt_stat64_t s = {};
251 _error = (kt_stat64(_filePath.c_str(), &s) == -1) ? errno : 0;
252 return s;
253 }
254
260 inline bool isFile()
261 {
262 auto s = info();
263 return _error == 0 && S_ISREG(s.st_mode);
264 }
265
272 bool readToString(std::string *str);
273
280 bool readToBuffer(std::vector<char> *buf);
281
288 bool copyToFile(const std::string &filePath)
289 {
290 KittyIOFile f(filePath, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0666);
291 return f.open() && copyToFd(f.fd());
292 }
293
300 bool copyToFd(int fd);
301
309 inline static bool readFileToString(const std::string &filePath, std::string *str)
310 {
311 KittyIOFile f(filePath, O_RDONLY | O_CLOEXEC);
312 return f.open() && f.readToString(str);
313 }
314
322 inline static bool readFileToBuffer(const std::string &filePath, std::vector<char> *buf)
323 {
324 KittyIOFile f(filePath, O_RDONLY | O_CLOEXEC);
325 return f.open() && f.readToBuffer(buf);
326 }
327
335 inline static bool copy(const std::string &srcFilePath, const std::string &dstFilePath)
336 {
337 KittyIOFile f(srcFilePath, O_RDONLY | O_CLOEXEC);
338 return f.open() && f.copyToFile(dstFilePath);
339 }
340
347 static bool listFilesCallback(const std::string &dir, std::function<bool(const std::string &)> cb);
348
370 static bool createDirectoryRecursive(const std::string &path, mode_t mode = 0755);
371};
This class provides an interface for file operations.
Definition KittyIOFile.hpp:9
std::string path() const
Returns the file path.
Definition KittyIOFile.hpp:129
KittyIOFile & operator=(KittyIOFile &&other) noexcept
Move assignment operator. Closes current file descriptor and transfers ownership.
Definition KittyIOFile.hpp:62
int flags() const
Returns the file flags.
Definition KittyIOFile.hpp:139
static bool listFilesCallback(const std::string &dir, std::function< bool(const std::string &)> cb)
Lists files in a directory.
bool canRead() const
Checks if the file can be read.
Definition KittyIOFile.hpp:207
KittyIOFile(KittyIOFile &&other) noexcept
Move constructor. Transfers ownership of the file descriptor.
Definition KittyIOFile.hpp:51
bool exists() const
Checks if the file exists.
Definition KittyIOFile.hpp:197
static bool readFileToString(const std::string &filePath, std::string *str)
Reads the contents of a file into a string.
Definition KittyIOFile.hpp:309
bool canWrite() const
Checks if the file can be written.
Definition KittyIOFile.hpp:217
bool copyToFd(int fd)
Writes the contents of the file to a file descriptor.
bool readToBuffer(std::vector< char > *buf)
Reads the contents of the file into a buffer.
KittyIOFile()
Default constructor creating an uninitialized file object.
Definition KittyIOFile.hpp:21
bool canExecute() const
Checks if the file can be executed.
Definition KittyIOFile.hpp:227
bool isFile()
Checks if the file is a regular file.
Definition KittyIOFile.hpp:260
static bool readFileToBuffer(const std::string &filePath, std::vector< char > *buf)
Reads the contents of a file into a buffer.
Definition KittyIOFile.hpp:322
ssize_t read(void *buffer, size_t len)
Reads data from the file and advances file pointer.
ssize_t write(const void *buffer, size_t len)
Writes data to the file and advances file pointer.
KittyIOFile(const std::string &filePath, int flags, mode_t mode=0)
Constructs a new KittyIOFile object with file path, flags, and optional mode.
Definition KittyIOFile.hpp:32
bool close()
Closes the file.
int fd() const
Returns the file descriptor.
Definition KittyIOFile.hpp:119
kt_stat64_t info()
Retrieves information about the file.
Definition KittyIOFile.hpp:248
bool open()
Opens the file.
ssize_t pread(kt_off64_t offset, void *buffer, size_t len)
Reads data from the file at a given offset without changing file pointer.
ssize_t pwrite(kt_off64_t offset, const void *buffer, size_t len)
Writes data to the file at a given offset without changing file pointer.
static bool copy(const std::string &srcFilePath, const std::string &dstFilePath)
Copies the contents of a file to another file.
Definition KittyIOFile.hpp:335
bool remove()
Removes the file.
Definition KittyIOFile.hpp:237
bool copyToFile(const std::string &filePath)
Copies the contents of the file to another file.
Definition KittyIOFile.hpp:288
bool readToString(std::string *str)
Reads the contents of the file into a string.
static bool createDirectoryRecursive(const std::string &path, mode_t mode=0755)
Recursively creates a directory path.
mode_t mode() const
Returns the file mode.
Definition KittyIOFile.hpp:149
int lastError() const
Returns the last error.
Definition KittyIOFile.hpp:99
std::string lastStrError() const
Returns the last error message.
Definition KittyIOFile.hpp:109