Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(287)

Side by Side Diff: sql/connection.cc

Issue 104593010: AppCache: Run a quick integrity check on the sqlite database when opening. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/connection.h" 5 #include "sql/connection.h"
6 6
7 #include <string.h> 7 #include <string.h>
8 8
9 #include "base/files/file_path.h" 9 #include "base/files/file_path.h"
10 #include "base/file_util.h" 10 #include "base/file_util.h"
(...skipping 1032 matching lines...) Expand 10 before | Expand all | Expand 10 after
1043 ErrorCallback(error_callback_).Run(err, stmt); 1043 ErrorCallback(error_callback_).Run(err, stmt);
1044 return err; 1044 return err;
1045 } 1045 }
1046 1046
1047 // The default handling is to assert on debug and to ignore on release. 1047 // The default handling is to assert on debug and to ignore on release.
1048 if (!ShouldIgnoreSqliteError(err)) 1048 if (!ShouldIgnoreSqliteError(err))
1049 DLOG(FATAL) << GetErrorMessage(); 1049 DLOG(FATAL) << GetErrorMessage();
1050 return err; 1050 return err;
1051 } 1051 }
1052 1052
1053 // TODO(shess): Allow specifying integrity_check versus quick_check. 1053 bool Connection::FullIntegrityCheck(std::vector<std::string>* messages) {
1054 return IntegrityCheckHelper("PRAGMA integrity_check", messages);
1055 }
1056
1057 bool Connection::QuickIntegrityCheck() {
1058 std::vector<std::string> messages;
1059 if (!IntegrityCheckHelper("PRAGMA quick_check", &messages))
1060 return false;
1061 return messages.size() == 1 && messages[0] == "ok";
1062 }
1063
1054 // TODO(shess): Allow specifying maximum results (default 100 lines). 1064 // TODO(shess): Allow specifying maximum results (default 100 lines).
1055 bool Connection::IntegrityCheck(std::vector<std::string>* messages) { 1065 bool Connection::IntegrityCheckHelper(
1066 const char* pragma_sql,
1067 std::vector<std::string>* messages) {
1056 messages->clear(); 1068 messages->clear();
1057 1069
1058 // This has the side effect of setting SQLITE_RecoveryMode, which 1070 // This has the side effect of setting SQLITE_RecoveryMode, which
1059 // allows SQLite to process through certain cases of corruption. 1071 // allows SQLite to process through certain cases of corruption.
1060 // Failing to set this pragma probably means that the database is 1072 // Failing to set this pragma probably means that the database is
1061 // beyond recovery. 1073 // beyond recovery.
1062 const char kWritableSchema[] = "PRAGMA writable_schema = ON"; 1074 const char kWritableSchema[] = "PRAGMA writable_schema = ON";
1063 if (!Execute(kWritableSchema)) 1075 if (!Execute(kWritableSchema))
1064 return false; 1076 return false;
1065 1077
1066 bool ret = false; 1078 bool ret = false;
1067 { 1079 {
1068 const char kSql[] = "PRAGMA integrity_check"; 1080 sql::Statement stmt(GetUniqueStatement(pragma_sql));
1069 sql::Statement stmt(GetUniqueStatement(kSql));
1070 1081
1071 // The pragma appears to return all results (up to 100 by default) 1082 // The pragma appears to return all results (up to 100 by default)
1072 // as a single string. This doesn't appear to be an API contract, 1083 // as a single string. This doesn't appear to be an API contract,
1073 // it could return separate lines, so loop _and_ split. 1084 // it could return separate lines, so loop _and_ split.
1074 while (stmt.Step()) { 1085 while (stmt.Step()) {
1075 std::string result(stmt.ColumnString(0)); 1086 std::string result(stmt.ColumnString(0));
1076 base::SplitString(result, '\n', messages); 1087 base::SplitString(result, '\n', messages);
1077 } 1088 }
1078 ret = stmt.Succeeded(); 1089 ret = stmt.Succeeded();
1079 } 1090 }
1080 1091
1081 // Best effort to put things back as they were before. 1092 // Best effort to put things back as they were before.
1082 const char kNoWritableSchema[] = "PRAGMA writable_schema = OFF"; 1093 const char kNoWritableSchema[] = "PRAGMA writable_schema = OFF";
1083 ignore_result(Execute(kNoWritableSchema)); 1094 ignore_result(Execute(kNoWritableSchema));
1084 1095
1085 return ret; 1096 return ret;
1086 } 1097 }
1087 1098
1088 } // namespace sql 1099 } // namespace sql
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698