1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
|
/******************************************************************************
* $Id: ogrsqlitevfs.cpp 24807 2012-08-19 20:14:37Z rouault $
*
* Project: OpenGIS Simple Features Reference Implementation
* Purpose: Implements SQLite VFS
* Author: Even Rouault, <even dot rouault at mines dash paris dot org>
******************************************************************************
* Copyright (c) 2011, Even Rouault, <even dot rouault at mines dash paris dot org>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#include "cpl_atomic_ops.h"
#include "ogr_sqlite.h"
CPL_CVSID("$Id: ogrsqlitevfs.cpp 24807 2012-08-19 20:14:37Z rouault $");
//#define DEBUG_IO 1
#ifdef HAVE_SQLITE_VFS
typedef struct
{
char szVFSName[64];
sqlite3_vfs *pDefaultVFS;
pfnNotifyFileOpenedType pfn;
void *pfnUserData;
int nCounter;
} OGRSQLiteVFSAppDataStruct;
#define GET_UNDERLYING_VFS(pVFS) ((OGRSQLiteVFSAppDataStruct* )pVFS->pAppData)->pDefaultVFS
typedef struct
{
const struct sqlite3_io_methods *pMethods;
VSILFILE *fp;
int bDeleteOnClose;
char *pszFilename;
} OGRSQLiteFileStruct;
static int OGRSQLiteIOClose(sqlite3_file* pFile)
{
OGRSQLiteFileStruct* pMyFile = (OGRSQLiteFileStruct*) pFile;
#ifdef DEBUG_IO
CPLDebug("SQLITE", "OGRSQLiteIOClose(%p (%s))", pMyFile->fp, pMyFile->pszFilename);
#endif
VSIFCloseL(pMyFile->fp);
if (pMyFile->bDeleteOnClose)
VSIUnlink(pMyFile->pszFilename);
CPLFree(pMyFile->pszFilename);
return SQLITE_OK;
}
static int OGRSQLiteIORead(sqlite3_file* pFile, void* pBuffer, int iAmt, sqlite3_int64 iOfst)
{
OGRSQLiteFileStruct* pMyFile = (OGRSQLiteFileStruct*) pFile;
VSIFSeekL(pMyFile->fp, (vsi_l_offset) iOfst, SEEK_SET);
int nRead = (int)VSIFReadL(pBuffer, 1, iAmt, pMyFile->fp);
#ifdef DEBUG_IO
CPLDebug("SQLITE", "OGRSQLiteIORead(%p, %d, %d) = %d", pMyFile->fp, iAmt, (int)iOfst, nRead);
#endif
if (nRead < iAmt)
{
memset(((char*)pBuffer) + nRead, 0, iAmt - nRead);
return SQLITE_IOERR_SHORT_READ;
}
return SQLITE_OK;
}
static int OGRSQLiteIOWrite(sqlite3_file* pFile, const void* pBuffer, int iAmt, sqlite3_int64 iOfst)
{
OGRSQLiteFileStruct* pMyFile = (OGRSQLiteFileStruct*) pFile;
VSIFSeekL(pMyFile->fp, (vsi_l_offset) iOfst, SEEK_SET);
int nWritten = (int)VSIFWriteL(pBuffer, 1, iAmt, pMyFile->fp);
#ifdef DEBUG_IO
CPLDebug("SQLITE", "OGRSQLiteIOWrite(%p, %d, %d) = %d", pMyFile->fp, iAmt, (int)iOfst, nWritten);
#endif
if (nWritten < iAmt)
{
return SQLITE_IOERR_WRITE;
}
return SQLITE_OK;
}
static int OGRSQLiteIOTruncate(sqlite3_file* pFile, sqlite3_int64 size)
{
OGRSQLiteFileStruct* pMyFile = (OGRSQLiteFileStruct*) pFile;
#ifdef DEBUG_IO
CPLDebug("SQLITE", "OGRSQLiteIOTruncate(%p, " CPL_FRMT_GIB ")", pMyFile->fp, size);
#endif
int nRet = VSIFTruncateL(pMyFile->fp, size);
return (nRet == 0) ? SQLITE_OK : SQLITE_IOERR_TRUNCATE;
}
static int OGRSQLiteIOSync(sqlite3_file* pFile, int flags)
{
#ifdef DEBUG_IO
OGRSQLiteFileStruct* pMyFile = (OGRSQLiteFileStruct*) pFile;
CPLDebug("SQLITE", "OGRSQLiteIOSync(%p, %d)", pMyFile->fp, flags);
#endif
return SQLITE_OK;
}
static int OGRSQLiteIOFileSize(sqlite3_file* pFile, sqlite3_int64 *pSize)
{
OGRSQLiteFileStruct* pMyFile = (OGRSQLiteFileStruct*) pFile;
vsi_l_offset nCurOffset = VSIFTellL(pMyFile->fp);
VSIFSeekL(pMyFile->fp, 0, SEEK_END);
*pSize = VSIFTellL(pMyFile->fp);
VSIFSeekL(pMyFile->fp, nCurOffset, SEEK_SET);
#ifdef DEBUG_IO
CPLDebug("SQLITE", "OGRSQLiteIOFileSize(%p) = " CPL_FRMT_GIB, pMyFile->fp, *pSize);
#endif
return SQLITE_OK;
}
static int OGRSQLiteIOLock(sqlite3_file* pFile, int flags)
{
#ifdef DEBUG_IO
OGRSQLiteFileStruct* pMyFile = (OGRSQLiteFileStruct*) pFile;
CPLDebug("SQLITE", "OGRSQLiteIOLock(%p)", pMyFile->fp);
#endif
return SQLITE_OK;
}
static int OGRSQLiteIOUnlock(sqlite3_file* pFile, int flags)
{
#ifdef DEBUG_IO
OGRSQLiteFileStruct* pMyFile = (OGRSQLiteFileStruct*) pFile;
CPLDebug("SQLITE", "OGRSQLiteIOUnlock(%p)", pMyFile->fp);
#endif
return SQLITE_OK;
}
static int OGRSQLiteIOCheckReservedLock(sqlite3_file* pFile, int *pResOut)
{
#ifdef DEBUG_IO
OGRSQLiteFileStruct* pMyFile = (OGRSQLiteFileStruct*) pFile;
CPLDebug("SQLITE", "OGRSQLiteIOCheckReservedLock(%p)", pMyFile->fp);
#endif
*pResOut = 0;
return SQLITE_OK;
}
static int OGRSQLiteIOFileControl(sqlite3_file* pFile, int op, void *pArg)
{
#ifdef DEBUG_IO
OGRSQLiteFileStruct* pMyFile = (OGRSQLiteFileStruct*) pFile;
CPLDebug("SQLITE", "OGRSQLiteIOFileControl(%p, %d)", pMyFile->fp, op);
#endif
return SQLITE_NOTFOUND;
}
static int OGRSQLiteIOSectorSize(sqlite3_file* pFile)
{
#ifdef DEBUG_IO
OGRSQLiteFileStruct* pMyFile = (OGRSQLiteFileStruct*) pFile;
CPLDebug("SQLITE", "OGRSQLiteIOSectorSize(%p)", pMyFile->fp);
#endif
return 0;
}
static int OGRSQLiteIODeviceCharacteristics(sqlite3_file* pFile)
{
#ifdef DEBUG_IO
OGRSQLiteFileStruct* pMyFile = (OGRSQLiteFileStruct*) pFile;
CPLDebug("SQLITE", "OGRSQLiteIODeviceCharacteristics(%p)", pMyFile->fp);
#endif
return 0;
}
static const sqlite3_io_methods OGRSQLiteIOMethods =
{
1,
OGRSQLiteIOClose,
OGRSQLiteIORead,
OGRSQLiteIOWrite,
OGRSQLiteIOTruncate,
OGRSQLiteIOSync,
OGRSQLiteIOFileSize,
OGRSQLiteIOLock,
OGRSQLiteIOUnlock,
OGRSQLiteIOCheckReservedLock,
OGRSQLiteIOFileControl,
OGRSQLiteIOSectorSize,
OGRSQLiteIODeviceCharacteristics
};
static int OGRSQLiteVFSOpen(sqlite3_vfs* pVFS,
const char *zName,
sqlite3_file* pFile,
int flags,
int *pOutFlags)
{
#ifdef DEBUG_IO
CPLDebug("SQLITE", "OGRSQLiteVFSOpen(%s, %d)", zName ? zName : "(null)", flags);
#endif
OGRSQLiteVFSAppDataStruct* pAppData = (OGRSQLiteVFSAppDataStruct* )pVFS->pAppData;
if (zName == NULL)
{
zName = CPLSPrintf("/vsimem/sqlite/%p_%d",
pVFS, CPLAtomicInc(&(pAppData->nCounter)));
}
OGRSQLiteFileStruct* pMyFile = (OGRSQLiteFileStruct*) pFile;
pMyFile->pMethods = NULL;
pMyFile->bDeleteOnClose = FALSE;
pMyFile->pszFilename = NULL;
if ( flags & SQLITE_OPEN_READONLY )
pMyFile->fp = VSIFOpenL(zName, "rb");
else if ( flags & SQLITE_OPEN_CREATE )
pMyFile->fp = VSIFOpenL(zName, "wb+");
else if ( flags & SQLITE_OPEN_READWRITE )
pMyFile->fp = VSIFOpenL(zName, "rb+");
else
pMyFile->fp = NULL;
if (pMyFile->fp == NULL)
return SQLITE_CANTOPEN;
#ifdef DEBUG_IO
CPLDebug("SQLITE", "OGRSQLiteVFSOpen() = %p", pMyFile->fp);
#endif
pfnNotifyFileOpenedType pfn = pAppData->pfn;
if (pfn)
{
pfn(pAppData->pfnUserData, zName, pMyFile->fp);
}
pMyFile->pMethods = &OGRSQLiteIOMethods;
pMyFile->bDeleteOnClose = ( flags & SQLITE_OPEN_DELETEONCLOSE );
pMyFile->pszFilename = CPLStrdup(zName);
if (pOutFlags != NULL)
*pOutFlags = flags;
return SQLITE_OK;
}
static int OGRSQLiteVFSDelete(sqlite3_vfs* pVFS, const char *zName, int syncDir)
{
#ifdef DEBUG_IO
CPLDebug("SQLITE", "OGRSQLiteVFSDelete(%s)", zName);
#endif
VSIUnlink(zName);
return SQLITE_OK;
}
static int OGRSQLiteVFSAccess (sqlite3_vfs* pVFS, const char *zName, int flags, int *pResOut)
{
#ifdef DEBUG_IO
CPLDebug("SQLITE", "OGRSQLiteVFSAccess(%s, %d)", zName, flags);
#endif
VSIStatBufL sStatBufL;
int nRet;
if (flags == SQLITE_ACCESS_EXISTS)
{
/* Do not try to check the presence of a journal on /vsicurl ! */
if ( strncmp(zName, "/vsicurl/", 9) == 0 &&
strlen(zName) > strlen("-journal") &&
strcmp(zName + strlen(zName) - strlen("-journal"), "-journal") == 0 )
nRet = -1;
else
nRet = VSIStatExL(zName, &sStatBufL, VSI_STAT_EXISTS_FLAG);
}
else if (flags == SQLITE_ACCESS_READ)
{
VSILFILE* fp = VSIFOpenL(zName, "rb");
nRet = fp ? 0 : -1;
if (fp)
VSIFCloseL(fp);
}
else if (flags == SQLITE_ACCESS_READWRITE)
{
VSILFILE* fp = VSIFOpenL(zName, "rb+");
nRet = fp ? 0 : -1;
if (fp)
VSIFCloseL(fp);
}
else
nRet = -1;
*pResOut = (nRet == 0);
return SQLITE_OK;
}
static int OGRSQLiteVFSFullPathname (sqlite3_vfs* pVFS, const char *zName, int nOut, char *zOut)
{
sqlite3_vfs* pUnderlyingVFS = GET_UNDERLYING_VFS(pVFS);
#ifdef DEBUG_IO
CPLDebug("SQLITE", "OGRSQLiteVFSFullPathname(%s)", zName);
#endif
if (zName[0] == '/')
{
strncpy(zOut, zName, nOut);
zOut[nOut-1] = '\0';
return SQLITE_OK;
}
return pUnderlyingVFS->xFullPathname(pUnderlyingVFS, zName, nOut, zOut);
}
static void* OGRSQLiteVFSDlOpen (sqlite3_vfs* pVFS, const char *zFilename)
{
sqlite3_vfs* pUnderlyingVFS = GET_UNDERLYING_VFS(pVFS);
//CPLDebug("SQLITE", "OGRSQLiteVFSDlOpen(%s)", zFilename);
return pUnderlyingVFS->xDlOpen(pUnderlyingVFS, zFilename);
}
static void OGRSQLiteVFSDlError (sqlite3_vfs* pVFS, int nByte, char *zErrMsg)
{
sqlite3_vfs* pUnderlyingVFS = GET_UNDERLYING_VFS(pVFS);
//CPLDebug("SQLITE", "OGRSQLiteVFSDlError()");
pUnderlyingVFS->xDlError(pUnderlyingVFS, nByte, zErrMsg);
}
/* xDlSym member signature changed in sqlite 3.6.7 (http://www.sqlite.org/changes.html) */
/* This was supposed to be done "in a way that is backwards compatible but which might cause compiler warnings" */
/* Perhaps in C, but definitely not in C++ ( #4515 ) */
#if SQLITE_VERSION_NUMBER >= 3006007
static void (*OGRSQLiteVFSDlSym (sqlite3_vfs* pVFS,void* pHandle, const char *zSymbol))(void)
#else
static void (*OGRSQLiteVFSDlSym (sqlite3_vfs* pVFS,void* pHandle, const char *zSymbol))
#endif
{
sqlite3_vfs* pUnderlyingVFS = GET_UNDERLYING_VFS(pVFS);
//CPLDebug("SQLITE", "OGRSQLiteVFSDlSym(%s)", zSymbol);
return pUnderlyingVFS->xDlSym(pUnderlyingVFS, pHandle, zSymbol);
}
static void OGRSQLiteVFSDlClose (sqlite3_vfs* pVFS, void* pHandle)
{
sqlite3_vfs* pUnderlyingVFS = GET_UNDERLYING_VFS(pVFS);
//CPLDebug("SQLITE", "OGRSQLiteVFSDlClose(%p)", pHandle);
pUnderlyingVFS->xDlClose(pUnderlyingVFS, pHandle);
}
static int OGRSQLiteVFSRandomness (sqlite3_vfs* pVFS, int nByte, char *zOut)
{
sqlite3_vfs* pUnderlyingVFS = GET_UNDERLYING_VFS(pVFS);
//CPLDebug("SQLITE", "OGRSQLiteVFSRandomness()");
return pUnderlyingVFS->xRandomness(pUnderlyingVFS, nByte, zOut);
}
static int OGRSQLiteVFSSleep (sqlite3_vfs* pVFS, int microseconds)
{
sqlite3_vfs* pUnderlyingVFS = GET_UNDERLYING_VFS(pVFS);
//CPLDebug("SQLITE", "OGRSQLiteVFSSleep()");
return pUnderlyingVFS->xSleep(pUnderlyingVFS, microseconds);
}
static int OGRSQLiteVFSCurrentTime (sqlite3_vfs* pVFS, double* p1)
{
sqlite3_vfs* pUnderlyingVFS = GET_UNDERLYING_VFS(pVFS);
//CPLDebug("SQLITE", "OGRSQLiteVFSCurrentTime()");
return pUnderlyingVFS->xCurrentTime(pUnderlyingVFS, p1);
}
static int OGRSQLiteVFSGetLastError (sqlite3_vfs* pVFS, int p1, char *p2)
{
sqlite3_vfs* pUnderlyingVFS = GET_UNDERLYING_VFS(pVFS);
//CPLDebug("SQLITE", "OGRSQLiteVFSGetLastError()");
return pUnderlyingVFS->xGetLastError(pUnderlyingVFS, p1, p2);
}
sqlite3_vfs* OGRSQLiteCreateVFS(pfnNotifyFileOpenedType pfn, void* pfnUserData)
{
sqlite3_vfs* pDefaultVFS = sqlite3_vfs_find(NULL);
sqlite3_vfs* pMyVFS = (sqlite3_vfs*) CPLCalloc(1, sizeof(sqlite3_vfs));
OGRSQLiteVFSAppDataStruct* pVFSAppData =
(OGRSQLiteVFSAppDataStruct*) CPLCalloc(1, sizeof(OGRSQLiteVFSAppDataStruct));
sprintf(pVFSAppData->szVFSName, "OGRSQLITEVFS_%p", pVFSAppData);
pVFSAppData->pDefaultVFS = pDefaultVFS;
pVFSAppData->pfn = pfn;
pVFSAppData->pfnUserData = pfnUserData;
pVFSAppData->nCounter = 0;
pMyVFS->iVersion = 1;
pMyVFS->szOsFile = sizeof(OGRSQLiteFileStruct);
pMyVFS->mxPathname = pDefaultVFS->mxPathname;
pMyVFS->zName = pVFSAppData->szVFSName;
pMyVFS->pAppData = pVFSAppData;
pMyVFS->xOpen = OGRSQLiteVFSOpen;
pMyVFS->xDelete = OGRSQLiteVFSDelete;
pMyVFS->xAccess = OGRSQLiteVFSAccess;
pMyVFS->xFullPathname = OGRSQLiteVFSFullPathname;
pMyVFS->xDlOpen = OGRSQLiteVFSDlOpen;
pMyVFS->xDlError = OGRSQLiteVFSDlError;
pMyVFS->xDlSym = OGRSQLiteVFSDlSym;
pMyVFS->xDlClose = OGRSQLiteVFSDlClose;
pMyVFS->xRandomness = OGRSQLiteVFSRandomness;
pMyVFS->xSleep = OGRSQLiteVFSSleep;
pMyVFS->xCurrentTime = OGRSQLiteVFSCurrentTime;
pMyVFS->xGetLastError = OGRSQLiteVFSGetLastError;
return pMyVFS;
}
#endif // HAVE_SQLITE_VFS
|