summaryrefslogtreecommitdiffstats
path: root/sql
diff options
context:
space:
mode:
authorshess@chromium.org <shess@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-06-24 05:55:08 +0000
committershess@chromium.org <shess@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-06-24 05:55:08 +0000
commit8d2e39e922347608d78c2260279c3072d03232db (patch)
treecadc844773d447080ace8201412961e665f6407b /sql
parent8b9211d4e1de4531f7b462625a0a5ceec1d4dcce (diff)
downloadchromium_src-8d2e39e922347608d78c2260279c3072d03232db.zip
chromium_src-8d2e39e922347608d78c2260279c3072d03232db.tar.gz
chromium_src-8d2e39e922347608d78c2260279c3072d03232db.tar.bz2
[sql] Static helper to delete database and all associated files.
Not all of the ad-hoc file_util::Delete() cases handle -journal, and if -wal was added all of them need to be tracked down. BUG=none Review URL: https://chromiumcodereview.appspot.com/17058004 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@208140 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'sql')
-rw-r--r--sql/connection.cc26
-rw-r--r--sql/connection.h12
-rw-r--r--sql/connection_unittest.cc15
3 files changed, 53 insertions, 0 deletions
diff --git a/sql/connection.cc b/sql/connection.cc
index 67cdf08..e99b6bc 100644
--- a/sql/connection.cc
+++ b/sql/connection.cc
@@ -377,6 +377,32 @@ bool Connection::RazeAndClose() {
return result;
}
+// TODO(shess): To the extent possible, figure out the optimal
+// ordering for these deletes which will prevent other connections
+// from seeing odd behavior. For instance, it may be necessary to
+// manually lock the main database file in a SQLite-compatible fashion
+// (to prevent other processes from opening it), then delete the
+// journal files, then delete the main database file. Another option
+// might be to lock the main database file and poison the header with
+// junk to prevent other processes from opening it successfully (like
+// Gears "SQLite poison 3" trick).
+//
+// static
+bool Connection::Delete(const base::FilePath& path) {
+ base::ThreadRestrictions::AssertIOAllowed();
+
+ base::FilePath journal_path(path.value() + FILE_PATH_LITERAL("-journal"));
+ base::FilePath wal_path(path.value() + FILE_PATH_LITERAL("-wal"));
+
+ file_util::Delete(journal_path, false);
+ file_util::Delete(wal_path, false);
+ file_util::Delete(path, false);
+
+ return !file_util::PathExists(journal_path) &&
+ !file_util::PathExists(wal_path) &&
+ !file_util::PathExists(path);
+}
+
bool Connection::BeginTransaction() {
if (needs_rollback_) {
DCHECK_GT(transaction_nesting_, 0);
diff --git a/sql/connection.h b/sql/connection.h
index 91139ce..6b03849 100644
--- a/sql/connection.h
+++ b/sql/connection.h
@@ -224,6 +224,18 @@ class SQL_EXPORT Connection {
// cases.
bool RazeAndClose();
+ // Delete the underlying database files associated with |path|.
+ // This should be used on a database which has no existing
+ // connections. If any other connections are open to the same
+ // database, this could cause odd results or corruption (for
+ // instance if a hot journal is deleted but the associated database
+ // is not).
+ //
+ // Returns true if the database file and associated journals no
+ // longer exist, false otherwise. If the database has never
+ // existed, this will return true.
+ static bool Delete(const base::FilePath& path);
+
// Transactions --------------------------------------------------------------
// Transaction management. We maintain a virtual transaction stack to emulate
diff --git a/sql/connection_unittest.cc b/sql/connection_unittest.cc
index b43e83c..1afd2dd 100644
--- a/sql/connection_unittest.cc
+++ b/sql/connection_unittest.cc
@@ -411,3 +411,18 @@ TEST_F(SQLConnectionTest, SetTempDirForSQL) {
ASSERT_TRUE(meta_table.Init(&db(), 4, 4));
}
#endif
+
+TEST_F(SQLConnectionTest, Delete) {
+ EXPECT_TRUE(db().Execute("CREATE TABLE x (x)"));
+ db().Close();
+
+ // Should have both a main database file and a journal file because
+ // of journal_mode PERSIST.
+ base::FilePath journal(db_path().value() + FILE_PATH_LITERAL("-journal"));
+ ASSERT_TRUE(file_util::PathExists(db_path()));
+ ASSERT_TRUE(file_util::PathExists(journal));
+
+ sql::Connection::Delete(db_path());
+ EXPECT_FALSE(file_util::PathExists(db_path()));
+ EXPECT_FALSE(file_util::PathExists(journal));
+}