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/sys_string_conversions.h" |
| 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" |
| 16 | 17 |
| 17 namespace { | 18 namespace { |
| 18 | 19 |
| 20 const FilePath::CharType kOriginDatabaseName[] = FILE_PATH_LITERAL("Origins"); | |
| 19 const char kOriginKeyPrefix[] = "ORIGIN:"; | 21 const char kOriginKeyPrefix[] = "ORIGIN:"; |
| 20 const char kLastPathKey[] = "LAST_PATH"; | 22 const char kLastPathKey[] = "LAST_PATH"; |
| 21 | 23 |
| 22 std::string OriginToOriginKey(const std::string& origin) { | 24 std::string OriginToOriginKey(const std::string& origin) { |
| 23 std::string key(kOriginKeyPrefix); | 25 std::string key(kOriginKeyPrefix); |
| 24 return key + origin; | 26 return key + origin; |
| 25 } | 27 } |
| 26 | 28 |
| 27 const char* LastPathKey() { | 29 const char* LastPathKey() { |
| 28 return kLastPathKey; | 30 return kLastPathKey; |
| 29 } | 31 } |
| 30 | 32 |
| 31 } | 33 } |
| 32 | 34 |
| 33 namespace fileapi { | 35 namespace fileapi { |
| 34 | 36 |
| 35 FileSystemOriginDatabase::OriginRecord::OriginRecord() { | 37 FileSystemOriginDatabase::OriginRecord::OriginRecord() { |
| 36 } | 38 } |
| 37 | 39 |
| 38 FileSystemOriginDatabase::OriginRecord::OriginRecord( | 40 FileSystemOriginDatabase::OriginRecord::OriginRecord( |
| 39 const std::string& origin_in, const FilePath& path_in) | 41 const std::string& origin_in, const FilePath& path_in) |
| 40 : origin(origin_in), path(path_in) { | 42 : origin(origin_in), path(path_in) { |
| 41 } | 43 } |
| 42 | 44 |
| 43 FileSystemOriginDatabase::OriginRecord::~OriginRecord() { | 45 FileSystemOriginDatabase::OriginRecord::~OriginRecord() { |
| 44 } | 46 } |
| 45 | 47 |
| 46 FileSystemOriginDatabase::FileSystemOriginDatabase(const FilePath& path) { | 48 FileSystemOriginDatabase::FileSystemOriginDatabase( |
| 47 #if defined(OS_POSIX) | 49 const FilePath& file_system_directory) |
| 48 path_ = path.value(); | 50 : file_system_directory_(file_system_directory) { |
| 49 #elif defined(OS_WIN) | |
| 50 path_ = base::SysWideToUTF8(path.value()); | |
| 51 #endif | |
| 52 } | 51 } |
| 53 | 52 |
| 54 FileSystemOriginDatabase::~FileSystemOriginDatabase() { | 53 FileSystemOriginDatabase::~FileSystemOriginDatabase() { |
| 55 } | 54 } |
| 56 | 55 |
| 57 bool FileSystemOriginDatabase::Init() { | 56 bool FileSystemOriginDatabase::Init(RecoveryOption recovery_option) { |
| 58 if (db_.get()) | 57 if (db_.get()) |
| 59 return true; | 58 return true; |
| 60 | 59 |
| 60 std::string path; | |
| 61 #if defined(OS_POSIX) | |
| 62 path = file_system_directory_.Append(kOriginDatabaseName).value(); | |
| 63 #elif defined(OS_WIN) | |
| 64 path = base::SysWideToUTF8( | |
| 65 file_system_directory_.Append(kOriginDatabaseName).value()); | |
| 66 #else | |
| 67 NOTREACHED(); | |
| 68 #endif | |
| 69 | |
| 61 leveldb::Options options; | 70 leveldb::Options options; |
| 62 options.create_if_missing = true; | 71 options.create_if_missing = true; |
| 63 leveldb::DB* db; | 72 leveldb::DB* db; |
| 64 leveldb::Status status = leveldb::DB::Open(options, path_, &db); | 73 leveldb::Status status = leveldb::DB::Open(options, path, &db); |
| 74 // TODO(tzik): Collect status metrics here. | |
| 65 if (status.ok()) { | 75 if (status.ok()) { |
| 66 db_.reset(db); | 76 db_.reset(db); |
| 67 return true; | 77 return true; |
| 68 } | 78 } |
| 69 HandleError(FROM_HERE, status); | 79 HandleError(FROM_HERE, status); |
| 70 return false; | 80 |
| 81 if (recovery_option == FAIL_ON_CORRUPTION) | |
| 82 return false; | |
| 83 | |
| 84 if (recovery_option == REPAIR_ON_CORRUPTION) | |
| 85 return RepairDB(path); | |
| 86 | |
| 87 DCHECK_EQ(DELETE_ON_CORRUPTION, recovery_option); | |
| 88 if (!file_util::Delete(file_system_directory_, true)) | |
| 89 return false; | |
| 90 if (!file_util::CreateDirectory(file_system_directory_)) | |
| 91 return false; | |
| 92 | |
| 93 return Init(FAIL_ON_CORRUPTION); | |
| 94 } | |
| 95 | |
| 96 bool FileSystemOriginDatabase::RepairDB(const std::string& db_path) { | |
| 97 DCHECK(!db_.get()); | |
| 98 if (!leveldb::RepairDB(db_path, leveldb::Options()).ok()) | |
| 99 return false; | |
| 100 if (!Init(FAIL_ON_CORRUPTION)) | |
| 101 return false; | |
| 102 | |
| 103 std::set<FilePath> directories; | |
| 104 file_util::FileEnumerator file_enum(file_system_directory_, | |
| 105 false /* recursive */, | |
| 106 file_util::FileEnumerator::DIRECTORIES); | |
| 107 FilePath path_each; | |
| 108 while (!(path_each = file_enum.Next()).empty()) | |
| 109 directories.insert(path_each.BaseName()); | |
| 110 std::set<FilePath>::iterator db_dir_itr = | |
| 111 directories.find(FilePath(kOriginDatabaseName)); | |
| 112 DCHECK(db_dir_itr != directories.end()); | |
| 113 directories.erase(db_dir_itr); | |
| 114 | |
| 115 std::vector<OriginRecord> origins; | |
| 116 if (!ListAllOrigins(&origins)) | |
| 117 return false; | |
| 118 for (std::vector<OriginRecord>::iterator db_origin_itr = origins.begin(); | |
| 119 db_origin_itr != origins.end(); | |
| 120 ++db_origin_itr) { | |
| 121 std::set<FilePath>::iterator dir_itr = | |
| 122 directories.find(db_origin_itr->path); | |
| 123 if (dir_itr != directories.end()) { | |
| 124 if (!RemovePathForOrigin(db_origin_itr->origin)) | |
| 125 return false; | |
| 126 directories.erase(dir_itr); | |
| 127 } | |
| 128 } | |
| 129 | |
| 130 for (std::set<FilePath>::iterator dir_itr = directories.begin(); | |
|
ericu
2012/03/19 16:50:05
Add comment: "Delete any directories not listed in
tzik
2012/03/21 08:59:21
Done.
| |
| 131 dir_itr != directories.end(); | |
| 132 ++dir_itr) | |
| 133 if (!file_util::Delete(*dir_itr, true /* recursive */)) | |
| 134 return false; | |
| 135 | |
| 136 return true; | |
| 71 } | 137 } |
| 72 | 138 |
| 73 void FileSystemOriginDatabase::HandleError( | 139 void FileSystemOriginDatabase::HandleError( |
| 74 const tracked_objects::Location& from_here, leveldb::Status status) { | 140 const tracked_objects::Location& from_here, leveldb::Status status) { |
| 75 db_.reset(); | 141 db_.reset(); |
| 76 LOG(ERROR) << "FileSystemOriginDatabase failed at: " | 142 LOG(ERROR) << "FileSystemOriginDatabase failed at: " |
| 77 << from_here.ToString() << " with error: " << status.ToString(); | 143 << from_here.ToString() << " with error: " << status.ToString(); |
| 78 } | 144 } |
| 79 | 145 |
| 80 bool FileSystemOriginDatabase::HasOriginPath(const std::string& origin) { | 146 bool FileSystemOriginDatabase::HasOriginPath(const std::string& origin) { |
| 81 if (!Init()) | 147 if (!Init(REPAIR_ON_CORRUPTION)) |
| 82 return false; | 148 return false; |
| 83 if (origin.empty()) | 149 if (origin.empty()) |
| 84 return false; | 150 return false; |
| 85 std::string path; | 151 std::string path; |
| 86 leveldb::Status status = | 152 leveldb::Status status = |
| 87 db_->Get(leveldb::ReadOptions(), OriginToOriginKey(origin), &path); | 153 db_->Get(leveldb::ReadOptions(), OriginToOriginKey(origin), &path); |
| 88 if (status.ok()) | 154 if (status.ok()) |
| 89 return true; | 155 return true; |
| 90 if (status.IsNotFound()) | 156 if (status.IsNotFound()) |
| 91 return false; | 157 return false; |
| 92 HandleError(FROM_HERE, status); | 158 HandleError(FROM_HERE, status); |
| 93 return false; | 159 return false; |
| 94 } | 160 } |
| 95 | 161 |
| 96 bool FileSystemOriginDatabase::GetPathForOrigin( | 162 bool FileSystemOriginDatabase::GetPathForOrigin( |
| 97 const std::string& origin, FilePath* directory) { | 163 const std::string& origin, FilePath* directory) { |
| 98 if (!Init()) | 164 if (!Init(REPAIR_ON_CORRUPTION)) |
| 99 return false; | 165 return false; |
| 100 DCHECK(directory); | 166 DCHECK(directory); |
| 101 if (origin.empty()) | 167 if (origin.empty()) |
| 102 return false; | 168 return false; |
| 103 std::string path_string; | 169 std::string path_string; |
| 104 std::string origin_key = OriginToOriginKey(origin); | 170 std::string origin_key = OriginToOriginKey(origin); |
| 105 leveldb::Status status = | 171 leveldb::Status status = |
| 106 db_->Get(leveldb::ReadOptions(), origin_key, &path_string); | 172 db_->Get(leveldb::ReadOptions(), origin_key, &path_string); |
| 107 if (status.IsNotFound()) { | 173 if (status.IsNotFound()) { |
| 108 int last_path_number; | 174 int last_path_number; |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 125 #elif defined(OS_WIN) | 191 #elif defined(OS_WIN) |
| 126 *directory = FilePath(base::SysUTF8ToWide(path_string)); | 192 *directory = FilePath(base::SysUTF8ToWide(path_string)); |
| 127 #endif | 193 #endif |
| 128 return true; | 194 return true; |
| 129 } | 195 } |
| 130 HandleError(FROM_HERE, status); | 196 HandleError(FROM_HERE, status); |
| 131 return false; | 197 return false; |
| 132 } | 198 } |
| 133 | 199 |
| 134 bool FileSystemOriginDatabase::RemovePathForOrigin(const std::string& origin) { | 200 bool FileSystemOriginDatabase::RemovePathForOrigin(const std::string& origin) { |
| 135 if (!Init()) | 201 if (!Init(REPAIR_ON_CORRUPTION)) |
| 136 return false; | 202 return false; |
| 137 leveldb::Status status = | 203 leveldb::Status status = |
| 138 db_->Delete(leveldb::WriteOptions(), OriginToOriginKey(origin)); | 204 db_->Delete(leveldb::WriteOptions(), OriginToOriginKey(origin)); |
| 139 if (status.ok() || status.IsNotFound()) | 205 if (status.ok() || status.IsNotFound()) |
| 140 return true; | 206 return true; |
| 141 HandleError(FROM_HERE, status); | 207 HandleError(FROM_HERE, status); |
| 142 return false; | 208 return false; |
| 143 } | 209 } |
| 144 | 210 |
| 145 bool FileSystemOriginDatabase::ListAllOrigins( | 211 bool FileSystemOriginDatabase::ListAllOrigins( |
| 146 std::vector<OriginRecord>* origins) { | 212 std::vector<OriginRecord>* origins) { |
| 147 if (!Init()) | 213 if (!Init(REPAIR_ON_CORRUPTION)) |
| 148 return false; | 214 return false; |
| 149 DCHECK(origins); | 215 DCHECK(origins); |
| 150 scoped_ptr<leveldb::Iterator> iter(db_->NewIterator(leveldb::ReadOptions())); | 216 scoped_ptr<leveldb::Iterator> iter(db_->NewIterator(leveldb::ReadOptions())); |
| 151 std::string origin_key_prefix = OriginToOriginKey(""); | 217 std::string origin_key_prefix = OriginToOriginKey(""); |
| 152 iter->Seek(origin_key_prefix); | 218 iter->Seek(origin_key_prefix); |
| 153 origins->clear(); | 219 origins->clear(); |
| 154 while(iter->Valid() && | 220 while(iter->Valid() && |
| 155 StartsWithASCII(iter->key().ToString(), origin_key_prefix, true)) { | 221 StartsWithASCII(iter->key().ToString(), origin_key_prefix, true)) { |
| 156 std::string origin = | 222 std::string origin = |
| 157 iter->key().ToString().substr(origin_key_prefix.length()); | 223 iter->key().ToString().substr(origin_key_prefix.length()); |
| 158 #if defined(OS_POSIX) | 224 #if defined(OS_POSIX) |
| 159 FilePath path = FilePath(iter->value().ToString()); | 225 FilePath path = FilePath(iter->value().ToString()); |
| 160 #elif defined(OS_WIN) | 226 #elif defined(OS_WIN) |
| 161 FilePath path = FilePath(base::SysUTF8ToWide(iter->value().ToString())); | 227 FilePath path = FilePath(base::SysUTF8ToWide(iter->value().ToString())); |
| 162 #endif | 228 #endif |
| 163 origins->push_back(OriginRecord(origin, path)); | 229 origins->push_back(OriginRecord(origin, path)); |
| 164 iter->Next(); | 230 iter->Next(); |
| 165 } | 231 } |
| 166 return true; | 232 return true; |
| 167 } | 233 } |
| 168 | 234 |
| 169 void FileSystemOriginDatabase::DropDatabase() { | 235 void FileSystemOriginDatabase::DropDatabase() { |
| 170 db_.reset(); | 236 db_.reset(); |
| 171 } | 237 } |
| 172 | 238 |
| 173 bool FileSystemOriginDatabase::GetLastPathNumber(int* number) { | 239 bool FileSystemOriginDatabase::GetLastPathNumber(int* number) { |
| 174 if (!Init()) | 240 if (!Init(REPAIR_ON_CORRUPTION)) |
| 175 return false; | 241 return false; |
| 176 DCHECK(number); | 242 DCHECK(number); |
| 177 std::string number_string; | 243 std::string number_string; |
| 178 leveldb::Status status = | 244 leveldb::Status status = |
| 179 db_->Get(leveldb::ReadOptions(), LastPathKey(), &number_string); | 245 db_->Get(leveldb::ReadOptions(), LastPathKey(), &number_string); |
| 180 if (status.ok()) | 246 if (status.ok()) |
| 181 return base::StringToInt(number_string, number); | 247 return base::StringToInt(number_string, number); |
| 182 if (!status.IsNotFound()) { | 248 if (!status.IsNotFound()) { |
| 183 HandleError(FROM_HERE, status); | 249 HandleError(FROM_HERE, status); |
| 184 return false; | 250 return false; |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 196 db_->Put(leveldb::WriteOptions(), LastPathKey(), std::string("-1")); | 262 db_->Put(leveldb::WriteOptions(), LastPathKey(), std::string("-1")); |
| 197 if (!status.ok()) { | 263 if (!status.ok()) { |
| 198 HandleError(FROM_HERE, status); | 264 HandleError(FROM_HERE, status); |
| 199 return false; | 265 return false; |
| 200 } | 266 } |
| 201 *number = -1; | 267 *number = -1; |
| 202 return true; | 268 return true; |
| 203 } | 269 } |
| 204 | 270 |
| 205 } // namespace fileapi | 271 } // namespace fileapi |
| OLD | NEW |