From 8cf35eb449ed0a250a798df7825195969337394d Mon Sep 17 00:00:00 2001 From: dan Date: Wed, 1 Sep 2010 11:40:05 +0000 Subject: [PATCH 1/7] Add file test_quota.c, demonstrating how file-system quotas may be implemented as a VFS wrapper. FossilOrigin-Name: 383eb87bbf560b20682dd5be0e18ddadf762f415 --- main.mk | 1 + manifest | 21 +- manifest.uuid | 2 +- src/tclsqlite.c | 2 + src/test_quota.c | 819 +++++++++++++++++++++++++++++++++++++++++++++++ test/quota.test | 150 +++++++++ 6 files changed, 986 insertions(+), 9 deletions(-) create mode 100644 src/test_quota.c create mode 100644 test/quota.test diff --git a/main.mk b/main.mk index 06a00a2d8a..b0186113a9 100644 --- a/main.mk +++ b/main.mk @@ -245,6 +245,7 @@ TESTSRC = \ $(TOP)/src/test_onefile.c \ $(TOP)/src/test_osinst.c \ $(TOP)/src/test_pcache.c \ + $(TOP)/src/test_quota.c \ $(TOP)/src/test_rtree.c \ $(TOP)/src/test_schema.c \ $(TOP)/src/test_server.c \ diff --git a/manifest b/manifest index 9d23d58aca..d621221d4d 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Fix\ssome\scompiler\swarnings\sunder\sMSVC. -D 2010-09-01T02:38:22 +C Add\sfile\stest_quota.c,\sdemonstrating\show\sfile-system\squotas\smay\sbe\simplemented\sas\sa\sVFS\swrapper. +D 2010-09-01T11:40:05 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f F Makefile.in c599a15d268b1db2aeadea19df2adc3bf2eb6bee F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23 @@ -96,7 +96,7 @@ F ext/rtree/tkt3363.test 142ab96eded44a3615ec79fba98c7bde7d0f96de F ext/rtree/viewrtree.tcl eea6224b3553599ae665b239bd827e182b466024 F install-sh 9d4de14ab9fb0facae2f48780b874848cbf2f895 x F ltmain.sh 3ff0879076df340d2e23ae905484d8c15d5fdea8 -F main.mk e5a8d2441a4949abfc73bb3fe752c65fb2f8a62c +F main.mk 1c6b2152dab78cc0c9f74958968c87fea232254c F mkdll.sh 7d09b23c05d56532e9d44a50868eb4b12ff4f74a F mkextu.sh 416f9b7089d80e5590a29692c9d9280a10dbad9f F mkextw.sh 4123480947681d9b434a5e7b1ee08135abe409ac @@ -179,7 +179,7 @@ F src/sqliteInt.h 4e31d3b58720d6251e08ba394641b9a63725eb8a F src/sqliteLimit.h a17dcd3fb775d63b64a43a55c54cb282f9726f44 F src/status.c 496913d4e8441195f6f2a75b1c95993a45b9b30b F src/table.c 2cd62736f845d82200acfa1287e33feb3c15d62e -F src/tclsqlite.c 3a6687806193a2610eb7c4c2d282493ff5c8d96c +F src/tclsqlite.c e91019fb6787166abca23a81b16c07fecc2ed751 F src/test1.c c1b74b15b55051e7004ede9233e4b2b323e8ba65 F src/test2.c 80d323d11e909cf0eb1b6fbb4ac22276483bcf31 F src/test3.c 056093cfef69ff4227a6bdb9108564dc7f45e4bc @@ -208,6 +208,7 @@ F src/test_mutex.c ce06b59aca168cd8c520b77159a24352a7469bd3 F src/test_onefile.c 40cf9e212a377a6511469384a64b01e6e34b2eec F src/test_osinst.c f408c6a181f2fb04c56273afd5c3e1e82f60392c F src/test_pcache.c 7bf828972ac0d2403f5cfa4cd14da41f8ebe73d8 +F src/test_quota.c 43b7e1b2bec8fa259bc3717f67b9f5c8026724ed F src/test_rtree.c e957a603a98871dcf005c1e96ae791cfe74eb7f6 F src/test_schema.c 8c06ef9ddb240c7a0fcd31bc221a6a2aade58bf0 F src/test_server.c bbba05c144b5fc4b52ff650a4328027b3fa5fcc6 @@ -561,6 +562,7 @@ F test/printf.test 05970cde31b1a9f54bd75af60597be75a5c54fea F test/progress.test 5b075c3c790c7b2a61419bc199db87aaf48b8301 F test/ptrchng.test ef1aa72d6cf35a2bbd0869a649b744e9d84977fc F test/quick.test 1681febc928d686362d50057c642f77a02c62e57 +F test/quota.test 0026c53f3ad78094f478c5e7048fb63602d59ee7 F test/quote.test 215897dbe8de1a6f701265836d6601cc6ed103e6 F test/randexpr1.tcl 40dec52119ed3a2b8b2a773bce24b63a3a746459 F test/randexpr1.test 1084050991e9ba22c1c10edd8d84673b501cc25a @@ -851,7 +853,10 @@ F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f -P 631423677bd7043e99987edc633005443125181c -R 05e5b03d9444a84913fc1df09eed54da -U shaneh -Z d536f60f29570cdd4e12c9463729e891 +P afdc82a99eba260aed8ae2cca4bcec629f384098 +R f576cc3079f8c23a4490d6ed337ce008 +T *branch * experimental +T *sym-experimental * +T -sym-trunk * +U dan +Z da8e3747f889e65babc56beff4831221 diff --git a/manifest.uuid b/manifest.uuid index 7b6323c10a..b7e5f5ac07 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -afdc82a99eba260aed8ae2cca4bcec629f384098 \ No newline at end of file +383eb87bbf560b20682dd5be0e18ddadf762f415 \ No newline at end of file diff --git a/src/tclsqlite.c b/src/tclsqlite.c index 164bc03b1f..3f1cb11d72 100644 --- a/src/tclsqlite.c +++ b/src/tclsqlite.c @@ -3577,6 +3577,7 @@ static void init_all(Tcl_Interp *interp){ extern int Sqlitetestvfs_Init(Tcl_Interp *); extern int SqlitetestStat_Init(Tcl_Interp*); extern int Sqlitetestrtree_Init(Tcl_Interp*); + extern int Sqlitequota_Init(Tcl_Interp*); Sqliteconfig_Init(interp); Sqlitetest1_Init(interp); @@ -3606,6 +3607,7 @@ static void init_all(Tcl_Interp *interp){ Sqlitetestvfs_Init(interp); SqlitetestStat_Init(interp); Sqlitetestrtree_Init(interp); + Sqlitequota_Init(interp); Tcl_CreateObjCommand(interp,"load_testfixture_extensions",init_all_cmd,0,0); diff --git a/src/test_quota.c b/src/test_quota.c new file mode 100644 index 0000000000..5b39743181 --- /dev/null +++ b/src/test_quota.c @@ -0,0 +1,819 @@ +/* +** 2010 September 31 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This file contains a VFS "shim" - a layer that sits in between the +** pager and the real VFS. +** +** This particular shim enforces a quota system on files. One or more +** database files are in a "quota group" that is defined by a GLOB +** pattern. A quota is set for the combined size of all files in the +** the group. A quota of zero means "no limit". If the total size +** of all files in the quota group is met or exceeded, then new +** write requests that attempt to enlarge a file fail with SQLITE_FULL. +** +** However, before returning SQLITE_FULL, the write requests invoke +** a callback function that is configurable for each quota group. +** This callback has the opportunity to enlarge the quota. If the +** callback does enlarge the quota such that the total size of all +** files within the group is less than the new quota, then the write +** continues as if nothing had happened. +*/ +#include "sqlite3.h" +#include +#include + +/************************ Object Definitions ******************************/ + +/* +** This module contains a table of filename patterns that have size +** quotas. The quota applies to the sum of the sizes of all open +** database files whose names match the GLOB pattern. +** +** Each quota is an instance of the following object. Quotas must +** be established (using sqlite3_quota_set()) prior to opening any +** of the database connections that access files governed by the +** quota. +** +** Each entry in the quota table is an instance of the following object. +*/ +typedef struct quotaGroup quotaGroup; +struct quotaGroup { + const char *zPattern; /* Filename pattern to be quotaed */ + sqlite3_int64 iLimit; /* Upper bound on total file size */ + sqlite3_int64 iSize; /* Current size of all files */ + void (*xCallback)( /* Callback invoked when going over quota */ + const char *zFilename, /* Name of file whose size increases */ + sqlite3_int64 *piLimit, /* IN/OUT: The current limit */ + sqlite3_int64 iSize, /* Total size of all files in the group */ + void *pArg /* Client data */ + ); + void *pArg; /* Third argument to the xCallback() */ + int nRef; /* Number of files in the group references. */ + quotaGroup *pNext, **ppPrev; /* Doubly linked list of all quota objects */ +}; + +/* +** An instance of the following object represents each file that +** participates in quota tracking. The sqlite3_file object for the +** underlying VFS is appended to this structure. +*/ +typedef struct quotaFile quotaFile; +struct quotaFile { + sqlite3_file base; /* Base class - must be first */ + const char *zFilename; /* Name of this file */ + quotaGroup *pGroup; /* Upper bound on file size */ + sqlite3_int64 iSize; /* Current size of this file */ + /* The underlying VFS sqlite3_file is appended to this object */ +}; + +/************************* Global Variables **********************************/ +/* +** All global variables used by this file are containing within the following +** gQuota structure. +*/ +static struct { + /* The pOrigVfs is a pointer to the real underlying VFS implementation. + ** Most operations pass-through to the real VFS. This value is read-only + ** during operation. It is only modified at start-time and thus does not + ** require a mutex. + */ + sqlite3_vfs *pOrigVfs; + + /* The sThisVfs is the VFS structure used by this shim. It is initialized + ** at start-time and thus does not require a mutex + */ + sqlite3_vfs sThisVfs; + + /* The sIoMethods defines the methods used by sqlite3_file objects + ** associated with this shim. It is initialized at start-time and does + ** not require a mutex. + ** + ** When the underlying VFS is called to open a file, it might return + ** either a version 1 or a version 2 sqlite3_file object. This shim + ** has to create a wrapper sqlite3_file of the same version. Hence + ** there are two I/O method structures, one for version 1 and the other + ** for version 2. + */ + sqlite3_io_methods sIoMethodsV1; + sqlite3_io_methods sIoMethodsV2; + + /* True when this shim as been initialized. + */ + int isInitialized; + + /* For run-time access any of the other global data structures in this + ** shim, the following mutex must be held. + */ + sqlite3_mutex *pMutex; + + /* List of quotaGroup objects. + */ + quotaGroup *pGroup; + +} gQuota; + +/************************* Utility Routines *********************************/ +/* +** Acquire and release the mutex used to serialize access to the +** list of quotaGroups. +*/ +static void quotaEnter(void){ sqlite3_mutex_enter(gQuota.pMutex); } +static void quotaLeave(void){ sqlite3_mutex_leave(gQuota.pMutex); } + + +/* If the reference count and threshold for a quotaGroup are both +** zero, then destroy the quotaGroup. +*/ +static void quotaGroupDeref(quotaGroup *p){ + if( p->nRef==0 && p->iLimit==0 ){ + if( p->pNext ) p->pNext->ppPrev = p->ppPrev; + if( p->ppPrev ) *p->ppPrev = p->pNext; + sqlite3_free(p); + } +} + +/* +** Return TRUE if string z matches glob pattern zGlob. +** +** Globbing rules: +** +** '*' Matches any sequence of zero or more characters. +** +** '?' Matches exactly one character. +** +** [...] Matches one character from the enclosed list of +** characters. +** +** [^...] Matches one character not in the enclosed list. +** +*/ +static int strglob(const char *zGlob, const char *z){ + int c, c2; + int invert; + int seen; + + while( (c = (*(zGlob++)))!=0 ){ + if( c=='*' ){ + while( (c=(*(zGlob++))) == '*' || c=='?' ){ + if( c=='?' && (*(z++))==0 ) return 0; + } + if( c==0 ){ + return 1; + }else if( c=='[' ){ + while( *z && strglob(zGlob-1,z)==0 ){ + z++; + } + return (*z)!=0; + } + while( (c2 = (*(z++)))!=0 ){ + while( c2!=c ){ + c2 = *(z++); + if( c2==0 ) return 0; + } + if( strglob(zGlob,z) ) return 1; + } + return 0; + }else if( c=='?' ){ + if( (*(z++))==0 ) return 0; + }else if( c=='[' ){ + int prior_c = 0; + seen = 0; + invert = 0; + c = *(z++); + if( c==0 ) return 0; + c2 = *(zGlob++); + if( c2=='^' ){ + invert = 1; + c2 = *(zGlob++); + } + if( c2==']' ){ + if( c==']' ) seen = 1; + c2 = *(zGlob++); + } + while( c2 && c2!=']' ){ + if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){ + c2 = *(zGlob++); + if( c>=prior_c && c<=c2 ) seen = 1; + prior_c = 0; + }else{ + if( c==c2 ){ + seen = 1; + } + prior_c = c2; + } + c2 = *(zGlob++); + } + if( c2==0 || (seen ^ invert)==0 ) return 0; + }else{ + if( c!=(*(z++)) ) return 0; + } + } + return *z==0; +} + + +/* Find a quotaGroup given the filename. +** +** Return a pointer to the quotaFile object. Return NULL if not found. +*/ +static quotaGroup *quotaGroupFind(const char *zFilename){ + quotaGroup *p; + for(p=gQuota.pGroup; p && strglob(p->zPattern, zFilename)==0; p=p->pNext){} + return p; +} + +/* Translate an sqlite3_file* that is really a quotaFile* into +** an sqlite3_file* for the underlying original VFS. +*/ +static sqlite3_file *quotaSubFile(sqlite3_file *pFile){ + quotaFile *p = (quotaFile*)pFile; + return (sqlite3_file*)&p[1]; +} + +/************************* VFS Method Wrappers *****************************/ +/* +** This is the xOpen method used for the "quota" VFS. +** +** Most of the work is done by the underlying original VFS. This method +** simply links the new file into the quota group if it is a file that +** needs to be tracked. +*/ +static int quotaOpen( + sqlite3_vfs *pVfs, + const char *zName, + sqlite3_file *pFile, + int flags, + int *pOutFlags +){ + int rc; + quotaFile *pQuotaFile; + quotaGroup *pGroup; + sqlite3_file *pSubFile; + sqlite3_vfs *pOrigVfs = gQuota.pOrigVfs; + + /* If the file is not a main database file or a WAL, then use the + ** normal xOpen method. + */ + if( (flags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_WAL))==0 ){ + return pOrigVfs->xOpen(pOrigVfs, zName, pFile, flags, pOutFlags); + } + + /* If the name of the file does not match any quota group, then + ** use the normal xOpen method. + */ + quotaEnter(); + pGroup = quotaGroupFind(zName); + if( pGroup==0 ){ + rc = pOrigVfs->xOpen(pOrigVfs, zName, pFile, flags, pOutFlags); + }else{ + /* If we get to this point, it means the file needs to be quota tracked. + */ + pQuotaFile = (quotaFile*)pFile; + pSubFile = quotaSubFile(pFile); + rc = pOrigVfs->xOpen(pOrigVfs, zName, pSubFile, flags, pOutFlags); + if( rc==SQLITE_OK ){ + pQuotaFile->iSize = 0; + pQuotaFile->pGroup = pGroup; + pGroup->nRef++; + pQuotaFile->zFilename = zName; + if( pSubFile->pMethods->iVersion==1 ){ + pQuotaFile->base.pMethods = &gQuota.sIoMethodsV1; + }else{ + pQuotaFile->base.pMethods = &gQuota.sIoMethodsV2; + } + } + } + quotaLeave(); + return rc; +} + +/************************ I/O Method Wrappers *******************************/ + +/* xClose requests get passed through to the original VFS. But we +** also have to unlink the quotaFile from the quotaGroup. +*/ +static int quotaClose(sqlite3_file *pFile){ + quotaFile *p = (quotaFile*)pFile; + quotaGroup *pGroup = p->pGroup; + sqlite3_file *pSubFile = quotaSubFile(pFile); + int rc; + rc = pSubFile->pMethods->xClose(pSubFile); + quotaEnter(); + pGroup->nRef--; + pGroup->iSize -= p->iSize; + quotaGroupDeref(pGroup); + quotaLeave(); + return rc; +} + +/* Pass xRead requests directory thru to the original VFS without +** further processing. +*/ +static int quotaRead( + sqlite3_file *pFile, + void *pBuf, + int iAmt, + sqlite3_int64 iOfst +){ + sqlite3_file *pSubFile = quotaSubFile(pFile); + return pSubFile->pMethods->xRead(pSubFile, pBuf, iAmt, iOfst); +} + +/* Check xWrite requests to see if they expand the file. If they do, +** the perform a quota check before passing them through to the +** original VFS. +*/ +static int quotaWrite( + sqlite3_file *pFile, + const void *pBuf, + int iAmt, + sqlite3_int64 iOfst +){ + quotaFile *p = (quotaFile*)pFile; + sqlite3_file *pSubFile = quotaSubFile(pFile); + sqlite3_int64 iEnd = iOfst+iAmt; + quotaGroup *pGroup; + sqlite3_int64 szNew; + + if( p->iSizepGroup; + quotaEnter(); + szNew = pGroup->iSize - p->iSize + iEnd; + if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){ + if( pGroup->xCallback ){ + pGroup->xCallback(p->zFilename, &pGroup->iLimit, szNew, + pGroup->pArg); + } + if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){ + quotaLeave(); + return SQLITE_FULL; + } + } + pGroup->iSize = szNew; + p->iSize = iEnd; + quotaLeave(); + } + return pSubFile->pMethods->xWrite(pSubFile, pBuf, iAmt, iOfst); +} + +/* Pass xTruncate requests thru to the original VFS. If the +** success, update the file size. +*/ +static int quotaTruncate(sqlite3_file *pFile, sqlite3_int64 size){ + quotaFile *p = (quotaFile*)pFile; + sqlite3_file *pSubFile = quotaSubFile(pFile); + int rc = pSubFile->pMethods->xTruncate(pSubFile, size); + quotaGroup *pGroup = p->pGroup; + if( rc==SQLITE_OK ){ + quotaEnter(); + pGroup->iSize -= p->iSize; + p->iSize = size; + pGroup->iSize += size; + quotaLeave(); + } + return rc; +} + +/* Pass xSync requests through to the original VFS without change +*/ +static int quotaSync(sqlite3_file *pFile, int flags){ + sqlite3_file *pSubFile = quotaSubFile(pFile); + return pSubFile->pMethods->xSync(pSubFile, flags); +} + +/* Pass xFileSize requests through to the original VFS but then +** update the quotaGroup with the new size before returning. +*/ +static int quotaFileSize(sqlite3_file *pFile, sqlite3_int64 *pSize){ + quotaFile *p = (quotaFile*)pFile; + sqlite3_file *pSubFile = quotaSubFile(pFile); + quotaGroup *pGroup; + sqlite3_int64 sz; + int rc; + + rc = pSubFile->pMethods->xFileSize(pSubFile, &sz); + if( rc==SQLITE_OK ){ + pGroup = p->pGroup; + quotaEnter(); + pGroup->iSize -= p->iSize; + p->iSize = sz; + pGroup->iSize += sz; + quotaLeave(); + *pSize = sz; + } + return rc; +} + +/* Pass xLock requests through to the original VFS unchanged. +*/ +static int quotaLock(sqlite3_file *pFile, int lock){ + sqlite3_file *pSubFile = quotaSubFile(pFile); + return pSubFile->pMethods->xLock(pSubFile, lock); +} + +/* Pass xUnlock requests through to the original VFS unchanged. +*/ +static int quotaUnlock(sqlite3_file *pFile, int lock){ + sqlite3_file *pSubFile = quotaSubFile(pFile); + return pSubFile->pMethods->xUnlock(pSubFile, lock); +} + +/* Pass xCheckReservedLock requests through to the original VFS unchanged. +*/ +static int quotaCheckReservedLock(sqlite3_file *pFile, int *pResOut){ + sqlite3_file *pSubFile = quotaSubFile(pFile); + return pSubFile->pMethods->xCheckReservedLock(pSubFile, pResOut); +} + +/* Pass xFileControl requests through to the original VFS unchanged. +*/ +static int quotaFileControl(sqlite3_file *pFile, int op, void *pArg){ + sqlite3_file *pSubFile = quotaSubFile(pFile); + return pSubFile->pMethods->xFileControl(pSubFile, op, pArg); +} + +/* Pass xSectorSize requests through to the original VFS unchanged. +*/ +static int quotaSectorSize(sqlite3_file *pFile){ + sqlite3_file *pSubFile = quotaSubFile(pFile); + return pSubFile->pMethods->xSectorSize(pSubFile); +} + +/* Pass xDeviceCharacteristics requests through to the original VFS unchanged. +*/ +static int quotaDeviceCharacteristics(sqlite3_file *pFile){ + sqlite3_file *pSubFile = quotaSubFile(pFile); + return pSubFile->pMethods->xDeviceCharacteristics(pSubFile); +} + +/* Pass xShmMap requests through to the original VFS unchanged. +*/ +static int quotaShmMap( + sqlite3_file *pFile, /* Handle open on database file */ + int iRegion, /* Region to retrieve */ + int szRegion, /* Size of regions */ + int bExtend, /* True to extend file if necessary */ + void volatile **pp /* OUT: Mapped memory */ +){ + sqlite3_file *pSubFile = quotaSubFile(pFile); + return pSubFile->pMethods->xShmMap(pSubFile, iRegion, szRegion, bExtend, pp); +} + +/* Pass xShmLock requests through to the original VFS unchanged. +*/ +static int quotaShmLock( + sqlite3_file *pFile, /* Database file holding the shared memory */ + int ofst, /* First lock to acquire or release */ + int n, /* Number of locks to acquire or release */ + int flags /* What to do with the lock */ +){ + sqlite3_file *pSubFile = quotaSubFile(pFile); + return pSubFile->pMethods->xShmLock(pSubFile, ofst, n, flags); +} + +/* Pass xShmBarrier requests through to the original VFS unchanged. +*/ +static void quotaShmBarrier(sqlite3_file *pFile){ + sqlite3_file *pSubFile = quotaSubFile(pFile); + pSubFile->pMethods->xShmBarrier(pSubFile); +} + +/* Pass xShmUnmap requests through to the original VFS unchanged. +*/ +static int quotaShmUnmap(sqlite3_file *pFile, int deleteFlag){ + sqlite3_file *pSubFile = quotaSubFile(pFile); + return pSubFile->pMethods->xShmUnmap(pSubFile, deleteFlag); +} + +/************************** Public Interfaces *****************************/ +/* +** Initialize the quota VFS shim. Use the VFS named zOrigVfsName +** as the VFS that does the actual work. Use the default if +** zOrigVfsName==NULL. +** +** The quota VFS shim is named "quota". It will become the default +** VFS if makeDefault is non-zero. +** +** THIS ROUTINE IS NOT THREADSAFE. Call this routine exactly once +** during start-up. +*/ +int sqlite3_quota_initialize(const char *zOrigVfsName, int makeDefault){ + sqlite3_vfs *pOrigVfs; + if( gQuota.isInitialized ) return SQLITE_MISUSE; + pOrigVfs = sqlite3_vfs_find(zOrigVfsName); + if( pOrigVfs==0 ) return SQLITE_ERROR; + assert( pOrigVfs!=&gQuota.sThisVfs ); + gQuota.pMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); + if( !gQuota.pMutex ){ + return SQLITE_NOMEM; + } + gQuota.isInitialized = 1; + gQuota.pOrigVfs = pOrigVfs; + gQuota.sThisVfs = *pOrigVfs; + gQuota.sThisVfs.xOpen = quotaOpen; + gQuota.sThisVfs.szOsFile += sizeof(quotaFile); + gQuota.sThisVfs.zName = "quota"; + gQuota.sIoMethodsV1.iVersion = 1; + gQuota.sIoMethodsV1.xClose = quotaClose; + gQuota.sIoMethodsV1.xRead = quotaRead; + gQuota.sIoMethodsV1.xWrite = quotaWrite; + gQuota.sIoMethodsV1.xTruncate = quotaTruncate; + gQuota.sIoMethodsV1.xSync = quotaSync; + gQuota.sIoMethodsV1.xFileSize = quotaFileSize; + gQuota.sIoMethodsV1.xLock = quotaLock; + gQuota.sIoMethodsV1.xUnlock = quotaUnlock; + gQuota.sIoMethodsV1.xCheckReservedLock = quotaCheckReservedLock; + gQuota.sIoMethodsV1.xFileControl = quotaFileControl; + gQuota.sIoMethodsV1.xSectorSize = quotaSectorSize; + gQuota.sIoMethodsV1.xDeviceCharacteristics = quotaDeviceCharacteristics; + gQuota.sIoMethodsV2 = gQuota.sIoMethodsV1; + gQuota.sIoMethodsV2.iVersion = 2; + gQuota.sIoMethodsV2.xShmMap = quotaShmMap; + gQuota.sIoMethodsV2.xShmLock = quotaShmLock; + gQuota.sIoMethodsV2.xShmBarrier = quotaShmBarrier; + gQuota.sIoMethodsV2.xShmUnmap = quotaShmUnmap; + sqlite3_vfs_register(&gQuota.sThisVfs, makeDefault); + return SQLITE_OK; +} + +/* +** Shutdown the quota system. +** +** All SQLite database connections must be closed before calling this +** routine. +** +** THIS ROUTINE IS NOT THREADSAFE. Call this routine exactly one while +** shutting down in order to free all remaining quota groups. +*/ +int sqlite3_quota_shutdown(void){ + quotaGroup *p; + if( gQuota.isInitialized==0 ) return SQLITE_MISUSE; + for(p=gQuota.pGroup; p; p=p->pNext){ + if( p->nRef ) return SQLITE_MISUSE; + } + while( gQuota.pGroup ){ + quotaGroup *p = gQuota.pGroup; + gQuota.pGroup = p->pNext; + sqlite3_free(p); + } + gQuota.isInitialized = 0; + sqlite3_mutex_free(gQuota.pMutex); + sqlite3_vfs_unregister(&gQuota.sThisVfs); + memset(&gQuota, 0, sizeof(gQuota)); + return SQLITE_OK; +} + +/* +** Create or destroy a quota group. +** +** The quota group is defined by the zPattern. When calling this routine +** with a zPattern for a quota group that already exists, this routine +** merely updates the iLimit, xCallback, and pArg values for that quota +** group. If zPattern is new, then a new quota group is created. +** +** If the iLimit for a quota group is set to zero, then the quota group +** is disabled and will be deleted when the last database connection using +** the quota group is closed. +** +** Calling this routine on a zPattern that does not exist and with a +** zero iLimit is a no-op. +** +** A quota group must exist with a non-zero iLimit prior to opening +** database connections if those connections are to participate in the +** quota group. Creating a quota group does not affect database connections +** that are already open. +*/ +int sqlite3_quota_set( + const char *zPattern, /* The filename pattern */ + sqlite3_int64 iLimit, /* New quota to set for this quota group */ + void (*xCallback)( /* Callback invoked when going over quota */ + const char *zFilename, /* Name of file whose size increases */ + sqlite3_int64 *piLimit, /* IN/OUT: The current limit */ + sqlite3_int64 iSize, /* Total size of all files in the group */ + void *pArg /* Client data */ + ), + void *pArg /* client data passed thru to callback */ +){ + quotaGroup *pGroup; + quotaEnter(); + pGroup = gQuota.pGroup; + while( pGroup && strcmp(pGroup->zPattern, zPattern)!=0 ){ + pGroup = pGroup->pNext; + } + if( pGroup==0 && iLimit>0 ){ + int nPattern = strlen(zPattern); + pGroup = sqlite3_malloc( sizeof(*pGroup) + nPattern + 1 ); + if( pGroup==0 ){ + quotaLeave(); + return SQLITE_NOMEM; + } + memset(pGroup, 0, sizeof(*pGroup)); + pGroup->zPattern = (char*)&pGroup[1]; + memcpy((char *)pGroup->zPattern, zPattern, nPattern+1); + if( gQuota.pGroup ) gQuota.pGroup->ppPrev = &pGroup->pNext; + pGroup->pNext = gQuota.pGroup; + pGroup->ppPrev = &gQuota.pGroup; + gQuota.pGroup = pGroup; + } + pGroup->iLimit = iLimit; + pGroup->xCallback = xCallback; + pGroup->pArg = pArg; + quotaGroupDeref(pGroup); + quotaLeave(); + return SQLITE_OK; +} + + +/***************************** Test Code ***********************************/ +#ifdef SQLITE_TEST +#include + +typedef struct TclQuotaCallback TclQuotaCallback; +struct TclQuotaCallback { + Tcl_Interp *interp; + Tcl_Obj *pScript; + TclQuotaCallback *pNext; +}; +static TclQuotaCallback *pQuotaCallbackList = 0; + +extern const char *sqlite3TestErrorName(int); + +static void tclQuotaCallback( + const char *zFilename, /* Name of file whose size increases */ + sqlite3_int64 *piLimit, /* IN/OUT: The current limit */ + sqlite3_int64 iSize, /* Total size of all files in the group */ + void *pArg /* Client data */ +){ + TclQuotaCallback *p; /* Callback script object */ + Tcl_Obj *pEval; /* Script to evaluate */ + Tcl_Obj *pVarname; /* Name of variable to pass as 2nd arg */ + unsigned int rnd; /* Random part of pVarname */ + int rc; /* Tcl error code */ + + p = (TclQuotaCallback *)pArg; + + pVarname = Tcl_NewStringObj("::piLimit_", -1); + Tcl_IncrRefCount(pVarname); + sqlite3_randomness(sizeof(rnd), (void *)&rnd); + Tcl_AppendObjToObj(pVarname, Tcl_NewIntObj((int)(rnd&0x7FFFFFFF))); + Tcl_ObjSetVar2(p->interp, pVarname, 0, Tcl_NewWideIntObj(*piLimit), 0); + + pEval = Tcl_DuplicateObj(p->pScript); + Tcl_IncrRefCount(pEval); + Tcl_ListObjAppendElement(0, pEval, Tcl_NewStringObj(zFilename, -1)); + Tcl_ListObjAppendElement(0, pEval, pVarname); + Tcl_ListObjAppendElement(0, pEval, Tcl_NewWideIntObj(iSize)); + rc = Tcl_EvalObjEx(p->interp, pEval, TCL_EVAL_GLOBAL); + + if( rc==TCL_OK ){ + Tcl_Obj *pLimit = Tcl_ObjGetVar2(p->interp, pVarname, 0, 0); + rc = Tcl_GetWideIntFromObj(p->interp, pLimit, piLimit); + Tcl_UnsetVar(p->interp, Tcl_GetString(pVarname), 0); + } + + Tcl_DecrRefCount(pEval); + Tcl_DecrRefCount(pVarname); + if( rc!=TCL_OK ) Tcl_BackgroundError(p->interp); +} + +/* +** tclcmd: sqlite3_quota_initialize NAME MAKEDEFAULT +*/ +static int test_quota_initialize( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + const char *zName; /* Name of new quota VFS */ + int makeDefault; /* True to make the new VFS the default */ + int rc; /* Value returned by quota_initialize() */ + + /* Process arguments */ + if( objc!=3 ){ + Tcl_WrongNumArgs(interp, 1, objv, "NAME MAKEDEFAULT"); + return TCL_ERROR; + } + zName = Tcl_GetString(objv[1]); + if( Tcl_GetBooleanFromObj(interp, objv[2], &makeDefault) ) return TCL_ERROR; + if( zName[0]=='\0' ) zName = 0; + + /* Call sqlite3_quota_initialize() */ + rc = sqlite3_quota_initialize(zName, makeDefault); + Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC); + + return TCL_OK; +} + +/* +** tclcmd: sqlite3_quota_shutdown +*/ +static int test_quota_shutdown( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + int rc; /* Value returned by quota_shutdown() */ + + if( objc!=1 ){ + Tcl_WrongNumArgs(interp, 1, objv, ""); + return TCL_ERROR; + } + + /* Call sqlite3_quota_shutdown() */ + rc = sqlite3_quota_shutdown(); + Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC); + + /* If the quota system was successfully shut down, delete all the quota + ** callback script objects in the global linked list. + */ + if( rc==SQLITE_OK ){ + TclQuotaCallback *p, *pNext; + for(p=pQuotaCallbackList; p; p=pNext){ + pNext = p->pNext; + Tcl_DecrRefCount(p->pScript); + ckfree((char *)p); + } + pQuotaCallbackList = 0; + } + + return TCL_OK; +} + +/* +** tclcmd: sqlite3_quota_set PATTERN LIMIT SCRIPT +*/ +static int test_quota_set( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + const char *zPattern; /* File pattern to configure */ + sqlite3_int64 iLimit; /* Initial quota in bytes */ + Tcl_Obj *pScript; /* Tcl script to invoke to increase quota */ + int rc; /* Value returned by quota_set() */ + TclQuotaCallback *p; /* Callback object */ + + /* Process arguments */ + if( objc!=4 ){ + Tcl_WrongNumArgs(interp, 1, objv, "PATTERN LIMIT SCRIPT"); + return TCL_ERROR; + } + zPattern = Tcl_GetString(objv[1]); + if( Tcl_GetWideIntFromObj(interp, objv[2], &iLimit) ) return TCL_ERROR; + pScript = objv[3]; + + /* Allocate a TclQuotaCallback object */ + p = (TclQuotaCallback *)ckalloc(sizeof(TclQuotaCallback)); + memset(p, 0, sizeof(TclQuotaCallback)); + + /* Invoke sqlite3_quota_set() */ + rc = sqlite3_quota_set(zPattern, iLimit, tclQuotaCallback, (void *)p); + if( rc!=SQLITE_OK ){ + ckfree((char *)p); + }else{ + Tcl_IncrRefCount(pScript); + p->interp = interp; + p->pScript = pScript; + p->pNext = pQuotaCallbackList; + pQuotaCallbackList = p; + } + + Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC); + return TCL_OK; +} + +/* +** This routine registers the custom TCL commands defined in this +** module. This should be the only procedure visible from outside +** of this module. +*/ +int Sqlitequota_Init(Tcl_Interp *interp){ + static struct { + char *zName; + Tcl_ObjCmdProc *xProc; + } aCmd[] = { + { "sqlite3_quota_initialize", test_quota_initialize }, + { "sqlite3_quota_shutdown", test_quota_shutdown }, + { "sqlite3_quota_set", test_quota_set }, + }; + int i; + + for(i=0; i Date: Wed, 1 Sep 2010 12:50:54 +0000 Subject: [PATCH 2/7] Update the quota shim so that when the same file is opened multiple times, its size only counts against the quota once. FossilOrigin-Name: f5d263803084107389c5541face8d536b62fffe3 --- manifest | 27 ++++-- manifest.uuid | 2 +- src/test_quota.c | 247 ++++++++++++++++++++++++++++------------------- 3 files changed, 164 insertions(+), 112 deletions(-) diff --git a/manifest b/manifest index d621221d4d..d628d785ce 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,8 @@ -C Add\sfile\stest_quota.c,\sdemonstrating\show\sfile-system\squotas\smay\sbe\simplemented\sas\sa\sVFS\swrapper. -D 2010-09-01T11:40:05 +-----BEGIN PGP SIGNED MESSAGE----- +Hash: SHA1 + +C Update\sthe\squota\sshim\sso\sthat\swhen\sthe\ssame\sfile\sis\sopened\smultiple\stimes,\nits\ssize\sonly\scounts\sagainst\sthe\squota\sonce. +D 2010-09-01T12:50:54 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f F Makefile.in c599a15d268b1db2aeadea19df2adc3bf2eb6bee F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23 @@ -208,7 +211,7 @@ F src/test_mutex.c ce06b59aca168cd8c520b77159a24352a7469bd3 F src/test_onefile.c 40cf9e212a377a6511469384a64b01e6e34b2eec F src/test_osinst.c f408c6a181f2fb04c56273afd5c3e1e82f60392c F src/test_pcache.c 7bf828972ac0d2403f5cfa4cd14da41f8ebe73d8 -F src/test_quota.c 43b7e1b2bec8fa259bc3717f67b9f5c8026724ed +F src/test_quota.c 3bd9629e4b88d1aa7086a987a7eaee35d8683280 F src/test_rtree.c e957a603a98871dcf005c1e96ae791cfe74eb7f6 F src/test_schema.c 8c06ef9ddb240c7a0fcd31bc221a6a2aade58bf0 F src/test_server.c bbba05c144b5fc4b52ff650a4328027b3fa5fcc6 @@ -853,10 +856,14 @@ F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f -P afdc82a99eba260aed8ae2cca4bcec629f384098 -R f576cc3079f8c23a4490d6ed337ce008 -T *branch * experimental -T *sym-experimental * -T -sym-trunk * -U dan -Z da8e3747f889e65babc56beff4831221 +P 383eb87bbf560b20682dd5be0e18ddadf762f415 +R b5a4d36b9d6562d3c3442739bfabc1f0 +U drh +Z 71992168532d47edde2bb9ee2fe75389 +-----BEGIN PGP SIGNATURE----- +Version: GnuPG v1.4.6 (GNU/Linux) + +iD8DBQFMfkwxoxKgR168RlERAgANAJ9B4KtMV72Ji0JYDm0FJNxZrpK+kwCfT8lT +0hQEz4MNPkLhC93D3DNoV70= +=rL9o +-----END PGP SIGNATURE----- diff --git a/manifest.uuid b/manifest.uuid index b7e5f5ac07..37c93b22bf 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -383eb87bbf560b20682dd5be0e18ddadf762f415 \ No newline at end of file +f5d263803084107389c5541face8d536b62fffe3 \ No newline at end of file diff --git a/src/test_quota.c b/src/test_quota.c index 5b39743181..941a637632 100644 --- a/src/test_quota.c +++ b/src/test_quota.c @@ -33,6 +33,11 @@ /************************ Object Definitions ******************************/ +/* Forward declaration of all object types */ +typedef struct quotaGroup quotaGroup; +typedef struct quotaOpen quotaOpen; +typedef struct quotaFile quotaFile; + /* ** This module contains a table of filename patterns that have size ** quotas. The quota applies to the sum of the sizes of all open @@ -45,7 +50,6 @@ ** ** Each entry in the quota table is an instance of the following object. */ -typedef struct quotaGroup quotaGroup; struct quotaGroup { const char *zPattern; /* Filename pattern to be quotaed */ sqlite3_int64 iLimit; /* Upper bound on total file size */ @@ -57,21 +61,34 @@ struct quotaGroup { void *pArg /* Client data */ ); void *pArg; /* Third argument to the xCallback() */ - int nRef; /* Number of files in the group references. */ quotaGroup *pNext, **ppPrev; /* Doubly linked list of all quota objects */ + quotaFile *pFile; /* Files within this group */ }; /* -** An instance of the following object represents each file that -** participates in quota tracking. The sqlite3_file object for the -** underlying VFS is appended to this structure. +** An instance of this structure represents a single file that is part +** of a quota group. A single file can be opened multiple times. In +** order keep multiple openings of the same file from causing the size +** of the file counting against the quota multiple times, each file +** has a unique instance of this object and multiple open connections +** to the same file each point to a single instance of this object. */ -typedef struct quotaFile quotaFile; struct quotaFile { - sqlite3_file base; /* Base class - must be first */ - const char *zFilename; /* Name of this file */ + char *zFilename; /* Name of this file */ quotaGroup *pGroup; /* Upper bound on file size */ sqlite3_int64 iSize; /* Current size of this file */ + int nRef; /* Number of times this file is open */ + quotaFile *pNext, **ppPrev; /* Linked list of files in the same group */ +}; + +/* +** An instance of the following object represents each open connection +** to a file that participates in quota tracking. The sqlite3_file object +** for the underlying VFS is appended to this structure. +*/ +struct quotaOpen { + sqlite3_file base; /* Base class - must be first */ + quotaFile *pFile; /* The underlying file */ /* The underlying VFS sqlite3_file is appended to this object */ }; @@ -134,7 +151,7 @@ static void quotaLeave(void){ sqlite3_mutex_leave(gQuota.pMutex); } ** zero, then destroy the quotaGroup. */ static void quotaGroupDeref(quotaGroup *p){ - if( p->nRef==0 && p->iLimit==0 ){ + if( p->pFile==0 && p->iLimit==0 ){ if( p->pNext ) p->pNext->ppPrev = p->ppPrev; if( p->ppPrev ) *p->ppPrev = p->pNext; sqlite3_free(p); @@ -223,7 +240,7 @@ static int strglob(const char *zGlob, const char *z){ /* Find a quotaGroup given the filename. ** -** Return a pointer to the quotaFile object. Return NULL if not found. +** Return a pointer to the quotaOpen object. Return NULL if not found. */ static quotaGroup *quotaGroupFind(const char *zFilename){ quotaGroup *p; @@ -231,11 +248,11 @@ static quotaGroup *quotaGroupFind(const char *zFilename){ return p; } -/* Translate an sqlite3_file* that is really a quotaFile* into +/* Translate an sqlite3_file* that is really a quotaOpen* into ** an sqlite3_file* for the underlying original VFS. */ -static sqlite3_file *quotaSubFile(sqlite3_file *pFile){ - quotaFile *p = (quotaFile*)pFile; +static sqlite3_file *quotaSubOpen(sqlite3_file *pOpen){ + quotaOpen *p = (quotaOpen*)pOpen; return (sqlite3_file*)&p[1]; } @@ -247,24 +264,25 @@ static sqlite3_file *quotaSubFile(sqlite3_file *pFile){ ** simply links the new file into the quota group if it is a file that ** needs to be tracked. */ -static int quotaOpen( +static int quotaxOpen( sqlite3_vfs *pVfs, const char *zName, - sqlite3_file *pFile, + sqlite3_file *pOpen, int flags, int *pOutFlags ){ int rc; - quotaFile *pQuotaFile; + quotaOpen *pQuotaOpen; + quotaFile *pFile; quotaGroup *pGroup; - sqlite3_file *pSubFile; + sqlite3_file *pSubOpen; sqlite3_vfs *pOrigVfs = gQuota.pOrigVfs; /* If the file is not a main database file or a WAL, then use the ** normal xOpen method. */ if( (flags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_WAL))==0 ){ - return pOrigVfs->xOpen(pOrigVfs, zName, pFile, flags, pOutFlags); + return pOrigVfs->xOpen(pOrigVfs, zName, pOpen, flags, pOutFlags); } /* If the name of the file does not match any quota group, then @@ -273,22 +291,39 @@ static int quotaOpen( quotaEnter(); pGroup = quotaGroupFind(zName); if( pGroup==0 ){ - rc = pOrigVfs->xOpen(pOrigVfs, zName, pFile, flags, pOutFlags); + rc = pOrigVfs->xOpen(pOrigVfs, zName, pOpen, flags, pOutFlags); }else{ /* If we get to this point, it means the file needs to be quota tracked. */ - pQuotaFile = (quotaFile*)pFile; - pSubFile = quotaSubFile(pFile); - rc = pOrigVfs->xOpen(pOrigVfs, zName, pSubFile, flags, pOutFlags); + pQuotaOpen = (quotaOpen*)pOpen; + pSubOpen = quotaSubOpen(pOpen); + rc = pOrigVfs->xOpen(pOrigVfs, zName, pSubOpen, flags, pOutFlags); if( rc==SQLITE_OK ){ - pQuotaFile->iSize = 0; - pQuotaFile->pGroup = pGroup; - pGroup->nRef++; - pQuotaFile->zFilename = zName; - if( pSubFile->pMethods->iVersion==1 ){ - pQuotaFile->base.pMethods = &gQuota.sIoMethodsV1; + for(pFile=pGroup->pFile; pFile && strcmp(pFile->zFilename, zName); + pFile=pFile->pNext){} + if( pFile==0 ){ + int nName = strlen(zName); + pFile = sqlite3_malloc( sizeof(*pFile) + nName + 1 ); + if( pFile==0 ){ + quotaLeave(); + pSubOpen->pMethods->xClose(pSubOpen); + return SQLITE_NOMEM; + } + memset(pFile, 0, sizeof(*pFile)); + pFile->zFilename = (char*)&pFile[1]; + memcpy(pFile->zFilename, zName, nName+1); + pFile->pNext = pGroup->pFile; + if( pGroup->pFile ) pGroup->pFile->ppPrev = &pFile->pNext; + pFile->ppPrev = &pGroup->pFile; + pGroup->pFile = pFile; + pFile->pGroup = pGroup; + } + pFile->nRef++; + pQuotaOpen->pFile = pFile; + if( pSubOpen->pMethods->iVersion==1 ){ + pQuotaOpen->base.pMethods = &gQuota.sIoMethodsV1; }else{ - pQuotaFile->base.pMethods = &gQuota.sIoMethodsV2; + pQuotaOpen->base.pMethods = &gQuota.sIoMethodsV2; } } } @@ -299,18 +334,24 @@ static int quotaOpen( /************************ I/O Method Wrappers *******************************/ /* xClose requests get passed through to the original VFS. But we -** also have to unlink the quotaFile from the quotaGroup. +** also have to unlink the quotaOpen from the quotaGroup. */ -static int quotaClose(sqlite3_file *pFile){ - quotaFile *p = (quotaFile*)pFile; - quotaGroup *pGroup = p->pGroup; - sqlite3_file *pSubFile = quotaSubFile(pFile); +static int quotaClose(sqlite3_file *pOpen){ + quotaOpen *p = (quotaOpen*)pOpen; + quotaFile *pFile = p->pFile; + sqlite3_file *pSubOpen = quotaSubOpen(pOpen); int rc; - rc = pSubFile->pMethods->xClose(pSubFile); + rc = pSubOpen->pMethods->xClose(pSubOpen); quotaEnter(); - pGroup->nRef--; - pGroup->iSize -= p->iSize; - quotaGroupDeref(pGroup); + pFile->nRef--; + if( pFile->nRef==0 ){ + quotaGroup *pGroup = pFile->pGroup; + pGroup->iSize -= pFile->iSize; + if( pFile->pNext ) pFile->pNext->ppPrev = pFile->ppPrev; + *pFile->ppPrev = pFile->pNext; + quotaGroupDeref(pGroup); + sqlite3_free(pFile); + } quotaLeave(); return rc; } @@ -319,13 +360,13 @@ static int quotaClose(sqlite3_file *pFile){ ** further processing. */ static int quotaRead( - sqlite3_file *pFile, + sqlite3_file *pOpen, void *pBuf, int iAmt, sqlite3_int64 iOfst ){ - sqlite3_file *pSubFile = quotaSubFile(pFile); - return pSubFile->pMethods->xRead(pSubFile, pBuf, iAmt, iOfst); + sqlite3_file *pSubOpen = quotaSubOpen(pOpen); + return pSubOpen->pMethods->xRead(pSubOpen, pBuf, iAmt, iOfst); } /* Check xWrite requests to see if they expand the file. If they do, @@ -333,24 +374,25 @@ static int quotaRead( ** original VFS. */ static int quotaWrite( - sqlite3_file *pFile, + sqlite3_file *pOpen, const void *pBuf, int iAmt, sqlite3_int64 iOfst ){ - quotaFile *p = (quotaFile*)pFile; - sqlite3_file *pSubFile = quotaSubFile(pFile); + quotaOpen *p = (quotaOpen*)pOpen; + sqlite3_file *pSubOpen = quotaSubOpen(pOpen); sqlite3_int64 iEnd = iOfst+iAmt; quotaGroup *pGroup; + quotaFile *pFile = p->pFile; sqlite3_int64 szNew; - if( p->iSizepGroup; + if( pFile->iSizepGroup; quotaEnter(); - szNew = pGroup->iSize - p->iSize + iEnd; + szNew = pGroup->iSize - pFile->iSize + iEnd; if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){ if( pGroup->xCallback ){ - pGroup->xCallback(p->zFilename, &pGroup->iLimit, szNew, + pGroup->xCallback(pFile->zFilename, &pGroup->iLimit, szNew, pGroup->pArg); } if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){ @@ -359,24 +401,26 @@ static int quotaWrite( } } pGroup->iSize = szNew; - p->iSize = iEnd; + pFile->iSize = iEnd; quotaLeave(); } - return pSubFile->pMethods->xWrite(pSubFile, pBuf, iAmt, iOfst); + return pSubOpen->pMethods->xWrite(pSubOpen, pBuf, iAmt, iOfst); } /* Pass xTruncate requests thru to the original VFS. If the ** success, update the file size. */ -static int quotaTruncate(sqlite3_file *pFile, sqlite3_int64 size){ - quotaFile *p = (quotaFile*)pFile; - sqlite3_file *pSubFile = quotaSubFile(pFile); - int rc = pSubFile->pMethods->xTruncate(pSubFile, size); - quotaGroup *pGroup = p->pGroup; +static int quotaTruncate(sqlite3_file *pOpen, sqlite3_int64 size){ + quotaOpen *p = (quotaOpen*)pOpen; + sqlite3_file *pSubOpen = quotaSubOpen(pOpen); + int rc = pSubOpen->pMethods->xTruncate(pSubOpen, size); + quotaFile *pFile = p->pFile; + quotaGroup *pGroup; if( rc==SQLITE_OK ){ quotaEnter(); - pGroup->iSize -= p->iSize; - p->iSize = size; + pGroup = pFile->pGroup; + pGroup->iSize -= pFile->iSize; + pFile->iSize = size; pGroup->iSize += size; quotaLeave(); } @@ -385,27 +429,28 @@ static int quotaTruncate(sqlite3_file *pFile, sqlite3_int64 size){ /* Pass xSync requests through to the original VFS without change */ -static int quotaSync(sqlite3_file *pFile, int flags){ - sqlite3_file *pSubFile = quotaSubFile(pFile); - return pSubFile->pMethods->xSync(pSubFile, flags); +static int quotaSync(sqlite3_file *pOpen, int flags){ + sqlite3_file *pSubOpen = quotaSubOpen(pOpen); + return pSubOpen->pMethods->xSync(pSubOpen, flags); } /* Pass xFileSize requests through to the original VFS but then ** update the quotaGroup with the new size before returning. */ -static int quotaFileSize(sqlite3_file *pFile, sqlite3_int64 *pSize){ - quotaFile *p = (quotaFile*)pFile; - sqlite3_file *pSubFile = quotaSubFile(pFile); +static int quotaFileSize(sqlite3_file *pOpen, sqlite3_int64 *pSize){ + quotaOpen *p = (quotaOpen*)pOpen; + sqlite3_file *pSubOpen = quotaSubOpen(pOpen); + quotaFile *pFile = p->pFile; quotaGroup *pGroup; sqlite3_int64 sz; int rc; - rc = pSubFile->pMethods->xFileSize(pSubFile, &sz); + rc = pSubOpen->pMethods->xFileSize(pSubOpen, &sz); if( rc==SQLITE_OK ){ - pGroup = p->pGroup; quotaEnter(); - pGroup->iSize -= p->iSize; - p->iSize = sz; + pGroup = pFile->pGroup; + pGroup->iSize -= pFile->iSize; + pFile->iSize = sz; pGroup->iSize += sz; quotaLeave(); *pSize = sz; @@ -415,83 +460,83 @@ static int quotaFileSize(sqlite3_file *pFile, sqlite3_int64 *pSize){ /* Pass xLock requests through to the original VFS unchanged. */ -static int quotaLock(sqlite3_file *pFile, int lock){ - sqlite3_file *pSubFile = quotaSubFile(pFile); - return pSubFile->pMethods->xLock(pSubFile, lock); +static int quotaLock(sqlite3_file *pOpen, int lock){ + sqlite3_file *pSubOpen = quotaSubOpen(pOpen); + return pSubOpen->pMethods->xLock(pSubOpen, lock); } /* Pass xUnlock requests through to the original VFS unchanged. */ -static int quotaUnlock(sqlite3_file *pFile, int lock){ - sqlite3_file *pSubFile = quotaSubFile(pFile); - return pSubFile->pMethods->xUnlock(pSubFile, lock); +static int quotaUnlock(sqlite3_file *pOpen, int lock){ + sqlite3_file *pSubOpen = quotaSubOpen(pOpen); + return pSubOpen->pMethods->xUnlock(pSubOpen, lock); } /* Pass xCheckReservedLock requests through to the original VFS unchanged. */ -static int quotaCheckReservedLock(sqlite3_file *pFile, int *pResOut){ - sqlite3_file *pSubFile = quotaSubFile(pFile); - return pSubFile->pMethods->xCheckReservedLock(pSubFile, pResOut); +static int quotaCheckReservedLock(sqlite3_file *pOpen, int *pResOut){ + sqlite3_file *pSubOpen = quotaSubOpen(pOpen); + return pSubOpen->pMethods->xCheckReservedLock(pSubOpen, pResOut); } /* Pass xFileControl requests through to the original VFS unchanged. */ -static int quotaFileControl(sqlite3_file *pFile, int op, void *pArg){ - sqlite3_file *pSubFile = quotaSubFile(pFile); - return pSubFile->pMethods->xFileControl(pSubFile, op, pArg); +static int quotaOpenControl(sqlite3_file *pOpen, int op, void *pArg){ + sqlite3_file *pSubOpen = quotaSubOpen(pOpen); + return pSubOpen->pMethods->xFileControl(pSubOpen, op, pArg); } /* Pass xSectorSize requests through to the original VFS unchanged. */ -static int quotaSectorSize(sqlite3_file *pFile){ - sqlite3_file *pSubFile = quotaSubFile(pFile); - return pSubFile->pMethods->xSectorSize(pSubFile); +static int quotaSectorSize(sqlite3_file *pOpen){ + sqlite3_file *pSubOpen = quotaSubOpen(pOpen); + return pSubOpen->pMethods->xSectorSize(pSubOpen); } /* Pass xDeviceCharacteristics requests through to the original VFS unchanged. */ -static int quotaDeviceCharacteristics(sqlite3_file *pFile){ - sqlite3_file *pSubFile = quotaSubFile(pFile); - return pSubFile->pMethods->xDeviceCharacteristics(pSubFile); +static int quotaDeviceCharacteristics(sqlite3_file *pOpen){ + sqlite3_file *pSubOpen = quotaSubOpen(pOpen); + return pSubOpen->pMethods->xDeviceCharacteristics(pSubOpen); } /* Pass xShmMap requests through to the original VFS unchanged. */ static int quotaShmMap( - sqlite3_file *pFile, /* Handle open on database file */ + sqlite3_file *pOpen, /* Handle open on database file */ int iRegion, /* Region to retrieve */ int szRegion, /* Size of regions */ int bExtend, /* True to extend file if necessary */ void volatile **pp /* OUT: Mapped memory */ ){ - sqlite3_file *pSubFile = quotaSubFile(pFile); - return pSubFile->pMethods->xShmMap(pSubFile, iRegion, szRegion, bExtend, pp); + sqlite3_file *pSubOpen = quotaSubOpen(pOpen); + return pSubOpen->pMethods->xShmMap(pSubOpen, iRegion, szRegion, bExtend, pp); } /* Pass xShmLock requests through to the original VFS unchanged. */ static int quotaShmLock( - sqlite3_file *pFile, /* Database file holding the shared memory */ + sqlite3_file *pOpen, /* Database file holding the shared memory */ int ofst, /* First lock to acquire or release */ int n, /* Number of locks to acquire or release */ int flags /* What to do with the lock */ ){ - sqlite3_file *pSubFile = quotaSubFile(pFile); - return pSubFile->pMethods->xShmLock(pSubFile, ofst, n, flags); + sqlite3_file *pSubOpen = quotaSubOpen(pOpen); + return pSubOpen->pMethods->xShmLock(pSubOpen, ofst, n, flags); } /* Pass xShmBarrier requests through to the original VFS unchanged. */ -static void quotaShmBarrier(sqlite3_file *pFile){ - sqlite3_file *pSubFile = quotaSubFile(pFile); - pSubFile->pMethods->xShmBarrier(pSubFile); +static void quotaShmBarrier(sqlite3_file *pOpen){ + sqlite3_file *pSubOpen = quotaSubOpen(pOpen); + pSubOpen->pMethods->xShmBarrier(pSubOpen); } /* Pass xShmUnmap requests through to the original VFS unchanged. */ -static int quotaShmUnmap(sqlite3_file *pFile, int deleteFlag){ - sqlite3_file *pSubFile = quotaSubFile(pFile); - return pSubFile->pMethods->xShmUnmap(pSubFile, deleteFlag); +static int quotaShmUnmap(sqlite3_file *pOpen, int deleteFlag){ + sqlite3_file *pSubOpen = quotaSubOpen(pOpen); + return pSubOpen->pMethods->xShmUnmap(pSubOpen, deleteFlag); } /************************** Public Interfaces *****************************/ @@ -519,8 +564,8 @@ int sqlite3_quota_initialize(const char *zOrigVfsName, int makeDefault){ gQuota.isInitialized = 1; gQuota.pOrigVfs = pOrigVfs; gQuota.sThisVfs = *pOrigVfs; - gQuota.sThisVfs.xOpen = quotaOpen; - gQuota.sThisVfs.szOsFile += sizeof(quotaFile); + gQuota.sThisVfs.xOpen = quotaxOpen; + gQuota.sThisVfs.szOsFile += sizeof(quotaOpen); gQuota.sThisVfs.zName = "quota"; gQuota.sIoMethodsV1.iVersion = 1; gQuota.sIoMethodsV1.xClose = quotaClose; @@ -532,7 +577,7 @@ int sqlite3_quota_initialize(const char *zOrigVfsName, int makeDefault){ gQuota.sIoMethodsV1.xLock = quotaLock; gQuota.sIoMethodsV1.xUnlock = quotaUnlock; gQuota.sIoMethodsV1.xCheckReservedLock = quotaCheckReservedLock; - gQuota.sIoMethodsV1.xFileControl = quotaFileControl; + gQuota.sIoMethodsV1.xFileControl = quotaOpenControl; gQuota.sIoMethodsV1.xSectorSize = quotaSectorSize; gQuota.sIoMethodsV1.xDeviceCharacteristics = quotaDeviceCharacteristics; gQuota.sIoMethodsV2 = gQuota.sIoMethodsV1; @@ -558,7 +603,7 @@ int sqlite3_quota_shutdown(void){ quotaGroup *p; if( gQuota.isInitialized==0 ) return SQLITE_MISUSE; for(p=gQuota.pGroup; p; p=p->pNext){ - if( p->nRef ) return SQLITE_MISUSE; + if( p->pFile ) return SQLITE_MISUSE; } while( gQuota.pGroup ){ quotaGroup *p = gQuota.pGroup; From 549dc1ba61ddd527b0c8294e07d3c3c5aaa2a491 Mon Sep 17 00:00:00 2001 From: drh Date: Wed, 1 Sep 2010 13:09:57 +0000 Subject: [PATCH 3/7] Clean up comments in the test_quota.c source file. FossilOrigin-Name: c1eec7dba698e5bad0870cb80079203f0fb89514 --- manifest | 18 ++++++++-------- manifest.uuid | 2 +- src/test_quota.c | 55 +++++++++++++++++++++++++----------------------- 3 files changed, 39 insertions(+), 36 deletions(-) diff --git a/manifest b/manifest index d628d785ce..f7d1163f36 100644 --- a/manifest +++ b/manifest @@ -1,8 +1,8 @@ -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 -C Update\sthe\squota\sshim\sso\sthat\swhen\sthe\ssame\sfile\sis\sopened\smultiple\stimes,\nits\ssize\sonly\scounts\sagainst\sthe\squota\sonce. -D 2010-09-01T12:50:54 +C Clean\sup\scomments\sin\sthe\stest_quota.c\ssource\sfile. +D 2010-09-01T13:09:57 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f F Makefile.in c599a15d268b1db2aeadea19df2adc3bf2eb6bee F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23 @@ -211,7 +211,7 @@ F src/test_mutex.c ce06b59aca168cd8c520b77159a24352a7469bd3 F src/test_onefile.c 40cf9e212a377a6511469384a64b01e6e34b2eec F src/test_osinst.c f408c6a181f2fb04c56273afd5c3e1e82f60392c F src/test_pcache.c 7bf828972ac0d2403f5cfa4cd14da41f8ebe73d8 -F src/test_quota.c 3bd9629e4b88d1aa7086a987a7eaee35d8683280 +F src/test_quota.c e542d96d5e0ad7bbc064ab9912da3d8ed65cbe28 F src/test_rtree.c e957a603a98871dcf005c1e96ae791cfe74eb7f6 F src/test_schema.c 8c06ef9ddb240c7a0fcd31bc221a6a2aade58bf0 F src/test_server.c bbba05c144b5fc4b52ff650a4328027b3fa5fcc6 @@ -856,14 +856,14 @@ F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f -P 383eb87bbf560b20682dd5be0e18ddadf762f415 -R b5a4d36b9d6562d3c3442739bfabc1f0 +P f5d263803084107389c5541face8d536b62fffe3 +R 35c143fcc0c1d935b095e49fe0b10b28 U drh -Z 71992168532d47edde2bb9ee2fe75389 +Z 22044fa4707460c35e1dd2a52d813a99 -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (GNU/Linux) -iD8DBQFMfkwxoxKgR168RlERAgANAJ9B4KtMV72Ji0JYDm0FJNxZrpK+kwCfT8lT -0hQEz4MNPkLhC93D3DNoV70= -=rL9o +iD8DBQFMflCroxKgR168RlERAupSAJ41VgmVbutErFMPzEt158JfEp4N/gCeLiry +ne2YPJhXeQQSv7tL27HzCOk= +=nNgv -----END PGP SIGNATURE----- diff --git a/manifest.uuid b/manifest.uuid index 37c93b22bf..63e92c447c 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -f5d263803084107389c5541face8d536b62fffe3 \ No newline at end of file +c1eec7dba698e5bad0870cb80079203f0fb89514 \ No newline at end of file diff --git a/src/test_quota.c b/src/test_quota.c index 941a637632..61a8f6e8be 100644 --- a/src/test_quota.c +++ b/src/test_quota.c @@ -39,16 +39,18 @@ typedef struct quotaOpen quotaOpen; typedef struct quotaFile quotaFile; /* -** This module contains a table of filename patterns that have size -** quotas. The quota applies to the sum of the sizes of all open -** database files whose names match the GLOB pattern. +** A "quota group" is a collection of files whose collective size we want +** to limit. Each quota group is defined by a GLOB pattern. ** -** Each quota is an instance of the following object. Quotas must -** be established (using sqlite3_quota_set()) prior to opening any -** of the database connections that access files governed by the -** quota. +** There is an instance of the following object for each defined quota +** group. This object records the GLOB pattern that defines which files +** belong to the quota group. The object also remembers the size limit +** for the group (the quota) and the callback to be invoked when the +** sum of the sizes of the files within the group goes over the limit. ** -** Each entry in the quota table is an instance of the following object. +** A quota group must be established (using sqlite3_quota_set(...)) +** prior to opening any of the database connections that access files +** within the quota group. */ struct quotaGroup { const char *zPattern; /* Filename pattern to be quotaed */ @@ -69,7 +71,7 @@ struct quotaGroup { ** An instance of this structure represents a single file that is part ** of a quota group. A single file can be opened multiple times. In ** order keep multiple openings of the same file from causing the size -** of the file counting against the quota multiple times, each file +** of the file to count against the quota multiple times, each file ** has a unique instance of this object and multiple open connections ** to the same file each point to a single instance of this object. */ @@ -83,8 +85,9 @@ struct quotaFile { /* ** An instance of the following object represents each open connection -** to a file that participates in quota tracking. The sqlite3_file object -** for the underlying VFS is appended to this structure. +** to a file that participates in quota tracking. This object is a +** subclass of sqlite3_file. The sqlite3_file object for the underlying +** VFS is appended to this structure. */ struct quotaOpen { sqlite3_file base; /* Base class - must be first */ @@ -98,7 +101,7 @@ struct quotaOpen { ** gQuota structure. */ static struct { - /* The pOrigVfs is a pointer to the real underlying VFS implementation. + /* The pOrigVfs is the real, original underlying VFS implementation. ** Most operations pass-through to the real VFS. This value is read-only ** during operation. It is only modified at start-time and thus does not ** require a mutex. @@ -249,7 +252,7 @@ static quotaGroup *quotaGroupFind(const char *zFilename){ } /* Translate an sqlite3_file* that is really a quotaOpen* into -** an sqlite3_file* for the underlying original VFS. +** the sqlite3_file* for the underlying original VFS. */ static sqlite3_file *quotaSubOpen(sqlite3_file *pOpen){ quotaOpen *p = (quotaOpen*)pOpen; @@ -261,22 +264,22 @@ static sqlite3_file *quotaSubOpen(sqlite3_file *pOpen){ ** This is the xOpen method used for the "quota" VFS. ** ** Most of the work is done by the underlying original VFS. This method -** simply links the new file into the quota group if it is a file that -** needs to be tracked. +** simply links the new file into the appropriate quota group if it is a +** file that needs to be tracked. */ static int quotaxOpen( - sqlite3_vfs *pVfs, - const char *zName, - sqlite3_file *pOpen, - int flags, - int *pOutFlags + sqlite3_vfs *pVfs, /* The quota VFS */ + const char *zName, /* Name of file to be opened */ + sqlite3_file *pOpen, /* Fill in this file descriptor */ + int flags, /* Flags to control the opening */ + int *pOutFlags /* Flags showing results of opening */ ){ - int rc; - quotaOpen *pQuotaOpen; - quotaFile *pFile; - quotaGroup *pGroup; - sqlite3_file *pSubOpen; - sqlite3_vfs *pOrigVfs = gQuota.pOrigVfs; + int rc; /* Result code */ + quotaOpen *pQuotaOpen; /* The new quota file descriptor */ + quotaFile *pFile; /* Corresponding quotaFile obj */ + quotaGroup *pGroup; /* The group file belongs to */ + sqlite3_file *pSubOpen; /* Real file descriptor */ + sqlite3_vfs *pOrigVfs = gQuota.pOrigVfs; /* Real VFS */ /* If the file is not a main database file or a WAL, then use the ** normal xOpen method. From ef9659babd225394119c8c890940f3974bd071b4 Mon Sep 17 00:00:00 2001 From: drh Date: Wed, 1 Sep 2010 14:35:48 +0000 Subject: [PATCH 4/7] Add the sqlite3_quota_dump test command. Add a destructor argument on the sqlite3_quota_set() interface. FossilOrigin-Name: 7a624b5ae2abff21bcbbb34af88d1c7656f3b729 --- manifest | 20 ++--- manifest.uuid | 2 +- src/test_quota.c | 220 +++++++++++++++++++++++++++++------------------ test/quota.test | 33 +++++-- 4 files changed, 176 insertions(+), 99 deletions(-) diff --git a/manifest b/manifest index f7d1163f36..ebafdadb9d 100644 --- a/manifest +++ b/manifest @@ -1,8 +1,8 @@ -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 -C Clean\sup\scomments\sin\sthe\stest_quota.c\ssource\sfile. -D 2010-09-01T13:09:57 +C Add\sthe\ssqlite3_quota_dump\stest\scommand.\s\sAdd\sa\sdestructor\sargument\son\nthe\ssqlite3_quota_set()\sinterface. +D 2010-09-01T14:35:49 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f F Makefile.in c599a15d268b1db2aeadea19df2adc3bf2eb6bee F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23 @@ -211,7 +211,7 @@ F src/test_mutex.c ce06b59aca168cd8c520b77159a24352a7469bd3 F src/test_onefile.c 40cf9e212a377a6511469384a64b01e6e34b2eec F src/test_osinst.c f408c6a181f2fb04c56273afd5c3e1e82f60392c F src/test_pcache.c 7bf828972ac0d2403f5cfa4cd14da41f8ebe73d8 -F src/test_quota.c e542d96d5e0ad7bbc064ab9912da3d8ed65cbe28 +F src/test_quota.c 92015bbb781f58a7073d03a4faf9981e46d679c4 F src/test_rtree.c e957a603a98871dcf005c1e96ae791cfe74eb7f6 F src/test_schema.c 8c06ef9ddb240c7a0fcd31bc221a6a2aade58bf0 F src/test_server.c bbba05c144b5fc4b52ff650a4328027b3fa5fcc6 @@ -565,7 +565,7 @@ F test/printf.test 05970cde31b1a9f54bd75af60597be75a5c54fea F test/progress.test 5b075c3c790c7b2a61419bc199db87aaf48b8301 F test/ptrchng.test ef1aa72d6cf35a2bbd0869a649b744e9d84977fc F test/quick.test 1681febc928d686362d50057c642f77a02c62e57 -F test/quota.test 0026c53f3ad78094f478c5e7048fb63602d59ee7 +F test/quota.test e6ea65a69ab79e77a9945d97004204fd6d8c3d97 F test/quote.test 215897dbe8de1a6f701265836d6601cc6ed103e6 F test/randexpr1.tcl 40dec52119ed3a2b8b2a773bce24b63a3a746459 F test/randexpr1.test 1084050991e9ba22c1c10edd8d84673b501cc25a @@ -856,14 +856,14 @@ F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f -P f5d263803084107389c5541face8d536b62fffe3 -R 35c143fcc0c1d935b095e49fe0b10b28 +P c1eec7dba698e5bad0870cb80079203f0fb89514 +R 5ccdd6b5ff8c4a7fd8c3537f33664271 U drh -Z 22044fa4707460c35e1dd2a52d813a99 +Z 37a3d6817393597a2d599801ebf7d8b4 -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (GNU/Linux) -iD8DBQFMflCroxKgR168RlERAupSAJ41VgmVbutErFMPzEt158JfEp4N/gCeLiry -ne2YPJhXeQQSv7tL27HzCOk= -=nNgv +iD8DBQFMfmTIoxKgR168RlERAkXOAJ9rg479NHW8icHIeLtH5Tt3WleCsQCfVywb +rV5uQ2LKNw+EzD5POJ3VPYQ= +=vORW -----END PGP SIGNATURE----- diff --git a/manifest.uuid b/manifest.uuid index 63e92c447c..e1c19f426f 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -c1eec7dba698e5bad0870cb80079203f0fb89514 \ No newline at end of file +7a624b5ae2abff21bcbbb34af88d1c7656f3b729 \ No newline at end of file diff --git a/src/test_quota.c b/src/test_quota.c index 61a8f6e8be..1e372ca9cc 100644 --- a/src/test_quota.c +++ b/src/test_quota.c @@ -35,7 +35,7 @@ /* Forward declaration of all object types */ typedef struct quotaGroup quotaGroup; -typedef struct quotaOpen quotaOpen; +typedef struct quotaConn quotaConn; typedef struct quotaFile quotaFile; /* @@ -63,6 +63,7 @@ struct quotaGroup { void *pArg /* Client data */ ); void *pArg; /* Third argument to the xCallback() */ + void (*xDestroy)(void*); /* Optional destructor for pArg */ quotaGroup *pNext, **ppPrev; /* Doubly linked list of all quota objects */ quotaFile *pFile; /* Files within this group */ }; @@ -89,7 +90,7 @@ struct quotaFile { ** subclass of sqlite3_file. The sqlite3_file object for the underlying ** VFS is appended to this structure. */ -struct quotaOpen { +struct quotaConn { sqlite3_file base; /* Base class - must be first */ quotaFile *pFile; /* The underlying file */ /* The underlying VFS sqlite3_file is appended to this object */ @@ -157,6 +158,7 @@ static void quotaGroupDeref(quotaGroup *p){ if( p->pFile==0 && p->iLimit==0 ){ if( p->pNext ) p->pNext->ppPrev = p->ppPrev; if( p->ppPrev ) *p->ppPrev = p->pNext; + if( p->xDestroy ) p->xDestroy(p->pArg); sqlite3_free(p); } } @@ -243,7 +245,7 @@ static int strglob(const char *zGlob, const char *z){ /* Find a quotaGroup given the filename. ** -** Return a pointer to the quotaOpen object. Return NULL if not found. +** Return a pointer to the quotaGroup object. Return NULL if not found. */ static quotaGroup *quotaGroupFind(const char *zFilename){ quotaGroup *p; @@ -251,11 +253,11 @@ static quotaGroup *quotaGroupFind(const char *zFilename){ return p; } -/* Translate an sqlite3_file* that is really a quotaOpen* into +/* Translate an sqlite3_file* that is really a quotaConn* into ** the sqlite3_file* for the underlying original VFS. */ -static sqlite3_file *quotaSubOpen(sqlite3_file *pOpen){ - quotaOpen *p = (quotaOpen*)pOpen; +static sqlite3_file *quotaSubOpen(sqlite3_file *pConn){ + quotaConn *p = (quotaConn*)pConn; return (sqlite3_file*)&p[1]; } @@ -267,15 +269,15 @@ static sqlite3_file *quotaSubOpen(sqlite3_file *pOpen){ ** simply links the new file into the appropriate quota group if it is a ** file that needs to be tracked. */ -static int quotaxOpen( +static int quotaOpen( sqlite3_vfs *pVfs, /* The quota VFS */ const char *zName, /* Name of file to be opened */ - sqlite3_file *pOpen, /* Fill in this file descriptor */ + sqlite3_file *pConn, /* Fill in this file descriptor */ int flags, /* Flags to control the opening */ int *pOutFlags /* Flags showing results of opening */ ){ int rc; /* Result code */ - quotaOpen *pQuotaOpen; /* The new quota file descriptor */ + quotaConn *pQuotaOpen; /* The new quota file descriptor */ quotaFile *pFile; /* Corresponding quotaFile obj */ quotaGroup *pGroup; /* The group file belongs to */ sqlite3_file *pSubOpen; /* Real file descriptor */ @@ -285,7 +287,7 @@ static int quotaxOpen( ** normal xOpen method. */ if( (flags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_WAL))==0 ){ - return pOrigVfs->xOpen(pOrigVfs, zName, pOpen, flags, pOutFlags); + return pOrigVfs->xOpen(pOrigVfs, zName, pConn, flags, pOutFlags); } /* If the name of the file does not match any quota group, then @@ -294,12 +296,12 @@ static int quotaxOpen( quotaEnter(); pGroup = quotaGroupFind(zName); if( pGroup==0 ){ - rc = pOrigVfs->xOpen(pOrigVfs, zName, pOpen, flags, pOutFlags); + rc = pOrigVfs->xOpen(pOrigVfs, zName, pConn, flags, pOutFlags); }else{ /* If we get to this point, it means the file needs to be quota tracked. */ - pQuotaOpen = (quotaOpen*)pOpen; - pSubOpen = quotaSubOpen(pOpen); + pQuotaOpen = (quotaConn*)pConn; + pSubOpen = quotaSubOpen(pConn); rc = pOrigVfs->xOpen(pOrigVfs, zName, pSubOpen, flags, pOutFlags); if( rc==SQLITE_OK ){ for(pFile=pGroup->pFile; pFile && strcmp(pFile->zFilename, zName); @@ -337,12 +339,13 @@ static int quotaxOpen( /************************ I/O Method Wrappers *******************************/ /* xClose requests get passed through to the original VFS. But we -** also have to unlink the quotaOpen from the quotaGroup. +** also have to unlink the quotaConn from the quotaFile and quotaGroup. +** The quotaFile and/or quotaGroup are freed if they are no longer in use. */ -static int quotaClose(sqlite3_file *pOpen){ - quotaOpen *p = (quotaOpen*)pOpen; +static int quotaClose(sqlite3_file *pConn){ + quotaConn *p = (quotaConn*)pConn; quotaFile *pFile = p->pFile; - sqlite3_file *pSubOpen = quotaSubOpen(pOpen); + sqlite3_file *pSubOpen = quotaSubOpen(pConn); int rc; rc = pSubOpen->pMethods->xClose(pSubOpen); quotaEnter(); @@ -363,12 +366,12 @@ static int quotaClose(sqlite3_file *pOpen){ ** further processing. */ static int quotaRead( - sqlite3_file *pOpen, + sqlite3_file *pConn, void *pBuf, int iAmt, sqlite3_int64 iOfst ){ - sqlite3_file *pSubOpen = quotaSubOpen(pOpen); + sqlite3_file *pSubOpen = quotaSubOpen(pConn); return pSubOpen->pMethods->xRead(pSubOpen, pBuf, iAmt, iOfst); } @@ -377,13 +380,13 @@ static int quotaRead( ** original VFS. */ static int quotaWrite( - sqlite3_file *pOpen, + sqlite3_file *pConn, const void *pBuf, int iAmt, sqlite3_int64 iOfst ){ - quotaOpen *p = (quotaOpen*)pOpen; - sqlite3_file *pSubOpen = quotaSubOpen(pOpen); + quotaConn *p = (quotaConn*)pConn; + sqlite3_file *pSubOpen = quotaSubOpen(pConn); sqlite3_int64 iEnd = iOfst+iAmt; quotaGroup *pGroup; quotaFile *pFile = p->pFile; @@ -413,9 +416,9 @@ static int quotaWrite( /* Pass xTruncate requests thru to the original VFS. If the ** success, update the file size. */ -static int quotaTruncate(sqlite3_file *pOpen, sqlite3_int64 size){ - quotaOpen *p = (quotaOpen*)pOpen; - sqlite3_file *pSubOpen = quotaSubOpen(pOpen); +static int quotaTruncate(sqlite3_file *pConn, sqlite3_int64 size){ + quotaConn *p = (quotaConn*)pConn; + sqlite3_file *pSubOpen = quotaSubOpen(pConn); int rc = pSubOpen->pMethods->xTruncate(pSubOpen, size); quotaFile *pFile = p->pFile; quotaGroup *pGroup; @@ -432,17 +435,17 @@ static int quotaTruncate(sqlite3_file *pOpen, sqlite3_int64 size){ /* Pass xSync requests through to the original VFS without change */ -static int quotaSync(sqlite3_file *pOpen, int flags){ - sqlite3_file *pSubOpen = quotaSubOpen(pOpen); +static int quotaSync(sqlite3_file *pConn, int flags){ + sqlite3_file *pSubOpen = quotaSubOpen(pConn); return pSubOpen->pMethods->xSync(pSubOpen, flags); } /* Pass xFileSize requests through to the original VFS but then ** update the quotaGroup with the new size before returning. */ -static int quotaFileSize(sqlite3_file *pOpen, sqlite3_int64 *pSize){ - quotaOpen *p = (quotaOpen*)pOpen; - sqlite3_file *pSubOpen = quotaSubOpen(pOpen); +static int quotaFileSize(sqlite3_file *pConn, sqlite3_int64 *pSize){ + quotaConn *p = (quotaConn*)pConn; + sqlite3_file *pSubOpen = quotaSubOpen(pConn); quotaFile *pFile = p->pFile; quotaGroup *pGroup; sqlite3_int64 sz; @@ -463,82 +466,82 @@ static int quotaFileSize(sqlite3_file *pOpen, sqlite3_int64 *pSize){ /* Pass xLock requests through to the original VFS unchanged. */ -static int quotaLock(sqlite3_file *pOpen, int lock){ - sqlite3_file *pSubOpen = quotaSubOpen(pOpen); +static int quotaLock(sqlite3_file *pConn, int lock){ + sqlite3_file *pSubOpen = quotaSubOpen(pConn); return pSubOpen->pMethods->xLock(pSubOpen, lock); } /* Pass xUnlock requests through to the original VFS unchanged. */ -static int quotaUnlock(sqlite3_file *pOpen, int lock){ - sqlite3_file *pSubOpen = quotaSubOpen(pOpen); +static int quotaUnlock(sqlite3_file *pConn, int lock){ + sqlite3_file *pSubOpen = quotaSubOpen(pConn); return pSubOpen->pMethods->xUnlock(pSubOpen, lock); } /* Pass xCheckReservedLock requests through to the original VFS unchanged. */ -static int quotaCheckReservedLock(sqlite3_file *pOpen, int *pResOut){ - sqlite3_file *pSubOpen = quotaSubOpen(pOpen); +static int quotaCheckReservedLock(sqlite3_file *pConn, int *pResOut){ + sqlite3_file *pSubOpen = quotaSubOpen(pConn); return pSubOpen->pMethods->xCheckReservedLock(pSubOpen, pResOut); } /* Pass xFileControl requests through to the original VFS unchanged. */ -static int quotaOpenControl(sqlite3_file *pOpen, int op, void *pArg){ - sqlite3_file *pSubOpen = quotaSubOpen(pOpen); +static int quotaFileControl(sqlite3_file *pConn, int op, void *pArg){ + sqlite3_file *pSubOpen = quotaSubOpen(pConn); return pSubOpen->pMethods->xFileControl(pSubOpen, op, pArg); } /* Pass xSectorSize requests through to the original VFS unchanged. */ -static int quotaSectorSize(sqlite3_file *pOpen){ - sqlite3_file *pSubOpen = quotaSubOpen(pOpen); +static int quotaSectorSize(sqlite3_file *pConn){ + sqlite3_file *pSubOpen = quotaSubOpen(pConn); return pSubOpen->pMethods->xSectorSize(pSubOpen); } /* Pass xDeviceCharacteristics requests through to the original VFS unchanged. */ -static int quotaDeviceCharacteristics(sqlite3_file *pOpen){ - sqlite3_file *pSubOpen = quotaSubOpen(pOpen); +static int quotaDeviceCharacteristics(sqlite3_file *pConn){ + sqlite3_file *pSubOpen = quotaSubOpen(pConn); return pSubOpen->pMethods->xDeviceCharacteristics(pSubOpen); } /* Pass xShmMap requests through to the original VFS unchanged. */ static int quotaShmMap( - sqlite3_file *pOpen, /* Handle open on database file */ + sqlite3_file *pConn, /* Handle open on database file */ int iRegion, /* Region to retrieve */ int szRegion, /* Size of regions */ int bExtend, /* True to extend file if necessary */ void volatile **pp /* OUT: Mapped memory */ ){ - sqlite3_file *pSubOpen = quotaSubOpen(pOpen); + sqlite3_file *pSubOpen = quotaSubOpen(pConn); return pSubOpen->pMethods->xShmMap(pSubOpen, iRegion, szRegion, bExtend, pp); } /* Pass xShmLock requests through to the original VFS unchanged. */ static int quotaShmLock( - sqlite3_file *pOpen, /* Database file holding the shared memory */ + sqlite3_file *pConn, /* Database file holding the shared memory */ int ofst, /* First lock to acquire or release */ int n, /* Number of locks to acquire or release */ int flags /* What to do with the lock */ ){ - sqlite3_file *pSubOpen = quotaSubOpen(pOpen); + sqlite3_file *pSubOpen = quotaSubOpen(pConn); return pSubOpen->pMethods->xShmLock(pSubOpen, ofst, n, flags); } /* Pass xShmBarrier requests through to the original VFS unchanged. */ -static void quotaShmBarrier(sqlite3_file *pOpen){ - sqlite3_file *pSubOpen = quotaSubOpen(pOpen); +static void quotaShmBarrier(sqlite3_file *pConn){ + sqlite3_file *pSubOpen = quotaSubOpen(pConn); pSubOpen->pMethods->xShmBarrier(pSubOpen); } /* Pass xShmUnmap requests through to the original VFS unchanged. */ -static int quotaShmUnmap(sqlite3_file *pOpen, int deleteFlag){ - sqlite3_file *pSubOpen = quotaSubOpen(pOpen); +static int quotaShmUnmap(sqlite3_file *pConn, int deleteFlag){ + sqlite3_file *pSubOpen = quotaSubOpen(pConn); return pSubOpen->pMethods->xShmUnmap(pSubOpen, deleteFlag); } @@ -567,8 +570,8 @@ int sqlite3_quota_initialize(const char *zOrigVfsName, int makeDefault){ gQuota.isInitialized = 1; gQuota.pOrigVfs = pOrigVfs; gQuota.sThisVfs = *pOrigVfs; - gQuota.sThisVfs.xOpen = quotaxOpen; - gQuota.sThisVfs.szOsFile += sizeof(quotaOpen); + gQuota.sThisVfs.xOpen = quotaOpen; + gQuota.sThisVfs.szOsFile += sizeof(quotaConn); gQuota.sThisVfs.zName = "quota"; gQuota.sIoMethodsV1.iVersion = 1; gQuota.sIoMethodsV1.xClose = quotaClose; @@ -580,7 +583,7 @@ int sqlite3_quota_initialize(const char *zOrigVfsName, int makeDefault){ gQuota.sIoMethodsV1.xLock = quotaLock; gQuota.sIoMethodsV1.xUnlock = quotaUnlock; gQuota.sIoMethodsV1.xCheckReservedLock = quotaCheckReservedLock; - gQuota.sIoMethodsV1.xFileControl = quotaOpenControl; + gQuota.sIoMethodsV1.xFileControl = quotaFileControl; gQuota.sIoMethodsV1.xSectorSize = quotaSectorSize; gQuota.sIoMethodsV1.xDeviceCharacteristics = quotaDeviceCharacteristics; gQuota.sIoMethodsV2 = gQuota.sIoMethodsV1; @@ -649,7 +652,8 @@ int sqlite3_quota_set( sqlite3_int64 iSize, /* Total size of all files in the group */ void *pArg /* Client data */ ), - void *pArg /* client data passed thru to callback */ + void *pArg, /* client data passed thru to callback */ + void (*xDestroy)(void*) /* Optional destructor for pArg */ ){ quotaGroup *pGroup; quotaEnter(); @@ -674,7 +678,11 @@ int sqlite3_quota_set( } pGroup->iLimit = iLimit; pGroup->xCallback = xCallback; + if( pGroup->xDestroy && pGroup->pArg!=pArg ){ + pGroup->xDestroy(pGroup->pArg); + } pGroup->pArg = pArg; + pGroup->xDestroy = xDestroy; quotaGroupDeref(pGroup); quotaLeave(); return SQLITE_OK; @@ -685,16 +693,21 @@ int sqlite3_quota_set( #ifdef SQLITE_TEST #include +/* +** Argument passed to a TCL quota-over-limit callback. +*/ typedef struct TclQuotaCallback TclQuotaCallback; struct TclQuotaCallback { - Tcl_Interp *interp; - Tcl_Obj *pScript; - TclQuotaCallback *pNext; + Tcl_Interp *interp; /* Interpreter in which to run the script */ + Tcl_Obj *pScript; /* Script to be run */ }; -static TclQuotaCallback *pQuotaCallbackList = 0; extern const char *sqlite3TestErrorName(int); + +/* +** This is the callback from a quota-over-limit. +*/ static void tclQuotaCallback( const char *zFilename, /* Name of file whose size increases */ sqlite3_int64 *piLimit, /* IN/OUT: The current limit */ @@ -733,6 +746,17 @@ static void tclQuotaCallback( if( rc!=TCL_OK ) Tcl_BackgroundError(p->interp); } +/* +** Destructor for a TCL quota-over-limit callback. +*/ +static void tclCallbackDestructor(void *pObj){ + TclQuotaCallback *p = (TclQuotaCallback*)pObj; + if( p ){ + Tcl_DecrRefCount(p->pScript); + ckfree((char *)p); + } +} + /* ** tclcmd: sqlite3_quota_initialize NAME MAKEDEFAULT */ @@ -782,19 +806,6 @@ static int test_quota_shutdown( rc = sqlite3_quota_shutdown(); Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC); - /* If the quota system was successfully shut down, delete all the quota - ** callback script objects in the global linked list. - */ - if( rc==SQLITE_OK ){ - TclQuotaCallback *p, *pNext; - for(p=pQuotaCallbackList; p; p=pNext){ - pNext = p->pNext; - Tcl_DecrRefCount(p->pScript); - ckfree((char *)p); - } - pQuotaCallbackList = 0; - } - return TCL_OK; } @@ -812,6 +823,7 @@ static int test_quota_set( Tcl_Obj *pScript; /* Tcl script to invoke to increase quota */ int rc; /* Value returned by quota_set() */ TclQuotaCallback *p; /* Callback object */ + int nScript; /* Length of callback script */ /* Process arguments */ if( objc!=4 ){ @@ -821,27 +833,68 @@ static int test_quota_set( zPattern = Tcl_GetString(objv[1]); if( Tcl_GetWideIntFromObj(interp, objv[2], &iLimit) ) return TCL_ERROR; pScript = objv[3]; + Tcl_GetStringFromObj(pScript, &nScript); - /* Allocate a TclQuotaCallback object */ - p = (TclQuotaCallback *)ckalloc(sizeof(TclQuotaCallback)); - memset(p, 0, sizeof(TclQuotaCallback)); - - /* Invoke sqlite3_quota_set() */ - rc = sqlite3_quota_set(zPattern, iLimit, tclQuotaCallback, (void *)p); - if( rc!=SQLITE_OK ){ - ckfree((char *)p); - }else{ - Tcl_IncrRefCount(pScript); + if( nScript>0 ){ + /* Allocate a TclQuotaCallback object */ + p = (TclQuotaCallback *)ckalloc(sizeof(TclQuotaCallback)); + memset(p, 0, sizeof(TclQuotaCallback)); p->interp = interp; + Tcl_IncrRefCount(pScript); p->pScript = pScript; - p->pNext = pQuotaCallbackList; - pQuotaCallbackList = p; + }else{ + p = 0; } + /* Invoke sqlite3_quota_set() */ + rc = sqlite3_quota_set(zPattern, iLimit, tclQuotaCallback, + (void*)p, tclCallbackDestructor); Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC); return TCL_OK; } +/* +** tclcmd: sqlite3_quota_dump +*/ +static int test_quota_dump( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + Tcl_Obj *pResult; + Tcl_Obj *pGroupTerm; + Tcl_Obj *pFileTerm; + quotaGroup *pGroup; + quotaFile *pFile; + + pResult = Tcl_NewObj(); + quotaEnter(); + for(pGroup=gQuota.pGroup; pGroup; pGroup=pGroup->pNext){ + pGroupTerm = Tcl_NewObj(); + Tcl_ListObjAppendElement(interp, pGroupTerm, + Tcl_NewStringObj(pGroup->zPattern, -1)); + Tcl_ListObjAppendElement(interp, pGroupTerm, + Tcl_NewWideIntObj(pGroup->iLimit)); + Tcl_ListObjAppendElement(interp, pGroupTerm, + Tcl_NewWideIntObj(pGroup->iSize)); + for(pFile=pGroup->pFile; pFile; pFile=pFile->pNext){ + pFileTerm = Tcl_NewObj(); + Tcl_ListObjAppendElement(interp, pFileTerm, + Tcl_NewStringObj(pFile->zFilename, -1)); + Tcl_ListObjAppendElement(interp, pFileTerm, + Tcl_NewWideIntObj(pFile->iSize)); + Tcl_ListObjAppendElement(interp, pFileTerm, + Tcl_NewWideIntObj(pFile->nRef)); + Tcl_ListObjAppendElement(interp, pGroupTerm, pFileTerm); + } + Tcl_ListObjAppendElement(interp, pResult, pGroupTerm); + } + quotaLeave(); + Tcl_SetObjResult(interp, pResult); + return TCL_OK; +} + /* ** This routine registers the custom TCL commands defined in this ** module. This should be the only procedure visible from outside @@ -855,6 +908,7 @@ int Sqlitequota_Init(Tcl_Interp *interp){ { "sqlite3_quota_initialize", test_quota_initialize }, { "sqlite3_quota_shutdown", test_quota_shutdown }, { "sqlite3_quota_set", test_quota_set }, + { "sqlite3_quota_dump", test_quota_dump }, }; int i; diff --git a/test/quota.test b/test/quota.test index 9288b5f8b8..2890fe0624 100644 --- a/test/quota.test +++ b/test/quota.test @@ -101,7 +101,7 @@ do_test quota-2.4.2 { } {1 {database or disk is full}} do_test quota-2.4.3 { set ::quota } {5120 6144} do_test quota-2.4.4 { file size test.db } {5120} -do_test quota-2.4.5 { +do_test quota-2.4.99 { db close sqlite3_quota_shutdown } {SQLITE_OK} @@ -139,12 +139,35 @@ do_test quota-3.1.3 { execsql { CREATE TABLE t2(a, b) } db2 set ::quota } {} +puts "quotas: [sqlite3_quota_dump]" do_test quota-3.X { catch { db close } catch { db2 close } - sqlite3_quota_shutdown -} {SQLITE_OK} +} {0} + +#------------------------------------------------------------------------- +# Quotas are deleted when unused and when there limit is set to zero +# + +# Return a list of all currently defined quotas. Each quota is identified +# by its pattern. +proc quota_list {} { + set allq {} + foreach q [sqlite3_quota_dump] { + lappend allq [lindex $q 0] + } + return [lsort $allq] +} +do_test quota-4.1 { + sqlite3_quota_set *test.db 0 {} + quota_list +} {} +do_test quota-4.2 { + sqlite3_quota_set *test.db 4096 {} + quota_list +} {*test.db} + + +sqlite3_quota_shutdown finish_test - - From 61cc423aa3c8c147abf46039ab9a84474a6ef278 Mon Sep 17 00:00:00 2001 From: drh Date: Wed, 1 Sep 2010 14:45:16 +0000 Subject: [PATCH 5/7] Make all private routines in test_quota.c begin with "quota". Fix a test_quota.c segfault when setting a zero-quota. FossilOrigin-Name: c0d0fc3a1cca7ee28db8add745c71baf0545776c --- manifest | 20 ++++++++++---------- manifest.uuid | 2 +- src/test_quota.c | 15 ++++++++++----- test/quota.test | 12 ++++++++++++ 4 files changed, 33 insertions(+), 16 deletions(-) diff --git a/manifest b/manifest index ebafdadb9d..58d0ccff43 100644 --- a/manifest +++ b/manifest @@ -1,8 +1,8 @@ -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 -C Add\sthe\ssqlite3_quota_dump\stest\scommand.\s\sAdd\sa\sdestructor\sargument\son\nthe\ssqlite3_quota_set()\sinterface. -D 2010-09-01T14:35:49 +C Make\sall\sprivate\sroutines\sin\stest_quota.c\sbegin\swith\s"quota".\nFix\sa\stest_quota.c\ssegfault\swhen\ssetting\sa\szero-quota. +D 2010-09-01T14:45:16 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f F Makefile.in c599a15d268b1db2aeadea19df2adc3bf2eb6bee F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23 @@ -211,7 +211,7 @@ F src/test_mutex.c ce06b59aca168cd8c520b77159a24352a7469bd3 F src/test_onefile.c 40cf9e212a377a6511469384a64b01e6e34b2eec F src/test_osinst.c f408c6a181f2fb04c56273afd5c3e1e82f60392c F src/test_pcache.c 7bf828972ac0d2403f5cfa4cd14da41f8ebe73d8 -F src/test_quota.c 92015bbb781f58a7073d03a4faf9981e46d679c4 +F src/test_quota.c 8c28bb3f66a8bc90a458f16876ebaa89e4bbad3e F src/test_rtree.c e957a603a98871dcf005c1e96ae791cfe74eb7f6 F src/test_schema.c 8c06ef9ddb240c7a0fcd31bc221a6a2aade58bf0 F src/test_server.c bbba05c144b5fc4b52ff650a4328027b3fa5fcc6 @@ -565,7 +565,7 @@ F test/printf.test 05970cde31b1a9f54bd75af60597be75a5c54fea F test/progress.test 5b075c3c790c7b2a61419bc199db87aaf48b8301 F test/ptrchng.test ef1aa72d6cf35a2bbd0869a649b744e9d84977fc F test/quick.test 1681febc928d686362d50057c642f77a02c62e57 -F test/quota.test e6ea65a69ab79e77a9945d97004204fd6d8c3d97 +F test/quota.test a2faf03b630bf9d6dc03d5f4cf18ddb7ef1b2372 F test/quote.test 215897dbe8de1a6f701265836d6601cc6ed103e6 F test/randexpr1.tcl 40dec52119ed3a2b8b2a773bce24b63a3a746459 F test/randexpr1.test 1084050991e9ba22c1c10edd8d84673b501cc25a @@ -856,14 +856,14 @@ F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f -P c1eec7dba698e5bad0870cb80079203f0fb89514 -R 5ccdd6b5ff8c4a7fd8c3537f33664271 +P 7a624b5ae2abff21bcbbb34af88d1c7656f3b729 +R 4041710572588b49cf8e3c314659cdde U drh -Z 37a3d6817393597a2d599801ebf7d8b4 +Z c4eac6ed50191a086b6db4a1d87dd5c9 -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (GNU/Linux) -iD8DBQFMfmTIoxKgR168RlERAkXOAJ9rg479NHW8icHIeLtH5Tt3WleCsQCfVywb -rV5uQ2LKNw+EzD5POJ3VPYQ= -=vORW +iD8DBQFMfmb+oxKgR168RlERAualAJ4oGP4giCs27HA69gTkp0EaPArqVwCdEPwb +lAxAbOSJt/sYZvU7C3FlLw0= +=nKqo -----END PGP SIGNATURE----- diff --git a/manifest.uuid b/manifest.uuid index e1c19f426f..67a012f598 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -7a624b5ae2abff21bcbbb34af88d1c7656f3b729 \ No newline at end of file +c0d0fc3a1cca7ee28db8add745c71baf0545776c \ No newline at end of file diff --git a/src/test_quota.c b/src/test_quota.c index 1e372ca9cc..3790b5721c 100644 --- a/src/test_quota.c +++ b/src/test_quota.c @@ -178,7 +178,7 @@ static void quotaGroupDeref(quotaGroup *p){ ** [^...] Matches one character not in the enclosed list. ** */ -static int strglob(const char *zGlob, const char *z){ +static int quotaStrglob(const char *zGlob, const char *z){ int c, c2; int invert; int seen; @@ -191,7 +191,7 @@ static int strglob(const char *zGlob, const char *z){ if( c==0 ){ return 1; }else if( c=='[' ){ - while( *z && strglob(zGlob-1,z)==0 ){ + while( *z && quotaStrglob(zGlob-1,z)==0 ){ z++; } return (*z)!=0; @@ -201,7 +201,7 @@ static int strglob(const char *zGlob, const char *z){ c2 = *(z++); if( c2==0 ) return 0; } - if( strglob(zGlob,z) ) return 1; + if( quotaStrglob(zGlob,z) ) return 1; } return 0; }else if( c=='?' ){ @@ -249,7 +249,8 @@ static int strglob(const char *zGlob, const char *z){ */ static quotaGroup *quotaGroupFind(const char *zFilename){ quotaGroup *p; - for(p=gQuota.pGroup; p && strglob(p->zPattern, zFilename)==0; p=p->pNext){} + for(p=gQuota.pGroup; p && quotaStrglob(p->zPattern, zFilename)==0; + p=p->pNext){} return p; } @@ -661,8 +662,12 @@ int sqlite3_quota_set( while( pGroup && strcmp(pGroup->zPattern, zPattern)!=0 ){ pGroup = pGroup->pNext; } - if( pGroup==0 && iLimit>0 ){ + if( pGroup==0 ){ int nPattern = strlen(zPattern); + if( iLimit<=0 ){ + quotaLeave(); + return SQLITE_OK; + } pGroup = sqlite3_malloc( sizeof(*pGroup) + nPattern + 1 ); if( pGroup==0 ){ quotaLeave(); diff --git a/test/quota.test b/test/quota.test index 2890fe0624..82d04aa7ef 100644 --- a/test/quota.test +++ b/test/quota.test @@ -167,6 +167,18 @@ do_test quota-4.2 { sqlite3_quota_set *test.db 4096 {} quota_list } {*test.db} +do_test quota-4.3 { + sqlite3_quota_set *test2.db 0 {} + quota_list +} {*test.db} +do_test quota-4.4 { + sqlite3_quota_set *test2.db 12345 {} + quota_list +} {*test.db *test2.db} +do_test quota-4.5 { + sqlite3_quota_set *test.db 0 {} + quota_list +} {*test2.db} sqlite3_quota_shutdown From 75c19174230e1cc4505c00f93a1829c97d1752f9 Mon Sep 17 00:00:00 2001 From: drh Date: Wed, 1 Sep 2010 14:58:10 +0000 Subject: [PATCH 6/7] Variable name and comment changes to test_quota.c for clearer presentation. FossilOrigin-Name: 38ed1992c800cb1d1ca67977d704df73d97c78d7 --- manifest | 18 +++++++++--------- manifest.uuid | 2 +- src/test_quota.c | 40 ++++++++++++++++++++-------------------- 3 files changed, 30 insertions(+), 30 deletions(-) diff --git a/manifest b/manifest index 58d0ccff43..8bb0bb16bd 100644 --- a/manifest +++ b/manifest @@ -1,8 +1,8 @@ -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 -C Make\sall\sprivate\sroutines\sin\stest_quota.c\sbegin\swith\s"quota".\nFix\sa\stest_quota.c\ssegfault\swhen\ssetting\sa\szero-quota. -D 2010-09-01T14:45:16 +C Variable\sname\sand\scomment\schanges\sto\stest_quota.c\sfor\sclearer\spresentation. +D 2010-09-01T14:58:11 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f F Makefile.in c599a15d268b1db2aeadea19df2adc3bf2eb6bee F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23 @@ -211,7 +211,7 @@ F src/test_mutex.c ce06b59aca168cd8c520b77159a24352a7469bd3 F src/test_onefile.c 40cf9e212a377a6511469384a64b01e6e34b2eec F src/test_osinst.c f408c6a181f2fb04c56273afd5c3e1e82f60392c F src/test_pcache.c 7bf828972ac0d2403f5cfa4cd14da41f8ebe73d8 -F src/test_quota.c 8c28bb3f66a8bc90a458f16876ebaa89e4bbad3e +F src/test_quota.c 303e68971829ed6c7058f982e7361b967344abda F src/test_rtree.c e957a603a98871dcf005c1e96ae791cfe74eb7f6 F src/test_schema.c 8c06ef9ddb240c7a0fcd31bc221a6a2aade58bf0 F src/test_server.c bbba05c144b5fc4b52ff650a4328027b3fa5fcc6 @@ -856,14 +856,14 @@ F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f -P 7a624b5ae2abff21bcbbb34af88d1c7656f3b729 -R 4041710572588b49cf8e3c314659cdde +P c0d0fc3a1cca7ee28db8add745c71baf0545776c +R 5150c60917fb33e6d6c75a2627b04e72 U drh -Z c4eac6ed50191a086b6db4a1d87dd5c9 +Z 293ea454d360072e187630189bf4cbd5 -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (GNU/Linux) -iD8DBQFMfmb+oxKgR168RlERAualAJ4oGP4giCs27HA69gTkp0EaPArqVwCdEPwb -lAxAbOSJt/sYZvU7C3FlLw0= -=nKqo +iD8DBQFMfmoGoxKgR168RlERAo+aAJ9FtwNxBNFRgcjszaS9G+1zDHJu5ACeLSkW +b7HAKmHRdtJZIZFAnlXrRmk= +=zaPS -----END PGP SIGNATURE----- diff --git a/manifest.uuid b/manifest.uuid index 67a012f598..58db5a3abb 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -c0d0fc3a1cca7ee28db8add745c71baf0545776c \ No newline at end of file +38ed1992c800cb1d1ca67977d704df73d97c78d7 \ No newline at end of file diff --git a/src/test_quota.c b/src/test_quota.c index 3790b5721c..692c28f982 100644 --- a/src/test_quota.c +++ b/src/test_quota.c @@ -65,7 +65,7 @@ struct quotaGroup { void *pArg; /* Third argument to the xCallback() */ void (*xDestroy)(void*); /* Optional destructor for pArg */ quotaGroup *pNext, **ppPrev; /* Doubly linked list of all quota objects */ - quotaFile *pFile; /* Files within this group */ + quotaFile *pFiles; /* Files within this group */ }; /* @@ -78,7 +78,7 @@ struct quotaGroup { */ struct quotaFile { char *zFilename; /* Name of this file */ - quotaGroup *pGroup; /* Upper bound on file size */ + quotaGroup *pGroup; /* Quota group to which this file belongs */ sqlite3_int64 iSize; /* Current size of this file */ int nRef; /* Number of times this file is open */ quotaFile *pNext, **ppPrev; /* Linked list of files in the same group */ @@ -154,12 +154,12 @@ static void quotaLeave(void){ sqlite3_mutex_leave(gQuota.pMutex); } /* If the reference count and threshold for a quotaGroup are both ** zero, then destroy the quotaGroup. */ -static void quotaGroupDeref(quotaGroup *p){ - if( p->pFile==0 && p->iLimit==0 ){ - if( p->pNext ) p->pNext->ppPrev = p->ppPrev; - if( p->ppPrev ) *p->ppPrev = p->pNext; - if( p->xDestroy ) p->xDestroy(p->pArg); - sqlite3_free(p); +static void quotaGroupDeref(quotaGroup *pGroup){ + if( pGroup->pFiles==0 && pGroup->iLimit==0 ){ + if( pGroup->pNext ) pGroup->pNext->ppPrev = pGroup->ppPrev; + if( pGroup->ppPrev ) *pGroup->ppPrev = pGroup->pNext; + if( pGroup->xDestroy ) pGroup->xDestroy(pGroup->pArg); + sqlite3_free(pGroup); } } @@ -305,7 +305,7 @@ static int quotaOpen( pSubOpen = quotaSubOpen(pConn); rc = pOrigVfs->xOpen(pOrigVfs, zName, pSubOpen, flags, pOutFlags); if( rc==SQLITE_OK ){ - for(pFile=pGroup->pFile; pFile && strcmp(pFile->zFilename, zName); + for(pFile=pGroup->pFiles; pFile && strcmp(pFile->zFilename, zName); pFile=pFile->pNext){} if( pFile==0 ){ int nName = strlen(zName); @@ -318,10 +318,10 @@ static int quotaOpen( memset(pFile, 0, sizeof(*pFile)); pFile->zFilename = (char*)&pFile[1]; memcpy(pFile->zFilename, zName, nName+1); - pFile->pNext = pGroup->pFile; - if( pGroup->pFile ) pGroup->pFile->ppPrev = &pFile->pNext; - pFile->ppPrev = &pGroup->pFile; - pGroup->pFile = pFile; + pFile->pNext = pGroup->pFiles; + if( pGroup->pFiles ) pGroup->pFiles->ppPrev = &pFile->pNext; + pFile->ppPrev = &pGroup->pFiles; + pGroup->pFiles = pFile; pFile->pGroup = pGroup; } pFile->nRef++; @@ -607,15 +607,15 @@ int sqlite3_quota_initialize(const char *zOrigVfsName, int makeDefault){ ** shutting down in order to free all remaining quota groups. */ int sqlite3_quota_shutdown(void){ - quotaGroup *p; + quotaGroup *pGroup; if( gQuota.isInitialized==0 ) return SQLITE_MISUSE; - for(p=gQuota.pGroup; p; p=p->pNext){ - if( p->pFile ) return SQLITE_MISUSE; + for(pGroup=gQuota.pGroup; pGroup; pGroup=pGroup->pNext){ + if( pGroup->pFiles ) return SQLITE_MISUSE; } while( gQuota.pGroup ){ - quotaGroup *p = gQuota.pGroup; - gQuota.pGroup = p->pNext; - sqlite3_free(p); + pGroup = gQuota.pGroup; + gQuota.pGroup = pGroup->pNext; + sqlite3_free(pGroup); } gQuota.isInitialized = 0; sqlite3_mutex_free(gQuota.pMutex); @@ -883,7 +883,7 @@ static int test_quota_dump( Tcl_NewWideIntObj(pGroup->iLimit)); Tcl_ListObjAppendElement(interp, pGroupTerm, Tcl_NewWideIntObj(pGroup->iSize)); - for(pFile=pGroup->pFile; pFile; pFile=pFile->pNext){ + for(pFile=pGroup->pFiles; pFile; pFile=pFile->pNext){ pFileTerm = Tcl_NewObj(); Tcl_ListObjAppendElement(interp, pFileTerm, Tcl_NewStringObj(pFile->zFilename, -1)); From 3d3e60e4816ae15b47b1c4f8e0b2e7ecd3041dce Mon Sep 17 00:00:00 2001 From: drh Date: Wed, 1 Sep 2010 15:11:38 +0000 Subject: [PATCH 7/7] Fix the TCL interface to test_quota.c so that it works with empty callback scripts. FossilOrigin-Name: 19e95f63359589766da673aac99d19a355a92678 --- manifest | 20 ++++++++++---------- manifest.uuid | 2 +- src/test_quota.c | 1 + test/quota.test | 31 ++++++++++++++++++++++++++++++- 4 files changed, 42 insertions(+), 12 deletions(-) diff --git a/manifest b/manifest index 8bb0bb16bd..f425682c8e 100644 --- a/manifest +++ b/manifest @@ -1,8 +1,8 @@ -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 -C Variable\sname\sand\scomment\schanges\sto\stest_quota.c\sfor\sclearer\spresentation. -D 2010-09-01T14:58:11 +C Fix\sthe\sTCL\sinterface\sto\stest_quota.c\sso\sthat\sit\sworks\swith\sempty\scallback\nscripts. +D 2010-09-01T15:11:39 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f F Makefile.in c599a15d268b1db2aeadea19df2adc3bf2eb6bee F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23 @@ -211,7 +211,7 @@ F src/test_mutex.c ce06b59aca168cd8c520b77159a24352a7469bd3 F src/test_onefile.c 40cf9e212a377a6511469384a64b01e6e34b2eec F src/test_osinst.c f408c6a181f2fb04c56273afd5c3e1e82f60392c F src/test_pcache.c 7bf828972ac0d2403f5cfa4cd14da41f8ebe73d8 -F src/test_quota.c 303e68971829ed6c7058f982e7361b967344abda +F src/test_quota.c 875292fc8a731d6d35eeb637c70b834b75f9b479 F src/test_rtree.c e957a603a98871dcf005c1e96ae791cfe74eb7f6 F src/test_schema.c 8c06ef9ddb240c7a0fcd31bc221a6a2aade58bf0 F src/test_server.c bbba05c144b5fc4b52ff650a4328027b3fa5fcc6 @@ -565,7 +565,7 @@ F test/printf.test 05970cde31b1a9f54bd75af60597be75a5c54fea F test/progress.test 5b075c3c790c7b2a61419bc199db87aaf48b8301 F test/ptrchng.test ef1aa72d6cf35a2bbd0869a649b744e9d84977fc F test/quick.test 1681febc928d686362d50057c642f77a02c62e57 -F test/quota.test a2faf03b630bf9d6dc03d5f4cf18ddb7ef1b2372 +F test/quota.test d406716c5e91df17ef7821facae6a9aefaa4dd91 F test/quote.test 215897dbe8de1a6f701265836d6601cc6ed103e6 F test/randexpr1.tcl 40dec52119ed3a2b8b2a773bce24b63a3a746459 F test/randexpr1.test 1084050991e9ba22c1c10edd8d84673b501cc25a @@ -856,14 +856,14 @@ F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f -P c0d0fc3a1cca7ee28db8add745c71baf0545776c -R 5150c60917fb33e6d6c75a2627b04e72 +P 38ed1992c800cb1d1ca67977d704df73d97c78d7 +R b0e1f5354d97250a31257e5a21d367a2 U drh -Z 293ea454d360072e187630189bf4cbd5 +Z 77e1612242203a9e9c25a20eeda4feae -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (GNU/Linux) -iD8DBQFMfmoGoxKgR168RlERAo+aAJ9FtwNxBNFRgcjszaS9G+1zDHJu5ACeLSkW -b7HAKmHRdtJZIZFAnlXrRmk= -=zaPS +iD8DBQFMfm0uoxKgR168RlERAoI8AKCOKs12QYn4Q1N8Y7BoPjxhD0VzxQCfVXt0 +LDxzSW3L5N21o+i8h29WkWg= +=8m6y -----END PGP SIGNATURE----- diff --git a/manifest.uuid b/manifest.uuid index 58db5a3abb..083b42c310 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -38ed1992c800cb1d1ca67977d704df73d97c78d7 \ No newline at end of file +19e95f63359589766da673aac99d19a355a92678 \ No newline at end of file diff --git a/src/test_quota.c b/src/test_quota.c index 692c28f982..0ed6e7dda3 100644 --- a/src/test_quota.c +++ b/src/test_quota.c @@ -726,6 +726,7 @@ static void tclQuotaCallback( int rc; /* Tcl error code */ p = (TclQuotaCallback *)pArg; + if( p==0 ) return; pVarname = Tcl_NewStringObj("::piLimit_", -1); Tcl_IncrRefCount(pVarname); diff --git a/test/quota.test b/test/quota.test index 82d04aa7ef..9dca2d7204 100644 --- a/test/quota.test +++ b/test/quota.test @@ -159,6 +159,7 @@ proc quota_list {} { } return [lsort $allq] } + do_test quota-4.1 { sqlite3_quota_set *test.db 0 {} quota_list @@ -172,13 +173,41 @@ do_test quota-4.3 { quota_list } {*test.db} do_test quota-4.4 { - sqlite3_quota_set *test2.db 12345 {} + sqlite3_quota_set *test2.db 100000 {} quota_list } {*test.db *test2.db} do_test quota-4.5 { sqlite3_quota_set *test.db 0 {} quota_list } {*test2.db} +do_test quota-4.6 { + file delete -force test2.db test2.db-journal test2.db-wal + sqlite3 db test2.db + db eval {CREATE TABLE t2(x); INSERT INTO t2 VALUES('tab-t2');} + quota_list +} {*test2.db} +do_test quota-4.7 { + catchsql {INSERT INTO t2 VALUES(zeroblob(200000))} +} {1 {database or disk is full}} +do_test quota-4.8 { + sqlite3 db2 test2.db + db2 eval {SELECT * FROM t2} +} {tab-t2} +do_test quota-4.9 { + sqlite3_quota_set *test2.db 0 {} + catchsql {INSERT INTO t2 VALUES(zeroblob(200000))} +} {0 {}} +do_test quota-4.10 { + quota_list +} {*test2.db} +do_test quota-4.11 { + db2 close + quota_list +} {*test2.db} +do_test quota-4.12 { + db close + quota_list +} {} sqlite3_quota_shutdown