LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
tkeyvfs.cc File Reference
#include <sqlite3.h>
#include "TFile.h"
#include "TKey.h"
#include <cassert>
#include <cerrno>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <dlfcn.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>

Go to the source code of this file.

Classes

struct  unixFile
 
struct  unix_syscall
 
class  RootFileSentry
 

Macros

#define SQLITE_OPEN_WAL   0x00080000 /* VFS only */
 
#define SQLITE_FCNTL_SIZE_HINT   5
 
#define SQLITE_FCNTL_CHUNK_SIZE   6
 
#define SQLITE_FCNTL_SYNC_OMITTED   8
 
#define SQLITE_FCNTL_DB_UNCHANGED   0xca093fa0
 
#define SQLITE_DEFAULT_SECTOR_SIZE   512
 
#define UNUSED_PARAMETER(x)   (void)(x)
 
#define UNUSED_PARAMETER2(x, y)   UNUSED_PARAMETER(x), UNUSED_PARAMETER(y)
 
#define ArraySize(X)   ((int)(sizeof(X) / sizeof(X[0])))
 
#define _LARGE_FILE   1
 
#define _FILE_OFFSET_BITS   64
 
#define _LARGEFILE_SOURCE   1
 
#define TKEYVFS_TRACE   0
 
#define MEMPAGE   2048
 
#define MAX_PATHNAME   512
 
#define SQLITE_TEMP_FILE_PREFIX   "etilqs_"
 
#define O_LARGEFILE   0
 
#define O_NOFOLLOW   0
 
#define O_BINARY   0
 
#define SQLITE_CANTOPEN_BKPT   sqlite3CantopenError(__LINE__)
 
#define osOpen   ((int (*)(const char*, int, ...))aSyscall[0].pCurrent)
 
#define osClose   ((int (*)(int))aSyscall[1].pCurrent)
 
#define osAccess   ((int (*)(const char*, int))aSyscall[2].pCurrent)
 
#define osGetcwd   ((char* (*)(char*, size_t))aSyscall[3].pCurrent)
 
#define osStat   ((int (*)(const char*, struct stat*))aSyscall[4].pCurrent)
 
#define osFstat   ((int (*)(int, struct stat*))aSyscall[5].pCurrent)
 
#define osFtruncate   ((int (*)(int, off_t))aSyscall[6].pCurrent)
 
#define osFcntl   ((int (*)(int, int, ...))aSyscall[7].pCurrent)
 
#define osRead   ((ssize_t(*)(int, void*, size_t))aSyscall[8].pCurrent)
 
#define osPread   ((ssize_t(*)(int, void*, size_t, off_t))aSyscall[9].pCurrent)
 
#define osPread64   ((ssize_t(*)(int, void*, size_t, off_t))aSyscall[10].pCurrent)
 
#define osWrite   ((ssize_t(*)(int, const void*, size_t))aSyscall[11].pCurrent)
 
#define osPwrite   ((ssize_t(*)(int, const void*, size_t, off_t))aSyscall[12].pCurrent)
 
#define osPwrite64   ((ssize_t(*)(int, const void*, size_t, off_t))aSyscall[13].pCurrent)
 
#define osFchmod   ((int (*)(int, mode_t))aSyscall[14].pCurrent)
 
#define osFallocate   ((int (*)(int, off_t, off_t))aSyscall[15].pCurrent)
 
#define unixLogError(a, b, c)   unixLogErrorAtLine(a, b, c, __LINE__)
 
#define UNIXVFS(VFSNAME, FINDER)
 

Typedefs

typedef sqlite_int64 i64
 
typedef struct unixFile unixFile
 
typedef const sqlite3_io_methods *(* finder_type) (const char *, unixFile *)
 

Functions

static int sqlite3CantopenError (int lineno)
 
static int sqlite3Strlen30 (const char *z)
 
static const sqlite3_io_methods * nolockIoFinderImpl (const char *z, unixFile *p)
 
static int unixLogErrorAtLine (int errcode, const char *zFunc, const char *zPath, int iLine)
 
static int robust_open (const char *z, int f, int m)
 
static void robust_close (unixFile *pFile, int h, int lineno)
 
static int unixGetTempname (int nBuf, char *zBuf)
 
static int fcntlSizeHint (unixFile *pFile, i64 nByte)
 
static int seekAndRead (unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt)
 
static int seekAndWrite (unixFile *id, i64 offset, const void *pBuf, int cnt)
 
static int nolockClose (sqlite3_file *id)
 
static int unixRead (sqlite3_file *id, void *pBuf, int amt, sqlite3_int64 offset)
 
static int unixWrite (sqlite3_file *id, const void *pBuf, int amt, sqlite3_int64 offset)
 
static int unixTruncate (sqlite3_file *id, i64 nByte)
 
static int unixSync (sqlite3_file *id, int flags)
 
static int unixFileSize (sqlite3_file *id, i64 *pSize)
 
static int nolockLock (sqlite3_file *NotUsed, int NotUsed2)
 
static int nolockUnlock (sqlite3_file *NotUsed, int NotUsed2)
 
static int nolockCheckReservedLock (sqlite3_file *NotUsed, int *pResOut)
 
static int unixFileControl (sqlite3_file *id, int op, void *pArg)
 
static int unixSectorSize (sqlite3_file *NotUsed)
 
static int unixDeviceCharacteristics (sqlite3_file *NotUsed)
 
static int unixOpen (sqlite3_vfs *pVfs, const char *zPath, sqlite3_file *pFile, int flags, int *pOutFlags)
 
static int unixDelete (sqlite3_vfs *NotUsed, const char *zPath, int dirSync)
 
static int unixAccess (sqlite3_vfs *NotUsed, const char *zPath, int flags, int *pResOut)
 
static int unixFullPathname (sqlite3_vfs *pVfs, const char *zPath, int nOut, char *zOut)
 
static void * unixDlOpen (sqlite3_vfs *NotUsed, const char *zFilename)
 
static void unixDlError (sqlite3_vfs *NotUsed, int nBuf, char *zBufOut)
 
static void unixDlClose (sqlite3_vfs *NotUsed, void *pHandle)
 
static int unixRandomness (sqlite3_vfs *NotUsed, int nBuf, char *zBuf)
 
static int unixSleep (sqlite3_vfs *NotUsed, int microseconds)
 
static int unixCurrentTime (sqlite3_vfs *NotUsed, double *prNow)
 
static int unixGetLastError (sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3)
 
static int unixCurrentTimeInt64 (sqlite3_vfs *NotUsed, sqlite3_int64 *piNow)
 
static int unixSetSystemCall (sqlite3_vfs *pNotUsed, const char *zName, sqlite3_syscall_ptr pNewFunc)
 
static sqlite3_syscall_ptr unixGetSystemCall (sqlite3_vfs *pNotUsed, const char *zName)
 
static const char * unixNextSystemCall (sqlite3_vfs *p, const char *zName)
 
static int closeUnixFile (sqlite3_file *id)
 
int tkeyvfs_init (void)
 
int tkeyvfs_open_v2 (const char *filename, sqlite3 **ppDb, int flags, TFile *rootFile )
 

Variables

static TFile * gRootFile
 
static void(*)(void) unixDlSym (sqlite3_vfs *NotUsed, void *p, const char *zSym)
 
static struct unix_syscall aSyscall []
 
static const sqlite3_io_methods nolockIoMethods
 
static const sqlite3_io_methods *(*const nolockIoFinder )(const char *, unixFile *p)
 

Macro Definition Documentation

#define _FILE_OFFSET_BITS   64

Definition at line 37 of file tkeyvfs.cc.

#define _LARGE_FILE   1

Definition at line 36 of file tkeyvfs.cc.

#define _LARGEFILE_SOURCE   1

Definition at line 38 of file tkeyvfs.cc.

#define ArraySize (   X)    ((int)(sizeof(X) / sizeof(X[0])))

Definition at line 32 of file tkeyvfs.cc.

Referenced by tkeyvfs_init(), and unixNextSystemCall().

#define MAX_PATHNAME   512

Definition at line 88 of file tkeyvfs.cc.

Referenced by unixFullPathname(), and unixOpen().

#define MEMPAGE   2048

Definition at line 83 of file tkeyvfs.cc.

Referenced by fcntlSizeHint(), seekAndWrite(), unixOpen(), and unixTruncate().

#define O_BINARY   0

Definition at line 137 of file tkeyvfs.cc.

#define O_LARGEFILE   0

Definition at line 127 of file tkeyvfs.cc.

#define O_NOFOLLOW   0

Definition at line 134 of file tkeyvfs.cc.

#define osAccess   ((int (*)(const char*, int))aSyscall[2].pCurrent)
#define osClose   ((int (*)(int))aSyscall[1].pCurrent)

Referenced by robust_close().

#define osFallocate   ((int (*)(int, off_t, off_t))aSyscall[15].pCurrent)
#define osFchmod   ((int (*)(int, mode_t))aSyscall[14].pCurrent)
#define osFcntl   ((int (*)(int, int, ...))aSyscall[7].pCurrent)
#define osFstat   ((int (*)(int, struct stat*))aSyscall[5].pCurrent)
#define osFtruncate   ((int (*)(int, off_t))aSyscall[6].pCurrent)
#define osGetcwd   ((char* (*)(char*, size_t))aSyscall[3].pCurrent)
#define osOpen   ((int (*)(const char*, int, ...))aSyscall[0].pCurrent)

Referenced by robust_open().

#define osPread   ((ssize_t(*)(int, void*, size_t, off_t))aSyscall[9].pCurrent)
#define osPread64   ((ssize_t(*)(int, void*, size_t, off_t))aSyscall[10].pCurrent)
#define osPwrite   ((ssize_t(*)(int, const void*, size_t, off_t))aSyscall[12].pCurrent)
#define osPwrite64   ((ssize_t(*)(int, const void*, size_t, off_t))aSyscall[13].pCurrent)
#define osRead   ((ssize_t(*)(int, void*, size_t))aSyscall[8].pCurrent)

Referenced by unixRandomness().

#define osStat   ((int (*)(const char*, struct stat*))aSyscall[4].pCurrent)
#define osWrite   ((ssize_t(*)(int, const void*, size_t))aSyscall[11].pCurrent)
#define SQLITE_CANTOPEN_BKPT   sqlite3CantopenError(__LINE__)

Definition at line 217 of file tkeyvfs.cc.

Referenced by unixOpen().

#define SQLITE_DEFAULT_SECTOR_SIZE   512

Definition at line 27 of file tkeyvfs.cc.

Referenced by unixSectorSize().

#define SQLITE_FCNTL_CHUNK_SIZE   6

Definition at line 23 of file tkeyvfs.cc.

Referenced by unixFileControl().

#define SQLITE_FCNTL_DB_UNCHANGED   0xca093fa0

Definition at line 26 of file tkeyvfs.cc.

Referenced by unixFileControl().

#define SQLITE_FCNTL_SIZE_HINT   5

Definition at line 22 of file tkeyvfs.cc.

Referenced by unixFileControl().

#define SQLITE_FCNTL_SYNC_OMITTED   8

Definition at line 24 of file tkeyvfs.cc.

Referenced by unixFileControl().

#define SQLITE_OPEN_WAL   0x00080000 /* VFS only */

Definition at line 21 of file tkeyvfs.cc.

#define SQLITE_TEMP_FILE_PREFIX   "etilqs_"

Definition at line 90 of file tkeyvfs.cc.

Referenced by unixGetTempname().

#define TKEYVFS_TRACE   0

Definition at line 69 of file tkeyvfs.cc.

#define unixLogError (   a,
  b,
 
)    unixLogErrorAtLine(a, b, c, __LINE__)

Definition at line 386 of file tkeyvfs.cc.

Referenced by unixOpen(), and unixTruncate().

#define UNIXVFS (   VFSNAME,
  FINDER 
)
Value:
{ \
1, /* iVersion */ \
sizeof(unixFile), /* szOsFile */ \
MAX_PATHNAME, /* mxPathname */ \
0, /* pNext */ \
VFSNAME, /* zName */ \
(void*)&FINDER, /* pAppData */ \
unixOpen, /* xOpen */ \
unixDelete, /* xDelete */ \
unixAccess, /* xAccess */ \
unixFullPathname, /* xFullPathname */ \
unixDlOpen, /* xDlOpen */ \
unixDlError, /* xDlError */ \
unixDlSym, /* xDlSym */ \
unixDlClose, /* xDlClose */ \
unixRandomness, /* xRandomness */ \
unixSleep, /* xSleep */ \
unixCurrentTime, /* xCurrentTime */ \
unixGetLastError, /* xGetLastError */ \
/* unixCurrentTimeInt64, v2, xCurrentTimeInt64 */ \
/* unixSetSystemCall, v3, xSetSystemCall */ \
/* unixGetSystemCall, v3, xGetSystemCall */ \
/* unixNextSystemCall, v3, xNextSystemCall */ \
}
static void * unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename)
Definition: tkeyvfs.cc:1414
static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow)
Definition: tkeyvfs.cc:1570
static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3)
Definition: tkeyvfs.cc:1593
static void(*)(void) unixDlSym(sqlite3_vfs *NotUsed, void *p, const char *zSym)
Definition: tkeyvfs.cc:194
static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut)
Definition: tkeyvfs.cc:1435
static int unixOpen(sqlite3_vfs *pVfs, const char *zPath, sqlite3_file *pFile, int flags, int *pOutFlags)
Definition: tkeyvfs.cc:1171
static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf)
Definition: tkeyvfs.cc:1498
static int unixFullPathname(sqlite3_vfs *pVfs, const char *zPath, int nOut, char *zOut)
Definition: tkeyvfs.cc:1386
static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle)
Definition: tkeyvfs.cc:1481
static int unixAccess(sqlite3_vfs *NotUsed, const char *zPath, int flags, int *pResOut)
Definition: tkeyvfs.cc:1330
static int unixDelete(sqlite3_vfs *NotUsed, const char *zPath, int dirSync)
Definition: tkeyvfs.cc:1300
static int unixSleep(sqlite3_vfs *NotUsed, int microseconds)
Definition: tkeyvfs.cc:1551

Referenced by tkeyvfs_init().

#define UNUSED_PARAMETER2 (   x,
  y 
)    UNUSED_PARAMETER(x), UNUSED_PARAMETER(y)

Definition at line 30 of file tkeyvfs.cc.

Referenced by unixSync().

Typedef Documentation

typedef const sqlite3_io_methods*(* finder_type) (const char *, unixFile *)

Definition at line 368 of file tkeyvfs.cc.

typedef sqlite_int64 i64

Definition at line 34 of file tkeyvfs.cc.

typedef struct unixFile unixFile

Definition at line 120 of file tkeyvfs.cc.

Function Documentation

static int closeUnixFile ( sqlite3_file *  id)
static

Definition at line 473 of file tkeyvfs.cc.

References unixFile::fileSize, unixFile::pBuf, unixFile::rootFile, unixFile::saveToRootFile, and unixFile::zPath.

Referenced by nolockClose().

474 {
475  unixFile* pFile = (unixFile*)id;
476 #if TKEYVFS_TRACE
477  fprintf(stderr, "Begin closeUnixFile ...\n");
478  if (((unixFile*)id)->zPath) {
479  fprintf(stderr, "filename: %s\n", ((unixFile*)id)->zPath);
480  }
481 #ifndef TKEYVFS_NO_ROOT
482  fprintf(stderr, "saveToRootFile: %d\n", ((unixFile*)id)->saveToRootFile);
483 #endif // TKEYVFS_NO_ROOT
484 #endif /* TKEYVFS_TRACE */
485 #ifndef TKEYVFS_NO_ROOT
486  if (pFile->saveToRootFile) {
487 
488 #if TKEYVFS_TRACE
489  fprintf(
490  stderr, "fileSize: 0x%016lx\n", (unsigned long long)pFile->fileSize);
491 #endif /* TKEYVFS_TRACE */
492  /* Create a tkey which will contain the contents
493  ** of the database in the root file */
494  TKey* k = new TKey(pFile->zPath,
495  "sqlite3 database file",
496  TKey::Class(),
497  pFile->fileSize /*nbytes*/,
498  pFile->rootFile /*dir*/);
499 #if TKEYVFS_TRACE
500  /* Ask the key for the size of the database file it contains. */
501  Int_t objlen = k->GetObjlen();
502  fprintf(stderr, "objlen: %d\n", objlen);
503 #endif /* TKEYVFS_TRACE */
504  /* Add the new key to the root file toplevel directory. */
505  /* Note: The tkey is now owned by the root file. */
506  Int_t cycle = pFile->rootFile->AppendKey(k);
507  /* Get a pointer to the i/o buffer inside the tkey. */
508  char* p = k->GetBuffer();
509  /* Copy the entire in-memory database file into the tkey i/o buffer. */
510  (void*)memcpy((void*)p, (void*)pFile->pBuf, (size_t)pFile->fileSize);
511  /* Write the tkey contents to the root file. */
512  /* Note: This has not yet written the top-level directory entry for the key.
513  */
514  Int_t cnt = k->WriteFile(cycle, 0 /*file*/);
515  if (cnt == -1) {
516  /* bad */
517  fprintf(stderr,
518  "tkeyvfs: failed to write root tkey containing database to root "
519  "file!\n");
520  }
521  /* Force the root file to flush the top-level directory entry for our tkey
522  * to disk. */
523  cnt = pFile->rootFile->Write("", TObject::kOverwrite);
524  if (cnt < 0) {
525  /* bad */
526  fprintf(stderr, "tkeyvfs: failed to write root file to disk!\n");
527  }
528  }
529 #endif // TKEYVFS_NO_ROOT
530  if (pFile->pBuf != nullptr) {
531  free(pFile->pBuf);
532  }
533  if (pFile->zPath != nullptr) {
534  free((void*)pFile->zPath);
535  }
536  memset(pFile, 0, sizeof(unixFile));
537 #if TKEYVFS_TRACE
538  fprintf(stderr, "End closeUnixFile ...\n");
539 #endif /* TKEYVFS_TRACE */
540  return SQLITE_OK;
541 }
const char * zPath
Definition: tkeyvfs.cc:107
int saveToRootFile
Definition: tkeyvfs.cc:100
i64 fileSize
Definition: tkeyvfs.cc:104
char * pBuf
Definition: tkeyvfs.cc:102
TFile * rootFile
Definition: tkeyvfs.cc:99
static int fcntlSizeHint ( unixFile pFile,
i64  nByte 
)
static

Definition at line 590 of file tkeyvfs.cc.

References unixFile::bufAllocated, unixFile::fileSize, MEMPAGE, unixFile::pBuf, and unixFile::szChunk.

Referenced by unixFileControl().

591 {
592 
593 #if TKEYVFS_TRACE
594  fprintf(stderr, "Begin fcntlSizeHint ...\n");
595 #endif /* TKEYVFS_TRACE */
596  if (pFile->szChunk) {
597  i64 nSize; /* Required file size */
598  i64 nAlloc;
599  nSize = ((nByte + (pFile->szChunk - 1)) / pFile->szChunk) * pFile->szChunk;
600  nAlloc = ((nSize + ((i64)(MEMPAGE - 1))) / ((i64)MEMPAGE)) * ((i64)MEMPAGE);
601  if ((nSize > pFile->fileSize) && (nAlloc > pFile->bufAllocated)) {
602  if (nAlloc > pFile->bufAllocated) {
603  char* pNewBuf = (char*)realloc((void*)pFile->pBuf, (size_t)nAlloc);
604  if (pNewBuf == nullptr) {
605 
606 #if TKEYVFS_TRACE
607  fprintf(stderr, "End fcntlSizeHint ...\n");
608 #endif /* TKEYVFS_TRACE */
609  return SQLITE_IOERR_WRITE;
610  }
611  (void)memset(
612  pNewBuf + pFile->fileSize, 0, (size_t)(nAlloc - pFile->fileSize));
613  pFile->pBuf = pNewBuf;
614  pFile->bufAllocated = nAlloc;
615  } else {
616  (void)memset(
617  pFile->pBuf + pFile->fileSize, 0, (size_t)(nSize - pFile->fileSize));
618  }
619  pFile->fileSize = nSize;
620  }
621  }
622 #if TKEYVFS_TRACE
623  fprintf(stderr, "End fcntlSizeHint ...\n");
624 #endif /* TKEYVFS_TRACE */
625  return SQLITE_OK;
626 }
sqlite_int64 i64
Definition: tkeyvfs.cc:34
#define MEMPAGE
Definition: tkeyvfs.cc:83
i64 bufAllocated
Definition: tkeyvfs.cc:103
int szChunk
Definition: tkeyvfs.cc:108
i64 fileSize
Definition: tkeyvfs.cc:104
char * pBuf
Definition: tkeyvfs.cc:102
static int nolockCheckReservedLock ( sqlite3_file *  NotUsed,
int *  pResOut 
)
static

Definition at line 998 of file tkeyvfs.cc.

References unixFile::zPath.

999 {
1000 /*UNUSED_PARAMETER(NotUsed);*/
1001 #if TKEYVFS_TRACE
1002  fprintf(stderr, "Begin nolockCheckReservedLock ...\n");
1003  if (((unixFile*)id)->zPath) {
1004  fprintf(stderr, "filename: %s\n", ((unixFile*)id)->zPath);
1005  }
1006 #endif /* TKEYVFS_TRACE */
1007  *pResOut = 0;
1008 #if TKEYVFS_TRACE
1009  fprintf(stderr, "End nolockCheckReservedLock ...\n");
1010 #endif /* TKEYVFS_TRACE */
1011  return SQLITE_OK;
1012 }
static int nolockClose ( sqlite3_file *  id)
static

Definition at line 718 of file tkeyvfs.cc.

References closeUnixFile().

719 {
720 
721 #if TKEYVFS_TRACE
722  fprintf(stderr, "Begin nolockClose ...\n");
723 #endif /* TKEYVFS_TRACE */
724  int val = closeUnixFile(id);
725 #if TKEYVFS_TRACE
726  fprintf(stderr, "End nolockClose ...\n");
727 #endif /* TKEYVFS_TRACE */
728  return val;
729 }
static int closeUnixFile(sqlite3_file *id)
Definition: tkeyvfs.cc:473
static const sqlite3_io_methods * nolockIoFinderImpl ( const char *  z,
unixFile p 
)
static

Definition at line 357 of file tkeyvfs.cc.

References nolockIoMethods, and UNUSED_PARAMETER.

358 {
360  UNUSED_PARAMETER(p);
361  return &nolockIoMethods;
362 }
Double_t z
Definition: plot.C:279
#define UNUSED_PARAMETER(x)
Definition: tkeyvfs.cc:29
static const sqlite3_io_methods nolockIoMethods
Definition: tkeyvfs.cc:334
static int nolockLock ( sqlite3_file *  NotUsed,
int  NotUsed2 
)
static

Definition at line 964 of file tkeyvfs.cc.

References UNUSED_PARAMETER, and unixFile::zPath.

965 {
966  /*UNUSED_PARAMETER2(NotUsed, NotUsed2);*/
967  UNUSED_PARAMETER(NotUsed2);
968 #if TKEYVFS_TRACE
969  fprintf(stderr, "Begin nolockLock ...\n");
970  if (((unixFile*)id)->zPath) {
971  fprintf(stderr, "filename: %s\n", ((unixFile*)id)->zPath);
972  }
973 #endif /* TKEYVFS_TRACE */
974 #if TKEYVFS_TRACE
975  fprintf(stderr, "End nolockLock ...\n");
976 #endif /* TKEYVFS_TRACE */
977  return SQLITE_OK;
978 }
#define UNUSED_PARAMETER(x)
Definition: tkeyvfs.cc:29
static int nolockUnlock ( sqlite3_file *  NotUsed,
int  NotUsed2 
)
static

Definition at line 981 of file tkeyvfs.cc.

References UNUSED_PARAMETER, and unixFile::zPath.

982 {
983  /*UNUSED_PARAMETER2(NotUsed, NotUsed2);*/
984  UNUSED_PARAMETER(NotUsed2);
985 #if TKEYVFS_TRACE
986  fprintf(stderr, "Begin nolockUnlock ...\n");
987  if (((unixFile*)id)->zPath) {
988  fprintf(stderr, "filename: %s\n", ((unixFile*)id)->zPath);
989  }
990 #endif /* TKEYVFS_TRACE */
991 #if TKEYVFS_TRACE
992  fprintf(stderr, "End nolockUnlock ...\n");
993 #endif /* TKEYVFS_TRACE */
994  return SQLITE_OK;
995 }
#define UNUSED_PARAMETER(x)
Definition: tkeyvfs.cc:29
static void robust_close ( unixFile pFile,
int  h,
int  lineno 
)
static

Definition at line 443 of file tkeyvfs.cc.

References osClose, unixLogErrorAtLine(), and unixFile::zPath.

Referenced by unixRandomness().

444 {
445 
446 #if TKEYVFS_TRACE
447  fprintf(stderr, "Begin robust_close ...\n");
448 #endif /* TKEYVFS_TRACE */
449  if (osClose(h)) {
450  if (pFile) {
451  unixLogErrorAtLine(SQLITE_IOERR_CLOSE, "close", pFile->zPath, lineno);
452  } else {
453  unixLogErrorAtLine(SQLITE_IOERR_CLOSE, "close", 0, lineno);
454  }
455  }
456 #if TKEYVFS_TRACE
457  fprintf(stderr, "End robust_close ...\n");
458 #endif /* TKEYVFS_TRACE */
459 
460 }
const char * zPath
Definition: tkeyvfs.cc:107
#define osClose
static int unixLogErrorAtLine(int errcode, const char *zFunc, const char *zPath, int iLine)
Definition: tkeyvfs.cc:388
static int robust_open ( const char *  z,
int  f,
int  m 
)
static

Definition at line 414 of file tkeyvfs.cc.

References osOpen.

Referenced by unixRandomness().

415 {
416  int rc;
417 #if TKEYVFS_TRACE
418  fprintf(stderr, "Begin robust_open ...\n");
419 #endif /* TKEYVFS_TRACE */
420  do {
421  rc = osOpen(z, f, m);
422  } while (rc < 0 && errno == EINTR);
423 #if TKEYVFS_TRACE
424  fprintf(stderr, "End robust_open ...\n");
425 #endif /* TKEYVFS_TRACE */
426  return rc;
427 }
Double_t z
Definition: plot.C:279
TFile f
Definition: plotHisto.C:6
#define osOpen
static int seekAndRead ( unixFile id,
sqlite3_int64  offset,
void *  pBuf,
int  cnt 
)
static

Definition at line 642 of file tkeyvfs.cc.

References unixFile::fileSize.

Referenced by unixRead(), and unixWrite().

643 {
644 
645 #if TKEYVFS_TRACE
646  fprintf(stderr, "Begin seekAndRead ...\n");
647 #endif /* TKEYVFS_TRACE */
648  if (offset >= id->fileSize) {
649  id->lastErrno = 0;
650 #if TKEYVFS_TRACE
651  fprintf(stderr, "End seekAndRead ...\n");
652 #endif /* TKEYVFS_TRACE */
653  return 0;
654  }
655  if ((offset + cnt) > id->fileSize) {
656  cnt = (offset + cnt) - id->fileSize;
657  }
658  (void*)memcpy(pBuf, (const void*)(id->pBuf + offset), (size_t)cnt);
659 #if TKEYVFS_TRACE
660  fprintf(stderr, "End seekAndRead ...\n");
661 #endif /* TKEYVFS_TRACE */
662  return cnt;
663 }
i64 fileSize
Definition: tkeyvfs.cc:104
static int seekAndWrite ( unixFile id,
i64  offset,
const void *  pBuf,
int  cnt 
)
static

Definition at line 673 of file tkeyvfs.cc.

References unixFile::bufAllocated, unixFile::fileSize, MEMPAGE, and unixFile::szChunk.

Referenced by unixWrite().

674 {
675  unixFile* pFile = (unixFile*)id;
676 #if TKEYVFS_TRACE
677  fprintf(stderr, "Begin seekAndWrite ...\n");
678 #endif /* TKEYVFS_TRACE */
679  if ((offset + (i64)cnt) > id->bufAllocated) {
680  i64 nByte;
681  i64 newBufSize;
682  nByte = offset + ((i64)cnt);
683  if (pFile->szChunk) {
684  nByte =
685  ((nByte + (pFile->szChunk - 1)) / pFile->szChunk) * pFile->szChunk;
686  }
687  newBufSize =
688  ((nByte + (i64)(MEMPAGE - 1)) / ((i64)MEMPAGE)) * ((i64)MEMPAGE);
689  char* pNewBuf = (char*)realloc((void*)id->pBuf, (size_t)(newBufSize));
690  if (pNewBuf == nullptr) {
691  id->lastErrno = errno;
692 #if TKEYVFS_TRACE
693  fprintf(stderr, "End seekAndWrite ...\n");
694 #endif /* TKEYVFS_TRACE */
695  return 0;
696  }
697  if ((offset + (i64)cnt) < newBufSize) {
698  i64 zeroCnt = newBufSize - (offset + (i64)cnt);
699  (void*)memset((void*)(pNewBuf + offset + (i64)cnt), 0, (size_t)zeroCnt);
700  }
701  id->pBuf = pNewBuf;
702  id->bufAllocated = newBufSize;
703  }
704  (void*)memcpy((void*)(id->pBuf + offset), pBuf, (size_t)cnt);
705  if ((offset + (i64)cnt) > id->fileSize) {
706  id->fileSize = offset + (i64)cnt;
707  }
708 #if TKEYVFS_TRACE
709  fprintf(stderr, "End seekAndWrite ...\n");
710 #endif /* TKEYVFS_TRACE */
711  return cnt;
712 }
sqlite_int64 i64
Definition: tkeyvfs.cc:34
#define MEMPAGE
Definition: tkeyvfs.cc:83
i64 bufAllocated
Definition: tkeyvfs.cc:103
int szChunk
Definition: tkeyvfs.cc:108
i64 fileSize
Definition: tkeyvfs.cc:104
static int sqlite3CantopenError ( int  lineno)
static

Definition at line 209 of file tkeyvfs.cc.

210 {
211  fprintf(stderr,
212  "tkeyvfs.c: cannot open file at line %d of [%.10s]",
213  lineno,
214  20 + sqlite3_sourceid());
215  return SQLITE_CANTOPEN;
216 }
static int sqlite3Strlen30 ( const char *  z)
static

Definition at line 228 of file tkeyvfs.cc.

References z.

229 {
230  const char* z2 = z;
231  if (z == 0) {
232  return 0;
233  }
234  while (*z2) {
235  z2++;
236  }
237  return 0x3fffffff & (int)(z2 - z);
238 }
Double_t z
Definition: plot.C:279
int tkeyvfs_init ( void  )

Definition at line 1768 of file tkeyvfs.cc.

References ArraySize, aSyscall, nolockIoFinder, and UNIXVFS.

Referenced by art::completeRootHandlers(), and main().

1769 {
1770 /*
1771 ** The following macro defines an initializer for an sqlite3_vfs object.
1772 ** The name of the VFS is NAME. The pAppData is a pointer to a pointer
1773 ** to the "finder" function. (pAppData is a pointer to a pointer because
1774 ** silly C90 rules prohibit a void* from being cast to a function pointer
1775 ** and so we have to go through the intermediate pointer to avoid problems
1776 ** when compiling with -pedantic-errors on GCC.)
1777 **
1778 ** The FINDER parameter to this macro is the name of the pointer to the
1779 ** finder-function. The finder-function returns a pointer to the
1780 ** sqlite_io_methods object that implements the desired locking
1781 ** behaviors. See the division above that contains the IOMETHODS
1782 ** macro for addition information on finder-functions.
1783 **
1784 ** Most finders simply return a pointer to a fixed sqlite3_io_methods
1785 ** object. But the "autolockIoFinder" available on MacOSX does a little
1786 ** more than that; it looks at the filesystem type that hosts the
1787 ** database file and tries to choose an locking method appropriate for
1788 ** that filesystem time.
1789 */
1790 #define UNIXVFS(VFSNAME, FINDER) \
1791  { \
1792  1, /* iVersion */ \
1793  sizeof(unixFile), /* szOsFile */ \
1794  MAX_PATHNAME, /* mxPathname */ \
1795  0, /* pNext */ \
1796  VFSNAME, /* zName */ \
1797  (void*)&FINDER, /* pAppData */ \
1798  unixOpen, /* xOpen */ \
1799  unixDelete, /* xDelete */ \
1800  unixAccess, /* xAccess */ \
1801  unixFullPathname, /* xFullPathname */ \
1802  unixDlOpen, /* xDlOpen */ \
1803  unixDlError, /* xDlError */ \
1804  unixDlSym, /* xDlSym */ \
1805  unixDlClose, /* xDlClose */ \
1806  unixRandomness, /* xRandomness */ \
1807  unixSleep, /* xSleep */ \
1808  unixCurrentTime, /* xCurrentTime */ \
1809  unixGetLastError, /* xGetLastError */ \
1810  /* unixCurrentTimeInt64, v2, xCurrentTimeInt64 */ \
1811  /* unixSetSystemCall, v3, xSetSystemCall */ \
1812  /* unixGetSystemCall, v3, xGetSystemCall */ \
1813  /* unixNextSystemCall, v3, xNextSystemCall */ \
1814  }
1815  /*
1816  ** All default VFSes for unix are contained in the following array.
1817  **
1818  ** Note that the sqlite3_vfs.pNext field of the VFS object is modified
1819  ** by the SQLite core when the VFS is registered. So the following
1820  ** array cannot be const.
1821  */
1822  static sqlite3_vfs aVfs[] = {
1823  UNIXVFS("tkeyvfs", nolockIoFinder),
1824  };
1825  unsigned int i; /* Loop counter */
1826  /* Double-check that the aSyscall[] array has been constructed
1827  ** correctly. See ticket [bb3a86e890c8e96ab] */
1828  assert(ArraySize(aSyscall) == 16);
1829  /* Register all VFSes defined in the aVfs[] array */
1830  for (i = 0; i < (sizeof(aVfs) / sizeof(sqlite3_vfs)); i++) {
1831  sqlite3_vfs_register(&aVfs[i], 0);
1832  }
1833  return SQLITE_OK;
1834 }
static const sqlite3_io_methods *(*const nolockIoFinder)(const char *, unixFile *p)
Definition: tkeyvfs.cc:364
static struct unix_syscall aSyscall[]
#define UNIXVFS(VFSNAME, FINDER)
#define ArraySize(X)
Definition: tkeyvfs.cc:32
int tkeyvfs_open_v2 ( const char *  filename,
sqlite3 **  ppDb,
int  flags,
TFile *  rootFile 
)

Definition at line 1837 of file tkeyvfs.cc.

References unixFile::rootFile.

Referenced by art::SQLite3Wrapper::initDB(), and art::TKeyVFSOpenPolicy::open().

1845 {
1846 #ifndef TKEYVFS_NO_ROOT
1847  RootFileSentry rfs(rootFile);
1848  // Note that the sentry *is* the correct thing to do, here:
1849  // gRootFile is required in unixOpen(), which is called as part of
1850  // the chain of functions of which sqlite3_open_v2() is the first
1851  // call. By the time we return from sqlite3_open_v2() then, we no
1852  // longer require gRootFile and the sentry can do the job of
1853  // cleaning up when it goes out of scope.
1854 #endif // TKEYVFS_NO_ROOT
1855  return sqlite3_open_v2(filename,
1856  ppDb,
1857  flags,
1858 #ifdef TKEYVFS_NO_ROOT
1859  nullptr
1860 #else
1861  "tkeyvfs"
1862 #endif
1863  );
1864 }
static int unixAccess ( sqlite3_vfs *  NotUsed,
const char *  zPath,
int  flags,
int *  pResOut 
)
static

Definition at line 1330 of file tkeyvfs.cc.

References UNUSED_PARAMETER, and unixFile::zPath.

1335 {
1336  int amode = 0;
1337  UNUSED_PARAMETER(NotUsed);
1338 #if TKEYVFS_TRACE
1339  fprintf(stderr, "Begin unixAccess ...\n");
1340  if (zPath != nullptr) {
1341  fprintf(stderr, "filename: %s\n", zPath);
1342  }
1343 #endif /* TKEYVFS_TRACE */
1344  switch (flags) {
1345  case SQLITE_ACCESS_EXISTS:
1346 
1347 #if TKEYVFS_TRACE
1348  fprintf(stderr, "op: SQLITE_ACCESS_EXISTS\n");
1349 #endif /* TKEYVFS_TRACE */
1350  amode = F_OK;
1351  break;
1352  case SQLITE_ACCESS_READWRITE:
1353 
1354 #if TKEYVFS_TRACE
1355  fprintf(stderr, "op: SQLITE_ACCESS_READWRITE\n");
1356 #endif /* TKEYVFS_TRACE */
1357  amode = W_OK | R_OK;
1358  break;
1359  case SQLITE_ACCESS_READ:
1360 
1361 #if TKEYVFS_TRACE
1362  fprintf(stderr, "op: SQLITE_ACCESS_READ\n");
1363 #endif /* TKEYVFS_TRACE */
1364  amode = R_OK;
1365  break;
1366  default:
1367  assert(!"Invalid flags argument");
1368  }
1369  *pResOut = 0;
1370 #if TKEYVFS_TRACE
1371  fprintf(stderr, "End unixAccess ...\n");
1372 #endif /* TKEYVFS_TRACE */
1373  return SQLITE_OK;
1374 }
#define UNUSED_PARAMETER(x)
Definition: tkeyvfs.cc:29
static int unixCurrentTime ( sqlite3_vfs *  NotUsed,
double *  prNow 
)
static

Definition at line 1570 of file tkeyvfs.cc.

References unixCurrentTimeInt64(), and UNUSED_PARAMETER.

1571 {
1572  sqlite3_int64 i;
1573  UNUSED_PARAMETER(NotUsed);
1574 #if TKEYVFS_TRACE
1575  fprintf(stderr, "Begin unixCurrentTime ...\n");
1576 #endif /* TKEYVFS_TRACE */
1577  unixCurrentTimeInt64(0, &i);
1578  *prNow = i / 86400000.0;
1579 #if TKEYVFS_TRACE
1580  fprintf(stderr, "End unixCurrentTime ...\n");
1581 #endif /* TKEYVFS_TRACE */
1582  return 0;
1583 }
static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow)
Definition: tkeyvfs.cc:1615
#define UNUSED_PARAMETER(x)
Definition: tkeyvfs.cc:29
static int unixCurrentTimeInt64 ( sqlite3_vfs *  NotUsed,
sqlite3_int64 *  piNow 
)
static

Definition at line 1615 of file tkeyvfs.cc.

References UNUSED_PARAMETER.

Referenced by unixCurrentTime().

1616 {
1617  static const sqlite3_int64 unixEpoch = 24405875 * (sqlite3_int64)8640000;
1618  struct timeval sNow;
1619 #if TKEYVFS_TRACE
1620  fprintf(stderr, "Begin unixCurrentTimeInt64 ...\n");
1621 #endif /* TKEYVFS_TRACE */
1622  gettimeofday(&sNow, 0);
1623  *piNow = unixEpoch + 1000 * (sqlite3_int64)sNow.tv_sec + sNow.tv_usec / 1000;
1624  UNUSED_PARAMETER(NotUsed);
1625 #if TKEYVFS_TRACE
1626  fprintf(stderr, "End unixCurrentTimeInt64 ...\n");
1627 #endif /* TKEYVFS_TRACE */
1628  return 0;
1629 }
#define UNUSED_PARAMETER(x)
Definition: tkeyvfs.cc:29
static int unixDelete ( sqlite3_vfs *  NotUsed,
const char *  zPath,
int  dirSync 
)
static

Definition at line 1300 of file tkeyvfs.cc.

References UNUSED_PARAMETER, and unixFile::zPath.

1304 {
1305  int rc = SQLITE_OK;
1306  UNUSED_PARAMETER(NotUsed);
1307 #if TKEYVFS_TRACE
1308  fprintf(stderr, "Begin unixDelete ...\n");
1309  if (zPath != nullptr) {
1310  fprintf(stderr, "filename: %s\n", zPath);
1311  }
1312 #endif /* TKEYVFS_TRACE */
1313 #if TKEYVFS_TRACE
1314  fprintf(stderr, "End unixDelete ...\n");
1315 #endif /* TKEYVFS_TRACE */
1316  return rc;
1317 }
#define UNUSED_PARAMETER(x)
Definition: tkeyvfs.cc:29
static int unixDeviceCharacteristics ( sqlite3_file *  NotUsed)
static

Definition at line 1133 of file tkeyvfs.cc.

References UNUSED_PARAMETER.

1134 {
1135  UNUSED_PARAMETER(NotUsed);
1136 #if TKEYVFS_TRACE
1137  fprintf(stderr, "Begin unixDeviceCharacteristics ...\n");
1138 #endif /* TKEYVFS_TRACE */
1139 #if TKEYVFS_TRACE
1140  fprintf(stderr, "End unixDeviceCharacteristics ...\n");
1141 #endif /* TKEYVFS_TRACE */
1142  return 0;
1143 }
#define UNUSED_PARAMETER(x)
Definition: tkeyvfs.cc:29
static void unixDlClose ( sqlite3_vfs *  NotUsed,
void *  pHandle 
)
static

Definition at line 1481 of file tkeyvfs.cc.

References UNUSED_PARAMETER.

1482 {
1483  UNUSED_PARAMETER(NotUsed);
1484 #if TKEYVFS_TRACE
1485  fprintf(stderr, "Begin unixDlClose ...\n");
1486 #endif /* TKEYVFS_TRACE */
1487  dlclose(pHandle);
1488 #if TKEYVFS_TRACE
1489  fprintf(stderr, "End unixDlClose ...\n");
1490 #endif /* TKEYVFS_TRACE */
1491 
1492 }
#define UNUSED_PARAMETER(x)
Definition: tkeyvfs.cc:29
static void unixDlError ( sqlite3_vfs *  NotUsed,
int  nBuf,
char *  zBufOut 
)
static

Definition at line 1435 of file tkeyvfs.cc.

References unixDlSym, UNUSED_PARAMETER, and x.

1436 {
1437  const char* zErr;
1438  UNUSED_PARAMETER(NotUsed);
1439 #if TKEYVFS_TRACE
1440  fprintf(stderr, "Begin unixDlError ...\n");
1441 #endif /* TKEYVFS_TRACE */
1442  zErr = dlerror();
1443  if (zErr) {
1444  sqlite3_snprintf(nBuf, zBufOut, "%s", zErr);
1445  }
1446 #if TKEYVFS_TRACE
1447  fprintf(stderr, "End unixDlError ...\n");
1448 #endif /* TKEYVFS_TRACE */
1449 
1450 }
#define UNUSED_PARAMETER(x)
Definition: tkeyvfs.cc:29
static void * unixDlOpen ( sqlite3_vfs *  NotUsed,
const char *  zFilename 
)
static

Definition at line 1414 of file tkeyvfs.cc.

References UNUSED_PARAMETER.

1415 {
1416  UNUSED_PARAMETER(NotUsed);
1417 #if TKEYVFS_TRACE
1418  fprintf(stderr, "Begin unixFullPathName ...\n");
1419 #endif /* TKEYVFS_TRACE */
1420  void* p = dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
1421 #if TKEYVFS_TRACE
1422  fprintf(stderr, "End unixFullPathName ...\n");
1423 #endif /* TKEYVFS_TRACE */
1424  return p;
1425 }
#define UNUSED_PARAMETER(x)
Definition: tkeyvfs.cc:29
static int unixFileControl ( sqlite3_file *  id,
int  op,
void *  pArg 
)
static

Definition at line 1018 of file tkeyvfs.cc.

References unixFile::eFileLock, fcntlSizeHint(), unixFile::lastErrno, SQLITE_FCNTL_CHUNK_SIZE, SQLITE_FCNTL_DB_UNCHANGED, SQLITE_FCNTL_SIZE_HINT, SQLITE_FCNTL_SYNC_OMITTED, and unixFile::zPath.

1019 {
1020 
1021 #if TKEYVFS_TRACE
1022  fprintf(stderr, "Begin unixFileControl ...\n");
1023  if (((unixFile*)id)->zPath) {
1024  fprintf(stderr, "filename: %s\n", ((unixFile*)id)->zPath);
1025  }
1026 #endif /* TKEYVFS_TRACE */
1027  switch (op) {
1028  case SQLITE_FCNTL_LOCKSTATE: {
1029 
1030 #if TKEYVFS_TRACE
1031  fprintf(stderr, "op: LOCKSTATE\n");
1032 #endif /* TKEYVFS_TRACE */
1033  *(int*)pArg = ((unixFile*)id)->eFileLock;
1034  /*SQLITE_LOCK_NONE*/
1035 #if TKEYVFS_TRACE
1036  fprintf(stderr, "End unixFileControl ...\n");
1037 #endif /* TKEYVFS_TRACE */
1038  return SQLITE_OK;
1039  }
1040  case SQLITE_LAST_ERRNO: {
1041 
1042 #if TKEYVFS_TRACE
1043  fprintf(stderr, "op: LAST_ERRNO\n");
1044 #endif /* TKEYVFS_TRACE */
1045  *(int*)pArg = ((unixFile*)id)->lastErrno;
1046 #if TKEYVFS_TRACE
1047  fprintf(stderr, "End unixFileControl ...\n");
1048 #endif /* TKEYVFS_TRACE */
1049  return SQLITE_OK;
1050  }
1051  case SQLITE_FCNTL_CHUNK_SIZE: {
1052 
1053 #if TKEYVFS_TRACE
1054  fprintf(stderr, "op: CHUNK_SIZE\n");
1055  fprintf(stderr, "szChunk: %d\n", *(int*)pArg);
1056 #endif /* TKEYVFS_TRACE */
1057  ((unixFile*)id)->szChunk = *(int*)pArg;
1058 #if TKEYVFS_TRACE
1059  fprintf(stderr, "End unixFileControl ...\n");
1060 #endif /* TKEYVFS_TRACE */
1061  return SQLITE_OK;
1062  }
1063  case SQLITE_FCNTL_SIZE_HINT: {
1064 
1065 #if TKEYVFS_TRACE
1066  fprintf(stderr, "op: SIZE_HINT\n");
1067  fprintf(stderr, "hint: 0x%016lx\n", *(i64*)pArg);
1068 #endif /* TKEYVFS_TRACE */
1069  int val = fcntlSizeHint((unixFile*)id, *(i64*)pArg);
1070 #if TKEYVFS_TRACE
1071  fprintf(stderr, "End unixFileControl ...\n");
1072 #endif /* TKEYVFS_TRACE */
1073  return val;
1074  }
1075  /* The pager calls this method to signal that it has done
1076  ** a rollback and that the database is therefore unchanged and
1077  ** it hence it is OK for the transaction change counter to be
1078  ** unchanged.
1079  */
1081 
1082 #if TKEYVFS_TRACE
1083  fprintf(stderr, "op: DB_UNCHANGED\n");
1084 #endif /* TKEYVFS_TRACE */
1085  ((unixFile*)id)->dbUpdate = 0;
1086 #if TKEYVFS_TRACE
1087  fprintf(stderr, "End unixFileControl ...\n");
1088 #endif /* TKEYVFS_TRACE */
1089  return SQLITE_OK;
1090  }
1092 
1093 #if TKEYVFS_TRACE
1094  fprintf(stderr, "op: SYNC_OMITTED\n");
1095  fprintf(stderr, "End unixFileControl ...\n");
1096 #endif /* TKEYVFS_TRACE */
1097  return SQLITE_OK; /* A no-op */
1098  }
1099  }
1100 #if TKEYVFS_TRACE
1101  fprintf(stderr, "End unixFileControl ...\n");
1102 #endif /* TKEYVFS_TRACE */
1103  return SQLITE_NOTFOUND;
1104 }
#define SQLITE_FCNTL_SYNC_OMITTED
Definition: tkeyvfs.cc:24
sqlite_int64 i64
Definition: tkeyvfs.cc:34
static int fcntlSizeHint(unixFile *pFile, i64 nByte)
Definition: tkeyvfs.cc:590
#define SQLITE_FCNTL_CHUNK_SIZE
Definition: tkeyvfs.cc:23
#define SQLITE_FCNTL_SIZE_HINT
Definition: tkeyvfs.cc:22
#define SQLITE_FCNTL_DB_UNCHANGED
Definition: tkeyvfs.cc:26
static int unixFileSize ( sqlite3_file *  id,
i64 pSize 
)
static

Definition at line 938 of file tkeyvfs.cc.

References unixFile::fileSize, and unixFile::zPath.

939 {
940  unixFile* p = (unixFile*)id;
941 #if TKEYVFS_TRACE
942  fprintf(stderr, "Begin unixFileSize ...\n");
943  if (((unixFile*)id)->zPath) {
944  fprintf(stderr, "filename: %s\n", ((unixFile*)id)->zPath);
945  }
946 #endif /* TKEYVFS_TRACE */
947  *pSize = p->fileSize;
948  /* When opening a zero-size database, the findInodeInfo() procedure
949  ** writes a single byte into that file in order to work around a bug
950  ** in the OS-X msdos filesystem. In order to avoid problems with upper
951  ** layers, we need to report this file size as zero even though it is
952  ** really 1. Ticket #3260.
953  */
954  if (*pSize == 1) {
955  *pSize = 0;
956  }
957 #if TKEYVFS_TRACE
958  fprintf(stderr, "End unixFileSize ...\n");
959 #endif /* TKEYVFS_TRACE */
960  return SQLITE_OK;
961 }
i64 fileSize
Definition: tkeyvfs.cc:104
static int unixFullPathname ( sqlite3_vfs *  pVfs,
const char *  zPath,
int  nOut,
char *  zOut 
)
static

Definition at line 1386 of file tkeyvfs.cc.

References MAX_PATHNAME, and UNUSED_PARAMETER.

1391 {
1392 
1393 #if TKEYVFS_TRACE
1394  fprintf(stderr, "Begin unixFullPathName ...\n");
1395  if (zPath != nullptr) {
1396  fprintf(stderr, "filename: %s\n", zPath);
1397  }
1398 #endif /* TKEYVFS_TRACE */
1399  assert(pVfs->mxPathname == MAX_PATHNAME);
1400  UNUSED_PARAMETER(pVfs);
1401  zOut[nOut - 1] = '\0';
1402  sqlite3_snprintf(nOut, zOut, "%s", zPath);
1403 #if TKEYVFS_TRACE
1404  fprintf(stderr, "End unixFullPathName ...\n");
1405 #endif /* TKEYVFS_TRACE */
1406  return SQLITE_OK;
1407 }
#define UNUSED_PARAMETER(x)
Definition: tkeyvfs.cc:29
#define MAX_PATHNAME
Definition: tkeyvfs.cc:88
static int unixGetLastError ( sqlite3_vfs *  NotUsed,
int  NotUsed2,
char *  NotUsed3 
)
static

Definition at line 1593 of file tkeyvfs.cc.

References UNUSED_PARAMETER.

1594 {
1595  UNUSED_PARAMETER(NotUsed);
1596  UNUSED_PARAMETER(NotUsed2);
1597  UNUSED_PARAMETER(NotUsed3);
1598 #if TKEYVFS_TRACE
1599  fprintf(stderr, "Begin unixGetLastError ...\n");
1600  fprintf(stderr, "End unixGetLastError ...\n");
1601 #endif /* TKEYVFS_TRACE */
1602  return 0;
1603 }
#define UNUSED_PARAMETER(x)
Definition: tkeyvfs.cc:29
static sqlite3_syscall_ptr unixGetSystemCall ( sqlite3_vfs *  pNotUsed,
const char *  zName 
)
static

Definition at line 1690 of file tkeyvfs.cc.

References aSyscall, unix_syscall::pCurrent, and UNUSED_PARAMETER.

1691 {
1692  unsigned int i;
1693  UNUSED_PARAMETER(pNotUsed);
1694 #if TKEYVFS_TRACE
1695  fprintf(stderr, "Begin unixGetSystemCall ...\n");
1696 #endif /* TKEYVFS_TRACE */
1697  for (i = 0; i < sizeof(aSyscall) / sizeof(aSyscall[0]); i++) {
1698  if (strcmp(zName, aSyscall[i].zName) == 0) {
1699 
1700 #if TKEYVFS_TRACE
1701  fprintf(stderr, "End unixGetSystemCall ...\n");
1702 #endif /* TKEYVFS_TRACE */
1703  return aSyscall[i].pCurrent;
1704  }
1705  }
1706 #if TKEYVFS_TRACE
1707  fprintf(stderr, "End unixGetSystemCall ...\n");
1708 #endif /* TKEYVFS_TRACE */
1709  return 0;
1710 }
sqlite3_syscall_ptr pCurrent
Definition: tkeyvfs.cc:248
#define UNUSED_PARAMETER(x)
Definition: tkeyvfs.cc:29
static struct unix_syscall aSyscall[]
static int unixGetTempname ( int  nBuf,
char *  zBuf 
)
static

Definition at line 549 of file tkeyvfs.cc.

References SQLITE_TEMP_FILE_PREFIX.

Referenced by unixOpen().

550 {
551  static const unsigned char zChars[] = "abcdefghijklmnopqrstuvwxyz"
552  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
553  "0123456789";
554  unsigned int i, j;
555 #if TKEYVFS_TRACE
556  fprintf(stderr, "Begin unixGetTempname ...\n");
557 #endif /* TKEYVFS_TRACE */
558  /* Check that the output buffer is large enough for the temporary file
559  ** name. If it is not, return SQLITE_ERROR.
560  */
561  if ((strlen(SQLITE_TEMP_FILE_PREFIX) + 17) >= (size_t)nBuf) {
562 
563 #if TKEYVFS_TRACE
564  fprintf(stderr, "End unixGetTempname ...\n");
565 #endif /* TKEYVFS_TRACE */
566  return SQLITE_ERROR;
567  }
568  sqlite3_snprintf(nBuf - 17, zBuf, SQLITE_TEMP_FILE_PREFIX);
569  j = (int)strlen(zBuf);
570  sqlite3_randomness(15, &zBuf[j]);
571  for (i = 0; i < 15; i++, j++) {
572  zBuf[j] = (char)zChars[((unsigned char)zBuf[j]) % (sizeof(zChars) - 1)];
573  }
574  zBuf[j] = 0;
575 #if TKEYVFS_TRACE
576  fprintf(stderr, "End unixGetTempname ...\n");
577 #endif /* TKEYVFS_TRACE */
578  return SQLITE_OK;
579 }
#define SQLITE_TEMP_FILE_PREFIX
Definition: tkeyvfs.cc:90
static int unixLogErrorAtLine ( int  errcode,
const char *  zFunc,
const char *  zPath,
int  iLine 
)
static

Definition at line 388 of file tkeyvfs.cc.

Referenced by robust_close().

393 {
394  char* zErr; /* Message from strerror() or equivalent */
395  int iErrno = errno; /* Saved syscall error number */
396  zErr = strerror(iErrno);
397  if (zPath == 0) {
398  zPath = "";
399  }
400  fprintf(stderr,
401  "tkeyvfs.c:%d: (%d) %s(%s) - %s",
402  iLine,
403  iErrno,
404  zFunc,
405  zPath,
406  zErr);
407  return errcode;
408 }
static const char * unixNextSystemCall ( sqlite3_vfs *  p,
const char *  zName 
)
static

Definition at line 1719 of file tkeyvfs.cc.

References ArraySize, aSyscall, UNUSED_PARAMETER, and unix_syscall::zName.

1720 {
1721  int i = -1;
1722  UNUSED_PARAMETER(p);
1723 #if TKEYVFS_TRACE
1724  fprintf(stderr, "Begin unixNextSystemCall ...\n");
1725 #endif /* TKEYVFS_TRACE */
1726  if (zName) {
1727  for (i = 0; i < ArraySize(aSyscall) - 1; i++) {
1728  if (strcmp(zName, aSyscall[i].zName) == 0) {
1729  break;
1730  }
1731  }
1732  }
1733  for (i++; i < ArraySize(aSyscall); i++) {
1734  if (aSyscall[i].pCurrent != 0) {
1735 
1736 #if TKEYVFS_TRACE
1737  fprintf(stderr, "End unixNextSystemCall ...\n");
1738 #endif /* TKEYVFS_TRACE */
1739  return aSyscall[i].zName;
1740  }
1741  }
1742 #if TKEYVFS_TRACE
1743  fprintf(stderr, "End unixNextSystemCall ...\n");
1744 #endif /* TKEYVFS_TRACE */
1745  return nullptr;
1746 }
const char * zName
Definition: tkeyvfs.cc:247
#define UNUSED_PARAMETER(x)
Definition: tkeyvfs.cc:29
static struct unix_syscall aSyscall[]
#define ArraySize(X)
Definition: tkeyvfs.cc:32
static int unixOpen ( sqlite3_vfs *  pVfs,
const char *  zPath,
sqlite3_file *  pFile,
int  flags,
int *  pOutFlags 
)
static

Definition at line 1171 of file tkeyvfs.cc.

References unixFile::bufAllocated, unixFile::fileSize, gRootFile, unixFile::lastErrno, MAX_PATHNAME, MEMPAGE, nolockIoMethods, unixFile::pBuf, unixFile::pMethod, unixFile::rootFile, unixFile::saveToRootFile, SQLITE_CANTOPEN_BKPT, unixGetTempname(), unixLogError, and unixFile::zPath.

1177 {
1178  unixFile* p = (unixFile*)pFile;
1179  int eType = flags & 0xFFFFFF00; /* Type of file to open */
1180  int rc = SQLITE_OK;
1181  // int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE); // Not used.
1182  int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
1183  int isCreate = (flags & SQLITE_OPEN_CREATE);
1184  int isReadonly = (flags & SQLITE_OPEN_READONLY);
1185  int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
1186  char zTmpname[MAX_PATHNAME + 1];
1187  const char* zName = zPath;
1188 #if TKEYVFS_TRACE
1189  fprintf(stderr, "Begin unixOpen ...\n");
1190  if (zPath != nullptr) {
1191  fprintf(stderr, "filename: %s\n", zPath);
1192  }
1193 #endif /* TKEYVFS_TRACE */
1194  memset(p, 0, sizeof(unixFile));
1195  if (pOutFlags) {
1196  *pOutFlags = flags;
1197  }
1198  if (!zName) {
1199  rc = unixGetTempname(MAX_PATHNAME + 1, zTmpname);
1200  if (rc != SQLITE_OK) {
1201  return rc;
1202  }
1203  zName = zTmpname;
1204  }
1205  if (zName != nullptr) {
1206  p->zPath = (char*)malloc(strlen(zName) + 1);
1207  if (p->zPath != nullptr) {
1208  (void*)strcpy((char*)p->zPath, zName);
1209  }
1210  }
1211  p->lastErrno = 0;
1212  p->pMethod = &nolockIoMethods;
1213 #ifndef TKEYVFS_NO_ROOT
1214  p->rootFile = nullptr;
1215  if (eType & SQLITE_OPEN_MAIN_DB) {
1216  p->rootFile = gRootFile;
1217  }
1218  p->saveToRootFile =
1219  (p->rootFile && p->rootFile->IsWritable() &&
1220  (eType & SQLITE_OPEN_MAIN_DB) && (isCreate || isReadWrite) && !isDelete);
1221 #endif // TKEYVFS_NO_ROOT
1222  if ((eType & SQLITE_OPEN_MAIN_DB) && !isCreate) {
1223 
1224  i64 nBytes = 0;
1225  i64 nAlloc = 0;
1226 #ifndef TKEYVFS_NO_ROOT
1227  Bool_t status = kFALSE;
1228  TKey* k = 0;
1229  char* pKeyBuf = 0;
1230  /* Read the highest numbered cycle of the tkey which contains
1231  ** the database from the root file. */
1232  k = p->rootFile->GetKey(p->zPath, 9999 /*cycle*/);
1233  /* Force the tkey to allocate an i/o buffer for its contents. */
1234  k->SetBuffer();
1235  /* Read the contents of the tkey from the root file. */
1236  status = k->ReadFile();
1237  if (!status) {
1238 
1239 #if TKEYVFS_TRACE
1240  fprintf(stderr, "End unixOpen ...\n");
1241 #endif /* TKEYVFS_TRACE */
1242  rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName);
1243  return rc;
1244  }
1245  /* Get a pointer to the tkey i/o buffer. */
1246  pKeyBuf = k->GetBuffer();
1247  /* Get the size of the contained database file from the tkey. */
1248  nBytes = k->GetObjlen();
1249  /* Allocate enough memory pages to contain the database file. */
1250  nAlloc =
1251  ((nBytes + ((i64)(MEMPAGE - 1))) / ((i64)MEMPAGE)) * ((i64)MEMPAGE);
1252  p->pBuf = (char*)malloc((size_t)nAlloc);
1253 #else // TKEYVFS_NO_ROOT
1254  /* If not using root, a database file read is a noop. */
1255  nBytes = 0;
1256  nAlloc = MEMPAGE;
1257  p->pBuf = (char*)calloc(1, MEMPAGE);
1258 #endif // TKEYVFS_NO_ROOT
1259  if (p->pBuf == nullptr) {
1260 
1261 #if TKEYVFS_TRACE
1262  fprintf(stderr, "End unixOpen ...\n");
1263 #endif /* TKEYVFS_TRACE */
1264  rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName);
1265  return rc;
1266  }
1267 #ifndef TKEYVFS_NO_ROOT
1268  /* Copy the entire database file from the tkey i/o buffer
1269  ** into our in-memory database. */
1270  (void*)memcpy(p->pBuf, pKeyBuf, (size_t)nBytes);
1271 #endif // TKEYVFS_NO_ROOT
1272  p->bufAllocated = nAlloc;
1273  p->fileSize = nBytes;
1274 
1275  } else {
1276  p->pBuf = (char*)calloc(1, MEMPAGE);
1277  if (p->pBuf == nullptr) {
1278 
1279 #if TKEYVFS_TRACE
1280  fprintf(stderr, "End unixOpen ...\n");
1281 #endif /* TKEYVFS_TRACE */
1282  rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName);
1283  return rc;
1284  }
1285  p->bufAllocated = MEMPAGE;
1286  p->fileSize = 0;
1287  }
1288  rc = SQLITE_OK;
1289 #if TKEYVFS_TRACE
1290  fprintf(stderr, "End unixOpen ...\n");
1291 #endif /* TKEYVFS_TRACE */
1292  return rc;
1293 }
static int unixGetTempname(int nBuf, char *zBuf)
Definition: tkeyvfs.cc:549
sqlite_int64 i64
Definition: tkeyvfs.cc:34
const char * zPath
Definition: tkeyvfs.cc:107
static const sqlite3_io_methods nolockIoMethods
Definition: tkeyvfs.cc:334
#define MEMPAGE
Definition: tkeyvfs.cc:83
sqlite3_io_methods const * pMethod
Definition: tkeyvfs.cc:97
int saveToRootFile
Definition: tkeyvfs.cc:100
#define MAX_PATHNAME
Definition: tkeyvfs.cc:88
#define SQLITE_CANTOPEN_BKPT
Definition: tkeyvfs.cc:217
i64 bufAllocated
Definition: tkeyvfs.cc:103
int lastErrno
Definition: tkeyvfs.cc:106
static TFile * gRootFile
Definition: tkeyvfs.cc:76
i64 fileSize
Definition: tkeyvfs.cc:104
#define unixLogError(a, b, c)
Definition: tkeyvfs.cc:386
char * pBuf
Definition: tkeyvfs.cc:102
TFile * rootFile
Definition: tkeyvfs.cc:99
static int unixRandomness ( sqlite3_vfs *  NotUsed,
int  nBuf,
char *  zBuf 
)
static

Definition at line 1498 of file tkeyvfs.cc.

References osRead, robust_close(), robust_open(), and UNUSED_PARAMETER.

1499 {
1500  UNUSED_PARAMETER(NotUsed);
1501  assert((size_t)nBuf >= (sizeof(time_t) + sizeof(int)));
1502 #if TKEYVFS_TRACE
1503  fprintf(stderr, "Begin unixRandomness ...\n");
1504 #endif /* TKEYVFS_TRACE */
1505  /* We have to initialize zBuf to prevent valgrind from reporting
1506  ** errors. The reports issued by valgrind are incorrect - we would
1507  ** prefer that the randomness be increased by making use of the
1508  ** uninitialized space in zBuf - but valgrind errors tend to worry
1509  ** some users. Rather than argue, it seems easier just to initialize
1510  ** the whole array and silence valgrind, even if that means less randomness
1511  ** in the random seed.
1512  **
1513  ** When testing, initializing zBuf[] to zero is all we do. That means
1514  ** that we always use the same random number sequence. This makes the
1515  ** tests repeatable.
1516  */
1517  memset(zBuf, 0, nBuf);
1518  {
1519  int pid, fd;
1520  fd = robust_open("/dev/urandom", O_RDONLY, 0);
1521  if (fd < 0) {
1522  time_t t;
1523  time(&t);
1524  memcpy(zBuf, &t, sizeof(t));
1525  pid = getpid();
1526  memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid));
1527  assert(sizeof(t) + sizeof(pid) <= (size_t)nBuf);
1528  nBuf = sizeof(t) + sizeof(pid);
1529  } else {
1530  do {
1531  nBuf = osRead(fd, zBuf, nBuf);
1532  } while (nBuf < 0 && errno == EINTR);
1533  robust_close(0, fd, __LINE__);
1534  }
1535  }
1536 #if TKEYVFS_TRACE
1537  fprintf(stderr, "End unixRandomness ...\n");
1538 #endif /* TKEYVFS_TRACE */
1539  return nBuf;
1540 }
#define osRead
static void robust_close(unixFile *pFile, int h, int lineno)
Definition: tkeyvfs.cc:443
#define UNUSED_PARAMETER(x)
Definition: tkeyvfs.cc:29
static int robust_open(const char *z, int f, int m)
Definition: tkeyvfs.cc:414
static int unixRead ( sqlite3_file *  id,
void *  pBuf,
int  amt,
sqlite3_int64  offset 
)
static

Definition at line 737 of file tkeyvfs.cc.

References unixFile::lastErrno, seekAndRead(), and unixFile::zPath.

738 {
739  unixFile* pFile = (unixFile*)id;
740  int got;
741 #if TKEYVFS_TRACE
742  fprintf(stderr, "Begin unixRead ...\n");
743  if (((unixFile*)id)->zPath) {
744  fprintf(stderr, "filename: %s\n", ((unixFile*)id)->zPath);
745  }
746  fprintf(
747  stderr, "offset: 0x%016lx amt: 0x%08x\n", (unsigned long long)offset, amt);
748 #endif /* TKEYVFS_TRACE */
749  got = seekAndRead(pFile, offset, pBuf, amt);
750  if (got == amt) {
751 
752 #if TKEYVFS_TRACE
753  fprintf(stderr, "End unixRead ...\n");
754 #endif /* TKEYVFS_TRACE */
755  return SQLITE_OK;
756  } else if (got < 0) {
757  /* lastErrno set by seekAndRead */
758 #if TKEYVFS_TRACE
759  fprintf(stderr, "End unixRead ...\n");
760 #endif /* TKEYVFS_TRACE */
761  return SQLITE_IOERR_READ;
762  } else {
763  pFile->lastErrno = 0; /* not a system error */
764  /* Unread parts of the buffer must be zero-filled */
765  memset(&((char*)pBuf)[got], 0, amt - got);
766 #if TKEYVFS_TRACE
767  fprintf(stderr, "End unixRead ...\n");
768 #endif /* TKEYVFS_TRACE */
769  return SQLITE_IOERR_SHORT_READ;
770  }
771 }
int lastErrno
Definition: tkeyvfs.cc:106
static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt)
Definition: tkeyvfs.cc:642
static int unixSectorSize ( sqlite3_file *  NotUsed)
static

Definition at line 1117 of file tkeyvfs.cc.

References SQLITE_DEFAULT_SECTOR_SIZE, and UNUSED_PARAMETER.

1118 {
1119  UNUSED_PARAMETER(NotUsed);
1120 #if TKEYVFS_TRACE
1121  fprintf(stderr, "Begin unixSectorSize ...\n");
1122 #endif /* TKEYVFS_TRACE */
1123 #if TKEYVFS_TRACE
1124  fprintf(stderr, "End unixSectorSize ...\n");
1125 #endif /* TKEYVFS_TRACE */
1127 }
#define UNUSED_PARAMETER(x)
Definition: tkeyvfs.cc:29
#define SQLITE_DEFAULT_SECTOR_SIZE
Definition: tkeyvfs.cc:27
static int unixSetSystemCall ( sqlite3_vfs *  pNotUsed,
const char *  zName,
sqlite3_syscall_ptr  pNewFunc 
)
static

Definition at line 1638 of file tkeyvfs.cc.

References aSyscall, unix_syscall::pCurrent, unix_syscall::pDefault, and UNUSED_PARAMETER.

1643 {
1644  unsigned int i;
1645  int rc = SQLITE_NOTFOUND;
1646  UNUSED_PARAMETER(pNotUsed);
1647 #if TKEYVFS_TRACE
1648  fprintf(stderr, "Begin unixSetSystemCall ...\n");
1649 #endif /* TKEYVFS_TRACE */
1650  if (zName == 0) {
1651  /* If no zName is given, restore all system calls to their default
1652  ** settings and return nullptr
1653  */
1654  rc = SQLITE_OK;
1655  for (i = 0; i < sizeof(aSyscall) / sizeof(aSyscall[0]); i++) {
1656  if (aSyscall[i].pDefault) {
1658  }
1659  }
1660  } else {
1661  /* If zName is specified, operate on only the one system call
1662  ** specified.
1663  */
1664  for (i = 0; i < sizeof(aSyscall) / sizeof(aSyscall[0]); i++) {
1665  if (strcmp(zName, aSyscall[i].zName) == 0) {
1666  if (aSyscall[i].pDefault == 0) {
1668  }
1669  rc = SQLITE_OK;
1670  if (pNewFunc == 0) {
1671  pNewFunc = aSyscall[i].pDefault;
1672  }
1673  aSyscall[i].pCurrent = pNewFunc;
1674  break;
1675  }
1676  }
1677  }
1678 #if TKEYVFS_TRACE
1679  fprintf(stderr, "End unixSetSystemCall ...\n");
1680 #endif /* TKEYVFS_TRACE */
1681  return rc;
1682 }
sqlite3_syscall_ptr pDefault
Definition: tkeyvfs.cc:249
sqlite3_syscall_ptr pCurrent
Definition: tkeyvfs.cc:248
#define UNUSED_PARAMETER(x)
Definition: tkeyvfs.cc:29
static struct unix_syscall aSyscall[]
static int unixSleep ( sqlite3_vfs *  NotUsed,
int  microseconds 
)
static

Definition at line 1551 of file tkeyvfs.cc.

References UNUSED_PARAMETER.

1552 {
1553  UNUSED_PARAMETER(NotUsed);
1554 #if TKEYVFS_TRACE
1555  fprintf(stderr, "Begin unixSleep ...\n");
1556 #endif /* TKEYVFS_TRACE */
1557  usleep(microseconds);
1558 #if TKEYVFS_TRACE
1559  fprintf(stderr, "End unixSleep ...\n");
1560 #endif /* TKEYVFS_TRACE */
1561  return microseconds;
1562 }
#define UNUSED_PARAMETER(x)
Definition: tkeyvfs.cc:29
static int unixSync ( sqlite3_file *  id,
int  flags 
)
static

Definition at line 919 of file tkeyvfs.cc.

References UNUSED_PARAMETER2, and unixFile::zPath.

920 {
921  UNUSED_PARAMETER2(id, flags);
922 #if TKEYVFS_TRACE
923  fprintf(stderr, "Begin unixSync ...\n");
924  if (((unixFile*)id)->zPath) {
925  fprintf(stderr, "filename: %s\n", ((unixFile*)id)->zPath);
926  }
927 #endif /* TKEYVFS_TRACE */
928 #if TKEYVFS_TRACE
929  fprintf(stderr, "End unixSync ...\n");
930 #endif /* TKEYVFS_TRACE */
931  return SQLITE_OK;
932 }
#define UNUSED_PARAMETER2(x, y)
Definition: tkeyvfs.cc:30
static int unixTruncate ( sqlite3_file *  id,
i64  nByte 
)
static

Definition at line 837 of file tkeyvfs.cc.

References unixFile::bufAllocated, unixFile::fileSize, unixFile::inNormalWrite, unixFile::lastErrno, MEMPAGE, unixFile::pBuf, unixFile::szChunk, unixFile::transCntrChng, unixLogError, and unixFile::zPath.

838 {
839  unixFile* pFile = (unixFile*)id;
840  int rc;
841 #if TKEYVFS_TRACE
842  fprintf(stderr, "Begin unixTruncate ...\n");
843  if (((unixFile*)id)->zPath) {
844  fprintf(stderr, "filename: %s\n", ((unixFile*)id)->zPath);
845  }
846  fprintf(stderr, "nByte: 0x%016lx\n", (unsigned long long)nByte);
847 #endif /* TKEYVFS_TRACE */
848  /* If the user has configured a chunk-size for this file, truncate the
849  ** file so that it consists of an integer number of chunks (i.e. the
850  ** actual file size after the operation may be larger than the requested
851  ** size).
852  */
853  if (pFile->szChunk) {
854  nByte = ((nByte + pFile->szChunk - 1) / pFile->szChunk) * pFile->szChunk;
855  }
856  if (nByte == 0) {
857  free(pFile->pBuf);
858  pFile->pBuf = (char*)calloc(1, MEMPAGE);
859  if (pFile->pBuf == nullptr) {
860  pFile->bufAllocated = 0;
861  pFile->fileSize = 0;
862  pFile->lastErrno = errno;
863 #if TKEYVFS_TRACE
864  fprintf(stderr, "End unixTruncate ...\n");
865 #endif /* TKEYVFS_TRACE */
866  return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
867  }
868  pFile->bufAllocated = MEMPAGE;
869  pFile->fileSize = 0;
870  } else {
871  i64 newBufSize =
872  ((nByte + (i64)(MEMPAGE - 1)) / ((i64)MEMPAGE)) * ((i64)MEMPAGE);
873  i64 zeroCnt = newBufSize - nByte;
874  char* pNewBuf = (char*)realloc((void*)pFile->pBuf, (size_t)newBufSize);
875  if (pNewBuf == nullptr) {
876  pFile->lastErrno = errno;
877 #if TKEYVFS_TRACE
878  fprintf(stderr, "End unixTruncate ...\n");
879 #endif /* TKEYVFS_TRACE */
880  return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
881  }
882  (void*)memset((void*)(pNewBuf + nByte), 0, (size_t)zeroCnt);
883  pFile->pBuf = pNewBuf;
884  pFile->bufAllocated = newBufSize;
885  pFile->fileSize = nByte;
886  }
887  /* If we are doing a normal write to a database file (as opposed to
888  ** doing a hot-journal rollback or a write to some file other than a
889  ** normal database file) and we truncate the file to zero length,
890  ** that effectively updates the change counter. This might happen
891  ** when restoring a database using the backup API from a zero-length
892  ** source.
893  */
894  if (pFile->inNormalWrite && (nByte == 0)) {
895  pFile->transCntrChng = 1;
896  }
897 #if TKEYVFS_TRACE
898  fprintf(stderr, "End unixTruncate ...\n");
899 #endif /* TKEYVFS_TRACE */
900  return SQLITE_OK;
901 }
sqlite_int64 i64
Definition: tkeyvfs.cc:34
unsigned char transCntrChng
Definition: tkeyvfs.cc:116
const char * zPath
Definition: tkeyvfs.cc:107
#define MEMPAGE
Definition: tkeyvfs.cc:83
unsigned char inNormalWrite
Definition: tkeyvfs.cc:118
i64 bufAllocated
Definition: tkeyvfs.cc:103
int lastErrno
Definition: tkeyvfs.cc:106
int szChunk
Definition: tkeyvfs.cc:108
i64 fileSize
Definition: tkeyvfs.cc:104
#define unixLogError(a, b, c)
Definition: tkeyvfs.cc:386
char * pBuf
Definition: tkeyvfs.cc:102
static int unixWrite ( sqlite3_file *  id,
const void *  pBuf,
int  amt,
sqlite3_int64  offset 
)
static

Definition at line 778 of file tkeyvfs.cc.

References unixFile::dbUpdate, unixFile::inNormalWrite, unixFile::lastErrno, seekAndRead(), seekAndWrite(), unixFile::transCntrChng, and unixFile::zPath.

779 {
780  unixFile* pFile = (unixFile*)id;
781  int wrote = 0;
782 #if TKEYVFS_TRACE
783  fprintf(stderr, "Begin unixWrite ...\n");
784  if (((unixFile*)id)->zPath) {
785  fprintf(stderr, "filename: %s\n", ((unixFile*)id)->zPath);
786  }
787  fprintf(
788  stderr, "offset: 0x%016lx amt: 0x%08x\n", (unsigned long long)offset, amt);
789 #endif /* TKEYVFS_TRACE */
790  /* If we are doing a normal write to a database file (as opposed to
791  ** doing a hot-journal rollback or a write to some file other than a
792  ** normal database file) then record the fact that the database
793  ** has changed. If the transaction counter is modified, record that
794  ** fact too.
795  */
796  if (pFile->inNormalWrite) {
797  pFile->dbUpdate = 1; /* The database has been modified */
798  if ((offset <= 24) && (offset + amt >= 27)) {
799  int rc;
800  char oldCntr[4];
801  rc = seekAndRead(pFile, 24, oldCntr, 4);
802  if (rc != 4 || memcmp(oldCntr, &((char*)pBuf)[24 - offset], 4) != 0) {
803  pFile->transCntrChng = 1; /* The transaction counter has changed */
804  }
805  }
806  }
807  while ((amt > 0) && ((wrote = seekAndWrite(pFile, offset, pBuf, amt)) > 0)) {
808  amt -= wrote;
809  offset += wrote;
810  pBuf = &((char*)pBuf)[wrote];
811  }
812  if (amt > 0) {
813  if (wrote < 0) {
814  /* lastErrno set by seekAndWrite */
815 #if TKEYVFS_TRACE
816  fprintf(stderr, "End unixWrite ...\n");
817 #endif /* TKEYVFS_TRACE */
818  return SQLITE_IOERR_WRITE;
819  } else {
820  pFile->lastErrno = 0; /* not a system error */
821 #if TKEYVFS_TRACE
822  fprintf(stderr, "End unixWrite ...\n");
823 #endif /* TKEYVFS_TRACE */
824  return SQLITE_FULL;
825  }
826  }
827 #if TKEYVFS_TRACE
828  fprintf(stderr, "End unixWrite ...\n");
829 #endif /* TKEYVFS_TRACE */
830  return SQLITE_OK;
831 }
unsigned char transCntrChng
Definition: tkeyvfs.cc:116
static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt)
Definition: tkeyvfs.cc:673
unsigned char inNormalWrite
Definition: tkeyvfs.cc:118
int lastErrno
Definition: tkeyvfs.cc:106
unsigned char dbUpdate
Definition: tkeyvfs.cc:117
static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt)
Definition: tkeyvfs.cc:642

Variable Documentation

TFile* gRootFile
static
const sqlite3_io_methods*(*const nolockIoFinder) (const char *, unixFile *p)
static
Initial value:
=
static const sqlite3_io_methods * nolockIoFinderImpl(const char *z, unixFile *p)
Definition: tkeyvfs.cc:357

Definition at line 364 of file tkeyvfs.cc.

Referenced by tkeyvfs_init().

const sqlite3_io_methods nolockIoMethods
static
Initial value:
= {
1,
}
static int unixWrite(sqlite3_file *id, const void *pBuf, int amt, sqlite3_int64 offset)
Definition: tkeyvfs.cc:778
static int unixSectorSize(sqlite3_file *NotUsed)
Definition: tkeyvfs.cc:1117
static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2)
Definition: tkeyvfs.cc:981
static int unixTruncate(sqlite3_file *id, i64 nByte)
Definition: tkeyvfs.cc:837
static int unixFileControl(sqlite3_file *id, int op, void *pArg)
Definition: tkeyvfs.cc:1018
static int unixRead(sqlite3_file *id, void *pBuf, int amt, sqlite3_int64 offset)
Definition: tkeyvfs.cc:737
static int nolockLock(sqlite3_file *NotUsed, int NotUsed2)
Definition: tkeyvfs.cc:964
static int nolockClose(sqlite3_file *id)
Definition: tkeyvfs.cc:718
static int unixDeviceCharacteristics(sqlite3_file *NotUsed)
Definition: tkeyvfs.cc:1133
static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut)
Definition: tkeyvfs.cc:998
static int unixSync(sqlite3_file *id, int flags)
Definition: tkeyvfs.cc:919
static int unixFileSize(sqlite3_file *id, i64 *pSize)
Definition: tkeyvfs.cc:938

Definition at line 334 of file tkeyvfs.cc.

Referenced by nolockIoFinderImpl(), and unixOpen().

void(*)(void) unixDlSym(sqlite3_vfs *NotUsed, void *p, const char *zSym)
static

Definition at line 194 of file tkeyvfs.cc.

Referenced by unixDlError().