From 98016b0ac61c3e078a4df34f1ec5d2792958dcd4 Mon Sep 17 00:00:00 2001 From: Chongyu Zhu Date: Sun, 21 Oct 2018 08:58:16 +0800 Subject: [PATCH] pathname: `atomic_write` shouldn't make dir sticky unless world writable --- Library/Homebrew/extend/pathname.rb | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Library/Homebrew/extend/pathname.rb b/Library/Homebrew/extend/pathname.rb index 1f67eab52b..513e112908 100644 --- a/Library/Homebrew/extend/pathname.rb +++ b/Library/Homebrew/extend/pathname.rb @@ -165,13 +165,14 @@ class Pathname # The enclosing `mktmpdir` and the `chmod` are a workaround # for https://github.com/rails/rails/pull/34037. Dir.mktmpdir(".d", dirname) do |tmpdir| + should_fix_sticky_bit = dirname.world_writable? && !dirname.sticky? + FileUtils.chmod "+t", dirname if should_fix_sticky_bit begin - FileUtils.chmod "+t", dirname - rescue Errno::EPERM - :ignore - end - File.atomic_write(self, tmpdir) do |file| - file.write(content) + File.atomic_write(self, tmpdir) do |file| + file.write(content) + end + ensure + FileUtils.chmod "-t", dirname if should_fix_sticky_bit end end end