Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

atomic_write: repair permissions after writing #6012

Merged
merged 2 commits into from Apr 14, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions Library/Homebrew/extend/pathname.rb
Expand Up @@ -163,9 +163,31 @@ def append_lines(content, *open_args)

# 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.
amyspark marked this conversation as resolved.
Show resolved Hide resolved
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.
amyspark marked this conversation as resolved.
Show resolved Hide resolved
nil
end
end

# @private
Expand Down