Make safe_open with O_EXCL friendlier for NFS.
authorBrendan Cully <brendan@kublai.com>
Fri, 6 Apr 2007 19:54:46 +0000 (12:54 -0700)
committerBrendan Cully <brendan@kublai.com>
Fri, 6 Apr 2007 19:54:46 +0000 (12:54 -0700)
Per #2707, when an open file is moved into a different directory over
NFS, it may leave a .nfsXXX hardlink behind. This causes the rmdir in
safe_open to fail, leaving tempdir droppings around. This patch works
around the problem by closing the file after creating it and reopening
it after rename.

lib.c

diff --git a/lib.c b/lib.c
index e6e80c2edc253583c20d25524641ee492ea8e3b1..a0cbca22f45e8252eab903868fb6dd16a23d94e7 100644 (file)
--- a/lib.c
+++ b/lib.c
@@ -575,18 +575,15 @@ int safe_open (const char *path, int flags)
       rmdir (safe_dir);
       return fd;
     }
-    
+
+    /* NFS and I believe cygwin do not handle movement of open files well */
+    close (fd);
     if (mutt_put_file_in_place (path, safe_file, safe_dir) == -1)
-    {
-      close (fd);
       return -1;
-    }
-  }
-  else
-  {
-    if ((fd = open (path, flags, 0600)) < 0)
-      return fd;
   }
+
+  if ((fd = open (path, flags & ~O_EXCL, 0600)) < 0)
+    return fd;
     
   /* make sure the file is not symlink */
   if (lstat (path, &osb) < 0 || fstat (fd, &nsb) < 0 ||