1748a52cdSdrh /* 2748a52cdSdrh ** 2010 September 31 3748a52cdSdrh ** 4748a52cdSdrh ** The author disclaims copyright to this source code. In place of 5748a52cdSdrh ** a legal notice, here is a blessing: 6748a52cdSdrh ** 7748a52cdSdrh ** May you do good and not evil. 8748a52cdSdrh ** May you find forgiveness for yourself and forgive others. 9748a52cdSdrh ** May you share freely, never taking more than you give. 10748a52cdSdrh ** 11748a52cdSdrh ************************************************************************* 12748a52cdSdrh ** 13748a52cdSdrh ** This file contains a VFS "shim" - a layer that sits in between the 14748a52cdSdrh ** pager and the real VFS. 15748a52cdSdrh ** 16748a52cdSdrh ** This particular shim enforces a quota system on files. One or more 17748a52cdSdrh ** database files are in a "quota group" that is defined by a GLOB 18748a52cdSdrh ** pattern. A quota is set for the combined size of all files in the 19748a52cdSdrh ** the group. A quota of zero means "no limit". If the total size 209237a258Sdrh ** of all files in the quota group is greater than the limit, then 219237a258Sdrh ** write requests that attempt to enlarge a file fail with SQLITE_FULL. 22748a52cdSdrh ** 23748a52cdSdrh ** However, before returning SQLITE_FULL, the write requests invoke 24748a52cdSdrh ** a callback function that is configurable for each quota group. 25748a52cdSdrh ** This callback has the opportunity to enlarge the quota. If the 26748a52cdSdrh ** callback does enlarge the quota such that the total size of all 27748a52cdSdrh ** files within the group is less than the new quota, then the write 28748a52cdSdrh ** continues as if nothing had happened. 29748a52cdSdrh */ 30*60f21e4bSdrh #include "test_quota.h" 318cf35eb4Sdan #include <string.h> 328cf35eb4Sdan #include <assert.h> 33748a52cdSdrh 343778ac15Sdrh /* 353778ac15Sdrh ** For an build without mutexes, no-op the mutex calls. 363778ac15Sdrh */ 373778ac15Sdrh #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE==0 383778ac15Sdrh #define sqlite3_mutex_alloc(X) ((sqlite3_mutex*)8) 393778ac15Sdrh #define sqlite3_mutex_free(X) 403778ac15Sdrh #define sqlite3_mutex_enter(X) 413778ac15Sdrh #define sqlite3_mutex_try(X) SQLITE_OK 423778ac15Sdrh #define sqlite3_mutex_leave(X) 433778ac15Sdrh #define sqlite3_mutex_held(X) ((void)(X),1) 443778ac15Sdrh #define sqlite3_mutex_notheld(X) ((void)(X),1) 453778ac15Sdrh #endif /* SQLITE_THREADSAFE==0 */ 463778ac15Sdrh 473778ac15Sdrh 48748a52cdSdrh /************************ Object Definitions ******************************/ 49748a52cdSdrh 50745de2e4Sdrh /* Forward declaration of all object types */ 51748a52cdSdrh typedef struct quotaGroup quotaGroup; 52ef9659baSdrh typedef struct quotaConn quotaConn; 53745de2e4Sdrh typedef struct quotaFile quotaFile; 54745de2e4Sdrh 558cf35eb4Sdan /* 56549dc1baSdrh ** A "quota group" is a collection of files whose collective size we want 57549dc1baSdrh ** to limit. Each quota group is defined by a GLOB pattern. 588cf35eb4Sdan ** 59549dc1baSdrh ** There is an instance of the following object for each defined quota 60549dc1baSdrh ** group. This object records the GLOB pattern that defines which files 61549dc1baSdrh ** belong to the quota group. The object also remembers the size limit 62549dc1baSdrh ** for the group (the quota) and the callback to be invoked when the 63549dc1baSdrh ** sum of the sizes of the files within the group goes over the limit. 648cf35eb4Sdan ** 65549dc1baSdrh ** A quota group must be established (using sqlite3_quota_set(...)) 66549dc1baSdrh ** prior to opening any of the database connections that access files 67549dc1baSdrh ** within the quota group. 688cf35eb4Sdan */ 69748a52cdSdrh struct quotaGroup { 70748a52cdSdrh const char *zPattern; /* Filename pattern to be quotaed */ 71748a52cdSdrh sqlite3_int64 iLimit; /* Upper bound on total file size */ 72748a52cdSdrh sqlite3_int64 iSize; /* Current size of all files */ 73748a52cdSdrh void (*xCallback)( /* Callback invoked when going over quota */ 74748a52cdSdrh const char *zFilename, /* Name of file whose size increases */ 75748a52cdSdrh sqlite3_int64 *piLimit, /* IN/OUT: The current limit */ 76748a52cdSdrh sqlite3_int64 iSize, /* Total size of all files in the group */ 77748a52cdSdrh void *pArg /* Client data */ 78748a52cdSdrh ); 79748a52cdSdrh void *pArg; /* Third argument to the xCallback() */ 80ef9659baSdrh void (*xDestroy)(void*); /* Optional destructor for pArg */ 81748a52cdSdrh quotaGroup *pNext, **ppPrev; /* Doubly linked list of all quota objects */ 8275c19174Sdrh quotaFile *pFiles; /* Files within this group */ 83748a52cdSdrh }; 84748a52cdSdrh 85748a52cdSdrh /* 86745de2e4Sdrh ** An instance of this structure represents a single file that is part 87745de2e4Sdrh ** of a quota group. A single file can be opened multiple times. In 88745de2e4Sdrh ** order keep multiple openings of the same file from causing the size 89549dc1baSdrh ** of the file to count against the quota multiple times, each file 90745de2e4Sdrh ** has a unique instance of this object and multiple open connections 91745de2e4Sdrh ** to the same file each point to a single instance of this object. 92748a52cdSdrh */ 93748a52cdSdrh struct quotaFile { 94745de2e4Sdrh char *zFilename; /* Name of this file */ 9575c19174Sdrh quotaGroup *pGroup; /* Quota group to which this file belongs */ 968cf35eb4Sdan sqlite3_int64 iSize; /* Current size of this file */ 97745de2e4Sdrh int nRef; /* Number of times this file is open */ 98796af156Sdrh int deleteOnClose; /* True to delete this file when it closes */ 99745de2e4Sdrh quotaFile *pNext, **ppPrev; /* Linked list of files in the same group */ 100745de2e4Sdrh }; 101745de2e4Sdrh 102745de2e4Sdrh /* 103745de2e4Sdrh ** An instance of the following object represents each open connection 104549dc1baSdrh ** to a file that participates in quota tracking. This object is a 105549dc1baSdrh ** subclass of sqlite3_file. The sqlite3_file object for the underlying 106549dc1baSdrh ** VFS is appended to this structure. 107745de2e4Sdrh */ 108ef9659baSdrh struct quotaConn { 109748a52cdSdrh sqlite3_file base; /* Base class - must be first */ 110745de2e4Sdrh quotaFile *pFile; /* The underlying file */ 111748a52cdSdrh /* The underlying VFS sqlite3_file is appended to this object */ 112748a52cdSdrh }; 113748a52cdSdrh 114*60f21e4bSdrh /* 115*60f21e4bSdrh ** An instance of the following object records the state of an 116*60f21e4bSdrh ** open file. This object is opaque to all users - the internal 117*60f21e4bSdrh ** structure is only visible to the functions below. 118*60f21e4bSdrh */ 119*60f21e4bSdrh struct quota_FILE { 120*60f21e4bSdrh FILE *f; /* Open stdio file pointer */ 121*60f21e4bSdrh sqlite3_int64 iOfst; /* Current offset into the file */ 122*60f21e4bSdrh quotaFile *pFile; /* The file record in the quota system */ 123*60f21e4bSdrh }; 124*60f21e4bSdrh 125*60f21e4bSdrh 126748a52cdSdrh /************************* Global Variables **********************************/ 127748a52cdSdrh /* 128748a52cdSdrh ** All global variables used by this file are containing within the following 129748a52cdSdrh ** gQuota structure. 130748a52cdSdrh */ 131748a52cdSdrh static struct { 132549dc1baSdrh /* The pOrigVfs is the real, original underlying VFS implementation. 133748a52cdSdrh ** Most operations pass-through to the real VFS. This value is read-only 134748a52cdSdrh ** during operation. It is only modified at start-time and thus does not 135748a52cdSdrh ** require a mutex. 136748a52cdSdrh */ 137748a52cdSdrh sqlite3_vfs *pOrigVfs; 138748a52cdSdrh 139748a52cdSdrh /* The sThisVfs is the VFS structure used by this shim. It is initialized 140748a52cdSdrh ** at start-time and thus does not require a mutex 141748a52cdSdrh */ 142748a52cdSdrh sqlite3_vfs sThisVfs; 143748a52cdSdrh 144748a52cdSdrh /* The sIoMethods defines the methods used by sqlite3_file objects 145748a52cdSdrh ** associated with this shim. It is initialized at start-time and does 146748a52cdSdrh ** not require a mutex. 147748a52cdSdrh ** 148748a52cdSdrh ** When the underlying VFS is called to open a file, it might return 149748a52cdSdrh ** either a version 1 or a version 2 sqlite3_file object. This shim 150748a52cdSdrh ** has to create a wrapper sqlite3_file of the same version. Hence 151748a52cdSdrh ** there are two I/O method structures, one for version 1 and the other 152748a52cdSdrh ** for version 2. 153748a52cdSdrh */ 154748a52cdSdrh sqlite3_io_methods sIoMethodsV1; 155748a52cdSdrh sqlite3_io_methods sIoMethodsV2; 156748a52cdSdrh 157748a52cdSdrh /* True when this shim as been initialized. 158748a52cdSdrh */ 159748a52cdSdrh int isInitialized; 160748a52cdSdrh 161748a52cdSdrh /* For run-time access any of the other global data structures in this 162748a52cdSdrh ** shim, the following mutex must be held. 163748a52cdSdrh */ 164748a52cdSdrh sqlite3_mutex *pMutex; 165748a52cdSdrh 166748a52cdSdrh /* List of quotaGroup objects. 167748a52cdSdrh */ 168748a52cdSdrh quotaGroup *pGroup; 169748a52cdSdrh 170748a52cdSdrh } gQuota; 171748a52cdSdrh 172748a52cdSdrh /************************* Utility Routines *********************************/ 173748a52cdSdrh /* 174748a52cdSdrh ** Acquire and release the mutex used to serialize access to the 175748a52cdSdrh ** list of quotaGroups. 176748a52cdSdrh */ 177748a52cdSdrh static void quotaEnter(void){ sqlite3_mutex_enter(gQuota.pMutex); } 178748a52cdSdrh static void quotaLeave(void){ sqlite3_mutex_leave(gQuota.pMutex); } 179748a52cdSdrh 180796af156Sdrh /* Count the number of open files in a quotaGroup 181796af156Sdrh */ 182796af156Sdrh static int quotaGroupOpenFileCount(quotaGroup *pGroup){ 183796af156Sdrh int N = 0; 184796af156Sdrh quotaFile *pFile = pGroup->pFiles; 185796af156Sdrh while( pFile ){ 186796af156Sdrh if( pFile->nRef ) N++; 187796af156Sdrh pFile = pFile->pNext; 188796af156Sdrh } 189796af156Sdrh return N; 190796af156Sdrh } 191796af156Sdrh 192796af156Sdrh /* Remove a file from a quota group. 193796af156Sdrh */ 194796af156Sdrh static void quotaRemoveFile(quotaFile *pFile){ 195796af156Sdrh quotaGroup *pGroup = pFile->pGroup; 196796af156Sdrh pGroup->iSize -= pFile->iSize; 197796af156Sdrh *pFile->ppPrev = pFile->pNext; 198796af156Sdrh if( pFile->pNext ) pFile->pNext->ppPrev = pFile->ppPrev; 199796af156Sdrh sqlite3_free(pFile); 200796af156Sdrh } 201796af156Sdrh 202796af156Sdrh /* Remove all files from a quota group. It is always the case that 203796af156Sdrh ** all files will be closed when this routine is called. 204796af156Sdrh */ 205796af156Sdrh static void quotaRemoveAllFiles(quotaGroup *pGroup){ 206796af156Sdrh while( pGroup->pFiles ){ 207796af156Sdrh assert( pGroup->pFiles->nRef==0 ); 208796af156Sdrh quotaRemoveFile(pGroup->pFiles); 209796af156Sdrh } 210796af156Sdrh } 211796af156Sdrh 212748a52cdSdrh 213748a52cdSdrh /* If the reference count and threshold for a quotaGroup are both 214748a52cdSdrh ** zero, then destroy the quotaGroup. 215748a52cdSdrh */ 21675c19174Sdrh static void quotaGroupDeref(quotaGroup *pGroup){ 217796af156Sdrh if( pGroup->iLimit==0 && quotaGroupOpenFileCount(pGroup)==0 ){ 218796af156Sdrh quotaRemoveAllFiles(pGroup); 2199fc7702cSdan *pGroup->ppPrev = pGroup->pNext; 22075c19174Sdrh if( pGroup->pNext ) pGroup->pNext->ppPrev = pGroup->ppPrev; 22175c19174Sdrh if( pGroup->xDestroy ) pGroup->xDestroy(pGroup->pArg); 22275c19174Sdrh sqlite3_free(pGroup); 223748a52cdSdrh } 224748a52cdSdrh } 225748a52cdSdrh 226748a52cdSdrh /* 227748a52cdSdrh ** Return TRUE if string z matches glob pattern zGlob. 228748a52cdSdrh ** 229748a52cdSdrh ** Globbing rules: 230748a52cdSdrh ** 231748a52cdSdrh ** '*' Matches any sequence of zero or more characters. 232748a52cdSdrh ** 233748a52cdSdrh ** '?' Matches exactly one character. 234748a52cdSdrh ** 235748a52cdSdrh ** [...] Matches one character from the enclosed list of 236748a52cdSdrh ** characters. 237748a52cdSdrh ** 238748a52cdSdrh ** [^...] Matches one character not in the enclosed list. 239748a52cdSdrh ** 240748a52cdSdrh */ 24161cc423aSdrh static int quotaStrglob(const char *zGlob, const char *z){ 242748a52cdSdrh int c, c2; 243748a52cdSdrh int invert; 244748a52cdSdrh int seen; 245748a52cdSdrh 246748a52cdSdrh while( (c = (*(zGlob++)))!=0 ){ 247748a52cdSdrh if( c=='*' ){ 248748a52cdSdrh while( (c=(*(zGlob++))) == '*' || c=='?' ){ 249748a52cdSdrh if( c=='?' && (*(z++))==0 ) return 0; 250748a52cdSdrh } 251748a52cdSdrh if( c==0 ){ 252748a52cdSdrh return 1; 253748a52cdSdrh }else if( c=='[' ){ 25461cc423aSdrh while( *z && quotaStrglob(zGlob-1,z)==0 ){ 255748a52cdSdrh z++; 256748a52cdSdrh } 257748a52cdSdrh return (*z)!=0; 258748a52cdSdrh } 259748a52cdSdrh while( (c2 = (*(z++)))!=0 ){ 260748a52cdSdrh while( c2!=c ){ 261748a52cdSdrh c2 = *(z++); 262748a52cdSdrh if( c2==0 ) return 0; 263748a52cdSdrh } 26461cc423aSdrh if( quotaStrglob(zGlob,z) ) return 1; 265748a52cdSdrh } 266748a52cdSdrh return 0; 267748a52cdSdrh }else if( c=='?' ){ 268748a52cdSdrh if( (*(z++))==0 ) return 0; 269748a52cdSdrh }else if( c=='[' ){ 270748a52cdSdrh int prior_c = 0; 271748a52cdSdrh seen = 0; 272748a52cdSdrh invert = 0; 273748a52cdSdrh c = *(z++); 274748a52cdSdrh if( c==0 ) return 0; 275748a52cdSdrh c2 = *(zGlob++); 276748a52cdSdrh if( c2=='^' ){ 277748a52cdSdrh invert = 1; 278748a52cdSdrh c2 = *(zGlob++); 279748a52cdSdrh } 280748a52cdSdrh if( c2==']' ){ 281748a52cdSdrh if( c==']' ) seen = 1; 282748a52cdSdrh c2 = *(zGlob++); 283748a52cdSdrh } 284748a52cdSdrh while( c2 && c2!=']' ){ 285748a52cdSdrh if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){ 286748a52cdSdrh c2 = *(zGlob++); 287748a52cdSdrh if( c>=prior_c && c<=c2 ) seen = 1; 288748a52cdSdrh prior_c = 0; 289748a52cdSdrh }else{ 290748a52cdSdrh if( c==c2 ){ 291748a52cdSdrh seen = 1; 292748a52cdSdrh } 293748a52cdSdrh prior_c = c2; 294748a52cdSdrh } 295748a52cdSdrh c2 = *(zGlob++); 296748a52cdSdrh } 297748a52cdSdrh if( c2==0 || (seen ^ invert)==0 ) return 0; 298748a52cdSdrh }else{ 299748a52cdSdrh if( c!=(*(z++)) ) return 0; 300748a52cdSdrh } 301748a52cdSdrh } 302748a52cdSdrh return *z==0; 303748a52cdSdrh } 304748a52cdSdrh 305748a52cdSdrh 306748a52cdSdrh /* Find a quotaGroup given the filename. 3078cf35eb4Sdan ** 308ef9659baSdrh ** Return a pointer to the quotaGroup object. Return NULL if not found. 309748a52cdSdrh */ 310748a52cdSdrh static quotaGroup *quotaGroupFind(const char *zFilename){ 311748a52cdSdrh quotaGroup *p; 31261cc423aSdrh for(p=gQuota.pGroup; p && quotaStrglob(p->zPattern, zFilename)==0; 31361cc423aSdrh p=p->pNext){} 314748a52cdSdrh return p; 315748a52cdSdrh } 316748a52cdSdrh 317ef9659baSdrh /* Translate an sqlite3_file* that is really a quotaConn* into 318549dc1baSdrh ** the sqlite3_file* for the underlying original VFS. 319748a52cdSdrh */ 320ef9659baSdrh static sqlite3_file *quotaSubOpen(sqlite3_file *pConn){ 321ef9659baSdrh quotaConn *p = (quotaConn*)pConn; 322748a52cdSdrh return (sqlite3_file*)&p[1]; 323748a52cdSdrh } 324748a52cdSdrh 325796af156Sdrh /* Find a file in a quota group and return a pointer to that file. 326796af156Sdrh ** Return NULL if the file is not in the group. 327796af156Sdrh */ 328*60f21e4bSdrh static quotaFile *quotaFindFile( 329*60f21e4bSdrh quotaGroup *pGroup, /* Group in which to look for the file */ 330*60f21e4bSdrh const char *zName, /* Full pathname of the file */ 331*60f21e4bSdrh int createFlag /* Try to create the file if not found */ 332*60f21e4bSdrh ){ 333796af156Sdrh quotaFile *pFile = pGroup->pFiles; 334796af156Sdrh while( pFile && strcmp(pFile->zFilename, zName)!=0 ){ 335796af156Sdrh pFile = pFile->pNext; 336796af156Sdrh } 337*60f21e4bSdrh if( pFile==0 && createFlag ){ 338*60f21e4bSdrh int nName = strlen(zName); 339*60f21e4bSdrh pFile = (quotaFile *)sqlite3_malloc( sizeof(*pFile) + nName + 1 ); 340*60f21e4bSdrh if( pFile ){ 341*60f21e4bSdrh memset(pFile, 0, sizeof(*pFile)); 342*60f21e4bSdrh pFile->zFilename = (char*)&pFile[1]; 343*60f21e4bSdrh memcpy(pFile->zFilename, zName, nName+1); 344*60f21e4bSdrh pFile->pNext = pGroup->pFiles; 345*60f21e4bSdrh if( pGroup->pFiles ) pGroup->pFiles->ppPrev = &pFile->pNext; 346*60f21e4bSdrh pFile->ppPrev = &pGroup->pFiles; 347*60f21e4bSdrh pGroup->pFiles = pFile; 348*60f21e4bSdrh pFile->pGroup = pGroup; 349*60f21e4bSdrh } 350*60f21e4bSdrh } 351796af156Sdrh return pFile; 352796af156Sdrh } 353796af156Sdrh 354748a52cdSdrh /************************* VFS Method Wrappers *****************************/ 355748a52cdSdrh /* 356748a52cdSdrh ** This is the xOpen method used for the "quota" VFS. 357748a52cdSdrh ** 358748a52cdSdrh ** Most of the work is done by the underlying original VFS. This method 359549dc1baSdrh ** simply links the new file into the appropriate quota group if it is a 360549dc1baSdrh ** file that needs to be tracked. 361748a52cdSdrh */ 362748a52cdSdrh static int quotaOpen( 363549dc1baSdrh sqlite3_vfs *pVfs, /* The quota VFS */ 364549dc1baSdrh const char *zName, /* Name of file to be opened */ 365ef9659baSdrh sqlite3_file *pConn, /* Fill in this file descriptor */ 366549dc1baSdrh int flags, /* Flags to control the opening */ 367549dc1baSdrh int *pOutFlags /* Flags showing results of opening */ 368748a52cdSdrh ){ 369549dc1baSdrh int rc; /* Result code */ 370ef9659baSdrh quotaConn *pQuotaOpen; /* The new quota file descriptor */ 371549dc1baSdrh quotaFile *pFile; /* Corresponding quotaFile obj */ 372549dc1baSdrh quotaGroup *pGroup; /* The group file belongs to */ 373549dc1baSdrh sqlite3_file *pSubOpen; /* Real file descriptor */ 374549dc1baSdrh sqlite3_vfs *pOrigVfs = gQuota.pOrigVfs; /* Real VFS */ 375748a52cdSdrh 376748a52cdSdrh /* If the file is not a main database file or a WAL, then use the 377748a52cdSdrh ** normal xOpen method. 378748a52cdSdrh */ 3798cf35eb4Sdan if( (flags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_WAL))==0 ){ 380ef9659baSdrh return pOrigVfs->xOpen(pOrigVfs, zName, pConn, flags, pOutFlags); 381748a52cdSdrh } 382748a52cdSdrh 383748a52cdSdrh /* If the name of the file does not match any quota group, then 384748a52cdSdrh ** use the normal xOpen method. 385748a52cdSdrh */ 386748a52cdSdrh quotaEnter(); 3878cf35eb4Sdan pGroup = quotaGroupFind(zName); 388748a52cdSdrh if( pGroup==0 ){ 389ef9659baSdrh rc = pOrigVfs->xOpen(pOrigVfs, zName, pConn, flags, pOutFlags); 390748a52cdSdrh }else{ 3918cf35eb4Sdan /* If we get to this point, it means the file needs to be quota tracked. 3928cf35eb4Sdan */ 393ef9659baSdrh pQuotaOpen = (quotaConn*)pConn; 394ef9659baSdrh pSubOpen = quotaSubOpen(pConn); 395745de2e4Sdrh rc = pOrigVfs->xOpen(pOrigVfs, zName, pSubOpen, flags, pOutFlags); 3968cf35eb4Sdan if( rc==SQLITE_OK ){ 397*60f21e4bSdrh pFile = quotaFindFile(pGroup, zName, 1); 398745de2e4Sdrh if( pFile==0 ){ 399745de2e4Sdrh quotaLeave(); 400745de2e4Sdrh pSubOpen->pMethods->xClose(pSubOpen); 401745de2e4Sdrh return SQLITE_NOMEM; 402745de2e4Sdrh } 403796af156Sdrh pFile->deleteOnClose = (flags & SQLITE_OPEN_DELETEONCLOSE)!=0; 404745de2e4Sdrh pFile->nRef++; 405745de2e4Sdrh pQuotaOpen->pFile = pFile; 406745de2e4Sdrh if( pSubOpen->pMethods->iVersion==1 ){ 407745de2e4Sdrh pQuotaOpen->base.pMethods = &gQuota.sIoMethodsV1; 4088cf35eb4Sdan }else{ 409745de2e4Sdrh pQuotaOpen->base.pMethods = &gQuota.sIoMethodsV2; 410748a52cdSdrh } 411748a52cdSdrh } 4128cf35eb4Sdan } 4138cf35eb4Sdan quotaLeave(); 414748a52cdSdrh return rc; 415748a52cdSdrh } 416748a52cdSdrh 417796af156Sdrh /* 418796af156Sdrh ** This is the xDelete method used for the "quota" VFS. 419796af156Sdrh ** 420796af156Sdrh ** If the file being deleted is part of the quota group, then reduce 421796af156Sdrh ** the size of the quota group accordingly. And remove the file from 422796af156Sdrh ** the set of files in the quota group. 423796af156Sdrh */ 424796af156Sdrh static int quotaDelete( 425796af156Sdrh sqlite3_vfs *pVfs, /* The quota VFS */ 426796af156Sdrh const char *zName, /* Name of file to be deleted */ 427796af156Sdrh int syncDir /* Do a directory sync after deleting */ 428796af156Sdrh ){ 429796af156Sdrh int rc; /* Result code */ 430796af156Sdrh quotaFile *pFile; /* Files in the quota */ 431796af156Sdrh quotaGroup *pGroup; /* The group file belongs to */ 432796af156Sdrh sqlite3_vfs *pOrigVfs = gQuota.pOrigVfs; /* Real VFS */ 433796af156Sdrh 434796af156Sdrh /* Do the actual file delete */ 435796af156Sdrh rc = pOrigVfs->xDelete(pOrigVfs, zName, syncDir); 436796af156Sdrh 437796af156Sdrh /* If the file just deleted is a member of a quota group, then remove 438796af156Sdrh ** it from that quota group. 439796af156Sdrh */ 440796af156Sdrh if( rc==SQLITE_OK ){ 441796af156Sdrh quotaEnter(); 442796af156Sdrh pGroup = quotaGroupFind(zName); 443796af156Sdrh if( pGroup ){ 444*60f21e4bSdrh pFile = quotaFindFile(pGroup, zName, 0); 445796af156Sdrh if( pFile ){ 446796af156Sdrh if( pFile->nRef ){ 447796af156Sdrh pFile->deleteOnClose = 1; 448796af156Sdrh }else{ 449796af156Sdrh quotaRemoveFile(pFile); 450796af156Sdrh quotaGroupDeref(pGroup); 451796af156Sdrh } 452796af156Sdrh } 453796af156Sdrh } 454796af156Sdrh quotaLeave(); 455796af156Sdrh } 456796af156Sdrh return rc; 457796af156Sdrh } 458796af156Sdrh 459796af156Sdrh 460748a52cdSdrh /************************ I/O Method Wrappers *******************************/ 461748a52cdSdrh 462748a52cdSdrh /* xClose requests get passed through to the original VFS. But we 463ef9659baSdrh ** also have to unlink the quotaConn from the quotaFile and quotaGroup. 464ef9659baSdrh ** The quotaFile and/or quotaGroup are freed if they are no longer in use. 465748a52cdSdrh */ 466ef9659baSdrh static int quotaClose(sqlite3_file *pConn){ 467ef9659baSdrh quotaConn *p = (quotaConn*)pConn; 468745de2e4Sdrh quotaFile *pFile = p->pFile; 469ef9659baSdrh sqlite3_file *pSubOpen = quotaSubOpen(pConn); 470748a52cdSdrh int rc; 471745de2e4Sdrh rc = pSubOpen->pMethods->xClose(pSubOpen); 472748a52cdSdrh quotaEnter(); 473745de2e4Sdrh pFile->nRef--; 474745de2e4Sdrh if( pFile->nRef==0 ){ 475745de2e4Sdrh quotaGroup *pGroup = pFile->pGroup; 476796af156Sdrh if( pFile->deleteOnClose ) quotaRemoveFile(pFile); 477748a52cdSdrh quotaGroupDeref(pGroup); 478745de2e4Sdrh } 479748a52cdSdrh quotaLeave(); 480748a52cdSdrh return rc; 481748a52cdSdrh } 482748a52cdSdrh 483748a52cdSdrh /* Pass xRead requests directory thru to the original VFS without 484748a52cdSdrh ** further processing. 485748a52cdSdrh */ 486748a52cdSdrh static int quotaRead( 487ef9659baSdrh sqlite3_file *pConn, 488748a52cdSdrh void *pBuf, 489748a52cdSdrh int iAmt, 490748a52cdSdrh sqlite3_int64 iOfst 491748a52cdSdrh ){ 492ef9659baSdrh sqlite3_file *pSubOpen = quotaSubOpen(pConn); 493745de2e4Sdrh return pSubOpen->pMethods->xRead(pSubOpen, pBuf, iAmt, iOfst); 494748a52cdSdrh } 495748a52cdSdrh 496748a52cdSdrh /* Check xWrite requests to see if they expand the file. If they do, 497748a52cdSdrh ** the perform a quota check before passing them through to the 498748a52cdSdrh ** original VFS. 499748a52cdSdrh */ 500748a52cdSdrh static int quotaWrite( 501ef9659baSdrh sqlite3_file *pConn, 5028cf35eb4Sdan const void *pBuf, 503748a52cdSdrh int iAmt, 504748a52cdSdrh sqlite3_int64 iOfst 505748a52cdSdrh ){ 506ef9659baSdrh quotaConn *p = (quotaConn*)pConn; 507ef9659baSdrh sqlite3_file *pSubOpen = quotaSubOpen(pConn); 508748a52cdSdrh sqlite3_int64 iEnd = iOfst+iAmt; 509748a52cdSdrh quotaGroup *pGroup; 510745de2e4Sdrh quotaFile *pFile = p->pFile; 511748a52cdSdrh sqlite3_int64 szNew; 512748a52cdSdrh 513745de2e4Sdrh if( pFile->iSize<iEnd ){ 514745de2e4Sdrh pGroup = pFile->pGroup; 515748a52cdSdrh quotaEnter(); 516745de2e4Sdrh szNew = pGroup->iSize - pFile->iSize + iEnd; 5178cf35eb4Sdan if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){ 518748a52cdSdrh if( pGroup->xCallback ){ 519745de2e4Sdrh pGroup->xCallback(pFile->zFilename, &pGroup->iLimit, szNew, 520748a52cdSdrh pGroup->pArg); 521748a52cdSdrh } 5228cf35eb4Sdan if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){ 523748a52cdSdrh quotaLeave(); 524748a52cdSdrh return SQLITE_FULL; 525748a52cdSdrh } 526748a52cdSdrh } 527748a52cdSdrh pGroup->iSize = szNew; 528745de2e4Sdrh pFile->iSize = iEnd; 529748a52cdSdrh quotaLeave(); 530748a52cdSdrh } 531745de2e4Sdrh return pSubOpen->pMethods->xWrite(pSubOpen, pBuf, iAmt, iOfst); 532748a52cdSdrh } 533748a52cdSdrh 534748a52cdSdrh /* Pass xTruncate requests thru to the original VFS. If the 535748a52cdSdrh ** success, update the file size. 536748a52cdSdrh */ 537ef9659baSdrh static int quotaTruncate(sqlite3_file *pConn, sqlite3_int64 size){ 538ef9659baSdrh quotaConn *p = (quotaConn*)pConn; 539ef9659baSdrh sqlite3_file *pSubOpen = quotaSubOpen(pConn); 540745de2e4Sdrh int rc = pSubOpen->pMethods->xTruncate(pSubOpen, size); 541745de2e4Sdrh quotaFile *pFile = p->pFile; 542745de2e4Sdrh quotaGroup *pGroup; 543748a52cdSdrh if( rc==SQLITE_OK ){ 544748a52cdSdrh quotaEnter(); 545745de2e4Sdrh pGroup = pFile->pGroup; 546745de2e4Sdrh pGroup->iSize -= pFile->iSize; 547745de2e4Sdrh pFile->iSize = size; 548748a52cdSdrh pGroup->iSize += size; 549748a52cdSdrh quotaLeave(); 550748a52cdSdrh } 551748a52cdSdrh return rc; 552748a52cdSdrh } 553748a52cdSdrh 554748a52cdSdrh /* Pass xSync requests through to the original VFS without change 555748a52cdSdrh */ 556ef9659baSdrh static int quotaSync(sqlite3_file *pConn, int flags){ 557ef9659baSdrh sqlite3_file *pSubOpen = quotaSubOpen(pConn); 558745de2e4Sdrh return pSubOpen->pMethods->xSync(pSubOpen, flags); 559748a52cdSdrh } 560748a52cdSdrh 561748a52cdSdrh /* Pass xFileSize requests through to the original VFS but then 562748a52cdSdrh ** update the quotaGroup with the new size before returning. 563748a52cdSdrh */ 564ef9659baSdrh static int quotaFileSize(sqlite3_file *pConn, sqlite3_int64 *pSize){ 565ef9659baSdrh quotaConn *p = (quotaConn*)pConn; 566ef9659baSdrh sqlite3_file *pSubOpen = quotaSubOpen(pConn); 567745de2e4Sdrh quotaFile *pFile = p->pFile; 568748a52cdSdrh quotaGroup *pGroup; 569748a52cdSdrh sqlite3_int64 sz; 570748a52cdSdrh int rc; 571748a52cdSdrh 572745de2e4Sdrh rc = pSubOpen->pMethods->xFileSize(pSubOpen, &sz); 573748a52cdSdrh if( rc==SQLITE_OK ){ 574748a52cdSdrh quotaEnter(); 575745de2e4Sdrh pGroup = pFile->pGroup; 576745de2e4Sdrh pGroup->iSize -= pFile->iSize; 577745de2e4Sdrh pFile->iSize = sz; 578748a52cdSdrh pGroup->iSize += sz; 579748a52cdSdrh quotaLeave(); 580748a52cdSdrh *pSize = sz; 581748a52cdSdrh } 582748a52cdSdrh return rc; 583748a52cdSdrh } 584748a52cdSdrh 585748a52cdSdrh /* Pass xLock requests through to the original VFS unchanged. 586748a52cdSdrh */ 587ef9659baSdrh static int quotaLock(sqlite3_file *pConn, int lock){ 588ef9659baSdrh sqlite3_file *pSubOpen = quotaSubOpen(pConn); 589745de2e4Sdrh return pSubOpen->pMethods->xLock(pSubOpen, lock); 590748a52cdSdrh } 591748a52cdSdrh 592748a52cdSdrh /* Pass xUnlock requests through to the original VFS unchanged. 593748a52cdSdrh */ 594ef9659baSdrh static int quotaUnlock(sqlite3_file *pConn, int lock){ 595ef9659baSdrh sqlite3_file *pSubOpen = quotaSubOpen(pConn); 596745de2e4Sdrh return pSubOpen->pMethods->xUnlock(pSubOpen, lock); 597748a52cdSdrh } 598748a52cdSdrh 599748a52cdSdrh /* Pass xCheckReservedLock requests through to the original VFS unchanged. 600748a52cdSdrh */ 601ef9659baSdrh static int quotaCheckReservedLock(sqlite3_file *pConn, int *pResOut){ 602ef9659baSdrh sqlite3_file *pSubOpen = quotaSubOpen(pConn); 603745de2e4Sdrh return pSubOpen->pMethods->xCheckReservedLock(pSubOpen, pResOut); 604748a52cdSdrh } 605748a52cdSdrh 606748a52cdSdrh /* Pass xFileControl requests through to the original VFS unchanged. 607748a52cdSdrh */ 608ef9659baSdrh static int quotaFileControl(sqlite3_file *pConn, int op, void *pArg){ 609ef9659baSdrh sqlite3_file *pSubOpen = quotaSubOpen(pConn); 610745de2e4Sdrh return pSubOpen->pMethods->xFileControl(pSubOpen, op, pArg); 611748a52cdSdrh } 612748a52cdSdrh 613748a52cdSdrh /* Pass xSectorSize requests through to the original VFS unchanged. 614748a52cdSdrh */ 615ef9659baSdrh static int quotaSectorSize(sqlite3_file *pConn){ 616ef9659baSdrh sqlite3_file *pSubOpen = quotaSubOpen(pConn); 617745de2e4Sdrh return pSubOpen->pMethods->xSectorSize(pSubOpen); 618748a52cdSdrh } 619748a52cdSdrh 620748a52cdSdrh /* Pass xDeviceCharacteristics requests through to the original VFS unchanged. 621748a52cdSdrh */ 622ef9659baSdrh static int quotaDeviceCharacteristics(sqlite3_file *pConn){ 623ef9659baSdrh sqlite3_file *pSubOpen = quotaSubOpen(pConn); 624745de2e4Sdrh return pSubOpen->pMethods->xDeviceCharacteristics(pSubOpen); 625748a52cdSdrh } 626748a52cdSdrh 627748a52cdSdrh /* Pass xShmMap requests through to the original VFS unchanged. 628748a52cdSdrh */ 629748a52cdSdrh static int quotaShmMap( 630ef9659baSdrh sqlite3_file *pConn, /* Handle open on database file */ 631748a52cdSdrh int iRegion, /* Region to retrieve */ 632748a52cdSdrh int szRegion, /* Size of regions */ 633748a52cdSdrh int bExtend, /* True to extend file if necessary */ 634748a52cdSdrh void volatile **pp /* OUT: Mapped memory */ 635748a52cdSdrh ){ 636ef9659baSdrh sqlite3_file *pSubOpen = quotaSubOpen(pConn); 637745de2e4Sdrh return pSubOpen->pMethods->xShmMap(pSubOpen, iRegion, szRegion, bExtend, pp); 638748a52cdSdrh } 639748a52cdSdrh 640748a52cdSdrh /* Pass xShmLock requests through to the original VFS unchanged. 641748a52cdSdrh */ 642748a52cdSdrh static int quotaShmLock( 643ef9659baSdrh sqlite3_file *pConn, /* Database file holding the shared memory */ 644748a52cdSdrh int ofst, /* First lock to acquire or release */ 645748a52cdSdrh int n, /* Number of locks to acquire or release */ 646748a52cdSdrh int flags /* What to do with the lock */ 647748a52cdSdrh ){ 648ef9659baSdrh sqlite3_file *pSubOpen = quotaSubOpen(pConn); 649745de2e4Sdrh return pSubOpen->pMethods->xShmLock(pSubOpen, ofst, n, flags); 650748a52cdSdrh } 651748a52cdSdrh 652748a52cdSdrh /* Pass xShmBarrier requests through to the original VFS unchanged. 653748a52cdSdrh */ 654ef9659baSdrh static void quotaShmBarrier(sqlite3_file *pConn){ 655ef9659baSdrh sqlite3_file *pSubOpen = quotaSubOpen(pConn); 656745de2e4Sdrh pSubOpen->pMethods->xShmBarrier(pSubOpen); 657748a52cdSdrh } 658748a52cdSdrh 659748a52cdSdrh /* Pass xShmUnmap requests through to the original VFS unchanged. 660748a52cdSdrh */ 661ef9659baSdrh static int quotaShmUnmap(sqlite3_file *pConn, int deleteFlag){ 662ef9659baSdrh sqlite3_file *pSubOpen = quotaSubOpen(pConn); 663745de2e4Sdrh return pSubOpen->pMethods->xShmUnmap(pSubOpen, deleteFlag); 664748a52cdSdrh } 665748a52cdSdrh 666748a52cdSdrh /************************** Public Interfaces *****************************/ 667748a52cdSdrh /* 668748a52cdSdrh ** Initialize the quota VFS shim. Use the VFS named zOrigVfsName 669748a52cdSdrh ** as the VFS that does the actual work. Use the default if 670748a52cdSdrh ** zOrigVfsName==NULL. 671748a52cdSdrh ** 672748a52cdSdrh ** The quota VFS shim is named "quota". It will become the default 673748a52cdSdrh ** VFS if makeDefault is non-zero. 674748a52cdSdrh ** 675748a52cdSdrh ** THIS ROUTINE IS NOT THREADSAFE. Call this routine exactly once 676748a52cdSdrh ** during start-up. 677748a52cdSdrh */ 678748a52cdSdrh int sqlite3_quota_initialize(const char *zOrigVfsName, int makeDefault){ 679748a52cdSdrh sqlite3_vfs *pOrigVfs; 6808cf35eb4Sdan if( gQuota.isInitialized ) return SQLITE_MISUSE; 681748a52cdSdrh pOrigVfs = sqlite3_vfs_find(zOrigVfsName); 682748a52cdSdrh if( pOrigVfs==0 ) return SQLITE_ERROR; 6838cf35eb4Sdan assert( pOrigVfs!=&gQuota.sThisVfs ); 6848cf35eb4Sdan gQuota.pMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); 6858cf35eb4Sdan if( !gQuota.pMutex ){ 6868cf35eb4Sdan return SQLITE_NOMEM; 6878cf35eb4Sdan } 6888cf35eb4Sdan gQuota.isInitialized = 1; 689748a52cdSdrh gQuota.pOrigVfs = pOrigVfs; 690748a52cdSdrh gQuota.sThisVfs = *pOrigVfs; 691748a52cdSdrh gQuota.sThisVfs.xOpen = quotaOpen; 692796af156Sdrh gQuota.sThisVfs.xDelete = quotaDelete; 693ef9659baSdrh gQuota.sThisVfs.szOsFile += sizeof(quotaConn); 694748a52cdSdrh gQuota.sThisVfs.zName = "quota"; 695748a52cdSdrh gQuota.sIoMethodsV1.iVersion = 1; 696748a52cdSdrh gQuota.sIoMethodsV1.xClose = quotaClose; 697748a52cdSdrh gQuota.sIoMethodsV1.xRead = quotaRead; 698748a52cdSdrh gQuota.sIoMethodsV1.xWrite = quotaWrite; 699748a52cdSdrh gQuota.sIoMethodsV1.xTruncate = quotaTruncate; 700748a52cdSdrh gQuota.sIoMethodsV1.xSync = quotaSync; 701748a52cdSdrh gQuota.sIoMethodsV1.xFileSize = quotaFileSize; 702748a52cdSdrh gQuota.sIoMethodsV1.xLock = quotaLock; 703748a52cdSdrh gQuota.sIoMethodsV1.xUnlock = quotaUnlock; 704748a52cdSdrh gQuota.sIoMethodsV1.xCheckReservedLock = quotaCheckReservedLock; 705748a52cdSdrh gQuota.sIoMethodsV1.xFileControl = quotaFileControl; 7068cf35eb4Sdan gQuota.sIoMethodsV1.xSectorSize = quotaSectorSize; 707748a52cdSdrh gQuota.sIoMethodsV1.xDeviceCharacteristics = quotaDeviceCharacteristics; 708748a52cdSdrh gQuota.sIoMethodsV2 = gQuota.sIoMethodsV1; 709748a52cdSdrh gQuota.sIoMethodsV2.iVersion = 2; 710748a52cdSdrh gQuota.sIoMethodsV2.xShmMap = quotaShmMap; 711748a52cdSdrh gQuota.sIoMethodsV2.xShmLock = quotaShmLock; 712748a52cdSdrh gQuota.sIoMethodsV2.xShmBarrier = quotaShmBarrier; 713748a52cdSdrh gQuota.sIoMethodsV2.xShmUnmap = quotaShmUnmap; 7148cf35eb4Sdan sqlite3_vfs_register(&gQuota.sThisVfs, makeDefault); 7158cf35eb4Sdan return SQLITE_OK; 716748a52cdSdrh } 717748a52cdSdrh 718748a52cdSdrh /* 719748a52cdSdrh ** Shutdown the quota system. 720748a52cdSdrh ** 721748a52cdSdrh ** All SQLite database connections must be closed before calling this 722748a52cdSdrh ** routine. 723748a52cdSdrh ** 724796af156Sdrh ** THIS ROUTINE IS NOT THREADSAFE. Call this routine exactly once while 725748a52cdSdrh ** shutting down in order to free all remaining quota groups. 726748a52cdSdrh */ 727748a52cdSdrh int sqlite3_quota_shutdown(void){ 72875c19174Sdrh quotaGroup *pGroup; 729748a52cdSdrh if( gQuota.isInitialized==0 ) return SQLITE_MISUSE; 73075c19174Sdrh for(pGroup=gQuota.pGroup; pGroup; pGroup=pGroup->pNext){ 731796af156Sdrh if( quotaGroupOpenFileCount(pGroup)>0 ) return SQLITE_MISUSE; 7328cf35eb4Sdan } 7338cf35eb4Sdan while( gQuota.pGroup ){ 73475c19174Sdrh pGroup = gQuota.pGroup; 73575c19174Sdrh gQuota.pGroup = pGroup->pNext; 7369fc7702cSdan pGroup->iLimit = 0; 737796af156Sdrh assert( quotaGroupOpenFileCount(pGroup)==0 ); 7389fc7702cSdan quotaGroupDeref(pGroup); 7398cf35eb4Sdan } 740748a52cdSdrh gQuota.isInitialized = 0; 741748a52cdSdrh sqlite3_mutex_free(gQuota.pMutex); 7428cf35eb4Sdan sqlite3_vfs_unregister(&gQuota.sThisVfs); 743748a52cdSdrh memset(&gQuota, 0, sizeof(gQuota)); 7448cf35eb4Sdan return SQLITE_OK; 745748a52cdSdrh } 746748a52cdSdrh 747748a52cdSdrh /* 748748a52cdSdrh ** Create or destroy a quota group. 749748a52cdSdrh ** 750748a52cdSdrh ** The quota group is defined by the zPattern. When calling this routine 751748a52cdSdrh ** with a zPattern for a quota group that already exists, this routine 752748a52cdSdrh ** merely updates the iLimit, xCallback, and pArg values for that quota 753748a52cdSdrh ** group. If zPattern is new, then a new quota group is created. 754748a52cdSdrh ** 755748a52cdSdrh ** If the iLimit for a quota group is set to zero, then the quota group 756748a52cdSdrh ** is disabled and will be deleted when the last database connection using 757748a52cdSdrh ** the quota group is closed. 758748a52cdSdrh ** 759748a52cdSdrh ** Calling this routine on a zPattern that does not exist and with a 760748a52cdSdrh ** zero iLimit is a no-op. 761748a52cdSdrh ** 762748a52cdSdrh ** A quota group must exist with a non-zero iLimit prior to opening 763748a52cdSdrh ** database connections if those connections are to participate in the 7648cf35eb4Sdan ** quota group. Creating a quota group does not affect database connections 765748a52cdSdrh ** that are already open. 766748a52cdSdrh */ 767748a52cdSdrh int sqlite3_quota_set( 768748a52cdSdrh const char *zPattern, /* The filename pattern */ 769748a52cdSdrh sqlite3_int64 iLimit, /* New quota to set for this quota group */ 770748a52cdSdrh void (*xCallback)( /* Callback invoked when going over quota */ 771748a52cdSdrh const char *zFilename, /* Name of file whose size increases */ 772748a52cdSdrh sqlite3_int64 *piLimit, /* IN/OUT: The current limit */ 773748a52cdSdrh sqlite3_int64 iSize, /* Total size of all files in the group */ 774748a52cdSdrh void *pArg /* Client data */ 7758cf35eb4Sdan ), 776ef9659baSdrh void *pArg, /* client data passed thru to callback */ 777ef9659baSdrh void (*xDestroy)(void*) /* Optional destructor for pArg */ 778748a52cdSdrh ){ 779748a52cdSdrh quotaGroup *pGroup; 780748a52cdSdrh quotaEnter(); 781748a52cdSdrh pGroup = gQuota.pGroup; 782748a52cdSdrh while( pGroup && strcmp(pGroup->zPattern, zPattern)!=0 ){ 783748a52cdSdrh pGroup = pGroup->pNext; 784748a52cdSdrh } 78561cc423aSdrh if( pGroup==0 ){ 786748a52cdSdrh int nPattern = strlen(zPattern); 78761cc423aSdrh if( iLimit<=0 ){ 78861cc423aSdrh quotaLeave(); 78961cc423aSdrh return SQLITE_OK; 79061cc423aSdrh } 791b0a32832Sdan pGroup = (quotaGroup *)sqlite3_malloc( sizeof(*pGroup) + nPattern + 1 ); 792748a52cdSdrh if( pGroup==0 ){ 793748a52cdSdrh quotaLeave(); 794748a52cdSdrh return SQLITE_NOMEM; 795748a52cdSdrh } 796748a52cdSdrh memset(pGroup, 0, sizeof(*pGroup)); 797748a52cdSdrh pGroup->zPattern = (char*)&pGroup[1]; 7988cf35eb4Sdan memcpy((char *)pGroup->zPattern, zPattern, nPattern+1); 799748a52cdSdrh if( gQuota.pGroup ) gQuota.pGroup->ppPrev = &pGroup->pNext; 8008cf35eb4Sdan pGroup->pNext = gQuota.pGroup; 8018cf35eb4Sdan pGroup->ppPrev = &gQuota.pGroup; 8028cf35eb4Sdan gQuota.pGroup = pGroup; 803748a52cdSdrh } 804748a52cdSdrh pGroup->iLimit = iLimit; 805748a52cdSdrh pGroup->xCallback = xCallback; 806ef9659baSdrh if( pGroup->xDestroy && pGroup->pArg!=pArg ){ 807ef9659baSdrh pGroup->xDestroy(pGroup->pArg); 808ef9659baSdrh } 809748a52cdSdrh pGroup->pArg = pArg; 810ef9659baSdrh pGroup->xDestroy = xDestroy; 811748a52cdSdrh quotaGroupDeref(pGroup); 812748a52cdSdrh quotaLeave(); 813748a52cdSdrh return SQLITE_OK; 814748a52cdSdrh } 815748a52cdSdrh 816b6020c4eSdrh /* 817b6020c4eSdrh ** Bring the named file under quota management. Or if it is already under 818b6020c4eSdrh ** management, update its size. 819b6020c4eSdrh */ 820b6020c4eSdrh int sqlite3_quota_file(const char *zFilename){ 821b6020c4eSdrh char *zFull; 822b6020c4eSdrh sqlite3_file *fd; 823b6020c4eSdrh int rc; 824b6020c4eSdrh int outFlags = 0; 825b6020c4eSdrh sqlite3_int64 iSize; 826b6020c4eSdrh fd = sqlite3_malloc(gQuota.sThisVfs.szOsFile + gQuota.sThisVfs.mxPathname+1); 827b6020c4eSdrh if( fd==0 ) return SQLITE_NOMEM; 828b6020c4eSdrh zFull = gQuota.sThisVfs.szOsFile + (char*)fd; 829b6020c4eSdrh rc = gQuota.pOrigVfs->xFullPathname(gQuota.pOrigVfs, zFilename, 830b6020c4eSdrh gQuota.sThisVfs.mxPathname+1, zFull); 831b6020c4eSdrh if( rc==SQLITE_OK ){ 832b6020c4eSdrh rc = quotaOpen(&gQuota.sThisVfs, zFull, fd, 833b6020c4eSdrh SQLITE_OPEN_READONLY | SQLITE_OPEN_MAIN_DB, &outFlags); 834b6020c4eSdrh } 835b6020c4eSdrh if( rc==SQLITE_OK ){ 83626541c9aSdrh fd->pMethods->xFileSize(fd, &iSize); 83726541c9aSdrh fd->pMethods->xClose(fd); 838b6020c4eSdrh }else if( rc==SQLITE_CANTOPEN ){ 839b6020c4eSdrh quotaGroup *pGroup; 840b6020c4eSdrh quotaFile *pFile; 841b6020c4eSdrh quotaEnter(); 842b6020c4eSdrh pGroup = quotaGroupFind(zFull); 843b6020c4eSdrh if( pGroup ){ 844*60f21e4bSdrh pFile = quotaFindFile(pGroup, zFull, 0); 845b6020c4eSdrh if( pFile ) quotaRemoveFile(pFile); 846b6020c4eSdrh } 847b6020c4eSdrh quotaLeave(); 848b6020c4eSdrh } 849b6020c4eSdrh sqlite3_free(fd); 850b6020c4eSdrh return rc; 851b6020c4eSdrh } 852b6020c4eSdrh 853*60f21e4bSdrh /* 854*60f21e4bSdrh ** Open a potentially quotaed file for I/O. 855*60f21e4bSdrh */ 856*60f21e4bSdrh quota_FILE *sqlite3_quota_fopen(const char *zFilename, const char *zMode){ 857*60f21e4bSdrh quota_FILE *p = 0; 858*60f21e4bSdrh char *zFull = 0; 859*60f21e4bSdrh int rc; 860*60f21e4bSdrh quotaGroup *pGroup; 861*60f21e4bSdrh quotaFile *pFile; 862*60f21e4bSdrh 863*60f21e4bSdrh p = sqlite3_malloc(gQuota.sThisVfs.mxPathname + 1); 864*60f21e4bSdrh if( p==0 ) return 0; 865*60f21e4bSdrh zFull = (char*)&p[1]; 866*60f21e4bSdrh rc = gQuota.pOrigVfs->xFullPathname(gQuota.pOrigVfs, zFilename, 867*60f21e4bSdrh gQuota.sThisVfs.mxPathname+1, zFull); 868*60f21e4bSdrh if( rc ) goto quota_fopen_error; 869*60f21e4bSdrh p = sqlite3_malloc(sizeof(*p)); 870*60f21e4bSdrh if( p==0 ) goto quota_fopen_error; 871*60f21e4bSdrh memset(p, 0, sizeof(*p)); 872*60f21e4bSdrh p->f = fopen(zFull, zMode); 873*60f21e4bSdrh if( p->f==0 ) goto quota_fopen_error; 874*60f21e4bSdrh quotaEnter(); 875*60f21e4bSdrh pGroup = quotaGroupFind(zFull); 876*60f21e4bSdrh if( pGroup ){ 877*60f21e4bSdrh pFile = quotaFindFile(pGroup, zFull, 1); 878*60f21e4bSdrh if( pFile==0 ){ 879*60f21e4bSdrh quotaLeave(); 880*60f21e4bSdrh goto quota_fopen_error; 881*60f21e4bSdrh } 882*60f21e4bSdrh pFile->nRef++; 883*60f21e4bSdrh p->pFile = pFile; 884*60f21e4bSdrh } 885*60f21e4bSdrh quotaLeave(); 886*60f21e4bSdrh sqlite3_free(zFull); 887*60f21e4bSdrh return p; 888*60f21e4bSdrh 889*60f21e4bSdrh quota_fopen_error: 890*60f21e4bSdrh sqlite3_free(zFull); 891*60f21e4bSdrh if( p && p->f ) fclose(p->f); 892*60f21e4bSdrh sqlite3_free(p); 893*60f21e4bSdrh return 0; 894*60f21e4bSdrh } 895*60f21e4bSdrh 896*60f21e4bSdrh /* 897*60f21e4bSdrh ** Read content from a quota_FILE 898*60f21e4bSdrh */ 899*60f21e4bSdrh size_t sqlite3_quota_fread( 900*60f21e4bSdrh void *pBuf, /* Store the content here */ 901*60f21e4bSdrh size_t size, /* Size of each element */ 902*60f21e4bSdrh size_t nmemb, /* Number of elements to read */ 903*60f21e4bSdrh quota_FILE *p /* Read from this quota_FILE object */ 904*60f21e4bSdrh ){ 905*60f21e4bSdrh return fread(pBuf, size, nmemb, p->f); 906*60f21e4bSdrh } 907*60f21e4bSdrh 908*60f21e4bSdrh /* 909*60f21e4bSdrh ** Write content into a quota_FILE. Invoke the quota callback and block 910*60f21e4bSdrh ** the write if we exceed quota. 911*60f21e4bSdrh */ 912*60f21e4bSdrh size_t sqlite3_quota_fwrite( 913*60f21e4bSdrh void *pBuf, /* Take content to write from here */ 914*60f21e4bSdrh size_t size, /* Size of each element */ 915*60f21e4bSdrh size_t nmemb, /* Number of elements */ 916*60f21e4bSdrh quota_FILE *p /* Write to this quota_FILE objecct */ 917*60f21e4bSdrh ){ 918*60f21e4bSdrh sqlite3_int64 iOfst; 919*60f21e4bSdrh sqlite3_int64 iEnd; 920*60f21e4bSdrh sqlite3_int64 szNew; 921*60f21e4bSdrh quotaFile *pFile; 922*60f21e4bSdrh 923*60f21e4bSdrh iOfst = ftell(p->f); 924*60f21e4bSdrh iEnd = iOfst + size*nmemb; 925*60f21e4bSdrh pFile = p->pFile; 926*60f21e4bSdrh if( pFile->iSize<iEnd ){ 927*60f21e4bSdrh quotaGroup *pGroup = pFile->pGroup; 928*60f21e4bSdrh quotaEnter(); 929*60f21e4bSdrh szNew = pGroup->iSize - pFile->iSize + iEnd; 930*60f21e4bSdrh if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){ 931*60f21e4bSdrh if( pGroup->xCallback ){ 932*60f21e4bSdrh pGroup->xCallback(pFile->zFilename, &pGroup->iLimit, szNew, 933*60f21e4bSdrh pGroup->pArg); 934*60f21e4bSdrh } 935*60f21e4bSdrh if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){ 936*60f21e4bSdrh iEnd = pGroup->iLimit - pGroup->iSize + pFile->iSize; 937*60f21e4bSdrh nmemb = (iEnd - iOfst)/size; 938*60f21e4bSdrh iEnd = iOfst + size*nmemb; 939*60f21e4bSdrh szNew = pGroup->iSize - pFile->iSize + iEnd; 940*60f21e4bSdrh } 941*60f21e4bSdrh } 942*60f21e4bSdrh pGroup->iSize = szNew; 943*60f21e4bSdrh pFile->iSize = iEnd; 944*60f21e4bSdrh quotaLeave(); 945*60f21e4bSdrh } 946*60f21e4bSdrh return fwrite(pBuf, size, nmemb, p->f); 947*60f21e4bSdrh } 948*60f21e4bSdrh 949*60f21e4bSdrh /* 950*60f21e4bSdrh ** Close an open quota_FILE stream. 951*60f21e4bSdrh */ 952*60f21e4bSdrh int sqlite3_quota_fclose(quota_FILE *p){ 953*60f21e4bSdrh int rc; 954*60f21e4bSdrh quotaFile *pFile; 955*60f21e4bSdrh rc = fclose(p->f); 956*60f21e4bSdrh pFile = p->pFile; 957*60f21e4bSdrh quotaEnter(); 958*60f21e4bSdrh pFile->nRef--; 959*60f21e4bSdrh if( pFile->nRef==0 ){ 960*60f21e4bSdrh quotaGroup *pGroup = pFile->pGroup; 961*60f21e4bSdrh if( pFile->deleteOnClose ) quotaRemoveFile(pFile); 962*60f21e4bSdrh quotaGroupDeref(pGroup); 963*60f21e4bSdrh } 964*60f21e4bSdrh quotaLeave(); 965*60f21e4bSdrh sqlite3_free(p); 966*60f21e4bSdrh return rc; 967*60f21e4bSdrh } 968*60f21e4bSdrh 969*60f21e4bSdrh /* 970*60f21e4bSdrh ** Seek on a quota_FILE stream. 971*60f21e4bSdrh */ 972*60f21e4bSdrh int sqlite3_quota_fseek(quota_FILE *p, long offset, int whence){ 973*60f21e4bSdrh return fseek(p->f, offset, whence); 974*60f21e4bSdrh } 975*60f21e4bSdrh 976*60f21e4bSdrh /* 977*60f21e4bSdrh ** rewind a quota_FILE stream. 978*60f21e4bSdrh */ 979*60f21e4bSdrh void sqlite3_quota_rewind(quota_FILE *p){ 980*60f21e4bSdrh rewind(p->f); 981*60f21e4bSdrh } 982*60f21e4bSdrh 983*60f21e4bSdrh /* 984*60f21e4bSdrh ** Tell the current location of a quota_FILE stream. 985*60f21e4bSdrh */ 986*60f21e4bSdrh long sqlite3_quota_ftell(quota_FILE *p){ 987*60f21e4bSdrh return ftell(p->f); 988*60f21e4bSdrh } 989*60f21e4bSdrh 990*60f21e4bSdrh /* 991*60f21e4bSdrh ** Remove a file. Update quotas accordingly. 992*60f21e4bSdrh */ 993*60f21e4bSdrh int sqlite3_quota_remove(const char *zFilename){ 994*60f21e4bSdrh int rc = remove(zFilename); 995*60f21e4bSdrh sqlite3_quota_file(zFilename); 996*60f21e4bSdrh return rc; 997*60f21e4bSdrh } 998*60f21e4bSdrh 999748a52cdSdrh 1000748a52cdSdrh /***************************** Test Code ***********************************/ 1001748a52cdSdrh #ifdef SQLITE_TEST 1002748a52cdSdrh #include <tcl.h> 10038cf35eb4Sdan 1004ef9659baSdrh /* 1005ef9659baSdrh ** Argument passed to a TCL quota-over-limit callback. 1006ef9659baSdrh */ 10078cf35eb4Sdan typedef struct TclQuotaCallback TclQuotaCallback; 10088cf35eb4Sdan struct TclQuotaCallback { 1009ef9659baSdrh Tcl_Interp *interp; /* Interpreter in which to run the script */ 1010ef9659baSdrh Tcl_Obj *pScript; /* Script to be run */ 10118cf35eb4Sdan }; 10128cf35eb4Sdan 10138cf35eb4Sdan extern const char *sqlite3TestErrorName(int); 10148cf35eb4Sdan 1015ef9659baSdrh 1016ef9659baSdrh /* 1017ef9659baSdrh ** This is the callback from a quota-over-limit. 1018ef9659baSdrh */ 10198cf35eb4Sdan static void tclQuotaCallback( 10208cf35eb4Sdan const char *zFilename, /* Name of file whose size increases */ 10218cf35eb4Sdan sqlite3_int64 *piLimit, /* IN/OUT: The current limit */ 10228cf35eb4Sdan sqlite3_int64 iSize, /* Total size of all files in the group */ 10238cf35eb4Sdan void *pArg /* Client data */ 10248cf35eb4Sdan ){ 10258cf35eb4Sdan TclQuotaCallback *p; /* Callback script object */ 10268cf35eb4Sdan Tcl_Obj *pEval; /* Script to evaluate */ 10278cf35eb4Sdan Tcl_Obj *pVarname; /* Name of variable to pass as 2nd arg */ 10288cf35eb4Sdan unsigned int rnd; /* Random part of pVarname */ 10298cf35eb4Sdan int rc; /* Tcl error code */ 10308cf35eb4Sdan 10318cf35eb4Sdan p = (TclQuotaCallback *)pArg; 10323d3e60e4Sdrh if( p==0 ) return; 10338cf35eb4Sdan 10348cf35eb4Sdan pVarname = Tcl_NewStringObj("::piLimit_", -1); 10358cf35eb4Sdan Tcl_IncrRefCount(pVarname); 10368cf35eb4Sdan sqlite3_randomness(sizeof(rnd), (void *)&rnd); 10378cf35eb4Sdan Tcl_AppendObjToObj(pVarname, Tcl_NewIntObj((int)(rnd&0x7FFFFFFF))); 10388cf35eb4Sdan Tcl_ObjSetVar2(p->interp, pVarname, 0, Tcl_NewWideIntObj(*piLimit), 0); 10398cf35eb4Sdan 10408cf35eb4Sdan pEval = Tcl_DuplicateObj(p->pScript); 10418cf35eb4Sdan Tcl_IncrRefCount(pEval); 10428cf35eb4Sdan Tcl_ListObjAppendElement(0, pEval, Tcl_NewStringObj(zFilename, -1)); 10438cf35eb4Sdan Tcl_ListObjAppendElement(0, pEval, pVarname); 10448cf35eb4Sdan Tcl_ListObjAppendElement(0, pEval, Tcl_NewWideIntObj(iSize)); 10458cf35eb4Sdan rc = Tcl_EvalObjEx(p->interp, pEval, TCL_EVAL_GLOBAL); 10468cf35eb4Sdan 10478cf35eb4Sdan if( rc==TCL_OK ){ 10488cf35eb4Sdan Tcl_Obj *pLimit = Tcl_ObjGetVar2(p->interp, pVarname, 0, 0); 10498cf35eb4Sdan rc = Tcl_GetWideIntFromObj(p->interp, pLimit, piLimit); 10508cf35eb4Sdan Tcl_UnsetVar(p->interp, Tcl_GetString(pVarname), 0); 10518cf35eb4Sdan } 10528cf35eb4Sdan 10538cf35eb4Sdan Tcl_DecrRefCount(pEval); 10548cf35eb4Sdan Tcl_DecrRefCount(pVarname); 10558cf35eb4Sdan if( rc!=TCL_OK ) Tcl_BackgroundError(p->interp); 10568cf35eb4Sdan } 10578cf35eb4Sdan 10588cf35eb4Sdan /* 1059ef9659baSdrh ** Destructor for a TCL quota-over-limit callback. 1060ef9659baSdrh */ 1061ef9659baSdrh static void tclCallbackDestructor(void *pObj){ 1062ef9659baSdrh TclQuotaCallback *p = (TclQuotaCallback*)pObj; 1063ef9659baSdrh if( p ){ 1064ef9659baSdrh Tcl_DecrRefCount(p->pScript); 10659fc7702cSdan sqlite3_free((char *)p); 1066ef9659baSdrh } 1067ef9659baSdrh } 1068ef9659baSdrh 1069ef9659baSdrh /* 10708cf35eb4Sdan ** tclcmd: sqlite3_quota_initialize NAME MAKEDEFAULT 10718cf35eb4Sdan */ 10728cf35eb4Sdan static int test_quota_initialize( 10738cf35eb4Sdan void * clientData, 10748cf35eb4Sdan Tcl_Interp *interp, 10758cf35eb4Sdan int objc, 10768cf35eb4Sdan Tcl_Obj *CONST objv[] 10778cf35eb4Sdan ){ 10788cf35eb4Sdan const char *zName; /* Name of new quota VFS */ 10798cf35eb4Sdan int makeDefault; /* True to make the new VFS the default */ 10808cf35eb4Sdan int rc; /* Value returned by quota_initialize() */ 10818cf35eb4Sdan 10828cf35eb4Sdan /* Process arguments */ 10838cf35eb4Sdan if( objc!=3 ){ 10848cf35eb4Sdan Tcl_WrongNumArgs(interp, 1, objv, "NAME MAKEDEFAULT"); 10858cf35eb4Sdan return TCL_ERROR; 10868cf35eb4Sdan } 10878cf35eb4Sdan zName = Tcl_GetString(objv[1]); 10888cf35eb4Sdan if( Tcl_GetBooleanFromObj(interp, objv[2], &makeDefault) ) return TCL_ERROR; 10898cf35eb4Sdan if( zName[0]=='\0' ) zName = 0; 10908cf35eb4Sdan 10918cf35eb4Sdan /* Call sqlite3_quota_initialize() */ 10928cf35eb4Sdan rc = sqlite3_quota_initialize(zName, makeDefault); 10938cf35eb4Sdan Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC); 10948cf35eb4Sdan 10958cf35eb4Sdan return TCL_OK; 10968cf35eb4Sdan } 10978cf35eb4Sdan 10988cf35eb4Sdan /* 10998cf35eb4Sdan ** tclcmd: sqlite3_quota_shutdown 11008cf35eb4Sdan */ 11018cf35eb4Sdan static int test_quota_shutdown( 11028cf35eb4Sdan void * clientData, 11038cf35eb4Sdan Tcl_Interp *interp, 11048cf35eb4Sdan int objc, 11058cf35eb4Sdan Tcl_Obj *CONST objv[] 11068cf35eb4Sdan ){ 11078cf35eb4Sdan int rc; /* Value returned by quota_shutdown() */ 11088cf35eb4Sdan 11098cf35eb4Sdan if( objc!=1 ){ 11108cf35eb4Sdan Tcl_WrongNumArgs(interp, 1, objv, ""); 11118cf35eb4Sdan return TCL_ERROR; 11128cf35eb4Sdan } 11138cf35eb4Sdan 11148cf35eb4Sdan /* Call sqlite3_quota_shutdown() */ 11158cf35eb4Sdan rc = sqlite3_quota_shutdown(); 11168cf35eb4Sdan Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC); 11178cf35eb4Sdan 11188cf35eb4Sdan return TCL_OK; 11198cf35eb4Sdan } 11208cf35eb4Sdan 11218cf35eb4Sdan /* 11228cf35eb4Sdan ** tclcmd: sqlite3_quota_set PATTERN LIMIT SCRIPT 11238cf35eb4Sdan */ 11248cf35eb4Sdan static int test_quota_set( 11258cf35eb4Sdan void * clientData, 11268cf35eb4Sdan Tcl_Interp *interp, 11278cf35eb4Sdan int objc, 11288cf35eb4Sdan Tcl_Obj *CONST objv[] 11298cf35eb4Sdan ){ 11308cf35eb4Sdan const char *zPattern; /* File pattern to configure */ 11318cf35eb4Sdan sqlite3_int64 iLimit; /* Initial quota in bytes */ 11328cf35eb4Sdan Tcl_Obj *pScript; /* Tcl script to invoke to increase quota */ 11338cf35eb4Sdan int rc; /* Value returned by quota_set() */ 11348cf35eb4Sdan TclQuotaCallback *p; /* Callback object */ 1135ef9659baSdrh int nScript; /* Length of callback script */ 11369fc7702cSdan void (*xDestroy)(void*); /* Optional destructor for pArg */ 113706bfd36aSdan void (*xCallback)(const char *, sqlite3_int64 *, sqlite3_int64, void *); 11388cf35eb4Sdan 11398cf35eb4Sdan /* Process arguments */ 11408cf35eb4Sdan if( objc!=4 ){ 11418cf35eb4Sdan Tcl_WrongNumArgs(interp, 1, objv, "PATTERN LIMIT SCRIPT"); 11428cf35eb4Sdan return TCL_ERROR; 11438cf35eb4Sdan } 11448cf35eb4Sdan zPattern = Tcl_GetString(objv[1]); 11458cf35eb4Sdan if( Tcl_GetWideIntFromObj(interp, objv[2], &iLimit) ) return TCL_ERROR; 11468cf35eb4Sdan pScript = objv[3]; 1147ef9659baSdrh Tcl_GetStringFromObj(pScript, &nScript); 11488cf35eb4Sdan 1149ef9659baSdrh if( nScript>0 ){ 11508cf35eb4Sdan /* Allocate a TclQuotaCallback object */ 11519fc7702cSdan p = (TclQuotaCallback *)sqlite3_malloc(sizeof(TclQuotaCallback)); 11529fc7702cSdan if( !p ){ 11539fc7702cSdan Tcl_SetResult(interp, (char *)"SQLITE_NOMEM", TCL_STATIC); 11549fc7702cSdan return TCL_OK; 11559fc7702cSdan } 11568cf35eb4Sdan memset(p, 0, sizeof(TclQuotaCallback)); 11578cf35eb4Sdan p->interp = interp; 1158ef9659baSdrh Tcl_IncrRefCount(pScript); 11598cf35eb4Sdan p->pScript = pScript; 11609fc7702cSdan xDestroy = tclCallbackDestructor; 116106bfd36aSdan xCallback = tclQuotaCallback; 1162ef9659baSdrh }else{ 1163ef9659baSdrh p = 0; 11649fc7702cSdan xDestroy = 0; 116506bfd36aSdan xCallback = 0; 11668cf35eb4Sdan } 11679fc7702cSdan 1168ef9659baSdrh /* Invoke sqlite3_quota_set() */ 116906bfd36aSdan rc = sqlite3_quota_set(zPattern, iLimit, xCallback, (void*)p, xDestroy); 11708cf35eb4Sdan 11718cf35eb4Sdan Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC); 11728cf35eb4Sdan return TCL_OK; 11738cf35eb4Sdan } 11748cf35eb4Sdan 11758cf35eb4Sdan /* 1176b6020c4eSdrh ** tclcmd: sqlite3_quota_file FILENAME 1177b6020c4eSdrh */ 1178b6020c4eSdrh static int test_quota_file( 1179b6020c4eSdrh void * clientData, 1180b6020c4eSdrh Tcl_Interp *interp, 1181b6020c4eSdrh int objc, 1182b6020c4eSdrh Tcl_Obj *CONST objv[] 1183b6020c4eSdrh ){ 1184b6020c4eSdrh const char *zFilename; /* File pattern to configure */ 1185b6020c4eSdrh int rc; /* Value returned by quota_file() */ 1186b6020c4eSdrh 1187b6020c4eSdrh /* Process arguments */ 1188b6020c4eSdrh if( objc!=2 ){ 1189b6020c4eSdrh Tcl_WrongNumArgs(interp, 1, objv, "FILENAME"); 1190b6020c4eSdrh return TCL_ERROR; 1191b6020c4eSdrh } 1192b6020c4eSdrh zFilename = Tcl_GetString(objv[1]); 1193b6020c4eSdrh 1194b6020c4eSdrh /* Invoke sqlite3_quota_file() */ 1195b6020c4eSdrh rc = sqlite3_quota_file(zFilename); 1196b6020c4eSdrh 1197b6020c4eSdrh Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC); 1198b6020c4eSdrh return TCL_OK; 1199b6020c4eSdrh } 1200b6020c4eSdrh 1201b6020c4eSdrh /* 1202ef9659baSdrh ** tclcmd: sqlite3_quota_dump 1203ef9659baSdrh */ 1204ef9659baSdrh static int test_quota_dump( 1205ef9659baSdrh void * clientData, 1206ef9659baSdrh Tcl_Interp *interp, 1207ef9659baSdrh int objc, 1208ef9659baSdrh Tcl_Obj *CONST objv[] 1209ef9659baSdrh ){ 1210ef9659baSdrh Tcl_Obj *pResult; 1211ef9659baSdrh Tcl_Obj *pGroupTerm; 1212ef9659baSdrh Tcl_Obj *pFileTerm; 1213ef9659baSdrh quotaGroup *pGroup; 1214ef9659baSdrh quotaFile *pFile; 1215ef9659baSdrh 1216ef9659baSdrh pResult = Tcl_NewObj(); 1217ef9659baSdrh quotaEnter(); 1218ef9659baSdrh for(pGroup=gQuota.pGroup; pGroup; pGroup=pGroup->pNext){ 1219ef9659baSdrh pGroupTerm = Tcl_NewObj(); 1220ef9659baSdrh Tcl_ListObjAppendElement(interp, pGroupTerm, 1221ef9659baSdrh Tcl_NewStringObj(pGroup->zPattern, -1)); 1222ef9659baSdrh Tcl_ListObjAppendElement(interp, pGroupTerm, 1223ef9659baSdrh Tcl_NewWideIntObj(pGroup->iLimit)); 1224ef9659baSdrh Tcl_ListObjAppendElement(interp, pGroupTerm, 1225ef9659baSdrh Tcl_NewWideIntObj(pGroup->iSize)); 122675c19174Sdrh for(pFile=pGroup->pFiles; pFile; pFile=pFile->pNext){ 1227ef9659baSdrh pFileTerm = Tcl_NewObj(); 1228ef9659baSdrh Tcl_ListObjAppendElement(interp, pFileTerm, 1229ef9659baSdrh Tcl_NewStringObj(pFile->zFilename, -1)); 1230ef9659baSdrh Tcl_ListObjAppendElement(interp, pFileTerm, 1231ef9659baSdrh Tcl_NewWideIntObj(pFile->iSize)); 1232ef9659baSdrh Tcl_ListObjAppendElement(interp, pFileTerm, 1233ef9659baSdrh Tcl_NewWideIntObj(pFile->nRef)); 1234796af156Sdrh Tcl_ListObjAppendElement(interp, pFileTerm, 1235796af156Sdrh Tcl_NewWideIntObj(pFile->deleteOnClose)); 1236ef9659baSdrh Tcl_ListObjAppendElement(interp, pGroupTerm, pFileTerm); 1237ef9659baSdrh } 1238ef9659baSdrh Tcl_ListObjAppendElement(interp, pResult, pGroupTerm); 1239ef9659baSdrh } 1240ef9659baSdrh quotaLeave(); 1241ef9659baSdrh Tcl_SetObjResult(interp, pResult); 1242ef9659baSdrh return TCL_OK; 1243ef9659baSdrh } 1244ef9659baSdrh 1245748a52cdSdrh /* 1246748a52cdSdrh ** This routine registers the custom TCL commands defined in this 1247748a52cdSdrh ** module. This should be the only procedure visible from outside 1248748a52cdSdrh ** of this module. 1249748a52cdSdrh */ 1250748a52cdSdrh int Sqlitequota_Init(Tcl_Interp *interp){ 12518cf35eb4Sdan static struct { 12528cf35eb4Sdan char *zName; 12538cf35eb4Sdan Tcl_ObjCmdProc *xProc; 12548cf35eb4Sdan } aCmd[] = { 12558cf35eb4Sdan { "sqlite3_quota_initialize", test_quota_initialize }, 12568cf35eb4Sdan { "sqlite3_quota_shutdown", test_quota_shutdown }, 12578cf35eb4Sdan { "sqlite3_quota_set", test_quota_set }, 1258b6020c4eSdrh { "sqlite3_quota_file", test_quota_file }, 1259ef9659baSdrh { "sqlite3_quota_dump", test_quota_dump }, 12608cf35eb4Sdan }; 12618cf35eb4Sdan int i; 12628cf35eb4Sdan 12638cf35eb4Sdan for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){ 12648cf35eb4Sdan Tcl_CreateObjCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0); 12658cf35eb4Sdan } 12668cf35eb4Sdan 1267748a52cdSdrh return TCL_OK; 1268748a52cdSdrh } 1269748a52cdSdrh #endif 1270