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