[epiphany/mcatanzaro/#1768] file-helpers: always delete tmp directory




commit 62df8f77fac5221162d44f134cf3f6d47adbbdfb
Author: Michael Catanzaro <mcatanzaro redhat com>
Date:   Fri Apr 29 15:21:31 2022 -0500

    file-helpers: always delete tmp directory
    
    The KEEP_DIRECTORY flag is being misinterpreted as indication that
    the profile directory should not be deleted. In fact, it controls
    whether the Epiphany temporary directory is deleted. And that should
    always be deleted, because leaving temp directories around is
    unfriendly, so get rid of it altogether.
    
    Fixes #1768

 lib/ephy-file-helpers.c                      | 14 ++---
 src/ephy-main.c                              |  2 -
 src/profile-migrator/ephy-profile-migrator.c |  3 +-
 tests/ephy-file-helpers-test.c               | 76 +++++++---------------------
 4 files changed, 21 insertions(+), 74 deletions(-)
---
diff --git a/lib/ephy-file-helpers.c b/lib/ephy-file-helpers.c
index 1e4f827c4..9bd27ac5f 100644
--- a/lib/ephy-file-helpers.c
+++ b/lib/ephy-file-helpers.c
@@ -68,7 +68,6 @@ typedef enum {
 static GHashTable *files;
 static GHashTable *mime_table;
 
-static gboolean keep_directory;
 static char *profile_dir_global;
 static char *cache_dir;
 static char *config_dir;
@@ -398,7 +397,6 @@ ephy_file_helpers_init (const char            *profile_dir,
                                  (GDestroyNotify)g_free,
                                  (GDestroyNotify)g_free);
 
-  keep_directory = flags & EPHY_FILE_HELPERS_KEEP_DIR;
   private_profile = (flags & EPHY_FILE_HELPERS_PRIVATE_PROFILE || flags & EPHY_FILE_HELPERS_TESTING_MODE);
   steal_data_from_profile = flags & EPHY_FILE_HELPERS_STEAL_DATA;
 
@@ -518,15 +516,9 @@ ephy_file_helpers_shutdown (void)
   g_clear_pointer (&config_dir, g_free);
 
   if (tmp_dir != NULL) {
-    if (!keep_directory) {
-      /* recursively delete the contents and the
-       * directory */
-      LOG ("shutdown: delete tmp_dir %s", tmp_dir);
-      ephy_file_delete_dir_recursively (tmp_dir, NULL);
-    }
-
-    g_free (tmp_dir);
-    tmp_dir = NULL;
+    LOG ("shutdown: delete tmp_dir %s", tmp_dir);
+    ephy_file_delete_dir_recursively (tmp_dir, NULL);
+    g_clear_pointer (&tmp_dir, g_free);
   }
 
   g_clear_object (&global_portal);
diff --git a/src/ephy-main.c b/src/ephy-main.c
index 77027c5ab..be9b6dba7 100644
--- a/src/ephy-main.c
+++ b/src/ephy-main.c
@@ -327,8 +327,6 @@ main (int   argc,
     flags |= EPHY_FILE_HELPERS_PRIVATE_PROFILE;
   if (incognito_mode)
     flags |= EPHY_FILE_HELPERS_STEAL_DATA;
-  if (profile_directory && !incognito_mode)
-    flags |= EPHY_FILE_HELPERS_KEEP_DIR;
 
   if (!ephy_file_helpers_init (profile_directory, flags, &error)) {
     g_error ("Fatal initialization error: %s", error->message);
diff --git a/src/profile-migrator/ephy-profile-migrator.c b/src/profile-migrator/ephy-profile-migrator.c
index 8267d3fd0..3b569582d 100644
--- a/src/profile-migrator/ephy-profile-migrator.c
+++ b/src/profile-migrator/ephy-profile-migrator.c
@@ -1855,8 +1855,7 @@ main (int   argc,
   ephy_debug_init ();
 
   if (profile_dir != NULL)
-    file_helpers_flags = EPHY_FILE_HELPERS_PRIVATE_PROFILE |
-                         EPHY_FILE_HELPERS_KEEP_DIR;
+    file_helpers_flags |= EPHY_FILE_HELPERS_PRIVATE_PROFILE;
 
   if (!ephy_file_helpers_init (profile_dir, file_helpers_flags, NULL)) {
     LOG ("Something wrong happened with ephy_file_helpers_init()");
diff --git a/tests/ephy-file-helpers-test.c b/tests/ephy-file-helpers-test.c
index a591e2b58..817e3c1c9 100644
--- a/tests/ephy-file-helpers-test.c
+++ b/tests/ephy-file-helpers-test.c
@@ -27,75 +27,33 @@
 #include <glib.h>
 #include <gtk/gtk.h>
 
-typedef struct {
-  const char *dir;
-  EphyFileHelpersFlags flags;
-} FileInitTest;
-
-static const FileInitTest private_tests[] = {
-  { "private", EPHY_FILE_HELPERS_PRIVATE_PROFILE },
-  { "private, keep-dir", EPHY_FILE_HELPERS_PRIVATE_PROFILE | EPHY_FILE_HELPERS_KEEP_DIR }
-};
-
 static void
 test_ephy_file_helpers_init (void)
 {
-  guint i;
-
-  for (i = 0; i < G_N_ELEMENTS (private_tests); i++) {
-    FileInitTest test;
-
-    char *tmp_dir = NULL;
-    char *profile_dir = NULL;
+  char *tmp_dir = NULL;
+  char *profile_dir = NULL;
 
-    gboolean private_profile = FALSE;
-    gboolean keep_dir = FALSE;
-    gboolean ensure_exists = FALSE;
+  g_assert_null (ephy_profile_dir ());
+  g_assert_true (ephy_file_helpers_init (NULL, EPHY_FILE_HELPERS_PRIVATE_PROFILE, NULL));
 
-    test = private_tests[i];
+  tmp_dir = g_strdup (ephy_file_tmp_dir ());
+  profile_dir = g_strdup (ephy_profile_dir ());
 
-    if (test.flags & EPHY_FILE_HELPERS_PRIVATE_PROFILE) private_profile = TRUE;
-    if (test.flags & EPHY_FILE_HELPERS_KEEP_DIR) keep_dir = TRUE;
-    if (test.flags & EPHY_FILE_HELPERS_ENSURE_EXISTS) ensure_exists = TRUE;
+  g_assert_nonnull (tmp_dir);
+  g_assert_nonnull (profile_dir);
 
-    g_test_message ("INIT: dir: %s; private: %s; keep_dir: %s; ensure_exists: %s",
-                    test.dir,
-                    private_profile ? "TRUE" : "FALSE",
-                    keep_dir ? "TRUE" : "FALSE",
-                    ensure_exists ? "TRUE" : "FALSE");
+  /* Should always exist after ephy_file_tmp_dir(). */
+  g_assert_true (g_file_test (tmp_dir, G_FILE_TEST_EXISTS));
+  g_assert_false (g_file_test (profile_dir, G_FILE_TEST_EXISTS));
 
-    g_assert_null (ephy_profile_dir ());
-    g_assert_true (ephy_file_helpers_init (NULL, test.flags, NULL));
-
-    tmp_dir = g_strdup (ephy_file_tmp_dir ());
-    profile_dir = g_strdup (ephy_profile_dir ());
-
-    g_assert_nonnull (tmp_dir);
-    g_assert_nonnull (profile_dir);
-
-    /* Should always exist after ephy_file_tmp_dir(). */
-    g_assert_true (g_file_test (tmp_dir, G_FILE_TEST_EXISTS));
-    g_assert_true (g_file_test (profile_dir, G_FILE_TEST_EXISTS) == ensure_exists);
-
-    ephy_file_helpers_shutdown ();
-
-    /* Private profiles have their profile_dir inside tmp_dir. */
-    g_assert_true (g_file_test (tmp_dir, G_FILE_TEST_EXISTS) == keep_dir);
-    g_assert_true (g_file_test (profile_dir, G_FILE_TEST_EXISTS) == (keep_dir && ensure_exists));
+  ephy_file_helpers_shutdown ();
 
-    /* Cleanup dir left behind. */
-    if (keep_dir) {
-      /* As a safety measure, only try recursive delete on paths
-       * prefixed with /tmp. */
-      if (g_str_has_prefix (tmp_dir, "/tmp"))
-        g_assert_true (ephy_file_delete_dir_recursively (tmp_dir, NULL));
-      else
-        g_warning ("INIT: dangerous path returned as tmp_dir: %s", tmp_dir);
-    }
+  /* Private profiles have their profile_dir inside tmp_dir. */
+  g_assert_false (g_file_test (tmp_dir, G_FILE_TEST_EXISTS));
+  g_assert_false (g_file_test (profile_dir, G_FILE_TEST_EXISTS));
 
-    g_free (tmp_dir);
-    g_free (profile_dir);
-  }
+  g_free (tmp_dir);
+  g_free (profile_dir);
 }
 
 typedef struct {


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