3#include "KittyUtils.hpp"
12 std::string _filePath;
33 : _fd(-1), _filePath(filePath), _flags(
flags), _mode(
mode), _error(0)
52 : _fd(other._fd), _filePath(std::move(other._filePath)), _flags(other._flags), _mode(other._mode),
69 _filePath = std::move(other._filePath);
70 _flags = other._flags;
72 _error = other._error;
111 return _error ? strerror(_error) :
"";
119 inline int fd()
const
129 inline std::string
path()
const
161 ssize_t
read(
void *buffer,
size_t len);
170 ssize_t
write(
const void *buffer,
size_t len);
180 ssize_t
pread(kt_off64_t offset,
void *buffer,
size_t len);
190 ssize_t
pwrite(kt_off64_t offset,
const void *buffer,
size_t len);
199 return access(_filePath.c_str(), F_OK) != -1;
209 return access(_filePath.c_str(), R_OK) != -1;
219 return access(_filePath.c_str(), W_OK) != -1;
229 return access(_filePath.c_str(), X_OK) != -1;
239 _error = (unlink(_filePath.c_str()) == -1) ? errno : 0;
251 _error = (kt_stat64(_filePath.c_str(), &s) == -1) ? errno : 0;
263 return _error == 0 && S_ISREG(s.st_mode);
290 KittyIOFile f(filePath, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0666);
335 inline static bool copy(
const std::string &srcFilePath,
const std::string &dstFilePath)
347 static bool listFilesCallback(
const std::string &dir, std::function<
bool(
const std::string &)> cb);
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