Merge pull request #5009 from reitermarkus/atomic_write

Fix `atomic_write` inside non-sticky directory.
This commit is contained in:
Markus Reiter 2018-10-02 16:56:27 +02:00 committed by GitHub
commit 72f2bb2643
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -160,9 +160,16 @@ class Pathname
open("a", *open_args) { |f| f.puts(content) } open("a", *open_args) { |f| f.puts(content) }
end end
# NOTE always overwrites # NOTE: This always overwrites.
def atomic_write(content) 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| Dir.mktmpdir(".d", dirname) do |tmpdir|
begin
FileUtils.chmod "+t", dirname
rescue Errno::EPERM
:ignore
end
File.atomic_write(self, tmpdir) do |file| File.atomic_write(self, tmpdir) do |file|
file.write(content) file.write(content)
end end