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