18cf35eb4Sdan /* 28cf35eb4Sdan ** 2010 September 31 38cf35eb4Sdan ** 48cf35eb4Sdan ** The author disclaims copyright to this source code. In place of 58cf35eb4Sdan ** a legal notice, here is a blessing: 68cf35eb4Sdan ** 78cf35eb4Sdan ** May you do good and not evil. 88cf35eb4Sdan ** May you find forgiveness for yourself and forgive others. 98cf35eb4Sdan ** May you share freely, never taking more than you give. 108cf35eb4Sdan ** 118cf35eb4Sdan ************************************************************************* 128cf35eb4Sdan ** 138cf35eb4Sdan ** This file contains a VFS "shim" - a layer that sits in between the 148cf35eb4Sdan ** pager and the real VFS. 158cf35eb4Sdan ** 168cf35eb4Sdan ** This particular shim enforces a quota system on files. One or more 178cf35eb4Sdan ** database files are in a "quota group" that is defined by a GLOB 188cf35eb4Sdan ** pattern. A quota is set for the combined size of all files in the 198cf35eb4Sdan ** the group. A quota of zero means "no limit". If the total size 208cf35eb4Sdan ** of all files in the quota group is met or exceeded, then new 218cf35eb4Sdan ** write requests that attempt to enlarge a file fail with SQLITE_FULL. 228cf35eb4Sdan ** 238cf35eb4Sdan ** However, before returning SQLITE_FULL, the write requests invoke 248cf35eb4Sdan ** a callback function that is configurable for each quota group. 258cf35eb4Sdan ** This callback has the opportunity to enlarge the quota. If the 268cf35eb4Sdan ** callback does enlarge the quota such that the total size of all 278cf35eb4Sdan ** files within the group is less than the new quota, then the write 288cf35eb4Sdan ** continues as if nothing had happened. 298cf35eb4Sdan */ 308cf35eb4Sdan #include "sqlite3.h" 318cf35eb4Sdan #include <string.h> 328cf35eb4Sdan #include <assert.h> 338cf35eb4Sdan 348cf35eb4Sdan /************************ Object Definitions ******************************/ 358cf35eb4Sdan 36745de2e4Sdrh /* Forward declaration of all object types */ 37745de2e4Sdrh typedef struct quotaGroup quotaGroup; 38745de2e4Sdrh typedef struct quotaOpen quotaOpen; 39745de2e4Sdrh typedef struct quotaFile quotaFile; 40745de2e4Sdrh 418cf35eb4Sdan /* 42*549dc1baSdrh ** A "quota group" is a collection of files whose collective size we want 43*549dc1baSdrh ** to limit. Each quota group is defined by a GLOB pattern. 448cf35eb4Sdan ** 45*549dc1baSdrh ** There is an instance of the following object for each defined quota 46*549dc1baSdrh ** group. This object records the GLOB pattern that defines which files 47*549dc1baSdrh ** belong to the quota group. The object also remembers the size limit 48*549dc1baSdrh ** for the group (the quota) and the callback to be invoked when the 49*549dc1baSdrh ** sum of the sizes of the files within the group goes over the limit. 508cf35eb4Sdan ** 51*549dc1baSdrh ** A quota group must be established (using sqlite3_quota_set(...)) 52*549dc1baSdrh ** prior to opening any of the database connections that access files 53*549dc1baSdrh ** within the quota group. 548cf35eb4Sdan */ 558cf35eb4Sdan struct quotaGroup { 568cf35eb4Sdan const char *zPattern; /* Filename pattern to be quotaed */ 578cf35eb4Sdan sqlite3_int64 iLimit; /* Upper bound on total file size */ 588cf35eb4Sdan sqlite3_int64 iSize; /* Current size of all files */ 598cf35eb4Sdan void (*xCallback)( /* Callback invoked when going over quota */ 608cf35eb4Sdan const char *zFilename, /* Name of file whose size increases */ 618cf35eb4Sdan sqlite3_int64 *piLimit, /* IN/OUT: The current limit */ 628cf35eb4Sdan sqlite3_int64 iSize, /* Total size of all files in the group */ 638cf35eb4Sdan void *pArg /* Client data */ 648cf35eb4Sdan ); 658cf35eb4Sdan void *pArg; /* Third argument to the xCallback() */ 668cf35eb4Sdan quotaGroup *pNext, **ppPrev; /* Doubly linked list of all quota objects */ 67745de2e4Sdrh quotaFile *pFile; /* Files within this group */ 688cf35eb4Sdan }; 698cf35eb4Sdan 708cf35eb4Sdan /* 71745de2e4Sdrh ** An instance of this structure represents a single file that is part 72745de2e4Sdrh ** of a quota group. A single file can be opened multiple times. In 73745de2e4Sdrh ** order keep multiple openings of the same file from causing the size 74*549dc1baSdrh ** of the file to count against the quota multiple times, each file 75745de2e4Sdrh ** has a unique instance of this object and multiple open connections 76745de2e4Sdrh ** to the same file each point to a single instance of this object. 778cf35eb4Sdan */ 788cf35eb4Sdan struct quotaFile { 79745de2e4Sdrh char *zFilename; /* Name of this file */ 808cf35eb4Sdan quotaGroup *pGroup; /* Upper bound on file size */ 818cf35eb4Sdan sqlite3_int64 iSize; /* Current size of this file */ 82745de2e4Sdrh int nRef; /* Number of times this file is open */ 83745de2e4Sdrh quotaFile *pNext, **ppPrev; /* Linked list of files in the same group */ 84745de2e4Sdrh }; 85745de2e4Sdrh 86745de2e4Sdrh /* 87745de2e4Sdrh ** An instance of the following object represents each open connection 88*549dc1baSdrh ** to a file that participates in quota tracking. This object is a 89*549dc1baSdrh ** subclass of sqlite3_file. The sqlite3_file object for the underlying 90*549dc1baSdrh ** VFS is appended to this structure. 91745de2e4Sdrh */ 92745de2e4Sdrh struct quotaOpen { 93745de2e4Sdrh sqlite3_file base; /* Base class - must be first */ 94745de2e4Sdrh quotaFile *pFile; /* The underlying file */ 958cf35eb4Sdan /* The underlying VFS sqlite3_file is appended to this object */ 968cf35eb4Sdan }; 978cf35eb4Sdan 988cf35eb4Sdan /************************* Global Variables **********************************/ 998cf35eb4Sdan /* 1008cf35eb4Sdan ** All global variables used by this file are containing within the following 1018cf35eb4Sdan ** gQuota structure. 1028cf35eb4Sdan */ 1038cf35eb4Sdan static struct { 104*549dc1baSdrh /* The pOrigVfs is the real, original underlying VFS implementation. 1058cf35eb4Sdan ** Most operations pass-through to the real VFS. This value is read-only 1068cf35eb4Sdan ** during operation. It is only modified at start-time and thus does not 1078cf35eb4Sdan ** require a mutex. 1088cf35eb4Sdan */ 1098cf35eb4Sdan sqlite3_vfs *pOrigVfs; 1108cf35eb4Sdan 1118cf35eb4Sdan /* The sThisVfs is the VFS structure used by this shim. It is initialized 1128cf35eb4Sdan ** at start-time and thus does not require a mutex 1138cf35eb4Sdan */ 1148cf35eb4Sdan sqlite3_vfs sThisVfs; 1158cf35eb4Sdan 1168cf35eb4Sdan /* The sIoMethods defines the methods used by sqlite3_file objects 1178cf35eb4Sdan ** associated with this shim. It is initialized at start-time and does 1188cf35eb4Sdan ** not require a mutex. 1198cf35eb4Sdan ** 1208cf35eb4Sdan ** When the underlying VFS is called to open a file, it might return 1218cf35eb4Sdan ** either a version 1 or a version 2 sqlite3_file object. This shim 1228cf35eb4Sdan ** has to create a wrapper sqlite3_file of the same version. Hence 1238cf35eb4Sdan ** there are two I/O method structures, one for version 1 and the other 1248cf35eb4Sdan ** for version 2. 1258cf35eb4Sdan */ 1268cf35eb4Sdan sqlite3_io_methods sIoMethodsV1; 1278cf35eb4Sdan sqlite3_io_methods sIoMethodsV2; 1288cf35eb4Sdan 1298cf35eb4Sdan /* True when this shim as been initialized. 1308cf35eb4Sdan */ 1318cf35eb4Sdan int isInitialized; 1328cf35eb4Sdan 1338cf35eb4Sdan /* For run-time access any of the other global data structures in this 1348cf35eb4Sdan ** shim, the following mutex must be held. 1358cf35eb4Sdan */ 1368cf35eb4Sdan sqlite3_mutex *pMutex; 1378cf35eb4Sdan 1388cf35eb4Sdan /* List of quotaGroup objects. 1398cf35eb4Sdan */ 1408cf35eb4Sdan quotaGroup *pGroup; 1418cf35eb4Sdan 1428cf35eb4Sdan } gQuota; 1438cf35eb4Sdan 1448cf35eb4Sdan /************************* Utility Routines *********************************/ 1458cf35eb4Sdan /* 1468cf35eb4Sdan ** Acquire and release the mutex used to serialize access to the 1478cf35eb4Sdan ** list of quotaGroups. 1488cf35eb4Sdan */ 1498cf35eb4Sdan static void quotaEnter(void){ sqlite3_mutex_enter(gQuota.pMutex); } 1508cf35eb4Sdan static void quotaLeave(void){ sqlite3_mutex_leave(gQuota.pMutex); } 1518cf35eb4Sdan 1528cf35eb4Sdan 1538cf35eb4Sdan /* If the reference count and threshold for a quotaGroup are both 1548cf35eb4Sdan ** zero, then destroy the quotaGroup. 1558cf35eb4Sdan */ 1568cf35eb4Sdan static void quotaGroupDeref(quotaGroup *p){ 157745de2e4Sdrh if( p->pFile==0 && p->iLimit==0 ){ 1588cf35eb4Sdan if( p->pNext ) p->pNext->ppPrev = p->ppPrev; 1598cf35eb4Sdan if( p->ppPrev ) *p->ppPrev = p->pNext; 1608cf35eb4Sdan sqlite3_free(p); 1618cf35eb4Sdan } 1628cf35eb4Sdan } 1638cf35eb4Sdan 1648cf35eb4Sdan /* 1658cf35eb4Sdan ** Return TRUE if string z matches glob pattern zGlob. 1668cf35eb4Sdan ** 1678cf35eb4Sdan ** Globbing rules: 1688cf35eb4Sdan ** 1698cf35eb4Sdan ** '*' Matches any sequence of zero or more characters. 1708cf35eb4Sdan ** 1718cf35eb4Sdan ** '?' Matches exactly one character. 1728cf35eb4Sdan ** 1738cf35eb4Sdan ** [...] Matches one character from the enclosed list of 1748cf35eb4Sdan ** characters. 1758cf35eb4Sdan ** 1768cf35eb4Sdan ** [^...] Matches one character not in the enclosed list. 1778cf35eb4Sdan ** 1788cf35eb4Sdan */ 1798cf35eb4Sdan static int strglob(const char *zGlob, const char *z){ 1808cf35eb4Sdan int c, c2; 1818cf35eb4Sdan int invert; 1828cf35eb4Sdan int seen; 1838cf35eb4Sdan 1848cf35eb4Sdan while( (c = (*(zGlob++)))!=0 ){ 1858cf35eb4Sdan if( c=='*' ){ 1868cf35eb4Sdan while( (c=(*(zGlob++))) == '*' || c=='?' ){ 1878cf35eb4Sdan if( c=='?' && (*(z++))==0 ) return 0; 1888cf35eb4Sdan } 1898cf35eb4Sdan if( c==0 ){ 1908cf35eb4Sdan return 1; 1918cf35eb4Sdan }else if( c=='[' ){ 1928cf35eb4Sdan while( *z && strglob(zGlob-1,z)==0 ){ 1938cf35eb4Sdan z++; 1948cf35eb4Sdan } 1958cf35eb4Sdan return (*z)!=0; 1968cf35eb4Sdan } 1978cf35eb4Sdan while( (c2 = (*(z++)))!=0 ){ 1988cf35eb4Sdan while( c2!=c ){ 1998cf35eb4Sdan c2 = *(z++); 2008cf35eb4Sdan if( c2==0 ) return 0; 2018cf35eb4Sdan } 2028cf35eb4Sdan if( strglob(zGlob,z) ) return 1; 2038cf35eb4Sdan } 2048cf35eb4Sdan return 0; 2058cf35eb4Sdan }else if( c=='?' ){ 2068cf35eb4Sdan if( (*(z++))==0 ) return 0; 2078cf35eb4Sdan }else if( c=='[' ){ 2088cf35eb4Sdan int prior_c = 0; 2098cf35eb4Sdan seen = 0; 2108cf35eb4Sdan invert = 0; 2118cf35eb4Sdan c = *(z++); 2128cf35eb4Sdan if( c==0 ) return 0; 2138cf35eb4Sdan c2 = *(zGlob++); 2148cf35eb4Sdan if( c2=='^' ){ 2158cf35eb4Sdan invert = 1; 2168cf35eb4Sdan c2 = *(zGlob++); 2178cf35eb4Sdan } 2188cf35eb4Sdan if( c2==']' ){ 2198cf35eb4Sdan if( c==']' ) seen = 1; 2208cf35eb4Sdan c2 = *(zGlob++); 2218cf35eb4Sdan } 2228cf35eb4Sdan while( c2 && c2!=']' ){ 2238cf35eb4Sdan if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){ 2248cf35eb4Sdan c2 = *(zGlob++); 2258cf35eb4Sdan if( c>=prior_c && c<=c2 ) seen = 1; 2268cf35eb4Sdan prior_c = 0; 2278cf35eb4Sdan }else{ 2288cf35eb4Sdan if( c==c2 ){ 2298cf35eb4Sdan seen = 1; 2308cf35eb4Sdan } 2318cf35eb4Sdan prior_c = c2; 2328cf35eb4Sdan } 2338cf35eb4Sdan c2 = *(zGlob++); 2348cf35eb4Sdan } 2358cf35eb4Sdan if( c2==0 || (seen ^ invert)==0 ) return 0; 2368cf35eb4Sdan }else{ 2378cf35eb4Sdan if( c!=(*(z++)) ) return 0; 2388cf35eb4Sdan } 2398cf35eb4Sdan } 2408cf35eb4Sdan return *z==0; 2418cf35eb4Sdan } 2428cf35eb4Sdan 2438cf35eb4Sdan 2448cf35eb4Sdan /* Find a quotaGroup given the filename. 2458cf35eb4Sdan ** 246745de2e4Sdrh ** Return a pointer to the quotaOpen object. Return NULL if not found. 2478cf35eb4Sdan */ 2488cf35eb4Sdan static quotaGroup *quotaGroupFind(const char *zFilename){ 2498cf35eb4Sdan quotaGroup *p; 2508cf35eb4Sdan for(p=gQuota.pGroup; p && strglob(p->zPattern, zFilename)==0; p=p->pNext){} 2518cf35eb4Sdan return p; 2528cf35eb4Sdan } 2538cf35eb4Sdan 254745de2e4Sdrh /* Translate an sqlite3_file* that is really a quotaOpen* into 255*549dc1baSdrh ** the sqlite3_file* for the underlying original VFS. 2568cf35eb4Sdan */ 257745de2e4Sdrh static sqlite3_file *quotaSubOpen(sqlite3_file *pOpen){ 258745de2e4Sdrh quotaOpen *p = (quotaOpen*)pOpen; 2598cf35eb4Sdan return (sqlite3_file*)&p[1]; 2608cf35eb4Sdan } 2618cf35eb4Sdan 2628cf35eb4Sdan /************************* VFS Method Wrappers *****************************/ 2638cf35eb4Sdan /* 2648cf35eb4Sdan ** This is the xOpen method used for the "quota" VFS. 2658cf35eb4Sdan ** 2668cf35eb4Sdan ** Most of the work is done by the underlying original VFS. This method 267*549dc1baSdrh ** simply links the new file into the appropriate quota group if it is a 268*549dc1baSdrh ** file that needs to be tracked. 2698cf35eb4Sdan */ 270745de2e4Sdrh static int quotaxOpen( 271*549dc1baSdrh sqlite3_vfs *pVfs, /* The quota VFS */ 272*549dc1baSdrh const char *zName, /* Name of file to be opened */ 273*549dc1baSdrh sqlite3_file *pOpen, /* Fill in this file descriptor */ 274*549dc1baSdrh int flags, /* Flags to control the opening */ 275*549dc1baSdrh int *pOutFlags /* Flags showing results of opening */ 2768cf35eb4Sdan ){ 277*549dc1baSdrh int rc; /* Result code */ 278*549dc1baSdrh quotaOpen *pQuotaOpen; /* The new quota file descriptor */ 279*549dc1baSdrh quotaFile *pFile; /* Corresponding quotaFile obj */ 280*549dc1baSdrh quotaGroup *pGroup; /* The group file belongs to */ 281*549dc1baSdrh sqlite3_file *pSubOpen; /* Real file descriptor */ 282*549dc1baSdrh sqlite3_vfs *pOrigVfs = gQuota.pOrigVfs; /* Real VFS */ 2838cf35eb4Sdan 2848cf35eb4Sdan /* If the file is not a main database file or a WAL, then use the 2858cf35eb4Sdan ** normal xOpen method. 2868cf35eb4Sdan */ 2878cf35eb4Sdan if( (flags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_WAL))==0 ){ 288745de2e4Sdrh return pOrigVfs->xOpen(pOrigVfs, zName, pOpen, flags, pOutFlags); 2898cf35eb4Sdan } 2908cf35eb4Sdan 2918cf35eb4Sdan /* If the name of the file does not match any quota group, then 2928cf35eb4Sdan ** use the normal xOpen method. 2938cf35eb4Sdan */ 2948cf35eb4Sdan quotaEnter(); 2958cf35eb4Sdan pGroup = quotaGroupFind(zName); 2968cf35eb4Sdan if( pGroup==0 ){ 297745de2e4Sdrh rc = pOrigVfs->xOpen(pOrigVfs, zName, pOpen, flags, pOutFlags); 2988cf35eb4Sdan }else{ 2998cf35eb4Sdan /* If we get to this point, it means the file needs to be quota tracked. 3008cf35eb4Sdan */ 301745de2e4Sdrh pQuotaOpen = (quotaOpen*)pOpen; 302745de2e4Sdrh pSubOpen = quotaSubOpen(pOpen); 303745de2e4Sdrh rc = pOrigVfs->xOpen(pOrigVfs, zName, pSubOpen, flags, pOutFlags); 3048cf35eb4Sdan if( rc==SQLITE_OK ){ 305745de2e4Sdrh for(pFile=pGroup->pFile; pFile && strcmp(pFile->zFilename, zName); 306745de2e4Sdrh pFile=pFile->pNext){} 307745de2e4Sdrh if( pFile==0 ){ 308745de2e4Sdrh int nName = strlen(zName); 309745de2e4Sdrh pFile = sqlite3_malloc( sizeof(*pFile) + nName + 1 ); 310745de2e4Sdrh if( pFile==0 ){ 311745de2e4Sdrh quotaLeave(); 312745de2e4Sdrh pSubOpen->pMethods->xClose(pSubOpen); 313745de2e4Sdrh return SQLITE_NOMEM; 314745de2e4Sdrh } 315745de2e4Sdrh memset(pFile, 0, sizeof(*pFile)); 316745de2e4Sdrh pFile->zFilename = (char*)&pFile[1]; 317745de2e4Sdrh memcpy(pFile->zFilename, zName, nName+1); 318745de2e4Sdrh pFile->pNext = pGroup->pFile; 319745de2e4Sdrh if( pGroup->pFile ) pGroup->pFile->ppPrev = &pFile->pNext; 320745de2e4Sdrh pFile->ppPrev = &pGroup->pFile; 321745de2e4Sdrh pGroup->pFile = pFile; 322745de2e4Sdrh pFile->pGroup = pGroup; 323745de2e4Sdrh } 324745de2e4Sdrh pFile->nRef++; 325745de2e4Sdrh pQuotaOpen->pFile = pFile; 326745de2e4Sdrh if( pSubOpen->pMethods->iVersion==1 ){ 327745de2e4Sdrh pQuotaOpen->base.pMethods = &gQuota.sIoMethodsV1; 3288cf35eb4Sdan }else{ 329745de2e4Sdrh pQuotaOpen->base.pMethods = &gQuota.sIoMethodsV2; 3308cf35eb4Sdan } 3318cf35eb4Sdan } 3328cf35eb4Sdan } 3338cf35eb4Sdan quotaLeave(); 3348cf35eb4Sdan return rc; 3358cf35eb4Sdan } 3368cf35eb4Sdan 3378cf35eb4Sdan /************************ I/O Method Wrappers *******************************/ 3388cf35eb4Sdan 3398cf35eb4Sdan /* xClose requests get passed through to the original VFS. But we 340745de2e4Sdrh ** also have to unlink the quotaOpen from the quotaGroup. 3418cf35eb4Sdan */ 342745de2e4Sdrh static int quotaClose(sqlite3_file *pOpen){ 343745de2e4Sdrh quotaOpen *p = (quotaOpen*)pOpen; 344745de2e4Sdrh quotaFile *pFile = p->pFile; 345745de2e4Sdrh sqlite3_file *pSubOpen = quotaSubOpen(pOpen); 3468cf35eb4Sdan int rc; 347745de2e4Sdrh rc = pSubOpen->pMethods->xClose(pSubOpen); 3488cf35eb4Sdan quotaEnter(); 349745de2e4Sdrh pFile->nRef--; 350745de2e4Sdrh if( pFile->nRef==0 ){ 351745de2e4Sdrh quotaGroup *pGroup = pFile->pGroup; 352745de2e4Sdrh pGroup->iSize -= pFile->iSize; 353745de2e4Sdrh if( pFile->pNext ) pFile->pNext->ppPrev = pFile->ppPrev; 354745de2e4Sdrh *pFile->ppPrev = pFile->pNext; 3558cf35eb4Sdan quotaGroupDeref(pGroup); 356745de2e4Sdrh sqlite3_free(pFile); 357745de2e4Sdrh } 3588cf35eb4Sdan quotaLeave(); 3598cf35eb4Sdan return rc; 3608cf35eb4Sdan } 3618cf35eb4Sdan 3628cf35eb4Sdan /* Pass xRead requests directory thru to the original VFS without 3638cf35eb4Sdan ** further processing. 3648cf35eb4Sdan */ 3658cf35eb4Sdan static int quotaRead( 366745de2e4Sdrh sqlite3_file *pOpen, 3678cf35eb4Sdan void *pBuf, 3688cf35eb4Sdan int iAmt, 3698cf35eb4Sdan sqlite3_int64 iOfst 3708cf35eb4Sdan ){ 371745de2e4Sdrh sqlite3_file *pSubOpen = quotaSubOpen(pOpen); 372745de2e4Sdrh return pSubOpen->pMethods->xRead(pSubOpen, pBuf, iAmt, iOfst); 3738cf35eb4Sdan } 3748cf35eb4Sdan 3758cf35eb4Sdan /* Check xWrite requests to see if they expand the file. If they do, 3768cf35eb4Sdan ** the perform a quota check before passing them through to the 3778cf35eb4Sdan ** original VFS. 3788cf35eb4Sdan */ 3798cf35eb4Sdan static int quotaWrite( 380745de2e4Sdrh sqlite3_file *pOpen, 3818cf35eb4Sdan const void *pBuf, 3828cf35eb4Sdan int iAmt, 3838cf35eb4Sdan sqlite3_int64 iOfst 3848cf35eb4Sdan ){ 385745de2e4Sdrh quotaOpen *p = (quotaOpen*)pOpen; 386745de2e4Sdrh sqlite3_file *pSubOpen = quotaSubOpen(pOpen); 3878cf35eb4Sdan sqlite3_int64 iEnd = iOfst+iAmt; 3888cf35eb4Sdan quotaGroup *pGroup; 389745de2e4Sdrh quotaFile *pFile = p->pFile; 3908cf35eb4Sdan sqlite3_int64 szNew; 3918cf35eb4Sdan 392745de2e4Sdrh if( pFile->iSize<iEnd ){ 393745de2e4Sdrh pGroup = pFile->pGroup; 3948cf35eb4Sdan quotaEnter(); 395745de2e4Sdrh szNew = pGroup->iSize - pFile->iSize + iEnd; 3968cf35eb4Sdan if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){ 3978cf35eb4Sdan if( pGroup->xCallback ){ 398745de2e4Sdrh pGroup->xCallback(pFile->zFilename, &pGroup->iLimit, szNew, 3998cf35eb4Sdan pGroup->pArg); 4008cf35eb4Sdan } 4018cf35eb4Sdan if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){ 4028cf35eb4Sdan quotaLeave(); 4038cf35eb4Sdan return SQLITE_FULL; 4048cf35eb4Sdan } 4058cf35eb4Sdan } 4068cf35eb4Sdan pGroup->iSize = szNew; 407745de2e4Sdrh pFile->iSize = iEnd; 4088cf35eb4Sdan quotaLeave(); 4098cf35eb4Sdan } 410745de2e4Sdrh return pSubOpen->pMethods->xWrite(pSubOpen, pBuf, iAmt, iOfst); 4118cf35eb4Sdan } 4128cf35eb4Sdan 4138cf35eb4Sdan /* Pass xTruncate requests thru to the original VFS. If the 4148cf35eb4Sdan ** success, update the file size. 4158cf35eb4Sdan */ 416745de2e4Sdrh static int quotaTruncate(sqlite3_file *pOpen, sqlite3_int64 size){ 417745de2e4Sdrh quotaOpen *p = (quotaOpen*)pOpen; 418745de2e4Sdrh sqlite3_file *pSubOpen = quotaSubOpen(pOpen); 419745de2e4Sdrh int rc = pSubOpen->pMethods->xTruncate(pSubOpen, size); 420745de2e4Sdrh quotaFile *pFile = p->pFile; 421745de2e4Sdrh quotaGroup *pGroup; 4228cf35eb4Sdan if( rc==SQLITE_OK ){ 4238cf35eb4Sdan quotaEnter(); 424745de2e4Sdrh pGroup = pFile->pGroup; 425745de2e4Sdrh pGroup->iSize -= pFile->iSize; 426745de2e4Sdrh pFile->iSize = size; 4278cf35eb4Sdan pGroup->iSize += size; 4288cf35eb4Sdan quotaLeave(); 4298cf35eb4Sdan } 4308cf35eb4Sdan return rc; 4318cf35eb4Sdan } 4328cf35eb4Sdan 4338cf35eb4Sdan /* Pass xSync requests through to the original VFS without change 4348cf35eb4Sdan */ 435745de2e4Sdrh static int quotaSync(sqlite3_file *pOpen, int flags){ 436745de2e4Sdrh sqlite3_file *pSubOpen = quotaSubOpen(pOpen); 437745de2e4Sdrh return pSubOpen->pMethods->xSync(pSubOpen, flags); 4388cf35eb4Sdan } 4398cf35eb4Sdan 4408cf35eb4Sdan /* Pass xFileSize requests through to the original VFS but then 4418cf35eb4Sdan ** update the quotaGroup with the new size before returning. 4428cf35eb4Sdan */ 443745de2e4Sdrh static int quotaFileSize(sqlite3_file *pOpen, sqlite3_int64 *pSize){ 444745de2e4Sdrh quotaOpen *p = (quotaOpen*)pOpen; 445745de2e4Sdrh sqlite3_file *pSubOpen = quotaSubOpen(pOpen); 446745de2e4Sdrh quotaFile *pFile = p->pFile; 4478cf35eb4Sdan quotaGroup *pGroup; 4488cf35eb4Sdan sqlite3_int64 sz; 4498cf35eb4Sdan int rc; 4508cf35eb4Sdan 451745de2e4Sdrh rc = pSubOpen->pMethods->xFileSize(pSubOpen, &sz); 4528cf35eb4Sdan if( rc==SQLITE_OK ){ 4538cf35eb4Sdan quotaEnter(); 454745de2e4Sdrh pGroup = pFile->pGroup; 455745de2e4Sdrh pGroup->iSize -= pFile->iSize; 456745de2e4Sdrh pFile->iSize = sz; 4578cf35eb4Sdan pGroup->iSize += sz; 4588cf35eb4Sdan quotaLeave(); 4598cf35eb4Sdan *pSize = sz; 4608cf35eb4Sdan } 4618cf35eb4Sdan return rc; 4628cf35eb4Sdan } 4638cf35eb4Sdan 4648cf35eb4Sdan /* Pass xLock requests through to the original VFS unchanged. 4658cf35eb4Sdan */ 466745de2e4Sdrh static int quotaLock(sqlite3_file *pOpen, int lock){ 467745de2e4Sdrh sqlite3_file *pSubOpen = quotaSubOpen(pOpen); 468745de2e4Sdrh return pSubOpen->pMethods->xLock(pSubOpen, lock); 4698cf35eb4Sdan } 4708cf35eb4Sdan 4718cf35eb4Sdan /* Pass xUnlock requests through to the original VFS unchanged. 4728cf35eb4Sdan */ 473745de2e4Sdrh static int quotaUnlock(sqlite3_file *pOpen, int lock){ 474745de2e4Sdrh sqlite3_file *pSubOpen = quotaSubOpen(pOpen); 475745de2e4Sdrh return pSubOpen->pMethods->xUnlock(pSubOpen, lock); 4768cf35eb4Sdan } 4778cf35eb4Sdan 4788cf35eb4Sdan /* Pass xCheckReservedLock requests through to the original VFS unchanged. 4798cf35eb4Sdan */ 480745de2e4Sdrh static int quotaCheckReservedLock(sqlite3_file *pOpen, int *pResOut){ 481745de2e4Sdrh sqlite3_file *pSubOpen = quotaSubOpen(pOpen); 482745de2e4Sdrh return pSubOpen->pMethods->xCheckReservedLock(pSubOpen, pResOut); 4838cf35eb4Sdan } 4848cf35eb4Sdan 4858cf35eb4Sdan /* Pass xFileControl requests through to the original VFS unchanged. 4868cf35eb4Sdan */ 487745de2e4Sdrh static int quotaOpenControl(sqlite3_file *pOpen, int op, void *pArg){ 488745de2e4Sdrh sqlite3_file *pSubOpen = quotaSubOpen(pOpen); 489745de2e4Sdrh return pSubOpen->pMethods->xFileControl(pSubOpen, op, pArg); 4908cf35eb4Sdan } 4918cf35eb4Sdan 4928cf35eb4Sdan /* Pass xSectorSize requests through to the original VFS unchanged. 4938cf35eb4Sdan */ 494745de2e4Sdrh static int quotaSectorSize(sqlite3_file *pOpen){ 495745de2e4Sdrh sqlite3_file *pSubOpen = quotaSubOpen(pOpen); 496745de2e4Sdrh return pSubOpen->pMethods->xSectorSize(pSubOpen); 4978cf35eb4Sdan } 4988cf35eb4Sdan 4998cf35eb4Sdan /* Pass xDeviceCharacteristics requests through to the original VFS unchanged. 5008cf35eb4Sdan */ 501745de2e4Sdrh static int quotaDeviceCharacteristics(sqlite3_file *pOpen){ 502745de2e4Sdrh sqlite3_file *pSubOpen = quotaSubOpen(pOpen); 503745de2e4Sdrh return pSubOpen->pMethods->xDeviceCharacteristics(pSubOpen); 5048cf35eb4Sdan } 5058cf35eb4Sdan 5068cf35eb4Sdan /* Pass xShmMap requests through to the original VFS unchanged. 5078cf35eb4Sdan */ 5088cf35eb4Sdan static int quotaShmMap( 509745de2e4Sdrh sqlite3_file *pOpen, /* Handle open on database file */ 5108cf35eb4Sdan int iRegion, /* Region to retrieve */ 5118cf35eb4Sdan int szRegion, /* Size of regions */ 5128cf35eb4Sdan int bExtend, /* True to extend file if necessary */ 5138cf35eb4Sdan void volatile **pp /* OUT: Mapped memory */ 5148cf35eb4Sdan ){ 515745de2e4Sdrh sqlite3_file *pSubOpen = quotaSubOpen(pOpen); 516745de2e4Sdrh return pSubOpen->pMethods->xShmMap(pSubOpen, iRegion, szRegion, bExtend, pp); 5178cf35eb4Sdan } 5188cf35eb4Sdan 5198cf35eb4Sdan /* Pass xShmLock requests through to the original VFS unchanged. 5208cf35eb4Sdan */ 5218cf35eb4Sdan static int quotaShmLock( 522745de2e4Sdrh sqlite3_file *pOpen, /* Database file holding the shared memory */ 5238cf35eb4Sdan int ofst, /* First lock to acquire or release */ 5248cf35eb4Sdan int n, /* Number of locks to acquire or release */ 5258cf35eb4Sdan int flags /* What to do with the lock */ 5268cf35eb4Sdan ){ 527745de2e4Sdrh sqlite3_file *pSubOpen = quotaSubOpen(pOpen); 528745de2e4Sdrh return pSubOpen->pMethods->xShmLock(pSubOpen, ofst, n, flags); 5298cf35eb4Sdan } 5308cf35eb4Sdan 5318cf35eb4Sdan /* Pass xShmBarrier requests through to the original VFS unchanged. 5328cf35eb4Sdan */ 533745de2e4Sdrh static void quotaShmBarrier(sqlite3_file *pOpen){ 534745de2e4Sdrh sqlite3_file *pSubOpen = quotaSubOpen(pOpen); 535745de2e4Sdrh pSubOpen->pMethods->xShmBarrier(pSubOpen); 5368cf35eb4Sdan } 5378cf35eb4Sdan 5388cf35eb4Sdan /* Pass xShmUnmap requests through to the original VFS unchanged. 5398cf35eb4Sdan */ 540745de2e4Sdrh static int quotaShmUnmap(sqlite3_file *pOpen, int deleteFlag){ 541745de2e4Sdrh sqlite3_file *pSubOpen = quotaSubOpen(pOpen); 542745de2e4Sdrh return pSubOpen->pMethods->xShmUnmap(pSubOpen, deleteFlag); 5438cf35eb4Sdan } 5448cf35eb4Sdan 5458cf35eb4Sdan /************************** Public Interfaces *****************************/ 5468cf35eb4Sdan /* 5478cf35eb4Sdan ** Initialize the quota VFS shim. Use the VFS named zOrigVfsName 5488cf35eb4Sdan ** as the VFS that does the actual work. Use the default if 5498cf35eb4Sdan ** zOrigVfsName==NULL. 5508cf35eb4Sdan ** 5518cf35eb4Sdan ** The quota VFS shim is named "quota". It will become the default 5528cf35eb4Sdan ** VFS if makeDefault is non-zero. 5538cf35eb4Sdan ** 5548cf35eb4Sdan ** THIS ROUTINE IS NOT THREADSAFE. Call this routine exactly once 5558cf35eb4Sdan ** during start-up. 5568cf35eb4Sdan */ 5578cf35eb4Sdan int sqlite3_quota_initialize(const char *zOrigVfsName, int makeDefault){ 5588cf35eb4Sdan sqlite3_vfs *pOrigVfs; 5598cf35eb4Sdan if( gQuota.isInitialized ) return SQLITE_MISUSE; 5608cf35eb4Sdan pOrigVfs = sqlite3_vfs_find(zOrigVfsName); 5618cf35eb4Sdan if( pOrigVfs==0 ) return SQLITE_ERROR; 5628cf35eb4Sdan assert( pOrigVfs!=&gQuota.sThisVfs ); 5638cf35eb4Sdan gQuota.pMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); 5648cf35eb4Sdan if( !gQuota.pMutex ){ 5658cf35eb4Sdan return SQLITE_NOMEM; 5668cf35eb4Sdan } 5678cf35eb4Sdan gQuota.isInitialized = 1; 5688cf35eb4Sdan gQuota.pOrigVfs = pOrigVfs; 5698cf35eb4Sdan gQuota.sThisVfs = *pOrigVfs; 570745de2e4Sdrh gQuota.sThisVfs.xOpen = quotaxOpen; 571745de2e4Sdrh gQuota.sThisVfs.szOsFile += sizeof(quotaOpen); 5728cf35eb4Sdan gQuota.sThisVfs.zName = "quota"; 5738cf35eb4Sdan gQuota.sIoMethodsV1.iVersion = 1; 5748cf35eb4Sdan gQuota.sIoMethodsV1.xClose = quotaClose; 5758cf35eb4Sdan gQuota.sIoMethodsV1.xRead = quotaRead; 5768cf35eb4Sdan gQuota.sIoMethodsV1.xWrite = quotaWrite; 5778cf35eb4Sdan gQuota.sIoMethodsV1.xTruncate = quotaTruncate; 5788cf35eb4Sdan gQuota.sIoMethodsV1.xSync = quotaSync; 5798cf35eb4Sdan gQuota.sIoMethodsV1.xFileSize = quotaFileSize; 5808cf35eb4Sdan gQuota.sIoMethodsV1.xLock = quotaLock; 5818cf35eb4Sdan gQuota.sIoMethodsV1.xUnlock = quotaUnlock; 5828cf35eb4Sdan gQuota.sIoMethodsV1.xCheckReservedLock = quotaCheckReservedLock; 583745de2e4Sdrh gQuota.sIoMethodsV1.xFileControl = quotaOpenControl; 5848cf35eb4Sdan gQuota.sIoMethodsV1.xSectorSize = quotaSectorSize; 5858cf35eb4Sdan gQuota.sIoMethodsV1.xDeviceCharacteristics = quotaDeviceCharacteristics; 5868cf35eb4Sdan gQuota.sIoMethodsV2 = gQuota.sIoMethodsV1; 5878cf35eb4Sdan gQuota.sIoMethodsV2.iVersion = 2; 5888cf35eb4Sdan gQuota.sIoMethodsV2.xShmMap = quotaShmMap; 5898cf35eb4Sdan gQuota.sIoMethodsV2.xShmLock = quotaShmLock; 5908cf35eb4Sdan gQuota.sIoMethodsV2.xShmBarrier = quotaShmBarrier; 5918cf35eb4Sdan gQuota.sIoMethodsV2.xShmUnmap = quotaShmUnmap; 5928cf35eb4Sdan sqlite3_vfs_register(&gQuota.sThisVfs, makeDefault); 5938cf35eb4Sdan return SQLITE_OK; 5948cf35eb4Sdan } 5958cf35eb4Sdan 5968cf35eb4Sdan /* 5978cf35eb4Sdan ** Shutdown the quota system. 5988cf35eb4Sdan ** 5998cf35eb4Sdan ** All SQLite database connections must be closed before calling this 6008cf35eb4Sdan ** routine. 6018cf35eb4Sdan ** 6028cf35eb4Sdan ** THIS ROUTINE IS NOT THREADSAFE. Call this routine exactly one while 6038cf35eb4Sdan ** shutting down in order to free all remaining quota groups. 6048cf35eb4Sdan */ 6058cf35eb4Sdan int sqlite3_quota_shutdown(void){ 6068cf35eb4Sdan quotaGroup *p; 6078cf35eb4Sdan if( gQuota.isInitialized==0 ) return SQLITE_MISUSE; 6088cf35eb4Sdan for(p=gQuota.pGroup; p; p=p->pNext){ 609745de2e4Sdrh if( p->pFile ) return SQLITE_MISUSE; 6108cf35eb4Sdan } 6118cf35eb4Sdan while( gQuota.pGroup ){ 6128cf35eb4Sdan quotaGroup *p = gQuota.pGroup; 6138cf35eb4Sdan gQuota.pGroup = p->pNext; 6148cf35eb4Sdan sqlite3_free(p); 6158cf35eb4Sdan } 6168cf35eb4Sdan gQuota.isInitialized = 0; 6178cf35eb4Sdan sqlite3_mutex_free(gQuota.pMutex); 6188cf35eb4Sdan sqlite3_vfs_unregister(&gQuota.sThisVfs); 6198cf35eb4Sdan memset(&gQuota, 0, sizeof(gQuota)); 6208cf35eb4Sdan return SQLITE_OK; 6218cf35eb4Sdan } 6228cf35eb4Sdan 6238cf35eb4Sdan /* 6248cf35eb4Sdan ** Create or destroy a quota group. 6258cf35eb4Sdan ** 6268cf35eb4Sdan ** The quota group is defined by the zPattern. When calling this routine 6278cf35eb4Sdan ** with a zPattern for a quota group that already exists, this routine 6288cf35eb4Sdan ** merely updates the iLimit, xCallback, and pArg values for that quota 6298cf35eb4Sdan ** group. If zPattern is new, then a new quota group is created. 6308cf35eb4Sdan ** 6318cf35eb4Sdan ** If the iLimit for a quota group is set to zero, then the quota group 6328cf35eb4Sdan ** is disabled and will be deleted when the last database connection using 6338cf35eb4Sdan ** the quota group is closed. 6348cf35eb4Sdan ** 6358cf35eb4Sdan ** Calling this routine on a zPattern that does not exist and with a 6368cf35eb4Sdan ** zero iLimit is a no-op. 6378cf35eb4Sdan ** 6388cf35eb4Sdan ** A quota group must exist with a non-zero iLimit prior to opening 6398cf35eb4Sdan ** database connections if those connections are to participate in the 6408cf35eb4Sdan ** quota group. Creating a quota group does not affect database connections 6418cf35eb4Sdan ** that are already open. 6428cf35eb4Sdan */ 6438cf35eb4Sdan int sqlite3_quota_set( 6448cf35eb4Sdan const char *zPattern, /* The filename pattern */ 6458cf35eb4Sdan sqlite3_int64 iLimit, /* New quota to set for this quota group */ 6468cf35eb4Sdan void (*xCallback)( /* Callback invoked when going over quota */ 6478cf35eb4Sdan const char *zFilename, /* Name of file whose size increases */ 6488cf35eb4Sdan sqlite3_int64 *piLimit, /* IN/OUT: The current limit */ 6498cf35eb4Sdan sqlite3_int64 iSize, /* Total size of all files in the group */ 6508cf35eb4Sdan void *pArg /* Client data */ 6518cf35eb4Sdan ), 6528cf35eb4Sdan void *pArg /* client data passed thru to callback */ 6538cf35eb4Sdan ){ 6548cf35eb4Sdan quotaGroup *pGroup; 6558cf35eb4Sdan quotaEnter(); 6568cf35eb4Sdan pGroup = gQuota.pGroup; 6578cf35eb4Sdan while( pGroup && strcmp(pGroup->zPattern, zPattern)!=0 ){ 6588cf35eb4Sdan pGroup = pGroup->pNext; 6598cf35eb4Sdan } 6608cf35eb4Sdan if( pGroup==0 && iLimit>0 ){ 6618cf35eb4Sdan int nPattern = strlen(zPattern); 6628cf35eb4Sdan pGroup = sqlite3_malloc( sizeof(*pGroup) + nPattern + 1 ); 6638cf35eb4Sdan if( pGroup==0 ){ 6648cf35eb4Sdan quotaLeave(); 6658cf35eb4Sdan return SQLITE_NOMEM; 6668cf35eb4Sdan } 6678cf35eb4Sdan memset(pGroup, 0, sizeof(*pGroup)); 6688cf35eb4Sdan pGroup->zPattern = (char*)&pGroup[1]; 6698cf35eb4Sdan memcpy((char *)pGroup->zPattern, zPattern, nPattern+1); 6708cf35eb4Sdan if( gQuota.pGroup ) gQuota.pGroup->ppPrev = &pGroup->pNext; 6718cf35eb4Sdan pGroup->pNext = gQuota.pGroup; 6728cf35eb4Sdan pGroup->ppPrev = &gQuota.pGroup; 6738cf35eb4Sdan gQuota.pGroup = pGroup; 6748cf35eb4Sdan } 6758cf35eb4Sdan pGroup->iLimit = iLimit; 6768cf35eb4Sdan pGroup->xCallback = xCallback; 6778cf35eb4Sdan pGroup->pArg = pArg; 6788cf35eb4Sdan quotaGroupDeref(pGroup); 6798cf35eb4Sdan quotaLeave(); 6808cf35eb4Sdan return SQLITE_OK; 6818cf35eb4Sdan } 6828cf35eb4Sdan 6838cf35eb4Sdan 6848cf35eb4Sdan /***************************** Test Code ***********************************/ 6858cf35eb4Sdan #ifdef SQLITE_TEST 6868cf35eb4Sdan #include <tcl.h> 6878cf35eb4Sdan 6888cf35eb4Sdan typedef struct TclQuotaCallback TclQuotaCallback; 6898cf35eb4Sdan struct TclQuotaCallback { 6908cf35eb4Sdan Tcl_Interp *interp; 6918cf35eb4Sdan Tcl_Obj *pScript; 6928cf35eb4Sdan TclQuotaCallback *pNext; 6938cf35eb4Sdan }; 6948cf35eb4Sdan static TclQuotaCallback *pQuotaCallbackList = 0; 6958cf35eb4Sdan 6968cf35eb4Sdan extern const char *sqlite3TestErrorName(int); 6978cf35eb4Sdan 6988cf35eb4Sdan static void tclQuotaCallback( 6998cf35eb4Sdan const char *zFilename, /* Name of file whose size increases */ 7008cf35eb4Sdan sqlite3_int64 *piLimit, /* IN/OUT: The current limit */ 7018cf35eb4Sdan sqlite3_int64 iSize, /* Total size of all files in the group */ 7028cf35eb4Sdan void *pArg /* Client data */ 7038cf35eb4Sdan ){ 7048cf35eb4Sdan TclQuotaCallback *p; /* Callback script object */ 7058cf35eb4Sdan Tcl_Obj *pEval; /* Script to evaluate */ 7068cf35eb4Sdan Tcl_Obj *pVarname; /* Name of variable to pass as 2nd arg */ 7078cf35eb4Sdan unsigned int rnd; /* Random part of pVarname */ 7088cf35eb4Sdan int rc; /* Tcl error code */ 7098cf35eb4Sdan 7108cf35eb4Sdan p = (TclQuotaCallback *)pArg; 7118cf35eb4Sdan 7128cf35eb4Sdan pVarname = Tcl_NewStringObj("::piLimit_", -1); 7138cf35eb4Sdan Tcl_IncrRefCount(pVarname); 7148cf35eb4Sdan sqlite3_randomness(sizeof(rnd), (void *)&rnd); 7158cf35eb4Sdan Tcl_AppendObjToObj(pVarname, Tcl_NewIntObj((int)(rnd&0x7FFFFFFF))); 7168cf35eb4Sdan Tcl_ObjSetVar2(p->interp, pVarname, 0, Tcl_NewWideIntObj(*piLimit), 0); 7178cf35eb4Sdan 7188cf35eb4Sdan pEval = Tcl_DuplicateObj(p->pScript); 7198cf35eb4Sdan Tcl_IncrRefCount(pEval); 7208cf35eb4Sdan Tcl_ListObjAppendElement(0, pEval, Tcl_NewStringObj(zFilename, -1)); 7218cf35eb4Sdan Tcl_ListObjAppendElement(0, pEval, pVarname); 7228cf35eb4Sdan Tcl_ListObjAppendElement(0, pEval, Tcl_NewWideIntObj(iSize)); 7238cf35eb4Sdan rc = Tcl_EvalObjEx(p->interp, pEval, TCL_EVAL_GLOBAL); 7248cf35eb4Sdan 7258cf35eb4Sdan if( rc==TCL_OK ){ 7268cf35eb4Sdan Tcl_Obj *pLimit = Tcl_ObjGetVar2(p->interp, pVarname, 0, 0); 7278cf35eb4Sdan rc = Tcl_GetWideIntFromObj(p->interp, pLimit, piLimit); 7288cf35eb4Sdan Tcl_UnsetVar(p->interp, Tcl_GetString(pVarname), 0); 7298cf35eb4Sdan } 7308cf35eb4Sdan 7318cf35eb4Sdan Tcl_DecrRefCount(pEval); 7328cf35eb4Sdan Tcl_DecrRefCount(pVarname); 7338cf35eb4Sdan if( rc!=TCL_OK ) Tcl_BackgroundError(p->interp); 7348cf35eb4Sdan } 7358cf35eb4Sdan 7368cf35eb4Sdan /* 7378cf35eb4Sdan ** tclcmd: sqlite3_quota_initialize NAME MAKEDEFAULT 7388cf35eb4Sdan */ 7398cf35eb4Sdan static int test_quota_initialize( 7408cf35eb4Sdan void * clientData, 7418cf35eb4Sdan Tcl_Interp *interp, 7428cf35eb4Sdan int objc, 7438cf35eb4Sdan Tcl_Obj *CONST objv[] 7448cf35eb4Sdan ){ 7458cf35eb4Sdan const char *zName; /* Name of new quota VFS */ 7468cf35eb4Sdan int makeDefault; /* True to make the new VFS the default */ 7478cf35eb4Sdan int rc; /* Value returned by quota_initialize() */ 7488cf35eb4Sdan 7498cf35eb4Sdan /* Process arguments */ 7508cf35eb4Sdan if( objc!=3 ){ 7518cf35eb4Sdan Tcl_WrongNumArgs(interp, 1, objv, "NAME MAKEDEFAULT"); 7528cf35eb4Sdan return TCL_ERROR; 7538cf35eb4Sdan } 7548cf35eb4Sdan zName = Tcl_GetString(objv[1]); 7558cf35eb4Sdan if( Tcl_GetBooleanFromObj(interp, objv[2], &makeDefault) ) return TCL_ERROR; 7568cf35eb4Sdan if( zName[0]=='\0' ) zName = 0; 7578cf35eb4Sdan 7588cf35eb4Sdan /* Call sqlite3_quota_initialize() */ 7598cf35eb4Sdan rc = sqlite3_quota_initialize(zName, makeDefault); 7608cf35eb4Sdan Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC); 7618cf35eb4Sdan 7628cf35eb4Sdan return TCL_OK; 7638cf35eb4Sdan } 7648cf35eb4Sdan 7658cf35eb4Sdan /* 7668cf35eb4Sdan ** tclcmd: sqlite3_quota_shutdown 7678cf35eb4Sdan */ 7688cf35eb4Sdan static int test_quota_shutdown( 7698cf35eb4Sdan void * clientData, 7708cf35eb4Sdan Tcl_Interp *interp, 7718cf35eb4Sdan int objc, 7728cf35eb4Sdan Tcl_Obj *CONST objv[] 7738cf35eb4Sdan ){ 7748cf35eb4Sdan int rc; /* Value returned by quota_shutdown() */ 7758cf35eb4Sdan 7768cf35eb4Sdan if( objc!=1 ){ 7778cf35eb4Sdan Tcl_WrongNumArgs(interp, 1, objv, ""); 7788cf35eb4Sdan return TCL_ERROR; 7798cf35eb4Sdan } 7808cf35eb4Sdan 7818cf35eb4Sdan /* Call sqlite3_quota_shutdown() */ 7828cf35eb4Sdan rc = sqlite3_quota_shutdown(); 7838cf35eb4Sdan Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC); 7848cf35eb4Sdan 7858cf35eb4Sdan /* If the quota system was successfully shut down, delete all the quota 7868cf35eb4Sdan ** callback script objects in the global linked list. 7878cf35eb4Sdan */ 7888cf35eb4Sdan if( rc==SQLITE_OK ){ 7898cf35eb4Sdan TclQuotaCallback *p, *pNext; 7908cf35eb4Sdan for(p=pQuotaCallbackList; p; p=pNext){ 7918cf35eb4Sdan pNext = p->pNext; 7928cf35eb4Sdan Tcl_DecrRefCount(p->pScript); 7938cf35eb4Sdan ckfree((char *)p); 7948cf35eb4Sdan } 7958cf35eb4Sdan pQuotaCallbackList = 0; 7968cf35eb4Sdan } 7978cf35eb4Sdan 7988cf35eb4Sdan return TCL_OK; 7998cf35eb4Sdan } 8008cf35eb4Sdan 8018cf35eb4Sdan /* 8028cf35eb4Sdan ** tclcmd: sqlite3_quota_set PATTERN LIMIT SCRIPT 8038cf35eb4Sdan */ 8048cf35eb4Sdan static int test_quota_set( 8058cf35eb4Sdan void * clientData, 8068cf35eb4Sdan Tcl_Interp *interp, 8078cf35eb4Sdan int objc, 8088cf35eb4Sdan Tcl_Obj *CONST objv[] 8098cf35eb4Sdan ){ 8108cf35eb4Sdan const char *zPattern; /* File pattern to configure */ 8118cf35eb4Sdan sqlite3_int64 iLimit; /* Initial quota in bytes */ 8128cf35eb4Sdan Tcl_Obj *pScript; /* Tcl script to invoke to increase quota */ 8138cf35eb4Sdan int rc; /* Value returned by quota_set() */ 8148cf35eb4Sdan TclQuotaCallback *p; /* Callback object */ 8158cf35eb4Sdan 8168cf35eb4Sdan /* Process arguments */ 8178cf35eb4Sdan if( objc!=4 ){ 8188cf35eb4Sdan Tcl_WrongNumArgs(interp, 1, objv, "PATTERN LIMIT SCRIPT"); 8198cf35eb4Sdan return TCL_ERROR; 8208cf35eb4Sdan } 8218cf35eb4Sdan zPattern = Tcl_GetString(objv[1]); 8228cf35eb4Sdan if( Tcl_GetWideIntFromObj(interp, objv[2], &iLimit) ) return TCL_ERROR; 8238cf35eb4Sdan pScript = objv[3]; 8248cf35eb4Sdan 8258cf35eb4Sdan /* Allocate a TclQuotaCallback object */ 8268cf35eb4Sdan p = (TclQuotaCallback *)ckalloc(sizeof(TclQuotaCallback)); 8278cf35eb4Sdan memset(p, 0, sizeof(TclQuotaCallback)); 8288cf35eb4Sdan 8298cf35eb4Sdan /* Invoke sqlite3_quota_set() */ 8308cf35eb4Sdan rc = sqlite3_quota_set(zPattern, iLimit, tclQuotaCallback, (void *)p); 8318cf35eb4Sdan if( rc!=SQLITE_OK ){ 8328cf35eb4Sdan ckfree((char *)p); 8338cf35eb4Sdan }else{ 8348cf35eb4Sdan Tcl_IncrRefCount(pScript); 8358cf35eb4Sdan p->interp = interp; 8368cf35eb4Sdan p->pScript = pScript; 8378cf35eb4Sdan p->pNext = pQuotaCallbackList; 8388cf35eb4Sdan pQuotaCallbackList = p; 8398cf35eb4Sdan } 8408cf35eb4Sdan 8418cf35eb4Sdan Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC); 8428cf35eb4Sdan return TCL_OK; 8438cf35eb4Sdan } 8448cf35eb4Sdan 8458cf35eb4Sdan /* 8468cf35eb4Sdan ** This routine registers the custom TCL commands defined in this 8478cf35eb4Sdan ** module. This should be the only procedure visible from outside 8488cf35eb4Sdan ** of this module. 8498cf35eb4Sdan */ 8508cf35eb4Sdan int Sqlitequota_Init(Tcl_Interp *interp){ 8518cf35eb4Sdan static struct { 8528cf35eb4Sdan char *zName; 8538cf35eb4Sdan Tcl_ObjCmdProc *xProc; 8548cf35eb4Sdan } aCmd[] = { 8558cf35eb4Sdan { "sqlite3_quota_initialize", test_quota_initialize }, 8568cf35eb4Sdan { "sqlite3_quota_shutdown", test_quota_shutdown }, 8578cf35eb4Sdan { "sqlite3_quota_set", test_quota_set }, 8588cf35eb4Sdan }; 8598cf35eb4Sdan int i; 8608cf35eb4Sdan 8618cf35eb4Sdan for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){ 8628cf35eb4Sdan Tcl_CreateObjCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0); 8638cf35eb4Sdan } 8648cf35eb4Sdan 8658cf35eb4Sdan return TCL_OK; 8668cf35eb4Sdan } 8678cf35eb4Sdan #endif 868