Classes | Macros | Typedefs | Functions | Variables
myvfs.c File Reference
#include <sqlite3.h>
#include <assert.h>
#include <dlfcn.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.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  unixFileId
 
struct  UnixUnusedFd
 
struct  unixInodeInfo
 
struct  unixFile
 
struct  unix_syscall
 

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 SQLITE_DEFAULT_FILE_PERMISSIONS   0644
 
#define MAX_PATHNAME   512
 
#define SQLITE_TEMP_FILE_PREFIX   "etilqs_"
 
#define UNIXFILE_EXCL   0x01 /* Connections from one process only */
 
#define UNIXFILE_RDONLY   0x02 /* Connection is read only */
 
#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
 
#define osPwrite64
 
#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 unixShmNode unixShmNode
 
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 robust_ftruncate (int h, sqlite3_int64 sz)
 
static int fillInUnixFile (sqlite3_vfs *pVfs, int h, int dirfd, sqlite3_file *pId, const char *zFilename, int noLock, int isDelete, int isReadOnly)
 
static int openDirectory (const char *zFilename, int *pFd)
 
static const char * unixTempFileDir (void)
 
static int unixGetTempname (int nBuf, char *zBuf)
 
static struct UnixUnusedFdfindReusableFd (const char *zPath, int flags)
 
static int findCreateFileMode (const char *zPath, int flags, mode_t *pMode)
 
static int fcntlSizeHint (unixFile *pFile, i64 nByte)
 
static int full_fsync (int fd, int fullSync, int dataOnly)
 
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)
 
int sqlite3_os_init (void)
 
static int closeUnixFile (sqlite3_file *id)
 
int myvfs_init (void)
 

Variables

static struct unixInodeInfoinodeList = 0
 
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) = nolockIoFinderImpl
 

Macro Definition Documentation

#define _FILE_OFFSET_BITS   64

Definition at line 24 of file myvfs.c.

#define _LARGE_FILE   1

Definition at line 23 of file myvfs.c.

#define _LARGEFILE_SOURCE   1

Definition at line 25 of file myvfs.c.

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

Definition at line 19 of file myvfs.c.

#define MAX_PATHNAME   512

Definition at line 50 of file myvfs.c.

#define O_BINARY   0

Definition at line 154 of file myvfs.c.

#define O_LARGEFILE   0

Definition at line 144 of file myvfs.c.

#define O_NOFOLLOW   0

Definition at line 151 of file myvfs.c.

#define osAccess   ((int(*)(const char*,int))aSyscall[2].pCurrent)
#define osClose   ((int(*)(int))aSyscall[1].pCurrent)
#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)
#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
Value:
((ssize_t(*)(int,const void*,size_t,off_t))\
aSyscall[12].pCurrent)
#define osPwrite64
Value:
((ssize_t(*)(int,const void*,size_t,off_t))\
aSyscall[13].pCurrent)
#define osRead   ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent)
#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 224 of file myvfs.c.

#define SQLITE_DEFAULT_FILE_PERMISSIONS   0644

Definition at line 44 of file myvfs.c.

#define SQLITE_DEFAULT_SECTOR_SIZE   512

Definition at line 14 of file myvfs.c.

#define SQLITE_FCNTL_CHUNK_SIZE   6

Definition at line 10 of file myvfs.c.

#define SQLITE_FCNTL_DB_UNCHANGED   0xca093fa0

Definition at line 13 of file myvfs.c.

#define SQLITE_FCNTL_SIZE_HINT   5

Definition at line 9 of file myvfs.c.

#define SQLITE_FCNTL_SYNC_OMITTED   8

Definition at line 11 of file myvfs.c.

#define SQLITE_OPEN_WAL   0x00080000 /* VFS only */

Definition at line 8 of file myvfs.c.

#define SQLITE_TEMP_FILE_PREFIX   "etilqs_"

Definition at line 52 of file myvfs.c.

#define UNIXFILE_EXCL   0x01 /* Connections from one process only */

Definition at line 137 of file myvfs.c.

#define UNIXFILE_RDONLY   0x02 /* Connection is read only */

Definition at line 138 of file myvfs.c.

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

Definition at line 390 of file myvfs.c.

#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 int unixOpen(sqlite3_vfs *pVfs, const char *zPath, sqlite3_file *pFile, int flags, int *pOutFlags)
Definition: myvfs.c:1231
static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3)
Definition: myvfs.c:1632
static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf)
Definition: myvfs.c:1555
static int unixFullPathname(sqlite3_vfs *pVfs, const char *zPath, int nOut, char *zOut)
Definition: myvfs.c:1465
static void * unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename)
Definition: myvfs.c:1498
static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut)
Definition: myvfs.c:1511
static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow)
Definition: myvfs.c:1616
static int unixDelete(sqlite3_vfs *NotUsed, const char *zPath, int dirSync)
Definition: myvfs.c:1390
static int unixSleep(sqlite3_vfs *NotUsed, int microseconds)
Definition: myvfs.c:1604
static int unixAccess(sqlite3_vfs *NotUsed, const char *zPath, int flags, int *pResOut)
Definition: myvfs.c:1424
static void(*)(void) unixDlSym(sqlite3_vfs *NotUsed, void *p, const char *zSym)
Definition: myvfs.c:199
static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle)
Definition: myvfs.c:1546
#define UNUSED_PARAMETER (   x)    (void)(x)

Definition at line 16 of file myvfs.c.

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

Definition at line 17 of file myvfs.c.

Typedef Documentation

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

Definition at line 372 of file myvfs.c.

typedef sqlite_int64 i64

Definition at line 21 of file myvfs.c.

typedef struct unixFile unixFile

Definition at line 109 of file myvfs.c.

typedef struct unixShmNode unixShmNode

Definition at line 54 of file myvfs.c.

Function Documentation

static int closeUnixFile ( sqlite3_file *  id)
static

Definition at line 474 of file myvfs.c.

475 {
476  unixFile * pFile = (unixFile *)id;
477  if (pFile->dirfd >= 0) {
478  robust_close(pFile, pFile->dirfd, __LINE__);
479  pFile->dirfd = -1;
480  }
481  if (pFile->h >= 0) {
482  robust_close(pFile, pFile->h, __LINE__);
483  pFile->h = -1;
484  }
485  sqlite3_free(pFile->pUnused);
486  memset(pFile, 0, sizeof(unixFile));
487  return SQLITE_OK;
488 }
struct UnixUnusedFd * pUnused
Definition: myvfs.c:119
int h
Definition: myvfs.c:113
int dirfd
Definition: myvfs.c:114
static void robust_close(unixFile *pFile, int h, int lineno)
Definition: myvfs.c:444
static int fcntlSizeHint ( unixFile pFile,
i64  nByte 
)
static

Definition at line 776 of file myvfs.c.

777 {
778  if (pFile->szChunk) {
779  i64 nSize; /* Required file size */
780  struct stat buf; /* Used to hold return values of fstat() */
781  if (osFstat(pFile->h, &buf)) {
782  return SQLITE_IOERR_FSTAT;
783  }
784  nSize = ((nByte + pFile->szChunk - 1) / pFile->szChunk) * pFile->szChunk;
785  if (nSize > (i64)buf.st_size) {
786  /* The code below is handling the return value of osFallocate()
787  ** correctly. posix_fallocate() is defined to "returns zero on success,
788  ** or an error number on failure". See the manpage for details. */
789  int err;
790  do {
791  err = osFallocate(pFile->h, buf.st_size, nSize - buf.st_size);
792  }
793  while (err == EINTR);
794  if (err) {
795  return SQLITE_IOERR_WRITE;
796  }
797  }
798  }
799  return SQLITE_OK;
800 }
sqlite_int64 i64
Definition: tkeyvfs.cc:34
#define osFallocate
int h
Definition: myvfs.c:113
#define osFstat
int szChunk
Definition: tkeyvfs.cc:108
static int fillInUnixFile ( sqlite3_vfs *  pVfs,
int  h,
int  dirfd,
sqlite3_file *  pId,
const char *  zFilename,
int  noLock,
int  isDelete,
int  isReadOnly 
)
static

Definition at line 493 of file myvfs.c.

503 {
504  const sqlite3_io_methods * pLockingStyle;
505  unixFile * pNew = (unixFile *)pId;
506  int rc = SQLITE_OK;
507  assert(pNew->pInode == NULL);
508  /* Parameter isDelete is only used on vxworks. Express this explicitly
509  ** here to prevent compiler warnings about unused parameters.
510  */
511  UNUSED_PARAMETER(isDelete);
512  /* Usually the path zFilename should not be a relative pathname. The
513  ** exception is when opening the proxy "conch" file in builds that
514  ** include the special Apple locking styles.
515  */
516  assert(zFilename == 0 || zFilename[0] == '/');
517  pNew->h = h;
518  pNew->dirfd = dirfd;
519  pNew->zPath = zFilename;
520  if (strcmp(pVfs->zName, "unix-excl") == 0) {
521  pNew->ctrlFlags = UNIXFILE_EXCL;
522  }
523  else {
524  pNew->ctrlFlags = 0;
525  }
526  if (isReadOnly) {
527  pNew->ctrlFlags |= UNIXFILE_RDONLY;
528  }
529  pLockingStyle = &nolockIoMethods;
530  pNew->lastErrno = 0;
531  if (rc != SQLITE_OK) {
532  if (dirfd >= 0) {
533  robust_close(pNew, dirfd, __LINE__);
534  }
535  if (h >= 0) {
536  robust_close(pNew, h, __LINE__);
537  }
538  }
539  else {
540  pNew->pMethod = pLockingStyle;
541  }
542  return rc;
543 }
#define UNIXFILE_EXCL
Definition: myvfs.c:137
#define UNUSED_PARAMETER(x)
Definition: myvfs.c:16
#define UNIXFILE_RDONLY
Definition: myvfs.c:138
unsigned char ctrlFlags
Definition: myvfs.c:116
sqlite3_io_methods const * pMethod
Definition: tkeyvfs.cc:97
int h
Definition: myvfs.c:113
int dirfd
Definition: myvfs.c:114
int lastErrno
Definition: tkeyvfs.cc:106
struct unixInodeInfo * pInode
Definition: myvfs.c:112
const char * zPath
Definition: tkeyvfs.cc:107
static const sqlite3_io_methods nolockIoMethods
Definition: myvfs.c:340
static void robust_close(unixFile *pFile, int h, int lineno)
Definition: myvfs.c:444
h
training ###############################
Definition: train_cnn.py:186
static int findCreateFileMode ( const char *  zPath,
int  flags,
mode_t *  pMode 
)
static

Definition at line 722 of file myvfs.c.

727 {
728  int rc = SQLITE_OK; /* Return Code */
729  if (flags & (SQLITE_OPEN_WAL | SQLITE_OPEN_MAIN_JOURNAL)) {
730  char zDb[MAX_PATHNAME + 1]; /* Database file path */
731  int nDb; /* Number of valid bytes in zDb */
732  struct stat sStat; /* Output of stat() on database file */
733  /* zPath is a path to a WAL or journal file. The following block derives
734  ** the path to the associated database file from zPath. This block handles
735  ** the following naming conventions:
736  **
737  ** "<path to db>-journal"
738  ** "<path to db>-wal"
739  ** "<path to db>-journal-NNNN"
740  ** "<path to db>-wal-NNNN"
741  **
742  ** where NNNN is a 4 digit decimal number. The NNNN naming schemes are
743  ** used by the test_multiplex.c module.
744  */
745  nDb = sqlite3Strlen30(zPath) - 1;
746  while (nDb > 0 && zPath[nDb] != 'l') {
747  nDb--;
748  }
749  nDb -= ((flags & SQLITE_OPEN_WAL) ? 3 : 7);
750  memcpy(zDb, zPath, nDb);
751  zDb[nDb] = '\0';
752  if (0 == stat(zDb, &sStat)) {
753  *pMode = sStat.st_mode & 0777;
754  }
755  else {
756  rc = SQLITE_IOERR_FSTAT;
757  }
758  }
759  else if (flags & SQLITE_OPEN_DELETEONCLOSE) {
760  *pMode = 0600;
761  }
762  else {
764  }
765  return rc;
766 }
static int sqlite3Strlen30(const char *z)
Definition: myvfs.c:234
#define MAX_PATHNAME
Definition: myvfs.c:50
#define SQLITE_OPEN_WAL
Definition: myvfs.c:8
#define SQLITE_DEFAULT_FILE_PERMISSIONS
Definition: myvfs.c:44
static struct UnixUnusedFd * findReusableFd ( const char *  zPath,
int  flags 
)
static

Definition at line 667 of file myvfs.c.

667  {
668  struct UnixUnusedFd * pUnused = 0;
669  /* Do not search for an unused file descriptor on vxworks. Not because
670  ** vxworks would not benefit from the change (it might, we're not sure),
671  ** but because no way to test it is currently available. It is better
672  ** not to risk breaking vxworks support for the sake of such an obscure
673  ** feature. */
674  struct stat sStat; /* Results of stat() call */
675  /* A stat() call may fail for various reasons. If this happens, it is
676  ** almost certain that an open() call on the same path will also fail.
677  ** For this reason, if an error occurs in the stat() call here, it is
678  ** ignored and -1 is returned. The caller will try to open a new file
679  ** descriptor on the same path, fail, and return an error to SQLite.
680  **
681  ** Even if a subsequent open() call does succeed, the consequences of
682  ** not searching for a resusable file descriptor are not dire. */
683  if (0 == stat(zPath, &sStat)) {
684  struct unixInodeInfo * pInode;
685  pInode = inodeList;
686  while (pInode && (pInode->fileId.dev != sStat.st_dev
687  || pInode->fileId.ino != sStat.st_ino)) {
688  pInode = pInode->pNext;
689  }
690  if (pInode) {
691  struct UnixUnusedFd ** pp;
692  for (pp = &pInode->pUnused; *pp && (*pp)->flags != flags; pp = &((*pp)->pNext)) {
693  ;
694  }
695  pUnused = *pp;
696  if (pUnused) {
697  *pp = pUnused->pNext;
698  }
699  }
700  }
701  return pUnused;
702 }
struct unixInodeInfo * pNext
Definition: myvfs.c:96
struct UnixUnusedFd * pUnused
Definition: myvfs.c:95
ino_t ino
Definition: myvfs.c:62
static struct unixInodeInfo * inodeList
Definition: myvfs.c:103
struct unixFileId fileId
Definition: myvfs.c:88
struct UnixUnusedFd * pNext
Definition: myvfs.c:75
dev_t dev
Definition: myvfs.c:61
int flags
Definition: myvfs.c:74
static int full_fsync ( int  fd,
int  fullSync,
int  dataOnly 
)
static

Definition at line 826 of file myvfs.c.

827 {
828  int rc;
829  UNUSED_PARAMETER(fullSync);
830  UNUSED_PARAMETER(dataOnly);
831 #if __APPLE__ && __MACH__
832  rc = fcntl(fd, F_FULLFSYNC);
833 #else
834  rc = fdatasync(fd);
835 #endif
836  return rc;
837 }
#define UNUSED_PARAMETER(x)
Definition: myvfs.c:16
int fd
Definition: myvfs.c:73
int myvfs_init ( void  )

Definition at line 1748 of file myvfs.c.

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

Definition at line 1137 of file myvfs.c.

1138 {
1139  UNUSED_PARAMETER(NotUsed);
1140  *pResOut = 0;
1141  return SQLITE_OK;
1142 }
#define UNUSED_PARAMETER(x)
Definition: myvfs.c:16
static int nolockClose ( sqlite3_file *  id)
static

Definition at line 910 of file myvfs.c.

911 {
912  fprintf(stderr, "trace: begin nolockClose ...\n");
913  return closeUnixFile(id);
914 }
static int closeUnixFile(sqlite3_file *id)
Definition: myvfs.c:474
static const sqlite3_io_methods * nolockIoFinderImpl ( const char *  z,
unixFile p 
)
static

Definition at line 362 of file myvfs.c.

363 {
365  UNUSED_PARAMETER(p);
366  return &nolockIoMethods;
367 }
#define UNUSED_PARAMETER(x)
Definition: myvfs.c:16
double z
static const sqlite3_io_methods nolockIoMethods
Definition: myvfs.c:340
static int nolockLock ( sqlite3_file *  NotUsed,
int  NotUsed2 
)
static

Definition at line 1125 of file myvfs.c.

1126 {
1127  UNUSED_PARAMETER2(NotUsed, NotUsed2);
1128  return SQLITE_OK;
1129 }
#define UNUSED_PARAMETER2(x, y)
Definition: myvfs.c:17
static int nolockUnlock ( sqlite3_file *  NotUsed,
int  NotUsed2 
)
static

Definition at line 1131 of file myvfs.c.

1132 {
1133  UNUSED_PARAMETER2(NotUsed, NotUsed2);
1134  return SQLITE_OK;
1135 }
#define UNUSED_PARAMETER2(x, y)
Definition: myvfs.c:17
static int openDirectory ( const char *  zFilename,
int *  pFd 
)
static

Definition at line 555 of file myvfs.c.

556 {
557  int ii;
558  int fd = -1;
559  char zDirname[MAX_PATHNAME + 1];
560  sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
561  for (ii = (int)strlen(zDirname); ii > 1 && zDirname[ii] != '/'; ii--) {
562  ;
563  }
564  if (ii > 0) {
565  zDirname[ii] = '\0';
566  fd = robust_open(zDirname, O_RDONLY | O_BINARY, 0);
567  }
568  *pFd = fd;
569  return (fd >= 0 ? SQLITE_OK : unixLogError(SQLITE_CANTOPEN_BKPT, "open", zDirname));
570 }
#define MAX_PATHNAME
Definition: myvfs.c:50
#define unixLogError(a, b, c)
Definition: myvfs.c:390
#define O_BINARY
Definition: myvfs.c:154
static int robust_open(const char *z, int f, int m)
Definition: myvfs.c:421
#define SQLITE_CANTOPEN_BKPT
Definition: myvfs.c:224
int fd
Definition: myvfs.c:73
static void robust_close ( unixFile pFile,
int  h,
int  lineno 
)
static

Definition at line 444 of file myvfs.c.

445 {
446  if (osClose(h)) {
447  unixLogErrorAtLine(SQLITE_IOERR_CLOSE, "close", pFile ? pFile->zPath : 0, lineno);
448  }
449 }
#define osClose
static int unixLogErrorAtLine(int errcode, const char *zFunc, const char *zPath, int iLine)
Definition: myvfs.c:391
const char * zPath
Definition: tkeyvfs.cc:107
h
training ###############################
Definition: train_cnn.py:186
static int robust_ftruncate ( int  h,
sqlite3_int64  sz 
)
static

Definition at line 454 of file myvfs.c.

455 {
456  int rc;
457  do {
458  rc = osFtruncate(h, sz);
459  }
460  while (rc < 0 && errno == EINTR);
461  return rc;
462 }
#define osFtruncate
h
training ###############################
Definition: train_cnn.py:186
static int robust_open ( const char *  z,
int  f,
int  m 
)
static

Definition at line 421 of file myvfs.c.

422 {
423  int rc;
424  do {
425  rc = osOpen(z, f, m);
426  }
427  while (rc < 0 && errno == EINTR);
428  return rc;
429 }
static const double m
Definition: Units.h:79
#define osOpen
double z
static int seekAndRead ( unixFile id,
sqlite3_int64  offset,
void *  pBuf,
int  cnt 
)
static

Definition at line 852 of file myvfs.c.

853 {
854  int got;
855  i64 newOffset;
856  newOffset = lseek(id->h, offset, SEEK_SET);
857  if (newOffset != offset) {
858  if (newOffset == -1) {
859  ((unixFile *)id)->lastErrno = errno;
860  }
861  else {
862  ((unixFile *)id)->lastErrno = 0;
863  }
864  return -1;
865  }
866  do {
867  got = osRead(id->h, pBuf, cnt);
868  }
869  while (got < 0 && errno == EINTR);
870  if (got < 0) {
871  ((unixFile *)id)->lastErrno = errno;
872  }
873  return got;
874 }
sqlite_int64 i64
Definition: tkeyvfs.cc:34
int h
Definition: myvfs.c:113
#define osRead
static int seekAndWrite ( unixFile id,
i64  offset,
const void *  pBuf,
int  cnt 
)
static

Definition at line 883 of file myvfs.c.

884 {
885  int got;
886  i64 newOffset;
887  newOffset = lseek(id->h, offset, SEEK_SET);
888  if (newOffset != offset) {
889  if (newOffset == -1) {
890  ((unixFile *)id)->lastErrno = errno;
891  }
892  else {
893  ((unixFile *)id)->lastErrno = 0;
894  }
895  return -1;
896  }
897  do {
898  got = osWrite(id->h, pBuf, cnt);
899  }
900  while (got < 0 && errno == EINTR);
901  if (got < 0) {
902  ((unixFile *)id)->lastErrno = errno;
903  }
904  return got;
905 }
sqlite_int64 i64
Definition: tkeyvfs.cc:34
int h
Definition: myvfs.c:113
#define osWrite
int sqlite3_os_init ( void  )
static int sqlite3CantopenError ( int  lineno)
static

Definition at line 212 of file myvfs.c.

213 {
214 #if 0
215  sqlite3_log(SQLITE_CANTOPEN,
216  "cannot open file at line %d of [%.10s]",
217  lineno, 20 + sqlite3_sourceid());
218 #endif /* 0 */
219  fprintf(stderr,
220  "myvfs.c: cannot open file at line %d of [%.10s]",
221  lineno, 20 + sqlite3_sourceid());
222  return SQLITE_CANTOPEN;
223 }
static int sqlite3Strlen30 ( const char *  z)
static

Definition at line 234 of file myvfs.c.

235 {
236  const char * z2 = z;
237  if (z == 0) {
238  return 0;
239  }
240  while (*z2) {
241  z2++;
242  }
243  return 0x3fffffff & (int)(z2 - z);
244 }
double z
static int unixAccess ( sqlite3_vfs *  NotUsed,
const char *  zPath,
int  flags,
int *  pResOut 
)
static

Definition at line 1424 of file myvfs.c.

1430 {
1431  int amode = 0;
1432  UNUSED_PARAMETER(NotUsed);
1433  switch (flags) {
1434  case SQLITE_ACCESS_EXISTS:
1435  amode = F_OK;
1436  break;
1437  case SQLITE_ACCESS_READWRITE:
1438  amode = W_OK | R_OK;
1439  break;
1440  case SQLITE_ACCESS_READ:
1441  amode = R_OK;
1442  break;
1443  default:
1444  assert(!"Invalid flags argument");
1445  }
1446  *pResOut = (osAccess(zPath, amode) == 0);
1447  if (flags == SQLITE_ACCESS_EXISTS && *pResOut) {
1448  struct stat buf;
1449  if (0 == stat(zPath, &buf) && buf.st_size == 0) {
1450  *pResOut = 0;
1451  }
1452  }
1453  return SQLITE_OK;
1454 }
#define UNUSED_PARAMETER(x)
Definition: myvfs.c:16
int flags
Definition: myvfs.c:74
#define osAccess
static int unixCurrentTime ( sqlite3_vfs *  NotUsed,
double *  prNow 
)
static

Definition at line 1616 of file myvfs.c.

1617 {
1618  sqlite3_int64 i;
1619  UNUSED_PARAMETER(NotUsed);
1620  unixCurrentTimeInt64(0, &i);
1621  *prNow = i / 86400000.0;
1622  return 0;
1623 }
static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow)
Definition: myvfs.c:1649
#define UNUSED_PARAMETER(x)
Definition: myvfs.c:16
static int unixCurrentTimeInt64 ( sqlite3_vfs *  NotUsed,
sqlite3_int64 *  piNow 
)
static

Definition at line 1649 of file myvfs.c.

1650 {
1651  static const sqlite3_int64 unixEpoch = 24405875 * (sqlite3_int64)8640000;
1652  struct timeval sNow;
1653  gettimeofday(&sNow, 0);
1654  *piNow = unixEpoch + 1000 * (sqlite3_int64)sNow.tv_sec + sNow.tv_usec / 1000;
1655  UNUSED_PARAMETER(NotUsed);
1656  return 0;
1657 }
#define UNUSED_PARAMETER(x)
Definition: myvfs.c:16
static int unixDelete ( sqlite3_vfs *  NotUsed,
const char *  zPath,
int  dirSync 
)
static

Definition at line 1390 of file myvfs.c.

1395 {
1396  int rc = SQLITE_OK;
1397  UNUSED_PARAMETER(NotUsed);
1398  if (unlink(zPath) == (-1) && errno != ENOENT) {
1399  return unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath);
1400  }
1401  if (dirSync) {
1402  int fd;
1403  rc = openDirectory(zPath, &fd);
1404  if (rc == SQLITE_OK) {
1405  if (fsync(fd)) {
1406  rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath);
1407  }
1408  robust_close(0, fd, __LINE__);
1409  }
1410  }
1411  return rc;
1412 }
#define UNUSED_PARAMETER(x)
Definition: myvfs.c:16
static int openDirectory(const char *zFilename, int *pFd)
Definition: myvfs.c:555
#define unixLogError(a, b, c)
Definition: myvfs.c:390
static void robust_close(unixFile *pFile, int h, int lineno)
Definition: myvfs.c:444
static int unixDeviceCharacteristics ( sqlite3_file *  NotUsed)
static

Definition at line 1200 of file myvfs.c.

1201 {
1202  UNUSED_PARAMETER(NotUsed);
1203  return 0;
1204 }
#define UNUSED_PARAMETER(x)
Definition: myvfs.c:16
static void unixDlClose ( sqlite3_vfs *  NotUsed,
void *  pHandle 
)
static

Definition at line 1546 of file myvfs.c.

1547 {
1548  UNUSED_PARAMETER(NotUsed);
1549  dlclose(pHandle);
1550 }
#define UNUSED_PARAMETER(x)
Definition: myvfs.c:16
static void unixDlError ( sqlite3_vfs *  NotUsed,
int  nBuf,
char *  zBufOut 
)
static

Definition at line 1511 of file myvfs.c.

1512 {
1513  const char * zErr;
1514  UNUSED_PARAMETER(NotUsed);
1515  zErr = dlerror();
1516  if (zErr) {
1517  sqlite3_snprintf(nBuf, zBufOut, "%s", zErr);
1518  }
1519 }
#define UNUSED_PARAMETER(x)
Definition: myvfs.c:16
static void * unixDlOpen ( sqlite3_vfs *  NotUsed,
const char *  zFilename 
)
static

Definition at line 1498 of file myvfs.c.

1499 {
1500  UNUSED_PARAMETER(NotUsed);
1501  return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
1502 }
#define UNUSED_PARAMETER(x)
Definition: myvfs.c:16
static int unixFileControl ( sqlite3_file *  id,
int  op,
void *  pArg 
)
static

Definition at line 1147 of file myvfs.c.

1148 {
1149  switch (op) {
1150  case SQLITE_FCNTL_LOCKSTATE: {
1151  *(int *)pArg = ((unixFile *)id)->eFileLock;
1152  return SQLITE_OK;
1153  }
1154  case SQLITE_LAST_ERRNO: {
1155  *(int *)pArg = ((unixFile *)id)->lastErrno;
1156  return SQLITE_OK;
1157  }
1158  case SQLITE_FCNTL_CHUNK_SIZE: {
1159  ((unixFile *)id)->szChunk = *(int *)pArg;
1160  return SQLITE_OK;
1161  }
1162  case SQLITE_FCNTL_SIZE_HINT: {
1163  return fcntlSizeHint((unixFile *)id, *(i64 *)pArg);
1164  }
1165  /* The pager calls this method to signal that it has done
1166  ** a rollback and that the database is therefore unchanged and
1167  ** it hence it is OK for the transaction change counter to be
1168  ** unchanged.
1169  */
1171  ((unixFile *)id)->dbUpdate = 0;
1172  return SQLITE_OK;
1173  }
1175  return SQLITE_OK; /* A no-op */
1176  }
1177  }
1178  return SQLITE_NOTFOUND;
1179 }
sqlite_int64 i64
Definition: tkeyvfs.cc:34
#define SQLITE_FCNTL_SYNC_OMITTED
Definition: myvfs.c:11
#define SQLITE_FCNTL_SIZE_HINT
Definition: myvfs.c:9
static int fcntlSizeHint(unixFile *pFile, i64 nByte)
Definition: myvfs.c:776
#define SQLITE_FCNTL_DB_UNCHANGED
Definition: myvfs.c:13
#define SQLITE_FCNTL_CHUNK_SIZE
Definition: myvfs.c:10
static int unixFileSize ( sqlite3_file *  id,
i64 pSize 
)
static

Definition at line 1102 of file myvfs.c.

1103 {
1104  int rc;
1105  struct stat buf;
1106  assert(id);
1107  rc = osFstat(((unixFile *)id)->h, &buf);
1108  if (rc != 0) {
1109  ((unixFile *)id)->lastErrno = errno;
1110  return SQLITE_IOERR_FSTAT;
1111  }
1112  *pSize = buf.st_size;
1113  /* When opening a zero-size database, the findInodeInfo() procedure
1114  ** writes a single byte into that file in order to work around a bug
1115  ** in the OS-X msdos filesystem. In order to avoid problems with upper
1116  ** layers, we need to report this file size as zero even though it is
1117  ** really 1. Ticket #3260.
1118  */
1119  if (*pSize == 1) {
1120  *pSize = 0;
1121  }
1122  return SQLITE_OK;
1123 }
#define osFstat
h
training ###############################
Definition: train_cnn.py:186
static int unixFullPathname ( sqlite3_vfs *  pVfs,
const char *  zPath,
int  nOut,
char *  zOut 
)
static

Definition at line 1465 of file myvfs.c.

1471 {
1472  /* It's odd to simulate an io-error here, but really this is just
1473  ** using the io-error infrastructure to test that SQLite handles this
1474  ** function failing. This function could fail if, for example, the
1475  ** current working directory has been unlinked.
1476  */
1477  assert(pVfs->mxPathname == MAX_PATHNAME);
1478  UNUSED_PARAMETER(pVfs);
1479  zOut[nOut - 1] = '\0';
1480  if (zPath[0] == '/') {
1481  sqlite3_snprintf(nOut, zOut, "%s", zPath);
1482  }
1483  else {
1484  int nCwd;
1485  if (osGetcwd(zOut, nOut - 1) == 0) {
1486  return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath);
1487  }
1488  nCwd = (int)strlen(zOut);
1489  sqlite3_snprintf(nOut - nCwd, &zOut[nCwd], "/%s", zPath);
1490  }
1491  return SQLITE_OK;
1492 }
#define UNUSED_PARAMETER(x)
Definition: myvfs.c:16
#define MAX_PATHNAME
Definition: myvfs.c:50
#define unixLogError(a, b, c)
Definition: myvfs.c:390
#define SQLITE_CANTOPEN_BKPT
Definition: myvfs.c:224
#define osGetcwd
static int unixGetLastError ( sqlite3_vfs *  NotUsed,
int  NotUsed2,
char *  NotUsed3 
)
static

Definition at line 1632 of file myvfs.c.

1633 {
1634  UNUSED_PARAMETER(NotUsed);
1635  UNUSED_PARAMETER(NotUsed2);
1636  UNUSED_PARAMETER(NotUsed3);
1637  return 0;
1638 }
#define UNUSED_PARAMETER(x)
Definition: myvfs.c:16
static sqlite3_syscall_ptr unixGetSystemCall ( sqlite3_vfs *  pNotUsed,
const char *  zName 
)
static

Definition at line 1711 of file myvfs.c.

1712 {
1713  unsigned int i;
1714  UNUSED_PARAMETER(pNotUsed);
1715  for (i = 0; i < sizeof(aSyscall) / sizeof(aSyscall[0]); i++) {
1716  if (strcmp(zName, aSyscall[i].zName) == 0) {
1717  return aSyscall[i].pCurrent;
1718  }
1719  }
1720  return 0;
1721 }
#define UNUSED_PARAMETER(x)
Definition: myvfs.c:16
sqlite3_syscall_ptr pCurrent
Definition: tkeyvfs.cc:248
static struct unix_syscall aSyscall[]
static int unixGetTempname ( int  nBuf,
char *  zBuf 
)
static

Definition at line 616 of file myvfs.c.

617 {
618  static const unsigned char zChars[] =
619  "abcdefghijklmnopqrstuvwxyz"
620  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
621  "0123456789";
622  unsigned int i, j;
623  const char * zDir;
624  /* It's odd to simulate an io-error here, but really this is just
625  ** using the io-error infrastructure to test that SQLite handles this
626  ** function failing.
627  */
628  zDir = unixTempFileDir();
629  if (zDir == 0) {
630  zDir = ".";
631  }
632  /* Check that the output buffer is large enough for the temporary file
633  ** name. If it is not, return SQLITE_ERROR.
634  */
635  if ((strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 17) >= (size_t)nBuf) {
636  return SQLITE_ERROR;
637  }
638  do {
639  sqlite3_snprintf(nBuf - 17, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir);
640  j = (int)strlen(zBuf);
641  sqlite3_randomness(15, &zBuf[j]);
642  for (i = 0; i < 15; i++, j++) {
643  zBuf[j] = (char)zChars[((unsigned char)zBuf[j]) % (sizeof(zChars) - 1) ];
644  }
645  zBuf[j] = 0;
646  }
647  while (osAccess(zBuf, 0) == 0);
648  return SQLITE_OK;
649 }
#define SQLITE_TEMP_FILE_PREFIX
Definition: myvfs.c:52
static const char * unixTempFileDir(void)
Definition: myvfs.c:576
#define osAccess
static int unixLogErrorAtLine ( int  errcode,
const char *  zFunc,
const char *  zPath,
int  iLine 
)
static

Definition at line 391 of file myvfs.c.

397 {
398  char * zErr; /* Message from strerror() or equivalent */
399  int iErrno = errno; /* Saved syscall error number */
400  zErr = strerror(iErrno);
401  assert(errcode != SQLITE_OK);
402  if (zPath == 0) {
403  zPath = "";
404  }
405 #if 0
406  sqlite3_log(errcode,
407  "os_unix.c:%d: (%d) %s(%s) - %s",
408  iLine, iErrno, zFunc, zPath, zErr
409  );
410 #endif /* 0 */
411  fprintf(stderr,
412  "myvfs.c:%d: (%d) %s(%s) - %s",
413  iLine, iErrno, zFunc, zPath, zErr
414  );
415  return errcode;
416 }
static const char * unixNextSystemCall ( sqlite3_vfs *  p,
const char *  zName 
)
static

Definition at line 1729 of file myvfs.c.

1730 {
1731  int i = -1;
1733  if (zName) {
1734  for (i = 0; i < ArraySize(aSyscall) - 1; i++) {
1735  if (strcmp(zName, aSyscall[i].zName) == 0) {
1736  break;
1737  }
1738  }
1739  }
1740  for (i++; i < ArraySize(aSyscall); i++) {
1741  if (aSyscall[i].pCurrent != 0) {
1742  return aSyscall[i].zName;
1743  }
1744  }
1745  return 0;
1746 }
const char * zName
Definition: tkeyvfs.cc:247
#define UNUSED_PARAMETER(x)
Definition: myvfs.c:16
p
Definition: test.py:228
#define ArraySize(X)
Definition: myvfs.c:19
static struct unix_syscall aSyscall[]
static int unixOpen ( sqlite3_vfs *  pVfs,
const char *  zPath,
sqlite3_file *  pFile,
int  flags,
int *  pOutFlags 
)
static

Definition at line 1231 of file myvfs.c.

1238 {
1239  unixFile * p = (unixFile *)pFile;
1240  int fd = -1; /* File descriptor returned by open() */
1241  int dirfd = -1; /* Directory file descriptor */
1242  int openFlags = 0; /* Flags to pass to open() */
1243  int eType = flags & 0xFFFFFF00; /* Type of file to open */
1244  int noLock; /* True to omit locking primitives */
1245  int rc = SQLITE_OK; /* Function Return Code */
1246  int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
1247  int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
1248  int isCreate = (flags & SQLITE_OPEN_CREATE);
1249  int isReadonly = (flags & SQLITE_OPEN_READONLY);
1250  int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
1251  /* If creating a master or main-file journal, this function will open
1252  ** a file-descriptor on the directory too. The first time unixSync()
1253  ** is called the directory file descriptor will be fsync()ed and close()d.
1254  */
1255  int isOpenDirectory = (isCreate && (
1256  eType == SQLITE_OPEN_MASTER_JOURNAL
1257  || eType == SQLITE_OPEN_MAIN_JOURNAL
1258  || eType == SQLITE_OPEN_WAL
1259  ));
1260  /* If argument zPath is a NULL pointer, this function is required to open
1261  ** a temporary file. Use this buffer to store the file name in.
1262  */
1263  char zTmpname[MAX_PATHNAME + 1];
1264  const char * zName = zPath;
1265  fprintf(stderr, "trace: begin unixOpen ...\n");
1266  /* Check the following statements are true:
1267  **
1268  ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
1269  ** (b) if CREATE is set, then READWRITE must also be set, and
1270  ** (c) if EXCLUSIVE is set, then CREATE must also be set.
1271  ** (d) if DELETEONCLOSE is set, then CREATE must also be set.
1272  */
1273  assert((isReadonly == 0 || isReadWrite == 0) && (isReadWrite || isReadonly));
1274  assert(isCreate == 0 || isReadWrite);
1275  assert(isExclusive == 0 || isCreate);
1276  assert(isDelete == 0 || isCreate);
1277  /* The main DB, main journal, WAL file and master journal are never
1278  ** automatically deleted. Nor are they ever temporary files. */
1279  assert((!isDelete && zName) || eType != SQLITE_OPEN_MAIN_DB);
1280  assert((!isDelete && zName) || eType != SQLITE_OPEN_MAIN_JOURNAL);
1281  assert((!isDelete && zName) || eType != SQLITE_OPEN_MASTER_JOURNAL);
1282  assert((!isDelete && zName) || eType != SQLITE_OPEN_WAL);
1283  /* Assert that the upper layer has set one of the "file-type" flags. */
1284  assert(eType == SQLITE_OPEN_MAIN_DB || eType == SQLITE_OPEN_TEMP_DB
1285  || eType == SQLITE_OPEN_MAIN_JOURNAL || eType == SQLITE_OPEN_TEMP_JOURNAL
1286  || eType == SQLITE_OPEN_SUBJOURNAL || eType == SQLITE_OPEN_MASTER_JOURNAL
1287  || eType == SQLITE_OPEN_TRANSIENT_DB || eType == SQLITE_OPEN_WAL
1288  );
1289  memset(p, 0, sizeof(unixFile));
1290  if (eType == SQLITE_OPEN_MAIN_DB) {
1291  struct UnixUnusedFd * pUnused;
1292  pUnused = findReusableFd(zName, flags);
1293  if (pUnused) {
1294  fd = pUnused->fd;
1295  }
1296  else {
1297  pUnused = sqlite3_malloc(sizeof(*pUnused));
1298  if (!pUnused) {
1299  return SQLITE_NOMEM;
1300  }
1301  }
1302  p->pUnused = pUnused;
1303  }
1304  else if (!zName) {
1305  /* If zName is NULL, the upper layer is requesting a temp file. */
1306  assert(isDelete && !isOpenDirectory);
1307  rc = unixGetTempname(MAX_PATHNAME + 1, zTmpname);
1308  if (rc != SQLITE_OK) {
1309  return rc;
1310  }
1311  zName = zTmpname;
1312  }
1313  /* Determine the value of the flags parameter passed to POSIX function
1314  ** open(). These must be calculated even if open() is not called, as
1315  ** they may be stored as part of the file handle and used by the
1316  ** 'conch file' locking functions later on. */
1317  if (isReadonly) {
1318  openFlags |= O_RDONLY;
1319  }
1320  if (isReadWrite) {
1321  openFlags |= O_RDWR;
1322  }
1323  if (isCreate) {
1324  openFlags |= O_CREAT;
1325  }
1326  if (isExclusive) {
1327  openFlags |= (O_EXCL | O_NOFOLLOW);
1328  }
1329  openFlags |= (O_LARGEFILE | O_BINARY);
1330  if (fd < 0) {
1331  mode_t openMode; /* Permissions to create file with */
1332  rc = findCreateFileMode(zName, flags, &openMode);
1333  if (rc != SQLITE_OK) {
1334  assert(!p->pUnused);
1335  assert(eType == SQLITE_OPEN_WAL || eType == SQLITE_OPEN_MAIN_JOURNAL);
1336  return rc;
1337  }
1338  fd = robust_open(zName, openFlags, openMode);
1339  if (fd < 0 && errno != EISDIR && isReadWrite && !isExclusive) {
1340  /* Failed to open the file for read/write access. Try read-only. */
1341  flags &= ~(SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
1342  openFlags &= ~(O_RDWR | O_CREAT);
1343  flags |= SQLITE_OPEN_READONLY;
1344  openFlags |= O_RDONLY;
1345  isReadonly = 1;
1346  fd = robust_open(zName, openFlags, openMode);
1347  }
1348  if (fd < 0) {
1349  rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName);
1350  goto open_finished;
1351  }
1352  }
1353  assert(fd >= 0);
1354  if (pOutFlags) {
1355  *pOutFlags = flags;
1356  }
1357  if (p->pUnused) {
1358  p->pUnused->fd = fd;
1359  p->pUnused->flags = flags;
1360  }
1361  if (isDelete) {
1362  unlink(zName);
1363  }
1364  if (isOpenDirectory) {
1365  rc = openDirectory(zPath, &dirfd);
1366  if (rc != SQLITE_OK) {
1367  /* It is safe to close fd at this point, because it is guaranteed not
1368  ** to be open on a database file. If it were open on a database file,
1369  ** it would not be safe to close as this would release any locks held
1370  ** on the file by this process. */
1371  assert(eType != SQLITE_OPEN_MAIN_DB);
1372  robust_close(p, fd, __LINE__);
1373  goto open_finished;
1374  }
1375  }
1376  noLock = eType != SQLITE_OPEN_MAIN_DB;
1377  rc = fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock,
1378  isDelete, isReadonly);
1379 open_finished:
1380  if (rc != SQLITE_OK) {
1381  sqlite3_free(p->pUnused);
1382  }
1383  return rc;
1384 }
struct UnixUnusedFd * pUnused
Definition: myvfs.c:119
static int fillInUnixFile(sqlite3_vfs *pVfs, int h, int dirfd, sqlite3_file *pId, const char *zFilename, int noLock, int isDelete, int isReadOnly)
Definition: myvfs.c:493
static int openDirectory(const char *zFilename, int *pFd)
Definition: myvfs.c:555
#define MAX_PATHNAME
Definition: myvfs.c:50
#define unixLogError(a, b, c)
Definition: myvfs.c:390
static int findCreateFileMode(const char *zPath, int flags, mode_t *pMode)
Definition: myvfs.c:722
#define O_BINARY
Definition: myvfs.c:154
#define SQLITE_OPEN_WAL
Definition: myvfs.c:8
static struct UnixUnusedFd * findReusableFd(const char *zPath, int flags)
Definition: myvfs.c:667
static int robust_open(const char *z, int f, int m)
Definition: myvfs.c:421
static int unixGetTempname(int nBuf, char *zBuf)
Definition: myvfs.c:616
#define SQLITE_CANTOPEN_BKPT
Definition: myvfs.c:224
p
Definition: test.py:228
#define O_LARGEFILE
Definition: myvfs.c:144
int flags
Definition: myvfs.c:74
int fd
Definition: myvfs.c:73
static void robust_close(unixFile *pFile, int h, int lineno)
Definition: myvfs.c:444
#define O_NOFOLLOW
Definition: myvfs.c:151
static int unixRandomness ( sqlite3_vfs *  NotUsed,
int  nBuf,
char *  zBuf 
)
static

Definition at line 1555 of file myvfs.c.

1556 {
1557  UNUSED_PARAMETER(NotUsed);
1558  assert((size_t)nBuf >= (sizeof(time_t) + sizeof(int)));
1559  /* We have to initialize zBuf to prevent valgrind from reporting
1560  ** errors. The reports issued by valgrind are incorrect - we would
1561  ** prefer that the randomness be increased by making use of the
1562  ** uninitialized space in zBuf - but valgrind errors tend to worry
1563  ** some users. Rather than argue, it seems easier just to initialize
1564  ** the whole array and silence valgrind, even if that means less randomness
1565  ** in the random seed.
1566  **
1567  ** When testing, initializing zBuf[] to zero is all we do. That means
1568  ** that we always use the same random number sequence. This makes the
1569  ** tests repeatable.
1570  */
1571  memset(zBuf, 0, nBuf);
1572  {
1573  int pid, fd;
1574  fd = robust_open("/dev/urandom", O_RDONLY, 0);
1575  if (fd < 0) {
1576  time_t t;
1577  time(&t);
1578  memcpy(zBuf, &t, sizeof(t));
1579  pid = getpid();
1580  memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid));
1581  assert(sizeof(t) + sizeof(pid) <= (size_t)nBuf);
1582  nBuf = sizeof(t) + sizeof(pid);
1583  }
1584  else {
1585  do {
1586  nBuf = osRead(fd, zBuf, nBuf);
1587  }
1588  while (nBuf < 0 && errno == EINTR);
1589  robust_close(0, fd, __LINE__);
1590  }
1591  }
1592  return nBuf;
1593 }
#define UNUSED_PARAMETER(x)
Definition: myvfs.c:16
static int robust_open(const char *z, int f, int m)
Definition: myvfs.c:421
#define osRead
int fd
Definition: myvfs.c:73
static void robust_close(unixFile *pFile, int h, int lineno)
Definition: myvfs.c:444
static int unixRead ( sqlite3_file *  id,
void *  pBuf,
int  amt,
sqlite3_int64  offset 
)
static

Definition at line 921 of file myvfs.c.

922 {
923  unixFile * pFile = (unixFile *)id;
924  int got;
925  fprintf(stderr, "trace: begin unixRead ...\n");
926  assert(id);
927  /* If this is a database file (not a journal, master-journal or temp
928  ** file), the bytes in the locking range should never be read or written. */
929 #if 0
930  assert(pFile->pUnused == 0
931  || offset >= PENDING_BYTE + 512
932  || offset + amt <= PENDING_BYTE
933  );
934 #endif
935  got = seekAndRead(pFile, offset, pBuf, amt);
936  if (got == amt) {
937  return SQLITE_OK;
938  }
939  else if (got < 0) {
940  /* lastErrno set by seekAndRead */
941  return SQLITE_IOERR_READ;
942  }
943  else {
944  pFile->lastErrno = 0; /* not a system error */
945  /* Unread parts of the buffer must be zero-filled */
946  memset(&((char *)pBuf)[got], 0, amt - got);
947  return SQLITE_IOERR_SHORT_READ;
948  }
949 }
struct UnixUnusedFd * pUnused
Definition: myvfs.c:119
static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt)
Definition: myvfs.c:852
int lastErrno
Definition: tkeyvfs.cc:106
static int unixSectorSize ( sqlite3_file *  NotUsed)
static

Definition at line 1191 of file myvfs.c.

1192 {
1193  UNUSED_PARAMETER(NotUsed);
1195 }
#define SQLITE_DEFAULT_SECTOR_SIZE
Definition: myvfs.c:14
#define UNUSED_PARAMETER(x)
Definition: myvfs.c:16
static int unixSetSystemCall ( sqlite3_vfs *  pNotUsed,
const char *  zName,
sqlite3_syscall_ptr  pNewFunc 
)
static

Definition at line 1665 of file myvfs.c.

1670 {
1671  unsigned int i;
1672  int rc = SQLITE_NOTFOUND;
1673  UNUSED_PARAMETER(pNotUsed);
1674  if (zName == 0) {
1675  /* If no zName is given, restore all system calls to their default
1676  ** settings and return NULL
1677  */
1678  rc = SQLITE_OK;
1679  for (i = 0; i < sizeof(aSyscall) / sizeof(aSyscall[0]); i++) {
1680  if (aSyscall[i].pDefault) {
1682  }
1683  }
1684  }
1685  else {
1686  /* If zName is specified, operate on only the one system call
1687  ** specified.
1688  */
1689  for (i = 0; i < sizeof(aSyscall) / sizeof(aSyscall[0]); i++) {
1690  if (strcmp(zName, aSyscall[i].zName) == 0) {
1691  if (aSyscall[i].pDefault == 0) {
1693  }
1694  rc = SQLITE_OK;
1695  if (pNewFunc == 0) {
1696  pNewFunc = aSyscall[i].pDefault;
1697  }
1698  aSyscall[i].pCurrent = pNewFunc;
1699  break;
1700  }
1701  }
1702  }
1703  return rc;
1704 }
sqlite3_syscall_ptr pDefault
Definition: tkeyvfs.cc:249
#define UNUSED_PARAMETER(x)
Definition: myvfs.c:16
sqlite3_syscall_ptr pCurrent
Definition: tkeyvfs.cc:248
static struct unix_syscall aSyscall[]
static int unixSleep ( sqlite3_vfs *  NotUsed,
int  microseconds 
)
static

Definition at line 1604 of file myvfs.c.

1605 {
1606  usleep(microseconds);
1607  UNUSED_PARAMETER(NotUsed);
1608  return microseconds;
1609 }
#define UNUSED_PARAMETER(x)
Definition: myvfs.c:16
static int unixSync ( sqlite3_file *  id,
int  flags 
)
static

Definition at line 1057 of file myvfs.c.

1058 {
1059  int rc;
1060  unixFile * pFile = (unixFile *)id;
1061  int isDataOnly = (flags & SQLITE_SYNC_DATAONLY);
1062  int isFullsync = (flags & 0x0F) == SQLITE_SYNC_FULL;
1063  fprintf(stderr, "trace: begin unixSync ...\n");
1064  /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
1065  assert((flags & 0x0F) == SQLITE_SYNC_NORMAL
1066  || (flags & 0x0F) == SQLITE_SYNC_FULL
1067  );
1068  /* Unix cannot, but some systems may return SQLITE_FULL from here. This
1069  ** line is to test that doing so does not cause any problems.
1070  */
1071  assert(pFile);
1072  rc = full_fsync(pFile->h, isFullsync, isDataOnly);
1073  if (rc) {
1074  pFile->lastErrno = errno;
1075  return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath);
1076  }
1077  if (pFile->dirfd >= 0) {
1078  /* The directory sync is only attempted if full_fsync is
1079  ** turned off or unavailable. If a full_fsync occurred above,
1080  ** then the directory sync is superfluous.
1081  */
1082  if (full_fsync(pFile->dirfd, 0, 0)) {
1083  /*
1084  ** We have received multiple reports of fsync() returning
1085  ** errors when applied to directories on certain file systems.
1086  ** A failed directory sync is not a big deal. So it seems
1087  ** better to ignore the error. Ticket #1657
1088  */
1089  /* pFile->lastErrno = errno; */
1090  /* return SQLITE_IOERR; */
1091  }
1092  /* Only need to sync once, so close the directory when we are done */
1093  robust_close(pFile, pFile->dirfd, __LINE__);
1094  pFile->dirfd = -1;
1095  }
1096  return rc;
1097 }
static int full_fsync(int fd, int fullSync, int dataOnly)
Definition: myvfs.c:826
#define unixLogError(a, b, c)
Definition: myvfs.c:390
int h
Definition: myvfs.c:113
int dirfd
Definition: myvfs.c:114
int lastErrno
Definition: tkeyvfs.cc:106
const char * zPath
Definition: tkeyvfs.cc:107
int flags
Definition: myvfs.c:74
static void robust_close(unixFile *pFile, int h, int lineno)
Definition: myvfs.c:444
static const char * unixTempFileDir ( void  )
static

Definition at line 576 of file myvfs.c.

577 {
578  static const char * azDirs[] = {
579  0,
580  0,
581  "/var/tmp",
582  "/usr/tmp",
583  "/tmp",
584  0 /* List terminator */
585  };
586  unsigned int i;
587  struct stat buf;
588  const char * zDir = 0;
589  azDirs[0] = sqlite3_temp_directory;
590  if (!azDirs[1]) {
591  azDirs[1] = getenv("TMPDIR");
592  }
593  for (i = 0; i < sizeof(azDirs) / sizeof(azDirs[0]); zDir = azDirs[i++]) {
594  if (zDir == 0) {
595  continue;
596  }
597  if (osStat(zDir, &buf)) {
598  continue;
599  }
600  if (!S_ISDIR(buf.st_mode)) {
601  continue;
602  }
603  if (osAccess(zDir, 07)) {
604  continue;
605  }
606  break;
607  }
608  return zDir;
609 }
std::string getenv(std::string const &name)
Definition: getenv.cc:15
#define osStat
#define osAccess
static int unixTruncate ( sqlite3_file *  id,
i64  nByte 
)
static

Definition at line 1008 of file myvfs.c.

1009 {
1010  unixFile * pFile = (unixFile *)id;
1011  int rc;
1012  fprintf(stderr, "trace: begin unixTruncate ...\n");
1013  assert(pFile);
1014  /* If the user has configured a chunk-size for this file, truncate the
1015  ** file so that it consists of an integer number of chunks (i.e. the
1016  ** actual file size after the operation may be larger than the requested
1017  ** size).
1018  */
1019  if (pFile->szChunk) {
1020  nByte = ((nByte + pFile->szChunk - 1) / pFile->szChunk) * pFile->szChunk;
1021  }
1022  rc = robust_ftruncate(pFile->h, (off_t)nByte);
1023  if (rc) {
1024  pFile->lastErrno = errno;
1025  return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
1026  }
1027  else {
1028  /* If we are doing a normal write to a database file (as opposed to
1029  ** doing a hot-journal rollback or a write to some file other than a
1030  ** normal database file) and we truncate the file to zero length,
1031  ** that effectively updates the change counter. This might happen
1032  ** when restoring a database using the backup API from a zero-length
1033  ** source.
1034  */
1035  if (pFile->inNormalWrite && nByte == 0) {
1036  pFile->transCntrChng = 1;
1037  }
1038  return SQLITE_OK;
1039  }
1040 }
unsigned char transCntrChng
Definition: tkeyvfs.cc:116
static int robust_ftruncate(int h, sqlite3_int64 sz)
Definition: myvfs.c:454
#define unixLogError(a, b, c)
Definition: myvfs.c:390
unsigned char inNormalWrite
Definition: tkeyvfs.cc:118
int h
Definition: myvfs.c:113
int lastErrno
Definition: tkeyvfs.cc:106
const char * zPath
Definition: tkeyvfs.cc:107
int szChunk
Definition: tkeyvfs.cc:108
static int unixWrite ( sqlite3_file *  id,
const void *  pBuf,
int  amt,
sqlite3_int64  offset 
)
static

Definition at line 955 of file myvfs.c.

956 {
957  unixFile * pFile = (unixFile *)id;
958  int wrote = 0;
959  fprintf(stderr, "trace: begin unixWrite ...\n");
960  assert(id);
961  assert(amt > 0);
962  /* If this is a database file (not a journal, master-journal or temp
963  ** file), the bytes in the locking range should never be read or written. */
964 #if 0
965  assert(pFile->pUnused == 0
966  || offset >= PENDING_BYTE + 512
967  || offset + amt <= PENDING_BYTE
968  );
969 #endif
970  /* If we are doing a normal write to a database file (as opposed to
971  ** doing a hot-journal rollback or a write to some file other than a
972  ** normal database file) then record the fact that the database
973  ** has changed. If the transaction counter is modified, record that
974  ** fact too.
975  */
976  if (pFile->inNormalWrite) {
977  pFile->dbUpdate = 1; /* The database has been modified */
978  if (offset <= 24 && offset + amt >= 27) {
979  int rc;
980  char oldCntr[4];
981  rc = seekAndRead(pFile, 24, oldCntr, 4);
982  if (rc != 4 || memcmp(oldCntr, &((char *)pBuf)[24 - offset], 4) != 0) {
983  pFile->transCntrChng = 1; /* The transaction counter has changed */
984  }
985  }
986  }
987  while (amt > 0 && (wrote = seekAndWrite(pFile, offset, pBuf, amt)) > 0) {
988  amt -= wrote;
989  offset += wrote;
990  pBuf = &((char *)pBuf)[wrote];
991  }
992  if (amt > 0) {
993  if (wrote < 0) {
994  /* lastErrno set by seekAndWrite */
995  return SQLITE_IOERR_WRITE;
996  }
997  else {
998  pFile->lastErrno = 0; /* not a system error */
999  return SQLITE_FULL;
1000  }
1001  }
1002  return SQLITE_OK;
1003 }
unsigned char transCntrChng
Definition: tkeyvfs.cc:116
struct UnixUnusedFd * pUnused
Definition: myvfs.c:119
static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt)
Definition: myvfs.c:852
unsigned char inNormalWrite
Definition: tkeyvfs.cc:118
int lastErrno
Definition: tkeyvfs.cc:106
static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt)
Definition: myvfs.c:883
unsigned char dbUpdate
Definition: tkeyvfs.cc:117

Variable Documentation

struct unix_syscall aSyscall[]
static
struct unixInodeInfo* inodeList = 0
static

Definition at line 103 of file myvfs.c.

const sqlite3_io_methods*(*const nolockIoFinder) (const char *, unixFile *p) = nolockIoFinderImpl
static

Definition at line 369 of file myvfs.c.

const sqlite3_io_methods nolockIoMethods
static
Initial value:
= {
1,
}
static int unixFileSize(sqlite3_file *id, i64 *pSize)
Definition: myvfs.c:1102
static int unixRead(sqlite3_file *id, void *pBuf, int amt, sqlite3_int64 offset)
Definition: myvfs.c:921
static int unixSync(sqlite3_file *id, int flags)
Definition: myvfs.c:1057
static int unixSectorSize(sqlite3_file *NotUsed)
Definition: myvfs.c:1191
static int unixWrite(sqlite3_file *id, const void *pBuf, int amt, sqlite3_int64 offset)
Definition: myvfs.c:955
static int unixTruncate(sqlite3_file *id, i64 nByte)
Definition: myvfs.c:1008
static int unixDeviceCharacteristics(sqlite3_file *NotUsed)
Definition: myvfs.c:1200
static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2)
Definition: myvfs.c:1131
static int nolockLock(sqlite3_file *NotUsed, int NotUsed2)
Definition: myvfs.c:1125
static int unixFileControl(sqlite3_file *id, int op, void *pArg)
Definition: myvfs.c:1147
static int nolockClose(sqlite3_file *id)
Definition: myvfs.c:910
static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut)
Definition: myvfs.c:1137

Definition at line 340 of file myvfs.c.

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

Definition at line 199 of file myvfs.c.