[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef SQL_RECOVERY_H_ |
| 6 | #define SQL_RECOVERY_H_ |
| 7 | |
avi | 0b51920 | 2015-12-21 07:25:19 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | |
mostynb | d82cd995 | 2016-04-11 20:05:34 | [diff] [blame] | 10 | #include <memory> |
| 11 | |
tfarina | 720d4f3 | 2015-05-11 22:31:26 | [diff] [blame] | 12 | #include "base/macros.h" |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 13 | #include "sql/connection.h" |
| 14 | |
| 15 | namespace base { |
| 16 | class FilePath; |
| 17 | } |
| 18 | |
| 19 | namespace sql { |
| 20 | |
shess | a402e75 | 2016-07-02 00:25:11 | [diff] [blame] | 21 | // Recovery module for sql/. The basic idea is to create a fresh database and |
| 22 | // populate it with the recovered contents of the original database. If |
| 23 | // recovery is successful, the recovered database is backed up over the original |
| 24 | // database. If recovery is not successful, the original database is razed. In |
| 25 | // either case, the original handle is poisoned so that operations on the stack |
| 26 | // do not accidentally disrupt the restored data. |
| 27 | // |
| 28 | // RecoverDatabaseOrRaze() automates this, including recoverying the schema of |
| 29 | // from the suspect database. If a database requires special handling, such as |
| 30 | // recovering between different schema, or tables requiring post-processing, |
| 31 | // then the module can be used manually like: |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 32 | // |
| 33 | // { |
mostynb | d82cd995 | 2016-04-11 20:05:34 | [diff] [blame] | 34 | // std::unique_ptr<sql::Recovery> r = |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 35 | // sql::Recovery::Begin(orig_db, orig_db_path); |
| 36 | // if (r) { |
[email protected] | ae4f162 | 2013-12-08 06:49:12 | [diff] [blame] | 37 | // // Create the schema to recover to. On failure, clear the |
| 38 | // // database. |
| 39 | // if (!r.db()->Execute(kCreateSchemaSql)) { |
dcheng | 7061e5f | 2016-03-04 01:21:47 | [diff] [blame] | 40 | // sql::Recovery::Unrecoverable(std::move(r)); |
[email protected] | ae4f162 | 2013-12-08 06:49:12 | [diff] [blame] | 41 | // return; |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 42 | // } |
[email protected] | ae4f162 | 2013-12-08 06:49:12 | [diff] [blame] | 43 | // |
| 44 | // // Recover data in "mytable". |
| 45 | // size_t rows_recovered = 0; |
| 46 | // if (!r.AutoRecoverTable("mytable", 0, &rows_recovered)) { |
dcheng | 7061e5f | 2016-03-04 01:21:47 | [diff] [blame] | 47 | // sql::Recovery::Unrecoverable(std::move(r)); |
[email protected] | ae4f162 | 2013-12-08 06:49:12 | [diff] [blame] | 48 | // return; |
| 49 | // } |
| 50 | // |
| 51 | // // Manually cleanup additional constraints. |
| 52 | // if (!r.db()->Execute(kCleanupSql)) { |
dcheng | 7061e5f | 2016-03-04 01:21:47 | [diff] [blame] | 53 | // sql::Recovery::Unrecoverable(std::move(r)); |
[email protected] | ae4f162 | 2013-12-08 06:49:12 | [diff] [blame] | 54 | // return; |
| 55 | // } |
| 56 | // |
| 57 | // // Commit the recovered data to the original database file. |
dcheng | 7061e5f | 2016-03-04 01:21:47 | [diff] [blame] | 58 | // sql::Recovery::Recovered(std::move(r)); |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 59 | // } |
| 60 | // } |
| 61 | // |
| 62 | // If Recovered() is not called, then RazeAndClose() is called on |
| 63 | // orig_db. |
| 64 | |
| 65 | class SQL_EXPORT Recovery { |
| 66 | public: |
| 67 | ~Recovery(); |
| 68 | |
[email protected] | df5d95c4 | 2013-10-07 22:24:22 | [diff] [blame] | 69 | // This module is intended to be used in concert with a virtual |
| 70 | // table module (see third_party/sqlite/src/src/recover.c). If the |
| 71 | // build defines USE_SYSTEM_SQLITE, this module will not be present. |
| 72 | // TODO(shess): I am still debating how to handle this - perhaps it |
| 73 | // will just imply Unrecoverable(). This is exposed to allow tests |
| 74 | // to adapt to the cases, please do not rely on it in production |
| 75 | // code. |
| 76 | static bool FullRecoverySupported(); |
| 77 | |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 78 | // Begin the recovery process by opening a temporary database handle |
| 79 | // and attach the existing database to it at "corrupt". To prevent |
| 80 | // deadlock, all transactions on |connection| are rolled back. |
| 81 | // |
| 82 | // Returns NULL in case of failure, with no cleanup done on the |
| 83 | // original connection (except for breaking the transactions). The |
| 84 | // caller should Raze() or otherwise cleanup as appropriate. |
| 85 | // |
| 86 | // TODO(shess): Later versions of SQLite allow extracting the path |
| 87 | // from the connection. |
| 88 | // TODO(shess): Allow specifying the connection point? |
mostynb | d82cd995 | 2016-04-11 20:05:34 | [diff] [blame] | 89 | static std::unique_ptr<Recovery> Begin(Connection* connection, |
| 90 | const base::FilePath& db_path) |
| 91 | WARN_UNUSED_RESULT; |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 92 | |
| 93 | // Mark recovery completed by replicating the recovery database over |
| 94 | // the original database, then closing the recovery database. The |
| 95 | // original database handle is poisoned, causing future calls |
| 96 | // against it to fail. |
| 97 | // |
| 98 | // If Recovered() is not called, the destructor will call |
| 99 | // Unrecoverable(). |
| 100 | // |
[email protected] | 74cdede | 2013-09-25 05:39:57 | [diff] [blame] | 101 | // TODO(shess): At this time, this function can fail while leaving |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 102 | // the original database intact. Figure out which failure cases |
| 103 | // should go to RazeAndClose() instead. |
mostynb | d82cd995 | 2016-04-11 20:05:34 | [diff] [blame] | 104 | static bool Recovered(std::unique_ptr<Recovery> r) WARN_UNUSED_RESULT; |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 105 | |
| 106 | // Indicate that the database is unrecoverable. The original |
| 107 | // database is razed, and the handle poisoned. |
mostynb | d82cd995 | 2016-04-11 20:05:34 | [diff] [blame] | 108 | static void Unrecoverable(std::unique_ptr<Recovery> r); |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 109 | |
[email protected] | 74cdede | 2013-09-25 05:39:57 | [diff] [blame] | 110 | // When initially developing recovery code, sometimes the possible |
| 111 | // database states are not well-understood without further |
| 112 | // diagnostics. Abandon recovery but do not raze the original |
| 113 | // database. |
| 114 | // NOTE(shess): Only call this when adding recovery support. In the |
| 115 | // steady state, all databases should progress to recovered or razed. |
mostynb | d82cd995 | 2016-04-11 20:05:34 | [diff] [blame] | 116 | static void Rollback(std::unique_ptr<Recovery> r); |
[email protected] | 74cdede | 2013-09-25 05:39:57 | [diff] [blame] | 117 | |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 118 | // Handle to the temporary recovery database. |
| 119 | sql::Connection* db() { return &recover_db_; } |
| 120 | |
[email protected] | a8848a7 | 2013-11-18 04:18:47 | [diff] [blame] | 121 | // Attempt to recover the named table from the corrupt database into |
| 122 | // the recovery database using a temporary recover virtual table. |
| 123 | // The virtual table schema is derived from the named table's schema |
shess | 806f499 | 2016-02-04 21:12:09 | [diff] [blame] | 124 | // in database [main]. Data is copied using INSERT OR IGNORE, so |
| 125 | // duplicates are dropped. |
[email protected] | a8848a7 | 2013-11-18 04:18:47 | [diff] [blame] | 126 | // |
shess | 6f68bd3 | 2016-02-04 19:29:44 | [diff] [blame] | 127 | // If the source table has fewer columns than the target, the target |
| 128 | // DEFAULT value will be used for those columns. |
[email protected] | a8848a7 | 2013-11-18 04:18:47 | [diff] [blame] | 129 | // |
| 130 | // Returns true if all operations succeeded, with the number of rows |
| 131 | // recovered in |*rows_recovered|. |
| 132 | // |
| 133 | // NOTE(shess): Due to a flaw in the recovery virtual table, at this |
| 134 | // time this code injects the DEFAULT value of the target table in |
| 135 | // locations where the recovery table returns NULL. This is not |
| 136 | // entirely correct, because it happens both when there is a short |
| 137 | // row (correct) but also where there is an actual NULL value |
| 138 | // (incorrect). |
| 139 | // |
| 140 | // TODO(shess): Flag for INSERT OR REPLACE vs IGNORE. |
| 141 | // TODO(shess): Handle extended table names. |
shess | 6f68bd3 | 2016-02-04 19:29:44 | [diff] [blame] | 142 | bool AutoRecoverTable(const char* table_name, size_t* rows_recovered); |
[email protected] | a8848a7 | 2013-11-18 04:18:47 | [diff] [blame] | 143 | |
| 144 | // Setup a recover virtual table at temp.recover_meta, reading from |
| 145 | // corrupt.meta. Returns true if created. |
| 146 | // TODO(shess): Perhaps integrate into Begin(). |
| 147 | // TODO(shess): Add helpers to fetch additional items from the meta |
| 148 | // table as needed. |
| 149 | bool SetupMeta(); |
| 150 | |
| 151 | // Fetch the version number from temp.recover_meta. Returns false |
| 152 | // if the query fails, or if there is no version row. Otherwise |
| 153 | // returns true, with the version in |*version_number|. |
| 154 | // |
| 155 | // Only valid to call after successful SetupMeta(). |
| 156 | bool GetMetaVersionNumber(int* version_number); |
| 157 | |
shess | a402e75 | 2016-07-02 00:25:11 | [diff] [blame] | 158 | // Attempt to recover the database by creating a new database with schema from |
| 159 | // |db|, then copying over as much data as possible. After this call, the |
| 160 | // |db| handle will be poisoned (though technically remaining open) so that |
| 161 | // future calls will return errors until the handle is re-opened. |
| 162 | // |
| 163 | // If a corrupt database contains tables without unique indices, the resulting |
| 164 | // table may contain duplication. If this is not acceptable, the client |
| 165 | // should use the manual process as described in the example at the top of the |
| 166 | // file, cleaning up data at the appropriate points. |
| 167 | static void RecoverDatabase(Connection* db, const base::FilePath& db_path); |
| 168 | |
| 169 | // Returns true for SQLite errors which RecoverDatabase() can plausibly fix. |
| 170 | // This does not guarantee that RecoverDatabase() will successfully recover |
| 171 | // the database. |
| 172 | static bool ShouldRecover(int extended_error); |
| 173 | |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 174 | private: |
| 175 | explicit Recovery(Connection* connection); |
| 176 | |
| 177 | // Setup the recovery database handle for Begin(). Returns false in |
| 178 | // case anything failed. |
| 179 | bool Init(const base::FilePath& db_path) WARN_UNUSED_RESULT; |
| 180 | |
| 181 | // Copy the recovered database over the original database. |
| 182 | bool Backup() WARN_UNUSED_RESULT; |
| 183 | |
| 184 | // Close the recovery database, and poison the original handle. |
| 185 | // |raze| controls whether the original database is razed or just |
| 186 | // poisoned. |
| 187 | enum Disposition { |
| 188 | RAZE_AND_POISON, |
| 189 | POISON, |
| 190 | }; |
| 191 | void Shutdown(Disposition raze); |
| 192 | |
| 193 | Connection* db_; // Original database connection. |
| 194 | Connection recover_db_; // Recovery connection. |
| 195 | |
| 196 | DISALLOW_COPY_AND_ASSIGN(Recovery); |
| 197 | }; |
| 198 | |
| 199 | } // namespace sql |
| 200 | |
| 201 | #endif // SQL_RECOVERY_H_ |