You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

240 lines
9.7 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. ** 2022-08-27
  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 public interface to the "recover" extension -
  14. ** an SQLite extension designed to recover data from corrupted database
  15. ** files.
  16. */
  17. /*
  18. ** OVERVIEW:
  19. **
  20. ** To use the API to recover data from a corrupted database, an
  21. ** application:
  22. **
  23. ** 1) Creates an sqlite3_recover handle by calling either
  24. ** sqlite3_recover_init() or sqlite3_recover_init_sql().
  25. **
  26. ** 2) Configures the new handle using one or more calls to
  27. ** sqlite3_recover_config().
  28. **
  29. ** 3) Executes the recovery by repeatedly calling sqlite3_recover_step() on
  30. ** the handle until it returns something other than SQLITE_OK. If it
  31. ** returns SQLITE_DONE, then the recovery operation completed without
  32. ** error. If it returns some other non-SQLITE_OK value, then an error
  33. ** has occurred.
  34. **
  35. ** 4) Retrieves any error code and English language error message using the
  36. ** sqlite3_recover_errcode() and sqlite3_recover_errmsg() APIs,
  37. ** respectively.
  38. **
  39. ** 5) Destroys the sqlite3_recover handle and frees all resources
  40. ** using sqlite3_recover_finish().
  41. **
  42. ** The application may abandon the recovery operation at any point
  43. ** before it is finished by passing the sqlite3_recover handle to
  44. ** sqlite3_recover_finish(). This is not an error, but the final state
  45. ** of the output database, or the results of running the partial script
  46. ** delivered to the SQL callback, are undefined.
  47. */
  48. #ifndef _SQLITE_RECOVER_H
  49. #define _SQLITE_RECOVER_H
  50. #include "sqlite3.h"
  51. #ifdef __cplusplus
  52. extern "C"
  53. {
  54. #endif
  55. /*
  56. ** An instance of the sqlite3_recover object represents a recovery
  57. ** operation in progress.
  58. **
  59. ** Constructors:
  60. **
  61. ** sqlite3_recover_init()
  62. ** sqlite3_recover_init_sql()
  63. **
  64. ** Destructor:
  65. **
  66. ** sqlite3_recover_finish()
  67. **
  68. ** Methods:
  69. **
  70. ** sqlite3_recover_config()
  71. ** sqlite3_recover_errcode()
  72. ** sqlite3_recover_errmsg()
  73. ** sqlite3_recover_run()
  74. ** sqlite3_recover_step()
  75. */
  76. typedef struct sqlite3_recover sqlite3_recover;
  77. /*
  78. ** These two APIs attempt to create and return a new sqlite3_recover object.
  79. ** In both cases the first two arguments identify the (possibly
  80. ** corrupt) database to recover data from. The first argument is an open
  81. ** database handle and the second the name of a database attached to that
  82. ** handle (i.e. "main", "temp" or the name of an attached database).
  83. **
  84. ** If sqlite3_recover_init() is used to create the new sqlite3_recover
  85. ** handle, then data is recovered into a new database, identified by
  86. ** string parameter zUri. zUri may be an absolute or relative file path,
  87. ** or may be an SQLite URI. If the identified database file already exists,
  88. ** it is overwritten.
  89. **
  90. ** If sqlite3_recover_init_sql() is invoked, then any recovered data will
  91. ** be returned to the user as a series of SQL statements. Executing these
  92. ** SQL statements results in the same database as would have been created
  93. ** had sqlite3_recover_init() been used. For each SQL statement in the
  94. ** output, the callback function passed as the third argument (xSql) is
  95. ** invoked once. The first parameter is a passed a copy of the fourth argument
  96. ** to this function (pCtx) as its first parameter, and a pointer to a
  97. ** nul-terminated buffer containing the SQL statement formated as UTF-8 as
  98. ** the second. If the xSql callback returns any value other than SQLITE_OK,
  99. ** then processing is immediately abandoned and the value returned used as
  100. ** the recover handle error code (see below).
  101. **
  102. ** If an out-of-memory error occurs, NULL may be returned instead of
  103. ** a valid handle. In all other cases, it is the responsibility of the
  104. ** application to avoid resource leaks by ensuring that
  105. ** sqlite3_recover_finish() is called on all allocated handles.
  106. */
  107. sqlite3_recover * sqlite3_recover_init(sqlite3 * db, const char * zDb, const char * zUri);
  108. sqlite3_recover * sqlite3_recover_init_sql(sqlite3 * db, const char * zDb, int (*xSql)(void *, const char *), void * pCtx);
  109. /*
  110. ** Configure an sqlite3_recover object that has just been created using
  111. ** sqlite3_recover_init() or sqlite3_recover_init_sql(). This function
  112. ** may only be called before the first call to sqlite3_recover_step()
  113. ** or sqlite3_recover_run() on the object.
  114. **
  115. ** The second argument passed to this function must be one of the
  116. ** SQLITE_RECOVER_* symbols defined below. Valid values for the third argument
  117. ** depend on the specific SQLITE_RECOVER_* symbol in use.
  118. **
  119. ** SQLITE_OK is returned if the configuration operation was successful,
  120. ** or an SQLite error code otherwise.
  121. */
  122. int sqlite3_recover_config(sqlite3_recover *, int op, void * pArg);
  123. /*
  124. ** SQLITE_RECOVER_LOST_AND_FOUND:
  125. ** The pArg argument points to a string buffer containing the name
  126. ** of a "lost-and-found" table in the output database, or NULL. If
  127. ** the argument is non-NULL and the database contains seemingly
  128. ** valid pages that cannot be associated with any table in the
  129. ** recovered part of the schema, data is extracted from these
  130. ** pages to add to the lost-and-found table.
  131. **
  132. ** SQLITE_RECOVER_FREELIST_CORRUPT:
  133. ** The pArg value must actually be a pointer to a value of type
  134. ** int containing value 0 or 1 cast as a (void*). If this option is set
  135. ** (argument is 1) and a lost-and-found table has been configured using
  136. ** SQLITE_RECOVER_LOST_AND_FOUND, then is assumed that the freelist is
  137. ** corrupt and an attempt is made to recover records from pages that
  138. ** appear to be linked into the freelist. Otherwise, pages on the freelist
  139. ** are ignored. Setting this option can recover more data from the
  140. ** database, but often ends up "recovering" deleted records. The default
  141. ** value is 0 (clear).
  142. **
  143. ** SQLITE_RECOVER_ROWIDS:
  144. ** The pArg value must actually be a pointer to a value of type
  145. ** int containing value 0 or 1 cast as a (void*). If this option is set
  146. ** (argument is 1), then an attempt is made to recover rowid values
  147. ** that are not also INTEGER PRIMARY KEY values. If this option is
  148. ** clear, then new rowids are assigned to all recovered rows. The
  149. ** default value is 1 (set).
  150. **
  151. ** SQLITE_RECOVER_SLOWINDEXES:
  152. ** The pArg value must actually be a pointer to a value of type
  153. ** int containing value 0 or 1 cast as a (void*). If this option is clear
  154. ** (argument is 0), then when creating an output database, the recover
  155. ** module creates and populates non-UNIQUE indexes right at the end of the
  156. ** recovery operation - after all recoverable data has been inserted
  157. ** into the new database. This is faster overall, but means that the
  158. ** final call to sqlite3_recover_step() for a recovery operation may
  159. ** be need to create a large number of indexes, which may be very slow.
  160. **
  161. ** Or, if this option is set (argument is 1), then non-UNIQUE indexes
  162. ** are created in the output database before it is populated with
  163. ** recovered data. This is slower overall, but avoids the slow call
  164. ** to sqlite3_recover_step() at the end of the recovery operation.
  165. **
  166. ** The default option value is 0.
  167. */
  168. #define SQLITE_RECOVER_LOST_AND_FOUND 1
  169. #define SQLITE_RECOVER_FREELIST_CORRUPT 2
  170. #define SQLITE_RECOVER_ROWIDS 3
  171. #define SQLITE_RECOVER_SLOWINDEXES 4
  172. /*
  173. ** Perform a unit of work towards the recovery operation. This function
  174. ** must normally be called multiple times to complete database recovery.
  175. **
  176. ** If no error occurs but the recovery operation is not completed, this
  177. ** function returns SQLITE_OK. If recovery has been completed successfully
  178. ** then SQLITE_DONE is returned. If an error has occurred, then an SQLite
  179. ** error code (e.g. SQLITE_IOERR or SQLITE_NOMEM) is returned. It is not
  180. ** considered an error if some or all of the data cannot be recovered
  181. ** due to database corruption.
  182. **
  183. ** Once sqlite3_recover_step() has returned a value other than SQLITE_OK,
  184. ** all further such calls on the same recover handle are no-ops that return
  185. ** the same non-SQLITE_OK value.
  186. */
  187. int sqlite3_recover_step(sqlite3_recover *);
  188. /*
  189. ** Run the recovery operation to completion. Return SQLITE_OK if successful,
  190. ** or an SQLite error code otherwise. Calling this function is the same
  191. ** as executing:
  192. **
  193. ** while( SQLITE_OK==sqlite3_recover_step(p) );
  194. ** return sqlite3_recover_errcode(p);
  195. */
  196. int sqlite3_recover_run(sqlite3_recover *);
  197. /*
  198. ** If an error has been encountered during a prior call to
  199. ** sqlite3_recover_step(), then this function attempts to return a
  200. ** pointer to a buffer containing an English language explanation of
  201. ** the error. If no error message is available, or if an out-of memory
  202. ** error occurs while attempting to allocate a buffer in which to format
  203. ** the error message, NULL is returned.
  204. **
  205. ** The returned buffer remains valid until the sqlite3_recover handle is
  206. ** destroyed using sqlite3_recover_finish().
  207. */
  208. const char * sqlite3_recover_errmsg(sqlite3_recover *);
  209. /*
  210. ** If this function is called on an sqlite3_recover handle after
  211. ** an error occurs, an SQLite error code is returned. Otherwise, SQLITE_OK.
  212. */
  213. int sqlite3_recover_errcode(sqlite3_recover *);
  214. /*
  215. ** Clean up a recovery object created by a call to sqlite3_recover_init().
  216. ** The results of using a recovery object with any API after it has been
  217. ** passed to this function are undefined.
  218. **
  219. ** This function returns the same value as sqlite3_recover_errcode().
  220. */
  221. int sqlite3_recover_finish(sqlite3_recover *);
  222. #ifdef __cplusplus
  223. } /* end of the 'extern "C"' block */
  224. #endif
  225. #endif /* ifndef _SQLITE_RECOVER_H */