Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Add file test_quota.c, demonstrating how file-system quotas may be implemented as a VFS wrapper. |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | experimental |
Files: | files | file ages | folders |
SHA1: |
383eb87bbf560b20682dd5be0e18ddad |
User & Date: | dan 2010-09-01 11:40:05.000 |
Context
2010-09-01
| ||
12:50 | Update the quota shim so that when the same file is opened multiple times, its size only counts against the quota once. (check-in: f5d2638030 user: drh tags: experimental) | |
11:40 | Add file test_quota.c, demonstrating how file-system quotas may be implemented as a VFS wrapper. (check-in: 383eb87bbf user: dan tags: experimental) | |
02:38 | Fix some compiler warnings under MSVC. (check-in: afdc82a99e user: shaneh tags: trunk) | |
Changes
Changes to main.mk.
︙ | ︙ | |||
241 242 243 244 245 246 247 248 249 250 251 252 253 254 | $(TOP)/src/test_intarray.c \ $(TOP)/src/test_journal.c \ $(TOP)/src/test_malloc.c \ $(TOP)/src/test_mutex.c \ $(TOP)/src/test_onefile.c \ $(TOP)/src/test_osinst.c \ $(TOP)/src/test_pcache.c \ $(TOP)/src/test_rtree.c \ $(TOP)/src/test_schema.c \ $(TOP)/src/test_server.c \ $(TOP)/src/test_stat.c \ $(TOP)/src/test_tclvar.c \ $(TOP)/src/test_thread.c \ $(TOP)/src/test_vfs.c \ | > | 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | $(TOP)/src/test_intarray.c \ $(TOP)/src/test_journal.c \ $(TOP)/src/test_malloc.c \ $(TOP)/src/test_mutex.c \ $(TOP)/src/test_onefile.c \ $(TOP)/src/test_osinst.c \ $(TOP)/src/test_pcache.c \ $(TOP)/src/test_quota.c \ $(TOP)/src/test_rtree.c \ $(TOP)/src/test_schema.c \ $(TOP)/src/test_server.c \ $(TOP)/src/test_stat.c \ $(TOP)/src/test_tclvar.c \ $(TOP)/src/test_thread.c \ $(TOP)/src/test_vfs.c \ |
︙ | ︙ |
Changes to src/tclsqlite.c.
︙ | ︙ | |||
3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 | extern int SqlitetestOnefile_Init(); extern int SqlitetestOsinst_Init(Tcl_Interp*); extern int Sqlitetestbackup_Init(Tcl_Interp*); extern int Sqlitetestintarray_Init(Tcl_Interp*); extern int Sqlitetestvfs_Init(Tcl_Interp *); extern int SqlitetestStat_Init(Tcl_Interp*); extern int Sqlitetestrtree_Init(Tcl_Interp*); Sqliteconfig_Init(interp); Sqlitetest1_Init(interp); Sqlitetest2_Init(interp); Sqlitetest3_Init(interp); Sqlitetest4_Init(interp); Sqlitetest5_Init(interp); | > | 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 | extern int SqlitetestOnefile_Init(); extern int SqlitetestOsinst_Init(Tcl_Interp*); extern int Sqlitetestbackup_Init(Tcl_Interp*); extern int Sqlitetestintarray_Init(Tcl_Interp*); extern int Sqlitetestvfs_Init(Tcl_Interp *); extern int SqlitetestStat_Init(Tcl_Interp*); extern int Sqlitetestrtree_Init(Tcl_Interp*); extern int Sqlitequota_Init(Tcl_Interp*); Sqliteconfig_Init(interp); Sqlitetest1_Init(interp); Sqlitetest2_Init(interp); Sqlitetest3_Init(interp); Sqlitetest4_Init(interp); Sqlitetest5_Init(interp); |
︙ | ︙ | |||
3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 | SqlitetestOnefile_Init(interp); SqlitetestOsinst_Init(interp); Sqlitetestbackup_Init(interp); Sqlitetestintarray_Init(interp); Sqlitetestvfs_Init(interp); SqlitetestStat_Init(interp); Sqlitetestrtree_Init(interp); Tcl_CreateObjCommand(interp,"load_testfixture_extensions",init_all_cmd,0,0); #ifdef SQLITE_SSE Sqlitetestsse_Init(interp); #endif } | > | 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 | SqlitetestOnefile_Init(interp); SqlitetestOsinst_Init(interp); Sqlitetestbackup_Init(interp); Sqlitetestintarray_Init(interp); Sqlitetestvfs_Init(interp); SqlitetestStat_Init(interp); Sqlitetestrtree_Init(interp); Sqlitequota_Init(interp); Tcl_CreateObjCommand(interp,"load_testfixture_extensions",init_all_cmd,0,0); #ifdef SQLITE_SSE Sqlitetestsse_Init(interp); #endif } |
︙ | ︙ |
Added src/test_quota.c.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 | /* ** 2010 September 31 ** ** The author disclaims copyright to this source code. In place of ** a legal notice, here is a blessing: ** ** May you do good and not evil. ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ************************************************************************* ** ** This file contains a VFS "shim" - a layer that sits in between the ** pager and the real VFS. ** ** This particular shim enforces a quota system on files. One or more ** database files are in a "quota group" that is defined by a GLOB ** pattern. A quota is set for the combined size of all files in the ** the group. A quota of zero means "no limit". If the total size ** of all files in the quota group is met or exceeded, then new ** write requests that attempt to enlarge a file fail with SQLITE_FULL. ** ** However, before returning SQLITE_FULL, the write requests invoke ** a callback function that is configurable for each quota group. ** This callback has the opportunity to enlarge the quota. If the ** callback does enlarge the quota such that the total size of all ** files within the group is less than the new quota, then the write ** continues as if nothing had happened. */ #include "sqlite3.h" #include <string.h> #include <assert.h> /************************ Object Definitions ******************************/ /* ** This module contains a table of filename patterns that have size ** quotas. The quota applies to the sum of the sizes of all open ** database files whose names match the GLOB pattern. ** ** Each quota is an instance of the following object. Quotas must ** be established (using sqlite3_quota_set()) prior to opening any ** of the database connections that access files governed by the ** quota. ** ** Each entry in the quota table is an instance of the following object. */ typedef struct quotaGroup quotaGroup; struct quotaGroup { const char *zPattern; /* Filename pattern to be quotaed */ sqlite3_int64 iLimit; /* Upper bound on total file size */ sqlite3_int64 iSize; /* Current size of all files */ void (*xCallback)( /* Callback invoked when going over quota */ const char *zFilename, /* Name of file whose size increases */ sqlite3_int64 *piLimit, /* IN/OUT: The current limit */ sqlite3_int64 iSize, /* Total size of all files in the group */ void *pArg /* Client data */ ); void *pArg; /* Third argument to the xCallback() */ int nRef; /* Number of files in the group references. */ quotaGroup *pNext, **ppPrev; /* Doubly linked list of all quota objects */ }; /* ** An instance of the following object represents each file that ** participates in quota tracking. The sqlite3_file object for the ** underlying VFS is appended to this structure. */ typedef struct quotaFile quotaFile; struct quotaFile { sqlite3_file base; /* Base class - must be first */ const char *zFilename; /* Name of this file */ quotaGroup *pGroup; /* Upper bound on file size */ sqlite3_int64 iSize; /* Current size of this file */ /* The underlying VFS sqlite3_file is appended to this object */ }; /************************* Global Variables **********************************/ /* ** All global variables used by this file are containing within the following ** gQuota structure. */ static struct { /* The pOrigVfs is a pointer to the real underlying VFS implementation. ** Most operations pass-through to the real VFS. This value is read-only ** during operation. It is only modified at start-time and thus does not ** require a mutex. */ sqlite3_vfs *pOrigVfs; /* The sThisVfs is the VFS structure used by this shim. It is initialized ** at start-time and thus does not require a mutex */ sqlite3_vfs sThisVfs; /* The sIoMethods defines the methods used by sqlite3_file objects ** associated with this shim. It is initialized at start-time and does ** not require a mutex. ** ** When the underlying VFS is called to open a file, it might return ** either a version 1 or a version 2 sqlite3_file object. This shim ** has to create a wrapper sqlite3_file of the same version. Hence ** there are two I/O method structures, one for version 1 and the other ** for version 2. */ sqlite3_io_methods sIoMethodsV1; sqlite3_io_methods sIoMethodsV2; /* True when this shim as been initialized. */ int isInitialized; /* For run-time access any of the other global data structures in this ** shim, the following mutex must be held. */ sqlite3_mutex *pMutex; /* List of quotaGroup objects. */ quotaGroup *pGroup; } gQuota; /************************* Utility Routines *********************************/ /* ** Acquire and release the mutex used to serialize access to the ** list of quotaGroups. */ static void quotaEnter(void){ sqlite3_mutex_enter(gQuota.pMutex); } static void quotaLeave(void){ sqlite3_mutex_leave(gQuota.pMutex); } /* If the reference count and threshold for a quotaGroup are both ** zero, then destroy the quotaGroup. */ static void quotaGroupDeref(quotaGroup *p){ if( p->nRef==0 && p->iLimit==0 ){ if( p->pNext ) p->pNext->ppPrev = p->ppPrev; if( p->ppPrev ) *p->ppPrev = p->pNext; sqlite3_free(p); } } /* ** Return TRUE if string z matches glob pattern zGlob. ** ** Globbing rules: ** ** '*' Matches any sequence of zero or more characters. ** ** '?' Matches exactly one character. ** ** [...] Matches one character from the enclosed list of ** characters. ** ** [^...] Matches one character not in the enclosed list. ** */ static int strglob(const char *zGlob, const char *z){ int c, c2; int invert; int seen; while( (c = (*(zGlob++)))!=0 ){ if( c=='*' ){ while( (c=(*(zGlob++))) == '*' || c=='?' ){ if( c=='?' && (*(z++))==0 ) return 0; } if( c==0 ){ return 1; }else if( c=='[' ){ while( *z && strglob(zGlob-1,z)==0 ){ z++; } return (*z)!=0; } while( (c2 = (*(z++)))!=0 ){ while( c2!=c ){ c2 = *(z++); if( c2==0 ) return 0; } if( strglob(zGlob,z) ) return 1; } return 0; }else if( c=='?' ){ if( (*(z++))==0 ) return 0; }else if( c=='[' ){ int prior_c = 0; seen = 0; invert = 0; c = *(z++); if( c==0 ) return 0; c2 = *(zGlob++); if( c2=='^' ){ invert = 1; c2 = *(zGlob++); } if( c2==']' ){ if( c==']' ) seen = 1; c2 = *(zGlob++); } while( c2 && c2!=']' ){ if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){ c2 = *(zGlob++); if( c>=prior_c && c<=c2 ) seen = 1; prior_c = 0; }else{ if( c==c2 ){ seen = 1; } prior_c = c2; } c2 = *(zGlob++); } if( c2==0 || (seen ^ invert)==0 ) return 0; }else{ if( c!=(*(z++)) ) return 0; } } return *z==0; } /* Find a quotaGroup given the filename. ** ** Return a pointer to the quotaFile object. Return NULL if not found. */ static quotaGroup *quotaGroupFind(const char *zFilename){ quotaGroup *p; for(p=gQuota.pGroup; p && strglob(p->zPattern, zFilename)==0; p=p->pNext){} return p; } /* Translate an sqlite3_file* that is really a quotaFile* into ** an sqlite3_file* for the underlying original VFS. */ static sqlite3_file *quotaSubFile(sqlite3_file *pFile){ quotaFile *p = (quotaFile*)pFile; return (sqlite3_file*)&p[1]; } /************************* VFS Method Wrappers *****************************/ /* ** This is the xOpen method used for the "quota" VFS. ** ** Most of the work is done by the underlying original VFS. This method ** simply links the new file into the quota group if it is a file that ** needs to be tracked. */ static int quotaOpen( sqlite3_vfs *pVfs, const char *zName, sqlite3_file *pFile, int flags, int *pOutFlags ){ int rc; quotaFile *pQuotaFile; quotaGroup *pGroup; sqlite3_file *pSubFile; sqlite3_vfs *pOrigVfs = gQuota.pOrigVfs; /* If the file is not a main database file or a WAL, then use the ** normal xOpen method. */ if( (flags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_WAL))==0 ){ return pOrigVfs->xOpen(pOrigVfs, zName, pFile, flags, pOutFlags); } /* If the name of the file does not match any quota group, then ** use the normal xOpen method. */ quotaEnter(); pGroup = quotaGroupFind(zName); if( pGroup==0 ){ rc = pOrigVfs->xOpen(pOrigVfs, zName, pFile, flags, pOutFlags); }else{ /* If we get to this point, it means the file needs to be quota tracked. */ pQuotaFile = (quotaFile*)pFile; pSubFile = quotaSubFile(pFile); rc = pOrigVfs->xOpen(pOrigVfs, zName, pSubFile, flags, pOutFlags); if( rc==SQLITE_OK ){ pQuotaFile->iSize = 0; pQuotaFile->pGroup = pGroup; pGroup->nRef++; pQuotaFile->zFilename = zName; if( pSubFile->pMethods->iVersion==1 ){ pQuotaFile->base.pMethods = &gQuota.sIoMethodsV1; }else{ pQuotaFile->base.pMethods = &gQuota.sIoMethodsV2; } } } quotaLeave(); return rc; } /************************ I/O Method Wrappers *******************************/ /* xClose requests get passed through to the original VFS. But we ** also have to unlink the quotaFile from the quotaGroup. */ static int quotaClose(sqlite3_file *pFile){ quotaFile *p = (quotaFile*)pFile; quotaGroup *pGroup = p->pGroup; sqlite3_file *pSubFile = quotaSubFile(pFile); int rc; rc = pSubFile->pMethods->xClose(pSubFile); quotaEnter(); pGroup->nRef--; pGroup->iSize -= p->iSize; quotaGroupDeref(pGroup); quotaLeave(); return rc; } /* Pass xRead requests directory thru to the original VFS without ** further processing. */ static int quotaRead( sqlite3_file *pFile, void *pBuf, int iAmt, sqlite3_int64 iOfst ){ sqlite3_file *pSubFile = quotaSubFile(pFile); return pSubFile->pMethods->xRead(pSubFile, pBuf, iAmt, iOfst); } /* Check xWrite requests to see if they expand the file. If they do, ** the perform a quota check before passing them through to the ** original VFS. */ static int quotaWrite( sqlite3_file *pFile, const void *pBuf, int iAmt, sqlite3_int64 iOfst ){ quotaFile *p = (quotaFile*)pFile; sqlite3_file *pSubFile = quotaSubFile(pFile); sqlite3_int64 iEnd = iOfst+iAmt; quotaGroup *pGroup; sqlite3_int64 szNew; if( p->iSize<iEnd ){ pGroup = p->pGroup; quotaEnter(); szNew = pGroup->iSize - p->iSize + iEnd; if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){ if( pGroup->xCallback ){ pGroup->xCallback(p->zFilename, &pGroup->iLimit, szNew, pGroup->pArg); } if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){ quotaLeave(); return SQLITE_FULL; } } pGroup->iSize = szNew; p->iSize = iEnd; quotaLeave(); } return pSubFile->pMethods->xWrite(pSubFile, pBuf, iAmt, iOfst); } /* Pass xTruncate requests thru to the original VFS. If the ** success, update the file size. */ static int quotaTruncate(sqlite3_file *pFile, sqlite3_int64 size){ quotaFile *p = (quotaFile*)pFile; sqlite3_file *pSubFile = quotaSubFile(pFile); int rc = pSubFile->pMethods->xTruncate(pSubFile, size); quotaGroup *pGroup = p->pGroup; if( rc==SQLITE_OK ){ quotaEnter(); pGroup->iSize -= p->iSize; p->iSize = size; pGroup->iSize += size; quotaLeave(); } return rc; } /* Pass xSync requests through to the original VFS without change */ static int quotaSync(sqlite3_file *pFile, int flags){ sqlite3_file *pSubFile = quotaSubFile(pFile); return pSubFile->pMethods->xSync(pSubFile, flags); } /* Pass xFileSize requests through to the original VFS but then ** update the quotaGroup with the new size before returning. */ static int quotaFileSize(sqlite3_file *pFile, sqlite3_int64 *pSize){ quotaFile *p = (quotaFile*)pFile; sqlite3_file *pSubFile = quotaSubFile(pFile); quotaGroup *pGroup; sqlite3_int64 sz; int rc; rc = pSubFile->pMethods->xFileSize(pSubFile, &sz); if( rc==SQLITE_OK ){ pGroup = p->pGroup; quotaEnter(); pGroup->iSize -= p->iSize; p->iSize = sz; pGroup->iSize += sz; quotaLeave(); *pSize = sz; } return rc; } /* Pass xLock requests through to the original VFS unchanged. */ static int quotaLock(sqlite3_file *pFile, int lock){ sqlite3_file *pSubFile = quotaSubFile(pFile); return pSubFile->pMethods->xLock(pSubFile, lock); } /* Pass xUnlock requests through to the original VFS unchanged. */ static int quotaUnlock(sqlite3_file *pFile, int lock){ sqlite3_file *pSubFile = quotaSubFile(pFile); return pSubFile->pMethods->xUnlock(pSubFile, lock); } /* Pass xCheckReservedLock requests through to the original VFS unchanged. */ static int quotaCheckReservedLock(sqlite3_file *pFile, int *pResOut){ sqlite3_file *pSubFile = quotaSubFile(pFile); return pSubFile->pMethods->xCheckReservedLock(pSubFile, pResOut); } /* Pass xFileControl requests through to the original VFS unchanged. */ static int quotaFileControl(sqlite3_file *pFile, int op, void *pArg){ sqlite3_file *pSubFile = quotaSubFile(pFile); return pSubFile->pMethods->xFileControl(pSubFile, op, pArg); } /* Pass xSectorSize requests through to the original VFS unchanged. */ static int quotaSectorSize(sqlite3_file *pFile){ sqlite3_file *pSubFile = quotaSubFile(pFile); return pSubFile->pMethods->xSectorSize(pSubFile); } /* Pass xDeviceCharacteristics requests through to the original VFS unchanged. */ static int quotaDeviceCharacteristics(sqlite3_file *pFile){ sqlite3_file *pSubFile = quotaSubFile(pFile); return pSubFile->pMethods->xDeviceCharacteristics(pSubFile); } /* Pass xShmMap requests through to the original VFS unchanged. */ static int quotaShmMap( sqlite3_file *pFile, /* Handle open on database file */ int iRegion, /* Region to retrieve */ int szRegion, /* Size of regions */ int bExtend, /* True to extend file if necessary */ void volatile **pp /* OUT: Mapped memory */ ){ sqlite3_file *pSubFile = quotaSubFile(pFile); return pSubFile->pMethods->xShmMap(pSubFile, iRegion, szRegion, bExtend, pp); } /* Pass xShmLock requests through to the original VFS unchanged. */ static int quotaShmLock( sqlite3_file *pFile, /* Database file holding the shared memory */ int ofst, /* First lock to acquire or release */ int n, /* Number of locks to acquire or release */ int flags /* What to do with the lock */ ){ sqlite3_file *pSubFile = quotaSubFile(pFile); return pSubFile->pMethods->xShmLock(pSubFile, ofst, n, flags); } /* Pass xShmBarrier requests through to the original VFS unchanged. */ static void quotaShmBarrier(sqlite3_file *pFile){ sqlite3_file *pSubFile = quotaSubFile(pFile); pSubFile->pMethods->xShmBarrier(pSubFile); } /* Pass xShmUnmap requests through to the original VFS unchanged. */ static int quotaShmUnmap(sqlite3_file *pFile, int deleteFlag){ sqlite3_file *pSubFile = quotaSubFile(pFile); return pSubFile->pMethods->xShmUnmap(pSubFile, deleteFlag); } /************************** Public Interfaces *****************************/ /* ** Initialize the quota VFS shim. Use the VFS named zOrigVfsName ** as the VFS that does the actual work. Use the default if ** zOrigVfsName==NULL. ** ** The quota VFS shim is named "quota". It will become the default ** VFS if makeDefault is non-zero. ** ** THIS ROUTINE IS NOT THREADSAFE. Call this routine exactly once ** during start-up. */ int sqlite3_quota_initialize(const char *zOrigVfsName, int makeDefault){ sqlite3_vfs *pOrigVfs; if( gQuota.isInitialized ) return SQLITE_MISUSE; pOrigVfs = sqlite3_vfs_find(zOrigVfsName); if( pOrigVfs==0 ) return SQLITE_ERROR; assert( pOrigVfs!=&gQuota.sThisVfs ); gQuota.pMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); if( !gQuota.pMutex ){ return SQLITE_NOMEM; } gQuota.isInitialized = 1; gQuota.pOrigVfs = pOrigVfs; gQuota.sThisVfs = *pOrigVfs; gQuota.sThisVfs.xOpen = quotaOpen; gQuota.sThisVfs.szOsFile += sizeof(quotaFile); gQuota.sThisVfs.zName = "quota"; gQuota.sIoMethodsV1.iVersion = 1; gQuota.sIoMethodsV1.xClose = quotaClose; gQuota.sIoMethodsV1.xRead = quotaRead; gQuota.sIoMethodsV1.xWrite = quotaWrite; gQuota.sIoMethodsV1.xTruncate = quotaTruncate; gQuota.sIoMethodsV1.xSync = quotaSync; gQuota.sIoMethodsV1.xFileSize = quotaFileSize; gQuota.sIoMethodsV1.xLock = quotaLock; gQuota.sIoMethodsV1.xUnlock = quotaUnlock; gQuota.sIoMethodsV1.xCheckReservedLock = quotaCheckReservedLock; gQuota.sIoMethodsV1.xFileControl = quotaFileControl; gQuota.sIoMethodsV1.xSectorSize = quotaSectorSize; gQuota.sIoMethodsV1.xDeviceCharacteristics = quotaDeviceCharacteristics; gQuota.sIoMethodsV2 = gQuota.sIoMethodsV1; gQuota.sIoMethodsV2.iVersion = 2; gQuota.sIoMethodsV2.xShmMap = quotaShmMap; gQuota.sIoMethodsV2.xShmLock = quotaShmLock; gQuota.sIoMethodsV2.xShmBarrier = quotaShmBarrier; gQuota.sIoMethodsV2.xShmUnmap = quotaShmUnmap; sqlite3_vfs_register(&gQuota.sThisVfs, makeDefault); return SQLITE_OK; } /* ** Shutdown the quota system. ** ** All SQLite database connections must be closed before calling this ** routine. ** ** THIS ROUTINE IS NOT THREADSAFE. Call this routine exactly one while ** shutting down in order to free all remaining quota groups. */ int sqlite3_quota_shutdown(void){ quotaGroup *p; if( gQuota.isInitialized==0 ) return SQLITE_MISUSE; for(p=gQuota.pGroup; p; p=p->pNext){ if( p->nRef ) return SQLITE_MISUSE; } while( gQuota.pGroup ){ quotaGroup *p = gQuota.pGroup; gQuota.pGroup = p->pNext; sqlite3_free(p); } gQuota.isInitialized = 0; sqlite3_mutex_free(gQuota.pMutex); sqlite3_vfs_unregister(&gQuota.sThisVfs); memset(&gQuota, 0, sizeof(gQuota)); return SQLITE_OK; } /* ** Create or destroy a quota group. ** ** The quota group is defined by the zPattern. When calling this routine ** with a zPattern for a quota group that already exists, this routine ** merely updates the iLimit, xCallback, and pArg values for that quota ** group. If zPattern is new, then a new quota group is created. ** ** If the iLimit for a quota group is set to zero, then the quota group ** is disabled and will be deleted when the last database connection using ** the quota group is closed. ** ** Calling this routine on a zPattern that does not exist and with a ** zero iLimit is a no-op. ** ** A quota group must exist with a non-zero iLimit prior to opening ** database connections if those connections are to participate in the ** quota group. Creating a quota group does not affect database connections ** that are already open. */ int sqlite3_quota_set( const char *zPattern, /* The filename pattern */ sqlite3_int64 iLimit, /* New quota to set for this quota group */ void (*xCallback)( /* Callback invoked when going over quota */ const char *zFilename, /* Name of file whose size increases */ sqlite3_int64 *piLimit, /* IN/OUT: The current limit */ sqlite3_int64 iSize, /* Total size of all files in the group */ void *pArg /* Client data */ ), void *pArg /* client data passed thru to callback */ ){ quotaGroup *pGroup; quotaEnter(); pGroup = gQuota.pGroup; while( pGroup && strcmp(pGroup->zPattern, zPattern)!=0 ){ pGroup = pGroup->pNext; } if( pGroup==0 && iLimit>0 ){ int nPattern = strlen(zPattern); pGroup = sqlite3_malloc( sizeof(*pGroup) + nPattern + 1 ); if( pGroup==0 ){ quotaLeave(); return SQLITE_NOMEM; } memset(pGroup, 0, sizeof(*pGroup)); pGroup->zPattern = (char*)&pGroup[1]; memcpy((char *)pGroup->zPattern, zPattern, nPattern+1); if( gQuota.pGroup ) gQuota.pGroup->ppPrev = &pGroup->pNext; pGroup->pNext = gQuota.pGroup; pGroup->ppPrev = &gQuota.pGroup; gQuota.pGroup = pGroup; } pGroup->iLimit = iLimit; pGroup->xCallback = xCallback; pGroup->pArg = pArg; quotaGroupDeref(pGroup); quotaLeave(); return SQLITE_OK; } /***************************** Test Code ***********************************/ #ifdef SQLITE_TEST #include <tcl.h> typedef struct TclQuotaCallback TclQuotaCallback; struct TclQuotaCallback { Tcl_Interp *interp; Tcl_Obj *pScript; TclQuotaCallback *pNext; }; static TclQuotaCallback *pQuotaCallbackList = 0; extern const char *sqlite3TestErrorName(int); static void tclQuotaCallback( const char *zFilename, /* Name of file whose size increases */ sqlite3_int64 *piLimit, /* IN/OUT: The current limit */ sqlite3_int64 iSize, /* Total size of all files in the group */ void *pArg /* Client data */ ){ TclQuotaCallback *p; /* Callback script object */ Tcl_Obj *pEval; /* Script to evaluate */ Tcl_Obj *pVarname; /* Name of variable to pass as 2nd arg */ unsigned int rnd; /* Random part of pVarname */ int rc; /* Tcl error code */ p = (TclQuotaCallback *)pArg; pVarname = Tcl_NewStringObj("::piLimit_", -1); Tcl_IncrRefCount(pVarname); sqlite3_randomness(sizeof(rnd), (void *)&rnd); Tcl_AppendObjToObj(pVarname, Tcl_NewIntObj((int)(rnd&0x7FFFFFFF))); Tcl_ObjSetVar2(p->interp, pVarname, 0, Tcl_NewWideIntObj(*piLimit), 0); pEval = Tcl_DuplicateObj(p->pScript); Tcl_IncrRefCount(pEval); Tcl_ListObjAppendElement(0, pEval, Tcl_NewStringObj(zFilename, -1)); Tcl_ListObjAppendElement(0, pEval, pVarname); Tcl_ListObjAppendElement(0, pEval, Tcl_NewWideIntObj(iSize)); rc = Tcl_EvalObjEx(p->interp, pEval, TCL_EVAL_GLOBAL); if( rc==TCL_OK ){ Tcl_Obj *pLimit = Tcl_ObjGetVar2(p->interp, pVarname, 0, 0); rc = Tcl_GetWideIntFromObj(p->interp, pLimit, piLimit); Tcl_UnsetVar(p->interp, Tcl_GetString(pVarname), 0); } Tcl_DecrRefCount(pEval); Tcl_DecrRefCount(pVarname); if( rc!=TCL_OK ) Tcl_BackgroundError(p->interp); } /* ** tclcmd: sqlite3_quota_initialize NAME MAKEDEFAULT */ static int test_quota_initialize( void * clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[] ){ const char *zName; /* Name of new quota VFS */ int makeDefault; /* True to make the new VFS the default */ int rc; /* Value returned by quota_initialize() */ /* Process arguments */ if( objc!=3 ){ Tcl_WrongNumArgs(interp, 1, objv, "NAME MAKEDEFAULT"); return TCL_ERROR; } zName = Tcl_GetString(objv[1]); if( Tcl_GetBooleanFromObj(interp, objv[2], &makeDefault) ) return TCL_ERROR; if( zName[0]=='\0' ) zName = 0; /* Call sqlite3_quota_initialize() */ rc = sqlite3_quota_initialize(zName, makeDefault); Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC); return TCL_OK; } /* ** tclcmd: sqlite3_quota_shutdown */ static int test_quota_shutdown( void * clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[] ){ int rc; /* Value returned by quota_shutdown() */ if( objc!=1 ){ Tcl_WrongNumArgs(interp, 1, objv, ""); return TCL_ERROR; } /* Call sqlite3_quota_shutdown() */ rc = sqlite3_quota_shutdown(); Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC); /* If the quota system was successfully shut down, delete all the quota ** callback script objects in the global linked list. */ if( rc==SQLITE_OK ){ TclQuotaCallback *p, *pNext; for(p=pQuotaCallbackList; p; p=pNext){ pNext = p->pNext; Tcl_DecrRefCount(p->pScript); ckfree((char *)p); } pQuotaCallbackList = 0; } return TCL_OK; } /* ** tclcmd: sqlite3_quota_set PATTERN LIMIT SCRIPT */ static int test_quota_set( void * clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[] ){ const char *zPattern; /* File pattern to configure */ sqlite3_int64 iLimit; /* Initial quota in bytes */ Tcl_Obj *pScript; /* Tcl script to invoke to increase quota */ int rc; /* Value returned by quota_set() */ TclQuotaCallback *p; /* Callback object */ /* Process arguments */ if( objc!=4 ){ Tcl_WrongNumArgs(interp, 1, objv, "PATTERN LIMIT SCRIPT"); return TCL_ERROR; } zPattern = Tcl_GetString(objv[1]); if( Tcl_GetWideIntFromObj(interp, objv[2], &iLimit) ) return TCL_ERROR; pScript = objv[3]; /* Allocate a TclQuotaCallback object */ p = (TclQuotaCallback *)ckalloc(sizeof(TclQuotaCallback)); memset(p, 0, sizeof(TclQuotaCallback)); /* Invoke sqlite3_quota_set() */ rc = sqlite3_quota_set(zPattern, iLimit, tclQuotaCallback, (void *)p); if( rc!=SQLITE_OK ){ ckfree((char *)p); }else{ Tcl_IncrRefCount(pScript); p->interp = interp; p->pScript = pScript; p->pNext = pQuotaCallbackList; pQuotaCallbackList = p; } Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC); return TCL_OK; } /* ** This routine registers the custom TCL commands defined in this ** module. This should be the only procedure visible from outside ** of this module. */ int Sqlitequota_Init(Tcl_Interp *interp){ static struct { char *zName; Tcl_ObjCmdProc *xProc; } aCmd[] = { { "sqlite3_quota_initialize", test_quota_initialize }, { "sqlite3_quota_shutdown", test_quota_shutdown }, { "sqlite3_quota_set", test_quota_set }, }; int i; for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){ Tcl_CreateObjCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0); } return TCL_OK; } #endif |
Added test/quota.test.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | # 2010 September 1 # # The author disclaims copyright to this source code. In place of # a legal notice, here is a blessing: # # May you do good and not evil. # May you find forgiveness for yourself and forgive others. # May you share freely, never taking more than you give. # #*********************************************************************** # set testdir [file dirname $argv0] source $testdir/tester.tcl db close do_test quota-1.1 { sqlite3_quota_initialize nosuchvfs 1 } {SQLITE_ERROR} do_test quota-1.2 { sqlite3_quota_initialize "" 1 } {SQLITE_OK} do_test quota-1.3 { sqlite3_quota_initialize "" 1 } {SQLITE_MISUSE} do_test quota-1.4 { sqlite3_quota_shutdown } {SQLITE_OK} do_test quota-1.5 { sqlite3_quota_initialize "" 0 } {SQLITE_OK} do_test quota-1.6 { sqlite3_quota_shutdown } {SQLITE_OK} do_test quota-1.7 { sqlite3_quota_initialize "" 1 } {SQLITE_OK} do_test quota-1.8 { sqlite3_quota_shutdown } {SQLITE_OK} #------------------------------------------------------------------------- # Some simple warm-body tests with a single database file in rollback # mode: # # quota-2.1.*: Test that SQLITE_FULL is returned if the database would # exceed the configured quota. # # quota-2.2.*: Test that SQLITE_FULL is not returned and the database # grows if the callback extends the quota when the database # attempts to grow beyond the configured quota. # # quota-2.3.*: Open and close a db that is not part of any quota group. At # one point this was causing mutex refs to be leaked. # # quota-2.4.*: Try to shutdown the quota system before closing the db # file. Check that this fails and the quota system still works # afterwards. Then close the database and successfully shut # down the quota system. # sqlite3_quota_initialize "" 1 proc quota_check {filename limitvar size} { upvar $limitvar limit lappend ::quota [set limit] $size if {[info exists ::quota_request_ok]} { set limit $size } } do_test quota-2.1.1 { sqlite3_quota_set *test.db 4096 quota_check } {SQLITE_OK} do_test quota-2.1.2 { sqlite3 db test.db execsql { PRAGMA page_size=1024; PRAGMA auto_vacuum=OFF; PRAGMA journal_mode=DELETE; } set ::quota [list] execsql { CREATE TABLE t1(a, b); INSERT INTO t1 VALUES(1, randomblob(1100)); INSERT INTO t1 VALUES(2, randomblob(1100)); } set ::quota } {} do_test quota-2.1.3 { file size test.db } {4096} do_test quota-2.1.4 { catchsql { INSERT INTO t1 VALUES(3, randomblob(1100)) } } {1 {database or disk is full}} do_test quota-2.1.5 { set ::quota } {4096 5120} set ::quota_request_ok 1 set ::quota [list] do_test quota-2.2.1 { execsql { INSERT INTO t1 VALUES(3, randomblob(1100)) } } {} do_test quota-2.2.2 { set ::quota } {4096 5120} do_test quota-2.2.3 { file size test.db } {5120} unset ::quota_request_ok do_test quota-2.3.1 { sqlite3 db2 bak.db db2 close } {} do_test quota-2.4.1 { sqlite3_quota_shutdown } {SQLITE_MISUSE} set ::quota [list] do_test quota-2.4.2 { catchsql { INSERT INTO t1 VALUES(3, randomblob(1100)) } } {1 {database or disk is full}} do_test quota-2.4.3 { set ::quota } {5120 6144} do_test quota-2.4.4 { file size test.db } {5120} do_test quota-2.4.5 { db close sqlite3_quota_shutdown } {SQLITE_OK} #------------------------------------------------------------------------- # Try some tests with more than one connection to a database file. Still # in rollback mode. # proc quota_check {filename limitvar size} { upvar $limitvar limit lappend ::quota [set limit] $size if {[info exists ::quota_request_ok]} { set limit $size } } do_test quota-3.1.1 { file delete -force test.db sqlite3_quota_initialize "" 1 sqlite3_quota_set *test.db 4096 quota_check } {SQLITE_OK} do_test quota-3.1.2 { sqlite3 db test.db execsql { PRAGMA page_size = 1024; PRAGMA journal_mode = delete; PRAGMA auto_vacuum = off; CREATE TABLE t1(a PRIMARY KEY, b); INSERT INTO t1 VALUES(1, 'one'); } file size test.db } {3072} do_test quota-3.1.3 { sqlite3 db2 test.db set ::quota [list] execsql { CREATE TABLE t2(a, b) } db2 set ::quota } {} do_test quota-3.X { catch { db close } catch { db2 close } sqlite3_quota_shutdown } {SQLITE_OK} finish_test |