From 130f81bd123de154be5247d3237610d1eeab5dca Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Wed, 9 Aug 2017 16:34:40 -0700 Subject: [PATCH 2/2] Remove ms-w32 mkostemp * lib-src/ntlib.c, nt/inc/ms-w32.h, src/w32.c (mkostemp): * nt/gnulib-cfg.mk (OMIT_GNULIB_MODULE_mkostemp): * nt/mingw-cfg.site (ac_cv_func_mkostemp): Remove, since the generic mkostemp should work on MS-Windows. --- lib-src/ntlib.c | 56 ------------------------------------------------------- nt/gnulib-cfg.mk | 1 - nt/inc/ms-w32.h | 2 -- nt/mingw-cfg.site | 1 - src/w32.c | 55 ------------------------------------------------------ 5 files changed, 115 deletions(-) diff --git a/lib-src/ntlib.c b/lib-src/ntlib.c index 78ba9061f6..078a43e79c 100644 --- a/lib-src/ntlib.c +++ b/lib-src/ntlib.c @@ -37,7 +37,6 @@ along with GNU Emacs. If not, see . */ char *sys_ctime (const time_t *); FILE *sys_fopen (const char *, const char *); int sys_chdir (const char *); -int mkostemp (char *, int); int sys_rename (const char *, const char *); /* MinGW64 defines _TIMEZONE_DEFINED and defines 'struct timespec' in @@ -396,61 +395,6 @@ lstat (const char * path, struct stat * buf) return stat (path, buf); } -/* Implementation of mkostemp for MS-Windows, to avoid race conditions - when using mktemp. Copied from w32.c. - - This is used only in update-game-score.c. It is overkill for that - use case, since update-game-score renames the temporary file into - the game score file, which isn't atomic on MS-Windows anyway, when - the game score already existed before running the program, which it - almost always does. But using a simpler implementation just to - make a point is uneconomical... */ - -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; -} - /* On Windows, you cannot rename into an existing file. */ int sys_rename (const char *from, const char *to) diff --git a/nt/gnulib-cfg.mk b/nt/gnulib-cfg.mk index 175329fb9e..ad798bbe37 100644 --- a/nt/gnulib-cfg.mk +++ b/nt/gnulib-cfg.mk @@ -50,7 +50,6 @@ OMIT_GNULIB_MODULE_dirfd = OMIT_GNULIB_MODULE_fcntl = true OMIT_GNULIB_MODULE_fcntl-h = true OMIT_GNULIB_MODULE_inttypes-incomplete = true -OMIT_GNULIB_MODULE_mkostemp = true OMIT_GNULIB_MODULE_pipe2 = true OMIT_GNULIB_MODULE_secure_getenv = true OMIT_GNULIB_MODULE_signal-h = true diff --git a/nt/inc/ms-w32.h b/nt/inc/ms-w32.h index 957d8c6bdb..6cbbb9f869 100644 --- a/nt/inc/ms-w32.h +++ b/nt/inc/ms-w32.h @@ -518,8 +518,6 @@ extern int getpagesize (void); extern void * memrchr (void const *, int, size_t); -extern int mkostemp (char *, int); - #if defined (__MINGW32__) diff --git a/nt/mingw-cfg.site b/nt/mingw-cfg.site index a106717979..d9a824008c 100644 --- a/nt/mingw-cfg.site +++ b/nt/mingw-cfg.site @@ -79,7 +79,6 @@ ac_cv_func_getaddrinfo=yes # Implemented as an inline function in ws2tcpip.h ac_cv_func_gai_strerror=yes # Implemented in w32.c -ac_cv_func_mkostemp=yes ac_cv_func_readlink=yes ac_cv_func_symlink=yes # Avoid run-time tests of readlink and symlink, which will fail diff --git a/src/w32.c b/src/w32.c index fa3cbe183f..1c58f92d57 100644 --- a/src/w32.c +++ b/src/w32.c @@ -4397,61 +4397,6 @@ sys_open (const char * path, int oflag, int mode) return res; } -/* 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 = sys_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; -} - int fchmod (int fd, mode_t mode) { -- 2.13.4