test_quota.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008
  1. /*
  2. ** 2010 September 31
  3. **
  4. ** The author disclaims copyright to this source code. In place of
  5. ** a legal notice, here is a blessing:
  6. **
  7. ** May you do good and not evil.
  8. ** May you find forgiveness for yourself and forgive others.
  9. ** May you share freely, never taking more than you give.
  10. **
  11. *************************************************************************
  12. **
  13. ** This file contains a VFS "shim" - a layer that sits in between the
  14. ** pager and the real VFS.
  15. **
  16. ** This particular shim enforces a quota system on files. One or more
  17. ** database files are in a "quota group" that is defined by a GLOB
  18. ** pattern. A quota is set for the combined size of all files in the
  19. ** the group. A quota of zero means "no limit". If the total size
  20. ** of all files in the quota group is greater than the limit, then
  21. ** write requests that attempt to enlarge a file fail with SQLITE_FULL.
  22. **
  23. ** However, before returning SQLITE_FULL, the write requests invoke
  24. ** a callback function that is configurable for each quota group.
  25. ** This callback has the opportunity to enlarge the quota. If the
  26. ** callback does enlarge the quota such that the total size of all
  27. ** files within the group is less than the new quota, then the write
  28. ** continues as if nothing had happened.
  29. */
  30. #include "test_quota.h"
  31. #include <string.h>
  32. #include <assert.h>
  33. /*
  34. ** For an build without mutexes, no-op the mutex calls.
  35. */
  36. #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE==0
  37. #define sqlite3_mutex_alloc(X) ((sqlite3_mutex*)8)
  38. #define sqlite3_mutex_free(X)
  39. #define sqlite3_mutex_enter(X)
  40. #define sqlite3_mutex_try(X) SQLITE_OK
  41. #define sqlite3_mutex_leave(X)
  42. #define sqlite3_mutex_held(X) ((void)(X),1)
  43. #define sqlite3_mutex_notheld(X) ((void)(X),1)
  44. #endif /* SQLITE_THREADSAFE==0 */
  45. /*
  46. ** Figure out if we are dealing with Unix, Windows, or some other
  47. ** operating system. After the following block of preprocess macros,
  48. ** all of SQLITE_OS_UNIX, SQLITE_OS_WIN, and SQLITE_OS_OTHER
  49. ** will defined to either 1 or 0. One of the four will be 1. The other
  50. ** three will be 0.
  51. */
  52. #if defined(SQLITE_OS_OTHER)
  53. # if SQLITE_OS_OTHER==1
  54. # undef SQLITE_OS_UNIX
  55. # define SQLITE_OS_UNIX 0
  56. # undef SQLITE_OS_WIN
  57. # define SQLITE_OS_WIN 0
  58. # else
  59. # undef SQLITE_OS_OTHER
  60. # endif
  61. #endif
  62. #if !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_OTHER)
  63. # define SQLITE_OS_OTHER 0
  64. # ifndef SQLITE_OS_WIN
  65. # if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) \
  66. || defined(__MINGW32__) || defined(__BORLANDC__)
  67. # define SQLITE_OS_WIN 1
  68. # define SQLITE_OS_UNIX 0
  69. # else
  70. # define SQLITE_OS_WIN 0
  71. # define SQLITE_OS_UNIX 1
  72. # endif
  73. # else
  74. # define SQLITE_OS_UNIX 0
  75. # endif
  76. #else
  77. # ifndef SQLITE_OS_WIN
  78. # define SQLITE_OS_WIN 0
  79. # endif
  80. #endif
  81. #if SQLITE_OS_UNIX
  82. # include <unistd.h>
  83. #endif
  84. #if SQLITE_OS_WIN
  85. # include <windows.h>
  86. # include <io.h>
  87. #endif
  88. /************************ Object Definitions ******************************/
  89. /* Forward declaration of all object types */
  90. typedef struct quotaGroup quotaGroup;
  91. typedef struct quotaConn quotaConn;
  92. typedef struct quotaFile quotaFile;
  93. /*
  94. ** A "quota group" is a collection of files whose collective size we want
  95. ** to limit. Each quota group is defined by a GLOB pattern.
  96. **
  97. ** There is an instance of the following object for each defined quota
  98. ** group. This object records the GLOB pattern that defines which files
  99. ** belong to the quota group. The object also remembers the size limit
  100. ** for the group (the quota) and the callback to be invoked when the
  101. ** sum of the sizes of the files within the group goes over the limit.
  102. **
  103. ** A quota group must be established (using sqlite3_quota_set(...))
  104. ** prior to opening any of the database connections that access files
  105. ** within the quota group.
  106. */
  107. struct quotaGroup {
  108. const char *zPattern; /* Filename pattern to be quotaed */
  109. sqlite3_int64 iLimit; /* Upper bound on total file size */
  110. sqlite3_int64 iSize; /* Current size of all files */
  111. void (*xCallback)( /* Callback invoked when going over quota */
  112. const char *zFilename, /* Name of file whose size increases */
  113. sqlite3_int64 *piLimit, /* IN/OUT: The current limit */
  114. sqlite3_int64 iSize, /* Total size of all files in the group */
  115. void *pArg /* Client data */
  116. );
  117. void *pArg; /* Third argument to the xCallback() */
  118. void (*xDestroy)(void*); /* Optional destructor for pArg */
  119. quotaGroup *pNext, **ppPrev; /* Doubly linked list of all quota objects */
  120. quotaFile *pFiles; /* Files within this group */
  121. };
  122. /*
  123. ** An instance of this structure represents a single file that is part
  124. ** of a quota group. A single file can be opened multiple times. In
  125. ** order keep multiple openings of the same file from causing the size
  126. ** of the file to count against the quota multiple times, each file
  127. ** has a unique instance of this object and multiple open connections
  128. ** to the same file each point to a single instance of this object.
  129. */
  130. struct quotaFile {
  131. char *zFilename; /* Name of this file */
  132. quotaGroup *pGroup; /* Quota group to which this file belongs */
  133. sqlite3_int64 iSize; /* Current size of this file */
  134. int nRef; /* Number of times this file is open */
  135. int deleteOnClose; /* True to delete this file when it closes */
  136. quotaFile *pNext, **ppPrev; /* Linked list of files in the same group */
  137. };
  138. /*
  139. ** An instance of the following object represents each open connection
  140. ** to a file that participates in quota tracking. This object is a
  141. ** subclass of sqlite3_file. The sqlite3_file object for the underlying
  142. ** VFS is appended to this structure.
  143. */
  144. struct quotaConn {
  145. sqlite3_file base; /* Base class - must be first */
  146. quotaFile *pFile; /* The underlying file */
  147. /* The underlying VFS sqlite3_file is appended to this object */
  148. };
  149. /*
  150. ** An instance of the following object records the state of an
  151. ** open file. This object is opaque to all users - the internal
  152. ** structure is only visible to the functions below.
  153. */
  154. struct quota_FILE {
  155. FILE *f; /* Open stdio file pointer */
  156. sqlite3_int64 iOfst; /* Current offset into the file */
  157. quotaFile *pFile; /* The file record in the quota system */
  158. #if SQLITE_OS_WIN
  159. char *zMbcsName; /* Full MBCS pathname of the file */
  160. #endif
  161. };
  162. /************************* Global Variables **********************************/
  163. /*
  164. ** All global variables used by this file are containing within the following
  165. ** gQuota structure.
  166. */
  167. static struct {
  168. /* The pOrigVfs is the real, original underlying VFS implementation.
  169. ** Most operations pass-through to the real VFS. This value is read-only
  170. ** during operation. It is only modified at start-time and thus does not
  171. ** require a mutex.
  172. */
  173. sqlite3_vfs *pOrigVfs;
  174. /* The sThisVfs is the VFS structure used by this shim. It is initialized
  175. ** at start-time and thus does not require a mutex
  176. */
  177. sqlite3_vfs sThisVfs;
  178. /* The sIoMethods defines the methods used by sqlite3_file objects
  179. ** associated with this shim. It is initialized at start-time and does
  180. ** not require a mutex.
  181. **
  182. ** When the underlying VFS is called to open a file, it might return
  183. ** either a version 1 or a version 2 sqlite3_file object. This shim
  184. ** has to create a wrapper sqlite3_file of the same version. Hence
  185. ** there are two I/O method structures, one for version 1 and the other
  186. ** for version 2.
  187. */
  188. sqlite3_io_methods sIoMethodsV1;
  189. sqlite3_io_methods sIoMethodsV2;
  190. /* True when this shim as been initialized.
  191. */
  192. int isInitialized;
  193. /* For run-time access any of the other global data structures in this
  194. ** shim, the following mutex must be held.
  195. */
  196. sqlite3_mutex *pMutex;
  197. /* List of quotaGroup objects.
  198. */
  199. quotaGroup *pGroup;
  200. } gQuota;
  201. /************************* Utility Routines *********************************/
  202. /*
  203. ** Acquire and release the mutex used to serialize access to the
  204. ** list of quotaGroups.
  205. */
  206. static void quotaEnter(void){ sqlite3_mutex_enter(gQuota.pMutex); }
  207. static void quotaLeave(void){ sqlite3_mutex_leave(gQuota.pMutex); }
  208. /* Count the number of open files in a quotaGroup
  209. */
  210. static int quotaGroupOpenFileCount(quotaGroup *pGroup){
  211. int N = 0;
  212. quotaFile *pFile = pGroup->pFiles;
  213. while( pFile ){
  214. if( pFile->nRef ) N++;
  215. pFile = pFile->pNext;
  216. }
  217. return N;
  218. }
  219. /* Remove a file from a quota group.
  220. */
  221. static void quotaRemoveFile(quotaFile *pFile){
  222. quotaGroup *pGroup = pFile->pGroup;
  223. pGroup->iSize -= pFile->iSize;
  224. *pFile->ppPrev = pFile->pNext;
  225. if( pFile->pNext ) pFile->pNext->ppPrev = pFile->ppPrev;
  226. sqlite3_free(pFile);
  227. }
  228. /* Remove all files from a quota group. It is always the case that
  229. ** all files will be closed when this routine is called.
  230. */
  231. static void quotaRemoveAllFiles(quotaGroup *pGroup){
  232. while( pGroup->pFiles ){
  233. assert( pGroup->pFiles->nRef==0 );
  234. quotaRemoveFile(pGroup->pFiles);
  235. }
  236. }
  237. /* If the reference count and threshold for a quotaGroup are both
  238. ** zero, then destroy the quotaGroup.
  239. */
  240. static void quotaGroupDeref(quotaGroup *pGroup){
  241. if( pGroup->iLimit==0 && quotaGroupOpenFileCount(pGroup)==0 ){
  242. quotaRemoveAllFiles(pGroup);
  243. *pGroup->ppPrev = pGroup->pNext;
  244. if( pGroup->pNext ) pGroup->pNext->ppPrev = pGroup->ppPrev;
  245. if( pGroup->xDestroy ) pGroup->xDestroy(pGroup->pArg);
  246. sqlite3_free(pGroup);
  247. }
  248. }
  249. /*
  250. ** Return TRUE if string z matches glob pattern zGlob.
  251. **
  252. ** Globbing rules:
  253. **
  254. ** '*' Matches any sequence of zero or more characters.
  255. **
  256. ** '?' Matches exactly one character.
  257. **
  258. ** [...] Matches one character from the enclosed list of
  259. ** characters.
  260. **
  261. ** [^...] Matches one character not in the enclosed list.
  262. **
  263. ** / Matches "/" or "\\"
  264. **
  265. */
  266. static int quotaStrglob(const char *zGlob, const char *z){
  267. int c, c2, cx;
  268. int invert;
  269. int seen;
  270. while( (c = (*(zGlob++)))!=0 ){
  271. if( c=='*' ){
  272. while( (c=(*(zGlob++))) == '*' || c=='?' ){
  273. if( c=='?' && (*(z++))==0 ) return 0;
  274. }
  275. if( c==0 ){
  276. return 1;
  277. }else if( c=='[' ){
  278. while( *z && quotaStrglob(zGlob-1,z)==0 ){
  279. z++;
  280. }
  281. return (*z)!=0;
  282. }
  283. cx = (c=='/') ? '\\' : c;
  284. while( (c2 = (*(z++)))!=0 ){
  285. while( c2!=c && c2!=cx ){
  286. c2 = *(z++);
  287. if( c2==0 ) return 0;
  288. }
  289. if( quotaStrglob(zGlob,z) ) return 1;
  290. }
  291. return 0;
  292. }else if( c=='?' ){
  293. if( (*(z++))==0 ) return 0;
  294. }else if( c=='[' ){
  295. int prior_c = 0;
  296. seen = 0;
  297. invert = 0;
  298. c = *(z++);
  299. if( c==0 ) return 0;
  300. c2 = *(zGlob++);
  301. if( c2=='^' ){
  302. invert = 1;
  303. c2 = *(zGlob++);
  304. }
  305. if( c2==']' ){
  306. if( c==']' ) seen = 1;
  307. c2 = *(zGlob++);
  308. }
  309. while( c2 && c2!=']' ){
  310. if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
  311. c2 = *(zGlob++);
  312. if( c>=prior_c && c<=c2 ) seen = 1;
  313. prior_c = 0;
  314. }else{
  315. if( c==c2 ){
  316. seen = 1;
  317. }
  318. prior_c = c2;
  319. }
  320. c2 = *(zGlob++);
  321. }
  322. if( c2==0 || (seen ^ invert)==0 ) return 0;
  323. }else if( c=='/' ){
  324. if( z[0]!='/' && z[0]!='\\' ) return 0;
  325. z++;
  326. }else{
  327. if( c!=(*(z++)) ) return 0;
  328. }
  329. }
  330. return *z==0;
  331. }
  332. /* Find a quotaGroup given the filename.
  333. **
  334. ** Return a pointer to the quotaGroup object. Return NULL if not found.
  335. */
  336. static quotaGroup *quotaGroupFind(const char *zFilename){
  337. quotaGroup *p;
  338. for(p=gQuota.pGroup; p && quotaStrglob(p->zPattern, zFilename)==0;
  339. p=p->pNext){}
  340. return p;
  341. }
  342. /* Translate an sqlite3_file* that is really a quotaConn* into
  343. ** the sqlite3_file* for the underlying original VFS.
  344. */
  345. static sqlite3_file *quotaSubOpen(sqlite3_file *pConn){
  346. quotaConn *p = (quotaConn*)pConn;
  347. return (sqlite3_file*)&p[1];
  348. }
  349. /* Find a file in a quota group and return a pointer to that file.
  350. ** Return NULL if the file is not in the group.
  351. */
  352. static quotaFile *quotaFindFile(
  353. quotaGroup *pGroup, /* Group in which to look for the file */
  354. const char *zName, /* Full pathname of the file */
  355. int createFlag /* Try to create the file if not found */
  356. ){
  357. quotaFile *pFile = pGroup->pFiles;
  358. while( pFile && strcmp(pFile->zFilename, zName)!=0 ){
  359. pFile = pFile->pNext;
  360. }
  361. if( pFile==0 && createFlag ){
  362. int nName = (int)(strlen(zName) & 0x3fffffff);
  363. pFile = (quotaFile *)sqlite3_malloc( sizeof(*pFile) + nName + 1 );
  364. if( pFile ){
  365. memset(pFile, 0, sizeof(*pFile));
  366. pFile->zFilename = (char*)&pFile[1];
  367. memcpy(pFile->zFilename, zName, nName+1);
  368. pFile->pNext = pGroup->pFiles;
  369. if( pGroup->pFiles ) pGroup->pFiles->ppPrev = &pFile->pNext;
  370. pFile->ppPrev = &pGroup->pFiles;
  371. pGroup->pFiles = pFile;
  372. pFile->pGroup = pGroup;
  373. }
  374. }
  375. return pFile;
  376. }
  377. /*
  378. ** Translate UTF8 to MBCS for use in fopen() calls. Return a pointer to the
  379. ** translated text.. Call quota_mbcs_free() to deallocate any memory
  380. ** used to store the returned pointer when done.
  381. */
  382. static char *quota_utf8_to_mbcs(const char *zUtf8){
  383. #if SQLITE_OS_WIN
  384. size_t n; /* Bytes in zUtf8 */
  385. int nWide; /* number of UTF-16 characters */
  386. int nMbcs; /* Bytes of MBCS */
  387. LPWSTR zTmpWide; /* The UTF16 text */
  388. char *zMbcs; /* The MBCS text */
  389. int codepage; /* Code page used by fopen() */
  390. n = strlen(zUtf8);
  391. nWide = MultiByteToWideChar(CP_UTF8, 0, zUtf8, -1, NULL, 0);
  392. if( nWide==0 ) return 0;
  393. zTmpWide = (LPWSTR)sqlite3_malloc( (nWide+1)*sizeof(zTmpWide[0]) );
  394. if( zTmpWide==0 ) return 0;
  395. MultiByteToWideChar(CP_UTF8, 0, zUtf8, -1, zTmpWide, nWide);
  396. codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
  397. nMbcs = WideCharToMultiByte(codepage, 0, zTmpWide, nWide, 0, 0, 0, 0);
  398. zMbcs = nMbcs ? (char*)sqlite3_malloc( nMbcs+1 ) : 0;
  399. if( zMbcs ){
  400. WideCharToMultiByte(codepage, 0, zTmpWide, nWide, zMbcs, nMbcs, 0, 0);
  401. }
  402. sqlite3_free(zTmpWide);
  403. return zMbcs;
  404. #else
  405. return (char*)zUtf8; /* No-op on unix */
  406. #endif
  407. }
  408. /*
  409. ** Deallocate any memory allocated by quota_utf8_to_mbcs().
  410. */
  411. static void quota_mbcs_free(char *zOld){
  412. #if SQLITE_OS_WIN
  413. sqlite3_free(zOld);
  414. #else
  415. /* No-op on unix */
  416. #endif
  417. }
  418. /************************* VFS Method Wrappers *****************************/
  419. /*
  420. ** This is the xOpen method used for the "quota" VFS.
  421. **
  422. ** Most of the work is done by the underlying original VFS. This method
  423. ** simply links the new file into the appropriate quota group if it is a
  424. ** file that needs to be tracked.
  425. */
  426. static int quotaOpen(
  427. sqlite3_vfs *pVfs, /* The quota VFS */
  428. const char *zName, /* Name of file to be opened */
  429. sqlite3_file *pConn, /* Fill in this file descriptor */
  430. int flags, /* Flags to control the opening */
  431. int *pOutFlags /* Flags showing results of opening */
  432. ){
  433. int rc; /* Result code */
  434. quotaConn *pQuotaOpen; /* The new quota file descriptor */
  435. quotaFile *pFile; /* Corresponding quotaFile obj */
  436. quotaGroup *pGroup; /* The group file belongs to */
  437. sqlite3_file *pSubOpen; /* Real file descriptor */
  438. sqlite3_vfs *pOrigVfs = gQuota.pOrigVfs; /* Real VFS */
  439. /* If the file is not a main database file or a WAL, then use the
  440. ** normal xOpen method.
  441. */
  442. if( (flags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_WAL))==0 ){
  443. return pOrigVfs->xOpen(pOrigVfs, zName, pConn, flags, pOutFlags);
  444. }
  445. /* If the name of the file does not match any quota group, then
  446. ** use the normal xOpen method.
  447. */
  448. quotaEnter();
  449. pGroup = quotaGroupFind(zName);
  450. if( pGroup==0 ){
  451. rc = pOrigVfs->xOpen(pOrigVfs, zName, pConn, flags, pOutFlags);
  452. }else{
  453. /* If we get to this point, it means the file needs to be quota tracked.
  454. */
  455. pQuotaOpen = (quotaConn*)pConn;
  456. pSubOpen = quotaSubOpen(pConn);
  457. rc = pOrigVfs->xOpen(pOrigVfs, zName, pSubOpen, flags, pOutFlags);
  458. if( rc==SQLITE_OK ){
  459. pFile = quotaFindFile(pGroup, zName, 1);
  460. if( pFile==0 ){
  461. quotaLeave();
  462. pSubOpen->pMethods->xClose(pSubOpen);
  463. return SQLITE_NOMEM;
  464. }
  465. pFile->deleteOnClose = (flags & SQLITE_OPEN_DELETEONCLOSE)!=0;
  466. pFile->nRef++;
  467. pQuotaOpen->pFile = pFile;
  468. if( pSubOpen->pMethods->iVersion==1 ){
  469. pQuotaOpen->base.pMethods = &gQuota.sIoMethodsV1;
  470. }else{
  471. pQuotaOpen->base.pMethods = &gQuota.sIoMethodsV2;
  472. }
  473. }
  474. }
  475. quotaLeave();
  476. return rc;
  477. }
  478. /*
  479. ** This is the xDelete method used for the "quota" VFS.
  480. **
  481. ** If the file being deleted is part of the quota group, then reduce
  482. ** the size of the quota group accordingly. And remove the file from
  483. ** the set of files in the quota group.
  484. */
  485. static int quotaDelete(
  486. sqlite3_vfs *pVfs, /* The quota VFS */
  487. const char *zName, /* Name of file to be deleted */
  488. int syncDir /* Do a directory sync after deleting */
  489. ){
  490. int rc; /* Result code */
  491. quotaFile *pFile; /* Files in the quota */
  492. quotaGroup *pGroup; /* The group file belongs to */
  493. sqlite3_vfs *pOrigVfs = gQuota.pOrigVfs; /* Real VFS */
  494. /* Do the actual file delete */
  495. rc = pOrigVfs->xDelete(pOrigVfs, zName, syncDir);
  496. /* If the file just deleted is a member of a quota group, then remove
  497. ** it from that quota group.
  498. */
  499. if( rc==SQLITE_OK ){
  500. quotaEnter();
  501. pGroup = quotaGroupFind(zName);
  502. if( pGroup ){
  503. pFile = quotaFindFile(pGroup, zName, 0);
  504. if( pFile ){
  505. if( pFile->nRef ){
  506. pFile->deleteOnClose = 1;
  507. }else{
  508. quotaRemoveFile(pFile);
  509. quotaGroupDeref(pGroup);
  510. }
  511. }
  512. }
  513. quotaLeave();
  514. }
  515. return rc;
  516. }
  517. /************************ I/O Method Wrappers *******************************/
  518. /* xClose requests get passed through to the original VFS. But we
  519. ** also have to unlink the quotaConn from the quotaFile and quotaGroup.
  520. ** The quotaFile and/or quotaGroup are freed if they are no longer in use.
  521. */
  522. static int quotaClose(sqlite3_file *pConn){
  523. quotaConn *p = (quotaConn*)pConn;
  524. quotaFile *pFile = p->pFile;
  525. sqlite3_file *pSubOpen = quotaSubOpen(pConn);
  526. int rc;
  527. rc = pSubOpen->pMethods->xClose(pSubOpen);
  528. quotaEnter();
  529. pFile->nRef--;
  530. if( pFile->nRef==0 ){
  531. quotaGroup *pGroup = pFile->pGroup;
  532. if( pFile->deleteOnClose ){
  533. gQuota.pOrigVfs->xDelete(gQuota.pOrigVfs, pFile->zFilename, 0);
  534. quotaRemoveFile(pFile);
  535. }
  536. quotaGroupDeref(pGroup);
  537. }
  538. quotaLeave();
  539. return rc;
  540. }
  541. /* Pass xRead requests directory thru to the original VFS without
  542. ** further processing.
  543. */
  544. static int quotaRead(
  545. sqlite3_file *pConn,
  546. void *pBuf,
  547. int iAmt,
  548. sqlite3_int64 iOfst
  549. ){
  550. sqlite3_file *pSubOpen = quotaSubOpen(pConn);
  551. return pSubOpen->pMethods->xRead(pSubOpen, pBuf, iAmt, iOfst);
  552. }
  553. /* Check xWrite requests to see if they expand the file. If they do,
  554. ** the perform a quota check before passing them through to the
  555. ** original VFS.
  556. */
  557. static int quotaWrite(
  558. sqlite3_file *pConn,
  559. const void *pBuf,
  560. int iAmt,
  561. sqlite3_int64 iOfst
  562. ){
  563. quotaConn *p = (quotaConn*)pConn;
  564. sqlite3_file *pSubOpen = quotaSubOpen(pConn);
  565. sqlite3_int64 iEnd = iOfst+iAmt;
  566. quotaGroup *pGroup;
  567. quotaFile *pFile = p->pFile;
  568. sqlite3_int64 szNew;
  569. if( pFile->iSize<iEnd ){
  570. pGroup = pFile->pGroup;
  571. quotaEnter();
  572. szNew = pGroup->iSize - pFile->iSize + iEnd;
  573. if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){
  574. if( pGroup->xCallback ){
  575. pGroup->xCallback(pFile->zFilename, &pGroup->iLimit, szNew,
  576. pGroup->pArg);
  577. }
  578. if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){
  579. quotaLeave();
  580. return SQLITE_FULL;
  581. }
  582. }
  583. pGroup->iSize = szNew;
  584. pFile->iSize = iEnd;
  585. quotaLeave();
  586. }
  587. return pSubOpen->pMethods->xWrite(pSubOpen, pBuf, iAmt, iOfst);
  588. }
  589. /* Pass xTruncate requests thru to the original VFS. If the
  590. ** success, update the file size.
  591. */
  592. static int quotaTruncate(sqlite3_file *pConn, sqlite3_int64 size){
  593. quotaConn *p = (quotaConn*)pConn;
  594. sqlite3_file *pSubOpen = quotaSubOpen(pConn);
  595. int rc = pSubOpen->pMethods->xTruncate(pSubOpen, size);
  596. quotaFile *pFile = p->pFile;
  597. quotaGroup *pGroup;
  598. if( rc==SQLITE_OK ){
  599. quotaEnter();
  600. pGroup = pFile->pGroup;
  601. pGroup->iSize -= pFile->iSize;
  602. pFile->iSize = size;
  603. pGroup->iSize += size;
  604. quotaLeave();
  605. }
  606. return rc;
  607. }
  608. /* Pass xSync requests through to the original VFS without change
  609. */
  610. static int quotaSync(sqlite3_file *pConn, int flags){
  611. sqlite3_file *pSubOpen = quotaSubOpen(pConn);
  612. return pSubOpen->pMethods->xSync(pSubOpen, flags);
  613. }
  614. /* Pass xFileSize requests through to the original VFS but then
  615. ** update the quotaGroup with the new size before returning.
  616. */
  617. static int quotaFileSize(sqlite3_file *pConn, sqlite3_int64 *pSize){
  618. quotaConn *p = (quotaConn*)pConn;
  619. sqlite3_file *pSubOpen = quotaSubOpen(pConn);
  620. quotaFile *pFile = p->pFile;
  621. quotaGroup *pGroup;
  622. sqlite3_int64 sz;
  623. int rc;
  624. rc = pSubOpen->pMethods->xFileSize(pSubOpen, &sz);
  625. if( rc==SQLITE_OK ){
  626. quotaEnter();
  627. pGroup = pFile->pGroup;
  628. pGroup->iSize -= pFile->iSize;
  629. pFile->iSize = sz;
  630. pGroup->iSize += sz;
  631. quotaLeave();
  632. *pSize = sz;
  633. }
  634. return rc;
  635. }
  636. /* Pass xLock requests through to the original VFS unchanged.
  637. */
  638. static int quotaLock(sqlite3_file *pConn, int lock){
  639. sqlite3_file *pSubOpen = quotaSubOpen(pConn);
  640. return pSubOpen->pMethods->xLock(pSubOpen, lock);
  641. }
  642. /* Pass xUnlock requests through to the original VFS unchanged.
  643. */
  644. static int quotaUnlock(sqlite3_file *pConn, int lock){
  645. sqlite3_file *pSubOpen = quotaSubOpen(pConn);
  646. return pSubOpen->pMethods->xUnlock(pSubOpen, lock);
  647. }
  648. /* Pass xCheckReservedLock requests through to the original VFS unchanged.
  649. */
  650. static int quotaCheckReservedLock(sqlite3_file *pConn, int *pResOut){
  651. sqlite3_file *pSubOpen = quotaSubOpen(pConn);
  652. return pSubOpen->pMethods->xCheckReservedLock(pSubOpen, pResOut);
  653. }
  654. /* Pass xFileControl requests through to the original VFS unchanged.
  655. */
  656. static int quotaFileControl(sqlite3_file *pConn, int op, void *pArg){
  657. sqlite3_file *pSubOpen = quotaSubOpen(pConn);
  658. int rc = pSubOpen->pMethods->xFileControl(pSubOpen, op, pArg);
  659. #if defined(SQLITE_FCNTL_VFSNAME)
  660. if( op==SQLITE_FCNTL_VFSNAME && rc==SQLITE_OK ){
  661. *(char**)pArg = sqlite3_mprintf("quota/%z", *(char**)pArg);
  662. }
  663. #endif
  664. return rc;
  665. }
  666. /* Pass xSectorSize requests through to the original VFS unchanged.
  667. */
  668. static int quotaSectorSize(sqlite3_file *pConn){
  669. sqlite3_file *pSubOpen = quotaSubOpen(pConn);
  670. return pSubOpen->pMethods->xSectorSize(pSubOpen);
  671. }
  672. /* Pass xDeviceCharacteristics requests through to the original VFS unchanged.
  673. */
  674. static int quotaDeviceCharacteristics(sqlite3_file *pConn){
  675. sqlite3_file *pSubOpen = quotaSubOpen(pConn);
  676. return pSubOpen->pMethods->xDeviceCharacteristics(pSubOpen);
  677. }
  678. /* Pass xShmMap requests through to the original VFS unchanged.
  679. */
  680. static int quotaShmMap(
  681. sqlite3_file *pConn, /* Handle open on database file */
  682. int iRegion, /* Region to retrieve */
  683. int szRegion, /* Size of regions */
  684. int bExtend, /* True to extend file if necessary */
  685. void volatile **pp /* OUT: Mapped memory */
  686. ){
  687. sqlite3_file *pSubOpen = quotaSubOpen(pConn);
  688. return pSubOpen->pMethods->xShmMap(pSubOpen, iRegion, szRegion, bExtend, pp);
  689. }
  690. /* Pass xShmLock requests through to the original VFS unchanged.
  691. */
  692. static int quotaShmLock(
  693. sqlite3_file *pConn, /* Database file holding the shared memory */
  694. int ofst, /* First lock to acquire or release */
  695. int n, /* Number of locks to acquire or release */
  696. int flags /* What to do with the lock */
  697. ){
  698. sqlite3_file *pSubOpen = quotaSubOpen(pConn);
  699. return pSubOpen->pMethods->xShmLock(pSubOpen, ofst, n, flags);
  700. }
  701. /* Pass xShmBarrier requests through to the original VFS unchanged.
  702. */
  703. static void quotaShmBarrier(sqlite3_file *pConn){
  704. sqlite3_file *pSubOpen = quotaSubOpen(pConn);
  705. pSubOpen->pMethods->xShmBarrier(pSubOpen);
  706. }
  707. /* Pass xShmUnmap requests through to the original VFS unchanged.
  708. */
  709. static int quotaShmUnmap(sqlite3_file *pConn, int deleteFlag){
  710. sqlite3_file *pSubOpen = quotaSubOpen(pConn);
  711. return pSubOpen->pMethods->xShmUnmap(pSubOpen, deleteFlag);
  712. }
  713. /************************** Public Interfaces *****************************/
  714. /*
  715. ** Initialize the quota VFS shim. Use the VFS named zOrigVfsName
  716. ** as the VFS that does the actual work. Use the default if
  717. ** zOrigVfsName==NULL.
  718. **
  719. ** The quota VFS shim is named "quota". It will become the default
  720. ** VFS if makeDefault is non-zero.
  721. **
  722. ** THIS ROUTINE IS NOT THREADSAFE. Call this routine exactly once
  723. ** during start-up.
  724. */
  725. int sqlite3_quota_initialize(const char *zOrigVfsName, int makeDefault){
  726. sqlite3_vfs *pOrigVfs;
  727. if( gQuota.isInitialized ) return SQLITE_MISUSE;
  728. pOrigVfs = sqlite3_vfs_find(zOrigVfsName);
  729. if( pOrigVfs==0 ) return SQLITE_ERROR;
  730. assert( pOrigVfs!=&gQuota.sThisVfs );
  731. gQuota.pMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
  732. if( !gQuota.pMutex ){
  733. return SQLITE_NOMEM;
  734. }
  735. gQuota.isInitialized = 1;
  736. gQuota.pOrigVfs = pOrigVfs;
  737. gQuota.sThisVfs = *pOrigVfs;
  738. gQuota.sThisVfs.xOpen = quotaOpen;
  739. gQuota.sThisVfs.xDelete = quotaDelete;
  740. gQuota.sThisVfs.szOsFile += sizeof(quotaConn);
  741. gQuota.sThisVfs.zName = "quota";
  742. gQuota.sIoMethodsV1.iVersion = 1;
  743. gQuota.sIoMethodsV1.xClose = quotaClose;
  744. gQuota.sIoMethodsV1.xRead = quotaRead;
  745. gQuota.sIoMethodsV1.xWrite = quotaWrite;
  746. gQuota.sIoMethodsV1.xTruncate = quotaTruncate;
  747. gQuota.sIoMethodsV1.xSync = quotaSync;
  748. gQuota.sIoMethodsV1.xFileSize = quotaFileSize;
  749. gQuota.sIoMethodsV1.xLock = quotaLock;
  750. gQuota.sIoMethodsV1.xUnlock = quotaUnlock;
  751. gQuota.sIoMethodsV1.xCheckReservedLock = quotaCheckReservedLock;
  752. gQuota.sIoMethodsV1.xFileControl = quotaFileControl;
  753. gQuota.sIoMethodsV1.xSectorSize = quotaSectorSize;
  754. gQuota.sIoMethodsV1.xDeviceCharacteristics = quotaDeviceCharacteristics;
  755. gQuota.sIoMethodsV2 = gQuota.sIoMethodsV1;
  756. gQuota.sIoMethodsV2.iVersion = 2;
  757. gQuota.sIoMethodsV2.xShmMap = quotaShmMap;
  758. gQuota.sIoMethodsV2.xShmLock = quotaShmLock;
  759. gQuota.sIoMethodsV2.xShmBarrier = quotaShmBarrier;
  760. gQuota.sIoMethodsV2.xShmUnmap = quotaShmUnmap;
  761. sqlite3_vfs_register(&gQuota.sThisVfs, makeDefault);
  762. return SQLITE_OK;
  763. }
  764. /*
  765. ** Shutdown the quota system.
  766. **
  767. ** All SQLite database connections must be closed before calling this
  768. ** routine.
  769. **
  770. ** THIS ROUTINE IS NOT THREADSAFE. Call this routine exactly once while
  771. ** shutting down in order to free all remaining quota groups.
  772. */
  773. int sqlite3_quota_shutdown(void){
  774. quotaGroup *pGroup;
  775. if( gQuota.isInitialized==0 ) return SQLITE_MISUSE;
  776. for(pGroup=gQuota.pGroup; pGroup; pGroup=pGroup->pNext){
  777. if( quotaGroupOpenFileCount(pGroup)>0 ) return SQLITE_MISUSE;
  778. }
  779. while( gQuota.pGroup ){
  780. pGroup = gQuota.pGroup;
  781. gQuota.pGroup = pGroup->pNext;
  782. pGroup->iLimit = 0;
  783. assert( quotaGroupOpenFileCount(pGroup)==0 );
  784. quotaGroupDeref(pGroup);
  785. }
  786. gQuota.isInitialized = 0;
  787. sqlite3_mutex_free(gQuota.pMutex);
  788. sqlite3_vfs_unregister(&gQuota.sThisVfs);
  789. memset(&gQuota, 0, sizeof(gQuota));
  790. return SQLITE_OK;
  791. }
  792. /*
  793. ** Create or destroy a quota group.
  794. **
  795. ** The quota group is defined by the zPattern. When calling this routine
  796. ** with a zPattern for a quota group that already exists, this routine
  797. ** merely updates the iLimit, xCallback, and pArg values for that quota
  798. ** group. If zPattern is new, then a new quota group is created.
  799. **
  800. ** If the iLimit for a quota group is set to zero, then the quota group
  801. ** is disabled and will be deleted when the last database connection using
  802. ** the quota group is closed.
  803. **
  804. ** Calling this routine on a zPattern that does not exist and with a
  805. ** zero iLimit is a no-op.
  806. **
  807. ** A quota group must exist with a non-zero iLimit prior to opening
  808. ** database connections if those connections are to participate in the
  809. ** quota group. Creating a quota group does not affect database connections
  810. ** that are already open.
  811. */
  812. int sqlite3_quota_set(
  813. const char *zPattern, /* The filename pattern */
  814. sqlite3_int64 iLimit, /* New quota to set for this quota group */
  815. void (*xCallback)( /* Callback invoked when going over quota */
  816. const char *zFilename, /* Name of file whose size increases */
  817. sqlite3_int64 *piLimit, /* IN/OUT: The current limit */
  818. sqlite3_int64 iSize, /* Total size of all files in the group */
  819. void *pArg /* Client data */
  820. ),
  821. void *pArg, /* client data passed thru to callback */
  822. void (*xDestroy)(void*) /* Optional destructor for pArg */
  823. ){
  824. quotaGroup *pGroup;
  825. quotaEnter();
  826. pGroup = gQuota.pGroup;
  827. while( pGroup && strcmp(pGroup->zPattern, zPattern)!=0 ){
  828. pGroup = pGroup->pNext;
  829. }
  830. if( pGroup==0 ){
  831. int nPattern = (int)(strlen(zPattern) & 0x3fffffff);
  832. if( iLimit<=0 ){
  833. quotaLeave();
  834. return SQLITE_OK;
  835. }
  836. pGroup = (quotaGroup *)sqlite3_malloc( sizeof(*pGroup) + nPattern + 1 );
  837. if( pGroup==0 ){
  838. quotaLeave();
  839. return SQLITE_NOMEM;
  840. }
  841. memset(pGroup, 0, sizeof(*pGroup));
  842. pGroup->zPattern = (char*)&pGroup[1];
  843. memcpy((char *)pGroup->zPattern, zPattern, nPattern+1);
  844. if( gQuota.pGroup ) gQuota.pGroup->ppPrev = &pGroup->pNext;
  845. pGroup->pNext = gQuota.pGroup;
  846. pGroup->ppPrev = &gQuota.pGroup;
  847. gQuota.pGroup = pGroup;
  848. }
  849. pGroup->iLimit = iLimit;
  850. pGroup->xCallback = xCallback;
  851. if( pGroup->xDestroy && pGroup->pArg!=pArg ){
  852. pGroup->xDestroy(pGroup->pArg);
  853. }
  854. pGroup->pArg = pArg;
  855. pGroup->xDestroy = xDestroy;
  856. quotaGroupDeref(pGroup);
  857. quotaLeave();
  858. return SQLITE_OK;
  859. }
  860. /*
  861. ** Bring the named file under quota management. Or if it is already under
  862. ** management, update its size.
  863. */
  864. int sqlite3_quota_file(const char *zFilename){
  865. char *zFull;
  866. sqlite3_file *fd;
  867. int rc;
  868. int outFlags = 0;
  869. sqlite3_int64 iSize;
  870. int nAlloc = gQuota.sThisVfs.szOsFile + gQuota.sThisVfs.mxPathname+2;
  871. /* Allocate space for a file-handle and the full path for file zFilename */
  872. fd = (sqlite3_file *)sqlite3_malloc(nAlloc);
  873. if( fd==0 ){
  874. rc = SQLITE_NOMEM;
  875. }else{
  876. zFull = &((char *)fd)[gQuota.sThisVfs.szOsFile];
  877. rc = gQuota.pOrigVfs->xFullPathname(gQuota.pOrigVfs, zFilename,
  878. gQuota.sThisVfs.mxPathname+1, zFull);
  879. }
  880. if( rc==SQLITE_OK ){
  881. zFull[strlen(zFull)+1] = '\0';
  882. rc = quotaOpen(&gQuota.sThisVfs, zFull, fd,
  883. SQLITE_OPEN_READONLY | SQLITE_OPEN_MAIN_DB, &outFlags);
  884. if( rc==SQLITE_OK ){
  885. fd->pMethods->xFileSize(fd, &iSize);
  886. fd->pMethods->xClose(fd);
  887. }else if( rc==SQLITE_CANTOPEN ){
  888. quotaGroup *pGroup;
  889. quotaFile *pFile;
  890. quotaEnter();
  891. pGroup = quotaGroupFind(zFull);
  892. if( pGroup ){
  893. pFile = quotaFindFile(pGroup, zFull, 0);
  894. if( pFile ) quotaRemoveFile(pFile);
  895. }
  896. quotaLeave();
  897. }
  898. }
  899. sqlite3_free(fd);
  900. return rc;
  901. }
  902. /*
  903. ** Open a potentially quotaed file for I/O.
  904. */
  905. quota_FILE *sqlite3_quota_fopen(const char *zFilename, const char *zMode){
  906. quota_FILE *p = 0;
  907. char *zFull = 0;
  908. char *zFullTranslated = 0;
  909. int rc;
  910. quotaGroup *pGroup;
  911. quotaFile *pFile;
  912. zFull = (char*)sqlite3_malloc(gQuota.sThisVfs.mxPathname + 1);
  913. if( zFull==0 ) return 0;
  914. rc = gQuota.pOrigVfs->xFullPathname(gQuota.pOrigVfs, zFilename,
  915. gQuota.sThisVfs.mxPathname+1, zFull);
  916. if( rc ) goto quota_fopen_error;
  917. p = (quota_FILE*)sqlite3_malloc(sizeof(*p));
  918. if( p==0 ) goto quota_fopen_error;
  919. memset(p, 0, sizeof(*p));
  920. zFullTranslated = quota_utf8_to_mbcs(zFull);
  921. if( zFullTranslated==0 ) goto quota_fopen_error;
  922. p->f = fopen(zFullTranslated, zMode);
  923. if( p->f==0 ) goto quota_fopen_error;
  924. quotaEnter();
  925. pGroup = quotaGroupFind(zFull);
  926. if( pGroup ){
  927. pFile = quotaFindFile(pGroup, zFull, 1);
  928. if( pFile==0 ){
  929. quotaLeave();
  930. goto quota_fopen_error;
  931. }
  932. pFile->nRef++;
  933. p->pFile = pFile;
  934. }
  935. quotaLeave();
  936. sqlite3_free(zFull);
  937. #if SQLITE_OS_WIN
  938. p->zMbcsName = zFullTranslated;
  939. #endif
  940. return p;
  941. quota_fopen_error:
  942. quota_mbcs_free(zFullTranslated);
  943. sqlite3_free(zFull);
  944. if( p && p->f ) fclose(p->f);
  945. sqlite3_free(p);
  946. return 0;
  947. }
  948. /*
  949. ** Read content from a quota_FILE
  950. */
  951. size_t sqlite3_quota_fread(
  952. void *pBuf, /* Store the content here */
  953. size_t size, /* Size of each element */
  954. size_t nmemb, /* Number of elements to read */
  955. quota_FILE *p /* Read from this quota_FILE object */
  956. ){
  957. return fread(pBuf, size, nmemb, p->f);
  958. }
  959. /*
  960. ** Write content into a quota_FILE. Invoke the quota callback and block
  961. ** the write if we exceed quota.
  962. */
  963. size_t sqlite3_quota_fwrite(
  964. const void *pBuf, /* Take content to write from here */
  965. size_t size, /* Size of each element */
  966. size_t nmemb, /* Number of elements */
  967. quota_FILE *p /* Write to this quota_FILE objecct */
  968. ){
  969. sqlite3_int64 iOfst;
  970. sqlite3_int64 iEnd;
  971. sqlite3_int64 szNew;
  972. quotaFile *pFile;
  973. size_t rc;
  974. iOfst = ftell(p->f);
  975. iEnd = iOfst + size*nmemb;
  976. pFile = p->pFile;
  977. if( pFile && pFile->iSize<iEnd ){
  978. quotaGroup *pGroup = pFile->pGroup;
  979. quotaEnter();
  980. szNew = pGroup->iSize - pFile->iSize + iEnd;
  981. if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){
  982. if( pGroup->xCallback ){
  983. pGroup->xCallback(pFile->zFilename, &pGroup->iLimit, szNew,
  984. pGroup->pArg);
  985. }
  986. if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){
  987. iEnd = pGroup->iLimit - pGroup->iSize + pFile->iSize;
  988. nmemb = (size_t)((iEnd - iOfst)/size);
  989. iEnd = iOfst + size*nmemb;
  990. szNew = pGroup->iSize - pFile->iSize + iEnd;
  991. }
  992. }
  993. pGroup->iSize = szNew;
  994. pFile->iSize = iEnd;
  995. quotaLeave();
  996. }else{
  997. pFile = 0;
  998. }
  999. rc = fwrite(pBuf, size, nmemb, p->f);
  1000. /* If the write was incomplete, adjust the file size and group size
  1001. ** downward */
  1002. if( rc<nmemb && pFile ){
  1003. size_t nWritten = rc;
  1004. sqlite3_int64 iNewEnd = iOfst + size*nWritten;
  1005. if( iNewEnd<iEnd ) iNewEnd = iEnd;
  1006. quotaEnter();
  1007. pFile->pGroup->iSize += iNewEnd - pFile->iSize;
  1008. pFile->iSize = iNewEnd;
  1009. quotaLeave();
  1010. }
  1011. return rc;
  1012. }
  1013. /*
  1014. ** Close an open quota_FILE stream.
  1015. */
  1016. int sqlite3_quota_fclose(quota_FILE *p){
  1017. int rc;
  1018. quotaFile *pFile;
  1019. rc = fclose(p->f);
  1020. pFile = p->pFile;
  1021. if( pFile ){
  1022. quotaEnter();
  1023. pFile->nRef--;
  1024. if( pFile->nRef==0 ){
  1025. quotaGroup *pGroup = pFile->pGroup;
  1026. if( pFile->deleteOnClose ){
  1027. gQuota.pOrigVfs->xDelete(gQuota.pOrigVfs, pFile->zFilename, 0);
  1028. quotaRemoveFile(pFile);
  1029. }
  1030. quotaGroupDeref(pGroup);
  1031. }
  1032. quotaLeave();
  1033. }
  1034. #if SQLITE_OS_WIN
  1035. quota_mbcs_free(p->zMbcsName);
  1036. #endif
  1037. sqlite3_free(p);
  1038. return rc;
  1039. }
  1040. /*
  1041. ** Flush memory buffers for a quota_FILE to disk.
  1042. */
  1043. int sqlite3_quota_fflush(quota_FILE *p, int doFsync){
  1044. int rc;
  1045. rc = fflush(p->f);
  1046. if( rc==0 && doFsync ){
  1047. #if SQLITE_OS_UNIX
  1048. rc = fsync(fileno(p->f));
  1049. #endif
  1050. #if SQLITE_OS_WIN
  1051. rc = _commit(_fileno(p->f));
  1052. #endif
  1053. }
  1054. return rc!=0;
  1055. }
  1056. /*
  1057. ** Seek on a quota_FILE stream.
  1058. */
  1059. int sqlite3_quota_fseek(quota_FILE *p, long offset, int whence){
  1060. return fseek(p->f, offset, whence);
  1061. }
  1062. /*
  1063. ** rewind a quota_FILE stream.
  1064. */
  1065. void sqlite3_quota_rewind(quota_FILE *p){
  1066. rewind(p->f);
  1067. }
  1068. /*
  1069. ** Tell the current location of a quota_FILE stream.
  1070. */
  1071. long sqlite3_quota_ftell(quota_FILE *p){
  1072. return ftell(p->f);
  1073. }
  1074. /*
  1075. ** Test the error indicator for the given file.
  1076. */
  1077. int sqlite3_quota_ferror(quota_FILE *p){
  1078. return ferror(p->f);
  1079. }
  1080. /*
  1081. ** Truncate a file to szNew bytes.
  1082. */
  1083. int sqlite3_quota_ftruncate(quota_FILE *p, sqlite3_int64 szNew){
  1084. quotaFile *pFile = p->pFile;
  1085. int rc;
  1086. if( (pFile = p->pFile)!=0 && pFile->iSize<szNew ){
  1087. quotaGroup *pGroup;
  1088. if( pFile->iSize<szNew ){
  1089. /* This routine cannot be used to extend a file that is under
  1090. ** quota management. Only true truncation is allowed. */
  1091. return -1;
  1092. }
  1093. pGroup = pFile->pGroup;
  1094. quotaEnter();
  1095. pGroup->iSize += szNew - pFile->iSize;
  1096. quotaLeave();
  1097. }
  1098. #if SQLITE_OS_UNIX
  1099. rc = ftruncate(fileno(p->f), szNew);
  1100. #endif
  1101. #if SQLITE_OS_WIN
  1102. # if defined(__MINGW32__) && defined(SQLITE_TEST)
  1103. /* _chsize_s() is missing from MingW (as of 2012-11-06). Use
  1104. ** _chsize() as a work-around for testing purposes. */
  1105. rc = _chsize(_fileno(p->f), (long)szNew);
  1106. # else
  1107. rc = _chsize_s(_fileno(p->f), szNew);
  1108. # endif
  1109. #endif
  1110. if( pFile && rc==0 ){
  1111. quotaGroup *pGroup = pFile->pGroup;
  1112. quotaEnter();
  1113. pGroup->iSize += szNew - pFile->iSize;
  1114. pFile->iSize = szNew;
  1115. quotaLeave();
  1116. }
  1117. return rc;
  1118. }
  1119. /*
  1120. ** Determine the time that the given file was last modified, in
  1121. ** seconds size 1970. Write the result into *pTime. Return 0 on
  1122. ** success and non-zero on any kind of error.
  1123. */
  1124. int sqlite3_quota_file_mtime(quota_FILE *p, time_t *pTime){
  1125. int rc;
  1126. #if SQLITE_OS_UNIX
  1127. struct stat buf;
  1128. rc = fstat(fileno(p->f), &buf);
  1129. #endif
  1130. #if SQLITE_OS_WIN
  1131. struct _stati64 buf;
  1132. rc = _stati64(p->zMbcsName, &buf);
  1133. #endif
  1134. if( rc==0 ) *pTime = buf.st_mtime;
  1135. return rc;
  1136. }
  1137. /*
  1138. ** Return the true size of the file, as reported by the operating
  1139. ** system.
  1140. */
  1141. sqlite3_int64 sqlite3_quota_file_truesize(quota_FILE *p){
  1142. int rc;
  1143. #if SQLITE_OS_UNIX
  1144. struct stat buf;
  1145. rc = fstat(fileno(p->f), &buf);
  1146. #endif
  1147. #if SQLITE_OS_WIN
  1148. struct _stati64 buf;
  1149. rc = _stati64(p->zMbcsName, &buf);
  1150. #endif
  1151. return rc==0 ? buf.st_size : -1;
  1152. }
  1153. /*
  1154. ** Return the size of the file, as it is known to the quota subsystem.
  1155. */
  1156. sqlite3_int64 sqlite3_quota_file_size(quota_FILE *p){
  1157. return p->pFile ? p->pFile->iSize : -1;
  1158. }
  1159. /*
  1160. ** Determine the amount of data in bytes available for reading
  1161. ** in the given file.
  1162. */
  1163. long sqlite3_quota_file_available(quota_FILE *p){
  1164. FILE* f = p->f;
  1165. long pos1, pos2;
  1166. int rc;
  1167. pos1 = ftell(f);
  1168. if ( pos1 < 0 ) return -1;
  1169. rc = fseek(f, 0, SEEK_END);
  1170. if ( rc != 0 ) return -1;
  1171. pos2 = ftell(f);
  1172. if ( pos2 < 0 ) return -1;
  1173. rc = fseek(f, pos1, SEEK_SET);
  1174. if ( rc != 0 ) return -1;
  1175. return pos2 - pos1;
  1176. }
  1177. /*
  1178. ** Remove a managed file. Update quotas accordingly.
  1179. */
  1180. int sqlite3_quota_remove(const char *zFilename){
  1181. char *zFull; /* Full pathname for zFilename */
  1182. size_t nFull; /* Number of bytes in zFilename */
  1183. int rc; /* Result code */
  1184. quotaGroup *pGroup; /* Group containing zFilename */
  1185. quotaFile *pFile; /* A file in the group */
  1186. quotaFile *pNextFile; /* next file in the group */
  1187. int diff; /* Difference between filenames */
  1188. char c; /* First character past end of pattern */
  1189. zFull = (char*)sqlite3_malloc(gQuota.sThisVfs.mxPathname + 1);
  1190. if( zFull==0 ) return SQLITE_NOMEM;
  1191. rc = gQuota.pOrigVfs->xFullPathname(gQuota.pOrigVfs, zFilename,
  1192. gQuota.sThisVfs.mxPathname+1, zFull);
  1193. if( rc ){
  1194. sqlite3_free(zFull);
  1195. return rc;
  1196. }
  1197. /* Figure out the length of the full pathname. If the name ends with
  1198. ** / (or \ on windows) then remove the trailing /.
  1199. */
  1200. nFull = strlen(zFull);
  1201. if( nFull>0 && (zFull[nFull-1]=='/' || zFull[nFull-1]=='\\') ){
  1202. nFull--;
  1203. zFull[nFull] = 0;
  1204. }
  1205. quotaEnter();
  1206. pGroup = quotaGroupFind(zFull);
  1207. if( pGroup ){
  1208. for(pFile=pGroup->pFiles; pFile && rc==SQLITE_OK; pFile=pNextFile){
  1209. pNextFile = pFile->pNext;
  1210. diff = strncmp(zFull, pFile->zFilename, nFull);
  1211. if( diff==0 && ((c = pFile->zFilename[nFull])==0 || c=='/' || c=='\\') ){
  1212. if( pFile->nRef ){
  1213. pFile->deleteOnClose = 1;
  1214. }else{
  1215. rc = gQuota.pOrigVfs->xDelete(gQuota.pOrigVfs, pFile->zFilename, 0);
  1216. quotaRemoveFile(pFile);
  1217. quotaGroupDeref(pGroup);
  1218. }
  1219. }
  1220. }
  1221. }
  1222. quotaLeave();
  1223. sqlite3_free(zFull);
  1224. return rc;
  1225. }
  1226. /***************************** Test Code ***********************************/
  1227. #ifdef SQLITE_TEST
  1228. #include <tcl.h>
  1229. /*
  1230. ** Argument passed to a TCL quota-over-limit callback.
  1231. */
  1232. typedef struct TclQuotaCallback TclQuotaCallback;
  1233. struct TclQuotaCallback {
  1234. Tcl_Interp *interp; /* Interpreter in which to run the script */
  1235. Tcl_Obj *pScript; /* Script to be run */
  1236. };
  1237. extern const char *sqlite3ErrName(int);
  1238. /*
  1239. ** This is the callback from a quota-over-limit.
  1240. */
  1241. static void tclQuotaCallback(
  1242. const char *zFilename, /* Name of file whose size increases */
  1243. sqlite3_int64 *piLimit, /* IN/OUT: The current limit */
  1244. sqlite3_int64 iSize, /* Total size of all files in the group */
  1245. void *pArg /* Client data */
  1246. ){
  1247. TclQuotaCallback *p; /* Callback script object */
  1248. Tcl_Obj *pEval; /* Script to evaluate */
  1249. Tcl_Obj *pVarname; /* Name of variable to pass as 2nd arg */
  1250. unsigned int rnd; /* Random part of pVarname */
  1251. int rc; /* Tcl error code */
  1252. p = (TclQuotaCallback *)pArg;
  1253. if( p==0 ) return;
  1254. pVarname = Tcl_NewStringObj("::piLimit_", -1);
  1255. Tcl_IncrRefCount(pVarname);
  1256. sqlite3_randomness(sizeof(rnd), (void *)&rnd);
  1257. Tcl_AppendObjToObj(pVarname, Tcl_NewIntObj((int)(rnd&0x7FFFFFFF)));
  1258. Tcl_ObjSetVar2(p->interp, pVarname, 0, Tcl_NewWideIntObj(*piLimit), 0);
  1259. pEval = Tcl_DuplicateObj(p->pScript);
  1260. Tcl_IncrRefCount(pEval);
  1261. Tcl_ListObjAppendElement(0, pEval, Tcl_NewStringObj(zFilename, -1));
  1262. Tcl_ListObjAppendElement(0, pEval, pVarname);
  1263. Tcl_ListObjAppendElement(0, pEval, Tcl_NewWideIntObj(iSize));
  1264. rc = Tcl_EvalObjEx(p->interp, pEval, TCL_EVAL_GLOBAL);
  1265. if( rc==TCL_OK ){
  1266. Tcl_WideInt x;
  1267. Tcl_Obj *pLimit = Tcl_ObjGetVar2(p->interp, pVarname, 0, 0);
  1268. rc = Tcl_GetWideIntFromObj(p->interp, pLimit, &x);
  1269. *piLimit = x;
  1270. Tcl_UnsetVar(p->interp, Tcl_GetString(pVarname), 0);
  1271. }
  1272. Tcl_DecrRefCount(pEval);
  1273. Tcl_DecrRefCount(pVarname);
  1274. if( rc!=TCL_OK ) Tcl_BackgroundError(p->interp);
  1275. }
  1276. /*
  1277. ** Destructor for a TCL quota-over-limit callback.
  1278. */
  1279. static void tclCallbackDestructor(void *pObj){
  1280. TclQuotaCallback *p = (TclQuotaCallback*)pObj;
  1281. if( p ){
  1282. Tcl_DecrRefCount(p->pScript);
  1283. sqlite3_free((char *)p);
  1284. }
  1285. }
  1286. /*
  1287. ** tclcmd: sqlite3_quota_initialize NAME MAKEDEFAULT
  1288. */
  1289. static int test_quota_initialize(
  1290. void * clientData,
  1291. Tcl_Interp *interp,
  1292. int objc,
  1293. Tcl_Obj *CONST objv[]
  1294. ){
  1295. const char *zName; /* Name of new quota VFS */
  1296. int makeDefault; /* True to make the new VFS the default */
  1297. int rc; /* Value returned by quota_initialize() */
  1298. /* Process arguments */
  1299. if( objc!=3 ){
  1300. Tcl_WrongNumArgs(interp, 1, objv, "NAME MAKEDEFAULT");
  1301. return TCL_ERROR;
  1302. }
  1303. zName = Tcl_GetString(objv[1]);
  1304. if( Tcl_GetBooleanFromObj(interp, objv[2], &makeDefault) ) return TCL_ERROR;
  1305. if( zName[0]=='\0' ) zName = 0;
  1306. /* Call sqlite3_quota_initialize() */
  1307. rc = sqlite3_quota_initialize(zName, makeDefault);
  1308. Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
  1309. return TCL_OK;
  1310. }
  1311. /*
  1312. ** tclcmd: sqlite3_quota_shutdown
  1313. */
  1314. static int test_quota_shutdown(
  1315. void * clientData,
  1316. Tcl_Interp *interp,
  1317. int objc,
  1318. Tcl_Obj *CONST objv[]
  1319. ){
  1320. int rc; /* Value returned by quota_shutdown() */
  1321. if( objc!=1 ){
  1322. Tcl_WrongNumArgs(interp, 1, objv, "");
  1323. return TCL_ERROR;
  1324. }
  1325. /* Call sqlite3_quota_shutdown() */
  1326. rc = sqlite3_quota_shutdown();
  1327. Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
  1328. return TCL_OK;
  1329. }
  1330. /*
  1331. ** tclcmd: sqlite3_quota_set PATTERN LIMIT SCRIPT
  1332. */
  1333. static int test_quota_set(
  1334. void * clientData,
  1335. Tcl_Interp *interp,
  1336. int objc,
  1337. Tcl_Obj *CONST objv[]
  1338. ){
  1339. const char *zPattern; /* File pattern to configure */
  1340. Tcl_WideInt iLimit; /* Initial quota in bytes */
  1341. Tcl_Obj *pScript; /* Tcl script to invoke to increase quota */
  1342. int rc; /* Value returned by quota_set() */
  1343. TclQuotaCallback *p; /* Callback object */
  1344. int nScript; /* Length of callback script */
  1345. void (*xDestroy)(void*); /* Optional destructor for pArg */
  1346. void (*xCallback)(const char *, sqlite3_int64 *, sqlite3_int64, void *);
  1347. /* Process arguments */
  1348. if( objc!=4 ){
  1349. Tcl_WrongNumArgs(interp, 1, objv, "PATTERN LIMIT SCRIPT");
  1350. return TCL_ERROR;
  1351. }
  1352. zPattern = Tcl_GetString(objv[1]);
  1353. if( Tcl_GetWideIntFromObj(interp, objv[2], &iLimit) ) return TCL_ERROR;
  1354. pScript = objv[3];
  1355. Tcl_GetStringFromObj(pScript, &nScript);
  1356. if( nScript>0 ){
  1357. /* Allocate a TclQuotaCallback object */
  1358. p = (TclQuotaCallback *)sqlite3_malloc(sizeof(TclQuotaCallback));
  1359. if( !p ){
  1360. Tcl_SetResult(interp, (char *)"SQLITE_NOMEM", TCL_STATIC);
  1361. return TCL_OK;
  1362. }
  1363. memset(p, 0, sizeof(TclQuotaCallback));
  1364. p->interp = interp;
  1365. Tcl_IncrRefCount(pScript);
  1366. p->pScript = pScript;
  1367. xDestroy = tclCallbackDestructor;
  1368. xCallback = tclQuotaCallback;
  1369. }else{
  1370. p = 0;
  1371. xDestroy = 0;
  1372. xCallback = 0;
  1373. }
  1374. /* Invoke sqlite3_quota_set() */
  1375. rc = sqlite3_quota_set(zPattern, iLimit, xCallback, (void*)p, xDestroy);
  1376. Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
  1377. return TCL_OK;
  1378. }
  1379. /*
  1380. ** tclcmd: sqlite3_quota_file FILENAME
  1381. */
  1382. static int test_quota_file(
  1383. void * clientData,
  1384. Tcl_Interp *interp,
  1385. int objc,
  1386. Tcl_Obj *CONST objv[]
  1387. ){
  1388. const char *zFilename; /* File pattern to configure */
  1389. int rc; /* Value returned by quota_file() */
  1390. /* Process arguments */
  1391. if( objc!=2 ){
  1392. Tcl_WrongNumArgs(interp, 1, objv, "FILENAME");
  1393. return TCL_ERROR;
  1394. }
  1395. zFilename = Tcl_GetString(objv[1]);
  1396. /* Invoke sqlite3_quota_file() */
  1397. rc = sqlite3_quota_file(zFilename);
  1398. Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
  1399. return TCL_OK;
  1400. }
  1401. /*
  1402. ** tclcmd: sqlite3_quota_dump
  1403. */
  1404. static int test_quota_dump(
  1405. void * clientData,
  1406. Tcl_Interp *interp,
  1407. int objc,
  1408. Tcl_Obj *CONST objv[]
  1409. ){
  1410. Tcl_Obj *pResult;
  1411. Tcl_Obj *pGroupTerm;
  1412. Tcl_Obj *pFileTerm;
  1413. quotaGroup *pGroup;
  1414. quotaFile *pFile;
  1415. pResult = Tcl_NewObj();
  1416. quotaEnter();
  1417. for(pGroup=gQuota.pGroup; pGroup; pGroup=pGroup->pNext){
  1418. pGroupTerm = Tcl_NewObj();
  1419. Tcl_ListObjAppendElement(interp, pGroupTerm,
  1420. Tcl_NewStringObj(pGroup->zPattern, -1));
  1421. Tcl_ListObjAppendElement(interp, pGroupTerm,
  1422. Tcl_NewWideIntObj(pGroup->iLimit));
  1423. Tcl_ListObjAppendElement(interp, pGroupTerm,
  1424. Tcl_NewWideIntObj(pGroup->iSize));
  1425. for(pFile=pGroup->pFiles; pFile; pFile=pFile->pNext){
  1426. int i;
  1427. char zTemp[1000];
  1428. pFileTerm = Tcl_NewObj();
  1429. sqlite3_snprintf(sizeof(zTemp), zTemp, "%s", pFile->zFilename);
  1430. for(i=0; zTemp[i]; i++){ if( zTemp[i]=='\\' ) zTemp[i] = '/'; }
  1431. Tcl_ListObjAppendElement(interp, pFileTerm,
  1432. Tcl_NewStringObj(zTemp, -1));
  1433. Tcl_ListObjAppendElement(interp, pFileTerm,
  1434. Tcl_NewWideIntObj(pFile->iSize));
  1435. Tcl_ListObjAppendElement(interp, pFileTerm,
  1436. Tcl_NewWideIntObj(pFile->nRef));
  1437. Tcl_ListObjAppendElement(interp, pFileTerm,
  1438. Tcl_NewWideIntObj(pFile->deleteOnClose));
  1439. Tcl_ListObjAppendElement(interp, pGroupTerm, pFileTerm);
  1440. }
  1441. Tcl_ListObjAppendElement(interp, pResult, pGroupTerm);
  1442. }
  1443. quotaLeave();
  1444. Tcl_SetObjResult(interp, pResult);
  1445. return TCL_OK;
  1446. }
  1447. /*
  1448. ** tclcmd: sqlite3_quota_fopen FILENAME MODE
  1449. */
  1450. static int test_quota_fopen(
  1451. void * clientData,
  1452. Tcl_Interp *interp,
  1453. int objc,
  1454. Tcl_Obj *CONST objv[]
  1455. ){
  1456. const char *zFilename; /* File pattern to configure */
  1457. const char *zMode; /* Mode string */
  1458. quota_FILE *p; /* Open string object */
  1459. char zReturn[50]; /* Name of pointer to return */
  1460. /* Process arguments */
  1461. if( objc!=3 ){
  1462. Tcl_WrongNumArgs(interp, 1, objv, "FILENAME MODE");
  1463. return TCL_ERROR;
  1464. }
  1465. zFilename = Tcl_GetString(objv[1]);
  1466. zMode = Tcl_GetString(objv[2]);
  1467. p = sqlite3_quota_fopen(zFilename, zMode);
  1468. sqlite3_snprintf(sizeof(zReturn), zReturn, "%p", p);
  1469. Tcl_SetResult(interp, zReturn, TCL_VOLATILE);
  1470. return TCL_OK;
  1471. }
  1472. /* Defined in test1.c */
  1473. extern void *sqlite3TestTextToPtr(const char*);
  1474. /*
  1475. ** tclcmd: sqlite3_quota_fread HANDLE SIZE NELEM
  1476. */
  1477. static int test_quota_fread(
  1478. void * clientData,
  1479. Tcl_Interp *interp,
  1480. int objc,
  1481. Tcl_Obj *CONST objv[]
  1482. ){
  1483. quota_FILE *p;
  1484. char *zBuf;
  1485. int sz;
  1486. int nElem;
  1487. size_t got;
  1488. if( objc!=4 ){
  1489. Tcl_WrongNumArgs(interp, 1, objv, "HANDLE SIZE NELEM");
  1490. return TCL_ERROR;
  1491. }
  1492. p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
  1493. if( Tcl_GetIntFromObj(interp, objv[2], &sz) ) return TCL_ERROR;
  1494. if( Tcl_GetIntFromObj(interp, objv[3], &nElem) ) return TCL_ERROR;
  1495. zBuf = (char*)sqlite3_malloc( sz*nElem + 1 );
  1496. if( zBuf==0 ){
  1497. Tcl_SetResult(interp, "out of memory", TCL_STATIC);
  1498. return TCL_ERROR;
  1499. }
  1500. got = sqlite3_quota_fread(zBuf, sz, nElem, p);
  1501. zBuf[got*sz] = 0;
  1502. Tcl_SetResult(interp, zBuf, TCL_VOLATILE);
  1503. sqlite3_free(zBuf);
  1504. return TCL_OK;
  1505. }
  1506. /*
  1507. ** tclcmd: sqlite3_quota_fwrite HANDLE SIZE NELEM CONTENT
  1508. */
  1509. static int test_quota_fwrite(
  1510. void * clientData,
  1511. Tcl_Interp *interp,
  1512. int objc,
  1513. Tcl_Obj *CONST objv[]
  1514. ){
  1515. quota_FILE *p;
  1516. char *zBuf;
  1517. int sz;
  1518. int nElem;
  1519. size_t got;
  1520. if( objc!=5 ){
  1521. Tcl_WrongNumArgs(interp, 1, objv, "HANDLE SIZE NELEM CONTENT");
  1522. return TCL_ERROR;
  1523. }
  1524. p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
  1525. if( Tcl_GetIntFromObj(interp, objv[2], &sz) ) return TCL_ERROR;
  1526. if( Tcl_GetIntFromObj(interp, objv[3], &nElem) ) return TCL_ERROR;
  1527. zBuf = Tcl_GetString(objv[4]);
  1528. got = sqlite3_quota_fwrite(zBuf, sz, nElem, p);
  1529. Tcl_SetObjResult(interp, Tcl_NewWideIntObj(got));
  1530. return TCL_OK;
  1531. }
  1532. /*
  1533. ** tclcmd: sqlite3_quota_fclose HANDLE
  1534. */
  1535. static int test_quota_fclose(
  1536. void * clientData,
  1537. Tcl_Interp *interp,
  1538. int objc,
  1539. Tcl_Obj *CONST objv[]
  1540. ){
  1541. quota_FILE *p;
  1542. int rc;
  1543. if( objc!=2 ){
  1544. Tcl_WrongNumArgs(interp, 1, objv, "HANDLE");
  1545. return TCL_ERROR;
  1546. }
  1547. p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
  1548. rc = sqlite3_quota_fclose(p);
  1549. Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
  1550. return TCL_OK;
  1551. }
  1552. /*
  1553. ** tclcmd: sqlite3_quota_fflush HANDLE ?HARDSYNC?
  1554. */
  1555. static int test_quota_fflush(
  1556. void * clientData,
  1557. Tcl_Interp *interp,
  1558. int objc,
  1559. Tcl_Obj *CONST objv[]
  1560. ){
  1561. quota_FILE *p;
  1562. int rc;
  1563. int doSync = 0;
  1564. if( objc!=2 && objc!=3 ){
  1565. Tcl_WrongNumArgs(interp, 1, objv, "HANDLE ?HARDSYNC?");
  1566. return TCL_ERROR;
  1567. }
  1568. p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
  1569. if( objc==3 ){
  1570. if( Tcl_GetBooleanFromObj(interp, objv[2], &doSync) ) return TCL_ERROR;
  1571. }
  1572. rc = sqlite3_quota_fflush(p, doSync);
  1573. Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
  1574. return TCL_OK;
  1575. }
  1576. /*
  1577. ** tclcmd: sqlite3_quota_fseek HANDLE OFFSET WHENCE
  1578. */
  1579. static int test_quota_fseek(
  1580. void * clientData,
  1581. Tcl_Interp *interp,
  1582. int objc,
  1583. Tcl_Obj *CONST objv[]
  1584. ){
  1585. quota_FILE *p;
  1586. int ofst;
  1587. const char *zWhence;
  1588. int whence;
  1589. int rc;
  1590. if( objc!=4 ){
  1591. Tcl_WrongNumArgs(interp, 1, objv, "HANDLE OFFSET WHENCE");
  1592. return TCL_ERROR;
  1593. }
  1594. p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
  1595. if( Tcl_GetIntFromObj(interp, objv[2], &ofst) ) return TCL_ERROR;
  1596. zWhence = Tcl_GetString(objv[3]);
  1597. if( strcmp(zWhence, "SEEK_SET")==0 ){
  1598. whence = SEEK_SET;
  1599. }else if( strcmp(zWhence, "SEEK_CUR")==0 ){
  1600. whence = SEEK_CUR;
  1601. }else if( strcmp(zWhence, "SEEK_END")==0 ){
  1602. whence = SEEK_END;
  1603. }else{
  1604. Tcl_AppendResult(interp,
  1605. "WHENCE should be SEEK_SET, SEEK_CUR, or SEEK_END", (char*)0);
  1606. return TCL_ERROR;
  1607. }
  1608. rc = sqlite3_quota_fseek(p, ofst, whence);
  1609. Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
  1610. return TCL_OK;
  1611. }
  1612. /*
  1613. ** tclcmd: sqlite3_quota_rewind HANDLE
  1614. */
  1615. static int test_quota_rewind(
  1616. void * clientData,
  1617. Tcl_Interp *interp,
  1618. int objc,
  1619. Tcl_Obj *CONST objv[]
  1620. ){
  1621. quota_FILE *p;
  1622. if( objc!=2 ){
  1623. Tcl_WrongNumArgs(interp, 1, objv, "HANDLE");
  1624. return TCL_ERROR;
  1625. }
  1626. p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
  1627. sqlite3_quota_rewind(p);
  1628. return TCL_OK;
  1629. }
  1630. /*
  1631. ** tclcmd: sqlite3_quota_ftell HANDLE
  1632. */
  1633. static int test_quota_ftell(
  1634. void * clientData,
  1635. Tcl_Interp *interp,
  1636. int objc,
  1637. Tcl_Obj *CONST objv[]
  1638. ){
  1639. quota_FILE *p;
  1640. sqlite3_int64 x;
  1641. if( objc!=2 ){
  1642. Tcl_WrongNumArgs(interp, 1, objv, "HANDLE");
  1643. return TCL_ERROR;
  1644. }
  1645. p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
  1646. x = sqlite3_quota_ftell(p);
  1647. Tcl_SetObjResult(interp, Tcl_NewWideIntObj(x));
  1648. return TCL_OK;
  1649. }
  1650. /*
  1651. ** tclcmd: sqlite3_quota_ftruncate HANDLE SIZE
  1652. */
  1653. static int test_quota_ftruncate(
  1654. void * clientData,
  1655. Tcl_Interp *interp,
  1656. int objc,
  1657. Tcl_Obj *CONST objv[]
  1658. ){
  1659. quota_FILE *p;
  1660. sqlite3_int64 x;
  1661. Tcl_WideInt w;
  1662. int rc;
  1663. if( objc!=3 ){
  1664. Tcl_WrongNumArgs(interp, 1, objv, "HANDLE SIZE");
  1665. return TCL_ERROR;
  1666. }
  1667. p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
  1668. if( Tcl_GetWideIntFromObj(interp, objv[2], &w) ) return TCL_ERROR;
  1669. x = (sqlite3_int64)w;
  1670. rc = sqlite3_quota_ftruncate(p, x);
  1671. Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
  1672. return TCL_OK;
  1673. }
  1674. /*
  1675. ** tclcmd: sqlite3_quota_file_size HANDLE
  1676. */
  1677. static int test_quota_file_size(
  1678. void * clientData,
  1679. Tcl_Interp *interp,
  1680. int objc,
  1681. Tcl_Obj *CONST objv[]
  1682. ){
  1683. quota_FILE *p;
  1684. sqlite3_int64 x;
  1685. if( objc!=2 ){
  1686. Tcl_WrongNumArgs(interp, 1, objv, "HANDLE");
  1687. return TCL_ERROR;
  1688. }
  1689. p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
  1690. x = sqlite3_quota_file_size(p);
  1691. Tcl_SetObjResult(interp, Tcl_NewWideIntObj(x));
  1692. return TCL_OK;
  1693. }
  1694. /*
  1695. ** tclcmd: sqlite3_quota_file_truesize HANDLE
  1696. */
  1697. static int test_quota_file_truesize(
  1698. void * clientData,
  1699. Tcl_Interp *interp,
  1700. int objc,
  1701. Tcl_Obj *CONST objv[]
  1702. ){
  1703. quota_FILE *p;
  1704. sqlite3_int64 x;
  1705. if( objc!=2 ){
  1706. Tcl_WrongNumArgs(interp, 1, objv, "HANDLE");
  1707. return TCL_ERROR;
  1708. }
  1709. p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
  1710. x = sqlite3_quota_file_truesize(p);
  1711. Tcl_SetObjResult(interp, Tcl_NewWideIntObj(x));
  1712. return TCL_OK;
  1713. }
  1714. /*
  1715. ** tclcmd: sqlite3_quota_file_mtime HANDLE
  1716. */
  1717. static int test_quota_file_mtime(
  1718. void * clientData,
  1719. Tcl_Interp *interp,
  1720. int objc,
  1721. Tcl_Obj *CONST objv[]
  1722. ){
  1723. quota_FILE *p;
  1724. time_t t;
  1725. if( objc!=2 ){
  1726. Tcl_WrongNumArgs(interp, 1, objv, "HANDLE");
  1727. return TCL_ERROR;
  1728. }
  1729. p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
  1730. t = 0;
  1731. sqlite3_quota_file_mtime(p, &t);
  1732. Tcl_SetObjResult(interp, Tcl_NewWideIntObj(t));
  1733. return TCL_OK;
  1734. }
  1735. /*
  1736. ** tclcmd: sqlite3_quota_remove FILENAME
  1737. */
  1738. static int test_quota_remove(
  1739. void * clientData,
  1740. Tcl_Interp *interp,
  1741. int objc,
  1742. Tcl_Obj *CONST objv[]
  1743. ){
  1744. const char *zFilename; /* File pattern to configure */
  1745. int rc;
  1746. if( objc!=2 ){
  1747. Tcl_WrongNumArgs(interp, 1, objv, "FILENAME");
  1748. return TCL_ERROR;
  1749. }
  1750. zFilename = Tcl_GetString(objv[1]);
  1751. rc = sqlite3_quota_remove(zFilename);
  1752. Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
  1753. return TCL_OK;
  1754. }
  1755. /*
  1756. ** tclcmd: sqlite3_quota_glob PATTERN TEXT
  1757. **
  1758. ** Test the glob pattern matching. Return 1 if TEXT matches PATTERN
  1759. ** and return 0 if it does not.
  1760. */
  1761. static int test_quota_glob(
  1762. void * clientData,
  1763. Tcl_Interp *interp,
  1764. int objc,
  1765. Tcl_Obj *CONST objv[]
  1766. ){
  1767. const char *zPattern; /* The glob pattern */
  1768. const char *zText; /* Text to compare agains the pattern */
  1769. int rc;
  1770. if( objc!=3 ){
  1771. Tcl_WrongNumArgs(interp, 1, objv, "PATTERN TEXT");
  1772. return TCL_ERROR;
  1773. }
  1774. zPattern = Tcl_GetString(objv[1]);
  1775. zText = Tcl_GetString(objv[2]);
  1776. rc = quotaStrglob(zPattern, zText);
  1777. Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
  1778. return TCL_OK;
  1779. }
  1780. /*
  1781. ** tclcmd: sqlite3_quota_file_available HANDLE
  1782. **
  1783. ** Return the number of bytes from the current file point to the end of
  1784. ** the file.
  1785. */
  1786. static int test_quota_file_available(
  1787. void * clientData,
  1788. Tcl_Interp *interp,
  1789. int objc,
  1790. Tcl_Obj *CONST objv[]
  1791. ){
  1792. quota_FILE *p;
  1793. sqlite3_int64 x;
  1794. if( objc!=2 ){
  1795. Tcl_WrongNumArgs(interp, 1, objv, "HANDLE");
  1796. return TCL_ERROR;
  1797. }
  1798. p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
  1799. x = sqlite3_quota_file_available(p);
  1800. Tcl_SetObjResult(interp, Tcl_NewWideIntObj(x));
  1801. return TCL_OK;
  1802. }
  1803. /*
  1804. ** tclcmd: sqlite3_quota_ferror HANDLE
  1805. **
  1806. ** Return true if the file handle is in the error state.
  1807. */
  1808. static int test_quota_ferror(
  1809. void * clientData,
  1810. Tcl_Interp *interp,
  1811. int objc,
  1812. Tcl_Obj *CONST objv[]
  1813. ){
  1814. quota_FILE *p;
  1815. int x;
  1816. if( objc!=2 ){
  1817. Tcl_WrongNumArgs(interp, 1, objv, "HANDLE");
  1818. return TCL_ERROR;
  1819. }
  1820. p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
  1821. x = sqlite3_quota_ferror(p);
  1822. Tcl_SetObjResult(interp, Tcl_NewIntObj(x));
  1823. return TCL_OK;
  1824. }
  1825. /*
  1826. ** This routine registers the custom TCL commands defined in this
  1827. ** module. This should be the only procedure visible from outside
  1828. ** of this module.
  1829. */
  1830. int Sqlitequota_Init(Tcl_Interp *interp){
  1831. static struct {
  1832. char *zName;
  1833. Tcl_ObjCmdProc *xProc;
  1834. } aCmd[] = {
  1835. { "sqlite3_quota_initialize", test_quota_initialize },
  1836. { "sqlite3_quota_shutdown", test_quota_shutdown },
  1837. { "sqlite3_quota_set", test_quota_set },
  1838. { "sqlite3_quota_file", test_quota_file },
  1839. { "sqlite3_quota_dump", test_quota_dump },
  1840. { "sqlite3_quota_fopen", test_quota_fopen },
  1841. { "sqlite3_quota_fread", test_quota_fread },
  1842. { "sqlite3_quota_fwrite", test_quota_fwrite },
  1843. { "sqlite3_quota_fclose", test_quota_fclose },
  1844. { "sqlite3_quota_fflush", test_quota_fflush },
  1845. { "sqlite3_quota_fseek", test_quota_fseek },
  1846. { "sqlite3_quota_rewind", test_quota_rewind },
  1847. { "sqlite3_quota_ftell", test_quota_ftell },
  1848. { "sqlite3_quota_ftruncate", test_quota_ftruncate },
  1849. { "sqlite3_quota_file_size", test_quota_file_size },
  1850. { "sqlite3_quota_file_truesize", test_quota_file_truesize },
  1851. { "sqlite3_quota_file_mtime", test_quota_file_mtime },
  1852. { "sqlite3_quota_remove", test_quota_remove },
  1853. { "sqlite3_quota_glob", test_quota_glob },
  1854. { "sqlite3_quota_file_available",test_quota_file_available },
  1855. { "sqlite3_quota_ferror", test_quota_ferror },
  1856. };
  1857. int i;
  1858. for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
  1859. Tcl_CreateObjCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0);
  1860. }
  1861. return TCL_OK;
  1862. }
  1863. #endif