aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorRafael França <rafaelmfranca@gmail.com>2018-11-22 15:47:19 -0500
committerGitHub <noreply@github.com>2018-11-22 15:47:19 -0500
commit548bd509857ca5bba300915aa5ad9bff6e8c0238 (patch)
tree0411fe305a705172df1d2b19044d30a0a301f5e2 /activesupport
parent1b68ead8f8cb0d7a1771ab82ab20bd79857b40ee (diff)
parentc08272ac2469e6982f81d4e61cecae46f89fed7b (diff)
downloadrails-548bd509857ca5bba300915aa5ad9bff6e8c0238.tar.gz
rails-548bd509857ca5bba300915aa5ad9bff6e8c0238.tar.bz2
rails-548bd509857ca5bba300915aa5ad9bff6e8c0238.zip
Merge pull request #34037 from reitermarkus/atomic_write-permissions
`atomic_write`: Ensure correct permission when `tmpdir` is the same as `dirname`.
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/core_ext/file/atomic.rb2
-rw-r--r--activesupport/test/core_ext/file_test.rb14
2 files changed, 15 insertions, 1 deletions
diff --git a/activesupport/lib/active_support/core_ext/file/atomic.rb b/activesupport/lib/active_support/core_ext/file/atomic.rb
index 8e288833b6..9deceb1bb4 100644
--- a/activesupport/lib/active_support/core_ext/file/atomic.rb
+++ b/activesupport/lib/active_support/core_ext/file/atomic.rb
@@ -29,7 +29,7 @@ class File
old_stat = if exist?(file_name)
# Get original file permissions
stat(file_name)
- elsif temp_dir != dirname(file_name)
+ else
# If not possible, probe which are the default permissions in the
# destination directory.
probe_stat_in(dirname(file_name))
diff --git a/activesupport/test/core_ext/file_test.rb b/activesupport/test/core_ext/file_test.rb
index 9c97700e5d..186c863f91 100644
--- a/activesupport/test/core_ext/file_test.rb
+++ b/activesupport/test/core_ext/file_test.rb
@@ -59,6 +59,20 @@ class AtomicWriteTest < ActiveSupport::TestCase
File.unlink(file_name) rescue nil
end
+ def test_atomic_write_preserves_file_permissions_same_directory
+ Dir.mktmpdir do |temp_dir|
+ File.chmod 0700, temp_dir
+
+ probed_permissions = File.probe_stat_in(temp_dir).mode.to_s(8)
+
+ File.atomic_write(File.join(temp_dir, file_name), &:close)
+
+ actual_permissions = File.stat(File.join(temp_dir, file_name)).mode.to_s(8)
+
+ assert_equal actual_permissions, probed_permissions
+ end
+ end
+
def test_atomic_write_returns_result_from_yielded_block
block_return_value = File.atomic_write(file_name, Dir.pwd) do |file|
"Hello world!"