Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/file_util.h" | |
| 7 #include "base/format_macros.h" | 8 #include "base/format_macros.h" |
| 8 #include "base/location.h" | 9 #include "base/location.h" |
| 9 #include "base/logging.h" | 10 #include "base/logging.h" |
| 10 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
| 11 #include "base/stringprintf.h" | 12 #include "base/stringprintf.h" |
| 12 #include "base/string_util.h" | 13 #include "base/string_util.h" |
| 13 #include "base/sys_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
|
ericu
2012/03/26 18:39:18
Remove utf_string_conversions.h?
tzik
2012/03/27 02:44:18
Done.
| |
| 14 #include "third_party/leveldatabase/src/include/leveldb/iterator.h" | 15 #include "third_party/leveldatabase/src/include/leveldb/iterator.h" |
| 15 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" | 16 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" |
| 17 #include "webkit/fileapi/file_system_util.h" | |
| 16 | 18 |
| 17 namespace { | 19 namespace { |
| 18 | 20 |
| 21 const FilePath::CharType kOriginDatabaseName[] = FILE_PATH_LITERAL("Origins"); | |
| 19 const char kOriginKeyPrefix[] = "ORIGIN:"; | 22 const char kOriginKeyPrefix[] = "ORIGIN:"; |
| 20 const char kLastPathKey[] = "LAST_PATH"; | 23 const char kLastPathKey[] = "LAST_PATH"; |
| 21 | 24 |
| 22 std::string OriginToOriginKey(const std::string& origin) { | 25 std::string OriginToOriginKey(const std::string& origin) { |
| 23 std::string key(kOriginKeyPrefix); | 26 std::string key(kOriginKeyPrefix); |
| 24 return key + origin; | 27 return key + origin; |
| 25 } | 28 } |
| 26 | 29 |
| 27 const char* LastPathKey() { | 30 const char* LastPathKey() { |
| 28 return kLastPathKey; | 31 return kLastPathKey; |
| 29 } | 32 } |
| 30 | 33 |
| 31 } | 34 } |
| 32 | 35 |
| 33 namespace fileapi { | 36 namespace fileapi { |
| 34 | 37 |
| 35 FileSystemOriginDatabase::OriginRecord::OriginRecord() { | 38 FileSystemOriginDatabase::OriginRecord::OriginRecord() { |
| 36 } | 39 } |
| 37 | 40 |
| 38 FileSystemOriginDatabase::OriginRecord::OriginRecord( | 41 FileSystemOriginDatabase::OriginRecord::OriginRecord( |
| 39 const std::string& origin_in, const FilePath& path_in) | 42 const std::string& origin_in, const FilePath& path_in) |
| 40 : origin(origin_in), path(path_in) { | 43 : origin(origin_in), path(path_in) { |
| 41 } | 44 } |
| 42 | 45 |
| 43 FileSystemOriginDatabase::OriginRecord::~OriginRecord() { | 46 FileSystemOriginDatabase::OriginRecord::~OriginRecord() { |
| 44 } | 47 } |
| 45 | 48 |
| 46 FileSystemOriginDatabase::FileSystemOriginDatabase(const FilePath& path) { | 49 FileSystemOriginDatabase::FileSystemOriginDatabase( |
| 47 #if defined(OS_POSIX) | 50 const FilePath& file_system_directory) |
| 48 path_ = path.value(); | 51 : file_system_directory_(file_system_directory) { |
| 49 #elif defined(OS_WIN) | |
| 50 path_ = base::SysWideToUTF8(path.value()); | |
| 51 #endif | |
| 52 } | 52 } |
| 53 | 53 |
| 54 FileSystemOriginDatabase::~FileSystemOriginDatabase() { | 54 FileSystemOriginDatabase::~FileSystemOriginDatabase() { |
| 55 } | 55 } |
| 56 | 56 |
| 57 bool FileSystemOriginDatabase::Init() { | 57 bool FileSystemOriginDatabase::Init(RecoveryOption recovery_option) { |
| 58 if (db_.get()) | 58 if (db_.get()) |
| 59 return true; | 59 return true; |
| 60 | 60 |
| 61 std::string path = | |
| 62 FilePathToString(file_system_directory_.Append(kOriginDatabaseName)); | |
| 61 leveldb::Options options; | 63 leveldb::Options options; |
| 62 options.create_if_missing = true; | 64 options.create_if_missing = true; |
| 63 leveldb::DB* db; | 65 leveldb::DB* db; |
| 64 leveldb::Status status = leveldb::DB::Open(options, path_, &db); | 66 leveldb::Status status = leveldb::DB::Open(options, path, &db); |
| 67 // TODO(tzik): Collect status metrics here. | |
| 65 if (status.ok()) { | 68 if (status.ok()) { |
| 66 db_.reset(db); | 69 db_.reset(db); |
| 67 return true; | 70 return true; |
| 68 } | 71 } |
| 69 HandleError(FROM_HERE, status); | 72 HandleError(FROM_HERE, status); |
| 73 | |
| 74 switch (recovery_option) { | |
| 75 case FAIL_ON_CORRUPTION: | |
| 76 return false; | |
| 77 case REPAIR_ON_CORRUPTION: | |
| 78 LOG(WARNING) << "Attempting to repair FileSystemOriginDatabase."; | |
| 79 if (RepairDatabase(path)) { | |
| 80 LOG(WARNING) << "Repairing FileSystemOriginDatabase completed."; | |
| 81 return true; | |
| 82 } | |
| 83 // fall through | |
| 84 case DELETE_ON_CORRUPTION: | |
| 85 if (!file_util::Delete(file_system_directory_, true)) | |
| 86 return false; | |
| 87 if (!file_util::CreateDirectory(file_system_directory_)) | |
| 88 return false; | |
| 89 return Init(FAIL_ON_CORRUPTION); | |
| 90 } | |
| 91 NOTREACHED(); | |
| 70 return false; | 92 return false; |
| 71 } | 93 } |
| 72 | 94 |
| 95 bool FileSystemOriginDatabase::RepairDatabase(const std::string& db_path) { | |
| 96 DCHECK(!db_.get()); | |
| 97 if (!leveldb::RepairDB(db_path, leveldb::Options()).ok() || | |
| 98 !Init(FAIL_ON_CORRUPTION)) { | |
| 99 LOG(WARNING) << "Failed to repair FileSystemOriginDatabase."; | |
| 100 return false; | |
| 101 } | |
| 102 | |
| 103 // See if the repaired entries match with what we have on disk. | |
| 104 std::set<FilePath> directories; | |
| 105 file_util::FileEnumerator file_enum(file_system_directory_, | |
| 106 false /* recursive */, | |
| 107 file_util::FileEnumerator::DIRECTORIES); | |
| 108 FilePath path_each; | |
| 109 while (!(path_each = file_enum.Next()).empty()) | |
| 110 directories.insert(path_each.BaseName()); | |
| 111 std::set<FilePath>::iterator db_dir_itr = | |
| 112 directories.find(FilePath(kOriginDatabaseName)); | |
| 113 // Make sure we have the database file in its directory and therefore we are | |
| 114 // working on the correct path. | |
| 115 DCHECK(db_dir_itr != directories.end()); | |
| 116 directories.erase(db_dir_itr); | |
| 117 | |
| 118 std::vector<OriginRecord> origins; | |
| 119 if (!ListAllOrigins(&origins)) { | |
| 120 DropDatabase(); | |
| 121 return false; | |
| 122 } | |
|
ericu
2012/03/26 18:39:18
How about a comment here to match that below:
"De
tzik
2012/03/27 02:44:18
Done. Sounds good.
| |
| 123 for (std::vector<OriginRecord>::iterator db_origin_itr = origins.begin(); | |
| 124 db_origin_itr != origins.end(); | |
| 125 ++db_origin_itr) { | |
| 126 std::set<FilePath>::iterator dir_itr = | |
| 127 directories.find(db_origin_itr->path); | |
| 128 if (dir_itr == directories.end()) { | |
| 129 if (!RemovePathForOrigin(db_origin_itr->origin)) { | |
| 130 DropDatabase(); | |
| 131 return false; | |
| 132 } | |
| 133 } else { | |
| 134 directories.erase(dir_itr); | |
| 135 } | |
| 136 } | |
| 137 | |
| 138 // Delete any directories not listed in the origins database. | |
| 139 for (std::set<FilePath>::iterator dir_itr = directories.begin(); | |
| 140 dir_itr != directories.end(); | |
| 141 ++dir_itr) { | |
| 142 if (!file_util::Delete(file_system_directory_.Append(*dir_itr), | |
| 143 true /* recursive */)) { | |
| 144 DropDatabase(); | |
| 145 return false; | |
| 146 } | |
| 147 } | |
| 148 | |
| 149 return true; | |
| 150 } | |
| 151 | |
| 73 void FileSystemOriginDatabase::HandleError( | 152 void FileSystemOriginDatabase::HandleError( |
| 74 const tracked_objects::Location& from_here, leveldb::Status status) { | 153 const tracked_objects::Location& from_here, leveldb::Status status) { |
| 75 db_.reset(); | 154 db_.reset(); |
| 76 LOG(ERROR) << "FileSystemOriginDatabase failed at: " | 155 LOG(ERROR) << "FileSystemOriginDatabase failed at: " |
| 77 << from_here.ToString() << " with error: " << status.ToString(); | 156 << from_here.ToString() << " with error: " << status.ToString(); |
| 78 } | 157 } |
| 79 | 158 |
| 80 bool FileSystemOriginDatabase::HasOriginPath(const std::string& origin) { | 159 bool FileSystemOriginDatabase::HasOriginPath(const std::string& origin) { |
| 81 if (!Init()) | 160 if (!Init(REPAIR_ON_CORRUPTION)) |
| 82 return false; | 161 return false; |
| 83 if (origin.empty()) | 162 if (origin.empty()) |
| 84 return false; | 163 return false; |
| 85 std::string path; | 164 std::string path; |
| 86 leveldb::Status status = | 165 leveldb::Status status = |
| 87 db_->Get(leveldb::ReadOptions(), OriginToOriginKey(origin), &path); | 166 db_->Get(leveldb::ReadOptions(), OriginToOriginKey(origin), &path); |
| 88 if (status.ok()) | 167 if (status.ok()) |
| 89 return true; | 168 return true; |
| 90 if (status.IsNotFound()) | 169 if (status.IsNotFound()) |
| 91 return false; | 170 return false; |
| 92 HandleError(FROM_HERE, status); | 171 HandleError(FROM_HERE, status); |
| 93 return false; | 172 return false; |
| 94 } | 173 } |
| 95 | 174 |
| 96 bool FileSystemOriginDatabase::GetPathForOrigin( | 175 bool FileSystemOriginDatabase::GetPathForOrigin( |
| 97 const std::string& origin, FilePath* directory) { | 176 const std::string& origin, FilePath* directory) { |
| 98 if (!Init()) | 177 if (!Init(REPAIR_ON_CORRUPTION)) |
| 99 return false; | 178 return false; |
| 100 DCHECK(directory); | 179 DCHECK(directory); |
| 101 if (origin.empty()) | 180 if (origin.empty()) |
| 102 return false; | 181 return false; |
| 103 std::string path_string; | 182 std::string path_string; |
| 104 std::string origin_key = OriginToOriginKey(origin); | 183 std::string origin_key = OriginToOriginKey(origin); |
| 105 leveldb::Status status = | 184 leveldb::Status status = |
| 106 db_->Get(leveldb::ReadOptions(), origin_key, &path_string); | 185 db_->Get(leveldb::ReadOptions(), origin_key, &path_string); |
| 107 if (status.IsNotFound()) { | 186 if (status.IsNotFound()) { |
| 108 int last_path_number; | 187 int last_path_number; |
| 109 if (!GetLastPathNumber(&last_path_number)) | 188 if (!GetLastPathNumber(&last_path_number)) |
| 110 return false; | 189 return false; |
| 111 path_string = StringPrintf("%03u", last_path_number + 1); | 190 path_string = StringPrintf("%03u", last_path_number + 1); |
| 112 // store both back as a single transaction | 191 // store both back as a single transaction |
| 113 leveldb::WriteBatch batch; | 192 leveldb::WriteBatch batch; |
| 114 batch.Put(LastPathKey(), path_string); | 193 batch.Put(LastPathKey(), path_string); |
| 115 batch.Put(origin_key, path_string); | 194 batch.Put(origin_key, path_string); |
| 116 status = db_->Write(leveldb::WriteOptions(), &batch); | 195 status = db_->Write(leveldb::WriteOptions(), &batch); |
| 117 if (!status.ok()) { | 196 if (!status.ok()) { |
| 118 HandleError(FROM_HERE, status); | 197 HandleError(FROM_HERE, status); |
| 119 return false; | 198 return false; |
| 120 } | 199 } |
| 121 } | 200 } |
| 122 if (status.ok()) { | 201 if (status.ok()) { |
| 123 #if defined(OS_POSIX) | 202 *directory = StringToFilePath(path_string); |
| 124 *directory = FilePath(path_string); | |
| 125 #elif defined(OS_WIN) | |
| 126 *directory = FilePath(base::SysUTF8ToWide(path_string)); | |
| 127 #endif | |
| 128 return true; | 203 return true; |
| 129 } | 204 } |
| 130 HandleError(FROM_HERE, status); | 205 HandleError(FROM_HERE, status); |
| 131 return false; | 206 return false; |
| 132 } | 207 } |
| 133 | 208 |
| 134 bool FileSystemOriginDatabase::RemovePathForOrigin(const std::string& origin) { | 209 bool FileSystemOriginDatabase::RemovePathForOrigin(const std::string& origin) { |
| 135 if (!Init()) | 210 if (!Init(REPAIR_ON_CORRUPTION)) |
| 136 return false; | 211 return false; |
| 137 leveldb::Status status = | 212 leveldb::Status status = |
| 138 db_->Delete(leveldb::WriteOptions(), OriginToOriginKey(origin)); | 213 db_->Delete(leveldb::WriteOptions(), OriginToOriginKey(origin)); |
| 139 if (status.ok() || status.IsNotFound()) | 214 if (status.ok() || status.IsNotFound()) |
| 140 return true; | 215 return true; |
| 141 HandleError(FROM_HERE, status); | 216 HandleError(FROM_HERE, status); |
| 142 return false; | 217 return false; |
| 143 } | 218 } |
| 144 | 219 |
| 145 bool FileSystemOriginDatabase::ListAllOrigins( | 220 bool FileSystemOriginDatabase::ListAllOrigins( |
| 146 std::vector<OriginRecord>* origins) { | 221 std::vector<OriginRecord>* origins) { |
| 147 if (!Init()) | 222 if (!Init(REPAIR_ON_CORRUPTION)) |
| 148 return false; | 223 return false; |
| 149 DCHECK(origins); | 224 DCHECK(origins); |
| 150 scoped_ptr<leveldb::Iterator> iter(db_->NewIterator(leveldb::ReadOptions())); | 225 scoped_ptr<leveldb::Iterator> iter(db_->NewIterator(leveldb::ReadOptions())); |
| 151 std::string origin_key_prefix = OriginToOriginKey(""); | 226 std::string origin_key_prefix = OriginToOriginKey(""); |
| 152 iter->Seek(origin_key_prefix); | 227 iter->Seek(origin_key_prefix); |
| 153 origins->clear(); | 228 origins->clear(); |
| 154 while(iter->Valid() && | 229 while(iter->Valid() && |
| 155 StartsWithASCII(iter->key().ToString(), origin_key_prefix, true)) { | 230 StartsWithASCII(iter->key().ToString(), origin_key_prefix, true)) { |
| 156 std::string origin = | 231 std::string origin = |
| 157 iter->key().ToString().substr(origin_key_prefix.length()); | 232 iter->key().ToString().substr(origin_key_prefix.length()); |
| 158 #if defined(OS_POSIX) | 233 FilePath path = StringToFilePath(iter->value().ToString()); |
| 159 FilePath path = FilePath(iter->value().ToString()); | |
| 160 #elif defined(OS_WIN) | |
| 161 FilePath path = FilePath(base::SysUTF8ToWide(iter->value().ToString())); | |
| 162 #endif | |
| 163 origins->push_back(OriginRecord(origin, path)); | 234 origins->push_back(OriginRecord(origin, path)); |
| 164 iter->Next(); | 235 iter->Next(); |
| 165 } | 236 } |
| 166 return true; | 237 return true; |
| 167 } | 238 } |
| 168 | 239 |
| 169 void FileSystemOriginDatabase::DropDatabase() { | 240 void FileSystemOriginDatabase::DropDatabase() { |
| 170 db_.reset(); | 241 db_.reset(); |
| 171 } | 242 } |
| 172 | 243 |
| 173 bool FileSystemOriginDatabase::GetLastPathNumber(int* number) { | 244 bool FileSystemOriginDatabase::GetLastPathNumber(int* number) { |
| 174 if (!Init()) | 245 if (!Init(REPAIR_ON_CORRUPTION)) |
| 175 return false; | 246 return false; |
| 176 DCHECK(number); | 247 DCHECK(number); |
| 177 std::string number_string; | 248 std::string number_string; |
| 178 leveldb::Status status = | 249 leveldb::Status status = |
| 179 db_->Get(leveldb::ReadOptions(), LastPathKey(), &number_string); | 250 db_->Get(leveldb::ReadOptions(), LastPathKey(), &number_string); |
| 180 if (status.ok()) | 251 if (status.ok()) |
| 181 return base::StringToInt(number_string, number); | 252 return base::StringToInt(number_string, number); |
| 182 if (!status.IsNotFound()) { | 253 if (!status.IsNotFound()) { |
| 183 HandleError(FROM_HERE, status); | 254 HandleError(FROM_HERE, status); |
| 184 return false; | 255 return false; |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 196 db_->Put(leveldb::WriteOptions(), LastPathKey(), std::string("-1")); | 267 db_->Put(leveldb::WriteOptions(), LastPathKey(), std::string("-1")); |
| 197 if (!status.ok()) { | 268 if (!status.ok()) { |
| 198 HandleError(FROM_HERE, status); | 269 HandleError(FROM_HERE, status); |
| 199 return false; | 270 return false; |
| 200 } | 271 } |
| 201 *number = -1; | 272 *number = -1; |
| 202 return true; | 273 return true; |
| 203 } | 274 } |
| 204 | 275 |
| 205 } // namespace fileapi | 276 } // namespace fileapi |
| OLD | NEW |