| 1 |
// For all support, instructions and copyright go to: |
| 2 |
// http://e2guardian.org/ |
| 3 |
// Released under the GPL v2, with the OpenSSL exception described in the README file. |
| 4 |
|
| 5 |
#ifndef __HPP_BACKEDSTORE |
| 6 |
#define __HPP_BACKEDSTORE |
| 7 |
|
| 8 |
// Class into which data can be liberally shoved into RAM up to threshold |
| 9 |
// A, then automagically stored on disk instead up to threshold B, then |
| 10 |
// start failing (but with sensible errors). |
| 11 |
class BackedStore |
| 12 |
{ |
| 13 |
public: |
| 14 |
// Constructor - pass in RAM & disk thresholds |
| 15 |
// and a directory path for temp files |
| 16 |
BackedStore(size_t _ramsize, size_t _disksize, |
| 17 |
const char *_tempdir = "/tmp"); |
| 18 |
~BackedStore(); |
| 19 |
|
| 20 |
// Add data to the store - returns false if |
| 21 |
// disksize would be exceeded or store has |
| 22 |
// been finalised |
| 23 |
bool append(const char *data, size_t len); |
| 24 |
|
| 25 |
// Finalise the store - cannot append any more |
| 26 |
// data after this. Needed because if we are |
| 27 |
// writing to a temp file, mmap is used to access |
| 28 |
// the data, which requires a known file length. |
| 29 |
void finalise(); |
| 30 |
|
| 31 |
// Data access |
| 32 |
const char *getData() const; |
| 33 |
|
| 34 |
// Get length of buffer |
| 35 |
size_t getLength() const; |
| 36 |
|
| 37 |
// Store the contents of the buffer using the given |
| 38 |
// prefix to generate a unique filename. Return the filename. |
| 39 |
std::string store(const char *prefix); |
| 40 |
|
| 41 |
private: |
| 42 |
// Buffer & file descriptor for in-memory/on-disk storage |
| 43 |
std::vector<char> rambuf; |
| 44 |
int fd; |
| 45 |
|
| 46 |
// Size of buffer/file |
| 47 |
size_t length; |
| 48 |
|
| 49 |
// Temp file name |
| 50 |
char *filename; |
| 51 |
|
| 52 |
// Thresholds |
| 53 |
size_t ramsize; |
| 54 |
size_t disksize; |
| 55 |
|
| 56 |
// Temp directory path |
| 57 |
std::string tempdir; |
| 58 |
|
| 59 |
// Pointer to mmapped file contents |
| 60 |
void *map; |
| 61 |
}; |
| 62 |
|
| 63 |
#endif |