diff options
author | Xavier Noria <fxn@hashref.com> | 2012-08-22 23:10:33 +0200 |
---|---|---|
committer | Xavier Noria <fxn@hashref.com> | 2012-08-22 23:10:33 +0200 |
commit | 8b16ff64f2ad4debd546314c3cd42b1f4bdbc9e8 (patch) | |
tree | 2fe99448f4a6421e8e317856a9b48b9b4356eb7d /activesupport/lib | |
parent | 79e880f7f4f90c5f31e1f2387165322df453f0b6 (diff) | |
download | rails-8b16ff64f2ad4debd546314c3cd42b1f4bdbc9e8.tar.gz rails-8b16ff64f2ad4debd546314c3cd42b1f4bdbc9e8.tar.bz2 rails-8b16ff64f2ad4debd546314c3cd42b1f4bdbc9e8.zip |
revises a brittle test in Active Support
The revised test assumed that the default permissions of a file
matched the umask of the process, but in the general case that
depends also on the file system. This test was failing in the
/vagrant shared folder of Rails development boxes.
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support/core_ext/file/atomic.rb | 32 |
1 files changed, 21 insertions, 11 deletions
diff --git a/activesupport/lib/active_support/core_ext/file/atomic.rb b/activesupport/lib/active_support/core_ext/file/atomic.rb index 9e504851e7..81beb4e85d 100644 --- a/activesupport/lib/active_support/core_ext/file/atomic.rb +++ b/activesupport/lib/active_support/core_ext/file/atomic.rb @@ -1,3 +1,5 @@ +require 'fileutils' + class File # Write to a file atomically. Useful for situations where you don't # want other processes or threads to see half-written files. @@ -25,17 +27,9 @@ class File # Get original file permissions old_stat = stat(file_name) rescue Errno::ENOENT - # No old permissions, write a temp file to determine the defaults - temp_file_name = [ - '.permissions_check', - Thread.current.object_id, - Process.pid, - rand(1000000) - ].join('.') - check_name = join(dirname(file_name), temp_file_name) - open(check_name, 'w') { } - old_stat = stat(check_name) - unlink(check_name) + # If not possible, probe which are the default permissions in the + # destination directory. + old_stat = probe_stat_in(dirname(file_name)) end # Overwrite original file with temp file @@ -45,4 +39,20 @@ class File chown(old_stat.uid, old_stat.gid, file_name) chmod(old_stat.mode, file_name) end + + # Private utility method. + def self.probe_stat_in(dir) #:nodoc: + basename = [ + '.permissions_check', + Thread.current.object_id, + Process.pid, + rand(1000000) + ].join('.') + + file_name = join(dir, basename) + FileUtils.touch(file_name) + stat(file_name) + ensure + FileUtils.rm_f(file_name) if file_name + end end |