[epiphany/wip/google-safe-browsing: 15/21] gsb-service: Delete long expired full hashes at every update



commit 1c04d2a37fd28dfe8ee18eec7a3482c85d71f799
Author: Gabriel Ivascu <gabrielivascu gnome org>
Date:   Mon Sep 18 21:29:22 2017 +0300

    gsb-service: Delete long expired full hashes at every update

 lib/safe-browsing/ephy-gsb-service.c |    2 +
 lib/safe-browsing/ephy-gsb-storage.c |   39 ++++++++++++++++++++++++++++++++++
 lib/safe-browsing/ephy-gsb-storage.h |    1 +
 3 files changed, 42 insertions(+), 0 deletions(-)
---
diff --git a/lib/safe-browsing/ephy-gsb-service.c b/lib/safe-browsing/ephy-gsb-service.c
index f4ecc96..47d87d1 100644
--- a/lib/safe-browsing/ephy-gsb-service.c
+++ b/lib/safe-browsing/ephy-gsb-service.c
@@ -165,6 +165,8 @@ ephy_gsb_service_update_thread (GTask          *task,
   g_assert (EPHY_IS_GSB_SERVICE (self));
   g_assert (ephy_gsb_storage_is_operable (self->storage));
 
+  ephy_gsb_storage_delete_old_full_hashes (self->storage);
+
   threat_lists = ephy_gsb_storage_get_threat_lists (self->storage);
   if (!threat_lists)
     goto out;
diff --git a/lib/safe-browsing/ephy-gsb-storage.c b/lib/safe-browsing/ephy-gsb-storage.c
index 19183a2..934cd85 100644
--- a/lib/safe-browsing/ephy-gsb-storage.c
+++ b/lib/safe-browsing/ephy-gsb-storage.c
@@ -28,6 +28,8 @@
 #include <glib/gstdio.h>
 #include <string.h>
 
+#define EXPIRATION_THRESHOLD (8 * 60 * 60)
+
 /* Keep this lower than 200 or else you'll get "too many SQL variables" error
  * in ephy_gsb_storage_insert_batch(). SQLITE_MAX_VARIABLE_NUMBER is hardcoded
  * in sqlite3 as 999.
@@ -1352,6 +1354,43 @@ out:
 }
 
 void
+ephy_gsb_storage_delete_old_full_hashes (EphyGSBStorage *self)
+{
+  EphySQLiteStatement *statement = NULL;
+  GError *error = NULL;
+  const char *sql;
+
+  g_assert (EPHY_IS_GSB_STORAGE (self));
+  g_assert (self->is_operable);
+
+  LOG ("Deleting full hashes expired for more than %d seconds", EXPIRATION_THRESHOLD);
+
+  sql = "DELETE FROM hash_full "
+        "WHERE expires_at <= (CAST(strftime('%s', 'now') AS INT)) - ?";
+  statement = ephy_sqlite_connection_create_statement (self->db, sql, &error);
+  if (error) {
+    g_warning ("Failed to create delete full hash statement: %s", error->message);
+    goto out;
+  }
+
+  ephy_sqlite_statement_bind_int64 (statement, 0, EXPIRATION_THRESHOLD, &error);
+  if (error) {
+    g_warning ("Failed to bind int64 in delete full hash statement: %s", error->message);
+    goto out;
+  }
+
+  ephy_sqlite_statement_step (statement, &error);
+  if (error)
+    g_warning ("Failed to execute delete full hash statement: %s", error->message);
+
+out:
+  if (statement)
+    g_object_unref (statement);
+  if (error)
+    g_error_free (error);
+}
+
+void
 ephy_gsb_storage_update_hash_prefix_expiration (EphyGSBStorage *self,
                                                 GBytes         *prefix,
                                                 gint64          duration)
diff --git a/lib/safe-browsing/ephy-gsb-storage.h b/lib/safe-browsing/ephy-gsb-storage.h
index 4cd5981..01ef05e 100644
--- a/lib/safe-browsing/ephy-gsb-storage.h
+++ b/lib/safe-browsing/ephy-gsb-storage.h
@@ -59,6 +59,7 @@ void            ephy_gsb_storage_insert_full_hash               (EphyGSBStorage
                                                                  const guint8      *hash,
                                                                  gint64             duration,
                                                                  const char        *malware_threat_type);
+void            ephy_gsb_storage_delete_old_full_hashes         (EphyGSBStorage *self);
 void            ephy_gsb_storage_update_hash_prefix_expiration  (EphyGSBStorage *self,
                                                                  GBytes         *prefix,
                                                                  gint64          duration);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]