From 67654fe8c933af2932e2c87ec2a4560cb064c545 Mon Sep 17 00:00:00 2001 From: Carlos Garcia Campos Date: Wed, 15 Nov 2006 11:33:09 +0000 Subject: Use always ev_tmp_dir instead of g_get_tmp_dir. Fix a race condition in 2006-11-15 Carlos Garcia Campos * 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. --- diff --git a/ChangeLog b/ChangeLog index 9015f1b..1afc00e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,17 @@ +2006-11-15 Carlos Garcia Campos + + * 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 * data/evince-ui.xml: diff --git a/backend/Makefile.am b/backend/Makefile.am index f92c607..f7ae5ec 100644 --- a/backend/Makefile.am +++ b/backend/Makefile.am @@ -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 \ diff --git a/backend/ev-attachment.c b/backend/ev-attachment.c index 63619af..7e7ca12 100644 --- a/backend/ev-attachment.c +++ b/backend/ev-attachment.c @@ -22,6 +22,7 @@ #include #include #include +#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)) { diff --git a/lib/ev-file-helpers.c b/lib/ev-file-helpers.c index 130c62b..9763831 100644 --- a/lib/ev-file-helpers.c +++ b/lib/ev-file-helpers.c @@ -22,9 +22,12 @@ #include "config.h" #endif -#include +#include +#include #include +#include #include +#include #include #include #include @@ -33,36 +36,30 @@ #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)); diff --git a/lib/ev-file-helpers.h b/lib/ev-file-helpers.h index 2cc817a..69ff83d 100644 --- a/lib/ev-file-helpers.h +++ b/lib/ev-file-helpers.h @@ -25,17 +25,19 @@ 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 diff --git a/shell/ev-sidebar-attachments.c b/shell/ev-sidebar-attachments.c index 4bdf5d9..e735b13 100644 --- a/shell/ev-sidebar-attachments.c +++ b/shell/ev-sidebar-attachments.c @@ -31,6 +31,7 @@ #include #include +#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); diff --git a/shell/ev-window.c b/shell/ev-window.c index c803140..436096c 100644 --- a/shell/ev-window.c +++ b/shell/ev-window.c @@ -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); } -- cgit v0.9.1