Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(84)

Side by Side Diff: webkit/fileapi/file_system_directory_database.cc

Issue 9663021: Add database recovery for FileSystemOriginDatabase (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: build fix Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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_directory_database.h" 5 #include "webkit/fileapi/file_system_directory_database.h"
6 6
7 #include <math.h> 7 #include <math.h>
8 8
9 #include "base/file_util.h"
9 #include "base/location.h" 10 #include "base/location.h"
10 #include "base/pickle.h" 11 #include "base/pickle.h"
11 #include "base/string_number_conversions.h" 12 #include "base/string_number_conversions.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"
16 #include "third_party/leveldatabase/src/include/leveldb/status.h"
15 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" 17 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h"
16 #include "webkit/fileapi/file_system_util.h" 18 #include "webkit/fileapi/file_system_util.h"
17 19
18 namespace { 20 namespace {
19 21
20 bool PickleFromFileInfo( 22 bool PickleFromFileInfo(
21 const fileapi::FileSystemDirectoryDatabase::FileInfo& info, 23 const fileapi::FileSystemDirectoryDatabase::FileInfo& info,
22 Pickle* pickle) { 24 Pickle* pickle) {
23 DCHECK(pickle); 25 DCHECK(pickle);
24 std::string data_path; 26 std::string data_path;
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 info->data_path = FilePath(base::SysUTF8ToWide(data_path)); 65 info->data_path = FilePath(base::SysUTF8ToWide(data_path));
64 info->name = base::SysUTF8ToWide(name); 66 info->name = base::SysUTF8ToWide(name);
65 #endif 67 #endif
66 info->modification_time = base::Time::FromInternalValue(internal_time); 68 info->modification_time = base::Time::FromInternalValue(internal_time);
67 return true; 69 return true;
68 } 70 }
69 LOG(ERROR) << "Pickle could not be digested!"; 71 LOG(ERROR) << "Pickle could not be digested!";
70 return false; 72 return false;
71 } 73 }
72 74
75 const FilePath::CharType kDirectoryDatabaseName[] = FILE_PATH_LITERAL("Paths");
73 const char kChildLookupPrefix[] = "CHILD_OF:"; 76 const char kChildLookupPrefix[] = "CHILD_OF:";
74 const char kChildLookupSeparator[] = ":"; 77 const char kChildLookupSeparator[] = ":";
75 const char kLastFileIdKey[] = "LAST_FILE_ID"; 78 const char kLastFileIdKey[] = "LAST_FILE_ID";
76 const char kLastIntegerKey[] = "LAST_INTEGER"; 79 const char kLastIntegerKey[] = "LAST_INTEGER";
77 80
78 std::string GetChildLookupKey( 81 std::string GetChildLookupKey(
79 fileapi::FileSystemDirectoryDatabase::FileId parent_id, 82 fileapi::FileSystemDirectoryDatabase::FileId parent_id,
80 const FilePath::StringType& child_name) { 83 const FilePath::StringType& child_name) {
81 std::string name; 84 std::string name;
82 #if defined(OS_POSIX) 85 #if defined(OS_POSIX)
(...skipping 27 matching lines...) Expand all
110 } // namespace 113 } // namespace
111 114
112 namespace fileapi { 115 namespace fileapi {
113 116
114 FileSystemDirectoryDatabase::FileInfo::FileInfo() : parent_id(0) { 117 FileSystemDirectoryDatabase::FileInfo::FileInfo() : parent_id(0) {
115 } 118 }
116 119
117 FileSystemDirectoryDatabase::FileInfo::~FileInfo() { 120 FileSystemDirectoryDatabase::FileInfo::~FileInfo() {
118 } 121 }
119 122
120 FileSystemDirectoryDatabase::FileSystemDirectoryDatabase(const FilePath& path) { 123 FileSystemDirectoryDatabase::FileSystemDirectoryDatabase(
121 #if defined(OS_POSIX) 124 const FilePath& sandbox_directory)
122 path_ = path.value(); 125 : sandbox_directory_(sandbox_directory) {
123 #elif defined(OS_WIN)
124 path_ = base::SysWideToUTF8(path.value());
125 #endif
126 } 126 }
127 127
128 FileSystemDirectoryDatabase::~FileSystemDirectoryDatabase() { 128 FileSystemDirectoryDatabase::~FileSystemDirectoryDatabase() {
129 } 129 }
130 130
131 bool FileSystemDirectoryDatabase::GetChildWithName( 131 bool FileSystemDirectoryDatabase::GetChildWithName(
132 FileId parent_id, const FilePath::StringType& name, FileId* child_id) { 132 FileId parent_id, const FilePath::StringType& name, FileId* child_id) {
133 if (!Init()) 133 if (!Init(FAIL_ON_CORRUPTION))
134 return false; 134 return false;
135 DCHECK(child_id); 135 DCHECK(child_id);
136 std::string child_key = GetChildLookupKey(parent_id, name); 136 std::string child_key = GetChildLookupKey(parent_id, name);
137 std::string child_id_string; 137 std::string child_id_string;
138 leveldb::Status status = 138 leveldb::Status status =
139 db_->Get(leveldb::ReadOptions(), child_key, &child_id_string); 139 db_->Get(leveldb::ReadOptions(), child_key, &child_id_string);
140 if (status.IsNotFound()) 140 if (status.IsNotFound())
141 return false; 141 return false;
142 if (status.ok()) { 142 if (status.ok()) {
143 if (!base::StringToInt64(child_id_string, child_id)) { 143 if (!base::StringToInt64(child_id_string, child_id)) {
(...skipping 20 matching lines...) Expand all
164 if (!GetChildWithName(local_id, name, &local_id)) 164 if (!GetChildWithName(local_id, name, &local_id))
165 return false; 165 return false;
166 } 166 }
167 *file_id = local_id; 167 *file_id = local_id;
168 return true; 168 return true;
169 } 169 }
170 170
171 bool FileSystemDirectoryDatabase::ListChildren( 171 bool FileSystemDirectoryDatabase::ListChildren(
172 FileId parent_id, std::vector<FileId>* children) { 172 FileId parent_id, std::vector<FileId>* children) {
173 // Check to add later: fail if parent is a file, at least in debug builds. 173 // Check to add later: fail if parent is a file, at least in debug builds.
174 if (!Init()) 174 if (!Init(FAIL_ON_CORRUPTION))
175 return false; 175 return false;
176 DCHECK(children); 176 DCHECK(children);
177 std::string child_key_prefix = GetChildListingKeyPrefix(parent_id); 177 std::string child_key_prefix = GetChildListingKeyPrefix(parent_id);
178 178
179 scoped_ptr<leveldb::Iterator> iter(db_->NewIterator(leveldb::ReadOptions())); 179 scoped_ptr<leveldb::Iterator> iter(db_->NewIterator(leveldb::ReadOptions()));
180 iter->Seek(child_key_prefix); 180 iter->Seek(child_key_prefix);
181 children->clear(); 181 children->clear();
182 while (iter->Valid() && 182 while (iter->Valid() &&
183 StartsWithASCII(iter->key().ToString(), child_key_prefix, true)) { 183 StartsWithASCII(iter->key().ToString(), child_key_prefix, true)) {
184 std::string child_id_string = iter->value().ToString(); 184 std::string child_id_string = iter->value().ToString();
185 FileId child_id; 185 FileId child_id;
186 if (!base::StringToInt64(child_id_string, &child_id)) { 186 if (!base::StringToInt64(child_id_string, &child_id)) {
187 LOG(ERROR) << "Hit database corruption!"; 187 LOG(ERROR) << "Hit database corruption!";
188 return false; 188 return false;
189 } 189 }
190 children->push_back(child_id); 190 children->push_back(child_id);
191 iter->Next(); 191 iter->Next();
192 } 192 }
193 return true; 193 return true;
194 } 194 }
195 195
196 bool FileSystemDirectoryDatabase::GetFileInfo(FileId file_id, FileInfo* info) { 196 bool FileSystemDirectoryDatabase::GetFileInfo(FileId file_id, FileInfo* info) {
197 if (!Init()) 197 if (!Init(FAIL_ON_CORRUPTION))
198 return false; 198 return false;
199 DCHECK(info); 199 DCHECK(info);
200 std::string file_key = GetFileLookupKey(file_id); 200 std::string file_key = GetFileLookupKey(file_id);
201 std::string file_data_string; 201 std::string file_data_string;
202 leveldb::Status status = 202 leveldb::Status status =
203 db_->Get(leveldb::ReadOptions(), file_key, &file_data_string); 203 db_->Get(leveldb::ReadOptions(), file_key, &file_data_string);
204 if (status.ok()) { 204 if (status.ok()) {
205 return FileInfoFromPickle( 205 return FileInfoFromPickle(
206 Pickle(file_data_string.data(), file_data_string.length()), info); 206 Pickle(file_data_string.data(), file_data_string.length()), info);
207 } 207 }
208 // Special-case the root, for databases that haven't been initialized yet. 208 // Special-case the root, for databases that haven't been initialized yet.
209 // Without this, a query for the root's file info, made before creating the 209 // Without this, a query for the root's file info, made before creating the
210 // first file in the database, will fail and confuse callers. 210 // first file in the database, will fail and confuse callers.
211 if (status.IsNotFound() && !file_id) { 211 if (status.IsNotFound() && !file_id) {
212 info->name = FilePath::StringType(); 212 info->name = FilePath::StringType();
213 info->data_path = FilePath(); 213 info->data_path = FilePath();
214 info->modification_time = base::Time::Now(); 214 info->modification_time = base::Time::Now();
215 info->parent_id = 0; 215 info->parent_id = 0;
216 return true; 216 return true;
217 } 217 }
218 HandleError(FROM_HERE, status); 218 HandleError(FROM_HERE, status);
219 return false; 219 return false;
220 } 220 }
221 221
222 bool FileSystemDirectoryDatabase::AddFileInfo( 222 bool FileSystemDirectoryDatabase::AddFileInfo(
223 const FileInfo& info, FileId* file_id) { 223 const FileInfo& info, FileId* file_id) {
224 if (!Init()) 224 if (!Init(FAIL_ON_CORRUPTION))
225 return false; 225 return false;
226 DCHECK(file_id); 226 DCHECK(file_id);
227 std::string child_key = GetChildLookupKey(info.parent_id, info.name); 227 std::string child_key = GetChildLookupKey(info.parent_id, info.name);
228 std::string child_id_string; 228 std::string child_id_string;
229 leveldb::Status status = 229 leveldb::Status status =
230 db_->Get(leveldb::ReadOptions(), child_key, &child_id_string); 230 db_->Get(leveldb::ReadOptions(), child_key, &child_id_string);
231 if (status.ok()) { 231 if (status.ok()) {
232 LOG(ERROR) << "File exists already!"; 232 LOG(ERROR) << "File exists already!";
233 return false; 233 return false;
234 } 234 }
(...skipping 21 matching lines...) Expand all
256 status = db_->Write(leveldb::WriteOptions(), &batch); 256 status = db_->Write(leveldb::WriteOptions(), &batch);
257 if (!status.ok()) { 257 if (!status.ok()) {
258 HandleError(FROM_HERE, status); 258 HandleError(FROM_HERE, status);
259 return false; 259 return false;
260 } 260 }
261 *file_id = temp_id; 261 *file_id = temp_id;
262 return true; 262 return true;
263 } 263 }
264 264
265 bool FileSystemDirectoryDatabase::RemoveFileInfo(FileId file_id) { 265 bool FileSystemDirectoryDatabase::RemoveFileInfo(FileId file_id) {
266 if (!Init()) 266 if (!Init(FAIL_ON_CORRUPTION))
267 return false; 267 return false;
268 leveldb::WriteBatch batch; 268 leveldb::WriteBatch batch;
269 if (!RemoveFileInfoHelper(file_id, &batch)) 269 if (!RemoveFileInfoHelper(file_id, &batch))
270 return false; 270 return false;
271 leveldb::Status status = db_->Write(leveldb::WriteOptions(), &batch); 271 leveldb::Status status = db_->Write(leveldb::WriteOptions(), &batch);
272 if (!status.ok()) { 272 if (!status.ok()) {
273 HandleError(FROM_HERE, status); 273 HandleError(FROM_HERE, status);
274 return false; 274 return false;
275 } 275 }
276 return true; 276 return true;
277 } 277 }
278 278
279 bool FileSystemDirectoryDatabase::UpdateFileInfo( 279 bool FileSystemDirectoryDatabase::UpdateFileInfo(
280 FileId file_id, const FileInfo& new_info) { 280 FileId file_id, const FileInfo& new_info) {
281 // TODO: We should also check to see that this doesn't create a loop, but 281 // TODO: We should also check to see that this doesn't create a loop, but
282 // perhaps only in a debug build. 282 // perhaps only in a debug build.
283 if (!Init()) 283 if (!Init(FAIL_ON_CORRUPTION))
284 return false; 284 return false;
285 DCHECK(file_id); // You can't remove the root, ever. Just delete the DB. 285 DCHECK(file_id); // You can't remove the root, ever. Just delete the DB.
286 FileInfo old_info; 286 FileInfo old_info;
287 if (!GetFileInfo(file_id, &old_info)) 287 if (!GetFileInfo(file_id, &old_info))
288 return false; 288 return false;
289 if (old_info.parent_id != new_info.parent_id && 289 if (old_info.parent_id != new_info.parent_id &&
290 !VerifyIsDirectory(new_info.parent_id)) 290 !VerifyIsDirectory(new_info.parent_id))
291 return false; 291 return false;
292 if (old_info.parent_id != new_info.parent_id || 292 if (old_info.parent_id != new_info.parent_id ||
293 old_info.name != new_info.name) { 293 old_info.name != new_info.name) {
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
357 pickle.size())); 357 pickle.size()));
358 leveldb::Status status = db_->Write(leveldb::WriteOptions(), &batch); 358 leveldb::Status status = db_->Write(leveldb::WriteOptions(), &batch);
359 if (!status.ok()) { 359 if (!status.ok()) {
360 HandleError(FROM_HERE, status); 360 HandleError(FROM_HERE, status);
361 return false; 361 return false;
362 } 362 }
363 return true; 363 return true;
364 } 364 }
365 365
366 bool FileSystemDirectoryDatabase::GetNextInteger(int64* next) { 366 bool FileSystemDirectoryDatabase::GetNextInteger(int64* next) {
367 if (!Init()) 367 if (!Init(FAIL_ON_CORRUPTION))
368 return false; 368 return false;
369 DCHECK(next); 369 DCHECK(next);
370 std::string int_string; 370 std::string int_string;
371 leveldb::Status status = 371 leveldb::Status status =
372 db_->Get(leveldb::ReadOptions(), LastIntegerKey(), &int_string); 372 db_->Get(leveldb::ReadOptions(), LastIntegerKey(), &int_string);
373 if (status.ok()) { 373 if (status.ok()) {
374 int64 temp; 374 int64 temp;
375 if (!base::StringToInt64(int_string, &temp)) { 375 if (!base::StringToInt64(int_string, &temp)) {
376 LOG(ERROR) << "Hit database corruption!"; 376 LOG(ERROR) << "Hit database corruption!";
377 return false; 377 return false;
(...skipping 28 matching lines...) Expand all
406 name = base::SysWideToUTF8(path.value()); 406 name = base::SysWideToUTF8(path.value());
407 #endif 407 #endif
408 leveldb::Status status = leveldb::DestroyDB(name, leveldb::Options()); 408 leveldb::Status status = leveldb::DestroyDB(name, leveldb::Options());
409 if (status.ok()) 409 if (status.ok())
410 return true; 410 return true;
411 LOG(WARNING) << "Failed to destroy a database with status " << 411 LOG(WARNING) << "Failed to destroy a database with status " <<
412 status.ToString(); 412 status.ToString();
413 return false; 413 return false;
414 } 414 }
415 415
416 bool FileSystemDirectoryDatabase::Init() { 416 bool FileSystemDirectoryDatabase::Init(RecoveryOption recovery_option) {
417 if (db_.get()) 417 if (db_.get())
418 return true; 418 return true;
419 419
420 leveldb::Options options; 420 std::string path;
421 options.create_if_missing = true; 421 #if defined(OS_POSIX)
422 leveldb::DB* db; 422 path = sandbox_directory_.Append(kDirectoryDatabaseName).value();
423 leveldb::Status status = leveldb::DB::Open(options, path_, &db); 423 #elif defined(OS_WIN)
424 if (status.ok()) { 424 path = base::SysWideToUTF8(
425 db_.reset(db); 425 sandbox_directory_.Append(kDirectoryDatabaseName).value());
kinuko 2012/03/23 04:01:33 Can we use AsUTF8Unsafe() and remove ifdefs?
kinuko 2012/03/23 06:27:21 (We chatted offline) ok this does something slight
tzik 2012/03/23 07:09:00 On linux, it calls SysNativeMBToWide() and WideToU
426 return true; 426 #else
427 } 427 NOTREACHED()
428 HandleError(FROM_HERE, status); 428 #endif
429 return false; 429
430 leveldb::Options options;
431 options.create_if_missing = true;
432 leveldb::DB* db;
433 leveldb::Status status = leveldb::DB::Open(options, path, &db);
434 // TODO(tzik): Collect status metrics here.
435 if (status.ok()) {
436 db_.reset(db);
437 return true;
438 }
439 HandleError(FROM_HERE, status);
440
441 if (recovery_option == FAIL_ON_CORRUPTION)
442 return false;
443
444 DCHECK_EQ(FAIL_ON_CORRUPTION, recovery_option);
kinuko 2012/03/23 04:01:33 DELETE_ON_CORRUPTION?
tzik 2012/03/23 07:09:00 Done.
445 if (!file_util::Delete(sandbox_directory_, true))
446 return false;
447 if (!file_util::CreateDirectory(sandbox_directory_))
448 return false;
449 return Init(FAIL_ON_CORRUPTION);
430 } 450 }
431 451
432 bool FileSystemDirectoryDatabase::StoreDefaultValues() { 452 bool FileSystemDirectoryDatabase::StoreDefaultValues() {
433 // Verify that this is a totally new database, and initialize it. 453 // Verify that this is a totally new database, and initialize it.
434 scoped_ptr<leveldb::Iterator> iter(db_->NewIterator(leveldb::ReadOptions())); 454 scoped_ptr<leveldb::Iterator> iter(db_->NewIterator(leveldb::ReadOptions()));
435 iter->SeekToFirst(); 455 iter->SeekToFirst();
436 if (iter->Valid()) { // DB was not empty--we shouldn't have been called. 456 if (iter->Valid()) { // DB was not empty--we shouldn't have been called.
437 LOG(ERROR) << "File system origin database is corrupt!"; 457 LOG(ERROR) << "File system origin database is corrupt!";
438 return false; 458 return false;
439 } 459 }
440 // This is always the first write into the database. If we ever add a 460 // This is always the first write into the database. If we ever add a
441 // version number, it should go in this transaction too. 461 // version number, it should go in this transaction too.
442 FileInfo root; 462 FileInfo root;
443 root.parent_id = 0; 463 root.parent_id = 0;
444 root.modification_time = base::Time::Now(); 464 root.modification_time = base::Time::Now();
445 leveldb::WriteBatch batch; 465 leveldb::WriteBatch batch;
446 if (!AddFileInfoHelper(root, 0, &batch)) 466 if (!AddFileInfoHelper(root, 0, &batch))
447 return false; 467 return false;
448 batch.Put(LastFileIdKey(), base::Int64ToString(0)); 468 batch.Put(LastFileIdKey(), base::Int64ToString(0));
449 batch.Put(LastIntegerKey(), base::Int64ToString(-1)); 469 batch.Put(LastIntegerKey(), base::Int64ToString(-1));
450 leveldb::Status status = db_->Write(leveldb::WriteOptions(), &batch); 470 leveldb::Status status = db_->Write(leveldb::WriteOptions(), &batch);
451 if (!status.ok()) { 471 if (!status.ok()) {
452 HandleError(FROM_HERE, status); 472 HandleError(FROM_HERE, status);
453 return false; 473 return false;
454 } 474 }
455 return true; 475 return true;
456 } 476 }
457 477
458 bool FileSystemDirectoryDatabase::GetLastFileId(FileId* file_id) { 478 bool FileSystemDirectoryDatabase::GetLastFileId(FileId* file_id) {
459 if (!Init()) 479 if (!Init(FAIL_ON_CORRUPTION))
460 return false; 480 return false;
461 DCHECK(file_id); 481 DCHECK(file_id);
462 std::string id_string; 482 std::string id_string;
463 leveldb::Status status = 483 leveldb::Status status =
464 db_->Get(leveldb::ReadOptions(), LastFileIdKey(), &id_string); 484 db_->Get(leveldb::ReadOptions(), LastFileIdKey(), &id_string);
465 if (status.ok()) { 485 if (status.ok()) {
466 if (!base::StringToInt64(id_string, file_id)) { 486 if (!base::StringToInt64(id_string, file_id)) {
467 LOG(ERROR) << "Hit database corruption!"; 487 LOG(ERROR) << "Hit database corruption!";
468 return false; 488 return false;
469 } 489 }
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
539 559
540 void FileSystemDirectoryDatabase::HandleError( 560 void FileSystemDirectoryDatabase::HandleError(
541 const tracked_objects::Location& from_here, 561 const tracked_objects::Location& from_here,
542 leveldb::Status status) { 562 leveldb::Status status) {
543 LOG(ERROR) << "FileSystemDirectoryDatabase failed at: " 563 LOG(ERROR) << "FileSystemDirectoryDatabase failed at: "
544 << from_here.ToString() << " with error: " << status.ToString(); 564 << from_here.ToString() << " with error: " << status.ToString();
545 db_.reset(); 565 db_.reset();
546 } 566 }
547 567
548 } // namespace fileapi 568 } // namespace fileapi
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698