xref: /sqlite-3.40.0/src/test_quota.c (revision 49ed5c85)
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 */
3060f21e4bSdrh #include "test_quota.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 */
98796af156Sdrh   int deleteOnClose;              /* True to delete this file when it closes */
99745de2e4Sdrh   quotaFile *pNext, **ppPrev;     /* Linked list of files in the same group */
100745de2e4Sdrh };
101745de2e4Sdrh 
102745de2e4Sdrh /*
103745de2e4Sdrh ** An instance of the following object represents each open connection
104549dc1baSdrh ** to a file that participates in quota tracking.  This object is a
105549dc1baSdrh ** subclass of sqlite3_file.  The sqlite3_file object for the underlying
106549dc1baSdrh ** VFS is appended to this structure.
107745de2e4Sdrh */
108ef9659baSdrh struct quotaConn {
109748a52cdSdrh   sqlite3_file base;              /* Base class - must be first */
110745de2e4Sdrh   quotaFile *pFile;               /* The underlying file */
111748a52cdSdrh   /* The underlying VFS sqlite3_file is appended to this object */
112748a52cdSdrh };
113748a52cdSdrh 
11460f21e4bSdrh /*
11560f21e4bSdrh ** An instance of the following object records the state of an
11660f21e4bSdrh ** open file.  This object is opaque to all users - the internal
11760f21e4bSdrh ** structure is only visible to the functions below.
11860f21e4bSdrh */
11960f21e4bSdrh struct quota_FILE {
12060f21e4bSdrh   FILE *f;                /* Open stdio file pointer */
12160f21e4bSdrh   sqlite3_int64 iOfst;    /* Current offset into the file */
12260f21e4bSdrh   quotaFile *pFile;       /* The file record in the quota system */
12360f21e4bSdrh };
12460f21e4bSdrh 
12560f21e4bSdrh 
126748a52cdSdrh /************************* Global Variables **********************************/
127748a52cdSdrh /*
128748a52cdSdrh ** All global variables used by this file are containing within the following
129748a52cdSdrh ** gQuota structure.
130748a52cdSdrh */
131748a52cdSdrh static struct {
132549dc1baSdrh   /* The pOrigVfs is the real, original underlying VFS implementation.
133748a52cdSdrh   ** Most operations pass-through to the real VFS.  This value is read-only
134748a52cdSdrh   ** during operation.  It is only modified at start-time and thus does not
135748a52cdSdrh   ** require a mutex.
136748a52cdSdrh   */
137748a52cdSdrh   sqlite3_vfs *pOrigVfs;
138748a52cdSdrh 
139748a52cdSdrh   /* The sThisVfs is the VFS structure used by this shim.  It is initialized
140748a52cdSdrh   ** at start-time and thus does not require a mutex
141748a52cdSdrh   */
142748a52cdSdrh   sqlite3_vfs sThisVfs;
143748a52cdSdrh 
144748a52cdSdrh   /* The sIoMethods defines the methods used by sqlite3_file objects
145748a52cdSdrh   ** associated with this shim.  It is initialized at start-time and does
146748a52cdSdrh   ** not require a mutex.
147748a52cdSdrh   **
148748a52cdSdrh   ** When the underlying VFS is called to open a file, it might return
149748a52cdSdrh   ** either a version 1 or a version 2 sqlite3_file object.  This shim
150748a52cdSdrh   ** has to create a wrapper sqlite3_file of the same version.  Hence
151748a52cdSdrh   ** there are two I/O method structures, one for version 1 and the other
152748a52cdSdrh   ** for version 2.
153748a52cdSdrh   */
154748a52cdSdrh   sqlite3_io_methods sIoMethodsV1;
155748a52cdSdrh   sqlite3_io_methods sIoMethodsV2;
156748a52cdSdrh 
157748a52cdSdrh   /* True when this shim as been initialized.
158748a52cdSdrh   */
159748a52cdSdrh   int isInitialized;
160748a52cdSdrh 
161748a52cdSdrh   /* For run-time access any of the other global data structures in this
162748a52cdSdrh   ** shim, the following mutex must be held.
163748a52cdSdrh   */
164748a52cdSdrh   sqlite3_mutex *pMutex;
165748a52cdSdrh 
166748a52cdSdrh   /* List of quotaGroup objects.
167748a52cdSdrh   */
168748a52cdSdrh   quotaGroup *pGroup;
169748a52cdSdrh 
170748a52cdSdrh } gQuota;
171748a52cdSdrh 
172748a52cdSdrh /************************* Utility Routines *********************************/
173748a52cdSdrh /*
174748a52cdSdrh ** Acquire and release the mutex used to serialize access to the
175748a52cdSdrh ** list of quotaGroups.
176748a52cdSdrh */
177748a52cdSdrh static void quotaEnter(void){ sqlite3_mutex_enter(gQuota.pMutex); }
178748a52cdSdrh static void quotaLeave(void){ sqlite3_mutex_leave(gQuota.pMutex); }
179748a52cdSdrh 
180796af156Sdrh /* Count the number of open files in a quotaGroup
181796af156Sdrh */
182796af156Sdrh static int quotaGroupOpenFileCount(quotaGroup *pGroup){
183796af156Sdrh   int N = 0;
184796af156Sdrh   quotaFile *pFile = pGroup->pFiles;
185796af156Sdrh   while( pFile ){
186796af156Sdrh     if( pFile->nRef ) N++;
187796af156Sdrh     pFile = pFile->pNext;
188796af156Sdrh   }
189796af156Sdrh   return N;
190796af156Sdrh }
191796af156Sdrh 
192796af156Sdrh /* Remove a file from a quota group.
193796af156Sdrh */
194796af156Sdrh static void quotaRemoveFile(quotaFile *pFile){
195796af156Sdrh   quotaGroup *pGroup = pFile->pGroup;
196796af156Sdrh   pGroup->iSize -= pFile->iSize;
197796af156Sdrh   *pFile->ppPrev = pFile->pNext;
198796af156Sdrh   if( pFile->pNext ) pFile->pNext->ppPrev = pFile->ppPrev;
199796af156Sdrh   sqlite3_free(pFile);
200796af156Sdrh }
201796af156Sdrh 
202796af156Sdrh /* Remove all files from a quota group.  It is always the case that
203796af156Sdrh ** all files will be closed when this routine is called.
204796af156Sdrh */
205796af156Sdrh static void quotaRemoveAllFiles(quotaGroup *pGroup){
206796af156Sdrh   while( pGroup->pFiles ){
207796af156Sdrh     assert( pGroup->pFiles->nRef==0 );
208796af156Sdrh     quotaRemoveFile(pGroup->pFiles);
209796af156Sdrh   }
210796af156Sdrh }
211796af156Sdrh 
212748a52cdSdrh 
213748a52cdSdrh /* If the reference count and threshold for a quotaGroup are both
214748a52cdSdrh ** zero, then destroy the quotaGroup.
215748a52cdSdrh */
21675c19174Sdrh static void quotaGroupDeref(quotaGroup *pGroup){
217796af156Sdrh   if( pGroup->iLimit==0 && quotaGroupOpenFileCount(pGroup)==0 ){
218796af156Sdrh     quotaRemoveAllFiles(pGroup);
2199fc7702cSdan     *pGroup->ppPrev = pGroup->pNext;
22075c19174Sdrh     if( pGroup->pNext ) pGroup->pNext->ppPrev = pGroup->ppPrev;
22175c19174Sdrh     if( pGroup->xDestroy ) pGroup->xDestroy(pGroup->pArg);
22275c19174Sdrh     sqlite3_free(pGroup);
223748a52cdSdrh   }
224748a52cdSdrh }
225748a52cdSdrh 
226748a52cdSdrh /*
227748a52cdSdrh ** Return TRUE if string z matches glob pattern zGlob.
228748a52cdSdrh **
229748a52cdSdrh ** Globbing rules:
230748a52cdSdrh **
231748a52cdSdrh **      '*'       Matches any sequence of zero or more characters.
232748a52cdSdrh **
233748a52cdSdrh **      '?'       Matches exactly one character.
234748a52cdSdrh **
235748a52cdSdrh **     [...]      Matches one character from the enclosed list of
236748a52cdSdrh **                characters.
237748a52cdSdrh **
238748a52cdSdrh **     [^...]     Matches one character not in the enclosed list.
239748a52cdSdrh **
240a76e8589Sdrh **     /          Matches "/" or "\\"
241a76e8589Sdrh **
242748a52cdSdrh */
24361cc423aSdrh static int quotaStrglob(const char *zGlob, const char *z){
244c8ccda60Sdrh   int c, c2, cx;
245748a52cdSdrh   int invert;
246748a52cdSdrh   int seen;
247748a52cdSdrh 
248748a52cdSdrh   while( (c = (*(zGlob++)))!=0 ){
249748a52cdSdrh     if( c=='*' ){
250748a52cdSdrh       while( (c=(*(zGlob++))) == '*' || c=='?' ){
251748a52cdSdrh         if( c=='?' && (*(z++))==0 ) return 0;
252748a52cdSdrh       }
253748a52cdSdrh       if( c==0 ){
254748a52cdSdrh         return 1;
255748a52cdSdrh       }else if( c=='[' ){
25661cc423aSdrh         while( *z && quotaStrglob(zGlob-1,z)==0 ){
257748a52cdSdrh           z++;
258748a52cdSdrh         }
259748a52cdSdrh         return (*z)!=0;
260748a52cdSdrh       }
261c8ccda60Sdrh       cx = (c=='/') ? '\\' : c;
262748a52cdSdrh       while( (c2 = (*(z++)))!=0 ){
263c8ccda60Sdrh         while( c2!=c && c2!=cx ){
264748a52cdSdrh           c2 = *(z++);
265748a52cdSdrh           if( c2==0 ) return 0;
266748a52cdSdrh         }
26761cc423aSdrh         if( quotaStrglob(zGlob,z) ) return 1;
268748a52cdSdrh       }
269748a52cdSdrh       return 0;
270748a52cdSdrh     }else if( c=='?' ){
271748a52cdSdrh       if( (*(z++))==0 ) return 0;
272748a52cdSdrh     }else if( c=='[' ){
273748a52cdSdrh       int prior_c = 0;
274748a52cdSdrh       seen = 0;
275748a52cdSdrh       invert = 0;
276748a52cdSdrh       c = *(z++);
277748a52cdSdrh       if( c==0 ) return 0;
278748a52cdSdrh       c2 = *(zGlob++);
279748a52cdSdrh       if( c2=='^' ){
280748a52cdSdrh         invert = 1;
281748a52cdSdrh         c2 = *(zGlob++);
282748a52cdSdrh       }
283748a52cdSdrh       if( c2==']' ){
284748a52cdSdrh         if( c==']' ) seen = 1;
285748a52cdSdrh         c2 = *(zGlob++);
286748a52cdSdrh       }
287748a52cdSdrh       while( c2 && c2!=']' ){
288748a52cdSdrh         if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
289748a52cdSdrh           c2 = *(zGlob++);
290748a52cdSdrh           if( c>=prior_c && c<=c2 ) seen = 1;
291748a52cdSdrh           prior_c = 0;
292748a52cdSdrh         }else{
293748a52cdSdrh           if( c==c2 ){
294748a52cdSdrh             seen = 1;
295748a52cdSdrh           }
296748a52cdSdrh           prior_c = c2;
297748a52cdSdrh         }
298748a52cdSdrh         c2 = *(zGlob++);
299748a52cdSdrh       }
300748a52cdSdrh       if( c2==0 || (seen ^ invert)==0 ) return 0;
301a76e8589Sdrh     }else if( c=='/' ){
302a76e8589Sdrh       if( z[0]!='/' && z[0]!='\\' ) return 0;
303a76e8589Sdrh       z++;
304748a52cdSdrh     }else{
305748a52cdSdrh       if( c!=(*(z++)) ) return 0;
306748a52cdSdrh     }
307748a52cdSdrh   }
308748a52cdSdrh   return *z==0;
309748a52cdSdrh }
310748a52cdSdrh 
311748a52cdSdrh 
312748a52cdSdrh /* Find a quotaGroup given the filename.
3138cf35eb4Sdan **
314ef9659baSdrh ** Return a pointer to the quotaGroup object. Return NULL if not found.
315748a52cdSdrh */
316748a52cdSdrh static quotaGroup *quotaGroupFind(const char *zFilename){
317748a52cdSdrh   quotaGroup *p;
31861cc423aSdrh   for(p=gQuota.pGroup; p && quotaStrglob(p->zPattern, zFilename)==0;
31961cc423aSdrh       p=p->pNext){}
320748a52cdSdrh   return p;
321748a52cdSdrh }
322748a52cdSdrh 
323ef9659baSdrh /* Translate an sqlite3_file* that is really a quotaConn* into
324549dc1baSdrh ** the sqlite3_file* for the underlying original VFS.
325748a52cdSdrh */
326ef9659baSdrh static sqlite3_file *quotaSubOpen(sqlite3_file *pConn){
327ef9659baSdrh   quotaConn *p = (quotaConn*)pConn;
328748a52cdSdrh   return (sqlite3_file*)&p[1];
329748a52cdSdrh }
330748a52cdSdrh 
331796af156Sdrh /* Find a file in a quota group and return a pointer to that file.
332796af156Sdrh ** Return NULL if the file is not in the group.
333796af156Sdrh */
33460f21e4bSdrh static quotaFile *quotaFindFile(
33560f21e4bSdrh   quotaGroup *pGroup,     /* Group in which to look for the file */
33660f21e4bSdrh   const char *zName,      /* Full pathname of the file */
33760f21e4bSdrh   int createFlag          /* Try to create the file if not found */
33860f21e4bSdrh ){
339796af156Sdrh   quotaFile *pFile = pGroup->pFiles;
340796af156Sdrh   while( pFile && strcmp(pFile->zFilename, zName)!=0 ){
341796af156Sdrh     pFile = pFile->pNext;
342796af156Sdrh   }
34360f21e4bSdrh   if( pFile==0 && createFlag ){
34460f21e4bSdrh     int nName = strlen(zName);
34560f21e4bSdrh     pFile = (quotaFile *)sqlite3_malloc( sizeof(*pFile) + nName + 1 );
34660f21e4bSdrh     if( pFile ){
34760f21e4bSdrh       memset(pFile, 0, sizeof(*pFile));
34860f21e4bSdrh       pFile->zFilename = (char*)&pFile[1];
34960f21e4bSdrh       memcpy(pFile->zFilename, zName, nName+1);
35060f21e4bSdrh       pFile->pNext = pGroup->pFiles;
35160f21e4bSdrh       if( pGroup->pFiles ) pGroup->pFiles->ppPrev = &pFile->pNext;
35260f21e4bSdrh       pFile->ppPrev = &pGroup->pFiles;
35360f21e4bSdrh       pGroup->pFiles = pFile;
35460f21e4bSdrh       pFile->pGroup = pGroup;
35560f21e4bSdrh     }
35660f21e4bSdrh   }
357796af156Sdrh   return pFile;
358796af156Sdrh }
359796af156Sdrh 
360a76e8589Sdrh /*
361a76e8589Sdrh ** Figure out if we are dealing with Unix, Windows, or some other
362a76e8589Sdrh ** operating system.  After the following block of preprocess macros,
363a76e8589Sdrh ** all of SQLITE_OS_UNIX, SQLITE_OS_WIN, SQLITE_OS_OS2, and SQLITE_OS_OTHER
364a76e8589Sdrh ** will defined to either 1 or 0.  One of the four will be 1.  The other
365a76e8589Sdrh ** three will be 0.
366a76e8589Sdrh */
367a76e8589Sdrh #if defined(SQLITE_OS_OTHER)
368a76e8589Sdrh # if SQLITE_OS_OTHER==1
369a76e8589Sdrh #   undef SQLITE_OS_UNIX
370a76e8589Sdrh #   define SQLITE_OS_UNIX 0
371a76e8589Sdrh #   undef SQLITE_OS_WIN
372a76e8589Sdrh #   define SQLITE_OS_WIN 0
373a76e8589Sdrh #   undef SQLITE_OS_OS2
374a76e8589Sdrh #   define SQLITE_OS_OS2 0
375a76e8589Sdrh # else
376a76e8589Sdrh #   undef SQLITE_OS_OTHER
377a76e8589Sdrh # endif
378a76e8589Sdrh #endif
379a76e8589Sdrh #if !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_OTHER)
380a76e8589Sdrh # define SQLITE_OS_OTHER 0
381a76e8589Sdrh # ifndef SQLITE_OS_WIN
382a76e8589Sdrh #   if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) \
383a76e8589Sdrh                        || defined(__MINGW32__) || defined(__BORLANDC__)
384a76e8589Sdrh #     define SQLITE_OS_WIN 1
385a76e8589Sdrh #     define SQLITE_OS_UNIX 0
386a76e8589Sdrh #     define SQLITE_OS_OS2 0
387a76e8589Sdrh #   elif defined(__EMX__) || defined(_OS2) || defined(OS2) \
388a76e8589Sdrh                           || defined(_OS2_) || defined(__OS2__)
389a76e8589Sdrh #     define SQLITE_OS_WIN 0
390a76e8589Sdrh #     define SQLITE_OS_UNIX 0
391a76e8589Sdrh #     define SQLITE_OS_OS2 1
392a76e8589Sdrh #   else
393a76e8589Sdrh #     define SQLITE_OS_WIN 0
394a76e8589Sdrh #     define SQLITE_OS_UNIX 1
395a76e8589Sdrh #     define SQLITE_OS_OS2 0
396a76e8589Sdrh #  endif
397a76e8589Sdrh # else
398a76e8589Sdrh #  define SQLITE_OS_UNIX 0
399a76e8589Sdrh #  define SQLITE_OS_OS2 0
400a76e8589Sdrh # endif
401a76e8589Sdrh #else
402a76e8589Sdrh # ifndef SQLITE_OS_WIN
403a76e8589Sdrh #  define SQLITE_OS_WIN 0
404a76e8589Sdrh # endif
405a76e8589Sdrh #endif
406a76e8589Sdrh 
40727cec37dSdrh #if SQLITE_OS_UNIX
40827cec37dSdrh # include <unistd.h>
40927cec37dSdrh #endif
41027cec37dSdrh #if SQLITE_OS_WIN
41127cec37dSdrh # include <windows.h>
41227cec37dSdrh #endif
413a76e8589Sdrh 
414a76e8589Sdrh /*
415a76e8589Sdrh ** Translate UTF8 to MBCS for use in fopen() calls.  Return a pointer to the
416a76e8589Sdrh ** translated text..  Call quota_mbcs_free() to deallocate any memory
417a76e8589Sdrh ** used to store the returned pointer when done.
418a76e8589Sdrh */
419a76e8589Sdrh static char *quota_utf8_to_mbcs(const char *zUtf8){
420a76e8589Sdrh #if SQLITE_OS_WIN
42127cec37dSdrh   int n;             /* Bytes in zUtf8 */
42227cec37dSdrh   int nWide;         /* number of UTF-16 characters */
42327cec37dSdrh   int nMbcs;         /* Bytes of MBCS */
42427cec37dSdrh   LPWSTR zTmpWide;   /* The UTF16 text */
42527cec37dSdrh   char *zMbcs;       /* The MBCS text */
42627cec37dSdrh   int codepage;      /* Code page used by fopen() */
42727cec37dSdrh 
42827cec37dSdrh   n = strlen(zUtf8);
42927cec37dSdrh   nWide = MultiByteToWideChar(CP_UTF8, 0, zUtf8, -1, NULL, 0);
430*49ed5c85Sdrh   if( nWide==0 ) return 0;
431*49ed5c85Sdrh   zTmpWide = sqlite3_malloc( (nWide+1)*sizeof(zTmpWide[0]) );
43227cec37dSdrh   if( zTmpWide==0 ) return 0;
43327cec37dSdrh   MultiByteToWideChar(CP_UTF8, 0, zUtf8, -1, zTmpWide, nWide);
43427cec37dSdrh   codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
43527cec37dSdrh   nMbcs = WideCharToMultiByte(codepage, 0, zTmpWide, nWide, 0, 0, 0, 0);
436*49ed5c85Sdrh   zMbcs = nMbcs ? sqlite3_malloc( nMbcs+1 ) : 0;
43727cec37dSdrh   if( zMbcs ){
43827cec37dSdrh     WideCharToMultiByte(codepage, 0, zTmpWide, nWide, zMbcs, nMbcs, 0, 0);
43927cec37dSdrh   }
44027cec37dSdrh   sqlite3_free(zTmpWide);
44127cec37dSdrh   return zMbcs;
442a76e8589Sdrh #else
443a76e8589Sdrh   return (char*)zUtf8;  /* No-op on unix */
444a76e8589Sdrh #endif
445a76e8589Sdrh }
446a76e8589Sdrh 
447a76e8589Sdrh /*
448a76e8589Sdrh ** Deallocate any memory allocated by quota_utf8_to_mbcs().
449a76e8589Sdrh */
450a76e8589Sdrh static void quota_mbcs_free(char *zOld){
451a76e8589Sdrh #if SQLITE_OS_WIN
452c8ccda60Sdrh   sqlite3_free(zOld);
453a76e8589Sdrh #else
454a76e8589Sdrh   /* No-op on unix */
455a76e8589Sdrh #endif
456a76e8589Sdrh }
457a76e8589Sdrh 
458748a52cdSdrh /************************* VFS Method Wrappers *****************************/
459748a52cdSdrh /*
460748a52cdSdrh ** This is the xOpen method used for the "quota" VFS.
461748a52cdSdrh **
462748a52cdSdrh ** Most of the work is done by the underlying original VFS.  This method
463549dc1baSdrh ** simply links the new file into the appropriate quota group if it is a
464549dc1baSdrh ** file that needs to be tracked.
465748a52cdSdrh */
466748a52cdSdrh static int quotaOpen(
467549dc1baSdrh   sqlite3_vfs *pVfs,          /* The quota VFS */
468549dc1baSdrh   const char *zName,          /* Name of file to be opened */
469ef9659baSdrh   sqlite3_file *pConn,        /* Fill in this file descriptor */
470549dc1baSdrh   int flags,                  /* Flags to control the opening */
471549dc1baSdrh   int *pOutFlags              /* Flags showing results of opening */
472748a52cdSdrh ){
473549dc1baSdrh   int rc;                                    /* Result code */
474ef9659baSdrh   quotaConn *pQuotaOpen;                     /* The new quota file descriptor */
475549dc1baSdrh   quotaFile *pFile;                          /* Corresponding quotaFile obj */
476549dc1baSdrh   quotaGroup *pGroup;                        /* The group file belongs to */
477549dc1baSdrh   sqlite3_file *pSubOpen;                    /* Real file descriptor */
478549dc1baSdrh   sqlite3_vfs *pOrigVfs = gQuota.pOrigVfs;   /* Real VFS */
479748a52cdSdrh 
480748a52cdSdrh   /* If the file is not a main database file or a WAL, then use the
481748a52cdSdrh   ** normal xOpen method.
482748a52cdSdrh   */
4838cf35eb4Sdan   if( (flags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_WAL))==0 ){
484ef9659baSdrh     return pOrigVfs->xOpen(pOrigVfs, zName, pConn, flags, pOutFlags);
485748a52cdSdrh   }
486748a52cdSdrh 
487748a52cdSdrh   /* If the name of the file does not match any quota group, then
488748a52cdSdrh   ** use the normal xOpen method.
489748a52cdSdrh   */
490748a52cdSdrh   quotaEnter();
4918cf35eb4Sdan   pGroup = quotaGroupFind(zName);
492748a52cdSdrh   if( pGroup==0 ){
493ef9659baSdrh     rc = pOrigVfs->xOpen(pOrigVfs, zName, pConn, flags, pOutFlags);
494748a52cdSdrh   }else{
4958cf35eb4Sdan     /* If we get to this point, it means the file needs to be quota tracked.
4968cf35eb4Sdan     */
497ef9659baSdrh     pQuotaOpen = (quotaConn*)pConn;
498ef9659baSdrh     pSubOpen = quotaSubOpen(pConn);
499745de2e4Sdrh     rc = pOrigVfs->xOpen(pOrigVfs, zName, pSubOpen, flags, pOutFlags);
5008cf35eb4Sdan     if( rc==SQLITE_OK ){
50160f21e4bSdrh       pFile = quotaFindFile(pGroup, zName, 1);
502745de2e4Sdrh       if( pFile==0 ){
503745de2e4Sdrh         quotaLeave();
504745de2e4Sdrh         pSubOpen->pMethods->xClose(pSubOpen);
505745de2e4Sdrh         return SQLITE_NOMEM;
506745de2e4Sdrh       }
507796af156Sdrh       pFile->deleteOnClose = (flags & SQLITE_OPEN_DELETEONCLOSE)!=0;
508745de2e4Sdrh       pFile->nRef++;
509745de2e4Sdrh       pQuotaOpen->pFile = pFile;
510745de2e4Sdrh       if( pSubOpen->pMethods->iVersion==1 ){
511745de2e4Sdrh         pQuotaOpen->base.pMethods = &gQuota.sIoMethodsV1;
5128cf35eb4Sdan       }else{
513745de2e4Sdrh         pQuotaOpen->base.pMethods = &gQuota.sIoMethodsV2;
514748a52cdSdrh       }
515748a52cdSdrh     }
5168cf35eb4Sdan   }
5178cf35eb4Sdan   quotaLeave();
518748a52cdSdrh   return rc;
519748a52cdSdrh }
520748a52cdSdrh 
521796af156Sdrh /*
522796af156Sdrh ** This is the xDelete method used for the "quota" VFS.
523796af156Sdrh **
524796af156Sdrh ** If the file being deleted is part of the quota group, then reduce
525796af156Sdrh ** the size of the quota group accordingly.  And remove the file from
526796af156Sdrh ** the set of files in the quota group.
527796af156Sdrh */
528796af156Sdrh static int quotaDelete(
529796af156Sdrh   sqlite3_vfs *pVfs,          /* The quota VFS */
530796af156Sdrh   const char *zName,          /* Name of file to be deleted */
531796af156Sdrh   int syncDir                 /* Do a directory sync after deleting */
532796af156Sdrh ){
533796af156Sdrh   int rc;                                    /* Result code */
534796af156Sdrh   quotaFile *pFile;                          /* Files in the quota */
535796af156Sdrh   quotaGroup *pGroup;                        /* The group file belongs to */
536796af156Sdrh   sqlite3_vfs *pOrigVfs = gQuota.pOrigVfs;   /* Real VFS */
537796af156Sdrh 
538796af156Sdrh   /* Do the actual file delete */
539796af156Sdrh   rc = pOrigVfs->xDelete(pOrigVfs, zName, syncDir);
540796af156Sdrh 
541796af156Sdrh   /* If the file just deleted is a member of a quota group, then remove
542796af156Sdrh   ** it from that quota group.
543796af156Sdrh   */
544796af156Sdrh   if( rc==SQLITE_OK ){
545796af156Sdrh     quotaEnter();
546796af156Sdrh     pGroup = quotaGroupFind(zName);
547796af156Sdrh     if( pGroup ){
54860f21e4bSdrh       pFile = quotaFindFile(pGroup, zName, 0);
549796af156Sdrh       if( pFile ){
550796af156Sdrh         if( pFile->nRef ){
551796af156Sdrh           pFile->deleteOnClose = 1;
552796af156Sdrh         }else{
553796af156Sdrh           quotaRemoveFile(pFile);
554796af156Sdrh           quotaGroupDeref(pGroup);
555796af156Sdrh         }
556796af156Sdrh       }
557796af156Sdrh     }
558796af156Sdrh     quotaLeave();
559796af156Sdrh   }
560796af156Sdrh   return rc;
561796af156Sdrh }
562796af156Sdrh 
563796af156Sdrh 
564748a52cdSdrh /************************ I/O Method Wrappers *******************************/
565748a52cdSdrh 
566748a52cdSdrh /* xClose requests get passed through to the original VFS.  But we
567ef9659baSdrh ** also have to unlink the quotaConn from the quotaFile and quotaGroup.
568ef9659baSdrh ** The quotaFile and/or quotaGroup are freed if they are no longer in use.
569748a52cdSdrh */
570ef9659baSdrh static int quotaClose(sqlite3_file *pConn){
571ef9659baSdrh   quotaConn *p = (quotaConn*)pConn;
572745de2e4Sdrh   quotaFile *pFile = p->pFile;
573ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
574748a52cdSdrh   int rc;
575745de2e4Sdrh   rc = pSubOpen->pMethods->xClose(pSubOpen);
576748a52cdSdrh   quotaEnter();
577745de2e4Sdrh   pFile->nRef--;
578745de2e4Sdrh   if( pFile->nRef==0 ){
579745de2e4Sdrh     quotaGroup *pGroup = pFile->pGroup;
58069b2232dSdrh     if( pFile->deleteOnClose ){
58169b2232dSdrh       gQuota.pOrigVfs->xDelete(gQuota.pOrigVfs, pFile->zFilename, 0);
58269b2232dSdrh       quotaRemoveFile(pFile);
58369b2232dSdrh     }
584748a52cdSdrh     quotaGroupDeref(pGroup);
585745de2e4Sdrh   }
586748a52cdSdrh   quotaLeave();
587748a52cdSdrh   return rc;
588748a52cdSdrh }
589748a52cdSdrh 
590748a52cdSdrh /* Pass xRead requests directory thru to the original VFS without
591748a52cdSdrh ** further processing.
592748a52cdSdrh */
593748a52cdSdrh static int quotaRead(
594ef9659baSdrh   sqlite3_file *pConn,
595748a52cdSdrh   void *pBuf,
596748a52cdSdrh   int iAmt,
597748a52cdSdrh   sqlite3_int64 iOfst
598748a52cdSdrh ){
599ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
600745de2e4Sdrh   return pSubOpen->pMethods->xRead(pSubOpen, pBuf, iAmt, iOfst);
601748a52cdSdrh }
602748a52cdSdrh 
603748a52cdSdrh /* Check xWrite requests to see if they expand the file.  If they do,
604748a52cdSdrh ** the perform a quota check before passing them through to the
605748a52cdSdrh ** original VFS.
606748a52cdSdrh */
607748a52cdSdrh static int quotaWrite(
608ef9659baSdrh   sqlite3_file *pConn,
6098cf35eb4Sdan   const void *pBuf,
610748a52cdSdrh   int iAmt,
611748a52cdSdrh   sqlite3_int64 iOfst
612748a52cdSdrh ){
613ef9659baSdrh   quotaConn *p = (quotaConn*)pConn;
614ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
615748a52cdSdrh   sqlite3_int64 iEnd = iOfst+iAmt;
616748a52cdSdrh   quotaGroup *pGroup;
617745de2e4Sdrh   quotaFile *pFile = p->pFile;
618748a52cdSdrh   sqlite3_int64 szNew;
619748a52cdSdrh 
620745de2e4Sdrh   if( pFile->iSize<iEnd ){
621745de2e4Sdrh     pGroup = pFile->pGroup;
622748a52cdSdrh     quotaEnter();
623745de2e4Sdrh     szNew = pGroup->iSize - pFile->iSize + iEnd;
6248cf35eb4Sdan     if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){
625748a52cdSdrh       if( pGroup->xCallback ){
626745de2e4Sdrh         pGroup->xCallback(pFile->zFilename, &pGroup->iLimit, szNew,
627748a52cdSdrh                           pGroup->pArg);
628748a52cdSdrh       }
6298cf35eb4Sdan       if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){
630748a52cdSdrh         quotaLeave();
631748a52cdSdrh         return SQLITE_FULL;
632748a52cdSdrh       }
633748a52cdSdrh     }
634748a52cdSdrh     pGroup->iSize = szNew;
635745de2e4Sdrh     pFile->iSize = iEnd;
636748a52cdSdrh     quotaLeave();
637748a52cdSdrh   }
638745de2e4Sdrh   return pSubOpen->pMethods->xWrite(pSubOpen, pBuf, iAmt, iOfst);
639748a52cdSdrh }
640748a52cdSdrh 
641748a52cdSdrh /* Pass xTruncate requests thru to the original VFS.  If the
642748a52cdSdrh ** success, update the file size.
643748a52cdSdrh */
644ef9659baSdrh static int quotaTruncate(sqlite3_file *pConn, sqlite3_int64 size){
645ef9659baSdrh   quotaConn *p = (quotaConn*)pConn;
646ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
647745de2e4Sdrh   int rc = pSubOpen->pMethods->xTruncate(pSubOpen, size);
648745de2e4Sdrh   quotaFile *pFile = p->pFile;
649745de2e4Sdrh   quotaGroup *pGroup;
650748a52cdSdrh   if( rc==SQLITE_OK ){
651748a52cdSdrh     quotaEnter();
652745de2e4Sdrh     pGroup = pFile->pGroup;
653745de2e4Sdrh     pGroup->iSize -= pFile->iSize;
654745de2e4Sdrh     pFile->iSize = size;
655748a52cdSdrh     pGroup->iSize += size;
656748a52cdSdrh     quotaLeave();
657748a52cdSdrh   }
658748a52cdSdrh   return rc;
659748a52cdSdrh }
660748a52cdSdrh 
661748a52cdSdrh /* Pass xSync requests through to the original VFS without change
662748a52cdSdrh */
663ef9659baSdrh static int quotaSync(sqlite3_file *pConn, int flags){
664ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
665745de2e4Sdrh   return pSubOpen->pMethods->xSync(pSubOpen, flags);
666748a52cdSdrh }
667748a52cdSdrh 
668748a52cdSdrh /* Pass xFileSize requests through to the original VFS but then
669748a52cdSdrh ** update the quotaGroup with the new size before returning.
670748a52cdSdrh */
671ef9659baSdrh static int quotaFileSize(sqlite3_file *pConn, sqlite3_int64 *pSize){
672ef9659baSdrh   quotaConn *p = (quotaConn*)pConn;
673ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
674745de2e4Sdrh   quotaFile *pFile = p->pFile;
675748a52cdSdrh   quotaGroup *pGroup;
676748a52cdSdrh   sqlite3_int64 sz;
677748a52cdSdrh   int rc;
678748a52cdSdrh 
679745de2e4Sdrh   rc = pSubOpen->pMethods->xFileSize(pSubOpen, &sz);
680748a52cdSdrh   if( rc==SQLITE_OK ){
681748a52cdSdrh     quotaEnter();
682745de2e4Sdrh     pGroup = pFile->pGroup;
683745de2e4Sdrh     pGroup->iSize -= pFile->iSize;
684745de2e4Sdrh     pFile->iSize = sz;
685748a52cdSdrh     pGroup->iSize += sz;
686748a52cdSdrh     quotaLeave();
687748a52cdSdrh     *pSize = sz;
688748a52cdSdrh   }
689748a52cdSdrh   return rc;
690748a52cdSdrh }
691748a52cdSdrh 
692748a52cdSdrh /* Pass xLock requests through to the original VFS unchanged.
693748a52cdSdrh */
694ef9659baSdrh static int quotaLock(sqlite3_file *pConn, int lock){
695ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
696745de2e4Sdrh   return pSubOpen->pMethods->xLock(pSubOpen, lock);
697748a52cdSdrh }
698748a52cdSdrh 
699748a52cdSdrh /* Pass xUnlock requests through to the original VFS unchanged.
700748a52cdSdrh */
701ef9659baSdrh static int quotaUnlock(sqlite3_file *pConn, int lock){
702ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
703745de2e4Sdrh   return pSubOpen->pMethods->xUnlock(pSubOpen, lock);
704748a52cdSdrh }
705748a52cdSdrh 
706748a52cdSdrh /* Pass xCheckReservedLock requests through to the original VFS unchanged.
707748a52cdSdrh */
708ef9659baSdrh static int quotaCheckReservedLock(sqlite3_file *pConn, int *pResOut){
709ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
710745de2e4Sdrh   return pSubOpen->pMethods->xCheckReservedLock(pSubOpen, pResOut);
711748a52cdSdrh }
712748a52cdSdrh 
713748a52cdSdrh /* Pass xFileControl requests through to the original VFS unchanged.
714748a52cdSdrh */
715ef9659baSdrh static int quotaFileControl(sqlite3_file *pConn, int op, void *pArg){
716ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
717745de2e4Sdrh   return pSubOpen->pMethods->xFileControl(pSubOpen, op, pArg);
718748a52cdSdrh }
719748a52cdSdrh 
720748a52cdSdrh /* Pass xSectorSize requests through to the original VFS unchanged.
721748a52cdSdrh */
722ef9659baSdrh static int quotaSectorSize(sqlite3_file *pConn){
723ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
724745de2e4Sdrh   return pSubOpen->pMethods->xSectorSize(pSubOpen);
725748a52cdSdrh }
726748a52cdSdrh 
727748a52cdSdrh /* Pass xDeviceCharacteristics requests through to the original VFS unchanged.
728748a52cdSdrh */
729ef9659baSdrh static int quotaDeviceCharacteristics(sqlite3_file *pConn){
730ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
731745de2e4Sdrh   return pSubOpen->pMethods->xDeviceCharacteristics(pSubOpen);
732748a52cdSdrh }
733748a52cdSdrh 
734748a52cdSdrh /* Pass xShmMap requests through to the original VFS unchanged.
735748a52cdSdrh */
736748a52cdSdrh static int quotaShmMap(
737ef9659baSdrh   sqlite3_file *pConn,            /* Handle open on database file */
738748a52cdSdrh   int iRegion,                    /* Region to retrieve */
739748a52cdSdrh   int szRegion,                   /* Size of regions */
740748a52cdSdrh   int bExtend,                    /* True to extend file if necessary */
741748a52cdSdrh   void volatile **pp              /* OUT: Mapped memory */
742748a52cdSdrh ){
743ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
744745de2e4Sdrh   return pSubOpen->pMethods->xShmMap(pSubOpen, iRegion, szRegion, bExtend, pp);
745748a52cdSdrh }
746748a52cdSdrh 
747748a52cdSdrh /* Pass xShmLock requests through to the original VFS unchanged.
748748a52cdSdrh */
749748a52cdSdrh static int quotaShmLock(
750ef9659baSdrh   sqlite3_file *pConn,       /* Database file holding the shared memory */
751748a52cdSdrh   int ofst,                  /* First lock to acquire or release */
752748a52cdSdrh   int n,                     /* Number of locks to acquire or release */
753748a52cdSdrh   int flags                  /* What to do with the lock */
754748a52cdSdrh ){
755ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
756745de2e4Sdrh   return pSubOpen->pMethods->xShmLock(pSubOpen, ofst, n, flags);
757748a52cdSdrh }
758748a52cdSdrh 
759748a52cdSdrh /* Pass xShmBarrier requests through to the original VFS unchanged.
760748a52cdSdrh */
761ef9659baSdrh static void quotaShmBarrier(sqlite3_file *pConn){
762ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
763745de2e4Sdrh   pSubOpen->pMethods->xShmBarrier(pSubOpen);
764748a52cdSdrh }
765748a52cdSdrh 
766748a52cdSdrh /* Pass xShmUnmap requests through to the original VFS unchanged.
767748a52cdSdrh */
768ef9659baSdrh static int quotaShmUnmap(sqlite3_file *pConn, int deleteFlag){
769ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
770745de2e4Sdrh   return pSubOpen->pMethods->xShmUnmap(pSubOpen, deleteFlag);
771748a52cdSdrh }
772748a52cdSdrh 
773748a52cdSdrh /************************** Public Interfaces *****************************/
774748a52cdSdrh /*
775748a52cdSdrh ** Initialize the quota VFS shim.  Use the VFS named zOrigVfsName
776748a52cdSdrh ** as the VFS that does the actual work.  Use the default if
777748a52cdSdrh ** zOrigVfsName==NULL.
778748a52cdSdrh **
779748a52cdSdrh ** The quota VFS shim is named "quota".  It will become the default
780748a52cdSdrh ** VFS if makeDefault is non-zero.
781748a52cdSdrh **
782748a52cdSdrh ** THIS ROUTINE IS NOT THREADSAFE.  Call this routine exactly once
783748a52cdSdrh ** during start-up.
784748a52cdSdrh */
785748a52cdSdrh int sqlite3_quota_initialize(const char *zOrigVfsName, int makeDefault){
786748a52cdSdrh   sqlite3_vfs *pOrigVfs;
7878cf35eb4Sdan   if( gQuota.isInitialized ) return SQLITE_MISUSE;
788748a52cdSdrh   pOrigVfs = sqlite3_vfs_find(zOrigVfsName);
789748a52cdSdrh   if( pOrigVfs==0 ) return SQLITE_ERROR;
7908cf35eb4Sdan   assert( pOrigVfs!=&gQuota.sThisVfs );
7918cf35eb4Sdan   gQuota.pMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
7928cf35eb4Sdan   if( !gQuota.pMutex ){
7938cf35eb4Sdan     return SQLITE_NOMEM;
7948cf35eb4Sdan   }
7958cf35eb4Sdan   gQuota.isInitialized = 1;
796748a52cdSdrh   gQuota.pOrigVfs = pOrigVfs;
797748a52cdSdrh   gQuota.sThisVfs = *pOrigVfs;
798748a52cdSdrh   gQuota.sThisVfs.xOpen = quotaOpen;
799796af156Sdrh   gQuota.sThisVfs.xDelete = quotaDelete;
800ef9659baSdrh   gQuota.sThisVfs.szOsFile += sizeof(quotaConn);
801748a52cdSdrh   gQuota.sThisVfs.zName = "quota";
802748a52cdSdrh   gQuota.sIoMethodsV1.iVersion = 1;
803748a52cdSdrh   gQuota.sIoMethodsV1.xClose = quotaClose;
804748a52cdSdrh   gQuota.sIoMethodsV1.xRead = quotaRead;
805748a52cdSdrh   gQuota.sIoMethodsV1.xWrite = quotaWrite;
806748a52cdSdrh   gQuota.sIoMethodsV1.xTruncate = quotaTruncate;
807748a52cdSdrh   gQuota.sIoMethodsV1.xSync = quotaSync;
808748a52cdSdrh   gQuota.sIoMethodsV1.xFileSize = quotaFileSize;
809748a52cdSdrh   gQuota.sIoMethodsV1.xLock = quotaLock;
810748a52cdSdrh   gQuota.sIoMethodsV1.xUnlock = quotaUnlock;
811748a52cdSdrh   gQuota.sIoMethodsV1.xCheckReservedLock = quotaCheckReservedLock;
812748a52cdSdrh   gQuota.sIoMethodsV1.xFileControl = quotaFileControl;
8138cf35eb4Sdan   gQuota.sIoMethodsV1.xSectorSize = quotaSectorSize;
814748a52cdSdrh   gQuota.sIoMethodsV1.xDeviceCharacteristics = quotaDeviceCharacteristics;
815748a52cdSdrh   gQuota.sIoMethodsV2 = gQuota.sIoMethodsV1;
816748a52cdSdrh   gQuota.sIoMethodsV2.iVersion = 2;
817748a52cdSdrh   gQuota.sIoMethodsV2.xShmMap = quotaShmMap;
818748a52cdSdrh   gQuota.sIoMethodsV2.xShmLock = quotaShmLock;
819748a52cdSdrh   gQuota.sIoMethodsV2.xShmBarrier = quotaShmBarrier;
820748a52cdSdrh   gQuota.sIoMethodsV2.xShmUnmap = quotaShmUnmap;
8218cf35eb4Sdan   sqlite3_vfs_register(&gQuota.sThisVfs, makeDefault);
8228cf35eb4Sdan   return SQLITE_OK;
823748a52cdSdrh }
824748a52cdSdrh 
825748a52cdSdrh /*
826748a52cdSdrh ** Shutdown the quota system.
827748a52cdSdrh **
828748a52cdSdrh ** All SQLite database connections must be closed before calling this
829748a52cdSdrh ** routine.
830748a52cdSdrh **
831796af156Sdrh ** THIS ROUTINE IS NOT THREADSAFE.  Call this routine exactly once while
832748a52cdSdrh ** shutting down in order to free all remaining quota groups.
833748a52cdSdrh */
834748a52cdSdrh int sqlite3_quota_shutdown(void){
83575c19174Sdrh   quotaGroup *pGroup;
836748a52cdSdrh   if( gQuota.isInitialized==0 ) return SQLITE_MISUSE;
83775c19174Sdrh   for(pGroup=gQuota.pGroup; pGroup; pGroup=pGroup->pNext){
838796af156Sdrh     if( quotaGroupOpenFileCount(pGroup)>0 ) return SQLITE_MISUSE;
8398cf35eb4Sdan   }
8408cf35eb4Sdan   while( gQuota.pGroup ){
84175c19174Sdrh     pGroup = gQuota.pGroup;
84275c19174Sdrh     gQuota.pGroup = pGroup->pNext;
8439fc7702cSdan     pGroup->iLimit = 0;
844796af156Sdrh     assert( quotaGroupOpenFileCount(pGroup)==0 );
8459fc7702cSdan     quotaGroupDeref(pGroup);
8468cf35eb4Sdan   }
847748a52cdSdrh   gQuota.isInitialized = 0;
848748a52cdSdrh   sqlite3_mutex_free(gQuota.pMutex);
8498cf35eb4Sdan   sqlite3_vfs_unregister(&gQuota.sThisVfs);
850748a52cdSdrh   memset(&gQuota, 0, sizeof(gQuota));
8518cf35eb4Sdan   return SQLITE_OK;
852748a52cdSdrh }
853748a52cdSdrh 
854748a52cdSdrh /*
855748a52cdSdrh ** Create or destroy a quota group.
856748a52cdSdrh **
857748a52cdSdrh ** The quota group is defined by the zPattern.  When calling this routine
858748a52cdSdrh ** with a zPattern for a quota group that already exists, this routine
859748a52cdSdrh ** merely updates the iLimit, xCallback, and pArg values for that quota
860748a52cdSdrh ** group.  If zPattern is new, then a new quota group is created.
861748a52cdSdrh **
862748a52cdSdrh ** If the iLimit for a quota group is set to zero, then the quota group
863748a52cdSdrh ** is disabled and will be deleted when the last database connection using
864748a52cdSdrh ** the quota group is closed.
865748a52cdSdrh **
866748a52cdSdrh ** Calling this routine on a zPattern that does not exist and with a
867748a52cdSdrh ** zero iLimit is a no-op.
868748a52cdSdrh **
869748a52cdSdrh ** A quota group must exist with a non-zero iLimit prior to opening
870748a52cdSdrh ** database connections if those connections are to participate in the
8718cf35eb4Sdan ** quota group.  Creating a quota group does not affect database connections
872748a52cdSdrh ** that are already open.
873748a52cdSdrh */
874748a52cdSdrh int sqlite3_quota_set(
875748a52cdSdrh   const char *zPattern,           /* The filename pattern */
876748a52cdSdrh   sqlite3_int64 iLimit,           /* New quota to set for this quota group */
877748a52cdSdrh   void (*xCallback)(              /* Callback invoked when going over quota */
878748a52cdSdrh      const char *zFilename,         /* Name of file whose size increases */
879748a52cdSdrh      sqlite3_int64 *piLimit,        /* IN/OUT: The current limit */
880748a52cdSdrh      sqlite3_int64 iSize,           /* Total size of all files in the group */
881748a52cdSdrh      void *pArg                     /* Client data */
8828cf35eb4Sdan   ),
883ef9659baSdrh   void *pArg,                     /* client data passed thru to callback */
884ef9659baSdrh   void (*xDestroy)(void*)         /* Optional destructor for pArg */
885748a52cdSdrh ){
886748a52cdSdrh   quotaGroup *pGroup;
887748a52cdSdrh   quotaEnter();
888748a52cdSdrh   pGroup = gQuota.pGroup;
889748a52cdSdrh   while( pGroup && strcmp(pGroup->zPattern, zPattern)!=0 ){
890748a52cdSdrh     pGroup = pGroup->pNext;
891748a52cdSdrh   }
89261cc423aSdrh   if( pGroup==0 ){
893748a52cdSdrh     int nPattern = strlen(zPattern);
89461cc423aSdrh     if( iLimit<=0 ){
89561cc423aSdrh       quotaLeave();
89661cc423aSdrh       return SQLITE_OK;
89761cc423aSdrh     }
898b0a32832Sdan     pGroup = (quotaGroup *)sqlite3_malloc( sizeof(*pGroup) + nPattern + 1 );
899748a52cdSdrh     if( pGroup==0 ){
900748a52cdSdrh       quotaLeave();
901748a52cdSdrh       return SQLITE_NOMEM;
902748a52cdSdrh     }
903748a52cdSdrh     memset(pGroup, 0, sizeof(*pGroup));
904748a52cdSdrh     pGroup->zPattern = (char*)&pGroup[1];
9058cf35eb4Sdan     memcpy((char *)pGroup->zPattern, zPattern, nPattern+1);
906748a52cdSdrh     if( gQuota.pGroup ) gQuota.pGroup->ppPrev = &pGroup->pNext;
9078cf35eb4Sdan     pGroup->pNext = gQuota.pGroup;
9088cf35eb4Sdan     pGroup->ppPrev = &gQuota.pGroup;
9098cf35eb4Sdan     gQuota.pGroup = pGroup;
910748a52cdSdrh   }
911748a52cdSdrh   pGroup->iLimit = iLimit;
912748a52cdSdrh   pGroup->xCallback = xCallback;
913ef9659baSdrh   if( pGroup->xDestroy && pGroup->pArg!=pArg ){
914ef9659baSdrh     pGroup->xDestroy(pGroup->pArg);
915ef9659baSdrh   }
916748a52cdSdrh   pGroup->pArg = pArg;
917ef9659baSdrh   pGroup->xDestroy = xDestroy;
918748a52cdSdrh   quotaGroupDeref(pGroup);
919748a52cdSdrh   quotaLeave();
920748a52cdSdrh   return SQLITE_OK;
921748a52cdSdrh }
922748a52cdSdrh 
923b6020c4eSdrh /*
924b6020c4eSdrh ** Bring the named file under quota management.  Or if it is already under
925b6020c4eSdrh ** management, update its size.
926b6020c4eSdrh */
927b6020c4eSdrh int sqlite3_quota_file(const char *zFilename){
928b6020c4eSdrh   char *zFull;
929b6020c4eSdrh   sqlite3_file *fd;
930b6020c4eSdrh   int rc;
931b6020c4eSdrh   int outFlags = 0;
932b6020c4eSdrh   sqlite3_int64 iSize;
933663cebfeSdrh   fd = (sqlite3_file*)sqlite3_malloc(gQuota.sThisVfs.szOsFile +
934663cebfeSdrh                                      gQuota.sThisVfs.mxPathname+1);
935b6020c4eSdrh   if( fd==0 ) return SQLITE_NOMEM;
936b6020c4eSdrh   zFull = gQuota.sThisVfs.szOsFile + (char*)fd;
937b6020c4eSdrh   rc = gQuota.pOrigVfs->xFullPathname(gQuota.pOrigVfs, zFilename,
938b6020c4eSdrh                                       gQuota.sThisVfs.mxPathname+1, zFull);
939b6020c4eSdrh   if( rc==SQLITE_OK ){
940b6020c4eSdrh     rc = quotaOpen(&gQuota.sThisVfs, zFull, fd,
941b6020c4eSdrh                    SQLITE_OPEN_READONLY | SQLITE_OPEN_MAIN_DB, &outFlags);
942b6020c4eSdrh   }
943b6020c4eSdrh   if( rc==SQLITE_OK ){
94426541c9aSdrh     fd->pMethods->xFileSize(fd, &iSize);
94526541c9aSdrh     fd->pMethods->xClose(fd);
946b6020c4eSdrh   }else if( rc==SQLITE_CANTOPEN ){
947b6020c4eSdrh     quotaGroup *pGroup;
948b6020c4eSdrh     quotaFile *pFile;
949b6020c4eSdrh     quotaEnter();
950b6020c4eSdrh     pGroup = quotaGroupFind(zFull);
951b6020c4eSdrh     if( pGroup ){
95260f21e4bSdrh       pFile = quotaFindFile(pGroup, zFull, 0);
953b6020c4eSdrh       if( pFile ) quotaRemoveFile(pFile);
954b6020c4eSdrh     }
955b6020c4eSdrh     quotaLeave();
956b6020c4eSdrh   }
957b6020c4eSdrh   sqlite3_free(fd);
958b6020c4eSdrh   return rc;
959b6020c4eSdrh }
960b6020c4eSdrh 
96160f21e4bSdrh /*
96260f21e4bSdrh ** Open a potentially quotaed file for I/O.
96360f21e4bSdrh */
96460f21e4bSdrh quota_FILE *sqlite3_quota_fopen(const char *zFilename, const char *zMode){
96560f21e4bSdrh   quota_FILE *p = 0;
96660f21e4bSdrh   char *zFull = 0;
967a76e8589Sdrh   char *zFullTranslated;
96860f21e4bSdrh   int rc;
96960f21e4bSdrh   quotaGroup *pGroup;
97060f21e4bSdrh   quotaFile *pFile;
97160f21e4bSdrh 
972663cebfeSdrh   zFull = (char*)sqlite3_malloc(gQuota.sThisVfs.mxPathname + 1);
973a76e8589Sdrh   if( zFull==0 ) return 0;
97460f21e4bSdrh   rc = gQuota.pOrigVfs->xFullPathname(gQuota.pOrigVfs, zFilename,
97560f21e4bSdrh                                       gQuota.sThisVfs.mxPathname+1, zFull);
97660f21e4bSdrh   if( rc ) goto quota_fopen_error;
977663cebfeSdrh   p = (quota_FILE*)sqlite3_malloc(sizeof(*p));
97860f21e4bSdrh   if( p==0 ) goto quota_fopen_error;
97960f21e4bSdrh   memset(p, 0, sizeof(*p));
980a76e8589Sdrh   zFullTranslated = quota_utf8_to_mbcs(zFull);
981a76e8589Sdrh   if( zFullTranslated==0 ) goto quota_fopen_error;
982a76e8589Sdrh   p->f = fopen(zFullTranslated, zMode);
983a76e8589Sdrh   quota_mbcs_free(zFullTranslated);
98460f21e4bSdrh   if( p->f==0 ) goto quota_fopen_error;
98560f21e4bSdrh   quotaEnter();
98660f21e4bSdrh   pGroup = quotaGroupFind(zFull);
98760f21e4bSdrh   if( pGroup ){
98860f21e4bSdrh     pFile = quotaFindFile(pGroup, zFull, 1);
98960f21e4bSdrh     if( pFile==0 ){
99060f21e4bSdrh       quotaLeave();
99160f21e4bSdrh       goto quota_fopen_error;
99260f21e4bSdrh     }
99360f21e4bSdrh     pFile->nRef++;
99460f21e4bSdrh     p->pFile = pFile;
99560f21e4bSdrh   }
99660f21e4bSdrh   quotaLeave();
99760f21e4bSdrh   sqlite3_free(zFull);
99860f21e4bSdrh   return p;
99960f21e4bSdrh 
100060f21e4bSdrh quota_fopen_error:
100160f21e4bSdrh   sqlite3_free(zFull);
100260f21e4bSdrh   if( p && p->f ) fclose(p->f);
100360f21e4bSdrh   sqlite3_free(p);
100460f21e4bSdrh   return 0;
100560f21e4bSdrh }
100660f21e4bSdrh 
100760f21e4bSdrh /*
100860f21e4bSdrh ** Read content from a quota_FILE
100960f21e4bSdrh */
101060f21e4bSdrh size_t sqlite3_quota_fread(
101160f21e4bSdrh   void *pBuf,            /* Store the content here */
101260f21e4bSdrh   size_t size,           /* Size of each element */
101360f21e4bSdrh   size_t nmemb,          /* Number of elements to read */
101460f21e4bSdrh   quota_FILE *p          /* Read from this quota_FILE object */
101560f21e4bSdrh ){
101660f21e4bSdrh   return fread(pBuf, size, nmemb, p->f);
101760f21e4bSdrh }
101860f21e4bSdrh 
101960f21e4bSdrh /*
102060f21e4bSdrh ** Write content into a quota_FILE.  Invoke the quota callback and block
102160f21e4bSdrh ** the write if we exceed quota.
102260f21e4bSdrh */
102360f21e4bSdrh size_t sqlite3_quota_fwrite(
102460f21e4bSdrh   void *pBuf,            /* Take content to write from here */
102560f21e4bSdrh   size_t size,           /* Size of each element */
102660f21e4bSdrh   size_t nmemb,          /* Number of elements */
102760f21e4bSdrh   quota_FILE *p          /* Write to this quota_FILE objecct */
102860f21e4bSdrh ){
102960f21e4bSdrh   sqlite3_int64 iOfst;
103060f21e4bSdrh   sqlite3_int64 iEnd;
103160f21e4bSdrh   sqlite3_int64 szNew;
103260f21e4bSdrh   quotaFile *pFile;
103360f21e4bSdrh 
103460f21e4bSdrh   iOfst = ftell(p->f);
103560f21e4bSdrh   iEnd = iOfst + size*nmemb;
103660f21e4bSdrh   pFile = p->pFile;
1037c8ccda60Sdrh   if( pFile && pFile->iSize<iEnd ){
103860f21e4bSdrh     quotaGroup *pGroup = pFile->pGroup;
103960f21e4bSdrh     quotaEnter();
104060f21e4bSdrh     szNew = pGroup->iSize - pFile->iSize + iEnd;
104160f21e4bSdrh     if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){
104260f21e4bSdrh       if( pGroup->xCallback ){
104360f21e4bSdrh         pGroup->xCallback(pFile->zFilename, &pGroup->iLimit, szNew,
104460f21e4bSdrh                           pGroup->pArg);
104560f21e4bSdrh       }
104660f21e4bSdrh       if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){
104760f21e4bSdrh         iEnd = pGroup->iLimit - pGroup->iSize + pFile->iSize;
104860f21e4bSdrh         nmemb = (iEnd - iOfst)/size;
104960f21e4bSdrh         iEnd = iOfst + size*nmemb;
105060f21e4bSdrh         szNew = pGroup->iSize - pFile->iSize + iEnd;
105160f21e4bSdrh       }
105260f21e4bSdrh     }
105360f21e4bSdrh     pGroup->iSize = szNew;
105460f21e4bSdrh     pFile->iSize = iEnd;
105560f21e4bSdrh     quotaLeave();
105660f21e4bSdrh   }
105760f21e4bSdrh   return fwrite(pBuf, size, nmemb, p->f);
105860f21e4bSdrh }
105960f21e4bSdrh 
106060f21e4bSdrh /*
106160f21e4bSdrh ** Close an open quota_FILE stream.
106260f21e4bSdrh */
106360f21e4bSdrh int sqlite3_quota_fclose(quota_FILE *p){
106460f21e4bSdrh   int rc;
106560f21e4bSdrh   quotaFile *pFile;
106660f21e4bSdrh   rc = fclose(p->f);
106760f21e4bSdrh   pFile = p->pFile;
1068c8ccda60Sdrh   if( pFile ){
106960f21e4bSdrh     quotaEnter();
107060f21e4bSdrh     pFile->nRef--;
107160f21e4bSdrh     if( pFile->nRef==0 ){
107260f21e4bSdrh       quotaGroup *pGroup = pFile->pGroup;
107369b2232dSdrh       if( pFile->deleteOnClose ){
107469b2232dSdrh         gQuota.pOrigVfs->xDelete(gQuota.pOrigVfs, pFile->zFilename, 0);
107569b2232dSdrh         quotaRemoveFile(pFile);
107669b2232dSdrh       }
107760f21e4bSdrh       quotaGroupDeref(pGroup);
107860f21e4bSdrh     }
107960f21e4bSdrh     quotaLeave();
1080c8ccda60Sdrh   }
108160f21e4bSdrh   sqlite3_free(p);
108260f21e4bSdrh   return rc;
108360f21e4bSdrh }
108460f21e4bSdrh 
108560f21e4bSdrh /*
108669b2232dSdrh ** Flush memory buffers for a quota_FILE to disk.
108769b2232dSdrh */
108827cec37dSdrh int sqlite3_quota_fflush(quota_FILE *p, int doFsync){
108927cec37dSdrh   int rc;
109027cec37dSdrh   rc = fflush(p->f);
109127cec37dSdrh   if( rc==0 && doFsync ){
109227cec37dSdrh #if SQLITE_OS_UNIX
109327cec37dSdrh     rc = fsync(fileno(p->f));
109427cec37dSdrh #endif
109527cec37dSdrh #if SQLITE_OS_WIN
109627cec37dSdrh     rc = 0==FlushFileBuffers((HANDLE)_fileno(p->f));
109727cec37dSdrh #endif
109827cec37dSdrh   }
109927cec37dSdrh   return rc;
110069b2232dSdrh }
110169b2232dSdrh 
110269b2232dSdrh /*
110360f21e4bSdrh ** Seek on a quota_FILE stream.
110460f21e4bSdrh */
110560f21e4bSdrh int sqlite3_quota_fseek(quota_FILE *p, long offset, int whence){
110660f21e4bSdrh   return fseek(p->f, offset, whence);
110760f21e4bSdrh }
110860f21e4bSdrh 
110960f21e4bSdrh /*
111060f21e4bSdrh ** rewind a quota_FILE stream.
111160f21e4bSdrh */
111260f21e4bSdrh void sqlite3_quota_rewind(quota_FILE *p){
111360f21e4bSdrh   rewind(p->f);
111460f21e4bSdrh }
111560f21e4bSdrh 
111660f21e4bSdrh /*
111760f21e4bSdrh ** Tell the current location of a quota_FILE stream.
111860f21e4bSdrh */
111960f21e4bSdrh long sqlite3_quota_ftell(quota_FILE *p){
112060f21e4bSdrh   return ftell(p->f);
112160f21e4bSdrh }
112260f21e4bSdrh 
112360f21e4bSdrh /*
112469b2232dSdrh ** Remove a managed file.  Update quotas accordingly.
112560f21e4bSdrh */
112660f21e4bSdrh int sqlite3_quota_remove(const char *zFilename){
112769b2232dSdrh   char *zFull;            /* Full pathname for zFilename */
112869b2232dSdrh   int nFull;              /* Number of bytes in zFilename */
112969b2232dSdrh   int rc;                 /* Result code */
113069b2232dSdrh   quotaGroup *pGroup;     /* Group containing zFilename */
113169b2232dSdrh   quotaFile *pFile;       /* A file in the group */
113269b2232dSdrh   quotaFile *pNextFile;   /* next file in the group */
113369b2232dSdrh   int diff;               /* Difference between filenames */
113469b2232dSdrh   char c;                 /* First character past end of pattern */
113569b2232dSdrh 
1136663cebfeSdrh   zFull = (char*)sqlite3_malloc(gQuota.sThisVfs.mxPathname + 1);
113769b2232dSdrh   if( zFull==0 ) return SQLITE_NOMEM;
113869b2232dSdrh   rc = gQuota.pOrigVfs->xFullPathname(gQuota.pOrigVfs, zFilename,
113969b2232dSdrh                                       gQuota.sThisVfs.mxPathname+1, zFull);
114069b2232dSdrh   if( rc ){
114169b2232dSdrh     sqlite3_free(zFull);
114260f21e4bSdrh     return rc;
114360f21e4bSdrh   }
114460f21e4bSdrh 
114569b2232dSdrh   /* Figure out the length of the full pathname.  If the name ends with
114669b2232dSdrh   ** / (or \ on windows) then remove the trailing /.
114769b2232dSdrh   */
114869b2232dSdrh   nFull = strlen(zFull);
114969b2232dSdrh   if( nFull>0 && (zFull[nFull-1]=='/' || zFull[nFull-1]=='\\') ){
115069b2232dSdrh     nFull--;
115169b2232dSdrh     zFull[nFull] = 0;
115269b2232dSdrh   }
115369b2232dSdrh 
115469b2232dSdrh   quotaEnter();
115569b2232dSdrh   pGroup = quotaGroupFind(zFull);
115669b2232dSdrh   if( pGroup ){
115769b2232dSdrh     for(pFile=pGroup->pFiles; pFile && rc==SQLITE_OK; pFile=pNextFile){
115869b2232dSdrh       pNextFile = pFile->pNext;
115969b2232dSdrh       diff = memcmp(zFull, pFile->zFilename, nFull);
116069b2232dSdrh       if( diff==0 && ((c = pFile->zFilename[nFull])==0 || c=='/' || c=='\\') ){
116169b2232dSdrh         if( pFile->nRef ){
116269b2232dSdrh           pFile->deleteOnClose = 1;
116369b2232dSdrh         }else{
116469b2232dSdrh           rc = gQuota.pOrigVfs->xDelete(gQuota.pOrigVfs, pFile->zFilename, 0);
116569b2232dSdrh           quotaRemoveFile(pFile);
116669b2232dSdrh           quotaGroupDeref(pGroup);
116769b2232dSdrh         }
116869b2232dSdrh       }
116969b2232dSdrh     }
117069b2232dSdrh   }
117169b2232dSdrh   quotaLeave();
117269b2232dSdrh   sqlite3_free(zFull);
117369b2232dSdrh   return rc;
117469b2232dSdrh }
1175748a52cdSdrh 
1176748a52cdSdrh /***************************** Test Code ***********************************/
1177748a52cdSdrh #ifdef SQLITE_TEST
1178748a52cdSdrh #include <tcl.h>
11798cf35eb4Sdan 
1180ef9659baSdrh /*
1181ef9659baSdrh ** Argument passed to a TCL quota-over-limit callback.
1182ef9659baSdrh */
11838cf35eb4Sdan typedef struct TclQuotaCallback TclQuotaCallback;
11848cf35eb4Sdan struct TclQuotaCallback {
1185ef9659baSdrh   Tcl_Interp *interp;    /* Interpreter in which to run the script */
1186ef9659baSdrh   Tcl_Obj *pScript;      /* Script to be run */
11878cf35eb4Sdan };
11888cf35eb4Sdan 
11898cf35eb4Sdan extern const char *sqlite3TestErrorName(int);
11908cf35eb4Sdan 
1191ef9659baSdrh 
1192ef9659baSdrh /*
1193ef9659baSdrh ** This is the callback from a quota-over-limit.
1194ef9659baSdrh */
11958cf35eb4Sdan static void tclQuotaCallback(
11968cf35eb4Sdan   const char *zFilename,          /* Name of file whose size increases */
11978cf35eb4Sdan   sqlite3_int64 *piLimit,         /* IN/OUT: The current limit */
11988cf35eb4Sdan   sqlite3_int64 iSize,            /* Total size of all files in the group */
11998cf35eb4Sdan   void *pArg                      /* Client data */
12008cf35eb4Sdan ){
12018cf35eb4Sdan   TclQuotaCallback *p;            /* Callback script object */
12028cf35eb4Sdan   Tcl_Obj *pEval;                 /* Script to evaluate */
12038cf35eb4Sdan   Tcl_Obj *pVarname;              /* Name of variable to pass as 2nd arg */
12048cf35eb4Sdan   unsigned int rnd;               /* Random part of pVarname */
12058cf35eb4Sdan   int rc;                         /* Tcl error code */
12068cf35eb4Sdan 
12078cf35eb4Sdan   p = (TclQuotaCallback *)pArg;
12083d3e60e4Sdrh   if( p==0 ) return;
12098cf35eb4Sdan 
12108cf35eb4Sdan   pVarname = Tcl_NewStringObj("::piLimit_", -1);
12118cf35eb4Sdan   Tcl_IncrRefCount(pVarname);
12128cf35eb4Sdan   sqlite3_randomness(sizeof(rnd), (void *)&rnd);
12138cf35eb4Sdan   Tcl_AppendObjToObj(pVarname, Tcl_NewIntObj((int)(rnd&0x7FFFFFFF)));
12148cf35eb4Sdan   Tcl_ObjSetVar2(p->interp, pVarname, 0, Tcl_NewWideIntObj(*piLimit), 0);
12158cf35eb4Sdan 
12168cf35eb4Sdan   pEval = Tcl_DuplicateObj(p->pScript);
12178cf35eb4Sdan   Tcl_IncrRefCount(pEval);
12188cf35eb4Sdan   Tcl_ListObjAppendElement(0, pEval, Tcl_NewStringObj(zFilename, -1));
12198cf35eb4Sdan   Tcl_ListObjAppendElement(0, pEval, pVarname);
12208cf35eb4Sdan   Tcl_ListObjAppendElement(0, pEval, Tcl_NewWideIntObj(iSize));
12218cf35eb4Sdan   rc = Tcl_EvalObjEx(p->interp, pEval, TCL_EVAL_GLOBAL);
12228cf35eb4Sdan 
12238cf35eb4Sdan   if( rc==TCL_OK ){
12248cf35eb4Sdan     Tcl_Obj *pLimit = Tcl_ObjGetVar2(p->interp, pVarname, 0, 0);
12258cf35eb4Sdan     rc = Tcl_GetWideIntFromObj(p->interp, pLimit, piLimit);
12268cf35eb4Sdan     Tcl_UnsetVar(p->interp, Tcl_GetString(pVarname), 0);
12278cf35eb4Sdan   }
12288cf35eb4Sdan 
12298cf35eb4Sdan   Tcl_DecrRefCount(pEval);
12308cf35eb4Sdan   Tcl_DecrRefCount(pVarname);
12318cf35eb4Sdan   if( rc!=TCL_OK ) Tcl_BackgroundError(p->interp);
12328cf35eb4Sdan }
12338cf35eb4Sdan 
12348cf35eb4Sdan /*
1235ef9659baSdrh ** Destructor for a TCL quota-over-limit callback.
1236ef9659baSdrh */
1237ef9659baSdrh static void tclCallbackDestructor(void *pObj){
1238ef9659baSdrh   TclQuotaCallback *p = (TclQuotaCallback*)pObj;
1239ef9659baSdrh   if( p ){
1240ef9659baSdrh     Tcl_DecrRefCount(p->pScript);
12419fc7702cSdan     sqlite3_free((char *)p);
1242ef9659baSdrh   }
1243ef9659baSdrh }
1244ef9659baSdrh 
1245ef9659baSdrh /*
12468cf35eb4Sdan ** tclcmd: sqlite3_quota_initialize NAME MAKEDEFAULT
12478cf35eb4Sdan */
12488cf35eb4Sdan static int test_quota_initialize(
12498cf35eb4Sdan   void * clientData,
12508cf35eb4Sdan   Tcl_Interp *interp,
12518cf35eb4Sdan   int objc,
12528cf35eb4Sdan   Tcl_Obj *CONST objv[]
12538cf35eb4Sdan ){
12548cf35eb4Sdan   const char *zName;              /* Name of new quota VFS */
12558cf35eb4Sdan   int makeDefault;                /* True to make the new VFS the default */
12568cf35eb4Sdan   int rc;                         /* Value returned by quota_initialize() */
12578cf35eb4Sdan 
12588cf35eb4Sdan   /* Process arguments */
12598cf35eb4Sdan   if( objc!=3 ){
12608cf35eb4Sdan     Tcl_WrongNumArgs(interp, 1, objv, "NAME MAKEDEFAULT");
12618cf35eb4Sdan     return TCL_ERROR;
12628cf35eb4Sdan   }
12638cf35eb4Sdan   zName = Tcl_GetString(objv[1]);
12648cf35eb4Sdan   if( Tcl_GetBooleanFromObj(interp, objv[2], &makeDefault) ) return TCL_ERROR;
12658cf35eb4Sdan   if( zName[0]=='\0' ) zName = 0;
12668cf35eb4Sdan 
12678cf35eb4Sdan   /* Call sqlite3_quota_initialize() */
12688cf35eb4Sdan   rc = sqlite3_quota_initialize(zName, makeDefault);
12698cf35eb4Sdan   Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC);
12708cf35eb4Sdan 
12718cf35eb4Sdan   return TCL_OK;
12728cf35eb4Sdan }
12738cf35eb4Sdan 
12748cf35eb4Sdan /*
12758cf35eb4Sdan ** tclcmd: sqlite3_quota_shutdown
12768cf35eb4Sdan */
12778cf35eb4Sdan static int test_quota_shutdown(
12788cf35eb4Sdan   void * clientData,
12798cf35eb4Sdan   Tcl_Interp *interp,
12808cf35eb4Sdan   int objc,
12818cf35eb4Sdan   Tcl_Obj *CONST objv[]
12828cf35eb4Sdan ){
12838cf35eb4Sdan   int rc;                         /* Value returned by quota_shutdown() */
12848cf35eb4Sdan 
12858cf35eb4Sdan   if( objc!=1 ){
12868cf35eb4Sdan     Tcl_WrongNumArgs(interp, 1, objv, "");
12878cf35eb4Sdan     return TCL_ERROR;
12888cf35eb4Sdan   }
12898cf35eb4Sdan 
12908cf35eb4Sdan   /* Call sqlite3_quota_shutdown() */
12918cf35eb4Sdan   rc = sqlite3_quota_shutdown();
12928cf35eb4Sdan   Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC);
12938cf35eb4Sdan 
12948cf35eb4Sdan   return TCL_OK;
12958cf35eb4Sdan }
12968cf35eb4Sdan 
12978cf35eb4Sdan /*
12988cf35eb4Sdan ** tclcmd: sqlite3_quota_set PATTERN LIMIT SCRIPT
12998cf35eb4Sdan */
13008cf35eb4Sdan static int test_quota_set(
13018cf35eb4Sdan   void * clientData,
13028cf35eb4Sdan   Tcl_Interp *interp,
13038cf35eb4Sdan   int objc,
13048cf35eb4Sdan   Tcl_Obj *CONST objv[]
13058cf35eb4Sdan ){
13068cf35eb4Sdan   const char *zPattern;           /* File pattern to configure */
13078cf35eb4Sdan   sqlite3_int64 iLimit;           /* Initial quota in bytes */
13088cf35eb4Sdan   Tcl_Obj *pScript;               /* Tcl script to invoke to increase quota */
13098cf35eb4Sdan   int rc;                         /* Value returned by quota_set() */
13108cf35eb4Sdan   TclQuotaCallback *p;            /* Callback object */
1311ef9659baSdrh   int nScript;                    /* Length of callback script */
13129fc7702cSdan   void (*xDestroy)(void*);        /* Optional destructor for pArg */
131306bfd36aSdan   void (*xCallback)(const char *, sqlite3_int64 *, sqlite3_int64, void *);
13148cf35eb4Sdan 
13158cf35eb4Sdan   /* Process arguments */
13168cf35eb4Sdan   if( objc!=4 ){
13178cf35eb4Sdan     Tcl_WrongNumArgs(interp, 1, objv, "PATTERN LIMIT SCRIPT");
13188cf35eb4Sdan     return TCL_ERROR;
13198cf35eb4Sdan   }
13208cf35eb4Sdan   zPattern = Tcl_GetString(objv[1]);
13218cf35eb4Sdan   if( Tcl_GetWideIntFromObj(interp, objv[2], &iLimit) ) return TCL_ERROR;
13228cf35eb4Sdan   pScript = objv[3];
1323ef9659baSdrh   Tcl_GetStringFromObj(pScript, &nScript);
13248cf35eb4Sdan 
1325ef9659baSdrh   if( nScript>0 ){
13268cf35eb4Sdan     /* Allocate a TclQuotaCallback object */
13279fc7702cSdan     p = (TclQuotaCallback *)sqlite3_malloc(sizeof(TclQuotaCallback));
13289fc7702cSdan     if( !p ){
13299fc7702cSdan       Tcl_SetResult(interp, (char *)"SQLITE_NOMEM", TCL_STATIC);
13309fc7702cSdan       return TCL_OK;
13319fc7702cSdan     }
13328cf35eb4Sdan     memset(p, 0, sizeof(TclQuotaCallback));
13338cf35eb4Sdan     p->interp = interp;
1334ef9659baSdrh     Tcl_IncrRefCount(pScript);
13358cf35eb4Sdan     p->pScript = pScript;
13369fc7702cSdan     xDestroy = tclCallbackDestructor;
133706bfd36aSdan     xCallback = tclQuotaCallback;
1338ef9659baSdrh   }else{
1339ef9659baSdrh     p = 0;
13409fc7702cSdan     xDestroy = 0;
134106bfd36aSdan     xCallback = 0;
13428cf35eb4Sdan   }
13439fc7702cSdan 
1344ef9659baSdrh   /* Invoke sqlite3_quota_set() */
134506bfd36aSdan   rc = sqlite3_quota_set(zPattern, iLimit, xCallback, (void*)p, xDestroy);
13468cf35eb4Sdan 
13478cf35eb4Sdan   Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC);
13488cf35eb4Sdan   return TCL_OK;
13498cf35eb4Sdan }
13508cf35eb4Sdan 
13518cf35eb4Sdan /*
1352b6020c4eSdrh ** tclcmd: sqlite3_quota_file FILENAME
1353b6020c4eSdrh */
1354b6020c4eSdrh static int test_quota_file(
1355b6020c4eSdrh   void * clientData,
1356b6020c4eSdrh   Tcl_Interp *interp,
1357b6020c4eSdrh   int objc,
1358b6020c4eSdrh   Tcl_Obj *CONST objv[]
1359b6020c4eSdrh ){
1360b6020c4eSdrh   const char *zFilename;          /* File pattern to configure */
1361b6020c4eSdrh   int rc;                         /* Value returned by quota_file() */
1362b6020c4eSdrh 
1363b6020c4eSdrh   /* Process arguments */
1364b6020c4eSdrh   if( objc!=2 ){
1365b6020c4eSdrh     Tcl_WrongNumArgs(interp, 1, objv, "FILENAME");
1366b6020c4eSdrh     return TCL_ERROR;
1367b6020c4eSdrh   }
1368b6020c4eSdrh   zFilename = Tcl_GetString(objv[1]);
1369b6020c4eSdrh 
1370b6020c4eSdrh   /* Invoke sqlite3_quota_file() */
1371b6020c4eSdrh   rc = sqlite3_quota_file(zFilename);
1372b6020c4eSdrh 
1373b6020c4eSdrh   Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC);
1374b6020c4eSdrh   return TCL_OK;
1375b6020c4eSdrh }
1376b6020c4eSdrh 
1377b6020c4eSdrh /*
1378ef9659baSdrh ** tclcmd:  sqlite3_quota_dump
1379ef9659baSdrh */
1380ef9659baSdrh static int test_quota_dump(
1381ef9659baSdrh   void * clientData,
1382ef9659baSdrh   Tcl_Interp *interp,
1383ef9659baSdrh   int objc,
1384ef9659baSdrh   Tcl_Obj *CONST objv[]
1385ef9659baSdrh ){
1386ef9659baSdrh   Tcl_Obj *pResult;
1387ef9659baSdrh   Tcl_Obj *pGroupTerm;
1388ef9659baSdrh   Tcl_Obj *pFileTerm;
1389ef9659baSdrh   quotaGroup *pGroup;
1390ef9659baSdrh   quotaFile *pFile;
1391ef9659baSdrh 
1392ef9659baSdrh   pResult = Tcl_NewObj();
1393ef9659baSdrh   quotaEnter();
1394ef9659baSdrh   for(pGroup=gQuota.pGroup; pGroup; pGroup=pGroup->pNext){
1395ef9659baSdrh     pGroupTerm = Tcl_NewObj();
1396ef9659baSdrh     Tcl_ListObjAppendElement(interp, pGroupTerm,
1397ef9659baSdrh           Tcl_NewStringObj(pGroup->zPattern, -1));
1398ef9659baSdrh     Tcl_ListObjAppendElement(interp, pGroupTerm,
1399ef9659baSdrh           Tcl_NewWideIntObj(pGroup->iLimit));
1400ef9659baSdrh     Tcl_ListObjAppendElement(interp, pGroupTerm,
1401ef9659baSdrh           Tcl_NewWideIntObj(pGroup->iSize));
140275c19174Sdrh     for(pFile=pGroup->pFiles; pFile; pFile=pFile->pNext){
1403c8ccda60Sdrh       int i;
1404c8ccda60Sdrh       char zTemp[1000];
1405ef9659baSdrh       pFileTerm = Tcl_NewObj();
1406c8ccda60Sdrh       sqlite3_snprintf(sizeof(zTemp), zTemp, "%s", pFile->zFilename);
1407c8ccda60Sdrh       for(i=0; zTemp[i]; i++){ if( zTemp[i]=='\\' ) zTemp[i] = '/'; }
1408ef9659baSdrh       Tcl_ListObjAppendElement(interp, pFileTerm,
1409c8ccda60Sdrh             Tcl_NewStringObj(zTemp, -1));
1410ef9659baSdrh       Tcl_ListObjAppendElement(interp, pFileTerm,
1411ef9659baSdrh             Tcl_NewWideIntObj(pFile->iSize));
1412ef9659baSdrh       Tcl_ListObjAppendElement(interp, pFileTerm,
1413ef9659baSdrh             Tcl_NewWideIntObj(pFile->nRef));
1414796af156Sdrh       Tcl_ListObjAppendElement(interp, pFileTerm,
1415796af156Sdrh             Tcl_NewWideIntObj(pFile->deleteOnClose));
1416ef9659baSdrh       Tcl_ListObjAppendElement(interp, pGroupTerm, pFileTerm);
1417ef9659baSdrh     }
1418ef9659baSdrh     Tcl_ListObjAppendElement(interp, pResult, pGroupTerm);
1419ef9659baSdrh   }
1420ef9659baSdrh   quotaLeave();
1421ef9659baSdrh   Tcl_SetObjResult(interp, pResult);
1422ef9659baSdrh   return TCL_OK;
1423ef9659baSdrh }
1424ef9659baSdrh 
1425748a52cdSdrh /*
1426a76e8589Sdrh ** tclcmd: sqlite3_quota_fopen FILENAME MODE
1427a76e8589Sdrh */
1428a76e8589Sdrh static int test_quota_fopen(
1429a76e8589Sdrh   void * clientData,
1430a76e8589Sdrh   Tcl_Interp *interp,
1431a76e8589Sdrh   int objc,
1432a76e8589Sdrh   Tcl_Obj *CONST objv[]
1433a76e8589Sdrh ){
1434a76e8589Sdrh   const char *zFilename;          /* File pattern to configure */
1435a76e8589Sdrh   const char *zMode;              /* Mode string */
1436a76e8589Sdrh   quota_FILE *p;                  /* Open string object */
1437a76e8589Sdrh   char zReturn[50];               /* Name of pointer to return */
1438a76e8589Sdrh 
1439a76e8589Sdrh   /* Process arguments */
1440a76e8589Sdrh   if( objc!=3 ){
1441a76e8589Sdrh     Tcl_WrongNumArgs(interp, 1, objv, "FILENAME MODE");
1442a76e8589Sdrh     return TCL_ERROR;
1443a76e8589Sdrh   }
1444a76e8589Sdrh   zFilename = Tcl_GetString(objv[1]);
1445a76e8589Sdrh   zMode = Tcl_GetString(objv[2]);
1446a76e8589Sdrh   p = sqlite3_quota_fopen(zFilename, zMode);
1447a76e8589Sdrh   sqlite3_snprintf(sizeof(zReturn), zReturn, "%p", p);
1448a76e8589Sdrh   Tcl_SetResult(interp, zReturn, TCL_VOLATILE);
1449a76e8589Sdrh   return TCL_OK;
1450a76e8589Sdrh }
1451a76e8589Sdrh 
1452a76e8589Sdrh /* Defined in test1.c */
1453a76e8589Sdrh extern void *sqlite3TestTextToPtr(const char*);
1454a76e8589Sdrh 
1455a76e8589Sdrh /*
1456a76e8589Sdrh ** tclcmd: sqlite3_quota_fread HANDLE SIZE NELEM
1457a76e8589Sdrh */
1458a76e8589Sdrh static int test_quota_fread(
1459a76e8589Sdrh   void * clientData,
1460a76e8589Sdrh   Tcl_Interp *interp,
1461a76e8589Sdrh   int objc,
1462a76e8589Sdrh   Tcl_Obj *CONST objv[]
1463a76e8589Sdrh ){
1464a76e8589Sdrh   quota_FILE *p;
1465a76e8589Sdrh   char *zBuf;
1466a76e8589Sdrh   int sz;
1467a76e8589Sdrh   int nElem;
1468a76e8589Sdrh   int got;
1469a76e8589Sdrh 
1470a76e8589Sdrh   if( objc!=4 ){
1471a76e8589Sdrh     Tcl_WrongNumArgs(interp, 1, objv, "HANDLE SIZE NELEM");
1472a76e8589Sdrh     return TCL_ERROR;
1473a76e8589Sdrh   }
1474a76e8589Sdrh   p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
1475a76e8589Sdrh   if( Tcl_GetIntFromObj(interp, objv[2], &sz) ) return TCL_ERROR;
1476a76e8589Sdrh   if( Tcl_GetIntFromObj(interp, objv[3], &nElem) ) return TCL_ERROR;
1477663cebfeSdrh   zBuf = (char*)sqlite3_malloc( sz*nElem + 1 );
1478a76e8589Sdrh   if( zBuf==0 ){
1479a76e8589Sdrh     Tcl_SetResult(interp, "out of memory", TCL_STATIC);
1480a76e8589Sdrh     return TCL_ERROR;
1481a76e8589Sdrh   }
1482a76e8589Sdrh   got = sqlite3_quota_fread(zBuf, sz, nElem, p);
1483a76e8589Sdrh   if( got<0 ) got = 0;
1484a76e8589Sdrh   zBuf[got*sz] = 0;
1485a76e8589Sdrh   Tcl_SetResult(interp, zBuf, TCL_VOLATILE);
1486a76e8589Sdrh   sqlite3_free(zBuf);
1487a76e8589Sdrh   return TCL_OK;
1488a76e8589Sdrh }
1489a76e8589Sdrh 
1490a76e8589Sdrh /*
1491a76e8589Sdrh ** tclcmd: sqlite3_quota_fwrite HANDLE SIZE NELEM CONTENT
1492a76e8589Sdrh */
1493a76e8589Sdrh static int test_quota_fwrite(
1494a76e8589Sdrh   void * clientData,
1495a76e8589Sdrh   Tcl_Interp *interp,
1496a76e8589Sdrh   int objc,
1497a76e8589Sdrh   Tcl_Obj *CONST objv[]
1498a76e8589Sdrh ){
1499a76e8589Sdrh   quota_FILE *p;
1500a76e8589Sdrh   char *zBuf;
1501a76e8589Sdrh   int sz;
1502a76e8589Sdrh   int nElem;
1503a76e8589Sdrh   int got;
1504a76e8589Sdrh 
1505a76e8589Sdrh   if( objc!=5 ){
1506a76e8589Sdrh     Tcl_WrongNumArgs(interp, 1, objv, "HANDLE SIZE NELEM CONTENT");
1507a76e8589Sdrh     return TCL_ERROR;
1508a76e8589Sdrh   }
1509a76e8589Sdrh   p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
1510a76e8589Sdrh   if( Tcl_GetIntFromObj(interp, objv[2], &sz) ) return TCL_ERROR;
1511a76e8589Sdrh   if( Tcl_GetIntFromObj(interp, objv[3], &nElem) ) return TCL_ERROR;
1512a76e8589Sdrh   zBuf = Tcl_GetString(objv[4]);
1513a76e8589Sdrh   got = sqlite3_quota_fwrite(zBuf, sz, nElem, p);
1514a76e8589Sdrh   Tcl_SetObjResult(interp, Tcl_NewIntObj(got));
1515a76e8589Sdrh   return TCL_OK;
1516a76e8589Sdrh }
1517a76e8589Sdrh 
1518a76e8589Sdrh /*
1519a76e8589Sdrh ** tclcmd: sqlite3_quota_fclose HANDLE
1520a76e8589Sdrh */
1521a76e8589Sdrh static int test_quota_fclose(
1522a76e8589Sdrh   void * clientData,
1523a76e8589Sdrh   Tcl_Interp *interp,
1524a76e8589Sdrh   int objc,
1525a76e8589Sdrh   Tcl_Obj *CONST objv[]
1526a76e8589Sdrh ){
1527a76e8589Sdrh   quota_FILE *p;
1528a76e8589Sdrh   int rc;
1529a76e8589Sdrh 
1530a76e8589Sdrh   if( objc!=2 ){
1531a76e8589Sdrh     Tcl_WrongNumArgs(interp, 1, objv, "HANDLE");
1532a76e8589Sdrh     return TCL_ERROR;
1533a76e8589Sdrh   }
1534a76e8589Sdrh   p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
1535a76e8589Sdrh   rc = sqlite3_quota_fclose(p);
1536a76e8589Sdrh   Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
1537a76e8589Sdrh   return TCL_OK;
1538a76e8589Sdrh }
1539a76e8589Sdrh 
1540a76e8589Sdrh /*
154127cec37dSdrh ** tclcmd: sqlite3_quota_fflush HANDLE ?HARDSYNC?
154269b2232dSdrh */
154369b2232dSdrh static int test_quota_fflush(
154469b2232dSdrh   void * clientData,
154569b2232dSdrh   Tcl_Interp *interp,
154669b2232dSdrh   int objc,
154769b2232dSdrh   Tcl_Obj *CONST objv[]
154869b2232dSdrh ){
154969b2232dSdrh   quota_FILE *p;
155069b2232dSdrh   int rc;
155127cec37dSdrh   int doSync = 0;
155269b2232dSdrh 
155327cec37dSdrh   if( objc!=2 && objc!=3 ){
155427cec37dSdrh     Tcl_WrongNumArgs(interp, 1, objv, "HANDLE ?HARDSYNC?");
155569b2232dSdrh     return TCL_ERROR;
155669b2232dSdrh   }
155769b2232dSdrh   p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
155827cec37dSdrh   if( objc==3 ){
155927cec37dSdrh     if( Tcl_GetBooleanFromObj(interp, objv[2], &doSync) ) return TCL_ERROR;
156027cec37dSdrh   }
156127cec37dSdrh   rc = sqlite3_quota_fflush(p, doSync);
156269b2232dSdrh   Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
156369b2232dSdrh   return TCL_OK;
156469b2232dSdrh }
156569b2232dSdrh 
156669b2232dSdrh /*
1567a76e8589Sdrh ** tclcmd: sqlite3_quota_fseek HANDLE OFFSET WHENCE
1568a76e8589Sdrh */
1569a76e8589Sdrh static int test_quota_fseek(
1570a76e8589Sdrh   void * clientData,
1571a76e8589Sdrh   Tcl_Interp *interp,
1572a76e8589Sdrh   int objc,
1573a76e8589Sdrh   Tcl_Obj *CONST objv[]
1574a76e8589Sdrh ){
1575a76e8589Sdrh   quota_FILE *p;
1576a76e8589Sdrh   int ofst;
1577a76e8589Sdrh   const char *zWhence;
1578a76e8589Sdrh   int whence;
1579a76e8589Sdrh   int rc;
1580a76e8589Sdrh 
1581a76e8589Sdrh   if( objc!=4 ){
1582a76e8589Sdrh     Tcl_WrongNumArgs(interp, 1, objv, "HANDLE OFFSET WHENCE");
1583a76e8589Sdrh     return TCL_ERROR;
1584a76e8589Sdrh   }
1585a76e8589Sdrh   p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
1586a76e8589Sdrh   if( Tcl_GetIntFromObj(interp, objv[2], &ofst) ) return TCL_ERROR;
1587a76e8589Sdrh   zWhence = Tcl_GetString(objv[3]);
1588a76e8589Sdrh   if( strcmp(zWhence, "SEEK_SET")==0 ){
1589a76e8589Sdrh     whence = SEEK_SET;
1590a76e8589Sdrh   }else if( strcmp(zWhence, "SEEK_CUR")==0 ){
1591a76e8589Sdrh     whence = SEEK_CUR;
1592a76e8589Sdrh   }else if( strcmp(zWhence, "SEEK_END")==0 ){
1593a76e8589Sdrh     whence = SEEK_END;
1594a76e8589Sdrh   }else{
1595a76e8589Sdrh     Tcl_AppendResult(interp,
1596a76e8589Sdrh            "WHENCE should be SEEK_SET, SEEK_CUR, or SEEK_END", (char*)0);
1597a76e8589Sdrh     return TCL_ERROR;
1598a76e8589Sdrh   }
1599a76e8589Sdrh   rc = sqlite3_quota_fseek(p, ofst, whence);
1600a76e8589Sdrh   Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
1601a76e8589Sdrh   return TCL_OK;
1602a76e8589Sdrh }
1603a76e8589Sdrh 
1604a76e8589Sdrh /*
1605a76e8589Sdrh ** tclcmd: sqlite3_quota_rewind HANDLE
1606a76e8589Sdrh */
1607a76e8589Sdrh static int test_quota_rewind(
1608a76e8589Sdrh   void * clientData,
1609a76e8589Sdrh   Tcl_Interp *interp,
1610a76e8589Sdrh   int objc,
1611a76e8589Sdrh   Tcl_Obj *CONST objv[]
1612a76e8589Sdrh ){
1613a76e8589Sdrh   quota_FILE *p;
1614a76e8589Sdrh   if( objc!=2 ){
1615a76e8589Sdrh     Tcl_WrongNumArgs(interp, 1, objv, "HANDLE");
1616a76e8589Sdrh     return TCL_ERROR;
1617a76e8589Sdrh   }
1618a76e8589Sdrh   p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
1619a76e8589Sdrh   sqlite3_quota_rewind(p);
1620a76e8589Sdrh   return TCL_OK;
1621a76e8589Sdrh }
1622a76e8589Sdrh 
1623a76e8589Sdrh /*
1624a76e8589Sdrh ** tclcmd: sqlite3_quota_ftell HANDLE
1625a76e8589Sdrh */
1626a76e8589Sdrh static int test_quota_ftell(
1627a76e8589Sdrh   void * clientData,
1628a76e8589Sdrh   Tcl_Interp *interp,
1629a76e8589Sdrh   int objc,
1630a76e8589Sdrh   Tcl_Obj *CONST objv[]
1631a76e8589Sdrh ){
1632a76e8589Sdrh   quota_FILE *p;
1633a76e8589Sdrh   sqlite3_int64 x;
1634a76e8589Sdrh   if( objc!=2 ){
1635a76e8589Sdrh     Tcl_WrongNumArgs(interp, 1, objv, "HANDLE");
1636a76e8589Sdrh     return TCL_ERROR;
1637a76e8589Sdrh   }
1638a76e8589Sdrh   p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
1639a76e8589Sdrh   x = sqlite3_quota_ftell(p);
1640a76e8589Sdrh   Tcl_SetObjResult(interp, Tcl_NewWideIntObj(x));
1641a76e8589Sdrh   return TCL_OK;
1642a76e8589Sdrh }
1643a76e8589Sdrh 
1644a76e8589Sdrh /*
1645a76e8589Sdrh ** tclcmd: sqlite3_quota_remove FILENAME
1646a76e8589Sdrh */
1647a76e8589Sdrh static int test_quota_remove(
1648a76e8589Sdrh   void * clientData,
1649a76e8589Sdrh   Tcl_Interp *interp,
1650a76e8589Sdrh   int objc,
1651a76e8589Sdrh   Tcl_Obj *CONST objv[]
1652a76e8589Sdrh ){
1653a76e8589Sdrh   const char *zFilename;          /* File pattern to configure */
1654a76e8589Sdrh   int rc;
1655a76e8589Sdrh   if( objc!=2 ){
1656a76e8589Sdrh     Tcl_WrongNumArgs(interp, 1, objv, "FILENAME");
1657a76e8589Sdrh     return TCL_ERROR;
1658a76e8589Sdrh   }
1659a76e8589Sdrh   zFilename = Tcl_GetString(objv[1]);
1660a76e8589Sdrh   rc = sqlite3_quota_remove(zFilename);
1661a76e8589Sdrh   Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
1662a76e8589Sdrh   return TCL_OK;
1663a76e8589Sdrh }
1664a76e8589Sdrh 
1665a76e8589Sdrh /*
1666c8ccda60Sdrh ** tclcmd: sqlite3_quota_glob PATTERN TEXT
1667c8ccda60Sdrh **
1668c8ccda60Sdrh ** Test the glob pattern matching.  Return 1 if TEXT matches PATTERN
1669c8ccda60Sdrh ** and return 0 if it does not.
1670c8ccda60Sdrh */
1671c8ccda60Sdrh static int test_quota_glob(
1672c8ccda60Sdrh   void * clientData,
1673c8ccda60Sdrh   Tcl_Interp *interp,
1674c8ccda60Sdrh   int objc,
1675c8ccda60Sdrh   Tcl_Obj *CONST objv[]
1676c8ccda60Sdrh ){
1677c8ccda60Sdrh   const char *zPattern;          /* The glob pattern */
1678c8ccda60Sdrh   const char *zText;             /* Text to compare agains the pattern */
1679c8ccda60Sdrh   int rc;
1680c8ccda60Sdrh   if( objc!=3 ){
1681c8ccda60Sdrh     Tcl_WrongNumArgs(interp, 1, objv, "PATTERN TEXT");
1682c8ccda60Sdrh     return TCL_ERROR;
1683c8ccda60Sdrh   }
1684c8ccda60Sdrh   zPattern = Tcl_GetString(objv[1]);
1685c8ccda60Sdrh   zText = Tcl_GetString(objv[2]);
1686c8ccda60Sdrh   rc = quotaStrglob(zPattern, zText);
1687c8ccda60Sdrh   Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
1688c8ccda60Sdrh   return TCL_OK;
1689c8ccda60Sdrh }
1690c8ccda60Sdrh 
1691c8ccda60Sdrh /*
1692748a52cdSdrh ** This routine registers the custom TCL commands defined in this
1693748a52cdSdrh ** module.  This should be the only procedure visible from outside
1694748a52cdSdrh ** of this module.
1695748a52cdSdrh */
1696748a52cdSdrh int Sqlitequota_Init(Tcl_Interp *interp){
16978cf35eb4Sdan   static struct {
16988cf35eb4Sdan      char *zName;
16998cf35eb4Sdan      Tcl_ObjCmdProc *xProc;
17008cf35eb4Sdan   } aCmd[] = {
17018cf35eb4Sdan     { "sqlite3_quota_initialize", test_quota_initialize },
17028cf35eb4Sdan     { "sqlite3_quota_shutdown",   test_quota_shutdown },
17038cf35eb4Sdan     { "sqlite3_quota_set",        test_quota_set },
1704b6020c4eSdrh     { "sqlite3_quota_file",       test_quota_file },
1705ef9659baSdrh     { "sqlite3_quota_dump",       test_quota_dump },
1706a76e8589Sdrh     { "sqlite3_quota_fopen",      test_quota_fopen },
1707a76e8589Sdrh     { "sqlite3_quota_fread",      test_quota_fread },
1708a76e8589Sdrh     { "sqlite3_quota_fwrite",     test_quota_fwrite },
1709a76e8589Sdrh     { "sqlite3_quota_fclose",     test_quota_fclose },
171069b2232dSdrh     { "sqlite3_quota_fflush",     test_quota_fflush },
1711a76e8589Sdrh     { "sqlite3_quota_fseek",      test_quota_fseek },
1712a76e8589Sdrh     { "sqlite3_quota_rewind",     test_quota_rewind },
1713a76e8589Sdrh     { "sqlite3_quota_ftell",      test_quota_ftell },
1714a76e8589Sdrh     { "sqlite3_quota_remove",     test_quota_remove },
1715c8ccda60Sdrh     { "sqlite3_quota_glob",       test_quota_glob },
17168cf35eb4Sdan   };
17178cf35eb4Sdan   int i;
17188cf35eb4Sdan 
17198cf35eb4Sdan   for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
17208cf35eb4Sdan     Tcl_CreateObjCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0);
17218cf35eb4Sdan   }
17228cf35eb4Sdan 
1723748a52cdSdrh   return TCL_OK;
1724748a52cdSdrh }
1725748a52cdSdrh #endif
1726