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