autoconf-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH 2/2] Ignore failure of setting mode on a temporary file on OS


From: Zack Weinberg
Subject: Re: [PATCH 2/2] Ignore failure of setting mode on a temporary file on OS/2
Date: Wed, 18 Oct 2023 13:34:35 -0400
User-agent: Cyrus-JMAP/3.9.0-alpha0-1019-ged83ad8595-fm-20231002.001-ged83ad85

On Wed, Oct 18, 2023, at 10:21 AM, KO Myung-Hun wrote:
> Zack Weinberg wrote:
>> I don’t want to have code in Autoconf that is only safe because of
>> non-obvious details of the context it’s used in.  People might
>> reuse the code in a different context where it’s *not* safe, without
>> realizing the danger.  So I suggest we use the appended patch
>> instead of your patch.  I’ve tested this on Unix systems with both
>> Perl 5.6 and Perl 5.38.  Could you please test it on your OS/2 system
>> as well?
>
> It works well here.

Great! I have committed the version below.

zw

>From c4a695510d240491f89d78204a3d5a6fdbe03648 Mon Sep 17 00:00:00 2001
From: Zack Weinberg <zack@owlfolio.org>
Date: Wed, 18 Oct 2023 13:23:36 -0400
Subject: [PATCH] autom4te: OS/2 compat: Do not attempt to chmod an open file.

On OS/2, chmod(2) cannot be applied to an open file.

Instead set the desired permissions when the file is initially
created, using the PERMS argument to File::Temp::tempfile if
possible, or by manually emulating that feature if the system
perl does not provide a new enough version of File::Temp.

This has the nice side effect that we no longer need to handle
the umask manually.

* autom4te.in (tempfile_with_mode): New function.
  (handle_output): Use tempfile_with_mode instead of directly using
  File::Temp plus chmod.

Co-authored-by: KO Myung-Hun <komh78@gmail.com>
---
 bin/autom4te.in | 56 +++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 47 insertions(+), 9 deletions(-)

diff --git a/bin/autom4te.in b/bin/autom4te.in
index 71d7e6a6..41da77b0 100644
--- a/bin/autom4te.in
+++ b/bin/autom4te.in
@@ -222,6 +222,52 @@ Written by Akim Demaille.
 ## Routines.  ##
 ## ---------- ##
 
+# tempfile_with_mode ($dir, $mode)
+# --------------------------------
+# Create a temporary file in $dir with access control bits $mode.
+# Returns a list ($fh, $fname) where $fh is a filehandle open for
+# writing to the file, and $fname is the name of the file.
+sub tempfile_with_mode ($$)
+{
+  my ($dir, $mode) = @_;
+
+  require File::Temp;
+  my $template = "actmp." . File::Temp::TEMPXXX;
+
+  # The PERMS argument was added to File::Temp::tempfile in version
+  # 0.2310 of the File::Temp module; it will be silently ignored if
+  # passed to an older version of the function.  This is the simplest
+  # way to do a non-fatal version check without features of Perl 5.10.
+  local $@;
+  if (eval { File::Temp->VERSION("0.2310"); 1 })
+    {
+      # Can use PERMS argument to tempfile().
+      return File::Temp::tempfile ($template, DIR => $dir, PERMS => $mode,
+                                   UNLINK => 0);
+    }
+  else
+    {
+      # PERMS is not available.
+      # This is functionally equivalent to what it would do.
+      require Fcntl;
+      my $openflags = Fcntl::O_RDWR | Fcntl::O_CREAT | Fcntl::O_EXCL;
+
+      require File::Spec;
+      $template = File::Spec->catfile($dir, $template);
+
+      # 50 = $MAX_GUESS in File::Temp (not an exported constant).
+      for (my $i = 0; $i < 50; $i++)
+        {
+          my $filename = File::Temp::mktemp($template);
+          my $fh;
+          my $success = sysopen ($fh, $filename, $openflags, $mode);
+          return ($fh, $filename) if $success;
+          fatal "Could not create temp file $filename: $!"
+            unless $!{EEXIST};
+        }
+      fatal "Could not create any temp file from $template: $!";
+    }
+}
 
 # $OPTION
 # files_to_options (@FILE)
@@ -563,15 +609,7 @@ sub handle_output ($$)
   else
     {
       my (undef, $outdir, undef) = fileparse ($output);
-
-      use File::Temp qw (tempfile);
-      ($out, $scratchfile) = tempfile (UNLINK => 0, DIR => $outdir);
-      fatal "cannot create a file in $outdir: $!"
-        unless $out;
-
-      # File::Temp doesn't give us access to 3-arg open(2), unfortunately.
-      chmod (oct ($mode) & ~(umask), $scratchfile)
-        or fatal "setting mode of " . $scratchfile . ": $!";
+      ($out, $scratchfile) = tempfile_with_mode ($outdir, oct($mode));
     }
 
   my $in = new Autom4te::XFile ($ocache . $req->id, "<");
-- 
2.41.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]