Author: mattm
Date: 2017-08-12 16:04:53 +0200 (Sat, 12 Aug 2017)
New Revision: 29342

Modified:
   trunk/gvm/ChangeLog
   trunk/gvm/src/manage.h
   trunk/gvm/src/manage_sql.c
   trunk/gvm/src/manage_sql_nvts.c
   trunk/gvm/src/otp.c
Log:
        For the NVT sync, always delete the existing info in a single 
transaction,
        and always insert the new info in chunks.  This combines parts of the 
old
        --update and --rebuild behaviours.

        * src/otp.c (process_otp_scanner_input): Remove the mode arg from the
        cache call.  There's only one option now.

        * src/manage_sql.c (manage_nvt_preference_add): Remove "remove".  All
        preferences are added in one transaction now.

        * src/manage_sql_nvts.c (make_nvt_from_nvti): Remove "remove".  Always 
do
        inserts in chunks.
        (insert_nvt_from_nvti, insert_nvt_preference): Replace mode with dummy
        arg.
        (insert_nvts_list): Remove mode.  Always chunked now.
        (insert_nvt_preferences_list): Remove mode arg.
        (manage_complete_nvt_cache_update): Remove mode arg.  Always delete all
        NVT info here and in a single transaction.  Wrap the remaining work in
        transactions too.

        * src/manage.h: Update headers accordingly.

Modified: trunk/gvm/ChangeLog
===================================================================
--- trunk/gvm/ChangeLog 2017-08-12 11:44:51 UTC (rev 29341)
+++ trunk/gvm/ChangeLog 2017-08-12 14:04:53 UTC (rev 29342)
@@ -1,5 +1,29 @@
 2017-08-12  Matthew Mundell <matthew.mund...@greenbone.net>
 
+       For the NVT sync, always delete the existing info in a single 
transaction,
+       and always insert the new info in chunks.  This combines parts of the 
old
+       --update and --rebuild behaviours.
+
+       * src/otp.c (process_otp_scanner_input): Remove the mode arg from the
+       cache call.  There's only one option now.
+
+       * src/manage_sql.c (manage_nvt_preference_add): Remove "remove".  All
+       preferences are added in one transaction now.
+
+       * src/manage_sql_nvts.c (make_nvt_from_nvti): Remove "remove".  Always 
do
+       inserts in chunks.
+       (insert_nvt_from_nvti, insert_nvt_preference): Replace mode with dummy
+       arg.
+       (insert_nvts_list): Remove mode.  Always chunked now.
+       (insert_nvt_preferences_list): Remove mode arg.
+       (manage_complete_nvt_cache_update): Remove mode arg.  Always delete all
+       NVT info here and in a single transaction.  Wrap the remaining work in
+       transactions too.
+
+       * src/manage.h: Update headers accordingly.
+
+2017-08-12  Matthew Mundell <matthew.mund...@greenbone.net>
+
        * src/manage_sql.h (progress): Remove.
 
        * src/manage_sql_nvts.c (insert_nvt_from_nvti, insert_nvt_preference)

Modified: trunk/gvm/src/manage.h
===================================================================
--- trunk/gvm/src/manage.h      2017-08-12 11:44:51 UTC (rev 29341)
+++ trunk/gvm/src/manage.h      2017-08-12 14:04:53 UTC (rev 29342)
@@ -1914,7 +1914,7 @@
 set_nvts_feed_version (const char*);
 
 nvt_t
-make_nvt_from_nvti (const nvti_t*, int);
+make_nvt_from_nvti (const nvti_t*);
 
 gboolean
 find_nvt (const char*, nvt_t*);
@@ -1981,7 +1981,7 @@
 family_nvt_count (const char *);
 
 void
-manage_complete_nvt_cache_update (GList *, GList *, int);
+manage_complete_nvt_cache_update (GList *, GList *);
 
 
 /* NVT selectors. */
@@ -2040,7 +2040,7 @@
 /* NVT preferences. */
 
 void
-manage_nvt_preference_add (const char*, const char*, int);
+manage_nvt_preference_add (const char*, const char*);
 
 void
 manage_nvt_preferences_enable ();

Modified: trunk/gvm/src/manage_sql.c
===================================================================
--- trunk/gvm/src/manage_sql.c  2017-08-12 11:44:51 UTC (rev 29341)
+++ trunk/gvm/src/manage_sql.c  2017-08-12 14:04:53 UTC (rev 29342)
@@ -38243,25 +38243,16 @@
  * @param[in]  remove  Whether to remove the preference from the database 
first.
  */
 void
-manage_nvt_preference_add (const char* name, const char* value, int remove)
+manage_nvt_preference_add (const char* name, const char* value)
 {
   gchar* quoted_name = sql_quote (name);
   gchar* quoted_value = sql_quote (value);
 
-  if (remove)
-    {
-      sql_begin_exclusive ();
-      sql ("DELETE FROM nvt_preferences WHERE name = '%s';", quoted_name);
-    }
-
   if (strcmp (name, "port_range"))
     sql ("INSERT into nvt_preferences (name, value)"
          " VALUES ('%s', '%s');",
          quoted_name, quoted_value);
 
-  if (remove)
-    sql_commit ();
-
   g_free (quoted_name);
   g_free (quoted_value);
 }

Modified: trunk/gvm/src/manage_sql_nvts.c
===================================================================
--- trunk/gvm/src/manage_sql_nvts.c     2017-08-12 11:44:51 UTC (rev 29341)
+++ trunk/gvm/src/manage_sql_nvts.c     2017-08-12 14:04:53 UTC (rev 29342)
@@ -182,12 +182,11 @@
  * @brief Make an nvt from an nvti.
  *
  * @param[in]  nvti    NVTI.
- * @param[in]  remove  Whether to remove the NVT from the cache first.
  *
  * @return An NVT.
  */
 nvt_t
-make_nvt_from_nvti (const nvti_t *nvti, int remove)
+make_nvt_from_nvti (const nvti_t *nvti)
 {
   gchar *qod_str, *qod_type;
   /** @todo Freeing string literals. */
@@ -198,20 +197,15 @@
 
   int creation_time, modification_time, qod;
 
-  if (remove)
+  if (chunk_count == 0)
     {
-      if (chunk_count == 0)
-        {
-          sql_begin_exclusive ();
-          chunk_count++;
-        }
-      else if (chunk_count == CHUNK_SIZE)
-        chunk_count = 0;
-      else
-        chunk_count++;
-      sql ("DELETE FROM nvt_cves where oid = '%s';", nvti_oid (nvti));
-      sql ("DELETE FROM nvts WHERE oid = '%s';", nvti_oid (nvti));
+      sql_begin_exclusive ();
+      chunk_count++;
     }
+  else if (chunk_count == CHUNK_SIZE)
+    chunk_count = 0;
+  else
+    chunk_count++;
 
   quoted_version = sql_quote (nvti_version (nvti));
   quoted_name = sql_quote (nvti_name (nvti) ? nvti_name (nvti) : "");
@@ -361,7 +355,7 @@
          modification_time, nvti_oid (nvti), quoted_solution_type,
          qod, quoted_qod_type);
 
-  if (remove && (chunk_count == 0))
+  if (chunk_count == 0)
     sql_commit ();
 
   g_free (quoted_version);
@@ -975,56 +969,48 @@
 /**
  * @brief Insert an NVT from an nvti structure.
  *
- * @param[in] nvti          nvti_t to insert in nvts table.
- * @param[in] mode_pointer  Mode.  -1 updating, -2 rebuilding.
- *
+ * @param[in] nvti   nvti_t to insert in nvts table.
+ * @param[in] dummy  Dummy arg for g_list_foreach.
  */
 static void
-insert_nvt_from_nvti (gpointer nvti, gpointer mode_pointer)
+insert_nvt_from_nvti (gpointer nvti, gpointer dummy)
 {
-  int mode;
-
   if (nvti == NULL)
     return;
 
-  mode = GPOINTER_TO_INT (mode_pointer);
-
-  make_nvt_from_nvti (nvti, mode == -1);
+  make_nvt_from_nvti (nvti);
 }
 
 /**
  * @brief Insert a NVT preferences.
  *
  * @param[in] nvt_preference  Preference.
- * @param[in] mode_pointer    Mode.  -1 updating, -2 rebuilding.
+ * @param[in] dummy           Dummy arg for g_list_foreach.
  *
  */
 static void
-insert_nvt_preference (gpointer nvt_preference, gpointer mode_pointer)
+insert_nvt_preference (gpointer nvt_preference, gpointer dummy)
 {
-  int mode;
   preference_t *preference;
 
   if (nvt_preference == NULL)
     return;
 
-  mode = GPOINTER_TO_INT (mode_pointer);
   preference = (preference_t*) nvt_preference;
-  manage_nvt_preference_add (preference->name, preference->value, mode == -1);
+  manage_nvt_preference_add (preference->name, preference->value);
 }
 
 /**
  * @brief Inserts NVTs in DB from a list of nvti_t structures.
  *
  * @param[in]  nvts_list     List of nvts to be inserted.
- * @param[in]  mode          -1 updating, -2 rebuilding.
  */
 static void
-insert_nvts_list (GList *nvts_list, int mode)
+insert_nvts_list (GList *nvts_list)
 {
   chunk_count = 0;
-  g_list_foreach (nvts_list, insert_nvt_from_nvti, GINT_TO_POINTER (mode));
-  if ((mode == -1) && (chunk_count > 0))
+  g_list_foreach (nvts_list, insert_nvt_from_nvti, NULL);
+  if (chunk_count > 0)
     sql_commit ();
 }
 
@@ -1032,13 +1018,11 @@
  * @brief Inserts NVT preferences in DB from a list of nvt_preference_t 
structures.
  *
  * @param[in]  nvt_preferences_list     List of nvts to be inserted.
- * @param[in]  mode                     -1 updating, -2 rebuilding.
  */
 static void
-insert_nvt_preferences_list (GList *nvt_preferences_list, int mode)
+insert_nvt_preferences_list (GList *nvt_preferences_list)
 {
-  g_list_foreach (nvt_preferences_list, insert_nvt_preference,
-                  GINT_TO_POINTER (mode));
+  g_list_foreach (nvt_preferences_list, insert_nvt_preference, NULL);
 }
 
 /**
@@ -1119,35 +1103,35 @@
  *
  * @param[in]  nvts_list             List of nvti_t to insert.
  * @param[in]  nvt_preferences_list  List of preference_t to insert.
- * @param[in]  mode                  -1 updating, -2 rebuilding.
  */
 void
-manage_complete_nvt_cache_update (GList *nvts_list, GList 
*nvt_preferences_list,
-                                  int mode)
+manage_complete_nvt_cache_update (GList *nvts_list, GList 
*nvt_preferences_list)
 {
   iterator_t configs;
   int count;
 
-  if (mode == -2)
+  sql_begin_exclusive ();
+  if (sql_is_sqlite3 ())
     {
-      sql_begin_exclusive ();
-      if (sql_is_sqlite3 ())
-        {
-          sql ("DELETE FROM nvt_cves;");
-          sql ("DELETE FROM nvts;");
-          sql ("DELETE FROM nvt_preferences;");
-        }
-      else
-        {
-          sql ("TRUNCATE nvts CASCADE;");
-          sql ("TRUNCATE nvt_preferences;");
-        }
+      sql ("DELETE FROM nvt_cves;");
+      sql ("DELETE FROM nvts;");
+      sql ("DELETE FROM nvt_preferences;");
     }
+  else
+    {
+      sql ("TRUNCATE nvts CASCADE;");
+      sql ("TRUNCATE nvt_preferences;");
+    }
+  sql_commit ();
 
   /* NVTs and preferences are buffered, insert them into DB. */
-  insert_nvts_list (nvts_list, mode);
-  insert_nvt_preferences_list (nvt_preferences_list, mode);
+  insert_nvts_list (nvts_list);
+  sql_begin_exclusive ();
+  insert_nvt_preferences_list (nvt_preferences_list);
+  sql_commit ();
 
+  sql_begin_exclusive ();
+
   /* Remove preferences from configs where the preference has vanished from
    * the associated NVT. */
   init_iterator (&configs, "SELECT id FROM configs;");
@@ -1165,11 +1149,7 @@
                __FUNCTION__);
   update_all_config_caches ();
 
-  if (mode == -1)
-    sql_begin_exclusive ();
   refresh_nvt_cves ();
-  if (mode == -1)
-    sql_commit ();
 
   if (sql_int ("SELECT NOT EXISTS (SELECT * FROM meta"
                "                   WHERE name = 'nvts_check_time')"))
@@ -1187,14 +1167,10 @@
            " WHERE name = 'nvts_check_time';");
     }
 
-  if (mode == -2)
-    sql_commit ();
+  sql_commit ();
 
   count = sql_int ("SELECT count (*) FROM nvts;");
-  if (mode == -2)
-    g_info ("Rebuilding NVT cache... done (%i NVTs).", count);
-  else
-    g_info ("Updating NVT cache... done (%i NVTs).", count);
+  g_info ("Updating NVT cache... done (%i NVTs).", count);
 }
 
 /**

Modified: trunk/gvm/src/otp.c
===================================================================
--- trunk/gvm/src/otp.c 2017-08-12 11:44:51 UTC (rev 29341)
+++ trunk/gvm/src/otp.c 2017-08-12 14:04:53 UTC (rev 29342)
@@ -1298,9 +1298,7 @@
                         {
                           manage_complete_nvt_cache_update
                            (scanner_plugins_list,
-                            scanner_preferences_list,
-                            scanner_init_state == SCANNER_INIT_DONE_CACHE_MODE
-                            ? -2 : -1);
+                            scanner_preferences_list);
                           set_scanner_init_state (SCANNER_INIT_DONE);
                           manage_nvt_preferences_enable ();
                           /* Return 1, as though the scanner sent BYE. */

_______________________________________________
Openvas-commits mailing list
Openvas-commits@wald.intevation.org
https://lists.wald.intevation.org/cgi-bin/mailman/listinfo/openvas-commits

Reply via email to