xref: /sqlite-3.40.0/src/test_quota.c (revision b3f787f4)
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 
4894e858b1Sdrh /*
4994e858b1Sdrh ** Figure out if we are dealing with Unix, Windows, or some other
5094e858b1Sdrh ** operating system.  After the following block of preprocess macros,
5183905c90Sdrh ** all of SQLITE_OS_UNIX, SQLITE_OS_WIN, and SQLITE_OS_OTHER
5294e858b1Sdrh ** will defined to either 1 or 0.  One of the four will be 1.  The other
5394e858b1Sdrh ** three will be 0.
5494e858b1Sdrh */
5594e858b1Sdrh #if defined(SQLITE_OS_OTHER)
5694e858b1Sdrh # if SQLITE_OS_OTHER==1
5794e858b1Sdrh #   undef SQLITE_OS_UNIX
5894e858b1Sdrh #   define SQLITE_OS_UNIX 0
5994e858b1Sdrh #   undef SQLITE_OS_WIN
6094e858b1Sdrh #   define SQLITE_OS_WIN 0
6194e858b1Sdrh # else
6294e858b1Sdrh #   undef SQLITE_OS_OTHER
6394e858b1Sdrh # endif
6494e858b1Sdrh #endif
6594e858b1Sdrh #if !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_OTHER)
6694e858b1Sdrh # define SQLITE_OS_OTHER 0
6794e858b1Sdrh # ifndef SQLITE_OS_WIN
6894e858b1Sdrh #   if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) \
6994e858b1Sdrh                        || defined(__MINGW32__) || defined(__BORLANDC__)
7094e858b1Sdrh #     define SQLITE_OS_WIN 1
7194e858b1Sdrh #     define SQLITE_OS_UNIX 0
7294e858b1Sdrh #   else
7394e858b1Sdrh #     define SQLITE_OS_WIN 0
7494e858b1Sdrh #     define SQLITE_OS_UNIX 1
7594e858b1Sdrh #  endif
7694e858b1Sdrh # else
7794e858b1Sdrh #  define SQLITE_OS_UNIX 0
7894e858b1Sdrh # endif
7994e858b1Sdrh #else
8094e858b1Sdrh # ifndef SQLITE_OS_WIN
8194e858b1Sdrh #  define SQLITE_OS_WIN 0
8294e858b1Sdrh # endif
8394e858b1Sdrh #endif
8494e858b1Sdrh 
8594e858b1Sdrh #if SQLITE_OS_UNIX
8694e858b1Sdrh # include <unistd.h>
8794e858b1Sdrh #endif
8894e858b1Sdrh #if SQLITE_OS_WIN
8994e858b1Sdrh # include <windows.h>
9094e858b1Sdrh # include <io.h>
9194e858b1Sdrh #endif
9294e858b1Sdrh 
9394e858b1Sdrh 
94748a52cdSdrh /************************ Object Definitions ******************************/
95748a52cdSdrh 
96745de2e4Sdrh /* Forward declaration of all object types */
97748a52cdSdrh typedef struct quotaGroup quotaGroup;
98ef9659baSdrh typedef struct quotaConn quotaConn;
99745de2e4Sdrh typedef struct quotaFile quotaFile;
100745de2e4Sdrh 
1018cf35eb4Sdan /*
102549dc1baSdrh ** A "quota group" is a collection of files whose collective size we want
103549dc1baSdrh ** to limit.  Each quota group is defined by a GLOB pattern.
1048cf35eb4Sdan **
105549dc1baSdrh ** There is an instance of the following object for each defined quota
106549dc1baSdrh ** group.  This object records the GLOB pattern that defines which files
107549dc1baSdrh ** belong to the quota group.  The object also remembers the size limit
108549dc1baSdrh ** for the group (the quota) and the callback to be invoked when the
109549dc1baSdrh ** sum of the sizes of the files within the group goes over the limit.
1108cf35eb4Sdan **
111549dc1baSdrh ** A quota group must be established (using sqlite3_quota_set(...))
112549dc1baSdrh ** prior to opening any of the database connections that access files
113549dc1baSdrh ** within the quota group.
1148cf35eb4Sdan */
115748a52cdSdrh struct quotaGroup {
116748a52cdSdrh   const char *zPattern;          /* Filename pattern to be quotaed */
117748a52cdSdrh   sqlite3_int64 iLimit;          /* Upper bound on total file size */
118748a52cdSdrh   sqlite3_int64 iSize;           /* Current size of all files */
119748a52cdSdrh   void (*xCallback)(             /* Callback invoked when going over quota */
120748a52cdSdrh      const char *zFilename,         /* Name of file whose size increases */
121748a52cdSdrh      sqlite3_int64 *piLimit,        /* IN/OUT: The current limit */
122748a52cdSdrh      sqlite3_int64 iSize,           /* Total size of all files in the group */
123748a52cdSdrh      void *pArg                     /* Client data */
124748a52cdSdrh   );
125748a52cdSdrh   void *pArg;                    /* Third argument to the xCallback() */
126ef9659baSdrh   void (*xDestroy)(void*);       /* Optional destructor for pArg */
127748a52cdSdrh   quotaGroup *pNext, **ppPrev;   /* Doubly linked list of all quota objects */
12875c19174Sdrh   quotaFile *pFiles;             /* Files within this group */
129748a52cdSdrh };
130748a52cdSdrh 
131748a52cdSdrh /*
132745de2e4Sdrh ** An instance of this structure represents a single file that is part
133745de2e4Sdrh ** of a quota group.  A single file can be opened multiple times.  In
134745de2e4Sdrh ** order keep multiple openings of the same file from causing the size
135549dc1baSdrh ** of the file to count against the quota multiple times, each file
136745de2e4Sdrh ** has a unique instance of this object and multiple open connections
137745de2e4Sdrh ** to the same file each point to a single instance of this object.
138748a52cdSdrh */
139748a52cdSdrh struct quotaFile {
140745de2e4Sdrh   char *zFilename;                /* Name of this file */
14175c19174Sdrh   quotaGroup *pGroup;             /* Quota group to which this file belongs */
1428cf35eb4Sdan   sqlite3_int64 iSize;            /* Current size of this file */
143745de2e4Sdrh   int nRef;                       /* Number of times this file is open */
144796af156Sdrh   int deleteOnClose;              /* True to delete this file when it closes */
145745de2e4Sdrh   quotaFile *pNext, **ppPrev;     /* Linked list of files in the same group */
146745de2e4Sdrh };
147745de2e4Sdrh 
148745de2e4Sdrh /*
149745de2e4Sdrh ** An instance of the following object represents each open connection
150549dc1baSdrh ** to a file that participates in quota tracking.  This object is a
151549dc1baSdrh ** subclass of sqlite3_file.  The sqlite3_file object for the underlying
152549dc1baSdrh ** VFS is appended to this structure.
153745de2e4Sdrh */
154ef9659baSdrh struct quotaConn {
155748a52cdSdrh   sqlite3_file base;              /* Base class - must be first */
156745de2e4Sdrh   quotaFile *pFile;               /* The underlying file */
157748a52cdSdrh   /* The underlying VFS sqlite3_file is appended to this object */
158748a52cdSdrh };
159748a52cdSdrh 
16060f21e4bSdrh /*
16160f21e4bSdrh ** An instance of the following object records the state of an
16260f21e4bSdrh ** open file.  This object is opaque to all users - the internal
16360f21e4bSdrh ** structure is only visible to the functions below.
16460f21e4bSdrh */
16560f21e4bSdrh struct quota_FILE {
16660f21e4bSdrh   FILE *f;                /* Open stdio file pointer */
16760f21e4bSdrh   sqlite3_int64 iOfst;    /* Current offset into the file */
16860f21e4bSdrh   quotaFile *pFile;       /* The file record in the quota system */
169c00ce490Sdrh #if SQLITE_OS_WIN
170c00ce490Sdrh   char *zMbcsName;        /* Full MBCS pathname of the file */
171c00ce490Sdrh #endif
17260f21e4bSdrh };
17360f21e4bSdrh 
17460f21e4bSdrh 
175748a52cdSdrh /************************* Global Variables **********************************/
176748a52cdSdrh /*
177748a52cdSdrh ** All global variables used by this file are containing within the following
178748a52cdSdrh ** gQuota structure.
179748a52cdSdrh */
180748a52cdSdrh static struct {
181549dc1baSdrh   /* The pOrigVfs is the real, original underlying VFS implementation.
182748a52cdSdrh   ** Most operations pass-through to the real VFS.  This value is read-only
183748a52cdSdrh   ** during operation.  It is only modified at start-time and thus does not
184748a52cdSdrh   ** require a mutex.
185748a52cdSdrh   */
186748a52cdSdrh   sqlite3_vfs *pOrigVfs;
187748a52cdSdrh 
188748a52cdSdrh   /* The sThisVfs is the VFS structure used by this shim.  It is initialized
189748a52cdSdrh   ** at start-time and thus does not require a mutex
190748a52cdSdrh   */
191748a52cdSdrh   sqlite3_vfs sThisVfs;
192748a52cdSdrh 
193748a52cdSdrh   /* The sIoMethods defines the methods used by sqlite3_file objects
194748a52cdSdrh   ** associated with this shim.  It is initialized at start-time and does
195748a52cdSdrh   ** not require a mutex.
196748a52cdSdrh   **
197748a52cdSdrh   ** When the underlying VFS is called to open a file, it might return
198748a52cdSdrh   ** either a version 1 or a version 2 sqlite3_file object.  This shim
199748a52cdSdrh   ** has to create a wrapper sqlite3_file of the same version.  Hence
200748a52cdSdrh   ** there are two I/O method structures, one for version 1 and the other
201748a52cdSdrh   ** for version 2.
202748a52cdSdrh   */
203748a52cdSdrh   sqlite3_io_methods sIoMethodsV1;
204748a52cdSdrh   sqlite3_io_methods sIoMethodsV2;
205748a52cdSdrh 
206748a52cdSdrh   /* True when this shim as been initialized.
207748a52cdSdrh   */
208748a52cdSdrh   int isInitialized;
209748a52cdSdrh 
210748a52cdSdrh   /* For run-time access any of the other global data structures in this
211748a52cdSdrh   ** shim, the following mutex must be held.
212748a52cdSdrh   */
213748a52cdSdrh   sqlite3_mutex *pMutex;
214748a52cdSdrh 
215748a52cdSdrh   /* List of quotaGroup objects.
216748a52cdSdrh   */
217748a52cdSdrh   quotaGroup *pGroup;
218748a52cdSdrh 
219748a52cdSdrh } gQuota;
220748a52cdSdrh 
221748a52cdSdrh /************************* Utility Routines *********************************/
222748a52cdSdrh /*
223748a52cdSdrh ** Acquire and release the mutex used to serialize access to the
224748a52cdSdrh ** list of quotaGroups.
225748a52cdSdrh */
226748a52cdSdrh static void quotaEnter(void){ sqlite3_mutex_enter(gQuota.pMutex); }
227748a52cdSdrh static void quotaLeave(void){ sqlite3_mutex_leave(gQuota.pMutex); }
228748a52cdSdrh 
229796af156Sdrh /* Count the number of open files in a quotaGroup
230796af156Sdrh */
231796af156Sdrh static int quotaGroupOpenFileCount(quotaGroup *pGroup){
232796af156Sdrh   int N = 0;
233796af156Sdrh   quotaFile *pFile = pGroup->pFiles;
234796af156Sdrh   while( pFile ){
235796af156Sdrh     if( pFile->nRef ) N++;
236796af156Sdrh     pFile = pFile->pNext;
237796af156Sdrh   }
238796af156Sdrh   return N;
239796af156Sdrh }
240796af156Sdrh 
241796af156Sdrh /* Remove a file from a quota group.
242796af156Sdrh */
243796af156Sdrh static void quotaRemoveFile(quotaFile *pFile){
244796af156Sdrh   quotaGroup *pGroup = pFile->pGroup;
245796af156Sdrh   pGroup->iSize -= pFile->iSize;
246796af156Sdrh   *pFile->ppPrev = pFile->pNext;
247796af156Sdrh   if( pFile->pNext ) pFile->pNext->ppPrev = pFile->ppPrev;
248796af156Sdrh   sqlite3_free(pFile);
249796af156Sdrh }
250796af156Sdrh 
251796af156Sdrh /* Remove all files from a quota group.  It is always the case that
252796af156Sdrh ** all files will be closed when this routine is called.
253796af156Sdrh */
254796af156Sdrh static void quotaRemoveAllFiles(quotaGroup *pGroup){
255796af156Sdrh   while( pGroup->pFiles ){
256796af156Sdrh     assert( pGroup->pFiles->nRef==0 );
257796af156Sdrh     quotaRemoveFile(pGroup->pFiles);
258796af156Sdrh   }
259796af156Sdrh }
260796af156Sdrh 
261748a52cdSdrh 
262748a52cdSdrh /* If the reference count and threshold for a quotaGroup are both
263748a52cdSdrh ** zero, then destroy the quotaGroup.
264748a52cdSdrh */
26575c19174Sdrh static void quotaGroupDeref(quotaGroup *pGroup){
266796af156Sdrh   if( pGroup->iLimit==0 && quotaGroupOpenFileCount(pGroup)==0 ){
267796af156Sdrh     quotaRemoveAllFiles(pGroup);
2689fc7702cSdan     *pGroup->ppPrev = pGroup->pNext;
26975c19174Sdrh     if( pGroup->pNext ) pGroup->pNext->ppPrev = pGroup->ppPrev;
27075c19174Sdrh     if( pGroup->xDestroy ) pGroup->xDestroy(pGroup->pArg);
27175c19174Sdrh     sqlite3_free(pGroup);
272748a52cdSdrh   }
273748a52cdSdrh }
274748a52cdSdrh 
275748a52cdSdrh /*
276748a52cdSdrh ** Return TRUE if string z matches glob pattern zGlob.
277748a52cdSdrh **
278748a52cdSdrh ** Globbing rules:
279748a52cdSdrh **
280748a52cdSdrh **      '*'       Matches any sequence of zero or more characters.
281748a52cdSdrh **
282748a52cdSdrh **      '?'       Matches exactly one character.
283748a52cdSdrh **
284748a52cdSdrh **     [...]      Matches one character from the enclosed list of
285748a52cdSdrh **                characters.
286748a52cdSdrh **
287748a52cdSdrh **     [^...]     Matches one character not in the enclosed list.
288748a52cdSdrh **
289a76e8589Sdrh **     /          Matches "/" or "\\"
290a76e8589Sdrh **
291748a52cdSdrh */
29261cc423aSdrh static int quotaStrglob(const char *zGlob, const char *z){
293c8ccda60Sdrh   int c, c2, cx;
294748a52cdSdrh   int invert;
295748a52cdSdrh   int seen;
296748a52cdSdrh 
297748a52cdSdrh   while( (c = (*(zGlob++)))!=0 ){
298748a52cdSdrh     if( c=='*' ){
299748a52cdSdrh       while( (c=(*(zGlob++))) == '*' || c=='?' ){
300748a52cdSdrh         if( c=='?' && (*(z++))==0 ) return 0;
301748a52cdSdrh       }
302748a52cdSdrh       if( c==0 ){
303748a52cdSdrh         return 1;
304748a52cdSdrh       }else if( c=='[' ){
30561cc423aSdrh         while( *z && quotaStrglob(zGlob-1,z)==0 ){
306748a52cdSdrh           z++;
307748a52cdSdrh         }
308748a52cdSdrh         return (*z)!=0;
309748a52cdSdrh       }
310c8ccda60Sdrh       cx = (c=='/') ? '\\' : c;
311748a52cdSdrh       while( (c2 = (*(z++)))!=0 ){
312c8ccda60Sdrh         while( c2!=c && c2!=cx ){
313748a52cdSdrh           c2 = *(z++);
314748a52cdSdrh           if( c2==0 ) return 0;
315748a52cdSdrh         }
31661cc423aSdrh         if( quotaStrglob(zGlob,z) ) return 1;
317748a52cdSdrh       }
318748a52cdSdrh       return 0;
319748a52cdSdrh     }else if( c=='?' ){
320748a52cdSdrh       if( (*(z++))==0 ) return 0;
321748a52cdSdrh     }else if( c=='[' ){
322748a52cdSdrh       int prior_c = 0;
323748a52cdSdrh       seen = 0;
324748a52cdSdrh       invert = 0;
325748a52cdSdrh       c = *(z++);
326748a52cdSdrh       if( c==0 ) return 0;
327748a52cdSdrh       c2 = *(zGlob++);
328748a52cdSdrh       if( c2=='^' ){
329748a52cdSdrh         invert = 1;
330748a52cdSdrh         c2 = *(zGlob++);
331748a52cdSdrh       }
332748a52cdSdrh       if( c2==']' ){
333748a52cdSdrh         if( c==']' ) seen = 1;
334748a52cdSdrh         c2 = *(zGlob++);
335748a52cdSdrh       }
336748a52cdSdrh       while( c2 && c2!=']' ){
337748a52cdSdrh         if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
338748a52cdSdrh           c2 = *(zGlob++);
339748a52cdSdrh           if( c>=prior_c && c<=c2 ) seen = 1;
340748a52cdSdrh           prior_c = 0;
341748a52cdSdrh         }else{
342748a52cdSdrh           if( c==c2 ){
343748a52cdSdrh             seen = 1;
344748a52cdSdrh           }
345748a52cdSdrh           prior_c = c2;
346748a52cdSdrh         }
347748a52cdSdrh         c2 = *(zGlob++);
348748a52cdSdrh       }
349748a52cdSdrh       if( c2==0 || (seen ^ invert)==0 ) return 0;
350a76e8589Sdrh     }else if( c=='/' ){
351a76e8589Sdrh       if( z[0]!='/' && z[0]!='\\' ) return 0;
352a76e8589Sdrh       z++;
353748a52cdSdrh     }else{
354748a52cdSdrh       if( c!=(*(z++)) ) return 0;
355748a52cdSdrh     }
356748a52cdSdrh   }
357748a52cdSdrh   return *z==0;
358748a52cdSdrh }
359748a52cdSdrh 
360748a52cdSdrh 
361748a52cdSdrh /* Find a quotaGroup given the filename.
3628cf35eb4Sdan **
363ef9659baSdrh ** Return a pointer to the quotaGroup object. Return NULL if not found.
364748a52cdSdrh */
365748a52cdSdrh static quotaGroup *quotaGroupFind(const char *zFilename){
366748a52cdSdrh   quotaGroup *p;
36761cc423aSdrh   for(p=gQuota.pGroup; p && quotaStrglob(p->zPattern, zFilename)==0;
36861cc423aSdrh       p=p->pNext){}
369748a52cdSdrh   return p;
370748a52cdSdrh }
371748a52cdSdrh 
372ef9659baSdrh /* Translate an sqlite3_file* that is really a quotaConn* into
373549dc1baSdrh ** the sqlite3_file* for the underlying original VFS.
374748a52cdSdrh */
375ef9659baSdrh static sqlite3_file *quotaSubOpen(sqlite3_file *pConn){
376ef9659baSdrh   quotaConn *p = (quotaConn*)pConn;
377748a52cdSdrh   return (sqlite3_file*)&p[1];
378748a52cdSdrh }
379748a52cdSdrh 
380796af156Sdrh /* Find a file in a quota group and return a pointer to that file.
381796af156Sdrh ** Return NULL if the file is not in the group.
382796af156Sdrh */
38360f21e4bSdrh static quotaFile *quotaFindFile(
38460f21e4bSdrh   quotaGroup *pGroup,     /* Group in which to look for the file */
38560f21e4bSdrh   const char *zName,      /* Full pathname of the file */
38660f21e4bSdrh   int createFlag          /* Try to create the file if not found */
38760f21e4bSdrh ){
388796af156Sdrh   quotaFile *pFile = pGroup->pFiles;
389796af156Sdrh   while( pFile && strcmp(pFile->zFilename, zName)!=0 ){
390796af156Sdrh     pFile = pFile->pNext;
391796af156Sdrh   }
39260f21e4bSdrh   if( pFile==0 && createFlag ){
39383cc1392Sdrh     int nName = (int)(strlen(zName) & 0x3fffffff);
39460f21e4bSdrh     pFile = (quotaFile *)sqlite3_malloc( sizeof(*pFile) + nName + 1 );
39560f21e4bSdrh     if( pFile ){
39660f21e4bSdrh       memset(pFile, 0, sizeof(*pFile));
39760f21e4bSdrh       pFile->zFilename = (char*)&pFile[1];
39860f21e4bSdrh       memcpy(pFile->zFilename, zName, nName+1);
39960f21e4bSdrh       pFile->pNext = pGroup->pFiles;
40060f21e4bSdrh       if( pGroup->pFiles ) pGroup->pFiles->ppPrev = &pFile->pNext;
40160f21e4bSdrh       pFile->ppPrev = &pGroup->pFiles;
40260f21e4bSdrh       pGroup->pFiles = pFile;
40360f21e4bSdrh       pFile->pGroup = pGroup;
40460f21e4bSdrh     }
40560f21e4bSdrh   }
406796af156Sdrh   return pFile;
407796af156Sdrh }
408a76e8589Sdrh /*
409a76e8589Sdrh ** Translate UTF8 to MBCS for use in fopen() calls.  Return a pointer to the
410a76e8589Sdrh ** translated text..  Call quota_mbcs_free() to deallocate any memory
411a76e8589Sdrh ** used to store the returned pointer when done.
412a76e8589Sdrh */
413a76e8589Sdrh static char *quota_utf8_to_mbcs(const char *zUtf8){
414a76e8589Sdrh #if SQLITE_OS_WIN
41583cc1392Sdrh   size_t n;          /* Bytes in zUtf8 */
41627cec37dSdrh   int nWide;         /* number of UTF-16 characters */
41727cec37dSdrh   int nMbcs;         /* Bytes of MBCS */
41827cec37dSdrh   LPWSTR zTmpWide;   /* The UTF16 text */
41927cec37dSdrh   char *zMbcs;       /* The MBCS text */
42027cec37dSdrh   int codepage;      /* Code page used by fopen() */
42127cec37dSdrh 
42227cec37dSdrh   n = strlen(zUtf8);
42327cec37dSdrh   nWide = MultiByteToWideChar(CP_UTF8, 0, zUtf8, -1, NULL, 0);
42449ed5c85Sdrh   if( nWide==0 ) return 0;
425c39f3e00Sdrh   zTmpWide = (LPWSTR)sqlite3_malloc( (nWide+1)*sizeof(zTmpWide[0]) );
42627cec37dSdrh   if( zTmpWide==0 ) return 0;
42727cec37dSdrh   MultiByteToWideChar(CP_UTF8, 0, zUtf8, -1, zTmpWide, nWide);
42827cec37dSdrh   codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
42927cec37dSdrh   nMbcs = WideCharToMultiByte(codepage, 0, zTmpWide, nWide, 0, 0, 0, 0);
430c39f3e00Sdrh   zMbcs = nMbcs ? (char*)sqlite3_malloc( nMbcs+1 ) : 0;
43127cec37dSdrh   if( zMbcs ){
43227cec37dSdrh     WideCharToMultiByte(codepage, 0, zTmpWide, nWide, zMbcs, nMbcs, 0, 0);
43327cec37dSdrh   }
43427cec37dSdrh   sqlite3_free(zTmpWide);
43527cec37dSdrh   return zMbcs;
436a76e8589Sdrh #else
437a76e8589Sdrh   return (char*)zUtf8;  /* No-op on unix */
438a76e8589Sdrh #endif
439a76e8589Sdrh }
440a76e8589Sdrh 
441a76e8589Sdrh /*
442a76e8589Sdrh ** Deallocate any memory allocated by quota_utf8_to_mbcs().
443a76e8589Sdrh */
444a76e8589Sdrh static void quota_mbcs_free(char *zOld){
445a76e8589Sdrh #if SQLITE_OS_WIN
446c8ccda60Sdrh   sqlite3_free(zOld);
447a76e8589Sdrh #else
448a76e8589Sdrh   /* No-op on unix */
449a76e8589Sdrh #endif
450a76e8589Sdrh }
451a76e8589Sdrh 
452748a52cdSdrh /************************* VFS Method Wrappers *****************************/
453748a52cdSdrh /*
454748a52cdSdrh ** This is the xOpen method used for the "quota" VFS.
455748a52cdSdrh **
456748a52cdSdrh ** Most of the work is done by the underlying original VFS.  This method
457549dc1baSdrh ** simply links the new file into the appropriate quota group if it is a
458549dc1baSdrh ** file that needs to be tracked.
459748a52cdSdrh */
460748a52cdSdrh static int quotaOpen(
461549dc1baSdrh   sqlite3_vfs *pVfs,          /* The quota VFS */
462549dc1baSdrh   const char *zName,          /* Name of file to be opened */
463ef9659baSdrh   sqlite3_file *pConn,        /* Fill in this file descriptor */
464549dc1baSdrh   int flags,                  /* Flags to control the opening */
465549dc1baSdrh   int *pOutFlags              /* Flags showing results of opening */
466748a52cdSdrh ){
467549dc1baSdrh   int rc;                                    /* Result code */
468ef9659baSdrh   quotaConn *pQuotaOpen;                     /* The new quota file descriptor */
469549dc1baSdrh   quotaFile *pFile;                          /* Corresponding quotaFile obj */
470549dc1baSdrh   quotaGroup *pGroup;                        /* The group file belongs to */
471549dc1baSdrh   sqlite3_file *pSubOpen;                    /* Real file descriptor */
472549dc1baSdrh   sqlite3_vfs *pOrigVfs = gQuota.pOrigVfs;   /* Real VFS */
473748a52cdSdrh 
474748a52cdSdrh   /* If the file is not a main database file or a WAL, then use the
475748a52cdSdrh   ** normal xOpen method.
476748a52cdSdrh   */
4778cf35eb4Sdan   if( (flags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_WAL))==0 ){
478ef9659baSdrh     return pOrigVfs->xOpen(pOrigVfs, zName, pConn, flags, pOutFlags);
479748a52cdSdrh   }
480748a52cdSdrh 
481748a52cdSdrh   /* If the name of the file does not match any quota group, then
482748a52cdSdrh   ** use the normal xOpen method.
483748a52cdSdrh   */
484748a52cdSdrh   quotaEnter();
4858cf35eb4Sdan   pGroup = quotaGroupFind(zName);
486748a52cdSdrh   if( pGroup==0 ){
487ef9659baSdrh     rc = pOrigVfs->xOpen(pOrigVfs, zName, pConn, flags, pOutFlags);
488748a52cdSdrh   }else{
4898cf35eb4Sdan     /* If we get to this point, it means the file needs to be quota tracked.
4908cf35eb4Sdan     */
491ef9659baSdrh     pQuotaOpen = (quotaConn*)pConn;
492ef9659baSdrh     pSubOpen = quotaSubOpen(pConn);
493745de2e4Sdrh     rc = pOrigVfs->xOpen(pOrigVfs, zName, pSubOpen, flags, pOutFlags);
4948cf35eb4Sdan     if( rc==SQLITE_OK ){
49560f21e4bSdrh       pFile = quotaFindFile(pGroup, zName, 1);
496745de2e4Sdrh       if( pFile==0 ){
497745de2e4Sdrh         quotaLeave();
498745de2e4Sdrh         pSubOpen->pMethods->xClose(pSubOpen);
499745de2e4Sdrh         return SQLITE_NOMEM;
500745de2e4Sdrh       }
501796af156Sdrh       pFile->deleteOnClose = (flags & SQLITE_OPEN_DELETEONCLOSE)!=0;
502745de2e4Sdrh       pFile->nRef++;
503745de2e4Sdrh       pQuotaOpen->pFile = pFile;
504745de2e4Sdrh       if( pSubOpen->pMethods->iVersion==1 ){
505745de2e4Sdrh         pQuotaOpen->base.pMethods = &gQuota.sIoMethodsV1;
5068cf35eb4Sdan       }else{
507745de2e4Sdrh         pQuotaOpen->base.pMethods = &gQuota.sIoMethodsV2;
508748a52cdSdrh       }
509748a52cdSdrh     }
5108cf35eb4Sdan   }
5118cf35eb4Sdan   quotaLeave();
512748a52cdSdrh   return rc;
513748a52cdSdrh }
514748a52cdSdrh 
515796af156Sdrh /*
516796af156Sdrh ** This is the xDelete method used for the "quota" VFS.
517796af156Sdrh **
518796af156Sdrh ** If the file being deleted is part of the quota group, then reduce
519796af156Sdrh ** the size of the quota group accordingly.  And remove the file from
520796af156Sdrh ** the set of files in the quota group.
521796af156Sdrh */
522796af156Sdrh static int quotaDelete(
523796af156Sdrh   sqlite3_vfs *pVfs,          /* The quota VFS */
524796af156Sdrh   const char *zName,          /* Name of file to be deleted */
525796af156Sdrh   int syncDir                 /* Do a directory sync after deleting */
526796af156Sdrh ){
527796af156Sdrh   int rc;                                    /* Result code */
528796af156Sdrh   quotaFile *pFile;                          /* Files in the quota */
529796af156Sdrh   quotaGroup *pGroup;                        /* The group file belongs to */
530796af156Sdrh   sqlite3_vfs *pOrigVfs = gQuota.pOrigVfs;   /* Real VFS */
531796af156Sdrh 
532796af156Sdrh   /* Do the actual file delete */
533796af156Sdrh   rc = pOrigVfs->xDelete(pOrigVfs, zName, syncDir);
534796af156Sdrh 
535796af156Sdrh   /* If the file just deleted is a member of a quota group, then remove
536796af156Sdrh   ** it from that quota group.
537796af156Sdrh   */
538796af156Sdrh   if( rc==SQLITE_OK ){
539796af156Sdrh     quotaEnter();
540796af156Sdrh     pGroup = quotaGroupFind(zName);
541796af156Sdrh     if( pGroup ){
54260f21e4bSdrh       pFile = quotaFindFile(pGroup, zName, 0);
543796af156Sdrh       if( pFile ){
544796af156Sdrh         if( pFile->nRef ){
545796af156Sdrh           pFile->deleteOnClose = 1;
546796af156Sdrh         }else{
547796af156Sdrh           quotaRemoveFile(pFile);
548796af156Sdrh           quotaGroupDeref(pGroup);
549796af156Sdrh         }
550796af156Sdrh       }
551796af156Sdrh     }
552796af156Sdrh     quotaLeave();
553796af156Sdrh   }
554796af156Sdrh   return rc;
555796af156Sdrh }
556796af156Sdrh 
557796af156Sdrh 
558748a52cdSdrh /************************ I/O Method Wrappers *******************************/
559748a52cdSdrh 
560748a52cdSdrh /* xClose requests get passed through to the original VFS.  But we
561ef9659baSdrh ** also have to unlink the quotaConn from the quotaFile and quotaGroup.
562ef9659baSdrh ** The quotaFile and/or quotaGroup are freed if they are no longer in use.
563748a52cdSdrh */
564ef9659baSdrh static int quotaClose(sqlite3_file *pConn){
565ef9659baSdrh   quotaConn *p = (quotaConn*)pConn;
566745de2e4Sdrh   quotaFile *pFile = p->pFile;
567ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
568748a52cdSdrh   int rc;
569745de2e4Sdrh   rc = pSubOpen->pMethods->xClose(pSubOpen);
570748a52cdSdrh   quotaEnter();
571745de2e4Sdrh   pFile->nRef--;
572745de2e4Sdrh   if( pFile->nRef==0 ){
573745de2e4Sdrh     quotaGroup *pGroup = pFile->pGroup;
57469b2232dSdrh     if( pFile->deleteOnClose ){
57569b2232dSdrh       gQuota.pOrigVfs->xDelete(gQuota.pOrigVfs, pFile->zFilename, 0);
57669b2232dSdrh       quotaRemoveFile(pFile);
57769b2232dSdrh     }
578748a52cdSdrh     quotaGroupDeref(pGroup);
579745de2e4Sdrh   }
580748a52cdSdrh   quotaLeave();
581748a52cdSdrh   return rc;
582748a52cdSdrh }
583748a52cdSdrh 
584748a52cdSdrh /* Pass xRead requests directory thru to the original VFS without
585748a52cdSdrh ** further processing.
586748a52cdSdrh */
587748a52cdSdrh static int quotaRead(
588ef9659baSdrh   sqlite3_file *pConn,
589748a52cdSdrh   void *pBuf,
590748a52cdSdrh   int iAmt,
591748a52cdSdrh   sqlite3_int64 iOfst
592748a52cdSdrh ){
593ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
594745de2e4Sdrh   return pSubOpen->pMethods->xRead(pSubOpen, pBuf, iAmt, iOfst);
595748a52cdSdrh }
596748a52cdSdrh 
597748a52cdSdrh /* Check xWrite requests to see if they expand the file.  If they do,
598748a52cdSdrh ** the perform a quota check before passing them through to the
599748a52cdSdrh ** original VFS.
600748a52cdSdrh */
601748a52cdSdrh static int quotaWrite(
602ef9659baSdrh   sqlite3_file *pConn,
6038cf35eb4Sdan   const void *pBuf,
604748a52cdSdrh   int iAmt,
605748a52cdSdrh   sqlite3_int64 iOfst
606748a52cdSdrh ){
607ef9659baSdrh   quotaConn *p = (quotaConn*)pConn;
608ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
609748a52cdSdrh   sqlite3_int64 iEnd = iOfst+iAmt;
610748a52cdSdrh   quotaGroup *pGroup;
611745de2e4Sdrh   quotaFile *pFile = p->pFile;
612748a52cdSdrh   sqlite3_int64 szNew;
613748a52cdSdrh 
614745de2e4Sdrh   if( pFile->iSize<iEnd ){
615745de2e4Sdrh     pGroup = pFile->pGroup;
616748a52cdSdrh     quotaEnter();
617745de2e4Sdrh     szNew = pGroup->iSize - pFile->iSize + iEnd;
6188cf35eb4Sdan     if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){
619748a52cdSdrh       if( pGroup->xCallback ){
620745de2e4Sdrh         pGroup->xCallback(pFile->zFilename, &pGroup->iLimit, szNew,
621748a52cdSdrh                           pGroup->pArg);
622748a52cdSdrh       }
6238cf35eb4Sdan       if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){
624748a52cdSdrh         quotaLeave();
625748a52cdSdrh         return SQLITE_FULL;
626748a52cdSdrh       }
627748a52cdSdrh     }
628748a52cdSdrh     pGroup->iSize = szNew;
629745de2e4Sdrh     pFile->iSize = iEnd;
630748a52cdSdrh     quotaLeave();
631748a52cdSdrh   }
632745de2e4Sdrh   return pSubOpen->pMethods->xWrite(pSubOpen, pBuf, iAmt, iOfst);
633748a52cdSdrh }
634748a52cdSdrh 
635748a52cdSdrh /* Pass xTruncate requests thru to the original VFS.  If the
636748a52cdSdrh ** success, update the file size.
637748a52cdSdrh */
638ef9659baSdrh static int quotaTruncate(sqlite3_file *pConn, sqlite3_int64 size){
639ef9659baSdrh   quotaConn *p = (quotaConn*)pConn;
640ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
641745de2e4Sdrh   int rc = pSubOpen->pMethods->xTruncate(pSubOpen, size);
642745de2e4Sdrh   quotaFile *pFile = p->pFile;
643745de2e4Sdrh   quotaGroup *pGroup;
644748a52cdSdrh   if( rc==SQLITE_OK ){
645748a52cdSdrh     quotaEnter();
646745de2e4Sdrh     pGroup = pFile->pGroup;
647745de2e4Sdrh     pGroup->iSize -= pFile->iSize;
648745de2e4Sdrh     pFile->iSize = size;
649748a52cdSdrh     pGroup->iSize += size;
650748a52cdSdrh     quotaLeave();
651748a52cdSdrh   }
652748a52cdSdrh   return rc;
653748a52cdSdrh }
654748a52cdSdrh 
655748a52cdSdrh /* Pass xSync requests through to the original VFS without change
656748a52cdSdrh */
657ef9659baSdrh static int quotaSync(sqlite3_file *pConn, int flags){
658ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
659745de2e4Sdrh   return pSubOpen->pMethods->xSync(pSubOpen, flags);
660748a52cdSdrh }
661748a52cdSdrh 
662748a52cdSdrh /* Pass xFileSize requests through to the original VFS but then
663748a52cdSdrh ** update the quotaGroup with the new size before returning.
664748a52cdSdrh */
665ef9659baSdrh static int quotaFileSize(sqlite3_file *pConn, sqlite3_int64 *pSize){
666ef9659baSdrh   quotaConn *p = (quotaConn*)pConn;
667ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
668745de2e4Sdrh   quotaFile *pFile = p->pFile;
669748a52cdSdrh   quotaGroup *pGroup;
670748a52cdSdrh   sqlite3_int64 sz;
671748a52cdSdrh   int rc;
672748a52cdSdrh 
673745de2e4Sdrh   rc = pSubOpen->pMethods->xFileSize(pSubOpen, &sz);
674748a52cdSdrh   if( rc==SQLITE_OK ){
675748a52cdSdrh     quotaEnter();
676745de2e4Sdrh     pGroup = pFile->pGroup;
677745de2e4Sdrh     pGroup->iSize -= pFile->iSize;
678745de2e4Sdrh     pFile->iSize = sz;
679748a52cdSdrh     pGroup->iSize += sz;
680748a52cdSdrh     quotaLeave();
681748a52cdSdrh     *pSize = sz;
682748a52cdSdrh   }
683748a52cdSdrh   return rc;
684748a52cdSdrh }
685748a52cdSdrh 
686748a52cdSdrh /* Pass xLock requests through to the original VFS unchanged.
687748a52cdSdrh */
688ef9659baSdrh static int quotaLock(sqlite3_file *pConn, int lock){
689ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
690745de2e4Sdrh   return pSubOpen->pMethods->xLock(pSubOpen, lock);
691748a52cdSdrh }
692748a52cdSdrh 
693748a52cdSdrh /* Pass xUnlock requests through to the original VFS unchanged.
694748a52cdSdrh */
695ef9659baSdrh static int quotaUnlock(sqlite3_file *pConn, int lock){
696ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
697745de2e4Sdrh   return pSubOpen->pMethods->xUnlock(pSubOpen, lock);
698748a52cdSdrh }
699748a52cdSdrh 
700748a52cdSdrh /* Pass xCheckReservedLock requests through to the original VFS unchanged.
701748a52cdSdrh */
702ef9659baSdrh static int quotaCheckReservedLock(sqlite3_file *pConn, int *pResOut){
703ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
704745de2e4Sdrh   return pSubOpen->pMethods->xCheckReservedLock(pSubOpen, pResOut);
705748a52cdSdrh }
706748a52cdSdrh 
707748a52cdSdrh /* Pass xFileControl requests through to the original VFS unchanged.
708748a52cdSdrh */
709ef9659baSdrh static int quotaFileControl(sqlite3_file *pConn, int op, void *pArg){
710ef9659baSdrh   sqlite3_file *pSubOpen = quotaSubOpen(pConn);
711c39f3e00Sdrh   int rc = pSubOpen->pMethods->xFileControl(pSubOpen, op, pArg);
712c39f3e00Sdrh #if defined(SQLITE_FCNTL_VFSNAME)
713c39f3e00Sdrh   if( op==SQLITE_FCNTL_VFSNAME && rc==SQLITE_OK ){
714c39f3e00Sdrh     *(char**)pArg = sqlite3_mprintf("quota/%z", *(char**)pArg);
715c39f3e00Sdrh   }
716c39f3e00Sdrh #endif
717c39f3e00Sdrh   return rc;
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 ){
89383cc1392Sdrh     int nPattern = (int)(strlen(zPattern) & 0x3fffffff);
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;
93372cd90acSdan   int nAlloc = gQuota.sThisVfs.szOsFile + gQuota.sThisVfs.mxPathname+2;
93472cd90acSdan 
93572cd90acSdan   /* Allocate space for a file-handle and the full path for file zFilename */
93672cd90acSdan   fd = (sqlite3_file *)sqlite3_malloc(nAlloc);
93772cd90acSdan   if( fd==0 ){
93872cd90acSdan     rc = SQLITE_NOMEM;
93972cd90acSdan   }else{
94072cd90acSdan     zFull = &((char *)fd)[gQuota.sThisVfs.szOsFile];
941b6020c4eSdrh     rc = gQuota.pOrigVfs->xFullPathname(gQuota.pOrigVfs, zFilename,
942b6020c4eSdrh         gQuota.sThisVfs.mxPathname+1, zFull);
94372cd90acSdan   }
94472cd90acSdan 
945b6020c4eSdrh   if( rc==SQLITE_OK ){
94672cd90acSdan     zFull[strlen(zFull)+1] = '\0';
947b6020c4eSdrh     rc = quotaOpen(&gQuota.sThisVfs, zFull, fd,
948b6020c4eSdrh                    SQLITE_OPEN_READONLY | SQLITE_OPEN_MAIN_DB, &outFlags);
949b6020c4eSdrh     if( rc==SQLITE_OK ){
95026541c9aSdrh       fd->pMethods->xFileSize(fd, &iSize);
95126541c9aSdrh       fd->pMethods->xClose(fd);
952b6020c4eSdrh     }else if( rc==SQLITE_CANTOPEN ){
953b6020c4eSdrh       quotaGroup *pGroup;
954b6020c4eSdrh       quotaFile *pFile;
955b6020c4eSdrh       quotaEnter();
956b6020c4eSdrh       pGroup = quotaGroupFind(zFull);
957b6020c4eSdrh       if( pGroup ){
95860f21e4bSdrh         pFile = quotaFindFile(pGroup, zFull, 0);
959b6020c4eSdrh         if( pFile ) quotaRemoveFile(pFile);
960b6020c4eSdrh       }
961b6020c4eSdrh       quotaLeave();
962b6020c4eSdrh     }
96372cd90acSdan   }
96472cd90acSdan 
965b6020c4eSdrh   sqlite3_free(fd);
966b6020c4eSdrh   return rc;
967b6020c4eSdrh }
968b6020c4eSdrh 
96960f21e4bSdrh /*
97060f21e4bSdrh ** Open a potentially quotaed file for I/O.
97160f21e4bSdrh */
97260f21e4bSdrh quota_FILE *sqlite3_quota_fopen(const char *zFilename, const char *zMode){
97360f21e4bSdrh   quota_FILE *p = 0;
97460f21e4bSdrh   char *zFull = 0;
975c00ce490Sdrh   char *zFullTranslated = 0;
97660f21e4bSdrh   int rc;
97760f21e4bSdrh   quotaGroup *pGroup;
97860f21e4bSdrh   quotaFile *pFile;
97960f21e4bSdrh 
980663cebfeSdrh   zFull = (char*)sqlite3_malloc(gQuota.sThisVfs.mxPathname + 1);
981a76e8589Sdrh   if( zFull==0 ) return 0;
98260f21e4bSdrh   rc = gQuota.pOrigVfs->xFullPathname(gQuota.pOrigVfs, zFilename,
98360f21e4bSdrh                                       gQuota.sThisVfs.mxPathname+1, zFull);
98460f21e4bSdrh   if( rc ) goto quota_fopen_error;
985663cebfeSdrh   p = (quota_FILE*)sqlite3_malloc(sizeof(*p));
98660f21e4bSdrh   if( p==0 ) goto quota_fopen_error;
98760f21e4bSdrh   memset(p, 0, sizeof(*p));
988a76e8589Sdrh   zFullTranslated = quota_utf8_to_mbcs(zFull);
989a76e8589Sdrh   if( zFullTranslated==0 ) goto quota_fopen_error;
990a76e8589Sdrh   p->f = fopen(zFullTranslated, zMode);
99160f21e4bSdrh   if( p->f==0 ) goto quota_fopen_error;
99260f21e4bSdrh   quotaEnter();
99360f21e4bSdrh   pGroup = quotaGroupFind(zFull);
99460f21e4bSdrh   if( pGroup ){
99560f21e4bSdrh     pFile = quotaFindFile(pGroup, zFull, 1);
99660f21e4bSdrh     if( pFile==0 ){
99760f21e4bSdrh       quotaLeave();
99860f21e4bSdrh       goto quota_fopen_error;
99960f21e4bSdrh     }
100060f21e4bSdrh     pFile->nRef++;
100160f21e4bSdrh     p->pFile = pFile;
100260f21e4bSdrh   }
100360f21e4bSdrh   quotaLeave();
100460f21e4bSdrh   sqlite3_free(zFull);
1005c00ce490Sdrh #if SQLITE_OS_WIN
1006c00ce490Sdrh   p->zMbcsName = zFullTranslated;
1007c00ce490Sdrh #endif
100860f21e4bSdrh   return p;
100960f21e4bSdrh 
101060f21e4bSdrh quota_fopen_error:
1011c00ce490Sdrh   quota_mbcs_free(zFullTranslated);
101260f21e4bSdrh   sqlite3_free(zFull);
101360f21e4bSdrh   if( p && p->f ) fclose(p->f);
101460f21e4bSdrh   sqlite3_free(p);
101560f21e4bSdrh   return 0;
101660f21e4bSdrh }
101760f21e4bSdrh 
101860f21e4bSdrh /*
101960f21e4bSdrh ** Read content from a quota_FILE
102060f21e4bSdrh */
102160f21e4bSdrh size_t sqlite3_quota_fread(
102260f21e4bSdrh   void *pBuf,            /* Store the content here */
102360f21e4bSdrh   size_t size,           /* Size of each element */
102460f21e4bSdrh   size_t nmemb,          /* Number of elements to read */
102560f21e4bSdrh   quota_FILE *p          /* Read from this quota_FILE object */
102660f21e4bSdrh ){
102760f21e4bSdrh   return fread(pBuf, size, nmemb, p->f);
102860f21e4bSdrh }
102960f21e4bSdrh 
103060f21e4bSdrh /*
103160f21e4bSdrh ** Write content into a quota_FILE.  Invoke the quota callback and block
103260f21e4bSdrh ** the write if we exceed quota.
103360f21e4bSdrh */
103460f21e4bSdrh size_t sqlite3_quota_fwrite(
103598c78ea6Sdrh   const void *pBuf,      /* Take content to write from here */
103660f21e4bSdrh   size_t size,           /* Size of each element */
103760f21e4bSdrh   size_t nmemb,          /* Number of elements */
103860f21e4bSdrh   quota_FILE *p          /* Write to this quota_FILE objecct */
103960f21e4bSdrh ){
104060f21e4bSdrh   sqlite3_int64 iOfst;
104160f21e4bSdrh   sqlite3_int64 iEnd;
104260f21e4bSdrh   sqlite3_int64 szNew;
104360f21e4bSdrh   quotaFile *pFile;
1044c00ce490Sdrh   size_t rc;
104560f21e4bSdrh 
104660f21e4bSdrh   iOfst = ftell(p->f);
104760f21e4bSdrh   iEnd = iOfst + size*nmemb;
104860f21e4bSdrh   pFile = p->pFile;
1049c8ccda60Sdrh   if( pFile && pFile->iSize<iEnd ){
105060f21e4bSdrh     quotaGroup *pGroup = pFile->pGroup;
105160f21e4bSdrh     quotaEnter();
105260f21e4bSdrh     szNew = pGroup->iSize - pFile->iSize + iEnd;
105360f21e4bSdrh     if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){
105460f21e4bSdrh       if( pGroup->xCallback ){
105560f21e4bSdrh         pGroup->xCallback(pFile->zFilename, &pGroup->iLimit, szNew,
105660f21e4bSdrh                           pGroup->pArg);
105760f21e4bSdrh       }
105860f21e4bSdrh       if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){
105960f21e4bSdrh         iEnd = pGroup->iLimit - pGroup->iSize + pFile->iSize;
10607da5fcb0Sdrh         nmemb = (size_t)((iEnd - iOfst)/size);
106160f21e4bSdrh         iEnd = iOfst + size*nmemb;
106260f21e4bSdrh         szNew = pGroup->iSize - pFile->iSize + iEnd;
106360f21e4bSdrh       }
106460f21e4bSdrh     }
106560f21e4bSdrh     pGroup->iSize = szNew;
106660f21e4bSdrh     pFile->iSize = iEnd;
106760f21e4bSdrh     quotaLeave();
1068c00ce490Sdrh   }else{
1069c00ce490Sdrh     pFile = 0;
107060f21e4bSdrh   }
1071c00ce490Sdrh   rc = fwrite(pBuf, size, nmemb, p->f);
1072c00ce490Sdrh 
1073c00ce490Sdrh   /* If the write was incomplete, adjust the file size and group size
1074c00ce490Sdrh   ** downward */
1075c00ce490Sdrh   if( rc<nmemb && pFile ){
1076*b3f787f4Sdrh     size_t nWritten = rc;
1077c00ce490Sdrh     sqlite3_int64 iNewEnd = iOfst + size*nWritten;
1078c00ce490Sdrh     if( iNewEnd<iEnd ) iNewEnd = iEnd;
1079c00ce490Sdrh     quotaEnter();
1080c00ce490Sdrh     pFile->pGroup->iSize += iNewEnd - pFile->iSize;
1081c00ce490Sdrh     pFile->iSize = iNewEnd;
1082c00ce490Sdrh     quotaLeave();
1083c00ce490Sdrh   }
1084c00ce490Sdrh   return rc;
108560f21e4bSdrh }
108660f21e4bSdrh 
108760f21e4bSdrh /*
108860f21e4bSdrh ** Close an open quota_FILE stream.
108960f21e4bSdrh */
109060f21e4bSdrh int sqlite3_quota_fclose(quota_FILE *p){
109160f21e4bSdrh   int rc;
109260f21e4bSdrh   quotaFile *pFile;
109360f21e4bSdrh   rc = fclose(p->f);
109460f21e4bSdrh   pFile = p->pFile;
1095c8ccda60Sdrh   if( pFile ){
109660f21e4bSdrh     quotaEnter();
109760f21e4bSdrh     pFile->nRef--;
109860f21e4bSdrh     if( pFile->nRef==0 ){
109960f21e4bSdrh       quotaGroup *pGroup = pFile->pGroup;
110069b2232dSdrh       if( pFile->deleteOnClose ){
110169b2232dSdrh         gQuota.pOrigVfs->xDelete(gQuota.pOrigVfs, pFile->zFilename, 0);
110269b2232dSdrh         quotaRemoveFile(pFile);
110369b2232dSdrh       }
110460f21e4bSdrh       quotaGroupDeref(pGroup);
110560f21e4bSdrh     }
110660f21e4bSdrh     quotaLeave();
1107c8ccda60Sdrh   }
1108c00ce490Sdrh #if SQLITE_OS_WIN
1109c00ce490Sdrh   quota_mbcs_free(p->zMbcsName);
1110c00ce490Sdrh #endif
111160f21e4bSdrh   sqlite3_free(p);
111260f21e4bSdrh   return rc;
111360f21e4bSdrh }
111460f21e4bSdrh 
111560f21e4bSdrh /*
111669b2232dSdrh ** Flush memory buffers for a quota_FILE to disk.
111769b2232dSdrh */
111827cec37dSdrh int sqlite3_quota_fflush(quota_FILE *p, int doFsync){
111927cec37dSdrh   int rc;
112027cec37dSdrh   rc = fflush(p->f);
112127cec37dSdrh   if( rc==0 && doFsync ){
112227cec37dSdrh #if SQLITE_OS_UNIX
112327cec37dSdrh     rc = fsync(fileno(p->f));
112427cec37dSdrh #endif
112527cec37dSdrh #if SQLITE_OS_WIN
1126c39f3e00Sdrh     rc = _commit(_fileno(p->f));
112727cec37dSdrh #endif
112827cec37dSdrh   }
1129c39f3e00Sdrh   return rc!=0;
113069b2232dSdrh }
113169b2232dSdrh 
113269b2232dSdrh /*
113360f21e4bSdrh ** Seek on a quota_FILE stream.
113460f21e4bSdrh */
113560f21e4bSdrh int sqlite3_quota_fseek(quota_FILE *p, long offset, int whence){
113660f21e4bSdrh   return fseek(p->f, offset, whence);
113760f21e4bSdrh }
113860f21e4bSdrh 
113960f21e4bSdrh /*
114060f21e4bSdrh ** rewind a quota_FILE stream.
114160f21e4bSdrh */
114260f21e4bSdrh void sqlite3_quota_rewind(quota_FILE *p){
114360f21e4bSdrh   rewind(p->f);
114460f21e4bSdrh }
114560f21e4bSdrh 
114660f21e4bSdrh /*
114760f21e4bSdrh ** Tell the current location of a quota_FILE stream.
114860f21e4bSdrh */
114960f21e4bSdrh long sqlite3_quota_ftell(quota_FILE *p){
115060f21e4bSdrh   return ftell(p->f);
115160f21e4bSdrh }
115260f21e4bSdrh 
115360f21e4bSdrh /*
115498c78ea6Sdrh ** Test the error indicator for the given file.
115598c78ea6Sdrh */
115698c78ea6Sdrh int sqlite3_quota_ferror(quota_FILE *p){
115798c78ea6Sdrh   return ferror(p->f);
115898c78ea6Sdrh }
115998c78ea6Sdrh 
116098c78ea6Sdrh /*
1161c00ce490Sdrh ** Truncate a file to szNew bytes.
1162c00ce490Sdrh */
1163c00ce490Sdrh int sqlite3_quota_ftruncate(quota_FILE *p, sqlite3_int64 szNew){
1164c00ce490Sdrh   quotaFile *pFile = p->pFile;
1165c00ce490Sdrh   int rc;
1166c00ce490Sdrh   if( (pFile = p->pFile)!=0 && pFile->iSize<szNew ){
1167c00ce490Sdrh     quotaGroup *pGroup;
1168c00ce490Sdrh     if( pFile->iSize<szNew ){
1169c00ce490Sdrh       /* This routine cannot be used to extend a file that is under
1170c00ce490Sdrh       ** quota management.  Only true truncation is allowed. */
1171c00ce490Sdrh       return -1;
1172c00ce490Sdrh     }
1173c00ce490Sdrh     pGroup = pFile->pGroup;
1174c00ce490Sdrh     quotaEnter();
1175c00ce490Sdrh     pGroup->iSize += szNew - pFile->iSize;
1176c00ce490Sdrh     quotaLeave();
1177c00ce490Sdrh   }
1178c00ce490Sdrh #if SQLITE_OS_UNIX
1179c00ce490Sdrh   rc = ftruncate(fileno(p->f), szNew);
1180c00ce490Sdrh #endif
1181c00ce490Sdrh #if SQLITE_OS_WIN
1182c00ce490Sdrh   rc = _chsize_s(_fileno(p->f), szNew);
1183c00ce490Sdrh #endif
1184c00ce490Sdrh   if( pFile && rc==0 ){
1185c00ce490Sdrh     quotaGroup *pGroup = pFile->pGroup;
1186c00ce490Sdrh     quotaEnter();
1187c00ce490Sdrh     pGroup->iSize += szNew - pFile->iSize;
1188c00ce490Sdrh     pFile->iSize = szNew;
1189c00ce490Sdrh     quotaLeave();
1190c00ce490Sdrh   }
1191c00ce490Sdrh   return rc;
1192c00ce490Sdrh }
1193c00ce490Sdrh 
1194c00ce490Sdrh /*
1195c00ce490Sdrh ** Determine the time that the given file was last modified, in
1196c00ce490Sdrh ** seconds size 1970.  Write the result into *pTime.  Return 0 on
1197c00ce490Sdrh ** success and non-zero on any kind of error.
1198c00ce490Sdrh */
1199c00ce490Sdrh int sqlite3_quota_file_mtime(quota_FILE *p, time_t *pTime){
1200c00ce490Sdrh   int rc;
1201c00ce490Sdrh #if SQLITE_OS_UNIX
1202c00ce490Sdrh   struct stat buf;
1203c00ce490Sdrh   rc = fstat(fileno(p->f), &buf);
1204c00ce490Sdrh #endif
1205c00ce490Sdrh #if SQLITE_OS_WIN
1206c00ce490Sdrh   struct _stati64 buf;
1207c00ce490Sdrh   rc = _stati64(p->zMbcsName, &buf);
1208c00ce490Sdrh #endif
1209c00ce490Sdrh   if( rc==0 ) *pTime = buf.st_mtime;
1210c00ce490Sdrh   return rc;
1211c00ce490Sdrh }
1212c00ce490Sdrh 
1213c00ce490Sdrh /*
1214c00ce490Sdrh ** Return the true size of the file, as reported by the operating
1215c00ce490Sdrh ** system.
1216c00ce490Sdrh */
1217c00ce490Sdrh sqlite3_int64 sqlite3_quota_file_truesize(quota_FILE *p){
1218c00ce490Sdrh   int rc;
1219c00ce490Sdrh #if SQLITE_OS_UNIX
1220c00ce490Sdrh   struct stat buf;
1221c00ce490Sdrh   rc = fstat(fileno(p->f), &buf);
1222c00ce490Sdrh #endif
1223c00ce490Sdrh #if SQLITE_OS_WIN
1224c00ce490Sdrh   struct _stati64 buf;
1225c00ce490Sdrh   rc = _stati64(p->zMbcsName, &buf);
1226c00ce490Sdrh #endif
1227c00ce490Sdrh   return rc==0 ? buf.st_size : -1;
1228c00ce490Sdrh }
1229c00ce490Sdrh 
1230c00ce490Sdrh /*
1231c00ce490Sdrh ** Return the size of the file, as it is known to the quota subsystem.
1232c00ce490Sdrh */
1233c00ce490Sdrh sqlite3_int64 sqlite3_quota_file_size(quota_FILE *p){
1234c00ce490Sdrh   return p->pFile ? p->pFile->iSize : -1;
1235c00ce490Sdrh }
1236c00ce490Sdrh 
1237c00ce490Sdrh /*
123898c78ea6Sdrh ** Determine the amount of data in bytes available for reading
123998c78ea6Sdrh ** in the given file.
124098c78ea6Sdrh */
124198c78ea6Sdrh long sqlite3_quota_file_available(quota_FILE *p){
124298c78ea6Sdrh   FILE* f = p->f;
124398c78ea6Sdrh   long pos1, pos2;
124498c78ea6Sdrh   int rc;
124598c78ea6Sdrh   pos1 = ftell(f);
124698c78ea6Sdrh   if ( pos1 < 0 ) return -1;
124798c78ea6Sdrh   rc = fseek(f, 0, SEEK_END);
124898c78ea6Sdrh   if ( rc != 0 ) return -1;
124998c78ea6Sdrh   pos2 = ftell(f);
125098c78ea6Sdrh   if ( pos2 < 0 ) return -1;
125198c78ea6Sdrh   rc = fseek(f, pos1, SEEK_SET);
125298c78ea6Sdrh   if ( rc != 0 ) return -1;
125398c78ea6Sdrh   return pos2 - pos1;
125498c78ea6Sdrh }
125598c78ea6Sdrh 
125698c78ea6Sdrh /*
125769b2232dSdrh ** Remove a managed file.  Update quotas accordingly.
125860f21e4bSdrh */
125960f21e4bSdrh int sqlite3_quota_remove(const char *zFilename){
126069b2232dSdrh   char *zFull;            /* Full pathname for zFilename */
126183cc1392Sdrh   size_t nFull;           /* Number of bytes in zFilename */
126269b2232dSdrh   int rc;                 /* Result code */
126369b2232dSdrh   quotaGroup *pGroup;     /* Group containing zFilename */
126469b2232dSdrh   quotaFile *pFile;       /* A file in the group */
126569b2232dSdrh   quotaFile *pNextFile;   /* next file in the group */
126669b2232dSdrh   int diff;               /* Difference between filenames */
126769b2232dSdrh   char c;                 /* First character past end of pattern */
126869b2232dSdrh 
1269663cebfeSdrh   zFull = (char*)sqlite3_malloc(gQuota.sThisVfs.mxPathname + 1);
127069b2232dSdrh   if( zFull==0 ) return SQLITE_NOMEM;
127169b2232dSdrh   rc = gQuota.pOrigVfs->xFullPathname(gQuota.pOrigVfs, zFilename,
127269b2232dSdrh                                       gQuota.sThisVfs.mxPathname+1, zFull);
127369b2232dSdrh   if( rc ){
127469b2232dSdrh     sqlite3_free(zFull);
127560f21e4bSdrh     return rc;
127660f21e4bSdrh   }
127760f21e4bSdrh 
127869b2232dSdrh   /* Figure out the length of the full pathname.  If the name ends with
127969b2232dSdrh   ** / (or \ on windows) then remove the trailing /.
128069b2232dSdrh   */
128169b2232dSdrh   nFull = strlen(zFull);
128269b2232dSdrh   if( nFull>0 && (zFull[nFull-1]=='/' || zFull[nFull-1]=='\\') ){
128369b2232dSdrh     nFull--;
128469b2232dSdrh     zFull[nFull] = 0;
128569b2232dSdrh   }
128669b2232dSdrh 
128769b2232dSdrh   quotaEnter();
128869b2232dSdrh   pGroup = quotaGroupFind(zFull);
128969b2232dSdrh   if( pGroup ){
129069b2232dSdrh     for(pFile=pGroup->pFiles; pFile && rc==SQLITE_OK; pFile=pNextFile){
129169b2232dSdrh       pNextFile = pFile->pNext;
129269b2232dSdrh       diff = memcmp(zFull, pFile->zFilename, nFull);
129369b2232dSdrh       if( diff==0 && ((c = pFile->zFilename[nFull])==0 || c=='/' || c=='\\') ){
129469b2232dSdrh         if( pFile->nRef ){
129569b2232dSdrh           pFile->deleteOnClose = 1;
129669b2232dSdrh         }else{
129769b2232dSdrh           rc = gQuota.pOrigVfs->xDelete(gQuota.pOrigVfs, pFile->zFilename, 0);
129869b2232dSdrh           quotaRemoveFile(pFile);
129969b2232dSdrh           quotaGroupDeref(pGroup);
130069b2232dSdrh         }
130169b2232dSdrh       }
130269b2232dSdrh     }
130369b2232dSdrh   }
130469b2232dSdrh   quotaLeave();
130569b2232dSdrh   sqlite3_free(zFull);
130669b2232dSdrh   return rc;
130769b2232dSdrh }
1308748a52cdSdrh 
1309748a52cdSdrh /***************************** Test Code ***********************************/
1310748a52cdSdrh #ifdef SQLITE_TEST
1311748a52cdSdrh #include <tcl.h>
13128cf35eb4Sdan 
1313ef9659baSdrh /*
1314ef9659baSdrh ** Argument passed to a TCL quota-over-limit callback.
1315ef9659baSdrh */
13168cf35eb4Sdan typedef struct TclQuotaCallback TclQuotaCallback;
13178cf35eb4Sdan struct TclQuotaCallback {
1318ef9659baSdrh   Tcl_Interp *interp;    /* Interpreter in which to run the script */
1319ef9659baSdrh   Tcl_Obj *pScript;      /* Script to be run */
13208cf35eb4Sdan };
13218cf35eb4Sdan 
13228cf35eb4Sdan extern const char *sqlite3TestErrorName(int);
13238cf35eb4Sdan 
1324ef9659baSdrh 
1325ef9659baSdrh /*
1326ef9659baSdrh ** This is the callback from a quota-over-limit.
1327ef9659baSdrh */
13288cf35eb4Sdan static void tclQuotaCallback(
13298cf35eb4Sdan   const char *zFilename,          /* Name of file whose size increases */
13308cf35eb4Sdan   sqlite3_int64 *piLimit,         /* IN/OUT: The current limit */
13318cf35eb4Sdan   sqlite3_int64 iSize,            /* Total size of all files in the group */
13328cf35eb4Sdan   void *pArg                      /* Client data */
13338cf35eb4Sdan ){
13348cf35eb4Sdan   TclQuotaCallback *p;            /* Callback script object */
13358cf35eb4Sdan   Tcl_Obj *pEval;                 /* Script to evaluate */
13368cf35eb4Sdan   Tcl_Obj *pVarname;              /* Name of variable to pass as 2nd arg */
13378cf35eb4Sdan   unsigned int rnd;               /* Random part of pVarname */
13388cf35eb4Sdan   int rc;                         /* Tcl error code */
13398cf35eb4Sdan 
13408cf35eb4Sdan   p = (TclQuotaCallback *)pArg;
13413d3e60e4Sdrh   if( p==0 ) return;
13428cf35eb4Sdan 
13438cf35eb4Sdan   pVarname = Tcl_NewStringObj("::piLimit_", -1);
13448cf35eb4Sdan   Tcl_IncrRefCount(pVarname);
13458cf35eb4Sdan   sqlite3_randomness(sizeof(rnd), (void *)&rnd);
13468cf35eb4Sdan   Tcl_AppendObjToObj(pVarname, Tcl_NewIntObj((int)(rnd&0x7FFFFFFF)));
13478cf35eb4Sdan   Tcl_ObjSetVar2(p->interp, pVarname, 0, Tcl_NewWideIntObj(*piLimit), 0);
13488cf35eb4Sdan 
13498cf35eb4Sdan   pEval = Tcl_DuplicateObj(p->pScript);
13508cf35eb4Sdan   Tcl_IncrRefCount(pEval);
13518cf35eb4Sdan   Tcl_ListObjAppendElement(0, pEval, Tcl_NewStringObj(zFilename, -1));
13528cf35eb4Sdan   Tcl_ListObjAppendElement(0, pEval, pVarname);
13538cf35eb4Sdan   Tcl_ListObjAppendElement(0, pEval, Tcl_NewWideIntObj(iSize));
13548cf35eb4Sdan   rc = Tcl_EvalObjEx(p->interp, pEval, TCL_EVAL_GLOBAL);
13558cf35eb4Sdan 
13568cf35eb4Sdan   if( rc==TCL_OK ){
1357*b3f787f4Sdrh     Tcl_WideInt x;
13588cf35eb4Sdan     Tcl_Obj *pLimit = Tcl_ObjGetVar2(p->interp, pVarname, 0, 0);
1359*b3f787f4Sdrh     rc = Tcl_GetWideIntFromObj(p->interp, pLimit, &x);
1360*b3f787f4Sdrh     *piLimit = x;
13618cf35eb4Sdan     Tcl_UnsetVar(p->interp, Tcl_GetString(pVarname), 0);
13628cf35eb4Sdan   }
13638cf35eb4Sdan 
13648cf35eb4Sdan   Tcl_DecrRefCount(pEval);
13658cf35eb4Sdan   Tcl_DecrRefCount(pVarname);
13668cf35eb4Sdan   if( rc!=TCL_OK ) Tcl_BackgroundError(p->interp);
13678cf35eb4Sdan }
13688cf35eb4Sdan 
13698cf35eb4Sdan /*
1370ef9659baSdrh ** Destructor for a TCL quota-over-limit callback.
1371ef9659baSdrh */
1372ef9659baSdrh static void tclCallbackDestructor(void *pObj){
1373ef9659baSdrh   TclQuotaCallback *p = (TclQuotaCallback*)pObj;
1374ef9659baSdrh   if( p ){
1375ef9659baSdrh     Tcl_DecrRefCount(p->pScript);
13769fc7702cSdan     sqlite3_free((char *)p);
1377ef9659baSdrh   }
1378ef9659baSdrh }
1379ef9659baSdrh 
1380ef9659baSdrh /*
13818cf35eb4Sdan ** tclcmd: sqlite3_quota_initialize NAME MAKEDEFAULT
13828cf35eb4Sdan */
13838cf35eb4Sdan static int test_quota_initialize(
13848cf35eb4Sdan   void * clientData,
13858cf35eb4Sdan   Tcl_Interp *interp,
13868cf35eb4Sdan   int objc,
13878cf35eb4Sdan   Tcl_Obj *CONST objv[]
13888cf35eb4Sdan ){
13898cf35eb4Sdan   const char *zName;              /* Name of new quota VFS */
13908cf35eb4Sdan   int makeDefault;                /* True to make the new VFS the default */
13918cf35eb4Sdan   int rc;                         /* Value returned by quota_initialize() */
13928cf35eb4Sdan 
13938cf35eb4Sdan   /* Process arguments */
13948cf35eb4Sdan   if( objc!=3 ){
13958cf35eb4Sdan     Tcl_WrongNumArgs(interp, 1, objv, "NAME MAKEDEFAULT");
13968cf35eb4Sdan     return TCL_ERROR;
13978cf35eb4Sdan   }
13988cf35eb4Sdan   zName = Tcl_GetString(objv[1]);
13998cf35eb4Sdan   if( Tcl_GetBooleanFromObj(interp, objv[2], &makeDefault) ) return TCL_ERROR;
14008cf35eb4Sdan   if( zName[0]=='\0' ) zName = 0;
14018cf35eb4Sdan 
14028cf35eb4Sdan   /* Call sqlite3_quota_initialize() */
14038cf35eb4Sdan   rc = sqlite3_quota_initialize(zName, makeDefault);
14048cf35eb4Sdan   Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC);
14058cf35eb4Sdan 
14068cf35eb4Sdan   return TCL_OK;
14078cf35eb4Sdan }
14088cf35eb4Sdan 
14098cf35eb4Sdan /*
14108cf35eb4Sdan ** tclcmd: sqlite3_quota_shutdown
14118cf35eb4Sdan */
14128cf35eb4Sdan static int test_quota_shutdown(
14138cf35eb4Sdan   void * clientData,
14148cf35eb4Sdan   Tcl_Interp *interp,
14158cf35eb4Sdan   int objc,
14168cf35eb4Sdan   Tcl_Obj *CONST objv[]
14178cf35eb4Sdan ){
14188cf35eb4Sdan   int rc;                         /* Value returned by quota_shutdown() */
14198cf35eb4Sdan 
14208cf35eb4Sdan   if( objc!=1 ){
14218cf35eb4Sdan     Tcl_WrongNumArgs(interp, 1, objv, "");
14228cf35eb4Sdan     return TCL_ERROR;
14238cf35eb4Sdan   }
14248cf35eb4Sdan 
14258cf35eb4Sdan   /* Call sqlite3_quota_shutdown() */
14268cf35eb4Sdan   rc = sqlite3_quota_shutdown();
14278cf35eb4Sdan   Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC);
14288cf35eb4Sdan 
14298cf35eb4Sdan   return TCL_OK;
14308cf35eb4Sdan }
14318cf35eb4Sdan 
14328cf35eb4Sdan /*
14338cf35eb4Sdan ** tclcmd: sqlite3_quota_set PATTERN LIMIT SCRIPT
14348cf35eb4Sdan */
14358cf35eb4Sdan static int test_quota_set(
14368cf35eb4Sdan   void * clientData,
14378cf35eb4Sdan   Tcl_Interp *interp,
14388cf35eb4Sdan   int objc,
14398cf35eb4Sdan   Tcl_Obj *CONST objv[]
14408cf35eb4Sdan ){
14418cf35eb4Sdan   const char *zPattern;           /* File pattern to configure */
1442*b3f787f4Sdrh   Tcl_WideInt iLimit;             /* Initial quota in bytes */
14438cf35eb4Sdan   Tcl_Obj *pScript;               /* Tcl script to invoke to increase quota */
14448cf35eb4Sdan   int rc;                         /* Value returned by quota_set() */
14458cf35eb4Sdan   TclQuotaCallback *p;            /* Callback object */
1446ef9659baSdrh   int nScript;                    /* Length of callback script */
14479fc7702cSdan   void (*xDestroy)(void*);        /* Optional destructor for pArg */
144806bfd36aSdan   void (*xCallback)(const char *, sqlite3_int64 *, sqlite3_int64, void *);
14498cf35eb4Sdan 
14508cf35eb4Sdan   /* Process arguments */
14518cf35eb4Sdan   if( objc!=4 ){
14528cf35eb4Sdan     Tcl_WrongNumArgs(interp, 1, objv, "PATTERN LIMIT SCRIPT");
14538cf35eb4Sdan     return TCL_ERROR;
14548cf35eb4Sdan   }
14558cf35eb4Sdan   zPattern = Tcl_GetString(objv[1]);
14568cf35eb4Sdan   if( Tcl_GetWideIntFromObj(interp, objv[2], &iLimit) ) return TCL_ERROR;
14578cf35eb4Sdan   pScript = objv[3];
1458ef9659baSdrh   Tcl_GetStringFromObj(pScript, &nScript);
14598cf35eb4Sdan 
1460ef9659baSdrh   if( nScript>0 ){
14618cf35eb4Sdan     /* Allocate a TclQuotaCallback object */
14629fc7702cSdan     p = (TclQuotaCallback *)sqlite3_malloc(sizeof(TclQuotaCallback));
14639fc7702cSdan     if( !p ){
14649fc7702cSdan       Tcl_SetResult(interp, (char *)"SQLITE_NOMEM", TCL_STATIC);
14659fc7702cSdan       return TCL_OK;
14669fc7702cSdan     }
14678cf35eb4Sdan     memset(p, 0, sizeof(TclQuotaCallback));
14688cf35eb4Sdan     p->interp = interp;
1469ef9659baSdrh     Tcl_IncrRefCount(pScript);
14708cf35eb4Sdan     p->pScript = pScript;
14719fc7702cSdan     xDestroy = tclCallbackDestructor;
147206bfd36aSdan     xCallback = tclQuotaCallback;
1473ef9659baSdrh   }else{
1474ef9659baSdrh     p = 0;
14759fc7702cSdan     xDestroy = 0;
147606bfd36aSdan     xCallback = 0;
14778cf35eb4Sdan   }
14789fc7702cSdan 
1479ef9659baSdrh   /* Invoke sqlite3_quota_set() */
148006bfd36aSdan   rc = sqlite3_quota_set(zPattern, iLimit, xCallback, (void*)p, xDestroy);
14818cf35eb4Sdan 
14828cf35eb4Sdan   Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC);
14838cf35eb4Sdan   return TCL_OK;
14848cf35eb4Sdan }
14858cf35eb4Sdan 
14868cf35eb4Sdan /*
1487b6020c4eSdrh ** tclcmd: sqlite3_quota_file FILENAME
1488b6020c4eSdrh */
1489b6020c4eSdrh static int test_quota_file(
1490b6020c4eSdrh   void * clientData,
1491b6020c4eSdrh   Tcl_Interp *interp,
1492b6020c4eSdrh   int objc,
1493b6020c4eSdrh   Tcl_Obj *CONST objv[]
1494b6020c4eSdrh ){
1495b6020c4eSdrh   const char *zFilename;          /* File pattern to configure */
1496b6020c4eSdrh   int rc;                         /* Value returned by quota_file() */
1497b6020c4eSdrh 
1498b6020c4eSdrh   /* Process arguments */
1499b6020c4eSdrh   if( objc!=2 ){
1500b6020c4eSdrh     Tcl_WrongNumArgs(interp, 1, objv, "FILENAME");
1501b6020c4eSdrh     return TCL_ERROR;
1502b6020c4eSdrh   }
1503b6020c4eSdrh   zFilename = Tcl_GetString(objv[1]);
1504b6020c4eSdrh 
1505b6020c4eSdrh   /* Invoke sqlite3_quota_file() */
1506b6020c4eSdrh   rc = sqlite3_quota_file(zFilename);
1507b6020c4eSdrh 
1508b6020c4eSdrh   Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC);
1509b6020c4eSdrh   return TCL_OK;
1510b6020c4eSdrh }
1511b6020c4eSdrh 
1512b6020c4eSdrh /*
1513ef9659baSdrh ** tclcmd:  sqlite3_quota_dump
1514ef9659baSdrh */
1515ef9659baSdrh static int test_quota_dump(
1516ef9659baSdrh   void * clientData,
1517ef9659baSdrh   Tcl_Interp *interp,
1518ef9659baSdrh   int objc,
1519ef9659baSdrh   Tcl_Obj *CONST objv[]
1520ef9659baSdrh ){
1521ef9659baSdrh   Tcl_Obj *pResult;
1522ef9659baSdrh   Tcl_Obj *pGroupTerm;
1523ef9659baSdrh   Tcl_Obj *pFileTerm;
1524ef9659baSdrh   quotaGroup *pGroup;
1525ef9659baSdrh   quotaFile *pFile;
1526ef9659baSdrh 
1527ef9659baSdrh   pResult = Tcl_NewObj();
1528ef9659baSdrh   quotaEnter();
1529ef9659baSdrh   for(pGroup=gQuota.pGroup; pGroup; pGroup=pGroup->pNext){
1530ef9659baSdrh     pGroupTerm = Tcl_NewObj();
1531ef9659baSdrh     Tcl_ListObjAppendElement(interp, pGroupTerm,
1532ef9659baSdrh           Tcl_NewStringObj(pGroup->zPattern, -1));
1533ef9659baSdrh     Tcl_ListObjAppendElement(interp, pGroupTerm,
1534ef9659baSdrh           Tcl_NewWideIntObj(pGroup->iLimit));
1535ef9659baSdrh     Tcl_ListObjAppendElement(interp, pGroupTerm,
1536ef9659baSdrh           Tcl_NewWideIntObj(pGroup->iSize));
153775c19174Sdrh     for(pFile=pGroup->pFiles; pFile; pFile=pFile->pNext){
1538c8ccda60Sdrh       int i;
1539c8ccda60Sdrh       char zTemp[1000];
1540ef9659baSdrh       pFileTerm = Tcl_NewObj();
1541c8ccda60Sdrh       sqlite3_snprintf(sizeof(zTemp), zTemp, "%s", pFile->zFilename);
1542c8ccda60Sdrh       for(i=0; zTemp[i]; i++){ if( zTemp[i]=='\\' ) zTemp[i] = '/'; }
1543ef9659baSdrh       Tcl_ListObjAppendElement(interp, pFileTerm,
1544c8ccda60Sdrh             Tcl_NewStringObj(zTemp, -1));
1545ef9659baSdrh       Tcl_ListObjAppendElement(interp, pFileTerm,
1546ef9659baSdrh             Tcl_NewWideIntObj(pFile->iSize));
1547ef9659baSdrh       Tcl_ListObjAppendElement(interp, pFileTerm,
1548ef9659baSdrh             Tcl_NewWideIntObj(pFile->nRef));
1549796af156Sdrh       Tcl_ListObjAppendElement(interp, pFileTerm,
1550796af156Sdrh             Tcl_NewWideIntObj(pFile->deleteOnClose));
1551ef9659baSdrh       Tcl_ListObjAppendElement(interp, pGroupTerm, pFileTerm);
1552ef9659baSdrh     }
1553ef9659baSdrh     Tcl_ListObjAppendElement(interp, pResult, pGroupTerm);
1554ef9659baSdrh   }
1555ef9659baSdrh   quotaLeave();
1556ef9659baSdrh   Tcl_SetObjResult(interp, pResult);
1557ef9659baSdrh   return TCL_OK;
1558ef9659baSdrh }
1559ef9659baSdrh 
1560748a52cdSdrh /*
1561a76e8589Sdrh ** tclcmd: sqlite3_quota_fopen FILENAME MODE
1562a76e8589Sdrh */
1563a76e8589Sdrh static int test_quota_fopen(
1564a76e8589Sdrh   void * clientData,
1565a76e8589Sdrh   Tcl_Interp *interp,
1566a76e8589Sdrh   int objc,
1567a76e8589Sdrh   Tcl_Obj *CONST objv[]
1568a76e8589Sdrh ){
1569a76e8589Sdrh   const char *zFilename;          /* File pattern to configure */
1570a76e8589Sdrh   const char *zMode;              /* Mode string */
1571a76e8589Sdrh   quota_FILE *p;                  /* Open string object */
1572a76e8589Sdrh   char zReturn[50];               /* Name of pointer to return */
1573a76e8589Sdrh 
1574a76e8589Sdrh   /* Process arguments */
1575a76e8589Sdrh   if( objc!=3 ){
1576a76e8589Sdrh     Tcl_WrongNumArgs(interp, 1, objv, "FILENAME MODE");
1577a76e8589Sdrh     return TCL_ERROR;
1578a76e8589Sdrh   }
1579a76e8589Sdrh   zFilename = Tcl_GetString(objv[1]);
1580a76e8589Sdrh   zMode = Tcl_GetString(objv[2]);
1581a76e8589Sdrh   p = sqlite3_quota_fopen(zFilename, zMode);
1582a76e8589Sdrh   sqlite3_snprintf(sizeof(zReturn), zReturn, "%p", p);
1583a76e8589Sdrh   Tcl_SetResult(interp, zReturn, TCL_VOLATILE);
1584a76e8589Sdrh   return TCL_OK;
1585a76e8589Sdrh }
1586a76e8589Sdrh 
1587a76e8589Sdrh /* Defined in test1.c */
1588a76e8589Sdrh extern void *sqlite3TestTextToPtr(const char*);
1589a76e8589Sdrh 
1590a76e8589Sdrh /*
1591a76e8589Sdrh ** tclcmd: sqlite3_quota_fread HANDLE SIZE NELEM
1592a76e8589Sdrh */
1593a76e8589Sdrh static int test_quota_fread(
1594a76e8589Sdrh   void * clientData,
1595a76e8589Sdrh   Tcl_Interp *interp,
1596a76e8589Sdrh   int objc,
1597a76e8589Sdrh   Tcl_Obj *CONST objv[]
1598a76e8589Sdrh ){
1599a76e8589Sdrh   quota_FILE *p;
1600a76e8589Sdrh   char *zBuf;
1601a76e8589Sdrh   int sz;
1602a76e8589Sdrh   int nElem;
160383cc1392Sdrh   size_t got;
1604a76e8589Sdrh 
1605a76e8589Sdrh   if( objc!=4 ){
1606a76e8589Sdrh     Tcl_WrongNumArgs(interp, 1, objv, "HANDLE SIZE NELEM");
1607a76e8589Sdrh     return TCL_ERROR;
1608a76e8589Sdrh   }
1609a76e8589Sdrh   p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
1610a76e8589Sdrh   if( Tcl_GetIntFromObj(interp, objv[2], &sz) ) return TCL_ERROR;
1611a76e8589Sdrh   if( Tcl_GetIntFromObj(interp, objv[3], &nElem) ) return TCL_ERROR;
1612663cebfeSdrh   zBuf = (char*)sqlite3_malloc( sz*nElem + 1 );
1613a76e8589Sdrh   if( zBuf==0 ){
1614a76e8589Sdrh     Tcl_SetResult(interp, "out of memory", TCL_STATIC);
1615a76e8589Sdrh     return TCL_ERROR;
1616a76e8589Sdrh   }
1617a76e8589Sdrh   got = sqlite3_quota_fread(zBuf, sz, nElem, p);
1618a76e8589Sdrh   zBuf[got*sz] = 0;
1619a76e8589Sdrh   Tcl_SetResult(interp, zBuf, TCL_VOLATILE);
1620a76e8589Sdrh   sqlite3_free(zBuf);
1621a76e8589Sdrh   return TCL_OK;
1622a76e8589Sdrh }
1623a76e8589Sdrh 
1624a76e8589Sdrh /*
1625a76e8589Sdrh ** tclcmd: sqlite3_quota_fwrite HANDLE SIZE NELEM CONTENT
1626a76e8589Sdrh */
1627a76e8589Sdrh static int test_quota_fwrite(
1628a76e8589Sdrh   void * clientData,
1629a76e8589Sdrh   Tcl_Interp *interp,
1630a76e8589Sdrh   int objc,
1631a76e8589Sdrh   Tcl_Obj *CONST objv[]
1632a76e8589Sdrh ){
1633a76e8589Sdrh   quota_FILE *p;
1634a76e8589Sdrh   char *zBuf;
1635a76e8589Sdrh   int sz;
1636a76e8589Sdrh   int nElem;
163783cc1392Sdrh   size_t got;
1638a76e8589Sdrh 
1639a76e8589Sdrh   if( objc!=5 ){
1640a76e8589Sdrh     Tcl_WrongNumArgs(interp, 1, objv, "HANDLE SIZE NELEM CONTENT");
1641a76e8589Sdrh     return TCL_ERROR;
1642a76e8589Sdrh   }
1643a76e8589Sdrh   p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
1644a76e8589Sdrh   if( Tcl_GetIntFromObj(interp, objv[2], &sz) ) return TCL_ERROR;
1645a76e8589Sdrh   if( Tcl_GetIntFromObj(interp, objv[3], &nElem) ) return TCL_ERROR;
1646a76e8589Sdrh   zBuf = Tcl_GetString(objv[4]);
1647a76e8589Sdrh   got = sqlite3_quota_fwrite(zBuf, sz, nElem, p);
164883cc1392Sdrh   Tcl_SetObjResult(interp, Tcl_NewWideIntObj(got));
1649a76e8589Sdrh   return TCL_OK;
1650a76e8589Sdrh }
1651a76e8589Sdrh 
1652a76e8589Sdrh /*
1653a76e8589Sdrh ** tclcmd: sqlite3_quota_fclose HANDLE
1654a76e8589Sdrh */
1655a76e8589Sdrh static int test_quota_fclose(
1656a76e8589Sdrh   void * clientData,
1657a76e8589Sdrh   Tcl_Interp *interp,
1658a76e8589Sdrh   int objc,
1659a76e8589Sdrh   Tcl_Obj *CONST objv[]
1660a76e8589Sdrh ){
1661a76e8589Sdrh   quota_FILE *p;
1662a76e8589Sdrh   int rc;
1663a76e8589Sdrh 
1664a76e8589Sdrh   if( objc!=2 ){
1665a76e8589Sdrh     Tcl_WrongNumArgs(interp, 1, objv, "HANDLE");
1666a76e8589Sdrh     return TCL_ERROR;
1667a76e8589Sdrh   }
1668a76e8589Sdrh   p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
1669a76e8589Sdrh   rc = sqlite3_quota_fclose(p);
1670a76e8589Sdrh   Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
1671a76e8589Sdrh   return TCL_OK;
1672a76e8589Sdrh }
1673a76e8589Sdrh 
1674a76e8589Sdrh /*
167527cec37dSdrh ** tclcmd: sqlite3_quota_fflush HANDLE ?HARDSYNC?
167669b2232dSdrh */
167769b2232dSdrh static int test_quota_fflush(
167869b2232dSdrh   void * clientData,
167969b2232dSdrh   Tcl_Interp *interp,
168069b2232dSdrh   int objc,
168169b2232dSdrh   Tcl_Obj *CONST objv[]
168269b2232dSdrh ){
168369b2232dSdrh   quota_FILE *p;
168469b2232dSdrh   int rc;
168527cec37dSdrh   int doSync = 0;
168669b2232dSdrh 
168727cec37dSdrh   if( objc!=2 && objc!=3 ){
168827cec37dSdrh     Tcl_WrongNumArgs(interp, 1, objv, "HANDLE ?HARDSYNC?");
168969b2232dSdrh     return TCL_ERROR;
169069b2232dSdrh   }
169169b2232dSdrh   p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
169227cec37dSdrh   if( objc==3 ){
169327cec37dSdrh     if( Tcl_GetBooleanFromObj(interp, objv[2], &doSync) ) return TCL_ERROR;
169427cec37dSdrh   }
169527cec37dSdrh   rc = sqlite3_quota_fflush(p, doSync);
169669b2232dSdrh   Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
169769b2232dSdrh   return TCL_OK;
169869b2232dSdrh }
169969b2232dSdrh 
170069b2232dSdrh /*
1701a76e8589Sdrh ** tclcmd: sqlite3_quota_fseek HANDLE OFFSET WHENCE
1702a76e8589Sdrh */
1703a76e8589Sdrh static int test_quota_fseek(
1704a76e8589Sdrh   void * clientData,
1705a76e8589Sdrh   Tcl_Interp *interp,
1706a76e8589Sdrh   int objc,
1707a76e8589Sdrh   Tcl_Obj *CONST objv[]
1708a76e8589Sdrh ){
1709a76e8589Sdrh   quota_FILE *p;
1710a76e8589Sdrh   int ofst;
1711a76e8589Sdrh   const char *zWhence;
1712a76e8589Sdrh   int whence;
1713a76e8589Sdrh   int rc;
1714a76e8589Sdrh 
1715a76e8589Sdrh   if( objc!=4 ){
1716a76e8589Sdrh     Tcl_WrongNumArgs(interp, 1, objv, "HANDLE OFFSET WHENCE");
1717a76e8589Sdrh     return TCL_ERROR;
1718a76e8589Sdrh   }
1719a76e8589Sdrh   p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
1720a76e8589Sdrh   if( Tcl_GetIntFromObj(interp, objv[2], &ofst) ) return TCL_ERROR;
1721a76e8589Sdrh   zWhence = Tcl_GetString(objv[3]);
1722a76e8589Sdrh   if( strcmp(zWhence, "SEEK_SET")==0 ){
1723a76e8589Sdrh     whence = SEEK_SET;
1724a76e8589Sdrh   }else if( strcmp(zWhence, "SEEK_CUR")==0 ){
1725a76e8589Sdrh     whence = SEEK_CUR;
1726a76e8589Sdrh   }else if( strcmp(zWhence, "SEEK_END")==0 ){
1727a76e8589Sdrh     whence = SEEK_END;
1728a76e8589Sdrh   }else{
1729a76e8589Sdrh     Tcl_AppendResult(interp,
1730a76e8589Sdrh            "WHENCE should be SEEK_SET, SEEK_CUR, or SEEK_END", (char*)0);
1731a76e8589Sdrh     return TCL_ERROR;
1732a76e8589Sdrh   }
1733a76e8589Sdrh   rc = sqlite3_quota_fseek(p, ofst, whence);
1734a76e8589Sdrh   Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
1735a76e8589Sdrh   return TCL_OK;
1736a76e8589Sdrh }
1737a76e8589Sdrh 
1738a76e8589Sdrh /*
1739a76e8589Sdrh ** tclcmd: sqlite3_quota_rewind HANDLE
1740a76e8589Sdrh */
1741a76e8589Sdrh static int test_quota_rewind(
1742a76e8589Sdrh   void * clientData,
1743a76e8589Sdrh   Tcl_Interp *interp,
1744a76e8589Sdrh   int objc,
1745a76e8589Sdrh   Tcl_Obj *CONST objv[]
1746a76e8589Sdrh ){
1747a76e8589Sdrh   quota_FILE *p;
1748a76e8589Sdrh   if( objc!=2 ){
1749a76e8589Sdrh     Tcl_WrongNumArgs(interp, 1, objv, "HANDLE");
1750a76e8589Sdrh     return TCL_ERROR;
1751a76e8589Sdrh   }
1752a76e8589Sdrh   p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
1753a76e8589Sdrh   sqlite3_quota_rewind(p);
1754a76e8589Sdrh   return TCL_OK;
1755a76e8589Sdrh }
1756a76e8589Sdrh 
1757a76e8589Sdrh /*
1758a76e8589Sdrh ** tclcmd: sqlite3_quota_ftell HANDLE
1759a76e8589Sdrh */
1760a76e8589Sdrh static int test_quota_ftell(
1761a76e8589Sdrh   void * clientData,
1762a76e8589Sdrh   Tcl_Interp *interp,
1763a76e8589Sdrh   int objc,
1764a76e8589Sdrh   Tcl_Obj *CONST objv[]
1765a76e8589Sdrh ){
1766a76e8589Sdrh   quota_FILE *p;
1767a76e8589Sdrh   sqlite3_int64 x;
1768a76e8589Sdrh   if( objc!=2 ){
1769a76e8589Sdrh     Tcl_WrongNumArgs(interp, 1, objv, "HANDLE");
1770a76e8589Sdrh     return TCL_ERROR;
1771a76e8589Sdrh   }
1772a76e8589Sdrh   p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
1773a76e8589Sdrh   x = sqlite3_quota_ftell(p);
1774a76e8589Sdrh   Tcl_SetObjResult(interp, Tcl_NewWideIntObj(x));
1775a76e8589Sdrh   return TCL_OK;
1776a76e8589Sdrh }
1777a76e8589Sdrh 
1778a76e8589Sdrh /*
1779c00ce490Sdrh ** tclcmd: sqlite3_quota_ftruncate HANDLE SIZE
1780c00ce490Sdrh */
1781c00ce490Sdrh static int test_quota_ftruncate(
1782c00ce490Sdrh   void * clientData,
1783c00ce490Sdrh   Tcl_Interp *interp,
1784c00ce490Sdrh   int objc,
1785c00ce490Sdrh   Tcl_Obj *CONST objv[]
1786c00ce490Sdrh ){
1787c00ce490Sdrh   quota_FILE *p;
1788c00ce490Sdrh   sqlite3_int64 x;
1789c00ce490Sdrh   Tcl_WideInt w;
1790c00ce490Sdrh   int rc;
1791c00ce490Sdrh   if( objc!=3 ){
1792c00ce490Sdrh     Tcl_WrongNumArgs(interp, 1, objv, "HANDLE SIZE");
1793c00ce490Sdrh     return TCL_ERROR;
1794c00ce490Sdrh   }
1795c00ce490Sdrh   p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
1796c00ce490Sdrh   if( Tcl_GetWideIntFromObj(interp, objv[2], &w) ) return TCL_ERROR;
1797c00ce490Sdrh   x = (sqlite3_int64)w;
1798c00ce490Sdrh   rc = sqlite3_quota_ftruncate(p, x);
1799c00ce490Sdrh   Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
1800c00ce490Sdrh   return TCL_OK;
1801c00ce490Sdrh }
1802c00ce490Sdrh 
1803c00ce490Sdrh /*
1804c00ce490Sdrh ** tclcmd: sqlite3_quota_file_size HANDLE
1805c00ce490Sdrh */
1806c00ce490Sdrh static int test_quota_file_size(
1807c00ce490Sdrh   void * clientData,
1808c00ce490Sdrh   Tcl_Interp *interp,
1809c00ce490Sdrh   int objc,
1810c00ce490Sdrh   Tcl_Obj *CONST objv[]
1811c00ce490Sdrh ){
1812c00ce490Sdrh   quota_FILE *p;
1813c00ce490Sdrh   sqlite3_int64 x;
1814c00ce490Sdrh   if( objc!=2 ){
1815c00ce490Sdrh     Tcl_WrongNumArgs(interp, 1, objv, "HANDLE");
1816c00ce490Sdrh     return TCL_ERROR;
1817c00ce490Sdrh   }
1818c00ce490Sdrh   p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
1819c00ce490Sdrh   x = sqlite3_quota_file_size(p);
1820c00ce490Sdrh   Tcl_SetObjResult(interp, Tcl_NewWideIntObj(x));
1821c00ce490Sdrh   return TCL_OK;
1822c00ce490Sdrh }
1823c00ce490Sdrh 
1824c00ce490Sdrh /*
1825c00ce490Sdrh ** tclcmd: sqlite3_quota_file_truesize HANDLE
1826c00ce490Sdrh */
1827c00ce490Sdrh static int test_quota_file_truesize(
1828c00ce490Sdrh   void * clientData,
1829c00ce490Sdrh   Tcl_Interp *interp,
1830c00ce490Sdrh   int objc,
1831c00ce490Sdrh   Tcl_Obj *CONST objv[]
1832c00ce490Sdrh ){
1833c00ce490Sdrh   quota_FILE *p;
1834c00ce490Sdrh   sqlite3_int64 x;
1835c00ce490Sdrh   if( objc!=2 ){
1836c00ce490Sdrh     Tcl_WrongNumArgs(interp, 1, objv, "HANDLE");
1837c00ce490Sdrh     return TCL_ERROR;
1838c00ce490Sdrh   }
1839c00ce490Sdrh   p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
1840c00ce490Sdrh   x = sqlite3_quota_file_truesize(p);
1841c00ce490Sdrh   Tcl_SetObjResult(interp, Tcl_NewWideIntObj(x));
1842c00ce490Sdrh   return TCL_OK;
1843c00ce490Sdrh }
1844c00ce490Sdrh 
1845c00ce490Sdrh /*
1846c00ce490Sdrh ** tclcmd: sqlite3_quota_file_mtime HANDLE
1847c00ce490Sdrh */
1848c00ce490Sdrh static int test_quota_file_mtime(
1849c00ce490Sdrh   void * clientData,
1850c00ce490Sdrh   Tcl_Interp *interp,
1851c00ce490Sdrh   int objc,
1852c00ce490Sdrh   Tcl_Obj *CONST objv[]
1853c00ce490Sdrh ){
1854c00ce490Sdrh   quota_FILE *p;
1855c00ce490Sdrh   time_t t;
1856c00ce490Sdrh   if( objc!=2 ){
1857c00ce490Sdrh     Tcl_WrongNumArgs(interp, 1, objv, "HANDLE");
1858c00ce490Sdrh     return TCL_ERROR;
1859c00ce490Sdrh   }
1860c00ce490Sdrh   p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
1861c00ce490Sdrh   t = 0;
1862c00ce490Sdrh   sqlite3_quota_file_mtime(p, &t);
1863c00ce490Sdrh   Tcl_SetObjResult(interp, Tcl_NewWideIntObj(t));
1864c00ce490Sdrh   return TCL_OK;
1865c00ce490Sdrh }
1866c00ce490Sdrh 
1867c00ce490Sdrh 
1868c00ce490Sdrh /*
1869a76e8589Sdrh ** tclcmd: sqlite3_quota_remove FILENAME
1870a76e8589Sdrh */
1871a76e8589Sdrh static int test_quota_remove(
1872a76e8589Sdrh   void * clientData,
1873a76e8589Sdrh   Tcl_Interp *interp,
1874a76e8589Sdrh   int objc,
1875a76e8589Sdrh   Tcl_Obj *CONST objv[]
1876a76e8589Sdrh ){
1877a76e8589Sdrh   const char *zFilename;          /* File pattern to configure */
1878a76e8589Sdrh   int rc;
1879a76e8589Sdrh   if( objc!=2 ){
1880a76e8589Sdrh     Tcl_WrongNumArgs(interp, 1, objv, "FILENAME");
1881a76e8589Sdrh     return TCL_ERROR;
1882a76e8589Sdrh   }
1883a76e8589Sdrh   zFilename = Tcl_GetString(objv[1]);
1884a76e8589Sdrh   rc = sqlite3_quota_remove(zFilename);
1885a76e8589Sdrh   Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
1886a76e8589Sdrh   return TCL_OK;
1887a76e8589Sdrh }
1888a76e8589Sdrh 
1889a76e8589Sdrh /*
1890c8ccda60Sdrh ** tclcmd: sqlite3_quota_glob PATTERN TEXT
1891c8ccda60Sdrh **
1892c8ccda60Sdrh ** Test the glob pattern matching.  Return 1 if TEXT matches PATTERN
1893c8ccda60Sdrh ** and return 0 if it does not.
1894c8ccda60Sdrh */
1895c8ccda60Sdrh static int test_quota_glob(
1896c8ccda60Sdrh   void * clientData,
1897c8ccda60Sdrh   Tcl_Interp *interp,
1898c8ccda60Sdrh   int objc,
1899c8ccda60Sdrh   Tcl_Obj *CONST objv[]
1900c8ccda60Sdrh ){
1901c8ccda60Sdrh   const char *zPattern;          /* The glob pattern */
1902c8ccda60Sdrh   const char *zText;             /* Text to compare agains the pattern */
1903c8ccda60Sdrh   int rc;
1904c8ccda60Sdrh   if( objc!=3 ){
1905c8ccda60Sdrh     Tcl_WrongNumArgs(interp, 1, objv, "PATTERN TEXT");
1906c8ccda60Sdrh     return TCL_ERROR;
1907c8ccda60Sdrh   }
1908c8ccda60Sdrh   zPattern = Tcl_GetString(objv[1]);
1909c8ccda60Sdrh   zText = Tcl_GetString(objv[2]);
1910c8ccda60Sdrh   rc = quotaStrglob(zPattern, zText);
1911c8ccda60Sdrh   Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
1912c8ccda60Sdrh   return TCL_OK;
1913c8ccda60Sdrh }
1914c8ccda60Sdrh 
1915c8ccda60Sdrh /*
191698c78ea6Sdrh ** tclcmd: sqlite3_quota_file_available HANDLE
191798c78ea6Sdrh **
191898c78ea6Sdrh ** Return the number of bytes from the current file point to the end of
191998c78ea6Sdrh ** the file.
192098c78ea6Sdrh */
192198c78ea6Sdrh static int test_quota_file_available(
192298c78ea6Sdrh   void * clientData,
192398c78ea6Sdrh   Tcl_Interp *interp,
192498c78ea6Sdrh   int objc,
192598c78ea6Sdrh   Tcl_Obj *CONST objv[]
192698c78ea6Sdrh ){
192798c78ea6Sdrh   quota_FILE *p;
192898c78ea6Sdrh   sqlite3_int64 x;
192998c78ea6Sdrh   if( objc!=2 ){
193098c78ea6Sdrh     Tcl_WrongNumArgs(interp, 1, objv, "HANDLE");
193198c78ea6Sdrh     return TCL_ERROR;
193298c78ea6Sdrh   }
193398c78ea6Sdrh   p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
193498c78ea6Sdrh   x = sqlite3_quota_file_available(p);
193598c78ea6Sdrh   Tcl_SetObjResult(interp, Tcl_NewWideIntObj(x));
193698c78ea6Sdrh   return TCL_OK;
193798c78ea6Sdrh }
193898c78ea6Sdrh 
193998c78ea6Sdrh /*
194098c78ea6Sdrh ** tclcmd: sqlite3_quota_ferror HANDLE
194198c78ea6Sdrh **
194298c78ea6Sdrh ** Return true if the file handle is in the error state.
194398c78ea6Sdrh */
194498c78ea6Sdrh static int test_quota_ferror(
194598c78ea6Sdrh   void * clientData,
194698c78ea6Sdrh   Tcl_Interp *interp,
194798c78ea6Sdrh   int objc,
194898c78ea6Sdrh   Tcl_Obj *CONST objv[]
194998c78ea6Sdrh ){
195098c78ea6Sdrh   quota_FILE *p;
195198c78ea6Sdrh   int x;
195298c78ea6Sdrh   if( objc!=2 ){
195398c78ea6Sdrh     Tcl_WrongNumArgs(interp, 1, objv, "HANDLE");
195498c78ea6Sdrh     return TCL_ERROR;
195598c78ea6Sdrh   }
195698c78ea6Sdrh   p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
195798c78ea6Sdrh   x = sqlite3_quota_ferror(p);
195898c78ea6Sdrh   Tcl_SetObjResult(interp, Tcl_NewIntObj(x));
195998c78ea6Sdrh   return TCL_OK;
196098c78ea6Sdrh }
196198c78ea6Sdrh 
196298c78ea6Sdrh /*
1963748a52cdSdrh ** This routine registers the custom TCL commands defined in this
1964748a52cdSdrh ** module.  This should be the only procedure visible from outside
1965748a52cdSdrh ** of this module.
1966748a52cdSdrh */
1967748a52cdSdrh int Sqlitequota_Init(Tcl_Interp *interp){
19688cf35eb4Sdan   static struct {
19698cf35eb4Sdan      char *zName;
19708cf35eb4Sdan      Tcl_ObjCmdProc *xProc;
19718cf35eb4Sdan   } aCmd[] = {
19728cf35eb4Sdan     { "sqlite3_quota_initialize",    test_quota_initialize },
19738cf35eb4Sdan     { "sqlite3_quota_shutdown",      test_quota_shutdown },
19748cf35eb4Sdan     { "sqlite3_quota_set",           test_quota_set },
1975b6020c4eSdrh     { "sqlite3_quota_file",          test_quota_file },
1976ef9659baSdrh     { "sqlite3_quota_dump",          test_quota_dump },
1977a76e8589Sdrh     { "sqlite3_quota_fopen",         test_quota_fopen },
1978a76e8589Sdrh     { "sqlite3_quota_fread",         test_quota_fread },
1979a76e8589Sdrh     { "sqlite3_quota_fwrite",        test_quota_fwrite },
1980a76e8589Sdrh     { "sqlite3_quota_fclose",        test_quota_fclose },
198169b2232dSdrh     { "sqlite3_quota_fflush",        test_quota_fflush },
1982a76e8589Sdrh     { "sqlite3_quota_fseek",         test_quota_fseek },
1983a76e8589Sdrh     { "sqlite3_quota_rewind",        test_quota_rewind },
1984a76e8589Sdrh     { "sqlite3_quota_ftell",         test_quota_ftell },
1985c00ce490Sdrh     { "sqlite3_quota_ftruncate",     test_quota_ftruncate },
1986c00ce490Sdrh     { "sqlite3_quota_file_size",     test_quota_file_size },
1987c00ce490Sdrh     { "sqlite3_quota_file_truesize", test_quota_file_truesize },
1988c00ce490Sdrh     { "sqlite3_quota_file_mtime",    test_quota_file_mtime },
1989a76e8589Sdrh     { "sqlite3_quota_remove",        test_quota_remove },
1990c8ccda60Sdrh     { "sqlite3_quota_glob",          test_quota_glob },
199198c78ea6Sdrh     { "sqlite3_quota_file_available",test_quota_file_available },
199298c78ea6Sdrh     { "sqlite3_quota_ferror",        test_quota_ferror },
19938cf35eb4Sdan   };
19948cf35eb4Sdan   int i;
19958cf35eb4Sdan 
19968cf35eb4Sdan   for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
19978cf35eb4Sdan     Tcl_CreateObjCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0);
19988cf35eb4Sdan   }
19998cf35eb4Sdan 
2000748a52cdSdrh   return TCL_OK;
2001748a52cdSdrh }
2002748a52cdSdrh #endif
2003