xref: /sqlite-3.40.0/src/test_quota.c (revision 9237a258)
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
20*9237a258Sdrh ** of all files in the quota group is greater than the limit, then
21*9237a258Sdrh ** 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 
34748a52cdSdrh /************************ Object Definitions ******************************/
35748a52cdSdrh 
36745de2e4Sdrh /* Forward declaration of all object types */
37748a52cdSdrh 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 */
55748a52cdSdrh struct quotaGroup {
56748a52cdSdrh   const char *zPattern;          /* Filename pattern to be quotaed */
57748a52cdSdrh   sqlite3_int64 iLimit;          /* Upper bound on total file size */
58748a52cdSdrh   sqlite3_int64 iSize;           /* Current size of all files */
59748a52cdSdrh   void (*xCallback)(             /* Callback invoked when going over quota */
60748a52cdSdrh      const char *zFilename,         /* Name of file whose size increases */
61748a52cdSdrh      sqlite3_int64 *piLimit,        /* IN/OUT: The current limit */
62748a52cdSdrh      sqlite3_int64 iSize,           /* Total size of all files in the group */
63748a52cdSdrh      void *pArg                     /* Client data */
64748a52cdSdrh   );
65748a52cdSdrh   void *pArg;                    /* Third argument to the xCallback() */
66ef9659baSdrh   void (*xDestroy)(void*);       /* Optional destructor for pArg */
67748a52cdSdrh   quotaGroup *pNext, **ppPrev;   /* Doubly linked list of all quota objects */
6875c19174Sdrh   quotaFile *pFiles;             /* Files within this group */
69748a52cdSdrh };
70748a52cdSdrh 
71748a52cdSdrh /*
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.
78748a52cdSdrh */
79748a52cdSdrh 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 {
94748a52cdSdrh   sqlite3_file base;              /* Base class - must be first */
95745de2e4Sdrh   quotaFile *pFile;               /* The underlying file */
96748a52cdSdrh   /* The underlying VFS sqlite3_file is appended to this object */
97748a52cdSdrh };
98748a52cdSdrh 
99748a52cdSdrh /************************* Global Variables **********************************/
100748a52cdSdrh /*
101748a52cdSdrh ** All global variables used by this file are containing within the following
102748a52cdSdrh ** gQuota structure.
103748a52cdSdrh */
104748a52cdSdrh static struct {
105549dc1baSdrh   /* The pOrigVfs is the real, original underlying VFS implementation.
106748a52cdSdrh   ** Most operations pass-through to the real VFS.  This value is read-only
107748a52cdSdrh   ** during operation.  It is only modified at start-time and thus does not
108748a52cdSdrh   ** require a mutex.
109748a52cdSdrh   */
110748a52cdSdrh   sqlite3_vfs *pOrigVfs;
111748a52cdSdrh 
112748a52cdSdrh   /* The sThisVfs is the VFS structure used by this shim.  It is initialized
113748a52cdSdrh   ** at start-time and thus does not require a mutex
114748a52cdSdrh   */
115748a52cdSdrh   sqlite3_vfs sThisVfs;
116748a52cdSdrh 
117748a52cdSdrh   /* The sIoMethods defines the methods used by sqlite3_file objects
118748a52cdSdrh   ** associated with this shim.  It is initialized at start-time and does
119748a52cdSdrh   ** not require a mutex.
120748a52cdSdrh   **
121748a52cdSdrh   ** When the underlying VFS is called to open a file, it might return
122748a52cdSdrh   ** either a version 1 or a version 2 sqlite3_file object.  This shim
123748a52cdSdrh   ** has to create a wrapper sqlite3_file of the same version.  Hence
124748a52cdSdrh   ** there are two I/O method structures, one for version 1 and the other
125748a52cdSdrh   ** for version 2.
126748a52cdSdrh   */
127748a52cdSdrh   sqlite3_io_methods sIoMethodsV1;
128748a52cdSdrh   sqlite3_io_methods sIoMethodsV2;
129748a52cdSdrh 
130748a52cdSdrh   /* True when this shim as been initialized.
131748a52cdSdrh   */
132748a52cdSdrh   int isInitialized;
133748a52cdSdrh 
134748a52cdSdrh   /* For run-time access any of the other global data structures in this
135748a52cdSdrh   ** shim, the following mutex must be held.
136748a52cdSdrh   */
137748a52cdSdrh   sqlite3_mutex *pMutex;
138748a52cdSdrh 
139748a52cdSdrh   /* List of quotaGroup objects.
140748a52cdSdrh   */
141748a52cdSdrh   quotaGroup *pGroup;
142748a52cdSdrh 
143748a52cdSdrh } gQuota;
144748a52cdSdrh 
145748a52cdSdrh /************************* Utility Routines *********************************/
146748a52cdSdrh /*
147748a52cdSdrh ** Acquire and release the mutex used to serialize access to the
148748a52cdSdrh ** list of quotaGroups.
149748a52cdSdrh */
150748a52cdSdrh static void quotaEnter(void){ sqlite3_mutex_enter(gQuota.pMutex); }
151748a52cdSdrh static void quotaLeave(void){ sqlite3_mutex_leave(gQuota.pMutex); }
152748a52cdSdrh 
153748a52cdSdrh 
154748a52cdSdrh /* If the reference count and threshold for a quotaGroup are both
155748a52cdSdrh ** zero, then destroy the quotaGroup.
156748a52cdSdrh */
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);
163748a52cdSdrh   }
164748a52cdSdrh }
165748a52cdSdrh 
166748a52cdSdrh /*
167748a52cdSdrh ** Return TRUE if string z matches glob pattern zGlob.
168748a52cdSdrh **
169748a52cdSdrh ** Globbing rules:
170748a52cdSdrh **
171748a52cdSdrh **      '*'       Matches any sequence of zero or more characters.
172748a52cdSdrh **
173748a52cdSdrh **      '?'       Matches exactly one character.
174748a52cdSdrh **
175748a52cdSdrh **     [...]      Matches one character from the enclosed list of
176748a52cdSdrh **                characters.
177748a52cdSdrh **
178748a52cdSdrh **     [^...]     Matches one character not in the enclosed list.
179748a52cdSdrh **
180748a52cdSdrh */
18161cc423aSdrh static int quotaStrglob(const char *zGlob, const char *z){
182748a52cdSdrh   int c, c2;
183748a52cdSdrh   int invert;
184748a52cdSdrh   int seen;
185748a52cdSdrh 
186748a52cdSdrh   while( (c = (*(zGlob++)))!=0 ){
187748a52cdSdrh     if( c=='*' ){
188748a52cdSdrh       while( (c=(*(zGlob++))) == '*' || c=='?' ){
189748a52cdSdrh         if( c=='?' && (*(z++))==0 ) return 0;
190748a52cdSdrh       }
191748a52cdSdrh       if( c==0 ){
192748a52cdSdrh         return 1;
193748a52cdSdrh       }else if( c=='[' ){
19461cc423aSdrh         while( *z && quotaStrglob(zGlob-1,z)==0 ){
195748a52cdSdrh           z++;
196748a52cdSdrh         }
197748a52cdSdrh         return (*z)!=0;
198748a52cdSdrh       }
199748a52cdSdrh       while( (c2 = (*(z++)))!=0 ){
200748a52cdSdrh         while( c2!=c ){
201748a52cdSdrh           c2 = *(z++);
202748a52cdSdrh           if( c2==0 ) return 0;
203748a52cdSdrh         }
20461cc423aSdrh         if( quotaStrglob(zGlob,z) ) return 1;
205748a52cdSdrh       }
206748a52cdSdrh       return 0;
207748a52cdSdrh     }else if( c=='?' ){
208748a52cdSdrh       if( (*(z++))==0 ) return 0;
209748a52cdSdrh     }else if( c=='[' ){
210748a52cdSdrh       int prior_c = 0;
211748a52cdSdrh       seen = 0;
212748a52cdSdrh       invert = 0;
213748a52cdSdrh       c = *(z++);
214748a52cdSdrh       if( c==0 ) return 0;
215748a52cdSdrh       c2 = *(zGlob++);
216748a52cdSdrh       if( c2=='^' ){
217748a52cdSdrh         invert = 1;
218748a52cdSdrh         c2 = *(zGlob++);
219748a52cdSdrh       }
220748a52cdSdrh       if( c2==']' ){
221748a52cdSdrh         if( c==']' ) seen = 1;
222748a52cdSdrh         c2 = *(zGlob++);
223748a52cdSdrh       }
224748a52cdSdrh       while( c2 && c2!=']' ){
225748a52cdSdrh         if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
226748a52cdSdrh           c2 = *(zGlob++);
227748a52cdSdrh           if( c>=prior_c && c<=c2 ) seen = 1;
228748a52cdSdrh           prior_c = 0;
229748a52cdSdrh         }else{
230748a52cdSdrh           if( c==c2 ){
231748a52cdSdrh             seen = 1;
232748a52cdSdrh           }
233748a52cdSdrh           prior_c = c2;
234748a52cdSdrh         }
235748a52cdSdrh         c2 = *(zGlob++);
236748a52cdSdrh       }
237748a52cdSdrh       if( c2==0 || (seen ^ invert)==0 ) return 0;
238748a52cdSdrh     }else{
239748a52cdSdrh       if( c!=(*(z++)) ) return 0;
240748a52cdSdrh     }
241748a52cdSdrh   }
242748a52cdSdrh   return *z==0;
243748a52cdSdrh }
244748a52cdSdrh 
245748a52cdSdrh 
246748a52cdSdrh /* Find a quotaGroup given the filename.
2478cf35eb4Sdan **
248ef9659baSdrh ** Return a pointer to the quotaGroup object. Return NULL if not found.
249748a52cdSdrh */
250748a52cdSdrh static quotaGroup *quotaGroupFind(const char *zFilename){
251748a52cdSdrh   quotaGroup *p;
25261cc423aSdrh   for(p=gQuota.pGroup; p && quotaStrglob(p->zPattern, zFilename)==0;
25361cc423aSdrh       p=p->pNext){}
254748a52cdSdrh   return p;
255748a52cdSdrh }
256748a52cdSdrh 
257ef9659baSdrh /* Translate an sqlite3_file* that is really a quotaConn* into
258549dc1baSdrh ** the sqlite3_file* for the underlying original VFS.
259748a52cdSdrh */
260ef9659baSdrh static sqlite3_file *quotaSubOpen(sqlite3_file *pConn){
261ef9659baSdrh   quotaConn *p = (quotaConn*)pConn;
262748a52cdSdrh   return (sqlite3_file*)&p[1];
263748a52cdSdrh }
264748a52cdSdrh 
265748a52cdSdrh /************************* VFS Method Wrappers *****************************/
266748a52cdSdrh /*
267748a52cdSdrh ** This is the xOpen method used for the "quota" VFS.
268748a52cdSdrh **
269748a52cdSdrh ** 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.
272748a52cdSdrh */
273748a52cdSdrh 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 */
279748a52cdSdrh ){
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 */
286748a52cdSdrh 
287748a52cdSdrh   /* If the file is not a main database file or a WAL, then use the
288748a52cdSdrh   ** normal xOpen method.
289748a52cdSdrh   */
2908cf35eb4Sdan   if( (flags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_WAL))==0 ){
291ef9659baSdrh     return pOrigVfs->xOpen(pOrigVfs, zName, pConn, flags, pOutFlags);
292748a52cdSdrh   }
293748a52cdSdrh 
294748a52cdSdrh   /* If the name of the file does not match any quota group, then
295748a52cdSdrh   ** use the normal xOpen method.
296748a52cdSdrh   */
297748a52cdSdrh   quotaEnter();
2988cf35eb4Sdan   pGroup = quotaGroupFind(zName);
299748a52cdSdrh   if( pGroup==0 ){
300ef9659baSdrh     rc = pOrigVfs->xOpen(pOrigVfs, zName, pConn, flags, pOutFlags);
301748a52cdSdrh   }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;
333748a52cdSdrh       }
334748a52cdSdrh     }
3358cf35eb4Sdan   }
3368cf35eb4Sdan   quotaLeave();
337748a52cdSdrh   return rc;
338748a52cdSdrh }
339748a52cdSdrh 
340748a52cdSdrh /************************ I/O Method Wrappers *******************************/
341748a52cdSdrh 
342748a52cdSdrh /* 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.
345748a52cdSdrh */
346ef9659baSdrh static int quotaClose(sqlite3_file *pConn){
347ef9659baSdrh   quotaConn *p = (quotaConn*)pConn;
348745de2e4Sdrh   quotaFile *pFile = p->pFile;
349ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
350748a52cdSdrh   int rc;
351745de2e4Sdrh   rc = pSubOpen->pMethods->xClose(pSubOpen);
352748a52cdSdrh   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;
359748a52cdSdrh     quotaGroupDeref(pGroup);
360745de2e4Sdrh     sqlite3_free(pFile);
361745de2e4Sdrh   }
362748a52cdSdrh   quotaLeave();
363748a52cdSdrh   return rc;
364748a52cdSdrh }
365748a52cdSdrh 
366748a52cdSdrh /* Pass xRead requests directory thru to the original VFS without
367748a52cdSdrh ** further processing.
368748a52cdSdrh */
369748a52cdSdrh static int quotaRead(
370ef9659baSdrh   sqlite3_file *pConn,
371748a52cdSdrh   void *pBuf,
372748a52cdSdrh   int iAmt,
373748a52cdSdrh   sqlite3_int64 iOfst
374748a52cdSdrh ){
375ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
376745de2e4Sdrh   return pSubOpen->pMethods->xRead(pSubOpen, pBuf, iAmt, iOfst);
377748a52cdSdrh }
378748a52cdSdrh 
379748a52cdSdrh /* Check xWrite requests to see if they expand the file.  If they do,
380748a52cdSdrh ** the perform a quota check before passing them through to the
381748a52cdSdrh ** original VFS.
382748a52cdSdrh */
383748a52cdSdrh static int quotaWrite(
384ef9659baSdrh   sqlite3_file *pConn,
3858cf35eb4Sdan   const void *pBuf,
386748a52cdSdrh   int iAmt,
387748a52cdSdrh   sqlite3_int64 iOfst
388748a52cdSdrh ){
389ef9659baSdrh   quotaConn *p = (quotaConn*)pConn;
390ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
391748a52cdSdrh   sqlite3_int64 iEnd = iOfst+iAmt;
392748a52cdSdrh   quotaGroup *pGroup;
393745de2e4Sdrh   quotaFile *pFile = p->pFile;
394748a52cdSdrh   sqlite3_int64 szNew;
395748a52cdSdrh 
396745de2e4Sdrh   if( pFile->iSize<iEnd ){
397745de2e4Sdrh     pGroup = pFile->pGroup;
398748a52cdSdrh     quotaEnter();
399745de2e4Sdrh     szNew = pGroup->iSize - pFile->iSize + iEnd;
4008cf35eb4Sdan     if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){
401748a52cdSdrh       if( pGroup->xCallback ){
402745de2e4Sdrh         pGroup->xCallback(pFile->zFilename, &pGroup->iLimit, szNew,
403748a52cdSdrh                           pGroup->pArg);
404748a52cdSdrh       }
4058cf35eb4Sdan       if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){
406748a52cdSdrh         quotaLeave();
407748a52cdSdrh         return SQLITE_FULL;
408748a52cdSdrh       }
409748a52cdSdrh     }
410748a52cdSdrh     pGroup->iSize = szNew;
411745de2e4Sdrh     pFile->iSize = iEnd;
412748a52cdSdrh     quotaLeave();
413748a52cdSdrh   }
414745de2e4Sdrh   return pSubOpen->pMethods->xWrite(pSubOpen, pBuf, iAmt, iOfst);
415748a52cdSdrh }
416748a52cdSdrh 
417748a52cdSdrh /* Pass xTruncate requests thru to the original VFS.  If the
418748a52cdSdrh ** success, update the file size.
419748a52cdSdrh */
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;
426748a52cdSdrh   if( rc==SQLITE_OK ){
427748a52cdSdrh     quotaEnter();
428745de2e4Sdrh     pGroup = pFile->pGroup;
429745de2e4Sdrh     pGroup->iSize -= pFile->iSize;
430745de2e4Sdrh     pFile->iSize = size;
431748a52cdSdrh     pGroup->iSize += size;
432748a52cdSdrh     quotaLeave();
433748a52cdSdrh   }
434748a52cdSdrh   return rc;
435748a52cdSdrh }
436748a52cdSdrh 
437748a52cdSdrh /* Pass xSync requests through to the original VFS without change
438748a52cdSdrh */
439ef9659baSdrh static int quotaSync(sqlite3_file *pConn, int flags){
440ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
441745de2e4Sdrh   return pSubOpen->pMethods->xSync(pSubOpen, flags);
442748a52cdSdrh }
443748a52cdSdrh 
444748a52cdSdrh /* Pass xFileSize requests through to the original VFS but then
445748a52cdSdrh ** update the quotaGroup with the new size before returning.
446748a52cdSdrh */
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;
451748a52cdSdrh   quotaGroup *pGroup;
452748a52cdSdrh   sqlite3_int64 sz;
453748a52cdSdrh   int rc;
454748a52cdSdrh 
455745de2e4Sdrh   rc = pSubOpen->pMethods->xFileSize(pSubOpen, &sz);
456748a52cdSdrh   if( rc==SQLITE_OK ){
457748a52cdSdrh     quotaEnter();
458745de2e4Sdrh     pGroup = pFile->pGroup;
459745de2e4Sdrh     pGroup->iSize -= pFile->iSize;
460745de2e4Sdrh     pFile->iSize = sz;
461748a52cdSdrh     pGroup->iSize += sz;
462748a52cdSdrh     quotaLeave();
463748a52cdSdrh     *pSize = sz;
464748a52cdSdrh   }
465748a52cdSdrh   return rc;
466748a52cdSdrh }
467748a52cdSdrh 
468748a52cdSdrh /* Pass xLock requests through to the original VFS unchanged.
469748a52cdSdrh */
470ef9659baSdrh static int quotaLock(sqlite3_file *pConn, int lock){
471ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
472745de2e4Sdrh   return pSubOpen->pMethods->xLock(pSubOpen, lock);
473748a52cdSdrh }
474748a52cdSdrh 
475748a52cdSdrh /* Pass xUnlock requests through to the original VFS unchanged.
476748a52cdSdrh */
477ef9659baSdrh static int quotaUnlock(sqlite3_file *pConn, int lock){
478ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
479745de2e4Sdrh   return pSubOpen->pMethods->xUnlock(pSubOpen, lock);
480748a52cdSdrh }
481748a52cdSdrh 
482748a52cdSdrh /* Pass xCheckReservedLock requests through to the original VFS unchanged.
483748a52cdSdrh */
484ef9659baSdrh static int quotaCheckReservedLock(sqlite3_file *pConn, int *pResOut){
485ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
486745de2e4Sdrh   return pSubOpen->pMethods->xCheckReservedLock(pSubOpen, pResOut);
487748a52cdSdrh }
488748a52cdSdrh 
489748a52cdSdrh /* Pass xFileControl requests through to the original VFS unchanged.
490748a52cdSdrh */
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);
494748a52cdSdrh }
495748a52cdSdrh 
496748a52cdSdrh /* Pass xSectorSize requests through to the original VFS unchanged.
497748a52cdSdrh */
498ef9659baSdrh static int quotaSectorSize(sqlite3_file *pConn){
499ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
500745de2e4Sdrh   return pSubOpen->pMethods->xSectorSize(pSubOpen);
501748a52cdSdrh }
502748a52cdSdrh 
503748a52cdSdrh /* Pass xDeviceCharacteristics requests through to the original VFS unchanged.
504748a52cdSdrh */
505ef9659baSdrh static int quotaDeviceCharacteristics(sqlite3_file *pConn){
506ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
507745de2e4Sdrh   return pSubOpen->pMethods->xDeviceCharacteristics(pSubOpen);
508748a52cdSdrh }
509748a52cdSdrh 
510748a52cdSdrh /* Pass xShmMap requests through to the original VFS unchanged.
511748a52cdSdrh */
512748a52cdSdrh static int quotaShmMap(
513ef9659baSdrh   sqlite3_file *pConn,            /* Handle open on database file */
514748a52cdSdrh   int iRegion,                    /* Region to retrieve */
515748a52cdSdrh   int szRegion,                   /* Size of regions */
516748a52cdSdrh   int bExtend,                    /* True to extend file if necessary */
517748a52cdSdrh   void volatile **pp              /* OUT: Mapped memory */
518748a52cdSdrh ){
519ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
520745de2e4Sdrh   return pSubOpen->pMethods->xShmMap(pSubOpen, iRegion, szRegion, bExtend, pp);
521748a52cdSdrh }
522748a52cdSdrh 
523748a52cdSdrh /* Pass xShmLock requests through to the original VFS unchanged.
524748a52cdSdrh */
525748a52cdSdrh static int quotaShmLock(
526ef9659baSdrh   sqlite3_file *pConn,       /* Database file holding the shared memory */
527748a52cdSdrh   int ofst,                  /* First lock to acquire or release */
528748a52cdSdrh   int n,                     /* Number of locks to acquire or release */
529748a52cdSdrh   int flags                  /* What to do with the lock */
530748a52cdSdrh ){
531ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
532745de2e4Sdrh   return pSubOpen->pMethods->xShmLock(pSubOpen, ofst, n, flags);
533748a52cdSdrh }
534748a52cdSdrh 
535748a52cdSdrh /* Pass xShmBarrier requests through to the original VFS unchanged.
536748a52cdSdrh */
537ef9659baSdrh static void quotaShmBarrier(sqlite3_file *pConn){
538ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
539745de2e4Sdrh   pSubOpen->pMethods->xShmBarrier(pSubOpen);
540748a52cdSdrh }
541748a52cdSdrh 
542748a52cdSdrh /* Pass xShmUnmap requests through to the original VFS unchanged.
543748a52cdSdrh */
544ef9659baSdrh static int quotaShmUnmap(sqlite3_file *pConn, int deleteFlag){
545ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
546745de2e4Sdrh   return pSubOpen->pMethods->xShmUnmap(pSubOpen, deleteFlag);
547748a52cdSdrh }
548748a52cdSdrh 
549748a52cdSdrh /************************** Public Interfaces *****************************/
550748a52cdSdrh /*
551748a52cdSdrh ** Initialize the quota VFS shim.  Use the VFS named zOrigVfsName
552748a52cdSdrh ** as the VFS that does the actual work.  Use the default if
553748a52cdSdrh ** zOrigVfsName==NULL.
554748a52cdSdrh **
555748a52cdSdrh ** The quota VFS shim is named "quota".  It will become the default
556748a52cdSdrh ** VFS if makeDefault is non-zero.
557748a52cdSdrh **
558748a52cdSdrh ** THIS ROUTINE IS NOT THREADSAFE.  Call this routine exactly once
559748a52cdSdrh ** during start-up.
560748a52cdSdrh */
561748a52cdSdrh int sqlite3_quota_initialize(const char *zOrigVfsName, int makeDefault){
562748a52cdSdrh   sqlite3_vfs *pOrigVfs;
5638cf35eb4Sdan   if( gQuota.isInitialized ) return SQLITE_MISUSE;
564748a52cdSdrh   pOrigVfs = sqlite3_vfs_find(zOrigVfsName);
565748a52cdSdrh   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;
572748a52cdSdrh   gQuota.pOrigVfs = pOrigVfs;
573748a52cdSdrh   gQuota.sThisVfs = *pOrigVfs;
574748a52cdSdrh   gQuota.sThisVfs.xOpen = quotaOpen;
575ef9659baSdrh   gQuota.sThisVfs.szOsFile += sizeof(quotaConn);
576748a52cdSdrh   gQuota.sThisVfs.zName = "quota";
577748a52cdSdrh   gQuota.sIoMethodsV1.iVersion = 1;
578748a52cdSdrh   gQuota.sIoMethodsV1.xClose = quotaClose;
579748a52cdSdrh   gQuota.sIoMethodsV1.xRead = quotaRead;
580748a52cdSdrh   gQuota.sIoMethodsV1.xWrite = quotaWrite;
581748a52cdSdrh   gQuota.sIoMethodsV1.xTruncate = quotaTruncate;
582748a52cdSdrh   gQuota.sIoMethodsV1.xSync = quotaSync;
583748a52cdSdrh   gQuota.sIoMethodsV1.xFileSize = quotaFileSize;
584748a52cdSdrh   gQuota.sIoMethodsV1.xLock = quotaLock;
585748a52cdSdrh   gQuota.sIoMethodsV1.xUnlock = quotaUnlock;
586748a52cdSdrh   gQuota.sIoMethodsV1.xCheckReservedLock = quotaCheckReservedLock;
587748a52cdSdrh   gQuota.sIoMethodsV1.xFileControl = quotaFileControl;
5888cf35eb4Sdan   gQuota.sIoMethodsV1.xSectorSize = quotaSectorSize;
589748a52cdSdrh   gQuota.sIoMethodsV1.xDeviceCharacteristics = quotaDeviceCharacteristics;
590748a52cdSdrh   gQuota.sIoMethodsV2 = gQuota.sIoMethodsV1;
591748a52cdSdrh   gQuota.sIoMethodsV2.iVersion = 2;
592748a52cdSdrh   gQuota.sIoMethodsV2.xShmMap = quotaShmMap;
593748a52cdSdrh   gQuota.sIoMethodsV2.xShmLock = quotaShmLock;
594748a52cdSdrh   gQuota.sIoMethodsV2.xShmBarrier = quotaShmBarrier;
595748a52cdSdrh   gQuota.sIoMethodsV2.xShmUnmap = quotaShmUnmap;
5968cf35eb4Sdan   sqlite3_vfs_register(&gQuota.sThisVfs, makeDefault);
5978cf35eb4Sdan   return SQLITE_OK;
598748a52cdSdrh }
599748a52cdSdrh 
600748a52cdSdrh /*
601748a52cdSdrh ** Shutdown the quota system.
602748a52cdSdrh **
603748a52cdSdrh ** All SQLite database connections must be closed before calling this
604748a52cdSdrh ** routine.
605748a52cdSdrh **
606748a52cdSdrh ** THIS ROUTINE IS NOT THREADSAFE.  Call this routine exactly one while
607748a52cdSdrh ** shutting down in order to free all remaining quota groups.
608748a52cdSdrh */
609748a52cdSdrh int sqlite3_quota_shutdown(void){
61075c19174Sdrh   quotaGroup *pGroup;
611748a52cdSdrh   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   }
620748a52cdSdrh   gQuota.isInitialized = 0;
621748a52cdSdrh   sqlite3_mutex_free(gQuota.pMutex);
6228cf35eb4Sdan   sqlite3_vfs_unregister(&gQuota.sThisVfs);
623748a52cdSdrh   memset(&gQuota, 0, sizeof(gQuota));
6248cf35eb4Sdan   return SQLITE_OK;
625748a52cdSdrh }
626748a52cdSdrh 
627748a52cdSdrh /*
628748a52cdSdrh ** Create or destroy a quota group.
629748a52cdSdrh **
630748a52cdSdrh ** The quota group is defined by the zPattern.  When calling this routine
631748a52cdSdrh ** with a zPattern for a quota group that already exists, this routine
632748a52cdSdrh ** merely updates the iLimit, xCallback, and pArg values for that quota
633748a52cdSdrh ** group.  If zPattern is new, then a new quota group is created.
634748a52cdSdrh **
635748a52cdSdrh ** If the iLimit for a quota group is set to zero, then the quota group
636748a52cdSdrh ** is disabled and will be deleted when the last database connection using
637748a52cdSdrh ** the quota group is closed.
638748a52cdSdrh **
639748a52cdSdrh ** Calling this routine on a zPattern that does not exist and with a
640748a52cdSdrh ** zero iLimit is a no-op.
641748a52cdSdrh **
642748a52cdSdrh ** A quota group must exist with a non-zero iLimit prior to opening
643748a52cdSdrh ** database connections if those connections are to participate in the
6448cf35eb4Sdan ** quota group.  Creating a quota group does not affect database connections
645748a52cdSdrh ** that are already open.
646748a52cdSdrh */
647748a52cdSdrh int sqlite3_quota_set(
648748a52cdSdrh   const char *zPattern,           /* The filename pattern */
649748a52cdSdrh   sqlite3_int64 iLimit,           /* New quota to set for this quota group */
650748a52cdSdrh   void (*xCallback)(              /* Callback invoked when going over quota */
651748a52cdSdrh      const char *zFilename,         /* Name of file whose size increases */
652748a52cdSdrh      sqlite3_int64 *piLimit,        /* IN/OUT: The current limit */
653748a52cdSdrh      sqlite3_int64 iSize,           /* Total size of all files in the group */
654748a52cdSdrh      void *pArg                     /* Client data */
6558cf35eb4Sdan   ),
656ef9659baSdrh   void *pArg,                     /* client data passed thru to callback */
657ef9659baSdrh   void (*xDestroy)(void*)         /* Optional destructor for pArg */
658748a52cdSdrh ){
659748a52cdSdrh   quotaGroup *pGroup;
660748a52cdSdrh   quotaEnter();
661748a52cdSdrh   pGroup = gQuota.pGroup;
662748a52cdSdrh   while( pGroup && strcmp(pGroup->zPattern, zPattern)!=0 ){
663748a52cdSdrh     pGroup = pGroup->pNext;
664748a52cdSdrh   }
66561cc423aSdrh   if( pGroup==0 ){
666748a52cdSdrh     int nPattern = strlen(zPattern);
66761cc423aSdrh     if( iLimit<=0 ){
66861cc423aSdrh       quotaLeave();
66961cc423aSdrh       return SQLITE_OK;
67061cc423aSdrh     }
671748a52cdSdrh     pGroup = sqlite3_malloc( sizeof(*pGroup) + nPattern + 1 );
672748a52cdSdrh     if( pGroup==0 ){
673748a52cdSdrh       quotaLeave();
674748a52cdSdrh       return SQLITE_NOMEM;
675748a52cdSdrh     }
676748a52cdSdrh     memset(pGroup, 0, sizeof(*pGroup));
677748a52cdSdrh     pGroup->zPattern = (char*)&pGroup[1];
6788cf35eb4Sdan     memcpy((char *)pGroup->zPattern, zPattern, nPattern+1);
679748a52cdSdrh     if( gQuota.pGroup ) gQuota.pGroup->ppPrev = &pGroup->pNext;
6808cf35eb4Sdan     pGroup->pNext = gQuota.pGroup;
6818cf35eb4Sdan     pGroup->ppPrev = &gQuota.pGroup;
6828cf35eb4Sdan     gQuota.pGroup = pGroup;
683748a52cdSdrh   }
684748a52cdSdrh   pGroup->iLimit = iLimit;
685748a52cdSdrh   pGroup->xCallback = xCallback;
686ef9659baSdrh   if( pGroup->xDestroy && pGroup->pArg!=pArg ){
687ef9659baSdrh     pGroup->xDestroy(pGroup->pArg);
688ef9659baSdrh   }
689748a52cdSdrh   pGroup->pArg = pArg;
690ef9659baSdrh   pGroup->xDestroy = xDestroy;
691748a52cdSdrh   quotaGroupDeref(pGroup);
692748a52cdSdrh   quotaLeave();
693748a52cdSdrh   return SQLITE_OK;
694748a52cdSdrh }
695748a52cdSdrh 
696748a52cdSdrh 
697748a52cdSdrh /***************************** Test Code ***********************************/
698748a52cdSdrh #ifdef SQLITE_TEST
699748a52cdSdrh #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;
7293d3e60e4Sdrh   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 
904748a52cdSdrh /*
905748a52cdSdrh ** This routine registers the custom TCL commands defined in this
906748a52cdSdrh ** module.  This should be the only procedure visible from outside
907748a52cdSdrh ** of this module.
908748a52cdSdrh */
909748a52cdSdrh 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 
925748a52cdSdrh   return TCL_OK;
926748a52cdSdrh }
927748a52cdSdrh #endif
928