Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 "sql/connection.h" | 5 #include "sql/connection.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| (...skipping 589 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 600 | 600 |
| 601 RollbackAllTransactions(); | 601 RollbackAllTransactions(); |
| 602 CloseInternal(true); | 602 CloseInternal(true); |
| 603 | 603 |
| 604 // Mark the database so that future API calls fail appropriately, | 604 // Mark the database so that future API calls fail appropriately, |
| 605 // but don't DCHECK (because after calling this function they are | 605 // but don't DCHECK (because after calling this function they are |
| 606 // expected to fail). | 606 // expected to fail). |
| 607 poisoned_ = true; | 607 poisoned_ = true; |
| 608 } | 608 } |
| 609 | 609 |
| 610 std::string AsUTF8ForSQL(const base::FilePath& path) { | |
|
Scott Hess - ex-Googler
2015/06/17 19:32:58
Please add this to Open()'s implementation while y
Elliot Glaysher
2015/06/17 21:59:34
Done.
| |
| 611 #if defined(OS_WIN) | |
| 612 return base::WideToUTF8(path.value()); | |
| 613 #elif defined(OS_POSIX) | |
| 614 return path.value(); | |
| 615 #endif | |
| 616 } | |
| 617 | |
| 610 // TODO(shess): To the extent possible, figure out the optimal | 618 // TODO(shess): To the extent possible, figure out the optimal |
| 611 // ordering for these deletes which will prevent other connections | 619 // ordering for these deletes which will prevent other connections |
| 612 // from seeing odd behavior. For instance, it may be necessary to | 620 // from seeing odd behavior. For instance, it may be necessary to |
| 613 // manually lock the main database file in a SQLite-compatible fashion | 621 // manually lock the main database file in a SQLite-compatible fashion |
| 614 // (to prevent other processes from opening it), then delete the | 622 // (to prevent other processes from opening it), then delete the |
| 615 // journal files, then delete the main database file. Another option | 623 // journal files, then delete the main database file. Another option |
| 616 // might be to lock the main database file and poison the header with | 624 // might be to lock the main database file and poison the header with |
| 617 // junk to prevent other processes from opening it successfully (like | 625 // junk to prevent other processes from opening it successfully (like |
| 618 // Gears "SQLite poison 3" trick). | 626 // Gears "SQLite poison 3" trick). |
| 619 // | 627 // |
| 620 // static | 628 // static |
| 621 bool Connection::Delete(const base::FilePath& path) { | 629 bool Connection::Delete(const base::FilePath& path) { |
| 622 base::ThreadRestrictions::AssertIOAllowed(); | 630 base::ThreadRestrictions::AssertIOAllowed(); |
| 623 | 631 |
| 624 base::FilePath journal_path(path.value() + FILE_PATH_LITERAL("-journal")); | 632 base::FilePath journal_path(path.value() + FILE_PATH_LITERAL("-journal")); |
| 625 base::FilePath wal_path(path.value() + FILE_PATH_LITERAL("-wal")); | 633 base::FilePath wal_path(path.value() + FILE_PATH_LITERAL("-wal")); |
| 626 | 634 |
| 627 base::DeleteFile(journal_path, false); | 635 std::string journal_str = AsUTF8ForSQL(journal_path); |
| 628 base::DeleteFile(wal_path, false); | 636 std::string wal_str = AsUTF8ForSQL(wal_path); |
| 629 base::DeleteFile(path, false); | 637 std::string path_str = AsUTF8ForSQL(path); |
| 630 | 638 |
| 631 return !base::PathExists(journal_path) && | 639 sqlite3_vfs* vfs = sqlite3_vfs_find(NULL); |
| 632 !base::PathExists(wal_path) && | 640 CHECK(vfs); |
| 633 !base::PathExists(path); | 641 CHECK(vfs->xDelete); |
| 642 CHECK(vfs->xAccess); | |
| 643 | |
| 644 // We only work with unix, win32 and mojo filesystems. If you're trying to | |
| 645 // use this code with any other VFS, you're not in a good place. | |
| 646 CHECK(strncmp(vfs->zName, "unix", 4) == 0 || | |
| 647 strncmp(vfs->zName, "win32", 5) == 0 || | |
| 648 strcmp(vfs->zName, "mojo") == 0); | |
| 649 | |
| 650 vfs->xDelete(vfs, journal_str.c_str(), 0); | |
| 651 vfs->xDelete(vfs, wal_str.c_str(), 0); | |
| 652 vfs->xDelete(vfs, path_str.c_str(), 0); | |
| 653 | |
| 654 int journal_exists = 0; | |
| 655 vfs->xAccess(vfs, journal_str.c_str(), SQLITE_ACCESS_EXISTS, | |
| 656 &journal_exists); | |
| 657 | |
| 658 int wal_exists = 0; | |
| 659 vfs->xAccess(vfs, wal_str.c_str(), SQLITE_ACCESS_EXISTS, | |
| 660 &wal_exists); | |
| 661 | |
| 662 int path_exists = 0; | |
| 663 vfs->xAccess(vfs, path_str.c_str(), SQLITE_ACCESS_EXISTS, | |
| 664 &path_exists); | |
| 665 | |
| 666 return !journal_exists && !wal_exists && !path_exists; | |
| 634 } | 667 } |
| 635 | 668 |
| 636 bool Connection::BeginTransaction() { | 669 bool Connection::BeginTransaction() { |
| 637 if (needs_rollback_) { | 670 if (needs_rollback_) { |
| 638 DCHECK_GT(transaction_nesting_, 0); | 671 DCHECK_GT(transaction_nesting_, 0); |
| 639 | 672 |
| 640 // When we're going to rollback, fail on this begin and don't actually | 673 // When we're going to rollback, fail on this begin and don't actually |
| 641 // mark us as entering the nested transaction. | 674 // mark us as entering the nested transaction. |
| 642 return false; | 675 return false; |
| 643 } | 676 } |
| (...skipping 663 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1307 ignore_result(Execute(kNoWritableSchema)); | 1340 ignore_result(Execute(kNoWritableSchema)); |
| 1308 | 1341 |
| 1309 return ret; | 1342 return ret; |
| 1310 } | 1343 } |
| 1311 | 1344 |
| 1312 base::TimeTicks TimeSource::Now() { | 1345 base::TimeTicks TimeSource::Now() { |
| 1313 return base::TimeTicks::Now(); | 1346 return base::TimeTicks::Now(); |
| 1314 } | 1347 } |
| 1315 | 1348 |
| 1316 } // namespace sql | 1349 } // namespace sql |
| OLD | NEW |