| 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" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
| 13 #include "base/metrics/sparse_histogram.h" | 13 #include "base/metrics/sparse_histogram.h" |
| 14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 15 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 16 #include "sql/connection.h" | 16 #include "sql/connection.h" |
| 17 #include "sql/statement.h" | 17 #include "sql/statement.h" |
| 18 #include "third_party/sqlite/ext/recover.h" |
| 18 #include "third_party/sqlite/sqlite3.h" | 19 #include "third_party/sqlite/sqlite3.h" |
| 19 | 20 |
| 20 namespace sql { | 21 namespace sql { |
| 21 | 22 |
| 22 namespace { | 23 namespace { |
| 23 | 24 |
| 24 enum RecoveryEventType { | 25 enum RecoveryEventType { |
| 25 // Init() completed successfully. | 26 // Init() completed successfully. |
| 26 RECOVERY_SUCCESS_INIT = 0, | 27 RECOVERY_SUCCESS_INIT = 0, |
| 27 | 28 |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 void RecordRecoveryEvent(RecoveryEventType recovery_event) { | 91 void RecordRecoveryEvent(RecoveryEventType recovery_event) { |
| 91 UMA_HISTOGRAM_ENUMERATION("Sqlite.RecoveryEvents", | 92 UMA_HISTOGRAM_ENUMERATION("Sqlite.RecoveryEvents", |
| 92 recovery_event, RECOVERY_EVENT_MAX); | 93 recovery_event, RECOVERY_EVENT_MAX); |
| 93 } | 94 } |
| 94 | 95 |
| 95 } // namespace | 96 } // namespace |
| 96 | 97 |
| 97 // static | 98 // static |
| 98 bool Recovery::FullRecoverySupported() { | 99 bool Recovery::FullRecoverySupported() { |
| 99 // TODO(shess): See comment in Init(). | 100 // TODO(shess): See comment in Init(). |
| 100 #if defined(USE_SYSTEM_SQLITE) | |
| 101 return false; | |
| 102 #else | |
| 103 return true; | 101 return true; |
| 104 #endif | |
| 105 } | 102 } |
| 106 | 103 |
| 107 // static | 104 // static |
| 108 scoped_ptr<Recovery> Recovery::Begin( | 105 scoped_ptr<Recovery> Recovery::Begin( |
| 109 Connection* connection, | 106 Connection* connection, |
| 110 const base::FilePath& db_path) { | 107 const base::FilePath& db_path) { |
| 111 // Recovery is likely to be used in error handling. Since recovery changes | 108 if (!connection->is_open()) |
| 112 // the state of the handle, protect against multiple layers attempting the | |
| 113 // same recovery. | |
| 114 if (!connection->is_open()) { | |
| 115 // Warn about API mis-use. | |
| 116 DLOG_IF(FATAL, !connection->poisoned_) | |
| 117 << "Illegal to recover with closed database"; | |
| 118 return scoped_ptr<Recovery>(); | 109 return scoped_ptr<Recovery>(); |
| 119 } | |
| 120 | 110 |
| 121 scoped_ptr<Recovery> r(new Recovery(connection)); | 111 scoped_ptr<Recovery> r(new Recovery(connection)); |
| 122 if (!r->Init(db_path)) { | 112 if (!r->Init(db_path)) { |
| 123 // TODO(shess): Should Init() failure result in Raze()? | 113 // TODO(shess): Should Init() failure result in Raze()? |
| 124 r->Shutdown(POISON); | 114 r->Shutdown(POISON); |
| 125 return scoped_ptr<Recovery>(); | 115 return scoped_ptr<Recovery>(); |
| 126 } | 116 } |
| 127 | 117 |
| 128 return r; | 118 return r; |
| 129 } | 119 } |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 | 194 |
| 205 // TODO(shess): Figure out a story for USE_SYSTEM_SQLITE. The | 195 // TODO(shess): Figure out a story for USE_SYSTEM_SQLITE. The |
| 206 // virtual table implementation relies on SQLite internals for some | 196 // virtual table implementation relies on SQLite internals for some |
| 207 // types and functions, which could be copied inline to make it | 197 // types and functions, which could be copied inline to make it |
| 208 // standalone. Or an alternate implementation could try to read | 198 // standalone. Or an alternate implementation could try to read |
| 209 // through errors entirely at the SQLite level. | 199 // through errors entirely at the SQLite level. |
| 210 // | 200 // |
| 211 // For now, defer to the caller. The setup will succeed, but the | 201 // For now, defer to the caller. The setup will succeed, but the |
| 212 // later CREATE VIRTUAL TABLE call will fail, at which point the | 202 // later CREATE VIRTUAL TABLE call will fail, at which point the |
| 213 // caller can fire Unrecoverable(). | 203 // caller can fire Unrecoverable(). |
| 214 #if !defined(USE_SYSTEM_SQLITE) | |
| 215 int rc = recoverVtableInit(recover_db_.db_); | 204 int rc = recoverVtableInit(recover_db_.db_); |
| 216 if (rc != SQLITE_OK) { | 205 if (rc != SQLITE_OK) { |
| 217 RecordRecoveryEvent(RECOVERY_FAILED_VIRTUAL_TABLE_INIT); | 206 RecordRecoveryEvent(RECOVERY_FAILED_VIRTUAL_TABLE_INIT); |
| 218 LOG(ERROR) << "Failed to initialize recover module: " | 207 LOG(ERROR) << "Failed to initialize recover module: " |
| 219 << recover_db_.GetErrorMessage(); | 208 << recover_db_.GetErrorMessage(); |
| 220 return false; | 209 return false; |
| 221 } | 210 } |
| 222 #else | |
| 223 // If this is infrequent enough, just wire it to Raze(). | |
| 224 RecordRecoveryEvent(RECOVERY_FAILED_VIRTUAL_TABLE_SYSTEM_SQLITE); | |
| 225 #endif | |
| 226 | 211 |
| 227 // Turn on |SQLITE_RecoveryMode| for the handle, which allows | 212 // Turn on |SQLITE_RecoveryMode| for the handle, which allows |
| 228 // reading certain broken databases. | 213 // reading certain broken databases. |
| 229 if (!recover_db_.Execute("PRAGMA writable_schema=1")) { | 214 if (!recover_db_.Execute("PRAGMA writable_schema=1")) { |
| 230 RecordRecoveryEvent(RECOVERY_FAILED_WRITABLE_SCHEMA); | 215 RecordRecoveryEvent(RECOVERY_FAILED_WRITABLE_SCHEMA); |
| 231 return false; | 216 return false; |
| 232 } | 217 } |
| 233 | 218 |
| 234 if (!recover_db_.AttachDatabase(db_path, "corrupt")) { | 219 if (!recover_db_.AttachDatabase(db_path, "corrupt")) { |
| 235 RecordRecoveryEvent(RECOVERY_FAILED_ATTACH); | 220 RecordRecoveryEvent(RECOVERY_FAILED_ATTACH); |
| (...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 519 } | 504 } |
| 520 return false; | 505 return false; |
| 521 } | 506 } |
| 522 | 507 |
| 523 RecordRecoveryEvent(RECOVERY_SUCCESS_META_VERSION); | 508 RecordRecoveryEvent(RECOVERY_SUCCESS_META_VERSION); |
| 524 *version = recovery_version.ColumnInt(0); | 509 *version = recovery_version.ColumnInt(0); |
| 525 return true; | 510 return true; |
| 526 } | 511 } |
| 527 | 512 |
| 528 } // namespace sql | 513 } // namespace sql |
| OLD | NEW |