Skip to content

Commit

Permalink
Merge pull request rails#34037 from reitermarkus/atomic_write-permiss…
Browse files Browse the repository at this point in the history
…ions

`atomic_write`: Ensure correct permission when `tmpdir` is the same as `dirname`.
  • Loading branch information
rafaelfranca authored Nov 22, 2018
2 parents 1b68ead + c08272a commit 548bd50
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion activesupport/lib/active_support/core_ext/file/atomic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def self.atomic_write(file_name, temp_dir = dirname(file_name))
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))
Expand Down
14 changes: 14 additions & 0 deletions activesupport/test/core_ext/file_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ def test_atomic_write_preserves_default_file_permissions
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!"
Expand Down

0 comments on commit 548bd50

Please sign in to comment.