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 */ 30748a52cdSdrh #include "sqlite3.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 114748a52cdSdrh /************************* Global Variables **********************************/ 115748a52cdSdrh /* 116748a52cdSdrh ** All global variables used by this file are containing within the following 117748a52cdSdrh ** gQuota structure. 118748a52cdSdrh */ 119748a52cdSdrh static struct { 120549dc1baSdrh /* The pOrigVfs is the real, original underlying VFS implementation. 121748a52cdSdrh ** Most operations pass-through to the real VFS. This value is read-only 122748a52cdSdrh ** during operation. It is only modified at start-time and thus does not 123748a52cdSdrh ** require a mutex. 124748a52cdSdrh */ 125748a52cdSdrh sqlite3_vfs *pOrigVfs; 126748a52cdSdrh 127748a52cdSdrh /* The sThisVfs is the VFS structure used by this shim. It is initialized 128748a52cdSdrh ** at start-time and thus does not require a mutex 129748a52cdSdrh */ 130748a52cdSdrh sqlite3_vfs sThisVfs; 131748a52cdSdrh 132748a52cdSdrh /* The sIoMethods defines the methods used by sqlite3_file objects 133748a52cdSdrh ** associated with this shim. It is initialized at start-time and does 134748a52cdSdrh ** not require a mutex. 135748a52cdSdrh ** 136748a52cdSdrh ** When the underlying VFS is called to open a file, it might return 137748a52cdSdrh ** either a version 1 or a version 2 sqlite3_file object. This shim 138748a52cdSdrh ** has to create a wrapper sqlite3_file of the same version. Hence 139748a52cdSdrh ** there are two I/O method structures, one for version 1 and the other 140748a52cdSdrh ** for version 2. 141748a52cdSdrh */ 142748a52cdSdrh sqlite3_io_methods sIoMethodsV1; 143748a52cdSdrh sqlite3_io_methods sIoMethodsV2; 144748a52cdSdrh 145748a52cdSdrh /* True when this shim as been initialized. 146748a52cdSdrh */ 147748a52cdSdrh int isInitialized; 148748a52cdSdrh 149748a52cdSdrh /* For run-time access any of the other global data structures in this 150748a52cdSdrh ** shim, the following mutex must be held. 151748a52cdSdrh */ 152748a52cdSdrh sqlite3_mutex *pMutex; 153748a52cdSdrh 154748a52cdSdrh /* List of quotaGroup objects. 155748a52cdSdrh */ 156748a52cdSdrh quotaGroup *pGroup; 157748a52cdSdrh 158748a52cdSdrh } gQuota; 159748a52cdSdrh 160748a52cdSdrh /************************* Utility Routines *********************************/ 161748a52cdSdrh /* 162748a52cdSdrh ** Acquire and release the mutex used to serialize access to the 163748a52cdSdrh ** list of quotaGroups. 164748a52cdSdrh */ 165748a52cdSdrh static void quotaEnter(void){ sqlite3_mutex_enter(gQuota.pMutex); } 166748a52cdSdrh static void quotaLeave(void){ sqlite3_mutex_leave(gQuota.pMutex); } 167748a52cdSdrh 168796af156Sdrh /* Count the number of open files in a quotaGroup 169796af156Sdrh */ 170796af156Sdrh static int quotaGroupOpenFileCount(quotaGroup *pGroup){ 171796af156Sdrh int N = 0; 172796af156Sdrh quotaFile *pFile = pGroup->pFiles; 173796af156Sdrh while( pFile ){ 174796af156Sdrh if( pFile->nRef ) N++; 175796af156Sdrh pFile = pFile->pNext; 176796af156Sdrh } 177796af156Sdrh return N; 178796af156Sdrh } 179796af156Sdrh 180796af156Sdrh /* Remove a file from a quota group. 181796af156Sdrh */ 182796af156Sdrh static void quotaRemoveFile(quotaFile *pFile){ 183796af156Sdrh quotaGroup *pGroup = pFile->pGroup; 184796af156Sdrh pGroup->iSize -= pFile->iSize; 185796af156Sdrh *pFile->ppPrev = pFile->pNext; 186796af156Sdrh if( pFile->pNext ) pFile->pNext->ppPrev = pFile->ppPrev; 187796af156Sdrh sqlite3_free(pFile); 188796af156Sdrh } 189796af156Sdrh 190796af156Sdrh /* Remove all files from a quota group. It is always the case that 191796af156Sdrh ** all files will be closed when this routine is called. 192796af156Sdrh */ 193796af156Sdrh static void quotaRemoveAllFiles(quotaGroup *pGroup){ 194796af156Sdrh while( pGroup->pFiles ){ 195796af156Sdrh assert( pGroup->pFiles->nRef==0 ); 196796af156Sdrh quotaRemoveFile(pGroup->pFiles); 197796af156Sdrh } 198796af156Sdrh } 199796af156Sdrh 200748a52cdSdrh 201748a52cdSdrh /* If the reference count and threshold for a quotaGroup are both 202748a52cdSdrh ** zero, then destroy the quotaGroup. 203748a52cdSdrh */ 20475c19174Sdrh static void quotaGroupDeref(quotaGroup *pGroup){ 205796af156Sdrh if( pGroup->iLimit==0 && quotaGroupOpenFileCount(pGroup)==0 ){ 206796af156Sdrh quotaRemoveAllFiles(pGroup); 2079fc7702cSdan *pGroup->ppPrev = pGroup->pNext; 20875c19174Sdrh if( pGroup->pNext ) pGroup->pNext->ppPrev = pGroup->ppPrev; 20975c19174Sdrh if( pGroup->xDestroy ) pGroup->xDestroy(pGroup->pArg); 21075c19174Sdrh sqlite3_free(pGroup); 211748a52cdSdrh } 212748a52cdSdrh } 213748a52cdSdrh 214748a52cdSdrh /* 215748a52cdSdrh ** Return TRUE if string z matches glob pattern zGlob. 216748a52cdSdrh ** 217748a52cdSdrh ** Globbing rules: 218748a52cdSdrh ** 219748a52cdSdrh ** '*' Matches any sequence of zero or more characters. 220748a52cdSdrh ** 221748a52cdSdrh ** '?' Matches exactly one character. 222748a52cdSdrh ** 223748a52cdSdrh ** [...] Matches one character from the enclosed list of 224748a52cdSdrh ** characters. 225748a52cdSdrh ** 226748a52cdSdrh ** [^...] Matches one character not in the enclosed list. 227748a52cdSdrh ** 228748a52cdSdrh */ 22961cc423aSdrh static int quotaStrglob(const char *zGlob, const char *z){ 230748a52cdSdrh int c, c2; 231748a52cdSdrh int invert; 232748a52cdSdrh int seen; 233748a52cdSdrh 234748a52cdSdrh while( (c = (*(zGlob++)))!=0 ){ 235748a52cdSdrh if( c=='*' ){ 236748a52cdSdrh while( (c=(*(zGlob++))) == '*' || c=='?' ){ 237748a52cdSdrh if( c=='?' && (*(z++))==0 ) return 0; 238748a52cdSdrh } 239748a52cdSdrh if( c==0 ){ 240748a52cdSdrh return 1; 241748a52cdSdrh }else if( c=='[' ){ 24261cc423aSdrh while( *z && quotaStrglob(zGlob-1,z)==0 ){ 243748a52cdSdrh z++; 244748a52cdSdrh } 245748a52cdSdrh return (*z)!=0; 246748a52cdSdrh } 247748a52cdSdrh while( (c2 = (*(z++)))!=0 ){ 248748a52cdSdrh while( c2!=c ){ 249748a52cdSdrh c2 = *(z++); 250748a52cdSdrh if( c2==0 ) return 0; 251748a52cdSdrh } 25261cc423aSdrh if( quotaStrglob(zGlob,z) ) return 1; 253748a52cdSdrh } 254748a52cdSdrh return 0; 255748a52cdSdrh }else if( c=='?' ){ 256748a52cdSdrh if( (*(z++))==0 ) return 0; 257748a52cdSdrh }else if( c=='[' ){ 258748a52cdSdrh int prior_c = 0; 259748a52cdSdrh seen = 0; 260748a52cdSdrh invert = 0; 261748a52cdSdrh c = *(z++); 262748a52cdSdrh if( c==0 ) return 0; 263748a52cdSdrh c2 = *(zGlob++); 264748a52cdSdrh if( c2=='^' ){ 265748a52cdSdrh invert = 1; 266748a52cdSdrh c2 = *(zGlob++); 267748a52cdSdrh } 268748a52cdSdrh if( c2==']' ){ 269748a52cdSdrh if( c==']' ) seen = 1; 270748a52cdSdrh c2 = *(zGlob++); 271748a52cdSdrh } 272748a52cdSdrh while( c2 && c2!=']' ){ 273748a52cdSdrh if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){ 274748a52cdSdrh c2 = *(zGlob++); 275748a52cdSdrh if( c>=prior_c && c<=c2 ) seen = 1; 276748a52cdSdrh prior_c = 0; 277748a52cdSdrh }else{ 278748a52cdSdrh if( c==c2 ){ 279748a52cdSdrh seen = 1; 280748a52cdSdrh } 281748a52cdSdrh prior_c = c2; 282748a52cdSdrh } 283748a52cdSdrh c2 = *(zGlob++); 284748a52cdSdrh } 285748a52cdSdrh if( c2==0 || (seen ^ invert)==0 ) return 0; 286748a52cdSdrh }else{ 287748a52cdSdrh if( c!=(*(z++)) ) return 0; 288748a52cdSdrh } 289748a52cdSdrh } 290748a52cdSdrh return *z==0; 291748a52cdSdrh } 292748a52cdSdrh 293748a52cdSdrh 294748a52cdSdrh /* Find a quotaGroup given the filename. 2958cf35eb4Sdan ** 296ef9659baSdrh ** Return a pointer to the quotaGroup object. Return NULL if not found. 297748a52cdSdrh */ 298748a52cdSdrh static quotaGroup *quotaGroupFind(const char *zFilename){ 299748a52cdSdrh quotaGroup *p; 30061cc423aSdrh for(p=gQuota.pGroup; p && quotaStrglob(p->zPattern, zFilename)==0; 30161cc423aSdrh p=p->pNext){} 302748a52cdSdrh return p; 303748a52cdSdrh } 304748a52cdSdrh 305ef9659baSdrh /* Translate an sqlite3_file* that is really a quotaConn* into 306549dc1baSdrh ** the sqlite3_file* for the underlying original VFS. 307748a52cdSdrh */ 308ef9659baSdrh static sqlite3_file *quotaSubOpen(sqlite3_file *pConn){ 309ef9659baSdrh quotaConn *p = (quotaConn*)pConn; 310748a52cdSdrh return (sqlite3_file*)&p[1]; 311748a52cdSdrh } 312748a52cdSdrh 313796af156Sdrh /* Find a file in a quota group and return a pointer to that file. 314796af156Sdrh ** Return NULL if the file is not in the group. 315796af156Sdrh */ 316796af156Sdrh static quotaFile *quotaFindFile(quotaGroup *pGroup, const char *zName){ 317796af156Sdrh quotaFile *pFile = pGroup->pFiles; 318796af156Sdrh while( pFile && strcmp(pFile->zFilename, zName)!=0 ){ 319796af156Sdrh pFile = pFile->pNext; 320796af156Sdrh } 321796af156Sdrh return pFile; 322796af156Sdrh } 323796af156Sdrh 324748a52cdSdrh /************************* VFS Method Wrappers *****************************/ 325748a52cdSdrh /* 326748a52cdSdrh ** This is the xOpen method used for the "quota" VFS. 327748a52cdSdrh ** 328748a52cdSdrh ** Most of the work is done by the underlying original VFS. This method 329549dc1baSdrh ** simply links the new file into the appropriate quota group if it is a 330549dc1baSdrh ** file that needs to be tracked. 331748a52cdSdrh */ 332748a52cdSdrh static int quotaOpen( 333549dc1baSdrh sqlite3_vfs *pVfs, /* The quota VFS */ 334549dc1baSdrh const char *zName, /* Name of file to be opened */ 335ef9659baSdrh sqlite3_file *pConn, /* Fill in this file descriptor */ 336549dc1baSdrh int flags, /* Flags to control the opening */ 337549dc1baSdrh int *pOutFlags /* Flags showing results of opening */ 338748a52cdSdrh ){ 339549dc1baSdrh int rc; /* Result code */ 340ef9659baSdrh quotaConn *pQuotaOpen; /* The new quota file descriptor */ 341549dc1baSdrh quotaFile *pFile; /* Corresponding quotaFile obj */ 342549dc1baSdrh quotaGroup *pGroup; /* The group file belongs to */ 343549dc1baSdrh sqlite3_file *pSubOpen; /* Real file descriptor */ 344549dc1baSdrh sqlite3_vfs *pOrigVfs = gQuota.pOrigVfs; /* Real VFS */ 345748a52cdSdrh 346748a52cdSdrh /* If the file is not a main database file or a WAL, then use the 347748a52cdSdrh ** normal xOpen method. 348748a52cdSdrh */ 3498cf35eb4Sdan if( (flags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_WAL))==0 ){ 350ef9659baSdrh return pOrigVfs->xOpen(pOrigVfs, zName, pConn, flags, pOutFlags); 351748a52cdSdrh } 352748a52cdSdrh 353748a52cdSdrh /* If the name of the file does not match any quota group, then 354748a52cdSdrh ** use the normal xOpen method. 355748a52cdSdrh */ 356748a52cdSdrh quotaEnter(); 3578cf35eb4Sdan pGroup = quotaGroupFind(zName); 358748a52cdSdrh if( pGroup==0 ){ 359ef9659baSdrh rc = pOrigVfs->xOpen(pOrigVfs, zName, pConn, flags, pOutFlags); 360748a52cdSdrh }else{ 3618cf35eb4Sdan /* If we get to this point, it means the file needs to be quota tracked. 3628cf35eb4Sdan */ 363ef9659baSdrh pQuotaOpen = (quotaConn*)pConn; 364ef9659baSdrh pSubOpen = quotaSubOpen(pConn); 365745de2e4Sdrh rc = pOrigVfs->xOpen(pOrigVfs, zName, pSubOpen, flags, pOutFlags); 3668cf35eb4Sdan if( rc==SQLITE_OK ){ 367796af156Sdrh pFile = quotaFindFile(pGroup, zName); 368745de2e4Sdrh if( pFile==0 ){ 369745de2e4Sdrh int nName = strlen(zName); 370b0a32832Sdan pFile = (quotaFile *)sqlite3_malloc( sizeof(*pFile) + nName + 1 ); 371745de2e4Sdrh if( pFile==0 ){ 372745de2e4Sdrh quotaLeave(); 373745de2e4Sdrh pSubOpen->pMethods->xClose(pSubOpen); 374745de2e4Sdrh return SQLITE_NOMEM; 375745de2e4Sdrh } 376745de2e4Sdrh memset(pFile, 0, sizeof(*pFile)); 377745de2e4Sdrh pFile->zFilename = (char*)&pFile[1]; 378745de2e4Sdrh memcpy(pFile->zFilename, zName, nName+1); 37975c19174Sdrh pFile->pNext = pGroup->pFiles; 38075c19174Sdrh if( pGroup->pFiles ) pGroup->pFiles->ppPrev = &pFile->pNext; 38175c19174Sdrh pFile->ppPrev = &pGroup->pFiles; 38275c19174Sdrh pGroup->pFiles = pFile; 383745de2e4Sdrh pFile->pGroup = pGroup; 384796af156Sdrh pFile->deleteOnClose = (flags & SQLITE_OPEN_DELETEONCLOSE)!=0; 385745de2e4Sdrh } 386745de2e4Sdrh pFile->nRef++; 387745de2e4Sdrh pQuotaOpen->pFile = pFile; 388745de2e4Sdrh if( pSubOpen->pMethods->iVersion==1 ){ 389745de2e4Sdrh pQuotaOpen->base.pMethods = &gQuota.sIoMethodsV1; 3908cf35eb4Sdan }else{ 391745de2e4Sdrh pQuotaOpen->base.pMethods = &gQuota.sIoMethodsV2; 392748a52cdSdrh } 393748a52cdSdrh } 3948cf35eb4Sdan } 3958cf35eb4Sdan quotaLeave(); 396748a52cdSdrh return rc; 397748a52cdSdrh } 398748a52cdSdrh 399796af156Sdrh /* 400796af156Sdrh ** This is the xDelete method used for the "quota" VFS. 401796af156Sdrh ** 402796af156Sdrh ** If the file being deleted is part of the quota group, then reduce 403796af156Sdrh ** the size of the quota group accordingly. And remove the file from 404796af156Sdrh ** the set of files in the quota group. 405796af156Sdrh */ 406796af156Sdrh static int quotaDelete( 407796af156Sdrh sqlite3_vfs *pVfs, /* The quota VFS */ 408796af156Sdrh const char *zName, /* Name of file to be deleted */ 409796af156Sdrh int syncDir /* Do a directory sync after deleting */ 410796af156Sdrh ){ 411796af156Sdrh int rc; /* Result code */ 412796af156Sdrh quotaFile *pFile; /* Files in the quota */ 413796af156Sdrh quotaGroup *pGroup; /* The group file belongs to */ 414796af156Sdrh sqlite3_vfs *pOrigVfs = gQuota.pOrigVfs; /* Real VFS */ 415796af156Sdrh 416796af156Sdrh /* Do the actual file delete */ 417796af156Sdrh rc = pOrigVfs->xDelete(pOrigVfs, zName, syncDir); 418796af156Sdrh 419796af156Sdrh /* If the file just deleted is a member of a quota group, then remove 420796af156Sdrh ** it from that quota group. 421796af156Sdrh */ 422796af156Sdrh if( rc==SQLITE_OK ){ 423796af156Sdrh quotaEnter(); 424796af156Sdrh pGroup = quotaGroupFind(zName); 425796af156Sdrh if( pGroup ){ 426796af156Sdrh pFile = quotaFindFile(pGroup, zName); 427796af156Sdrh if( pFile ){ 428796af156Sdrh if( pFile->nRef ){ 429796af156Sdrh pFile->deleteOnClose = 1; 430796af156Sdrh }else{ 431796af156Sdrh quotaRemoveFile(pFile); 432796af156Sdrh quotaGroupDeref(pGroup); 433796af156Sdrh } 434796af156Sdrh } 435796af156Sdrh } 436796af156Sdrh quotaLeave(); 437796af156Sdrh } 438796af156Sdrh return rc; 439796af156Sdrh } 440796af156Sdrh 441796af156Sdrh 442748a52cdSdrh /************************ I/O Method Wrappers *******************************/ 443748a52cdSdrh 444748a52cdSdrh /* xClose requests get passed through to the original VFS. But we 445ef9659baSdrh ** also have to unlink the quotaConn from the quotaFile and quotaGroup. 446ef9659baSdrh ** The quotaFile and/or quotaGroup are freed if they are no longer in use. 447748a52cdSdrh */ 448ef9659baSdrh static int quotaClose(sqlite3_file *pConn){ 449ef9659baSdrh quotaConn *p = (quotaConn*)pConn; 450745de2e4Sdrh quotaFile *pFile = p->pFile; 451ef9659baSdrh sqlite3_file *pSubOpen = quotaSubOpen(pConn); 452748a52cdSdrh int rc; 453745de2e4Sdrh rc = pSubOpen->pMethods->xClose(pSubOpen); 454748a52cdSdrh quotaEnter(); 455745de2e4Sdrh pFile->nRef--; 456745de2e4Sdrh if( pFile->nRef==0 ){ 457745de2e4Sdrh quotaGroup *pGroup = pFile->pGroup; 458796af156Sdrh if( pFile->deleteOnClose ) quotaRemoveFile(pFile); 459748a52cdSdrh quotaGroupDeref(pGroup); 460745de2e4Sdrh } 461748a52cdSdrh quotaLeave(); 462748a52cdSdrh return rc; 463748a52cdSdrh } 464748a52cdSdrh 465748a52cdSdrh /* Pass xRead requests directory thru to the original VFS without 466748a52cdSdrh ** further processing. 467748a52cdSdrh */ 468748a52cdSdrh static int quotaRead( 469ef9659baSdrh sqlite3_file *pConn, 470748a52cdSdrh void *pBuf, 471748a52cdSdrh int iAmt, 472748a52cdSdrh sqlite3_int64 iOfst 473748a52cdSdrh ){ 474ef9659baSdrh sqlite3_file *pSubOpen = quotaSubOpen(pConn); 475745de2e4Sdrh return pSubOpen->pMethods->xRead(pSubOpen, pBuf, iAmt, iOfst); 476748a52cdSdrh } 477748a52cdSdrh 478748a52cdSdrh /* Check xWrite requests to see if they expand the file. If they do, 479748a52cdSdrh ** the perform a quota check before passing them through to the 480748a52cdSdrh ** original VFS. 481748a52cdSdrh */ 482748a52cdSdrh static int quotaWrite( 483ef9659baSdrh sqlite3_file *pConn, 4848cf35eb4Sdan const void *pBuf, 485748a52cdSdrh int iAmt, 486748a52cdSdrh sqlite3_int64 iOfst 487748a52cdSdrh ){ 488ef9659baSdrh quotaConn *p = (quotaConn*)pConn; 489ef9659baSdrh sqlite3_file *pSubOpen = quotaSubOpen(pConn); 490748a52cdSdrh sqlite3_int64 iEnd = iOfst+iAmt; 491748a52cdSdrh quotaGroup *pGroup; 492745de2e4Sdrh quotaFile *pFile = p->pFile; 493748a52cdSdrh sqlite3_int64 szNew; 494748a52cdSdrh 495745de2e4Sdrh if( pFile->iSize<iEnd ){ 496745de2e4Sdrh pGroup = pFile->pGroup; 497748a52cdSdrh quotaEnter(); 498745de2e4Sdrh szNew = pGroup->iSize - pFile->iSize + iEnd; 4998cf35eb4Sdan if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){ 500748a52cdSdrh if( pGroup->xCallback ){ 501745de2e4Sdrh pGroup->xCallback(pFile->zFilename, &pGroup->iLimit, szNew, 502748a52cdSdrh pGroup->pArg); 503748a52cdSdrh } 5048cf35eb4Sdan if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){ 505748a52cdSdrh quotaLeave(); 506748a52cdSdrh return SQLITE_FULL; 507748a52cdSdrh } 508748a52cdSdrh } 509748a52cdSdrh pGroup->iSize = szNew; 510745de2e4Sdrh pFile->iSize = iEnd; 511748a52cdSdrh quotaLeave(); 512748a52cdSdrh } 513745de2e4Sdrh return pSubOpen->pMethods->xWrite(pSubOpen, pBuf, iAmt, iOfst); 514748a52cdSdrh } 515748a52cdSdrh 516748a52cdSdrh /* Pass xTruncate requests thru to the original VFS. If the 517748a52cdSdrh ** success, update the file size. 518748a52cdSdrh */ 519ef9659baSdrh static int quotaTruncate(sqlite3_file *pConn, sqlite3_int64 size){ 520ef9659baSdrh quotaConn *p = (quotaConn*)pConn; 521ef9659baSdrh sqlite3_file *pSubOpen = quotaSubOpen(pConn); 522745de2e4Sdrh int rc = pSubOpen->pMethods->xTruncate(pSubOpen, size); 523745de2e4Sdrh quotaFile *pFile = p->pFile; 524745de2e4Sdrh quotaGroup *pGroup; 525748a52cdSdrh if( rc==SQLITE_OK ){ 526748a52cdSdrh quotaEnter(); 527745de2e4Sdrh pGroup = pFile->pGroup; 528745de2e4Sdrh pGroup->iSize -= pFile->iSize; 529745de2e4Sdrh pFile->iSize = size; 530748a52cdSdrh pGroup->iSize += size; 531748a52cdSdrh quotaLeave(); 532748a52cdSdrh } 533748a52cdSdrh return rc; 534748a52cdSdrh } 535748a52cdSdrh 536748a52cdSdrh /* Pass xSync requests through to the original VFS without change 537748a52cdSdrh */ 538ef9659baSdrh static int quotaSync(sqlite3_file *pConn, int flags){ 539ef9659baSdrh sqlite3_file *pSubOpen = quotaSubOpen(pConn); 540745de2e4Sdrh return pSubOpen->pMethods->xSync(pSubOpen, flags); 541748a52cdSdrh } 542748a52cdSdrh 543748a52cdSdrh /* Pass xFileSize requests through to the original VFS but then 544748a52cdSdrh ** update the quotaGroup with the new size before returning. 545748a52cdSdrh */ 546ef9659baSdrh static int quotaFileSize(sqlite3_file *pConn, sqlite3_int64 *pSize){ 547ef9659baSdrh quotaConn *p = (quotaConn*)pConn; 548ef9659baSdrh sqlite3_file *pSubOpen = quotaSubOpen(pConn); 549745de2e4Sdrh quotaFile *pFile = p->pFile; 550748a52cdSdrh quotaGroup *pGroup; 551748a52cdSdrh sqlite3_int64 sz; 552748a52cdSdrh int rc; 553748a52cdSdrh 554745de2e4Sdrh rc = pSubOpen->pMethods->xFileSize(pSubOpen, &sz); 555748a52cdSdrh if( rc==SQLITE_OK ){ 556748a52cdSdrh quotaEnter(); 557745de2e4Sdrh pGroup = pFile->pGroup; 558745de2e4Sdrh pGroup->iSize -= pFile->iSize; 559745de2e4Sdrh pFile->iSize = sz; 560748a52cdSdrh pGroup->iSize += sz; 561748a52cdSdrh quotaLeave(); 562748a52cdSdrh *pSize = sz; 563748a52cdSdrh } 564748a52cdSdrh return rc; 565748a52cdSdrh } 566748a52cdSdrh 567748a52cdSdrh /* Pass xLock requests through to the original VFS unchanged. 568748a52cdSdrh */ 569ef9659baSdrh static int quotaLock(sqlite3_file *pConn, int lock){ 570ef9659baSdrh sqlite3_file *pSubOpen = quotaSubOpen(pConn); 571745de2e4Sdrh return pSubOpen->pMethods->xLock(pSubOpen, lock); 572748a52cdSdrh } 573748a52cdSdrh 574748a52cdSdrh /* Pass xUnlock requests through to the original VFS unchanged. 575748a52cdSdrh */ 576ef9659baSdrh static int quotaUnlock(sqlite3_file *pConn, int lock){ 577ef9659baSdrh sqlite3_file *pSubOpen = quotaSubOpen(pConn); 578745de2e4Sdrh return pSubOpen->pMethods->xUnlock(pSubOpen, lock); 579748a52cdSdrh } 580748a52cdSdrh 581748a52cdSdrh /* Pass xCheckReservedLock requests through to the original VFS unchanged. 582748a52cdSdrh */ 583ef9659baSdrh static int quotaCheckReservedLock(sqlite3_file *pConn, int *pResOut){ 584ef9659baSdrh sqlite3_file *pSubOpen = quotaSubOpen(pConn); 585745de2e4Sdrh return pSubOpen->pMethods->xCheckReservedLock(pSubOpen, pResOut); 586748a52cdSdrh } 587748a52cdSdrh 588748a52cdSdrh /* Pass xFileControl requests through to the original VFS unchanged. 589748a52cdSdrh */ 590ef9659baSdrh static int quotaFileControl(sqlite3_file *pConn, int op, void *pArg){ 591ef9659baSdrh sqlite3_file *pSubOpen = quotaSubOpen(pConn); 592745de2e4Sdrh return pSubOpen->pMethods->xFileControl(pSubOpen, op, pArg); 593748a52cdSdrh } 594748a52cdSdrh 595748a52cdSdrh /* Pass xSectorSize requests through to the original VFS unchanged. 596748a52cdSdrh */ 597ef9659baSdrh static int quotaSectorSize(sqlite3_file *pConn){ 598ef9659baSdrh sqlite3_file *pSubOpen = quotaSubOpen(pConn); 599745de2e4Sdrh return pSubOpen->pMethods->xSectorSize(pSubOpen); 600748a52cdSdrh } 601748a52cdSdrh 602748a52cdSdrh /* Pass xDeviceCharacteristics requests through to the original VFS unchanged. 603748a52cdSdrh */ 604ef9659baSdrh static int quotaDeviceCharacteristics(sqlite3_file *pConn){ 605ef9659baSdrh sqlite3_file *pSubOpen = quotaSubOpen(pConn); 606745de2e4Sdrh return pSubOpen->pMethods->xDeviceCharacteristics(pSubOpen); 607748a52cdSdrh } 608748a52cdSdrh 609748a52cdSdrh /* Pass xShmMap requests through to the original VFS unchanged. 610748a52cdSdrh */ 611748a52cdSdrh static int quotaShmMap( 612ef9659baSdrh sqlite3_file *pConn, /* Handle open on database file */ 613748a52cdSdrh int iRegion, /* Region to retrieve */ 614748a52cdSdrh int szRegion, /* Size of regions */ 615748a52cdSdrh int bExtend, /* True to extend file if necessary */ 616748a52cdSdrh void volatile **pp /* OUT: Mapped memory */ 617748a52cdSdrh ){ 618ef9659baSdrh sqlite3_file *pSubOpen = quotaSubOpen(pConn); 619745de2e4Sdrh return pSubOpen->pMethods->xShmMap(pSubOpen, iRegion, szRegion, bExtend, pp); 620748a52cdSdrh } 621748a52cdSdrh 622748a52cdSdrh /* Pass xShmLock requests through to the original VFS unchanged. 623748a52cdSdrh */ 624748a52cdSdrh static int quotaShmLock( 625ef9659baSdrh sqlite3_file *pConn, /* Database file holding the shared memory */ 626748a52cdSdrh int ofst, /* First lock to acquire or release */ 627748a52cdSdrh int n, /* Number of locks to acquire or release */ 628748a52cdSdrh int flags /* What to do with the lock */ 629748a52cdSdrh ){ 630ef9659baSdrh sqlite3_file *pSubOpen = quotaSubOpen(pConn); 631745de2e4Sdrh return pSubOpen->pMethods->xShmLock(pSubOpen, ofst, n, flags); 632748a52cdSdrh } 633748a52cdSdrh 634748a52cdSdrh /* Pass xShmBarrier requests through to the original VFS unchanged. 635748a52cdSdrh */ 636ef9659baSdrh static void quotaShmBarrier(sqlite3_file *pConn){ 637ef9659baSdrh sqlite3_file *pSubOpen = quotaSubOpen(pConn); 638745de2e4Sdrh pSubOpen->pMethods->xShmBarrier(pSubOpen); 639748a52cdSdrh } 640748a52cdSdrh 641748a52cdSdrh /* Pass xShmUnmap requests through to the original VFS unchanged. 642748a52cdSdrh */ 643ef9659baSdrh static int quotaShmUnmap(sqlite3_file *pConn, int deleteFlag){ 644ef9659baSdrh sqlite3_file *pSubOpen = quotaSubOpen(pConn); 645745de2e4Sdrh return pSubOpen->pMethods->xShmUnmap(pSubOpen, deleteFlag); 646748a52cdSdrh } 647748a52cdSdrh 648748a52cdSdrh /************************** Public Interfaces *****************************/ 649748a52cdSdrh /* 650748a52cdSdrh ** Initialize the quota VFS shim. Use the VFS named zOrigVfsName 651748a52cdSdrh ** as the VFS that does the actual work. Use the default if 652748a52cdSdrh ** zOrigVfsName==NULL. 653748a52cdSdrh ** 654748a52cdSdrh ** The quota VFS shim is named "quota". It will become the default 655748a52cdSdrh ** VFS if makeDefault is non-zero. 656748a52cdSdrh ** 657748a52cdSdrh ** THIS ROUTINE IS NOT THREADSAFE. Call this routine exactly once 658748a52cdSdrh ** during start-up. 659748a52cdSdrh */ 660748a52cdSdrh int sqlite3_quota_initialize(const char *zOrigVfsName, int makeDefault){ 661748a52cdSdrh sqlite3_vfs *pOrigVfs; 6628cf35eb4Sdan if( gQuota.isInitialized ) return SQLITE_MISUSE; 663748a52cdSdrh pOrigVfs = sqlite3_vfs_find(zOrigVfsName); 664748a52cdSdrh if( pOrigVfs==0 ) return SQLITE_ERROR; 6658cf35eb4Sdan assert( pOrigVfs!=&gQuota.sThisVfs ); 6668cf35eb4Sdan gQuota.pMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); 6678cf35eb4Sdan if( !gQuota.pMutex ){ 6688cf35eb4Sdan return SQLITE_NOMEM; 6698cf35eb4Sdan } 6708cf35eb4Sdan gQuota.isInitialized = 1; 671748a52cdSdrh gQuota.pOrigVfs = pOrigVfs; 672748a52cdSdrh gQuota.sThisVfs = *pOrigVfs; 673748a52cdSdrh gQuota.sThisVfs.xOpen = quotaOpen; 674796af156Sdrh gQuota.sThisVfs.xDelete = quotaDelete; 675ef9659baSdrh gQuota.sThisVfs.szOsFile += sizeof(quotaConn); 676748a52cdSdrh gQuota.sThisVfs.zName = "quota"; 677748a52cdSdrh gQuota.sIoMethodsV1.iVersion = 1; 678748a52cdSdrh gQuota.sIoMethodsV1.xClose = quotaClose; 679748a52cdSdrh gQuota.sIoMethodsV1.xRead = quotaRead; 680748a52cdSdrh gQuota.sIoMethodsV1.xWrite = quotaWrite; 681748a52cdSdrh gQuota.sIoMethodsV1.xTruncate = quotaTruncate; 682748a52cdSdrh gQuota.sIoMethodsV1.xSync = quotaSync; 683748a52cdSdrh gQuota.sIoMethodsV1.xFileSize = quotaFileSize; 684748a52cdSdrh gQuota.sIoMethodsV1.xLock = quotaLock; 685748a52cdSdrh gQuota.sIoMethodsV1.xUnlock = quotaUnlock; 686748a52cdSdrh gQuota.sIoMethodsV1.xCheckReservedLock = quotaCheckReservedLock; 687748a52cdSdrh gQuota.sIoMethodsV1.xFileControl = quotaFileControl; 6888cf35eb4Sdan gQuota.sIoMethodsV1.xSectorSize = quotaSectorSize; 689748a52cdSdrh gQuota.sIoMethodsV1.xDeviceCharacteristics = quotaDeviceCharacteristics; 690748a52cdSdrh gQuota.sIoMethodsV2 = gQuota.sIoMethodsV1; 691748a52cdSdrh gQuota.sIoMethodsV2.iVersion = 2; 692748a52cdSdrh gQuota.sIoMethodsV2.xShmMap = quotaShmMap; 693748a52cdSdrh gQuota.sIoMethodsV2.xShmLock = quotaShmLock; 694748a52cdSdrh gQuota.sIoMethodsV2.xShmBarrier = quotaShmBarrier; 695748a52cdSdrh gQuota.sIoMethodsV2.xShmUnmap = quotaShmUnmap; 6968cf35eb4Sdan sqlite3_vfs_register(&gQuota.sThisVfs, makeDefault); 6978cf35eb4Sdan return SQLITE_OK; 698748a52cdSdrh } 699748a52cdSdrh 700748a52cdSdrh /* 701748a52cdSdrh ** Shutdown the quota system. 702748a52cdSdrh ** 703748a52cdSdrh ** All SQLite database connections must be closed before calling this 704748a52cdSdrh ** routine. 705748a52cdSdrh ** 706796af156Sdrh ** THIS ROUTINE IS NOT THREADSAFE. Call this routine exactly once while 707748a52cdSdrh ** shutting down in order to free all remaining quota groups. 708748a52cdSdrh */ 709748a52cdSdrh int sqlite3_quota_shutdown(void){ 71075c19174Sdrh quotaGroup *pGroup; 711748a52cdSdrh if( gQuota.isInitialized==0 ) return SQLITE_MISUSE; 71275c19174Sdrh for(pGroup=gQuota.pGroup; pGroup; pGroup=pGroup->pNext){ 713796af156Sdrh if( quotaGroupOpenFileCount(pGroup)>0 ) return SQLITE_MISUSE; 7148cf35eb4Sdan } 7158cf35eb4Sdan while( gQuota.pGroup ){ 71675c19174Sdrh pGroup = gQuota.pGroup; 71775c19174Sdrh gQuota.pGroup = pGroup->pNext; 7189fc7702cSdan pGroup->iLimit = 0; 719796af156Sdrh assert( quotaGroupOpenFileCount(pGroup)==0 ); 7209fc7702cSdan quotaGroupDeref(pGroup); 7218cf35eb4Sdan } 722748a52cdSdrh gQuota.isInitialized = 0; 723748a52cdSdrh sqlite3_mutex_free(gQuota.pMutex); 7248cf35eb4Sdan sqlite3_vfs_unregister(&gQuota.sThisVfs); 725748a52cdSdrh memset(&gQuota, 0, sizeof(gQuota)); 7268cf35eb4Sdan return SQLITE_OK; 727748a52cdSdrh } 728748a52cdSdrh 729748a52cdSdrh /* 730748a52cdSdrh ** Create or destroy a quota group. 731748a52cdSdrh ** 732748a52cdSdrh ** The quota group is defined by the zPattern. When calling this routine 733748a52cdSdrh ** with a zPattern for a quota group that already exists, this routine 734748a52cdSdrh ** merely updates the iLimit, xCallback, and pArg values for that quota 735748a52cdSdrh ** group. If zPattern is new, then a new quota group is created. 736748a52cdSdrh ** 737748a52cdSdrh ** If the iLimit for a quota group is set to zero, then the quota group 738748a52cdSdrh ** is disabled and will be deleted when the last database connection using 739748a52cdSdrh ** the quota group is closed. 740748a52cdSdrh ** 741748a52cdSdrh ** Calling this routine on a zPattern that does not exist and with a 742748a52cdSdrh ** zero iLimit is a no-op. 743748a52cdSdrh ** 744748a52cdSdrh ** A quota group must exist with a non-zero iLimit prior to opening 745748a52cdSdrh ** database connections if those connections are to participate in the 7468cf35eb4Sdan ** quota group. Creating a quota group does not affect database connections 747748a52cdSdrh ** that are already open. 748748a52cdSdrh */ 749748a52cdSdrh int sqlite3_quota_set( 750748a52cdSdrh const char *zPattern, /* The filename pattern */ 751748a52cdSdrh sqlite3_int64 iLimit, /* New quota to set for this quota group */ 752748a52cdSdrh void (*xCallback)( /* Callback invoked when going over quota */ 753748a52cdSdrh const char *zFilename, /* Name of file whose size increases */ 754748a52cdSdrh sqlite3_int64 *piLimit, /* IN/OUT: The current limit */ 755748a52cdSdrh sqlite3_int64 iSize, /* Total size of all files in the group */ 756748a52cdSdrh void *pArg /* Client data */ 7578cf35eb4Sdan ), 758ef9659baSdrh void *pArg, /* client data passed thru to callback */ 759ef9659baSdrh void (*xDestroy)(void*) /* Optional destructor for pArg */ 760748a52cdSdrh ){ 761748a52cdSdrh quotaGroup *pGroup; 762748a52cdSdrh quotaEnter(); 763748a52cdSdrh pGroup = gQuota.pGroup; 764748a52cdSdrh while( pGroup && strcmp(pGroup->zPattern, zPattern)!=0 ){ 765748a52cdSdrh pGroup = pGroup->pNext; 766748a52cdSdrh } 76761cc423aSdrh if( pGroup==0 ){ 768748a52cdSdrh int nPattern = strlen(zPattern); 76961cc423aSdrh if( iLimit<=0 ){ 77061cc423aSdrh quotaLeave(); 77161cc423aSdrh return SQLITE_OK; 77261cc423aSdrh } 773b0a32832Sdan pGroup = (quotaGroup *)sqlite3_malloc( sizeof(*pGroup) + nPattern + 1 ); 774748a52cdSdrh if( pGroup==0 ){ 775748a52cdSdrh quotaLeave(); 776748a52cdSdrh return SQLITE_NOMEM; 777748a52cdSdrh } 778748a52cdSdrh memset(pGroup, 0, sizeof(*pGroup)); 779748a52cdSdrh pGroup->zPattern = (char*)&pGroup[1]; 7808cf35eb4Sdan memcpy((char *)pGroup->zPattern, zPattern, nPattern+1); 781748a52cdSdrh if( gQuota.pGroup ) gQuota.pGroup->ppPrev = &pGroup->pNext; 7828cf35eb4Sdan pGroup->pNext = gQuota.pGroup; 7838cf35eb4Sdan pGroup->ppPrev = &gQuota.pGroup; 7848cf35eb4Sdan gQuota.pGroup = pGroup; 785748a52cdSdrh } 786748a52cdSdrh pGroup->iLimit = iLimit; 787748a52cdSdrh pGroup->xCallback = xCallback; 788ef9659baSdrh if( pGroup->xDestroy && pGroup->pArg!=pArg ){ 789ef9659baSdrh pGroup->xDestroy(pGroup->pArg); 790ef9659baSdrh } 791748a52cdSdrh pGroup->pArg = pArg; 792ef9659baSdrh pGroup->xDestroy = xDestroy; 793748a52cdSdrh quotaGroupDeref(pGroup); 794748a52cdSdrh quotaLeave(); 795748a52cdSdrh return SQLITE_OK; 796748a52cdSdrh } 797748a52cdSdrh 798b6020c4eSdrh /* 799b6020c4eSdrh ** Bring the named file under quota management. Or if it is already under 800b6020c4eSdrh ** management, update its size. 801b6020c4eSdrh */ 802b6020c4eSdrh int sqlite3_quota_file(const char *zFilename){ 803b6020c4eSdrh char *zFull; 804b6020c4eSdrh sqlite3_file *fd; 805b6020c4eSdrh int rc; 806b6020c4eSdrh int outFlags = 0; 807b6020c4eSdrh sqlite3_int64 iSize; 808b6020c4eSdrh fd = sqlite3_malloc(gQuota.sThisVfs.szOsFile + gQuota.sThisVfs.mxPathname+1); 809b6020c4eSdrh if( fd==0 ) return SQLITE_NOMEM; 810b6020c4eSdrh zFull = gQuota.sThisVfs.szOsFile + (char*)fd; 811b6020c4eSdrh rc = gQuota.pOrigVfs->xFullPathname(gQuota.pOrigVfs, zFilename, 812b6020c4eSdrh gQuota.sThisVfs.mxPathname+1, zFull); 813b6020c4eSdrh if( rc==SQLITE_OK ){ 814b6020c4eSdrh rc = quotaOpen(&gQuota.sThisVfs, zFull, fd, 815b6020c4eSdrh SQLITE_OPEN_READONLY | SQLITE_OPEN_MAIN_DB, &outFlags); 816b6020c4eSdrh } 817b6020c4eSdrh if( rc==SQLITE_OK ){ 818*26541c9aSdrh fd->pMethods->xFileSize(fd, &iSize); 819*26541c9aSdrh fd->pMethods->xClose(fd); 820b6020c4eSdrh }else if( rc==SQLITE_CANTOPEN ){ 821b6020c4eSdrh quotaGroup *pGroup; 822b6020c4eSdrh quotaFile *pFile; 823b6020c4eSdrh quotaEnter(); 824b6020c4eSdrh pGroup = quotaGroupFind(zFull); 825b6020c4eSdrh if( pGroup ){ 826b6020c4eSdrh pFile = quotaFindFile(pGroup, zFull); 827b6020c4eSdrh if( pFile ) quotaRemoveFile(pFile); 828b6020c4eSdrh } 829b6020c4eSdrh quotaLeave(); 830b6020c4eSdrh } 831b6020c4eSdrh sqlite3_free(fd); 832b6020c4eSdrh return rc; 833b6020c4eSdrh } 834b6020c4eSdrh 835748a52cdSdrh 836748a52cdSdrh /***************************** Test Code ***********************************/ 837748a52cdSdrh #ifdef SQLITE_TEST 838748a52cdSdrh #include <tcl.h> 8398cf35eb4Sdan 840ef9659baSdrh /* 841ef9659baSdrh ** Argument passed to a TCL quota-over-limit callback. 842ef9659baSdrh */ 8438cf35eb4Sdan typedef struct TclQuotaCallback TclQuotaCallback; 8448cf35eb4Sdan struct TclQuotaCallback { 845ef9659baSdrh Tcl_Interp *interp; /* Interpreter in which to run the script */ 846ef9659baSdrh Tcl_Obj *pScript; /* Script to be run */ 8478cf35eb4Sdan }; 8488cf35eb4Sdan 8498cf35eb4Sdan extern const char *sqlite3TestErrorName(int); 8508cf35eb4Sdan 851ef9659baSdrh 852ef9659baSdrh /* 853ef9659baSdrh ** This is the callback from a quota-over-limit. 854ef9659baSdrh */ 8558cf35eb4Sdan static void tclQuotaCallback( 8568cf35eb4Sdan const char *zFilename, /* Name of file whose size increases */ 8578cf35eb4Sdan sqlite3_int64 *piLimit, /* IN/OUT: The current limit */ 8588cf35eb4Sdan sqlite3_int64 iSize, /* Total size of all files in the group */ 8598cf35eb4Sdan void *pArg /* Client data */ 8608cf35eb4Sdan ){ 8618cf35eb4Sdan TclQuotaCallback *p; /* Callback script object */ 8628cf35eb4Sdan Tcl_Obj *pEval; /* Script to evaluate */ 8638cf35eb4Sdan Tcl_Obj *pVarname; /* Name of variable to pass as 2nd arg */ 8648cf35eb4Sdan unsigned int rnd; /* Random part of pVarname */ 8658cf35eb4Sdan int rc; /* Tcl error code */ 8668cf35eb4Sdan 8678cf35eb4Sdan p = (TclQuotaCallback *)pArg; 8683d3e60e4Sdrh if( p==0 ) return; 8698cf35eb4Sdan 8708cf35eb4Sdan pVarname = Tcl_NewStringObj("::piLimit_", -1); 8718cf35eb4Sdan Tcl_IncrRefCount(pVarname); 8728cf35eb4Sdan sqlite3_randomness(sizeof(rnd), (void *)&rnd); 8738cf35eb4Sdan Tcl_AppendObjToObj(pVarname, Tcl_NewIntObj((int)(rnd&0x7FFFFFFF))); 8748cf35eb4Sdan Tcl_ObjSetVar2(p->interp, pVarname, 0, Tcl_NewWideIntObj(*piLimit), 0); 8758cf35eb4Sdan 8768cf35eb4Sdan pEval = Tcl_DuplicateObj(p->pScript); 8778cf35eb4Sdan Tcl_IncrRefCount(pEval); 8788cf35eb4Sdan Tcl_ListObjAppendElement(0, pEval, Tcl_NewStringObj(zFilename, -1)); 8798cf35eb4Sdan Tcl_ListObjAppendElement(0, pEval, pVarname); 8808cf35eb4Sdan Tcl_ListObjAppendElement(0, pEval, Tcl_NewWideIntObj(iSize)); 8818cf35eb4Sdan rc = Tcl_EvalObjEx(p->interp, pEval, TCL_EVAL_GLOBAL); 8828cf35eb4Sdan 8838cf35eb4Sdan if( rc==TCL_OK ){ 8848cf35eb4Sdan Tcl_Obj *pLimit = Tcl_ObjGetVar2(p->interp, pVarname, 0, 0); 8858cf35eb4Sdan rc = Tcl_GetWideIntFromObj(p->interp, pLimit, piLimit); 8868cf35eb4Sdan Tcl_UnsetVar(p->interp, Tcl_GetString(pVarname), 0); 8878cf35eb4Sdan } 8888cf35eb4Sdan 8898cf35eb4Sdan Tcl_DecrRefCount(pEval); 8908cf35eb4Sdan Tcl_DecrRefCount(pVarname); 8918cf35eb4Sdan if( rc!=TCL_OK ) Tcl_BackgroundError(p->interp); 8928cf35eb4Sdan } 8938cf35eb4Sdan 8948cf35eb4Sdan /* 895ef9659baSdrh ** Destructor for a TCL quota-over-limit callback. 896ef9659baSdrh */ 897ef9659baSdrh static void tclCallbackDestructor(void *pObj){ 898ef9659baSdrh TclQuotaCallback *p = (TclQuotaCallback*)pObj; 899ef9659baSdrh if( p ){ 900ef9659baSdrh Tcl_DecrRefCount(p->pScript); 9019fc7702cSdan sqlite3_free((char *)p); 902ef9659baSdrh } 903ef9659baSdrh } 904ef9659baSdrh 905ef9659baSdrh /* 9068cf35eb4Sdan ** tclcmd: sqlite3_quota_initialize NAME MAKEDEFAULT 9078cf35eb4Sdan */ 9088cf35eb4Sdan static int test_quota_initialize( 9098cf35eb4Sdan void * clientData, 9108cf35eb4Sdan Tcl_Interp *interp, 9118cf35eb4Sdan int objc, 9128cf35eb4Sdan Tcl_Obj *CONST objv[] 9138cf35eb4Sdan ){ 9148cf35eb4Sdan const char *zName; /* Name of new quota VFS */ 9158cf35eb4Sdan int makeDefault; /* True to make the new VFS the default */ 9168cf35eb4Sdan int rc; /* Value returned by quota_initialize() */ 9178cf35eb4Sdan 9188cf35eb4Sdan /* Process arguments */ 9198cf35eb4Sdan if( objc!=3 ){ 9208cf35eb4Sdan Tcl_WrongNumArgs(interp, 1, objv, "NAME MAKEDEFAULT"); 9218cf35eb4Sdan return TCL_ERROR; 9228cf35eb4Sdan } 9238cf35eb4Sdan zName = Tcl_GetString(objv[1]); 9248cf35eb4Sdan if( Tcl_GetBooleanFromObj(interp, objv[2], &makeDefault) ) return TCL_ERROR; 9258cf35eb4Sdan if( zName[0]=='\0' ) zName = 0; 9268cf35eb4Sdan 9278cf35eb4Sdan /* Call sqlite3_quota_initialize() */ 9288cf35eb4Sdan rc = sqlite3_quota_initialize(zName, makeDefault); 9298cf35eb4Sdan Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC); 9308cf35eb4Sdan 9318cf35eb4Sdan return TCL_OK; 9328cf35eb4Sdan } 9338cf35eb4Sdan 9348cf35eb4Sdan /* 9358cf35eb4Sdan ** tclcmd: sqlite3_quota_shutdown 9368cf35eb4Sdan */ 9378cf35eb4Sdan static int test_quota_shutdown( 9388cf35eb4Sdan void * clientData, 9398cf35eb4Sdan Tcl_Interp *interp, 9408cf35eb4Sdan int objc, 9418cf35eb4Sdan Tcl_Obj *CONST objv[] 9428cf35eb4Sdan ){ 9438cf35eb4Sdan int rc; /* Value returned by quota_shutdown() */ 9448cf35eb4Sdan 9458cf35eb4Sdan if( objc!=1 ){ 9468cf35eb4Sdan Tcl_WrongNumArgs(interp, 1, objv, ""); 9478cf35eb4Sdan return TCL_ERROR; 9488cf35eb4Sdan } 9498cf35eb4Sdan 9508cf35eb4Sdan /* Call sqlite3_quota_shutdown() */ 9518cf35eb4Sdan rc = sqlite3_quota_shutdown(); 9528cf35eb4Sdan Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC); 9538cf35eb4Sdan 9548cf35eb4Sdan return TCL_OK; 9558cf35eb4Sdan } 9568cf35eb4Sdan 9578cf35eb4Sdan /* 9588cf35eb4Sdan ** tclcmd: sqlite3_quota_set PATTERN LIMIT SCRIPT 9598cf35eb4Sdan */ 9608cf35eb4Sdan static int test_quota_set( 9618cf35eb4Sdan void * clientData, 9628cf35eb4Sdan Tcl_Interp *interp, 9638cf35eb4Sdan int objc, 9648cf35eb4Sdan Tcl_Obj *CONST objv[] 9658cf35eb4Sdan ){ 9668cf35eb4Sdan const char *zPattern; /* File pattern to configure */ 9678cf35eb4Sdan sqlite3_int64 iLimit; /* Initial quota in bytes */ 9688cf35eb4Sdan Tcl_Obj *pScript; /* Tcl script to invoke to increase quota */ 9698cf35eb4Sdan int rc; /* Value returned by quota_set() */ 9708cf35eb4Sdan TclQuotaCallback *p; /* Callback object */ 971ef9659baSdrh int nScript; /* Length of callback script */ 9729fc7702cSdan void (*xDestroy)(void*); /* Optional destructor for pArg */ 97306bfd36aSdan void (*xCallback)(const char *, sqlite3_int64 *, sqlite3_int64, void *); 9748cf35eb4Sdan 9758cf35eb4Sdan /* Process arguments */ 9768cf35eb4Sdan if( objc!=4 ){ 9778cf35eb4Sdan Tcl_WrongNumArgs(interp, 1, objv, "PATTERN LIMIT SCRIPT"); 9788cf35eb4Sdan return TCL_ERROR; 9798cf35eb4Sdan } 9808cf35eb4Sdan zPattern = Tcl_GetString(objv[1]); 9818cf35eb4Sdan if( Tcl_GetWideIntFromObj(interp, objv[2], &iLimit) ) return TCL_ERROR; 9828cf35eb4Sdan pScript = objv[3]; 983ef9659baSdrh Tcl_GetStringFromObj(pScript, &nScript); 9848cf35eb4Sdan 985ef9659baSdrh if( nScript>0 ){ 9868cf35eb4Sdan /* Allocate a TclQuotaCallback object */ 9879fc7702cSdan p = (TclQuotaCallback *)sqlite3_malloc(sizeof(TclQuotaCallback)); 9889fc7702cSdan if( !p ){ 9899fc7702cSdan Tcl_SetResult(interp, (char *)"SQLITE_NOMEM", TCL_STATIC); 9909fc7702cSdan return TCL_OK; 9919fc7702cSdan } 9928cf35eb4Sdan memset(p, 0, sizeof(TclQuotaCallback)); 9938cf35eb4Sdan p->interp = interp; 994ef9659baSdrh Tcl_IncrRefCount(pScript); 9958cf35eb4Sdan p->pScript = pScript; 9969fc7702cSdan xDestroy = tclCallbackDestructor; 99706bfd36aSdan xCallback = tclQuotaCallback; 998ef9659baSdrh }else{ 999ef9659baSdrh p = 0; 10009fc7702cSdan xDestroy = 0; 100106bfd36aSdan xCallback = 0; 10028cf35eb4Sdan } 10039fc7702cSdan 1004ef9659baSdrh /* Invoke sqlite3_quota_set() */ 100506bfd36aSdan rc = sqlite3_quota_set(zPattern, iLimit, xCallback, (void*)p, xDestroy); 10068cf35eb4Sdan 10078cf35eb4Sdan Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC); 10088cf35eb4Sdan return TCL_OK; 10098cf35eb4Sdan } 10108cf35eb4Sdan 10118cf35eb4Sdan /* 1012b6020c4eSdrh ** tclcmd: sqlite3_quota_file FILENAME 1013b6020c4eSdrh */ 1014b6020c4eSdrh static int test_quota_file( 1015b6020c4eSdrh void * clientData, 1016b6020c4eSdrh Tcl_Interp *interp, 1017b6020c4eSdrh int objc, 1018b6020c4eSdrh Tcl_Obj *CONST objv[] 1019b6020c4eSdrh ){ 1020b6020c4eSdrh const char *zFilename; /* File pattern to configure */ 1021b6020c4eSdrh int rc; /* Value returned by quota_file() */ 1022b6020c4eSdrh 1023b6020c4eSdrh /* Process arguments */ 1024b6020c4eSdrh if( objc!=2 ){ 1025b6020c4eSdrh Tcl_WrongNumArgs(interp, 1, objv, "FILENAME"); 1026b6020c4eSdrh return TCL_ERROR; 1027b6020c4eSdrh } 1028b6020c4eSdrh zFilename = Tcl_GetString(objv[1]); 1029b6020c4eSdrh 1030b6020c4eSdrh /* Invoke sqlite3_quota_file() */ 1031b6020c4eSdrh rc = sqlite3_quota_file(zFilename); 1032b6020c4eSdrh 1033b6020c4eSdrh Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC); 1034b6020c4eSdrh return TCL_OK; 1035b6020c4eSdrh } 1036b6020c4eSdrh 1037b6020c4eSdrh /* 1038ef9659baSdrh ** tclcmd: sqlite3_quota_dump 1039ef9659baSdrh */ 1040ef9659baSdrh static int test_quota_dump( 1041ef9659baSdrh void * clientData, 1042ef9659baSdrh Tcl_Interp *interp, 1043ef9659baSdrh int objc, 1044ef9659baSdrh Tcl_Obj *CONST objv[] 1045ef9659baSdrh ){ 1046ef9659baSdrh Tcl_Obj *pResult; 1047ef9659baSdrh Tcl_Obj *pGroupTerm; 1048ef9659baSdrh Tcl_Obj *pFileTerm; 1049ef9659baSdrh quotaGroup *pGroup; 1050ef9659baSdrh quotaFile *pFile; 1051ef9659baSdrh 1052ef9659baSdrh pResult = Tcl_NewObj(); 1053ef9659baSdrh quotaEnter(); 1054ef9659baSdrh for(pGroup=gQuota.pGroup; pGroup; pGroup=pGroup->pNext){ 1055ef9659baSdrh pGroupTerm = Tcl_NewObj(); 1056ef9659baSdrh Tcl_ListObjAppendElement(interp, pGroupTerm, 1057ef9659baSdrh Tcl_NewStringObj(pGroup->zPattern, -1)); 1058ef9659baSdrh Tcl_ListObjAppendElement(interp, pGroupTerm, 1059ef9659baSdrh Tcl_NewWideIntObj(pGroup->iLimit)); 1060ef9659baSdrh Tcl_ListObjAppendElement(interp, pGroupTerm, 1061ef9659baSdrh Tcl_NewWideIntObj(pGroup->iSize)); 106275c19174Sdrh for(pFile=pGroup->pFiles; pFile; pFile=pFile->pNext){ 1063ef9659baSdrh pFileTerm = Tcl_NewObj(); 1064ef9659baSdrh Tcl_ListObjAppendElement(interp, pFileTerm, 1065ef9659baSdrh Tcl_NewStringObj(pFile->zFilename, -1)); 1066ef9659baSdrh Tcl_ListObjAppendElement(interp, pFileTerm, 1067ef9659baSdrh Tcl_NewWideIntObj(pFile->iSize)); 1068ef9659baSdrh Tcl_ListObjAppendElement(interp, pFileTerm, 1069ef9659baSdrh Tcl_NewWideIntObj(pFile->nRef)); 1070796af156Sdrh Tcl_ListObjAppendElement(interp, pFileTerm, 1071796af156Sdrh Tcl_NewWideIntObj(pFile->deleteOnClose)); 1072ef9659baSdrh Tcl_ListObjAppendElement(interp, pGroupTerm, pFileTerm); 1073ef9659baSdrh } 1074ef9659baSdrh Tcl_ListObjAppendElement(interp, pResult, pGroupTerm); 1075ef9659baSdrh } 1076ef9659baSdrh quotaLeave(); 1077ef9659baSdrh Tcl_SetObjResult(interp, pResult); 1078ef9659baSdrh return TCL_OK; 1079ef9659baSdrh } 1080ef9659baSdrh 1081748a52cdSdrh /* 1082748a52cdSdrh ** This routine registers the custom TCL commands defined in this 1083748a52cdSdrh ** module. This should be the only procedure visible from outside 1084748a52cdSdrh ** of this module. 1085748a52cdSdrh */ 1086748a52cdSdrh int Sqlitequota_Init(Tcl_Interp *interp){ 10878cf35eb4Sdan static struct { 10888cf35eb4Sdan char *zName; 10898cf35eb4Sdan Tcl_ObjCmdProc *xProc; 10908cf35eb4Sdan } aCmd[] = { 10918cf35eb4Sdan { "sqlite3_quota_initialize", test_quota_initialize }, 10928cf35eb4Sdan { "sqlite3_quota_shutdown", test_quota_shutdown }, 10938cf35eb4Sdan { "sqlite3_quota_set", test_quota_set }, 1094b6020c4eSdrh { "sqlite3_quota_file", test_quota_file }, 1095ef9659baSdrh { "sqlite3_quota_dump", test_quota_dump }, 10968cf35eb4Sdan }; 10978cf35eb4Sdan int i; 10988cf35eb4Sdan 10998cf35eb4Sdan for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){ 11008cf35eb4Sdan Tcl_CreateObjCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0); 11018cf35eb4Sdan } 11028cf35eb4Sdan 1103748a52cdSdrh return TCL_OK; 1104748a52cdSdrh } 1105748a52cdSdrh #endif 1106