1 /* 2 ** 2010 September 31 3 ** 4 ** The author disclaims copyright to this source code. In place of 5 ** a legal notice, here is a blessing: 6 ** 7 ** May you do good and not evil. 8 ** May you find forgiveness for yourself and forgive others. 9 ** May you share freely, never taking more than you give. 10 ** 11 ************************************************************************* 12 ** 13 ** This file contains a VFS "shim" - a layer that sits in between the 14 ** pager and the real VFS. 15 ** 16 ** This particular shim enforces a quota system on files. One or more 17 ** database files are in a "quota group" that is defined by a GLOB 18 ** pattern. A quota is set for the combined size of all files in the 19 ** the group. A quota of zero means "no limit". If the total size 20 ** of all files in the quota group is greater than the limit, then 21 ** write requests that attempt to enlarge a file fail with SQLITE_FULL. 22 ** 23 ** However, before returning SQLITE_FULL, the write requests invoke 24 ** a callback function that is configurable for each quota group. 25 ** This callback has the opportunity to enlarge the quota. If the 26 ** callback does enlarge the quota such that the total size of all 27 ** files within the group is less than the new quota, then the write 28 ** continues as if nothing had happened. 29 */ 30 #include "sqlite3.h" 31 #include <string.h> 32 #include <assert.h> 33 34 /************************ Object Definitions ******************************/ 35 36 /* Forward declaration of all object types */ 37 typedef struct quotaGroup quotaGroup; 38 typedef struct quotaConn quotaConn; 39 typedef struct quotaFile quotaFile; 40 41 /* 42 ** A "quota group" is a collection of files whose collective size we want 43 ** to limit. Each quota group is defined by a GLOB pattern. 44 ** 45 ** There is an instance of the following object for each defined quota 46 ** group. This object records the GLOB pattern that defines which files 47 ** belong to the quota group. The object also remembers the size limit 48 ** for the group (the quota) and the callback to be invoked when the 49 ** sum of the sizes of the files within the group goes over the limit. 50 ** 51 ** A quota group must be established (using sqlite3_quota_set(...)) 52 ** prior to opening any of the database connections that access files 53 ** within the quota group. 54 */ 55 struct quotaGroup { 56 const char *zPattern; /* Filename pattern to be quotaed */ 57 sqlite3_int64 iLimit; /* Upper bound on total file size */ 58 sqlite3_int64 iSize; /* Current size of all files */ 59 void (*xCallback)( /* Callback invoked when going over quota */ 60 const char *zFilename, /* Name of file whose size increases */ 61 sqlite3_int64 *piLimit, /* IN/OUT: The current limit */ 62 sqlite3_int64 iSize, /* Total size of all files in the group */ 63 void *pArg /* Client data */ 64 ); 65 void *pArg; /* Third argument to the xCallback() */ 66 void (*xDestroy)(void*); /* Optional destructor for pArg */ 67 quotaGroup *pNext, **ppPrev; /* Doubly linked list of all quota objects */ 68 quotaFile *pFiles; /* Files within this group */ 69 }; 70 71 /* 72 ** An instance of this structure represents a single file that is part 73 ** of a quota group. A single file can be opened multiple times. In 74 ** order keep multiple openings of the same file from causing the size 75 ** of the file to count against the quota multiple times, each file 76 ** has a unique instance of this object and multiple open connections 77 ** to the same file each point to a single instance of this object. 78 */ 79 struct quotaFile { 80 char *zFilename; /* Name of this file */ 81 quotaGroup *pGroup; /* Quota group to which this file belongs */ 82 sqlite3_int64 iSize; /* Current size of this file */ 83 int nRef; /* Number of times this file is open */ 84 quotaFile *pNext, **ppPrev; /* Linked list of files in the same group */ 85 }; 86 87 /* 88 ** An instance of the following object represents each open connection 89 ** to a file that participates in quota tracking. This object is a 90 ** subclass of sqlite3_file. The sqlite3_file object for the underlying 91 ** VFS is appended to this structure. 92 */ 93 struct quotaConn { 94 sqlite3_file base; /* Base class - must be first */ 95 quotaFile *pFile; /* The underlying file */ 96 /* The underlying VFS sqlite3_file is appended to this object */ 97 }; 98 99 /************************* Global Variables **********************************/ 100 /* 101 ** All global variables used by this file are containing within the following 102 ** gQuota structure. 103 */ 104 static struct { 105 /* The pOrigVfs is the real, original underlying VFS implementation. 106 ** Most operations pass-through to the real VFS. This value is read-only 107 ** during operation. It is only modified at start-time and thus does not 108 ** require a mutex. 109 */ 110 sqlite3_vfs *pOrigVfs; 111 112 /* The sThisVfs is the VFS structure used by this shim. It is initialized 113 ** at start-time and thus does not require a mutex 114 */ 115 sqlite3_vfs sThisVfs; 116 117 /* The sIoMethods defines the methods used by sqlite3_file objects 118 ** associated with this shim. It is initialized at start-time and does 119 ** not require a mutex. 120 ** 121 ** When the underlying VFS is called to open a file, it might return 122 ** either a version 1 or a version 2 sqlite3_file object. This shim 123 ** has to create a wrapper sqlite3_file of the same version. Hence 124 ** there are two I/O method structures, one for version 1 and the other 125 ** for version 2. 126 */ 127 sqlite3_io_methods sIoMethodsV1; 128 sqlite3_io_methods sIoMethodsV2; 129 130 /* True when this shim as been initialized. 131 */ 132 int isInitialized; 133 134 /* For run-time access any of the other global data structures in this 135 ** shim, the following mutex must be held. 136 */ 137 sqlite3_mutex *pMutex; 138 139 /* List of quotaGroup objects. 140 */ 141 quotaGroup *pGroup; 142 143 } gQuota; 144 145 /************************* Utility Routines *********************************/ 146 /* 147 ** Acquire and release the mutex used to serialize access to the 148 ** list of quotaGroups. 149 */ 150 static void quotaEnter(void){ sqlite3_mutex_enter(gQuota.pMutex); } 151 static void quotaLeave(void){ sqlite3_mutex_leave(gQuota.pMutex); } 152 153 154 /* If the reference count and threshold for a quotaGroup are both 155 ** zero, then destroy the quotaGroup. 156 */ 157 static void quotaGroupDeref(quotaGroup *pGroup){ 158 if( pGroup->pFiles==0 && pGroup->iLimit==0 ){ 159 *pGroup->ppPrev = pGroup->pNext; 160 if( pGroup->pNext ) pGroup->pNext->ppPrev = pGroup->ppPrev; 161 if( pGroup->xDestroy ) pGroup->xDestroy(pGroup->pArg); 162 sqlite3_free(pGroup); 163 } 164 } 165 166 /* 167 ** Return TRUE if string z matches glob pattern zGlob. 168 ** 169 ** Globbing rules: 170 ** 171 ** '*' Matches any sequence of zero or more characters. 172 ** 173 ** '?' Matches exactly one character. 174 ** 175 ** [...] Matches one character from the enclosed list of 176 ** characters. 177 ** 178 ** [^...] Matches one character not in the enclosed list. 179 ** 180 */ 181 static int quotaStrglob(const char *zGlob, const char *z){ 182 int c, c2; 183 int invert; 184 int seen; 185 186 while( (c = (*(zGlob++)))!=0 ){ 187 if( c=='*' ){ 188 while( (c=(*(zGlob++))) == '*' || c=='?' ){ 189 if( c=='?' && (*(z++))==0 ) return 0; 190 } 191 if( c==0 ){ 192 return 1; 193 }else if( c=='[' ){ 194 while( *z && quotaStrglob(zGlob-1,z)==0 ){ 195 z++; 196 } 197 return (*z)!=0; 198 } 199 while( (c2 = (*(z++)))!=0 ){ 200 while( c2!=c ){ 201 c2 = *(z++); 202 if( c2==0 ) return 0; 203 } 204 if( quotaStrglob(zGlob,z) ) return 1; 205 } 206 return 0; 207 }else if( c=='?' ){ 208 if( (*(z++))==0 ) return 0; 209 }else if( c=='[' ){ 210 int prior_c = 0; 211 seen = 0; 212 invert = 0; 213 c = *(z++); 214 if( c==0 ) return 0; 215 c2 = *(zGlob++); 216 if( c2=='^' ){ 217 invert = 1; 218 c2 = *(zGlob++); 219 } 220 if( c2==']' ){ 221 if( c==']' ) seen = 1; 222 c2 = *(zGlob++); 223 } 224 while( c2 && c2!=']' ){ 225 if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){ 226 c2 = *(zGlob++); 227 if( c>=prior_c && c<=c2 ) seen = 1; 228 prior_c = 0; 229 }else{ 230 if( c==c2 ){ 231 seen = 1; 232 } 233 prior_c = c2; 234 } 235 c2 = *(zGlob++); 236 } 237 if( c2==0 || (seen ^ invert)==0 ) return 0; 238 }else{ 239 if( c!=(*(z++)) ) return 0; 240 } 241 } 242 return *z==0; 243 } 244 245 246 /* Find a quotaGroup given the filename. 247 ** 248 ** Return a pointer to the quotaGroup object. Return NULL if not found. 249 */ 250 static quotaGroup *quotaGroupFind(const char *zFilename){ 251 quotaGroup *p; 252 for(p=gQuota.pGroup; p && quotaStrglob(p->zPattern, zFilename)==0; 253 p=p->pNext){} 254 return p; 255 } 256 257 /* Translate an sqlite3_file* that is really a quotaConn* into 258 ** the sqlite3_file* for the underlying original VFS. 259 */ 260 static sqlite3_file *quotaSubOpen(sqlite3_file *pConn){ 261 quotaConn *p = (quotaConn*)pConn; 262 return (sqlite3_file*)&p[1]; 263 } 264 265 /************************* VFS Method Wrappers *****************************/ 266 /* 267 ** This is the xOpen method used for the "quota" VFS. 268 ** 269 ** Most of the work is done by the underlying original VFS. This method 270 ** simply links the new file into the appropriate quota group if it is a 271 ** file that needs to be tracked. 272 */ 273 static int quotaOpen( 274 sqlite3_vfs *pVfs, /* The quota VFS */ 275 const char *zName, /* Name of file to be opened */ 276 sqlite3_file *pConn, /* Fill in this file descriptor */ 277 int flags, /* Flags to control the opening */ 278 int *pOutFlags /* Flags showing results of opening */ 279 ){ 280 int rc; /* Result code */ 281 quotaConn *pQuotaOpen; /* The new quota file descriptor */ 282 quotaFile *pFile; /* Corresponding quotaFile obj */ 283 quotaGroup *pGroup; /* The group file belongs to */ 284 sqlite3_file *pSubOpen; /* Real file descriptor */ 285 sqlite3_vfs *pOrigVfs = gQuota.pOrigVfs; /* Real VFS */ 286 287 /* If the file is not a main database file or a WAL, then use the 288 ** normal xOpen method. 289 */ 290 if( (flags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_WAL))==0 ){ 291 return pOrigVfs->xOpen(pOrigVfs, zName, pConn, flags, pOutFlags); 292 } 293 294 /* If the name of the file does not match any quota group, then 295 ** use the normal xOpen method. 296 */ 297 quotaEnter(); 298 pGroup = quotaGroupFind(zName); 299 if( pGroup==0 ){ 300 rc = pOrigVfs->xOpen(pOrigVfs, zName, pConn, flags, pOutFlags); 301 }else{ 302 /* If we get to this point, it means the file needs to be quota tracked. 303 */ 304 pQuotaOpen = (quotaConn*)pConn; 305 pSubOpen = quotaSubOpen(pConn); 306 rc = pOrigVfs->xOpen(pOrigVfs, zName, pSubOpen, flags, pOutFlags); 307 if( rc==SQLITE_OK ){ 308 for(pFile=pGroup->pFiles; pFile && strcmp(pFile->zFilename, zName); 309 pFile=pFile->pNext){} 310 if( pFile==0 ){ 311 int nName = strlen(zName); 312 pFile = sqlite3_malloc( sizeof(*pFile) + nName + 1 ); 313 if( pFile==0 ){ 314 quotaLeave(); 315 pSubOpen->pMethods->xClose(pSubOpen); 316 return SQLITE_NOMEM; 317 } 318 memset(pFile, 0, sizeof(*pFile)); 319 pFile->zFilename = (char*)&pFile[1]; 320 memcpy(pFile->zFilename, zName, nName+1); 321 pFile->pNext = pGroup->pFiles; 322 if( pGroup->pFiles ) pGroup->pFiles->ppPrev = &pFile->pNext; 323 pFile->ppPrev = &pGroup->pFiles; 324 pGroup->pFiles = pFile; 325 pFile->pGroup = pGroup; 326 } 327 pFile->nRef++; 328 pQuotaOpen->pFile = pFile; 329 if( pSubOpen->pMethods->iVersion==1 ){ 330 pQuotaOpen->base.pMethods = &gQuota.sIoMethodsV1; 331 }else{ 332 pQuotaOpen->base.pMethods = &gQuota.sIoMethodsV2; 333 } 334 } 335 } 336 quotaLeave(); 337 return rc; 338 } 339 340 /************************ I/O Method Wrappers *******************************/ 341 342 /* xClose requests get passed through to the original VFS. But we 343 ** also have to unlink the quotaConn from the quotaFile and quotaGroup. 344 ** The quotaFile and/or quotaGroup are freed if they are no longer in use. 345 */ 346 static int quotaClose(sqlite3_file *pConn){ 347 quotaConn *p = (quotaConn*)pConn; 348 quotaFile *pFile = p->pFile; 349 sqlite3_file *pSubOpen = quotaSubOpen(pConn); 350 int rc; 351 rc = pSubOpen->pMethods->xClose(pSubOpen); 352 quotaEnter(); 353 pFile->nRef--; 354 if( pFile->nRef==0 ){ 355 quotaGroup *pGroup = pFile->pGroup; 356 pGroup->iSize -= pFile->iSize; 357 if( pFile->pNext ) pFile->pNext->ppPrev = pFile->ppPrev; 358 *pFile->ppPrev = pFile->pNext; 359 quotaGroupDeref(pGroup); 360 sqlite3_free(pFile); 361 } 362 quotaLeave(); 363 return rc; 364 } 365 366 /* Pass xRead requests directory thru to the original VFS without 367 ** further processing. 368 */ 369 static int quotaRead( 370 sqlite3_file *pConn, 371 void *pBuf, 372 int iAmt, 373 sqlite3_int64 iOfst 374 ){ 375 sqlite3_file *pSubOpen = quotaSubOpen(pConn); 376 return pSubOpen->pMethods->xRead(pSubOpen, pBuf, iAmt, iOfst); 377 } 378 379 /* Check xWrite requests to see if they expand the file. If they do, 380 ** the perform a quota check before passing them through to the 381 ** original VFS. 382 */ 383 static int quotaWrite( 384 sqlite3_file *pConn, 385 const void *pBuf, 386 int iAmt, 387 sqlite3_int64 iOfst 388 ){ 389 quotaConn *p = (quotaConn*)pConn; 390 sqlite3_file *pSubOpen = quotaSubOpen(pConn); 391 sqlite3_int64 iEnd = iOfst+iAmt; 392 quotaGroup *pGroup; 393 quotaFile *pFile = p->pFile; 394 sqlite3_int64 szNew; 395 396 if( pFile->iSize<iEnd ){ 397 pGroup = pFile->pGroup; 398 quotaEnter(); 399 szNew = pGroup->iSize - pFile->iSize + iEnd; 400 if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){ 401 if( pGroup->xCallback ){ 402 pGroup->xCallback(pFile->zFilename, &pGroup->iLimit, szNew, 403 pGroup->pArg); 404 } 405 if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){ 406 quotaLeave(); 407 return SQLITE_FULL; 408 } 409 } 410 pGroup->iSize = szNew; 411 pFile->iSize = iEnd; 412 quotaLeave(); 413 } 414 return pSubOpen->pMethods->xWrite(pSubOpen, pBuf, iAmt, iOfst); 415 } 416 417 /* Pass xTruncate requests thru to the original VFS. If the 418 ** success, update the file size. 419 */ 420 static int quotaTruncate(sqlite3_file *pConn, sqlite3_int64 size){ 421 quotaConn *p = (quotaConn*)pConn; 422 sqlite3_file *pSubOpen = quotaSubOpen(pConn); 423 int rc = pSubOpen->pMethods->xTruncate(pSubOpen, size); 424 quotaFile *pFile = p->pFile; 425 quotaGroup *pGroup; 426 if( rc==SQLITE_OK ){ 427 quotaEnter(); 428 pGroup = pFile->pGroup; 429 pGroup->iSize -= pFile->iSize; 430 pFile->iSize = size; 431 pGroup->iSize += size; 432 quotaLeave(); 433 } 434 return rc; 435 } 436 437 /* Pass xSync requests through to the original VFS without change 438 */ 439 static int quotaSync(sqlite3_file *pConn, int flags){ 440 sqlite3_file *pSubOpen = quotaSubOpen(pConn); 441 return pSubOpen->pMethods->xSync(pSubOpen, flags); 442 } 443 444 /* Pass xFileSize requests through to the original VFS but then 445 ** update the quotaGroup with the new size before returning. 446 */ 447 static int quotaFileSize(sqlite3_file *pConn, sqlite3_int64 *pSize){ 448 quotaConn *p = (quotaConn*)pConn; 449 sqlite3_file *pSubOpen = quotaSubOpen(pConn); 450 quotaFile *pFile = p->pFile; 451 quotaGroup *pGroup; 452 sqlite3_int64 sz; 453 int rc; 454 455 rc = pSubOpen->pMethods->xFileSize(pSubOpen, &sz); 456 if( rc==SQLITE_OK ){ 457 quotaEnter(); 458 pGroup = pFile->pGroup; 459 pGroup->iSize -= pFile->iSize; 460 pFile->iSize = sz; 461 pGroup->iSize += sz; 462 quotaLeave(); 463 *pSize = sz; 464 } 465 return rc; 466 } 467 468 /* Pass xLock requests through to the original VFS unchanged. 469 */ 470 static int quotaLock(sqlite3_file *pConn, int lock){ 471 sqlite3_file *pSubOpen = quotaSubOpen(pConn); 472 return pSubOpen->pMethods->xLock(pSubOpen, lock); 473 } 474 475 /* Pass xUnlock requests through to the original VFS unchanged. 476 */ 477 static int quotaUnlock(sqlite3_file *pConn, int lock){ 478 sqlite3_file *pSubOpen = quotaSubOpen(pConn); 479 return pSubOpen->pMethods->xUnlock(pSubOpen, lock); 480 } 481 482 /* Pass xCheckReservedLock requests through to the original VFS unchanged. 483 */ 484 static int quotaCheckReservedLock(sqlite3_file *pConn, int *pResOut){ 485 sqlite3_file *pSubOpen = quotaSubOpen(pConn); 486 return pSubOpen->pMethods->xCheckReservedLock(pSubOpen, pResOut); 487 } 488 489 /* Pass xFileControl requests through to the original VFS unchanged. 490 */ 491 static int quotaFileControl(sqlite3_file *pConn, int op, void *pArg){ 492 sqlite3_file *pSubOpen = quotaSubOpen(pConn); 493 return pSubOpen->pMethods->xFileControl(pSubOpen, op, pArg); 494 } 495 496 /* Pass xSectorSize requests through to the original VFS unchanged. 497 */ 498 static int quotaSectorSize(sqlite3_file *pConn){ 499 sqlite3_file *pSubOpen = quotaSubOpen(pConn); 500 return pSubOpen->pMethods->xSectorSize(pSubOpen); 501 } 502 503 /* Pass xDeviceCharacteristics requests through to the original VFS unchanged. 504 */ 505 static int quotaDeviceCharacteristics(sqlite3_file *pConn){ 506 sqlite3_file *pSubOpen = quotaSubOpen(pConn); 507 return pSubOpen->pMethods->xDeviceCharacteristics(pSubOpen); 508 } 509 510 /* Pass xShmMap requests through to the original VFS unchanged. 511 */ 512 static int quotaShmMap( 513 sqlite3_file *pConn, /* Handle open on database file */ 514 int iRegion, /* Region to retrieve */ 515 int szRegion, /* Size of regions */ 516 int bExtend, /* True to extend file if necessary */ 517 void volatile **pp /* OUT: Mapped memory */ 518 ){ 519 sqlite3_file *pSubOpen = quotaSubOpen(pConn); 520 return pSubOpen->pMethods->xShmMap(pSubOpen, iRegion, szRegion, bExtend, pp); 521 } 522 523 /* Pass xShmLock requests through to the original VFS unchanged. 524 */ 525 static int quotaShmLock( 526 sqlite3_file *pConn, /* Database file holding the shared memory */ 527 int ofst, /* First lock to acquire or release */ 528 int n, /* Number of locks to acquire or release */ 529 int flags /* What to do with the lock */ 530 ){ 531 sqlite3_file *pSubOpen = quotaSubOpen(pConn); 532 return pSubOpen->pMethods->xShmLock(pSubOpen, ofst, n, flags); 533 } 534 535 /* Pass xShmBarrier requests through to the original VFS unchanged. 536 */ 537 static void quotaShmBarrier(sqlite3_file *pConn){ 538 sqlite3_file *pSubOpen = quotaSubOpen(pConn); 539 pSubOpen->pMethods->xShmBarrier(pSubOpen); 540 } 541 542 /* Pass xShmUnmap requests through to the original VFS unchanged. 543 */ 544 static int quotaShmUnmap(sqlite3_file *pConn, int deleteFlag){ 545 sqlite3_file *pSubOpen = quotaSubOpen(pConn); 546 return pSubOpen->pMethods->xShmUnmap(pSubOpen, deleteFlag); 547 } 548 549 /************************** Public Interfaces *****************************/ 550 /* 551 ** Initialize the quota VFS shim. Use the VFS named zOrigVfsName 552 ** as the VFS that does the actual work. Use the default if 553 ** zOrigVfsName==NULL. 554 ** 555 ** The quota VFS shim is named "quota". It will become the default 556 ** VFS if makeDefault is non-zero. 557 ** 558 ** THIS ROUTINE IS NOT THREADSAFE. Call this routine exactly once 559 ** during start-up. 560 */ 561 int sqlite3_quota_initialize(const char *zOrigVfsName, int makeDefault){ 562 sqlite3_vfs *pOrigVfs; 563 if( gQuota.isInitialized ) return SQLITE_MISUSE; 564 pOrigVfs = sqlite3_vfs_find(zOrigVfsName); 565 if( pOrigVfs==0 ) return SQLITE_ERROR; 566 assert( pOrigVfs!=&gQuota.sThisVfs ); 567 gQuota.pMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); 568 if( !gQuota.pMutex ){ 569 return SQLITE_NOMEM; 570 } 571 gQuota.isInitialized = 1; 572 gQuota.pOrigVfs = pOrigVfs; 573 gQuota.sThisVfs = *pOrigVfs; 574 gQuota.sThisVfs.xOpen = quotaOpen; 575 gQuota.sThisVfs.szOsFile += sizeof(quotaConn); 576 gQuota.sThisVfs.zName = "quota"; 577 gQuota.sIoMethodsV1.iVersion = 1; 578 gQuota.sIoMethodsV1.xClose = quotaClose; 579 gQuota.sIoMethodsV1.xRead = quotaRead; 580 gQuota.sIoMethodsV1.xWrite = quotaWrite; 581 gQuota.sIoMethodsV1.xTruncate = quotaTruncate; 582 gQuota.sIoMethodsV1.xSync = quotaSync; 583 gQuota.sIoMethodsV1.xFileSize = quotaFileSize; 584 gQuota.sIoMethodsV1.xLock = quotaLock; 585 gQuota.sIoMethodsV1.xUnlock = quotaUnlock; 586 gQuota.sIoMethodsV1.xCheckReservedLock = quotaCheckReservedLock; 587 gQuota.sIoMethodsV1.xFileControl = quotaFileControl; 588 gQuota.sIoMethodsV1.xSectorSize = quotaSectorSize; 589 gQuota.sIoMethodsV1.xDeviceCharacteristics = quotaDeviceCharacteristics; 590 gQuota.sIoMethodsV2 = gQuota.sIoMethodsV1; 591 gQuota.sIoMethodsV2.iVersion = 2; 592 gQuota.sIoMethodsV2.xShmMap = quotaShmMap; 593 gQuota.sIoMethodsV2.xShmLock = quotaShmLock; 594 gQuota.sIoMethodsV2.xShmBarrier = quotaShmBarrier; 595 gQuota.sIoMethodsV2.xShmUnmap = quotaShmUnmap; 596 sqlite3_vfs_register(&gQuota.sThisVfs, makeDefault); 597 return SQLITE_OK; 598 } 599 600 /* 601 ** Shutdown the quota system. 602 ** 603 ** All SQLite database connections must be closed before calling this 604 ** routine. 605 ** 606 ** THIS ROUTINE IS NOT THREADSAFE. Call this routine exactly one while 607 ** shutting down in order to free all remaining quota groups. 608 */ 609 int sqlite3_quota_shutdown(void){ 610 quotaGroup *pGroup; 611 if( gQuota.isInitialized==0 ) return SQLITE_MISUSE; 612 for(pGroup=gQuota.pGroup; pGroup; pGroup=pGroup->pNext){ 613 if( pGroup->pFiles ) return SQLITE_MISUSE; 614 } 615 while( gQuota.pGroup ){ 616 pGroup = gQuota.pGroup; 617 gQuota.pGroup = pGroup->pNext; 618 pGroup->iLimit = 0; 619 quotaGroupDeref(pGroup); 620 } 621 gQuota.isInitialized = 0; 622 sqlite3_mutex_free(gQuota.pMutex); 623 sqlite3_vfs_unregister(&gQuota.sThisVfs); 624 memset(&gQuota, 0, sizeof(gQuota)); 625 return SQLITE_OK; 626 } 627 628 /* 629 ** Create or destroy a quota group. 630 ** 631 ** The quota group is defined by the zPattern. When calling this routine 632 ** with a zPattern for a quota group that already exists, this routine 633 ** merely updates the iLimit, xCallback, and pArg values for that quota 634 ** group. If zPattern is new, then a new quota group is created. 635 ** 636 ** If the iLimit for a quota group is set to zero, then the quota group 637 ** is disabled and will be deleted when the last database connection using 638 ** the quota group is closed. 639 ** 640 ** Calling this routine on a zPattern that does not exist and with a 641 ** zero iLimit is a no-op. 642 ** 643 ** A quota group must exist with a non-zero iLimit prior to opening 644 ** database connections if those connections are to participate in the 645 ** quota group. Creating a quota group does not affect database connections 646 ** that are already open. 647 */ 648 int sqlite3_quota_set( 649 const char *zPattern, /* The filename pattern */ 650 sqlite3_int64 iLimit, /* New quota to set for this quota group */ 651 void (*xCallback)( /* Callback invoked when going over quota */ 652 const char *zFilename, /* Name of file whose size increases */ 653 sqlite3_int64 *piLimit, /* IN/OUT: The current limit */ 654 sqlite3_int64 iSize, /* Total size of all files in the group */ 655 void *pArg /* Client data */ 656 ), 657 void *pArg, /* client data passed thru to callback */ 658 void (*xDestroy)(void*) /* Optional destructor for pArg */ 659 ){ 660 quotaGroup *pGroup; 661 quotaEnter(); 662 pGroup = gQuota.pGroup; 663 while( pGroup && strcmp(pGroup->zPattern, zPattern)!=0 ){ 664 pGroup = pGroup->pNext; 665 } 666 if( pGroup==0 ){ 667 int nPattern = strlen(zPattern); 668 if( iLimit<=0 ){ 669 quotaLeave(); 670 return SQLITE_OK; 671 } 672 pGroup = sqlite3_malloc( sizeof(*pGroup) + nPattern + 1 ); 673 if( pGroup==0 ){ 674 quotaLeave(); 675 return SQLITE_NOMEM; 676 } 677 memset(pGroup, 0, sizeof(*pGroup)); 678 pGroup->zPattern = (char*)&pGroup[1]; 679 memcpy((char *)pGroup->zPattern, zPattern, nPattern+1); 680 if( gQuota.pGroup ) gQuota.pGroup->ppPrev = &pGroup->pNext; 681 pGroup->pNext = gQuota.pGroup; 682 pGroup->ppPrev = &gQuota.pGroup; 683 gQuota.pGroup = pGroup; 684 } 685 pGroup->iLimit = iLimit; 686 pGroup->xCallback = xCallback; 687 if( pGroup->xDestroy && pGroup->pArg!=pArg ){ 688 pGroup->xDestroy(pGroup->pArg); 689 } 690 pGroup->pArg = pArg; 691 pGroup->xDestroy = xDestroy; 692 quotaGroupDeref(pGroup); 693 quotaLeave(); 694 return SQLITE_OK; 695 } 696 697 698 /***************************** Test Code ***********************************/ 699 #ifdef SQLITE_TEST 700 #include <tcl.h> 701 702 /* 703 ** Argument passed to a TCL quota-over-limit callback. 704 */ 705 typedef struct TclQuotaCallback TclQuotaCallback; 706 struct TclQuotaCallback { 707 Tcl_Interp *interp; /* Interpreter in which to run the script */ 708 Tcl_Obj *pScript; /* Script to be run */ 709 }; 710 711 extern const char *sqlite3TestErrorName(int); 712 713 714 /* 715 ** This is the callback from a quota-over-limit. 716 */ 717 static void tclQuotaCallback( 718 const char *zFilename, /* Name of file whose size increases */ 719 sqlite3_int64 *piLimit, /* IN/OUT: The current limit */ 720 sqlite3_int64 iSize, /* Total size of all files in the group */ 721 void *pArg /* Client data */ 722 ){ 723 TclQuotaCallback *p; /* Callback script object */ 724 Tcl_Obj *pEval; /* Script to evaluate */ 725 Tcl_Obj *pVarname; /* Name of variable to pass as 2nd arg */ 726 unsigned int rnd; /* Random part of pVarname */ 727 int rc; /* Tcl error code */ 728 729 p = (TclQuotaCallback *)pArg; 730 if( p==0 ) return; 731 732 pVarname = Tcl_NewStringObj("::piLimit_", -1); 733 Tcl_IncrRefCount(pVarname); 734 sqlite3_randomness(sizeof(rnd), (void *)&rnd); 735 Tcl_AppendObjToObj(pVarname, Tcl_NewIntObj((int)(rnd&0x7FFFFFFF))); 736 Tcl_ObjSetVar2(p->interp, pVarname, 0, Tcl_NewWideIntObj(*piLimit), 0); 737 738 pEval = Tcl_DuplicateObj(p->pScript); 739 Tcl_IncrRefCount(pEval); 740 Tcl_ListObjAppendElement(0, pEval, Tcl_NewStringObj(zFilename, -1)); 741 Tcl_ListObjAppendElement(0, pEval, pVarname); 742 Tcl_ListObjAppendElement(0, pEval, Tcl_NewWideIntObj(iSize)); 743 rc = Tcl_EvalObjEx(p->interp, pEval, TCL_EVAL_GLOBAL); 744 745 if( rc==TCL_OK ){ 746 Tcl_Obj *pLimit = Tcl_ObjGetVar2(p->interp, pVarname, 0, 0); 747 rc = Tcl_GetWideIntFromObj(p->interp, pLimit, piLimit); 748 Tcl_UnsetVar(p->interp, Tcl_GetString(pVarname), 0); 749 } 750 751 Tcl_DecrRefCount(pEval); 752 Tcl_DecrRefCount(pVarname); 753 if( rc!=TCL_OK ) Tcl_BackgroundError(p->interp); 754 } 755 756 /* 757 ** Destructor for a TCL quota-over-limit callback. 758 */ 759 static void tclCallbackDestructor(void *pObj){ 760 TclQuotaCallback *p = (TclQuotaCallback*)pObj; 761 if( p ){ 762 Tcl_DecrRefCount(p->pScript); 763 sqlite3_free((char *)p); 764 } 765 } 766 767 /* 768 ** tclcmd: sqlite3_quota_initialize NAME MAKEDEFAULT 769 */ 770 static int test_quota_initialize( 771 void * clientData, 772 Tcl_Interp *interp, 773 int objc, 774 Tcl_Obj *CONST objv[] 775 ){ 776 const char *zName; /* Name of new quota VFS */ 777 int makeDefault; /* True to make the new VFS the default */ 778 int rc; /* Value returned by quota_initialize() */ 779 780 /* Process arguments */ 781 if( objc!=3 ){ 782 Tcl_WrongNumArgs(interp, 1, objv, "NAME MAKEDEFAULT"); 783 return TCL_ERROR; 784 } 785 zName = Tcl_GetString(objv[1]); 786 if( Tcl_GetBooleanFromObj(interp, objv[2], &makeDefault) ) return TCL_ERROR; 787 if( zName[0]=='\0' ) zName = 0; 788 789 /* Call sqlite3_quota_initialize() */ 790 rc = sqlite3_quota_initialize(zName, makeDefault); 791 Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC); 792 793 return TCL_OK; 794 } 795 796 /* 797 ** tclcmd: sqlite3_quota_shutdown 798 */ 799 static int test_quota_shutdown( 800 void * clientData, 801 Tcl_Interp *interp, 802 int objc, 803 Tcl_Obj *CONST objv[] 804 ){ 805 int rc; /* Value returned by quota_shutdown() */ 806 807 if( objc!=1 ){ 808 Tcl_WrongNumArgs(interp, 1, objv, ""); 809 return TCL_ERROR; 810 } 811 812 /* Call sqlite3_quota_shutdown() */ 813 rc = sqlite3_quota_shutdown(); 814 Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC); 815 816 return TCL_OK; 817 } 818 819 /* 820 ** tclcmd: sqlite3_quota_set PATTERN LIMIT SCRIPT 821 */ 822 static int test_quota_set( 823 void * clientData, 824 Tcl_Interp *interp, 825 int objc, 826 Tcl_Obj *CONST objv[] 827 ){ 828 const char *zPattern; /* File pattern to configure */ 829 sqlite3_int64 iLimit; /* Initial quota in bytes */ 830 Tcl_Obj *pScript; /* Tcl script to invoke to increase quota */ 831 int rc; /* Value returned by quota_set() */ 832 TclQuotaCallback *p; /* Callback object */ 833 int nScript; /* Length of callback script */ 834 void (*xDestroy)(void*); /* Optional destructor for pArg */ 835 void (*xCallback)(const char *, sqlite3_int64 *, sqlite3_int64, void *); 836 837 /* Process arguments */ 838 if( objc!=4 ){ 839 Tcl_WrongNumArgs(interp, 1, objv, "PATTERN LIMIT SCRIPT"); 840 return TCL_ERROR; 841 } 842 zPattern = Tcl_GetString(objv[1]); 843 if( Tcl_GetWideIntFromObj(interp, objv[2], &iLimit) ) return TCL_ERROR; 844 pScript = objv[3]; 845 Tcl_GetStringFromObj(pScript, &nScript); 846 847 if( nScript>0 ){ 848 /* Allocate a TclQuotaCallback object */ 849 p = (TclQuotaCallback *)sqlite3_malloc(sizeof(TclQuotaCallback)); 850 if( !p ){ 851 Tcl_SetResult(interp, (char *)"SQLITE_NOMEM", TCL_STATIC); 852 return TCL_OK; 853 } 854 memset(p, 0, sizeof(TclQuotaCallback)); 855 p->interp = interp; 856 Tcl_IncrRefCount(pScript); 857 p->pScript = pScript; 858 xDestroy = tclCallbackDestructor; 859 xCallback = tclQuotaCallback; 860 }else{ 861 p = 0; 862 xDestroy = 0; 863 xCallback = 0; 864 } 865 866 /* Invoke sqlite3_quota_set() */ 867 rc = sqlite3_quota_set(zPattern, iLimit, xCallback, (void*)p, xDestroy); 868 869 Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC); 870 return TCL_OK; 871 } 872 873 /* 874 ** tclcmd: sqlite3_quota_dump 875 */ 876 static int test_quota_dump( 877 void * clientData, 878 Tcl_Interp *interp, 879 int objc, 880 Tcl_Obj *CONST objv[] 881 ){ 882 Tcl_Obj *pResult; 883 Tcl_Obj *pGroupTerm; 884 Tcl_Obj *pFileTerm; 885 quotaGroup *pGroup; 886 quotaFile *pFile; 887 888 pResult = Tcl_NewObj(); 889 quotaEnter(); 890 for(pGroup=gQuota.pGroup; pGroup; pGroup=pGroup->pNext){ 891 pGroupTerm = Tcl_NewObj(); 892 Tcl_ListObjAppendElement(interp, pGroupTerm, 893 Tcl_NewStringObj(pGroup->zPattern, -1)); 894 Tcl_ListObjAppendElement(interp, pGroupTerm, 895 Tcl_NewWideIntObj(pGroup->iLimit)); 896 Tcl_ListObjAppendElement(interp, pGroupTerm, 897 Tcl_NewWideIntObj(pGroup->iSize)); 898 for(pFile=pGroup->pFiles; pFile; pFile=pFile->pNext){ 899 pFileTerm = Tcl_NewObj(); 900 Tcl_ListObjAppendElement(interp, pFileTerm, 901 Tcl_NewStringObj(pFile->zFilename, -1)); 902 Tcl_ListObjAppendElement(interp, pFileTerm, 903 Tcl_NewWideIntObj(pFile->iSize)); 904 Tcl_ListObjAppendElement(interp, pFileTerm, 905 Tcl_NewWideIntObj(pFile->nRef)); 906 Tcl_ListObjAppendElement(interp, pGroupTerm, pFileTerm); 907 } 908 Tcl_ListObjAppendElement(interp, pResult, pGroupTerm); 909 } 910 quotaLeave(); 911 Tcl_SetObjResult(interp, pResult); 912 return TCL_OK; 913 } 914 915 /* 916 ** This routine registers the custom TCL commands defined in this 917 ** module. This should be the only procedure visible from outside 918 ** of this module. 919 */ 920 int Sqlitequota_Init(Tcl_Interp *interp){ 921 static struct { 922 char *zName; 923 Tcl_ObjCmdProc *xProc; 924 } aCmd[] = { 925 { "sqlite3_quota_initialize", test_quota_initialize }, 926 { "sqlite3_quota_shutdown", test_quota_shutdown }, 927 { "sqlite3_quota_set", test_quota_set }, 928 { "sqlite3_quota_dump", test_quota_dump }, 929 }; 930 int i; 931 932 for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){ 933 Tcl_CreateObjCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0); 934 } 935 936 return TCL_OK; 937 } 938 #endif 939