1748a52cdSdrh /* 2748a52cdSdrh ** 2010 September 31 3748a52cdSdrh ** 4748a52cdSdrh ** The author disclaims copyright to this source code. In place of 5748a52cdSdrh ** a legal notice, here is a blessing: 6748a52cdSdrh ** 7748a52cdSdrh ** May you do good and not evil. 8748a52cdSdrh ** May you find forgiveness for yourself and forgive others. 9748a52cdSdrh ** May you share freely, never taking more than you give. 10748a52cdSdrh ** 11748a52cdSdrh ************************************************************************* 12748a52cdSdrh ** 13748a52cdSdrh ** This file contains a VFS "shim" - a layer that sits in between the 14748a52cdSdrh ** pager and the real VFS. 15748a52cdSdrh ** 16748a52cdSdrh ** This particular shim enforces a quota system on files. One or more 17748a52cdSdrh ** database files are in a "quota group" that is defined by a GLOB 18748a52cdSdrh ** pattern. A quota is set for the combined size of all files in the 19748a52cdSdrh ** the group. A quota of zero means "no limit". If the total size 209237a258Sdrh ** of all files in the quota group is greater than the limit, then 219237a258Sdrh ** write requests that attempt to enlarge a file fail with SQLITE_FULL. 22748a52cdSdrh ** 23748a52cdSdrh ** However, before returning SQLITE_FULL, the write requests invoke 24748a52cdSdrh ** a callback function that is configurable for each quota group. 25748a52cdSdrh ** This callback has the opportunity to enlarge the quota. If the 26748a52cdSdrh ** callback does enlarge the quota such that the total size of all 27748a52cdSdrh ** files within the group is less than the new quota, then the write 28748a52cdSdrh ** continues as if nothing had happened. 29748a52cdSdrh */ 3060f21e4bSdrh #include "test_quota.h" 318cf35eb4Sdan #include <string.h> 328cf35eb4Sdan #include <assert.h> 33748a52cdSdrh 343778ac15Sdrh /* 353778ac15Sdrh ** For an build without mutexes, no-op the mutex calls. 363778ac15Sdrh */ 373778ac15Sdrh #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE==0 383778ac15Sdrh #define sqlite3_mutex_alloc(X) ((sqlite3_mutex*)8) 393778ac15Sdrh #define sqlite3_mutex_free(X) 403778ac15Sdrh #define sqlite3_mutex_enter(X) 413778ac15Sdrh #define sqlite3_mutex_try(X) SQLITE_OK 423778ac15Sdrh #define sqlite3_mutex_leave(X) 433778ac15Sdrh #define sqlite3_mutex_held(X) ((void)(X),1) 443778ac15Sdrh #define sqlite3_mutex_notheld(X) ((void)(X),1) 453778ac15Sdrh #endif /* SQLITE_THREADSAFE==0 */ 463778ac15Sdrh 473778ac15Sdrh 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 11460f21e4bSdrh /* 11560f21e4bSdrh ** An instance of the following object records the state of an 11660f21e4bSdrh ** open file. This object is opaque to all users - the internal 11760f21e4bSdrh ** structure is only visible to the functions below. 11860f21e4bSdrh */ 11960f21e4bSdrh struct quota_FILE { 12060f21e4bSdrh FILE *f; /* Open stdio file pointer */ 12160f21e4bSdrh sqlite3_int64 iOfst; /* Current offset into the file */ 12260f21e4bSdrh quotaFile *pFile; /* The file record in the quota system */ 12360f21e4bSdrh }; 12460f21e4bSdrh 12560f21e4bSdrh 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 ** 240*a76e8589Sdrh ** / Matches "/" or "\\" 241*a76e8589Sdrh ** 242748a52cdSdrh */ 24361cc423aSdrh static int quotaStrglob(const char *zGlob, const char *z){ 244748a52cdSdrh int c, c2; 245748a52cdSdrh int invert; 246748a52cdSdrh int seen; 247748a52cdSdrh 248748a52cdSdrh while( (c = (*(zGlob++)))!=0 ){ 249748a52cdSdrh if( c=='*' ){ 250748a52cdSdrh while( (c=(*(zGlob++))) == '*' || c=='?' ){ 251748a52cdSdrh if( c=='?' && (*(z++))==0 ) return 0; 252748a52cdSdrh } 253748a52cdSdrh if( c==0 ){ 254748a52cdSdrh return 1; 255748a52cdSdrh }else if( c=='[' ){ 25661cc423aSdrh while( *z && quotaStrglob(zGlob-1,z)==0 ){ 257748a52cdSdrh z++; 258748a52cdSdrh } 259748a52cdSdrh return (*z)!=0; 260748a52cdSdrh } 261748a52cdSdrh while( (c2 = (*(z++)))!=0 ){ 262748a52cdSdrh while( c2!=c ){ 263748a52cdSdrh c2 = *(z++); 264748a52cdSdrh if( c2==0 ) return 0; 265748a52cdSdrh } 26661cc423aSdrh if( quotaStrglob(zGlob,z) ) return 1; 267748a52cdSdrh } 268748a52cdSdrh return 0; 269748a52cdSdrh }else if( c=='?' ){ 270748a52cdSdrh if( (*(z++))==0 ) return 0; 271748a52cdSdrh }else if( c=='[' ){ 272748a52cdSdrh int prior_c = 0; 273748a52cdSdrh seen = 0; 274748a52cdSdrh invert = 0; 275748a52cdSdrh c = *(z++); 276748a52cdSdrh if( c==0 ) return 0; 277748a52cdSdrh c2 = *(zGlob++); 278748a52cdSdrh if( c2=='^' ){ 279748a52cdSdrh invert = 1; 280748a52cdSdrh c2 = *(zGlob++); 281748a52cdSdrh } 282748a52cdSdrh if( c2==']' ){ 283748a52cdSdrh if( c==']' ) seen = 1; 284748a52cdSdrh c2 = *(zGlob++); 285748a52cdSdrh } 286748a52cdSdrh while( c2 && c2!=']' ){ 287748a52cdSdrh if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){ 288748a52cdSdrh c2 = *(zGlob++); 289748a52cdSdrh if( c>=prior_c && c<=c2 ) seen = 1; 290748a52cdSdrh prior_c = 0; 291748a52cdSdrh }else{ 292748a52cdSdrh if( c==c2 ){ 293748a52cdSdrh seen = 1; 294748a52cdSdrh } 295748a52cdSdrh prior_c = c2; 296748a52cdSdrh } 297748a52cdSdrh c2 = *(zGlob++); 298748a52cdSdrh } 299748a52cdSdrh if( c2==0 || (seen ^ invert)==0 ) return 0; 300*a76e8589Sdrh }else if( c=='/' ){ 301*a76e8589Sdrh if( z[0]!='/' && z[0]!='\\' ) return 0; 302*a76e8589Sdrh z++; 303748a52cdSdrh }else{ 304748a52cdSdrh if( c!=(*(z++)) ) return 0; 305748a52cdSdrh } 306748a52cdSdrh } 307748a52cdSdrh return *z==0; 308748a52cdSdrh } 309748a52cdSdrh 310748a52cdSdrh 311748a52cdSdrh /* Find a quotaGroup given the filename. 3128cf35eb4Sdan ** 313ef9659baSdrh ** Return a pointer to the quotaGroup object. Return NULL if not found. 314748a52cdSdrh */ 315748a52cdSdrh static quotaGroup *quotaGroupFind(const char *zFilename){ 316748a52cdSdrh quotaGroup *p; 31761cc423aSdrh for(p=gQuota.pGroup; p && quotaStrglob(p->zPattern, zFilename)==0; 31861cc423aSdrh p=p->pNext){} 319748a52cdSdrh return p; 320748a52cdSdrh } 321748a52cdSdrh 322ef9659baSdrh /* Translate an sqlite3_file* that is really a quotaConn* into 323549dc1baSdrh ** the sqlite3_file* for the underlying original VFS. 324748a52cdSdrh */ 325ef9659baSdrh static sqlite3_file *quotaSubOpen(sqlite3_file *pConn){ 326ef9659baSdrh quotaConn *p = (quotaConn*)pConn; 327748a52cdSdrh return (sqlite3_file*)&p[1]; 328748a52cdSdrh } 329748a52cdSdrh 330796af156Sdrh /* Find a file in a quota group and return a pointer to that file. 331796af156Sdrh ** Return NULL if the file is not in the group. 332796af156Sdrh */ 33360f21e4bSdrh static quotaFile *quotaFindFile( 33460f21e4bSdrh quotaGroup *pGroup, /* Group in which to look for the file */ 33560f21e4bSdrh const char *zName, /* Full pathname of the file */ 33660f21e4bSdrh int createFlag /* Try to create the file if not found */ 33760f21e4bSdrh ){ 338796af156Sdrh quotaFile *pFile = pGroup->pFiles; 339796af156Sdrh while( pFile && strcmp(pFile->zFilename, zName)!=0 ){ 340796af156Sdrh pFile = pFile->pNext; 341796af156Sdrh } 34260f21e4bSdrh if( pFile==0 && createFlag ){ 34360f21e4bSdrh int nName = strlen(zName); 34460f21e4bSdrh pFile = (quotaFile *)sqlite3_malloc( sizeof(*pFile) + nName + 1 ); 34560f21e4bSdrh if( pFile ){ 34660f21e4bSdrh memset(pFile, 0, sizeof(*pFile)); 34760f21e4bSdrh pFile->zFilename = (char*)&pFile[1]; 34860f21e4bSdrh memcpy(pFile->zFilename, zName, nName+1); 34960f21e4bSdrh pFile->pNext = pGroup->pFiles; 35060f21e4bSdrh if( pGroup->pFiles ) pGroup->pFiles->ppPrev = &pFile->pNext; 35160f21e4bSdrh pFile->ppPrev = &pGroup->pFiles; 35260f21e4bSdrh pGroup->pFiles = pFile; 35360f21e4bSdrh pFile->pGroup = pGroup; 35460f21e4bSdrh } 35560f21e4bSdrh } 356796af156Sdrh return pFile; 357796af156Sdrh } 358796af156Sdrh 359*a76e8589Sdrh /* 360*a76e8589Sdrh ** Figure out if we are dealing with Unix, Windows, or some other 361*a76e8589Sdrh ** operating system. After the following block of preprocess macros, 362*a76e8589Sdrh ** all of SQLITE_OS_UNIX, SQLITE_OS_WIN, SQLITE_OS_OS2, and SQLITE_OS_OTHER 363*a76e8589Sdrh ** will defined to either 1 or 0. One of the four will be 1. The other 364*a76e8589Sdrh ** three will be 0. 365*a76e8589Sdrh */ 366*a76e8589Sdrh #if defined(SQLITE_OS_OTHER) 367*a76e8589Sdrh # if SQLITE_OS_OTHER==1 368*a76e8589Sdrh # undef SQLITE_OS_UNIX 369*a76e8589Sdrh # define SQLITE_OS_UNIX 0 370*a76e8589Sdrh # undef SQLITE_OS_WIN 371*a76e8589Sdrh # define SQLITE_OS_WIN 0 372*a76e8589Sdrh # undef SQLITE_OS_OS2 373*a76e8589Sdrh # define SQLITE_OS_OS2 0 374*a76e8589Sdrh # else 375*a76e8589Sdrh # undef SQLITE_OS_OTHER 376*a76e8589Sdrh # endif 377*a76e8589Sdrh #endif 378*a76e8589Sdrh #if !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_OTHER) 379*a76e8589Sdrh # define SQLITE_OS_OTHER 0 380*a76e8589Sdrh # ifndef SQLITE_OS_WIN 381*a76e8589Sdrh # if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) \ 382*a76e8589Sdrh || defined(__MINGW32__) || defined(__BORLANDC__) 383*a76e8589Sdrh # define SQLITE_OS_WIN 1 384*a76e8589Sdrh # define SQLITE_OS_UNIX 0 385*a76e8589Sdrh # define SQLITE_OS_OS2 0 386*a76e8589Sdrh # elif defined(__EMX__) || defined(_OS2) || defined(OS2) \ 387*a76e8589Sdrh || defined(_OS2_) || defined(__OS2__) 388*a76e8589Sdrh # define SQLITE_OS_WIN 0 389*a76e8589Sdrh # define SQLITE_OS_UNIX 0 390*a76e8589Sdrh # define SQLITE_OS_OS2 1 391*a76e8589Sdrh # else 392*a76e8589Sdrh # define SQLITE_OS_WIN 0 393*a76e8589Sdrh # define SQLITE_OS_UNIX 1 394*a76e8589Sdrh # define SQLITE_OS_OS2 0 395*a76e8589Sdrh # endif 396*a76e8589Sdrh # else 397*a76e8589Sdrh # define SQLITE_OS_UNIX 0 398*a76e8589Sdrh # define SQLITE_OS_OS2 0 399*a76e8589Sdrh # endif 400*a76e8589Sdrh #else 401*a76e8589Sdrh # ifndef SQLITE_OS_WIN 402*a76e8589Sdrh # define SQLITE_OS_WIN 0 403*a76e8589Sdrh # endif 404*a76e8589Sdrh #endif 405*a76e8589Sdrh 406*a76e8589Sdrh 407*a76e8589Sdrh /* 408*a76e8589Sdrh ** Translate UTF8 to MBCS for use in fopen() calls. Return a pointer to the 409*a76e8589Sdrh ** translated text.. Call quota_mbcs_free() to deallocate any memory 410*a76e8589Sdrh ** used to store the returned pointer when done. 411*a76e8589Sdrh */ 412*a76e8589Sdrh static char *quota_utf8_to_mbcs(const char *zUtf8){ 413*a76e8589Sdrh #if SQLITE_OS_WIN 414*a76e8589Sdrh extern char *sqlite3_win32_utf8_to_mbcs(const char*); 415*a76e8589Sdrh return sqlite3_win32_utf8_to_mbcs(zUtf8); 416*a76e8589Sdrh #else 417*a76e8589Sdrh return (char*)zUtf8; /* No-op on unix */ 418*a76e8589Sdrh #endif 419*a76e8589Sdrh } 420*a76e8589Sdrh 421*a76e8589Sdrh /* 422*a76e8589Sdrh ** Deallocate any memory allocated by quota_utf8_to_mbcs(). 423*a76e8589Sdrh */ 424*a76e8589Sdrh static void quota_mbcs_free(char *zOld){ 425*a76e8589Sdrh #if SQLITE_OS_WIN 426*a76e8589Sdrh free(zOld); 427*a76e8589Sdrh #else 428*a76e8589Sdrh /* No-op on unix */ 429*a76e8589Sdrh #endif 430*a76e8589Sdrh } 431*a76e8589Sdrh 432748a52cdSdrh /************************* VFS Method Wrappers *****************************/ 433748a52cdSdrh /* 434748a52cdSdrh ** This is the xOpen method used for the "quota" VFS. 435748a52cdSdrh ** 436748a52cdSdrh ** Most of the work is done by the underlying original VFS. This method 437549dc1baSdrh ** simply links the new file into the appropriate quota group if it is a 438549dc1baSdrh ** file that needs to be tracked. 439748a52cdSdrh */ 440748a52cdSdrh static int quotaOpen( 441549dc1baSdrh sqlite3_vfs *pVfs, /* The quota VFS */ 442549dc1baSdrh const char *zName, /* Name of file to be opened */ 443ef9659baSdrh sqlite3_file *pConn, /* Fill in this file descriptor */ 444549dc1baSdrh int flags, /* Flags to control the opening */ 445549dc1baSdrh int *pOutFlags /* Flags showing results of opening */ 446748a52cdSdrh ){ 447549dc1baSdrh int rc; /* Result code */ 448ef9659baSdrh quotaConn *pQuotaOpen; /* The new quota file descriptor */ 449549dc1baSdrh quotaFile *pFile; /* Corresponding quotaFile obj */ 450549dc1baSdrh quotaGroup *pGroup; /* The group file belongs to */ 451549dc1baSdrh sqlite3_file *pSubOpen; /* Real file descriptor */ 452549dc1baSdrh sqlite3_vfs *pOrigVfs = gQuota.pOrigVfs; /* Real VFS */ 453748a52cdSdrh 454748a52cdSdrh /* If the file is not a main database file or a WAL, then use the 455748a52cdSdrh ** normal xOpen method. 456748a52cdSdrh */ 4578cf35eb4Sdan if( (flags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_WAL))==0 ){ 458ef9659baSdrh return pOrigVfs->xOpen(pOrigVfs, zName, pConn, flags, pOutFlags); 459748a52cdSdrh } 460748a52cdSdrh 461748a52cdSdrh /* If the name of the file does not match any quota group, then 462748a52cdSdrh ** use the normal xOpen method. 463748a52cdSdrh */ 464748a52cdSdrh quotaEnter(); 4658cf35eb4Sdan pGroup = quotaGroupFind(zName); 466748a52cdSdrh if( pGroup==0 ){ 467ef9659baSdrh rc = pOrigVfs->xOpen(pOrigVfs, zName, pConn, flags, pOutFlags); 468748a52cdSdrh }else{ 4698cf35eb4Sdan /* If we get to this point, it means the file needs to be quota tracked. 4708cf35eb4Sdan */ 471ef9659baSdrh pQuotaOpen = (quotaConn*)pConn; 472ef9659baSdrh pSubOpen = quotaSubOpen(pConn); 473745de2e4Sdrh rc = pOrigVfs->xOpen(pOrigVfs, zName, pSubOpen, flags, pOutFlags); 4748cf35eb4Sdan if( rc==SQLITE_OK ){ 47560f21e4bSdrh pFile = quotaFindFile(pGroup, zName, 1); 476745de2e4Sdrh if( pFile==0 ){ 477745de2e4Sdrh quotaLeave(); 478745de2e4Sdrh pSubOpen->pMethods->xClose(pSubOpen); 479745de2e4Sdrh return SQLITE_NOMEM; 480745de2e4Sdrh } 481796af156Sdrh pFile->deleteOnClose = (flags & SQLITE_OPEN_DELETEONCLOSE)!=0; 482745de2e4Sdrh pFile->nRef++; 483745de2e4Sdrh pQuotaOpen->pFile = pFile; 484745de2e4Sdrh if( pSubOpen->pMethods->iVersion==1 ){ 485745de2e4Sdrh pQuotaOpen->base.pMethods = &gQuota.sIoMethodsV1; 4868cf35eb4Sdan }else{ 487745de2e4Sdrh pQuotaOpen->base.pMethods = &gQuota.sIoMethodsV2; 488748a52cdSdrh } 489748a52cdSdrh } 4908cf35eb4Sdan } 4918cf35eb4Sdan quotaLeave(); 492748a52cdSdrh return rc; 493748a52cdSdrh } 494748a52cdSdrh 495796af156Sdrh /* 496796af156Sdrh ** This is the xDelete method used for the "quota" VFS. 497796af156Sdrh ** 498796af156Sdrh ** If the file being deleted is part of the quota group, then reduce 499796af156Sdrh ** the size of the quota group accordingly. And remove the file from 500796af156Sdrh ** the set of files in the quota group. 501796af156Sdrh */ 502796af156Sdrh static int quotaDelete( 503796af156Sdrh sqlite3_vfs *pVfs, /* The quota VFS */ 504796af156Sdrh const char *zName, /* Name of file to be deleted */ 505796af156Sdrh int syncDir /* Do a directory sync after deleting */ 506796af156Sdrh ){ 507796af156Sdrh int rc; /* Result code */ 508796af156Sdrh quotaFile *pFile; /* Files in the quota */ 509796af156Sdrh quotaGroup *pGroup; /* The group file belongs to */ 510796af156Sdrh sqlite3_vfs *pOrigVfs = gQuota.pOrigVfs; /* Real VFS */ 511796af156Sdrh 512796af156Sdrh /* Do the actual file delete */ 513796af156Sdrh rc = pOrigVfs->xDelete(pOrigVfs, zName, syncDir); 514796af156Sdrh 515796af156Sdrh /* If the file just deleted is a member of a quota group, then remove 516796af156Sdrh ** it from that quota group. 517796af156Sdrh */ 518796af156Sdrh if( rc==SQLITE_OK ){ 519796af156Sdrh quotaEnter(); 520796af156Sdrh pGroup = quotaGroupFind(zName); 521796af156Sdrh if( pGroup ){ 52260f21e4bSdrh pFile = quotaFindFile(pGroup, zName, 0); 523796af156Sdrh if( pFile ){ 524796af156Sdrh if( pFile->nRef ){ 525796af156Sdrh pFile->deleteOnClose = 1; 526796af156Sdrh }else{ 527796af156Sdrh quotaRemoveFile(pFile); 528796af156Sdrh quotaGroupDeref(pGroup); 529796af156Sdrh } 530796af156Sdrh } 531796af156Sdrh } 532796af156Sdrh quotaLeave(); 533796af156Sdrh } 534796af156Sdrh return rc; 535796af156Sdrh } 536796af156Sdrh 537796af156Sdrh 538748a52cdSdrh /************************ I/O Method Wrappers *******************************/ 539748a52cdSdrh 540748a52cdSdrh /* xClose requests get passed through to the original VFS. But we 541ef9659baSdrh ** also have to unlink the quotaConn from the quotaFile and quotaGroup. 542ef9659baSdrh ** The quotaFile and/or quotaGroup are freed if they are no longer in use. 543748a52cdSdrh */ 544ef9659baSdrh static int quotaClose(sqlite3_file *pConn){ 545ef9659baSdrh quotaConn *p = (quotaConn*)pConn; 546745de2e4Sdrh quotaFile *pFile = p->pFile; 547ef9659baSdrh sqlite3_file *pSubOpen = quotaSubOpen(pConn); 548748a52cdSdrh int rc; 549745de2e4Sdrh rc = pSubOpen->pMethods->xClose(pSubOpen); 550748a52cdSdrh quotaEnter(); 551745de2e4Sdrh pFile->nRef--; 552745de2e4Sdrh if( pFile->nRef==0 ){ 553745de2e4Sdrh quotaGroup *pGroup = pFile->pGroup; 554796af156Sdrh if( pFile->deleteOnClose ) quotaRemoveFile(pFile); 555748a52cdSdrh quotaGroupDeref(pGroup); 556745de2e4Sdrh } 557748a52cdSdrh quotaLeave(); 558748a52cdSdrh return rc; 559748a52cdSdrh } 560748a52cdSdrh 561748a52cdSdrh /* Pass xRead requests directory thru to the original VFS without 562748a52cdSdrh ** further processing. 563748a52cdSdrh */ 564748a52cdSdrh static int quotaRead( 565ef9659baSdrh sqlite3_file *pConn, 566748a52cdSdrh void *pBuf, 567748a52cdSdrh int iAmt, 568748a52cdSdrh sqlite3_int64 iOfst 569748a52cdSdrh ){ 570ef9659baSdrh sqlite3_file *pSubOpen = quotaSubOpen(pConn); 571745de2e4Sdrh return pSubOpen->pMethods->xRead(pSubOpen, pBuf, iAmt, iOfst); 572748a52cdSdrh } 573748a52cdSdrh 574748a52cdSdrh /* Check xWrite requests to see if they expand the file. If they do, 575748a52cdSdrh ** the perform a quota check before passing them through to the 576748a52cdSdrh ** original VFS. 577748a52cdSdrh */ 578748a52cdSdrh static int quotaWrite( 579ef9659baSdrh sqlite3_file *pConn, 5808cf35eb4Sdan const void *pBuf, 581748a52cdSdrh int iAmt, 582748a52cdSdrh sqlite3_int64 iOfst 583748a52cdSdrh ){ 584ef9659baSdrh quotaConn *p = (quotaConn*)pConn; 585ef9659baSdrh sqlite3_file *pSubOpen = quotaSubOpen(pConn); 586748a52cdSdrh sqlite3_int64 iEnd = iOfst+iAmt; 587748a52cdSdrh quotaGroup *pGroup; 588745de2e4Sdrh quotaFile *pFile = p->pFile; 589748a52cdSdrh sqlite3_int64 szNew; 590748a52cdSdrh 591745de2e4Sdrh if( pFile->iSize<iEnd ){ 592745de2e4Sdrh pGroup = pFile->pGroup; 593748a52cdSdrh quotaEnter(); 594745de2e4Sdrh szNew = pGroup->iSize - pFile->iSize + iEnd; 5958cf35eb4Sdan if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){ 596748a52cdSdrh if( pGroup->xCallback ){ 597745de2e4Sdrh pGroup->xCallback(pFile->zFilename, &pGroup->iLimit, szNew, 598748a52cdSdrh pGroup->pArg); 599748a52cdSdrh } 6008cf35eb4Sdan if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){ 601748a52cdSdrh quotaLeave(); 602748a52cdSdrh return SQLITE_FULL; 603748a52cdSdrh } 604748a52cdSdrh } 605748a52cdSdrh pGroup->iSize = szNew; 606745de2e4Sdrh pFile->iSize = iEnd; 607748a52cdSdrh quotaLeave(); 608748a52cdSdrh } 609745de2e4Sdrh return pSubOpen->pMethods->xWrite(pSubOpen, pBuf, iAmt, iOfst); 610748a52cdSdrh } 611748a52cdSdrh 612748a52cdSdrh /* Pass xTruncate requests thru to the original VFS. If the 613748a52cdSdrh ** success, update the file size. 614748a52cdSdrh */ 615ef9659baSdrh static int quotaTruncate(sqlite3_file *pConn, sqlite3_int64 size){ 616ef9659baSdrh quotaConn *p = (quotaConn*)pConn; 617ef9659baSdrh sqlite3_file *pSubOpen = quotaSubOpen(pConn); 618745de2e4Sdrh int rc = pSubOpen->pMethods->xTruncate(pSubOpen, size); 619745de2e4Sdrh quotaFile *pFile = p->pFile; 620745de2e4Sdrh quotaGroup *pGroup; 621748a52cdSdrh if( rc==SQLITE_OK ){ 622748a52cdSdrh quotaEnter(); 623745de2e4Sdrh pGroup = pFile->pGroup; 624745de2e4Sdrh pGroup->iSize -= pFile->iSize; 625745de2e4Sdrh pFile->iSize = size; 626748a52cdSdrh pGroup->iSize += size; 627748a52cdSdrh quotaLeave(); 628748a52cdSdrh } 629748a52cdSdrh return rc; 630748a52cdSdrh } 631748a52cdSdrh 632748a52cdSdrh /* Pass xSync requests through to the original VFS without change 633748a52cdSdrh */ 634ef9659baSdrh static int quotaSync(sqlite3_file *pConn, int flags){ 635ef9659baSdrh sqlite3_file *pSubOpen = quotaSubOpen(pConn); 636745de2e4Sdrh return pSubOpen->pMethods->xSync(pSubOpen, flags); 637748a52cdSdrh } 638748a52cdSdrh 639748a52cdSdrh /* Pass xFileSize requests through to the original VFS but then 640748a52cdSdrh ** update the quotaGroup with the new size before returning. 641748a52cdSdrh */ 642ef9659baSdrh static int quotaFileSize(sqlite3_file *pConn, sqlite3_int64 *pSize){ 643ef9659baSdrh quotaConn *p = (quotaConn*)pConn; 644ef9659baSdrh sqlite3_file *pSubOpen = quotaSubOpen(pConn); 645745de2e4Sdrh quotaFile *pFile = p->pFile; 646748a52cdSdrh quotaGroup *pGroup; 647748a52cdSdrh sqlite3_int64 sz; 648748a52cdSdrh int rc; 649748a52cdSdrh 650745de2e4Sdrh rc = pSubOpen->pMethods->xFileSize(pSubOpen, &sz); 651748a52cdSdrh if( rc==SQLITE_OK ){ 652748a52cdSdrh quotaEnter(); 653745de2e4Sdrh pGroup = pFile->pGroup; 654745de2e4Sdrh pGroup->iSize -= pFile->iSize; 655745de2e4Sdrh pFile->iSize = sz; 656748a52cdSdrh pGroup->iSize += sz; 657748a52cdSdrh quotaLeave(); 658748a52cdSdrh *pSize = sz; 659748a52cdSdrh } 660748a52cdSdrh return rc; 661748a52cdSdrh } 662748a52cdSdrh 663748a52cdSdrh /* Pass xLock requests through to the original VFS unchanged. 664748a52cdSdrh */ 665ef9659baSdrh static int quotaLock(sqlite3_file *pConn, int lock){ 666ef9659baSdrh sqlite3_file *pSubOpen = quotaSubOpen(pConn); 667745de2e4Sdrh return pSubOpen->pMethods->xLock(pSubOpen, lock); 668748a52cdSdrh } 669748a52cdSdrh 670748a52cdSdrh /* Pass xUnlock requests through to the original VFS unchanged. 671748a52cdSdrh */ 672ef9659baSdrh static int quotaUnlock(sqlite3_file *pConn, int lock){ 673ef9659baSdrh sqlite3_file *pSubOpen = quotaSubOpen(pConn); 674745de2e4Sdrh return pSubOpen->pMethods->xUnlock(pSubOpen, lock); 675748a52cdSdrh } 676748a52cdSdrh 677748a52cdSdrh /* Pass xCheckReservedLock requests through to the original VFS unchanged. 678748a52cdSdrh */ 679ef9659baSdrh static int quotaCheckReservedLock(sqlite3_file *pConn, int *pResOut){ 680ef9659baSdrh sqlite3_file *pSubOpen = quotaSubOpen(pConn); 681745de2e4Sdrh return pSubOpen->pMethods->xCheckReservedLock(pSubOpen, pResOut); 682748a52cdSdrh } 683748a52cdSdrh 684748a52cdSdrh /* Pass xFileControl requests through to the original VFS unchanged. 685748a52cdSdrh */ 686ef9659baSdrh static int quotaFileControl(sqlite3_file *pConn, int op, void *pArg){ 687ef9659baSdrh sqlite3_file *pSubOpen = quotaSubOpen(pConn); 688745de2e4Sdrh return pSubOpen->pMethods->xFileControl(pSubOpen, op, pArg); 689748a52cdSdrh } 690748a52cdSdrh 691748a52cdSdrh /* Pass xSectorSize requests through to the original VFS unchanged. 692748a52cdSdrh */ 693ef9659baSdrh static int quotaSectorSize(sqlite3_file *pConn){ 694ef9659baSdrh sqlite3_file *pSubOpen = quotaSubOpen(pConn); 695745de2e4Sdrh return pSubOpen->pMethods->xSectorSize(pSubOpen); 696748a52cdSdrh } 697748a52cdSdrh 698748a52cdSdrh /* Pass xDeviceCharacteristics requests through to the original VFS unchanged. 699748a52cdSdrh */ 700ef9659baSdrh static int quotaDeviceCharacteristics(sqlite3_file *pConn){ 701ef9659baSdrh sqlite3_file *pSubOpen = quotaSubOpen(pConn); 702745de2e4Sdrh return pSubOpen->pMethods->xDeviceCharacteristics(pSubOpen); 703748a52cdSdrh } 704748a52cdSdrh 705748a52cdSdrh /* Pass xShmMap requests through to the original VFS unchanged. 706748a52cdSdrh */ 707748a52cdSdrh static int quotaShmMap( 708ef9659baSdrh sqlite3_file *pConn, /* Handle open on database file */ 709748a52cdSdrh int iRegion, /* Region to retrieve */ 710748a52cdSdrh int szRegion, /* Size of regions */ 711748a52cdSdrh int bExtend, /* True to extend file if necessary */ 712748a52cdSdrh void volatile **pp /* OUT: Mapped memory */ 713748a52cdSdrh ){ 714ef9659baSdrh sqlite3_file *pSubOpen = quotaSubOpen(pConn); 715745de2e4Sdrh return pSubOpen->pMethods->xShmMap(pSubOpen, iRegion, szRegion, bExtend, pp); 716748a52cdSdrh } 717748a52cdSdrh 718748a52cdSdrh /* Pass xShmLock requests through to the original VFS unchanged. 719748a52cdSdrh */ 720748a52cdSdrh static int quotaShmLock( 721ef9659baSdrh sqlite3_file *pConn, /* Database file holding the shared memory */ 722748a52cdSdrh int ofst, /* First lock to acquire or release */ 723748a52cdSdrh int n, /* Number of locks to acquire or release */ 724748a52cdSdrh int flags /* What to do with the lock */ 725748a52cdSdrh ){ 726ef9659baSdrh sqlite3_file *pSubOpen = quotaSubOpen(pConn); 727745de2e4Sdrh return pSubOpen->pMethods->xShmLock(pSubOpen, ofst, n, flags); 728748a52cdSdrh } 729748a52cdSdrh 730748a52cdSdrh /* Pass xShmBarrier requests through to the original VFS unchanged. 731748a52cdSdrh */ 732ef9659baSdrh static void quotaShmBarrier(sqlite3_file *pConn){ 733ef9659baSdrh sqlite3_file *pSubOpen = quotaSubOpen(pConn); 734745de2e4Sdrh pSubOpen->pMethods->xShmBarrier(pSubOpen); 735748a52cdSdrh } 736748a52cdSdrh 737748a52cdSdrh /* Pass xShmUnmap requests through to the original VFS unchanged. 738748a52cdSdrh */ 739ef9659baSdrh static int quotaShmUnmap(sqlite3_file *pConn, int deleteFlag){ 740ef9659baSdrh sqlite3_file *pSubOpen = quotaSubOpen(pConn); 741745de2e4Sdrh return pSubOpen->pMethods->xShmUnmap(pSubOpen, deleteFlag); 742748a52cdSdrh } 743748a52cdSdrh 744748a52cdSdrh /************************** Public Interfaces *****************************/ 745748a52cdSdrh /* 746748a52cdSdrh ** Initialize the quota VFS shim. Use the VFS named zOrigVfsName 747748a52cdSdrh ** as the VFS that does the actual work. Use the default if 748748a52cdSdrh ** zOrigVfsName==NULL. 749748a52cdSdrh ** 750748a52cdSdrh ** The quota VFS shim is named "quota". It will become the default 751748a52cdSdrh ** VFS if makeDefault is non-zero. 752748a52cdSdrh ** 753748a52cdSdrh ** THIS ROUTINE IS NOT THREADSAFE. Call this routine exactly once 754748a52cdSdrh ** during start-up. 755748a52cdSdrh */ 756748a52cdSdrh int sqlite3_quota_initialize(const char *zOrigVfsName, int makeDefault){ 757748a52cdSdrh sqlite3_vfs *pOrigVfs; 7588cf35eb4Sdan if( gQuota.isInitialized ) return SQLITE_MISUSE; 759748a52cdSdrh pOrigVfs = sqlite3_vfs_find(zOrigVfsName); 760748a52cdSdrh if( pOrigVfs==0 ) return SQLITE_ERROR; 7618cf35eb4Sdan assert( pOrigVfs!=&gQuota.sThisVfs ); 7628cf35eb4Sdan gQuota.pMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); 7638cf35eb4Sdan if( !gQuota.pMutex ){ 7648cf35eb4Sdan return SQLITE_NOMEM; 7658cf35eb4Sdan } 7668cf35eb4Sdan gQuota.isInitialized = 1; 767748a52cdSdrh gQuota.pOrigVfs = pOrigVfs; 768748a52cdSdrh gQuota.sThisVfs = *pOrigVfs; 769748a52cdSdrh gQuota.sThisVfs.xOpen = quotaOpen; 770796af156Sdrh gQuota.sThisVfs.xDelete = quotaDelete; 771ef9659baSdrh gQuota.sThisVfs.szOsFile += sizeof(quotaConn); 772748a52cdSdrh gQuota.sThisVfs.zName = "quota"; 773748a52cdSdrh gQuota.sIoMethodsV1.iVersion = 1; 774748a52cdSdrh gQuota.sIoMethodsV1.xClose = quotaClose; 775748a52cdSdrh gQuota.sIoMethodsV1.xRead = quotaRead; 776748a52cdSdrh gQuota.sIoMethodsV1.xWrite = quotaWrite; 777748a52cdSdrh gQuota.sIoMethodsV1.xTruncate = quotaTruncate; 778748a52cdSdrh gQuota.sIoMethodsV1.xSync = quotaSync; 779748a52cdSdrh gQuota.sIoMethodsV1.xFileSize = quotaFileSize; 780748a52cdSdrh gQuota.sIoMethodsV1.xLock = quotaLock; 781748a52cdSdrh gQuota.sIoMethodsV1.xUnlock = quotaUnlock; 782748a52cdSdrh gQuota.sIoMethodsV1.xCheckReservedLock = quotaCheckReservedLock; 783748a52cdSdrh gQuota.sIoMethodsV1.xFileControl = quotaFileControl; 7848cf35eb4Sdan gQuota.sIoMethodsV1.xSectorSize = quotaSectorSize; 785748a52cdSdrh gQuota.sIoMethodsV1.xDeviceCharacteristics = quotaDeviceCharacteristics; 786748a52cdSdrh gQuota.sIoMethodsV2 = gQuota.sIoMethodsV1; 787748a52cdSdrh gQuota.sIoMethodsV2.iVersion = 2; 788748a52cdSdrh gQuota.sIoMethodsV2.xShmMap = quotaShmMap; 789748a52cdSdrh gQuota.sIoMethodsV2.xShmLock = quotaShmLock; 790748a52cdSdrh gQuota.sIoMethodsV2.xShmBarrier = quotaShmBarrier; 791748a52cdSdrh gQuota.sIoMethodsV2.xShmUnmap = quotaShmUnmap; 7928cf35eb4Sdan sqlite3_vfs_register(&gQuota.sThisVfs, makeDefault); 7938cf35eb4Sdan return SQLITE_OK; 794748a52cdSdrh } 795748a52cdSdrh 796748a52cdSdrh /* 797748a52cdSdrh ** Shutdown the quota system. 798748a52cdSdrh ** 799748a52cdSdrh ** All SQLite database connections must be closed before calling this 800748a52cdSdrh ** routine. 801748a52cdSdrh ** 802796af156Sdrh ** THIS ROUTINE IS NOT THREADSAFE. Call this routine exactly once while 803748a52cdSdrh ** shutting down in order to free all remaining quota groups. 804748a52cdSdrh */ 805748a52cdSdrh int sqlite3_quota_shutdown(void){ 80675c19174Sdrh quotaGroup *pGroup; 807748a52cdSdrh if( gQuota.isInitialized==0 ) return SQLITE_MISUSE; 80875c19174Sdrh for(pGroup=gQuota.pGroup; pGroup; pGroup=pGroup->pNext){ 809796af156Sdrh if( quotaGroupOpenFileCount(pGroup)>0 ) return SQLITE_MISUSE; 8108cf35eb4Sdan } 8118cf35eb4Sdan while( gQuota.pGroup ){ 81275c19174Sdrh pGroup = gQuota.pGroup; 81375c19174Sdrh gQuota.pGroup = pGroup->pNext; 8149fc7702cSdan pGroup->iLimit = 0; 815796af156Sdrh assert( quotaGroupOpenFileCount(pGroup)==0 ); 8169fc7702cSdan quotaGroupDeref(pGroup); 8178cf35eb4Sdan } 818748a52cdSdrh gQuota.isInitialized = 0; 819748a52cdSdrh sqlite3_mutex_free(gQuota.pMutex); 8208cf35eb4Sdan sqlite3_vfs_unregister(&gQuota.sThisVfs); 821748a52cdSdrh memset(&gQuota, 0, sizeof(gQuota)); 8228cf35eb4Sdan return SQLITE_OK; 823748a52cdSdrh } 824748a52cdSdrh 825748a52cdSdrh /* 826748a52cdSdrh ** Create or destroy a quota group. 827748a52cdSdrh ** 828748a52cdSdrh ** The quota group is defined by the zPattern. When calling this routine 829748a52cdSdrh ** with a zPattern for a quota group that already exists, this routine 830748a52cdSdrh ** merely updates the iLimit, xCallback, and pArg values for that quota 831748a52cdSdrh ** group. If zPattern is new, then a new quota group is created. 832748a52cdSdrh ** 833748a52cdSdrh ** If the iLimit for a quota group is set to zero, then the quota group 834748a52cdSdrh ** is disabled and will be deleted when the last database connection using 835748a52cdSdrh ** the quota group is closed. 836748a52cdSdrh ** 837748a52cdSdrh ** Calling this routine on a zPattern that does not exist and with a 838748a52cdSdrh ** zero iLimit is a no-op. 839748a52cdSdrh ** 840748a52cdSdrh ** A quota group must exist with a non-zero iLimit prior to opening 841748a52cdSdrh ** database connections if those connections are to participate in the 8428cf35eb4Sdan ** quota group. Creating a quota group does not affect database connections 843748a52cdSdrh ** that are already open. 844748a52cdSdrh */ 845748a52cdSdrh int sqlite3_quota_set( 846748a52cdSdrh const char *zPattern, /* The filename pattern */ 847748a52cdSdrh sqlite3_int64 iLimit, /* New quota to set for this quota group */ 848748a52cdSdrh void (*xCallback)( /* Callback invoked when going over quota */ 849748a52cdSdrh const char *zFilename, /* Name of file whose size increases */ 850748a52cdSdrh sqlite3_int64 *piLimit, /* IN/OUT: The current limit */ 851748a52cdSdrh sqlite3_int64 iSize, /* Total size of all files in the group */ 852748a52cdSdrh void *pArg /* Client data */ 8538cf35eb4Sdan ), 854ef9659baSdrh void *pArg, /* client data passed thru to callback */ 855ef9659baSdrh void (*xDestroy)(void*) /* Optional destructor for pArg */ 856748a52cdSdrh ){ 857748a52cdSdrh quotaGroup *pGroup; 858748a52cdSdrh quotaEnter(); 859748a52cdSdrh pGroup = gQuota.pGroup; 860748a52cdSdrh while( pGroup && strcmp(pGroup->zPattern, zPattern)!=0 ){ 861748a52cdSdrh pGroup = pGroup->pNext; 862748a52cdSdrh } 86361cc423aSdrh if( pGroup==0 ){ 864748a52cdSdrh int nPattern = strlen(zPattern); 86561cc423aSdrh if( iLimit<=0 ){ 86661cc423aSdrh quotaLeave(); 86761cc423aSdrh return SQLITE_OK; 86861cc423aSdrh } 869b0a32832Sdan pGroup = (quotaGroup *)sqlite3_malloc( sizeof(*pGroup) + nPattern + 1 ); 870748a52cdSdrh if( pGroup==0 ){ 871748a52cdSdrh quotaLeave(); 872748a52cdSdrh return SQLITE_NOMEM; 873748a52cdSdrh } 874748a52cdSdrh memset(pGroup, 0, sizeof(*pGroup)); 875748a52cdSdrh pGroup->zPattern = (char*)&pGroup[1]; 8768cf35eb4Sdan memcpy((char *)pGroup->zPattern, zPattern, nPattern+1); 877748a52cdSdrh if( gQuota.pGroup ) gQuota.pGroup->ppPrev = &pGroup->pNext; 8788cf35eb4Sdan pGroup->pNext = gQuota.pGroup; 8798cf35eb4Sdan pGroup->ppPrev = &gQuota.pGroup; 8808cf35eb4Sdan gQuota.pGroup = pGroup; 881748a52cdSdrh } 882748a52cdSdrh pGroup->iLimit = iLimit; 883748a52cdSdrh pGroup->xCallback = xCallback; 884ef9659baSdrh if( pGroup->xDestroy && pGroup->pArg!=pArg ){ 885ef9659baSdrh pGroup->xDestroy(pGroup->pArg); 886ef9659baSdrh } 887748a52cdSdrh pGroup->pArg = pArg; 888ef9659baSdrh pGroup->xDestroy = xDestroy; 889748a52cdSdrh quotaGroupDeref(pGroup); 890748a52cdSdrh quotaLeave(); 891748a52cdSdrh return SQLITE_OK; 892748a52cdSdrh } 893748a52cdSdrh 894b6020c4eSdrh /* 895b6020c4eSdrh ** Bring the named file under quota management. Or if it is already under 896b6020c4eSdrh ** management, update its size. 897b6020c4eSdrh */ 898b6020c4eSdrh int sqlite3_quota_file(const char *zFilename){ 899b6020c4eSdrh char *zFull; 900b6020c4eSdrh sqlite3_file *fd; 901b6020c4eSdrh int rc; 902b6020c4eSdrh int outFlags = 0; 903b6020c4eSdrh sqlite3_int64 iSize; 904b6020c4eSdrh fd = sqlite3_malloc(gQuota.sThisVfs.szOsFile + gQuota.sThisVfs.mxPathname+1); 905b6020c4eSdrh if( fd==0 ) return SQLITE_NOMEM; 906b6020c4eSdrh zFull = gQuota.sThisVfs.szOsFile + (char*)fd; 907b6020c4eSdrh rc = gQuota.pOrigVfs->xFullPathname(gQuota.pOrigVfs, zFilename, 908b6020c4eSdrh gQuota.sThisVfs.mxPathname+1, zFull); 909b6020c4eSdrh if( rc==SQLITE_OK ){ 910b6020c4eSdrh rc = quotaOpen(&gQuota.sThisVfs, zFull, fd, 911b6020c4eSdrh SQLITE_OPEN_READONLY | SQLITE_OPEN_MAIN_DB, &outFlags); 912b6020c4eSdrh } 913b6020c4eSdrh if( rc==SQLITE_OK ){ 91426541c9aSdrh fd->pMethods->xFileSize(fd, &iSize); 91526541c9aSdrh fd->pMethods->xClose(fd); 916b6020c4eSdrh }else if( rc==SQLITE_CANTOPEN ){ 917b6020c4eSdrh quotaGroup *pGroup; 918b6020c4eSdrh quotaFile *pFile; 919b6020c4eSdrh quotaEnter(); 920b6020c4eSdrh pGroup = quotaGroupFind(zFull); 921b6020c4eSdrh if( pGroup ){ 92260f21e4bSdrh pFile = quotaFindFile(pGroup, zFull, 0); 923b6020c4eSdrh if( pFile ) quotaRemoveFile(pFile); 924b6020c4eSdrh } 925b6020c4eSdrh quotaLeave(); 926b6020c4eSdrh } 927b6020c4eSdrh sqlite3_free(fd); 928b6020c4eSdrh return rc; 929b6020c4eSdrh } 930b6020c4eSdrh 93160f21e4bSdrh /* 93260f21e4bSdrh ** Open a potentially quotaed file for I/O. 93360f21e4bSdrh */ 93460f21e4bSdrh quota_FILE *sqlite3_quota_fopen(const char *zFilename, const char *zMode){ 93560f21e4bSdrh quota_FILE *p = 0; 93660f21e4bSdrh char *zFull = 0; 937*a76e8589Sdrh char *zFullTranslated; 93860f21e4bSdrh int rc; 93960f21e4bSdrh quotaGroup *pGroup; 94060f21e4bSdrh quotaFile *pFile; 94160f21e4bSdrh 942*a76e8589Sdrh zFull = sqlite3_malloc(gQuota.sThisVfs.mxPathname + 1); 943*a76e8589Sdrh if( zFull==0 ) return 0; 94460f21e4bSdrh rc = gQuota.pOrigVfs->xFullPathname(gQuota.pOrigVfs, zFilename, 94560f21e4bSdrh gQuota.sThisVfs.mxPathname+1, zFull); 94660f21e4bSdrh if( rc ) goto quota_fopen_error; 94760f21e4bSdrh p = sqlite3_malloc(sizeof(*p)); 94860f21e4bSdrh if( p==0 ) goto quota_fopen_error; 94960f21e4bSdrh memset(p, 0, sizeof(*p)); 950*a76e8589Sdrh zFullTranslated = quota_utf8_to_mbcs(zFull); 951*a76e8589Sdrh if( zFullTranslated==0 ) goto quota_fopen_error; 952*a76e8589Sdrh p->f = fopen(zFullTranslated, zMode); 953*a76e8589Sdrh quota_mbcs_free(zFullTranslated); 95460f21e4bSdrh if( p->f==0 ) goto quota_fopen_error; 95560f21e4bSdrh quotaEnter(); 95660f21e4bSdrh pGroup = quotaGroupFind(zFull); 95760f21e4bSdrh if( pGroup ){ 95860f21e4bSdrh pFile = quotaFindFile(pGroup, zFull, 1); 95960f21e4bSdrh if( pFile==0 ){ 96060f21e4bSdrh quotaLeave(); 96160f21e4bSdrh goto quota_fopen_error; 96260f21e4bSdrh } 96360f21e4bSdrh pFile->nRef++; 96460f21e4bSdrh p->pFile = pFile; 96560f21e4bSdrh } 96660f21e4bSdrh quotaLeave(); 96760f21e4bSdrh sqlite3_free(zFull); 96860f21e4bSdrh return p; 96960f21e4bSdrh 97060f21e4bSdrh quota_fopen_error: 97160f21e4bSdrh sqlite3_free(zFull); 97260f21e4bSdrh if( p && p->f ) fclose(p->f); 97360f21e4bSdrh sqlite3_free(p); 97460f21e4bSdrh return 0; 97560f21e4bSdrh } 97660f21e4bSdrh 97760f21e4bSdrh /* 97860f21e4bSdrh ** Read content from a quota_FILE 97960f21e4bSdrh */ 98060f21e4bSdrh size_t sqlite3_quota_fread( 98160f21e4bSdrh void *pBuf, /* Store the content here */ 98260f21e4bSdrh size_t size, /* Size of each element */ 98360f21e4bSdrh size_t nmemb, /* Number of elements to read */ 98460f21e4bSdrh quota_FILE *p /* Read from this quota_FILE object */ 98560f21e4bSdrh ){ 98660f21e4bSdrh return fread(pBuf, size, nmemb, p->f); 98760f21e4bSdrh } 98860f21e4bSdrh 98960f21e4bSdrh /* 99060f21e4bSdrh ** Write content into a quota_FILE. Invoke the quota callback and block 99160f21e4bSdrh ** the write if we exceed quota. 99260f21e4bSdrh */ 99360f21e4bSdrh size_t sqlite3_quota_fwrite( 99460f21e4bSdrh void *pBuf, /* Take content to write from here */ 99560f21e4bSdrh size_t size, /* Size of each element */ 99660f21e4bSdrh size_t nmemb, /* Number of elements */ 99760f21e4bSdrh quota_FILE *p /* Write to this quota_FILE objecct */ 99860f21e4bSdrh ){ 99960f21e4bSdrh sqlite3_int64 iOfst; 100060f21e4bSdrh sqlite3_int64 iEnd; 100160f21e4bSdrh sqlite3_int64 szNew; 100260f21e4bSdrh quotaFile *pFile; 100360f21e4bSdrh 100460f21e4bSdrh iOfst = ftell(p->f); 100560f21e4bSdrh iEnd = iOfst + size*nmemb; 100660f21e4bSdrh pFile = p->pFile; 100760f21e4bSdrh if( pFile->iSize<iEnd ){ 100860f21e4bSdrh quotaGroup *pGroup = pFile->pGroup; 100960f21e4bSdrh quotaEnter(); 101060f21e4bSdrh szNew = pGroup->iSize - pFile->iSize + iEnd; 101160f21e4bSdrh if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){ 101260f21e4bSdrh if( pGroup->xCallback ){ 101360f21e4bSdrh pGroup->xCallback(pFile->zFilename, &pGroup->iLimit, szNew, 101460f21e4bSdrh pGroup->pArg); 101560f21e4bSdrh } 101660f21e4bSdrh if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){ 101760f21e4bSdrh iEnd = pGroup->iLimit - pGroup->iSize + pFile->iSize; 101860f21e4bSdrh nmemb = (iEnd - iOfst)/size; 101960f21e4bSdrh iEnd = iOfst + size*nmemb; 102060f21e4bSdrh szNew = pGroup->iSize - pFile->iSize + iEnd; 102160f21e4bSdrh } 102260f21e4bSdrh } 102360f21e4bSdrh pGroup->iSize = szNew; 102460f21e4bSdrh pFile->iSize = iEnd; 102560f21e4bSdrh quotaLeave(); 102660f21e4bSdrh } 102760f21e4bSdrh return fwrite(pBuf, size, nmemb, p->f); 102860f21e4bSdrh } 102960f21e4bSdrh 103060f21e4bSdrh /* 103160f21e4bSdrh ** Close an open quota_FILE stream. 103260f21e4bSdrh */ 103360f21e4bSdrh int sqlite3_quota_fclose(quota_FILE *p){ 103460f21e4bSdrh int rc; 103560f21e4bSdrh quotaFile *pFile; 103660f21e4bSdrh rc = fclose(p->f); 103760f21e4bSdrh pFile = p->pFile; 103860f21e4bSdrh quotaEnter(); 103960f21e4bSdrh pFile->nRef--; 104060f21e4bSdrh if( pFile->nRef==0 ){ 104160f21e4bSdrh quotaGroup *pGroup = pFile->pGroup; 104260f21e4bSdrh if( pFile->deleteOnClose ) quotaRemoveFile(pFile); 104360f21e4bSdrh quotaGroupDeref(pGroup); 104460f21e4bSdrh } 104560f21e4bSdrh quotaLeave(); 104660f21e4bSdrh sqlite3_free(p); 104760f21e4bSdrh return rc; 104860f21e4bSdrh } 104960f21e4bSdrh 105060f21e4bSdrh /* 105160f21e4bSdrh ** Seek on a quota_FILE stream. 105260f21e4bSdrh */ 105360f21e4bSdrh int sqlite3_quota_fseek(quota_FILE *p, long offset, int whence){ 105460f21e4bSdrh return fseek(p->f, offset, whence); 105560f21e4bSdrh } 105660f21e4bSdrh 105760f21e4bSdrh /* 105860f21e4bSdrh ** rewind a quota_FILE stream. 105960f21e4bSdrh */ 106060f21e4bSdrh void sqlite3_quota_rewind(quota_FILE *p){ 106160f21e4bSdrh rewind(p->f); 106260f21e4bSdrh } 106360f21e4bSdrh 106460f21e4bSdrh /* 106560f21e4bSdrh ** Tell the current location of a quota_FILE stream. 106660f21e4bSdrh */ 106760f21e4bSdrh long sqlite3_quota_ftell(quota_FILE *p){ 106860f21e4bSdrh return ftell(p->f); 106960f21e4bSdrh } 107060f21e4bSdrh 107160f21e4bSdrh /* 107260f21e4bSdrh ** Remove a file. Update quotas accordingly. 107360f21e4bSdrh */ 107460f21e4bSdrh int sqlite3_quota_remove(const char *zFilename){ 107560f21e4bSdrh int rc = remove(zFilename); 107660f21e4bSdrh sqlite3_quota_file(zFilename); 107760f21e4bSdrh return rc; 107860f21e4bSdrh } 107960f21e4bSdrh 1080748a52cdSdrh 1081748a52cdSdrh /***************************** Test Code ***********************************/ 1082748a52cdSdrh #ifdef SQLITE_TEST 1083748a52cdSdrh #include <tcl.h> 10848cf35eb4Sdan 1085ef9659baSdrh /* 1086ef9659baSdrh ** Argument passed to a TCL quota-over-limit callback. 1087ef9659baSdrh */ 10888cf35eb4Sdan typedef struct TclQuotaCallback TclQuotaCallback; 10898cf35eb4Sdan struct TclQuotaCallback { 1090ef9659baSdrh Tcl_Interp *interp; /* Interpreter in which to run the script */ 1091ef9659baSdrh Tcl_Obj *pScript; /* Script to be run */ 10928cf35eb4Sdan }; 10938cf35eb4Sdan 10948cf35eb4Sdan extern const char *sqlite3TestErrorName(int); 10958cf35eb4Sdan 1096ef9659baSdrh 1097ef9659baSdrh /* 1098ef9659baSdrh ** This is the callback from a quota-over-limit. 1099ef9659baSdrh */ 11008cf35eb4Sdan static void tclQuotaCallback( 11018cf35eb4Sdan const char *zFilename, /* Name of file whose size increases */ 11028cf35eb4Sdan sqlite3_int64 *piLimit, /* IN/OUT: The current limit */ 11038cf35eb4Sdan sqlite3_int64 iSize, /* Total size of all files in the group */ 11048cf35eb4Sdan void *pArg /* Client data */ 11058cf35eb4Sdan ){ 11068cf35eb4Sdan TclQuotaCallback *p; /* Callback script object */ 11078cf35eb4Sdan Tcl_Obj *pEval; /* Script to evaluate */ 11088cf35eb4Sdan Tcl_Obj *pVarname; /* Name of variable to pass as 2nd arg */ 11098cf35eb4Sdan unsigned int rnd; /* Random part of pVarname */ 11108cf35eb4Sdan int rc; /* Tcl error code */ 11118cf35eb4Sdan 11128cf35eb4Sdan p = (TclQuotaCallback *)pArg; 11133d3e60e4Sdrh if( p==0 ) return; 11148cf35eb4Sdan 11158cf35eb4Sdan pVarname = Tcl_NewStringObj("::piLimit_", -1); 11168cf35eb4Sdan Tcl_IncrRefCount(pVarname); 11178cf35eb4Sdan sqlite3_randomness(sizeof(rnd), (void *)&rnd); 11188cf35eb4Sdan Tcl_AppendObjToObj(pVarname, Tcl_NewIntObj((int)(rnd&0x7FFFFFFF))); 11198cf35eb4Sdan Tcl_ObjSetVar2(p->interp, pVarname, 0, Tcl_NewWideIntObj(*piLimit), 0); 11208cf35eb4Sdan 11218cf35eb4Sdan pEval = Tcl_DuplicateObj(p->pScript); 11228cf35eb4Sdan Tcl_IncrRefCount(pEval); 11238cf35eb4Sdan Tcl_ListObjAppendElement(0, pEval, Tcl_NewStringObj(zFilename, -1)); 11248cf35eb4Sdan Tcl_ListObjAppendElement(0, pEval, pVarname); 11258cf35eb4Sdan Tcl_ListObjAppendElement(0, pEval, Tcl_NewWideIntObj(iSize)); 11268cf35eb4Sdan rc = Tcl_EvalObjEx(p->interp, pEval, TCL_EVAL_GLOBAL); 11278cf35eb4Sdan 11288cf35eb4Sdan if( rc==TCL_OK ){ 11298cf35eb4Sdan Tcl_Obj *pLimit = Tcl_ObjGetVar2(p->interp, pVarname, 0, 0); 11308cf35eb4Sdan rc = Tcl_GetWideIntFromObj(p->interp, pLimit, piLimit); 11318cf35eb4Sdan Tcl_UnsetVar(p->interp, Tcl_GetString(pVarname), 0); 11328cf35eb4Sdan } 11338cf35eb4Sdan 11348cf35eb4Sdan Tcl_DecrRefCount(pEval); 11358cf35eb4Sdan Tcl_DecrRefCount(pVarname); 11368cf35eb4Sdan if( rc!=TCL_OK ) Tcl_BackgroundError(p->interp); 11378cf35eb4Sdan } 11388cf35eb4Sdan 11398cf35eb4Sdan /* 1140ef9659baSdrh ** Destructor for a TCL quota-over-limit callback. 1141ef9659baSdrh */ 1142ef9659baSdrh static void tclCallbackDestructor(void *pObj){ 1143ef9659baSdrh TclQuotaCallback *p = (TclQuotaCallback*)pObj; 1144ef9659baSdrh if( p ){ 1145ef9659baSdrh Tcl_DecrRefCount(p->pScript); 11469fc7702cSdan sqlite3_free((char *)p); 1147ef9659baSdrh } 1148ef9659baSdrh } 1149ef9659baSdrh 1150ef9659baSdrh /* 11518cf35eb4Sdan ** tclcmd: sqlite3_quota_initialize NAME MAKEDEFAULT 11528cf35eb4Sdan */ 11538cf35eb4Sdan static int test_quota_initialize( 11548cf35eb4Sdan void * clientData, 11558cf35eb4Sdan Tcl_Interp *interp, 11568cf35eb4Sdan int objc, 11578cf35eb4Sdan Tcl_Obj *CONST objv[] 11588cf35eb4Sdan ){ 11598cf35eb4Sdan const char *zName; /* Name of new quota VFS */ 11608cf35eb4Sdan int makeDefault; /* True to make the new VFS the default */ 11618cf35eb4Sdan int rc; /* Value returned by quota_initialize() */ 11628cf35eb4Sdan 11638cf35eb4Sdan /* Process arguments */ 11648cf35eb4Sdan if( objc!=3 ){ 11658cf35eb4Sdan Tcl_WrongNumArgs(interp, 1, objv, "NAME MAKEDEFAULT"); 11668cf35eb4Sdan return TCL_ERROR; 11678cf35eb4Sdan } 11688cf35eb4Sdan zName = Tcl_GetString(objv[1]); 11698cf35eb4Sdan if( Tcl_GetBooleanFromObj(interp, objv[2], &makeDefault) ) return TCL_ERROR; 11708cf35eb4Sdan if( zName[0]=='\0' ) zName = 0; 11718cf35eb4Sdan 11728cf35eb4Sdan /* Call sqlite3_quota_initialize() */ 11738cf35eb4Sdan rc = sqlite3_quota_initialize(zName, makeDefault); 11748cf35eb4Sdan Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC); 11758cf35eb4Sdan 11768cf35eb4Sdan return TCL_OK; 11778cf35eb4Sdan } 11788cf35eb4Sdan 11798cf35eb4Sdan /* 11808cf35eb4Sdan ** tclcmd: sqlite3_quota_shutdown 11818cf35eb4Sdan */ 11828cf35eb4Sdan static int test_quota_shutdown( 11838cf35eb4Sdan void * clientData, 11848cf35eb4Sdan Tcl_Interp *interp, 11858cf35eb4Sdan int objc, 11868cf35eb4Sdan Tcl_Obj *CONST objv[] 11878cf35eb4Sdan ){ 11888cf35eb4Sdan int rc; /* Value returned by quota_shutdown() */ 11898cf35eb4Sdan 11908cf35eb4Sdan if( objc!=1 ){ 11918cf35eb4Sdan Tcl_WrongNumArgs(interp, 1, objv, ""); 11928cf35eb4Sdan return TCL_ERROR; 11938cf35eb4Sdan } 11948cf35eb4Sdan 11958cf35eb4Sdan /* Call sqlite3_quota_shutdown() */ 11968cf35eb4Sdan rc = sqlite3_quota_shutdown(); 11978cf35eb4Sdan Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC); 11988cf35eb4Sdan 11998cf35eb4Sdan return TCL_OK; 12008cf35eb4Sdan } 12018cf35eb4Sdan 12028cf35eb4Sdan /* 12038cf35eb4Sdan ** tclcmd: sqlite3_quota_set PATTERN LIMIT SCRIPT 12048cf35eb4Sdan */ 12058cf35eb4Sdan static int test_quota_set( 12068cf35eb4Sdan void * clientData, 12078cf35eb4Sdan Tcl_Interp *interp, 12088cf35eb4Sdan int objc, 12098cf35eb4Sdan Tcl_Obj *CONST objv[] 12108cf35eb4Sdan ){ 12118cf35eb4Sdan const char *zPattern; /* File pattern to configure */ 12128cf35eb4Sdan sqlite3_int64 iLimit; /* Initial quota in bytes */ 12138cf35eb4Sdan Tcl_Obj *pScript; /* Tcl script to invoke to increase quota */ 12148cf35eb4Sdan int rc; /* Value returned by quota_set() */ 12158cf35eb4Sdan TclQuotaCallback *p; /* Callback object */ 1216ef9659baSdrh int nScript; /* Length of callback script */ 12179fc7702cSdan void (*xDestroy)(void*); /* Optional destructor for pArg */ 121806bfd36aSdan void (*xCallback)(const char *, sqlite3_int64 *, sqlite3_int64, void *); 12198cf35eb4Sdan 12208cf35eb4Sdan /* Process arguments */ 12218cf35eb4Sdan if( objc!=4 ){ 12228cf35eb4Sdan Tcl_WrongNumArgs(interp, 1, objv, "PATTERN LIMIT SCRIPT"); 12238cf35eb4Sdan return TCL_ERROR; 12248cf35eb4Sdan } 12258cf35eb4Sdan zPattern = Tcl_GetString(objv[1]); 12268cf35eb4Sdan if( Tcl_GetWideIntFromObj(interp, objv[2], &iLimit) ) return TCL_ERROR; 12278cf35eb4Sdan pScript = objv[3]; 1228ef9659baSdrh Tcl_GetStringFromObj(pScript, &nScript); 12298cf35eb4Sdan 1230ef9659baSdrh if( nScript>0 ){ 12318cf35eb4Sdan /* Allocate a TclQuotaCallback object */ 12329fc7702cSdan p = (TclQuotaCallback *)sqlite3_malloc(sizeof(TclQuotaCallback)); 12339fc7702cSdan if( !p ){ 12349fc7702cSdan Tcl_SetResult(interp, (char *)"SQLITE_NOMEM", TCL_STATIC); 12359fc7702cSdan return TCL_OK; 12369fc7702cSdan } 12378cf35eb4Sdan memset(p, 0, sizeof(TclQuotaCallback)); 12388cf35eb4Sdan p->interp = interp; 1239ef9659baSdrh Tcl_IncrRefCount(pScript); 12408cf35eb4Sdan p->pScript = pScript; 12419fc7702cSdan xDestroy = tclCallbackDestructor; 124206bfd36aSdan xCallback = tclQuotaCallback; 1243ef9659baSdrh }else{ 1244ef9659baSdrh p = 0; 12459fc7702cSdan xDestroy = 0; 124606bfd36aSdan xCallback = 0; 12478cf35eb4Sdan } 12489fc7702cSdan 1249ef9659baSdrh /* Invoke sqlite3_quota_set() */ 125006bfd36aSdan rc = sqlite3_quota_set(zPattern, iLimit, xCallback, (void*)p, xDestroy); 12518cf35eb4Sdan 12528cf35eb4Sdan Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC); 12538cf35eb4Sdan return TCL_OK; 12548cf35eb4Sdan } 12558cf35eb4Sdan 12568cf35eb4Sdan /* 1257b6020c4eSdrh ** tclcmd: sqlite3_quota_file FILENAME 1258b6020c4eSdrh */ 1259b6020c4eSdrh static int test_quota_file( 1260b6020c4eSdrh void * clientData, 1261b6020c4eSdrh Tcl_Interp *interp, 1262b6020c4eSdrh int objc, 1263b6020c4eSdrh Tcl_Obj *CONST objv[] 1264b6020c4eSdrh ){ 1265b6020c4eSdrh const char *zFilename; /* File pattern to configure */ 1266b6020c4eSdrh int rc; /* Value returned by quota_file() */ 1267b6020c4eSdrh 1268b6020c4eSdrh /* Process arguments */ 1269b6020c4eSdrh if( objc!=2 ){ 1270b6020c4eSdrh Tcl_WrongNumArgs(interp, 1, objv, "FILENAME"); 1271b6020c4eSdrh return TCL_ERROR; 1272b6020c4eSdrh } 1273b6020c4eSdrh zFilename = Tcl_GetString(objv[1]); 1274b6020c4eSdrh 1275b6020c4eSdrh /* Invoke sqlite3_quota_file() */ 1276b6020c4eSdrh rc = sqlite3_quota_file(zFilename); 1277b6020c4eSdrh 1278b6020c4eSdrh Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC); 1279b6020c4eSdrh return TCL_OK; 1280b6020c4eSdrh } 1281b6020c4eSdrh 1282b6020c4eSdrh /* 1283ef9659baSdrh ** tclcmd: sqlite3_quota_dump 1284ef9659baSdrh */ 1285ef9659baSdrh static int test_quota_dump( 1286ef9659baSdrh void * clientData, 1287ef9659baSdrh Tcl_Interp *interp, 1288ef9659baSdrh int objc, 1289ef9659baSdrh Tcl_Obj *CONST objv[] 1290ef9659baSdrh ){ 1291ef9659baSdrh Tcl_Obj *pResult; 1292ef9659baSdrh Tcl_Obj *pGroupTerm; 1293ef9659baSdrh Tcl_Obj *pFileTerm; 1294ef9659baSdrh quotaGroup *pGroup; 1295ef9659baSdrh quotaFile *pFile; 1296ef9659baSdrh 1297ef9659baSdrh pResult = Tcl_NewObj(); 1298ef9659baSdrh quotaEnter(); 1299ef9659baSdrh for(pGroup=gQuota.pGroup; pGroup; pGroup=pGroup->pNext){ 1300ef9659baSdrh pGroupTerm = Tcl_NewObj(); 1301ef9659baSdrh Tcl_ListObjAppendElement(interp, pGroupTerm, 1302ef9659baSdrh Tcl_NewStringObj(pGroup->zPattern, -1)); 1303ef9659baSdrh Tcl_ListObjAppendElement(interp, pGroupTerm, 1304ef9659baSdrh Tcl_NewWideIntObj(pGroup->iLimit)); 1305ef9659baSdrh Tcl_ListObjAppendElement(interp, pGroupTerm, 1306ef9659baSdrh Tcl_NewWideIntObj(pGroup->iSize)); 130775c19174Sdrh for(pFile=pGroup->pFiles; pFile; pFile=pFile->pNext){ 1308ef9659baSdrh pFileTerm = Tcl_NewObj(); 1309ef9659baSdrh Tcl_ListObjAppendElement(interp, pFileTerm, 1310ef9659baSdrh Tcl_NewStringObj(pFile->zFilename, -1)); 1311ef9659baSdrh Tcl_ListObjAppendElement(interp, pFileTerm, 1312ef9659baSdrh Tcl_NewWideIntObj(pFile->iSize)); 1313ef9659baSdrh Tcl_ListObjAppendElement(interp, pFileTerm, 1314ef9659baSdrh Tcl_NewWideIntObj(pFile->nRef)); 1315796af156Sdrh Tcl_ListObjAppendElement(interp, pFileTerm, 1316796af156Sdrh Tcl_NewWideIntObj(pFile->deleteOnClose)); 1317ef9659baSdrh Tcl_ListObjAppendElement(interp, pGroupTerm, pFileTerm); 1318ef9659baSdrh } 1319ef9659baSdrh Tcl_ListObjAppendElement(interp, pResult, pGroupTerm); 1320ef9659baSdrh } 1321ef9659baSdrh quotaLeave(); 1322ef9659baSdrh Tcl_SetObjResult(interp, pResult); 1323ef9659baSdrh return TCL_OK; 1324ef9659baSdrh } 1325ef9659baSdrh 1326748a52cdSdrh /* 1327*a76e8589Sdrh ** tclcmd: sqlite3_quota_fopen FILENAME MODE 1328*a76e8589Sdrh */ 1329*a76e8589Sdrh static int test_quota_fopen( 1330*a76e8589Sdrh void * clientData, 1331*a76e8589Sdrh Tcl_Interp *interp, 1332*a76e8589Sdrh int objc, 1333*a76e8589Sdrh Tcl_Obj *CONST objv[] 1334*a76e8589Sdrh ){ 1335*a76e8589Sdrh const char *zFilename; /* File pattern to configure */ 1336*a76e8589Sdrh const char *zMode; /* Mode string */ 1337*a76e8589Sdrh quota_FILE *p; /* Open string object */ 1338*a76e8589Sdrh char zReturn[50]; /* Name of pointer to return */ 1339*a76e8589Sdrh 1340*a76e8589Sdrh /* Process arguments */ 1341*a76e8589Sdrh if( objc!=3 ){ 1342*a76e8589Sdrh Tcl_WrongNumArgs(interp, 1, objv, "FILENAME MODE"); 1343*a76e8589Sdrh return TCL_ERROR; 1344*a76e8589Sdrh } 1345*a76e8589Sdrh zFilename = Tcl_GetString(objv[1]); 1346*a76e8589Sdrh zMode = Tcl_GetString(objv[2]); 1347*a76e8589Sdrh p = sqlite3_quota_fopen(zFilename, zMode); 1348*a76e8589Sdrh sqlite3_snprintf(sizeof(zReturn), zReturn, "%p", p); 1349*a76e8589Sdrh Tcl_SetResult(interp, zReturn, TCL_VOLATILE); 1350*a76e8589Sdrh return TCL_OK; 1351*a76e8589Sdrh } 1352*a76e8589Sdrh 1353*a76e8589Sdrh /* Defined in test1.c */ 1354*a76e8589Sdrh extern void *sqlite3TestTextToPtr(const char*); 1355*a76e8589Sdrh 1356*a76e8589Sdrh /* 1357*a76e8589Sdrh ** tclcmd: sqlite3_quota_fread HANDLE SIZE NELEM 1358*a76e8589Sdrh */ 1359*a76e8589Sdrh static int test_quota_fread( 1360*a76e8589Sdrh void * clientData, 1361*a76e8589Sdrh Tcl_Interp *interp, 1362*a76e8589Sdrh int objc, 1363*a76e8589Sdrh Tcl_Obj *CONST objv[] 1364*a76e8589Sdrh ){ 1365*a76e8589Sdrh quota_FILE *p; 1366*a76e8589Sdrh char *zBuf; 1367*a76e8589Sdrh int sz; 1368*a76e8589Sdrh int nElem; 1369*a76e8589Sdrh int got; 1370*a76e8589Sdrh 1371*a76e8589Sdrh if( objc!=4 ){ 1372*a76e8589Sdrh Tcl_WrongNumArgs(interp, 1, objv, "HANDLE SIZE NELEM"); 1373*a76e8589Sdrh return TCL_ERROR; 1374*a76e8589Sdrh } 1375*a76e8589Sdrh p = sqlite3TestTextToPtr(Tcl_GetString(objv[1])); 1376*a76e8589Sdrh if( Tcl_GetIntFromObj(interp, objv[2], &sz) ) return TCL_ERROR; 1377*a76e8589Sdrh if( Tcl_GetIntFromObj(interp, objv[3], &nElem) ) return TCL_ERROR; 1378*a76e8589Sdrh zBuf = sqlite3_malloc( sz*nElem + 1 ); 1379*a76e8589Sdrh if( zBuf==0 ){ 1380*a76e8589Sdrh Tcl_SetResult(interp, "out of memory", TCL_STATIC); 1381*a76e8589Sdrh return TCL_ERROR; 1382*a76e8589Sdrh } 1383*a76e8589Sdrh got = sqlite3_quota_fread(zBuf, sz, nElem, p); 1384*a76e8589Sdrh if( got<0 ) got = 0; 1385*a76e8589Sdrh zBuf[got*sz] = 0; 1386*a76e8589Sdrh Tcl_SetResult(interp, zBuf, TCL_VOLATILE); 1387*a76e8589Sdrh sqlite3_free(zBuf); 1388*a76e8589Sdrh return TCL_OK; 1389*a76e8589Sdrh } 1390*a76e8589Sdrh 1391*a76e8589Sdrh /* 1392*a76e8589Sdrh ** tclcmd: sqlite3_quota_fwrite HANDLE SIZE NELEM CONTENT 1393*a76e8589Sdrh */ 1394*a76e8589Sdrh static int test_quota_fwrite( 1395*a76e8589Sdrh void * clientData, 1396*a76e8589Sdrh Tcl_Interp *interp, 1397*a76e8589Sdrh int objc, 1398*a76e8589Sdrh Tcl_Obj *CONST objv[] 1399*a76e8589Sdrh ){ 1400*a76e8589Sdrh quota_FILE *p; 1401*a76e8589Sdrh char *zBuf; 1402*a76e8589Sdrh int sz; 1403*a76e8589Sdrh int nElem; 1404*a76e8589Sdrh int got; 1405*a76e8589Sdrh 1406*a76e8589Sdrh if( objc!=5 ){ 1407*a76e8589Sdrh Tcl_WrongNumArgs(interp, 1, objv, "HANDLE SIZE NELEM CONTENT"); 1408*a76e8589Sdrh return TCL_ERROR; 1409*a76e8589Sdrh } 1410*a76e8589Sdrh p = sqlite3TestTextToPtr(Tcl_GetString(objv[1])); 1411*a76e8589Sdrh if( Tcl_GetIntFromObj(interp, objv[2], &sz) ) return TCL_ERROR; 1412*a76e8589Sdrh if( Tcl_GetIntFromObj(interp, objv[3], &nElem) ) return TCL_ERROR; 1413*a76e8589Sdrh zBuf = Tcl_GetString(objv[4]); 1414*a76e8589Sdrh got = sqlite3_quota_fwrite(zBuf, sz, nElem, p); 1415*a76e8589Sdrh Tcl_SetObjResult(interp, Tcl_NewIntObj(got)); 1416*a76e8589Sdrh return TCL_OK; 1417*a76e8589Sdrh } 1418*a76e8589Sdrh 1419*a76e8589Sdrh /* 1420*a76e8589Sdrh ** tclcmd: sqlite3_quota_fclose HANDLE 1421*a76e8589Sdrh */ 1422*a76e8589Sdrh static int test_quota_fclose( 1423*a76e8589Sdrh void * clientData, 1424*a76e8589Sdrh Tcl_Interp *interp, 1425*a76e8589Sdrh int objc, 1426*a76e8589Sdrh Tcl_Obj *CONST objv[] 1427*a76e8589Sdrh ){ 1428*a76e8589Sdrh quota_FILE *p; 1429*a76e8589Sdrh int rc; 1430*a76e8589Sdrh 1431*a76e8589Sdrh if( objc!=2 ){ 1432*a76e8589Sdrh Tcl_WrongNumArgs(interp, 1, objv, "HANDLE"); 1433*a76e8589Sdrh return TCL_ERROR; 1434*a76e8589Sdrh } 1435*a76e8589Sdrh p = sqlite3TestTextToPtr(Tcl_GetString(objv[1])); 1436*a76e8589Sdrh rc = sqlite3_quota_fclose(p); 1437*a76e8589Sdrh Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); 1438*a76e8589Sdrh return TCL_OK; 1439*a76e8589Sdrh } 1440*a76e8589Sdrh 1441*a76e8589Sdrh /* 1442*a76e8589Sdrh ** tclcmd: sqlite3_quota_fseek HANDLE OFFSET WHENCE 1443*a76e8589Sdrh */ 1444*a76e8589Sdrh static int test_quota_fseek( 1445*a76e8589Sdrh void * clientData, 1446*a76e8589Sdrh Tcl_Interp *interp, 1447*a76e8589Sdrh int objc, 1448*a76e8589Sdrh Tcl_Obj *CONST objv[] 1449*a76e8589Sdrh ){ 1450*a76e8589Sdrh quota_FILE *p; 1451*a76e8589Sdrh int ofst; 1452*a76e8589Sdrh const char *zWhence; 1453*a76e8589Sdrh int whence; 1454*a76e8589Sdrh int rc; 1455*a76e8589Sdrh 1456*a76e8589Sdrh if( objc!=4 ){ 1457*a76e8589Sdrh Tcl_WrongNumArgs(interp, 1, objv, "HANDLE OFFSET WHENCE"); 1458*a76e8589Sdrh return TCL_ERROR; 1459*a76e8589Sdrh } 1460*a76e8589Sdrh p = sqlite3TestTextToPtr(Tcl_GetString(objv[1])); 1461*a76e8589Sdrh if( Tcl_GetIntFromObj(interp, objv[2], &ofst) ) return TCL_ERROR; 1462*a76e8589Sdrh zWhence = Tcl_GetString(objv[3]); 1463*a76e8589Sdrh if( strcmp(zWhence, "SEEK_SET")==0 ){ 1464*a76e8589Sdrh whence = SEEK_SET; 1465*a76e8589Sdrh }else if( strcmp(zWhence, "SEEK_CUR")==0 ){ 1466*a76e8589Sdrh whence = SEEK_CUR; 1467*a76e8589Sdrh }else if( strcmp(zWhence, "SEEK_END")==0 ){ 1468*a76e8589Sdrh whence = SEEK_END; 1469*a76e8589Sdrh }else{ 1470*a76e8589Sdrh Tcl_AppendResult(interp, 1471*a76e8589Sdrh "WHENCE should be SEEK_SET, SEEK_CUR, or SEEK_END", (char*)0); 1472*a76e8589Sdrh return TCL_ERROR; 1473*a76e8589Sdrh } 1474*a76e8589Sdrh rc = sqlite3_quota_fseek(p, ofst, whence); 1475*a76e8589Sdrh Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); 1476*a76e8589Sdrh return TCL_OK; 1477*a76e8589Sdrh } 1478*a76e8589Sdrh 1479*a76e8589Sdrh /* 1480*a76e8589Sdrh ** tclcmd: sqlite3_quota_rewind HANDLE 1481*a76e8589Sdrh */ 1482*a76e8589Sdrh static int test_quota_rewind( 1483*a76e8589Sdrh void * clientData, 1484*a76e8589Sdrh Tcl_Interp *interp, 1485*a76e8589Sdrh int objc, 1486*a76e8589Sdrh Tcl_Obj *CONST objv[] 1487*a76e8589Sdrh ){ 1488*a76e8589Sdrh quota_FILE *p; 1489*a76e8589Sdrh if( objc!=2 ){ 1490*a76e8589Sdrh Tcl_WrongNumArgs(interp, 1, objv, "HANDLE"); 1491*a76e8589Sdrh return TCL_ERROR; 1492*a76e8589Sdrh } 1493*a76e8589Sdrh p = sqlite3TestTextToPtr(Tcl_GetString(objv[1])); 1494*a76e8589Sdrh sqlite3_quota_rewind(p); 1495*a76e8589Sdrh return TCL_OK; 1496*a76e8589Sdrh } 1497*a76e8589Sdrh 1498*a76e8589Sdrh /* 1499*a76e8589Sdrh ** tclcmd: sqlite3_quota_ftell HANDLE 1500*a76e8589Sdrh */ 1501*a76e8589Sdrh static int test_quota_ftell( 1502*a76e8589Sdrh void * clientData, 1503*a76e8589Sdrh Tcl_Interp *interp, 1504*a76e8589Sdrh int objc, 1505*a76e8589Sdrh Tcl_Obj *CONST objv[] 1506*a76e8589Sdrh ){ 1507*a76e8589Sdrh quota_FILE *p; 1508*a76e8589Sdrh sqlite3_int64 x; 1509*a76e8589Sdrh if( objc!=2 ){ 1510*a76e8589Sdrh Tcl_WrongNumArgs(interp, 1, objv, "HANDLE"); 1511*a76e8589Sdrh return TCL_ERROR; 1512*a76e8589Sdrh } 1513*a76e8589Sdrh p = sqlite3TestTextToPtr(Tcl_GetString(objv[1])); 1514*a76e8589Sdrh x = sqlite3_quota_ftell(p); 1515*a76e8589Sdrh Tcl_SetObjResult(interp, Tcl_NewWideIntObj(x)); 1516*a76e8589Sdrh return TCL_OK; 1517*a76e8589Sdrh } 1518*a76e8589Sdrh 1519*a76e8589Sdrh /* 1520*a76e8589Sdrh ** tclcmd: sqlite3_quota_remove FILENAME 1521*a76e8589Sdrh */ 1522*a76e8589Sdrh static int test_quota_remove( 1523*a76e8589Sdrh void * clientData, 1524*a76e8589Sdrh Tcl_Interp *interp, 1525*a76e8589Sdrh int objc, 1526*a76e8589Sdrh Tcl_Obj *CONST objv[] 1527*a76e8589Sdrh ){ 1528*a76e8589Sdrh const char *zFilename; /* File pattern to configure */ 1529*a76e8589Sdrh int rc; 1530*a76e8589Sdrh if( objc!=2 ){ 1531*a76e8589Sdrh Tcl_WrongNumArgs(interp, 1, objv, "FILENAME"); 1532*a76e8589Sdrh return TCL_ERROR; 1533*a76e8589Sdrh } 1534*a76e8589Sdrh zFilename = Tcl_GetString(objv[1]); 1535*a76e8589Sdrh rc = sqlite3_quota_remove(zFilename); 1536*a76e8589Sdrh Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); 1537*a76e8589Sdrh return TCL_OK; 1538*a76e8589Sdrh } 1539*a76e8589Sdrh 1540*a76e8589Sdrh /* 1541748a52cdSdrh ** This routine registers the custom TCL commands defined in this 1542748a52cdSdrh ** module. This should be the only procedure visible from outside 1543748a52cdSdrh ** of this module. 1544748a52cdSdrh */ 1545748a52cdSdrh int Sqlitequota_Init(Tcl_Interp *interp){ 15468cf35eb4Sdan static struct { 15478cf35eb4Sdan char *zName; 15488cf35eb4Sdan Tcl_ObjCmdProc *xProc; 15498cf35eb4Sdan } aCmd[] = { 15508cf35eb4Sdan { "sqlite3_quota_initialize", test_quota_initialize }, 15518cf35eb4Sdan { "sqlite3_quota_shutdown", test_quota_shutdown }, 15528cf35eb4Sdan { "sqlite3_quota_set", test_quota_set }, 1553b6020c4eSdrh { "sqlite3_quota_file", test_quota_file }, 1554ef9659baSdrh { "sqlite3_quota_dump", test_quota_dump }, 1555*a76e8589Sdrh { "sqlite3_quota_fopen", test_quota_fopen }, 1556*a76e8589Sdrh { "sqlite3_quota_fread", test_quota_fread }, 1557*a76e8589Sdrh { "sqlite3_quota_fwrite", test_quota_fwrite }, 1558*a76e8589Sdrh { "sqlite3_quota_fclose", test_quota_fclose }, 1559*a76e8589Sdrh { "sqlite3_quota_fseek", test_quota_fseek }, 1560*a76e8589Sdrh { "sqlite3_quota_rewind", test_quota_rewind }, 1561*a76e8589Sdrh { "sqlite3_quota_ftell", test_quota_ftell }, 1562*a76e8589Sdrh { "sqlite3_quota_remove", test_quota_remove }, 15638cf35eb4Sdan }; 15648cf35eb4Sdan int i; 15658cf35eb4Sdan 15668cf35eb4Sdan for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){ 15678cf35eb4Sdan Tcl_CreateObjCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0); 15688cf35eb4Sdan } 15698cf35eb4Sdan 1570748a52cdSdrh return TCL_OK; 1571748a52cdSdrh } 1572748a52cdSdrh #endif 1573