OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "sql/recovery.h" | 5 #include "sql/recovery.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/format_macros.h" | 10 #include "base/format_macros.h" |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 void RecordRecoveryEvent(RecoveryEventType recovery_event) { | 91 void RecordRecoveryEvent(RecoveryEventType recovery_event) { |
92 UMA_HISTOGRAM_ENUMERATION("Sqlite.RecoveryEvents", | 92 UMA_HISTOGRAM_ENUMERATION("Sqlite.RecoveryEvents", |
93 recovery_event, RECOVERY_EVENT_MAX); | 93 recovery_event, RECOVERY_EVENT_MAX); |
94 } | 94 } |
95 | 95 |
96 } // namespace | 96 } // namespace |
97 | 97 |
98 // static | 98 // static |
99 bool Recovery::FullRecoverySupported() { | 99 bool Recovery::FullRecoverySupported() { |
100 // TODO(shess): See comment in Init(). | 100 // TODO(shess): See comment in Init(). |
101 #if defined(USE_SYSTEM_SQLITE) | |
102 return false; | |
103 #else | |
104 return true; | 101 return true; |
105 #endif | |
106 } | 102 } |
107 | 103 |
108 // static | 104 // static |
109 scoped_ptr<Recovery> Recovery::Begin( | 105 scoped_ptr<Recovery> Recovery::Begin( |
110 Connection* connection, | 106 Connection* connection, |
111 const base::FilePath& db_path) { | 107 const base::FilePath& db_path) { |
112 // Recovery is likely to be used in error handling. Since recovery changes | 108 // Recovery is likely to be used in error handling. Since recovery changes |
113 // the state of the handle, protect against multiple layers attempting the | 109 // the state of the handle, protect against multiple layers attempting the |
114 // same recovery. | 110 // same recovery. |
115 if (!connection->is_open()) { | 111 if (!connection->is_open()) { |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 | 192 |
197 // TODO(shess): If this is a common failure case, it might be | 193 // TODO(shess): If this is a common failure case, it might be |
198 // possible to fall back to a memory database. But it probably | 194 // possible to fall back to a memory database. But it probably |
199 // implies that the SQLite tmpdir logic is busted, which could cause | 195 // implies that the SQLite tmpdir logic is busted, which could cause |
200 // a variety of other random issues in our code. | 196 // a variety of other random issues in our code. |
201 if (!recover_db_.OpenTemporary()) { | 197 if (!recover_db_.OpenTemporary()) { |
202 RecordRecoveryEvent(RECOVERY_FAILED_OPEN_TEMPORARY); | 198 RecordRecoveryEvent(RECOVERY_FAILED_OPEN_TEMPORARY); |
203 return false; | 199 return false; |
204 } | 200 } |
205 | 201 |
206 // TODO(shess): Figure out a story for USE_SYSTEM_SQLITE. The | 202 // Enable the recover virtual table for this connection. |
207 // virtual table implementation relies on SQLite internals for some | |
208 // types and functions, which could be copied inline to make it | |
209 // standalone. Or an alternate implementation could try to read | |
210 // through errors entirely at the SQLite level. | |
211 // | |
212 // For now, defer to the caller. The setup will succeed, but the | |
213 // later CREATE VIRTUAL TABLE call will fail, at which point the | |
214 // caller can fire Unrecoverable(). | |
215 #if !defined(USE_SYSTEM_SQLITE) | |
216 int rc = recoverVtableInit(recover_db_.db_); | 203 int rc = recoverVtableInit(recover_db_.db_); |
217 if (rc != SQLITE_OK) { | 204 if (rc != SQLITE_OK) { |
218 RecordRecoveryEvent(RECOVERY_FAILED_VIRTUAL_TABLE_INIT); | 205 RecordRecoveryEvent(RECOVERY_FAILED_VIRTUAL_TABLE_INIT); |
219 LOG(ERROR) << "Failed to initialize recover module: " | 206 LOG(ERROR) << "Failed to initialize recover module: " |
220 << recover_db_.GetErrorMessage(); | 207 << recover_db_.GetErrorMessage(); |
221 return false; | 208 return false; |
222 } | 209 } |
223 #else | |
224 // If this is infrequent enough, just wire it to Raze(). | |
225 RecordRecoveryEvent(RECOVERY_FAILED_VIRTUAL_TABLE_SYSTEM_SQLITE); | |
226 #endif | |
227 | 210 |
228 // Turn on |SQLITE_RecoveryMode| for the handle, which allows | 211 // Turn on |SQLITE_RecoveryMode| for the handle, which allows |
229 // reading certain broken databases. | 212 // reading certain broken databases. |
230 if (!recover_db_.Execute("PRAGMA writable_schema=1")) { | 213 if (!recover_db_.Execute("PRAGMA writable_schema=1")) { |
231 RecordRecoveryEvent(RECOVERY_FAILED_WRITABLE_SCHEMA); | 214 RecordRecoveryEvent(RECOVERY_FAILED_WRITABLE_SCHEMA); |
232 return false; | 215 return false; |
233 } | 216 } |
234 | 217 |
235 if (!recover_db_.AttachDatabase(db_path, "corrupt")) { | 218 if (!recover_db_.AttachDatabase(db_path, "corrupt")) { |
236 RecordRecoveryEvent(RECOVERY_FAILED_ATTACH); | 219 RecordRecoveryEvent(RECOVERY_FAILED_ATTACH); |
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
520 } | 503 } |
521 return false; | 504 return false; |
522 } | 505 } |
523 | 506 |
524 RecordRecoveryEvent(RECOVERY_SUCCESS_META_VERSION); | 507 RecordRecoveryEvent(RECOVERY_SUCCESS_META_VERSION); |
525 *version = recovery_version.ColumnInt(0); | 508 *version = recovery_version.ColumnInt(0); |
526 return true; | 509 return true; |
527 } | 510 } |
528 | 511 |
529 } // namespace sql | 512 } // namespace sql |
OLD | NEW |