8b21adb
From 8e9cf86aa69cb79c91edf5ff0586f87bfe4c91bd Mon Sep 17 00:00:00 2001
8b21adb
From: Tony Cook <tony@develop-help.com>
8b21adb
Date: Tue, 2 Jul 2019 14:16:35 +1000
8b21adb
Subject: [PATCH] (perl #134221) support append mode for open .. undef
8b21adb
MIME-Version: 1.0
8b21adb
Content-Type: text/plain; charset=UTF-8
8b21adb
Content-Transfer-Encoding: 8bit
8b21adb
8b21adb
Petr Písař: Ported to 5.30.0 from
8b21adb
45b29440d38be155c5177c8d6f9a5d4e7c2c098c.
8b21adb
8b21adb
Signed-off-by: Petr Písař <ppisar@redhat.com>
8b21adb
---
8b21adb
 doio.c             | 15 +++++++++++++++
8b21adb
 embed.fnc          |  1 +
8b21adb
 perlio.c           | 26 +++++++++++++++++++++-----
8b21adb
 perlio.h           |  3 +++
8b21adb
 proto.h            |  5 +++++
8b21adb
 t/io/perlio_open.t | 14 ++++++++++++--
8b21adb
 6 files changed, 57 insertions(+), 7 deletions(-)
8b21adb
8b21adb
diff --git a/doio.c b/doio.c
8b21adb
index 05a0696..424e0e3 100644
8b21adb
--- a/doio.c
8b21adb
+++ b/doio.c
8b21adb
@@ -265,6 +265,21 @@ Perl_my_mkstemp_cloexec(char *templte)
8b21adb
 #endif
8b21adb
 }
8b21adb
 
8b21adb
+int
8b21adb
+Perl_my_mkostemp_cloexec(char *templte, int flags)
8b21adb
+{
8b21adb
+    dVAR;
8b21adb
+    PERL_ARGS_ASSERT_MY_MKOSTEMP_CLOEXEC;
8b21adb
+#if defined(O_CLOEXEC)
8b21adb
+    DO_ONEOPEN_EXPERIMENTING_CLOEXEC(
8b21adb
+        PL_strategy_mkstemp,
8b21adb
+	Perl_my_mkostemp(templte, flags | O_CLOEXEC),
8b21adb
+	Perl_my_mkostemp(templte, flags));
8b21adb
+#else
8b21adb
+    DO_ONEOPEN_THEN_CLOEXEC(Perl_my_mkostemp(templte, flags));
8b21adb
+#endif
8b21adb
+}
8b21adb
+
8b21adb
 #ifdef HAS_PIPE
8b21adb
 int
8b21adb
 Perl_PerlProc_pipe_cloexec(pTHX_ int *pipefd)
8b21adb
diff --git a/embed.fnc b/embed.fnc
8b21adb
index 259affd..c977d39 100644
8b21adb
--- a/embed.fnc
8b21adb
+++ b/embed.fnc
8b21adb
@@ -476,6 +476,7 @@ p	|int	|PerlLIO_dup2_cloexec|int oldfd|int newfd
8b21adb
 pR	|int	|PerlLIO_open_cloexec|NN const char *file|int flag
8b21adb
 pR	|int	|PerlLIO_open3_cloexec|NN const char *file|int flag|int perm
8b21adb
 pnoR	|int	|my_mkstemp_cloexec|NN char *templte
8b21adb
+pnoR	|int	|my_mkostemp_cloexec|NN char *templte|int flags
8b21adb
 #ifdef HAS_PIPE
8b21adb
 pR	|int	|PerlProc_pipe_cloexec|NN int *pipefd
8b21adb
 #endif
8b21adb
diff --git a/perlio.c b/perlio.c
8b21adb
index 904d47a..5a0cd36 100644
8b21adb
--- a/perlio.c
8b21adb
+++ b/perlio.c
8b21adb
@@ -1490,7 +1490,9 @@ PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd,
8b21adb
 	     int imode, int perm, PerlIO *f, int narg, SV **args)
8b21adb
 {
8b21adb
     if (!f && narg == 1 && *args == &PL_sv_undef) {
8b21adb
-	if ((f = PerlIO_tmpfile())) {
8b21adb
+        int imode = PerlIOUnix_oflags(mode);
8b21adb
+
8b21adb
+	if (imode != -1 && (f = PerlIO_tmpfile_flags(imode))) {
8b21adb
 	    if (!layers || !*layers)
8b21adb
 		layers = Perl_PerlIO_context_layers(aTHX_ mode);
8b21adb
 	    if (layers && *layers)
8b21adb
@@ -5048,6 +5050,15 @@ PerlIO_stdoutf(const char *fmt, ...)
8b21adb
 #undef PerlIO_tmpfile
8b21adb
 PerlIO *
8b21adb
 PerlIO_tmpfile(void)
8b21adb
+{
8b21adb
+    return PerlIO_tmpfile_flags(0);
8b21adb
+}
8b21adb
+
8b21adb
+#define MKOSTEMP_MODES ( O_RDWR | O_CREAT | O_EXCL )
8b21adb
+#define MKOSTEMP_MODE_MASK ( O_ACCMODE | O_CREAT | O_EXCL | O_TRUNC )
8b21adb
+
8b21adb
+PerlIO *
8b21adb
+PerlIO_tmpfile_flags(int imode)
8b21adb
 {
8b21adb
 #ifndef WIN32
8b21adb
      dTHX;
8b21adb
@@ -5063,27 +5074,32 @@ PerlIO_tmpfile(void)
8b21adb
      const char * const tmpdir = TAINTING_get ? NULL : PerlEnv_getenv("TMPDIR");
8b21adb
      SV * sv = NULL;
8b21adb
      int old_umask = umask(0177);
8b21adb
+     imode &= ~MKOSTEMP_MODE_MASK;
8b21adb
      if (tmpdir && *tmpdir) {
8b21adb
 	 /* if TMPDIR is set and not empty, we try that first */
8b21adb
 	 sv = newSVpv(tmpdir, 0);
8b21adb
 	 sv_catpv(sv, tempname + 4);
8b21adb
-	 fd = Perl_my_mkstemp_cloexec(SvPVX(sv));
8b21adb
+	 fd = Perl_my_mkostemp_cloexec(SvPVX(sv), imode);
8b21adb
      }
8b21adb
      if (fd < 0) {
8b21adb
 	 SvREFCNT_dec(sv);
8b21adb
 	 sv = NULL;
8b21adb
 	 /* else we try /tmp */
8b21adb
-	 fd = Perl_my_mkstemp_cloexec(tempname);
8b21adb
+	 fd = Perl_my_mkostemp_cloexec(tempname, imode);
8b21adb
      }
8b21adb
      if (fd < 0) {
8b21adb
          /* Try cwd */
8b21adb
          sv = newSVpvs(".");
8b21adb
          sv_catpv(sv, tempname + 4);
8b21adb
-         fd = Perl_my_mkstemp_cloexec(SvPVX(sv));
8b21adb
+         fd = Perl_my_mkostemp_cloexec(SvPVX(sv), imode);
8b21adb
      }
8b21adb
      umask(old_umask);
8b21adb
      if (fd >= 0) {
8b21adb
-	  f = PerlIO_fdopen(fd, "w+");
8b21adb
+         /* fdopen() with a numeric mode */
8b21adb
+         char mode[8];
8b21adb
+         int writing = 1;
8b21adb
+         (void)PerlIO_intmode2str(imode | MKOSTEMP_MODES, mode, &writing);
8b21adb
+         f = PerlIO_fdopen(fd, mode);
8b21adb
 	  if (f)
8b21adb
 	       PerlIOBase(f)->flags |= PERLIO_F_TEMP;
8b21adb
 	  PerlLIO_unlink(sv ? SvPVX_const(sv) : tempname);
8b21adb
diff --git a/perlio.h b/perlio.h
8b21adb
index d515020..ee16ab8 100644
8b21adb
--- a/perlio.h
8b21adb
+++ b/perlio.h
8b21adb
@@ -286,6 +286,9 @@ PERL_CALLCONV SSize_t PerlIO_get_bufsiz(PerlIO *);
8b21adb
 #ifndef PerlIO_tmpfile
8b21adb
 PERL_CALLCONV PerlIO *PerlIO_tmpfile(void);
8b21adb
 #endif
8b21adb
+#ifndef PerlIO_tmpfile_flags
8b21adb
+PERL_CALLCONV PerlIO *PerlIO_tmpfile_flags(int flags);
8b21adb
+#endif
8b21adb
 #ifndef PerlIO_stdin
8b21adb
 PERL_CALLCONV PerlIO *PerlIO_stdin(void);
8b21adb
 #endif
8b21adb
diff --git a/proto.h b/proto.h
8b21adb
index 74a8e46..e0ea55b 100644
8b21adb
--- a/proto.h
8b21adb
+++ b/proto.h
8b21adb
@@ -2270,6 +2270,11 @@ PERL_CALLCONV Pid_t	Perl_my_fork(void);
8b21adb
 PERL_CALLCONV I32	Perl_my_lstat(pTHX);
8b21adb
 #endif
8b21adb
 PERL_CALLCONV I32	Perl_my_lstat_flags(pTHX_ const U32 flags);
8b21adb
+PERL_CALLCONV int	Perl_my_mkostemp_cloexec(char *templte, int flags)
8b21adb
+			__attribute__warn_unused_result__;
8b21adb
+#define PERL_ARGS_ASSERT_MY_MKOSTEMP_CLOEXEC	\
8b21adb
+	assert(templte)
8b21adb
+
8b21adb
 PERL_CALLCONV int	Perl_my_mkstemp_cloexec(char *templte)
8b21adb
 			__attribute__warn_unused_result__;
8b21adb
 #define PERL_ARGS_ASSERT_MY_MKSTEMP_CLOEXEC	\
8b21adb
diff --git a/t/io/perlio_open.t b/t/io/perlio_open.t
8b21adb
index 99d7e51..56c354b 100644
8b21adb
--- a/t/io/perlio_open.t
8b21adb
+++ b/t/io/perlio_open.t
8b21adb
@@ -11,7 +11,7 @@ BEGIN {
8b21adb
 use strict;
8b21adb
 use warnings;
8b21adb
 
8b21adb
-plan tests => 6;
8b21adb
+plan tests => 10;
8b21adb
 
8b21adb
 use Fcntl qw(:seek);
8b21adb
 
8b21adb
@@ -31,6 +31,16 @@ use Fcntl qw(:seek);
8b21adb
     is($data, "the right read stuff", "found the right stuff");
8b21adb
 }
8b21adb
 
8b21adb
-
8b21adb
+SKIP:
8b21adb
+{
8b21adb
+    ok((open my $fh, "+>>", undef), "open my \$fh, '+>>', undef")
8b21adb
+      or skip "can't open temp for append: $!", 3;
8b21adb
+    print $fh "abc";
8b21adb
+    ok(seek($fh, 0, SEEK_SET), "seek to zero");
8b21adb
+    print $fh "xyz";
8b21adb
+    ok(seek($fh, 0, SEEK_SET), "seek to zero again");
8b21adb
+    my $data = <$fh>;
8b21adb
+    is($data, "abcxyz", "check the second write appended");
8b21adb
+}
8b21adb
 
8b21adb
 
8b21adb
-- 
8b21adb
2.20.1
8b21adb