 Chromium Code Reviews
 Chromium Code Reviews Issue 9663021:
  Add database recovery for FileSystemOriginDatabase  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src
    
  
    Issue 9663021:
  Add database recovery for FileSystemOriginDatabase  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src| 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 | |
| 
kinuko
2012/03/23 04:01:33
Can we use AsUTF8Unsafe and remove ifdefs? (Here a
 
tzik
2012/03/23 07:09:00
ditto
 | |
| 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 && RepairDB(path)) | |
| 85 return true; | |
| 86 | |
| 87 DCHECK(DELETE_ON_CORRUPTION == recovery_option || | |
| 88 REPAIR_ON_CORRUPTION == recovery_option); | |
| 89 if (!file_util::Delete(file_system_directory_, true)) | |
| 90 return false; | |
| 91 if (!file_util::CreateDirectory(file_system_directory_)) | |
| 92 return false; | |
| 93 | |
| 94 return Init(FAIL_ON_CORRUPTION); | |
| 95 } | |
| 96 | |
| 97 bool FileSystemOriginDatabase::RepairDB(const std::string& db_path) { | |
| 98 DCHECK(!db_.get()); | |
| 99 if (!leveldb::RepairDB(db_path, leveldb::Options()).ok()) | |
| 100 return false; | |
| 101 if (!Init(FAIL_ON_CORRUPTION)) | |
| 102 return false; | |
| 
kinuko
2012/03/23 04:01:33
If we think we shouldn't fail here (or somewhere)
 
tzik
2012/03/23 07:09:00
Done.
 | |
| 103 | |
| 
kinuko
2012/03/23 04:01:33
Can we add a brief comment to state what we're doi
 
tzik
2012/03/23 07:09:00
Done.
 | |
| 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()); | |
| 
kinuko
2012/03/23 04:01:33
Maybe we could skip inserting path here if it is k
 
tzik
2012/03/23 07:09:00
I'd like to leave it if it won't hurt anything.
Th
 
kinuko
2012/03/26 05:25:09
Ok.  Might be worth adding a short comment for the
 
tzik
2012/03/26 08:04:56
Done.
 | |
| 111 std::set<FilePath>::iterator db_dir_itr = | |
| 112 directories.find(FilePath(kOriginDatabaseName)); | |
| 113 DCHECK(db_dir_itr != directories.end()); | |
| 114 directories.erase(db_dir_itr); | |
| 115 | |
| 116 std::vector<OriginRecord> origins; | |
| 117 if (!ListAllOrigins(&origins)) { | |
| 118 DropDatabase(); | |
| 119 return false; | |
| 120 } | |
| 121 for (std::vector<OriginRecord>::iterator db_origin_itr = origins.begin(); | |
| 122 db_origin_itr != origins.end(); | |
| 123 ++db_origin_itr) { | |
| 124 std::set<FilePath>::iterator dir_itr = | |
| 125 directories.find(db_origin_itr->path); | |
| 126 if (dir_itr == directories.end()) { | |
| 127 if (!RemovePathForOrigin(db_origin_itr->origin)) { | |
| 128 DropDatabase(); | |
| 129 return false; | |
| 130 } | |
| 131 } else { | |
| 132 directories.erase(dir_itr); | |
| 133 } | |
| 134 } | |
| 135 | |
| 136 // Delete any directories not listed in the origins database. | |
| 137 for (std::set<FilePath>::iterator dir_itr = directories.begin(); | |
| 138 dir_itr != directories.end(); | |
| 139 ++dir_itr) | |
| 140 if (!file_util::Delete(file_system_directory_.Append(*dir_itr), | |
| 141 true /* recursive */)) { | |
| 142 DropDatabase(); | |
| 143 return false; | |
| 144 } | |
| 
kinuko
2012/03/23 04:01:33
Please put { } around for body if it spans multipl
 
tzik
2012/03/23 07:09:00
Done.
 | |
| 145 | |
| 146 return true; | |
| 71 } | 147 } | 
| 72 | 148 | 
| 73 void FileSystemOriginDatabase::HandleError( | 149 void FileSystemOriginDatabase::HandleError( | 
| 74 const tracked_objects::Location& from_here, leveldb::Status status) { | 150 const tracked_objects::Location& from_here, leveldb::Status status) { | 
| 75 db_.reset(); | 151 db_.reset(); | 
| 76 LOG(ERROR) << "FileSystemOriginDatabase failed at: " | 152 LOG(ERROR) << "FileSystemOriginDatabase failed at: " | 
| 77 << from_here.ToString() << " with error: " << status.ToString(); | 153 << from_here.ToString() << " with error: " << status.ToString(); | 
| 78 } | 154 } | 
| 79 | 155 | 
| 80 bool FileSystemOriginDatabase::HasOriginPath(const std::string& origin) { | 156 bool FileSystemOriginDatabase::HasOriginPath(const std::string& origin) { | 
| 81 if (!Init()) | 157 if (!Init(REPAIR_ON_CORRUPTION)) | 
| 82 return false; | 158 return false; | 
| 83 if (origin.empty()) | 159 if (origin.empty()) | 
| 84 return false; | 160 return false; | 
| 85 std::string path; | 161 std::string path; | 
| 86 leveldb::Status status = | 162 leveldb::Status status = | 
| 87 db_->Get(leveldb::ReadOptions(), OriginToOriginKey(origin), &path); | 163 db_->Get(leveldb::ReadOptions(), OriginToOriginKey(origin), &path); | 
| 88 if (status.ok()) | 164 if (status.ok()) | 
| 89 return true; | 165 return true; | 
| 90 if (status.IsNotFound()) | 166 if (status.IsNotFound()) | 
| 91 return false; | 167 return false; | 
| 92 HandleError(FROM_HERE, status); | 168 HandleError(FROM_HERE, status); | 
| 93 return false; | 169 return false; | 
| 94 } | 170 } | 
| 95 | 171 | 
| 96 bool FileSystemOriginDatabase::GetPathForOrigin( | 172 bool FileSystemOriginDatabase::GetPathForOrigin( | 
| 97 const std::string& origin, FilePath* directory) { | 173 const std::string& origin, FilePath* directory) { | 
| 98 if (!Init()) | 174 if (!Init(REPAIR_ON_CORRUPTION)) | 
| 99 return false; | 175 return false; | 
| 100 DCHECK(directory); | 176 DCHECK(directory); | 
| 101 if (origin.empty()) | 177 if (origin.empty()) | 
| 102 return false; | 178 return false; | 
| 103 std::string path_string; | 179 std::string path_string; | 
| 104 std::string origin_key = OriginToOriginKey(origin); | 180 std::string origin_key = OriginToOriginKey(origin); | 
| 105 leveldb::Status status = | 181 leveldb::Status status = | 
| 106 db_->Get(leveldb::ReadOptions(), origin_key, &path_string); | 182 db_->Get(leveldb::ReadOptions(), origin_key, &path_string); | 
| 107 if (status.IsNotFound()) { | 183 if (status.IsNotFound()) { | 
| 108 int last_path_number; | 184 int last_path_number; | 
| (...skipping 16 matching lines...) Expand all Loading... | |
| 125 #elif defined(OS_WIN) | 201 #elif defined(OS_WIN) | 
| 126 *directory = FilePath(base::SysUTF8ToWide(path_string)); | 202 *directory = FilePath(base::SysUTF8ToWide(path_string)); | 
| 127 #endif | 203 #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 #if defined(OS_POSIX) | 
| 159 FilePath path = FilePath(iter->value().ToString()); | 235 FilePath path = FilePath(iter->value().ToString()); | 
| 160 #elif defined(OS_WIN) | 236 #elif defined(OS_WIN) | 
| 161 FilePath path = FilePath(base::SysUTF8ToWide(iter->value().ToString())); | 237 FilePath path = FilePath(base::SysUTF8ToWide(iter->value().ToString())); | 
| 162 #endif | 238 #endif | 
| 163 origins->push_back(OriginRecord(origin, path)); | 239 origins->push_back(OriginRecord(origin, path)); | 
| 164 iter->Next(); | 240 iter->Next(); | 
| 165 } | 241 } | 
| 166 return true; | 242 return true; | 
| 167 } | 243 } | 
| 168 | 244 | 
| 169 void FileSystemOriginDatabase::DropDatabase() { | 245 void FileSystemOriginDatabase::DropDatabase() { | 
| 170 db_.reset(); | 246 db_.reset(); | 
| 171 } | 247 } | 
| 172 | 248 | 
| 173 bool FileSystemOriginDatabase::GetLastPathNumber(int* number) { | 249 bool FileSystemOriginDatabase::GetLastPathNumber(int* number) { | 
| 174 if (!Init()) | 250 if (!Init(REPAIR_ON_CORRUPTION)) | 
| 175 return false; | 251 return false; | 
| 176 DCHECK(number); | 252 DCHECK(number); | 
| 177 std::string number_string; | 253 std::string number_string; | 
| 178 leveldb::Status status = | 254 leveldb::Status status = | 
| 179 db_->Get(leveldb::ReadOptions(), LastPathKey(), &number_string); | 255 db_->Get(leveldb::ReadOptions(), LastPathKey(), &number_string); | 
| 180 if (status.ok()) | 256 if (status.ok()) | 
| 181 return base::StringToInt(number_string, number); | 257 return base::StringToInt(number_string, number); | 
| 182 if (!status.IsNotFound()) { | 258 if (!status.IsNotFound()) { | 
| 183 HandleError(FROM_HERE, status); | 259 HandleError(FROM_HERE, status); | 
| 184 return false; | 260 return false; | 
| (...skipping 11 matching lines...) Expand all Loading... | |
| 196 db_->Put(leveldb::WriteOptions(), LastPathKey(), std::string("-1")); | 272 db_->Put(leveldb::WriteOptions(), LastPathKey(), std::string("-1")); | 
| 197 if (!status.ok()) { | 273 if (!status.ok()) { | 
| 198 HandleError(FROM_HERE, status); | 274 HandleError(FROM_HERE, status); | 
| 199 return false; | 275 return false; | 
| 200 } | 276 } | 
| 201 *number = -1; | 277 *number = -1; | 
| 202 return true; | 278 return true; | 
| 203 } | 279 } | 
| 204 | 280 | 
| 205 } // namespace fileapi | 281 } // namespace fileapi | 
| OLD | NEW |