aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/os_unix.c62
-rw-r--r--src/sqlite.h.in3
-rw-r--r--src/test_devsym.c12
-rw-r--r--src/wal.c19
4 files changed, 47 insertions, 49 deletions
diff --git a/src/os_unix.c b/src/os_unix.c
index 314ad9dd7..986963a18 100644
--- a/src/os_unix.c
+++ b/src/os_unix.c
@@ -4923,7 +4923,15 @@ static void unixShmPurge(void){
}
/*
-** Open a shared-memory area. This implementation uses mmapped files.
+** Open a shared-memory area. This particular implementation uses
+** mmapped files.
+**
+** zName is a filename used to identify the shared-memory area. The
+** implementation does not (and perhaps should not) use this name
+** directly, but rather use it as a template for finding an appropriate
+** name for the shared-memory storage. In this implementation, the
+** string "-index" is appended to zName and used as the name of the
+** mmapped file.
**
** When opening a new shared-memory file, if no other instances of that
** file are currently open, in this process or in other processes, then
@@ -4931,25 +4939,38 @@ static void unixShmPurge(void){
*/
static int unixShmOpen(
sqlite3_vfs *pVfs, /* The VFS */
- const char *zName, /* Name of file to mmap */
+ const char *zName, /* Base name of file to mmap */
sqlite3_shm **pShm /* Write the unixShm object created here */
){
struct unixShm *p = 0; /* The connection to be opened */
struct unixShmFile *pFile = 0; /* The underlying mmapped file */
int rc; /* Result code */
struct unixFileId fid; /* Unix file identifier */
+ struct unixShmFile *pNew; /* Newly allocated pFile */
struct stat sStat; /* Result from stat() an fstat() */
+ int nName; /* Size of zName in bytes */
- /* Allocate space for the new sqlite3_shm object */
+ /* Allocate space for the new sqlite3_shm object. Also speculatively
+ ** allocate space for a new unixShmFile and filename.
+ */
p = sqlite3_malloc( sizeof(*p) );
if( p==0 ) return SQLITE_NOMEM;
memset(p, 0, sizeof(*p));
+ nName = strlen(zName);
+ pNew = sqlite3_malloc( sizeof(*pFile) + nName + 10 );
+ if( pNew==0 ){
+ rc = SQLITE_NOMEM;
+ goto shm_open_err;
+ }
+ memset(pNew, 0, sizeof(*pNew));
+ pNew->zFilename = (char*)&pNew[1];
+ sqlite3_snprintf(nName+10, pNew->zFilename, "%s-index", zName);
/* Look to see if there is an existing unixShmFile that can be used.
** If no matching unixShmFile currently exists, create a new one.
*/
unixEnterMutex();
- rc = stat(zName, &sStat);
+ rc = stat(pNew->zFilename, &sStat);
if( rc==0 ){
memset(&fid, 0, sizeof(fid));
fid.dev = sStat.st_dev;
@@ -4958,16 +4979,11 @@ static int unixShmOpen(
if( memcmp(&pFile->fid, &fid, sizeof(fid))==0 ) break;
}
}
- if( pFile==0 ){
- int nName = strlen(zName);
- pFile = sqlite3_malloc( sizeof(*pFile) + nName + 1 );
- if( pFile==0 ){
- rc = SQLITE_NOMEM;
- goto shm_open_err;
- }
- memset(pFile, 0, sizeof(*pFile));
- pFile->zFilename = (char*)&pFile[1];
- memcpy(pFile->zFilename, zName, nName+1);
+ if( pFile ){
+ sqlite3_free(pNew);
+ }else{
+ pFile = pNew;
+ pNew = 0;
pFile->h = -1;
pFile->pNext = unixShmFileList;
unixShmFileList = pFile;
@@ -4983,7 +4999,7 @@ static int unixShmOpen(
goto shm_open_err;
}
- pFile->h = open(zName, O_RDWR|O_CREAT, 0664);
+ pFile->h = open(pFile->zFilename, O_RDWR|O_CREAT, 0664);
if( pFile->h<0 ){
rc = SQLITE_CANTOPEN_BKPT;
goto shm_open_err;
@@ -5033,15 +5049,17 @@ shm_open_err:
unixShmPurge();
sqlite3_free(p);
sqlite3_free(pFile);
+ sqlite3_free(pNew);
*pShm = 0;
unixLeaveMutex();
return rc;
}
/*
-** Close a connectioon to shared-memory.
+** Close a connection to shared-memory. Delete the underlying
+** storage if deleteFlag is true.
*/
-static int unixShmClose(sqlite3_shm *pSharedMem){
+static int unixShmClose(sqlite3_shm *pSharedMem, int deleteFlag){
unixShm *p; /* The connection to be closed */
unixShmFile *pFile; /* The underlying shared-memory file */
unixShm **pp; /* For looping over sibling connections */
@@ -5069,6 +5087,7 @@ static int unixShmClose(sqlite3_shm *pSharedMem){
assert( pFile->nRef>0 );
pFile->nRef--;
if( pFile->nRef==0 ){
+ if( deleteFlag ) unlink(pFile->zFilename);
unixShmPurge();
}
unixLeaveMutex();
@@ -5348,13 +5367,6 @@ static int unixShmLock(
return rc;
}
-/*
-** Delete a shared-memory segment from the system.
-*/
-static int unixShmDelete(sqlite3_vfs *pVfs, const char *zName){
- return pVfs->xDelete(pVfs, zName, 0);
-}
-
#else
# define unixShmOpen 0
# define unixShmSize 0
@@ -5362,7 +5374,6 @@ static int unixShmDelete(sqlite3_vfs *pVfs, const char *zName){
# define unixShmRelease 0
# define unixShmLock 0
# define unixShmClose 0
-# define unixShmDelete 0
#endif /* #ifndef SQLITE_OMIT_WAL */
/*
@@ -6589,7 +6600,6 @@ int sqlite3_os_init(void){
unixShmRelease, /* xShmRelease */ \
unixShmLock, /* xShmLock */ \
unixShmClose, /* xShmClose */ \
- unixShmDelete, /* xShmDelete */ \
0, /* xRename */ \
0, /* xCurrentTimeInt64 */ \
}
diff --git a/src/sqlite.h.in b/src/sqlite.h.in
index 175141227..60984d6e5 100644
--- a/src/sqlite.h.in
+++ b/src/sqlite.h.in
@@ -848,8 +848,7 @@ struct sqlite3_vfs {
int (*xShmGet)(sqlite3_shm*, int reqMapSize, int *pMapSize, void**);
int (*xShmRelease)(sqlite3_shm*);
int (*xShmLock)(sqlite3_shm*, int desiredLock, int *gotLock);
- int (*xShmClose)(sqlite3_shm*);
- int (*xShmDelete)(sqlite3_vfs*, const char *zName);
+ int (*xShmClose)(sqlite3_shm*, int deleteFlag);
int (*xRename)(sqlite3_vfs*, const char *zOld, const char *zNew, int dirSync);
int (*xCurrentTimeInt64)(sqlite3_vfs*, sqlite3_int64*);
/*
diff --git a/src/test_devsym.c b/src/test_devsym.c
index dff7b9880..642073107 100644
--- a/src/test_devsym.c
+++ b/src/test_devsym.c
@@ -73,8 +73,7 @@ static int devsymShmSize(sqlite3_shm *, int , int *);
static int devsymShmGet(sqlite3_shm *, int , int *, void **);
static int devsymShmRelease(sqlite3_shm *);
static int devsymShmLock(sqlite3_shm *, int , int *);
-static int devsymShmClose(sqlite3_shm *);
-static int devsymShmDelete(sqlite3_vfs *, const char *);
+static int devsymShmClose(sqlite3_shm *, int);
static sqlite3_vfs devsym_vfs = {
2, /* iVersion */
@@ -110,7 +109,6 @@ static sqlite3_vfs devsym_vfs = {
0,
devsymShmLock,
devsymShmClose,
- devsymShmDelete,
0,
0,
};
@@ -378,11 +376,8 @@ static int devsymShmRelease(sqlite3_shm *p){
static int devsymShmLock(sqlite3_shm *p, int desiredLock, int *gotLock){
return g.pVfs->xShmLock(p, desiredLock, gotLock);
}
-static int devsymShmClose(sqlite3_shm *p){
- return g.pVfs->xShmClose(p);
-}
-static int devsymShmDelete(sqlite3_vfs *pVfs, const char *zName){
- return g.pVfs->xShmDelete(g.pVfs, zName);
+static int devsymShmClose(sqlite3_shm *p, int deleteFlag){
+ return g.pVfs->xShmClose(p, deleteFlag);
}
/*
@@ -400,7 +395,6 @@ void devsym_register(int iDeviceChar, int iSectorSize){
devsym_vfs.xShmRelease = (g.pVfs->xShmRelease ? devsymShmRelease : 0);
devsym_vfs.xShmLock = (g.pVfs->xShmLock ? devsymShmLock : 0);
devsym_vfs.xShmClose = (g.pVfs->xShmClose ? devsymShmClose : 0);
- devsym_vfs.xShmDelete = (g.pVfs->xShmDelete ? devsymShmDelete : 0);
sqlite3_vfs_register(&devsym_vfs, 0);
}
if( iDeviceChar>=0 ){
diff --git a/src/wal.c b/src/wal.c
index 146675ad6..071521001 100644
--- a/src/wal.c
+++ b/src/wal.c
@@ -133,6 +133,7 @@ struct Wal {
u8 lockState; /* SQLITE_SHM_xxxx constant showing lock state */
u8 readerType; /* SQLITE_SHM_READ or SQLITE_SHM_READ_FULL */
WalIndexHdr hdr; /* Wal-index for current snapshot */
+ char *zName; /* Name of underlying storage */
};
@@ -590,24 +591,23 @@ int sqlite3WalOpen(
/* Allocate an instance of struct Wal to return. */
*ppWal = 0;
nWal = strlen(zDb);
- pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile + nWal+11);
+ pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile + nWal+5);
if( !pRet ) goto wal_open_out;
pRet->pVfs = pVfs;
pRet->pFd = (sqlite3_file *)&pRet[1];
- zWal = pVfs->szOsFile + (char*)pRet->pFd;
- sqlite3_snprintf(nWal+11, zWal, "%s-wal-index", zDb);
+ pRet->zName = zWal = pVfs->szOsFile + (char*)pRet->pFd;
+ sqlite3_snprintf(nWal+5, zWal, "%s-wal", zDb);
rc = pVfs->xShmOpen(pVfs, zWal, &pRet->pWIndex);
if( rc ) goto wal_open_out;
/* Open file handle on the write-ahead log file. */
- zWal[nWal+4] = 0;
flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_MAIN_JOURNAL);
rc = sqlite3OsOpen(pVfs, zWal, pRet->pFd, flags, &flags);
wal_open_out:
if( rc!=SQLITE_OK ){
if( pRet ){
- pVfs->xShmClose(pRet->pWIndex);
+ pVfs->xShmClose(pRet->pWIndex, 0);
sqlite3OsClose(pRet->pFd);
sqlite3_free(pRet);
}
@@ -805,15 +805,10 @@ int sqlite3WalClose(
walIndexUnmap(pWal);
}
- pWal->pVfs->xShmClose(pWal->pWIndex);
+ pWal->pVfs->xShmClose(pWal->pWIndex, isDelete);
sqlite3OsClose(pWal->pFd);
if( isDelete ){
- int nWal;
- char *zWal = &((char *)pWal->pFd)[pWal->pVfs->szOsFile];
- sqlite3OsDelete(pWal->pVfs, zWal, 0);
- nWal = sqlite3Strlen30(zWal);
- memcpy(&zWal[nWal], "-index", 7);
- pWal->pVfs->xShmDelete(pWal->pVfs, zWal);
+ sqlite3OsDelete(pWal->pVfs, pWal->zName, 0);
}
sqlite3_free(pWal);
}