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