| 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/file_system_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_number_conversions.h" | 14 #include "base/string_number_conversions.h" |
| 15 #include "base/stringprintf.h" | 15 #include "base/stringprintf.h" |
| 16 #include "base/string_util.h" | 16 #include "base/string_util.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 FilePath::CharType kOriginDatabaseName[] = FILE_PATH_LITERAL("Origins"); | 23 const base::FilePath::CharType kOriginDatabaseName[] = |
| 24 FILE_PATH_LITERAL("Origins"); |
| 24 const char kOriginKeyPrefix[] = "ORIGIN:"; | 25 const char kOriginKeyPrefix[] = "ORIGIN:"; |
| 25 const char kLastPathKey[] = "LAST_PATH"; | 26 const char kLastPathKey[] = "LAST_PATH"; |
| 26 const int64 kMinimumReportIntervalHours = 1; | 27 const int64 kMinimumReportIntervalHours = 1; |
| 27 const char kInitStatusHistogramLabel[] = "FileSystem.OriginDatabaseInit"; | 28 const char kInitStatusHistogramLabel[] = "FileSystem.OriginDatabaseInit"; |
| 28 | 29 |
| 29 enum InitStatus { | 30 enum InitStatus { |
| 30 INIT_STATUS_OK = 0, | 31 INIT_STATUS_OK = 0, |
| 31 INIT_STATUS_CORRUPTION, | 32 INIT_STATUS_CORRUPTION, |
| 32 INIT_STATUS_IO_ERROR, | 33 INIT_STATUS_IO_ERROR, |
| 33 INIT_STATUS_UNKNOWN_ERROR, | 34 INIT_STATUS_UNKNOWN_ERROR, |
| (...skipping 10 matching lines...) Expand all Loading... |
| 44 } | 45 } |
| 45 | 46 |
| 46 } // namespace | 47 } // namespace |
| 47 | 48 |
| 48 namespace fileapi { | 49 namespace fileapi { |
| 49 | 50 |
| 50 FileSystemOriginDatabase::OriginRecord::OriginRecord() { | 51 FileSystemOriginDatabase::OriginRecord::OriginRecord() { |
| 51 } | 52 } |
| 52 | 53 |
| 53 FileSystemOriginDatabase::OriginRecord::OriginRecord( | 54 FileSystemOriginDatabase::OriginRecord::OriginRecord( |
| 54 const std::string& origin_in, const FilePath& path_in) | 55 const std::string& origin_in, const base::FilePath& path_in) |
| 55 : origin(origin_in), path(path_in) { | 56 : origin(origin_in), path(path_in) { |
| 56 } | 57 } |
| 57 | 58 |
| 58 FileSystemOriginDatabase::OriginRecord::~OriginRecord() { | 59 FileSystemOriginDatabase::OriginRecord::~OriginRecord() { |
| 59 } | 60 } |
| 60 | 61 |
| 61 FileSystemOriginDatabase::FileSystemOriginDatabase( | 62 FileSystemOriginDatabase::FileSystemOriginDatabase( |
| 62 const FilePath& file_system_directory) | 63 const base::FilePath& file_system_directory) |
| 63 : file_system_directory_(file_system_directory) { | 64 : file_system_directory_(file_system_directory) { |
| 64 } | 65 } |
| 65 | 66 |
| 66 FileSystemOriginDatabase::~FileSystemOriginDatabase() { | 67 FileSystemOriginDatabase::~FileSystemOriginDatabase() { |
| 67 } | 68 } |
| 68 | 69 |
| 69 bool FileSystemOriginDatabase::Init(RecoveryOption recovery_option) { | 70 bool FileSystemOriginDatabase::Init(RecoveryOption recovery_option) { |
| 70 if (db_.get()) | 71 if (db_.get()) |
| 71 return true; | 72 return true; |
| 72 | 73 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 | 110 |
| 110 bool FileSystemOriginDatabase::RepairDatabase(const std::string& db_path) { | 111 bool FileSystemOriginDatabase::RepairDatabase(const std::string& db_path) { |
| 111 DCHECK(!db_.get()); | 112 DCHECK(!db_.get()); |
| 112 if (!leveldb::RepairDB(db_path, leveldb::Options()).ok() || | 113 if (!leveldb::RepairDB(db_path, leveldb::Options()).ok() || |
| 113 !Init(FAIL_ON_CORRUPTION)) { | 114 !Init(FAIL_ON_CORRUPTION)) { |
| 114 LOG(WARNING) << "Failed to repair FileSystemOriginDatabase."; | 115 LOG(WARNING) << "Failed to repair FileSystemOriginDatabase."; |
| 115 return false; | 116 return false; |
| 116 } | 117 } |
| 117 | 118 |
| 118 // See if the repaired entries match with what we have on disk. | 119 // See if the repaired entries match with what we have on disk. |
| 119 std::set<FilePath> directories; | 120 std::set<base::FilePath> directories; |
| 120 file_util::FileEnumerator file_enum(file_system_directory_, | 121 file_util::FileEnumerator file_enum(file_system_directory_, |
| 121 false /* recursive */, | 122 false /* recursive */, |
| 122 file_util::FileEnumerator::DIRECTORIES); | 123 file_util::FileEnumerator::DIRECTORIES); |
| 123 FilePath path_each; | 124 base::FilePath path_each; |
| 124 while (!(path_each = file_enum.Next()).empty()) | 125 while (!(path_each = file_enum.Next()).empty()) |
| 125 directories.insert(path_each.BaseName()); | 126 directories.insert(path_each.BaseName()); |
| 126 std::set<FilePath>::iterator db_dir_itr = | 127 std::set<base::FilePath>::iterator db_dir_itr = |
| 127 directories.find(FilePath(kOriginDatabaseName)); | 128 directories.find(base::FilePath(kOriginDatabaseName)); |
| 128 // Make sure we have the database file in its directory and therefore we are | 129 // Make sure we have the database file in its directory and therefore we are |
| 129 // working on the correct path. | 130 // working on the correct path. |
| 130 DCHECK(db_dir_itr != directories.end()); | 131 DCHECK(db_dir_itr != directories.end()); |
| 131 directories.erase(db_dir_itr); | 132 directories.erase(db_dir_itr); |
| 132 | 133 |
| 133 std::vector<OriginRecord> origins; | 134 std::vector<OriginRecord> origins; |
| 134 if (!ListAllOrigins(&origins)) { | 135 if (!ListAllOrigins(&origins)) { |
| 135 DropDatabase(); | 136 DropDatabase(); |
| 136 return false; | 137 return false; |
| 137 } | 138 } |
| 138 | 139 |
| 139 // Delete any obsolete entries from the origins database. | 140 // Delete any obsolete entries from the origins database. |
| 140 for (std::vector<OriginRecord>::iterator db_origin_itr = origins.begin(); | 141 for (std::vector<OriginRecord>::iterator db_origin_itr = origins.begin(); |
| 141 db_origin_itr != origins.end(); | 142 db_origin_itr != origins.end(); |
| 142 ++db_origin_itr) { | 143 ++db_origin_itr) { |
| 143 std::set<FilePath>::iterator dir_itr = | 144 std::set<base::FilePath>::iterator dir_itr = |
| 144 directories.find(db_origin_itr->path); | 145 directories.find(db_origin_itr->path); |
| 145 if (dir_itr == directories.end()) { | 146 if (dir_itr == directories.end()) { |
| 146 if (!RemovePathForOrigin(db_origin_itr->origin)) { | 147 if (!RemovePathForOrigin(db_origin_itr->origin)) { |
| 147 DropDatabase(); | 148 DropDatabase(); |
| 148 return false; | 149 return false; |
| 149 } | 150 } |
| 150 } else { | 151 } else { |
| 151 directories.erase(dir_itr); | 152 directories.erase(dir_itr); |
| 152 } | 153 } |
| 153 } | 154 } |
| 154 | 155 |
| 155 // Delete any directories not listed in the origins database. | 156 // Delete any directories not listed in the origins database. |
| 156 for (std::set<FilePath>::iterator dir_itr = directories.begin(); | 157 for (std::set<base::FilePath>::iterator dir_itr = directories.begin(); |
| 157 dir_itr != directories.end(); | 158 dir_itr != directories.end(); |
| 158 ++dir_itr) { | 159 ++dir_itr) { |
| 159 if (!file_util::Delete(file_system_directory_.Append(*dir_itr), | 160 if (!file_util::Delete(file_system_directory_.Append(*dir_itr), |
| 160 true /* recursive */)) { | 161 true /* recursive */)) { |
| 161 DropDatabase(); | 162 DropDatabase(); |
| 162 return false; | 163 return false; |
| 163 } | 164 } |
| 164 } | 165 } |
| 165 | 166 |
| 166 return true; | 167 return true; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 db_->Get(leveldb::ReadOptions(), OriginToOriginKey(origin), &path); | 208 db_->Get(leveldb::ReadOptions(), OriginToOriginKey(origin), &path); |
| 208 if (status.ok()) | 209 if (status.ok()) |
| 209 return true; | 210 return true; |
| 210 if (status.IsNotFound()) | 211 if (status.IsNotFound()) |
| 211 return false; | 212 return false; |
| 212 HandleError(FROM_HERE, status); | 213 HandleError(FROM_HERE, status); |
| 213 return false; | 214 return false; |
| 214 } | 215 } |
| 215 | 216 |
| 216 bool FileSystemOriginDatabase::GetPathForOrigin( | 217 bool FileSystemOriginDatabase::GetPathForOrigin( |
| 217 const std::string& origin, FilePath* directory) { | 218 const std::string& origin, base::FilePath* directory) { |
| 218 if (!Init(REPAIR_ON_CORRUPTION)) | 219 if (!Init(REPAIR_ON_CORRUPTION)) |
| 219 return false; | 220 return false; |
| 220 DCHECK(directory); | 221 DCHECK(directory); |
| 221 if (origin.empty()) | 222 if (origin.empty()) |
| 222 return false; | 223 return false; |
| 223 std::string path_string; | 224 std::string path_string; |
| 224 std::string origin_key = OriginToOriginKey(origin); | 225 std::string origin_key = OriginToOriginKey(origin); |
| 225 leveldb::Status status = | 226 leveldb::Status status = |
| 226 db_->Get(leveldb::ReadOptions(), origin_key, &path_string); | 227 db_->Get(leveldb::ReadOptions(), origin_key, &path_string); |
| 227 if (status.IsNotFound()) { | 228 if (status.IsNotFound()) { |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 return false; | 265 return false; |
| 265 DCHECK(origins); | 266 DCHECK(origins); |
| 266 scoped_ptr<leveldb::Iterator> iter(db_->NewIterator(leveldb::ReadOptions())); | 267 scoped_ptr<leveldb::Iterator> iter(db_->NewIterator(leveldb::ReadOptions())); |
| 267 std::string origin_key_prefix = OriginToOriginKey(""); | 268 std::string origin_key_prefix = OriginToOriginKey(""); |
| 268 iter->Seek(origin_key_prefix); | 269 iter->Seek(origin_key_prefix); |
| 269 origins->clear(); | 270 origins->clear(); |
| 270 while (iter->Valid() && | 271 while (iter->Valid() && |
| 271 StartsWithASCII(iter->key().ToString(), origin_key_prefix, true)) { | 272 StartsWithASCII(iter->key().ToString(), origin_key_prefix, true)) { |
| 272 std::string origin = | 273 std::string origin = |
| 273 iter->key().ToString().substr(origin_key_prefix.length()); | 274 iter->key().ToString().substr(origin_key_prefix.length()); |
| 274 FilePath path = StringToFilePath(iter->value().ToString()); | 275 base::FilePath path = StringToFilePath(iter->value().ToString()); |
| 275 origins->push_back(OriginRecord(origin, path)); | 276 origins->push_back(OriginRecord(origin, path)); |
| 276 iter->Next(); | 277 iter->Next(); |
| 277 } | 278 } |
| 278 return true; | 279 return true; |
| 279 } | 280 } |
| 280 | 281 |
| 281 void FileSystemOriginDatabase::DropDatabase() { | 282 void FileSystemOriginDatabase::DropDatabase() { |
| 282 db_.reset(); | 283 db_.reset(); |
| 283 } | 284 } |
| 284 | 285 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 308 db_->Put(leveldb::WriteOptions(), LastPathKey(), std::string("-1")); | 309 db_->Put(leveldb::WriteOptions(), LastPathKey(), std::string("-1")); |
| 309 if (!status.ok()) { | 310 if (!status.ok()) { |
| 310 HandleError(FROM_HERE, status); | 311 HandleError(FROM_HERE, status); |
| 311 return false; | 312 return false; |
| 312 } | 313 } |
| 313 *number = -1; | 314 *number = -1; |
| 314 return true; | 315 return true; |
| 315 } | 316 } |
| 316 | 317 |
| 317 } // namespace fileapi | 318 } // namespace fileapi |
| OLD | NEW |