| 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/browser/fileapi/sandbox_origin_database.h" | 5 #include "webkit/browser/fileapi/sandbox_origin_database.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 #include <utility> |
| 8 | 9 |
| 9 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 10 #include "base/format_macros.h" | 11 #include "base/format_macros.h" |
| 11 #include "base/location.h" | 12 #include "base/location.h" |
| 12 #include "base/logging.h" | 13 #include "base/logging.h" |
| 13 #include "base/metrics/histogram.h" | 14 #include "base/metrics/histogram.h" |
| 14 #include "base/string_util.h" | 15 #include "base/string_util.h" |
| 15 #include "base/stringprintf.h" | 16 #include "base/stringprintf.h" |
| 16 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
| 17 #include "third_party/leveldatabase/src/include/leveldb/db.h" | 18 #include "third_party/leveldatabase/src/include/leveldb/db.h" |
| (...skipping 30 matching lines...) Expand all Loading... |
| 48 } | 49 } |
| 49 | 50 |
| 50 const char* LastPathKey() { | 51 const char* LastPathKey() { |
| 51 return kLastPathKey; | 52 return kLastPathKey; |
| 52 } | 53 } |
| 53 | 54 |
| 54 } // namespace | 55 } // namespace |
| 55 | 56 |
| 56 namespace fileapi { | 57 namespace fileapi { |
| 57 | 58 |
| 58 SandboxOriginDatabase::OriginRecord::OriginRecord() { | |
| 59 } | |
| 60 | |
| 61 SandboxOriginDatabase::OriginRecord::OriginRecord( | |
| 62 const std::string& origin_in, const base::FilePath& path_in) | |
| 63 : origin(origin_in), path(path_in) { | |
| 64 } | |
| 65 | |
| 66 SandboxOriginDatabase::OriginRecord::~OriginRecord() { | |
| 67 } | |
| 68 | |
| 69 SandboxOriginDatabase::SandboxOriginDatabase( | 59 SandboxOriginDatabase::SandboxOriginDatabase( |
| 70 const base::FilePath& file_system_directory) | 60 const base::FilePath& file_system_directory) |
| 71 : file_system_directory_(file_system_directory) { | 61 : file_system_directory_(file_system_directory) { |
| 72 } | 62 } |
| 73 | 63 |
| 74 SandboxOriginDatabase::~SandboxOriginDatabase() { | 64 SandboxOriginDatabase::~SandboxOriginDatabase() { |
| 75 } | 65 } |
| 76 | 66 |
| 77 bool SandboxOriginDatabase::Init(RecoveryOption recovery_option) { | 67 bool SandboxOriginDatabase::Init(InitOption init_option, |
| 68 RecoveryOption recovery_option) { |
| 78 if (db_) | 69 if (db_) |
| 79 return true; | 70 return true; |
| 80 | 71 |
| 81 std::string path = | 72 base::FilePath db_path = GetDatabasePath(); |
| 82 FilePathToString(file_system_directory_.Append(kOriginDatabaseName)); | 73 if (init_option == FAIL_IF_NONEXISTENT && !file_util::PathExists(db_path)) |
| 74 return false; |
| 75 |
| 76 std::string path = FilePathToString(db_path); |
| 83 leveldb::Options options; | 77 leveldb::Options options; |
| 84 options.create_if_missing = true; | 78 options.create_if_missing = true; |
| 85 leveldb::DB* db; | 79 leveldb::DB* db; |
| 86 leveldb::Status status = leveldb::DB::Open(options, path, &db); | 80 leveldb::Status status = leveldb::DB::Open(options, path, &db); |
| 87 ReportInitStatus(status); | 81 ReportInitStatus(status); |
| 88 if (status.ok()) { | 82 if (status.ok()) { |
| 89 db_.reset(db); | 83 db_.reset(db); |
| 90 return true; | 84 return true; |
| 91 } | 85 } |
| 92 HandleError(FROM_HERE, status); | 86 HandleError(FROM_HERE, status); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 110 return true; | 104 return true; |
| 111 } | 105 } |
| 112 UMA_HISTOGRAM_ENUMERATION(kDatabaseRepairHistogramLabel, | 106 UMA_HISTOGRAM_ENUMERATION(kDatabaseRepairHistogramLabel, |
| 113 DB_REPAIR_FAILED, DB_REPAIR_MAX); | 107 DB_REPAIR_FAILED, DB_REPAIR_MAX); |
| 114 // fall through | 108 // fall through |
| 115 case DELETE_ON_CORRUPTION: | 109 case DELETE_ON_CORRUPTION: |
| 116 if (!file_util::Delete(file_system_directory_, true)) | 110 if (!file_util::Delete(file_system_directory_, true)) |
| 117 return false; | 111 return false; |
| 118 if (!file_util::CreateDirectory(file_system_directory_)) | 112 if (!file_util::CreateDirectory(file_system_directory_)) |
| 119 return false; | 113 return false; |
| 120 return Init(FAIL_ON_CORRUPTION); | 114 return Init(init_option, FAIL_ON_CORRUPTION); |
| 121 } | 115 } |
| 122 NOTREACHED(); | 116 NOTREACHED(); |
| 123 return false; | 117 return false; |
| 124 } | 118 } |
| 125 | 119 |
| 126 bool SandboxOriginDatabase::RepairDatabase(const std::string& db_path) { | 120 bool SandboxOriginDatabase::RepairDatabase(const std::string& db_path) { |
| 127 DCHECK(!db_.get()); | 121 DCHECK(!db_.get()); |
| 128 if (!leveldb::RepairDB(db_path, leveldb::Options()).ok() || | 122 if (!leveldb::RepairDB(db_path, leveldb::Options()).ok() || |
| 129 !Init(FAIL_ON_CORRUPTION)) { | 123 !Init(FAIL_IF_NONEXISTENT, FAIL_ON_CORRUPTION)) { |
| 130 LOG(WARNING) << "Failed to repair SandboxOriginDatabase."; | 124 LOG(WARNING) << "Failed to repair SandboxOriginDatabase."; |
| 131 return false; | 125 return false; |
| 132 } | 126 } |
| 133 | 127 |
| 134 // See if the repaired entries match with what we have on disk. | 128 // See if the repaired entries match with what we have on disk. |
| 135 std::set<base::FilePath> directories; | 129 std::set<base::FilePath> directories; |
| 136 file_util::FileEnumerator file_enum(file_system_directory_, | 130 file_util::FileEnumerator file_enum(file_system_directory_, |
| 137 false /* recursive */, | 131 false /* recursive */, |
| 138 file_util::FileEnumerator::DIRECTORIES); | 132 file_util::FileEnumerator::DIRECTORIES); |
| 139 base::FilePath path_each; | 133 base::FilePath path_each; |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 } else if (status.IsIOError()) { | 201 } else if (status.IsIOError()) { |
| 208 UMA_HISTOGRAM_ENUMERATION(kInitStatusHistogramLabel, | 202 UMA_HISTOGRAM_ENUMERATION(kInitStatusHistogramLabel, |
| 209 INIT_STATUS_IO_ERROR, INIT_STATUS_MAX); | 203 INIT_STATUS_IO_ERROR, INIT_STATUS_MAX); |
| 210 } else { | 204 } else { |
| 211 UMA_HISTOGRAM_ENUMERATION(kInitStatusHistogramLabel, | 205 UMA_HISTOGRAM_ENUMERATION(kInitStatusHistogramLabel, |
| 212 INIT_STATUS_UNKNOWN_ERROR, INIT_STATUS_MAX); | 206 INIT_STATUS_UNKNOWN_ERROR, INIT_STATUS_MAX); |
| 213 } | 207 } |
| 214 } | 208 } |
| 215 | 209 |
| 216 bool SandboxOriginDatabase::HasOriginPath(const std::string& origin) { | 210 bool SandboxOriginDatabase::HasOriginPath(const std::string& origin) { |
| 217 if (!Init(REPAIR_ON_CORRUPTION)) | 211 if (!Init(FAIL_IF_NONEXISTENT, REPAIR_ON_CORRUPTION)) |
| 218 return false; | 212 return false; |
| 219 if (origin.empty()) | 213 if (origin.empty()) |
| 220 return false; | 214 return false; |
| 221 std::string path; | 215 std::string path; |
| 222 leveldb::Status status = | 216 leveldb::Status status = |
| 223 db_->Get(leveldb::ReadOptions(), OriginToOriginKey(origin), &path); | 217 db_->Get(leveldb::ReadOptions(), OriginToOriginKey(origin), &path); |
| 224 if (status.ok()) | 218 if (status.ok()) |
| 225 return true; | 219 return true; |
| 226 if (status.IsNotFound()) | 220 if (status.IsNotFound()) |
| 227 return false; | 221 return false; |
| 228 HandleError(FROM_HERE, status); | 222 HandleError(FROM_HERE, status); |
| 229 return false; | 223 return false; |
| 230 } | 224 } |
| 231 | 225 |
| 232 bool SandboxOriginDatabase::GetPathForOrigin( | 226 bool SandboxOriginDatabase::GetPathForOrigin( |
| 233 const std::string& origin, base::FilePath* directory) { | 227 const std::string& origin, base::FilePath* directory) { |
| 234 if (!Init(REPAIR_ON_CORRUPTION)) | 228 if (!Init(CREATE_IF_NONEXISTENT, REPAIR_ON_CORRUPTION)) |
| 235 return false; | 229 return false; |
| 236 DCHECK(directory); | 230 DCHECK(directory); |
| 237 if (origin.empty()) | 231 if (origin.empty()) |
| 238 return false; | 232 return false; |
| 239 std::string path_string; | 233 std::string path_string; |
| 240 std::string origin_key = OriginToOriginKey(origin); | 234 std::string origin_key = OriginToOriginKey(origin); |
| 241 leveldb::Status status = | 235 leveldb::Status status = |
| 242 db_->Get(leveldb::ReadOptions(), origin_key, &path_string); | 236 db_->Get(leveldb::ReadOptions(), origin_key, &path_string); |
| 243 if (status.IsNotFound()) { | 237 if (status.IsNotFound()) { |
| 244 int last_path_number; | 238 int last_path_number; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 257 } | 251 } |
| 258 if (status.ok()) { | 252 if (status.ok()) { |
| 259 *directory = StringToFilePath(path_string); | 253 *directory = StringToFilePath(path_string); |
| 260 return true; | 254 return true; |
| 261 } | 255 } |
| 262 HandleError(FROM_HERE, status); | 256 HandleError(FROM_HERE, status); |
| 263 return false; | 257 return false; |
| 264 } | 258 } |
| 265 | 259 |
| 266 bool SandboxOriginDatabase::RemovePathForOrigin(const std::string& origin) { | 260 bool SandboxOriginDatabase::RemovePathForOrigin(const std::string& origin) { |
| 267 if (!Init(REPAIR_ON_CORRUPTION)) | 261 if (!Init(CREATE_IF_NONEXISTENT, REPAIR_ON_CORRUPTION)) |
| 268 return false; | 262 return false; |
| 269 leveldb::Status status = | 263 leveldb::Status status = |
| 270 db_->Delete(leveldb::WriteOptions(), OriginToOriginKey(origin)); | 264 db_->Delete(leveldb::WriteOptions(), OriginToOriginKey(origin)); |
| 271 if (status.ok() || status.IsNotFound()) | 265 if (status.ok() || status.IsNotFound()) |
| 272 return true; | 266 return true; |
| 273 HandleError(FROM_HERE, status); | 267 HandleError(FROM_HERE, status); |
| 274 return false; | 268 return false; |
| 275 } | 269 } |
| 276 | 270 |
| 277 bool SandboxOriginDatabase::ListAllOrigins( | 271 bool SandboxOriginDatabase::ListAllOrigins( |
| 278 std::vector<OriginRecord>* origins) { | 272 std::vector<OriginRecord>* origins) { |
| 279 if (!Init(REPAIR_ON_CORRUPTION)) | 273 DCHECK(origins); |
| 274 if (!Init(CREATE_IF_NONEXISTENT, REPAIR_ON_CORRUPTION)) { |
| 275 origins->clear(); |
| 280 return false; | 276 return false; |
| 281 DCHECK(origins); | 277 } |
| 282 scoped_ptr<leveldb::Iterator> iter(db_->NewIterator(leveldb::ReadOptions())); | 278 scoped_ptr<leveldb::Iterator> iter(db_->NewIterator(leveldb::ReadOptions())); |
| 283 std::string origin_key_prefix = OriginToOriginKey(std::string()); | 279 std::string origin_key_prefix = OriginToOriginKey(std::string()); |
| 284 iter->Seek(origin_key_prefix); | 280 iter->Seek(origin_key_prefix); |
| 285 origins->clear(); | 281 origins->clear(); |
| 286 while (iter->Valid() && | 282 while (iter->Valid() && |
| 287 StartsWithASCII(iter->key().ToString(), origin_key_prefix, true)) { | 283 StartsWithASCII(iter->key().ToString(), origin_key_prefix, true)) { |
| 288 std::string origin = | 284 std::string origin = |
| 289 iter->key().ToString().substr(origin_key_prefix.length()); | 285 iter->key().ToString().substr(origin_key_prefix.length()); |
| 290 base::FilePath path = StringToFilePath(iter->value().ToString()); | 286 base::FilePath path = StringToFilePath(iter->value().ToString()); |
| 291 origins->push_back(OriginRecord(origin, path)); | 287 origins->push_back(OriginRecord(origin, path)); |
| 292 iter->Next(); | 288 iter->Next(); |
| 293 } | 289 } |
| 294 return true; | 290 return true; |
| 295 } | 291 } |
| 296 | 292 |
| 297 void SandboxOriginDatabase::DropDatabase() { | 293 void SandboxOriginDatabase::DropDatabase() { |
| 298 db_.reset(); | 294 db_.reset(); |
| 299 } | 295 } |
| 300 | 296 |
| 297 base::FilePath SandboxOriginDatabase::GetDatabasePath() const { |
| 298 return file_system_directory_.Append(kOriginDatabaseName); |
| 299 } |
| 300 |
| 301 void SandboxOriginDatabase::RemoveDatabase() { |
| 302 DropDatabase(); |
| 303 file_util::Delete(GetDatabasePath(), true /* recursive */); |
| 304 } |
| 305 |
| 301 bool SandboxOriginDatabase::GetLastPathNumber(int* number) { | 306 bool SandboxOriginDatabase::GetLastPathNumber(int* number) { |
| 302 if (!Init(REPAIR_ON_CORRUPTION)) | 307 DCHECK(db_); |
| 303 return false; | |
| 304 DCHECK(number); | 308 DCHECK(number); |
| 305 std::string number_string; | 309 std::string number_string; |
| 306 leveldb::Status status = | 310 leveldb::Status status = |
| 307 db_->Get(leveldb::ReadOptions(), LastPathKey(), &number_string); | 311 db_->Get(leveldb::ReadOptions(), LastPathKey(), &number_string); |
| 308 if (status.ok()) | 312 if (status.ok()) |
| 309 return base::StringToInt(number_string, number); | 313 return base::StringToInt(number_string, number); |
| 310 if (!status.IsNotFound()) { | 314 if (!status.IsNotFound()) { |
| 311 HandleError(FROM_HERE, status); | 315 HandleError(FROM_HERE, status); |
| 312 return false; | 316 return false; |
| 313 } | 317 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 324 db_->Put(leveldb::WriteOptions(), LastPathKey(), std::string("-1")); | 328 db_->Put(leveldb::WriteOptions(), LastPathKey(), std::string("-1")); |
| 325 if (!status.ok()) { | 329 if (!status.ok()) { |
| 326 HandleError(FROM_HERE, status); | 330 HandleError(FROM_HERE, status); |
| 327 return false; | 331 return false; |
| 328 } | 332 } |
| 329 *number = -1; | 333 *number = -1; |
| 330 return true; | 334 return true; |
| 331 } | 335 } |
| 332 | 336 |
| 333 } // namespace fileapi | 337 } // namespace fileapi |
| OLD | NEW |