10 #ifndef __STDC_FORMAT_MACROS
11 #define __STDC_FORMAT_MACROS
22 #include <sys/select.h>
24 #include <sys/types.h>
97 for (
unsigned i = 0; i < N; ++i) {
101 pFile->fd = (*fd_to_)[i];
111 VfsRdOnlyFile *p =
reinterpret_cast<VfsRdOnlyFile *
>(pFile);
113 int retval = p->vfs_rdonly->cache_mgr->Close(p->fd);
118 return SQLITE_IOERR_CLOSE;
129 sqlite_int64 iOfst) {
130 VfsRdOnlyFile *p =
reinterpret_cast<VfsRdOnlyFile *
>(pFile);
132 ssize_t got = p->vfs_rdonly->cache_mgr->Pread(p->fd, zBuf, iAmt, iOfst);
137 }
else if (got < 0) {
138 return SQLITE_IOERR_READ;
141 memset(reinterpret_cast<char *>(zBuf) + got, 0, iAmt - got);
142 return SQLITE_IOERR_SHORT_READ;
151 return SQLITE_READONLY;
157 return SQLITE_READONLY;
168 VfsRdOnlyFile *p =
reinterpret_cast<VfsRdOnlyFile *
>(pFile);
199 return SQLITE_NOTFOUND;
229 static const sqlite3_io_methods io_methods = {1,
243 VfsRdOnlyFile *p =
reinterpret_cast<VfsRdOnlyFile *
>(pFile);
244 CacheManager *cache_mgr =
reinterpret_cast<VfsRdOnly *
>(vfs->pAppData)
247 p->base.pMethods = NULL;
249 if (flags & SQLITE_OPEN_READWRITE)
251 if (flags & SQLITE_OPEN_DELETEONCLOSE)
253 if (flags & SQLITE_OPEN_EXCLUSIVE)
256 assert(zName && (zName[0] ==
'@'));
262 cache_mgr->
Close(p->fd);
264 return SQLITE_IOERR_FSTAT;
267 cache_mgr->
Close(p->fd);
271 p->size =
static_cast<uint64_t
>(
size);
274 p->vfs_rdonly =
reinterpret_cast<VfsRdOnly *
>(vfs->pAppData);
275 p->base.pMethods = &io_methods;
286 return SQLITE_IOERR_DELETE;
297 if (flags == SQLITE_ACCESS_READWRITE) {
308 perf::Inc(reinterpret_cast<VfsRdOnly *>(vfs->pAppData)->n_access);
320 zOut[nOut - 1] =
'\0';
321 sqlite3_snprintf(nOut, zOut,
"%s", zPath);
330 assert(static_cast<size_t>(nBuf) >= (
sizeof(time_t) +
sizeof(
int)));
331 perf::Inc(reinterpret_cast<VfsRdOnly *>(vfs->pAppData)->n_rand);
332 memset(zBuf, 0, nBuf);
333 pid_t randomnessPid = getpid();
335 fd = open(
"/dev/urandom", O_RDONLY, 0);
339 memcpy(zBuf, &t,
sizeof(t));
340 memcpy(&zBuf[
sizeof(t)], &randomnessPid,
sizeof(randomnessPid));
341 assert(
sizeof(t) +
sizeof(randomnessPid) <= static_cast<size_t>(nBuf));
342 nBuf =
sizeof(t) +
sizeof(randomnessPid);
345 got = read(fd, zBuf, nBuf);
346 }
while (got < 0 && errno == EINTR);
349 perf::Xadd(reinterpret_cast<VfsRdOnly *>(vfs->pAppData)->sz_rand, nBuf);
358 struct timeval wait_for;
359 wait_for.tv_sec = microseconds / (1000 * 1000);
360 wait_for.tv_usec = microseconds % (1000 * 1000);
361 select(0, NULL, NULL, NULL, &wait_for);
362 perf::Inc(reinterpret_cast<VfsRdOnly *>(vfs->pAppData)->n_sleep);
363 perf::Xadd(reinterpret_cast<VfsRdOnly *>(vfs->pAppData)->sz_sleep,
373 static const sqlite3_int64 unixEpoch = 24405875 * (sqlite3_int64)8640000;
376 if (gettimeofday(&sNow, 0) == 0) {
377 *piNow = unixEpoch + 1000 * (sqlite3_int64)sNow.tv_sec
378 + sNow.tv_usec / 1000;
379 perf::Inc(reinterpret_cast<VfsRdOnly *>(vfs->pAppData)->n_time);
393 *prNow = i / 86400000.0;
415 fd_to_ =
new std::vector<int>();
417 sqlite3_vfs *vfs =
reinterpret_cast<sqlite3_vfs *
>(
418 smalloc(
sizeof(sqlite3_vfs)));
419 memset(vfs, 0,
sizeof(sqlite3_vfs));
420 VfsRdOnly *vfs_rdonly =
new VfsRdOnly();
423 vfs->szOsFile =
sizeof(VfsRdOnlyFile);
424 vfs->mxPathname = PATH_MAX;
426 vfs->pAppData = vfs_rdonly;
432 vfs->xDlError = NULL;
434 vfs->xDlClose = NULL;
442 int retval = sqlite3_vfs_register(vfs, options ==
kVfsOptDefault);
443 if (retval != SQLITE_OK) {
444 free(const_cast<char *>(vfs->zName));
450 vfs_rdonly->cache_mgr = cache_mgr;
451 vfs_rdonly->n_access = statistics->
Register(
452 "sqlite.n_access",
"overall number of access() calls");
453 vfs_rdonly->no_open = statistics->
Register(
"sqlite.no_open",
454 "currently open sqlite files");
455 vfs_rdonly->n_rand = statistics->
Register(
"sqlite.n_rand",
456 "overall number of random() calls");
457 vfs_rdonly->sz_rand = statistics->
Register(
"sqlite.sz_rand",
458 "overall number of random bytes");
459 vfs_rdonly->n_read = statistics->
Register(
"sqlite.n_read",
460 "overall number of read() calls");
461 vfs_rdonly->sz_read = statistics->
Register(
"sqlite.sz_read",
462 "overall bytes read()");
463 vfs_rdonly->n_sleep = statistics->
Register(
"sqlite.n_sleep",
464 "overall number of sleep() calls");
465 vfs_rdonly->sz_sleep = statistics->
Register(
"sqlite.sz_sleep",
466 "overall microseconds slept");
467 vfs_rdonly->n_time = statistics->
Register(
"sqlite.n_time",
468 "overall number of time() calls");
479 sqlite3_vfs *vfs = sqlite3_vfs_find(
kVfsName);
482 int retval = sqlite3_vfs_unregister(vfs);
483 if (retval != SQLITE_OK)
486 delete reinterpret_cast<VfsRdOnly *
>(vfs->pAppData);
503 sqlite3_vfs *vfs = sqlite3_vfs_find(
kVfsName);
506 static_cast<VfsRdOnly *
>(vfs->pAppData)->cache_mgr = new_cache_mgr;
static int VfsRdOnlySync(sqlite3_file *pFile __attribute__((unused)), int flags __attribute__((unused)))
void Dec(class Counter *counter)
Counter * Register(const std::string &name, const std::string &desc)
void RegisterFdMapping(int from, int to)
std::vector< int > * fd_from_
virtual int64_t GetSize(int fd)=0
int64_t Xadd(class Counter *counter, const int64_t delta)
static int VfsRdOnlyOpen(sqlite3_vfs *vfs, const char *zName, sqlite3_file *pFile, int flags, int *pOutFlags)
static int VfsRdOnlySleep(sqlite3_vfs *vfs, int microseconds)
static void ApplyFdMap(VfsRdOnlyFile *pFile)
static int VfsRdOnlyTruncate(sqlite3_file *pFile __attribute__((unused)), sqlite_int64 size __attribute__((unused)))
static int VfsRdOnlyDeviceCharacteristics(sqlite3_file *p __attribute__((unused)))
assert((mem||(size==0))&&"Out Of Memory")
void ReplaceCacheManager(CacheManager *new_cache_mgr)
bool UnregisterVfsRdOnly()
struct cvmcache_object_info __attribute__
static int VfsRdOnlyWrite(sqlite3_file *pFile __attribute__((unused)), const void *zBuf __attribute__((unused)), int iAmt __attribute__((unused)), sqlite_int64 iOfst __attribute__((unused)))
int64_t String2Int64(const string &value)
static int VfsRdOnlyFileControl(sqlite3_file *p __attribute__((unused)), int op __attribute__((unused)), void *pArg __attribute__((unused)))
std::vector< int > * fd_to_
static int VfsRdOnlyRead(sqlite3_file *pFile, void *zBuf, int iAmt, sqlite_int64 iOfst)
bool RegisterVfsRdOnly(CacheManager *cache_mgr, perf::Statistics *statistics, const VfsOptions options)
static int VfsRdOnlyDelete(sqlite3_vfs *__attribute__((unused)), const char *zName __attribute__((unused)), int syncDir __attribute__((unused)))
bool HasSuffix(const std::string &str, const std::string &suffix, const bool ignore_case)
static int VfsRdOnlyUnlock(sqlite3_file *p __attribute__((unused)), int level __attribute__((unused)))
int VfsRdOnlyFullPathname(sqlite3_vfs *vfs __attribute__((unused)), const char *zPath, int nOut, char *zOut)
static int VfsRdOnlyCheckReservedLock(sqlite3_file *p __attribute__((unused)), int *pResOut)
static int VfsRdOnlyLock(sqlite3_file *p __attribute__((unused)), int level __attribute__((unused)))
static int VfsRdOnlyClose(sqlite3_file *pFile)
static int VfsRdOnlyRandomness(sqlite3_vfs *vfs, int nBuf, char *zBuf)
static int VfsRdOnlySectorSize(sqlite3_file *p __attribute__((unused)))
void Inc(class Counter *counter)
virtual int Close(int fd)=0
static int VfsRdOnlyGetLastError(sqlite3_vfs *vfs __attribute__((unused)), int not_used1 __attribute__((unused)), char *not_used2 __attribute__((unused)))
static int VfsRdOnlyFileSize(sqlite3_file *pFile, sqlite_int64 *pSize)
static int VfsRdOnlyCurrentTimeInt64(sqlite3_vfs *vfs, sqlite3_int64 *piNow)
static int VfsRdOnlyCurrentTime(sqlite3_vfs *vfs, double *prNow)
virtual int Readahead(int fd)=0
static int VfsRdOnlyAccess(sqlite3_vfs *vfs, const char *zPath, int flags, int *pResOut)
CVMFS_EXPORT void LogCvmfs(const LogSource source, const int mask, const char *format,...)