xref: /sqlite-3.40.0/src/test_quota.c (revision b0a32832)
1748a52cdSdrh /*
2748a52cdSdrh ** 2010 September 31
3748a52cdSdrh **
4748a52cdSdrh ** The author disclaims copyright to this source code.  In place of
5748a52cdSdrh ** a legal notice, here is a blessing:
6748a52cdSdrh **
7748a52cdSdrh **    May you do good and not evil.
8748a52cdSdrh **    May you find forgiveness for yourself and forgive others.
9748a52cdSdrh **    May you share freely, never taking more than you give.
10748a52cdSdrh **
11748a52cdSdrh *************************************************************************
12748a52cdSdrh **
13748a52cdSdrh ** This file contains a VFS "shim" - a layer that sits in between the
14748a52cdSdrh ** pager and the real VFS.
15748a52cdSdrh **
16748a52cdSdrh ** This particular shim enforces a quota system on files.  One or more
17748a52cdSdrh ** database files are in a "quota group" that is defined by a GLOB
18748a52cdSdrh ** pattern.  A quota is set for the combined size of all files in the
19748a52cdSdrh ** the group.  A quota of zero means "no limit".  If the total size
209237a258Sdrh ** of all files in the quota group is greater than the limit, then
219237a258Sdrh ** write requests that attempt to enlarge a file fail with SQLITE_FULL.
22748a52cdSdrh **
23748a52cdSdrh ** However, before returning SQLITE_FULL, the write requests invoke
24748a52cdSdrh ** a callback function that is configurable for each quota group.
25748a52cdSdrh ** This callback has the opportunity to enlarge the quota.  If the
26748a52cdSdrh ** callback does enlarge the quota such that the total size of all
27748a52cdSdrh ** files within the group is less than the new quota, then the write
28748a52cdSdrh ** continues as if nothing had happened.
29748a52cdSdrh */
30748a52cdSdrh #include "sqlite3.h"
318cf35eb4Sdan #include <string.h>
328cf35eb4Sdan #include <assert.h>
33748a52cdSdrh 
343778ac15Sdrh /*
353778ac15Sdrh ** For an build without mutexes, no-op the mutex calls.
363778ac15Sdrh */
373778ac15Sdrh #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE==0
383778ac15Sdrh #define sqlite3_mutex_alloc(X)    ((sqlite3_mutex*)8)
393778ac15Sdrh #define sqlite3_mutex_free(X)
403778ac15Sdrh #define sqlite3_mutex_enter(X)
413778ac15Sdrh #define sqlite3_mutex_try(X)      SQLITE_OK
423778ac15Sdrh #define sqlite3_mutex_leave(X)
433778ac15Sdrh #define sqlite3_mutex_held(X)     ((void)(X),1)
443778ac15Sdrh #define sqlite3_mutex_notheld(X)  ((void)(X),1)
453778ac15Sdrh #endif /* SQLITE_THREADSAFE==0 */
463778ac15Sdrh 
473778ac15Sdrh 
48748a52cdSdrh /************************ Object Definitions ******************************/
49748a52cdSdrh 
50745de2e4Sdrh /* Forward declaration of all object types */
51748a52cdSdrh typedef struct quotaGroup quotaGroup;
52ef9659baSdrh typedef struct quotaConn quotaConn;
53745de2e4Sdrh typedef struct quotaFile quotaFile;
54745de2e4Sdrh 
558cf35eb4Sdan /*
56549dc1baSdrh ** A "quota group" is a collection of files whose collective size we want
57549dc1baSdrh ** to limit.  Each quota group is defined by a GLOB pattern.
588cf35eb4Sdan **
59549dc1baSdrh ** There is an instance of the following object for each defined quota
60549dc1baSdrh ** group.  This object records the GLOB pattern that defines which files
61549dc1baSdrh ** belong to the quota group.  The object also remembers the size limit
62549dc1baSdrh ** for the group (the quota) and the callback to be invoked when the
63549dc1baSdrh ** sum of the sizes of the files within the group goes over the limit.
648cf35eb4Sdan **
65549dc1baSdrh ** A quota group must be established (using sqlite3_quota_set(...))
66549dc1baSdrh ** prior to opening any of the database connections that access files
67549dc1baSdrh ** within the quota group.
688cf35eb4Sdan */
69748a52cdSdrh struct quotaGroup {
70748a52cdSdrh   const char *zPattern;          /* Filename pattern to be quotaed */
71748a52cdSdrh   sqlite3_int64 iLimit;          /* Upper bound on total file size */
72748a52cdSdrh   sqlite3_int64 iSize;           /* Current size of all files */
73748a52cdSdrh   void (*xCallback)(             /* Callback invoked when going over quota */
74748a52cdSdrh      const char *zFilename,         /* Name of file whose size increases */
75748a52cdSdrh      sqlite3_int64 *piLimit,        /* IN/OUT: The current limit */
76748a52cdSdrh      sqlite3_int64 iSize,           /* Total size of all files in the group */
77748a52cdSdrh      void *pArg                     /* Client data */
78748a52cdSdrh   );
79748a52cdSdrh   void *pArg;                    /* Third argument to the xCallback() */
80ef9659baSdrh   void (*xDestroy)(void*);       /* Optional destructor for pArg */
81748a52cdSdrh   quotaGroup *pNext, **ppPrev;   /* Doubly linked list of all quota objects */
8275c19174Sdrh   quotaFile *pFiles;             /* Files within this group */
83748a52cdSdrh };
84748a52cdSdrh 
85748a52cdSdrh /*
86745de2e4Sdrh ** An instance of this structure represents a single file that is part
87745de2e4Sdrh ** of a quota group.  A single file can be opened multiple times.  In
88745de2e4Sdrh ** order keep multiple openings of the same file from causing the size
89549dc1baSdrh ** of the file to count against the quota multiple times, each file
90745de2e4Sdrh ** has a unique instance of this object and multiple open connections
91745de2e4Sdrh ** to the same file each point to a single instance of this object.
92748a52cdSdrh */
93748a52cdSdrh struct quotaFile {
94745de2e4Sdrh   char *zFilename;                /* Name of this file */
9575c19174Sdrh   quotaGroup *pGroup;             /* Quota group to which this file belongs */
968cf35eb4Sdan   sqlite3_int64 iSize;            /* Current size of this file */
97745de2e4Sdrh   int nRef;                       /* Number of times this file is open */
98745de2e4Sdrh   quotaFile *pNext, **ppPrev;     /* Linked list of files in the same group */
99745de2e4Sdrh };
100745de2e4Sdrh 
101745de2e4Sdrh /*
102745de2e4Sdrh ** An instance of the following object represents each open connection
103549dc1baSdrh ** to a file that participates in quota tracking.  This object is a
104549dc1baSdrh ** subclass of sqlite3_file.  The sqlite3_file object for the underlying
105549dc1baSdrh ** VFS is appended to this structure.
106745de2e4Sdrh */
107ef9659baSdrh struct quotaConn {
108748a52cdSdrh   sqlite3_file base;              /* Base class - must be first */
109745de2e4Sdrh   quotaFile *pFile;               /* The underlying file */
110748a52cdSdrh   /* The underlying VFS sqlite3_file is appended to this object */
111748a52cdSdrh };
112748a52cdSdrh 
113748a52cdSdrh /************************* Global Variables **********************************/
114748a52cdSdrh /*
115748a52cdSdrh ** All global variables used by this file are containing within the following
116748a52cdSdrh ** gQuota structure.
117748a52cdSdrh */
118748a52cdSdrh static struct {
119549dc1baSdrh   /* The pOrigVfs is the real, original underlying VFS implementation.
120748a52cdSdrh   ** Most operations pass-through to the real VFS.  This value is read-only
121748a52cdSdrh   ** during operation.  It is only modified at start-time and thus does not
122748a52cdSdrh   ** require a mutex.
123748a52cdSdrh   */
124748a52cdSdrh   sqlite3_vfs *pOrigVfs;
125748a52cdSdrh 
126748a52cdSdrh   /* The sThisVfs is the VFS structure used by this shim.  It is initialized
127748a52cdSdrh   ** at start-time and thus does not require a mutex
128748a52cdSdrh   */
129748a52cdSdrh   sqlite3_vfs sThisVfs;
130748a52cdSdrh 
131748a52cdSdrh   /* The sIoMethods defines the methods used by sqlite3_file objects
132748a52cdSdrh   ** associated with this shim.  It is initialized at start-time and does
133748a52cdSdrh   ** not require a mutex.
134748a52cdSdrh   **
135748a52cdSdrh   ** When the underlying VFS is called to open a file, it might return
136748a52cdSdrh   ** either a version 1 or a version 2 sqlite3_file object.  This shim
137748a52cdSdrh   ** has to create a wrapper sqlite3_file of the same version.  Hence
138748a52cdSdrh   ** there are two I/O method structures, one for version 1 and the other
139748a52cdSdrh   ** for version 2.
140748a52cdSdrh   */
141748a52cdSdrh   sqlite3_io_methods sIoMethodsV1;
142748a52cdSdrh   sqlite3_io_methods sIoMethodsV2;
143748a52cdSdrh 
144748a52cdSdrh   /* True when this shim as been initialized.
145748a52cdSdrh   */
146748a52cdSdrh   int isInitialized;
147748a52cdSdrh 
148748a52cdSdrh   /* For run-time access any of the other global data structures in this
149748a52cdSdrh   ** shim, the following mutex must be held.
150748a52cdSdrh   */
151748a52cdSdrh   sqlite3_mutex *pMutex;
152748a52cdSdrh 
153748a52cdSdrh   /* List of quotaGroup objects.
154748a52cdSdrh   */
155748a52cdSdrh   quotaGroup *pGroup;
156748a52cdSdrh 
157748a52cdSdrh } gQuota;
158748a52cdSdrh 
159748a52cdSdrh /************************* Utility Routines *********************************/
160748a52cdSdrh /*
161748a52cdSdrh ** Acquire and release the mutex used to serialize access to the
162748a52cdSdrh ** list of quotaGroups.
163748a52cdSdrh */
164748a52cdSdrh static void quotaEnter(void){ sqlite3_mutex_enter(gQuota.pMutex); }
165748a52cdSdrh static void quotaLeave(void){ sqlite3_mutex_leave(gQuota.pMutex); }
166748a52cdSdrh 
167748a52cdSdrh 
168748a52cdSdrh /* If the reference count and threshold for a quotaGroup are both
169748a52cdSdrh ** zero, then destroy the quotaGroup.
170748a52cdSdrh */
17175c19174Sdrh static void quotaGroupDeref(quotaGroup *pGroup){
17275c19174Sdrh   if( pGroup->pFiles==0 && pGroup->iLimit==0 ){
1739fc7702cSdan     *pGroup->ppPrev = pGroup->pNext;
17475c19174Sdrh     if( pGroup->pNext ) pGroup->pNext->ppPrev = pGroup->ppPrev;
17575c19174Sdrh     if( pGroup->xDestroy ) pGroup->xDestroy(pGroup->pArg);
17675c19174Sdrh     sqlite3_free(pGroup);
177748a52cdSdrh   }
178748a52cdSdrh }
179748a52cdSdrh 
180748a52cdSdrh /*
181748a52cdSdrh ** Return TRUE if string z matches glob pattern zGlob.
182748a52cdSdrh **
183748a52cdSdrh ** Globbing rules:
184748a52cdSdrh **
185748a52cdSdrh **      '*'       Matches any sequence of zero or more characters.
186748a52cdSdrh **
187748a52cdSdrh **      '?'       Matches exactly one character.
188748a52cdSdrh **
189748a52cdSdrh **     [...]      Matches one character from the enclosed list of
190748a52cdSdrh **                characters.
191748a52cdSdrh **
192748a52cdSdrh **     [^...]     Matches one character not in the enclosed list.
193748a52cdSdrh **
194748a52cdSdrh */
19561cc423aSdrh static int quotaStrglob(const char *zGlob, const char *z){
196748a52cdSdrh   int c, c2;
197748a52cdSdrh   int invert;
198748a52cdSdrh   int seen;
199748a52cdSdrh 
200748a52cdSdrh   while( (c = (*(zGlob++)))!=0 ){
201748a52cdSdrh     if( c=='*' ){
202748a52cdSdrh       while( (c=(*(zGlob++))) == '*' || c=='?' ){
203748a52cdSdrh         if( c=='?' && (*(z++))==0 ) return 0;
204748a52cdSdrh       }
205748a52cdSdrh       if( c==0 ){
206748a52cdSdrh         return 1;
207748a52cdSdrh       }else if( c=='[' ){
20861cc423aSdrh         while( *z && quotaStrglob(zGlob-1,z)==0 ){
209748a52cdSdrh           z++;
210748a52cdSdrh         }
211748a52cdSdrh         return (*z)!=0;
212748a52cdSdrh       }
213748a52cdSdrh       while( (c2 = (*(z++)))!=0 ){
214748a52cdSdrh         while( c2!=c ){
215748a52cdSdrh           c2 = *(z++);
216748a52cdSdrh           if( c2==0 ) return 0;
217748a52cdSdrh         }
21861cc423aSdrh         if( quotaStrglob(zGlob,z) ) return 1;
219748a52cdSdrh       }
220748a52cdSdrh       return 0;
221748a52cdSdrh     }else if( c=='?' ){
222748a52cdSdrh       if( (*(z++))==0 ) return 0;
223748a52cdSdrh     }else if( c=='[' ){
224748a52cdSdrh       int prior_c = 0;
225748a52cdSdrh       seen = 0;
226748a52cdSdrh       invert = 0;
227748a52cdSdrh       c = *(z++);
228748a52cdSdrh       if( c==0 ) return 0;
229748a52cdSdrh       c2 = *(zGlob++);
230748a52cdSdrh       if( c2=='^' ){
231748a52cdSdrh         invert = 1;
232748a52cdSdrh         c2 = *(zGlob++);
233748a52cdSdrh       }
234748a52cdSdrh       if( c2==']' ){
235748a52cdSdrh         if( c==']' ) seen = 1;
236748a52cdSdrh         c2 = *(zGlob++);
237748a52cdSdrh       }
238748a52cdSdrh       while( c2 && c2!=']' ){
239748a52cdSdrh         if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
240748a52cdSdrh           c2 = *(zGlob++);
241748a52cdSdrh           if( c>=prior_c && c<=c2 ) seen = 1;
242748a52cdSdrh           prior_c = 0;
243748a52cdSdrh         }else{
244748a52cdSdrh           if( c==c2 ){
245748a52cdSdrh             seen = 1;
246748a52cdSdrh           }
247748a52cdSdrh           prior_c = c2;
248748a52cdSdrh         }
249748a52cdSdrh         c2 = *(zGlob++);
250748a52cdSdrh       }
251748a52cdSdrh       if( c2==0 || (seen ^ invert)==0 ) return 0;
252748a52cdSdrh     }else{
253748a52cdSdrh       if( c!=(*(z++)) ) return 0;
254748a52cdSdrh     }
255748a52cdSdrh   }
256748a52cdSdrh   return *z==0;
257748a52cdSdrh }
258748a52cdSdrh 
259748a52cdSdrh 
260748a52cdSdrh /* Find a quotaGroup given the filename.
2618cf35eb4Sdan **
262ef9659baSdrh ** Return a pointer to the quotaGroup object. Return NULL if not found.
263748a52cdSdrh */
264748a52cdSdrh static quotaGroup *quotaGroupFind(const char *zFilename){
265748a52cdSdrh   quotaGroup *p;
26661cc423aSdrh   for(p=gQuota.pGroup; p && quotaStrglob(p->zPattern, zFilename)==0;
26761cc423aSdrh       p=p->pNext){}
268748a52cdSdrh   return p;
269748a52cdSdrh }
270748a52cdSdrh 
271ef9659baSdrh /* Translate an sqlite3_file* that is really a quotaConn* into
272549dc1baSdrh ** the sqlite3_file* for the underlying original VFS.
273748a52cdSdrh */
274ef9659baSdrh static sqlite3_file *quotaSubOpen(sqlite3_file *pConn){
275ef9659baSdrh   quotaConn *p = (quotaConn*)pConn;
276748a52cdSdrh   return (sqlite3_file*)&p[1];
277748a52cdSdrh }
278748a52cdSdrh 
279748a52cdSdrh /************************* VFS Method Wrappers *****************************/
280748a52cdSdrh /*
281748a52cdSdrh ** This is the xOpen method used for the "quota" VFS.
282748a52cdSdrh **
283748a52cdSdrh ** Most of the work is done by the underlying original VFS.  This method
284549dc1baSdrh ** simply links the new file into the appropriate quota group if it is a
285549dc1baSdrh ** file that needs to be tracked.
286748a52cdSdrh */
287748a52cdSdrh static int quotaOpen(
288549dc1baSdrh   sqlite3_vfs *pVfs,          /* The quota VFS */
289549dc1baSdrh   const char *zName,          /* Name of file to be opened */
290ef9659baSdrh   sqlite3_file *pConn,        /* Fill in this file descriptor */
291549dc1baSdrh   int flags,                  /* Flags to control the opening */
292549dc1baSdrh   int *pOutFlags              /* Flags showing results of opening */
293748a52cdSdrh ){
294549dc1baSdrh   int rc;                                    /* Result code */
295ef9659baSdrh   quotaConn *pQuotaOpen;                     /* The new quota file descriptor */
296549dc1baSdrh   quotaFile *pFile;                          /* Corresponding quotaFile obj */
297549dc1baSdrh   quotaGroup *pGroup;                        /* The group file belongs to */
298549dc1baSdrh   sqlite3_file *pSubOpen;                    /* Real file descriptor */
299549dc1baSdrh   sqlite3_vfs *pOrigVfs = gQuota.pOrigVfs;   /* Real VFS */
300748a52cdSdrh 
301748a52cdSdrh   /* If the file is not a main database file or a WAL, then use the
302748a52cdSdrh   ** normal xOpen method.
303748a52cdSdrh   */
3048cf35eb4Sdan   if( (flags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_WAL))==0 ){
305ef9659baSdrh     return pOrigVfs->xOpen(pOrigVfs, zName, pConn, flags, pOutFlags);
306748a52cdSdrh   }
307748a52cdSdrh 
308748a52cdSdrh   /* If the name of the file does not match any quota group, then
309748a52cdSdrh   ** use the normal xOpen method.
310748a52cdSdrh   */
311748a52cdSdrh   quotaEnter();
3128cf35eb4Sdan   pGroup = quotaGroupFind(zName);
313748a52cdSdrh   if( pGroup==0 ){
314ef9659baSdrh     rc = pOrigVfs->xOpen(pOrigVfs, zName, pConn, flags, pOutFlags);
315748a52cdSdrh   }else{
3168cf35eb4Sdan     /* If we get to this point, it means the file needs to be quota tracked.
3178cf35eb4Sdan     */
318ef9659baSdrh     pQuotaOpen = (quotaConn*)pConn;
319ef9659baSdrh     pSubOpen = quotaSubOpen(pConn);
320745de2e4Sdrh     rc = pOrigVfs->xOpen(pOrigVfs, zName, pSubOpen, flags, pOutFlags);
3218cf35eb4Sdan     if( rc==SQLITE_OK ){
32275c19174Sdrh       for(pFile=pGroup->pFiles; pFile && strcmp(pFile->zFilename, zName);
323745de2e4Sdrh           pFile=pFile->pNext){}
324745de2e4Sdrh       if( pFile==0 ){
325745de2e4Sdrh         int nName = strlen(zName);
326*b0a32832Sdan         pFile = (quotaFile *)sqlite3_malloc( sizeof(*pFile) + nName + 1 );
327745de2e4Sdrh         if( pFile==0 ){
328745de2e4Sdrh           quotaLeave();
329745de2e4Sdrh           pSubOpen->pMethods->xClose(pSubOpen);
330745de2e4Sdrh           return SQLITE_NOMEM;
331745de2e4Sdrh         }
332745de2e4Sdrh         memset(pFile, 0, sizeof(*pFile));
333745de2e4Sdrh         pFile->zFilename = (char*)&pFile[1];
334745de2e4Sdrh         memcpy(pFile->zFilename, zName, nName+1);
33575c19174Sdrh         pFile->pNext = pGroup->pFiles;
33675c19174Sdrh         if( pGroup->pFiles ) pGroup->pFiles->ppPrev = &pFile->pNext;
33775c19174Sdrh         pFile->ppPrev = &pGroup->pFiles;
33875c19174Sdrh         pGroup->pFiles = pFile;
339745de2e4Sdrh         pFile->pGroup = pGroup;
340745de2e4Sdrh       }
341745de2e4Sdrh       pFile->nRef++;
342745de2e4Sdrh       pQuotaOpen->pFile = pFile;
343745de2e4Sdrh       if( pSubOpen->pMethods->iVersion==1 ){
344745de2e4Sdrh         pQuotaOpen->base.pMethods = &gQuota.sIoMethodsV1;
3458cf35eb4Sdan       }else{
346745de2e4Sdrh         pQuotaOpen->base.pMethods = &gQuota.sIoMethodsV2;
347748a52cdSdrh       }
348748a52cdSdrh     }
3498cf35eb4Sdan   }
3508cf35eb4Sdan   quotaLeave();
351748a52cdSdrh   return rc;
352748a52cdSdrh }
353748a52cdSdrh 
354748a52cdSdrh /************************ I/O Method Wrappers *******************************/
355748a52cdSdrh 
356748a52cdSdrh /* xClose requests get passed through to the original VFS.  But we
357ef9659baSdrh ** also have to unlink the quotaConn from the quotaFile and quotaGroup.
358ef9659baSdrh ** The quotaFile and/or quotaGroup are freed if they are no longer in use.
359748a52cdSdrh */
360ef9659baSdrh static int quotaClose(sqlite3_file *pConn){
361ef9659baSdrh   quotaConn *p = (quotaConn*)pConn;
362745de2e4Sdrh   quotaFile *pFile = p->pFile;
363ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
364748a52cdSdrh   int rc;
365745de2e4Sdrh   rc = pSubOpen->pMethods->xClose(pSubOpen);
366748a52cdSdrh   quotaEnter();
367745de2e4Sdrh   pFile->nRef--;
368745de2e4Sdrh   if( pFile->nRef==0 ){
369745de2e4Sdrh     quotaGroup *pGroup = pFile->pGroup;
370745de2e4Sdrh     pGroup->iSize -= pFile->iSize;
371745de2e4Sdrh     if( pFile->pNext ) pFile->pNext->ppPrev = pFile->ppPrev;
372745de2e4Sdrh     *pFile->ppPrev = pFile->pNext;
373748a52cdSdrh     quotaGroupDeref(pGroup);
374745de2e4Sdrh     sqlite3_free(pFile);
375745de2e4Sdrh   }
376748a52cdSdrh   quotaLeave();
377748a52cdSdrh   return rc;
378748a52cdSdrh }
379748a52cdSdrh 
380748a52cdSdrh /* Pass xRead requests directory thru to the original VFS without
381748a52cdSdrh ** further processing.
382748a52cdSdrh */
383748a52cdSdrh static int quotaRead(
384ef9659baSdrh   sqlite3_file *pConn,
385748a52cdSdrh   void *pBuf,
386748a52cdSdrh   int iAmt,
387748a52cdSdrh   sqlite3_int64 iOfst
388748a52cdSdrh ){
389ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
390745de2e4Sdrh   return pSubOpen->pMethods->xRead(pSubOpen, pBuf, iAmt, iOfst);
391748a52cdSdrh }
392748a52cdSdrh 
393748a52cdSdrh /* Check xWrite requests to see if they expand the file.  If they do,
394748a52cdSdrh ** the perform a quota check before passing them through to the
395748a52cdSdrh ** original VFS.
396748a52cdSdrh */
397748a52cdSdrh static int quotaWrite(
398ef9659baSdrh   sqlite3_file *pConn,
3998cf35eb4Sdan   const void *pBuf,
400748a52cdSdrh   int iAmt,
401748a52cdSdrh   sqlite3_int64 iOfst
402748a52cdSdrh ){
403ef9659baSdrh   quotaConn *p = (quotaConn*)pConn;
404ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
405748a52cdSdrh   sqlite3_int64 iEnd = iOfst+iAmt;
406748a52cdSdrh   quotaGroup *pGroup;
407745de2e4Sdrh   quotaFile *pFile = p->pFile;
408748a52cdSdrh   sqlite3_int64 szNew;
409748a52cdSdrh 
410745de2e4Sdrh   if( pFile->iSize<iEnd ){
411745de2e4Sdrh     pGroup = pFile->pGroup;
412748a52cdSdrh     quotaEnter();
413745de2e4Sdrh     szNew = pGroup->iSize - pFile->iSize + iEnd;
4148cf35eb4Sdan     if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){
415748a52cdSdrh       if( pGroup->xCallback ){
416745de2e4Sdrh         pGroup->xCallback(pFile->zFilename, &pGroup->iLimit, szNew,
417748a52cdSdrh                           pGroup->pArg);
418748a52cdSdrh       }
4198cf35eb4Sdan       if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){
420748a52cdSdrh         quotaLeave();
421748a52cdSdrh         return SQLITE_FULL;
422748a52cdSdrh       }
423748a52cdSdrh     }
424748a52cdSdrh     pGroup->iSize = szNew;
425745de2e4Sdrh     pFile->iSize = iEnd;
426748a52cdSdrh     quotaLeave();
427748a52cdSdrh   }
428745de2e4Sdrh   return pSubOpen->pMethods->xWrite(pSubOpen, pBuf, iAmt, iOfst);
429748a52cdSdrh }
430748a52cdSdrh 
431748a52cdSdrh /* Pass xTruncate requests thru to the original VFS.  If the
432748a52cdSdrh ** success, update the file size.
433748a52cdSdrh */
434ef9659baSdrh static int quotaTruncate(sqlite3_file *pConn, sqlite3_int64 size){
435ef9659baSdrh   quotaConn *p = (quotaConn*)pConn;
436ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
437745de2e4Sdrh   int rc = pSubOpen->pMethods->xTruncate(pSubOpen, size);
438745de2e4Sdrh   quotaFile *pFile = p->pFile;
439745de2e4Sdrh   quotaGroup *pGroup;
440748a52cdSdrh   if( rc==SQLITE_OK ){
441748a52cdSdrh     quotaEnter();
442745de2e4Sdrh     pGroup = pFile->pGroup;
443745de2e4Sdrh     pGroup->iSize -= pFile->iSize;
444745de2e4Sdrh     pFile->iSize = size;
445748a52cdSdrh     pGroup->iSize += size;
446748a52cdSdrh     quotaLeave();
447748a52cdSdrh   }
448748a52cdSdrh   return rc;
449748a52cdSdrh }
450748a52cdSdrh 
451748a52cdSdrh /* Pass xSync requests through to the original VFS without change
452748a52cdSdrh */
453ef9659baSdrh static int quotaSync(sqlite3_file *pConn, int flags){
454ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
455745de2e4Sdrh   return pSubOpen->pMethods->xSync(pSubOpen, flags);
456748a52cdSdrh }
457748a52cdSdrh 
458748a52cdSdrh /* Pass xFileSize requests through to the original VFS but then
459748a52cdSdrh ** update the quotaGroup with the new size before returning.
460748a52cdSdrh */
461ef9659baSdrh static int quotaFileSize(sqlite3_file *pConn, sqlite3_int64 *pSize){
462ef9659baSdrh   quotaConn *p = (quotaConn*)pConn;
463ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
464745de2e4Sdrh   quotaFile *pFile = p->pFile;
465748a52cdSdrh   quotaGroup *pGroup;
466748a52cdSdrh   sqlite3_int64 sz;
467748a52cdSdrh   int rc;
468748a52cdSdrh 
469745de2e4Sdrh   rc = pSubOpen->pMethods->xFileSize(pSubOpen, &sz);
470748a52cdSdrh   if( rc==SQLITE_OK ){
471748a52cdSdrh     quotaEnter();
472745de2e4Sdrh     pGroup = pFile->pGroup;
473745de2e4Sdrh     pGroup->iSize -= pFile->iSize;
474745de2e4Sdrh     pFile->iSize = sz;
475748a52cdSdrh     pGroup->iSize += sz;
476748a52cdSdrh     quotaLeave();
477748a52cdSdrh     *pSize = sz;
478748a52cdSdrh   }
479748a52cdSdrh   return rc;
480748a52cdSdrh }
481748a52cdSdrh 
482748a52cdSdrh /* Pass xLock requests through to the original VFS unchanged.
483748a52cdSdrh */
484ef9659baSdrh static int quotaLock(sqlite3_file *pConn, int lock){
485ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
486745de2e4Sdrh   return pSubOpen->pMethods->xLock(pSubOpen, lock);
487748a52cdSdrh }
488748a52cdSdrh 
489748a52cdSdrh /* Pass xUnlock requests through to the original VFS unchanged.
490748a52cdSdrh */
491ef9659baSdrh static int quotaUnlock(sqlite3_file *pConn, int lock){
492ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
493745de2e4Sdrh   return pSubOpen->pMethods->xUnlock(pSubOpen, lock);
494748a52cdSdrh }
495748a52cdSdrh 
496748a52cdSdrh /* Pass xCheckReservedLock requests through to the original VFS unchanged.
497748a52cdSdrh */
498ef9659baSdrh static int quotaCheckReservedLock(sqlite3_file *pConn, int *pResOut){
499ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
500745de2e4Sdrh   return pSubOpen->pMethods->xCheckReservedLock(pSubOpen, pResOut);
501748a52cdSdrh }
502748a52cdSdrh 
503748a52cdSdrh /* Pass xFileControl requests through to the original VFS unchanged.
504748a52cdSdrh */
505ef9659baSdrh static int quotaFileControl(sqlite3_file *pConn, int op, void *pArg){
506ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
507745de2e4Sdrh   return pSubOpen->pMethods->xFileControl(pSubOpen, op, pArg);
508748a52cdSdrh }
509748a52cdSdrh 
510748a52cdSdrh /* Pass xSectorSize requests through to the original VFS unchanged.
511748a52cdSdrh */
512ef9659baSdrh static int quotaSectorSize(sqlite3_file *pConn){
513ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
514745de2e4Sdrh   return pSubOpen->pMethods->xSectorSize(pSubOpen);
515748a52cdSdrh }
516748a52cdSdrh 
517748a52cdSdrh /* Pass xDeviceCharacteristics requests through to the original VFS unchanged.
518748a52cdSdrh */
519ef9659baSdrh static int quotaDeviceCharacteristics(sqlite3_file *pConn){
520ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
521745de2e4Sdrh   return pSubOpen->pMethods->xDeviceCharacteristics(pSubOpen);
522748a52cdSdrh }
523748a52cdSdrh 
524748a52cdSdrh /* Pass xShmMap requests through to the original VFS unchanged.
525748a52cdSdrh */
526748a52cdSdrh static int quotaShmMap(
527ef9659baSdrh   sqlite3_file *pConn,            /* Handle open on database file */
528748a52cdSdrh   int iRegion,                    /* Region to retrieve */
529748a52cdSdrh   int szRegion,                   /* Size of regions */
530748a52cdSdrh   int bExtend,                    /* True to extend file if necessary */
531748a52cdSdrh   void volatile **pp              /* OUT: Mapped memory */
532748a52cdSdrh ){
533ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
534745de2e4Sdrh   return pSubOpen->pMethods->xShmMap(pSubOpen, iRegion, szRegion, bExtend, pp);
535748a52cdSdrh }
536748a52cdSdrh 
537748a52cdSdrh /* Pass xShmLock requests through to the original VFS unchanged.
538748a52cdSdrh */
539748a52cdSdrh static int quotaShmLock(
540ef9659baSdrh   sqlite3_file *pConn,       /* Database file holding the shared memory */
541748a52cdSdrh   int ofst,                  /* First lock to acquire or release */
542748a52cdSdrh   int n,                     /* Number of locks to acquire or release */
543748a52cdSdrh   int flags                  /* What to do with the lock */
544748a52cdSdrh ){
545ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
546745de2e4Sdrh   return pSubOpen->pMethods->xShmLock(pSubOpen, ofst, n, flags);
547748a52cdSdrh }
548748a52cdSdrh 
549748a52cdSdrh /* Pass xShmBarrier requests through to the original VFS unchanged.
550748a52cdSdrh */
551ef9659baSdrh static void quotaShmBarrier(sqlite3_file *pConn){
552ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
553745de2e4Sdrh   pSubOpen->pMethods->xShmBarrier(pSubOpen);
554748a52cdSdrh }
555748a52cdSdrh 
556748a52cdSdrh /* Pass xShmUnmap requests through to the original VFS unchanged.
557748a52cdSdrh */
558ef9659baSdrh static int quotaShmUnmap(sqlite3_file *pConn, int deleteFlag){
559ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
560745de2e4Sdrh   return pSubOpen->pMethods->xShmUnmap(pSubOpen, deleteFlag);
561748a52cdSdrh }
562748a52cdSdrh 
563748a52cdSdrh /************************** Public Interfaces *****************************/
564748a52cdSdrh /*
565748a52cdSdrh ** Initialize the quota VFS shim.  Use the VFS named zOrigVfsName
566748a52cdSdrh ** as the VFS that does the actual work.  Use the default if
567748a52cdSdrh ** zOrigVfsName==NULL.
568748a52cdSdrh **
569748a52cdSdrh ** The quota VFS shim is named "quota".  It will become the default
570748a52cdSdrh ** VFS if makeDefault is non-zero.
571748a52cdSdrh **
572748a52cdSdrh ** THIS ROUTINE IS NOT THREADSAFE.  Call this routine exactly once
573748a52cdSdrh ** during start-up.
574748a52cdSdrh */
575748a52cdSdrh int sqlite3_quota_initialize(const char *zOrigVfsName, int makeDefault){
576748a52cdSdrh   sqlite3_vfs *pOrigVfs;
5778cf35eb4Sdan   if( gQuota.isInitialized ) return SQLITE_MISUSE;
578748a52cdSdrh   pOrigVfs = sqlite3_vfs_find(zOrigVfsName);
579748a52cdSdrh   if( pOrigVfs==0 ) return SQLITE_ERROR;
5808cf35eb4Sdan   assert( pOrigVfs!=&gQuota.sThisVfs );
5818cf35eb4Sdan   gQuota.pMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
5828cf35eb4Sdan   if( !gQuota.pMutex ){
5838cf35eb4Sdan     return SQLITE_NOMEM;
5848cf35eb4Sdan   }
5858cf35eb4Sdan   gQuota.isInitialized = 1;
586748a52cdSdrh   gQuota.pOrigVfs = pOrigVfs;
587748a52cdSdrh   gQuota.sThisVfs = *pOrigVfs;
588748a52cdSdrh   gQuota.sThisVfs.xOpen = quotaOpen;
589ef9659baSdrh   gQuota.sThisVfs.szOsFile += sizeof(quotaConn);
590748a52cdSdrh   gQuota.sThisVfs.zName = "quota";
591748a52cdSdrh   gQuota.sIoMethodsV1.iVersion = 1;
592748a52cdSdrh   gQuota.sIoMethodsV1.xClose = quotaClose;
593748a52cdSdrh   gQuota.sIoMethodsV1.xRead = quotaRead;
594748a52cdSdrh   gQuota.sIoMethodsV1.xWrite = quotaWrite;
595748a52cdSdrh   gQuota.sIoMethodsV1.xTruncate = quotaTruncate;
596748a52cdSdrh   gQuota.sIoMethodsV1.xSync = quotaSync;
597748a52cdSdrh   gQuota.sIoMethodsV1.xFileSize = quotaFileSize;
598748a52cdSdrh   gQuota.sIoMethodsV1.xLock = quotaLock;
599748a52cdSdrh   gQuota.sIoMethodsV1.xUnlock = quotaUnlock;
600748a52cdSdrh   gQuota.sIoMethodsV1.xCheckReservedLock = quotaCheckReservedLock;
601748a52cdSdrh   gQuota.sIoMethodsV1.xFileControl = quotaFileControl;
6028cf35eb4Sdan   gQuota.sIoMethodsV1.xSectorSize = quotaSectorSize;
603748a52cdSdrh   gQuota.sIoMethodsV1.xDeviceCharacteristics = quotaDeviceCharacteristics;
604748a52cdSdrh   gQuota.sIoMethodsV2 = gQuota.sIoMethodsV1;
605748a52cdSdrh   gQuota.sIoMethodsV2.iVersion = 2;
606748a52cdSdrh   gQuota.sIoMethodsV2.xShmMap = quotaShmMap;
607748a52cdSdrh   gQuota.sIoMethodsV2.xShmLock = quotaShmLock;
608748a52cdSdrh   gQuota.sIoMethodsV2.xShmBarrier = quotaShmBarrier;
609748a52cdSdrh   gQuota.sIoMethodsV2.xShmUnmap = quotaShmUnmap;
6108cf35eb4Sdan   sqlite3_vfs_register(&gQuota.sThisVfs, makeDefault);
6118cf35eb4Sdan   return SQLITE_OK;
612748a52cdSdrh }
613748a52cdSdrh 
614748a52cdSdrh /*
615748a52cdSdrh ** Shutdown the quota system.
616748a52cdSdrh **
617748a52cdSdrh ** All SQLite database connections must be closed before calling this
618748a52cdSdrh ** routine.
619748a52cdSdrh **
620748a52cdSdrh ** THIS ROUTINE IS NOT THREADSAFE.  Call this routine exactly one while
621748a52cdSdrh ** shutting down in order to free all remaining quota groups.
622748a52cdSdrh */
623748a52cdSdrh int sqlite3_quota_shutdown(void){
62475c19174Sdrh   quotaGroup *pGroup;
625748a52cdSdrh   if( gQuota.isInitialized==0 ) return SQLITE_MISUSE;
62675c19174Sdrh   for(pGroup=gQuota.pGroup; pGroup; pGroup=pGroup->pNext){
62775c19174Sdrh     if( pGroup->pFiles ) return SQLITE_MISUSE;
6288cf35eb4Sdan   }
6298cf35eb4Sdan   while( gQuota.pGroup ){
63075c19174Sdrh     pGroup = gQuota.pGroup;
63175c19174Sdrh     gQuota.pGroup = pGroup->pNext;
6329fc7702cSdan     pGroup->iLimit = 0;
6339fc7702cSdan     quotaGroupDeref(pGroup);
6348cf35eb4Sdan   }
635748a52cdSdrh   gQuota.isInitialized = 0;
636748a52cdSdrh   sqlite3_mutex_free(gQuota.pMutex);
6378cf35eb4Sdan   sqlite3_vfs_unregister(&gQuota.sThisVfs);
638748a52cdSdrh   memset(&gQuota, 0, sizeof(gQuota));
6398cf35eb4Sdan   return SQLITE_OK;
640748a52cdSdrh }
641748a52cdSdrh 
642748a52cdSdrh /*
643748a52cdSdrh ** Create or destroy a quota group.
644748a52cdSdrh **
645748a52cdSdrh ** The quota group is defined by the zPattern.  When calling this routine
646748a52cdSdrh ** with a zPattern for a quota group that already exists, this routine
647748a52cdSdrh ** merely updates the iLimit, xCallback, and pArg values for that quota
648748a52cdSdrh ** group.  If zPattern is new, then a new quota group is created.
649748a52cdSdrh **
650748a52cdSdrh ** If the iLimit for a quota group is set to zero, then the quota group
651748a52cdSdrh ** is disabled and will be deleted when the last database connection using
652748a52cdSdrh ** the quota group is closed.
653748a52cdSdrh **
654748a52cdSdrh ** Calling this routine on a zPattern that does not exist and with a
655748a52cdSdrh ** zero iLimit is a no-op.
656748a52cdSdrh **
657748a52cdSdrh ** A quota group must exist with a non-zero iLimit prior to opening
658748a52cdSdrh ** database connections if those connections are to participate in the
6598cf35eb4Sdan ** quota group.  Creating a quota group does not affect database connections
660748a52cdSdrh ** that are already open.
661748a52cdSdrh */
662748a52cdSdrh int sqlite3_quota_set(
663748a52cdSdrh   const char *zPattern,           /* The filename pattern */
664748a52cdSdrh   sqlite3_int64 iLimit,           /* New quota to set for this quota group */
665748a52cdSdrh   void (*xCallback)(              /* Callback invoked when going over quota */
666748a52cdSdrh      const char *zFilename,         /* Name of file whose size increases */
667748a52cdSdrh      sqlite3_int64 *piLimit,        /* IN/OUT: The current limit */
668748a52cdSdrh      sqlite3_int64 iSize,           /* Total size of all files in the group */
669748a52cdSdrh      void *pArg                     /* Client data */
6708cf35eb4Sdan   ),
671ef9659baSdrh   void *pArg,                     /* client data passed thru to callback */
672ef9659baSdrh   void (*xDestroy)(void*)         /* Optional destructor for pArg */
673748a52cdSdrh ){
674748a52cdSdrh   quotaGroup *pGroup;
675748a52cdSdrh   quotaEnter();
676748a52cdSdrh   pGroup = gQuota.pGroup;
677748a52cdSdrh   while( pGroup && strcmp(pGroup->zPattern, zPattern)!=0 ){
678748a52cdSdrh     pGroup = pGroup->pNext;
679748a52cdSdrh   }
68061cc423aSdrh   if( pGroup==0 ){
681748a52cdSdrh     int nPattern = strlen(zPattern);
68261cc423aSdrh     if( iLimit<=0 ){
68361cc423aSdrh       quotaLeave();
68461cc423aSdrh       return SQLITE_OK;
68561cc423aSdrh     }
686*b0a32832Sdan     pGroup = (quotaGroup *)sqlite3_malloc( sizeof(*pGroup) + nPattern + 1 );
687748a52cdSdrh     if( pGroup==0 ){
688748a52cdSdrh       quotaLeave();
689748a52cdSdrh       return SQLITE_NOMEM;
690748a52cdSdrh     }
691748a52cdSdrh     memset(pGroup, 0, sizeof(*pGroup));
692748a52cdSdrh     pGroup->zPattern = (char*)&pGroup[1];
6938cf35eb4Sdan     memcpy((char *)pGroup->zPattern, zPattern, nPattern+1);
694748a52cdSdrh     if( gQuota.pGroup ) gQuota.pGroup->ppPrev = &pGroup->pNext;
6958cf35eb4Sdan     pGroup->pNext = gQuota.pGroup;
6968cf35eb4Sdan     pGroup->ppPrev = &gQuota.pGroup;
6978cf35eb4Sdan     gQuota.pGroup = pGroup;
698748a52cdSdrh   }
699748a52cdSdrh   pGroup->iLimit = iLimit;
700748a52cdSdrh   pGroup->xCallback = xCallback;
701ef9659baSdrh   if( pGroup->xDestroy && pGroup->pArg!=pArg ){
702ef9659baSdrh     pGroup->xDestroy(pGroup->pArg);
703ef9659baSdrh   }
704748a52cdSdrh   pGroup->pArg = pArg;
705ef9659baSdrh   pGroup->xDestroy = xDestroy;
706748a52cdSdrh   quotaGroupDeref(pGroup);
707748a52cdSdrh   quotaLeave();
708748a52cdSdrh   return SQLITE_OK;
709748a52cdSdrh }
710748a52cdSdrh 
711748a52cdSdrh 
712748a52cdSdrh /***************************** Test Code ***********************************/
713748a52cdSdrh #ifdef SQLITE_TEST
714748a52cdSdrh #include <tcl.h>
7158cf35eb4Sdan 
716ef9659baSdrh /*
717ef9659baSdrh ** Argument passed to a TCL quota-over-limit callback.
718ef9659baSdrh */
7198cf35eb4Sdan typedef struct TclQuotaCallback TclQuotaCallback;
7208cf35eb4Sdan struct TclQuotaCallback {
721ef9659baSdrh   Tcl_Interp *interp;    /* Interpreter in which to run the script */
722ef9659baSdrh   Tcl_Obj *pScript;      /* Script to be run */
7238cf35eb4Sdan };
7248cf35eb4Sdan 
7258cf35eb4Sdan extern const char *sqlite3TestErrorName(int);
7268cf35eb4Sdan 
727ef9659baSdrh 
728ef9659baSdrh /*
729ef9659baSdrh ** This is the callback from a quota-over-limit.
730ef9659baSdrh */
7318cf35eb4Sdan static void tclQuotaCallback(
7328cf35eb4Sdan   const char *zFilename,          /* Name of file whose size increases */
7338cf35eb4Sdan   sqlite3_int64 *piLimit,         /* IN/OUT: The current limit */
7348cf35eb4Sdan   sqlite3_int64 iSize,            /* Total size of all files in the group */
7358cf35eb4Sdan   void *pArg                      /* Client data */
7368cf35eb4Sdan ){
7378cf35eb4Sdan   TclQuotaCallback *p;            /* Callback script object */
7388cf35eb4Sdan   Tcl_Obj *pEval;                 /* Script to evaluate */
7398cf35eb4Sdan   Tcl_Obj *pVarname;              /* Name of variable to pass as 2nd arg */
7408cf35eb4Sdan   unsigned int rnd;               /* Random part of pVarname */
7418cf35eb4Sdan   int rc;                         /* Tcl error code */
7428cf35eb4Sdan 
7438cf35eb4Sdan   p = (TclQuotaCallback *)pArg;
7443d3e60e4Sdrh   if( p==0 ) return;
7458cf35eb4Sdan 
7468cf35eb4Sdan   pVarname = Tcl_NewStringObj("::piLimit_", -1);
7478cf35eb4Sdan   Tcl_IncrRefCount(pVarname);
7488cf35eb4Sdan   sqlite3_randomness(sizeof(rnd), (void *)&rnd);
7498cf35eb4Sdan   Tcl_AppendObjToObj(pVarname, Tcl_NewIntObj((int)(rnd&0x7FFFFFFF)));
7508cf35eb4Sdan   Tcl_ObjSetVar2(p->interp, pVarname, 0, Tcl_NewWideIntObj(*piLimit), 0);
7518cf35eb4Sdan 
7528cf35eb4Sdan   pEval = Tcl_DuplicateObj(p->pScript);
7538cf35eb4Sdan   Tcl_IncrRefCount(pEval);
7548cf35eb4Sdan   Tcl_ListObjAppendElement(0, pEval, Tcl_NewStringObj(zFilename, -1));
7558cf35eb4Sdan   Tcl_ListObjAppendElement(0, pEval, pVarname);
7568cf35eb4Sdan   Tcl_ListObjAppendElement(0, pEval, Tcl_NewWideIntObj(iSize));
7578cf35eb4Sdan   rc = Tcl_EvalObjEx(p->interp, pEval, TCL_EVAL_GLOBAL);
7588cf35eb4Sdan 
7598cf35eb4Sdan   if( rc==TCL_OK ){
7608cf35eb4Sdan     Tcl_Obj *pLimit = Tcl_ObjGetVar2(p->interp, pVarname, 0, 0);
7618cf35eb4Sdan     rc = Tcl_GetWideIntFromObj(p->interp, pLimit, piLimit);
7628cf35eb4Sdan     Tcl_UnsetVar(p->interp, Tcl_GetString(pVarname), 0);
7638cf35eb4Sdan   }
7648cf35eb4Sdan 
7658cf35eb4Sdan   Tcl_DecrRefCount(pEval);
7668cf35eb4Sdan   Tcl_DecrRefCount(pVarname);
7678cf35eb4Sdan   if( rc!=TCL_OK ) Tcl_BackgroundError(p->interp);
7688cf35eb4Sdan }
7698cf35eb4Sdan 
7708cf35eb4Sdan /*
771ef9659baSdrh ** Destructor for a TCL quota-over-limit callback.
772ef9659baSdrh */
773ef9659baSdrh static void tclCallbackDestructor(void *pObj){
774ef9659baSdrh   TclQuotaCallback *p = (TclQuotaCallback*)pObj;
775ef9659baSdrh   if( p ){
776ef9659baSdrh     Tcl_DecrRefCount(p->pScript);
7779fc7702cSdan     sqlite3_free((char *)p);
778ef9659baSdrh   }
779ef9659baSdrh }
780ef9659baSdrh 
781ef9659baSdrh /*
7828cf35eb4Sdan ** tclcmd: sqlite3_quota_initialize NAME MAKEDEFAULT
7838cf35eb4Sdan */
7848cf35eb4Sdan static int test_quota_initialize(
7858cf35eb4Sdan   void * clientData,
7868cf35eb4Sdan   Tcl_Interp *interp,
7878cf35eb4Sdan   int objc,
7888cf35eb4Sdan   Tcl_Obj *CONST objv[]
7898cf35eb4Sdan ){
7908cf35eb4Sdan   const char *zName;              /* Name of new quota VFS */
7918cf35eb4Sdan   int makeDefault;                /* True to make the new VFS the default */
7928cf35eb4Sdan   int rc;                         /* Value returned by quota_initialize() */
7938cf35eb4Sdan 
7948cf35eb4Sdan   /* Process arguments */
7958cf35eb4Sdan   if( objc!=3 ){
7968cf35eb4Sdan     Tcl_WrongNumArgs(interp, 1, objv, "NAME MAKEDEFAULT");
7978cf35eb4Sdan     return TCL_ERROR;
7988cf35eb4Sdan   }
7998cf35eb4Sdan   zName = Tcl_GetString(objv[1]);
8008cf35eb4Sdan   if( Tcl_GetBooleanFromObj(interp, objv[2], &makeDefault) ) return TCL_ERROR;
8018cf35eb4Sdan   if( zName[0]=='\0' ) zName = 0;
8028cf35eb4Sdan 
8038cf35eb4Sdan   /* Call sqlite3_quota_initialize() */
8048cf35eb4Sdan   rc = sqlite3_quota_initialize(zName, makeDefault);
8058cf35eb4Sdan   Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC);
8068cf35eb4Sdan 
8078cf35eb4Sdan   return TCL_OK;
8088cf35eb4Sdan }
8098cf35eb4Sdan 
8108cf35eb4Sdan /*
8118cf35eb4Sdan ** tclcmd: sqlite3_quota_shutdown
8128cf35eb4Sdan */
8138cf35eb4Sdan static int test_quota_shutdown(
8148cf35eb4Sdan   void * clientData,
8158cf35eb4Sdan   Tcl_Interp *interp,
8168cf35eb4Sdan   int objc,
8178cf35eb4Sdan   Tcl_Obj *CONST objv[]
8188cf35eb4Sdan ){
8198cf35eb4Sdan   int rc;                         /* Value returned by quota_shutdown() */
8208cf35eb4Sdan 
8218cf35eb4Sdan   if( objc!=1 ){
8228cf35eb4Sdan     Tcl_WrongNumArgs(interp, 1, objv, "");
8238cf35eb4Sdan     return TCL_ERROR;
8248cf35eb4Sdan   }
8258cf35eb4Sdan 
8268cf35eb4Sdan   /* Call sqlite3_quota_shutdown() */
8278cf35eb4Sdan   rc = sqlite3_quota_shutdown();
8288cf35eb4Sdan   Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC);
8298cf35eb4Sdan 
8308cf35eb4Sdan   return TCL_OK;
8318cf35eb4Sdan }
8328cf35eb4Sdan 
8338cf35eb4Sdan /*
8348cf35eb4Sdan ** tclcmd: sqlite3_quota_set PATTERN LIMIT SCRIPT
8358cf35eb4Sdan */
8368cf35eb4Sdan static int test_quota_set(
8378cf35eb4Sdan   void * clientData,
8388cf35eb4Sdan   Tcl_Interp *interp,
8398cf35eb4Sdan   int objc,
8408cf35eb4Sdan   Tcl_Obj *CONST objv[]
8418cf35eb4Sdan ){
8428cf35eb4Sdan   const char *zPattern;           /* File pattern to configure */
8438cf35eb4Sdan   sqlite3_int64 iLimit;           /* Initial quota in bytes */
8448cf35eb4Sdan   Tcl_Obj *pScript;               /* Tcl script to invoke to increase quota */
8458cf35eb4Sdan   int rc;                         /* Value returned by quota_set() */
8468cf35eb4Sdan   TclQuotaCallback *p;            /* Callback object */
847ef9659baSdrh   int nScript;                    /* Length of callback script */
8489fc7702cSdan   void (*xDestroy)(void*);        /* Optional destructor for pArg */
84906bfd36aSdan   void (*xCallback)(const char *, sqlite3_int64 *, sqlite3_int64, void *);
8508cf35eb4Sdan 
8518cf35eb4Sdan   /* Process arguments */
8528cf35eb4Sdan   if( objc!=4 ){
8538cf35eb4Sdan     Tcl_WrongNumArgs(interp, 1, objv, "PATTERN LIMIT SCRIPT");
8548cf35eb4Sdan     return TCL_ERROR;
8558cf35eb4Sdan   }
8568cf35eb4Sdan   zPattern = Tcl_GetString(objv[1]);
8578cf35eb4Sdan   if( Tcl_GetWideIntFromObj(interp, objv[2], &iLimit) ) return TCL_ERROR;
8588cf35eb4Sdan   pScript = objv[3];
859ef9659baSdrh   Tcl_GetStringFromObj(pScript, &nScript);
8608cf35eb4Sdan 
861ef9659baSdrh   if( nScript>0 ){
8628cf35eb4Sdan     /* Allocate a TclQuotaCallback object */
8639fc7702cSdan     p = (TclQuotaCallback *)sqlite3_malloc(sizeof(TclQuotaCallback));
8649fc7702cSdan     if( !p ){
8659fc7702cSdan       Tcl_SetResult(interp, (char *)"SQLITE_NOMEM", TCL_STATIC);
8669fc7702cSdan       return TCL_OK;
8679fc7702cSdan     }
8688cf35eb4Sdan     memset(p, 0, sizeof(TclQuotaCallback));
8698cf35eb4Sdan     p->interp = interp;
870ef9659baSdrh     Tcl_IncrRefCount(pScript);
8718cf35eb4Sdan     p->pScript = pScript;
8729fc7702cSdan     xDestroy = tclCallbackDestructor;
87306bfd36aSdan     xCallback = tclQuotaCallback;
874ef9659baSdrh   }else{
875ef9659baSdrh     p = 0;
8769fc7702cSdan     xDestroy = 0;
87706bfd36aSdan     xCallback = 0;
8788cf35eb4Sdan   }
8799fc7702cSdan 
880ef9659baSdrh   /* Invoke sqlite3_quota_set() */
88106bfd36aSdan   rc = sqlite3_quota_set(zPattern, iLimit, xCallback, (void*)p, xDestroy);
8828cf35eb4Sdan 
8838cf35eb4Sdan   Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC);
8848cf35eb4Sdan   return TCL_OK;
8858cf35eb4Sdan }
8868cf35eb4Sdan 
8878cf35eb4Sdan /*
888ef9659baSdrh ** tclcmd:  sqlite3_quota_dump
889ef9659baSdrh */
890ef9659baSdrh static int test_quota_dump(
891ef9659baSdrh   void * clientData,
892ef9659baSdrh   Tcl_Interp *interp,
893ef9659baSdrh   int objc,
894ef9659baSdrh   Tcl_Obj *CONST objv[]
895ef9659baSdrh ){
896ef9659baSdrh   Tcl_Obj *pResult;
897ef9659baSdrh   Tcl_Obj *pGroupTerm;
898ef9659baSdrh   Tcl_Obj *pFileTerm;
899ef9659baSdrh   quotaGroup *pGroup;
900ef9659baSdrh   quotaFile *pFile;
901ef9659baSdrh 
902ef9659baSdrh   pResult = Tcl_NewObj();
903ef9659baSdrh   quotaEnter();
904ef9659baSdrh   for(pGroup=gQuota.pGroup; pGroup; pGroup=pGroup->pNext){
905ef9659baSdrh     pGroupTerm = Tcl_NewObj();
906ef9659baSdrh     Tcl_ListObjAppendElement(interp, pGroupTerm,
907ef9659baSdrh           Tcl_NewStringObj(pGroup->zPattern, -1));
908ef9659baSdrh     Tcl_ListObjAppendElement(interp, pGroupTerm,
909ef9659baSdrh           Tcl_NewWideIntObj(pGroup->iLimit));
910ef9659baSdrh     Tcl_ListObjAppendElement(interp, pGroupTerm,
911ef9659baSdrh           Tcl_NewWideIntObj(pGroup->iSize));
91275c19174Sdrh     for(pFile=pGroup->pFiles; pFile; pFile=pFile->pNext){
913ef9659baSdrh       pFileTerm = Tcl_NewObj();
914ef9659baSdrh       Tcl_ListObjAppendElement(interp, pFileTerm,
915ef9659baSdrh             Tcl_NewStringObj(pFile->zFilename, -1));
916ef9659baSdrh       Tcl_ListObjAppendElement(interp, pFileTerm,
917ef9659baSdrh             Tcl_NewWideIntObj(pFile->iSize));
918ef9659baSdrh       Tcl_ListObjAppendElement(interp, pFileTerm,
919ef9659baSdrh             Tcl_NewWideIntObj(pFile->nRef));
920ef9659baSdrh       Tcl_ListObjAppendElement(interp, pGroupTerm, pFileTerm);
921ef9659baSdrh     }
922ef9659baSdrh     Tcl_ListObjAppendElement(interp, pResult, pGroupTerm);
923ef9659baSdrh   }
924ef9659baSdrh   quotaLeave();
925ef9659baSdrh   Tcl_SetObjResult(interp, pResult);
926ef9659baSdrh   return TCL_OK;
927ef9659baSdrh }
928ef9659baSdrh 
929748a52cdSdrh /*
930748a52cdSdrh ** This routine registers the custom TCL commands defined in this
931748a52cdSdrh ** module.  This should be the only procedure visible from outside
932748a52cdSdrh ** of this module.
933748a52cdSdrh */
934748a52cdSdrh int Sqlitequota_Init(Tcl_Interp *interp){
9358cf35eb4Sdan   static struct {
9368cf35eb4Sdan      char *zName;
9378cf35eb4Sdan      Tcl_ObjCmdProc *xProc;
9388cf35eb4Sdan   } aCmd[] = {
9398cf35eb4Sdan     { "sqlite3_quota_initialize", test_quota_initialize },
9408cf35eb4Sdan     { "sqlite3_quota_shutdown", test_quota_shutdown },
9418cf35eb4Sdan     { "sqlite3_quota_set", test_quota_set },
942ef9659baSdrh     { "sqlite3_quota_dump", test_quota_dump },
9438cf35eb4Sdan   };
9448cf35eb4Sdan   int i;
9458cf35eb4Sdan 
9468cf35eb4Sdan   for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
9478cf35eb4Sdan     Tcl_CreateObjCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0);
9488cf35eb4Sdan   }
9498cf35eb4Sdan 
950748a52cdSdrh   return TCL_OK;
951748a52cdSdrh }
952748a52cdSdrh #endif
953