10 #ifndef __STDC_FORMAT_MACROS
11 #define __STDC_FORMAT_MACROS
22 #include <sys/select.h>
24 #include <sys/types.h>
98 for (
unsigned i = 0; i < N; ++i) {
102 pFile->fd = (*fd_to_)[i];
112 VfsRdOnlyFile *p =
reinterpret_cast<VfsRdOnlyFile *
>(pFile);
114 int retval = p->vfs_rdonly->cache_mgr->Close(p->fd);
119 return SQLITE_IOERR_CLOSE;
133 VfsRdOnlyFile *p =
reinterpret_cast<VfsRdOnlyFile *
>(pFile);
135 ssize_t got = p->vfs_rdonly->cache_mgr->Pread(p->fd, zBuf, iAmt, iOfst);
140 }
else if (got < 0) {
141 return SQLITE_IOERR_READ;
144 memset(reinterpret_cast<char *>(zBuf) + got, 0, iAmt - got);
145 return SQLITE_IOERR_SHORT_READ;
156 return SQLITE_READONLY;
164 return SQLITE_READONLY;
177 VfsRdOnlyFile *p =
reinterpret_cast<VfsRdOnlyFile *
>(pFile);
216 return SQLITE_NOTFOUND;
249 static const sqlite3_io_methods io_methods = {
265 VfsRdOnlyFile *p =
reinterpret_cast<VfsRdOnlyFile *
>(pFile);
267 reinterpret_cast<VfsRdOnly *
>(vfs->pAppData)->cache_mgr;
269 p->base.pMethods = NULL;
271 if (flags & SQLITE_OPEN_READWRITE)
273 if (flags & SQLITE_OPEN_DELETEONCLOSE)
275 if (flags & SQLITE_OPEN_EXCLUSIVE)
278 assert(zName && (zName[0] ==
'@'));
284 cache_mgr->
Close(p->fd);
286 return SQLITE_IOERR_FSTAT;
289 cache_mgr->
Close(p->fd);
293 p->size =
static_cast<uint64_t
>(
size);
296 p->vfs_rdonly =
reinterpret_cast<VfsRdOnly *
>(vfs->pAppData);
297 p->base.pMethods = &io_methods;
310 return SQLITE_IOERR_DELETE;
323 if (flags == SQLITE_ACCESS_READWRITE) {
334 perf::Inc(reinterpret_cast<VfsRdOnly *>(vfs->pAppData)->n_access);
349 sqlite3_snprintf(nOut, zOut,
"%s", zPath);
362 assert(static_cast<size_t>(nBuf) >= (
sizeof(time_t) +
sizeof(
int)));
363 perf::Inc(reinterpret_cast<VfsRdOnly *>(vfs->pAppData)->n_rand);
364 memset(zBuf, 0, nBuf);
365 pid_t randomnessPid = getpid();
367 fd = open(
"/dev/urandom", O_RDONLY, 0);
371 memcpy(zBuf, &t,
sizeof(t));
372 memcpy(&zBuf[
sizeof(t)], &randomnessPid,
sizeof(randomnessPid));
373 assert(
sizeof(t) +
sizeof(randomnessPid) <= static_cast<size_t>(nBuf));
374 nBuf =
sizeof(t) +
sizeof(randomnessPid);
377 got = read(fd, zBuf, nBuf);
378 }
while (got < 0 && errno == EINTR);
381 perf::Xadd(reinterpret_cast<VfsRdOnly *>(vfs->pAppData)->sz_rand, nBuf);
393 struct timeval wait_for;
394 wait_for.tv_sec = microseconds / (1000*1000);
395 wait_for.tv_usec = microseconds % (1000 * 1000);
396 select(0, NULL, NULL, NULL, &wait_for);
397 perf::Inc(reinterpret_cast<VfsRdOnly *>(vfs->pAppData)->n_sleep);
398 perf::Xadd(reinterpret_cast<VfsRdOnly *>(vfs->pAppData)->sz_sleep,
409 sqlite3_int64 *piNow)
411 static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
414 if (gettimeofday(&sNow, 0) == 0) {
415 *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;
416 perf::Inc(reinterpret_cast<VfsRdOnly *>(vfs->pAppData)->n_time);
433 *prNow = i/86400000.0;
459 fd_to_ =
new std::vector<int>();
461 sqlite3_vfs *vfs =
reinterpret_cast<sqlite3_vfs *
>(
462 smalloc(
sizeof(sqlite3_vfs)));
463 memset(vfs, 0,
sizeof(sqlite3_vfs));
464 VfsRdOnly *vfs_rdonly =
new VfsRdOnly();
467 vfs->szOsFile =
sizeof(VfsRdOnlyFile);
468 vfs->mxPathname = PATH_MAX;
470 vfs->pAppData = vfs_rdonly;
476 vfs->xDlError = NULL;
478 vfs->xDlClose = NULL;
486 int retval = sqlite3_vfs_register(vfs, options ==
kVfsOptDefault);
487 if (retval != SQLITE_OK) {
488 free(const_cast<char *>(vfs->zName));
494 vfs_rdonly->cache_mgr = cache_mgr;
495 vfs_rdonly->n_access =
496 statistics->
Register(
"sqlite.n_access",
"overall number of access() calls");
497 vfs_rdonly->no_open =
498 statistics->
Register(
"sqlite.no_open",
"currently open sqlite files");
500 statistics->
Register(
"sqlite.n_rand",
"overall number of random() calls");
501 vfs_rdonly->sz_rand =
502 statistics->
Register(
"sqlite.sz_rand",
"overall number of random bytes");
504 statistics->
Register(
"sqlite.n_read",
"overall number of read() calls");
505 vfs_rdonly->sz_read =
506 statistics->
Register(
"sqlite.sz_read",
"overall bytes read()");
507 vfs_rdonly->n_sleep =
508 statistics->
Register(
"sqlite.n_sleep",
"overall number of sleep() calls");
509 vfs_rdonly->sz_sleep =
510 statistics->
Register(
"sqlite.sz_sleep",
"overall microseconds slept");
512 statistics->
Register(
"sqlite.n_time",
"overall number of time() calls");
523 sqlite3_vfs *vfs = sqlite3_vfs_find(
kVfsName);
526 int retval = sqlite3_vfs_unregister(vfs);
527 if (retval != SQLITE_OK)
530 delete reinterpret_cast<VfsRdOnly *
>(vfs->pAppData);
547 sqlite3_vfs *vfs = sqlite3_vfs_find(
kVfsName);
550 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,...)