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. --- (limited to 'lib') 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 -- cgit v0.9.1