From 071108b868387d9a6d5033c3d7a2dab4ee1e3996 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Mon, 1 Oct 2018 10:40:36 +0200 Subject: [PATCH] Fix `atomic_write` inside non-sticky directory. --- Library/Homebrew/extend/pathname.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Library/Homebrew/extend/pathname.rb b/Library/Homebrew/extend/pathname.rb index 57629775d8..02ff1e35b4 100644 --- a/Library/Homebrew/extend/pathname.rb +++ b/Library/Homebrew/extend/pathname.rb @@ -160,9 +160,16 @@ class Pathname open("a", *open_args) { |f| f.puts(content) } end - # NOTE always overwrites + # NOTE: This always overwrites. def atomic_write(content) + # The enclosing `mktmpdir` and the `chmod` are a workaround + # for https://github.com/rails/rails/pull/34037. Dir.mktmpdir(".d", dirname) do |tmpdir| + begin + FileUtils.chmod "+t", dirname + rescue Errno::EPERM + :ignore + end File.atomic_write(self, tmpdir) do |file| file.write(content) end