[evolution] Save temporary files into system temp, rather than under home



commit 409c789f744bb0b8e3463db6221cddfc6f5e0d07
Author: Milan Crha <mcrha redhat com>
Date:   Fri Jan 28 08:53:13 2022 +0100

    Save temporary files into system temp, rather than under home
    
    The home directory might not be accessible in some cases from other
    applications (like under snap), thus prefer system temp directory.
    An advantage is that the Evolution does not need to cleanup that
    directory.

 CMakeLists.txt                  |   1 -
 config.h.in                     |   3 -
 src/e-util/e-attachment-store.c |   2 +-
 src/e-util/e-mktemp.c           | 160 +++++++++++++++-------------------------
 4 files changed, 59 insertions(+), 107 deletions(-)
---
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 233fa55d38..262409b623 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -218,7 +218,6 @@ set(MATH_LDFLAGS -lm)
 
 CHECK_INCLUDE_FILE(sys/wait.h HAVE_SYS_WAIT_H)
 CHECK_INCLUDE_FILE(X11/XF86keysym.h HAVE_XFREE)
-CHECK_FUNCTION_EXISTS(mkdtemp HAVE_MKDTEMP)
 CHECK_FUNCTION_EXISTS(nl_langinfo HAVE_NL_LANGINFO)
 
 # ******************************
diff --git a/config.h.in b/config.h.in
index d1d13dac48..881641355a 100644
--- a/config.h.in
+++ b/config.h.in
@@ -57,9 +57,6 @@
 /* Define if libc defines an altzone variable */
 #cmakedefine HAVE_ALTZONE 1
 
-/* Define to 1 if you have the `mkdtemp' function. */
-#cmakedefine HAVE_MKDTEMP 1
-
 /* Define if you have the iso-codes package */
 #cmakedefine HAVE_ISO_CODES 1
 
diff --git a/src/e-util/e-attachment-store.c b/src/e-util/e-attachment-store.c
index 3a2b512679..04a64195b6 100644
--- a/src/e-util/e-attachment-store.c
+++ b/src/e-util/e-attachment-store.c
@@ -1560,7 +1560,7 @@ attachment_store_move_file (SaveContext *save_context,
         * we get a spurious G_IO_ERROR_WOULD_MERGE error and the
         * user has to try saving attachments again. */
        tmpl = g_strdup_printf (PACKAGE "-%s-XXXXXX", g_get_user_name ());
-       path = e_mktemp (tmpl);
+       path = e_mkdtemp (tmpl);
        g_free (tmpl);
 
        save_context->trash_directory = g_file_new_for_path (path);
diff --git a/src/e-util/e-mktemp.c b/src/e-util/e-mktemp.c
index 8d812d7bbc..2053604337 100644
--- a/src/e-util/e-mktemp.c
+++ b/src/e-util/e-mktemp.c
@@ -39,13 +39,15 @@
 #define d(x)
 
 /* define to put temporary files in ~/evolution/cache/tmp */
-#define TEMP_HOME (1)
+/* #define TEMP_HOME */
 
 /* how old things need to be to expire */
 #define TEMP_EXPIRE (60*60*2)
 /* don't scan more often than this */
 #define TEMP_SCAN (60)
 
+#ifdef TEMP_HOME
+
 static gint
 expire_dir_rec (const gchar *base,
                 time_t now)
@@ -98,156 +100,110 @@ expire_dir_rec (const gchar *base,
        return count;
 }
 
+#endif /* TEMP_HOME */
+
 static GString *
-get_dir (gboolean make)
+get_dir (const gchar *tmpl)
 {
        GString *path;
+       gchar *tmpdir;
+#ifdef TEMP_HOME
        time_t now = time (NULL);
        static time_t last = 0;
-
-#ifdef TEMP_HOME
        const gchar *user_cache_dir;
-       gchar *tmpdir;
+#else
+       GError *error = NULL;
+#endif
 
+       if (!tmpl || !*tmpl)
+               tmpl = "evolution-XXXXXX";
+
+#ifdef TEMP_HOME
        user_cache_dir = e_get_user_cache_dir ();
        tmpdir = g_build_filename (user_cache_dir, "tmp", NULL);
        path = g_string_new (tmpdir);
-       if (make && g_mkdir_with_parents (tmpdir, 0777) == -1) {
+       if (g_mkdir_with_parents (tmpdir, 0777) == -1) {
                g_string_free (path, TRUE);
                path = NULL;
        }
-       g_free (tmpdir);
-#else
-       path = g_string_new ("/tmp/evolution-");
-       g_string_append_printf (path, "%d", (gint) getuid ());
-       if (make) {
-               gint ret;
-
-               /* shoot now, ask questions later */
-               ret = g_mkdir (path->str, S_IRWXU);
-               if (ret == -1) {
-                       if (errno == EEXIST) {
-                               struct stat st;
-
-                               if (g_stat (path->str, &st) == -1) {
-                                       /* reset errno */
-                                       errno = EEXIST;
-                                       g_string_free (path, TRUE);
-                                       return NULL;
-                               }
-
-                               /* make sure this is a directory and
-                                * belongs to us... */
-                               if (!S_ISDIR (st.st_mode) || st.st_uid != getuid ()) {
-                                       /* eek! this is bad... */
-                                       g_string_free (path, TRUE);
-                                       return NULL;
-                               }
-                       } else {
-                               /* some other error...do not pass go,
-                                * do not collect $200 */
-                               g_string_free (path, TRUE);
-                               return NULL;
-                       }
-               }
-       }
-#endif
-
-       d (printf ("temp dir '%s'\n", path ? path->str : "(null)"));
 
        /* fire off an expiry attempt no more often than TEMP_SCAN seconds */
        if (path && (last + TEMP_SCAN) < now) {
                last = now;
                expire_dir_rec (path->str, now);
        }
+#else
+       tmpdir = g_dir_make_tmp (tmpl, &error);
+       if (!tmpdir) {
+               g_debug ("Failed to create tmp directory: %s", error ? error->message : "Unknown error");
+               g_clear_error (&error);
+
+               return NULL;
+       }
+
+       path = g_string_new (tmpdir);
+#endif
+       g_free (tmpdir);
+
+       d (printf ("temp dir '%s'\n", path ? path->str : "(null)"));
 
        return path;
 }
 
-gchar *
-e_mktemp (const gchar *template)
+static gint
+e_mkstemp_impl (const gchar *template,
+               gchar **out_path)
 {
        GString *path;
        gint fd;
 
-       path = get_dir (TRUE);
+       path = get_dir (NULL);
        if (!path)
-               return NULL;
+               return -1;
 
-       g_string_append_c (path, '/');
-       if (template)
-               g_string_append (path, template);
-       else
-               g_string_append (path, "unknown-XXXXXX");
+       g_string_append_c (path, G_DIR_SEPARATOR);
+       g_string_append (path, template && *template ? template : "unknown-XXXXXX");
 
        fd = g_mkstemp (path->str);
 
-       if (fd != -1) {
-               close (fd);
-               g_unlink (path->str);
-       }
+       if (out_path)
+               *out_path = g_string_free (path, fd == -1);
+       else
+               g_string_free (path, TRUE);
 
-       return g_string_free (path, fd == -1);
+       return fd;
 }
 
-gint
-e_mkstemp (const gchar *template)
+gchar *
+e_mktemp (const gchar *template)
 {
-       GString *path;
+       gchar *path = NULL;
        gint fd;
 
-       path = get_dir (TRUE);
-       if (!path)
-               return -1;
+       fd = e_mkstemp_impl (template, &path);
 
-       g_string_append_c (path, '/');
-       if (template)
-               g_string_append (path, template);
-       else
-               g_string_append (path, "unknown-XXXXXX");
+       if (fd != -1) {
+               close (fd);
+               g_unlink (path);
+       }
 
-       fd = g_mkstemp (path->str);
-       g_string_free (path, TRUE);
+       return path;
+}
 
-       return fd;
+gint
+e_mkstemp (const gchar *template)
+{
+       return e_mkstemp_impl (template, NULL);
 }
 
 gchar *
 e_mkdtemp (const gchar *template)
 {
        GString *path;
-       gchar *tmpdir;
 
-       path = get_dir (TRUE);
+       path = get_dir (template);
        if (!path)
                return NULL;
 
-       g_string_append_c (path, '/');
-       if (template)
-               g_string_append (path, template);
-       else
-               g_string_append (path, "unknown-XXXXXX");
-
-#ifdef HAVE_MKDTEMP
-       tmpdir = mkdtemp (path->str);
-#else
-       {
-               gint fd = g_mkstemp (path->str);
-               if (fd == -1) {
-                       tmpdir = NULL;
-               } else {
-                       close (fd);
-                       tmpdir = path->str;
-                       g_unlink (tmpdir);
-               }
-       }
-
-       if (tmpdir) {
-               if (g_mkdir (tmpdir, S_IRWXU) == -1)
-                       tmpdir = NULL;
-       }
-#endif
-       g_string_free (path, tmpdir == NULL);
-
-       return tmpdir;
+       return g_string_free (path, FALSE);
 }


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