mirror of
https://github.com/sqlite/sqlite.git
synced 2025-08-08 14:02:16 +03:00
Merge the test_quota.c module into the trunk.
FossilOrigin-Name: 2e1a02026a0635fe05ba55a6d4d36dd7fd0ae8e9
This commit is contained in:
1
main.mk
1
main.mk
@@ -245,6 +245,7 @@ TESTSRC = \
|
|||||||
$(TOP)/src/test_onefile.c \
|
$(TOP)/src/test_onefile.c \
|
||||||
$(TOP)/src/test_osinst.c \
|
$(TOP)/src/test_osinst.c \
|
||||||
$(TOP)/src/test_pcache.c \
|
$(TOP)/src/test_pcache.c \
|
||||||
|
$(TOP)/src/test_quota.c \
|
||||||
$(TOP)/src/test_rtree.c \
|
$(TOP)/src/test_rtree.c \
|
||||||
$(TOP)/src/test_schema.c \
|
$(TOP)/src/test_schema.c \
|
||||||
$(TOP)/src/test_server.c \
|
$(TOP)/src/test_server.c \
|
||||||
|
23
manifest
23
manifest
@@ -1,8 +1,8 @@
|
|||||||
-----BEGIN PGP SIGNED MESSAGE-----
|
-----BEGIN PGP SIGNED MESSAGE-----
|
||||||
Hash: SHA1
|
Hash: SHA1
|
||||||
|
|
||||||
C Add\san\sassert()\swith\san\sevidence\smark\sto\sshow\sthat\sautomatically\s\ngenerated\srowids\sare\spositive.
|
C Merge\sthe\stest_quota.c\smodule\sinto\sthe\strunk.
|
||||||
D 2010-09-01T11:50:09
|
D 2010-09-01T15:22:56
|
||||||
F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
|
F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
|
||||||
F Makefile.in c599a15d268b1db2aeadea19df2adc3bf2eb6bee
|
F Makefile.in c599a15d268b1db2aeadea19df2adc3bf2eb6bee
|
||||||
F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
|
F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
|
||||||
@@ -99,7 +99,7 @@ F ext/rtree/tkt3363.test 142ab96eded44a3615ec79fba98c7bde7d0f96de
|
|||||||
F ext/rtree/viewrtree.tcl eea6224b3553599ae665b239bd827e182b466024
|
F ext/rtree/viewrtree.tcl eea6224b3553599ae665b239bd827e182b466024
|
||||||
F install-sh 9d4de14ab9fb0facae2f48780b874848cbf2f895 x
|
F install-sh 9d4de14ab9fb0facae2f48780b874848cbf2f895 x
|
||||||
F ltmain.sh 3ff0879076df340d2e23ae905484d8c15d5fdea8
|
F ltmain.sh 3ff0879076df340d2e23ae905484d8c15d5fdea8
|
||||||
F main.mk e5a8d2441a4949abfc73bb3fe752c65fb2f8a62c
|
F main.mk 1c6b2152dab78cc0c9f74958968c87fea232254c
|
||||||
F mkdll.sh 7d09b23c05d56532e9d44a50868eb4b12ff4f74a
|
F mkdll.sh 7d09b23c05d56532e9d44a50868eb4b12ff4f74a
|
||||||
F mkextu.sh 416f9b7089d80e5590a29692c9d9280a10dbad9f
|
F mkextu.sh 416f9b7089d80e5590a29692c9d9280a10dbad9f
|
||||||
F mkextw.sh 4123480947681d9b434a5e7b1ee08135abe409ac
|
F mkextw.sh 4123480947681d9b434a5e7b1ee08135abe409ac
|
||||||
@@ -182,7 +182,7 @@ F src/sqliteInt.h 4e31d3b58720d6251e08ba394641b9a63725eb8a
|
|||||||
F src/sqliteLimit.h a17dcd3fb775d63b64a43a55c54cb282f9726f44
|
F src/sqliteLimit.h a17dcd3fb775d63b64a43a55c54cb282f9726f44
|
||||||
F src/status.c 496913d4e8441195f6f2a75b1c95993a45b9b30b
|
F src/status.c 496913d4e8441195f6f2a75b1c95993a45b9b30b
|
||||||
F src/table.c 2cd62736f845d82200acfa1287e33feb3c15d62e
|
F src/table.c 2cd62736f845d82200acfa1287e33feb3c15d62e
|
||||||
F src/tclsqlite.c 3a6687806193a2610eb7c4c2d282493ff5c8d96c
|
F src/tclsqlite.c e91019fb6787166abca23a81b16c07fecc2ed751
|
||||||
F src/test1.c c1b74b15b55051e7004ede9233e4b2b323e8ba65
|
F src/test1.c c1b74b15b55051e7004ede9233e4b2b323e8ba65
|
||||||
F src/test2.c 80d323d11e909cf0eb1b6fbb4ac22276483bcf31
|
F src/test2.c 80d323d11e909cf0eb1b6fbb4ac22276483bcf31
|
||||||
F src/test3.c 056093cfef69ff4227a6bdb9108564dc7f45e4bc
|
F src/test3.c 056093cfef69ff4227a6bdb9108564dc7f45e4bc
|
||||||
@@ -211,7 +211,7 @@ F src/test_mutex.c ce06b59aca168cd8c520b77159a24352a7469bd3
|
|||||||
F src/test_onefile.c 40cf9e212a377a6511469384a64b01e6e34b2eec
|
F src/test_onefile.c 40cf9e212a377a6511469384a64b01e6e34b2eec
|
||||||
F src/test_osinst.c f408c6a181f2fb04c56273afd5c3e1e82f60392c
|
F src/test_osinst.c f408c6a181f2fb04c56273afd5c3e1e82f60392c
|
||||||
F src/test_pcache.c 7bf828972ac0d2403f5cfa4cd14da41f8ebe73d8
|
F src/test_pcache.c 7bf828972ac0d2403f5cfa4cd14da41f8ebe73d8
|
||||||
F src/test_quota.c b5e4ca20c1cd2e1e1e08db8d403653460c6084fe
|
F src/test_quota.c 91cacc8c0d8f55814a9ad5b90ad501c53a3e1efa
|
||||||
F src/test_rtree.c e957a603a98871dcf005c1e96ae791cfe74eb7f6
|
F src/test_rtree.c e957a603a98871dcf005c1e96ae791cfe74eb7f6
|
||||||
F src/test_schema.c 8c06ef9ddb240c7a0fcd31bc221a6a2aade58bf0
|
F src/test_schema.c 8c06ef9ddb240c7a0fcd31bc221a6a2aade58bf0
|
||||||
F src/test_server.c bbba05c144b5fc4b52ff650a4328027b3fa5fcc6
|
F src/test_server.c bbba05c144b5fc4b52ff650a4328027b3fa5fcc6
|
||||||
@@ -565,6 +565,7 @@ F test/printf.test 05970cde31b1a9f54bd75af60597be75a5c54fea
|
|||||||
F test/progress.test 5b075c3c790c7b2a61419bc199db87aaf48b8301
|
F test/progress.test 5b075c3c790c7b2a61419bc199db87aaf48b8301
|
||||||
F test/ptrchng.test ef1aa72d6cf35a2bbd0869a649b744e9d84977fc
|
F test/ptrchng.test ef1aa72d6cf35a2bbd0869a649b744e9d84977fc
|
||||||
F test/quick.test 1681febc928d686362d50057c642f77a02c62e57
|
F test/quick.test 1681febc928d686362d50057c642f77a02c62e57
|
||||||
|
F test/quota.test d406716c5e91df17ef7821facae6a9aefaa4dd91
|
||||||
F test/quote.test 215897dbe8de1a6f701265836d6601cc6ed103e6
|
F test/quote.test 215897dbe8de1a6f701265836d6601cc6ed103e6
|
||||||
F test/randexpr1.tcl 40dec52119ed3a2b8b2a773bce24b63a3a746459
|
F test/randexpr1.tcl 40dec52119ed3a2b8b2a773bce24b63a3a746459
|
||||||
F test/randexpr1.test 1084050991e9ba22c1c10edd8d84673b501cc25a
|
F test/randexpr1.test 1084050991e9ba22c1c10edd8d84673b501cc25a
|
||||||
@@ -855,14 +856,14 @@ F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff
|
|||||||
F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224
|
F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224
|
||||||
F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e
|
F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e
|
||||||
F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f
|
F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f
|
||||||
P afdc82a99eba260aed8ae2cca4bcec629f384098
|
P 740adca34e7fd309f0f761b8cc8581a9b7890793 19e95f63359589766da673aac99d19a355a92678
|
||||||
R c242196b51201de554980140d2825d22
|
R 49d7c0965e9aa38b9a7b200ccf905b34
|
||||||
U drh
|
U drh
|
||||||
Z 7bc51b0dd753d40399251c5cc09bcc01
|
Z d2e178175efa127228d59b10ca5432e5
|
||||||
-----BEGIN PGP SIGNATURE-----
|
-----BEGIN PGP SIGNATURE-----
|
||||||
Version: GnuPG v1.4.6 (GNU/Linux)
|
Version: GnuPG v1.4.6 (GNU/Linux)
|
||||||
|
|
||||||
iD8DBQFMfj30oxKgR168RlERAuhNAJwOQ+vW/5/Bk0PpVHXkp7+wQ1tM2QCfSPFK
|
iD8DBQFMfm/SoxKgR168RlERAhKeAJ9GTWSgyTP43+AwCZCijdeOHh+pSACdFsL8
|
||||||
xwj8IPDm1OlzDiWxR1msH/8=
|
/nKn+kepTG8lPD+3DrG/4No=
|
||||||
=/euV
|
=GTcD
|
||||||
-----END PGP SIGNATURE-----
|
-----END PGP SIGNATURE-----
|
||||||
|
@@ -1 +1 @@
|
|||||||
740adca34e7fd309f0f761b8cc8581a9b7890793
|
2e1a02026a0635fe05ba55a6d4d36dd7fd0ae8e9
|
@@ -3577,6 +3577,7 @@ static void init_all(Tcl_Interp *interp){
|
|||||||
extern int Sqlitetestvfs_Init(Tcl_Interp *);
|
extern int Sqlitetestvfs_Init(Tcl_Interp *);
|
||||||
extern int SqlitetestStat_Init(Tcl_Interp*);
|
extern int SqlitetestStat_Init(Tcl_Interp*);
|
||||||
extern int Sqlitetestrtree_Init(Tcl_Interp*);
|
extern int Sqlitetestrtree_Init(Tcl_Interp*);
|
||||||
|
extern int Sqlitequota_Init(Tcl_Interp*);
|
||||||
|
|
||||||
Sqliteconfig_Init(interp);
|
Sqliteconfig_Init(interp);
|
||||||
Sqlitetest1_Init(interp);
|
Sqlitetest1_Init(interp);
|
||||||
@@ -3606,6 +3607,7 @@ static void init_all(Tcl_Interp *interp){
|
|||||||
Sqlitetestvfs_Init(interp);
|
Sqlitetestvfs_Init(interp);
|
||||||
SqlitetestStat_Init(interp);
|
SqlitetestStat_Init(interp);
|
||||||
Sqlitetestrtree_Init(interp);
|
Sqlitetestrtree_Init(interp);
|
||||||
|
Sqlitequota_Init(interp);
|
||||||
|
|
||||||
Tcl_CreateObjCommand(interp,"load_testfixture_extensions",init_all_cmd,0,0);
|
Tcl_CreateObjCommand(interp,"load_testfixture_extensions",init_all_cmd,0,0);
|
||||||
|
|
||||||
|
623
src/test_quota.c
623
src/test_quota.c
@@ -17,8 +17,8 @@
|
|||||||
** database files are in a "quota group" that is defined by a GLOB
|
** 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
|
** 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
|
** 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
|
** of all files in the quota group is greater than or equal to the limit,
|
||||||
** write requests that attempt to enlarge a file fail with SQLITE_FULL.
|
** then write requests that attempt to enlarge a file fail with SQLITE_FULL.
|
||||||
**
|
**
|
||||||
** However, before returning SQLITE_FULL, the write requests invoke
|
** However, before returning SQLITE_FULL, the write requests invoke
|
||||||
** a callback function that is configurable for each quota group.
|
** a callback function that is configurable for each quota group.
|
||||||
@@ -28,22 +28,30 @@
|
|||||||
** continues as if nothing had happened.
|
** continues as if nothing had happened.
|
||||||
*/
|
*/
|
||||||
#include "sqlite3.h"
|
#include "sqlite3.h"
|
||||||
|
#include <string.h>
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
/************************ Object Definitions ******************************/
|
/************************ Object Definitions ******************************/
|
||||||
|
|
||||||
/*
|
/* Forward declaration of all object types */
|
||||||
** 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;
|
typedef struct quotaGroup quotaGroup;
|
||||||
|
typedef struct quotaConn quotaConn;
|
||||||
|
typedef struct quotaFile quotaFile;
|
||||||
|
|
||||||
|
/*
|
||||||
|
** A "quota group" is a collection of files whose collective size we want
|
||||||
|
** to limit. Each quota group is defined by a GLOB pattern.
|
||||||
|
**
|
||||||
|
** There is an instance of the following object for each defined quota
|
||||||
|
** group. This object records the GLOB pattern that defines which files
|
||||||
|
** belong to the quota group. The object also remembers the size limit
|
||||||
|
** for the group (the quota) and the callback to be invoked when the
|
||||||
|
** sum of the sizes of the files within the group goes over the limit.
|
||||||
|
**
|
||||||
|
** A quota group must be established (using sqlite3_quota_set(...))
|
||||||
|
** prior to opening any of the database connections that access files
|
||||||
|
** within the quota group.
|
||||||
|
*/
|
||||||
struct quotaGroup {
|
struct quotaGroup {
|
||||||
const char *zPattern; /* Filename pattern to be quotaed */
|
const char *zPattern; /* Filename pattern to be quotaed */
|
||||||
sqlite3_int64 iLimit; /* Upper bound on total file size */
|
sqlite3_int64 iLimit; /* Upper bound on total file size */
|
||||||
@@ -55,20 +63,36 @@ struct quotaGroup {
|
|||||||
void *pArg /* Client data */
|
void *pArg /* Client data */
|
||||||
);
|
);
|
||||||
void *pArg; /* Third argument to the xCallback() */
|
void *pArg; /* Third argument to the xCallback() */
|
||||||
int nRef; /* Number of files in the group references. */
|
void (*xDestroy)(void*); /* Optional destructor for pArg */
|
||||||
quotaGroup *pNext, **ppPrev; /* Doubly linked list of all quota objects */
|
quotaGroup *pNext, **ppPrev; /* Doubly linked list of all quota objects */
|
||||||
|
quotaFile *pFiles; /* Files within this group */
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** An instance of the following object represents each file that
|
** An instance of this structure represents a single file that is part
|
||||||
** participates in quota tracking. The sqlite3_file object for the
|
** of a quota group. A single file can be opened multiple times. In
|
||||||
** underlying VFS is appended to this structure.
|
** order keep multiple openings of the same file from causing the size
|
||||||
|
** of the file to count against the quota multiple times, each file
|
||||||
|
** has a unique instance of this object and multiple open connections
|
||||||
|
** to the same file each point to a single instance of this object.
|
||||||
*/
|
*/
|
||||||
typedef struct quotaFile quotaFile;
|
|
||||||
struct quotaFile {
|
struct quotaFile {
|
||||||
sqlite3_file base; /* Base class - must be first */
|
char *zFilename; /* Name of this file */
|
||||||
const char *zFilename; /* Name of this file */
|
quotaGroup *pGroup; /* Quota group to which this file belongs */
|
||||||
quotaGroup *pGroup; /* Upper bound on file size */
|
sqlite3_int64 iSize; /* Current size of this file */
|
||||||
|
int nRef; /* Number of times this file is open */
|
||||||
|
quotaFile *pNext, **ppPrev; /* Linked list of files in the same group */
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
** An instance of the following object represents each open connection
|
||||||
|
** to a file that participates in quota tracking. This object is a
|
||||||
|
** subclass of sqlite3_file. The sqlite3_file object for the underlying
|
||||||
|
** VFS is appended to this structure.
|
||||||
|
*/
|
||||||
|
struct quotaConn {
|
||||||
|
sqlite3_file base; /* Base class - must be first */
|
||||||
|
quotaFile *pFile; /* The underlying file */
|
||||||
/* The underlying VFS sqlite3_file is appended to this object */
|
/* The underlying VFS sqlite3_file is appended to this object */
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -78,7 +102,7 @@ struct quotaFile {
|
|||||||
** gQuota structure.
|
** gQuota structure.
|
||||||
*/
|
*/
|
||||||
static struct {
|
static struct {
|
||||||
/* The pOrigVfs is a pointer to the real underlying VFS implementation.
|
/* The pOrigVfs is the real, original underlying VFS implementation.
|
||||||
** Most operations pass-through to the real VFS. This value is read-only
|
** 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
|
** during operation. It is only modified at start-time and thus does not
|
||||||
** require a mutex.
|
** require a mutex.
|
||||||
@@ -130,11 +154,12 @@ static void quotaLeave(void){ sqlite3_mutex_leave(gQuota.pMutex); }
|
|||||||
/* If the reference count and threshold for a quotaGroup are both
|
/* If the reference count and threshold for a quotaGroup are both
|
||||||
** zero, then destroy the quotaGroup.
|
** zero, then destroy the quotaGroup.
|
||||||
*/
|
*/
|
||||||
static void quotaGroupDeref(quotaFile *p){
|
static void quotaGroupDeref(quotaGroup *pGroup){
|
||||||
if( p->nRef==0 && p->iLimit==0 ){
|
if( pGroup->pFiles==0 && pGroup->iLimit==0 ){
|
||||||
if( p->pNext ) p->pNext->ppPrev = p->ppPrev;
|
if( pGroup->pNext ) pGroup->pNext->ppPrev = pGroup->ppPrev;
|
||||||
if( p->ppPrev ) *p->ppPrev = p->pNext;
|
if( pGroup->ppPrev ) *pGroup->ppPrev = pGroup->pNext;
|
||||||
sqlite3_free(p);
|
if( pGroup->xDestroy ) pGroup->xDestroy(pGroup->pArg);
|
||||||
|
sqlite3_free(pGroup);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -153,7 +178,7 @@ static void quotaGroupDeref(quotaFile *p){
|
|||||||
** [^...] Matches one character not in the enclosed list.
|
** [^...] Matches one character not in the enclosed list.
|
||||||
**
|
**
|
||||||
*/
|
*/
|
||||||
static int strglob(const char *zGlob, const char *z){
|
static int quotaStrglob(const char *zGlob, const char *z){
|
||||||
int c, c2;
|
int c, c2;
|
||||||
int invert;
|
int invert;
|
||||||
int seen;
|
int seen;
|
||||||
@@ -166,7 +191,7 @@ static int strglob(const char *zGlob, const char *z){
|
|||||||
if( c==0 ){
|
if( c==0 ){
|
||||||
return 1;
|
return 1;
|
||||||
}else if( c=='[' ){
|
}else if( c=='[' ){
|
||||||
while( *z && th3strglob(zGlob-1,z)==0 ){
|
while( *z && quotaStrglob(zGlob-1,z)==0 ){
|
||||||
z++;
|
z++;
|
||||||
}
|
}
|
||||||
return (*z)!=0;
|
return (*z)!=0;
|
||||||
@@ -176,7 +201,7 @@ static int strglob(const char *zGlob, const char *z){
|
|||||||
c2 = *(z++);
|
c2 = *(z++);
|
||||||
if( c2==0 ) return 0;
|
if( c2==0 ) return 0;
|
||||||
}
|
}
|
||||||
if( th3strglob(zGlob,z) ) return 1;
|
if( quotaStrglob(zGlob,z) ) return 1;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}else if( c=='?' ){
|
}else if( c=='?' ){
|
||||||
@@ -219,19 +244,21 @@ static int strglob(const char *zGlob, const char *z){
|
|||||||
|
|
||||||
|
|
||||||
/* Find a quotaGroup given the filename.
|
/* Find a quotaGroup given the filename.
|
||||||
** Return a pointer to the quotaFile object. return NULL if not found.
|
**
|
||||||
|
** Return a pointer to the quotaGroup object. Return NULL if not found.
|
||||||
*/
|
*/
|
||||||
static quotaGroup *quotaGroupFind(const char *zFilename){
|
static quotaGroup *quotaGroupFind(const char *zFilename){
|
||||||
quotaGroup *p;
|
quotaGroup *p;
|
||||||
for(p=pGroup; p && strglob(p->zPattern, zFilename)==0; p=p->pNext){}
|
for(p=gQuota.pGroup; p && quotaStrglob(p->zPattern, zFilename)==0;
|
||||||
|
p=p->pNext){}
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Translate an sqlite3_file* that is really a quotaFile* into
|
/* Translate an sqlite3_file* that is really a quotaConn* into
|
||||||
** an sqlite3_file* for the underlying original VFS.
|
** the sqlite3_file* for the underlying original VFS.
|
||||||
*/
|
*/
|
||||||
static sqlite3_file *quotaSubFile(sqlite3_file *pFile){
|
static sqlite3_file *quotaSubOpen(sqlite3_file *pConn){
|
||||||
quotaFile *p = (quotaFile*)pFile;
|
quotaConn *p = (quotaConn*)pConn;
|
||||||
return (sqlite3_file*)&p[1];
|
return (sqlite3_file*)&p[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -240,75 +267,98 @@ static sqlite3_file *quotaSubFile(sqlite3_file *pFile){
|
|||||||
** This is the xOpen method used for the "quota" VFS.
|
** This is the xOpen method used for the "quota" VFS.
|
||||||
**
|
**
|
||||||
** Most of the work is done by the underlying original VFS. This method
|
** 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
|
** simply links the new file into the appropriate quota group if it is a
|
||||||
** needs to be tracked.
|
** file that needs to be tracked.
|
||||||
*/
|
*/
|
||||||
static int quotaOpen(
|
static int quotaOpen(
|
||||||
sqlite3_vfs *pVfs,
|
sqlite3_vfs *pVfs, /* The quota VFS */
|
||||||
const char *zName,
|
const char *zName, /* Name of file to be opened */
|
||||||
sqlite3_file *pFile,
|
sqlite3_file *pConn, /* Fill in this file descriptor */
|
||||||
int flags,
|
int flags, /* Flags to control the opening */
|
||||||
int *pOutFlags
|
int *pOutFlags /* Flags showing results of opening */
|
||||||
){
|
){
|
||||||
int rc;
|
int rc; /* Result code */
|
||||||
quotaFile *pQuotaFile;
|
quotaConn *pQuotaOpen; /* The new quota file descriptor */
|
||||||
sqlite3_file *pSubFile;
|
quotaFile *pFile; /* Corresponding quotaFile obj */
|
||||||
|
quotaGroup *pGroup; /* The group file belongs to */
|
||||||
|
sqlite3_file *pSubOpen; /* Real file descriptor */
|
||||||
|
sqlite3_vfs *pOrigVfs = gQuota.pOrigVfs; /* Real VFS */
|
||||||
|
|
||||||
/* If the file is not a main database file or a WAL, then use the
|
/* If the file is not a main database file or a WAL, then use the
|
||||||
** normal xOpen method.
|
** normal xOpen method.
|
||||||
*/
|
*/
|
||||||
if( (flags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_MAIN_WAL))==0 ){
|
if( (flags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_WAL))==0 ){
|
||||||
return gQuota.pOrigVfs->xOpen(gQuota.pOrigVfs, zName,
|
return pOrigVfs->xOpen(pOrigVfs, zName, pConn, flags, pOutFlags);
|
||||||
pFile, flags, pOutFlags);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* If the name of the file does not match any quota group, then
|
/* If the name of the file does not match any quota group, then
|
||||||
** use the normal xOpen method.
|
** use the normal xOpen method.
|
||||||
*/
|
*/
|
||||||
quotaEnter();
|
quotaEnter();
|
||||||
pGroup = quotaFindGroup(zName);
|
pGroup = quotaGroupFind(zName);
|
||||||
if( pGroup==0 ){
|
if( pGroup==0 ){
|
||||||
return gQuota.pOrigVfs->xOpen(gQuota.pOrigVfs, zName,
|
rc = pOrigVfs->xOpen(pOrigVfs, zName, pConn, flags, pOutFlags);
|
||||||
pFile, flags, pOutFlags);
|
}else{
|
||||||
}
|
/* If we get to this point, it means the file needs to be quota tracked.
|
||||||
|
*/
|
||||||
/* If we get to this point, it means the file needs
|
pQuotaOpen = (quotaConn*)pConn;
|
||||||
** to be quota tracked.
|
pSubOpen = quotaSubOpen(pConn);
|
||||||
*/
|
rc = pOrigVfs->xOpen(pOrigVfs, zName, pSubOpen, flags, pOutFlags);
|
||||||
pQuotaFile = (quotaFile*)pFile;
|
if( rc==SQLITE_OK ){
|
||||||
pSubFile = quotaSubFile(pFile);
|
for(pFile=pGroup->pFiles; pFile && strcmp(pFile->zFilename, zName);
|
||||||
rc = gQuota.pOrigVfs->xOpen(gQuota.pOrigVfs, zName,
|
pFile=pFile->pNext){}
|
||||||
pSubFile, flags, pOutFlags);
|
if( pFile==0 ){
|
||||||
if( rc==SQLITE_OK ){
|
int nName = strlen(zName);
|
||||||
pQuotaFile->iSize = 0;
|
pFile = sqlite3_malloc( sizeof(*pFile) + nName + 1 );
|
||||||
pQuotaFile->pGroup = pGroup;
|
if( pFile==0 ){
|
||||||
pGroup->nRef++;
|
quotaLeave();
|
||||||
quotaLeave();
|
pSubOpen->pMethods->xClose(pSubOpen);
|
||||||
pQuotaFile->zFilename = zName;
|
return SQLITE_NOMEM;
|
||||||
if( pSubFile->pMethods.iVersion==1 ){
|
}
|
||||||
pQuotaFile->base.pMethods = &gQuota.sIoMethodsV1;
|
memset(pFile, 0, sizeof(*pFile));
|
||||||
}else{
|
pFile->zFilename = (char*)&pFile[1];
|
||||||
pQuotaFile->base.pMethods = &gQuota.sIoMethodsV2;
|
memcpy(pFile->zFilename, zName, nName+1);
|
||||||
|
pFile->pNext = pGroup->pFiles;
|
||||||
|
if( pGroup->pFiles ) pGroup->pFiles->ppPrev = &pFile->pNext;
|
||||||
|
pFile->ppPrev = &pGroup->pFiles;
|
||||||
|
pGroup->pFiles = pFile;
|
||||||
|
pFile->pGroup = pGroup;
|
||||||
|
}
|
||||||
|
pFile->nRef++;
|
||||||
|
pQuotaOpen->pFile = pFile;
|
||||||
|
if( pSubOpen->pMethods->iVersion==1 ){
|
||||||
|
pQuotaOpen->base.pMethods = &gQuota.sIoMethodsV1;
|
||||||
|
}else{
|
||||||
|
pQuotaOpen->base.pMethods = &gQuota.sIoMethodsV2;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
quotaLeave();
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
/************************ I/O Method Wrappers *******************************/
|
/************************ I/O Method Wrappers *******************************/
|
||||||
|
|
||||||
/* xClose requests get passed through to the original VFS. But we
|
/* xClose requests get passed through to the original VFS. But we
|
||||||
** also have to unlink the quotaFile from the quotaGroup.
|
** also have to unlink the quotaConn from the quotaFile and quotaGroup.
|
||||||
|
** The quotaFile and/or quotaGroup are freed if they are no longer in use.
|
||||||
*/
|
*/
|
||||||
static int quotaClose(sqlite3_file *pFile){
|
static int quotaClose(sqlite3_file *pConn){
|
||||||
quotaFile *p = (quotaFile*)pFile;
|
quotaConn *p = (quotaConn*)pConn;
|
||||||
quotaGroup *pGroup = p->pGroup;
|
quotaFile *pFile = p->pFile;
|
||||||
sqlite3_file *pSubFile = quotaSubFile(pFile);
|
sqlite3_file *pSubOpen = quotaSubOpen(pConn);
|
||||||
int rc;
|
int rc;
|
||||||
rc = pSubFile->pMethods.xClose(pSubFile);
|
rc = pSubOpen->pMethods->xClose(pSubOpen);
|
||||||
quotaEnter();
|
quotaEnter();
|
||||||
pGroup->nRef--;
|
pFile->nRef--;
|
||||||
pGroup->iSize -= p->iSize;
|
if( pFile->nRef==0 ){
|
||||||
quotaGroupDeref(pGroup);
|
quotaGroup *pGroup = pFile->pGroup;
|
||||||
|
pGroup->iSize -= pFile->iSize;
|
||||||
|
if( pFile->pNext ) pFile->pNext->ppPrev = pFile->ppPrev;
|
||||||
|
*pFile->ppPrev = pFile->pNext;
|
||||||
|
quotaGroupDeref(pGroup);
|
||||||
|
sqlite3_free(pFile);
|
||||||
|
}
|
||||||
quotaLeave();
|
quotaLeave();
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
@@ -317,13 +367,13 @@ static int quotaClose(sqlite3_file *pFile){
|
|||||||
** further processing.
|
** further processing.
|
||||||
*/
|
*/
|
||||||
static int quotaRead(
|
static int quotaRead(
|
||||||
sqlite3_file *pFile,
|
sqlite3_file *pConn,
|
||||||
void *pBuf,
|
void *pBuf,
|
||||||
int iAmt,
|
int iAmt,
|
||||||
sqlite3_int64 iOfst
|
sqlite3_int64 iOfst
|
||||||
){
|
){
|
||||||
sqlite3_file *pSubFile = quotaSubFile(pFile);
|
sqlite3_file *pSubOpen = quotaSubOpen(pConn);
|
||||||
return pSubFile->pMethods.xRead(pSubFile, pBuf, iAmt, iOfst);
|
return pSubOpen->pMethods->xRead(pSubOpen, pBuf, iAmt, iOfst);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Check xWrite requests to see if they expand the file. If they do,
|
/* Check xWrite requests to see if they expand the file. If they do,
|
||||||
@@ -331,49 +381,53 @@ static int quotaRead(
|
|||||||
** original VFS.
|
** original VFS.
|
||||||
*/
|
*/
|
||||||
static int quotaWrite(
|
static int quotaWrite(
|
||||||
sqlite3_file *pFile,
|
sqlite3_file *pConn,
|
||||||
void *pBuf,
|
const void *pBuf,
|
||||||
int iAmt,
|
int iAmt,
|
||||||
sqlite3_int64 iOfst
|
sqlite3_int64 iOfst
|
||||||
){
|
){
|
||||||
quotaFile *p = (quotaFile*)pFile;
|
quotaConn *p = (quotaConn*)pConn;
|
||||||
sqlite3_file *pSubFile = quotaSubFile(pFile);
|
sqlite3_file *pSubOpen = quotaSubOpen(pConn);
|
||||||
sqlite3_int64 iEnd = iOfst+iAmt;
|
sqlite3_int64 iEnd = iOfst+iAmt;
|
||||||
quotaGroup *pGroup;
|
quotaGroup *pGroup;
|
||||||
|
quotaFile *pFile = p->pFile;
|
||||||
sqlite3_int64 szNew;
|
sqlite3_int64 szNew;
|
||||||
|
|
||||||
if( p->iSize<iEnd ){
|
if( pFile->iSize<iEnd ){
|
||||||
pGroup = p->pGroup;
|
pGroup = pFile->pGroup;
|
||||||
quotaEnter();
|
quotaEnter();
|
||||||
szNew = pGroup->iSize - p->iSize + iEnd;
|
szNew = pGroup->iSize - pFile->iSize + iEnd;
|
||||||
if( szNew >= pGroup->iLimit && pGroup->iLimit > 0 ){
|
if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){
|
||||||
if( pGroup->xCallback ){
|
if( pGroup->xCallback ){
|
||||||
pGroup->xCallback(p->zFilename, &pGroup->iLimit, szNew,
|
pGroup->xCallback(pFile->zFilename, &pGroup->iLimit, szNew,
|
||||||
pGroup->pArg);
|
pGroup->pArg);
|
||||||
}
|
}
|
||||||
if( szNew >= pGroup->iLimit && pGroup->iLimit > 0 ){
|
if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){
|
||||||
quotaLeave();
|
quotaLeave();
|
||||||
return SQLITE_FULL;
|
return SQLITE_FULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pGroup->iSize = szNew;
|
pGroup->iSize = szNew;
|
||||||
|
pFile->iSize = iEnd;
|
||||||
quotaLeave();
|
quotaLeave();
|
||||||
}
|
}
|
||||||
return pSubFile->pMethods.xWrite(pSubFile, pBuf, iAmt, iOfst);
|
return pSubOpen->pMethods->xWrite(pSubOpen, pBuf, iAmt, iOfst);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Pass xTruncate requests thru to the original VFS. If the
|
/* Pass xTruncate requests thru to the original VFS. If the
|
||||||
** success, update the file size.
|
** success, update the file size.
|
||||||
*/
|
*/
|
||||||
static int quotaTruncate(sqlite3_file *pFile, sqlite3_int64 size){
|
static int quotaTruncate(sqlite3_file *pConn, sqlite3_int64 size){
|
||||||
quotaFile *p = (quotaFile*)pFile);
|
quotaConn *p = (quotaConn*)pConn;
|
||||||
sqlite3_file *pSubFile = quotaSubFile(pFile);
|
sqlite3_file *pSubOpen = quotaSubOpen(pConn);
|
||||||
int rc = pSubFile->pMethods.xTruncate(pSubFile, size);
|
int rc = pSubOpen->pMethods->xTruncate(pSubOpen, size);
|
||||||
quotaGroup *pGroup = p->pGroup;
|
quotaFile *pFile = p->pFile;
|
||||||
|
quotaGroup *pGroup;
|
||||||
if( rc==SQLITE_OK ){
|
if( rc==SQLITE_OK ){
|
||||||
quotaEnter();
|
quotaEnter();
|
||||||
pGroup->iSize -= p->iSize;
|
pGroup = pFile->pGroup;
|
||||||
p->iSize = size;
|
pGroup->iSize -= pFile->iSize;
|
||||||
|
pFile->iSize = size;
|
||||||
pGroup->iSize += size;
|
pGroup->iSize += size;
|
||||||
quotaLeave();
|
quotaLeave();
|
||||||
}
|
}
|
||||||
@@ -382,27 +436,28 @@ static int quotaTruncate(sqlite3_file *pFile, sqlite3_int64 size){
|
|||||||
|
|
||||||
/* Pass xSync requests through to the original VFS without change
|
/* Pass xSync requests through to the original VFS without change
|
||||||
*/
|
*/
|
||||||
static int quotaSync(sqlite3_file *pFile, int flags){
|
static int quotaSync(sqlite3_file *pConn, int flags){
|
||||||
sqlite3_file *pSubFile = quotaSubFile(pFile);
|
sqlite3_file *pSubOpen = quotaSubOpen(pConn);
|
||||||
return pSubFile->pMethods.xSync(pSubFile, flags);
|
return pSubOpen->pMethods->xSync(pSubOpen, flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Pass xFileSize requests through to the original VFS but then
|
/* Pass xFileSize requests through to the original VFS but then
|
||||||
** update the quotaGroup with the new size before returning.
|
** update the quotaGroup with the new size before returning.
|
||||||
*/
|
*/
|
||||||
static int quotaFileSize(sqlite3_file *pFile, sqlite3_int64 *pSize){
|
static int quotaFileSize(sqlite3_file *pConn, sqlite3_int64 *pSize){
|
||||||
quotaFile *p = (quotaFile*)pFile;
|
quotaConn *p = (quotaConn*)pConn;
|
||||||
sqlite3_file *pSubFile = quotaSubFile(pFile);
|
sqlite3_file *pSubOpen = quotaSubOpen(pConn);
|
||||||
|
quotaFile *pFile = p->pFile;
|
||||||
quotaGroup *pGroup;
|
quotaGroup *pGroup;
|
||||||
sqlite3_int64 sz;
|
sqlite3_int64 sz;
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
rc = pSubFile->pMethods.xFileSize(pSubFile, &sz);
|
rc = pSubOpen->pMethods->xFileSize(pSubOpen, &sz);
|
||||||
if( rc==SQLITE_OK ){
|
if( rc==SQLITE_OK ){
|
||||||
pGroup = p->pGroup;
|
|
||||||
quotaEnter();
|
quotaEnter();
|
||||||
pGroup->iSize -= p->iSize;
|
pGroup = pFile->pGroup;
|
||||||
p->iSize = sz;
|
pGroup->iSize -= pFile->iSize;
|
||||||
|
pFile->iSize = sz;
|
||||||
pGroup->iSize += sz;
|
pGroup->iSize += sz;
|
||||||
quotaLeave();
|
quotaLeave();
|
||||||
*pSize = sz;
|
*pSize = sz;
|
||||||
@@ -412,83 +467,83 @@ static int quotaFileSize(sqlite3_file *pFile, sqlite3_int64 *pSize){
|
|||||||
|
|
||||||
/* Pass xLock requests through to the original VFS unchanged.
|
/* Pass xLock requests through to the original VFS unchanged.
|
||||||
*/
|
*/
|
||||||
static int quotaLock(sqlite3_file *pFile, int lock){
|
static int quotaLock(sqlite3_file *pConn, int lock){
|
||||||
sqlite3_file *pSubFile = quotaSubFile(pFile);
|
sqlite3_file *pSubOpen = quotaSubOpen(pConn);
|
||||||
return pSubFile->pMethods.xLock(pSubFile, lock);
|
return pSubOpen->pMethods->xLock(pSubOpen, lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Pass xUnlock requests through to the original VFS unchanged.
|
/* Pass xUnlock requests through to the original VFS unchanged.
|
||||||
*/
|
*/
|
||||||
static int quotaUnlock(sqlite3_file *pFile, int lock){
|
static int quotaUnlock(sqlite3_file *pConn, int lock){
|
||||||
sqlite3_file *pSubFile = quotaSubFile(pFile);
|
sqlite3_file *pSubOpen = quotaSubOpen(pConn);
|
||||||
return pSubFile->pMethods.xUnlock(pSubFile, lock);
|
return pSubOpen->pMethods->xUnlock(pSubOpen, lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Pass xCheckReservedLock requests through to the original VFS unchanged.
|
/* Pass xCheckReservedLock requests through to the original VFS unchanged.
|
||||||
*/
|
*/
|
||||||
static int quotaCheckReservedLock(sqlite3_file *pFile, int *pResOut){
|
static int quotaCheckReservedLock(sqlite3_file *pConn, int *pResOut){
|
||||||
sqlite3_file *pSubFile = quotaSubFile(pFile);
|
sqlite3_file *pSubOpen = quotaSubOpen(pConn);
|
||||||
return pSubFile->pMethods.xCheckReservedLock(pSubFile, pResOut);
|
return pSubOpen->pMethods->xCheckReservedLock(pSubOpen, pResOut);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Pass xFileControl requests through to the original VFS unchanged.
|
/* Pass xFileControl requests through to the original VFS unchanged.
|
||||||
*/
|
*/
|
||||||
static int quotaFileControl(sqlite3_file *pFile, int op, void *pArg){
|
static int quotaFileControl(sqlite3_file *pConn, int op, void *pArg){
|
||||||
sqlite3_file *pSubFile = quotaSubFile(pFile);
|
sqlite3_file *pSubOpen = quotaSubOpen(pConn);
|
||||||
return pSubFile->pMethods.xFileControl(pSubFile, op, pArg);
|
return pSubOpen->pMethods->xFileControl(pSubOpen, op, pArg);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Pass xSectorSize requests through to the original VFS unchanged.
|
/* Pass xSectorSize requests through to the original VFS unchanged.
|
||||||
*/
|
*/
|
||||||
static int quotaSectorSize(sqlite3_file *pFile){
|
static int quotaSectorSize(sqlite3_file *pConn){
|
||||||
sqlite3_file *pSubFile = quotaSubFile(pFile);
|
sqlite3_file *pSubOpen = quotaSubOpen(pConn);
|
||||||
return pSubFile->pMethods.xSectorSize(pSubFile);
|
return pSubOpen->pMethods->xSectorSize(pSubOpen);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Pass xDeviceCharacteristics requests through to the original VFS unchanged.
|
/* Pass xDeviceCharacteristics requests through to the original VFS unchanged.
|
||||||
*/
|
*/
|
||||||
static int quotaDeviceCharacteristics(sqlite3_file *pFile){
|
static int quotaDeviceCharacteristics(sqlite3_file *pConn){
|
||||||
sqlite3_file *pSubFile = quotaSubFile(pFile);
|
sqlite3_file *pSubOpen = quotaSubOpen(pConn);
|
||||||
return pSubFile->pMethods.xDeviceCharacteristics(pSubFile);
|
return pSubOpen->pMethods->xDeviceCharacteristics(pSubOpen);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Pass xShmMap requests through to the original VFS unchanged.
|
/* Pass xShmMap requests through to the original VFS unchanged.
|
||||||
*/
|
*/
|
||||||
static int quotaShmMap(
|
static int quotaShmMap(
|
||||||
sqlite3_file *pFile, /* Handle open on database file */
|
sqlite3_file *pConn, /* Handle open on database file */
|
||||||
int iRegion, /* Region to retrieve */
|
int iRegion, /* Region to retrieve */
|
||||||
int szRegion, /* Size of regions */
|
int szRegion, /* Size of regions */
|
||||||
int bExtend, /* True to extend file if necessary */
|
int bExtend, /* True to extend file if necessary */
|
||||||
void volatile **pp /* OUT: Mapped memory */
|
void volatile **pp /* OUT: Mapped memory */
|
||||||
){
|
){
|
||||||
sqlite3_file *pSubFile = quotaSubFile(pFile);
|
sqlite3_file *pSubOpen = quotaSubOpen(pConn);
|
||||||
return pSubFile->pMethods.xShmMap(pSubFile, iRegion, szRegion, bExtend, pp);
|
return pSubOpen->pMethods->xShmMap(pSubOpen, iRegion, szRegion, bExtend, pp);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Pass xShmLock requests through to the original VFS unchanged.
|
/* Pass xShmLock requests through to the original VFS unchanged.
|
||||||
*/
|
*/
|
||||||
static int quotaShmLock(
|
static int quotaShmLock(
|
||||||
sqlite3_file *pFile, /* Database file holding the shared memory */
|
sqlite3_file *pConn, /* Database file holding the shared memory */
|
||||||
int ofst, /* First lock to acquire or release */
|
int ofst, /* First lock to acquire or release */
|
||||||
int n, /* Number of locks to acquire or release */
|
int n, /* Number of locks to acquire or release */
|
||||||
int flags /* What to do with the lock */
|
int flags /* What to do with the lock */
|
||||||
){
|
){
|
||||||
sqlite3_file *pSubFile = quotaSubFile(pFile);
|
sqlite3_file *pSubOpen = quotaSubOpen(pConn);
|
||||||
return pSubFile->pMethods.xShmLock(pSubFile, ofst, n, flags);
|
return pSubOpen->pMethods->xShmLock(pSubOpen, ofst, n, flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Pass xShmBarrier requests through to the original VFS unchanged.
|
/* Pass xShmBarrier requests through to the original VFS unchanged.
|
||||||
*/
|
*/
|
||||||
static int quotaShmBarrier(sqlite3_file *pFile){
|
static void quotaShmBarrier(sqlite3_file *pConn){
|
||||||
sqlite3_file *pSubFile = quotaSubFile(pFile);
|
sqlite3_file *pSubOpen = quotaSubOpen(pConn);
|
||||||
return pSubFile->pMethods.xShmBarrier(pSubFile);
|
pSubOpen->pMethods->xShmBarrier(pSubOpen);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Pass xShmUnmap requests through to the original VFS unchanged.
|
/* Pass xShmUnmap requests through to the original VFS unchanged.
|
||||||
*/
|
*/
|
||||||
static int quotaShmUnmap(sqlite3_file *pFile, int deleteFlag){
|
static int quotaShmUnmap(sqlite3_file *pConn, int deleteFlag){
|
||||||
sqlite3_file *pSubFile = quotaSubFile(pFile);
|
sqlite3_file *pSubOpen = quotaSubOpen(pConn);
|
||||||
return pSubFile->pMethods.xShmUnmap(pSubFile, deleteFlag);
|
return pSubOpen->pMethods->xShmUnmap(pSubOpen, deleteFlag);
|
||||||
}
|
}
|
||||||
|
|
||||||
/************************** Public Interfaces *****************************/
|
/************************** Public Interfaces *****************************/
|
||||||
@@ -505,14 +560,19 @@ static int quotaShmUnmap(sqlite3_file *pFile, int deleteFlag){
|
|||||||
*/
|
*/
|
||||||
int sqlite3_quota_initialize(const char *zOrigVfsName, int makeDefault){
|
int sqlite3_quota_initialize(const char *zOrigVfsName, int makeDefault){
|
||||||
sqlite3_vfs *pOrigVfs;
|
sqlite3_vfs *pOrigVfs;
|
||||||
if( gQuota.isInitialize ) return SQLITE_MISUSE;
|
if( gQuota.isInitialized ) return SQLITE_MISUSE;
|
||||||
gQuota.isInitialized = 1;
|
|
||||||
pOrigVfs = sqlite3_vfs_find(zOrigVfsName);
|
pOrigVfs = sqlite3_vfs_find(zOrigVfsName);
|
||||||
if( pOrigVfs==0 ) return SQLITE_ERROR;
|
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.pOrigVfs = pOrigVfs;
|
||||||
gQuota.sThisVfs = *pOrigVfs;
|
gQuota.sThisVfs = *pOrigVfs;
|
||||||
gQuota.sThisVfs.xOpen = quotaOpen;
|
gQuota.sThisVfs.xOpen = quotaOpen;
|
||||||
gQuota.sThisVfs.szOsFile += sizeof(quotaFile);
|
gQuota.sThisVfs.szOsFile += sizeof(quotaConn);
|
||||||
gQuota.sThisVfs.zName = "quota";
|
gQuota.sThisVfs.zName = "quota";
|
||||||
gQuota.sIoMethodsV1.iVersion = 1;
|
gQuota.sIoMethodsV1.iVersion = 1;
|
||||||
gQuota.sIoMethodsV1.xClose = quotaClose;
|
gQuota.sIoMethodsV1.xClose = quotaClose;
|
||||||
@@ -525,7 +585,7 @@ int sqlite3_quota_initialize(const char *zOrigVfsName, int makeDefault){
|
|||||||
gQuota.sIoMethodsV1.xUnlock = quotaUnlock;
|
gQuota.sIoMethodsV1.xUnlock = quotaUnlock;
|
||||||
gQuota.sIoMethodsV1.xCheckReservedLock = quotaCheckReservedLock;
|
gQuota.sIoMethodsV1.xCheckReservedLock = quotaCheckReservedLock;
|
||||||
gQuota.sIoMethodsV1.xFileControl = quotaFileControl;
|
gQuota.sIoMethodsV1.xFileControl = quotaFileControl;
|
||||||
gQuota.sIoMethodsV1.xSectorSize = quotaSectoSize;
|
gQuota.sIoMethodsV1.xSectorSize = quotaSectorSize;
|
||||||
gQuota.sIoMethodsV1.xDeviceCharacteristics = quotaDeviceCharacteristics;
|
gQuota.sIoMethodsV1.xDeviceCharacteristics = quotaDeviceCharacteristics;
|
||||||
gQuota.sIoMethodsV2 = gQuota.sIoMethodsV1;
|
gQuota.sIoMethodsV2 = gQuota.sIoMethodsV1;
|
||||||
gQuota.sIoMethodsV2.iVersion = 2;
|
gQuota.sIoMethodsV2.iVersion = 2;
|
||||||
@@ -533,7 +593,8 @@ int sqlite3_quota_initialize(const char *zOrigVfsName, int makeDefault){
|
|||||||
gQuota.sIoMethodsV2.xShmLock = quotaShmLock;
|
gQuota.sIoMethodsV2.xShmLock = quotaShmLock;
|
||||||
gQuota.sIoMethodsV2.xShmBarrier = quotaShmBarrier;
|
gQuota.sIoMethodsV2.xShmBarrier = quotaShmBarrier;
|
||||||
gQuota.sIoMethodsV2.xShmUnmap = quotaShmUnmap;
|
gQuota.sIoMethodsV2.xShmUnmap = quotaShmUnmap;
|
||||||
gQuota.pMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
|
sqlite3_vfs_register(&gQuota.sThisVfs, makeDefault);
|
||||||
|
return SQLITE_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -546,16 +607,21 @@ int sqlite3_quota_initialize(const char *zOrigVfsName, int makeDefault){
|
|||||||
** shutting down in order to free all remaining quota groups.
|
** shutting down in order to free all remaining quota groups.
|
||||||
*/
|
*/
|
||||||
int sqlite3_quota_shutdown(void){
|
int sqlite3_quota_shutdown(void){
|
||||||
|
quotaGroup *pGroup;
|
||||||
if( gQuota.isInitialized==0 ) return SQLITE_MISUSE;
|
if( gQuota.isInitialized==0 ) return SQLITE_MISUSE;
|
||||||
|
for(pGroup=gQuota.pGroup; pGroup; pGroup=pGroup->pNext){
|
||||||
|
if( pGroup->pFiles ) return SQLITE_MISUSE;
|
||||||
|
}
|
||||||
|
while( gQuota.pGroup ){
|
||||||
|
pGroup = gQuota.pGroup;
|
||||||
|
gQuota.pGroup = pGroup->pNext;
|
||||||
|
sqlite3_free(pGroup);
|
||||||
|
}
|
||||||
gQuota.isInitialized = 0;
|
gQuota.isInitialized = 0;
|
||||||
sqlite3_mutex_free(gQuota.pMutex);
|
sqlite3_mutex_free(gQuota.pMutex);
|
||||||
while( gQuota.pGroup ){
|
sqlite3_vfs_unregister(&gQuota.sThisVfs);
|
||||||
quotaGroup *p = gQuota.pGroup;
|
|
||||||
gQuota.pGroup = p->pNext;
|
|
||||||
if( p->nRef ) return SQLITE_MISUSE;
|
|
||||||
sqlite3_free(p);
|
|
||||||
}
|
|
||||||
memset(&gQuota, 0, sizeof(gQuota));
|
memset(&gQuota, 0, sizeof(gQuota));
|
||||||
|
return SQLITE_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -575,19 +641,20 @@ int sqlite3_quota_shutdown(void){
|
|||||||
**
|
**
|
||||||
** A quota group must exist with a non-zero iLimit prior to opening
|
** A quota group must exist with a non-zero iLimit prior to opening
|
||||||
** database connections if those connections are to participate in the
|
** database connections if those connections are to participate in the
|
||||||
** quota group. Creating a quota group does not effect database connections
|
** quota group. Creating a quota group does not affect database connections
|
||||||
** that are already open.
|
** that are already open.
|
||||||
*/
|
*/
|
||||||
int sqlite3_quota_set(
|
int sqlite3_quota_set(
|
||||||
const char *zPattern, /* The filename pattern */
|
const char *zPattern, /* The filename pattern */
|
||||||
sqlite3_int64 iLimit, /* New quota to set for this quota group */
|
sqlite3_int64 iLimit, /* New quota to set for this quota group */
|
||||||
void (*xCallback)( /* Callback invoked when going over quota */
|
void (*xCallback)( /* Callback invoked when going over quota */
|
||||||
const char *zFilename, /* Name of file whose size increases */
|
const char *zFilename, /* Name of file whose size increases */
|
||||||
sqlite3_int64 *piLimit, /* IN/OUT: The current limit */
|
sqlite3_int64 *piLimit, /* IN/OUT: The current limit */
|
||||||
sqlite3_int64 iSize, /* Total size of all files in the group */
|
sqlite3_int64 iSize, /* Total size of all files in the group */
|
||||||
void *pArg /* Client data */
|
void *pArg /* Client data */
|
||||||
);
|
),
|
||||||
void *pArg /* client data passed thru to callback */
|
void *pArg, /* client data passed thru to callback */
|
||||||
|
void (*xDestroy)(void*) /* Optional destructor for pArg */
|
||||||
){
|
){
|
||||||
quotaGroup *pGroup;
|
quotaGroup *pGroup;
|
||||||
quotaEnter();
|
quotaEnter();
|
||||||
@@ -595,8 +662,12 @@ int sqlite3_quota_set(
|
|||||||
while( pGroup && strcmp(pGroup->zPattern, zPattern)!=0 ){
|
while( pGroup && strcmp(pGroup->zPattern, zPattern)!=0 ){
|
||||||
pGroup = pGroup->pNext;
|
pGroup = pGroup->pNext;
|
||||||
}
|
}
|
||||||
if( pGroup==0 && iLimit>0 ){
|
if( pGroup==0 ){
|
||||||
int nPattern = strlen(zPattern);
|
int nPattern = strlen(zPattern);
|
||||||
|
if( iLimit<=0 ){
|
||||||
|
quotaLeave();
|
||||||
|
return SQLITE_OK;
|
||||||
|
}
|
||||||
pGroup = sqlite3_malloc( sizeof(*pGroup) + nPattern + 1 );
|
pGroup = sqlite3_malloc( sizeof(*pGroup) + nPattern + 1 );
|
||||||
if( pGroup==0 ){
|
if( pGroup==0 ){
|
||||||
quotaLeave();
|
quotaLeave();
|
||||||
@@ -604,14 +675,19 @@ int sqlite3_quota_set(
|
|||||||
}
|
}
|
||||||
memset(pGroup, 0, sizeof(*pGroup));
|
memset(pGroup, 0, sizeof(*pGroup));
|
||||||
pGroup->zPattern = (char*)&pGroup[1];
|
pGroup->zPattern = (char*)&pGroup[1];
|
||||||
memcpy(pGroup->zPattern, zPattern, nPattern+1);
|
memcpy((char *)pGroup->zPattern, zPattern, nPattern+1);
|
||||||
pGroup->pNext = gQuota.pGroup;
|
|
||||||
if( gQuota.pGroup ) gQuota.pGroup->ppPrev = &pGroup->pNext;
|
if( gQuota.pGroup ) gQuota.pGroup->ppPrev = &pGroup->pNext;
|
||||||
pGroup->ppPrev = &gQuota.pNext;
|
pGroup->pNext = gQuota.pGroup;
|
||||||
|
pGroup->ppPrev = &gQuota.pGroup;
|
||||||
|
gQuota.pGroup = pGroup;
|
||||||
}
|
}
|
||||||
pGroup->iLimit = iLimit;
|
pGroup->iLimit = iLimit;
|
||||||
pGroup->xCallback = xCallback;
|
pGroup->xCallback = xCallback;
|
||||||
|
if( pGroup->xDestroy && pGroup->pArg!=pArg ){
|
||||||
|
pGroup->xDestroy(pGroup->pArg);
|
||||||
|
}
|
||||||
pGroup->pArg = pArg;
|
pGroup->pArg = pArg;
|
||||||
|
pGroup->xDestroy = xDestroy;
|
||||||
quotaGroupDeref(pGroup);
|
quotaGroupDeref(pGroup);
|
||||||
quotaLeave();
|
quotaLeave();
|
||||||
return SQLITE_OK;
|
return SQLITE_OK;
|
||||||
@@ -621,14 +697,231 @@ int sqlite3_quota_set(
|
|||||||
/***************************** Test Code ***********************************/
|
/***************************** Test Code ***********************************/
|
||||||
#ifdef SQLITE_TEST
|
#ifdef SQLITE_TEST
|
||||||
#include <tcl.h>
|
#include <tcl.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Argument passed to a TCL quota-over-limit callback.
|
||||||
|
*/
|
||||||
|
typedef struct TclQuotaCallback TclQuotaCallback;
|
||||||
|
struct TclQuotaCallback {
|
||||||
|
Tcl_Interp *interp; /* Interpreter in which to run the script */
|
||||||
|
Tcl_Obj *pScript; /* Script to be run */
|
||||||
|
};
|
||||||
|
|
||||||
|
extern const char *sqlite3TestErrorName(int);
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** This is the callback from a quota-over-limit.
|
||||||
|
*/
|
||||||
|
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;
|
||||||
|
if( p==0 ) return;
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Destructor for a TCL quota-over-limit callback.
|
||||||
|
*/
|
||||||
|
static void tclCallbackDestructor(void *pObj){
|
||||||
|
TclQuotaCallback *p = (TclQuotaCallback*)pObj;
|
||||||
|
if( p ){
|
||||||
|
Tcl_DecrRefCount(p->pScript);
|
||||||
|
ckfree((char *)p);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** 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);
|
||||||
|
|
||||||
|
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 */
|
||||||
|
int nScript; /* Length of callback script */
|
||||||
|
|
||||||
|
/* 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];
|
||||||
|
Tcl_GetStringFromObj(pScript, &nScript);
|
||||||
|
|
||||||
|
if( nScript>0 ){
|
||||||
|
/* Allocate a TclQuotaCallback object */
|
||||||
|
p = (TclQuotaCallback *)ckalloc(sizeof(TclQuotaCallback));
|
||||||
|
memset(p, 0, sizeof(TclQuotaCallback));
|
||||||
|
p->interp = interp;
|
||||||
|
Tcl_IncrRefCount(pScript);
|
||||||
|
p->pScript = pScript;
|
||||||
|
}else{
|
||||||
|
p = 0;
|
||||||
|
}
|
||||||
|
/* Invoke sqlite3_quota_set() */
|
||||||
|
rc = sqlite3_quota_set(zPattern, iLimit, tclQuotaCallback,
|
||||||
|
(void*)p, tclCallbackDestructor);
|
||||||
|
|
||||||
|
Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC);
|
||||||
|
return TCL_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** tclcmd: sqlite3_quota_dump
|
||||||
|
*/
|
||||||
|
static int test_quota_dump(
|
||||||
|
void * clientData,
|
||||||
|
Tcl_Interp *interp,
|
||||||
|
int objc,
|
||||||
|
Tcl_Obj *CONST objv[]
|
||||||
|
){
|
||||||
|
Tcl_Obj *pResult;
|
||||||
|
Tcl_Obj *pGroupTerm;
|
||||||
|
Tcl_Obj *pFileTerm;
|
||||||
|
quotaGroup *pGroup;
|
||||||
|
quotaFile *pFile;
|
||||||
|
|
||||||
|
pResult = Tcl_NewObj();
|
||||||
|
quotaEnter();
|
||||||
|
for(pGroup=gQuota.pGroup; pGroup; pGroup=pGroup->pNext){
|
||||||
|
pGroupTerm = Tcl_NewObj();
|
||||||
|
Tcl_ListObjAppendElement(interp, pGroupTerm,
|
||||||
|
Tcl_NewStringObj(pGroup->zPattern, -1));
|
||||||
|
Tcl_ListObjAppendElement(interp, pGroupTerm,
|
||||||
|
Tcl_NewWideIntObj(pGroup->iLimit));
|
||||||
|
Tcl_ListObjAppendElement(interp, pGroupTerm,
|
||||||
|
Tcl_NewWideIntObj(pGroup->iSize));
|
||||||
|
for(pFile=pGroup->pFiles; pFile; pFile=pFile->pNext){
|
||||||
|
pFileTerm = Tcl_NewObj();
|
||||||
|
Tcl_ListObjAppendElement(interp, pFileTerm,
|
||||||
|
Tcl_NewStringObj(pFile->zFilename, -1));
|
||||||
|
Tcl_ListObjAppendElement(interp, pFileTerm,
|
||||||
|
Tcl_NewWideIntObj(pFile->iSize));
|
||||||
|
Tcl_ListObjAppendElement(interp, pFileTerm,
|
||||||
|
Tcl_NewWideIntObj(pFile->nRef));
|
||||||
|
Tcl_ListObjAppendElement(interp, pGroupTerm, pFileTerm);
|
||||||
|
}
|
||||||
|
Tcl_ListObjAppendElement(interp, pResult, pGroupTerm);
|
||||||
|
}
|
||||||
|
quotaLeave();
|
||||||
|
Tcl_SetObjResult(interp, pResult);
|
||||||
|
return TCL_OK;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** This routine registers the custom TCL commands defined in this
|
** This routine registers the custom TCL commands defined in this
|
||||||
** module. This should be the only procedure visible from outside
|
** module. This should be the only procedure visible from outside
|
||||||
** of this module.
|
** of this module.
|
||||||
*/
|
*/
|
||||||
int Sqlitequota_Init(Tcl_Interp *interp){
|
int Sqlitequota_Init(Tcl_Interp *interp){
|
||||||
#ifdef SQLITE_ENABLE_ASYNCIO
|
static struct {
|
||||||
#endif /* SQLITE_ENABLE_ASYNCIO */
|
char *zName;
|
||||||
|
Tcl_ObjCmdProc *xProc;
|
||||||
|
} aCmd[] = {
|
||||||
|
{ "sqlite3_quota_initialize", test_quota_initialize },
|
||||||
|
{ "sqlite3_quota_shutdown", test_quota_shutdown },
|
||||||
|
{ "sqlite3_quota_set", test_quota_set },
|
||||||
|
{ "sqlite3_quota_dump", test_quota_dump },
|
||||||
|
};
|
||||||
|
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;
|
return TCL_OK;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
214
test/quota.test
Normal file
214
test/quota.test
Normal file
@@ -0,0 +1,214 @@
|
|||||||
|
# 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.99 {
|
||||||
|
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
|
||||||
|
} {}
|
||||||
|
puts "quotas: [sqlite3_quota_dump]"
|
||||||
|
|
||||||
|
do_test quota-3.X {
|
||||||
|
catch { db close }
|
||||||
|
catch { db2 close }
|
||||||
|
} {0}
|
||||||
|
|
||||||
|
#-------------------------------------------------------------------------
|
||||||
|
# Quotas are deleted when unused and when there limit is set to zero
|
||||||
|
#
|
||||||
|
|
||||||
|
# Return a list of all currently defined quotas. Each quota is identified
|
||||||
|
# by its pattern.
|
||||||
|
proc quota_list {} {
|
||||||
|
set allq {}
|
||||||
|
foreach q [sqlite3_quota_dump] {
|
||||||
|
lappend allq [lindex $q 0]
|
||||||
|
}
|
||||||
|
return [lsort $allq]
|
||||||
|
}
|
||||||
|
|
||||||
|
do_test quota-4.1 {
|
||||||
|
sqlite3_quota_set *test.db 0 {}
|
||||||
|
quota_list
|
||||||
|
} {}
|
||||||
|
do_test quota-4.2 {
|
||||||
|
sqlite3_quota_set *test.db 4096 {}
|
||||||
|
quota_list
|
||||||
|
} {*test.db}
|
||||||
|
do_test quota-4.3 {
|
||||||
|
sqlite3_quota_set *test2.db 0 {}
|
||||||
|
quota_list
|
||||||
|
} {*test.db}
|
||||||
|
do_test quota-4.4 {
|
||||||
|
sqlite3_quota_set *test2.db 100000 {}
|
||||||
|
quota_list
|
||||||
|
} {*test.db *test2.db}
|
||||||
|
do_test quota-4.5 {
|
||||||
|
sqlite3_quota_set *test.db 0 {}
|
||||||
|
quota_list
|
||||||
|
} {*test2.db}
|
||||||
|
do_test quota-4.6 {
|
||||||
|
file delete -force test2.db test2.db-journal test2.db-wal
|
||||||
|
sqlite3 db test2.db
|
||||||
|
db eval {CREATE TABLE t2(x); INSERT INTO t2 VALUES('tab-t2');}
|
||||||
|
quota_list
|
||||||
|
} {*test2.db}
|
||||||
|
do_test quota-4.7 {
|
||||||
|
catchsql {INSERT INTO t2 VALUES(zeroblob(200000))}
|
||||||
|
} {1 {database or disk is full}}
|
||||||
|
do_test quota-4.8 {
|
||||||
|
sqlite3 db2 test2.db
|
||||||
|
db2 eval {SELECT * FROM t2}
|
||||||
|
} {tab-t2}
|
||||||
|
do_test quota-4.9 {
|
||||||
|
sqlite3_quota_set *test2.db 0 {}
|
||||||
|
catchsql {INSERT INTO t2 VALUES(zeroblob(200000))}
|
||||||
|
} {0 {}}
|
||||||
|
do_test quota-4.10 {
|
||||||
|
quota_list
|
||||||
|
} {*test2.db}
|
||||||
|
do_test quota-4.11 {
|
||||||
|
db2 close
|
||||||
|
quota_list
|
||||||
|
} {*test2.db}
|
||||||
|
do_test quota-4.12 {
|
||||||
|
db close
|
||||||
|
quota_list
|
||||||
|
} {}
|
||||||
|
|
||||||
|
|
||||||
|
sqlite3_quota_shutdown
|
||||||
|
finish_test
|
Reference in New Issue
Block a user