xref: /sqlite-3.40.0/src/test_quota.c (revision 3d3e60e4)
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;
38ef9659baSdrh 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() */
66ef9659baSdrh   void (*xDestroy)(void*);       /* Optional destructor for pArg */
678cf35eb4Sdan   quotaGroup *pNext, **ppPrev;   /* Doubly linked list of all quota objects */
6875c19174Sdrh   quotaFile *pFiles;             /* 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 */
8175c19174Sdrh   quotaGroup *pGroup;             /* Quota group to which this file belongs */
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 */
93ef9659baSdrh 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 */
15775c19174Sdrh static void quotaGroupDeref(quotaGroup *pGroup){
15875c19174Sdrh   if( pGroup->pFiles==0 && pGroup->iLimit==0 ){
15975c19174Sdrh     if( pGroup->pNext ) pGroup->pNext->ppPrev = pGroup->ppPrev;
16075c19174Sdrh     if( pGroup->ppPrev ) *pGroup->ppPrev = pGroup->pNext;
16175c19174Sdrh     if( pGroup->xDestroy ) pGroup->xDestroy(pGroup->pArg);
16275c19174Sdrh     sqlite3_free(pGroup);
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 */
18161cc423aSdrh static int quotaStrglob(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=='[' ){
19461cc423aSdrh         while( *z && quotaStrglob(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         }
20461cc423aSdrh         if( quotaStrglob(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 **
248ef9659baSdrh ** Return a pointer to the quotaGroup object. Return NULL if not found.
2498cf35eb4Sdan */
2508cf35eb4Sdan static quotaGroup *quotaGroupFind(const char *zFilename){
2518cf35eb4Sdan   quotaGroup *p;
25261cc423aSdrh   for(p=gQuota.pGroup; p && quotaStrglob(p->zPattern, zFilename)==0;
25361cc423aSdrh       p=p->pNext){}
2548cf35eb4Sdan   return p;
2558cf35eb4Sdan }
2568cf35eb4Sdan 
257ef9659baSdrh /* Translate an sqlite3_file* that is really a quotaConn* into
258549dc1baSdrh ** the sqlite3_file* for the underlying original VFS.
2598cf35eb4Sdan */
260ef9659baSdrh static sqlite3_file *quotaSubOpen(sqlite3_file *pConn){
261ef9659baSdrh   quotaConn *p = (quotaConn*)pConn;
2628cf35eb4Sdan   return (sqlite3_file*)&p[1];
2638cf35eb4Sdan }
2648cf35eb4Sdan 
2658cf35eb4Sdan /************************* VFS Method Wrappers *****************************/
2668cf35eb4Sdan /*
2678cf35eb4Sdan ** This is the xOpen method used for the "quota" VFS.
2688cf35eb4Sdan **
2698cf35eb4Sdan ** Most of the work is done by the underlying original VFS.  This method
270549dc1baSdrh ** simply links the new file into the appropriate quota group if it is a
271549dc1baSdrh ** file that needs to be tracked.
2728cf35eb4Sdan */
273ef9659baSdrh static int quotaOpen(
274549dc1baSdrh   sqlite3_vfs *pVfs,          /* The quota VFS */
275549dc1baSdrh   const char *zName,          /* Name of file to be opened */
276ef9659baSdrh   sqlite3_file *pConn,        /* Fill in this file descriptor */
277549dc1baSdrh   int flags,                  /* Flags to control the opening */
278549dc1baSdrh   int *pOutFlags              /* Flags showing results of opening */
2798cf35eb4Sdan ){
280549dc1baSdrh   int rc;                                    /* Result code */
281ef9659baSdrh   quotaConn *pQuotaOpen;                     /* The new quota file descriptor */
282549dc1baSdrh   quotaFile *pFile;                          /* Corresponding quotaFile obj */
283549dc1baSdrh   quotaGroup *pGroup;                        /* The group file belongs to */
284549dc1baSdrh   sqlite3_file *pSubOpen;                    /* Real file descriptor */
285549dc1baSdrh   sqlite3_vfs *pOrigVfs = gQuota.pOrigVfs;   /* Real VFS */
2868cf35eb4Sdan 
2878cf35eb4Sdan   /* If the file is not a main database file or a WAL, then use the
2888cf35eb4Sdan   ** normal xOpen method.
2898cf35eb4Sdan   */
2908cf35eb4Sdan   if( (flags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_WAL))==0 ){
291ef9659baSdrh     return pOrigVfs->xOpen(pOrigVfs, zName, pConn, flags, pOutFlags);
2928cf35eb4Sdan   }
2938cf35eb4Sdan 
2948cf35eb4Sdan   /* If the name of the file does not match any quota group, then
2958cf35eb4Sdan   ** use the normal xOpen method.
2968cf35eb4Sdan   */
2978cf35eb4Sdan   quotaEnter();
2988cf35eb4Sdan   pGroup = quotaGroupFind(zName);
2998cf35eb4Sdan   if( pGroup==0 ){
300ef9659baSdrh     rc = pOrigVfs->xOpen(pOrigVfs, zName, pConn, flags, pOutFlags);
3018cf35eb4Sdan   }else{
3028cf35eb4Sdan     /* If we get to this point, it means the file needs to be quota tracked.
3038cf35eb4Sdan     */
304ef9659baSdrh     pQuotaOpen = (quotaConn*)pConn;
305ef9659baSdrh     pSubOpen = quotaSubOpen(pConn);
306745de2e4Sdrh     rc = pOrigVfs->xOpen(pOrigVfs, zName, pSubOpen, flags, pOutFlags);
3078cf35eb4Sdan     if( rc==SQLITE_OK ){
30875c19174Sdrh       for(pFile=pGroup->pFiles; pFile && strcmp(pFile->zFilename, zName);
309745de2e4Sdrh           pFile=pFile->pNext){}
310745de2e4Sdrh       if( pFile==0 ){
311745de2e4Sdrh         int nName = strlen(zName);
312745de2e4Sdrh         pFile = sqlite3_malloc( sizeof(*pFile) + nName + 1 );
313745de2e4Sdrh         if( pFile==0 ){
314745de2e4Sdrh           quotaLeave();
315745de2e4Sdrh           pSubOpen->pMethods->xClose(pSubOpen);
316745de2e4Sdrh           return SQLITE_NOMEM;
317745de2e4Sdrh         }
318745de2e4Sdrh         memset(pFile, 0, sizeof(*pFile));
319745de2e4Sdrh         pFile->zFilename = (char*)&pFile[1];
320745de2e4Sdrh         memcpy(pFile->zFilename, zName, nName+1);
32175c19174Sdrh         pFile->pNext = pGroup->pFiles;
32275c19174Sdrh         if( pGroup->pFiles ) pGroup->pFiles->ppPrev = &pFile->pNext;
32375c19174Sdrh         pFile->ppPrev = &pGroup->pFiles;
32475c19174Sdrh         pGroup->pFiles = pFile;
325745de2e4Sdrh         pFile->pGroup = pGroup;
326745de2e4Sdrh       }
327745de2e4Sdrh       pFile->nRef++;
328745de2e4Sdrh       pQuotaOpen->pFile = pFile;
329745de2e4Sdrh       if( pSubOpen->pMethods->iVersion==1 ){
330745de2e4Sdrh         pQuotaOpen->base.pMethods = &gQuota.sIoMethodsV1;
3318cf35eb4Sdan       }else{
332745de2e4Sdrh         pQuotaOpen->base.pMethods = &gQuota.sIoMethodsV2;
3338cf35eb4Sdan       }
3348cf35eb4Sdan     }
3358cf35eb4Sdan   }
3368cf35eb4Sdan   quotaLeave();
3378cf35eb4Sdan   return rc;
3388cf35eb4Sdan }
3398cf35eb4Sdan 
3408cf35eb4Sdan /************************ I/O Method Wrappers *******************************/
3418cf35eb4Sdan 
3428cf35eb4Sdan /* xClose requests get passed through to the original VFS.  But we
343ef9659baSdrh ** also have to unlink the quotaConn from the quotaFile and quotaGroup.
344ef9659baSdrh ** The quotaFile and/or quotaGroup are freed if they are no longer in use.
3458cf35eb4Sdan */
346ef9659baSdrh static int quotaClose(sqlite3_file *pConn){
347ef9659baSdrh   quotaConn *p = (quotaConn*)pConn;
348745de2e4Sdrh   quotaFile *pFile = p->pFile;
349ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
3508cf35eb4Sdan   int rc;
351745de2e4Sdrh   rc = pSubOpen->pMethods->xClose(pSubOpen);
3528cf35eb4Sdan   quotaEnter();
353745de2e4Sdrh   pFile->nRef--;
354745de2e4Sdrh   if( pFile->nRef==0 ){
355745de2e4Sdrh     quotaGroup *pGroup = pFile->pGroup;
356745de2e4Sdrh     pGroup->iSize -= pFile->iSize;
357745de2e4Sdrh     if( pFile->pNext ) pFile->pNext->ppPrev = pFile->ppPrev;
358745de2e4Sdrh     *pFile->ppPrev = pFile->pNext;
3598cf35eb4Sdan     quotaGroupDeref(pGroup);
360745de2e4Sdrh     sqlite3_free(pFile);
361745de2e4Sdrh   }
3628cf35eb4Sdan   quotaLeave();
3638cf35eb4Sdan   return rc;
3648cf35eb4Sdan }
3658cf35eb4Sdan 
3668cf35eb4Sdan /* Pass xRead requests directory thru to the original VFS without
3678cf35eb4Sdan ** further processing.
3688cf35eb4Sdan */
3698cf35eb4Sdan static int quotaRead(
370ef9659baSdrh   sqlite3_file *pConn,
3718cf35eb4Sdan   void *pBuf,
3728cf35eb4Sdan   int iAmt,
3738cf35eb4Sdan   sqlite3_int64 iOfst
3748cf35eb4Sdan ){
375ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
376745de2e4Sdrh   return pSubOpen->pMethods->xRead(pSubOpen, pBuf, iAmt, iOfst);
3778cf35eb4Sdan }
3788cf35eb4Sdan 
3798cf35eb4Sdan /* Check xWrite requests to see if they expand the file.  If they do,
3808cf35eb4Sdan ** the perform a quota check before passing them through to the
3818cf35eb4Sdan ** original VFS.
3828cf35eb4Sdan */
3838cf35eb4Sdan static int quotaWrite(
384ef9659baSdrh   sqlite3_file *pConn,
3858cf35eb4Sdan   const void *pBuf,
3868cf35eb4Sdan   int iAmt,
3878cf35eb4Sdan   sqlite3_int64 iOfst
3888cf35eb4Sdan ){
389ef9659baSdrh   quotaConn *p = (quotaConn*)pConn;
390ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
3918cf35eb4Sdan   sqlite3_int64 iEnd = iOfst+iAmt;
3928cf35eb4Sdan   quotaGroup *pGroup;
393745de2e4Sdrh   quotaFile *pFile = p->pFile;
3948cf35eb4Sdan   sqlite3_int64 szNew;
3958cf35eb4Sdan 
396745de2e4Sdrh   if( pFile->iSize<iEnd ){
397745de2e4Sdrh     pGroup = pFile->pGroup;
3988cf35eb4Sdan     quotaEnter();
399745de2e4Sdrh     szNew = pGroup->iSize - pFile->iSize + iEnd;
4008cf35eb4Sdan     if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){
4018cf35eb4Sdan       if( pGroup->xCallback ){
402745de2e4Sdrh         pGroup->xCallback(pFile->zFilename, &pGroup->iLimit, szNew,
4038cf35eb4Sdan                           pGroup->pArg);
4048cf35eb4Sdan       }
4058cf35eb4Sdan       if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){
4068cf35eb4Sdan         quotaLeave();
4078cf35eb4Sdan         return SQLITE_FULL;
4088cf35eb4Sdan       }
4098cf35eb4Sdan     }
4108cf35eb4Sdan     pGroup->iSize = szNew;
411745de2e4Sdrh     pFile->iSize = iEnd;
4128cf35eb4Sdan     quotaLeave();
4138cf35eb4Sdan   }
414745de2e4Sdrh   return pSubOpen->pMethods->xWrite(pSubOpen, pBuf, iAmt, iOfst);
4158cf35eb4Sdan }
4168cf35eb4Sdan 
4178cf35eb4Sdan /* Pass xTruncate requests thru to the original VFS.  If the
4188cf35eb4Sdan ** success, update the file size.
4198cf35eb4Sdan */
420ef9659baSdrh static int quotaTruncate(sqlite3_file *pConn, sqlite3_int64 size){
421ef9659baSdrh   quotaConn *p = (quotaConn*)pConn;
422ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
423745de2e4Sdrh   int rc = pSubOpen->pMethods->xTruncate(pSubOpen, size);
424745de2e4Sdrh   quotaFile *pFile = p->pFile;
425745de2e4Sdrh   quotaGroup *pGroup;
4268cf35eb4Sdan   if( rc==SQLITE_OK ){
4278cf35eb4Sdan     quotaEnter();
428745de2e4Sdrh     pGroup = pFile->pGroup;
429745de2e4Sdrh     pGroup->iSize -= pFile->iSize;
430745de2e4Sdrh     pFile->iSize = size;
4318cf35eb4Sdan     pGroup->iSize += size;
4328cf35eb4Sdan     quotaLeave();
4338cf35eb4Sdan   }
4348cf35eb4Sdan   return rc;
4358cf35eb4Sdan }
4368cf35eb4Sdan 
4378cf35eb4Sdan /* Pass xSync requests through to the original VFS without change
4388cf35eb4Sdan */
439ef9659baSdrh static int quotaSync(sqlite3_file *pConn, int flags){
440ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
441745de2e4Sdrh   return pSubOpen->pMethods->xSync(pSubOpen, flags);
4428cf35eb4Sdan }
4438cf35eb4Sdan 
4448cf35eb4Sdan /* Pass xFileSize requests through to the original VFS but then
4458cf35eb4Sdan ** update the quotaGroup with the new size before returning.
4468cf35eb4Sdan */
447ef9659baSdrh static int quotaFileSize(sqlite3_file *pConn, sqlite3_int64 *pSize){
448ef9659baSdrh   quotaConn *p = (quotaConn*)pConn;
449ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
450745de2e4Sdrh   quotaFile *pFile = p->pFile;
4518cf35eb4Sdan   quotaGroup *pGroup;
4528cf35eb4Sdan   sqlite3_int64 sz;
4538cf35eb4Sdan   int rc;
4548cf35eb4Sdan 
455745de2e4Sdrh   rc = pSubOpen->pMethods->xFileSize(pSubOpen, &sz);
4568cf35eb4Sdan   if( rc==SQLITE_OK ){
4578cf35eb4Sdan     quotaEnter();
458745de2e4Sdrh     pGroup = pFile->pGroup;
459745de2e4Sdrh     pGroup->iSize -= pFile->iSize;
460745de2e4Sdrh     pFile->iSize = sz;
4618cf35eb4Sdan     pGroup->iSize += sz;
4628cf35eb4Sdan     quotaLeave();
4638cf35eb4Sdan     *pSize = sz;
4648cf35eb4Sdan   }
4658cf35eb4Sdan   return rc;
4668cf35eb4Sdan }
4678cf35eb4Sdan 
4688cf35eb4Sdan /* Pass xLock requests through to the original VFS unchanged.
4698cf35eb4Sdan */
470ef9659baSdrh static int quotaLock(sqlite3_file *pConn, int lock){
471ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
472745de2e4Sdrh   return pSubOpen->pMethods->xLock(pSubOpen, lock);
4738cf35eb4Sdan }
4748cf35eb4Sdan 
4758cf35eb4Sdan /* Pass xUnlock requests through to the original VFS unchanged.
4768cf35eb4Sdan */
477ef9659baSdrh static int quotaUnlock(sqlite3_file *pConn, int lock){
478ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
479745de2e4Sdrh   return pSubOpen->pMethods->xUnlock(pSubOpen, lock);
4808cf35eb4Sdan }
4818cf35eb4Sdan 
4828cf35eb4Sdan /* Pass xCheckReservedLock requests through to the original VFS unchanged.
4838cf35eb4Sdan */
484ef9659baSdrh static int quotaCheckReservedLock(sqlite3_file *pConn, int *pResOut){
485ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
486745de2e4Sdrh   return pSubOpen->pMethods->xCheckReservedLock(pSubOpen, pResOut);
4878cf35eb4Sdan }
4888cf35eb4Sdan 
4898cf35eb4Sdan /* Pass xFileControl requests through to the original VFS unchanged.
4908cf35eb4Sdan */
491ef9659baSdrh static int quotaFileControl(sqlite3_file *pConn, int op, void *pArg){
492ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
493745de2e4Sdrh   return pSubOpen->pMethods->xFileControl(pSubOpen, op, pArg);
4948cf35eb4Sdan }
4958cf35eb4Sdan 
4968cf35eb4Sdan /* Pass xSectorSize requests through to the original VFS unchanged.
4978cf35eb4Sdan */
498ef9659baSdrh static int quotaSectorSize(sqlite3_file *pConn){
499ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
500745de2e4Sdrh   return pSubOpen->pMethods->xSectorSize(pSubOpen);
5018cf35eb4Sdan }
5028cf35eb4Sdan 
5038cf35eb4Sdan /* Pass xDeviceCharacteristics requests through to the original VFS unchanged.
5048cf35eb4Sdan */
505ef9659baSdrh static int quotaDeviceCharacteristics(sqlite3_file *pConn){
506ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
507745de2e4Sdrh   return pSubOpen->pMethods->xDeviceCharacteristics(pSubOpen);
5088cf35eb4Sdan }
5098cf35eb4Sdan 
5108cf35eb4Sdan /* Pass xShmMap requests through to the original VFS unchanged.
5118cf35eb4Sdan */
5128cf35eb4Sdan static int quotaShmMap(
513ef9659baSdrh   sqlite3_file *pConn,            /* Handle open on database file */
5148cf35eb4Sdan   int iRegion,                    /* Region to retrieve */
5158cf35eb4Sdan   int szRegion,                   /* Size of regions */
5168cf35eb4Sdan   int bExtend,                    /* True to extend file if necessary */
5178cf35eb4Sdan   void volatile **pp              /* OUT: Mapped memory */
5188cf35eb4Sdan ){
519ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
520745de2e4Sdrh   return pSubOpen->pMethods->xShmMap(pSubOpen, iRegion, szRegion, bExtend, pp);
5218cf35eb4Sdan }
5228cf35eb4Sdan 
5238cf35eb4Sdan /* Pass xShmLock requests through to the original VFS unchanged.
5248cf35eb4Sdan */
5258cf35eb4Sdan static int quotaShmLock(
526ef9659baSdrh   sqlite3_file *pConn,       /* Database file holding the shared memory */
5278cf35eb4Sdan   int ofst,                  /* First lock to acquire or release */
5288cf35eb4Sdan   int n,                     /* Number of locks to acquire or release */
5298cf35eb4Sdan   int flags                  /* What to do with the lock */
5308cf35eb4Sdan ){
531ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
532745de2e4Sdrh   return pSubOpen->pMethods->xShmLock(pSubOpen, ofst, n, flags);
5338cf35eb4Sdan }
5348cf35eb4Sdan 
5358cf35eb4Sdan /* Pass xShmBarrier requests through to the original VFS unchanged.
5368cf35eb4Sdan */
537ef9659baSdrh static void quotaShmBarrier(sqlite3_file *pConn){
538ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
539745de2e4Sdrh   pSubOpen->pMethods->xShmBarrier(pSubOpen);
5408cf35eb4Sdan }
5418cf35eb4Sdan 
5428cf35eb4Sdan /* Pass xShmUnmap requests through to the original VFS unchanged.
5438cf35eb4Sdan */
544ef9659baSdrh static int quotaShmUnmap(sqlite3_file *pConn, int deleteFlag){
545ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
546745de2e4Sdrh   return pSubOpen->pMethods->xShmUnmap(pSubOpen, deleteFlag);
5478cf35eb4Sdan }
5488cf35eb4Sdan 
5498cf35eb4Sdan /************************** Public Interfaces *****************************/
5508cf35eb4Sdan /*
5518cf35eb4Sdan ** Initialize the quota VFS shim.  Use the VFS named zOrigVfsName
5528cf35eb4Sdan ** as the VFS that does the actual work.  Use the default if
5538cf35eb4Sdan ** zOrigVfsName==NULL.
5548cf35eb4Sdan **
5558cf35eb4Sdan ** The quota VFS shim is named "quota".  It will become the default
5568cf35eb4Sdan ** VFS if makeDefault is non-zero.
5578cf35eb4Sdan **
5588cf35eb4Sdan ** THIS ROUTINE IS NOT THREADSAFE.  Call this routine exactly once
5598cf35eb4Sdan ** during start-up.
5608cf35eb4Sdan */
5618cf35eb4Sdan int sqlite3_quota_initialize(const char *zOrigVfsName, int makeDefault){
5628cf35eb4Sdan   sqlite3_vfs *pOrigVfs;
5638cf35eb4Sdan   if( gQuota.isInitialized ) return SQLITE_MISUSE;
5648cf35eb4Sdan   pOrigVfs = sqlite3_vfs_find(zOrigVfsName);
5658cf35eb4Sdan   if( pOrigVfs==0 ) return SQLITE_ERROR;
5668cf35eb4Sdan   assert( pOrigVfs!=&gQuota.sThisVfs );
5678cf35eb4Sdan   gQuota.pMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
5688cf35eb4Sdan   if( !gQuota.pMutex ){
5698cf35eb4Sdan     return SQLITE_NOMEM;
5708cf35eb4Sdan   }
5718cf35eb4Sdan   gQuota.isInitialized = 1;
5728cf35eb4Sdan   gQuota.pOrigVfs = pOrigVfs;
5738cf35eb4Sdan   gQuota.sThisVfs = *pOrigVfs;
574ef9659baSdrh   gQuota.sThisVfs.xOpen = quotaOpen;
575ef9659baSdrh   gQuota.sThisVfs.szOsFile += sizeof(quotaConn);
5768cf35eb4Sdan   gQuota.sThisVfs.zName = "quota";
5778cf35eb4Sdan   gQuota.sIoMethodsV1.iVersion = 1;
5788cf35eb4Sdan   gQuota.sIoMethodsV1.xClose = quotaClose;
5798cf35eb4Sdan   gQuota.sIoMethodsV1.xRead = quotaRead;
5808cf35eb4Sdan   gQuota.sIoMethodsV1.xWrite = quotaWrite;
5818cf35eb4Sdan   gQuota.sIoMethodsV1.xTruncate = quotaTruncate;
5828cf35eb4Sdan   gQuota.sIoMethodsV1.xSync = quotaSync;
5838cf35eb4Sdan   gQuota.sIoMethodsV1.xFileSize = quotaFileSize;
5848cf35eb4Sdan   gQuota.sIoMethodsV1.xLock = quotaLock;
5858cf35eb4Sdan   gQuota.sIoMethodsV1.xUnlock = quotaUnlock;
5868cf35eb4Sdan   gQuota.sIoMethodsV1.xCheckReservedLock = quotaCheckReservedLock;
587ef9659baSdrh   gQuota.sIoMethodsV1.xFileControl = quotaFileControl;
5888cf35eb4Sdan   gQuota.sIoMethodsV1.xSectorSize = quotaSectorSize;
5898cf35eb4Sdan   gQuota.sIoMethodsV1.xDeviceCharacteristics = quotaDeviceCharacteristics;
5908cf35eb4Sdan   gQuota.sIoMethodsV2 = gQuota.sIoMethodsV1;
5918cf35eb4Sdan   gQuota.sIoMethodsV2.iVersion = 2;
5928cf35eb4Sdan   gQuota.sIoMethodsV2.xShmMap = quotaShmMap;
5938cf35eb4Sdan   gQuota.sIoMethodsV2.xShmLock = quotaShmLock;
5948cf35eb4Sdan   gQuota.sIoMethodsV2.xShmBarrier = quotaShmBarrier;
5958cf35eb4Sdan   gQuota.sIoMethodsV2.xShmUnmap = quotaShmUnmap;
5968cf35eb4Sdan   sqlite3_vfs_register(&gQuota.sThisVfs, makeDefault);
5978cf35eb4Sdan   return SQLITE_OK;
5988cf35eb4Sdan }
5998cf35eb4Sdan 
6008cf35eb4Sdan /*
6018cf35eb4Sdan ** Shutdown the quota system.
6028cf35eb4Sdan **
6038cf35eb4Sdan ** All SQLite database connections must be closed before calling this
6048cf35eb4Sdan ** routine.
6058cf35eb4Sdan **
6068cf35eb4Sdan ** THIS ROUTINE IS NOT THREADSAFE.  Call this routine exactly one while
6078cf35eb4Sdan ** shutting down in order to free all remaining quota groups.
6088cf35eb4Sdan */
6098cf35eb4Sdan int sqlite3_quota_shutdown(void){
61075c19174Sdrh   quotaGroup *pGroup;
6118cf35eb4Sdan   if( gQuota.isInitialized==0 ) return SQLITE_MISUSE;
61275c19174Sdrh   for(pGroup=gQuota.pGroup; pGroup; pGroup=pGroup->pNext){
61375c19174Sdrh     if( pGroup->pFiles ) return SQLITE_MISUSE;
6148cf35eb4Sdan   }
6158cf35eb4Sdan   while( gQuota.pGroup ){
61675c19174Sdrh     pGroup = gQuota.pGroup;
61775c19174Sdrh     gQuota.pGroup = pGroup->pNext;
61875c19174Sdrh     sqlite3_free(pGroup);
6198cf35eb4Sdan   }
6208cf35eb4Sdan   gQuota.isInitialized = 0;
6218cf35eb4Sdan   sqlite3_mutex_free(gQuota.pMutex);
6228cf35eb4Sdan   sqlite3_vfs_unregister(&gQuota.sThisVfs);
6238cf35eb4Sdan   memset(&gQuota, 0, sizeof(gQuota));
6248cf35eb4Sdan   return SQLITE_OK;
6258cf35eb4Sdan }
6268cf35eb4Sdan 
6278cf35eb4Sdan /*
6288cf35eb4Sdan ** Create or destroy a quota group.
6298cf35eb4Sdan **
6308cf35eb4Sdan ** The quota group is defined by the zPattern.  When calling this routine
6318cf35eb4Sdan ** with a zPattern for a quota group that already exists, this routine
6328cf35eb4Sdan ** merely updates the iLimit, xCallback, and pArg values for that quota
6338cf35eb4Sdan ** group.  If zPattern is new, then a new quota group is created.
6348cf35eb4Sdan **
6358cf35eb4Sdan ** If the iLimit for a quota group is set to zero, then the quota group
6368cf35eb4Sdan ** is disabled and will be deleted when the last database connection using
6378cf35eb4Sdan ** the quota group is closed.
6388cf35eb4Sdan **
6398cf35eb4Sdan ** Calling this routine on a zPattern that does not exist and with a
6408cf35eb4Sdan ** zero iLimit is a no-op.
6418cf35eb4Sdan **
6428cf35eb4Sdan ** A quota group must exist with a non-zero iLimit prior to opening
6438cf35eb4Sdan ** database connections if those connections are to participate in the
6448cf35eb4Sdan ** quota group.  Creating a quota group does not affect database connections
6458cf35eb4Sdan ** that are already open.
6468cf35eb4Sdan */
6478cf35eb4Sdan int sqlite3_quota_set(
6488cf35eb4Sdan   const char *zPattern,           /* The filename pattern */
6498cf35eb4Sdan   sqlite3_int64 iLimit,           /* New quota to set for this quota group */
6508cf35eb4Sdan   void (*xCallback)(              /* Callback invoked when going over quota */
6518cf35eb4Sdan      const char *zFilename,         /* Name of file whose size increases */
6528cf35eb4Sdan      sqlite3_int64 *piLimit,        /* IN/OUT: The current limit */
6538cf35eb4Sdan      sqlite3_int64 iSize,           /* Total size of all files in the group */
6548cf35eb4Sdan      void *pArg                     /* Client data */
6558cf35eb4Sdan   ),
656ef9659baSdrh   void *pArg,                     /* client data passed thru to callback */
657ef9659baSdrh   void (*xDestroy)(void*)         /* Optional destructor for pArg */
6588cf35eb4Sdan ){
6598cf35eb4Sdan   quotaGroup *pGroup;
6608cf35eb4Sdan   quotaEnter();
6618cf35eb4Sdan   pGroup = gQuota.pGroup;
6628cf35eb4Sdan   while( pGroup && strcmp(pGroup->zPattern, zPattern)!=0 ){
6638cf35eb4Sdan     pGroup = pGroup->pNext;
6648cf35eb4Sdan   }
66561cc423aSdrh   if( pGroup==0 ){
6668cf35eb4Sdan     int nPattern = strlen(zPattern);
66761cc423aSdrh     if( iLimit<=0 ){
66861cc423aSdrh       quotaLeave();
66961cc423aSdrh       return SQLITE_OK;
67061cc423aSdrh     }
6718cf35eb4Sdan     pGroup = sqlite3_malloc( sizeof(*pGroup) + nPattern + 1 );
6728cf35eb4Sdan     if( pGroup==0 ){
6738cf35eb4Sdan       quotaLeave();
6748cf35eb4Sdan       return SQLITE_NOMEM;
6758cf35eb4Sdan     }
6768cf35eb4Sdan     memset(pGroup, 0, sizeof(*pGroup));
6778cf35eb4Sdan     pGroup->zPattern = (char*)&pGroup[1];
6788cf35eb4Sdan     memcpy((char *)pGroup->zPattern, zPattern, nPattern+1);
6798cf35eb4Sdan     if( gQuota.pGroup ) gQuota.pGroup->ppPrev = &pGroup->pNext;
6808cf35eb4Sdan     pGroup->pNext = gQuota.pGroup;
6818cf35eb4Sdan     pGroup->ppPrev = &gQuota.pGroup;
6828cf35eb4Sdan     gQuota.pGroup = pGroup;
6838cf35eb4Sdan   }
6848cf35eb4Sdan   pGroup->iLimit = iLimit;
6858cf35eb4Sdan   pGroup->xCallback = xCallback;
686ef9659baSdrh   if( pGroup->xDestroy && pGroup->pArg!=pArg ){
687ef9659baSdrh     pGroup->xDestroy(pGroup->pArg);
688ef9659baSdrh   }
6898cf35eb4Sdan   pGroup->pArg = pArg;
690ef9659baSdrh   pGroup->xDestroy = xDestroy;
6918cf35eb4Sdan   quotaGroupDeref(pGroup);
6928cf35eb4Sdan   quotaLeave();
6938cf35eb4Sdan   return SQLITE_OK;
6948cf35eb4Sdan }
6958cf35eb4Sdan 
6968cf35eb4Sdan 
6978cf35eb4Sdan /***************************** Test Code ***********************************/
6988cf35eb4Sdan #ifdef SQLITE_TEST
6998cf35eb4Sdan #include <tcl.h>
7008cf35eb4Sdan 
701ef9659baSdrh /*
702ef9659baSdrh ** Argument passed to a TCL quota-over-limit callback.
703ef9659baSdrh */
7048cf35eb4Sdan typedef struct TclQuotaCallback TclQuotaCallback;
7058cf35eb4Sdan struct TclQuotaCallback {
706ef9659baSdrh   Tcl_Interp *interp;    /* Interpreter in which to run the script */
707ef9659baSdrh   Tcl_Obj *pScript;      /* Script to be run */
7088cf35eb4Sdan };
7098cf35eb4Sdan 
7108cf35eb4Sdan extern const char *sqlite3TestErrorName(int);
7118cf35eb4Sdan 
712ef9659baSdrh 
713ef9659baSdrh /*
714ef9659baSdrh ** This is the callback from a quota-over-limit.
715ef9659baSdrh */
7168cf35eb4Sdan static void tclQuotaCallback(
7178cf35eb4Sdan   const char *zFilename,          /* Name of file whose size increases */
7188cf35eb4Sdan   sqlite3_int64 *piLimit,         /* IN/OUT: The current limit */
7198cf35eb4Sdan   sqlite3_int64 iSize,            /* Total size of all files in the group */
7208cf35eb4Sdan   void *pArg                      /* Client data */
7218cf35eb4Sdan ){
7228cf35eb4Sdan   TclQuotaCallback *p;            /* Callback script object */
7238cf35eb4Sdan   Tcl_Obj *pEval;                 /* Script to evaluate */
7248cf35eb4Sdan   Tcl_Obj *pVarname;              /* Name of variable to pass as 2nd arg */
7258cf35eb4Sdan   unsigned int rnd;               /* Random part of pVarname */
7268cf35eb4Sdan   int rc;                         /* Tcl error code */
7278cf35eb4Sdan 
7288cf35eb4Sdan   p = (TclQuotaCallback *)pArg;
729*3d3e60e4Sdrh   if( p==0 ) return;
7308cf35eb4Sdan 
7318cf35eb4Sdan   pVarname = Tcl_NewStringObj("::piLimit_", -1);
7328cf35eb4Sdan   Tcl_IncrRefCount(pVarname);
7338cf35eb4Sdan   sqlite3_randomness(sizeof(rnd), (void *)&rnd);
7348cf35eb4Sdan   Tcl_AppendObjToObj(pVarname, Tcl_NewIntObj((int)(rnd&0x7FFFFFFF)));
7358cf35eb4Sdan   Tcl_ObjSetVar2(p->interp, pVarname, 0, Tcl_NewWideIntObj(*piLimit), 0);
7368cf35eb4Sdan 
7378cf35eb4Sdan   pEval = Tcl_DuplicateObj(p->pScript);
7388cf35eb4Sdan   Tcl_IncrRefCount(pEval);
7398cf35eb4Sdan   Tcl_ListObjAppendElement(0, pEval, Tcl_NewStringObj(zFilename, -1));
7408cf35eb4Sdan   Tcl_ListObjAppendElement(0, pEval, pVarname);
7418cf35eb4Sdan   Tcl_ListObjAppendElement(0, pEval, Tcl_NewWideIntObj(iSize));
7428cf35eb4Sdan   rc = Tcl_EvalObjEx(p->interp, pEval, TCL_EVAL_GLOBAL);
7438cf35eb4Sdan 
7448cf35eb4Sdan   if( rc==TCL_OK ){
7458cf35eb4Sdan     Tcl_Obj *pLimit = Tcl_ObjGetVar2(p->interp, pVarname, 0, 0);
7468cf35eb4Sdan     rc = Tcl_GetWideIntFromObj(p->interp, pLimit, piLimit);
7478cf35eb4Sdan     Tcl_UnsetVar(p->interp, Tcl_GetString(pVarname), 0);
7488cf35eb4Sdan   }
7498cf35eb4Sdan 
7508cf35eb4Sdan   Tcl_DecrRefCount(pEval);
7518cf35eb4Sdan   Tcl_DecrRefCount(pVarname);
7528cf35eb4Sdan   if( rc!=TCL_OK ) Tcl_BackgroundError(p->interp);
7538cf35eb4Sdan }
7548cf35eb4Sdan 
7558cf35eb4Sdan /*
756ef9659baSdrh ** Destructor for a TCL quota-over-limit callback.
757ef9659baSdrh */
758ef9659baSdrh static void tclCallbackDestructor(void *pObj){
759ef9659baSdrh   TclQuotaCallback *p = (TclQuotaCallback*)pObj;
760ef9659baSdrh   if( p ){
761ef9659baSdrh     Tcl_DecrRefCount(p->pScript);
762ef9659baSdrh     ckfree((char *)p);
763ef9659baSdrh   }
764ef9659baSdrh }
765ef9659baSdrh 
766ef9659baSdrh /*
7678cf35eb4Sdan ** tclcmd: sqlite3_quota_initialize NAME MAKEDEFAULT
7688cf35eb4Sdan */
7698cf35eb4Sdan static int test_quota_initialize(
7708cf35eb4Sdan   void * clientData,
7718cf35eb4Sdan   Tcl_Interp *interp,
7728cf35eb4Sdan   int objc,
7738cf35eb4Sdan   Tcl_Obj *CONST objv[]
7748cf35eb4Sdan ){
7758cf35eb4Sdan   const char *zName;              /* Name of new quota VFS */
7768cf35eb4Sdan   int makeDefault;                /* True to make the new VFS the default */
7778cf35eb4Sdan   int rc;                         /* Value returned by quota_initialize() */
7788cf35eb4Sdan 
7798cf35eb4Sdan   /* Process arguments */
7808cf35eb4Sdan   if( objc!=3 ){
7818cf35eb4Sdan     Tcl_WrongNumArgs(interp, 1, objv, "NAME MAKEDEFAULT");
7828cf35eb4Sdan     return TCL_ERROR;
7838cf35eb4Sdan   }
7848cf35eb4Sdan   zName = Tcl_GetString(objv[1]);
7858cf35eb4Sdan   if( Tcl_GetBooleanFromObj(interp, objv[2], &makeDefault) ) return TCL_ERROR;
7868cf35eb4Sdan   if( zName[0]=='\0' ) zName = 0;
7878cf35eb4Sdan 
7888cf35eb4Sdan   /* Call sqlite3_quota_initialize() */
7898cf35eb4Sdan   rc = sqlite3_quota_initialize(zName, makeDefault);
7908cf35eb4Sdan   Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC);
7918cf35eb4Sdan 
7928cf35eb4Sdan   return TCL_OK;
7938cf35eb4Sdan }
7948cf35eb4Sdan 
7958cf35eb4Sdan /*
7968cf35eb4Sdan ** tclcmd: sqlite3_quota_shutdown
7978cf35eb4Sdan */
7988cf35eb4Sdan static int test_quota_shutdown(
7998cf35eb4Sdan   void * clientData,
8008cf35eb4Sdan   Tcl_Interp *interp,
8018cf35eb4Sdan   int objc,
8028cf35eb4Sdan   Tcl_Obj *CONST objv[]
8038cf35eb4Sdan ){
8048cf35eb4Sdan   int rc;                         /* Value returned by quota_shutdown() */
8058cf35eb4Sdan 
8068cf35eb4Sdan   if( objc!=1 ){
8078cf35eb4Sdan     Tcl_WrongNumArgs(interp, 1, objv, "");
8088cf35eb4Sdan     return TCL_ERROR;
8098cf35eb4Sdan   }
8108cf35eb4Sdan 
8118cf35eb4Sdan   /* Call sqlite3_quota_shutdown() */
8128cf35eb4Sdan   rc = sqlite3_quota_shutdown();
8138cf35eb4Sdan   Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC);
8148cf35eb4Sdan 
8158cf35eb4Sdan   return TCL_OK;
8168cf35eb4Sdan }
8178cf35eb4Sdan 
8188cf35eb4Sdan /*
8198cf35eb4Sdan ** tclcmd: sqlite3_quota_set PATTERN LIMIT SCRIPT
8208cf35eb4Sdan */
8218cf35eb4Sdan static int test_quota_set(
8228cf35eb4Sdan   void * clientData,
8238cf35eb4Sdan   Tcl_Interp *interp,
8248cf35eb4Sdan   int objc,
8258cf35eb4Sdan   Tcl_Obj *CONST objv[]
8268cf35eb4Sdan ){
8278cf35eb4Sdan   const char *zPattern;           /* File pattern to configure */
8288cf35eb4Sdan   sqlite3_int64 iLimit;           /* Initial quota in bytes */
8298cf35eb4Sdan   Tcl_Obj *pScript;               /* Tcl script to invoke to increase quota */
8308cf35eb4Sdan   int rc;                         /* Value returned by quota_set() */
8318cf35eb4Sdan   TclQuotaCallback *p;            /* Callback object */
832ef9659baSdrh   int nScript;                    /* Length of callback script */
8338cf35eb4Sdan 
8348cf35eb4Sdan   /* Process arguments */
8358cf35eb4Sdan   if( objc!=4 ){
8368cf35eb4Sdan     Tcl_WrongNumArgs(interp, 1, objv, "PATTERN LIMIT SCRIPT");
8378cf35eb4Sdan     return TCL_ERROR;
8388cf35eb4Sdan   }
8398cf35eb4Sdan   zPattern = Tcl_GetString(objv[1]);
8408cf35eb4Sdan   if( Tcl_GetWideIntFromObj(interp, objv[2], &iLimit) ) return TCL_ERROR;
8418cf35eb4Sdan   pScript = objv[3];
842ef9659baSdrh   Tcl_GetStringFromObj(pScript, &nScript);
8438cf35eb4Sdan 
844ef9659baSdrh   if( nScript>0 ){
8458cf35eb4Sdan     /* Allocate a TclQuotaCallback object */
8468cf35eb4Sdan     p = (TclQuotaCallback *)ckalloc(sizeof(TclQuotaCallback));
8478cf35eb4Sdan     memset(p, 0, sizeof(TclQuotaCallback));
8488cf35eb4Sdan     p->interp = interp;
849ef9659baSdrh     Tcl_IncrRefCount(pScript);
8508cf35eb4Sdan     p->pScript = pScript;
851ef9659baSdrh   }else{
852ef9659baSdrh     p = 0;
8538cf35eb4Sdan   }
854ef9659baSdrh   /* Invoke sqlite3_quota_set() */
855ef9659baSdrh   rc = sqlite3_quota_set(zPattern, iLimit, tclQuotaCallback,
856ef9659baSdrh                          (void*)p, tclCallbackDestructor);
8578cf35eb4Sdan 
8588cf35eb4Sdan   Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC);
8598cf35eb4Sdan   return TCL_OK;
8608cf35eb4Sdan }
8618cf35eb4Sdan 
8628cf35eb4Sdan /*
863ef9659baSdrh ** tclcmd:  sqlite3_quota_dump
864ef9659baSdrh */
865ef9659baSdrh static int test_quota_dump(
866ef9659baSdrh   void * clientData,
867ef9659baSdrh   Tcl_Interp *interp,
868ef9659baSdrh   int objc,
869ef9659baSdrh   Tcl_Obj *CONST objv[]
870ef9659baSdrh ){
871ef9659baSdrh   Tcl_Obj *pResult;
872ef9659baSdrh   Tcl_Obj *pGroupTerm;
873ef9659baSdrh   Tcl_Obj *pFileTerm;
874ef9659baSdrh   quotaGroup *pGroup;
875ef9659baSdrh   quotaFile *pFile;
876ef9659baSdrh 
877ef9659baSdrh   pResult = Tcl_NewObj();
878ef9659baSdrh   quotaEnter();
879ef9659baSdrh   for(pGroup=gQuota.pGroup; pGroup; pGroup=pGroup->pNext){
880ef9659baSdrh     pGroupTerm = Tcl_NewObj();
881ef9659baSdrh     Tcl_ListObjAppendElement(interp, pGroupTerm,
882ef9659baSdrh           Tcl_NewStringObj(pGroup->zPattern, -1));
883ef9659baSdrh     Tcl_ListObjAppendElement(interp, pGroupTerm,
884ef9659baSdrh           Tcl_NewWideIntObj(pGroup->iLimit));
885ef9659baSdrh     Tcl_ListObjAppendElement(interp, pGroupTerm,
886ef9659baSdrh           Tcl_NewWideIntObj(pGroup->iSize));
88775c19174Sdrh     for(pFile=pGroup->pFiles; pFile; pFile=pFile->pNext){
888ef9659baSdrh       pFileTerm = Tcl_NewObj();
889ef9659baSdrh       Tcl_ListObjAppendElement(interp, pFileTerm,
890ef9659baSdrh             Tcl_NewStringObj(pFile->zFilename, -1));
891ef9659baSdrh       Tcl_ListObjAppendElement(interp, pFileTerm,
892ef9659baSdrh             Tcl_NewWideIntObj(pFile->iSize));
893ef9659baSdrh       Tcl_ListObjAppendElement(interp, pFileTerm,
894ef9659baSdrh             Tcl_NewWideIntObj(pFile->nRef));
895ef9659baSdrh       Tcl_ListObjAppendElement(interp, pGroupTerm, pFileTerm);
896ef9659baSdrh     }
897ef9659baSdrh     Tcl_ListObjAppendElement(interp, pResult, pGroupTerm);
898ef9659baSdrh   }
899ef9659baSdrh   quotaLeave();
900ef9659baSdrh   Tcl_SetObjResult(interp, pResult);
901ef9659baSdrh   return TCL_OK;
902ef9659baSdrh }
903ef9659baSdrh 
904ef9659baSdrh /*
9058cf35eb4Sdan ** This routine registers the custom TCL commands defined in this
9068cf35eb4Sdan ** module.  This should be the only procedure visible from outside
9078cf35eb4Sdan ** of this module.
9088cf35eb4Sdan */
9098cf35eb4Sdan int Sqlitequota_Init(Tcl_Interp *interp){
9108cf35eb4Sdan   static struct {
9118cf35eb4Sdan      char *zName;
9128cf35eb4Sdan      Tcl_ObjCmdProc *xProc;
9138cf35eb4Sdan   } aCmd[] = {
9148cf35eb4Sdan     { "sqlite3_quota_initialize", test_quota_initialize },
9158cf35eb4Sdan     { "sqlite3_quota_shutdown", test_quota_shutdown },
9168cf35eb4Sdan     { "sqlite3_quota_set", test_quota_set },
917ef9659baSdrh     { "sqlite3_quota_dump", test_quota_dump },
9188cf35eb4Sdan   };
9198cf35eb4Sdan   int i;
9208cf35eb4Sdan 
9218cf35eb4Sdan   for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
9228cf35eb4Sdan     Tcl_CreateObjCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0);
9238cf35eb4Sdan   }
9248cf35eb4Sdan 
9258cf35eb4Sdan   return TCL_OK;
9268cf35eb4Sdan }
9278cf35eb4Sdan #endif
928