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) { | |
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); |
Scott Hess - ex-Googler
2015/06/10 22:35:45
I don't think it's appropriate to change this code
Elliot Glaysher
2015/06/12 22:36:43
Per our conversation yesterday, I added a check on
Scott Hess - ex-Googler
2015/06/17 19:32:58
I'm happier with it, though not ecstatic. At leas
| |
632 !base::PathExists(wal_path) && | 640 CHECK(vfs); |
633 !base::PathExists(path); | 641 CHECK(vfs->xDelete); |
642 CHECK(vfs->xAccess); | |
643 | |
644 vfs->xDelete(vfs, journal_str.c_str(), 0); | |
645 vfs->xDelete(vfs, wal_str.c_str(), 0); | |
646 vfs->xDelete(vfs, path_str.c_str(), 0); | |
647 | |
648 int journal_exists = 0; | |
649 vfs->xAccess(vfs, journal_str.c_str(), SQLITE_ACCESS_EXISTS, | |
650 &journal_exists); | |
651 | |
652 int wal_exists = 0; | |
653 vfs->xAccess(vfs, wal_str.c_str(), SQLITE_ACCESS_EXISTS, | |
654 &wal_exists); | |
655 | |
656 int path_exists = 0; | |
657 vfs->xAccess(vfs, path_str.c_str(), SQLITE_ACCESS_EXISTS, | |
658 &path_exists); | |
659 | |
660 return !journal_exists && !wal_exists && !path_exists; | |
634 } | 661 } |
635 | 662 |
636 bool Connection::BeginTransaction() { | 663 bool Connection::BeginTransaction() { |
637 if (needs_rollback_) { | 664 if (needs_rollback_) { |
638 DCHECK_GT(transaction_nesting_, 0); | 665 DCHECK_GT(transaction_nesting_, 0); |
639 | 666 |
640 // When we're going to rollback, fail on this begin and don't actually | 667 // When we're going to rollback, fail on this begin and don't actually |
641 // mark us as entering the nested transaction. | 668 // mark us as entering the nested transaction. |
642 return false; | 669 return false; |
643 } | 670 } |
(...skipping 663 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1307 ignore_result(Execute(kNoWritableSchema)); | 1334 ignore_result(Execute(kNoWritableSchema)); |
1308 | 1335 |
1309 return ret; | 1336 return ret; |
1310 } | 1337 } |
1311 | 1338 |
1312 base::TimeTicks TimeSource::Now() { | 1339 base::TimeTicks TimeSource::Now() { |
1313 return base::TimeTicks::Now(); | 1340 return base::TimeTicks::Now(); |
1314 } | 1341 } |
1315 | 1342 |
1316 } // namespace sql | 1343 } // namespace sql |
OLD | NEW |