| 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 |
// INCLUDES |
| 6 |
|
| 7 |
#ifdef HAVE_CONFIG_H |
| 8 |
#include "e2config.h" |
| 9 |
#endif |
| 10 |
|
| 11 |
#include <cstddef> |
| 12 |
#include <vector> |
| 13 |
#include <string> |
| 14 |
#include <exception> |
| 15 |
#include <stdexcept> |
| 16 |
#include <cstdlib> |
| 17 |
#include <cstring> |
| 18 |
#include <cerrno> |
| 19 |
#include <sstream> |
| 20 |
#include <ctime> |
| 21 |
|
| 22 |
#include <unistd.h> |
| 23 |
|
| 24 |
#include <sys/stat.h> |
| 25 |
#include <sys/mman.h> |
| 26 |
#include <sys/time.h> |
| 27 |
|
| 28 |
#include "BackedStore.hpp" |
| 29 |
#include "Logger.hpp" |
| 30 |
|
| 31 |
|
| 32 |
// IMPLEMENTATION |
| 33 |
|
| 34 |
BackedStore::BackedStore(size_t _ramsize, size_t _disksize, const char *_tempdir) |
| 35 |
: fd(-1), length(0), filename(NULL), ramsize(_ramsize), disksize(_disksize), tempdir(_tempdir), map(MAP_FAILED) |
| 36 |
{ |
| 37 |
} |
| 38 |
|
| 39 |
BackedStore::~BackedStore() |
| 40 |
{ |
| 41 |
if (map != MAP_FAILED) |
| 42 |
munmap(map, length); |
| 43 |
|
| 44 |
if (fd >= 0) { |
| 45 |
DEBUG_debug("BackedStore: closing & deleting temp file ", filename, " BAE2ERS!"); |
| 46 |
int rc = 0; |
| 47 |
do { |
| 48 |
rc = close(fd); |
| 49 |
} while (rc < 0 && errno == EINTR); |
| 50 |
if (rc < 0) |
| 51 |
DEBUG_debug("BackedStore: cannot close temp file fd: ", strerror(errno)); |
| 52 |
rc = unlink(filename); |
| 53 |
if (rc < 0) |
| 54 |
DEBUG_debug("BackedStore: cannot delete temp file: ", strerror(errno)); |
| 55 |
free(filename); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
bool BackedStore::append(const char *data, size_t len) |
| 60 |
{ |
| 61 |
if (fd < 0) { |
| 62 |
DEBUG_debug("BackedStore: appending to RAM"); |
| 63 |
// Temp file not yet opened - try to write to RAM |
| 64 |
if (rambuf.size() + len > ramsize) { |
| 65 |
// Would exceed RAM threshold |
| 66 |
if (rambuf.size() + len > disksize) { |
| 67 |
// Would also exceed disk threshold |
| 68 |
// - give up |
| 69 |
DEBUG_debug("BackedStore: data would exceed both RAM and disk thresholds"); |
| 70 |
return false; |
| 71 |
} |
| 72 |
|
| 73 |
DEBUG_debug("BackedStore: data would exceed RAM threshold; dumping RAM to disk"); |
| 74 |
|
| 75 |
// Open temp file, dump current data in there, |
| 76 |
// leave code below this if{} to write current |
| 77 |
// data to the file as well |
| 78 |
std::string filename_str = tempdir + "/__dgbsXXXXXX"; |
| 79 |
filename = const_cast<char *>(filename_str.c_str()); |
| 80 |
DEBUG_debug("BackedStore: filename template: ", filename); |
| 81 |
// mode_t mask = umask(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); // this mask is reversed |
| 82 |
umask(0007); // only allow access to e2g user and group |
| 83 |
if ((fd = mkstemp(filename)) < 0) { |
| 84 |
std::ostringstream ss; |
| 85 |
ss << thread_id << "BackedStore could not create temp file: " << strerror(errno); |
| 86 |
free(filename); |
| 87 |
throw std::runtime_error(ss.str().c_str()); |
| 88 |
} |
| 89 |
|
| 90 |
DEBUG_debug("BackedStore: filename: ", filename); |
| 91 |
free(filename); |
| 92 |
|
| 93 |
size_t bytes_written = 0; |
| 94 |
ssize_t rc = 0; |
| 95 |
do { |
| 96 |
rc = write(fd, &(rambuf.front()) + bytes_written, rambuf.size() - bytes_written); |
| 97 |
if (rc > 0) |
| 98 |
bytes_written += rc; |
| 99 |
} while (bytes_written < rambuf.size() && (rc > 0 || errno == EINTR)); |
| 100 |
if (rc < 0 && errno != EINTR) { |
| 101 |
std::ostringstream ss; |
| 102 |
ss << thread_id << "BackedStore could not dump RAM buffer to temp file: " << strerror(errno); |
| 103 |
throw std::runtime_error(ss.str().c_str()); |
| 104 |
} |
| 105 |
length = rambuf.size(); |
| 106 |
rambuf.clear(); |
| 107 |
} else |
| 108 |
rambuf.insert(rambuf.end(), data, data + len); |
| 109 |
} |
| 110 |
|
| 111 |
if (fd >= 0) { |
| 112 |
DEBUG_debug("BackedStore: appending to disk"); |
| 113 |
// Temp file opened - try to write to disk |
| 114 |
if (map != MAP_FAILED) |
| 115 |
throw std::runtime_error("BackedStore could not append to temp file: store already finalised"); |
| 116 |
if (len + length > disksize) { |
| 117 |
DEBUG_debug("BackedStore: data would exceed disk threshold"); |
| 118 |
return false; |
| 119 |
} |
| 120 |
size_t bytes_written = 0; |
| 121 |
ssize_t rc = 0; |
| 122 |
do { |
| 123 |
rc = write(fd, data + bytes_written, len - bytes_written); |
| 124 |
if (rc > 0) |
| 125 |
bytes_written += rc; |
| 126 |
} while (bytes_written < len && (rc > 0 || errno == EINTR)); |
| 127 |
if (rc < 0 && errno != EINTR) { |
| 128 |
std::ostringstream ss; |
| 129 |
ss << thread_id << "BackedStore could not dump RAM buffer to temp file: " << strerror(errno); |
| 130 |
throw std::runtime_error(ss.str().c_str()); |
| 131 |
} |
| 132 |
length += len; |
| 133 |
} |
| 134 |
|
| 135 |
DEBUG_debug("BackedStore: finished appending"); |
| 136 |
|
| 137 |
return true; |
| 138 |
} |
| 139 |
|
| 140 |
size_t BackedStore::getLength() const |
| 141 |
{ |
| 142 |
if (fd >= 0) |
| 143 |
return length; |
| 144 |
else |
| 145 |
return rambuf.size(); |
| 146 |
} |
| 147 |
|
| 148 |
void BackedStore::finalise() |
| 149 |
{ |
| 150 |
if (fd < 0) |
| 151 |
// No temp file - nothing to finalise |
| 152 |
return; |
| 153 |
|
| 154 |
lseek(fd, 0, SEEK_SET); |
| 155 |
map = mmap(0, length, PROT_READ, MAP_PRIVATE, fd, 0); |
| 156 |
if (map == MAP_FAILED) { |
| 157 |
std::ostringstream ss; |
| 158 |
ss << thread_id << "BackedStore could not mmap() temp file: " << strerror(errno); |
| 159 |
throw std::runtime_error(ss.str().c_str()); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
const char *BackedStore::getData() const |
| 164 |
{ |
| 165 |
if (fd < 0) { |
| 166 |
DEBUG_debug("BackedStore: returning pointer to RAM"); |
| 167 |
return &(rambuf.front()); |
| 168 |
} else { |
| 169 |
DEBUG_debug("BackedStore: returning pointer to mmap-ed file"); |
| 170 |
if (map == MAP_FAILED) |
| 171 |
throw std::runtime_error("BackedStore could not return data pointer: store not finalised"); |
| 172 |
return (const char *)map; |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
std::string BackedStore::store(const char *prefix) |
| 177 |
{ |
| 178 |
if (fd >= 0) { |
| 179 |
// We already have a temp file on disk |
| 180 |
// Try creating a hardlink with the new name and see what happens |
| 181 |
std::ostringstream storedname; |
| 182 |
storedname << prefix; |
| 183 |
timeval tv; |
| 184 |
// Use time of day (in microsecond resolution) to try and generate |
| 185 |
// a "random" name for the hardlink - tempnam doesn't allow arbitrary |
| 186 |
// prefixes (POSIX says up to 5 chars). |
| 187 |
gettimeofday(&tv, NULL); |
| 188 |
storedname << '-' << tv.tv_sec << tv.tv_usec << std::flush; |
| 189 |
|
| 190 |
char *name = strrchr(filename, '/'); |
| 191 |
// DEBUG_debug("BackedStore: creating hard link: " << (char)storedname ); |
| 192 |
std::string storedname_str(storedname.str()); |
| 193 |
int rc = link(name, storedname_str.c_str()); |
| 194 |
if (rc >= 0) |
| 195 |
// Success! Return new filename |
| 196 |
return storedname_str; |
| 197 |
else if (errno != EXDEV) { |
| 198 |
// Failure - but ignore EXDEV, as we can "recover" |
| 199 |
// from that by taking a different approach |
| 200 |
std::ostringstream ss; |
| 201 |
ss << thread_id << "BackedStore could not create link to existing temp file: " << strerror(errno); |
| 202 |
throw std::runtime_error(ss.str().c_str()); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
// We don't already have a temp file, |
| 207 |
// or a simple link wasn't sufficient (EXDEV) |
| 208 |
// Generate a new filename in the given directory, with the given name prefix |
| 209 |
// Include timestamp in the name for added uniqueness |
| 210 |
std::ostringstream timedprefix; |
| 211 |
timedprefix << prefix << '-' << time(NULL) << '-' << std::flush; |
| 212 |
std::string storedname_str(timedprefix.str() + "XXXXXX"); |
| 213 |
char *storedname = const_cast<char *>(storedname_str.c_str()); |
| 214 |
DEBUG_debug("BackedStore: storedname template: ", storedname); |
| 215 |
int storefd; |
| 216 |
umask(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); |
| 217 |
if ((storefd = mkstemp(storedname)) < 0) { |
| 218 |
std::ostringstream ss; |
| 219 |
ss << thread_id << "BackedStore could not create stored file: " << strerror(errno); |
| 220 |
throw std::runtime_error(ss.str().c_str()); |
| 221 |
} |
| 222 |
DEBUG_debug("BackedStore: storedname: ", storedname); |
| 223 |
|
| 224 |
// Dump the RAM buffer/mmap-ed file contents to disk in the new location |
| 225 |
if (fd >= 0 && map == MAP_FAILED) |
| 226 |
throw std::runtime_error("BackedStore could not copy existing temp file: store not finalised"); |
| 227 |
|
| 228 |
size_t bytes_written = 0; |
| 229 |
ssize_t rc = 0; |
| 230 |
if (fd >= 0) { |
| 231 |
do { |
| 232 |
rc = write(storefd, (const char *)map + bytes_written, length - bytes_written); |
| 233 |
if (rc > 0) |
| 234 |
bytes_written += rc; |
| 235 |
} while (bytes_written < length && (rc > 0 || errno == EINTR)); |
| 236 |
} else { |
| 237 |
do { |
| 238 |
rc = write(storefd, &(rambuf.front()) + bytes_written, rambuf.size() - bytes_written); |
| 239 |
if (rc > 0) |
| 240 |
bytes_written += rc; |
| 241 |
} while (bytes_written < rambuf.size() && (rc > 0 || errno == EINTR)); |
| 242 |
} |
| 243 |
|
| 244 |
if (rc < 0 && errno != EINTR) { |
| 245 |
std::ostringstream ss; |
| 246 |
ss << thread_id << "BackedStore could not dump RAM buffer to temp file: " << strerror(errno); |
| 247 |
do { |
| 248 |
rc = close(storefd); |
| 249 |
} while (rc < 0 && errno == EINTR); |
| 250 |
throw std::runtime_error(ss.str().c_str()); |
| 251 |
} |
| 252 |
|
| 253 |
do { |
| 254 |
rc = close(storefd); |
| 255 |
} while (rc < 0 && errno == EINTR); |
| 256 |
|
| 257 |
return storedname; |
| 258 |
} |