xref: /sqlite-3.40.0/src/test_quota.c (revision 745de2e4)
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 
36*745de2e4Sdrh /* Forward declaration of all object types */
37*745de2e4Sdrh typedef struct quotaGroup quotaGroup;
38*745de2e4Sdrh typedef struct quotaOpen quotaOpen;
39*745de2e4Sdrh typedef struct quotaFile quotaFile;
40*745de2e4Sdrh 
418cf35eb4Sdan /*
428cf35eb4Sdan ** This module contains a table of filename patterns that have size
438cf35eb4Sdan ** quotas.  The quota applies to the sum of the sizes of all open
448cf35eb4Sdan ** database files whose names match the GLOB pattern.
458cf35eb4Sdan **
468cf35eb4Sdan ** Each quota is an instance of the following object.  Quotas must
478cf35eb4Sdan ** be established (using sqlite3_quota_set()) prior to opening any
488cf35eb4Sdan ** of the database connections that access files governed by the
498cf35eb4Sdan ** quota.
508cf35eb4Sdan **
518cf35eb4Sdan ** Each entry in the quota table is an instance of the following object.
528cf35eb4Sdan */
538cf35eb4Sdan struct quotaGroup {
548cf35eb4Sdan   const char *zPattern;          /* Filename pattern to be quotaed */
558cf35eb4Sdan   sqlite3_int64 iLimit;          /* Upper bound on total file size */
568cf35eb4Sdan   sqlite3_int64 iSize;           /* Current size of all files */
578cf35eb4Sdan   void (*xCallback)(             /* Callback invoked when going over quota */
588cf35eb4Sdan      const char *zFilename,         /* Name of file whose size increases */
598cf35eb4Sdan      sqlite3_int64 *piLimit,        /* IN/OUT: The current limit */
608cf35eb4Sdan      sqlite3_int64 iSize,           /* Total size of all files in the group */
618cf35eb4Sdan      void *pArg                     /* Client data */
628cf35eb4Sdan   );
638cf35eb4Sdan   void *pArg;                    /* Third argument to the xCallback() */
648cf35eb4Sdan   quotaGroup *pNext, **ppPrev;   /* Doubly linked list of all quota objects */
65*745de2e4Sdrh   quotaFile *pFile;              /* Files within this group */
668cf35eb4Sdan };
678cf35eb4Sdan 
688cf35eb4Sdan /*
69*745de2e4Sdrh ** An instance of this structure represents a single file that is part
70*745de2e4Sdrh ** of a quota group.  A single file can be opened multiple times.  In
71*745de2e4Sdrh ** order keep multiple openings of the same file from causing the size
72*745de2e4Sdrh ** of the file counting against the quota multiple times, each file
73*745de2e4Sdrh ** has a unique instance of this object and multiple open connections
74*745de2e4Sdrh ** to the same file each point to a single instance of this object.
758cf35eb4Sdan */
768cf35eb4Sdan struct quotaFile {
77*745de2e4Sdrh   char *zFilename;                /* Name of this file */
788cf35eb4Sdan   quotaGroup *pGroup;             /* Upper bound on file size */
798cf35eb4Sdan   sqlite3_int64 iSize;            /* Current size of this file */
80*745de2e4Sdrh   int nRef;                       /* Number of times this file is open */
81*745de2e4Sdrh   quotaFile *pNext, **ppPrev;     /* Linked list of files in the same group */
82*745de2e4Sdrh };
83*745de2e4Sdrh 
84*745de2e4Sdrh /*
85*745de2e4Sdrh ** An instance of the following object represents each open connection
86*745de2e4Sdrh ** to a file that participates in quota tracking.  The sqlite3_file object
87*745de2e4Sdrh ** for the underlying VFS is appended to this structure.
88*745de2e4Sdrh */
89*745de2e4Sdrh struct quotaOpen {
90*745de2e4Sdrh   sqlite3_file base;              /* Base class - must be first */
91*745de2e4Sdrh   quotaFile *pFile;               /* The underlying file */
928cf35eb4Sdan   /* The underlying VFS sqlite3_file is appended to this object */
938cf35eb4Sdan };
948cf35eb4Sdan 
958cf35eb4Sdan /************************* Global Variables **********************************/
968cf35eb4Sdan /*
978cf35eb4Sdan ** All global variables used by this file are containing within the following
988cf35eb4Sdan ** gQuota structure.
998cf35eb4Sdan */
1008cf35eb4Sdan static struct {
1018cf35eb4Sdan   /* The pOrigVfs is a pointer to the real underlying VFS implementation.
1028cf35eb4Sdan   ** Most operations pass-through to the real VFS.  This value is read-only
1038cf35eb4Sdan   ** during operation.  It is only modified at start-time and thus does not
1048cf35eb4Sdan   ** require a mutex.
1058cf35eb4Sdan   */
1068cf35eb4Sdan   sqlite3_vfs *pOrigVfs;
1078cf35eb4Sdan 
1088cf35eb4Sdan   /* The sThisVfs is the VFS structure used by this shim.  It is initialized
1098cf35eb4Sdan   ** at start-time and thus does not require a mutex
1108cf35eb4Sdan   */
1118cf35eb4Sdan   sqlite3_vfs sThisVfs;
1128cf35eb4Sdan 
1138cf35eb4Sdan   /* The sIoMethods defines the methods used by sqlite3_file objects
1148cf35eb4Sdan   ** associated with this shim.  It is initialized at start-time and does
1158cf35eb4Sdan   ** not require a mutex.
1168cf35eb4Sdan   **
1178cf35eb4Sdan   ** When the underlying VFS is called to open a file, it might return
1188cf35eb4Sdan   ** either a version 1 or a version 2 sqlite3_file object.  This shim
1198cf35eb4Sdan   ** has to create a wrapper sqlite3_file of the same version.  Hence
1208cf35eb4Sdan   ** there are two I/O method structures, one for version 1 and the other
1218cf35eb4Sdan   ** for version 2.
1228cf35eb4Sdan   */
1238cf35eb4Sdan   sqlite3_io_methods sIoMethodsV1;
1248cf35eb4Sdan   sqlite3_io_methods sIoMethodsV2;
1258cf35eb4Sdan 
1268cf35eb4Sdan   /* True when this shim as been initialized.
1278cf35eb4Sdan   */
1288cf35eb4Sdan   int isInitialized;
1298cf35eb4Sdan 
1308cf35eb4Sdan   /* For run-time access any of the other global data structures in this
1318cf35eb4Sdan   ** shim, the following mutex must be held.
1328cf35eb4Sdan   */
1338cf35eb4Sdan   sqlite3_mutex *pMutex;
1348cf35eb4Sdan 
1358cf35eb4Sdan   /* List of quotaGroup objects.
1368cf35eb4Sdan   */
1378cf35eb4Sdan   quotaGroup *pGroup;
1388cf35eb4Sdan 
1398cf35eb4Sdan } gQuota;
1408cf35eb4Sdan 
1418cf35eb4Sdan /************************* Utility Routines *********************************/
1428cf35eb4Sdan /*
1438cf35eb4Sdan ** Acquire and release the mutex used to serialize access to the
1448cf35eb4Sdan ** list of quotaGroups.
1458cf35eb4Sdan */
1468cf35eb4Sdan static void quotaEnter(void){ sqlite3_mutex_enter(gQuota.pMutex); }
1478cf35eb4Sdan static void quotaLeave(void){ sqlite3_mutex_leave(gQuota.pMutex); }
1488cf35eb4Sdan 
1498cf35eb4Sdan 
1508cf35eb4Sdan /* If the reference count and threshold for a quotaGroup are both
1518cf35eb4Sdan ** zero, then destroy the quotaGroup.
1528cf35eb4Sdan */
1538cf35eb4Sdan static void quotaGroupDeref(quotaGroup *p){
154*745de2e4Sdrh   if( p->pFile==0 && p->iLimit==0 ){
1558cf35eb4Sdan     if( p->pNext ) p->pNext->ppPrev = p->ppPrev;
1568cf35eb4Sdan     if( p->ppPrev ) *p->ppPrev = p->pNext;
1578cf35eb4Sdan     sqlite3_free(p);
1588cf35eb4Sdan   }
1598cf35eb4Sdan }
1608cf35eb4Sdan 
1618cf35eb4Sdan /*
1628cf35eb4Sdan ** Return TRUE if string z matches glob pattern zGlob.
1638cf35eb4Sdan **
1648cf35eb4Sdan ** Globbing rules:
1658cf35eb4Sdan **
1668cf35eb4Sdan **      '*'       Matches any sequence of zero or more characters.
1678cf35eb4Sdan **
1688cf35eb4Sdan **      '?'       Matches exactly one character.
1698cf35eb4Sdan **
1708cf35eb4Sdan **     [...]      Matches one character from the enclosed list of
1718cf35eb4Sdan **                characters.
1728cf35eb4Sdan **
1738cf35eb4Sdan **     [^...]     Matches one character not in the enclosed list.
1748cf35eb4Sdan **
1758cf35eb4Sdan */
1768cf35eb4Sdan static int strglob(const char *zGlob, const char *z){
1778cf35eb4Sdan   int c, c2;
1788cf35eb4Sdan   int invert;
1798cf35eb4Sdan   int seen;
1808cf35eb4Sdan 
1818cf35eb4Sdan   while( (c = (*(zGlob++)))!=0 ){
1828cf35eb4Sdan     if( c=='*' ){
1838cf35eb4Sdan       while( (c=(*(zGlob++))) == '*' || c=='?' ){
1848cf35eb4Sdan         if( c=='?' && (*(z++))==0 ) return 0;
1858cf35eb4Sdan       }
1868cf35eb4Sdan       if( c==0 ){
1878cf35eb4Sdan         return 1;
1888cf35eb4Sdan       }else if( c=='[' ){
1898cf35eb4Sdan         while( *z && strglob(zGlob-1,z)==0 ){
1908cf35eb4Sdan           z++;
1918cf35eb4Sdan         }
1928cf35eb4Sdan         return (*z)!=0;
1938cf35eb4Sdan       }
1948cf35eb4Sdan       while( (c2 = (*(z++)))!=0 ){
1958cf35eb4Sdan         while( c2!=c ){
1968cf35eb4Sdan           c2 = *(z++);
1978cf35eb4Sdan           if( c2==0 ) return 0;
1988cf35eb4Sdan         }
1998cf35eb4Sdan         if( strglob(zGlob,z) ) return 1;
2008cf35eb4Sdan       }
2018cf35eb4Sdan       return 0;
2028cf35eb4Sdan     }else if( c=='?' ){
2038cf35eb4Sdan       if( (*(z++))==0 ) return 0;
2048cf35eb4Sdan     }else if( c=='[' ){
2058cf35eb4Sdan       int prior_c = 0;
2068cf35eb4Sdan       seen = 0;
2078cf35eb4Sdan       invert = 0;
2088cf35eb4Sdan       c = *(z++);
2098cf35eb4Sdan       if( c==0 ) return 0;
2108cf35eb4Sdan       c2 = *(zGlob++);
2118cf35eb4Sdan       if( c2=='^' ){
2128cf35eb4Sdan         invert = 1;
2138cf35eb4Sdan         c2 = *(zGlob++);
2148cf35eb4Sdan       }
2158cf35eb4Sdan       if( c2==']' ){
2168cf35eb4Sdan         if( c==']' ) seen = 1;
2178cf35eb4Sdan         c2 = *(zGlob++);
2188cf35eb4Sdan       }
2198cf35eb4Sdan       while( c2 && c2!=']' ){
2208cf35eb4Sdan         if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
2218cf35eb4Sdan           c2 = *(zGlob++);
2228cf35eb4Sdan           if( c>=prior_c && c<=c2 ) seen = 1;
2238cf35eb4Sdan           prior_c = 0;
2248cf35eb4Sdan         }else{
2258cf35eb4Sdan           if( c==c2 ){
2268cf35eb4Sdan             seen = 1;
2278cf35eb4Sdan           }
2288cf35eb4Sdan           prior_c = c2;
2298cf35eb4Sdan         }
2308cf35eb4Sdan         c2 = *(zGlob++);
2318cf35eb4Sdan       }
2328cf35eb4Sdan       if( c2==0 || (seen ^ invert)==0 ) return 0;
2338cf35eb4Sdan     }else{
2348cf35eb4Sdan       if( c!=(*(z++)) ) return 0;
2358cf35eb4Sdan     }
2368cf35eb4Sdan   }
2378cf35eb4Sdan   return *z==0;
2388cf35eb4Sdan }
2398cf35eb4Sdan 
2408cf35eb4Sdan 
2418cf35eb4Sdan /* Find a quotaGroup given the filename.
2428cf35eb4Sdan **
243*745de2e4Sdrh ** Return a pointer to the quotaOpen object. Return NULL if not found.
2448cf35eb4Sdan */
2458cf35eb4Sdan static quotaGroup *quotaGroupFind(const char *zFilename){
2468cf35eb4Sdan   quotaGroup *p;
2478cf35eb4Sdan   for(p=gQuota.pGroup; p && strglob(p->zPattern, zFilename)==0; p=p->pNext){}
2488cf35eb4Sdan   return p;
2498cf35eb4Sdan }
2508cf35eb4Sdan 
251*745de2e4Sdrh /* Translate an sqlite3_file* that is really a quotaOpen* into
2528cf35eb4Sdan ** an sqlite3_file* for the underlying original VFS.
2538cf35eb4Sdan */
254*745de2e4Sdrh static sqlite3_file *quotaSubOpen(sqlite3_file *pOpen){
255*745de2e4Sdrh   quotaOpen *p = (quotaOpen*)pOpen;
2568cf35eb4Sdan   return (sqlite3_file*)&p[1];
2578cf35eb4Sdan }
2588cf35eb4Sdan 
2598cf35eb4Sdan /************************* VFS Method Wrappers *****************************/
2608cf35eb4Sdan /*
2618cf35eb4Sdan ** This is the xOpen method used for the "quota" VFS.
2628cf35eb4Sdan **
2638cf35eb4Sdan ** Most of the work is done by the underlying original VFS.  This method
2648cf35eb4Sdan ** simply links the new file into the quota group if it is a file that
2658cf35eb4Sdan ** needs to be tracked.
2668cf35eb4Sdan */
267*745de2e4Sdrh static int quotaxOpen(
2688cf35eb4Sdan   sqlite3_vfs *pVfs,
2698cf35eb4Sdan   const char *zName,
270*745de2e4Sdrh   sqlite3_file *pOpen,
2718cf35eb4Sdan   int flags,
2728cf35eb4Sdan   int *pOutFlags
2738cf35eb4Sdan ){
2748cf35eb4Sdan   int rc;
275*745de2e4Sdrh   quotaOpen *pQuotaOpen;
276*745de2e4Sdrh   quotaFile *pFile;
2778cf35eb4Sdan   quotaGroup *pGroup;
278*745de2e4Sdrh   sqlite3_file *pSubOpen;
2798cf35eb4Sdan   sqlite3_vfs *pOrigVfs = gQuota.pOrigVfs;
2808cf35eb4Sdan 
2818cf35eb4Sdan   /* If the file is not a main database file or a WAL, then use the
2828cf35eb4Sdan   ** normal xOpen method.
2838cf35eb4Sdan   */
2848cf35eb4Sdan   if( (flags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_WAL))==0 ){
285*745de2e4Sdrh     return pOrigVfs->xOpen(pOrigVfs, zName, pOpen, flags, pOutFlags);
2868cf35eb4Sdan   }
2878cf35eb4Sdan 
2888cf35eb4Sdan   /* If the name of the file does not match any quota group, then
2898cf35eb4Sdan   ** use the normal xOpen method.
2908cf35eb4Sdan   */
2918cf35eb4Sdan   quotaEnter();
2928cf35eb4Sdan   pGroup = quotaGroupFind(zName);
2938cf35eb4Sdan   if( pGroup==0 ){
294*745de2e4Sdrh     rc = pOrigVfs->xOpen(pOrigVfs, zName, pOpen, flags, pOutFlags);
2958cf35eb4Sdan   }else{
2968cf35eb4Sdan     /* If we get to this point, it means the file needs to be quota tracked.
2978cf35eb4Sdan     */
298*745de2e4Sdrh     pQuotaOpen = (quotaOpen*)pOpen;
299*745de2e4Sdrh     pSubOpen = quotaSubOpen(pOpen);
300*745de2e4Sdrh     rc = pOrigVfs->xOpen(pOrigVfs, zName, pSubOpen, flags, pOutFlags);
3018cf35eb4Sdan     if( rc==SQLITE_OK ){
302*745de2e4Sdrh       for(pFile=pGroup->pFile; pFile && strcmp(pFile->zFilename, zName);
303*745de2e4Sdrh           pFile=pFile->pNext){}
304*745de2e4Sdrh       if( pFile==0 ){
305*745de2e4Sdrh         int nName = strlen(zName);
306*745de2e4Sdrh         pFile = sqlite3_malloc( sizeof(*pFile) + nName + 1 );
307*745de2e4Sdrh         if( pFile==0 ){
308*745de2e4Sdrh           quotaLeave();
309*745de2e4Sdrh           pSubOpen->pMethods->xClose(pSubOpen);
310*745de2e4Sdrh           return SQLITE_NOMEM;
311*745de2e4Sdrh         }
312*745de2e4Sdrh         memset(pFile, 0, sizeof(*pFile));
313*745de2e4Sdrh         pFile->zFilename = (char*)&pFile[1];
314*745de2e4Sdrh         memcpy(pFile->zFilename, zName, nName+1);
315*745de2e4Sdrh         pFile->pNext = pGroup->pFile;
316*745de2e4Sdrh         if( pGroup->pFile ) pGroup->pFile->ppPrev = &pFile->pNext;
317*745de2e4Sdrh         pFile->ppPrev = &pGroup->pFile;
318*745de2e4Sdrh         pGroup->pFile = pFile;
319*745de2e4Sdrh         pFile->pGroup = pGroup;
320*745de2e4Sdrh       }
321*745de2e4Sdrh       pFile->nRef++;
322*745de2e4Sdrh       pQuotaOpen->pFile = pFile;
323*745de2e4Sdrh       if( pSubOpen->pMethods->iVersion==1 ){
324*745de2e4Sdrh         pQuotaOpen->base.pMethods = &gQuota.sIoMethodsV1;
3258cf35eb4Sdan       }else{
326*745de2e4Sdrh         pQuotaOpen->base.pMethods = &gQuota.sIoMethodsV2;
3278cf35eb4Sdan       }
3288cf35eb4Sdan     }
3298cf35eb4Sdan   }
3308cf35eb4Sdan   quotaLeave();
3318cf35eb4Sdan   return rc;
3328cf35eb4Sdan }
3338cf35eb4Sdan 
3348cf35eb4Sdan /************************ I/O Method Wrappers *******************************/
3358cf35eb4Sdan 
3368cf35eb4Sdan /* xClose requests get passed through to the original VFS.  But we
337*745de2e4Sdrh ** also have to unlink the quotaOpen from the quotaGroup.
3388cf35eb4Sdan */
339*745de2e4Sdrh static int quotaClose(sqlite3_file *pOpen){
340*745de2e4Sdrh   quotaOpen *p = (quotaOpen*)pOpen;
341*745de2e4Sdrh   quotaFile *pFile = p->pFile;
342*745de2e4Sdrh   sqlite3_file *pSubOpen = quotaSubOpen(pOpen);
3438cf35eb4Sdan   int rc;
344*745de2e4Sdrh   rc = pSubOpen->pMethods->xClose(pSubOpen);
3458cf35eb4Sdan   quotaEnter();
346*745de2e4Sdrh   pFile->nRef--;
347*745de2e4Sdrh   if( pFile->nRef==0 ){
348*745de2e4Sdrh     quotaGroup *pGroup = pFile->pGroup;
349*745de2e4Sdrh     pGroup->iSize -= pFile->iSize;
350*745de2e4Sdrh     if( pFile->pNext ) pFile->pNext->ppPrev = pFile->ppPrev;
351*745de2e4Sdrh     *pFile->ppPrev = pFile->pNext;
3528cf35eb4Sdan     quotaGroupDeref(pGroup);
353*745de2e4Sdrh     sqlite3_free(pFile);
354*745de2e4Sdrh   }
3558cf35eb4Sdan   quotaLeave();
3568cf35eb4Sdan   return rc;
3578cf35eb4Sdan }
3588cf35eb4Sdan 
3598cf35eb4Sdan /* Pass xRead requests directory thru to the original VFS without
3608cf35eb4Sdan ** further processing.
3618cf35eb4Sdan */
3628cf35eb4Sdan static int quotaRead(
363*745de2e4Sdrh   sqlite3_file *pOpen,
3648cf35eb4Sdan   void *pBuf,
3658cf35eb4Sdan   int iAmt,
3668cf35eb4Sdan   sqlite3_int64 iOfst
3678cf35eb4Sdan ){
368*745de2e4Sdrh   sqlite3_file *pSubOpen = quotaSubOpen(pOpen);
369*745de2e4Sdrh   return pSubOpen->pMethods->xRead(pSubOpen, pBuf, iAmt, iOfst);
3708cf35eb4Sdan }
3718cf35eb4Sdan 
3728cf35eb4Sdan /* Check xWrite requests to see if they expand the file.  If they do,
3738cf35eb4Sdan ** the perform a quota check before passing them through to the
3748cf35eb4Sdan ** original VFS.
3758cf35eb4Sdan */
3768cf35eb4Sdan static int quotaWrite(
377*745de2e4Sdrh   sqlite3_file *pOpen,
3788cf35eb4Sdan   const void *pBuf,
3798cf35eb4Sdan   int iAmt,
3808cf35eb4Sdan   sqlite3_int64 iOfst
3818cf35eb4Sdan ){
382*745de2e4Sdrh   quotaOpen *p = (quotaOpen*)pOpen;
383*745de2e4Sdrh   sqlite3_file *pSubOpen = quotaSubOpen(pOpen);
3848cf35eb4Sdan   sqlite3_int64 iEnd = iOfst+iAmt;
3858cf35eb4Sdan   quotaGroup *pGroup;
386*745de2e4Sdrh   quotaFile *pFile = p->pFile;
3878cf35eb4Sdan   sqlite3_int64 szNew;
3888cf35eb4Sdan 
389*745de2e4Sdrh   if( pFile->iSize<iEnd ){
390*745de2e4Sdrh     pGroup = pFile->pGroup;
3918cf35eb4Sdan     quotaEnter();
392*745de2e4Sdrh     szNew = pGroup->iSize - pFile->iSize + iEnd;
3938cf35eb4Sdan     if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){
3948cf35eb4Sdan       if( pGroup->xCallback ){
395*745de2e4Sdrh         pGroup->xCallback(pFile->zFilename, &pGroup->iLimit, szNew,
3968cf35eb4Sdan                           pGroup->pArg);
3978cf35eb4Sdan       }
3988cf35eb4Sdan       if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){
3998cf35eb4Sdan         quotaLeave();
4008cf35eb4Sdan         return SQLITE_FULL;
4018cf35eb4Sdan       }
4028cf35eb4Sdan     }
4038cf35eb4Sdan     pGroup->iSize = szNew;
404*745de2e4Sdrh     pFile->iSize = iEnd;
4058cf35eb4Sdan     quotaLeave();
4068cf35eb4Sdan   }
407*745de2e4Sdrh   return pSubOpen->pMethods->xWrite(pSubOpen, pBuf, iAmt, iOfst);
4088cf35eb4Sdan }
4098cf35eb4Sdan 
4108cf35eb4Sdan /* Pass xTruncate requests thru to the original VFS.  If the
4118cf35eb4Sdan ** success, update the file size.
4128cf35eb4Sdan */
413*745de2e4Sdrh static int quotaTruncate(sqlite3_file *pOpen, sqlite3_int64 size){
414*745de2e4Sdrh   quotaOpen *p = (quotaOpen*)pOpen;
415*745de2e4Sdrh   sqlite3_file *pSubOpen = quotaSubOpen(pOpen);
416*745de2e4Sdrh   int rc = pSubOpen->pMethods->xTruncate(pSubOpen, size);
417*745de2e4Sdrh   quotaFile *pFile = p->pFile;
418*745de2e4Sdrh   quotaGroup *pGroup;
4198cf35eb4Sdan   if( rc==SQLITE_OK ){
4208cf35eb4Sdan     quotaEnter();
421*745de2e4Sdrh     pGroup = pFile->pGroup;
422*745de2e4Sdrh     pGroup->iSize -= pFile->iSize;
423*745de2e4Sdrh     pFile->iSize = size;
4248cf35eb4Sdan     pGroup->iSize += size;
4258cf35eb4Sdan     quotaLeave();
4268cf35eb4Sdan   }
4278cf35eb4Sdan   return rc;
4288cf35eb4Sdan }
4298cf35eb4Sdan 
4308cf35eb4Sdan /* Pass xSync requests through to the original VFS without change
4318cf35eb4Sdan */
432*745de2e4Sdrh static int quotaSync(sqlite3_file *pOpen, int flags){
433*745de2e4Sdrh   sqlite3_file *pSubOpen = quotaSubOpen(pOpen);
434*745de2e4Sdrh   return pSubOpen->pMethods->xSync(pSubOpen, flags);
4358cf35eb4Sdan }
4368cf35eb4Sdan 
4378cf35eb4Sdan /* Pass xFileSize requests through to the original VFS but then
4388cf35eb4Sdan ** update the quotaGroup with the new size before returning.
4398cf35eb4Sdan */
440*745de2e4Sdrh static int quotaFileSize(sqlite3_file *pOpen, sqlite3_int64 *pSize){
441*745de2e4Sdrh   quotaOpen *p = (quotaOpen*)pOpen;
442*745de2e4Sdrh   sqlite3_file *pSubOpen = quotaSubOpen(pOpen);
443*745de2e4Sdrh   quotaFile *pFile = p->pFile;
4448cf35eb4Sdan   quotaGroup *pGroup;
4458cf35eb4Sdan   sqlite3_int64 sz;
4468cf35eb4Sdan   int rc;
4478cf35eb4Sdan 
448*745de2e4Sdrh   rc = pSubOpen->pMethods->xFileSize(pSubOpen, &sz);
4498cf35eb4Sdan   if( rc==SQLITE_OK ){
4508cf35eb4Sdan     quotaEnter();
451*745de2e4Sdrh     pGroup = pFile->pGroup;
452*745de2e4Sdrh     pGroup->iSize -= pFile->iSize;
453*745de2e4Sdrh     pFile->iSize = sz;
4548cf35eb4Sdan     pGroup->iSize += sz;
4558cf35eb4Sdan     quotaLeave();
4568cf35eb4Sdan     *pSize = sz;
4578cf35eb4Sdan   }
4588cf35eb4Sdan   return rc;
4598cf35eb4Sdan }
4608cf35eb4Sdan 
4618cf35eb4Sdan /* Pass xLock requests through to the original VFS unchanged.
4628cf35eb4Sdan */
463*745de2e4Sdrh static int quotaLock(sqlite3_file *pOpen, int lock){
464*745de2e4Sdrh   sqlite3_file *pSubOpen = quotaSubOpen(pOpen);
465*745de2e4Sdrh   return pSubOpen->pMethods->xLock(pSubOpen, lock);
4668cf35eb4Sdan }
4678cf35eb4Sdan 
4688cf35eb4Sdan /* Pass xUnlock requests through to the original VFS unchanged.
4698cf35eb4Sdan */
470*745de2e4Sdrh static int quotaUnlock(sqlite3_file *pOpen, int lock){
471*745de2e4Sdrh   sqlite3_file *pSubOpen = quotaSubOpen(pOpen);
472*745de2e4Sdrh   return pSubOpen->pMethods->xUnlock(pSubOpen, lock);
4738cf35eb4Sdan }
4748cf35eb4Sdan 
4758cf35eb4Sdan /* Pass xCheckReservedLock requests through to the original VFS unchanged.
4768cf35eb4Sdan */
477*745de2e4Sdrh static int quotaCheckReservedLock(sqlite3_file *pOpen, int *pResOut){
478*745de2e4Sdrh   sqlite3_file *pSubOpen = quotaSubOpen(pOpen);
479*745de2e4Sdrh   return pSubOpen->pMethods->xCheckReservedLock(pSubOpen, pResOut);
4808cf35eb4Sdan }
4818cf35eb4Sdan 
4828cf35eb4Sdan /* Pass xFileControl requests through to the original VFS unchanged.
4838cf35eb4Sdan */
484*745de2e4Sdrh static int quotaOpenControl(sqlite3_file *pOpen, int op, void *pArg){
485*745de2e4Sdrh   sqlite3_file *pSubOpen = quotaSubOpen(pOpen);
486*745de2e4Sdrh   return pSubOpen->pMethods->xFileControl(pSubOpen, op, pArg);
4878cf35eb4Sdan }
4888cf35eb4Sdan 
4898cf35eb4Sdan /* Pass xSectorSize requests through to the original VFS unchanged.
4908cf35eb4Sdan */
491*745de2e4Sdrh static int quotaSectorSize(sqlite3_file *pOpen){
492*745de2e4Sdrh   sqlite3_file *pSubOpen = quotaSubOpen(pOpen);
493*745de2e4Sdrh   return pSubOpen->pMethods->xSectorSize(pSubOpen);
4948cf35eb4Sdan }
4958cf35eb4Sdan 
4968cf35eb4Sdan /* Pass xDeviceCharacteristics requests through to the original VFS unchanged.
4978cf35eb4Sdan */
498*745de2e4Sdrh static int quotaDeviceCharacteristics(sqlite3_file *pOpen){
499*745de2e4Sdrh   sqlite3_file *pSubOpen = quotaSubOpen(pOpen);
500*745de2e4Sdrh   return pSubOpen->pMethods->xDeviceCharacteristics(pSubOpen);
5018cf35eb4Sdan }
5028cf35eb4Sdan 
5038cf35eb4Sdan /* Pass xShmMap requests through to the original VFS unchanged.
5048cf35eb4Sdan */
5058cf35eb4Sdan static int quotaShmMap(
506*745de2e4Sdrh   sqlite3_file *pOpen,            /* Handle open on database file */
5078cf35eb4Sdan   int iRegion,                    /* Region to retrieve */
5088cf35eb4Sdan   int szRegion,                   /* Size of regions */
5098cf35eb4Sdan   int bExtend,                    /* True to extend file if necessary */
5108cf35eb4Sdan   void volatile **pp              /* OUT: Mapped memory */
5118cf35eb4Sdan ){
512*745de2e4Sdrh   sqlite3_file *pSubOpen = quotaSubOpen(pOpen);
513*745de2e4Sdrh   return pSubOpen->pMethods->xShmMap(pSubOpen, iRegion, szRegion, bExtend, pp);
5148cf35eb4Sdan }
5158cf35eb4Sdan 
5168cf35eb4Sdan /* Pass xShmLock requests through to the original VFS unchanged.
5178cf35eb4Sdan */
5188cf35eb4Sdan static int quotaShmLock(
519*745de2e4Sdrh   sqlite3_file *pOpen,       /* Database file holding the shared memory */
5208cf35eb4Sdan   int ofst,                  /* First lock to acquire or release */
5218cf35eb4Sdan   int n,                     /* Number of locks to acquire or release */
5228cf35eb4Sdan   int flags                  /* What to do with the lock */
5238cf35eb4Sdan ){
524*745de2e4Sdrh   sqlite3_file *pSubOpen = quotaSubOpen(pOpen);
525*745de2e4Sdrh   return pSubOpen->pMethods->xShmLock(pSubOpen, ofst, n, flags);
5268cf35eb4Sdan }
5278cf35eb4Sdan 
5288cf35eb4Sdan /* Pass xShmBarrier requests through to the original VFS unchanged.
5298cf35eb4Sdan */
530*745de2e4Sdrh static void quotaShmBarrier(sqlite3_file *pOpen){
531*745de2e4Sdrh   sqlite3_file *pSubOpen = quotaSubOpen(pOpen);
532*745de2e4Sdrh   pSubOpen->pMethods->xShmBarrier(pSubOpen);
5338cf35eb4Sdan }
5348cf35eb4Sdan 
5358cf35eb4Sdan /* Pass xShmUnmap requests through to the original VFS unchanged.
5368cf35eb4Sdan */
537*745de2e4Sdrh static int quotaShmUnmap(sqlite3_file *pOpen, int deleteFlag){
538*745de2e4Sdrh   sqlite3_file *pSubOpen = quotaSubOpen(pOpen);
539*745de2e4Sdrh   return pSubOpen->pMethods->xShmUnmap(pSubOpen, deleteFlag);
5408cf35eb4Sdan }
5418cf35eb4Sdan 
5428cf35eb4Sdan /************************** Public Interfaces *****************************/
5438cf35eb4Sdan /*
5448cf35eb4Sdan ** Initialize the quota VFS shim.  Use the VFS named zOrigVfsName
5458cf35eb4Sdan ** as the VFS that does the actual work.  Use the default if
5468cf35eb4Sdan ** zOrigVfsName==NULL.
5478cf35eb4Sdan **
5488cf35eb4Sdan ** The quota VFS shim is named "quota".  It will become the default
5498cf35eb4Sdan ** VFS if makeDefault is non-zero.
5508cf35eb4Sdan **
5518cf35eb4Sdan ** THIS ROUTINE IS NOT THREADSAFE.  Call this routine exactly once
5528cf35eb4Sdan ** during start-up.
5538cf35eb4Sdan */
5548cf35eb4Sdan int sqlite3_quota_initialize(const char *zOrigVfsName, int makeDefault){
5558cf35eb4Sdan   sqlite3_vfs *pOrigVfs;
5568cf35eb4Sdan   if( gQuota.isInitialized ) return SQLITE_MISUSE;
5578cf35eb4Sdan   pOrigVfs = sqlite3_vfs_find(zOrigVfsName);
5588cf35eb4Sdan   if( pOrigVfs==0 ) return SQLITE_ERROR;
5598cf35eb4Sdan   assert( pOrigVfs!=&gQuota.sThisVfs );
5608cf35eb4Sdan   gQuota.pMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
5618cf35eb4Sdan   if( !gQuota.pMutex ){
5628cf35eb4Sdan     return SQLITE_NOMEM;
5638cf35eb4Sdan   }
5648cf35eb4Sdan   gQuota.isInitialized = 1;
5658cf35eb4Sdan   gQuota.pOrigVfs = pOrigVfs;
5668cf35eb4Sdan   gQuota.sThisVfs = *pOrigVfs;
567*745de2e4Sdrh   gQuota.sThisVfs.xOpen = quotaxOpen;
568*745de2e4Sdrh   gQuota.sThisVfs.szOsFile += sizeof(quotaOpen);
5698cf35eb4Sdan   gQuota.sThisVfs.zName = "quota";
5708cf35eb4Sdan   gQuota.sIoMethodsV1.iVersion = 1;
5718cf35eb4Sdan   gQuota.sIoMethodsV1.xClose = quotaClose;
5728cf35eb4Sdan   gQuota.sIoMethodsV1.xRead = quotaRead;
5738cf35eb4Sdan   gQuota.sIoMethodsV1.xWrite = quotaWrite;
5748cf35eb4Sdan   gQuota.sIoMethodsV1.xTruncate = quotaTruncate;
5758cf35eb4Sdan   gQuota.sIoMethodsV1.xSync = quotaSync;
5768cf35eb4Sdan   gQuota.sIoMethodsV1.xFileSize = quotaFileSize;
5778cf35eb4Sdan   gQuota.sIoMethodsV1.xLock = quotaLock;
5788cf35eb4Sdan   gQuota.sIoMethodsV1.xUnlock = quotaUnlock;
5798cf35eb4Sdan   gQuota.sIoMethodsV1.xCheckReservedLock = quotaCheckReservedLock;
580*745de2e4Sdrh   gQuota.sIoMethodsV1.xFileControl = quotaOpenControl;
5818cf35eb4Sdan   gQuota.sIoMethodsV1.xSectorSize = quotaSectorSize;
5828cf35eb4Sdan   gQuota.sIoMethodsV1.xDeviceCharacteristics = quotaDeviceCharacteristics;
5838cf35eb4Sdan   gQuota.sIoMethodsV2 = gQuota.sIoMethodsV1;
5848cf35eb4Sdan   gQuota.sIoMethodsV2.iVersion = 2;
5858cf35eb4Sdan   gQuota.sIoMethodsV2.xShmMap = quotaShmMap;
5868cf35eb4Sdan   gQuota.sIoMethodsV2.xShmLock = quotaShmLock;
5878cf35eb4Sdan   gQuota.sIoMethodsV2.xShmBarrier = quotaShmBarrier;
5888cf35eb4Sdan   gQuota.sIoMethodsV2.xShmUnmap = quotaShmUnmap;
5898cf35eb4Sdan   sqlite3_vfs_register(&gQuota.sThisVfs, makeDefault);
5908cf35eb4Sdan   return SQLITE_OK;
5918cf35eb4Sdan }
5928cf35eb4Sdan 
5938cf35eb4Sdan /*
5948cf35eb4Sdan ** Shutdown the quota system.
5958cf35eb4Sdan **
5968cf35eb4Sdan ** All SQLite database connections must be closed before calling this
5978cf35eb4Sdan ** routine.
5988cf35eb4Sdan **
5998cf35eb4Sdan ** THIS ROUTINE IS NOT THREADSAFE.  Call this routine exactly one while
6008cf35eb4Sdan ** shutting down in order to free all remaining quota groups.
6018cf35eb4Sdan */
6028cf35eb4Sdan int sqlite3_quota_shutdown(void){
6038cf35eb4Sdan   quotaGroup *p;
6048cf35eb4Sdan   if( gQuota.isInitialized==0 ) return SQLITE_MISUSE;
6058cf35eb4Sdan   for(p=gQuota.pGroup; p; p=p->pNext){
606*745de2e4Sdrh     if( p->pFile ) return SQLITE_MISUSE;
6078cf35eb4Sdan   }
6088cf35eb4Sdan   while( gQuota.pGroup ){
6098cf35eb4Sdan     quotaGroup *p = gQuota.pGroup;
6108cf35eb4Sdan     gQuota.pGroup = p->pNext;
6118cf35eb4Sdan     sqlite3_free(p);
6128cf35eb4Sdan   }
6138cf35eb4Sdan   gQuota.isInitialized = 0;
6148cf35eb4Sdan   sqlite3_mutex_free(gQuota.pMutex);
6158cf35eb4Sdan   sqlite3_vfs_unregister(&gQuota.sThisVfs);
6168cf35eb4Sdan   memset(&gQuota, 0, sizeof(gQuota));
6178cf35eb4Sdan   return SQLITE_OK;
6188cf35eb4Sdan }
6198cf35eb4Sdan 
6208cf35eb4Sdan /*
6218cf35eb4Sdan ** Create or destroy a quota group.
6228cf35eb4Sdan **
6238cf35eb4Sdan ** The quota group is defined by the zPattern.  When calling this routine
6248cf35eb4Sdan ** with a zPattern for a quota group that already exists, this routine
6258cf35eb4Sdan ** merely updates the iLimit, xCallback, and pArg values for that quota
6268cf35eb4Sdan ** group.  If zPattern is new, then a new quota group is created.
6278cf35eb4Sdan **
6288cf35eb4Sdan ** If the iLimit for a quota group is set to zero, then the quota group
6298cf35eb4Sdan ** is disabled and will be deleted when the last database connection using
6308cf35eb4Sdan ** the quota group is closed.
6318cf35eb4Sdan **
6328cf35eb4Sdan ** Calling this routine on a zPattern that does not exist and with a
6338cf35eb4Sdan ** zero iLimit is a no-op.
6348cf35eb4Sdan **
6358cf35eb4Sdan ** A quota group must exist with a non-zero iLimit prior to opening
6368cf35eb4Sdan ** database connections if those connections are to participate in the
6378cf35eb4Sdan ** quota group.  Creating a quota group does not affect database connections
6388cf35eb4Sdan ** that are already open.
6398cf35eb4Sdan */
6408cf35eb4Sdan int sqlite3_quota_set(
6418cf35eb4Sdan   const char *zPattern,           /* The filename pattern */
6428cf35eb4Sdan   sqlite3_int64 iLimit,           /* New quota to set for this quota group */
6438cf35eb4Sdan   void (*xCallback)(              /* Callback invoked when going over quota */
6448cf35eb4Sdan      const char *zFilename,         /* Name of file whose size increases */
6458cf35eb4Sdan      sqlite3_int64 *piLimit,        /* IN/OUT: The current limit */
6468cf35eb4Sdan      sqlite3_int64 iSize,           /* Total size of all files in the group */
6478cf35eb4Sdan      void *pArg                     /* Client data */
6488cf35eb4Sdan   ),
6498cf35eb4Sdan   void *pArg                      /* client data passed thru to callback */
6508cf35eb4Sdan ){
6518cf35eb4Sdan   quotaGroup *pGroup;
6528cf35eb4Sdan   quotaEnter();
6538cf35eb4Sdan   pGroup = gQuota.pGroup;
6548cf35eb4Sdan   while( pGroup && strcmp(pGroup->zPattern, zPattern)!=0 ){
6558cf35eb4Sdan     pGroup = pGroup->pNext;
6568cf35eb4Sdan   }
6578cf35eb4Sdan   if( pGroup==0 && iLimit>0 ){
6588cf35eb4Sdan     int nPattern = strlen(zPattern);
6598cf35eb4Sdan     pGroup = sqlite3_malloc( sizeof(*pGroup) + nPattern + 1 );
6608cf35eb4Sdan     if( pGroup==0 ){
6618cf35eb4Sdan       quotaLeave();
6628cf35eb4Sdan       return SQLITE_NOMEM;
6638cf35eb4Sdan     }
6648cf35eb4Sdan     memset(pGroup, 0, sizeof(*pGroup));
6658cf35eb4Sdan     pGroup->zPattern = (char*)&pGroup[1];
6668cf35eb4Sdan     memcpy((char *)pGroup->zPattern, zPattern, nPattern+1);
6678cf35eb4Sdan     if( gQuota.pGroup ) gQuota.pGroup->ppPrev = &pGroup->pNext;
6688cf35eb4Sdan     pGroup->pNext = gQuota.pGroup;
6698cf35eb4Sdan     pGroup->ppPrev = &gQuota.pGroup;
6708cf35eb4Sdan     gQuota.pGroup = pGroup;
6718cf35eb4Sdan   }
6728cf35eb4Sdan   pGroup->iLimit = iLimit;
6738cf35eb4Sdan   pGroup->xCallback = xCallback;
6748cf35eb4Sdan   pGroup->pArg = pArg;
6758cf35eb4Sdan   quotaGroupDeref(pGroup);
6768cf35eb4Sdan   quotaLeave();
6778cf35eb4Sdan   return SQLITE_OK;
6788cf35eb4Sdan }
6798cf35eb4Sdan 
6808cf35eb4Sdan 
6818cf35eb4Sdan /***************************** Test Code ***********************************/
6828cf35eb4Sdan #ifdef SQLITE_TEST
6838cf35eb4Sdan #include <tcl.h>
6848cf35eb4Sdan 
6858cf35eb4Sdan typedef struct TclQuotaCallback TclQuotaCallback;
6868cf35eb4Sdan struct TclQuotaCallback {
6878cf35eb4Sdan   Tcl_Interp *interp;
6888cf35eb4Sdan   Tcl_Obj *pScript;
6898cf35eb4Sdan   TclQuotaCallback *pNext;
6908cf35eb4Sdan };
6918cf35eb4Sdan static TclQuotaCallback *pQuotaCallbackList = 0;
6928cf35eb4Sdan 
6938cf35eb4Sdan extern const char *sqlite3TestErrorName(int);
6948cf35eb4Sdan 
6958cf35eb4Sdan static void tclQuotaCallback(
6968cf35eb4Sdan   const char *zFilename,          /* Name of file whose size increases */
6978cf35eb4Sdan   sqlite3_int64 *piLimit,         /* IN/OUT: The current limit */
6988cf35eb4Sdan   sqlite3_int64 iSize,            /* Total size of all files in the group */
6998cf35eb4Sdan   void *pArg                      /* Client data */
7008cf35eb4Sdan ){
7018cf35eb4Sdan   TclQuotaCallback *p;            /* Callback script object */
7028cf35eb4Sdan   Tcl_Obj *pEval;                 /* Script to evaluate */
7038cf35eb4Sdan   Tcl_Obj *pVarname;              /* Name of variable to pass as 2nd arg */
7048cf35eb4Sdan   unsigned int rnd;               /* Random part of pVarname */
7058cf35eb4Sdan   int rc;                         /* Tcl error code */
7068cf35eb4Sdan 
7078cf35eb4Sdan   p = (TclQuotaCallback *)pArg;
7088cf35eb4Sdan 
7098cf35eb4Sdan   pVarname = Tcl_NewStringObj("::piLimit_", -1);
7108cf35eb4Sdan   Tcl_IncrRefCount(pVarname);
7118cf35eb4Sdan   sqlite3_randomness(sizeof(rnd), (void *)&rnd);
7128cf35eb4Sdan   Tcl_AppendObjToObj(pVarname, Tcl_NewIntObj((int)(rnd&0x7FFFFFFF)));
7138cf35eb4Sdan   Tcl_ObjSetVar2(p->interp, pVarname, 0, Tcl_NewWideIntObj(*piLimit), 0);
7148cf35eb4Sdan 
7158cf35eb4Sdan   pEval = Tcl_DuplicateObj(p->pScript);
7168cf35eb4Sdan   Tcl_IncrRefCount(pEval);
7178cf35eb4Sdan   Tcl_ListObjAppendElement(0, pEval, Tcl_NewStringObj(zFilename, -1));
7188cf35eb4Sdan   Tcl_ListObjAppendElement(0, pEval, pVarname);
7198cf35eb4Sdan   Tcl_ListObjAppendElement(0, pEval, Tcl_NewWideIntObj(iSize));
7208cf35eb4Sdan   rc = Tcl_EvalObjEx(p->interp, pEval, TCL_EVAL_GLOBAL);
7218cf35eb4Sdan 
7228cf35eb4Sdan   if( rc==TCL_OK ){
7238cf35eb4Sdan     Tcl_Obj *pLimit = Tcl_ObjGetVar2(p->interp, pVarname, 0, 0);
7248cf35eb4Sdan     rc = Tcl_GetWideIntFromObj(p->interp, pLimit, piLimit);
7258cf35eb4Sdan     Tcl_UnsetVar(p->interp, Tcl_GetString(pVarname), 0);
7268cf35eb4Sdan   }
7278cf35eb4Sdan 
7288cf35eb4Sdan   Tcl_DecrRefCount(pEval);
7298cf35eb4Sdan   Tcl_DecrRefCount(pVarname);
7308cf35eb4Sdan   if( rc!=TCL_OK ) Tcl_BackgroundError(p->interp);
7318cf35eb4Sdan }
7328cf35eb4Sdan 
7338cf35eb4Sdan /*
7348cf35eb4Sdan ** tclcmd: sqlite3_quota_initialize NAME MAKEDEFAULT
7358cf35eb4Sdan */
7368cf35eb4Sdan static int test_quota_initialize(
7378cf35eb4Sdan   void * clientData,
7388cf35eb4Sdan   Tcl_Interp *interp,
7398cf35eb4Sdan   int objc,
7408cf35eb4Sdan   Tcl_Obj *CONST objv[]
7418cf35eb4Sdan ){
7428cf35eb4Sdan   const char *zName;              /* Name of new quota VFS */
7438cf35eb4Sdan   int makeDefault;                /* True to make the new VFS the default */
7448cf35eb4Sdan   int rc;                         /* Value returned by quota_initialize() */
7458cf35eb4Sdan 
7468cf35eb4Sdan   /* Process arguments */
7478cf35eb4Sdan   if( objc!=3 ){
7488cf35eb4Sdan     Tcl_WrongNumArgs(interp, 1, objv, "NAME MAKEDEFAULT");
7498cf35eb4Sdan     return TCL_ERROR;
7508cf35eb4Sdan   }
7518cf35eb4Sdan   zName = Tcl_GetString(objv[1]);
7528cf35eb4Sdan   if( Tcl_GetBooleanFromObj(interp, objv[2], &makeDefault) ) return TCL_ERROR;
7538cf35eb4Sdan   if( zName[0]=='\0' ) zName = 0;
7548cf35eb4Sdan 
7558cf35eb4Sdan   /* Call sqlite3_quota_initialize() */
7568cf35eb4Sdan   rc = sqlite3_quota_initialize(zName, makeDefault);
7578cf35eb4Sdan   Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC);
7588cf35eb4Sdan 
7598cf35eb4Sdan   return TCL_OK;
7608cf35eb4Sdan }
7618cf35eb4Sdan 
7628cf35eb4Sdan /*
7638cf35eb4Sdan ** tclcmd: sqlite3_quota_shutdown
7648cf35eb4Sdan */
7658cf35eb4Sdan static int test_quota_shutdown(
7668cf35eb4Sdan   void * clientData,
7678cf35eb4Sdan   Tcl_Interp *interp,
7688cf35eb4Sdan   int objc,
7698cf35eb4Sdan   Tcl_Obj *CONST objv[]
7708cf35eb4Sdan ){
7718cf35eb4Sdan   int rc;                         /* Value returned by quota_shutdown() */
7728cf35eb4Sdan 
7738cf35eb4Sdan   if( objc!=1 ){
7748cf35eb4Sdan     Tcl_WrongNumArgs(interp, 1, objv, "");
7758cf35eb4Sdan     return TCL_ERROR;
7768cf35eb4Sdan   }
7778cf35eb4Sdan 
7788cf35eb4Sdan   /* Call sqlite3_quota_shutdown() */
7798cf35eb4Sdan   rc = sqlite3_quota_shutdown();
7808cf35eb4Sdan   Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC);
7818cf35eb4Sdan 
7828cf35eb4Sdan   /* If the quota system was successfully shut down, delete all the quota
7838cf35eb4Sdan   ** callback script objects in the global linked list.
7848cf35eb4Sdan   */
7858cf35eb4Sdan   if( rc==SQLITE_OK ){
7868cf35eb4Sdan     TclQuotaCallback *p, *pNext;
7878cf35eb4Sdan     for(p=pQuotaCallbackList; p; p=pNext){
7888cf35eb4Sdan       pNext = p->pNext;
7898cf35eb4Sdan       Tcl_DecrRefCount(p->pScript);
7908cf35eb4Sdan       ckfree((char *)p);
7918cf35eb4Sdan     }
7928cf35eb4Sdan     pQuotaCallbackList = 0;
7938cf35eb4Sdan   }
7948cf35eb4Sdan 
7958cf35eb4Sdan   return TCL_OK;
7968cf35eb4Sdan }
7978cf35eb4Sdan 
7988cf35eb4Sdan /*
7998cf35eb4Sdan ** tclcmd: sqlite3_quota_set PATTERN LIMIT SCRIPT
8008cf35eb4Sdan */
8018cf35eb4Sdan static int test_quota_set(
8028cf35eb4Sdan   void * clientData,
8038cf35eb4Sdan   Tcl_Interp *interp,
8048cf35eb4Sdan   int objc,
8058cf35eb4Sdan   Tcl_Obj *CONST objv[]
8068cf35eb4Sdan ){
8078cf35eb4Sdan   const char *zPattern;           /* File pattern to configure */
8088cf35eb4Sdan   sqlite3_int64 iLimit;           /* Initial quota in bytes */
8098cf35eb4Sdan   Tcl_Obj *pScript;               /* Tcl script to invoke to increase quota */
8108cf35eb4Sdan   int rc;                         /* Value returned by quota_set() */
8118cf35eb4Sdan   TclQuotaCallback *p;            /* Callback object */
8128cf35eb4Sdan 
8138cf35eb4Sdan   /* Process arguments */
8148cf35eb4Sdan   if( objc!=4 ){
8158cf35eb4Sdan     Tcl_WrongNumArgs(interp, 1, objv, "PATTERN LIMIT SCRIPT");
8168cf35eb4Sdan     return TCL_ERROR;
8178cf35eb4Sdan   }
8188cf35eb4Sdan   zPattern = Tcl_GetString(objv[1]);
8198cf35eb4Sdan   if( Tcl_GetWideIntFromObj(interp, objv[2], &iLimit) ) return TCL_ERROR;
8208cf35eb4Sdan   pScript = objv[3];
8218cf35eb4Sdan 
8228cf35eb4Sdan   /* Allocate a TclQuotaCallback object */
8238cf35eb4Sdan   p = (TclQuotaCallback *)ckalloc(sizeof(TclQuotaCallback));
8248cf35eb4Sdan   memset(p, 0, sizeof(TclQuotaCallback));
8258cf35eb4Sdan 
8268cf35eb4Sdan   /* Invoke sqlite3_quota_set() */
8278cf35eb4Sdan   rc = sqlite3_quota_set(zPattern, iLimit, tclQuotaCallback, (void *)p);
8288cf35eb4Sdan   if( rc!=SQLITE_OK ){
8298cf35eb4Sdan     ckfree((char *)p);
8308cf35eb4Sdan   }else{
8318cf35eb4Sdan     Tcl_IncrRefCount(pScript);
8328cf35eb4Sdan     p->interp = interp;
8338cf35eb4Sdan     p->pScript = pScript;
8348cf35eb4Sdan     p->pNext = pQuotaCallbackList;
8358cf35eb4Sdan     pQuotaCallbackList = p;
8368cf35eb4Sdan   }
8378cf35eb4Sdan 
8388cf35eb4Sdan   Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC);
8398cf35eb4Sdan   return TCL_OK;
8408cf35eb4Sdan }
8418cf35eb4Sdan 
8428cf35eb4Sdan /*
8438cf35eb4Sdan ** This routine registers the custom TCL commands defined in this
8448cf35eb4Sdan ** module.  This should be the only procedure visible from outside
8458cf35eb4Sdan ** of this module.
8468cf35eb4Sdan */
8478cf35eb4Sdan int Sqlitequota_Init(Tcl_Interp *interp){
8488cf35eb4Sdan   static struct {
8498cf35eb4Sdan      char *zName;
8508cf35eb4Sdan      Tcl_ObjCmdProc *xProc;
8518cf35eb4Sdan   } aCmd[] = {
8528cf35eb4Sdan     { "sqlite3_quota_initialize", test_quota_initialize },
8538cf35eb4Sdan     { "sqlite3_quota_shutdown", test_quota_shutdown },
8548cf35eb4Sdan     { "sqlite3_quota_set", test_quota_set },
8558cf35eb4Sdan   };
8568cf35eb4Sdan   int i;
8578cf35eb4Sdan 
8588cf35eb4Sdan   for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
8598cf35eb4Sdan     Tcl_CreateObjCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0);
8608cf35eb4Sdan   }
8618cf35eb4Sdan 
8628cf35eb4Sdan   return TCL_OK;
8638cf35eb4Sdan }
8648cf35eb4Sdan #endif
865