Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/lib/ev-file-helpers.c
diff options
context:
space:
mode:
authorCarlos Garcia Campos <carlosgc@gnome.org>2006-11-15 11:33:09 (GMT)
committer Carlos Garcia Campos <carlosgc@src.gnome.org>2006-11-15 11:33:09 (GMT)
commit67654fe8c933af2932e2c87ec2a4560cb064c545 (patch)
treea272dcff310f3b4f7b9b34652d42db7cda755f67 /lib/ev-file-helpers.c
parent1dd619d11cc65d17b3fd08b650668fb3f85e35e9 (diff)
Use always ev_tmp_dir instead of g_get_tmp_dir. Fix a race condition in
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.
Diffstat (limited to 'lib/ev-file-helpers.c')
-rw-r--r--lib/ev-file-helpers.c86
1 files changed, 45 insertions, 41 deletions
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 <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>
@@ -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));