sqlite3recover.h 9.6 KB

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