]> code.delx.au - gnu-emacs/commitdiff
MS-Windows followup to last commit.
authorEli Zaretskii <eliz@gnu.org>
Sun, 4 Aug 2013 17:52:25 +0000 (20:52 +0300)
committerEli Zaretskii <eliz@gnu.org>
Sun, 4 Aug 2013 17:52:25 +0000 (20:52 +0300)
 lib-src/ntlib.h: Include fcntl.h.
 (mkostemp): Declare prototype.
 (mktemp): Don't redefine.
 lib-src/ntlib.c (mkostemp): New function.

Fixes: debbugs:15015
lib-src/ChangeLog
lib-src/ntlib.c
lib-src/ntlib.h

index 2cfe46f53527d6abbd922686ffbd4f1074a07c7e..ace62d9d8f7bcd6abd07b13b29f09fc38f5c98e2 100644 (file)
@@ -1,3 +1,11 @@
+2013-08-04  Eli Zaretskii  <eliz@gnu.org>
+
+       * ntlib.h: Include fcntl.h.
+       (mkostemp): Declare prototype.
+       (mktemp): Don't redefine.
+
+       * ntlib.c (mkostemp): New function.  (Bug#15015)
+
 2013-08-04  Paul Eggert  <eggert@cs.ucla.edu>
 
        Fix some minor races in hosts lacking mkostemp (Bug#15015).
index 41b4e3a0cbce870e6f0aae8d8f9cfbc0febb8778..0d0642d1bf225ed05044715054dc93a21c87440f 100644 (file)
@@ -422,3 +422,58 @@ lstat (const char * path, struct stat * buf)
 {
   return stat (path, buf);
 }
+
+/* Implementation of mkostemp for MS-Windows, to avoid race conditions
+   when using mktemp.
+
+   Standard algorithm for generating a temporary file name seems to be
+   use pid or tid with a letter on the front (in place of the 6 X's)
+   and cycle through the letters to find a unique name.  We extend
+   that to allow any reasonable character as the first of the 6 X's,
+   so that the number of simultaneously used temporary files will be
+   greater.  */
+
+int
+mkostemp (char * template, int flags)
+{
+  char * p;
+  int i, fd = -1;
+  unsigned uid = GetCurrentThreadId ();
+  int save_errno = errno;
+  static char first_char[] = "abcdefghijklmnopqrstuvwyz0123456789!%-_@#";
+
+  errno = EINVAL;
+  if (template == NULL)
+    return -1;
+
+  p = template + strlen (template);
+  i = 5;
+  /* replace up to the last 5 X's with uid in decimal */
+  while (--p >= template && p[0] == 'X' && --i >= 0)
+    {
+      p[0] = '0' + uid % 10;
+      uid /= 10;
+    }
+
+  if (i < 0 && p[0] == 'X')
+    {
+      i = 0;
+      do
+       {
+         p[0] = first_char[i];
+         if ((fd = open (template,
+                         flags | _O_CREAT | _O_EXCL | _O_RDWR,
+                         S_IRUSR | S_IWUSR)) >= 0
+             || errno != EEXIST)
+           {
+             if (fd >= 0)
+               errno = save_errno;
+             return fd;
+           }
+       }
+      while (++i < sizeof (first_char));
+    }
+
+  /* Template is badly formed or else we can't generate a unique name.  */
+  return -1;
+}
index 3e48d2997e0881efecc77100a02521d461b62da4..c30958365ca599eae498e9f6041a93f9950b6242 100644 (file)
@@ -22,6 +22,7 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 /* Include these headers now so we don't have to worry about include
    order dependencies in common source files.  */
 #include <direct.h>
+#include <fcntl.h>
 #include <io.h>
 #include <stdio.h>
 
@@ -41,6 +42,7 @@ int setuid (unsigned uid);
 int setregid (unsigned rgid, unsigned gid);
 char * getpass (const char * prompt);
 int fchown (int fd, unsigned uid, unsigned gid);
+int mkostemp (char * template, int flags);
 
 /* redirect or undo interceptions created by config.h */
 #undef access
@@ -61,8 +63,6 @@ int fchown (int fd, unsigned uid, unsigned gid);
 #undef fopen
 #undef mkdir
 #define mkdir   _mkdir
-#undef mktemp
-#define mktemp  _mktemp
 #undef open
 #define open    _open
 #undef pipe