Merge pull request #6012 from amyspark/hotpatch-fix-atomic_write
atomic_write: repair permissions after writing
This commit is contained in:
commit
356f72dcc6
@ -163,9 +163,31 @@ class Pathname
|
||||
|
||||
# NOTE: This always overwrites.
|
||||
def atomic_write(content)
|
||||
old_stat = stat if exist?
|
||||
File.atomic_write(self) do |file|
|
||||
file.write(content)
|
||||
end
|
||||
|
||||
return unless old_stat
|
||||
|
||||
# Try to restore original file's permissions separately
|
||||
# atomic_write does it itself, but it actually erases
|
||||
# them if chown fails
|
||||
begin
|
||||
# Set correct permissions on new file
|
||||
chown(old_stat.uid, nil)
|
||||
chown(nil, old_stat.gid)
|
||||
rescue Errno::EPERM, Errno::EACCES
|
||||
# Changing file ownership failed, moving on.
|
||||
nil
|
||||
end
|
||||
begin
|
||||
# This operation will affect filesystem ACL's
|
||||
chmod(old_stat.mode)
|
||||
rescue Errno::EPERM, Errno::EACCES
|
||||
# Changing file permissions failed, moving on.
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
# @private
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user