LeftHookRoll
An HTTP/1.0 compliant web server, as specified by RFC1945
Loading...
Searching...
No Matches
DataStore.hpp
Go to the documentation of this file.
1/**
2 * @file DataStore.hpp
3 * @brief A DataStore is a flexible data structure, it is either a RAM buffer or a file on disk, depending on the size of the data.
4 * you dont care about it's form, you just read/write from it using the helper functions provided.
5 */
6
7#pragma once
8
9#include <string>
10#include <vector>
11#include <sys/types.h>
12#include <unistd.h>
13#include <errno.h>
14#include <fcntl.h>
15#include <stdlib.h>
16#include <unistd.h>
17#include <sstream>
18#include <algorithm>
19#include <cstring>
20#include <cerrno>
21
22#define FILEPREFIX "/tmp/lefthookroll_"
23#define BUFFERLIMIT 1024 * 1024
24
25/**
26 * @enum BufferMode
27 * @brief Indicates where the data is currently being stored.
28 */
31 FILE_MODE // FILE_MODE to avoid naming collisions with C standard FILE
32};
33
34class DataStore {
35public:
36 // Canonical Form
37 DataStore();
38 DataStore(const DataStore& other);
40 ~DataStore();
41
42 // Core Behavior
43
44 /**
45 * @brief Appends raw byte data to the store.
46 * Automatically transitions from RAM to FILE_MODE if _bufferLimit is exceeded.
47 * @param data Pointer to the buffer to append.
48 * @param length Number of bytes to append.
49 */
50 void append(const char* data, size_t length);
51
52 /**
53 * @brief Convenience overload to append a std::string directly.
54 */
55 void append(const std::string& data);
56
57 /**
58 * @brief Resets the store, clears the vector, and closes the temp file descriptor.
59 */
60 void clear();
61
62 // Getters
63
64 /**
65 * @brief Returns the current storage mode (RAM or FILE_MODE).
66 */
67 BufferMode getMode() const;
68
69 /**
70 * @brief Returns a reference to the RAM buffer.
71 * @warning Only valid if mode is RAM. should never use in normal operation, only for testing/debugging purposes.
72 */
73 const std::vector<char>& getVector() const;
74
75 /**
76 * @brief Returns the file descriptor of the temporary file.
77 * @warning Only valid if mode is FILE_MODE. should never use in normal operation, only for testing/debugging purposes.
78 */
79 int getFd() const;
80
81 /**
82 * @brief Gets total bytes currently stored (whether in RAM or File).
83 */
84 size_t getSize() const;
85
86 /**
87 * @brief Gets the absolute path of the temporary file.
88 * @warning Only valid if mode is FILE_MODE. should never use in normal operation, only for testing/debugging purposes.
89 */
90 std::string getFilePath() const;
91
92 /**
93 * @brief Reads data from the store starting at the current read position.
94 * Advances the internal read position by the number of bytes read.
95 * @param buffer Pointer to the buffer to read into.
96 * @param length Maximum number of bytes to read.
97 * @return Number of bytes actually read (may be less if end of data is reached).
98 */
99 size_t read(char* buffer, size_t length);
100
101 /**
102 * @brief Resets the internal read position to the beginning of the data.
103 */
104 void resetReadPosition();
105
106 /**
107 * @brief Gets the current read position.
108 * @return Current read offset.
109 */
110 size_t getReadPosition() const;
111
112 /**
113 * @brief Handles the transition from RAM to a temporary file on disk.
114 * Uses the immediate unlink() trick to ensure OS-level cleanup on crashes.
115 */
116 void switchToFileMode();
117
118private:
119 // Identity & State
124
125 // RAM Storage
126 std::vector<char> _dataBuffer;
127
128 // File Storage
130 std::string _absolutePath;
131
132 // Private Helpers
133
134
135 /**
136 * @brief Generates a unique temporary filename (e.g., FILEPREFIX_XXXXXX).
137 */
139
140 /**
141 * @brief Ensures all data is written to the file descriptor.
142 */
143 void write_all(int fd, const char* data, size_t length);
144
145 /**
146 * @brief Copies data directly from one FD to another using a buffer.
147 */
148 void copy_fd_contents(const std::string& srcPath, int dstFd, size_t totalBytes);
149};
BufferMode
Indicates where the data is currently being stored.
Definition DataStore.hpp:29
@ RAM
Definition DataStore.hpp:30
@ FILE_MODE
Definition DataStore.hpp:31
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.
Definition DataStore.cpp:32
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.
BufferMode _mode
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).
size_t _bufferLimit
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...
size_t _currentSize
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.
Definition DataStore.cpp:14
size_t read(char *buffer, size_t length)
Reads data from the store starting at the current read position. Advances the internal read position ...
size_t _readOffset
DataStore & operator=(DataStore other)
Definition DataStore.cpp:96
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).