aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/extend/pathname.rb
diff options
context:
space:
mode:
authorJack Nagel2014-03-22 10:20:39 -0500
committerJack Nagel2014-03-22 10:58:28 -0500
commit9c8e2572dc4ca041cf36743e4d563c7c5a3a871a (patch)
treeeb3a263f8f3d7435c78015ddf57ce8773cbe45d0 /Library/Homebrew/extend/pathname.rb
parent30e2dcfabac4a4769c831eaa0785251efa8f83d0 (diff)
downloadhomebrew-9c8e2572dc4ca041cf36743e4d563c7c5a3a871a.tar.bz2
Preserve permissions when using Pathname#atomic_write
Diffstat (limited to 'Library/Homebrew/extend/pathname.rb')
-rw-r--r--Library/Homebrew/extend/pathname.rb28
1 files changed, 25 insertions, 3 deletions
diff --git a/Library/Homebrew/extend/pathname.rb b/Library/Homebrew/extend/pathname.rb
index 92b51f225..d37122951 100644
--- a/Library/Homebrew/extend/pathname.rb
+++ b/Library/Homebrew/extend/pathname.rb
@@ -96,12 +96,34 @@ class Pathname
# NOTE always overwrites
def atomic_write content
- require 'tempfile'
- tf = Tempfile.new(self.basename.to_s)
+ require "tempfile"
+ tf = Tempfile.new(basename.to_s)
tf.write(content)
tf.close
- FileUtils.mv tf.path, self.to_s
+
+ begin
+ old_stat = stat
+ rescue Errno::ENOENT
+ old_stat = default_stat
+ end
+
+ FileUtils.mv tf.path, self
+
+ begin
+ chown(old_stat.uid, old_stat.gid)
+ chmod(old_stat.mode)
+ rescue Errno::EPERM
+ end
+ end
+
+ def default_stat
+ sentinel = parent.join(".brew.#{Process.pid}.#{rand(Time.now.to_i)}")
+ sentinel.open("w") { }
+ sentinel.stat
+ ensure
+ sentinel.unlink
end
+ private :default_stat
def cp dst
if file?