| 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" |
| 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); |
| 70 return false; | 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 default: |
| 91 NOTREACHED(); |
| 92 return false; |
| 93 } |
| 94 } |
| 95 |
| 96 bool FileSystemOriginDatabase::RepairDatabase(const std::string& db_path) { |
| 97 DCHECK(!db_.get()); |
| 98 if (!leveldb::RepairDB(db_path, leveldb::Options()).ok() || |
| 99 !Init(FAIL_ON_CORRUPTION)) { |
| 100 LOG(WARNING) << "Failed to repair FileSystemOriginDatabase."; |
| 101 return false; |
| 102 } |
| 103 |
| 104 // See if the repaired entries match with what we have on disk. |
| 105 std::set<FilePath> directories; |
| 106 file_util::FileEnumerator file_enum(file_system_directory_, |
| 107 false /* recursive */, |
| 108 file_util::FileEnumerator::DIRECTORIES); |
| 109 FilePath path_each; |
| 110 while (!(path_each = file_enum.Next()).empty()) |
| 111 directories.insert(path_each.BaseName()); |
| 112 std::set<FilePath>::iterator db_dir_itr = |
| 113 directories.find(FilePath(kOriginDatabaseName)); |
| 114 // Make sure we have the database file in its directory and therefore we are |
| 115 // working on the correct path. |
| 116 DCHECK(db_dir_itr != directories.end()); |
| 117 directories.erase(db_dir_itr); |
| 118 |
| 119 std::vector<OriginRecord> origins; |
| 120 if (!ListAllOrigins(&origins)) { |
| 121 DropDatabase(); |
| 122 return false; |
| 123 } |
| 124 for (std::vector<OriginRecord>::iterator db_origin_itr = origins.begin(); |
| 125 db_origin_itr != origins.end(); |
| 126 ++db_origin_itr) { |
| 127 std::set<FilePath>::iterator dir_itr = |
| 128 directories.find(db_origin_itr->path); |
| 129 if (dir_itr == directories.end()) { |
| 130 if (!RemovePathForOrigin(db_origin_itr->origin)) { |
| 131 DropDatabase(); |
| 132 return false; |
| 133 } |
| 134 } else { |
| 135 directories.erase(dir_itr); |
| 136 } |
| 137 } |
| 138 |
| 139 // Delete any directories not listed in the origins database. |
| 140 for (std::set<FilePath>::iterator dir_itr = directories.begin(); |
| 141 dir_itr != directories.end(); |
| 142 ++dir_itr) { |
| 143 if (!file_util::Delete(file_system_directory_.Append(*dir_itr), |
| 144 true /* recursive */)) { |
| 145 DropDatabase(); |
| 146 return false; |
| 147 } |
| 148 } |
| 149 |
| 150 return true; |
| 71 } | 151 } |
| 72 | 152 |
| 73 void FileSystemOriginDatabase::HandleError( | 153 void FileSystemOriginDatabase::HandleError( |
| 74 const tracked_objects::Location& from_here, leveldb::Status status) { | 154 const tracked_objects::Location& from_here, leveldb::Status status) { |
| 75 db_.reset(); | 155 db_.reset(); |
| 76 LOG(ERROR) << "FileSystemOriginDatabase failed at: " | 156 LOG(ERROR) << "FileSystemOriginDatabase failed at: " |
| 77 << from_here.ToString() << " with error: " << status.ToString(); | 157 << from_here.ToString() << " with error: " << status.ToString(); |
| 78 } | 158 } |
| 79 | 159 |
| 80 bool FileSystemOriginDatabase::HasOriginPath(const std::string& origin) { | 160 bool FileSystemOriginDatabase::HasOriginPath(const std::string& origin) { |
| 81 if (!Init()) | 161 if (!Init(REPAIR_ON_CORRUPTION)) |
| 82 return false; | 162 return false; |
| 83 if (origin.empty()) | 163 if (origin.empty()) |
| 84 return false; | 164 return false; |
| 85 std::string path; | 165 std::string path; |
| 86 leveldb::Status status = | 166 leveldb::Status status = |
| 87 db_->Get(leveldb::ReadOptions(), OriginToOriginKey(origin), &path); | 167 db_->Get(leveldb::ReadOptions(), OriginToOriginKey(origin), &path); |
| 88 if (status.ok()) | 168 if (status.ok()) |
| 89 return true; | 169 return true; |
| 90 if (status.IsNotFound()) | 170 if (status.IsNotFound()) |
| 91 return false; | 171 return false; |
| 92 HandleError(FROM_HERE, status); | 172 HandleError(FROM_HERE, status); |
| 93 return false; | 173 return false; |
| 94 } | 174 } |
| 95 | 175 |
| 96 bool FileSystemOriginDatabase::GetPathForOrigin( | 176 bool FileSystemOriginDatabase::GetPathForOrigin( |
| 97 const std::string& origin, FilePath* directory) { | 177 const std::string& origin, FilePath* directory) { |
| 98 if (!Init()) | 178 if (!Init(REPAIR_ON_CORRUPTION)) |
| 99 return false; | 179 return false; |
| 100 DCHECK(directory); | 180 DCHECK(directory); |
| 101 if (origin.empty()) | 181 if (origin.empty()) |
| 102 return false; | 182 return false; |
| 103 std::string path_string; | 183 std::string path_string; |
| 104 std::string origin_key = OriginToOriginKey(origin); | 184 std::string origin_key = OriginToOriginKey(origin); |
| 105 leveldb::Status status = | 185 leveldb::Status status = |
| 106 db_->Get(leveldb::ReadOptions(), origin_key, &path_string); | 186 db_->Get(leveldb::ReadOptions(), origin_key, &path_string); |
| 107 if (status.IsNotFound()) { | 187 if (status.IsNotFound()) { |
| 108 int last_path_number; | 188 int last_path_number; |
| 109 if (!GetLastPathNumber(&last_path_number)) | 189 if (!GetLastPathNumber(&last_path_number)) |
| 110 return false; | 190 return false; |
| 111 path_string = StringPrintf("%03u", last_path_number + 1); | 191 path_string = StringPrintf("%03u", last_path_number + 1); |
| 112 // store both back as a single transaction | 192 // store both back as a single transaction |
| 113 leveldb::WriteBatch batch; | 193 leveldb::WriteBatch batch; |
| 114 batch.Put(LastPathKey(), path_string); | 194 batch.Put(LastPathKey(), path_string); |
| 115 batch.Put(origin_key, path_string); | 195 batch.Put(origin_key, path_string); |
| 116 status = db_->Write(leveldb::WriteOptions(), &batch); | 196 status = db_->Write(leveldb::WriteOptions(), &batch); |
| 117 if (!status.ok()) { | 197 if (!status.ok()) { |
| 118 HandleError(FROM_HERE, status); | 198 HandleError(FROM_HERE, status); |
| 119 return false; | 199 return false; |
| 120 } | 200 } |
| 121 } | 201 } |
| 122 if (status.ok()) { | 202 if (status.ok()) { |
| 123 #if defined(OS_POSIX) | 203 *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; | 204 return true; |
| 129 } | 205 } |
| 130 HandleError(FROM_HERE, status); | 206 HandleError(FROM_HERE, status); |
| 131 return false; | 207 return false; |
| 132 } | 208 } |
| 133 | 209 |
| 134 bool FileSystemOriginDatabase::RemovePathForOrigin(const std::string& origin) { | 210 bool FileSystemOriginDatabase::RemovePathForOrigin(const std::string& origin) { |
| 135 if (!Init()) | 211 if (!Init(REPAIR_ON_CORRUPTION)) |
| 136 return false; | 212 return false; |
| 137 leveldb::Status status = | 213 leveldb::Status status = |
| 138 db_->Delete(leveldb::WriteOptions(), OriginToOriginKey(origin)); | 214 db_->Delete(leveldb::WriteOptions(), OriginToOriginKey(origin)); |
| 139 if (status.ok() || status.IsNotFound()) | 215 if (status.ok() || status.IsNotFound()) |
| 140 return true; | 216 return true; |
| 141 HandleError(FROM_HERE, status); | 217 HandleError(FROM_HERE, status); |
| 142 return false; | 218 return false; |
| 143 } | 219 } |
| 144 | 220 |
| 145 bool FileSystemOriginDatabase::ListAllOrigins( | 221 bool FileSystemOriginDatabase::ListAllOrigins( |
| 146 std::vector<OriginRecord>* origins) { | 222 std::vector<OriginRecord>* origins) { |
| 147 if (!Init()) | 223 if (!Init(REPAIR_ON_CORRUPTION)) |
| 148 return false; | 224 return false; |
| 149 DCHECK(origins); | 225 DCHECK(origins); |
| 150 scoped_ptr<leveldb::Iterator> iter(db_->NewIterator(leveldb::ReadOptions())); | 226 scoped_ptr<leveldb::Iterator> iter(db_->NewIterator(leveldb::ReadOptions())); |
| 151 std::string origin_key_prefix = OriginToOriginKey(""); | 227 std::string origin_key_prefix = OriginToOriginKey(""); |
| 152 iter->Seek(origin_key_prefix); | 228 iter->Seek(origin_key_prefix); |
| 153 origins->clear(); | 229 origins->clear(); |
| 154 while(iter->Valid() && | 230 while(iter->Valid() && |
| 155 StartsWithASCII(iter->key().ToString(), origin_key_prefix, true)) { | 231 StartsWithASCII(iter->key().ToString(), origin_key_prefix, true)) { |
| 156 std::string origin = | 232 std::string origin = |
| 157 iter->key().ToString().substr(origin_key_prefix.length()); | 233 iter->key().ToString().substr(origin_key_prefix.length()); |
| 158 #if defined(OS_POSIX) | 234 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)); | 235 origins->push_back(OriginRecord(origin, path)); |
| 164 iter->Next(); | 236 iter->Next(); |
| 165 } | 237 } |
| 166 return true; | 238 return true; |
| 167 } | 239 } |
| 168 | 240 |
| 169 void FileSystemOriginDatabase::DropDatabase() { | 241 void FileSystemOriginDatabase::DropDatabase() { |
| 170 db_.reset(); | 242 db_.reset(); |
| 171 } | 243 } |
| 172 | 244 |
| 173 bool FileSystemOriginDatabase::GetLastPathNumber(int* number) { | 245 bool FileSystemOriginDatabase::GetLastPathNumber(int* number) { |
| 174 if (!Init()) | 246 if (!Init(REPAIR_ON_CORRUPTION)) |
| 175 return false; | 247 return false; |
| 176 DCHECK(number); | 248 DCHECK(number); |
| 177 std::string number_string; | 249 std::string number_string; |
| 178 leveldb::Status status = | 250 leveldb::Status status = |
| 179 db_->Get(leveldb::ReadOptions(), LastPathKey(), &number_string); | 251 db_->Get(leveldb::ReadOptions(), LastPathKey(), &number_string); |
| 180 if (status.ok()) | 252 if (status.ok()) |
| 181 return base::StringToInt(number_string, number); | 253 return base::StringToInt(number_string, number); |
| 182 if (!status.IsNotFound()) { | 254 if (!status.IsNotFound()) { |
| 183 HandleError(FROM_HERE, status); | 255 HandleError(FROM_HERE, status); |
| 184 return false; | 256 return false; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 196 db_->Put(leveldb::WriteOptions(), LastPathKey(), std::string("-1")); | 268 db_->Put(leveldb::WriteOptions(), LastPathKey(), std::string("-1")); |
| 197 if (!status.ok()) { | 269 if (!status.ok()) { |
| 198 HandleError(FROM_HERE, status); | 270 HandleError(FROM_HERE, status); |
| 199 return false; | 271 return false; |
| 200 } | 272 } |
| 201 *number = -1; | 273 *number = -1; |
| 202 return true; | 274 return true; |
| 203 } | 275 } |
| 204 | 276 |
| 205 } // namespace fileapi | 277 } // namespace fileapi |
| OLD | NEW |