From 33c47b5734771e54f10b2e3cf42cf2ae943b09c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amadeusz=20S=C5=82awi=C5=84ski?= Date: Fri, 21 Jun 2013 13:54:11 +0400 Subject: [PATCH] Ticket #3025: race condition when creating temporary directory. When there's no mc-tmpdir and a user tries to start two mc sessions simultaneously, sometimes (in one out of ten attempts on my machine) one mc session emits the following error message: Cannot create temporary directory /tmp/mc-lars: File exists (17) Temporary files will be created in /tmp Press any key to continue... Steps to reproduce: # rm /tmp/mc-$(whoami) -rf # uxterm -e mc & uxterm -e mc Signed-off-by: Andrew Borodin --- lib/vfs/interface.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/vfs/interface.c b/lib/vfs/interface.c index dd685319f..cc3d41819 100644 --- a/lib/vfs/interface.c +++ b/lib/vfs/interface.c @@ -840,20 +840,20 @@ mc_tmpdir (void) canonicalize_pathname (buffer); - if (lstat (buffer, &st) == 0) + /* Try to create directory */ + if (mkdir (buffer, S_IRWXU) != 0) { - /* Sanity check for existing directory */ - if (!S_ISDIR (st.st_mode)) - error = _("%s is not a directory\n"); - else if (st.st_uid != getuid ()) - error = _("Directory %s is not owned by you\n"); - else if (((st.st_mode & 0777) != 0700) && (chmod (buffer, 0700) != 0)) - error = _("Cannot set correct permissions for directory %s\n"); - } - else - { - /* Need to create directory */ - if (mkdir (buffer, S_IRWXU) != 0) + if (errno == EEXIST && lstat (buffer, &st) == 0) + { + /* Sanity check for existing directory */ + if (!S_ISDIR (st.st_mode)) + error = _("%s is not a directory\n"); + else if (st.st_uid != getuid ()) + error = _("Directory %s is not owned by you\n"); + else if (((st.st_mode & 0777) != 0700) && (chmod (buffer, 0700) != 0)) + error = _("Cannot set correct permissions for directory %s\n"); + } + else { fprintf (stderr, _("Cannot create temporary directory %s: %s\n"),