xref: /sqlite-3.40.0/src/test_quota.c (revision ef9659ba)
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;
38*ef9659baSdrh typedef struct quotaConn quotaConn;
39745de2e4Sdrh typedef struct quotaFile quotaFile;
40745de2e4Sdrh 
418cf35eb4Sdan /*
42549dc1baSdrh ** A "quota group" is a collection of files whose collective size we want
43549dc1baSdrh ** to limit.  Each quota group is defined by a GLOB pattern.
448cf35eb4Sdan **
45549dc1baSdrh ** There is an instance of the following object for each defined quota
46549dc1baSdrh ** group.  This object records the GLOB pattern that defines which files
47549dc1baSdrh ** belong to the quota group.  The object also remembers the size limit
48549dc1baSdrh ** for the group (the quota) and the callback to be invoked when the
49549dc1baSdrh ** sum of the sizes of the files within the group goes over the limit.
508cf35eb4Sdan **
51549dc1baSdrh ** A quota group must be established (using sqlite3_quota_set(...))
52549dc1baSdrh ** prior to opening any of the database connections that access files
53549dc1baSdrh ** 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() */
66*ef9659baSdrh   void (*xDestroy)(void*);       /* Optional destructor for pArg */
678cf35eb4Sdan   quotaGroup *pNext, **ppPrev;   /* Doubly linked list of all quota objects */
68745de2e4Sdrh   quotaFile *pFile;              /* Files within this group */
698cf35eb4Sdan };
708cf35eb4Sdan 
718cf35eb4Sdan /*
72745de2e4Sdrh ** An instance of this structure represents a single file that is part
73745de2e4Sdrh ** of a quota group.  A single file can be opened multiple times.  In
74745de2e4Sdrh ** order keep multiple openings of the same file from causing the size
75549dc1baSdrh ** of the file to count against the quota multiple times, each file
76745de2e4Sdrh ** has a unique instance of this object and multiple open connections
77745de2e4Sdrh ** to the same file each point to a single instance of this object.
788cf35eb4Sdan */
798cf35eb4Sdan struct quotaFile {
80745de2e4Sdrh   char *zFilename;                /* Name of this file */
818cf35eb4Sdan   quotaGroup *pGroup;             /* Upper bound on file size */
828cf35eb4Sdan   sqlite3_int64 iSize;            /* Current size of this file */
83745de2e4Sdrh   int nRef;                       /* Number of times this file is open */
84745de2e4Sdrh   quotaFile *pNext, **ppPrev;     /* Linked list of files in the same group */
85745de2e4Sdrh };
86745de2e4Sdrh 
87745de2e4Sdrh /*
88745de2e4Sdrh ** An instance of the following object represents each open connection
89549dc1baSdrh ** to a file that participates in quota tracking.  This object is a
90549dc1baSdrh ** subclass of sqlite3_file.  The sqlite3_file object for the underlying
91549dc1baSdrh ** VFS is appended to this structure.
92745de2e4Sdrh */
93*ef9659baSdrh struct quotaConn {
94745de2e4Sdrh   sqlite3_file base;              /* Base class - must be first */
95745de2e4Sdrh   quotaFile *pFile;               /* The underlying file */
968cf35eb4Sdan   /* The underlying VFS sqlite3_file is appended to this object */
978cf35eb4Sdan };
988cf35eb4Sdan 
998cf35eb4Sdan /************************* Global Variables **********************************/
1008cf35eb4Sdan /*
1018cf35eb4Sdan ** All global variables used by this file are containing within the following
1028cf35eb4Sdan ** gQuota structure.
1038cf35eb4Sdan */
1048cf35eb4Sdan static struct {
105549dc1baSdrh   /* The pOrigVfs is the real, original underlying VFS implementation.
1068cf35eb4Sdan   ** Most operations pass-through to the real VFS.  This value is read-only
1078cf35eb4Sdan   ** during operation.  It is only modified at start-time and thus does not
1088cf35eb4Sdan   ** require a mutex.
1098cf35eb4Sdan   */
1108cf35eb4Sdan   sqlite3_vfs *pOrigVfs;
1118cf35eb4Sdan 
1128cf35eb4Sdan   /* The sThisVfs is the VFS structure used by this shim.  It is initialized
1138cf35eb4Sdan   ** at start-time and thus does not require a mutex
1148cf35eb4Sdan   */
1158cf35eb4Sdan   sqlite3_vfs sThisVfs;
1168cf35eb4Sdan 
1178cf35eb4Sdan   /* The sIoMethods defines the methods used by sqlite3_file objects
1188cf35eb4Sdan   ** associated with this shim.  It is initialized at start-time and does
1198cf35eb4Sdan   ** not require a mutex.
1208cf35eb4Sdan   **
1218cf35eb4Sdan   ** When the underlying VFS is called to open a file, it might return
1228cf35eb4Sdan   ** either a version 1 or a version 2 sqlite3_file object.  This shim
1238cf35eb4Sdan   ** has to create a wrapper sqlite3_file of the same version.  Hence
1248cf35eb4Sdan   ** there are two I/O method structures, one for version 1 and the other
1258cf35eb4Sdan   ** for version 2.
1268cf35eb4Sdan   */
1278cf35eb4Sdan   sqlite3_io_methods sIoMethodsV1;
1288cf35eb4Sdan   sqlite3_io_methods sIoMethodsV2;
1298cf35eb4Sdan 
1308cf35eb4Sdan   /* True when this shim as been initialized.
1318cf35eb4Sdan   */
1328cf35eb4Sdan   int isInitialized;
1338cf35eb4Sdan 
1348cf35eb4Sdan   /* For run-time access any of the other global data structures in this
1358cf35eb4Sdan   ** shim, the following mutex must be held.
1368cf35eb4Sdan   */
1378cf35eb4Sdan   sqlite3_mutex *pMutex;
1388cf35eb4Sdan 
1398cf35eb4Sdan   /* List of quotaGroup objects.
1408cf35eb4Sdan   */
1418cf35eb4Sdan   quotaGroup *pGroup;
1428cf35eb4Sdan 
1438cf35eb4Sdan } gQuota;
1448cf35eb4Sdan 
1458cf35eb4Sdan /************************* Utility Routines *********************************/
1468cf35eb4Sdan /*
1478cf35eb4Sdan ** Acquire and release the mutex used to serialize access to the
1488cf35eb4Sdan ** list of quotaGroups.
1498cf35eb4Sdan */
1508cf35eb4Sdan static void quotaEnter(void){ sqlite3_mutex_enter(gQuota.pMutex); }
1518cf35eb4Sdan static void quotaLeave(void){ sqlite3_mutex_leave(gQuota.pMutex); }
1528cf35eb4Sdan 
1538cf35eb4Sdan 
1548cf35eb4Sdan /* If the reference count and threshold for a quotaGroup are both
1558cf35eb4Sdan ** zero, then destroy the quotaGroup.
1568cf35eb4Sdan */
1578cf35eb4Sdan static void quotaGroupDeref(quotaGroup *p){
158745de2e4Sdrh   if( p->pFile==0 && p->iLimit==0 ){
1598cf35eb4Sdan     if( p->pNext ) p->pNext->ppPrev = p->ppPrev;
1608cf35eb4Sdan     if( p->ppPrev ) *p->ppPrev = p->pNext;
161*ef9659baSdrh     if( p->xDestroy ) p->xDestroy(p->pArg);
1628cf35eb4Sdan     sqlite3_free(p);
1638cf35eb4Sdan   }
1648cf35eb4Sdan }
1658cf35eb4Sdan 
1668cf35eb4Sdan /*
1678cf35eb4Sdan ** Return TRUE if string z matches glob pattern zGlob.
1688cf35eb4Sdan **
1698cf35eb4Sdan ** Globbing rules:
1708cf35eb4Sdan **
1718cf35eb4Sdan **      '*'       Matches any sequence of zero or more characters.
1728cf35eb4Sdan **
1738cf35eb4Sdan **      '?'       Matches exactly one character.
1748cf35eb4Sdan **
1758cf35eb4Sdan **     [...]      Matches one character from the enclosed list of
1768cf35eb4Sdan **                characters.
1778cf35eb4Sdan **
1788cf35eb4Sdan **     [^...]     Matches one character not in the enclosed list.
1798cf35eb4Sdan **
1808cf35eb4Sdan */
1818cf35eb4Sdan static int strglob(const char *zGlob, const char *z){
1828cf35eb4Sdan   int c, c2;
1838cf35eb4Sdan   int invert;
1848cf35eb4Sdan   int seen;
1858cf35eb4Sdan 
1868cf35eb4Sdan   while( (c = (*(zGlob++)))!=0 ){
1878cf35eb4Sdan     if( c=='*' ){
1888cf35eb4Sdan       while( (c=(*(zGlob++))) == '*' || c=='?' ){
1898cf35eb4Sdan         if( c=='?' && (*(z++))==0 ) return 0;
1908cf35eb4Sdan       }
1918cf35eb4Sdan       if( c==0 ){
1928cf35eb4Sdan         return 1;
1938cf35eb4Sdan       }else if( c=='[' ){
1948cf35eb4Sdan         while( *z && strglob(zGlob-1,z)==0 ){
1958cf35eb4Sdan           z++;
1968cf35eb4Sdan         }
1978cf35eb4Sdan         return (*z)!=0;
1988cf35eb4Sdan       }
1998cf35eb4Sdan       while( (c2 = (*(z++)))!=0 ){
2008cf35eb4Sdan         while( c2!=c ){
2018cf35eb4Sdan           c2 = *(z++);
2028cf35eb4Sdan           if( c2==0 ) return 0;
2038cf35eb4Sdan         }
2048cf35eb4Sdan         if( strglob(zGlob,z) ) return 1;
2058cf35eb4Sdan       }
2068cf35eb4Sdan       return 0;
2078cf35eb4Sdan     }else if( c=='?' ){
2088cf35eb4Sdan       if( (*(z++))==0 ) return 0;
2098cf35eb4Sdan     }else if( c=='[' ){
2108cf35eb4Sdan       int prior_c = 0;
2118cf35eb4Sdan       seen = 0;
2128cf35eb4Sdan       invert = 0;
2138cf35eb4Sdan       c = *(z++);
2148cf35eb4Sdan       if( c==0 ) return 0;
2158cf35eb4Sdan       c2 = *(zGlob++);
2168cf35eb4Sdan       if( c2=='^' ){
2178cf35eb4Sdan         invert = 1;
2188cf35eb4Sdan         c2 = *(zGlob++);
2198cf35eb4Sdan       }
2208cf35eb4Sdan       if( c2==']' ){
2218cf35eb4Sdan         if( c==']' ) seen = 1;
2228cf35eb4Sdan         c2 = *(zGlob++);
2238cf35eb4Sdan       }
2248cf35eb4Sdan       while( c2 && c2!=']' ){
2258cf35eb4Sdan         if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
2268cf35eb4Sdan           c2 = *(zGlob++);
2278cf35eb4Sdan           if( c>=prior_c && c<=c2 ) seen = 1;
2288cf35eb4Sdan           prior_c = 0;
2298cf35eb4Sdan         }else{
2308cf35eb4Sdan           if( c==c2 ){
2318cf35eb4Sdan             seen = 1;
2328cf35eb4Sdan           }
2338cf35eb4Sdan           prior_c = c2;
2348cf35eb4Sdan         }
2358cf35eb4Sdan         c2 = *(zGlob++);
2368cf35eb4Sdan       }
2378cf35eb4Sdan       if( c2==0 || (seen ^ invert)==0 ) return 0;
2388cf35eb4Sdan     }else{
2398cf35eb4Sdan       if( c!=(*(z++)) ) return 0;
2408cf35eb4Sdan     }
2418cf35eb4Sdan   }
2428cf35eb4Sdan   return *z==0;
2438cf35eb4Sdan }
2448cf35eb4Sdan 
2458cf35eb4Sdan 
2468cf35eb4Sdan /* Find a quotaGroup given the filename.
2478cf35eb4Sdan **
248*ef9659baSdrh ** Return a pointer to the quotaGroup object. Return NULL if not found.
2498cf35eb4Sdan */
2508cf35eb4Sdan static quotaGroup *quotaGroupFind(const char *zFilename){
2518cf35eb4Sdan   quotaGroup *p;
2528cf35eb4Sdan   for(p=gQuota.pGroup; p && strglob(p->zPattern, zFilename)==0; p=p->pNext){}
2538cf35eb4Sdan   return p;
2548cf35eb4Sdan }
2558cf35eb4Sdan 
256*ef9659baSdrh /* Translate an sqlite3_file* that is really a quotaConn* into
257549dc1baSdrh ** the sqlite3_file* for the underlying original VFS.
2588cf35eb4Sdan */
259*ef9659baSdrh static sqlite3_file *quotaSubOpen(sqlite3_file *pConn){
260*ef9659baSdrh   quotaConn *p = (quotaConn*)pConn;
2618cf35eb4Sdan   return (sqlite3_file*)&p[1];
2628cf35eb4Sdan }
2638cf35eb4Sdan 
2648cf35eb4Sdan /************************* VFS Method Wrappers *****************************/
2658cf35eb4Sdan /*
2668cf35eb4Sdan ** This is the xOpen method used for the "quota" VFS.
2678cf35eb4Sdan **
2688cf35eb4Sdan ** Most of the work is done by the underlying original VFS.  This method
269549dc1baSdrh ** simply links the new file into the appropriate quota group if it is a
270549dc1baSdrh ** file that needs to be tracked.
2718cf35eb4Sdan */
272*ef9659baSdrh static int quotaOpen(
273549dc1baSdrh   sqlite3_vfs *pVfs,          /* The quota VFS */
274549dc1baSdrh   const char *zName,          /* Name of file to be opened */
275*ef9659baSdrh   sqlite3_file *pConn,        /* Fill in this file descriptor */
276549dc1baSdrh   int flags,                  /* Flags to control the opening */
277549dc1baSdrh   int *pOutFlags              /* Flags showing results of opening */
2788cf35eb4Sdan ){
279549dc1baSdrh   int rc;                                    /* Result code */
280*ef9659baSdrh   quotaConn *pQuotaOpen;                     /* The new quota file descriptor */
281549dc1baSdrh   quotaFile *pFile;                          /* Corresponding quotaFile obj */
282549dc1baSdrh   quotaGroup *pGroup;                        /* The group file belongs to */
283549dc1baSdrh   sqlite3_file *pSubOpen;                    /* Real file descriptor */
284549dc1baSdrh   sqlite3_vfs *pOrigVfs = gQuota.pOrigVfs;   /* Real VFS */
2858cf35eb4Sdan 
2868cf35eb4Sdan   /* If the file is not a main database file or a WAL, then use the
2878cf35eb4Sdan   ** normal xOpen method.
2888cf35eb4Sdan   */
2898cf35eb4Sdan   if( (flags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_WAL))==0 ){
290*ef9659baSdrh     return pOrigVfs->xOpen(pOrigVfs, zName, pConn, flags, pOutFlags);
2918cf35eb4Sdan   }
2928cf35eb4Sdan 
2938cf35eb4Sdan   /* If the name of the file does not match any quota group, then
2948cf35eb4Sdan   ** use the normal xOpen method.
2958cf35eb4Sdan   */
2968cf35eb4Sdan   quotaEnter();
2978cf35eb4Sdan   pGroup = quotaGroupFind(zName);
2988cf35eb4Sdan   if( pGroup==0 ){
299*ef9659baSdrh     rc = pOrigVfs->xOpen(pOrigVfs, zName, pConn, flags, pOutFlags);
3008cf35eb4Sdan   }else{
3018cf35eb4Sdan     /* If we get to this point, it means the file needs to be quota tracked.
3028cf35eb4Sdan     */
303*ef9659baSdrh     pQuotaOpen = (quotaConn*)pConn;
304*ef9659baSdrh     pSubOpen = quotaSubOpen(pConn);
305745de2e4Sdrh     rc = pOrigVfs->xOpen(pOrigVfs, zName, pSubOpen, flags, pOutFlags);
3068cf35eb4Sdan     if( rc==SQLITE_OK ){
307745de2e4Sdrh       for(pFile=pGroup->pFile; pFile && strcmp(pFile->zFilename, zName);
308745de2e4Sdrh           pFile=pFile->pNext){}
309745de2e4Sdrh       if( pFile==0 ){
310745de2e4Sdrh         int nName = strlen(zName);
311745de2e4Sdrh         pFile = sqlite3_malloc( sizeof(*pFile) + nName + 1 );
312745de2e4Sdrh         if( pFile==0 ){
313745de2e4Sdrh           quotaLeave();
314745de2e4Sdrh           pSubOpen->pMethods->xClose(pSubOpen);
315745de2e4Sdrh           return SQLITE_NOMEM;
316745de2e4Sdrh         }
317745de2e4Sdrh         memset(pFile, 0, sizeof(*pFile));
318745de2e4Sdrh         pFile->zFilename = (char*)&pFile[1];
319745de2e4Sdrh         memcpy(pFile->zFilename, zName, nName+1);
320745de2e4Sdrh         pFile->pNext = pGroup->pFile;
321745de2e4Sdrh         if( pGroup->pFile ) pGroup->pFile->ppPrev = &pFile->pNext;
322745de2e4Sdrh         pFile->ppPrev = &pGroup->pFile;
323745de2e4Sdrh         pGroup->pFile = pFile;
324745de2e4Sdrh         pFile->pGroup = pGroup;
325745de2e4Sdrh       }
326745de2e4Sdrh       pFile->nRef++;
327745de2e4Sdrh       pQuotaOpen->pFile = pFile;
328745de2e4Sdrh       if( pSubOpen->pMethods->iVersion==1 ){
329745de2e4Sdrh         pQuotaOpen->base.pMethods = &gQuota.sIoMethodsV1;
3308cf35eb4Sdan       }else{
331745de2e4Sdrh         pQuotaOpen->base.pMethods = &gQuota.sIoMethodsV2;
3328cf35eb4Sdan       }
3338cf35eb4Sdan     }
3348cf35eb4Sdan   }
3358cf35eb4Sdan   quotaLeave();
3368cf35eb4Sdan   return rc;
3378cf35eb4Sdan }
3388cf35eb4Sdan 
3398cf35eb4Sdan /************************ I/O Method Wrappers *******************************/
3408cf35eb4Sdan 
3418cf35eb4Sdan /* xClose requests get passed through to the original VFS.  But we
342*ef9659baSdrh ** also have to unlink the quotaConn from the quotaFile and quotaGroup.
343*ef9659baSdrh ** The quotaFile and/or quotaGroup are freed if they are no longer in use.
3448cf35eb4Sdan */
345*ef9659baSdrh static int quotaClose(sqlite3_file *pConn){
346*ef9659baSdrh   quotaConn *p = (quotaConn*)pConn;
347745de2e4Sdrh   quotaFile *pFile = p->pFile;
348*ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
3498cf35eb4Sdan   int rc;
350745de2e4Sdrh   rc = pSubOpen->pMethods->xClose(pSubOpen);
3518cf35eb4Sdan   quotaEnter();
352745de2e4Sdrh   pFile->nRef--;
353745de2e4Sdrh   if( pFile->nRef==0 ){
354745de2e4Sdrh     quotaGroup *pGroup = pFile->pGroup;
355745de2e4Sdrh     pGroup->iSize -= pFile->iSize;
356745de2e4Sdrh     if( pFile->pNext ) pFile->pNext->ppPrev = pFile->ppPrev;
357745de2e4Sdrh     *pFile->ppPrev = pFile->pNext;
3588cf35eb4Sdan     quotaGroupDeref(pGroup);
359745de2e4Sdrh     sqlite3_free(pFile);
360745de2e4Sdrh   }
3618cf35eb4Sdan   quotaLeave();
3628cf35eb4Sdan   return rc;
3638cf35eb4Sdan }
3648cf35eb4Sdan 
3658cf35eb4Sdan /* Pass xRead requests directory thru to the original VFS without
3668cf35eb4Sdan ** further processing.
3678cf35eb4Sdan */
3688cf35eb4Sdan static int quotaRead(
369*ef9659baSdrh   sqlite3_file *pConn,
3708cf35eb4Sdan   void *pBuf,
3718cf35eb4Sdan   int iAmt,
3728cf35eb4Sdan   sqlite3_int64 iOfst
3738cf35eb4Sdan ){
374*ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
375745de2e4Sdrh   return pSubOpen->pMethods->xRead(pSubOpen, pBuf, iAmt, iOfst);
3768cf35eb4Sdan }
3778cf35eb4Sdan 
3788cf35eb4Sdan /* Check xWrite requests to see if they expand the file.  If they do,
3798cf35eb4Sdan ** the perform a quota check before passing them through to the
3808cf35eb4Sdan ** original VFS.
3818cf35eb4Sdan */
3828cf35eb4Sdan static int quotaWrite(
383*ef9659baSdrh   sqlite3_file *pConn,
3848cf35eb4Sdan   const void *pBuf,
3858cf35eb4Sdan   int iAmt,
3868cf35eb4Sdan   sqlite3_int64 iOfst
3878cf35eb4Sdan ){
388*ef9659baSdrh   quotaConn *p = (quotaConn*)pConn;
389*ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
3908cf35eb4Sdan   sqlite3_int64 iEnd = iOfst+iAmt;
3918cf35eb4Sdan   quotaGroup *pGroup;
392745de2e4Sdrh   quotaFile *pFile = p->pFile;
3938cf35eb4Sdan   sqlite3_int64 szNew;
3948cf35eb4Sdan 
395745de2e4Sdrh   if( pFile->iSize<iEnd ){
396745de2e4Sdrh     pGroup = pFile->pGroup;
3978cf35eb4Sdan     quotaEnter();
398745de2e4Sdrh     szNew = pGroup->iSize - pFile->iSize + iEnd;
3998cf35eb4Sdan     if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){
4008cf35eb4Sdan       if( pGroup->xCallback ){
401745de2e4Sdrh         pGroup->xCallback(pFile->zFilename, &pGroup->iLimit, szNew,
4028cf35eb4Sdan                           pGroup->pArg);
4038cf35eb4Sdan       }
4048cf35eb4Sdan       if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){
4058cf35eb4Sdan         quotaLeave();
4068cf35eb4Sdan         return SQLITE_FULL;
4078cf35eb4Sdan       }
4088cf35eb4Sdan     }
4098cf35eb4Sdan     pGroup->iSize = szNew;
410745de2e4Sdrh     pFile->iSize = iEnd;
4118cf35eb4Sdan     quotaLeave();
4128cf35eb4Sdan   }
413745de2e4Sdrh   return pSubOpen->pMethods->xWrite(pSubOpen, pBuf, iAmt, iOfst);
4148cf35eb4Sdan }
4158cf35eb4Sdan 
4168cf35eb4Sdan /* Pass xTruncate requests thru to the original VFS.  If the
4178cf35eb4Sdan ** success, update the file size.
4188cf35eb4Sdan */
419*ef9659baSdrh static int quotaTruncate(sqlite3_file *pConn, sqlite3_int64 size){
420*ef9659baSdrh   quotaConn *p = (quotaConn*)pConn;
421*ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
422745de2e4Sdrh   int rc = pSubOpen->pMethods->xTruncate(pSubOpen, size);
423745de2e4Sdrh   quotaFile *pFile = p->pFile;
424745de2e4Sdrh   quotaGroup *pGroup;
4258cf35eb4Sdan   if( rc==SQLITE_OK ){
4268cf35eb4Sdan     quotaEnter();
427745de2e4Sdrh     pGroup = pFile->pGroup;
428745de2e4Sdrh     pGroup->iSize -= pFile->iSize;
429745de2e4Sdrh     pFile->iSize = size;
4308cf35eb4Sdan     pGroup->iSize += size;
4318cf35eb4Sdan     quotaLeave();
4328cf35eb4Sdan   }
4338cf35eb4Sdan   return rc;
4348cf35eb4Sdan }
4358cf35eb4Sdan 
4368cf35eb4Sdan /* Pass xSync requests through to the original VFS without change
4378cf35eb4Sdan */
438*ef9659baSdrh static int quotaSync(sqlite3_file *pConn, int flags){
439*ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
440745de2e4Sdrh   return pSubOpen->pMethods->xSync(pSubOpen, flags);
4418cf35eb4Sdan }
4428cf35eb4Sdan 
4438cf35eb4Sdan /* Pass xFileSize requests through to the original VFS but then
4448cf35eb4Sdan ** update the quotaGroup with the new size before returning.
4458cf35eb4Sdan */
446*ef9659baSdrh static int quotaFileSize(sqlite3_file *pConn, sqlite3_int64 *pSize){
447*ef9659baSdrh   quotaConn *p = (quotaConn*)pConn;
448*ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
449745de2e4Sdrh   quotaFile *pFile = p->pFile;
4508cf35eb4Sdan   quotaGroup *pGroup;
4518cf35eb4Sdan   sqlite3_int64 sz;
4528cf35eb4Sdan   int rc;
4538cf35eb4Sdan 
454745de2e4Sdrh   rc = pSubOpen->pMethods->xFileSize(pSubOpen, &sz);
4558cf35eb4Sdan   if( rc==SQLITE_OK ){
4568cf35eb4Sdan     quotaEnter();
457745de2e4Sdrh     pGroup = pFile->pGroup;
458745de2e4Sdrh     pGroup->iSize -= pFile->iSize;
459745de2e4Sdrh     pFile->iSize = sz;
4608cf35eb4Sdan     pGroup->iSize += sz;
4618cf35eb4Sdan     quotaLeave();
4628cf35eb4Sdan     *pSize = sz;
4638cf35eb4Sdan   }
4648cf35eb4Sdan   return rc;
4658cf35eb4Sdan }
4668cf35eb4Sdan 
4678cf35eb4Sdan /* Pass xLock requests through to the original VFS unchanged.
4688cf35eb4Sdan */
469*ef9659baSdrh static int quotaLock(sqlite3_file *pConn, int lock){
470*ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
471745de2e4Sdrh   return pSubOpen->pMethods->xLock(pSubOpen, lock);
4728cf35eb4Sdan }
4738cf35eb4Sdan 
4748cf35eb4Sdan /* Pass xUnlock requests through to the original VFS unchanged.
4758cf35eb4Sdan */
476*ef9659baSdrh static int quotaUnlock(sqlite3_file *pConn, int lock){
477*ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
478745de2e4Sdrh   return pSubOpen->pMethods->xUnlock(pSubOpen, lock);
4798cf35eb4Sdan }
4808cf35eb4Sdan 
4818cf35eb4Sdan /* Pass xCheckReservedLock requests through to the original VFS unchanged.
4828cf35eb4Sdan */
483*ef9659baSdrh static int quotaCheckReservedLock(sqlite3_file *pConn, int *pResOut){
484*ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
485745de2e4Sdrh   return pSubOpen->pMethods->xCheckReservedLock(pSubOpen, pResOut);
4868cf35eb4Sdan }
4878cf35eb4Sdan 
4888cf35eb4Sdan /* Pass xFileControl requests through to the original VFS unchanged.
4898cf35eb4Sdan */
490*ef9659baSdrh static int quotaFileControl(sqlite3_file *pConn, int op, void *pArg){
491*ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
492745de2e4Sdrh   return pSubOpen->pMethods->xFileControl(pSubOpen, op, pArg);
4938cf35eb4Sdan }
4948cf35eb4Sdan 
4958cf35eb4Sdan /* Pass xSectorSize requests through to the original VFS unchanged.
4968cf35eb4Sdan */
497*ef9659baSdrh static int quotaSectorSize(sqlite3_file *pConn){
498*ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
499745de2e4Sdrh   return pSubOpen->pMethods->xSectorSize(pSubOpen);
5008cf35eb4Sdan }
5018cf35eb4Sdan 
5028cf35eb4Sdan /* Pass xDeviceCharacteristics requests through to the original VFS unchanged.
5038cf35eb4Sdan */
504*ef9659baSdrh static int quotaDeviceCharacteristics(sqlite3_file *pConn){
505*ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
506745de2e4Sdrh   return pSubOpen->pMethods->xDeviceCharacteristics(pSubOpen);
5078cf35eb4Sdan }
5088cf35eb4Sdan 
5098cf35eb4Sdan /* Pass xShmMap requests through to the original VFS unchanged.
5108cf35eb4Sdan */
5118cf35eb4Sdan static int quotaShmMap(
512*ef9659baSdrh   sqlite3_file *pConn,            /* Handle open on database file */
5138cf35eb4Sdan   int iRegion,                    /* Region to retrieve */
5148cf35eb4Sdan   int szRegion,                   /* Size of regions */
5158cf35eb4Sdan   int bExtend,                    /* True to extend file if necessary */
5168cf35eb4Sdan   void volatile **pp              /* OUT: Mapped memory */
5178cf35eb4Sdan ){
518*ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
519745de2e4Sdrh   return pSubOpen->pMethods->xShmMap(pSubOpen, iRegion, szRegion, bExtend, pp);
5208cf35eb4Sdan }
5218cf35eb4Sdan 
5228cf35eb4Sdan /* Pass xShmLock requests through to the original VFS unchanged.
5238cf35eb4Sdan */
5248cf35eb4Sdan static int quotaShmLock(
525*ef9659baSdrh   sqlite3_file *pConn,       /* Database file holding the shared memory */
5268cf35eb4Sdan   int ofst,                  /* First lock to acquire or release */
5278cf35eb4Sdan   int n,                     /* Number of locks to acquire or release */
5288cf35eb4Sdan   int flags                  /* What to do with the lock */
5298cf35eb4Sdan ){
530*ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
531745de2e4Sdrh   return pSubOpen->pMethods->xShmLock(pSubOpen, ofst, n, flags);
5328cf35eb4Sdan }
5338cf35eb4Sdan 
5348cf35eb4Sdan /* Pass xShmBarrier requests through to the original VFS unchanged.
5358cf35eb4Sdan */
536*ef9659baSdrh static void quotaShmBarrier(sqlite3_file *pConn){
537*ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
538745de2e4Sdrh   pSubOpen->pMethods->xShmBarrier(pSubOpen);
5398cf35eb4Sdan }
5408cf35eb4Sdan 
5418cf35eb4Sdan /* Pass xShmUnmap requests through to the original VFS unchanged.
5428cf35eb4Sdan */
543*ef9659baSdrh static int quotaShmUnmap(sqlite3_file *pConn, int deleteFlag){
544*ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
545745de2e4Sdrh   return pSubOpen->pMethods->xShmUnmap(pSubOpen, deleteFlag);
5468cf35eb4Sdan }
5478cf35eb4Sdan 
5488cf35eb4Sdan /************************** Public Interfaces *****************************/
5498cf35eb4Sdan /*
5508cf35eb4Sdan ** Initialize the quota VFS shim.  Use the VFS named zOrigVfsName
5518cf35eb4Sdan ** as the VFS that does the actual work.  Use the default if
5528cf35eb4Sdan ** zOrigVfsName==NULL.
5538cf35eb4Sdan **
5548cf35eb4Sdan ** The quota VFS shim is named "quota".  It will become the default
5558cf35eb4Sdan ** VFS if makeDefault is non-zero.
5568cf35eb4Sdan **
5578cf35eb4Sdan ** THIS ROUTINE IS NOT THREADSAFE.  Call this routine exactly once
5588cf35eb4Sdan ** during start-up.
5598cf35eb4Sdan */
5608cf35eb4Sdan int sqlite3_quota_initialize(const char *zOrigVfsName, int makeDefault){
5618cf35eb4Sdan   sqlite3_vfs *pOrigVfs;
5628cf35eb4Sdan   if( gQuota.isInitialized ) return SQLITE_MISUSE;
5638cf35eb4Sdan   pOrigVfs = sqlite3_vfs_find(zOrigVfsName);
5648cf35eb4Sdan   if( pOrigVfs==0 ) return SQLITE_ERROR;
5658cf35eb4Sdan   assert( pOrigVfs!=&gQuota.sThisVfs );
5668cf35eb4Sdan   gQuota.pMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
5678cf35eb4Sdan   if( !gQuota.pMutex ){
5688cf35eb4Sdan     return SQLITE_NOMEM;
5698cf35eb4Sdan   }
5708cf35eb4Sdan   gQuota.isInitialized = 1;
5718cf35eb4Sdan   gQuota.pOrigVfs = pOrigVfs;
5728cf35eb4Sdan   gQuota.sThisVfs = *pOrigVfs;
573*ef9659baSdrh   gQuota.sThisVfs.xOpen = quotaOpen;
574*ef9659baSdrh   gQuota.sThisVfs.szOsFile += sizeof(quotaConn);
5758cf35eb4Sdan   gQuota.sThisVfs.zName = "quota";
5768cf35eb4Sdan   gQuota.sIoMethodsV1.iVersion = 1;
5778cf35eb4Sdan   gQuota.sIoMethodsV1.xClose = quotaClose;
5788cf35eb4Sdan   gQuota.sIoMethodsV1.xRead = quotaRead;
5798cf35eb4Sdan   gQuota.sIoMethodsV1.xWrite = quotaWrite;
5808cf35eb4Sdan   gQuota.sIoMethodsV1.xTruncate = quotaTruncate;
5818cf35eb4Sdan   gQuota.sIoMethodsV1.xSync = quotaSync;
5828cf35eb4Sdan   gQuota.sIoMethodsV1.xFileSize = quotaFileSize;
5838cf35eb4Sdan   gQuota.sIoMethodsV1.xLock = quotaLock;
5848cf35eb4Sdan   gQuota.sIoMethodsV1.xUnlock = quotaUnlock;
5858cf35eb4Sdan   gQuota.sIoMethodsV1.xCheckReservedLock = quotaCheckReservedLock;
586*ef9659baSdrh   gQuota.sIoMethodsV1.xFileControl = quotaFileControl;
5878cf35eb4Sdan   gQuota.sIoMethodsV1.xSectorSize = quotaSectorSize;
5888cf35eb4Sdan   gQuota.sIoMethodsV1.xDeviceCharacteristics = quotaDeviceCharacteristics;
5898cf35eb4Sdan   gQuota.sIoMethodsV2 = gQuota.sIoMethodsV1;
5908cf35eb4Sdan   gQuota.sIoMethodsV2.iVersion = 2;
5918cf35eb4Sdan   gQuota.sIoMethodsV2.xShmMap = quotaShmMap;
5928cf35eb4Sdan   gQuota.sIoMethodsV2.xShmLock = quotaShmLock;
5938cf35eb4Sdan   gQuota.sIoMethodsV2.xShmBarrier = quotaShmBarrier;
5948cf35eb4Sdan   gQuota.sIoMethodsV2.xShmUnmap = quotaShmUnmap;
5958cf35eb4Sdan   sqlite3_vfs_register(&gQuota.sThisVfs, makeDefault);
5968cf35eb4Sdan   return SQLITE_OK;
5978cf35eb4Sdan }
5988cf35eb4Sdan 
5998cf35eb4Sdan /*
6008cf35eb4Sdan ** Shutdown the quota system.
6018cf35eb4Sdan **
6028cf35eb4Sdan ** All SQLite database connections must be closed before calling this
6038cf35eb4Sdan ** routine.
6048cf35eb4Sdan **
6058cf35eb4Sdan ** THIS ROUTINE IS NOT THREADSAFE.  Call this routine exactly one while
6068cf35eb4Sdan ** shutting down in order to free all remaining quota groups.
6078cf35eb4Sdan */
6088cf35eb4Sdan int sqlite3_quota_shutdown(void){
6098cf35eb4Sdan   quotaGroup *p;
6108cf35eb4Sdan   if( gQuota.isInitialized==0 ) return SQLITE_MISUSE;
6118cf35eb4Sdan   for(p=gQuota.pGroup; p; p=p->pNext){
612745de2e4Sdrh     if( p->pFile ) return SQLITE_MISUSE;
6138cf35eb4Sdan   }
6148cf35eb4Sdan   while( gQuota.pGroup ){
6158cf35eb4Sdan     quotaGroup *p = gQuota.pGroup;
6168cf35eb4Sdan     gQuota.pGroup = p->pNext;
6178cf35eb4Sdan     sqlite3_free(p);
6188cf35eb4Sdan   }
6198cf35eb4Sdan   gQuota.isInitialized = 0;
6208cf35eb4Sdan   sqlite3_mutex_free(gQuota.pMutex);
6218cf35eb4Sdan   sqlite3_vfs_unregister(&gQuota.sThisVfs);
6228cf35eb4Sdan   memset(&gQuota, 0, sizeof(gQuota));
6238cf35eb4Sdan   return SQLITE_OK;
6248cf35eb4Sdan }
6258cf35eb4Sdan 
6268cf35eb4Sdan /*
6278cf35eb4Sdan ** Create or destroy a quota group.
6288cf35eb4Sdan **
6298cf35eb4Sdan ** The quota group is defined by the zPattern.  When calling this routine
6308cf35eb4Sdan ** with a zPattern for a quota group that already exists, this routine
6318cf35eb4Sdan ** merely updates the iLimit, xCallback, and pArg values for that quota
6328cf35eb4Sdan ** group.  If zPattern is new, then a new quota group is created.
6338cf35eb4Sdan **
6348cf35eb4Sdan ** If the iLimit for a quota group is set to zero, then the quota group
6358cf35eb4Sdan ** is disabled and will be deleted when the last database connection using
6368cf35eb4Sdan ** the quota group is closed.
6378cf35eb4Sdan **
6388cf35eb4Sdan ** Calling this routine on a zPattern that does not exist and with a
6398cf35eb4Sdan ** zero iLimit is a no-op.
6408cf35eb4Sdan **
6418cf35eb4Sdan ** A quota group must exist with a non-zero iLimit prior to opening
6428cf35eb4Sdan ** database connections if those connections are to participate in the
6438cf35eb4Sdan ** quota group.  Creating a quota group does not affect database connections
6448cf35eb4Sdan ** that are already open.
6458cf35eb4Sdan */
6468cf35eb4Sdan int sqlite3_quota_set(
6478cf35eb4Sdan   const char *zPattern,           /* The filename pattern */
6488cf35eb4Sdan   sqlite3_int64 iLimit,           /* New quota to set for this quota group */
6498cf35eb4Sdan   void (*xCallback)(              /* Callback invoked when going over quota */
6508cf35eb4Sdan      const char *zFilename,         /* Name of file whose size increases */
6518cf35eb4Sdan      sqlite3_int64 *piLimit,        /* IN/OUT: The current limit */
6528cf35eb4Sdan      sqlite3_int64 iSize,           /* Total size of all files in the group */
6538cf35eb4Sdan      void *pArg                     /* Client data */
6548cf35eb4Sdan   ),
655*ef9659baSdrh   void *pArg,                     /* client data passed thru to callback */
656*ef9659baSdrh   void (*xDestroy)(void*)         /* Optional destructor for pArg */
6578cf35eb4Sdan ){
6588cf35eb4Sdan   quotaGroup *pGroup;
6598cf35eb4Sdan   quotaEnter();
6608cf35eb4Sdan   pGroup = gQuota.pGroup;
6618cf35eb4Sdan   while( pGroup && strcmp(pGroup->zPattern, zPattern)!=0 ){
6628cf35eb4Sdan     pGroup = pGroup->pNext;
6638cf35eb4Sdan   }
6648cf35eb4Sdan   if( pGroup==0 && iLimit>0 ){
6658cf35eb4Sdan     int nPattern = strlen(zPattern);
6668cf35eb4Sdan     pGroup = sqlite3_malloc( sizeof(*pGroup) + nPattern + 1 );
6678cf35eb4Sdan     if( pGroup==0 ){
6688cf35eb4Sdan       quotaLeave();
6698cf35eb4Sdan       return SQLITE_NOMEM;
6708cf35eb4Sdan     }
6718cf35eb4Sdan     memset(pGroup, 0, sizeof(*pGroup));
6728cf35eb4Sdan     pGroup->zPattern = (char*)&pGroup[1];
6738cf35eb4Sdan     memcpy((char *)pGroup->zPattern, zPattern, nPattern+1);
6748cf35eb4Sdan     if( gQuota.pGroup ) gQuota.pGroup->ppPrev = &pGroup->pNext;
6758cf35eb4Sdan     pGroup->pNext = gQuota.pGroup;
6768cf35eb4Sdan     pGroup->ppPrev = &gQuota.pGroup;
6778cf35eb4Sdan     gQuota.pGroup = pGroup;
6788cf35eb4Sdan   }
6798cf35eb4Sdan   pGroup->iLimit = iLimit;
6808cf35eb4Sdan   pGroup->xCallback = xCallback;
681*ef9659baSdrh   if( pGroup->xDestroy && pGroup->pArg!=pArg ){
682*ef9659baSdrh     pGroup->xDestroy(pGroup->pArg);
683*ef9659baSdrh   }
6848cf35eb4Sdan   pGroup->pArg = pArg;
685*ef9659baSdrh   pGroup->xDestroy = xDestroy;
6868cf35eb4Sdan   quotaGroupDeref(pGroup);
6878cf35eb4Sdan   quotaLeave();
6888cf35eb4Sdan   return SQLITE_OK;
6898cf35eb4Sdan }
6908cf35eb4Sdan 
6918cf35eb4Sdan 
6928cf35eb4Sdan /***************************** Test Code ***********************************/
6938cf35eb4Sdan #ifdef SQLITE_TEST
6948cf35eb4Sdan #include <tcl.h>
6958cf35eb4Sdan 
696*ef9659baSdrh /*
697*ef9659baSdrh ** Argument passed to a TCL quota-over-limit callback.
698*ef9659baSdrh */
6998cf35eb4Sdan typedef struct TclQuotaCallback TclQuotaCallback;
7008cf35eb4Sdan struct TclQuotaCallback {
701*ef9659baSdrh   Tcl_Interp *interp;    /* Interpreter in which to run the script */
702*ef9659baSdrh   Tcl_Obj *pScript;      /* Script to be run */
7038cf35eb4Sdan };
7048cf35eb4Sdan 
7058cf35eb4Sdan extern const char *sqlite3TestErrorName(int);
7068cf35eb4Sdan 
707*ef9659baSdrh 
708*ef9659baSdrh /*
709*ef9659baSdrh ** This is the callback from a quota-over-limit.
710*ef9659baSdrh */
7118cf35eb4Sdan static void tclQuotaCallback(
7128cf35eb4Sdan   const char *zFilename,          /* Name of file whose size increases */
7138cf35eb4Sdan   sqlite3_int64 *piLimit,         /* IN/OUT: The current limit */
7148cf35eb4Sdan   sqlite3_int64 iSize,            /* Total size of all files in the group */
7158cf35eb4Sdan   void *pArg                      /* Client data */
7168cf35eb4Sdan ){
7178cf35eb4Sdan   TclQuotaCallback *p;            /* Callback script object */
7188cf35eb4Sdan   Tcl_Obj *pEval;                 /* Script to evaluate */
7198cf35eb4Sdan   Tcl_Obj *pVarname;              /* Name of variable to pass as 2nd arg */
7208cf35eb4Sdan   unsigned int rnd;               /* Random part of pVarname */
7218cf35eb4Sdan   int rc;                         /* Tcl error code */
7228cf35eb4Sdan 
7238cf35eb4Sdan   p = (TclQuotaCallback *)pArg;
7248cf35eb4Sdan 
7258cf35eb4Sdan   pVarname = Tcl_NewStringObj("::piLimit_", -1);
7268cf35eb4Sdan   Tcl_IncrRefCount(pVarname);
7278cf35eb4Sdan   sqlite3_randomness(sizeof(rnd), (void *)&rnd);
7288cf35eb4Sdan   Tcl_AppendObjToObj(pVarname, Tcl_NewIntObj((int)(rnd&0x7FFFFFFF)));
7298cf35eb4Sdan   Tcl_ObjSetVar2(p->interp, pVarname, 0, Tcl_NewWideIntObj(*piLimit), 0);
7308cf35eb4Sdan 
7318cf35eb4Sdan   pEval = Tcl_DuplicateObj(p->pScript);
7328cf35eb4Sdan   Tcl_IncrRefCount(pEval);
7338cf35eb4Sdan   Tcl_ListObjAppendElement(0, pEval, Tcl_NewStringObj(zFilename, -1));
7348cf35eb4Sdan   Tcl_ListObjAppendElement(0, pEval, pVarname);
7358cf35eb4Sdan   Tcl_ListObjAppendElement(0, pEval, Tcl_NewWideIntObj(iSize));
7368cf35eb4Sdan   rc = Tcl_EvalObjEx(p->interp, pEval, TCL_EVAL_GLOBAL);
7378cf35eb4Sdan 
7388cf35eb4Sdan   if( rc==TCL_OK ){
7398cf35eb4Sdan     Tcl_Obj *pLimit = Tcl_ObjGetVar2(p->interp, pVarname, 0, 0);
7408cf35eb4Sdan     rc = Tcl_GetWideIntFromObj(p->interp, pLimit, piLimit);
7418cf35eb4Sdan     Tcl_UnsetVar(p->interp, Tcl_GetString(pVarname), 0);
7428cf35eb4Sdan   }
7438cf35eb4Sdan 
7448cf35eb4Sdan   Tcl_DecrRefCount(pEval);
7458cf35eb4Sdan   Tcl_DecrRefCount(pVarname);
7468cf35eb4Sdan   if( rc!=TCL_OK ) Tcl_BackgroundError(p->interp);
7478cf35eb4Sdan }
7488cf35eb4Sdan 
7498cf35eb4Sdan /*
750*ef9659baSdrh ** Destructor for a TCL quota-over-limit callback.
751*ef9659baSdrh */
752*ef9659baSdrh static void tclCallbackDestructor(void *pObj){
753*ef9659baSdrh   TclQuotaCallback *p = (TclQuotaCallback*)pObj;
754*ef9659baSdrh   if( p ){
755*ef9659baSdrh     Tcl_DecrRefCount(p->pScript);
756*ef9659baSdrh     ckfree((char *)p);
757*ef9659baSdrh   }
758*ef9659baSdrh }
759*ef9659baSdrh 
760*ef9659baSdrh /*
7618cf35eb4Sdan ** tclcmd: sqlite3_quota_initialize NAME MAKEDEFAULT
7628cf35eb4Sdan */
7638cf35eb4Sdan static int test_quota_initialize(
7648cf35eb4Sdan   void * clientData,
7658cf35eb4Sdan   Tcl_Interp *interp,
7668cf35eb4Sdan   int objc,
7678cf35eb4Sdan   Tcl_Obj *CONST objv[]
7688cf35eb4Sdan ){
7698cf35eb4Sdan   const char *zName;              /* Name of new quota VFS */
7708cf35eb4Sdan   int makeDefault;                /* True to make the new VFS the default */
7718cf35eb4Sdan   int rc;                         /* Value returned by quota_initialize() */
7728cf35eb4Sdan 
7738cf35eb4Sdan   /* Process arguments */
7748cf35eb4Sdan   if( objc!=3 ){
7758cf35eb4Sdan     Tcl_WrongNumArgs(interp, 1, objv, "NAME MAKEDEFAULT");
7768cf35eb4Sdan     return TCL_ERROR;
7778cf35eb4Sdan   }
7788cf35eb4Sdan   zName = Tcl_GetString(objv[1]);
7798cf35eb4Sdan   if( Tcl_GetBooleanFromObj(interp, objv[2], &makeDefault) ) return TCL_ERROR;
7808cf35eb4Sdan   if( zName[0]=='\0' ) zName = 0;
7818cf35eb4Sdan 
7828cf35eb4Sdan   /* Call sqlite3_quota_initialize() */
7838cf35eb4Sdan   rc = sqlite3_quota_initialize(zName, makeDefault);
7848cf35eb4Sdan   Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC);
7858cf35eb4Sdan 
7868cf35eb4Sdan   return TCL_OK;
7878cf35eb4Sdan }
7888cf35eb4Sdan 
7898cf35eb4Sdan /*
7908cf35eb4Sdan ** tclcmd: sqlite3_quota_shutdown
7918cf35eb4Sdan */
7928cf35eb4Sdan static int test_quota_shutdown(
7938cf35eb4Sdan   void * clientData,
7948cf35eb4Sdan   Tcl_Interp *interp,
7958cf35eb4Sdan   int objc,
7968cf35eb4Sdan   Tcl_Obj *CONST objv[]
7978cf35eb4Sdan ){
7988cf35eb4Sdan   int rc;                         /* Value returned by quota_shutdown() */
7998cf35eb4Sdan 
8008cf35eb4Sdan   if( objc!=1 ){
8018cf35eb4Sdan     Tcl_WrongNumArgs(interp, 1, objv, "");
8028cf35eb4Sdan     return TCL_ERROR;
8038cf35eb4Sdan   }
8048cf35eb4Sdan 
8058cf35eb4Sdan   /* Call sqlite3_quota_shutdown() */
8068cf35eb4Sdan   rc = sqlite3_quota_shutdown();
8078cf35eb4Sdan   Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC);
8088cf35eb4Sdan 
8098cf35eb4Sdan   return TCL_OK;
8108cf35eb4Sdan }
8118cf35eb4Sdan 
8128cf35eb4Sdan /*
8138cf35eb4Sdan ** tclcmd: sqlite3_quota_set PATTERN LIMIT SCRIPT
8148cf35eb4Sdan */
8158cf35eb4Sdan static int test_quota_set(
8168cf35eb4Sdan   void * clientData,
8178cf35eb4Sdan   Tcl_Interp *interp,
8188cf35eb4Sdan   int objc,
8198cf35eb4Sdan   Tcl_Obj *CONST objv[]
8208cf35eb4Sdan ){
8218cf35eb4Sdan   const char *zPattern;           /* File pattern to configure */
8228cf35eb4Sdan   sqlite3_int64 iLimit;           /* Initial quota in bytes */
8238cf35eb4Sdan   Tcl_Obj *pScript;               /* Tcl script to invoke to increase quota */
8248cf35eb4Sdan   int rc;                         /* Value returned by quota_set() */
8258cf35eb4Sdan   TclQuotaCallback *p;            /* Callback object */
826*ef9659baSdrh   int nScript;                    /* Length of callback script */
8278cf35eb4Sdan 
8288cf35eb4Sdan   /* Process arguments */
8298cf35eb4Sdan   if( objc!=4 ){
8308cf35eb4Sdan     Tcl_WrongNumArgs(interp, 1, objv, "PATTERN LIMIT SCRIPT");
8318cf35eb4Sdan     return TCL_ERROR;
8328cf35eb4Sdan   }
8338cf35eb4Sdan   zPattern = Tcl_GetString(objv[1]);
8348cf35eb4Sdan   if( Tcl_GetWideIntFromObj(interp, objv[2], &iLimit) ) return TCL_ERROR;
8358cf35eb4Sdan   pScript = objv[3];
836*ef9659baSdrh   Tcl_GetStringFromObj(pScript, &nScript);
8378cf35eb4Sdan 
838*ef9659baSdrh   if( nScript>0 ){
8398cf35eb4Sdan     /* Allocate a TclQuotaCallback object */
8408cf35eb4Sdan     p = (TclQuotaCallback *)ckalloc(sizeof(TclQuotaCallback));
8418cf35eb4Sdan     memset(p, 0, sizeof(TclQuotaCallback));
8428cf35eb4Sdan     p->interp = interp;
843*ef9659baSdrh     Tcl_IncrRefCount(pScript);
8448cf35eb4Sdan     p->pScript = pScript;
845*ef9659baSdrh   }else{
846*ef9659baSdrh     p = 0;
8478cf35eb4Sdan   }
848*ef9659baSdrh   /* Invoke sqlite3_quota_set() */
849*ef9659baSdrh   rc = sqlite3_quota_set(zPattern, iLimit, tclQuotaCallback,
850*ef9659baSdrh                          (void*)p, tclCallbackDestructor);
8518cf35eb4Sdan 
8528cf35eb4Sdan   Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC);
8538cf35eb4Sdan   return TCL_OK;
8548cf35eb4Sdan }
8558cf35eb4Sdan 
8568cf35eb4Sdan /*
857*ef9659baSdrh ** tclcmd:  sqlite3_quota_dump
858*ef9659baSdrh */
859*ef9659baSdrh static int test_quota_dump(
860*ef9659baSdrh   void * clientData,
861*ef9659baSdrh   Tcl_Interp *interp,
862*ef9659baSdrh   int objc,
863*ef9659baSdrh   Tcl_Obj *CONST objv[]
864*ef9659baSdrh ){
865*ef9659baSdrh   Tcl_Obj *pResult;
866*ef9659baSdrh   Tcl_Obj *pGroupTerm;
867*ef9659baSdrh   Tcl_Obj *pFileTerm;
868*ef9659baSdrh   quotaGroup *pGroup;
869*ef9659baSdrh   quotaFile *pFile;
870*ef9659baSdrh 
871*ef9659baSdrh   pResult = Tcl_NewObj();
872*ef9659baSdrh   quotaEnter();
873*ef9659baSdrh   for(pGroup=gQuota.pGroup; pGroup; pGroup=pGroup->pNext){
874*ef9659baSdrh     pGroupTerm = Tcl_NewObj();
875*ef9659baSdrh     Tcl_ListObjAppendElement(interp, pGroupTerm,
876*ef9659baSdrh           Tcl_NewStringObj(pGroup->zPattern, -1));
877*ef9659baSdrh     Tcl_ListObjAppendElement(interp, pGroupTerm,
878*ef9659baSdrh           Tcl_NewWideIntObj(pGroup->iLimit));
879*ef9659baSdrh     Tcl_ListObjAppendElement(interp, pGroupTerm,
880*ef9659baSdrh           Tcl_NewWideIntObj(pGroup->iSize));
881*ef9659baSdrh     for(pFile=pGroup->pFile; pFile; pFile=pFile->pNext){
882*ef9659baSdrh       pFileTerm = Tcl_NewObj();
883*ef9659baSdrh       Tcl_ListObjAppendElement(interp, pFileTerm,
884*ef9659baSdrh             Tcl_NewStringObj(pFile->zFilename, -1));
885*ef9659baSdrh       Tcl_ListObjAppendElement(interp, pFileTerm,
886*ef9659baSdrh             Tcl_NewWideIntObj(pFile->iSize));
887*ef9659baSdrh       Tcl_ListObjAppendElement(interp, pFileTerm,
888*ef9659baSdrh             Tcl_NewWideIntObj(pFile->nRef));
889*ef9659baSdrh       Tcl_ListObjAppendElement(interp, pGroupTerm, pFileTerm);
890*ef9659baSdrh     }
891*ef9659baSdrh     Tcl_ListObjAppendElement(interp, pResult, pGroupTerm);
892*ef9659baSdrh   }
893*ef9659baSdrh   quotaLeave();
894*ef9659baSdrh   Tcl_SetObjResult(interp, pResult);
895*ef9659baSdrh   return TCL_OK;
896*ef9659baSdrh }
897*ef9659baSdrh 
898*ef9659baSdrh /*
8998cf35eb4Sdan ** This routine registers the custom TCL commands defined in this
9008cf35eb4Sdan ** module.  This should be the only procedure visible from outside
9018cf35eb4Sdan ** of this module.
9028cf35eb4Sdan */
9038cf35eb4Sdan int Sqlitequota_Init(Tcl_Interp *interp){
9048cf35eb4Sdan   static struct {
9058cf35eb4Sdan      char *zName;
9068cf35eb4Sdan      Tcl_ObjCmdProc *xProc;
9078cf35eb4Sdan   } aCmd[] = {
9088cf35eb4Sdan     { "sqlite3_quota_initialize", test_quota_initialize },
9098cf35eb4Sdan     { "sqlite3_quota_shutdown", test_quota_shutdown },
9108cf35eb4Sdan     { "sqlite3_quota_set", test_quota_set },
911*ef9659baSdrh     { "sqlite3_quota_dump", test_quota_dump },
9128cf35eb4Sdan   };
9138cf35eb4Sdan   int i;
9148cf35eb4Sdan 
9158cf35eb4Sdan   for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
9168cf35eb4Sdan     Tcl_CreateObjCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0);
9178cf35eb4Sdan   }
9188cf35eb4Sdan 
9198cf35eb4Sdan   return TCL_OK;
9208cf35eb4Sdan }
9218cf35eb4Sdan #endif
922