aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEli Zaretskii2013-08-04 20:52:25 +0300
committerEli Zaretskii2013-08-04 20:52:25 +0300
commite443729d658ee2b9e0f55bbbb90241819bf516a6 (patch)
tree99b2252965cb7586270cbe13d16726df82f25a8e
parente0fdb6943066032db294720915c3bd644bf2bcd1 (diff)
downloademacs-e443729d658ee2b9e0f55bbbb90241819bf516a6.tar.gz
emacs-e443729d658ee2b9e0f55bbbb90241819bf516a6.zip
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. Fixes: debbugs:15015
-rw-r--r--lib-src/ChangeLog8
-rw-r--r--lib-src/ntlib.c55
-rw-r--r--lib-src/ntlib.h4
3 files changed, 65 insertions, 2 deletions
diff --git a/lib-src/ChangeLog b/lib-src/ChangeLog
index 2cfe46f5352..ace62d9d8f7 100644
--- a/lib-src/ChangeLog
+++ b/lib-src/ChangeLog
@@ -1,3 +1,11 @@
12013-08-04 Eli Zaretskii <eliz@gnu.org>
2
3 * ntlib.h: Include fcntl.h.
4 (mkostemp): Declare prototype.
5 (mktemp): Don't redefine.
6
7 * ntlib.c (mkostemp): New function. (Bug#15015)
8
12013-08-04 Paul Eggert <eggert@cs.ucla.edu> 92013-08-04 Paul Eggert <eggert@cs.ucla.edu>
2 10
3 Fix some minor races in hosts lacking mkostemp (Bug#15015). 11 Fix some minor races in hosts lacking mkostemp (Bug#15015).
diff --git a/lib-src/ntlib.c b/lib-src/ntlib.c
index 41b4e3a0cbc..0d0642d1bf2 100644
--- a/lib-src/ntlib.c
+++ b/lib-src/ntlib.c
@@ -422,3 +422,58 @@ lstat (const char * path, struct stat * buf)
422{ 422{
423 return stat (path, buf); 423 return stat (path, buf);
424} 424}
425
426/* Implementation of mkostemp for MS-Windows, to avoid race conditions
427 when using mktemp.
428
429 Standard algorithm for generating a temporary file name seems to be
430 use pid or tid with a letter on the front (in place of the 6 X's)
431 and cycle through the letters to find a unique name. We extend
432 that to allow any reasonable character as the first of the 6 X's,
433 so that the number of simultaneously used temporary files will be
434 greater. */
435
436int
437mkostemp (char * template, int flags)
438{
439 char * p;
440 int i, fd = -1;
441 unsigned uid = GetCurrentThreadId ();
442 int save_errno = errno;
443 static char first_char[] = "abcdefghijklmnopqrstuvwyz0123456789!%-_@#";
444
445 errno = EINVAL;
446 if (template == NULL)
447 return -1;
448
449 p = template + strlen (template);
450 i = 5;
451 /* replace up to the last 5 X's with uid in decimal */
452 while (--p >= template && p[0] == 'X' && --i >= 0)
453 {
454 p[0] = '0' + uid % 10;
455 uid /= 10;
456 }
457
458 if (i < 0 && p[0] == 'X')
459 {
460 i = 0;
461 do
462 {
463 p[0] = first_char[i];
464 if ((fd = open (template,
465 flags | _O_CREAT | _O_EXCL | _O_RDWR,
466 S_IRUSR | S_IWUSR)) >= 0
467 || errno != EEXIST)
468 {
469 if (fd >= 0)
470 errno = save_errno;
471 return fd;
472 }
473 }
474 while (++i < sizeof (first_char));
475 }
476
477 /* Template is badly formed or else we can't generate a unique name. */
478 return -1;
479}
diff --git a/lib-src/ntlib.h b/lib-src/ntlib.h
index 3e48d2997e0..c30958365ca 100644
--- a/lib-src/ntlib.h
+++ b/lib-src/ntlib.h
@@ -22,6 +22,7 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
22/* Include these headers now so we don't have to worry about include 22/* Include these headers now so we don't have to worry about include
23 order dependencies in common source files. */ 23 order dependencies in common source files. */
24#include <direct.h> 24#include <direct.h>
25#include <fcntl.h>
25#include <io.h> 26#include <io.h>
26#include <stdio.h> 27#include <stdio.h>
27 28
@@ -41,6 +42,7 @@ int setuid (unsigned uid);
41int setregid (unsigned rgid, unsigned gid); 42int setregid (unsigned rgid, unsigned gid);
42char * getpass (const char * prompt); 43char * getpass (const char * prompt);
43int fchown (int fd, unsigned uid, unsigned gid); 44int fchown (int fd, unsigned uid, unsigned gid);
45int mkostemp (char * template, int flags);
44 46
45/* redirect or undo interceptions created by config.h */ 47/* redirect or undo interceptions created by config.h */
46#undef access 48#undef access
@@ -61,8 +63,6 @@ int fchown (int fd, unsigned uid, unsigned gid);
61#undef fopen 63#undef fopen
62#undef mkdir 64#undef mkdir
63#define mkdir _mkdir 65#define mkdir _mkdir
64#undef mktemp
65#define mktemp _mktemp
66#undef open 66#undef open
67#define open _open 67#define open _open
68#undef pipe 68#undef pipe