| 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 |
| 101 return true; | 104 return true; |
| 105 #endif |
| 102 } | 106 } |
| 103 | 107 |
| 104 // static | 108 // static |
| 105 scoped_ptr<Recovery> Recovery::Begin( | 109 scoped_ptr<Recovery> Recovery::Begin( |
| 106 Connection* connection, | 110 Connection* connection, |
| 107 const base::FilePath& db_path) { | 111 const base::FilePath& db_path) { |
| 108 // Recovery is likely to be used in error handling. Since recovery changes | 112 // Recovery is likely to be used in error handling. Since recovery changes |
| 109 // the state of the handle, protect against multiple layers attempting the | 113 // the state of the handle, protect against multiple layers attempting the |
| 110 // same recovery. | 114 // same recovery. |
| 111 if (!connection->is_open()) { | 115 if (!connection->is_open()) { |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 | 196 |
| 193 // TODO(shess): If this is a common failure case, it might be | 197 // TODO(shess): If this is a common failure case, it might be |
| 194 // possible to fall back to a memory database. But it probably | 198 // possible to fall back to a memory database. But it probably |
| 195 // implies that the SQLite tmpdir logic is busted, which could cause | 199 // implies that the SQLite tmpdir logic is busted, which could cause |
| 196 // a variety of other random issues in our code. | 200 // a variety of other random issues in our code. |
| 197 if (!recover_db_.OpenTemporary()) { | 201 if (!recover_db_.OpenTemporary()) { |
| 198 RecordRecoveryEvent(RECOVERY_FAILED_OPEN_TEMPORARY); | 202 RecordRecoveryEvent(RECOVERY_FAILED_OPEN_TEMPORARY); |
| 199 return false; | 203 return false; |
| 200 } | 204 } |
| 201 | 205 |
| 202 // Enable the recover virtual table for this connection. | 206 // TODO(shess): Figure out a story for USE_SYSTEM_SQLITE. The |
| 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) |
| 203 int rc = recoverVtableInit(recover_db_.db_); | 216 int rc = recoverVtableInit(recover_db_.db_); |
| 204 if (rc != SQLITE_OK) { | 217 if (rc != SQLITE_OK) { |
| 205 RecordRecoveryEvent(RECOVERY_FAILED_VIRTUAL_TABLE_INIT); | 218 RecordRecoveryEvent(RECOVERY_FAILED_VIRTUAL_TABLE_INIT); |
| 206 LOG(ERROR) << "Failed to initialize recover module: " | 219 LOG(ERROR) << "Failed to initialize recover module: " |
| 207 << recover_db_.GetErrorMessage(); | 220 << recover_db_.GetErrorMessage(); |
| 208 return false; | 221 return false; |
| 209 } | 222 } |
| 223 #else |
| 224 // If this is infrequent enough, just wire it to Raze(). |
| 225 RecordRecoveryEvent(RECOVERY_FAILED_VIRTUAL_TABLE_SYSTEM_SQLITE); |
| 226 #endif |
| 210 | 227 |
| 211 // Turn on |SQLITE_RecoveryMode| for the handle, which allows | 228 // Turn on |SQLITE_RecoveryMode| for the handle, which allows |
| 212 // reading certain broken databases. | 229 // reading certain broken databases. |
| 213 if (!recover_db_.Execute("PRAGMA writable_schema=1")) { | 230 if (!recover_db_.Execute("PRAGMA writable_schema=1")) { |
| 214 RecordRecoveryEvent(RECOVERY_FAILED_WRITABLE_SCHEMA); | 231 RecordRecoveryEvent(RECOVERY_FAILED_WRITABLE_SCHEMA); |
| 215 return false; | 232 return false; |
| 216 } | 233 } |
| 217 | 234 |
| 218 if (!recover_db_.AttachDatabase(db_path, "corrupt")) { | 235 if (!recover_db_.AttachDatabase(db_path, "corrupt")) { |
| 219 RecordRecoveryEvent(RECOVERY_FAILED_ATTACH); | 236 RecordRecoveryEvent(RECOVERY_FAILED_ATTACH); |
| (...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 503 } | 520 } |
| 504 return false; | 521 return false; |
| 505 } | 522 } |
| 506 | 523 |
| 507 RecordRecoveryEvent(RECOVERY_SUCCESS_META_VERSION); | 524 RecordRecoveryEvent(RECOVERY_SUCCESS_META_VERSION); |
| 508 *version = recovery_version.ColumnInt(0); | 525 *version = recovery_version.ColumnInt(0); |
| 509 return true; | 526 return true; |
| 510 } | 527 } |
| 511 | 528 |
| 512 } // namespace sql | 529 } // namespace sql |
| OLD | NEW |