683572
From 0424723402ef153af8ee44222315d9b6a818d1ba Mon Sep 17 00:00:00 2001
683572
From: Tony Cook <tony@develop-help.com>
683572
Date: Tue, 2 Jul 2019 15:22:26 +1000
683572
Subject: [PATCH 2/3] (perl #134221) support append mode temp files on Win32
683572
 too
683572
MIME-Version: 1.0
683572
Content-Type: text/plain; charset=UTF-8
683572
Content-Transfer-Encoding: 8bit
683572
683572
Signed-off-by: Petr Písař <ppisar@redhat.com>
683572
---
683572
 perlio.c         |  2 +-
683572
 win32/win32.c    | 10 +++++++++-
683572
 win32/win32iop.h |  1 +
683572
 3 files changed, 11 insertions(+), 2 deletions(-)
683572
683572
diff --git a/perlio.c b/perlio.c
683572
index a737e79e02..81ebc156ad 100644
683572
--- a/perlio.c
683572
+++ b/perlio.c
683572
@@ -5059,7 +5059,7 @@ PerlIO_tmpfile_flags(int imode)
683572
 #endif
683572
      PerlIO *f = NULL;
683572
 #ifdef WIN32
683572
-     const int fd = win32_tmpfd();
683572
+     const int fd = win32_tmpfd_mode(imode);
683572
      if (fd >= 0)
683572
 	  f = PerlIO_fdopen(fd, "w+b");
683572
 #elif ! defined(VMS) && ! defined(OS2)
683572
diff --git a/win32/win32.c b/win32/win32.c
683572
index 8104d864c2..91fdffe09b 100644
683572
--- a/win32/win32.c
683572
+++ b/win32/win32.c
683572
@@ -2907,10 +2907,18 @@ win32_rewind(FILE *pf)
683572
 
683572
 DllExport int
683572
 win32_tmpfd(void)
683572
+{
683572
+    return win32_tmpfd_mode(0);
683572
+}
683572
+
683572
+DllExport int
683572
+win32_tmpfd_mode(int mode)
683572
 {
683572
     char prefix[MAX_PATH+1];
683572
     char filename[MAX_PATH+1];
683572
     DWORD len = GetTempPath(MAX_PATH, prefix);
683572
+    mode &= ~( O_ACCMODE | O_CREAT | O_EXCL );
683572
+    mode |= O_RDWR;
683572
     if (len && len < MAX_PATH) {
683572
 	if (GetTempFileName(prefix, "plx", 0, filename)) {
683572
 	    HANDLE fh = CreateFile(filename,
683572
@@ -2922,7 +2930,7 @@ win32_tmpfd(void)
683572
 				   | FILE_FLAG_DELETE_ON_CLOSE,
683572
 				   NULL);
683572
 	    if (fh != INVALID_HANDLE_VALUE) {
683572
-		int fd = win32_open_osfhandle((intptr_t)fh, 0);
683572
+		int fd = win32_open_osfhandle((intptr_t)fh, mode);
683572
 		if (fd >= 0) {
683572
 		    PERL_DEB(dTHX;)
683572
 		    DEBUG_p(PerlIO_printf(Perl_debug_log,
683572
diff --git a/win32/win32iop.h b/win32/win32iop.h
683572
index 53330e5951..559e1f9cd2 100644
683572
--- a/win32/win32iop.h
683572
+++ b/win32/win32iop.h
683572
@@ -64,6 +64,7 @@ DllExport  int		win32_fgetpos(FILE *pf,fpos_t *p);
683572
 DllExport  int		win32_fsetpos(FILE *pf,const fpos_t *p);
683572
 DllExport  void		win32_rewind(FILE *pf);
683572
 DllExport  int		win32_tmpfd(void);
683572
+DllExport  int		win32_tmpfd_mode(int mode);
683572
 DllExport  FILE*	win32_tmpfile(void);
683572
 DllExport  void		win32_abort(void);
683572
 DllExport  int  	win32_fstat(int fd,Stat_t *sbufptr);
683572
-- 
683572
2.20.1
683572