7#include "../includes/DataStore.hpp"
16 while (offset < length) {
17 ssize_t written = ::write(fd, data + offset, length - offset);
20 throw std::runtime_error(std::string(
"DataStore: write failed - ") + std::strerror(errno));
23 throw std::runtime_error(
"DataStore: write failed - 0 bytes written (possible disk full)");
25 offset +=
static_cast<size_t>(written);
33 int srcFd = ::open(srcPath.c_str(), O_RDONLY);
35 throw std::runtime_error(std::string(
"DataStore: open failed during copy - ") + std::strerror(errno));
37 static const size_t kChunkSize = 8192;
38 std::vector<char> buffer(kChunkSize);
41 while (offset < totalBytes) {
42 size_t toRead = std::min(kChunkSize, totalBytes - offset);
43 ssize_t readBytes =
::read(srcFd, &buffer[0], toRead);
47 throw std::runtime_error(std::string(
"DataStore: read failed during copy - ") + std::strerror(errno));
50 throw std::runtime_error(
"DataStore: copy failed - unexpected EOF (source truncated)");
53 write_all(dstFd, &buffer[0],
static_cast<size_t>(readBytes));
54 offset +=
static_cast<size_t>(readBytes);
69DataStore::DataStore(
const DataStore& other): _mode(
RAM), _bufferLimit(other._bufferLimit), _currentSize(0), _readOffset(0), _dataBuffer(), _fileFd(-1), _absolutePath()
120 if (data == NULL || length == 0) {
146 append(data.c_str(), data.size());
203 size_t toRead = std::min(length, available);
211 size_t totalRead = 0;
212 while (totalRead < toRead) {
213 ssize_t bytesRead =
::read(
_fileFd, buffer + totalRead, toRead - totalRead);
216 throw std::runtime_error(std::string(
"DataStore: read failed - ") + std::strerror(errno));
218 if (bytesRead == 0) {
221 totalRead +=
static_cast<size_t>(bytesRead);
237 throw std::runtime_error(
"DataStore: Failed to reopen for reset");
278 static long long file_counter = 0;
280 std::string currentName;
283 std::stringstream ss;
285 currentName = ss.str();
287 fd = ::open(currentName.c_str(), O_CREAT | O_EXCL | O_RDWR | O_APPEND, 0600);
290 if (errno == EEXIST) {
293 throw std::runtime_error(std::string(
"DataStore: open failed - ") + std::strerror(errno));
BufferMode
Indicates where the data is currently being stored.
size_t getReadPosition() const
Gets the current read position.
void copy_fd_contents(const std::string &srcPath, int dstFd, size_t totalBytes)
Copies data directly from one FD to another using a buffer.
std::string _absolutePath
int getFd() const
Returns the file descriptor of the temporary file.
const std::vector< char > & getVector() const
Returns a reference to the RAM buffer.
std::string getFilePath() const
Gets the absolute path of the temporary file.
void _generateTempFileName()
Generates a unique temporary filename (e.g., FILEPREFIX_XXXXXX).
size_t getSize() const
Gets total bytes currently stored (whether in RAM or File).
std::vector< char > _dataBuffer
void append(const char *data, size_t length)
Appends raw byte data to the store. Automatically transitions from RAM to FILE_MODE if _bufferLimit i...
void switchToFileMode()
Handles the transition from RAM to a temporary file on disk. Uses the immediate unlink() trick to ens...
void resetReadPosition()
Resets the internal read position to the beginning of the data.
void write_all(int fd, const char *data, size_t length)
Ensures all data is written to the file descriptor.
size_t read(char *buffer, size_t length)
Reads data from the store starting at the current read position. Advances the internal read position ...
DataStore & operator=(DataStore other)
void clear()
Resets the store, clears the vector, and closes the temp file descriptor.
BufferMode getMode() const
Returns the current storage mode (RAM or FILE_MODE).