Skip to content

Commit 859023a

Browse files
committed
update helper to active_support 4.2.4
1 parent 63a429e commit 859023a

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

lib/filewatch/helper.rb

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# code downloaded from Ruby on Rails 4.2.1
2-
# https://raw.githubusercontent.com/rails/rails/v4.2.1/activesupport/lib/active_support/core_ext/file/atomic.rb
1+
# code downloaded from Ruby on Rails 4.2.4
2+
# https://raw.githubusercontent.com/rails/rails/v4.2.4/activesupport/lib/active_support/core_ext/file/atomic.rb
33
require 'fileutils'
44

55
class File
@@ -16,7 +16,14 @@ class File
1616
# File.atomic_write('/data/something.important', '/data/tmp') do |file|
1717
# file.write('hello')
1818
# end
19-
def self.atomic_write(file_name)
19+
def self.atomic_write(file_name, temp_dir = Dir.tmpdir)
20+
require 'tempfile' unless defined?(Tempfile)
21+
require 'fileutils' unless defined?(FileUtils)
22+
23+
temp_file = Tempfile.new(basename(file_name), temp_dir)
24+
temp_file.binmode
25+
yield temp_file
26+
temp_file.close
2027

2128
if File.exist?(file_name)
2229
# Get original file permissions
@@ -27,42 +34,35 @@ def self.atomic_write(file_name)
2734
old_stat = probe_stat_in(dirname(file_name))
2835
end
2936

30-
mode = old_stat ? old_stat.mode : nil
31-
32-
# Create temporary file with identical permissions
33-
temp_file = File.new(rand_filename(file_name), "w", mode)
34-
temp_file.binmode
35-
return_val = yield temp_file
36-
temp_file.close
37-
3837
# Overwrite original file with temp file
39-
File.rename(temp_file.path, file_name)
38+
FileUtils.mv(temp_file.path, file_name)
4039

41-
# Unable to get permissions of the original file => return
42-
return return_val if old_stat.nil?
43-
44-
# Set correct uid/gid on new file
45-
chown(old_stat.uid, old_stat.gid, file_name) if old_stat
46-
47-
return_val
40+
# Set correct permissions on new file
41+
begin
42+
chown(old_stat.uid, old_stat.gid, file_name)
43+
# This operation will affect filesystem ACL's
44+
chmod(old_stat.mode, file_name)
45+
rescue Errno::EPERM, Errno::EACCES
46+
# Changing file ownership failed, moving on.
47+
end
4848
end
4949

5050
# Private utility method.
5151
def self.probe_stat_in(dir) #:nodoc:
52-
basename = rand_filename(".permissions_check")
52+
basename = [
53+
'.permissions_check',
54+
Thread.current.object_id,
55+
Process.pid,
56+
rand(1000000)
57+
].join('.')
58+
5359
file_name = join(dir, basename)
5460
FileUtils.touch(file_name)
5561
stat(file_name)
56-
rescue
57-
# ...
5862
ensure
59-
FileUtils.rm_f(file_name) if File.exist?(file_name)
63+
FileUtils.rm_f(file_name) if file_name
6064
end
61-
62-
def self.rand_filename(prefix)
63-
[ prefix, Thread.current.object_id, Process.pid, rand(1000000) ].join('.')
64-
end
65-
65+
6666
def self.device?(file_name)
6767
chardev?(file_name) || blockdev?(file_name)
6868
end

0 commit comments

Comments
 (0)