diff options
author | Gabriel Ivascu <gabrielivascu@gnome.org> | 2017-10-03 17:56:11 +0200 |
---|---|---|
committer | Gabriel Ivascu <gabrielivascu@gnome.org> | 2017-10-03 18:29:53 +0200 |
commit | b015f75398c10dbf86ba20016e361a35b398bfc0 (patch) | |
tree | eee6d08385dcb892922c2856a4d620363089f265 | |
parent | 2e686fba88016d0265bf3fa1252f21b01bb0408a (diff) | |
download | epiphany-wip/google-safe-browsing.tar.gz |
sqlite-connection: Add function to delete database filewip/google-safe-browsing
EphySQLiteConnection now stores the path to the database file.
This removes the duplicate code in EphyHistoryService and EphyGSBStorage
and also removes the need of ensure_empty_database() in ephy-sqlite-test.c
-rw-r--r-- | lib/ephy-sqlite-connection.c | 39 | ||||
-rw-r--r-- | lib/ephy-sqlite-connection.h | 5 | ||||
-rw-r--r-- | lib/history/ephy-history-service.c | 16 | ||||
-rw-r--r-- | lib/safe-browsing/ephy-gsb-storage.c | 49 | ||||
-rw-r--r-- | src/bookmarks/ephy-bookmarks-import.c | 4 | ||||
-rw-r--r-- | src/profile-migrator/ephy-profile-migrator.c | 5 | ||||
-rw-r--r-- | tests/ephy-sqlite-test.c | 93 |
7 files changed, 117 insertions, 94 deletions
diff --git a/lib/ephy-sqlite-connection.c b/lib/ephy-sqlite-connection.c index 8f91e6b2b..e470bf6aa 100644 --- a/lib/ephy-sqlite-connection.c +++ b/lib/ephy-sqlite-connection.c @@ -23,11 +23,14 @@ #include "ephy-lib-type-builtins.h" +#include <errno.h> +#include <glib/gstdio.h> #include <sqlite3.h> struct _EphySQLiteConnection { GObject parent_instance; + char *database_path; sqlite3 *database; EphySQLiteConnectionMode mode; }; @@ -37,6 +40,7 @@ G_DEFINE_TYPE (EphySQLiteConnection, ephy_sqlite_connection, G_TYPE_OBJECT); enum { PROP_0, PROP_MODE, + PROP_DATABASE_PATH, LAST_PROP }; @@ -45,6 +49,7 @@ static GParamSpec *obj_properties[LAST_PROP]; static void ephy_sqlite_connection_finalize (GObject *self) { + g_free (EPHY_SQLITE_CONNECTION (self)->database_path); ephy_sqlite_connection_close (EPHY_SQLITE_CONNECTION (self)); G_OBJECT_CLASS (ephy_sqlite_connection_parent_class)->finalize (self); } @@ -61,6 +66,9 @@ ephy_sqlite_connection_set_property (GObject *object, case PROP_MODE: self->mode = g_value_get_enum (value); break; + case PROP_DATABASE_PATH: + self->database_path = g_value_dup_string (value); + break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (self, property_id, pspec); break; @@ -83,6 +91,13 @@ ephy_sqlite_connection_class_init (EphySQLiteConnectionClass *klass) EPHY_SQLITE_CONNECTION_MODE_READWRITE, G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS); + obj_properties[PROP_DATABASE_PATH] = + g_param_spec_string ("database-path", + "Database path", + "The path of the SQLite database file", + NULL, + G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS); + g_object_class_install_properties (gobject_class, LAST_PROP, obj_properties); } @@ -105,22 +120,23 @@ set_error_from_string (const char *string, GError **error) } EphySQLiteConnection * -ephy_sqlite_connection_new (EphySQLiteConnectionMode mode) +ephy_sqlite_connection_new (EphySQLiteConnectionMode mode, const char *database_path) { return EPHY_SQLITE_CONNECTION (g_object_new (EPHY_TYPE_SQLITE_CONNECTION, "mode", mode, + "database-path", database_path, NULL)); } gboolean -ephy_sqlite_connection_open (EphySQLiteConnection *self, const gchar *filename, GError **error) +ephy_sqlite_connection_open (EphySQLiteConnection *self, GError **error) { if (self->database) { set_error_from_string ("Connection already open.", error); return FALSE; } - if (sqlite3_open_v2 (filename, + if (sqlite3_open_v2 (self->database_path, &self->database, self->mode == EPHY_SQLITE_CONNECTION_MODE_READ_ONLY ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, @@ -143,6 +159,23 @@ ephy_sqlite_connection_close (EphySQLiteConnection *self) } void +ephy_sqlite_connection_delete_database (EphySQLiteConnection *self) +{ + char *journal; + + g_assert (EPHY_IS_SQLITE_CONNECTION (self)); + + if (g_file_test (self->database_path, G_FILE_TEST_EXISTS) && g_unlink (self->database_path) == -1) + g_warning ("Failed to delete database at %s: %s", self->database_path, g_strerror (errno)); + + journal = g_strdup_printf ("%s-journal", self->database_path); + if (g_file_test (journal, G_FILE_TEST_EXISTS) && g_unlink (journal) == -1) + g_warning ("Failed to delete database journal at %s: %s", journal, g_strerror (errno)); + + g_free (journal); +} + +void ephy_sqlite_connection_get_error (EphySQLiteConnection *self, GError **error) { if (error) diff --git a/lib/ephy-sqlite-connection.h b/lib/ephy-sqlite-connection.h index b332c270d..868a45a2f 100644 --- a/lib/ephy-sqlite-connection.h +++ b/lib/ephy-sqlite-connection.h @@ -36,10 +36,11 @@ typedef enum { EPHY_SQLITE_CONNECTION_MODE_READWRITE } EphySQLiteConnectionMode; -EphySQLiteConnection * ephy_sqlite_connection_new (EphySQLiteConnectionMode mode); +EphySQLiteConnection * ephy_sqlite_connection_new (EphySQLiteConnectionMode mode, const char *database_path); -gboolean ephy_sqlite_connection_open (EphySQLiteConnection *self, const gchar *filename, GError **error); +gboolean ephy_sqlite_connection_open (EphySQLiteConnection *self, GError **error); void ephy_sqlite_connection_close (EphySQLiteConnection *self); +void ephy_sqlite_connection_delete_database (EphySQLiteConnection *self); void ephy_sqlite_connection_get_error (EphySQLiteConnection *self, GError **error); diff --git a/lib/history/ephy-history-service.c b/lib/history/ephy-history-service.c index 448c4ebef..2dcc8f9ce 100644 --- a/lib/history/ephy-history-service.c +++ b/lib/history/ephy-history-service.c @@ -402,8 +402,9 @@ ephy_history_service_open_database_connections (EphyHistoryService *self) g_object_unref (self->history_database); self->history_database = ephy_sqlite_connection_new (self->read_only ? EPHY_SQLITE_CONNECTION_MODE_READ_ONLY - : EPHY_SQLITE_CONNECTION_MODE_READWRITE); - ephy_sqlite_connection_open (self->history_database, self->history_filename, &error); + : EPHY_SQLITE_CONNECTION_MODE_READWRITE, + self->history_filename); + ephy_sqlite_connection_open (self->history_database, &error); if (error) { g_object_unref (self->history_database); self->history_database = NULL; @@ -1128,22 +1129,13 @@ ephy_history_service_execute_clear (EphyHistoryService *self, gpointer pointer, gpointer *result) { - char *journal_filename; - if (self->history_database == NULL || self->read_only) return FALSE; ephy_history_service_commit_transaction (self); ephy_sqlite_connection_close (self->history_database); - - if (g_unlink (self->history_filename) == -1) - g_warning ("Failed to delete %s: %s", self->history_filename, g_strerror (errno)); - - journal_filename = g_strdup_printf ("%s-journal", self->history_filename); - if (g_unlink (journal_filename) == -1 && errno != ENOENT) - g_warning ("Failed to delete %s: %s", journal_filename, g_strerror (errno)); - g_free (journal_filename); + ephy_sqlite_connection_delete_database (self->history_database); ephy_history_service_open_database_connections (self); ephy_history_service_open_transaction (self); diff --git a/lib/safe-browsing/ephy-gsb-storage.c b/lib/safe-browsing/ephy-gsb-storage.c index 18950ee64..157ff4a5b 100644 --- a/lib/safe-browsing/ephy-gsb-storage.c +++ b/lib/safe-browsing/ephy-gsb-storage.c @@ -24,8 +24,6 @@ #include "ephy-debug.h" #include "ephy-sqlite-connection.h" -#include <errno.h> -#include <glib/gstdio.h> #include <string.h> #define EXPIRATION_THRESHOLD (8 * 60 * 60) @@ -287,16 +285,6 @@ ephy_gsb_storage_init_hash_full_table (EphyGSBStorage *self) return TRUE; } -static void -ephy_gsb_storage_close_db (EphyGSBStorage *self) -{ - g_assert (EPHY_IS_GSB_STORAGE (self)); - g_assert (EPHY_IS_SQLITE_CONNECTION (self->db)); - - ephy_sqlite_connection_close (self->db); - g_clear_object (&self->db); -} - static gboolean ephy_gsb_storage_open_db (EphyGSBStorage *self) { @@ -305,8 +293,8 @@ ephy_gsb_storage_open_db (EphyGSBStorage *self) g_assert (EPHY_IS_GSB_STORAGE (self)); g_assert (!self->db); - self->db = ephy_sqlite_connection_new (EPHY_SQLITE_CONNECTION_MODE_READWRITE); - ephy_sqlite_connection_open (self->db, self->db_path, &error); + self->db = ephy_sqlite_connection_new (EPHY_SQLITE_CONNECTION_MODE_READWRITE, self->db_path); + ephy_sqlite_connection_open (self->db, &error); if (error) { g_warning ("Failed to open GSB database at %s: %s", self->db_path, error->message); g_error_free (error); @@ -325,23 +313,6 @@ ephy_gsb_storage_open_db (EphyGSBStorage *self) return TRUE; } -static void -ephy_gsb_storage_delete_db (EphyGSBStorage *self) -{ - char *journal; - - g_assert (EPHY_IS_GSB_STORAGE (self)); - - if (g_unlink (self->db_path) == -1 && errno != ENOENT) - g_warning ("Failed to delete GSB database at %s: %s", self->db_path, g_strerror (errno)); - - journal = g_strdup_printf ("%s-journal", self->db_path); - if (g_unlink (journal) == -1 && errno != ENOENT) - g_warning ("Failed to delete GSB database journal at %s: %s", journal, g_strerror (errno)); - - g_free (journal); -} - static gboolean ephy_gsb_storage_init_db (EphyGSBStorage *self) { @@ -359,8 +330,9 @@ ephy_gsb_storage_init_db (EphyGSBStorage *self) ephy_gsb_storage_init_hash_full_table (self); if (!success) { - ephy_gsb_storage_close_db (self); - ephy_gsb_storage_delete_db (self); + ephy_sqlite_connection_close (self->db); + ephy_sqlite_connection_delete_database (self->db); + g_clear_object (&self->db); } return success; @@ -420,8 +392,10 @@ ephy_gsb_storage_finalize (GObject *object) EphyGSBStorage *self = EPHY_GSB_STORAGE (object); g_free (self->db_path); - if (self->db) - ephy_gsb_storage_close_db (self); + if (self->db) { + ephy_sqlite_connection_close (self->db); + g_object_unref (self->db); + } G_OBJECT_CLASS (ephy_gsb_storage_parent_class)->finalize (object); } @@ -442,8 +416,9 @@ ephy_gsb_storage_constructed (GObject *object) success = ephy_gsb_storage_open_db (self); if (success && !ephy_gsb_storage_check_schema_version (self)) { LOG ("GSB database schema incompatibility, recreating database..."); - ephy_gsb_storage_close_db (self); - ephy_gsb_storage_delete_db (self); + ephy_sqlite_connection_close (self->db); + ephy_sqlite_connection_delete_database (self->db); + g_clear_object (&self->db); success = ephy_gsb_storage_init_db (self); } } diff --git a/src/bookmarks/ephy-bookmarks-import.c b/src/bookmarks/ephy-bookmarks-import.c index c68476ebc..b8cfd0aed 100644 --- a/src/bookmarks/ephy-bookmarks-import.c +++ b/src/bookmarks/ephy-bookmarks-import.c @@ -229,8 +229,8 @@ ephy_bookmarks_import_from_firefox (EphyBookmarksManager *manager, FIREFOX_BOOKMARKS_FILE, NULL); - connection = ephy_sqlite_connection_new (EPHY_SQLITE_CONNECTION_MODE_READ_ONLY); - ephy_sqlite_connection_open (connection, filename, error); + connection = ephy_sqlite_connection_new (EPHY_SQLITE_CONNECTION_MODE_READ_ONLY, filename); + ephy_sqlite_connection_open (connection, error); if (*error) { g_warning ("Could not open database at %s: %s", filename, (*error)->message); g_error_free (*error); diff --git a/src/profile-migrator/ephy-profile-migrator.c b/src/profile-migrator/ephy-profile-migrator.c index 4204fa056..f4e75b41f 100644 --- a/src/profile-migrator/ephy-profile-migrator.c +++ b/src/profile-migrator/ephy-profile-migrator.c @@ -762,8 +762,9 @@ migrate_history_to_firefox_sync_history (void) goto out; } - history_db = ephy_sqlite_connection_new (EPHY_SQLITE_CONNECTION_MODE_READWRITE); - ephy_sqlite_connection_open (history_db, history_filename, &error); + history_db = ephy_sqlite_connection_new (EPHY_SQLITE_CONNECTION_MODE_READWRITE, + history_filename); + ephy_sqlite_connection_open (history_db, &error); if (error) { g_warning ("Failed to open history database: %s", error->message); g_clear_object (&history_db); diff --git a/tests/ephy-sqlite-test.c b/tests/ephy-sqlite-test.c index c5ffc4693..9576a5139 100644 --- a/tests/ephy-sqlite-test.c +++ b/tests/ephy-sqlite-test.c @@ -23,53 +23,51 @@ #include "ephy-sqlite-connection.h" #include "ephy-sqlite-statement.h" #include <glib.h> -#include <glib/gstdio.h> #include <gtk/gtk.h> -static EphySQLiteConnection * -ensure_empty_database (const char *filename) -{ - EphySQLiteConnection *connection = ephy_sqlite_connection_new (EPHY_SQLITE_CONNECTION_MODE_READWRITE); - GError *error = NULL; - - if (g_file_test (filename, G_FILE_TEST_IS_REGULAR)) - g_unlink (filename); - - g_assert (ephy_sqlite_connection_open (connection, filename, &error)); - g_assert (!error); - return connection; -} - static void test_create_connection (void) { + gchar *temporary_file; + EphySQLiteConnection *connection; GError *error = NULL; - gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-sqlite-test.db", NULL); - - EphySQLiteConnection *connection = ensure_empty_database (temporary_file); - ephy_sqlite_connection_close (connection); + temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-sqlite-test.db", NULL); + connection = ephy_sqlite_connection_new (EPHY_SQLITE_CONNECTION_MODE_READWRITE, temporary_file); + g_assert (ephy_sqlite_connection_open (connection, &error)); + g_assert (!error); g_assert (g_file_test (temporary_file, G_FILE_TEST_IS_REGULAR)); - g_unlink (temporary_file); + + ephy_sqlite_connection_close (connection); + ephy_sqlite_connection_delete_database (connection); g_assert (!g_file_test (temporary_file, G_FILE_TEST_IS_REGULAR)); + g_free (temporary_file); + g_object_unref (connection); temporary_file = g_build_filename (g_get_tmp_dir (), "directory-that-does-not-exist", "epiphany_sqlite_test.db", NULL); - g_assert (!ephy_sqlite_connection_open (connection, temporary_file, &error)); + connection = ephy_sqlite_connection_new (EPHY_SQLITE_CONNECTION_MODE_READWRITE, temporary_file); + g_assert (!ephy_sqlite_connection_open (connection, &error)); g_assert (error); g_assert (!g_file_test (temporary_file, G_FILE_TEST_IS_REGULAR)); + + g_free (temporary_file); g_object_unref (connection); } - static void test_create_statement (void) { - gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-sqlite-test.db", NULL); - EphySQLiteConnection *connection = ensure_empty_database (temporary_file); + gchar *temporary_file; + EphySQLiteConnection *connection; GError *error = NULL; EphySQLiteStatement *statement = NULL; + temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-sqlite-test.db", NULL); + connection = ephy_sqlite_connection_new (EPHY_SQLITE_CONNECTION_MODE_READWRITE, temporary_file); + g_assert (ephy_sqlite_connection_open (connection, &error)); + g_assert (!error); + statement = ephy_sqlite_connection_create_statement (connection, "CREATE TABLE TEST (id INTEGER)", &error); g_assert (statement); g_assert (!error); @@ -80,9 +78,9 @@ test_create_statement (void) g_assert (error); ephy_sqlite_connection_close (connection); - g_unlink (temporary_file); - g_free (temporary_file); + ephy_sqlite_connection_delete_database (connection); + g_free (temporary_file); g_object_unref (connection); } @@ -131,24 +129,37 @@ create_table_and_insert_row (EphySQLiteConnection *connection) static void test_create_table_and_insert_row (void) { - gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-sqlite-test.db", NULL); - EphySQLiteConnection *connection = ensure_empty_database (temporary_file); + gchar *temporary_file; + EphySQLiteConnection *connection; + GError *error = NULL; + + temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-sqlite-test.db", NULL); + connection = ephy_sqlite_connection_new (EPHY_SQLITE_CONNECTION_MODE_READWRITE, temporary_file); + g_assert (ephy_sqlite_connection_open (connection, &error)); + g_assert (!error); create_table_and_insert_row (connection); - g_object_unref (connection); - g_unlink (temporary_file); + ephy_sqlite_connection_close (connection); + ephy_sqlite_connection_delete_database (connection); + g_free (temporary_file); + g_object_unref (connection); } static void test_bind_data (void) { - gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-sqlite-test.db", NULL); - EphySQLiteConnection *connection = ensure_empty_database (temporary_file); + gchar *temporary_file; + EphySQLiteConnection *connection; GError *error = NULL; EphySQLiteStatement *statement = NULL; + temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-sqlite-test.db", NULL); + connection = ephy_sqlite_connection_new (EPHY_SQLITE_CONNECTION_MODE_READWRITE, temporary_file); + g_assert (ephy_sqlite_connection_open (connection, &error)); + g_assert (!error); + ephy_sqlite_connection_execute (connection, "CREATE TABLE test (id INTEGER, text LONGVARCHAR)", &error); statement = ephy_sqlite_connection_create_statement (connection, "INSERT INTO test (id, text) VALUES (?, ?)", &error); @@ -174,16 +185,24 @@ test_bind_data (void) g_assert_cmpint (ephy_sqlite_statement_get_column_as_int (statement, 0), ==, 3); g_assert_cmpstr (ephy_sqlite_statement_get_column_as_string (statement, 1), ==, "foo"); + ephy_sqlite_connection_close (connection); + ephy_sqlite_connection_delete_database (connection); + g_object_unref (connection); - g_unlink (temporary_file); g_free (temporary_file); } static void test_table_exists (void) { - gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-sqlite-test.db", NULL); - EphySQLiteConnection *connection = ensure_empty_database (temporary_file); + gchar *temporary_file; + EphySQLiteConnection *connection; + GError *error = NULL; + + temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-sqlite-test.db", NULL); + connection = ephy_sqlite_connection_new (EPHY_SQLITE_CONNECTION_MODE_READWRITE, temporary_file); + g_assert (ephy_sqlite_connection_open (connection, &error)); + g_assert (!error); g_assert (!ephy_sqlite_connection_table_exists (connection, "test")); g_assert (!ephy_sqlite_connection_table_exists (connection, "something_fakey")); @@ -191,8 +210,10 @@ test_table_exists (void) g_assert (ephy_sqlite_connection_table_exists (connection, "test")); g_assert (!ephy_sqlite_connection_table_exists (connection, "something_fakey")); + ephy_sqlite_connection_close (connection); + ephy_sqlite_connection_delete_database (connection); + g_object_unref (connection); - g_unlink (temporary_file); g_free (temporary_file); } |