blob: 352f289d3e6624c1fa69e9f556d7db4146db787e [file] [log] [blame]
drh748a52c2010-09-01 11:50:08 +00001/*
2** 2010 September 31
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11*************************************************************************
12**
13** This file contains a VFS "shim" - a layer that sits in between the
14** pager and the real VFS.
15**
16** This particular shim enforces a quota system on files. One or more
17** database files are in a "quota group" that is defined by a GLOB
18** pattern. A quota is set for the combined size of all files in the
19** the group. A quota of zero means "no limit". If the total size
drh9237a252010-09-01 15:26:18 +000020** of all files in the quota group is greater than the limit, then
21** write requests that attempt to enlarge a file fail with SQLITE_FULL.
drh748a52c2010-09-01 11:50:08 +000022**
23** However, before returning SQLITE_FULL, the write requests invoke
24** a callback function that is configurable for each quota group.
25** This callback has the opportunity to enlarge the quota. If the
26** callback does enlarge the quota such that the total size of all
27** files within the group is less than the new quota, then the write
28** continues as if nothing had happened.
29*/
30#include "sqlite3.h"
dan8cf35eb2010-09-01 11:40:05 +000031#include <string.h>
32#include <assert.h>
drh748a52c2010-09-01 11:50:08 +000033
34/************************ Object Definitions ******************************/
35
drh745de2e2010-09-01 12:50:54 +000036/* Forward declaration of all object types */
drh748a52c2010-09-01 11:50:08 +000037typedef struct quotaGroup quotaGroup;
drhef9659b2010-09-01 14:35:48 +000038typedef struct quotaConn quotaConn;
drh745de2e2010-09-01 12:50:54 +000039typedef struct quotaFile quotaFile;
40
dan8cf35eb2010-09-01 11:40:05 +000041/*
drh549dc1b2010-09-01 13:09:57 +000042** A "quota group" is a collection of files whose collective size we want
43** to limit. Each quota group is defined by a GLOB pattern.
dan8cf35eb2010-09-01 11:40:05 +000044**
drh549dc1b2010-09-01 13:09:57 +000045** There is an instance of the following object for each defined quota
46** group. This object records the GLOB pattern that defines which files
47** belong to the quota group. The object also remembers the size limit
48** for the group (the quota) and the callback to be invoked when the
49** sum of the sizes of the files within the group goes over the limit.
dan8cf35eb2010-09-01 11:40:05 +000050**
drh549dc1b2010-09-01 13:09:57 +000051** A quota group must be established (using sqlite3_quota_set(...))
52** prior to opening any of the database connections that access files
53** within the quota group.
dan8cf35eb2010-09-01 11:40:05 +000054*/
drh748a52c2010-09-01 11:50:08 +000055struct quotaGroup {
56 const char *zPattern; /* Filename pattern to be quotaed */
57 sqlite3_int64 iLimit; /* Upper bound on total file size */
58 sqlite3_int64 iSize; /* Current size of all files */
59 void (*xCallback)( /* Callback invoked when going over quota */
60 const char *zFilename, /* Name of file whose size increases */
61 sqlite3_int64 *piLimit, /* IN/OUT: The current limit */
62 sqlite3_int64 iSize, /* Total size of all files in the group */
63 void *pArg /* Client data */
64 );
65 void *pArg; /* Third argument to the xCallback() */
drhef9659b2010-09-01 14:35:48 +000066 void (*xDestroy)(void*); /* Optional destructor for pArg */
drh748a52c2010-09-01 11:50:08 +000067 quotaGroup *pNext, **ppPrev; /* Doubly linked list of all quota objects */
drh75c19172010-09-01 14:58:10 +000068 quotaFile *pFiles; /* Files within this group */
drh748a52c2010-09-01 11:50:08 +000069};
70
71/*
drh745de2e2010-09-01 12:50:54 +000072** An instance of this structure represents a single file that is part
73** of a quota group. A single file can be opened multiple times. In
74** order keep multiple openings of the same file from causing the size
drh549dc1b2010-09-01 13:09:57 +000075** of the file to count against the quota multiple times, each file
drh745de2e2010-09-01 12:50:54 +000076** has a unique instance of this object and multiple open connections
77** to the same file each point to a single instance of this object.
drh748a52c2010-09-01 11:50:08 +000078*/
drh748a52c2010-09-01 11:50:08 +000079struct quotaFile {
drh745de2e2010-09-01 12:50:54 +000080 char *zFilename; /* Name of this file */
drh75c19172010-09-01 14:58:10 +000081 quotaGroup *pGroup; /* Quota group to which this file belongs */
dan8cf35eb2010-09-01 11:40:05 +000082 sqlite3_int64 iSize; /* Current size of this file */
drh745de2e2010-09-01 12:50:54 +000083 int nRef; /* Number of times this file is open */
84 quotaFile *pNext, **ppPrev; /* Linked list of files in the same group */
85};
86
87/*
88** An instance of the following object represents each open connection
drh549dc1b2010-09-01 13:09:57 +000089** to a file that participates in quota tracking. This object is a
90** subclass of sqlite3_file. The sqlite3_file object for the underlying
91** VFS is appended to this structure.
drh745de2e2010-09-01 12:50:54 +000092*/
drhef9659b2010-09-01 14:35:48 +000093struct quotaConn {
drh745de2e2010-09-01 12:50:54 +000094 sqlite3_file base; /* Base class - must be first */
95 quotaFile *pFile; /* The underlying file */
drh748a52c2010-09-01 11:50:08 +000096 /* The underlying VFS sqlite3_file is appended to this object */
97};
98
99/************************* Global Variables **********************************/
100/*
101** All global variables used by this file are containing within the following
102** gQuota structure.
103*/
104static struct {
drh549dc1b2010-09-01 13:09:57 +0000105 /* The pOrigVfs is the real, original underlying VFS implementation.
drh748a52c2010-09-01 11:50:08 +0000106 ** Most operations pass-through to the real VFS. This value is read-only
107 ** during operation. It is only modified at start-time and thus does not
108 ** require a mutex.
109 */
110 sqlite3_vfs *pOrigVfs;
111
112 /* The sThisVfs is the VFS structure used by this shim. It is initialized
113 ** at start-time and thus does not require a mutex
114 */
115 sqlite3_vfs sThisVfs;
116
117 /* The sIoMethods defines the methods used by sqlite3_file objects
118 ** associated with this shim. It is initialized at start-time and does
119 ** not require a mutex.
120 **
121 ** When the underlying VFS is called to open a file, it might return
122 ** either a version 1 or a version 2 sqlite3_file object. This shim
123 ** has to create a wrapper sqlite3_file of the same version. Hence
124 ** there are two I/O method structures, one for version 1 and the other
125 ** for version 2.
126 */
127 sqlite3_io_methods sIoMethodsV1;
128 sqlite3_io_methods sIoMethodsV2;
129
130 /* True when this shim as been initialized.
131 */
132 int isInitialized;
133
134 /* For run-time access any of the other global data structures in this
135 ** shim, the following mutex must be held.
136 */
137 sqlite3_mutex *pMutex;
138
139 /* List of quotaGroup objects.
140 */
141 quotaGroup *pGroup;
142
143} gQuota;
144
145/************************* Utility Routines *********************************/
146/*
147** Acquire and release the mutex used to serialize access to the
148** list of quotaGroups.
149*/
150static void quotaEnter(void){ sqlite3_mutex_enter(gQuota.pMutex); }
151static void quotaLeave(void){ sqlite3_mutex_leave(gQuota.pMutex); }
152
153
154/* If the reference count and threshold for a quotaGroup are both
155** zero, then destroy the quotaGroup.
156*/
drh75c19172010-09-01 14:58:10 +0000157static void quotaGroupDeref(quotaGroup *pGroup){
158 if( pGroup->pFiles==0 && pGroup->iLimit==0 ){
dan9fc77022010-09-01 16:19:57 +0000159 *pGroup->ppPrev = pGroup->pNext;
drh75c19172010-09-01 14:58:10 +0000160 if( pGroup->pNext ) pGroup->pNext->ppPrev = pGroup->ppPrev;
drh75c19172010-09-01 14:58:10 +0000161 if( pGroup->xDestroy ) pGroup->xDestroy(pGroup->pArg);
162 sqlite3_free(pGroup);
drh748a52c2010-09-01 11:50:08 +0000163 }
164}
165
166/*
167** Return TRUE if string z matches glob pattern zGlob.
168**
169** Globbing rules:
170**
171** '*' Matches any sequence of zero or more characters.
172**
173** '?' Matches exactly one character.
174**
175** [...] Matches one character from the enclosed list of
176** characters.
177**
178** [^...] Matches one character not in the enclosed list.
179**
180*/
drh61cc4232010-09-01 14:45:16 +0000181static int quotaStrglob(const char *zGlob, const char *z){
drh748a52c2010-09-01 11:50:08 +0000182 int c, c2;
183 int invert;
184 int seen;
185
186 while( (c = (*(zGlob++)))!=0 ){
187 if( c=='*' ){
188 while( (c=(*(zGlob++))) == '*' || c=='?' ){
189 if( c=='?' && (*(z++))==0 ) return 0;
190 }
191 if( c==0 ){
192 return 1;
193 }else if( c=='[' ){
drh61cc4232010-09-01 14:45:16 +0000194 while( *z && quotaStrglob(zGlob-1,z)==0 ){
drh748a52c2010-09-01 11:50:08 +0000195 z++;
196 }
197 return (*z)!=0;
198 }
199 while( (c2 = (*(z++)))!=0 ){
200 while( c2!=c ){
201 c2 = *(z++);
202 if( c2==0 ) return 0;
203 }
drh61cc4232010-09-01 14:45:16 +0000204 if( quotaStrglob(zGlob,z) ) return 1;
drh748a52c2010-09-01 11:50:08 +0000205 }
206 return 0;
207 }else if( c=='?' ){
208 if( (*(z++))==0 ) return 0;
209 }else if( c=='[' ){
210 int prior_c = 0;
211 seen = 0;
212 invert = 0;
213 c = *(z++);
214 if( c==0 ) return 0;
215 c2 = *(zGlob++);
216 if( c2=='^' ){
217 invert = 1;
218 c2 = *(zGlob++);
219 }
220 if( c2==']' ){
221 if( c==']' ) seen = 1;
222 c2 = *(zGlob++);
223 }
224 while( c2 && c2!=']' ){
225 if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
226 c2 = *(zGlob++);
227 if( c>=prior_c && c<=c2 ) seen = 1;
228 prior_c = 0;
229 }else{
230 if( c==c2 ){
231 seen = 1;
232 }
233 prior_c = c2;
234 }
235 c2 = *(zGlob++);
236 }
237 if( c2==0 || (seen ^ invert)==0 ) return 0;
238 }else{
239 if( c!=(*(z++)) ) return 0;
240 }
241 }
242 return *z==0;
243}
244
245
246/* Find a quotaGroup given the filename.
dan8cf35eb2010-09-01 11:40:05 +0000247**
drhef9659b2010-09-01 14:35:48 +0000248** Return a pointer to the quotaGroup object. Return NULL if not found.
drh748a52c2010-09-01 11:50:08 +0000249*/
250static quotaGroup *quotaGroupFind(const char *zFilename){
251 quotaGroup *p;
drh61cc4232010-09-01 14:45:16 +0000252 for(p=gQuota.pGroup; p && quotaStrglob(p->zPattern, zFilename)==0;
253 p=p->pNext){}
drh748a52c2010-09-01 11:50:08 +0000254 return p;
255}
256
drhef9659b2010-09-01 14:35:48 +0000257/* Translate an sqlite3_file* that is really a quotaConn* into
drh549dc1b2010-09-01 13:09:57 +0000258** the sqlite3_file* for the underlying original VFS.
drh748a52c2010-09-01 11:50:08 +0000259*/
drhef9659b2010-09-01 14:35:48 +0000260static sqlite3_file *quotaSubOpen(sqlite3_file *pConn){
261 quotaConn *p = (quotaConn*)pConn;
drh748a52c2010-09-01 11:50:08 +0000262 return (sqlite3_file*)&p[1];
263}
264
265/************************* VFS Method Wrappers *****************************/
266/*
267** This is the xOpen method used for the "quota" VFS.
268**
269** Most of the work is done by the underlying original VFS. This method
drh549dc1b2010-09-01 13:09:57 +0000270** simply links the new file into the appropriate quota group if it is a
271** file that needs to be tracked.
drh748a52c2010-09-01 11:50:08 +0000272*/
273static int quotaOpen(
drh549dc1b2010-09-01 13:09:57 +0000274 sqlite3_vfs *pVfs, /* The quota VFS */
275 const char *zName, /* Name of file to be opened */
drhef9659b2010-09-01 14:35:48 +0000276 sqlite3_file *pConn, /* Fill in this file descriptor */
drh549dc1b2010-09-01 13:09:57 +0000277 int flags, /* Flags to control the opening */
278 int *pOutFlags /* Flags showing results of opening */
drh748a52c2010-09-01 11:50:08 +0000279){
drh549dc1b2010-09-01 13:09:57 +0000280 int rc; /* Result code */
drhef9659b2010-09-01 14:35:48 +0000281 quotaConn *pQuotaOpen; /* The new quota file descriptor */
drh549dc1b2010-09-01 13:09:57 +0000282 quotaFile *pFile; /* Corresponding quotaFile obj */
283 quotaGroup *pGroup; /* The group file belongs to */
284 sqlite3_file *pSubOpen; /* Real file descriptor */
285 sqlite3_vfs *pOrigVfs = gQuota.pOrigVfs; /* Real VFS */
drh748a52c2010-09-01 11:50:08 +0000286
287 /* If the file is not a main database file or a WAL, then use the
288 ** normal xOpen method.
289 */
dan8cf35eb2010-09-01 11:40:05 +0000290 if( (flags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_WAL))==0 ){
drhef9659b2010-09-01 14:35:48 +0000291 return pOrigVfs->xOpen(pOrigVfs, zName, pConn, flags, pOutFlags);
drh748a52c2010-09-01 11:50:08 +0000292 }
293
294 /* If the name of the file does not match any quota group, then
295 ** use the normal xOpen method.
296 */
297 quotaEnter();
dan8cf35eb2010-09-01 11:40:05 +0000298 pGroup = quotaGroupFind(zName);
drh748a52c2010-09-01 11:50:08 +0000299 if( pGroup==0 ){
drhef9659b2010-09-01 14:35:48 +0000300 rc = pOrigVfs->xOpen(pOrigVfs, zName, pConn, flags, pOutFlags);
dan8cf35eb2010-09-01 11:40:05 +0000301 }else{
302 /* If we get to this point, it means the file needs to be quota tracked.
303 */
drhef9659b2010-09-01 14:35:48 +0000304 pQuotaOpen = (quotaConn*)pConn;
305 pSubOpen = quotaSubOpen(pConn);
drh745de2e2010-09-01 12:50:54 +0000306 rc = pOrigVfs->xOpen(pOrigVfs, zName, pSubOpen, flags, pOutFlags);
dan8cf35eb2010-09-01 11:40:05 +0000307 if( rc==SQLITE_OK ){
drh75c19172010-09-01 14:58:10 +0000308 for(pFile=pGroup->pFiles; pFile && strcmp(pFile->zFilename, zName);
drh745de2e2010-09-01 12:50:54 +0000309 pFile=pFile->pNext){}
310 if( pFile==0 ){
311 int nName = strlen(zName);
312 pFile = sqlite3_malloc( sizeof(*pFile) + nName + 1 );
313 if( pFile==0 ){
314 quotaLeave();
315 pSubOpen->pMethods->xClose(pSubOpen);
316 return SQLITE_NOMEM;
317 }
318 memset(pFile, 0, sizeof(*pFile));
319 pFile->zFilename = (char*)&pFile[1];
320 memcpy(pFile->zFilename, zName, nName+1);
drh75c19172010-09-01 14:58:10 +0000321 pFile->pNext = pGroup->pFiles;
322 if( pGroup->pFiles ) pGroup->pFiles->ppPrev = &pFile->pNext;
323 pFile->ppPrev = &pGroup->pFiles;
324 pGroup->pFiles = pFile;
drh745de2e2010-09-01 12:50:54 +0000325 pFile->pGroup = pGroup;
326 }
327 pFile->nRef++;
328 pQuotaOpen->pFile = pFile;
329 if( pSubOpen->pMethods->iVersion==1 ){
330 pQuotaOpen->base.pMethods = &gQuota.sIoMethodsV1;
dan8cf35eb2010-09-01 11:40:05 +0000331 }else{
drh745de2e2010-09-01 12:50:54 +0000332 pQuotaOpen->base.pMethods = &gQuota.sIoMethodsV2;
dan8cf35eb2010-09-01 11:40:05 +0000333 }
drh748a52c2010-09-01 11:50:08 +0000334 }
335 }
dan8cf35eb2010-09-01 11:40:05 +0000336 quotaLeave();
drh748a52c2010-09-01 11:50:08 +0000337 return rc;
338}
339
340/************************ I/O Method Wrappers *******************************/
341
342/* xClose requests get passed through to the original VFS. But we
drhef9659b2010-09-01 14:35:48 +0000343** also have to unlink the quotaConn from the quotaFile and quotaGroup.
344** The quotaFile and/or quotaGroup are freed if they are no longer in use.
drh748a52c2010-09-01 11:50:08 +0000345*/
drhef9659b2010-09-01 14:35:48 +0000346static int quotaClose(sqlite3_file *pConn){
347 quotaConn *p = (quotaConn*)pConn;
drh745de2e2010-09-01 12:50:54 +0000348 quotaFile *pFile = p->pFile;
drhef9659b2010-09-01 14:35:48 +0000349 sqlite3_file *pSubOpen = quotaSubOpen(pConn);
drh748a52c2010-09-01 11:50:08 +0000350 int rc;
drh745de2e2010-09-01 12:50:54 +0000351 rc = pSubOpen->pMethods->xClose(pSubOpen);
drh748a52c2010-09-01 11:50:08 +0000352 quotaEnter();
drh745de2e2010-09-01 12:50:54 +0000353 pFile->nRef--;
354 if( pFile->nRef==0 ){
355 quotaGroup *pGroup = pFile->pGroup;
356 pGroup->iSize -= pFile->iSize;
357 if( pFile->pNext ) pFile->pNext->ppPrev = pFile->ppPrev;
358 *pFile->ppPrev = pFile->pNext;
359 quotaGroupDeref(pGroup);
360 sqlite3_free(pFile);
361 }
drh748a52c2010-09-01 11:50:08 +0000362 quotaLeave();
363 return rc;
364}
365
366/* Pass xRead requests directory thru to the original VFS without
367** further processing.
368*/
369static int quotaRead(
drhef9659b2010-09-01 14:35:48 +0000370 sqlite3_file *pConn,
drh748a52c2010-09-01 11:50:08 +0000371 void *pBuf,
372 int iAmt,
373 sqlite3_int64 iOfst
374){
drhef9659b2010-09-01 14:35:48 +0000375 sqlite3_file *pSubOpen = quotaSubOpen(pConn);
drh745de2e2010-09-01 12:50:54 +0000376 return pSubOpen->pMethods->xRead(pSubOpen, pBuf, iAmt, iOfst);
drh748a52c2010-09-01 11:50:08 +0000377}
378
379/* Check xWrite requests to see if they expand the file. If they do,
380** the perform a quota check before passing them through to the
381** original VFS.
382*/
383static int quotaWrite(
drhef9659b2010-09-01 14:35:48 +0000384 sqlite3_file *pConn,
dan8cf35eb2010-09-01 11:40:05 +0000385 const void *pBuf,
drh748a52c2010-09-01 11:50:08 +0000386 int iAmt,
387 sqlite3_int64 iOfst
388){
drhef9659b2010-09-01 14:35:48 +0000389 quotaConn *p = (quotaConn*)pConn;
390 sqlite3_file *pSubOpen = quotaSubOpen(pConn);
drh748a52c2010-09-01 11:50:08 +0000391 sqlite3_int64 iEnd = iOfst+iAmt;
392 quotaGroup *pGroup;
drh745de2e2010-09-01 12:50:54 +0000393 quotaFile *pFile = p->pFile;
drh748a52c2010-09-01 11:50:08 +0000394 sqlite3_int64 szNew;
395
drh745de2e2010-09-01 12:50:54 +0000396 if( pFile->iSize<iEnd ){
397 pGroup = pFile->pGroup;
drh748a52c2010-09-01 11:50:08 +0000398 quotaEnter();
drh745de2e2010-09-01 12:50:54 +0000399 szNew = pGroup->iSize - pFile->iSize + iEnd;
dan8cf35eb2010-09-01 11:40:05 +0000400 if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){
drh748a52c2010-09-01 11:50:08 +0000401 if( pGroup->xCallback ){
drh745de2e2010-09-01 12:50:54 +0000402 pGroup->xCallback(pFile->zFilename, &pGroup->iLimit, szNew,
drh748a52c2010-09-01 11:50:08 +0000403 pGroup->pArg);
404 }
dan8cf35eb2010-09-01 11:40:05 +0000405 if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){
drh748a52c2010-09-01 11:50:08 +0000406 quotaLeave();
407 return SQLITE_FULL;
408 }
409 }
410 pGroup->iSize = szNew;
drh745de2e2010-09-01 12:50:54 +0000411 pFile->iSize = iEnd;
drh748a52c2010-09-01 11:50:08 +0000412 quotaLeave();
413 }
drh745de2e2010-09-01 12:50:54 +0000414 return pSubOpen->pMethods->xWrite(pSubOpen, pBuf, iAmt, iOfst);
drh748a52c2010-09-01 11:50:08 +0000415}
416
417/* Pass xTruncate requests thru to the original VFS. If the
418** success, update the file size.
419*/
drhef9659b2010-09-01 14:35:48 +0000420static int quotaTruncate(sqlite3_file *pConn, sqlite3_int64 size){
421 quotaConn *p = (quotaConn*)pConn;
422 sqlite3_file *pSubOpen = quotaSubOpen(pConn);
drh745de2e2010-09-01 12:50:54 +0000423 int rc = pSubOpen->pMethods->xTruncate(pSubOpen, size);
424 quotaFile *pFile = p->pFile;
425 quotaGroup *pGroup;
drh748a52c2010-09-01 11:50:08 +0000426 if( rc==SQLITE_OK ){
427 quotaEnter();
drh745de2e2010-09-01 12:50:54 +0000428 pGroup = pFile->pGroup;
429 pGroup->iSize -= pFile->iSize;
430 pFile->iSize = size;
drh748a52c2010-09-01 11:50:08 +0000431 pGroup->iSize += size;
432 quotaLeave();
433 }
434 return rc;
435}
436
437/* Pass xSync requests through to the original VFS without change
438*/
drhef9659b2010-09-01 14:35:48 +0000439static int quotaSync(sqlite3_file *pConn, int flags){
440 sqlite3_file *pSubOpen = quotaSubOpen(pConn);
drh745de2e2010-09-01 12:50:54 +0000441 return pSubOpen->pMethods->xSync(pSubOpen, flags);
drh748a52c2010-09-01 11:50:08 +0000442}
443
444/* Pass xFileSize requests through to the original VFS but then
445** update the quotaGroup with the new size before returning.
446*/
drhef9659b2010-09-01 14:35:48 +0000447static int quotaFileSize(sqlite3_file *pConn, sqlite3_int64 *pSize){
448 quotaConn *p = (quotaConn*)pConn;
449 sqlite3_file *pSubOpen = quotaSubOpen(pConn);
drh745de2e2010-09-01 12:50:54 +0000450 quotaFile *pFile = p->pFile;
drh748a52c2010-09-01 11:50:08 +0000451 quotaGroup *pGroup;
452 sqlite3_int64 sz;
453 int rc;
454
drh745de2e2010-09-01 12:50:54 +0000455 rc = pSubOpen->pMethods->xFileSize(pSubOpen, &sz);
drh748a52c2010-09-01 11:50:08 +0000456 if( rc==SQLITE_OK ){
drh748a52c2010-09-01 11:50:08 +0000457 quotaEnter();
drh745de2e2010-09-01 12:50:54 +0000458 pGroup = pFile->pGroup;
459 pGroup->iSize -= pFile->iSize;
460 pFile->iSize = sz;
drh748a52c2010-09-01 11:50:08 +0000461 pGroup->iSize += sz;
462 quotaLeave();
463 *pSize = sz;
464 }
465 return rc;
466}
467
468/* Pass xLock requests through to the original VFS unchanged.
469*/
drhef9659b2010-09-01 14:35:48 +0000470static int quotaLock(sqlite3_file *pConn, int lock){
471 sqlite3_file *pSubOpen = quotaSubOpen(pConn);
drh745de2e2010-09-01 12:50:54 +0000472 return pSubOpen->pMethods->xLock(pSubOpen, lock);
drh748a52c2010-09-01 11:50:08 +0000473}
474
475/* Pass xUnlock requests through to the original VFS unchanged.
476*/
drhef9659b2010-09-01 14:35:48 +0000477static int quotaUnlock(sqlite3_file *pConn, int lock){
478 sqlite3_file *pSubOpen = quotaSubOpen(pConn);
drh745de2e2010-09-01 12:50:54 +0000479 return pSubOpen->pMethods->xUnlock(pSubOpen, lock);
drh748a52c2010-09-01 11:50:08 +0000480}
481
482/* Pass xCheckReservedLock requests through to the original VFS unchanged.
483*/
drhef9659b2010-09-01 14:35:48 +0000484static int quotaCheckReservedLock(sqlite3_file *pConn, int *pResOut){
485 sqlite3_file *pSubOpen = quotaSubOpen(pConn);
drh745de2e2010-09-01 12:50:54 +0000486 return pSubOpen->pMethods->xCheckReservedLock(pSubOpen, pResOut);
drh748a52c2010-09-01 11:50:08 +0000487}
488
489/* Pass xFileControl requests through to the original VFS unchanged.
490*/
drhef9659b2010-09-01 14:35:48 +0000491static int quotaFileControl(sqlite3_file *pConn, int op, void *pArg){
492 sqlite3_file *pSubOpen = quotaSubOpen(pConn);
drh745de2e2010-09-01 12:50:54 +0000493 return pSubOpen->pMethods->xFileControl(pSubOpen, op, pArg);
drh748a52c2010-09-01 11:50:08 +0000494}
495
496/* Pass xSectorSize requests through to the original VFS unchanged.
497*/
drhef9659b2010-09-01 14:35:48 +0000498static int quotaSectorSize(sqlite3_file *pConn){
499 sqlite3_file *pSubOpen = quotaSubOpen(pConn);
drh745de2e2010-09-01 12:50:54 +0000500 return pSubOpen->pMethods->xSectorSize(pSubOpen);
drh748a52c2010-09-01 11:50:08 +0000501}
502
503/* Pass xDeviceCharacteristics requests through to the original VFS unchanged.
504*/
drhef9659b2010-09-01 14:35:48 +0000505static int quotaDeviceCharacteristics(sqlite3_file *pConn){
506 sqlite3_file *pSubOpen = quotaSubOpen(pConn);
drh745de2e2010-09-01 12:50:54 +0000507 return pSubOpen->pMethods->xDeviceCharacteristics(pSubOpen);
drh748a52c2010-09-01 11:50:08 +0000508}
509
510/* Pass xShmMap requests through to the original VFS unchanged.
511*/
512static int quotaShmMap(
drhef9659b2010-09-01 14:35:48 +0000513 sqlite3_file *pConn, /* Handle open on database file */
drh748a52c2010-09-01 11:50:08 +0000514 int iRegion, /* Region to retrieve */
515 int szRegion, /* Size of regions */
516 int bExtend, /* True to extend file if necessary */
517 void volatile **pp /* OUT: Mapped memory */
518){
drhef9659b2010-09-01 14:35:48 +0000519 sqlite3_file *pSubOpen = quotaSubOpen(pConn);
drh745de2e2010-09-01 12:50:54 +0000520 return pSubOpen->pMethods->xShmMap(pSubOpen, iRegion, szRegion, bExtend, pp);
drh748a52c2010-09-01 11:50:08 +0000521}
522
523/* Pass xShmLock requests through to the original VFS unchanged.
524*/
525static int quotaShmLock(
drhef9659b2010-09-01 14:35:48 +0000526 sqlite3_file *pConn, /* Database file holding the shared memory */
drh748a52c2010-09-01 11:50:08 +0000527 int ofst, /* First lock to acquire or release */
528 int n, /* Number of locks to acquire or release */
529 int flags /* What to do with the lock */
530){
drhef9659b2010-09-01 14:35:48 +0000531 sqlite3_file *pSubOpen = quotaSubOpen(pConn);
drh745de2e2010-09-01 12:50:54 +0000532 return pSubOpen->pMethods->xShmLock(pSubOpen, ofst, n, flags);
drh748a52c2010-09-01 11:50:08 +0000533}
534
535/* Pass xShmBarrier requests through to the original VFS unchanged.
536*/
drhef9659b2010-09-01 14:35:48 +0000537static void quotaShmBarrier(sqlite3_file *pConn){
538 sqlite3_file *pSubOpen = quotaSubOpen(pConn);
drh745de2e2010-09-01 12:50:54 +0000539 pSubOpen->pMethods->xShmBarrier(pSubOpen);
drh748a52c2010-09-01 11:50:08 +0000540}
541
542/* Pass xShmUnmap requests through to the original VFS unchanged.
543*/
drhef9659b2010-09-01 14:35:48 +0000544static int quotaShmUnmap(sqlite3_file *pConn, int deleteFlag){
545 sqlite3_file *pSubOpen = quotaSubOpen(pConn);
drh745de2e2010-09-01 12:50:54 +0000546 return pSubOpen->pMethods->xShmUnmap(pSubOpen, deleteFlag);
drh748a52c2010-09-01 11:50:08 +0000547}
548
549/************************** Public Interfaces *****************************/
550/*
551** Initialize the quota VFS shim. Use the VFS named zOrigVfsName
552** as the VFS that does the actual work. Use the default if
553** zOrigVfsName==NULL.
554**
555** The quota VFS shim is named "quota". It will become the default
556** VFS if makeDefault is non-zero.
557**
558** THIS ROUTINE IS NOT THREADSAFE. Call this routine exactly once
559** during start-up.
560*/
561int sqlite3_quota_initialize(const char *zOrigVfsName, int makeDefault){
562 sqlite3_vfs *pOrigVfs;
dan8cf35eb2010-09-01 11:40:05 +0000563 if( gQuota.isInitialized ) return SQLITE_MISUSE;
drh748a52c2010-09-01 11:50:08 +0000564 pOrigVfs = sqlite3_vfs_find(zOrigVfsName);
565 if( pOrigVfs==0 ) return SQLITE_ERROR;
dan8cf35eb2010-09-01 11:40:05 +0000566 assert( pOrigVfs!=&gQuota.sThisVfs );
567 gQuota.pMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
568 if( !gQuota.pMutex ){
569 return SQLITE_NOMEM;
570 }
571 gQuota.isInitialized = 1;
drh748a52c2010-09-01 11:50:08 +0000572 gQuota.pOrigVfs = pOrigVfs;
573 gQuota.sThisVfs = *pOrigVfs;
574 gQuota.sThisVfs.xOpen = quotaOpen;
drhef9659b2010-09-01 14:35:48 +0000575 gQuota.sThisVfs.szOsFile += sizeof(quotaConn);
drh748a52c2010-09-01 11:50:08 +0000576 gQuota.sThisVfs.zName = "quota";
577 gQuota.sIoMethodsV1.iVersion = 1;
578 gQuota.sIoMethodsV1.xClose = quotaClose;
579 gQuota.sIoMethodsV1.xRead = quotaRead;
580 gQuota.sIoMethodsV1.xWrite = quotaWrite;
581 gQuota.sIoMethodsV1.xTruncate = quotaTruncate;
582 gQuota.sIoMethodsV1.xSync = quotaSync;
583 gQuota.sIoMethodsV1.xFileSize = quotaFileSize;
584 gQuota.sIoMethodsV1.xLock = quotaLock;
585 gQuota.sIoMethodsV1.xUnlock = quotaUnlock;
586 gQuota.sIoMethodsV1.xCheckReservedLock = quotaCheckReservedLock;
587 gQuota.sIoMethodsV1.xFileControl = quotaFileControl;
dan8cf35eb2010-09-01 11:40:05 +0000588 gQuota.sIoMethodsV1.xSectorSize = quotaSectorSize;
drh748a52c2010-09-01 11:50:08 +0000589 gQuota.sIoMethodsV1.xDeviceCharacteristics = quotaDeviceCharacteristics;
590 gQuota.sIoMethodsV2 = gQuota.sIoMethodsV1;
591 gQuota.sIoMethodsV2.iVersion = 2;
592 gQuota.sIoMethodsV2.xShmMap = quotaShmMap;
593 gQuota.sIoMethodsV2.xShmLock = quotaShmLock;
594 gQuota.sIoMethodsV2.xShmBarrier = quotaShmBarrier;
595 gQuota.sIoMethodsV2.xShmUnmap = quotaShmUnmap;
dan8cf35eb2010-09-01 11:40:05 +0000596 sqlite3_vfs_register(&gQuota.sThisVfs, makeDefault);
597 return SQLITE_OK;
drh748a52c2010-09-01 11:50:08 +0000598}
599
600/*
601** Shutdown the quota system.
602**
603** All SQLite database connections must be closed before calling this
604** routine.
605**
606** THIS ROUTINE IS NOT THREADSAFE. Call this routine exactly one while
607** shutting down in order to free all remaining quota groups.
608*/
609int sqlite3_quota_shutdown(void){
drh75c19172010-09-01 14:58:10 +0000610 quotaGroup *pGroup;
drh748a52c2010-09-01 11:50:08 +0000611 if( gQuota.isInitialized==0 ) return SQLITE_MISUSE;
drh75c19172010-09-01 14:58:10 +0000612 for(pGroup=gQuota.pGroup; pGroup; pGroup=pGroup->pNext){
613 if( pGroup->pFiles ) return SQLITE_MISUSE;
dan8cf35eb2010-09-01 11:40:05 +0000614 }
615 while( gQuota.pGroup ){
drh75c19172010-09-01 14:58:10 +0000616 pGroup = gQuota.pGroup;
617 gQuota.pGroup = pGroup->pNext;
dan9fc77022010-09-01 16:19:57 +0000618 pGroup->iLimit = 0;
619 quotaGroupDeref(pGroup);
dan8cf35eb2010-09-01 11:40:05 +0000620 }
drh748a52c2010-09-01 11:50:08 +0000621 gQuota.isInitialized = 0;
622 sqlite3_mutex_free(gQuota.pMutex);
dan8cf35eb2010-09-01 11:40:05 +0000623 sqlite3_vfs_unregister(&gQuota.sThisVfs);
drh748a52c2010-09-01 11:50:08 +0000624 memset(&gQuota, 0, sizeof(gQuota));
dan8cf35eb2010-09-01 11:40:05 +0000625 return SQLITE_OK;
drh748a52c2010-09-01 11:50:08 +0000626}
627
628/*
629** Create or destroy a quota group.
630**
631** The quota group is defined by the zPattern. When calling this routine
632** with a zPattern for a quota group that already exists, this routine
633** merely updates the iLimit, xCallback, and pArg values for that quota
634** group. If zPattern is new, then a new quota group is created.
635**
636** If the iLimit for a quota group is set to zero, then the quota group
637** is disabled and will be deleted when the last database connection using
638** the quota group is closed.
639**
640** Calling this routine on a zPattern that does not exist and with a
641** zero iLimit is a no-op.
642**
643** A quota group must exist with a non-zero iLimit prior to opening
644** database connections if those connections are to participate in the
dan8cf35eb2010-09-01 11:40:05 +0000645** quota group. Creating a quota group does not affect database connections
drh748a52c2010-09-01 11:50:08 +0000646** that are already open.
647*/
648int sqlite3_quota_set(
dan8cf35eb2010-09-01 11:40:05 +0000649 const char *zPattern, /* The filename pattern */
650 sqlite3_int64 iLimit, /* New quota to set for this quota group */
651 void (*xCallback)( /* Callback invoked when going over quota */
drh748a52c2010-09-01 11:50:08 +0000652 const char *zFilename, /* Name of file whose size increases */
653 sqlite3_int64 *piLimit, /* IN/OUT: The current limit */
654 sqlite3_int64 iSize, /* Total size of all files in the group */
655 void *pArg /* Client data */
dan8cf35eb2010-09-01 11:40:05 +0000656 ),
drhef9659b2010-09-01 14:35:48 +0000657 void *pArg, /* client data passed thru to callback */
658 void (*xDestroy)(void*) /* Optional destructor for pArg */
drh748a52c2010-09-01 11:50:08 +0000659){
660 quotaGroup *pGroup;
661 quotaEnter();
662 pGroup = gQuota.pGroup;
663 while( pGroup && strcmp(pGroup->zPattern, zPattern)!=0 ){
664 pGroup = pGroup->pNext;
665 }
drh61cc4232010-09-01 14:45:16 +0000666 if( pGroup==0 ){
drh748a52c2010-09-01 11:50:08 +0000667 int nPattern = strlen(zPattern);
drh61cc4232010-09-01 14:45:16 +0000668 if( iLimit<=0 ){
669 quotaLeave();
670 return SQLITE_OK;
671 }
drh748a52c2010-09-01 11:50:08 +0000672 pGroup = sqlite3_malloc( sizeof(*pGroup) + nPattern + 1 );
673 if( pGroup==0 ){
674 quotaLeave();
675 return SQLITE_NOMEM;
676 }
677 memset(pGroup, 0, sizeof(*pGroup));
678 pGroup->zPattern = (char*)&pGroup[1];
dan8cf35eb2010-09-01 11:40:05 +0000679 memcpy((char *)pGroup->zPattern, zPattern, nPattern+1);
drh748a52c2010-09-01 11:50:08 +0000680 if( gQuota.pGroup ) gQuota.pGroup->ppPrev = &pGroup->pNext;
dan8cf35eb2010-09-01 11:40:05 +0000681 pGroup->pNext = gQuota.pGroup;
682 pGroup->ppPrev = &gQuota.pGroup;
683 gQuota.pGroup = pGroup;
drh748a52c2010-09-01 11:50:08 +0000684 }
685 pGroup->iLimit = iLimit;
686 pGroup->xCallback = xCallback;
drhef9659b2010-09-01 14:35:48 +0000687 if( pGroup->xDestroy && pGroup->pArg!=pArg ){
688 pGroup->xDestroy(pGroup->pArg);
689 }
drh748a52c2010-09-01 11:50:08 +0000690 pGroup->pArg = pArg;
drhef9659b2010-09-01 14:35:48 +0000691 pGroup->xDestroy = xDestroy;
drh748a52c2010-09-01 11:50:08 +0000692 quotaGroupDeref(pGroup);
693 quotaLeave();
694 return SQLITE_OK;
695}
696
697
698/***************************** Test Code ***********************************/
699#ifdef SQLITE_TEST
700#include <tcl.h>
dan8cf35eb2010-09-01 11:40:05 +0000701
drhef9659b2010-09-01 14:35:48 +0000702/*
703** Argument passed to a TCL quota-over-limit callback.
704*/
dan8cf35eb2010-09-01 11:40:05 +0000705typedef struct TclQuotaCallback TclQuotaCallback;
706struct TclQuotaCallback {
drhef9659b2010-09-01 14:35:48 +0000707 Tcl_Interp *interp; /* Interpreter in which to run the script */
708 Tcl_Obj *pScript; /* Script to be run */
dan8cf35eb2010-09-01 11:40:05 +0000709};
dan8cf35eb2010-09-01 11:40:05 +0000710
711extern const char *sqlite3TestErrorName(int);
712
drhef9659b2010-09-01 14:35:48 +0000713
714/*
715** This is the callback from a quota-over-limit.
716*/
dan8cf35eb2010-09-01 11:40:05 +0000717static void tclQuotaCallback(
718 const char *zFilename, /* Name of file whose size increases */
719 sqlite3_int64 *piLimit, /* IN/OUT: The current limit */
720 sqlite3_int64 iSize, /* Total size of all files in the group */
721 void *pArg /* Client data */
722){
723 TclQuotaCallback *p; /* Callback script object */
724 Tcl_Obj *pEval; /* Script to evaluate */
725 Tcl_Obj *pVarname; /* Name of variable to pass as 2nd arg */
726 unsigned int rnd; /* Random part of pVarname */
727 int rc; /* Tcl error code */
728
729 p = (TclQuotaCallback *)pArg;
drh3d3e60e2010-09-01 15:11:38 +0000730 if( p==0 ) return;
dan8cf35eb2010-09-01 11:40:05 +0000731
732 pVarname = Tcl_NewStringObj("::piLimit_", -1);
733 Tcl_IncrRefCount(pVarname);
734 sqlite3_randomness(sizeof(rnd), (void *)&rnd);
735 Tcl_AppendObjToObj(pVarname, Tcl_NewIntObj((int)(rnd&0x7FFFFFFF)));
736 Tcl_ObjSetVar2(p->interp, pVarname, 0, Tcl_NewWideIntObj(*piLimit), 0);
737
738 pEval = Tcl_DuplicateObj(p->pScript);
739 Tcl_IncrRefCount(pEval);
740 Tcl_ListObjAppendElement(0, pEval, Tcl_NewStringObj(zFilename, -1));
741 Tcl_ListObjAppendElement(0, pEval, pVarname);
742 Tcl_ListObjAppendElement(0, pEval, Tcl_NewWideIntObj(iSize));
743 rc = Tcl_EvalObjEx(p->interp, pEval, TCL_EVAL_GLOBAL);
744
745 if( rc==TCL_OK ){
746 Tcl_Obj *pLimit = Tcl_ObjGetVar2(p->interp, pVarname, 0, 0);
747 rc = Tcl_GetWideIntFromObj(p->interp, pLimit, piLimit);
748 Tcl_UnsetVar(p->interp, Tcl_GetString(pVarname), 0);
749 }
750
751 Tcl_DecrRefCount(pEval);
752 Tcl_DecrRefCount(pVarname);
753 if( rc!=TCL_OK ) Tcl_BackgroundError(p->interp);
754}
755
756/*
drhef9659b2010-09-01 14:35:48 +0000757** Destructor for a TCL quota-over-limit callback.
758*/
759static void tclCallbackDestructor(void *pObj){
760 TclQuotaCallback *p = (TclQuotaCallback*)pObj;
761 if( p ){
762 Tcl_DecrRefCount(p->pScript);
dan9fc77022010-09-01 16:19:57 +0000763 sqlite3_free((char *)p);
drhef9659b2010-09-01 14:35:48 +0000764 }
765}
766
767/*
dan8cf35eb2010-09-01 11:40:05 +0000768** tclcmd: sqlite3_quota_initialize NAME MAKEDEFAULT
769*/
770static int test_quota_initialize(
771 void * clientData,
772 Tcl_Interp *interp,
773 int objc,
774 Tcl_Obj *CONST objv[]
775){
776 const char *zName; /* Name of new quota VFS */
777 int makeDefault; /* True to make the new VFS the default */
778 int rc; /* Value returned by quota_initialize() */
779
780 /* Process arguments */
781 if( objc!=3 ){
782 Tcl_WrongNumArgs(interp, 1, objv, "NAME MAKEDEFAULT");
783 return TCL_ERROR;
784 }
785 zName = Tcl_GetString(objv[1]);
786 if( Tcl_GetBooleanFromObj(interp, objv[2], &makeDefault) ) return TCL_ERROR;
787 if( zName[0]=='\0' ) zName = 0;
788
789 /* Call sqlite3_quota_initialize() */
790 rc = sqlite3_quota_initialize(zName, makeDefault);
791 Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC);
792
793 return TCL_OK;
794}
795
796/*
797** tclcmd: sqlite3_quota_shutdown
798*/
799static int test_quota_shutdown(
800 void * clientData,
801 Tcl_Interp *interp,
802 int objc,
803 Tcl_Obj *CONST objv[]
804){
805 int rc; /* Value returned by quota_shutdown() */
806
807 if( objc!=1 ){
808 Tcl_WrongNumArgs(interp, 1, objv, "");
809 return TCL_ERROR;
810 }
811
812 /* Call sqlite3_quota_shutdown() */
813 rc = sqlite3_quota_shutdown();
814 Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC);
815
dan8cf35eb2010-09-01 11:40:05 +0000816 return TCL_OK;
817}
818
819/*
820** tclcmd: sqlite3_quota_set PATTERN LIMIT SCRIPT
821*/
822static int test_quota_set(
823 void * clientData,
824 Tcl_Interp *interp,
825 int objc,
826 Tcl_Obj *CONST objv[]
827){
828 const char *zPattern; /* File pattern to configure */
829 sqlite3_int64 iLimit; /* Initial quota in bytes */
830 Tcl_Obj *pScript; /* Tcl script to invoke to increase quota */
831 int rc; /* Value returned by quota_set() */
832 TclQuotaCallback *p; /* Callback object */
drhef9659b2010-09-01 14:35:48 +0000833 int nScript; /* Length of callback script */
dan9fc77022010-09-01 16:19:57 +0000834 void (*xDestroy)(void*); /* Optional destructor for pArg */
dan06bfd362010-09-01 18:00:09 +0000835 void (*xCallback)(const char *, sqlite3_int64 *, sqlite3_int64, void *);
dan8cf35eb2010-09-01 11:40:05 +0000836
837 /* Process arguments */
838 if( objc!=4 ){
839 Tcl_WrongNumArgs(interp, 1, objv, "PATTERN LIMIT SCRIPT");
840 return TCL_ERROR;
841 }
842 zPattern = Tcl_GetString(objv[1]);
843 if( Tcl_GetWideIntFromObj(interp, objv[2], &iLimit) ) return TCL_ERROR;
844 pScript = objv[3];
drhef9659b2010-09-01 14:35:48 +0000845 Tcl_GetStringFromObj(pScript, &nScript);
dan8cf35eb2010-09-01 11:40:05 +0000846
drhef9659b2010-09-01 14:35:48 +0000847 if( nScript>0 ){
848 /* Allocate a TclQuotaCallback object */
dan9fc77022010-09-01 16:19:57 +0000849 p = (TclQuotaCallback *)sqlite3_malloc(sizeof(TclQuotaCallback));
850 if( !p ){
851 Tcl_SetResult(interp, (char *)"SQLITE_NOMEM", TCL_STATIC);
852 return TCL_OK;
853 }
drhef9659b2010-09-01 14:35:48 +0000854 memset(p, 0, sizeof(TclQuotaCallback));
dan8cf35eb2010-09-01 11:40:05 +0000855 p->interp = interp;
drhef9659b2010-09-01 14:35:48 +0000856 Tcl_IncrRefCount(pScript);
dan8cf35eb2010-09-01 11:40:05 +0000857 p->pScript = pScript;
dan9fc77022010-09-01 16:19:57 +0000858 xDestroy = tclCallbackDestructor;
dan06bfd362010-09-01 18:00:09 +0000859 xCallback = tclQuotaCallback;
drhef9659b2010-09-01 14:35:48 +0000860 }else{
861 p = 0;
dan9fc77022010-09-01 16:19:57 +0000862 xDestroy = 0;
dan06bfd362010-09-01 18:00:09 +0000863 xCallback = 0;
dan8cf35eb2010-09-01 11:40:05 +0000864 }
dan9fc77022010-09-01 16:19:57 +0000865
drhef9659b2010-09-01 14:35:48 +0000866 /* Invoke sqlite3_quota_set() */
dan06bfd362010-09-01 18:00:09 +0000867 rc = sqlite3_quota_set(zPattern, iLimit, xCallback, (void*)p, xDestroy);
dan8cf35eb2010-09-01 11:40:05 +0000868
869 Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC);
870 return TCL_OK;
871}
872
873/*
drhef9659b2010-09-01 14:35:48 +0000874** tclcmd: sqlite3_quota_dump
875*/
876static int test_quota_dump(
877 void * clientData,
878 Tcl_Interp *interp,
879 int objc,
880 Tcl_Obj *CONST objv[]
881){
882 Tcl_Obj *pResult;
883 Tcl_Obj *pGroupTerm;
884 Tcl_Obj *pFileTerm;
885 quotaGroup *pGroup;
886 quotaFile *pFile;
887
888 pResult = Tcl_NewObj();
889 quotaEnter();
890 for(pGroup=gQuota.pGroup; pGroup; pGroup=pGroup->pNext){
891 pGroupTerm = Tcl_NewObj();
892 Tcl_ListObjAppendElement(interp, pGroupTerm,
893 Tcl_NewStringObj(pGroup->zPattern, -1));
894 Tcl_ListObjAppendElement(interp, pGroupTerm,
895 Tcl_NewWideIntObj(pGroup->iLimit));
896 Tcl_ListObjAppendElement(interp, pGroupTerm,
897 Tcl_NewWideIntObj(pGroup->iSize));
drh75c19172010-09-01 14:58:10 +0000898 for(pFile=pGroup->pFiles; pFile; pFile=pFile->pNext){
drhef9659b2010-09-01 14:35:48 +0000899 pFileTerm = Tcl_NewObj();
900 Tcl_ListObjAppendElement(interp, pFileTerm,
901 Tcl_NewStringObj(pFile->zFilename, -1));
902 Tcl_ListObjAppendElement(interp, pFileTerm,
903 Tcl_NewWideIntObj(pFile->iSize));
904 Tcl_ListObjAppendElement(interp, pFileTerm,
905 Tcl_NewWideIntObj(pFile->nRef));
906 Tcl_ListObjAppendElement(interp, pGroupTerm, pFileTerm);
907 }
908 Tcl_ListObjAppendElement(interp, pResult, pGroupTerm);
909 }
910 quotaLeave();
911 Tcl_SetObjResult(interp, pResult);
912 return TCL_OK;
913}
914
drh748a52c2010-09-01 11:50:08 +0000915/*
916** This routine registers the custom TCL commands defined in this
917** module. This should be the only procedure visible from outside
918** of this module.
919*/
920int Sqlitequota_Init(Tcl_Interp *interp){
dan8cf35eb2010-09-01 11:40:05 +0000921 static struct {
922 char *zName;
923 Tcl_ObjCmdProc *xProc;
924 } aCmd[] = {
925 { "sqlite3_quota_initialize", test_quota_initialize },
926 { "sqlite3_quota_shutdown", test_quota_shutdown },
927 { "sqlite3_quota_set", test_quota_set },
drhef9659b2010-09-01 14:35:48 +0000928 { "sqlite3_quota_dump", test_quota_dump },
dan8cf35eb2010-09-01 11:40:05 +0000929 };
930 int i;
931
932 for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
933 Tcl_CreateObjCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0);
934 }
935
drh748a52c2010-09-01 11:50:08 +0000936 return TCL_OK;
937}
938#endif