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