emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Emacs-diffs] trunk r113690: MS-Windows followup to last commit.


From: Eli Zaretskii
Subject: [Emacs-diffs] trunk r113690: MS-Windows followup to last commit.
Date: Sun, 04 Aug 2013 17:53:18 +0000
User-agent: Bazaar (2.6b2)

------------------------------------------------------------
revno: 113690
revision-id: address@hidden
parent: address@hidden
fixes bug: http://debbugs.gnu.org/15015
committer: Eli Zaretskii <address@hidden>
branch nick: trunk
timestamp: Sun 2013-08-04 20:52:25 +0300
message:
  MS-Windows followup to last commit.
  
   lib-src/ntlib.h: Include fcntl.h.
   (mkostemp): Declare prototype.
   (mktemp): Don't redefine.
   lib-src/ntlib.c (mkostemp): New function.
modified:
  lib-src/ChangeLog              changelog-20091113204419-o5vbwnq5f7feedwu-1608
  lib-src/ntlib.c                ntlib.c-20091113204419-o5vbwnq5f7feedwu-803
  lib-src/ntlib.h                ntlib.h-20091113204419-o5vbwnq5f7feedwu-971
=== modified file 'lib-src/ChangeLog'
--- a/lib-src/ChangeLog 2013-08-04 16:56:56 +0000
+++ b/lib-src/ChangeLog 2013-08-04 17:52:25 +0000
@@ -1,3 +1,11 @@
+2013-08-04  Eli Zaretskii  <address@hidden>
+
+       * ntlib.h: Include fcntl.h.
+       (mkostemp): Declare prototype.
+       (mktemp): Don't redefine.
+
+       * ntlib.c (mkostemp): New function.  (Bug#15015)
+
 2013-08-04  Paul Eggert  <address@hidden>
 
        Fix some minor races in hosts lacking mkostemp (Bug#15015).

=== modified file 'lib-src/ntlib.c'
--- a/lib-src/ntlib.c   2013-03-31 14:04:49 +0000
+++ b/lib-src/ntlib.c   2013-08-04 17:52:25 +0000
@@ -422,3 +422,58 @@
 {
   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[] = "address@hidden";
+
+  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;
+}

=== modified file 'lib-src/ntlib.h'
--- a/lib-src/ntlib.h   2013-03-30 17:00:51 +0000
+++ b/lib-src/ntlib.h   2013-08-04 17:52:25 +0000
@@ -22,6 +22,7 @@
 /* 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 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 @@
 #undef fopen
 #undef mkdir
 #define mkdir   _mkdir
-#undef mktemp
-#define mktemp  _mktemp
 #undef open
 #define open    _open
 #undef pipe


reply via email to

[Prev in Thread] Current Thread [Next in Thread]