test_quota.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. ** 2011 December 1
  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 the interface definition for the quota a VFS shim.
  14. **
  15. ** This particular shim enforces a quota system on files. One or more
  16. ** database files are in a "quota group" that is defined by a GLOB
  17. ** pattern. A quota is set for the combined size of all files in the
  18. ** the group. A quota of zero means "no limit". If the total size
  19. ** of all files in the quota group is greater than the limit, then
  20. ** write requests that attempt to enlarge a file fail with SQLITE_FULL.
  21. **
  22. ** However, before returning SQLITE_FULL, the write requests invoke
  23. ** a callback function that is configurable for each quota group.
  24. ** This callback has the opportunity to enlarge the quota. If the
  25. ** callback does enlarge the quota such that the total size of all
  26. ** files within the group is less than the new quota, then the write
  27. ** continues as if nothing had happened.
  28. */
  29. #ifndef _QUOTA_H_
  30. #include "sqlite3.h"
  31. #include <stdio.h>
  32. #include <sys/types.h>
  33. #include <sys/stat.h>
  34. #if SQLITE_OS_UNIX
  35. # include <unistd.h>
  36. #endif
  37. #if SQLITE_OS_WIN
  38. # include <windows.h>
  39. #endif
  40. /* Make this callable from C++ */
  41. #ifdef __cplusplus
  42. extern "C" {
  43. #endif
  44. /*
  45. ** Initialize the quota VFS shim. Use the VFS named zOrigVfsName
  46. ** as the VFS that does the actual work. Use the default if
  47. ** zOrigVfsName==NULL.
  48. **
  49. ** The quota VFS shim is named "quota". It will become the default
  50. ** VFS if makeDefault is non-zero.
  51. **
  52. ** THIS ROUTINE IS NOT THREADSAFE. Call this routine exactly once
  53. ** during start-up.
  54. */
  55. int sqlite3_quota_initialize(const char *zOrigVfsName, int makeDefault);
  56. /*
  57. ** Shutdown the quota system.
  58. **
  59. ** All SQLite database connections must be closed before calling this
  60. ** routine.
  61. **
  62. ** THIS ROUTINE IS NOT THREADSAFE. Call this routine exactly once while
  63. ** shutting down in order to free all remaining quota groups.
  64. */
  65. int sqlite3_quota_shutdown(void);
  66. /*
  67. ** Create or destroy a quota group.
  68. **
  69. ** The quota group is defined by the zPattern. When calling this routine
  70. ** with a zPattern for a quota group that already exists, this routine
  71. ** merely updates the iLimit, xCallback, and pArg values for that quota
  72. ** group. If zPattern is new, then a new quota group is created.
  73. **
  74. ** The zPattern is always compared against the full pathname of the file.
  75. ** Even if APIs are called with relative pathnames, SQLite converts the
  76. ** name to a full pathname before comparing it against zPattern. zPattern
  77. ** is a glob pattern with the following matching rules:
  78. **
  79. ** '*' Matches any sequence of zero or more characters.
  80. **
  81. ** '?' Matches exactly one character.
  82. **
  83. ** [...] Matches one character from the enclosed list of
  84. ** characters. "]" can be part of the list if it is
  85. ** the first character. Within the list "X-Y" matches
  86. ** characters X or Y or any character in between the
  87. ** two. Ex: "[0-9]" matches any digit.
  88. **
  89. ** [^...] Matches one character not in the enclosed list.
  90. **
  91. ** / Matches either / or \. This allows glob patterns
  92. ** containing / to work on both unix and windows.
  93. **
  94. ** Note that, unlike unix shell globbing, the directory separator "/"
  95. ** can match a wildcard. So, for example, the pattern "/abc/xyz/" "*"
  96. ** matches any files anywhere in the directory hierarchy beneath
  97. ** /abc/xyz.
  98. **
  99. ** The glob algorithm works on bytes. Multi-byte UTF8 characters are
  100. ** matched as if each byte were a separate character.
  101. **
  102. ** If the iLimit for a quota group is set to zero, then the quota group
  103. ** is disabled and will be deleted when the last database connection using
  104. ** the quota group is closed.
  105. **
  106. ** Calling this routine on a zPattern that does not exist and with a
  107. ** zero iLimit is a no-op.
  108. **
  109. ** A quota group must exist with a non-zero iLimit prior to opening
  110. ** database connections if those connections are to participate in the
  111. ** quota group. Creating a quota group does not affect database connections
  112. ** that are already open.
  113. **
  114. ** The patterns that define the various quota groups should be distinct.
  115. ** If the same filename matches more than one quota group pattern, then
  116. ** the behavior of this package is undefined.
  117. */
  118. int sqlite3_quota_set(
  119. const char *zPattern, /* The filename pattern */
  120. sqlite3_int64 iLimit, /* New quota to set for this quota group */
  121. void (*xCallback)( /* Callback invoked when going over quota */
  122. const char *zFilename, /* Name of file whose size increases */
  123. sqlite3_int64 *piLimit, /* IN/OUT: The current limit */
  124. sqlite3_int64 iSize, /* Total size of all files in the group */
  125. void *pArg /* Client data */
  126. ),
  127. void *pArg, /* client data passed thru to callback */
  128. void (*xDestroy)(void*) /* Optional destructor for pArg */
  129. );
  130. /*
  131. ** Bring the named file under quota management, assuming its name matches
  132. ** the glob pattern of some quota group. Or if it is already under
  133. ** management, update its size. If zFilename does not match the glob
  134. ** pattern of any quota group, this routine is a no-op.
  135. */
  136. int sqlite3_quota_file(const char *zFilename);
  137. /*
  138. ** The following object serves the same role as FILE in the standard C
  139. ** library. It represents an open connection to a file on disk for I/O.
  140. **
  141. ** A single quota_FILE should not be used by two or more threads at the
  142. ** same time. Multiple threads can be using different quota_FILE objects
  143. ** simultaneously, but not the same quota_FILE object.
  144. */
  145. typedef struct quota_FILE quota_FILE;
  146. /*
  147. ** Create a new quota_FILE object used to read and/or write to the
  148. ** file zFilename. The zMode parameter is as with standard library zMode.
  149. */
  150. quota_FILE *sqlite3_quota_fopen(const char *zFilename, const char *zMode);
  151. /*
  152. ** Perform I/O against a quota_FILE object. When doing writes, the
  153. ** quota mechanism may result in a short write, in order to prevent
  154. ** the sum of sizes of all files from going over quota.
  155. */
  156. size_t sqlite3_quota_fread(void*, size_t, size_t, quota_FILE*);
  157. size_t sqlite3_quota_fwrite(const void*, size_t, size_t, quota_FILE*);
  158. /*
  159. ** Flush all written content held in memory buffers out to disk.
  160. ** This is the equivalent of fflush() in the standard library.
  161. **
  162. ** If the hardSync parameter is true (non-zero) then this routine
  163. ** also forces OS buffers to disk - the equivalent of fsync().
  164. **
  165. ** This routine return zero on success and non-zero if something goes
  166. ** wrong.
  167. */
  168. int sqlite3_quota_fflush(quota_FILE*, int hardSync);
  169. /*
  170. ** Close a quota_FILE object and free all associated resources. The
  171. ** file remains under quota management.
  172. */
  173. int sqlite3_quota_fclose(quota_FILE*);
  174. /*
  175. ** Move the read/write pointer for a quota_FILE object. Or tell the
  176. ** current location of the read/write pointer.
  177. */
  178. int sqlite3_quota_fseek(quota_FILE*, long, int);
  179. void sqlite3_quota_rewind(quota_FILE*);
  180. long sqlite3_quota_ftell(quota_FILE*);
  181. /*
  182. ** Test the error indicator for the given file.
  183. **
  184. ** Return non-zero if the error indicator is set.
  185. */
  186. int sqlite3_quota_ferror(quota_FILE*);
  187. /*
  188. ** Truncate a file previously opened by sqlite3_quota_fopen(). Return
  189. ** zero on success and non-zero on any kind of failure.
  190. **
  191. ** The newSize argument must be less than or equal to the current file size.
  192. ** Any attempt to "truncate" a file to a larger size results in
  193. ** undefined behavior.
  194. */
  195. int sqlite3_quota_ftruncate(quota_FILE*, sqlite3_int64 newSize);
  196. /*
  197. ** Return the last modification time of the opened file, in seconds
  198. ** since 1970.
  199. */
  200. int sqlite3_quota_file_mtime(quota_FILE*, time_t *pTime);
  201. /*
  202. ** Return the size of the file as it is known to the quota system.
  203. **
  204. ** This size might be different from the true size of the file on
  205. ** disk if some outside process has modified the file without using the
  206. ** quota mechanism, or if calls to sqlite3_quota_fwrite() have occurred
  207. ** which have increased the file size, but those writes have not yet been
  208. ** forced to disk using sqlite3_quota_fflush().
  209. **
  210. ** Return -1 if the file is not participating in quota management.
  211. */
  212. sqlite3_int64 sqlite3_quota_file_size(quota_FILE*);
  213. /*
  214. ** Return the true size of the file.
  215. **
  216. ** The true size should be the same as the size of the file as known
  217. ** to the quota system, however the sizes might be different if the
  218. ** file has been extended or truncated via some outside process or if
  219. ** pending writes have not yet been flushed to disk.
  220. **
  221. ** Return -1 if the file does not exist or if the size of the file
  222. ** cannot be determined for some reason.
  223. */
  224. sqlite3_int64 sqlite3_quota_file_truesize(quota_FILE*);
  225. /*
  226. ** Determine the amount of data in bytes available for reading
  227. ** in the given file.
  228. **
  229. ** Return -1 if the amount cannot be determined for some reason.
  230. */
  231. long sqlite3_quota_file_available(quota_FILE*);
  232. /*
  233. ** Delete a file from the disk, if that file is under quota management.
  234. ** Adjust quotas accordingly.
  235. **
  236. ** If zFilename is the name of a directory that matches one of the
  237. ** quota glob patterns, then all files under quota management that
  238. ** are contained within that directory are deleted.
  239. **
  240. ** A standard SQLite result code is returned (SQLITE_OK, SQLITE_NOMEM, etc.)
  241. ** When deleting a directory of files, if the deletion of any one
  242. ** file fails (for example due to an I/O error), then this routine
  243. ** returns immediately, with the error code, and does not try to
  244. ** delete any of the other files in the specified directory.
  245. **
  246. ** All files are removed from quota management and deleted from disk.
  247. ** However, no attempt is made to remove empty directories.
  248. **
  249. ** This routine is a no-op for files that are not under quota management.
  250. */
  251. int sqlite3_quota_remove(const char *zFilename);
  252. #ifdef __cplusplus
  253. } /* end of the 'extern "C"' block */
  254. #endif
  255. #endif /* _QUOTA_H_ */