Author: mattm
Date: 2016-10-18 12:43:08 +0200 (Tue, 18 Oct 2016)
New Revision: 26424

Modified:
   trunk/openvas-manager/ChangeLog
   trunk/openvas-manager/src/manage_migrators.c
   trunk/openvas-manager/src/manage_sql.c
Log:
        * src/manage_migrators.c (migrate_170_to_171): If the old directory is
        missing, just print a warning instead of trying to move directories
        out of it.

        * src/manage_sql.c (check_db_trash_report_formats): New function.  
Removes
        all trash report formats if the directory is missing.
        (check_db_report_formats): Check trash.

Modified: trunk/openvas-manager/ChangeLog
===================================================================
--- trunk/openvas-manager/ChangeLog     2016-10-18 09:11:55 UTC (rev 26423)
+++ trunk/openvas-manager/ChangeLog     2016-10-18 10:43:08 UTC (rev 26424)
@@ -1,3 +1,13 @@
+2016-10-18  Matthew Mundell <matthew.mund...@greenbone.net>
+
+       * src/manage_migrators.c (migrate_170_to_171): If the old directory is
+       missing, just print a warning instead of trying to move directories
+       out of it.
+
+       * src/manage_sql.c (check_db_trash_report_formats): New function.  
Removes
+       all trash report formats if the directory is missing.
+       (check_db_report_formats): Check trash.
+
 2016-10-17  Matthew Mundell <matthew.mund...@greenbone.net>
 
        * src/manage_migrators.c (migrate_165_to_166): Skip creating the

Modified: trunk/openvas-manager/src/manage_migrators.c
===================================================================
--- trunk/openvas-manager/src/manage_migrators.c        2016-10-18 09:11:55 UTC 
(rev 26423)
+++ trunk/openvas-manager/src/manage_migrators.c        2016-10-18 10:43:08 UTC 
(rev 26424)
@@ -12771,6 +12771,7 @@
 migrate_170_to_171 ()
 {
   gchar *old_dir, *new_dir;
+  struct stat state;
 
   sql_begin_exclusive ();
 
@@ -12803,55 +12804,72 @@
                               "report_formats_trash",
                               NULL);
 
-  /* Ensure the old dir exists. */
-  g_mkdir_with_parents (old_dir, 0755 /* "rwxr-xr-x" */);
+  if (g_lstat (old_dir, &state))
+    {
+      /* The old dir is missing.  Assume there are no trash report formats.
+       * This helps when the database has been restored without the trash
+       * directory. */
 
-  {
-    gchar **cmd;
-    gchar *standard_out = NULL;
-    gchar *standard_err = NULL;
-    gint exit_status;
+      if (errno != ENOENT)
+        g_warning ("%s: g_lstat (%s) failed: %s\n",
+                   __FUNCTION__, old_dir, g_strerror (errno));
+      else
+        g_warning ("%s: trash report formats directory missing (%s)\n",
+                   __FUNCTION__, old_dir);
+      g_warning ("%s: any trash report formats will be removed on startup\n",
+                 __FUNCTION__);
+    }
+  else
+    {
+      gchar **cmd;
+      gchar *standard_out = NULL;
+      gchar *standard_err = NULL;
+      gint exit_status;
 
-    cmd = (gchar **) g_malloc (4 * sizeof (gchar *));
-    cmd[0] = g_strdup ("mv");
-    cmd[1] = old_dir;
-    cmd[2] = new_dir;
-    cmd[3] = NULL;
-    g_debug ("%s: Spawning in .: %s %s %s\n",
-             __FUNCTION__, cmd[0], cmd[1], cmd[2]);
-    if ((g_spawn_sync (".",
-                       cmd,
-                       NULL,                  /* Environment. */
-                       G_SPAWN_SEARCH_PATH,
-                       NULL,                  /* Setup function. */
-                       NULL,
-                       &standard_out,
-                       &standard_err,
-                       &exit_status,
-                       NULL)
-         == FALSE)
-        || (WIFEXITED (exit_status) == 0)
-        || WEXITSTATUS (exit_status))
-      {
-        g_warning ("%s: failed rename: %d (WIF %i, WEX %i)",
-                   __FUNCTION__,
-                   exit_status,
-                   WIFEXITED (exit_status),
-                 WEXITSTATUS (exit_status));
-        g_debug ("%s: stdout: %s\n", __FUNCTION__, standard_out);
-        g_debug ("%s: stderr: %s\n", __FUNCTION__, standard_err);
-        g_free (old_dir);
-        g_free (new_dir);
-        g_free (cmd[0]);
-        g_free (cmd);
-        sql_rollback ();
-        return -1;
-      }
+      /* Move the directory. */
 
-    g_free (cmd[0]);
-    g_free (cmd);
-  }
+      g_mkdir_with_parents (old_dir, 0755 /* "rwxr-xr-x" */);
 
+      cmd = (gchar **) g_malloc (4 * sizeof (gchar *));
+      cmd[0] = g_strdup ("mv");
+      cmd[1] = old_dir;
+      cmd[2] = new_dir;
+      cmd[3] = NULL;
+      g_debug ("%s: Spawning in .: %s %s %s\n",
+               __FUNCTION__, cmd[0], cmd[1], cmd[2]);
+      if ((g_spawn_sync (".",
+                         cmd,
+                         NULL,                  /* Environment. */
+                         G_SPAWN_SEARCH_PATH,
+                         NULL,                  /* Setup function. */
+                         NULL,
+                         &standard_out,
+                         &standard_err,
+                         &exit_status,
+                         NULL)
+           == FALSE)
+          || (WIFEXITED (exit_status) == 0)
+          || WEXITSTATUS (exit_status))
+        {
+          g_warning ("%s: failed rename: %d (WIF %i, WEX %i)",
+                     __FUNCTION__,
+                     exit_status,
+                     WIFEXITED (exit_status),
+                   WEXITSTATUS (exit_status));
+          g_debug ("%s: stdout: %s\n", __FUNCTION__, standard_out);
+          g_debug ("%s: stderr: %s\n", __FUNCTION__, standard_err);
+          g_free (old_dir);
+          g_free (new_dir);
+          g_free (cmd[0]);
+          g_free (cmd);
+          sql_rollback ();
+          return -1;
+        }
+
+      g_free (cmd[0]);
+      g_free (cmd);
+    }
+
   g_free (old_dir);
   g_free (new_dir);
 

Modified: trunk/openvas-manager/src/manage_sql.c
===================================================================
--- trunk/openvas-manager/src/manage_sql.c      2016-10-18 09:11:55 UTC (rev 
26423)
+++ trunk/openvas-manager/src/manage_sql.c      2016-10-18 10:43:08 UTC (rev 
26424)
@@ -14964,6 +14964,81 @@
 }
 
 /**
+ * @brief Check that trash report formats are correct.
+ *
+ * @return 0 success, -1 error.
+ */
+static int
+check_db_trash_report_formats ()
+{
+  gchar *dir;
+  struct stat state;
+
+  dir = g_build_filename (OPENVAS_STATE_DIR,
+                          "openvasmd",
+                          "report_formats_trash",
+                          NULL);
+
+  if (g_lstat (dir, &state))
+    {
+      iterator_t report_formats;
+      int count;
+
+      if (errno != ENOENT)
+        {
+          g_warning ("%s: g_lstat (%s) failed: %s\n",
+                     __FUNCTION__, dir, g_strerror (errno));
+          g_free (dir);
+          return -1;
+        }
+
+      /* Remove all trash report formats. */
+
+      count = 0;
+      init_iterator (&report_formats, "SELECT id FROM report_formats_trash;");
+      while (next (&report_formats))
+        {
+          report_format_t report_format;
+
+          report_format = iterator_int64 (&report_formats, 0);
+
+          sql ("DELETE FROM alert_method_data_trash"
+               " WHERE data = (SELECT original_uuid"
+               "               FROM report_formats_trash"
+               "               WHERE id = %llu)"
+               " AND (name = 'notice_attach_format'"
+               "      OR name = 'notice_report_format');",
+               report_format);
+
+          permissions_set_orphans ("report_format", report_format,
+                                   LOCATION_TRASH);
+          tags_set_orphans ("report_format", report_format, LOCATION_TRASH);
+
+          sql ("DELETE FROM report_format_param_options_trash"
+               " WHERE report_format_param"
+               " IN (SELECT id from report_format_params_trash"
+               "     WHERE report_format = %llu);",
+               report_format);
+          sql ("DELETE FROM report_format_params_trash WHERE report_format = 
%llu;",
+               report_format);
+          sql ("DELETE FROM report_formats_trash WHERE id = %llu;",
+               report_format);
+
+          count++;
+        }
+      cleanup_iterator (&report_formats);
+
+      if (count)
+        g_message ("Trash report format directory was missing."
+                   " Removed all %i trash report formats.",
+                   count);
+    }
+
+  g_free (dir);
+  return 0;
+}
+
+/**
  * @brief Ensure the predefined report formats exist.
  *
  * @return 0 success, -1 error.
@@ -14977,6 +15052,9 @@
   const gchar *report_format_path;
   iterator_t report_formats;
 
+  if (check_db_trash_report_formats ())
+    return -1;
+
   /* Bring report format UUIDs in database up to date. */
   update_report_format_uuids ();
   if (make_report_format_uuids_unique ())

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

Reply via email to