]> www.fi.muni.cz Git - evince.git/commitdiff
Use always ev_tmp_dir instead of g_get_tmp_dir. Fix a race condition in
authorCarlos Garcia Campos <carlosgc@gnome.org>
Wed, 15 Nov 2006 11:33:09 +0000 (11:33 +0000)
committerCarlos Garcia Campos <carlosgc@src.gnome.org>
Wed, 15 Nov 2006 11:33:09 +0000 (11:33 +0000)
2006-11-15  Carlos Garcia Campos  <carlosgc@gnome.org>
* backend/Makefile.am:
* backend/ev-attachment.c: (ev_attachment_open):
* lib/ev-file-helpers.[ch]: (ensure_dir_exists), (ev_dot_dir),
(ev_tmp_dir), (ev_tmp_filename):
* shell/ev-sidebar-attachments.c:
(ev_sidebar_attachments_drag_data_get):
* shell/ev-window.c: (ev_window_clear_temp_file):
Use always ev_tmp_dir instead of g_get_tmp_dir. Fix a race condition
in ensure_dir_exists. Abort without crashing when we can't create
user's directory.

ChangeLog
backend/Makefile.am
backend/ev-attachment.c
lib/ev-file-helpers.c
lib/ev-file-helpers.h
shell/ev-sidebar-attachments.c
shell/ev-window.c

index 9015f1b2023737453d095f2320f11ea0550f6f2d..1afc00ea98413445c36f3d71c73d349f5220e5d0 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+2006-11-15  Carlos Garcia Campos  <carlosgc@gnome.org>
+
+       * backend/Makefile.am:
+       * backend/ev-attachment.c: (ev_attachment_open):
+       * lib/ev-file-helpers.[ch]: (ensure_dir_exists), (ev_dot_dir),
+       (ev_tmp_dir), (ev_tmp_filename):
+       * shell/ev-sidebar-attachments.c:
+       (ev_sidebar_attachments_drag_data_get):
+       * shell/ev-window.c: (ev_window_clear_temp_file):
+
+       Use always ev_tmp_dir instead of g_get_tmp_dir. Fix a race condition
+       in ensure_dir_exists. Abort without crashing when we can't create
+       user's directory.
+
 2006-11-14  Carlos Garcia Campos  <carlosgc@gnome.org>
 
        * data/evince-ui.xml:
index f92c607adfa091b33ce6fc052facaab64ecea192..f7ae5ec5d8b52d87301da17dfb93821cde09a793 100644 (file)
@@ -1,6 +1,7 @@
 INCLUDES=                                      \
        -DEVINCE_UIDIR=\"$(pkgdatadir)\"        \
        -DGNOMELOCALEDIR=\"$(datadir)/locale\"  \
+       -I$(top_srcdir)/lib                     \
        -I$(top_srcdir)/pdf                     \
        -I$(top_srcdir)/pixbuf                  \
        -I$(top_srcdir)/tiff                    \
index 63619afa61dc0f2772f2d645b72dae9a8a4c24f2..7e7ca127ea0b35483179d555e11253c852db3b9e 100644 (file)
@@ -22,6 +22,7 @@
 #include <libgnomevfs/gnome-vfs.h>
 #include <libgnomevfs/gnome-vfs-mime-handlers.h>
 #include <libgnomevfs/gnome-vfs-mime-utils.h>
+#include "ev-file-helpers.h"
 #include "ev-attachment.h"
 
 enum
@@ -389,7 +390,7 @@ ev_attachment_open (EvAttachment *attachment,
        } else {
                gchar *uri, *filename;
                
-               filename = g_build_filename (g_get_tmp_dir (), attachment->priv->name, NULL);
+               filename = g_build_filename (ev_tmp_dir (), attachment->priv->name, NULL);
                uri = g_filename_to_uri (filename, NULL, NULL);
 
                if (ev_attachment_save (attachment, uri, error)) {
index 130c62b20a48c91475db47c47be066e04beed654..9763831d783bab59e3fafdebf48d14d8f1872bed 100644 (file)
 #include "config.h"
 #endif
 
-#include <sys/stat.h>
+#include <stdlib.h>
+#include <sys/types.h>
 #include <unistd.h>
+#include <string.h>
 #include <glib.h>
+#include <glib/gstdio.h>
 #include <libgnome/gnome-init.h>
 #include <libgnomevfs/gnome-vfs-uri.h>
 #include <libgnomevfs/gnome-vfs-utils.h>
 
 #include "ev-file-helpers.h"
 
-static char *dot_dir = NULL;
-static char *tmp_dir = NULL;
-static int  count = 0;
+static gchar *dot_dir = NULL;
+static gchar *tmp_dir = NULL;
+static gint   count = 0;
 
 static gboolean
 ensure_dir_exists (const char *dir)
 {
-       if (g_file_test (dir, G_FILE_TEST_IS_DIR) == FALSE)
-       {
-               if (g_file_test (dir, G_FILE_TEST_EXISTS) == TRUE)
-               {
-                       g_warning ("%s exists, please move it out of the way.", dir);
-                       return FALSE;
-               }
-
-               if (mkdir (dir, 488) != 0)
-               {
-                       g_warning ("Failed to create directory %s.", dir);
-                       return FALSE;
-               }
-       }
+       if (g_file_test (dir, G_FILE_TEST_IS_DIR))
+               return TRUE;
+       
+       if (g_mkdir (dir, 488) == 0)
+               return TRUE;
 
-       return TRUE;
+       if (errno == EEXIST)
+               return g_file_test (dir, G_FILE_TEST_IS_DIR);
+       
+       g_warning ("Failed to create directory %s: %s", dir, strerror (errno));
+       return FALSE;
 }
 
-const char *
+const gchar *
 ev_dot_dir (void)
 {
-       if (dot_dir == NULL)
-       {
+       if (dot_dir == NULL) {
                gboolean exists;
 
                dot_dir = g_build_filename (gnome_user_dir_get (),
@@ -70,12 +67,33 @@ ev_dot_dir (void)
                                            NULL);
 
                exists = ensure_dir_exists (dot_dir);
-               g_assert (exists);
+               if (!exists)
+                       exit (1);
        }
 
        return dot_dir;
 }
 
+const gchar *
+ev_tmp_dir (void)
+{
+       if (tmp_dir == NULL) {
+               gboolean exists;
+               gchar   *dirname;
+
+               dirname = g_strdup_printf ("evince-%u", getpid ());
+               tmp_dir = g_build_filename (g_get_tmp_dir (),
+                                           dirname,
+                                           NULL);
+               g_free (dirname);
+
+               exists = ensure_dir_exists (tmp_dir);
+               g_assert (exists);
+       }
+
+       return tmp_dir;
+}
+
 void
 ev_file_helpers_init (void)
 {
@@ -85,7 +103,7 @@ void
 ev_file_helpers_shutdown (void)
 {      
        if (tmp_dir != NULL)    
-               rmdir (tmp_dir);
+               g_rmdir (tmp_dir);
 
        g_free (tmp_dir);
        g_free (dot_dir);
@@ -94,34 +112,20 @@ ev_file_helpers_shutdown (void)
        tmp_dir = NULL;
 }
 
-gchar* 
+gchar 
 ev_tmp_filename (void)
 {
        gchar *basename;
        gchar *filename = NULL;
 
-       if (tmp_dir == NULL) {
-               gboolean exists;
-               gchar   *dirname;
-               
-               dirname = g_strdup_printf ("evince-%u", getpid());
-               tmp_dir = g_build_filename (g_get_tmp_dir (),
-                                           dirname,
-                                           NULL);
-               g_free (dirname);
-
-               exists = ensure_dir_exists (tmp_dir);
-               g_assert (exists);
-       }
-       
-       
        do {
                if (filename != NULL)
                        g_free (filename);
                        
                basename = g_strdup_printf ("document-%d", count ++);
                
-               filename = g_build_filename (tmp_dir, basename, NULL);
+               filename = g_build_filename (ev_tmp_dir (),
+                                            basename, NULL);
                
                g_free (basename);
        } while (g_file_test (filename, G_FILE_TEST_EXISTS));
index 2cc817ac2370b7e331a57e99fd30f5414294430d..69ff83ddbf4497c97536909b956895737e4627ee 100644 (file)
 
 G_BEGIN_DECLS
 
-const char *ev_dot_dir               (void);
+const gchar *ev_dot_dir               (void);
 
-void        ev_file_helpers_init     (void);
+const gchar *ev_tmp_dir               (void);
 
-void        ev_file_helpers_shutdown (void);
+void         ev_file_helpers_init     (void);
 
-gchar*      ev_tmp_filename          (void);
+void         ev_file_helpers_shutdown (void);
 
-gboolean   ev_xfer_uri_simple        (const char *from,
-                                     const char *to,
-                                     GError     **error);
+gchar*       ev_tmp_filename          (void);
+
+gboolean     ev_xfer_uri_simple       (const char *from,
+                                      const char *to,
+                                      GError     **error);
 
 G_END_DECLS
 
index 4bdf5d96acdd6d0fb069b6666b0af47e503ca38a..e735b131ce0a29e69836d226ffff34f0ed957dd4 100644 (file)
@@ -31,6 +31,7 @@
 #include <string.h>
 #include <libgnomeui/gnome-icon-lookup.h>
 
+#include "ev-file-helpers.h"
 #include "ev-sidebar-attachments.h"
 #include "ev-sidebar-page.h"
 
@@ -409,7 +410,7 @@ ev_sidebar_attachments_drag_data_get (GtkWidget        *widget,
                                    COLUMN_ATTACHMENT, &attachment,
                                    -1);
 
-               filename = g_build_filename (g_get_tmp_dir (),
+               filename = g_build_filename (ev_tmp_dir (),
                                             ev_attachment_get_name (attachment),
                                             NULL);
                
index c80314090e12bf6cbe33eefbb1fb604e983757c2..436096ce66deb37de6f8397e980beeddc1a958f0 100644 (file)
@@ -994,7 +994,7 @@ ev_window_clear_temp_file (EvWindow *ev_window)
 {
        GnomeVFSURI *uri;
        gchar       *filename;
-       gchar       *dir;
+       const gchar *tempdir;
 
        if (!ev_window->priv->uri)
                return;
@@ -1010,12 +1010,11 @@ ev_window_clear_temp_file (EvWindow *ev_window)
        if (!filename)
                return;
 
-       dir = g_path_get_dirname (filename);
-       if (g_ascii_strcasecmp (dir, g_get_tmp_dir ()) == 0) {
+       tempdir = g_get_tmp_dir ();
+       if (g_ascii_strncasecmp (filename, tempdir, strlen (tempdir)) == 0) {
                g_unlink (filename);
        }
 
-       g_free (dir);
        g_free (filename);
 }