| OLD | NEW |
| 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 "webkit/fileapi/file_system_origin_database.h" | 5 #include "webkit/fileapi/sandbox_origin_database.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/format_macros.h" | 10 #include "base/format_macros.h" |
| 11 #include "base/location.h" | 11 #include "base/location.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
| 14 #include "base/string_util.h" |
| 14 #include "base/stringprintf.h" | 15 #include "base/stringprintf.h" |
| 15 #include "base/string_util.h" | |
| 16 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
| 17 #include "third_party/leveldatabase/src/include/leveldb/db.h" | 17 #include "third_party/leveldatabase/src/include/leveldb/db.h" |
| 18 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" | 18 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" |
| 19 #include "webkit/fileapi/file_system_util.h" | 19 #include "webkit/fileapi/file_system_util.h" |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 const base::FilePath::CharType kOriginDatabaseName[] = | 23 const base::FilePath::CharType kOriginDatabaseName[] = |
| 24 FILE_PATH_LITERAL("Origins"); | 24 FILE_PATH_LITERAL("Origins"); |
| 25 const char kOriginKeyPrefix[] = "ORIGIN:"; | 25 const char kOriginKeyPrefix[] = "ORIGIN:"; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 48 } | 48 } |
| 49 | 49 |
| 50 const char* LastPathKey() { | 50 const char* LastPathKey() { |
| 51 return kLastPathKey; | 51 return kLastPathKey; |
| 52 } | 52 } |
| 53 | 53 |
| 54 } // namespace | 54 } // namespace |
| 55 | 55 |
| 56 namespace fileapi { | 56 namespace fileapi { |
| 57 | 57 |
| 58 FileSystemOriginDatabase::OriginRecord::OriginRecord() { | 58 SandboxOriginDatabase::OriginRecord::OriginRecord() { |
| 59 } | 59 } |
| 60 | 60 |
| 61 FileSystemOriginDatabase::OriginRecord::OriginRecord( | 61 SandboxOriginDatabase::OriginRecord::OriginRecord( |
| 62 const std::string& origin_in, const base::FilePath& path_in) | 62 const std::string& origin_in, const base::FilePath& path_in) |
| 63 : origin(origin_in), path(path_in) { | 63 : origin(origin_in), path(path_in) { |
| 64 } | 64 } |
| 65 | 65 |
| 66 FileSystemOriginDatabase::OriginRecord::~OriginRecord() { | 66 SandboxOriginDatabase::OriginRecord::~OriginRecord() { |
| 67 } | 67 } |
| 68 | 68 |
| 69 FileSystemOriginDatabase::FileSystemOriginDatabase( | 69 SandboxOriginDatabase::SandboxOriginDatabase( |
| 70 const base::FilePath& file_system_directory) | 70 const base::FilePath& file_system_directory) |
| 71 : file_system_directory_(file_system_directory) { | 71 : file_system_directory_(file_system_directory) { |
| 72 } | 72 } |
| 73 | 73 |
| 74 FileSystemOriginDatabase::~FileSystemOriginDatabase() { | 74 SandboxOriginDatabase::~SandboxOriginDatabase() { |
| 75 } | 75 } |
| 76 | 76 |
| 77 bool FileSystemOriginDatabase::Init(RecoveryOption recovery_option) { | 77 bool SandboxOriginDatabase::Init(RecoveryOption recovery_option) { |
| 78 if (db_) | 78 if (db_) |
| 79 return true; | 79 return true; |
| 80 | 80 |
| 81 std::string path = | 81 std::string path = |
| 82 FilePathToString(file_system_directory_.Append(kOriginDatabaseName)); | 82 FilePathToString(file_system_directory_.Append(kOriginDatabaseName)); |
| 83 leveldb::Options options; | 83 leveldb::Options options; |
| 84 options.create_if_missing = true; | 84 options.create_if_missing = true; |
| 85 leveldb::DB* db; | 85 leveldb::DB* db; |
| 86 leveldb::Status status = leveldb::DB::Open(options, path, &db); | 86 leveldb::Status status = leveldb::DB::Open(options, path, &db); |
| 87 ReportInitStatus(status); | 87 ReportInitStatus(status); |
| 88 if (status.ok()) { | 88 if (status.ok()) { |
| 89 db_.reset(db); | 89 db_.reset(db); |
| 90 return true; | 90 return true; |
| 91 } | 91 } |
| 92 HandleError(FROM_HERE, status); | 92 HandleError(FROM_HERE, status); |
| 93 | 93 |
| 94 // Corruption due to missing necessary MANIFEST-* file causes IOError instead | 94 // Corruption due to missing necessary MANIFEST-* file causes IOError instead |
| 95 // of Corruption error. | 95 // of Corruption error. |
| 96 // Try to repair database even when IOError case. | 96 // Try to repair database even when IOError case. |
| 97 if (!status.IsCorruption() && !status.IsIOError()) | 97 if (!status.IsCorruption() && !status.IsIOError()) |
| 98 return false; | 98 return false; |
| 99 | 99 |
| 100 switch (recovery_option) { | 100 switch (recovery_option) { |
| 101 case FAIL_ON_CORRUPTION: | 101 case FAIL_ON_CORRUPTION: |
| 102 return false; | 102 return false; |
| 103 case REPAIR_ON_CORRUPTION: | 103 case REPAIR_ON_CORRUPTION: |
| 104 LOG(WARNING) << "Attempting to repair FileSystemOriginDatabase."; | 104 LOG(WARNING) << "Attempting to repair SandboxOriginDatabase."; |
| 105 | 105 |
| 106 if (RepairDatabase(path)) { | 106 if (RepairDatabase(path)) { |
| 107 UMA_HISTOGRAM_ENUMERATION(kDatabaseRepairHistogramLabel, | 107 UMA_HISTOGRAM_ENUMERATION(kDatabaseRepairHistogramLabel, |
| 108 DB_REPAIR_SUCCEEDED, DB_REPAIR_MAX); | 108 DB_REPAIR_SUCCEEDED, DB_REPAIR_MAX); |
| 109 LOG(WARNING) << "Repairing FileSystemOriginDatabase completed."; | 109 LOG(WARNING) << "Repairing SandboxOriginDatabase completed."; |
| 110 return true; | 110 return true; |
| 111 } | 111 } |
| 112 UMA_HISTOGRAM_ENUMERATION(kDatabaseRepairHistogramLabel, | 112 UMA_HISTOGRAM_ENUMERATION(kDatabaseRepairHistogramLabel, |
| 113 DB_REPAIR_FAILED, DB_REPAIR_MAX); | 113 DB_REPAIR_FAILED, DB_REPAIR_MAX); |
| 114 // fall through | 114 // fall through |
| 115 case DELETE_ON_CORRUPTION: | 115 case DELETE_ON_CORRUPTION: |
| 116 if (!file_util::Delete(file_system_directory_, true)) | 116 if (!file_util::Delete(file_system_directory_, true)) |
| 117 return false; | 117 return false; |
| 118 if (!file_util::CreateDirectory(file_system_directory_)) | 118 if (!file_util::CreateDirectory(file_system_directory_)) |
| 119 return false; | 119 return false; |
| 120 return Init(FAIL_ON_CORRUPTION); | 120 return Init(FAIL_ON_CORRUPTION); |
| 121 } | 121 } |
| 122 NOTREACHED(); | 122 NOTREACHED(); |
| 123 return false; | 123 return false; |
| 124 } | 124 } |
| 125 | 125 |
| 126 bool FileSystemOriginDatabase::RepairDatabase(const std::string& db_path) { | 126 bool SandboxOriginDatabase::RepairDatabase(const std::string& db_path) { |
| 127 DCHECK(!db_.get()); | 127 DCHECK(!db_.get()); |
| 128 if (!leveldb::RepairDB(db_path, leveldb::Options()).ok() || | 128 if (!leveldb::RepairDB(db_path, leveldb::Options()).ok() || |
| 129 !Init(FAIL_ON_CORRUPTION)) { | 129 !Init(FAIL_ON_CORRUPTION)) { |
| 130 LOG(WARNING) << "Failed to repair FileSystemOriginDatabase."; | 130 LOG(WARNING) << "Failed to repair SandboxOriginDatabase."; |
| 131 return false; | 131 return false; |
| 132 } | 132 } |
| 133 | 133 |
| 134 // See if the repaired entries match with what we have on disk. | 134 // See if the repaired entries match with what we have on disk. |
| 135 std::set<base::FilePath> directories; | 135 std::set<base::FilePath> directories; |
| 136 file_util::FileEnumerator file_enum(file_system_directory_, | 136 file_util::FileEnumerator file_enum(file_system_directory_, |
| 137 false /* recursive */, | 137 false /* recursive */, |
| 138 file_util::FileEnumerator::DIRECTORIES); | 138 file_util::FileEnumerator::DIRECTORIES); |
| 139 base::FilePath path_each; | 139 base::FilePath path_each; |
| 140 while (!(path_each = file_enum.Next()).empty()) | 140 while (!(path_each = file_enum.Next()).empty()) |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 if (!file_util::Delete(file_system_directory_.Append(*dir_itr), | 175 if (!file_util::Delete(file_system_directory_.Append(*dir_itr), |
| 176 true /* recursive */)) { | 176 true /* recursive */)) { |
| 177 DropDatabase(); | 177 DropDatabase(); |
| 178 return false; | 178 return false; |
| 179 } | 179 } |
| 180 } | 180 } |
| 181 | 181 |
| 182 return true; | 182 return true; |
| 183 } | 183 } |
| 184 | 184 |
| 185 void FileSystemOriginDatabase::HandleError( | 185 void SandboxOriginDatabase::HandleError( |
| 186 const tracked_objects::Location& from_here, | 186 const tracked_objects::Location& from_here, |
| 187 const leveldb::Status& status) { | 187 const leveldb::Status& status) { |
| 188 db_.reset(); | 188 db_.reset(); |
| 189 LOG(ERROR) << "FileSystemOriginDatabase failed at: " | 189 LOG(ERROR) << "SandboxOriginDatabase failed at: " |
| 190 << from_here.ToString() << " with error: " << status.ToString(); | 190 << from_here.ToString() << " with error: " << status.ToString(); |
| 191 } | 191 } |
| 192 | 192 |
| 193 void FileSystemOriginDatabase::ReportInitStatus(const leveldb::Status& status) { | 193 void SandboxOriginDatabase::ReportInitStatus(const leveldb::Status& status) { |
| 194 base::Time now = base::Time::Now(); | 194 base::Time now = base::Time::Now(); |
| 195 base::TimeDelta minimum_interval = | 195 base::TimeDelta minimum_interval = |
| 196 base::TimeDelta::FromHours(kMinimumReportIntervalHours); | 196 base::TimeDelta::FromHours(kMinimumReportIntervalHours); |
| 197 if (last_reported_time_ + minimum_interval >= now) | 197 if (last_reported_time_ + minimum_interval >= now) |
| 198 return; | 198 return; |
| 199 last_reported_time_ = now; | 199 last_reported_time_ = now; |
| 200 | 200 |
| 201 if (status.ok()) { | 201 if (status.ok()) { |
| 202 UMA_HISTOGRAM_ENUMERATION(kInitStatusHistogramLabel, | 202 UMA_HISTOGRAM_ENUMERATION(kInitStatusHistogramLabel, |
| 203 INIT_STATUS_OK, INIT_STATUS_MAX); | 203 INIT_STATUS_OK, INIT_STATUS_MAX); |
| 204 } else if (status.IsCorruption()) { | 204 } else if (status.IsCorruption()) { |
| 205 UMA_HISTOGRAM_ENUMERATION(kInitStatusHistogramLabel, | 205 UMA_HISTOGRAM_ENUMERATION(kInitStatusHistogramLabel, |
| 206 INIT_STATUS_CORRUPTION, INIT_STATUS_MAX); | 206 INIT_STATUS_CORRUPTION, INIT_STATUS_MAX); |
| 207 } else if (status.IsIOError()) { | 207 } else if (status.IsIOError()) { |
| 208 UMA_HISTOGRAM_ENUMERATION(kInitStatusHistogramLabel, | 208 UMA_HISTOGRAM_ENUMERATION(kInitStatusHistogramLabel, |
| 209 INIT_STATUS_IO_ERROR, INIT_STATUS_MAX); | 209 INIT_STATUS_IO_ERROR, INIT_STATUS_MAX); |
| 210 } else { | 210 } else { |
| 211 UMA_HISTOGRAM_ENUMERATION(kInitStatusHistogramLabel, | 211 UMA_HISTOGRAM_ENUMERATION(kInitStatusHistogramLabel, |
| 212 INIT_STATUS_UNKNOWN_ERROR, INIT_STATUS_MAX); | 212 INIT_STATUS_UNKNOWN_ERROR, INIT_STATUS_MAX); |
| 213 } | 213 } |
| 214 } | 214 } |
| 215 | 215 |
| 216 bool FileSystemOriginDatabase::HasOriginPath(const std::string& origin) { | 216 bool SandboxOriginDatabase::HasOriginPath(const std::string& origin) { |
| 217 if (!Init(REPAIR_ON_CORRUPTION)) | 217 if (!Init(REPAIR_ON_CORRUPTION)) |
| 218 return false; | 218 return false; |
| 219 if (origin.empty()) | 219 if (origin.empty()) |
| 220 return false; | 220 return false; |
| 221 std::string path; | 221 std::string path; |
| 222 leveldb::Status status = | 222 leveldb::Status status = |
| 223 db_->Get(leveldb::ReadOptions(), OriginToOriginKey(origin), &path); | 223 db_->Get(leveldb::ReadOptions(), OriginToOriginKey(origin), &path); |
| 224 if (status.ok()) | 224 if (status.ok()) |
| 225 return true; | 225 return true; |
| 226 if (status.IsNotFound()) | 226 if (status.IsNotFound()) |
| 227 return false; | 227 return false; |
| 228 HandleError(FROM_HERE, status); | 228 HandleError(FROM_HERE, status); |
| 229 return false; | 229 return false; |
| 230 } | 230 } |
| 231 | 231 |
| 232 bool FileSystemOriginDatabase::GetPathForOrigin( | 232 bool SandboxOriginDatabase::GetPathForOrigin( |
| 233 const std::string& origin, base::FilePath* directory) { | 233 const std::string& origin, base::FilePath* directory) { |
| 234 if (!Init(REPAIR_ON_CORRUPTION)) | 234 if (!Init(REPAIR_ON_CORRUPTION)) |
| 235 return false; | 235 return false; |
| 236 DCHECK(directory); | 236 DCHECK(directory); |
| 237 if (origin.empty()) | 237 if (origin.empty()) |
| 238 return false; | 238 return false; |
| 239 std::string path_string; | 239 std::string path_string; |
| 240 std::string origin_key = OriginToOriginKey(origin); | 240 std::string origin_key = OriginToOriginKey(origin); |
| 241 leveldb::Status status = | 241 leveldb::Status status = |
| 242 db_->Get(leveldb::ReadOptions(), origin_key, &path_string); | 242 db_->Get(leveldb::ReadOptions(), origin_key, &path_string); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 256 } | 256 } |
| 257 } | 257 } |
| 258 if (status.ok()) { | 258 if (status.ok()) { |
| 259 *directory = StringToFilePath(path_string); | 259 *directory = StringToFilePath(path_string); |
| 260 return true; | 260 return true; |
| 261 } | 261 } |
| 262 HandleError(FROM_HERE, status); | 262 HandleError(FROM_HERE, status); |
| 263 return false; | 263 return false; |
| 264 } | 264 } |
| 265 | 265 |
| 266 bool FileSystemOriginDatabase::RemovePathForOrigin(const std::string& origin) { | 266 bool SandboxOriginDatabase::RemovePathForOrigin(const std::string& origin) { |
| 267 if (!Init(REPAIR_ON_CORRUPTION)) | 267 if (!Init(REPAIR_ON_CORRUPTION)) |
| 268 return false; | 268 return false; |
| 269 leveldb::Status status = | 269 leveldb::Status status = |
| 270 db_->Delete(leveldb::WriteOptions(), OriginToOriginKey(origin)); | 270 db_->Delete(leveldb::WriteOptions(), OriginToOriginKey(origin)); |
| 271 if (status.ok() || status.IsNotFound()) | 271 if (status.ok() || status.IsNotFound()) |
| 272 return true; | 272 return true; |
| 273 HandleError(FROM_HERE, status); | 273 HandleError(FROM_HERE, status); |
| 274 return false; | 274 return false; |
| 275 } | 275 } |
| 276 | 276 |
| 277 bool FileSystemOriginDatabase::ListAllOrigins( | 277 bool SandboxOriginDatabase::ListAllOrigins( |
| 278 std::vector<OriginRecord>* origins) { | 278 std::vector<OriginRecord>* origins) { |
| 279 if (!Init(REPAIR_ON_CORRUPTION)) | 279 if (!Init(REPAIR_ON_CORRUPTION)) |
| 280 return false; | 280 return false; |
| 281 DCHECK(origins); | 281 DCHECK(origins); |
| 282 scoped_ptr<leveldb::Iterator> iter(db_->NewIterator(leveldb::ReadOptions())); | 282 scoped_ptr<leveldb::Iterator> iter(db_->NewIterator(leveldb::ReadOptions())); |
| 283 std::string origin_key_prefix = OriginToOriginKey(std::string()); | 283 std::string origin_key_prefix = OriginToOriginKey(std::string()); |
| 284 iter->Seek(origin_key_prefix); | 284 iter->Seek(origin_key_prefix); |
| 285 origins->clear(); | 285 origins->clear(); |
| 286 while (iter->Valid() && | 286 while (iter->Valid() && |
| 287 StartsWithASCII(iter->key().ToString(), origin_key_prefix, true)) { | 287 StartsWithASCII(iter->key().ToString(), origin_key_prefix, true)) { |
| 288 std::string origin = | 288 std::string origin = |
| 289 iter->key().ToString().substr(origin_key_prefix.length()); | 289 iter->key().ToString().substr(origin_key_prefix.length()); |
| 290 base::FilePath path = StringToFilePath(iter->value().ToString()); | 290 base::FilePath path = StringToFilePath(iter->value().ToString()); |
| 291 origins->push_back(OriginRecord(origin, path)); | 291 origins->push_back(OriginRecord(origin, path)); |
| 292 iter->Next(); | 292 iter->Next(); |
| 293 } | 293 } |
| 294 return true; | 294 return true; |
| 295 } | 295 } |
| 296 | 296 |
| 297 void FileSystemOriginDatabase::DropDatabase() { | 297 void SandboxOriginDatabase::DropDatabase() { |
| 298 db_.reset(); | 298 db_.reset(); |
| 299 } | 299 } |
| 300 | 300 |
| 301 bool FileSystemOriginDatabase::GetLastPathNumber(int* number) { | 301 bool SandboxOriginDatabase::GetLastPathNumber(int* number) { |
| 302 if (!Init(REPAIR_ON_CORRUPTION)) | 302 if (!Init(REPAIR_ON_CORRUPTION)) |
| 303 return false; | 303 return false; |
| 304 DCHECK(number); | 304 DCHECK(number); |
| 305 std::string number_string; | 305 std::string number_string; |
| 306 leveldb::Status status = | 306 leveldb::Status status = |
| 307 db_->Get(leveldb::ReadOptions(), LastPathKey(), &number_string); | 307 db_->Get(leveldb::ReadOptions(), LastPathKey(), &number_string); |
| 308 if (status.ok()) | 308 if (status.ok()) |
| 309 return base::StringToInt(number_string, number); | 309 return base::StringToInt(number_string, number); |
| 310 if (!status.IsNotFound()) { | 310 if (!status.IsNotFound()) { |
| 311 HandleError(FROM_HERE, status); | 311 HandleError(FROM_HERE, status); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 324 db_->Put(leveldb::WriteOptions(), LastPathKey(), std::string("-1")); | 324 db_->Put(leveldb::WriteOptions(), LastPathKey(), std::string("-1")); |
| 325 if (!status.ok()) { | 325 if (!status.ok()) { |
| 326 HandleError(FROM_HERE, status); | 326 HandleError(FROM_HERE, status); |
| 327 return false; | 327 return false; |
| 328 } | 328 } |
| 329 *number = -1; | 329 *number = -1; |
| 330 return true; | 330 return true; |
| 331 } | 331 } |
| 332 | 332 |
| 333 } // namespace fileapi | 333 } // namespace fileapi |
| OLD | NEW |