interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / src / test_quota.c

1965 lines · 58022 bytes Raw Blame History
1/*
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
20** 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.
22**
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 "test_quota.h"
31#include <string.h>
32#include <assert.h>
33
34/*
35** For an build without mutexes, no-op the mutex calls.
36*/
37#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE==0
38#define sqlite3_mutex_alloc(X) ((sqlite3_mutex*)8)
39#define sqlite3_mutex_free(X)
40#define sqlite3_mutex_enter(X)
41#define sqlite3_mutex_try(X) SQLITE_OK
42#define sqlite3_mutex_leave(X)
43#define sqlite3_mutex_held(X) ((void)(X),1)
44#define sqlite3_mutex_notheld(X) ((void)(X),1)
45#endif /* SQLITE_THREADSAFE==0 */
46
47
48#ifdef _WIN32
49# include <windows.h>
50# include <io.h>
51#else
52# include <unistd.h>
53#endif
54
55
56/************************ Object Definitions ******************************/
57
58/* Forward declaration of all object types */
59typedef struct quotaGroup quotaGroup;
60typedef struct quotaConn quotaConn;
61typedef struct quotaFile quotaFile;
62
63/*
64** A "quota group" is a collection of files whose collective size we want
65** to limit. Each quota group is defined by a GLOB pattern.
66**
67** There is an instance of the following object for each defined quota
68** group. This object records the GLOB pattern that defines which files
69** belong to the quota group. The object also remembers the size limit
70** for the group (the quota) and the callback to be invoked when the
71** sum of the sizes of the files within the group goes over the limit.
72**
73** A quota group must be established (using sqlite3_quota_set(...))
74** prior to opening any of the database connections that access files
75** within the quota group.
76*/
77struct quotaGroup {
78 const char *zPattern; /* Filename pattern to be quotaed */
79 sqlite3_int64 iLimit; /* Upper bound on total file size */
80 sqlite3_int64 iSize; /* Current size of all files */
81 void (*xCallback)( /* Callback invoked when going over quota */
82 const char *zFilename, /* Name of file whose size increases */
83 sqlite3_int64 *piLimit, /* IN/OUT: The current limit */
84 sqlite3_int64 iSize, /* Total size of all files in the group */
85 void *pArg /* Client data */
86 );
87 void *pArg; /* Third argument to the xCallback() */
88 void (*xDestroy)(void*); /* Optional destructor for pArg */
89 quotaGroup *pNext, **ppPrev; /* Doubly linked list of all quota objects */
90 quotaFile *pFiles; /* Files within this group */
91};
92
93/*
94** An instance of this structure represents a single file that is part
95** of a quota group. A single file can be opened multiple times. In
96** order keep multiple openings of the same file from causing the size
97** of the file to count against the quota multiple times, each file
98** has a unique instance of this object and multiple open connections
99** to the same file each point to a single instance of this object.
100*/
101struct quotaFile {
102 char *zFilename; /* Name of this file */
103 quotaGroup *pGroup; /* Quota group to which this file belongs */
104 sqlite3_int64 iSize; /* Current size of this file */
105 int nRef; /* Number of times this file is open */
106 int deleteOnClose; /* True to delete this file when it closes */
107 quotaFile *pNext, **ppPrev; /* Linked list of files in the same group */
108};
109
110/*
111** An instance of the following object represents each open connection
112** to a file that participates in quota tracking. This object is a
113** subclass of sqlite3_file. The sqlite3_file object for the underlying
114** VFS is appended to this structure.
115*/
116struct quotaConn {
117 sqlite3_file base; /* Base class - must be first */
118 quotaFile *pFile; /* The underlying file */
119 /* The underlying VFS sqlite3_file is appended to this object */
120};
121
122/*
123** An instance of the following object records the state of an
124** open file. This object is opaque to all users - the internal
125** structure is only visible to the functions below.
126*/
127struct quota_FILE {
128 FILE *f; /* Open stdio file pointer */
129 sqlite3_int64 iOfst; /* Current offset into the file */
130 quotaFile *pFile; /* The file record in the quota system */
131#ifdef _WIN32
132 char *zMbcsName; /* Full MBCS pathname of the file */
133#endif
134};
135
136
137/************************* Global Variables **********************************/
138/*
139** All global variables used by this file are containing within the following
140** gQuota structure.
141*/
142static struct {
143 /* The pOrigVfs is the real, original underlying VFS implementation.
144 ** Most operations pass-through to the real VFS. This value is read-only
145 ** during operation. It is only modified at start-time and thus does not
146 ** require a mutex.
147 */
148 sqlite3_vfs *pOrigVfs;
149
150 /* The sThisVfs is the VFS structure used by this shim. It is initialized
151 ** at start-time and thus does not require a mutex
152 */
153 sqlite3_vfs sThisVfs;
154
155 /* The sIoMethods defines the methods used by sqlite3_file objects
156 ** associated with this shim. It is initialized at start-time and does
157 ** not require a mutex.
158 **
159 ** When the underlying VFS is called to open a file, it might return
160 ** either a version 1 or a version 2 sqlite3_file object. This shim
161 ** has to create a wrapper sqlite3_file of the same version. Hence
162 ** there are two I/O method structures, one for version 1 and the other
163 ** for version 2.
164 */
165 sqlite3_io_methods sIoMethodsV1;
166 sqlite3_io_methods sIoMethodsV2;
167
168 /* True when this shim as been initialized.
169 */
170 int isInitialized;
171
172 /* For run-time access any of the other global data structures in this
173 ** shim, the following mutex must be held.
174 */
175 sqlite3_mutex *pMutex;
176
177 /* List of quotaGroup objects.
178 */
179 quotaGroup *pGroup;
180
181} gQuota;
182
183/************************* Utility Routines *********************************/
184/*
185** Acquire and release the mutex used to serialize access to the
186** list of quotaGroups.
187*/
188static void quotaEnter(void){ sqlite3_mutex_enter(gQuota.pMutex); }
189static void quotaLeave(void){ sqlite3_mutex_leave(gQuota.pMutex); }
190
191/* Count the number of open files in a quotaGroup
192*/
193static int quotaGroupOpenFileCount(quotaGroup *pGroup){
194 int N = 0;
195 quotaFile *pFile = pGroup->pFiles;
196 while( pFile ){
197 if( pFile->nRef ) N++;
198 pFile = pFile->pNext;
199 }
200 return N;
201}
202
203/* Remove a file from a quota group.
204*/
205static void quotaRemoveFile(quotaFile *pFile){
206 quotaGroup *pGroup = pFile->pGroup;
207 pGroup->iSize -= pFile->iSize;
208 *pFile->ppPrev = pFile->pNext;
209 if( pFile->pNext ) pFile->pNext->ppPrev = pFile->ppPrev;
210 sqlite3_free(pFile);
211}
212
213/* Remove all files from a quota group. It is always the case that
214** all files will be closed when this routine is called.
215*/
216static void quotaRemoveAllFiles(quotaGroup *pGroup){
217 while( pGroup->pFiles ){
218 assert( pGroup->pFiles->nRef==0 );
219 quotaRemoveFile(pGroup->pFiles);
220 }
221}
222
223
224/* If the reference count and threshold for a quotaGroup are both
225** zero, then destroy the quotaGroup.
226*/
227static void quotaGroupDeref(quotaGroup *pGroup){
228 if( pGroup->iLimit==0 && quotaGroupOpenFileCount(pGroup)==0 ){
229 quotaRemoveAllFiles(pGroup);
230 *pGroup->ppPrev = pGroup->pNext;
231 if( pGroup->pNext ) pGroup->pNext->ppPrev = pGroup->ppPrev;
232 if( pGroup->xDestroy ) pGroup->xDestroy(pGroup->pArg);
233 sqlite3_free(pGroup);
234 }
235}
236
237/*
238** Return TRUE if string z matches glob pattern zGlob.
239**
240** Globbing rules:
241**
242** '*' Matches any sequence of zero or more characters.
243**
244** '?' Matches exactly one character.
245**
246** [...] Matches one character from the enclosed list of
247** characters.
248**
249** [^...] Matches one character not in the enclosed list.
250**
251** / Matches "/" or "\\"
252**
253*/
254static int quotaStrglob(const char *zGlob, const char *z){
255 int c, c2, cx;
256 int invert;
257 int seen;
258
259 while( (c = (*(zGlob++)))!=0 ){
260 if( c=='*' ){
261 while( (c=(*(zGlob++))) == '*' || c=='?' ){
262 if( c=='?' && (*(z++))==0 ) return 0;
263 }
264 if( c==0 ){
265 return 1;
266 }else if( c=='[' ){
267 while( *z && quotaStrglob(zGlob-1,z)==0 ){
268 z++;
269 }
270 return (*z)!=0;
271 }
272 cx = (c=='/') ? '\\' : c;
273 while( (c2 = (*(z++)))!=0 ){
274 while( c2!=c && c2!=cx ){
275 c2 = *(z++);
276 if( c2==0 ) return 0;
277 }
278 if( quotaStrglob(zGlob,z) ) return 1;
279 }
280 return 0;
281 }else if( c=='?' ){
282 if( (*(z++))==0 ) return 0;
283 }else if( c=='[' ){
284 int prior_c = 0;
285 seen = 0;
286 invert = 0;
287 c = *(z++);
288 if( c==0 ) return 0;
289 c2 = *(zGlob++);
290 if( c2=='^' ){
291 invert = 1;
292 c2 = *(zGlob++);
293 }
294 if( c2==']' ){
295 if( c==']' ) seen = 1;
296 c2 = *(zGlob++);
297 }
298 while( c2 && c2!=']' ){
299 if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
300 c2 = *(zGlob++);
301 if( c>=prior_c && c<=c2 ) seen = 1;
302 prior_c = 0;
303 }else{
304 if( c==c2 ){
305 seen = 1;
306 }
307 prior_c = c2;
308 }
309 c2 = *(zGlob++);
310 }
311 if( c2==0 || (seen ^ invert)==0 ) return 0;
312 }else if( c=='/' ){
313 if( z[0]!='/' && z[0]!='\\' ) return 0;
314 z++;
315 }else{
316 if( c!=(*(z++)) ) return 0;
317 }
318 }
319 return *z==0;
320}
321
322
323/* Find a quotaGroup given the filename.
324**
325** Return a pointer to the quotaGroup object. Return NULL if not found.
326*/
327static quotaGroup *quotaGroupFind(const char *zFilename){
328 quotaGroup *p;
329 for(p=gQuota.pGroup; p && quotaStrglob(p->zPattern, zFilename)==0;
330 p=p->pNext){}
331 return p;
332}
333
334/* Translate an sqlite3_file* that is really a quotaConn* into
335** the sqlite3_file* for the underlying original VFS.
336*/
337static sqlite3_file *quotaSubOpen(sqlite3_file *pConn){
338 quotaConn *p = (quotaConn*)pConn;
339 return (sqlite3_file*)&p[1];
340}
341
342/* Find a file in a quota group and return a pointer to that file.
343** Return NULL if the file is not in the group.
344*/
345static quotaFile *quotaFindFile(
346 quotaGroup *pGroup, /* Group in which to look for the file */
347 const char *zName, /* Full pathname of the file */
348 int createFlag /* Try to create the file if not found */
349){
350 quotaFile *pFile = pGroup->pFiles;
351 while( pFile && strcmp(pFile->zFilename, zName)!=0 ){
352 pFile = pFile->pNext;
353 }
354 if( pFile==0 && createFlag ){
355 int nName = (int)(strlen(zName) & 0x3fffffff);
356 pFile = (quotaFile *)sqlite3_malloc( sizeof(*pFile) + nName + 1 );
357 if( pFile ){
358 memset(pFile, 0, sizeof(*pFile));
359 pFile->zFilename = (char*)&pFile[1];
360 memcpy(pFile->zFilename, zName, nName+1);
361 pFile->pNext = pGroup->pFiles;
362 if( pGroup->pFiles ) pGroup->pFiles->ppPrev = &pFile->pNext;
363 pFile->ppPrev = &pGroup->pFiles;
364 pGroup->pFiles = pFile;
365 pFile->pGroup = pGroup;
366 }
367 }
368 return pFile;
369}
370/*
371** Translate UTF8 to MBCS for use in fopen() calls. Return a pointer to the
372** translated text.. Call quota_mbcs_free() to deallocate any memory
373** used to store the returned pointer when done.
374*/
375static char *quota_utf8_to_mbcs(const char *zUtf8){
376#ifdef _WIN32
377 size_t n; /* Bytes in zUtf8 */
378 int nWide; /* number of UTF-16 characters */
379 int nMbcs; /* Bytes of MBCS */
380 LPWSTR zTmpWide; /* The UTF16 text */
381 char *zMbcs; /* The MBCS text */
382 int codepage; /* Code page used by fopen() */
383
384 n = strlen(zUtf8);
385 nWide = MultiByteToWideChar(CP_UTF8, 0, zUtf8, -1, NULL, 0);
386 if( nWide==0 ) return 0;
387 zTmpWide = (LPWSTR)sqlite3_malloc( (nWide+1)*sizeof(zTmpWide[0]) );
388 if( zTmpWide==0 ) return 0;
389 MultiByteToWideChar(CP_UTF8, 0, zUtf8, -1, zTmpWide, nWide);
390 codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
391 nMbcs = WideCharToMultiByte(codepage, 0, zTmpWide, nWide, 0, 0, 0, 0);
392 zMbcs = nMbcs ? (char*)sqlite3_malloc( nMbcs+1 ) : 0;
393 if( zMbcs ){
394 WideCharToMultiByte(codepage, 0, zTmpWide, nWide, zMbcs, nMbcs, 0, 0);
395 }
396 sqlite3_free(zTmpWide);
397 return zMbcs;
398#else
399 return (char*)zUtf8; /* No-op on unix */
400#endif
401}
402
403/*
404** Deallocate any memory allocated by quota_utf8_to_mbcs().
405*/
406static void quota_mbcs_free(char *zOld){
407#ifdef _WIN32
408 sqlite3_free(zOld);
409#else
410 /* No-op on unix */
411#endif
412}
413
414/************************* VFS Method Wrappers *****************************/
415/*
416** This is the xOpen method used for the "quota" VFS.
417**
418** Most of the work is done by the underlying original VFS. This method
419** simply links the new file into the appropriate quota group if it is a
420** file that needs to be tracked.
421*/
422static int quotaOpen(
423 sqlite3_vfs *pVfs, /* The quota VFS */
424 const char *zName, /* Name of file to be opened */
425 sqlite3_file *pConn, /* Fill in this file descriptor */
426 int flags, /* Flags to control the opening */
427 int *pOutFlags /* Flags showing results of opening */
428){
429 int rc; /* Result code */
430 quotaConn *pQuotaOpen; /* The new quota file descriptor */
431 quotaFile *pFile; /* Corresponding quotaFile obj */
432 quotaGroup *pGroup; /* The group file belongs to */
433 sqlite3_file *pSubOpen; /* Real file descriptor */
434 sqlite3_vfs *pOrigVfs = gQuota.pOrigVfs; /* Real VFS */
435
436 /* If the file is not a main database file or a WAL, then use the
437 ** normal xOpen method.
438 */
439 if( (flags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_WAL))==0 ){
440 return pOrigVfs->xOpen(pOrigVfs, zName, pConn, flags, pOutFlags);
441 }
442
443 /* If the name of the file does not match any quota group, then
444 ** use the normal xOpen method.
445 */
446 quotaEnter();
447 pGroup = quotaGroupFind(zName);
448 if( pGroup==0 ){
449 rc = pOrigVfs->xOpen(pOrigVfs, zName, pConn, flags, pOutFlags);
450 }else{
451 /* If we get to this point, it means the file needs to be quota tracked.
452 */
453 pQuotaOpen = (quotaConn*)pConn;
454 pSubOpen = quotaSubOpen(pConn);
455 rc = pOrigVfs->xOpen(pOrigVfs, zName, pSubOpen, flags, pOutFlags);
456 if( rc==SQLITE_OK ){
457 pFile = quotaFindFile(pGroup, zName, 1);
458 if( pFile==0 ){
459 quotaLeave();
460 pSubOpen->pMethods->xClose(pSubOpen);
461 return SQLITE_NOMEM;
462 }
463 pFile->deleteOnClose = (flags & SQLITE_OPEN_DELETEONCLOSE)!=0;
464 pFile->nRef++;
465 pQuotaOpen->pFile = pFile;
466 if( pSubOpen->pMethods->iVersion==1 ){
467 pQuotaOpen->base.pMethods = &gQuota.sIoMethodsV1;
468 }else{
469 pQuotaOpen->base.pMethods = &gQuota.sIoMethodsV2;
470 }
471 }
472 }
473 quotaLeave();
474 return rc;
475}
476
477/*
478** This is the xDelete method used for the "quota" VFS.
479**
480** If the file being deleted is part of the quota group, then reduce
481** the size of the quota group accordingly. And remove the file from
482** the set of files in the quota group.
483*/
484static int quotaDelete(
485 sqlite3_vfs *pVfs, /* The quota VFS */
486 const char *zName, /* Name of file to be deleted */
487 int syncDir /* Do a directory sync after deleting */
488){
489 int rc; /* Result code */
490 quotaFile *pFile; /* Files in the quota */
491 quotaGroup *pGroup; /* The group file belongs to */
492 sqlite3_vfs *pOrigVfs = gQuota.pOrigVfs; /* Real VFS */
493
494 /* Do the actual file delete */
495 rc = pOrigVfs->xDelete(pOrigVfs, zName, syncDir);
496
497 /* If the file just deleted is a member of a quota group, then remove
498 ** it from that quota group.
499 */
500 if( rc==SQLITE_OK ){
501 quotaEnter();
502 pGroup = quotaGroupFind(zName);
503 if( pGroup ){
504 pFile = quotaFindFile(pGroup, zName, 0);
505 if( pFile ){
506 if( pFile->nRef ){
507 pFile->deleteOnClose = 1;
508 }else{
509 quotaRemoveFile(pFile);
510 quotaGroupDeref(pGroup);
511 }
512 }
513 }
514 quotaLeave();
515 }
516 return rc;
517}
518
519
520/************************ I/O Method Wrappers *******************************/
521
522/* xClose requests get passed through to the original VFS. But we
523** also have to unlink the quotaConn from the quotaFile and quotaGroup.
524** The quotaFile and/or quotaGroup are freed if they are no longer in use.
525*/
526static int quotaClose(sqlite3_file *pConn){
527 quotaConn *p = (quotaConn*)pConn;
528 quotaFile *pFile = p->pFile;
529 sqlite3_file *pSubOpen = quotaSubOpen(pConn);
530 int rc;
531 rc = pSubOpen->pMethods->xClose(pSubOpen);
532 quotaEnter();
533 pFile->nRef--;
534 if( pFile->nRef==0 ){
535 quotaGroup *pGroup = pFile->pGroup;
536 if( pFile->deleteOnClose ){
537 gQuota.pOrigVfs->xDelete(gQuota.pOrigVfs, pFile->zFilename, 0);
538 quotaRemoveFile(pFile);
539 }
540 quotaGroupDeref(pGroup);
541 }
542 quotaLeave();
543 return rc;
544}
545
546/* Pass xRead requests directory thru to the original VFS without
547** further processing.
548*/
549static int quotaRead(
550 sqlite3_file *pConn,
551 void *pBuf,
552 int iAmt,
553 sqlite3_int64 iOfst
554){
555 sqlite3_file *pSubOpen = quotaSubOpen(pConn);
556 return pSubOpen->pMethods->xRead(pSubOpen, pBuf, iAmt, iOfst);
557}
558
559/* Check xWrite requests to see if they expand the file. If they do,
560** the perform a quota check before passing them through to the
561** original VFS.
562*/
563static int quotaWrite(
564 sqlite3_file *pConn,
565 const void *pBuf,
566 int iAmt,
567 sqlite3_int64 iOfst
568){
569 quotaConn *p = (quotaConn*)pConn;
570 sqlite3_file *pSubOpen = quotaSubOpen(pConn);
571 sqlite3_int64 iEnd = iOfst+iAmt;
572 quotaGroup *pGroup;
573 quotaFile *pFile = p->pFile;
574 sqlite3_int64 szNew;
575
576 if( pFile->iSize<iEnd ){
577 pGroup = pFile->pGroup;
578 quotaEnter();
579 szNew = pGroup->iSize - pFile->iSize + iEnd;
580 if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){
581 if( pGroup->xCallback ){
582 pGroup->xCallback(pFile->zFilename, &pGroup->iLimit, szNew,
583 pGroup->pArg);
584 }
585 if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){
586 quotaLeave();
587 return SQLITE_FULL;
588 }
589 }
590 pGroup->iSize = szNew;
591 pFile->iSize = iEnd;
592 quotaLeave();
593 }
594 return pSubOpen->pMethods->xWrite(pSubOpen, pBuf, iAmt, iOfst);
595}
596
597/* Pass xTruncate requests thru to the original VFS. If the
598** success, update the file size.
599*/
600static int quotaTruncate(sqlite3_file *pConn, sqlite3_int64 size){
601 quotaConn *p = (quotaConn*)pConn;
602 sqlite3_file *pSubOpen = quotaSubOpen(pConn);
603 int rc = pSubOpen->pMethods->xTruncate(pSubOpen, size);
604 quotaFile *pFile = p->pFile;
605 quotaGroup *pGroup;
606 if( rc==SQLITE_OK ){
607 quotaEnter();
608 pGroup = pFile->pGroup;
609 pGroup->iSize -= pFile->iSize;
610 pFile->iSize = size;
611 pGroup->iSize += size;
612 quotaLeave();
613 }
614 return rc;
615}
616
617/* Pass xSync requests through to the original VFS without change
618*/
619static int quotaSync(sqlite3_file *pConn, int flags){
620 sqlite3_file *pSubOpen = quotaSubOpen(pConn);
621 return pSubOpen->pMethods->xSync(pSubOpen, flags);
622}
623
624/* Pass xFileSize requests through to the original VFS but then
625** update the quotaGroup with the new size before returning.
626*/
627static int quotaFileSize(sqlite3_file *pConn, sqlite3_int64 *pSize){
628 quotaConn *p = (quotaConn*)pConn;
629 sqlite3_file *pSubOpen = quotaSubOpen(pConn);
630 quotaFile *pFile = p->pFile;
631 quotaGroup *pGroup;
632 sqlite3_int64 sz;
633 int rc;
634
635 rc = pSubOpen->pMethods->xFileSize(pSubOpen, &sz);
636 if( rc==SQLITE_OK ){
637 quotaEnter();
638 pGroup = pFile->pGroup;
639 pGroup->iSize -= pFile->iSize;
640 pFile->iSize = sz;
641 pGroup->iSize += sz;
642 quotaLeave();
643 *pSize = sz;
644 }
645 return rc;
646}
647
648/* Pass xLock requests through to the original VFS unchanged.
649*/
650static int quotaLock(sqlite3_file *pConn, int lock){
651 sqlite3_file *pSubOpen = quotaSubOpen(pConn);
652 return pSubOpen->pMethods->xLock(pSubOpen, lock);
653}
654
655/* Pass xUnlock requests through to the original VFS unchanged.
656*/
657static int quotaUnlock(sqlite3_file *pConn, int lock){
658 sqlite3_file *pSubOpen = quotaSubOpen(pConn);
659 return pSubOpen->pMethods->xUnlock(pSubOpen, lock);
660}
661
662/* Pass xCheckReservedLock requests through to the original VFS unchanged.
663*/
664static int quotaCheckReservedLock(sqlite3_file *pConn, int *pResOut){
665 sqlite3_file *pSubOpen = quotaSubOpen(pConn);
666 return pSubOpen->pMethods->xCheckReservedLock(pSubOpen, pResOut);
667}
668
669/* Pass xFileControl requests through to the original VFS unchanged.
670*/
671static int quotaFileControl(sqlite3_file *pConn, int op, void *pArg){
672 sqlite3_file *pSubOpen = quotaSubOpen(pConn);
673 int rc = pSubOpen->pMethods->xFileControl(pSubOpen, op, pArg);
674#if defined(SQLITE_FCNTL_VFSNAME)
675 if( op==SQLITE_FCNTL_VFSNAME && rc==SQLITE_OK ){
676 *(char**)pArg = sqlite3_mprintf("quota/%z", *(char**)pArg);
677 }
678#endif
679 return rc;
680}
681
682/* Pass xSectorSize requests through to the original VFS unchanged.
683*/
684static int quotaSectorSize(sqlite3_file *pConn){
685 sqlite3_file *pSubOpen = quotaSubOpen(pConn);
686 return pSubOpen->pMethods->xSectorSize(pSubOpen);
687}
688
689/* Pass xDeviceCharacteristics requests through to the original VFS unchanged.
690*/
691static int quotaDeviceCharacteristics(sqlite3_file *pConn){
692 sqlite3_file *pSubOpen = quotaSubOpen(pConn);
693 return pSubOpen->pMethods->xDeviceCharacteristics(pSubOpen);
694}
695
696/* Pass xShmMap requests through to the original VFS unchanged.
697*/
698static int quotaShmMap(
699 sqlite3_file *pConn, /* Handle open on database file */
700 int iRegion, /* Region to retrieve */
701 int szRegion, /* Size of regions */
702 int bExtend, /* True to extend file if necessary */
703 void volatile **pp /* OUT: Mapped memory */
704){
705 sqlite3_file *pSubOpen = quotaSubOpen(pConn);
706 return pSubOpen->pMethods->xShmMap(pSubOpen, iRegion, szRegion, bExtend, pp);
707}
708
709/* Pass xShmLock requests through to the original VFS unchanged.
710*/
711static int quotaShmLock(
712 sqlite3_file *pConn, /* Database file holding the shared memory */
713 int ofst, /* First lock to acquire or release */
714 int n, /* Number of locks to acquire or release */
715 int flags /* What to do with the lock */
716){
717 sqlite3_file *pSubOpen = quotaSubOpen(pConn);
718 return pSubOpen->pMethods->xShmLock(pSubOpen, ofst, n, flags);
719}
720
721/* Pass xShmBarrier requests through to the original VFS unchanged.
722*/
723static void quotaShmBarrier(sqlite3_file *pConn){
724 sqlite3_file *pSubOpen = quotaSubOpen(pConn);
725 pSubOpen->pMethods->xShmBarrier(pSubOpen);
726}
727
728/* Pass xShmUnmap requests through to the original VFS unchanged.
729*/
730static int quotaShmUnmap(sqlite3_file *pConn, int deleteFlag){
731 sqlite3_file *pSubOpen = quotaSubOpen(pConn);
732 return pSubOpen->pMethods->xShmUnmap(pSubOpen, deleteFlag);
733}
734
735/************************** Public Interfaces *****************************/
736/*
737** Initialize the quota VFS shim. Use the VFS named zOrigVfsName
738** as the VFS that does the actual work. Use the default if
739** zOrigVfsName==NULL.
740**
741** The quota VFS shim is named "quota". It will become the default
742** VFS if makeDefault is non-zero.
743**
744** THIS ROUTINE IS NOT THREADSAFE. Call this routine exactly once
745** during start-up.
746*/
747int sqlite3_quota_initialize(const char *zOrigVfsName, int makeDefault){
748 sqlite3_vfs *pOrigVfs;
749 if( gQuota.isInitialized ) return SQLITE_MISUSE;
750 pOrigVfs = sqlite3_vfs_find(zOrigVfsName);
751 if( pOrigVfs==0 ) return SQLITE_ERROR;
752 assert( pOrigVfs!=&gQuota.sThisVfs );
753 gQuota.pMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
754 if( !gQuota.pMutex ){
755 return SQLITE_NOMEM;
756 }
757 gQuota.isInitialized = 1;
758 gQuota.pOrigVfs = pOrigVfs;
759 gQuota.sThisVfs = *pOrigVfs;
760 gQuota.sThisVfs.xOpen = quotaOpen;
761 gQuota.sThisVfs.xDelete = quotaDelete;
762 gQuota.sThisVfs.szOsFile += sizeof(quotaConn);
763 gQuota.sThisVfs.zName = "quota";
764 gQuota.sIoMethodsV1.iVersion = 1;
765 gQuota.sIoMethodsV1.xClose = quotaClose;
766 gQuota.sIoMethodsV1.xRead = quotaRead;
767 gQuota.sIoMethodsV1.xWrite = quotaWrite;
768 gQuota.sIoMethodsV1.xTruncate = quotaTruncate;
769 gQuota.sIoMethodsV1.xSync = quotaSync;
770 gQuota.sIoMethodsV1.xFileSize = quotaFileSize;
771 gQuota.sIoMethodsV1.xLock = quotaLock;
772 gQuota.sIoMethodsV1.xUnlock = quotaUnlock;
773 gQuota.sIoMethodsV1.xCheckReservedLock = quotaCheckReservedLock;
774 gQuota.sIoMethodsV1.xFileControl = quotaFileControl;
775 gQuota.sIoMethodsV1.xSectorSize = quotaSectorSize;
776 gQuota.sIoMethodsV1.xDeviceCharacteristics = quotaDeviceCharacteristics;
777 gQuota.sIoMethodsV2 = gQuota.sIoMethodsV1;
778 gQuota.sIoMethodsV2.iVersion = 2;
779 gQuota.sIoMethodsV2.xShmMap = quotaShmMap;
780 gQuota.sIoMethodsV2.xShmLock = quotaShmLock;
781 gQuota.sIoMethodsV2.xShmBarrier = quotaShmBarrier;
782 gQuota.sIoMethodsV2.xShmUnmap = quotaShmUnmap;
783 sqlite3_vfs_register(&gQuota.sThisVfs, makeDefault);
784 return SQLITE_OK;
785}
786
787/*
788** Shutdown the quota system.
789**
790** All SQLite database connections must be closed before calling this
791** routine.
792**
793** THIS ROUTINE IS NOT THREADSAFE. Call this routine exactly once while
794** shutting down in order to free all remaining quota groups.
795*/
796int sqlite3_quota_shutdown(void){
797 quotaGroup *pGroup;
798 if( gQuota.isInitialized==0 ) return SQLITE_MISUSE;
799 for(pGroup=gQuota.pGroup; pGroup; pGroup=pGroup->pNext){
800 if( quotaGroupOpenFileCount(pGroup)>0 ) return SQLITE_MISUSE;
801 }
802 while( gQuota.pGroup ){
803 pGroup = gQuota.pGroup;
804 gQuota.pGroup = pGroup->pNext;
805 pGroup->iLimit = 0;
806 assert( quotaGroupOpenFileCount(pGroup)==0 );
807 quotaGroupDeref(pGroup);
808 }
809 gQuota.isInitialized = 0;
810 sqlite3_mutex_free(gQuota.pMutex);
811 sqlite3_vfs_unregister(&gQuota.sThisVfs);
812 memset(&gQuota, 0, sizeof(gQuota));
813 return SQLITE_OK;
814}
815
816/*
817** Create or destroy a quota group.
818**
819** The quota group is defined by the zPattern. When calling this routine
820** with a zPattern for a quota group that already exists, this routine
821** merely updates the iLimit, xCallback, and pArg values for that quota
822** group. If zPattern is new, then a new quota group is created.
823**
824** If the iLimit for a quota group is set to zero, then the quota group
825** is disabled and will be deleted when the last database connection using
826** the quota group is closed.
827**
828** Calling this routine on a zPattern that does not exist and with a
829** zero iLimit is a no-op.
830**
831** A quota group must exist with a non-zero iLimit prior to opening
832** database connections if those connections are to participate in the
833** quota group. Creating a quota group does not affect database connections
834** that are already open.
835*/
836int sqlite3_quota_set(
837 const char *zPattern, /* The filename pattern */
838 sqlite3_int64 iLimit, /* New quota to set for this quota group */
839 void (*xCallback)( /* Callback invoked when going over quota */
840 const char *zFilename, /* Name of file whose size increases */
841 sqlite3_int64 *piLimit, /* IN/OUT: The current limit */
842 sqlite3_int64 iSize, /* Total size of all files in the group */
843 void *pArg /* Client data */
844 ),
845 void *pArg, /* client data passed thru to callback */
846 void (*xDestroy)(void*) /* Optional destructor for pArg */
847){
848 quotaGroup *pGroup;
849 quotaEnter();
850 pGroup = gQuota.pGroup;
851 while( pGroup && strcmp(pGroup->zPattern, zPattern)!=0 ){
852 pGroup = pGroup->pNext;
853 }
854 if( pGroup==0 ){
855 int nPattern = (int)(strlen(zPattern) & 0x3fffffff);
856 if( iLimit<=0 ){
857 quotaLeave();
858 return SQLITE_OK;
859 }
860 pGroup = (quotaGroup *)sqlite3_malloc( sizeof(*pGroup) + nPattern + 1 );
861 if( pGroup==0 ){
862 quotaLeave();
863 return SQLITE_NOMEM;
864 }
865 memset(pGroup, 0, sizeof(*pGroup));
866 pGroup->zPattern = (char*)&pGroup[1];
867 memcpy((char *)pGroup->zPattern, zPattern, nPattern+1);
868 if( gQuota.pGroup ) gQuota.pGroup->ppPrev = &pGroup->pNext;
869 pGroup->pNext = gQuota.pGroup;
870 pGroup->ppPrev = &gQuota.pGroup;
871 gQuota.pGroup = pGroup;
872 }
873 pGroup->iLimit = iLimit;
874 pGroup->xCallback = xCallback;
875 if( pGroup->xDestroy && pGroup->pArg!=pArg ){
876 pGroup->xDestroy(pGroup->pArg);
877 }
878 pGroup->pArg = pArg;
879 pGroup->xDestroy = xDestroy;
880 quotaGroupDeref(pGroup);
881 quotaLeave();
882 return SQLITE_OK;
883}
884
885/*
886** Bring the named file under quota management. Or if it is already under
887** management, update its size.
888*/
889int sqlite3_quota_file(const char *zFilename){
890 char *zFull = 0;
891 sqlite3_file *fd;
892 int rc;
893 int outFlags = 0;
894 sqlite3_int64 iSize;
895 int nAlloc = gQuota.sThisVfs.szOsFile + gQuota.sThisVfs.mxPathname+2;
896
897 /* Allocate space for a file-handle and the full path for file zFilename */
898 fd = (sqlite3_file *)sqlite3_malloc(nAlloc);
899 if( fd==0 ){
900 rc = SQLITE_NOMEM;
901 }else{
902 zFull = &((char *)fd)[gQuota.sThisVfs.szOsFile];
903 rc = gQuota.pOrigVfs->xFullPathname(gQuota.pOrigVfs, zFilename,
904 gQuota.sThisVfs.mxPathname+1, zFull);
905 }
906
907 if( rc==SQLITE_OK ){
908 zFull[strlen(zFull)+1] = '\0';
909 rc = quotaOpen(&gQuota.sThisVfs, zFull, fd,
910 SQLITE_OPEN_READONLY | SQLITE_OPEN_MAIN_DB, &outFlags);
911 if( rc==SQLITE_OK ){
912 fd->pMethods->xFileSize(fd, &iSize);
913 fd->pMethods->xClose(fd);
914 }else if( rc==SQLITE_CANTOPEN ){
915 quotaGroup *pGroup;
916 quotaFile *pFile;
917 quotaEnter();
918 pGroup = quotaGroupFind(zFull);
919 if( pGroup ){
920 pFile = quotaFindFile(pGroup, zFull, 0);
921 if( pFile ) quotaRemoveFile(pFile);
922 }
923 quotaLeave();
924 }
925 }
926
927 sqlite3_free(fd);
928 return rc;
929}
930
931/*
932** Open a potentially quotaed file for I/O.
933*/
934quota_FILE *sqlite3_quota_fopen(const char *zFilename, const char *zMode){
935 quota_FILE *p = 0;
936 char *zFull = 0;
937 char *zFullTranslated = 0;
938 int rc;
939 quotaGroup *pGroup;
940 quotaFile *pFile;
941
942 zFull = (char*)sqlite3_malloc(gQuota.sThisVfs.mxPathname + 1);
943 if( zFull==0 ) return 0;
944 rc = gQuota.pOrigVfs->xFullPathname(gQuota.pOrigVfs, zFilename,
945 gQuota.sThisVfs.mxPathname+1, zFull);
946 if( rc ) goto quota_fopen_error;
947 p = (quota_FILE*)sqlite3_malloc(sizeof(*p));
948 if( p==0 ) goto quota_fopen_error;
949 memset(p, 0, sizeof(*p));
950 zFullTranslated = quota_utf8_to_mbcs(zFull);
951 if( zFullTranslated==0 ) goto quota_fopen_error;
952 p->f = fopen(zFullTranslated, zMode);
953 if( p->f==0 ) goto quota_fopen_error;
954 quotaEnter();
955 pGroup = quotaGroupFind(zFull);
956 if( pGroup ){
957 pFile = quotaFindFile(pGroup, zFull, 1);
958 if( pFile==0 ){
959 quotaLeave();
960 goto quota_fopen_error;
961 }
962 pFile->nRef++;
963 p->pFile = pFile;
964 }
965 quotaLeave();
966 sqlite3_free(zFull);
967#ifdef _WIN32
968 p->zMbcsName = zFullTranslated;
969#endif
970 return p;
971
972quota_fopen_error:
973 quota_mbcs_free(zFullTranslated);
974 sqlite3_free(zFull);
975 if( p && p->f ) fclose(p->f);
976 sqlite3_free(p);
977 return 0;
978}
979
980/*
981** Read content from a quota_FILE
982*/
983size_t sqlite3_quota_fread(
984 void *pBuf, /* Store the content here */
985 size_t size, /* Size of each element */
986 size_t nmemb, /* Number of elements to read */
987 quota_FILE *p /* Read from this quota_FILE object */
988){
989 return fread(pBuf, size, nmemb, p->f);
990}
991
992/*
993** Write content into a quota_FILE. Invoke the quota callback and block
994** the write if we exceed quota.
995*/
996size_t sqlite3_quota_fwrite(
997 const void *pBuf, /* Take content to write from here */
998 size_t size, /* Size of each element */
999 size_t nmemb, /* Number of elements */
1000 quota_FILE *p /* Write to this quota_FILE object */
1001){
1002 sqlite3_int64 iOfst;
1003 sqlite3_int64 iEnd;
1004 sqlite3_int64 szNew;
1005 quotaFile *pFile;
1006 size_t rc;
1007
1008 iOfst = ftell(p->f);
1009 iEnd = iOfst + size*nmemb;
1010 pFile = p->pFile;
1011 if( pFile && pFile->iSize<iEnd ){
1012 quotaGroup *pGroup = pFile->pGroup;
1013 quotaEnter();
1014 szNew = pGroup->iSize - pFile->iSize + iEnd;
1015 if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){
1016 if( pGroup->xCallback ){
1017 pGroup->xCallback(pFile->zFilename, &pGroup->iLimit, szNew,
1018 pGroup->pArg);
1019 }
1020 if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){
1021 iEnd = pGroup->iLimit - pGroup->iSize + pFile->iSize;
1022 nmemb = (size_t)((iEnd - iOfst)/size);
1023 iEnd = iOfst + size*nmemb;
1024 szNew = pGroup->iSize - pFile->iSize + iEnd;
1025 }
1026 }
1027 pGroup->iSize = szNew;
1028 pFile->iSize = iEnd;
1029 quotaLeave();
1030 }else{
1031 pFile = 0;
1032 }
1033 rc = fwrite(pBuf, size, nmemb, p->f);
1034
1035 /* If the write was incomplete, adjust the file size and group size
1036 ** downward */
1037 if( rc<nmemb && pFile ){
1038 size_t nWritten = rc;
1039 sqlite3_int64 iNewEnd = iOfst + size*nWritten;
1040 if( iNewEnd<iEnd ) iNewEnd = iEnd;
1041 quotaEnter();
1042 pFile->pGroup->iSize += iNewEnd - pFile->iSize;
1043 pFile->iSize = iNewEnd;
1044 quotaLeave();
1045 }
1046 return rc;
1047}
1048
1049/*
1050** Close an open quota_FILE stream.
1051*/
1052int sqlite3_quota_fclose(quota_FILE *p){
1053 int rc;
1054 quotaFile *pFile;
1055 rc = fclose(p->f);
1056 pFile = p->pFile;
1057 if( pFile ){
1058 quotaEnter();
1059 pFile->nRef--;
1060 if( pFile->nRef==0 ){
1061 quotaGroup *pGroup = pFile->pGroup;
1062 if( pFile->deleteOnClose ){
1063 gQuota.pOrigVfs->xDelete(gQuota.pOrigVfs, pFile->zFilename, 0);
1064 quotaRemoveFile(pFile);
1065 }
1066 quotaGroupDeref(pGroup);
1067 }
1068 quotaLeave();
1069 }
1070#ifdef _WIN32
1071 quota_mbcs_free(p->zMbcsName);
1072#endif
1073 sqlite3_free(p);
1074 return rc;
1075}
1076
1077/*
1078** Flush memory buffers for a quota_FILE to disk.
1079*/
1080int sqlite3_quota_fflush(quota_FILE *p, int doFsync){
1081 int rc;
1082 rc = fflush(p->f);
1083 if( rc==0 && doFsync ){
1084#ifdef _WIN32
1085 rc = _commit(_fileno(p->f));
1086#else
1087 rc = fsync(fileno(p->f));
1088#endif
1089 }
1090 return rc!=0;
1091}
1092
1093/*
1094** Seek on a quota_FILE stream.
1095*/
1096int sqlite3_quota_fseek(quota_FILE *p, long offset, int whence){
1097 return fseek(p->f, offset, whence);
1098}
1099
1100/*
1101** rewind a quota_FILE stream.
1102*/
1103void sqlite3_quota_rewind(quota_FILE *p){
1104 rewind(p->f);
1105}
1106
1107/*
1108** Tell the current location of a quota_FILE stream.
1109*/
1110long sqlite3_quota_ftell(quota_FILE *p){
1111 return ftell(p->f);
1112}
1113
1114/*
1115** Test the error indicator for the given file.
1116*/
1117int sqlite3_quota_ferror(quota_FILE *p){
1118 return ferror(p->f);
1119}
1120
1121/*
1122** Truncate a file to szNew bytes.
1123*/
1124int sqlite3_quota_ftruncate(quota_FILE *p, sqlite3_int64 szNew){
1125 quotaFile *pFile = p->pFile;
1126 int rc;
1127 if( (pFile = p->pFile)!=0 && pFile->iSize<szNew ){
1128 quotaGroup *pGroup;
1129 if( pFile->iSize<szNew ){
1130 /* This routine cannot be used to extend a file that is under
1131 ** quota management. Only true truncation is allowed. */
1132 return -1;
1133 }
1134 pGroup = pFile->pGroup;
1135 quotaEnter();
1136 pGroup->iSize += szNew - pFile->iSize;
1137 quotaLeave();
1138 }
1139#ifdef _WIN32
1140# if defined(__MSVCRT__) && defined(SQLITE_TEST)
1141 /* _chsize_s() is missing from MingW (as of 2012-11-06). Use
1142 ** _chsize() as a work-around for testing purposes. */
1143 rc = _chsize(_fileno(p->f), (long)szNew);
1144# else
1145 rc = _chsize_s(_fileno(p->f), szNew);
1146# endif
1147#else
1148 rc = ftruncate(fileno(p->f), szNew);
1149#endif
1150 if( pFile && rc==0 ){
1151 quotaGroup *pGroup = pFile->pGroup;
1152 quotaEnter();
1153 pGroup->iSize += szNew - pFile->iSize;
1154 pFile->iSize = szNew;
1155 quotaLeave();
1156 }
1157 return rc;
1158}
1159
1160/*
1161** Determine the time that the given file was last modified, in
1162** seconds size 1970. Write the result into *pTime. Return 0 on
1163** success and non-zero on any kind of error.
1164*/
1165int sqlite3_quota_file_mtime(quota_FILE *p, time_t *pTime){
1166 int rc;
1167#ifdef _WIN32
1168 struct _stati64 buf;
1169 rc = _stati64(p->zMbcsName, &buf);
1170#else
1171 struct stat buf;
1172 rc = fstat(fileno(p->f), &buf);
1173#endif
1174 if( rc==0 ) *pTime = buf.st_mtime;
1175 return rc;
1176}
1177
1178/*
1179** Return the true size of the file, as reported by the operating
1180** system.
1181*/
1182sqlite3_int64 sqlite3_quota_file_truesize(quota_FILE *p){
1183 int rc;
1184#ifdef _WIN32
1185 struct _stati64 buf;
1186 rc = _stati64(p->zMbcsName, &buf);
1187#else
1188 struct stat buf;
1189 rc = fstat(fileno(p->f), &buf);
1190#endif
1191 return rc==0 ? buf.st_size : -1;
1192}
1193
1194/*
1195** Return the size of the file, as it is known to the quota subsystem.
1196*/
1197sqlite3_int64 sqlite3_quota_file_size(quota_FILE *p){
1198 return p->pFile ? p->pFile->iSize : -1;
1199}
1200
1201/*
1202** Determine the amount of data in bytes available for reading
1203** in the given file.
1204*/
1205long sqlite3_quota_file_available(quota_FILE *p){
1206 FILE* f = p->f;
1207 long pos1, pos2;
1208 int rc;
1209 pos1 = ftell(f);
1210 if ( pos1 < 0 ) return -1;
1211 rc = fseek(f, 0, SEEK_END);
1212 if ( rc != 0 ) return -1;
1213 pos2 = ftell(f);
1214 if ( pos2 < 0 ) return -1;
1215 rc = fseek(f, pos1, SEEK_SET);
1216 if ( rc != 0 ) return -1;
1217 return pos2 - pos1;
1218}
1219
1220/*
1221** Remove a managed file. Update quotas accordingly.
1222*/
1223int sqlite3_quota_remove(const char *zFilename){
1224 char *zFull; /* Full pathname for zFilename */
1225 size_t nFull; /* Number of bytes in zFilename */
1226 int rc; /* Result code */
1227 quotaGroup *pGroup; /* Group containing zFilename */
1228 quotaFile *pFile; /* A file in the group */
1229 quotaFile *pNextFile; /* next file in the group */
1230 int diff; /* Difference between filenames */
1231 char c; /* First character past end of pattern */
1232
1233 zFull = (char*)sqlite3_malloc(gQuota.sThisVfs.mxPathname + 1);
1234 if( zFull==0 ) return SQLITE_NOMEM;
1235 rc = gQuota.pOrigVfs->xFullPathname(gQuota.pOrigVfs, zFilename,
1236 gQuota.sThisVfs.mxPathname+1, zFull);
1237 if( rc ){
1238 sqlite3_free(zFull);
1239 return rc;
1240 }
1241
1242 /* Figure out the length of the full pathname. If the name ends with
1243 ** / (or \ on windows) then remove the trailing /.
1244 */
1245 nFull = strlen(zFull);
1246 if( nFull>0 && (zFull[nFull-1]=='/' || zFull[nFull-1]=='\\') ){
1247 nFull--;
1248 zFull[nFull] = 0;
1249 }
1250
1251 quotaEnter();
1252 pGroup = quotaGroupFind(zFull);
1253 if( pGroup ){
1254 for(pFile=pGroup->pFiles; pFile && rc==SQLITE_OK; pFile=pNextFile){
1255 pNextFile = pFile->pNext;
1256 diff = strncmp(zFull, pFile->zFilename, nFull);
1257 if( diff==0 && ((c = pFile->zFilename[nFull])==0 || c=='/' || c=='\\') ){
1258 if( pFile->nRef ){
1259 pFile->deleteOnClose = 1;
1260 }else{
1261 rc = gQuota.pOrigVfs->xDelete(gQuota.pOrigVfs, pFile->zFilename, 0);
1262 quotaRemoveFile(pFile);
1263 quotaGroupDeref(pGroup);
1264 }
1265 }
1266 }
1267 }
1268 quotaLeave();
1269 sqlite3_free(zFull);
1270 return rc;
1271}
1272
1273/***************************** Test Code ***********************************/
1274#ifdef SQLITE_TEST
1275#include "tclsqlite.h"
1276
1277/*
1278** Argument passed to a TCL quota-over-limit callback.
1279*/
1280typedef struct TclQuotaCallback TclQuotaCallback;
1281struct TclQuotaCallback {
1282 Tcl_Interp *interp; /* Interpreter in which to run the script */
1283 Tcl_Obj *pScript; /* Script to be run */
1284};
1285
1286extern const char *sqlite3ErrName(int);
1287
1288
1289/*
1290** This is the callback from a quota-over-limit.
1291*/
1292static void tclQuotaCallback(
1293 const char *zFilename, /* Name of file whose size increases */
1294 sqlite3_int64 *piLimit, /* IN/OUT: The current limit */
1295 sqlite3_int64 iSize, /* Total size of all files in the group */
1296 void *pArg /* Client data */
1297){
1298 TclQuotaCallback *p; /* Callback script object */
1299 Tcl_Obj *pEval; /* Script to evaluate */
1300 Tcl_Obj *pVarname; /* Name of variable to pass as 2nd arg */
1301 unsigned int rnd; /* Random part of pVarname */
1302 int rc; /* Tcl error code */
1303
1304 p = (TclQuotaCallback *)pArg;
1305 if( p==0 ) return;
1306
1307 pVarname = Tcl_NewStringObj("::piLimit_", -1);
1308 Tcl_IncrRefCount(pVarname);
1309 sqlite3_randomness(sizeof(rnd), (void *)&rnd);
1310 Tcl_AppendObjToObj(pVarname, Tcl_NewIntObj((int)(rnd&0x7FFFFFFF)));
1311 Tcl_ObjSetVar2(p->interp, pVarname, 0, Tcl_NewWideIntObj(*piLimit), 0);
1312
1313 pEval = Tcl_DuplicateObj(p->pScript);
1314 Tcl_IncrRefCount(pEval);
1315 Tcl_ListObjAppendElement(0, pEval, Tcl_NewStringObj(zFilename, -1));
1316 Tcl_ListObjAppendElement(0, pEval, pVarname);
1317 Tcl_ListObjAppendElement(0, pEval, Tcl_NewWideIntObj(iSize));
1318 rc = Tcl_EvalObjEx(p->interp, pEval, TCL_EVAL_GLOBAL);
1319
1320 if( rc==TCL_OK ){
1321 Tcl_WideInt x;
1322 Tcl_Obj *pLimit = Tcl_ObjGetVar2(p->interp, pVarname, 0, 0);
1323 rc = Tcl_GetWideIntFromObj(p->interp, pLimit, &x);
1324 *piLimit = x;
1325 Tcl_UnsetVar(p->interp, Tcl_GetString(pVarname), 0);
1326 }
1327
1328 Tcl_DecrRefCount(pEval);
1329 Tcl_DecrRefCount(pVarname);
1330 if( rc!=TCL_OK ) Tcl_BackgroundError(p->interp);
1331}
1332
1333/*
1334** Destructor for a TCL quota-over-limit callback.
1335*/
1336static void tclCallbackDestructor(void *pObj){
1337 TclQuotaCallback *p = (TclQuotaCallback*)pObj;
1338 if( p ){
1339 Tcl_DecrRefCount(p->pScript);
1340 sqlite3_free((char *)p);
1341 }
1342}
1343
1344/*
1345** tclcmd: sqlite3_quota_initialize NAME MAKEDEFAULT
1346*/
1347static int SQLITE_TCLAPI test_quota_initialize(
1348 void * clientData,
1349 Tcl_Interp *interp,
1350 int objc,
1351 Tcl_Obj *CONST objv[]
1352){
1353 const char *zName; /* Name of new quota VFS */
1354 int makeDefault; /* True to make the new VFS the default */
1355 int rc; /* Value returned by quota_initialize() */
1356
1357 /* Process arguments */
1358 if( objc!=3 ){
1359 Tcl_WrongNumArgs(interp, 1, objv, "NAME MAKEDEFAULT");
1360 return TCL_ERROR;
1361 }
1362 zName = Tcl_GetString(objv[1]);
1363 if( Tcl_GetBooleanFromObj(interp, objv[2], &makeDefault) ) return TCL_ERROR;
1364 if( zName[0]=='\0' ) zName = 0;
1365
1366 /* Call sqlite3_quota_initialize() */
1367 rc = sqlite3_quota_initialize(zName, makeDefault);
1368 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
1369
1370 return TCL_OK;
1371}
1372
1373/*
1374** tclcmd: sqlite3_quota_shutdown
1375*/
1376static int SQLITE_TCLAPI test_quota_shutdown(
1377 void * clientData,
1378 Tcl_Interp *interp,
1379 int objc,
1380 Tcl_Obj *CONST objv[]
1381){
1382 int rc; /* Value returned by quota_shutdown() */
1383
1384 if( objc!=1 ){
1385 Tcl_WrongNumArgs(interp, 1, objv, "");
1386 return TCL_ERROR;
1387 }
1388
1389 /* Call sqlite3_quota_shutdown() */
1390 rc = sqlite3_quota_shutdown();
1391 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
1392
1393 return TCL_OK;
1394}
1395
1396/*
1397** tclcmd: sqlite3_quota_set PATTERN LIMIT SCRIPT
1398*/
1399static int SQLITE_TCLAPI test_quota_set(
1400 void * clientData,
1401 Tcl_Interp *interp,
1402 int objc,
1403 Tcl_Obj *CONST objv[]
1404){
1405 const char *zPattern; /* File pattern to configure */
1406 Tcl_WideInt iLimit; /* Initial quota in bytes */
1407 Tcl_Obj *pScript; /* Tcl script to invoke to increase quota */
1408 int rc; /* Value returned by quota_set() */
1409 TclQuotaCallback *p; /* Callback object */
1410 Tcl_Size nScript; /* Length of callback script */
1411 void (*xDestroy)(void*); /* Optional destructor for pArg */
1412 void (*xCallback)(const char *, sqlite3_int64 *, sqlite3_int64, void *);
1413
1414 /* Process arguments */
1415 if( objc!=4 ){
1416 Tcl_WrongNumArgs(interp, 1, objv, "PATTERN LIMIT SCRIPT");
1417 return TCL_ERROR;
1418 }
1419 zPattern = Tcl_GetString(objv[1]);
1420 if( Tcl_GetWideIntFromObj(interp, objv[2], &iLimit) ) return TCL_ERROR;
1421 pScript = objv[3];
1422 Tcl_GetStringFromObj(pScript, &nScript);
1423
1424 if( nScript>0 ){
1425 /* Allocate a TclQuotaCallback object */
1426 p = (TclQuotaCallback *)sqlite3_malloc(sizeof(TclQuotaCallback));
1427 if( !p ){
1428 Tcl_SetResult(interp, (char *)"SQLITE_NOMEM", TCL_STATIC);
1429 return TCL_OK;
1430 }
1431 memset(p, 0, sizeof(TclQuotaCallback));
1432 p->interp = interp;
1433 Tcl_IncrRefCount(pScript);
1434 p->pScript = pScript;
1435 xDestroy = tclCallbackDestructor;
1436 xCallback = tclQuotaCallback;
1437 }else{
1438 p = 0;
1439 xDestroy = 0;
1440 xCallback = 0;
1441 }
1442
1443 /* Invoke sqlite3_quota_set() */
1444 rc = sqlite3_quota_set(zPattern, iLimit, xCallback, (void*)p, xDestroy);
1445
1446 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
1447 return TCL_OK;
1448}
1449
1450/*
1451** tclcmd: sqlite3_quota_file FILENAME
1452*/
1453static int SQLITE_TCLAPI test_quota_file(
1454 void * clientData,
1455 Tcl_Interp *interp,
1456 int objc,
1457 Tcl_Obj *CONST objv[]
1458){
1459 const char *zFilename; /* File pattern to configure */
1460 int rc; /* Value returned by quota_file() */
1461
1462 /* Process arguments */
1463 if( objc!=2 ){
1464 Tcl_WrongNumArgs(interp, 1, objv, "FILENAME");
1465 return TCL_ERROR;
1466 }
1467 zFilename = Tcl_GetString(objv[1]);
1468
1469 /* Invoke sqlite3_quota_file() */
1470 rc = sqlite3_quota_file(zFilename);
1471
1472 Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
1473 return TCL_OK;
1474}
1475
1476/*
1477** tclcmd: sqlite3_quota_dump
1478*/
1479static int SQLITE_TCLAPI test_quota_dump(
1480 void * clientData,
1481 Tcl_Interp *interp,
1482 int objc,
1483 Tcl_Obj *CONST objv[]
1484){
1485 Tcl_Obj *pResult;
1486 Tcl_Obj *pGroupTerm;
1487 Tcl_Obj *pFileTerm;
1488 quotaGroup *pGroup;
1489 quotaFile *pFile;
1490
1491 pResult = Tcl_NewObj();
1492 quotaEnter();
1493 for(pGroup=gQuota.pGroup; pGroup; pGroup=pGroup->pNext){
1494 pGroupTerm = Tcl_NewObj();
1495 Tcl_ListObjAppendElement(interp, pGroupTerm,
1496 Tcl_NewStringObj(pGroup->zPattern, -1));
1497 Tcl_ListObjAppendElement(interp, pGroupTerm,
1498 Tcl_NewWideIntObj(pGroup->iLimit));
1499 Tcl_ListObjAppendElement(interp, pGroupTerm,
1500 Tcl_NewWideIntObj(pGroup->iSize));
1501 for(pFile=pGroup->pFiles; pFile; pFile=pFile->pNext){
1502 int i;
1503 char zTemp[1000];
1504 pFileTerm = Tcl_NewObj();
1505 sqlite3_snprintf(sizeof(zTemp), zTemp, "%s", pFile->zFilename);
1506 for(i=0; zTemp[i]; i++){ if( zTemp[i]=='\\' ) zTemp[i] = '/'; }
1507 Tcl_ListObjAppendElement(interp, pFileTerm,
1508 Tcl_NewStringObj(zTemp, -1));
1509 Tcl_ListObjAppendElement(interp, pFileTerm,
1510 Tcl_NewWideIntObj(pFile->iSize));
1511 Tcl_ListObjAppendElement(interp, pFileTerm,
1512 Tcl_NewWideIntObj(pFile->nRef));
1513 Tcl_ListObjAppendElement(interp, pFileTerm,
1514 Tcl_NewWideIntObj(pFile->deleteOnClose));
1515 Tcl_ListObjAppendElement(interp, pGroupTerm, pFileTerm);
1516 }
1517 Tcl_ListObjAppendElement(interp, pResult, pGroupTerm);
1518 }
1519 quotaLeave();
1520 Tcl_SetObjResult(interp, pResult);
1521 return TCL_OK;
1522}
1523
1524/* Defined in test1.c */
1525extern void *sqlite3TestTextToPtr(const char*);
1526extern const char *sqlite3TestPtrToText(void*);
1527
1528/*
1529** tclcmd: sqlite3_quota_fopen FILENAME MODE
1530*/
1531static int SQLITE_TCLAPI test_quota_fopen(
1532 void * clientData,
1533 Tcl_Interp *interp,
1534 int objc,
1535 Tcl_Obj *CONST objv[]
1536){
1537 const char *zFilename; /* File pattern to configure */
1538 const char *zMode; /* Mode string */
1539 quota_FILE *p; /* Open string object */
1540
1541 /* Process arguments */
1542 if( objc!=3 ){
1543 Tcl_WrongNumArgs(interp, 1, objv, "FILENAME MODE");
1544 return TCL_ERROR;
1545 }
1546 zFilename = Tcl_GetString(objv[1]);
1547 zMode = Tcl_GetString(objv[2]);
1548 p = sqlite3_quota_fopen(zFilename, zMode);
1549 Tcl_SetResult(interp, (char*)sqlite3TestPtrToText(p), TCL_VOLATILE);
1550 return TCL_OK;
1551}
1552
1553/*
1554** tclcmd: sqlite3_quota_fread HANDLE SIZE NELEM
1555*/
1556static int SQLITE_TCLAPI test_quota_fread(
1557 void * clientData,
1558 Tcl_Interp *interp,
1559 int objc,
1560 Tcl_Obj *CONST objv[]
1561){
1562 quota_FILE *p;
1563 char *zBuf;
1564 int sz;
1565 int nElem;
1566 size_t got;
1567
1568 if( objc!=4 ){
1569 Tcl_WrongNumArgs(interp, 1, objv, "HANDLE SIZE NELEM");
1570 return TCL_ERROR;
1571 }
1572 p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
1573 if( Tcl_GetIntFromObj(interp, objv[2], &sz) ) return TCL_ERROR;
1574 if( Tcl_GetIntFromObj(interp, objv[3], &nElem) ) return TCL_ERROR;
1575 zBuf = (char*)sqlite3_malloc( sz*nElem + 1 );
1576 if( zBuf==0 ){
1577 Tcl_SetResult(interp, "out of memory", TCL_STATIC);
1578 return TCL_ERROR;
1579 }
1580 got = sqlite3_quota_fread(zBuf, sz, nElem, p);
1581 zBuf[got*sz] = 0;
1582 Tcl_SetResult(interp, zBuf, TCL_VOLATILE);
1583 sqlite3_free(zBuf);
1584 return TCL_OK;
1585}
1586
1587/*
1588** tclcmd: sqlite3_quota_fwrite HANDLE SIZE NELEM CONTENT
1589*/
1590static int SQLITE_TCLAPI test_quota_fwrite(
1591 void * clientData,
1592 Tcl_Interp *interp,
1593 int objc,
1594 Tcl_Obj *CONST objv[]
1595){
1596 quota_FILE *p;
1597 char *zBuf;
1598 int sz;
1599 int nElem;
1600 size_t got;
1601
1602 if( objc!=5 ){
1603 Tcl_WrongNumArgs(interp, 1, objv, "HANDLE SIZE NELEM CONTENT");
1604 return TCL_ERROR;
1605 }
1606 p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
1607 if( Tcl_GetIntFromObj(interp, objv[2], &sz) ) return TCL_ERROR;
1608 if( Tcl_GetIntFromObj(interp, objv[3], &nElem) ) return TCL_ERROR;
1609 zBuf = Tcl_GetString(objv[4]);
1610 got = sqlite3_quota_fwrite(zBuf, sz, nElem, p);
1611 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(got));
1612 return TCL_OK;
1613}
1614
1615/*
1616** tclcmd: sqlite3_quota_fclose HANDLE
1617*/
1618static int SQLITE_TCLAPI test_quota_fclose(
1619 void * clientData,
1620 Tcl_Interp *interp,
1621 int objc,
1622 Tcl_Obj *CONST objv[]
1623){
1624 quota_FILE *p;
1625 int rc;
1626
1627 if( objc!=2 ){
1628 Tcl_WrongNumArgs(interp, 1, objv, "HANDLE");
1629 return TCL_ERROR;
1630 }
1631 p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
1632 rc = sqlite3_quota_fclose(p);
1633 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
1634 return TCL_OK;
1635}
1636
1637/*
1638** tclcmd: sqlite3_quota_fflush HANDLE ?HARDSYNC?
1639*/
1640static int SQLITE_TCLAPI test_quota_fflush(
1641 void * clientData,
1642 Tcl_Interp *interp,
1643 int objc,
1644 Tcl_Obj *CONST objv[]
1645){
1646 quota_FILE *p;
1647 int rc;
1648 int doSync = 0;
1649
1650 if( objc!=2 && objc!=3 ){
1651 Tcl_WrongNumArgs(interp, 1, objv, "HANDLE ?HARDSYNC?");
1652 return TCL_ERROR;
1653 }
1654 p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
1655 if( objc==3 ){
1656 if( Tcl_GetBooleanFromObj(interp, objv[2], &doSync) ) return TCL_ERROR;
1657 }
1658 rc = sqlite3_quota_fflush(p, doSync);
1659 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
1660 return TCL_OK;
1661}
1662
1663/*
1664** tclcmd: sqlite3_quota_fseek HANDLE OFFSET WHENCE
1665*/
1666static int SQLITE_TCLAPI test_quota_fseek(
1667 void * clientData,
1668 Tcl_Interp *interp,
1669 int objc,
1670 Tcl_Obj *CONST objv[]
1671){
1672 quota_FILE *p;
1673 int ofst;
1674 const char *zWhence;
1675 int whence;
1676 int rc;
1677
1678 if( objc!=4 ){
1679 Tcl_WrongNumArgs(interp, 1, objv, "HANDLE OFFSET WHENCE");
1680 return TCL_ERROR;
1681 }
1682 p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
1683 if( Tcl_GetIntFromObj(interp, objv[2], &ofst) ) return TCL_ERROR;
1684 zWhence = Tcl_GetString(objv[3]);
1685 if( strcmp(zWhence, "SEEK_SET")==0 ){
1686 whence = SEEK_SET;
1687 }else if( strcmp(zWhence, "SEEK_CUR")==0 ){
1688 whence = SEEK_CUR;
1689 }else if( strcmp(zWhence, "SEEK_END")==0 ){
1690 whence = SEEK_END;
1691 }else{
1692 Tcl_AppendResult(interp,
1693 "WHENCE should be SEEK_SET, SEEK_CUR, or SEEK_END", (char*)0);
1694 return TCL_ERROR;
1695 }
1696 rc = sqlite3_quota_fseek(p, ofst, whence);
1697 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
1698 return TCL_OK;
1699}
1700
1701/*
1702** tclcmd: sqlite3_quota_rewind HANDLE
1703*/
1704static int SQLITE_TCLAPI test_quota_rewind(
1705 void * clientData,
1706 Tcl_Interp *interp,
1707 int objc,
1708 Tcl_Obj *CONST objv[]
1709){
1710 quota_FILE *p;
1711 if( objc!=2 ){
1712 Tcl_WrongNumArgs(interp, 1, objv, "HANDLE");
1713 return TCL_ERROR;
1714 }
1715 p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
1716 sqlite3_quota_rewind(p);
1717 return TCL_OK;
1718}
1719
1720/*
1721** tclcmd: sqlite3_quota_ftell HANDLE
1722*/
1723static int SQLITE_TCLAPI test_quota_ftell(
1724 void * clientData,
1725 Tcl_Interp *interp,
1726 int objc,
1727 Tcl_Obj *CONST objv[]
1728){
1729 quota_FILE *p;
1730 sqlite3_int64 x;
1731 if( objc!=2 ){
1732 Tcl_WrongNumArgs(interp, 1, objv, "HANDLE");
1733 return TCL_ERROR;
1734 }
1735 p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
1736 x = sqlite3_quota_ftell(p);
1737 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(x));
1738 return TCL_OK;
1739}
1740
1741/*
1742** tclcmd: sqlite3_quota_ftruncate HANDLE SIZE
1743*/
1744static int SQLITE_TCLAPI test_quota_ftruncate(
1745 void * clientData,
1746 Tcl_Interp *interp,
1747 int objc,
1748 Tcl_Obj *CONST objv[]
1749){
1750 quota_FILE *p;
1751 sqlite3_int64 x;
1752 Tcl_WideInt w;
1753 int rc;
1754 if( objc!=3 ){
1755 Tcl_WrongNumArgs(interp, 1, objv, "HANDLE SIZE");
1756 return TCL_ERROR;
1757 }
1758 p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
1759 if( Tcl_GetWideIntFromObj(interp, objv[2], &w) ) return TCL_ERROR;
1760 x = (sqlite3_int64)w;
1761 rc = sqlite3_quota_ftruncate(p, x);
1762 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
1763 return TCL_OK;
1764}
1765
1766/*
1767** tclcmd: sqlite3_quota_file_size HANDLE
1768*/
1769static int SQLITE_TCLAPI test_quota_file_size(
1770 void * clientData,
1771 Tcl_Interp *interp,
1772 int objc,
1773 Tcl_Obj *CONST objv[]
1774){
1775 quota_FILE *p;
1776 sqlite3_int64 x;
1777 if( objc!=2 ){
1778 Tcl_WrongNumArgs(interp, 1, objv, "HANDLE");
1779 return TCL_ERROR;
1780 }
1781 p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
1782 x = sqlite3_quota_file_size(p);
1783 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(x));
1784 return TCL_OK;
1785}
1786
1787/*
1788** tclcmd: sqlite3_quota_file_truesize HANDLE
1789*/
1790static int SQLITE_TCLAPI test_quota_file_truesize(
1791 void * clientData,
1792 Tcl_Interp *interp,
1793 int objc,
1794 Tcl_Obj *CONST objv[]
1795){
1796 quota_FILE *p;
1797 sqlite3_int64 x;
1798 if( objc!=2 ){
1799 Tcl_WrongNumArgs(interp, 1, objv, "HANDLE");
1800 return TCL_ERROR;
1801 }
1802 p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
1803 x = sqlite3_quota_file_truesize(p);
1804 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(x));
1805 return TCL_OK;
1806}
1807
1808/*
1809** tclcmd: sqlite3_quota_file_mtime HANDLE
1810*/
1811static int SQLITE_TCLAPI test_quota_file_mtime(
1812 void * clientData,
1813 Tcl_Interp *interp,
1814 int objc,
1815 Tcl_Obj *CONST objv[]
1816){
1817 quota_FILE *p;
1818 time_t t;
1819 if( objc!=2 ){
1820 Tcl_WrongNumArgs(interp, 1, objv, "HANDLE");
1821 return TCL_ERROR;
1822 }
1823 p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
1824 t = 0;
1825 sqlite3_quota_file_mtime(p, &t);
1826 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(t));
1827 return TCL_OK;
1828}
1829
1830
1831/*
1832** tclcmd: sqlite3_quota_remove FILENAME
1833*/
1834static int SQLITE_TCLAPI test_quota_remove(
1835 void * clientData,
1836 Tcl_Interp *interp,
1837 int objc,
1838 Tcl_Obj *CONST objv[]
1839){
1840 const char *zFilename; /* File pattern to configure */
1841 int rc;
1842 if( objc!=2 ){
1843 Tcl_WrongNumArgs(interp, 1, objv, "FILENAME");
1844 return TCL_ERROR;
1845 }
1846 zFilename = Tcl_GetString(objv[1]);
1847 rc = sqlite3_quota_remove(zFilename);
1848 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
1849 return TCL_OK;
1850}
1851
1852/*
1853** tclcmd: sqlite3_quota_glob PATTERN TEXT
1854**
1855** Test the glob pattern matching. Return 1 if TEXT matches PATTERN
1856** and return 0 if it does not.
1857*/
1858static int SQLITE_TCLAPI test_quota_glob(
1859 void * clientData,
1860 Tcl_Interp *interp,
1861 int objc,
1862 Tcl_Obj *CONST objv[]
1863){
1864 const char *zPattern; /* The glob pattern */
1865 const char *zText; /* Text to compare against the pattern */
1866 int rc;
1867 if( objc!=3 ){
1868 Tcl_WrongNumArgs(interp, 1, objv, "PATTERN TEXT");
1869 return TCL_ERROR;
1870 }
1871 zPattern = Tcl_GetString(objv[1]);
1872 zText = Tcl_GetString(objv[2]);
1873 rc = quotaStrglob(zPattern, zText);
1874 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
1875 return TCL_OK;
1876}
1877
1878/*
1879** tclcmd: sqlite3_quota_file_available HANDLE
1880**
1881** Return the number of bytes from the current file point to the end of
1882** the file.
1883*/
1884static int SQLITE_TCLAPI test_quota_file_available(
1885 void * clientData,
1886 Tcl_Interp *interp,
1887 int objc,
1888 Tcl_Obj *CONST objv[]
1889){
1890 quota_FILE *p;
1891 sqlite3_int64 x;
1892 if( objc!=2 ){
1893 Tcl_WrongNumArgs(interp, 1, objv, "HANDLE");
1894 return TCL_ERROR;
1895 }
1896 p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
1897 x = sqlite3_quota_file_available(p);
1898 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(x));
1899 return TCL_OK;
1900}
1901
1902/*
1903** tclcmd: sqlite3_quota_ferror HANDLE
1904**
1905** Return true if the file handle is in the error state.
1906*/
1907static int SQLITE_TCLAPI test_quota_ferror(
1908 void * clientData,
1909 Tcl_Interp *interp,
1910 int objc,
1911 Tcl_Obj *CONST objv[]
1912){
1913 quota_FILE *p;
1914 int x;
1915 if( objc!=2 ){
1916 Tcl_WrongNumArgs(interp, 1, objv, "HANDLE");
1917 return TCL_ERROR;
1918 }
1919 p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
1920 x = sqlite3_quota_ferror(p);
1921 Tcl_SetObjResult(interp, Tcl_NewIntObj(x));
1922 return TCL_OK;
1923}
1924
1925/*
1926** This routine registers the custom TCL commands defined in this
1927** module. This should be the only procedure visible from outside
1928** of this module.
1929*/
1930int Sqlitequota_Init(Tcl_Interp *interp){
1931 static struct {
1932 char *zName;
1933 Tcl_ObjCmdProc *xProc;
1934 } aCmd[] = {
1935 { "sqlite3_quota_initialize", test_quota_initialize },
1936 { "sqlite3_quota_shutdown", test_quota_shutdown },
1937 { "sqlite3_quota_set", test_quota_set },
1938 { "sqlite3_quota_file", test_quota_file },
1939 { "sqlite3_quota_dump", test_quota_dump },
1940 { "sqlite3_quota_fopen", test_quota_fopen },
1941 { "sqlite3_quota_fread", test_quota_fread },
1942 { "sqlite3_quota_fwrite", test_quota_fwrite },
1943 { "sqlite3_quota_fclose", test_quota_fclose },
1944 { "sqlite3_quota_fflush", test_quota_fflush },
1945 { "sqlite3_quota_fseek", test_quota_fseek },
1946 { "sqlite3_quota_rewind", test_quota_rewind },
1947 { "sqlite3_quota_ftell", test_quota_ftell },
1948 { "sqlite3_quota_ftruncate", test_quota_ftruncate },
1949 { "sqlite3_quota_file_size", test_quota_file_size },
1950 { "sqlite3_quota_file_truesize", test_quota_file_truesize },
1951 { "sqlite3_quota_file_mtime", test_quota_file_mtime },
1952 { "sqlite3_quota_remove", test_quota_remove },
1953 { "sqlite3_quota_glob", test_quota_glob },
1954 { "sqlite3_quota_file_available",test_quota_file_available },
1955 { "sqlite3_quota_ferror", test_quota_ferror },
1956 };
1957 int i;
1958
1959 for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
1960 Tcl_CreateObjCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0);
1961 }
1962
1963 return TCL_OK;
1964}
1965#endif