pathname: atomic_write shouldn't make dir sticky unless world writable

This commit is contained in:
Chongyu Zhu 2018-10-21 08:58:16 +08:00
parent c56c3907e5
commit 98016b0ac6
No known key found for this signature in database
GPG Key ID: 1A43E3C9100B38F5

View File

@ -165,14 +165,15 @@ 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)
end
ensure
FileUtils.chmod "-t", dirname if should_fix_sticky_bit
end
end
end