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