1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #34037 from reitermarkus/atomic_write-permissions

`atomic_write`: Ensure correct permission when `tmpdir` is the same as `dirname`.
This commit is contained in:
Rafael França 2018-11-22 15:47:19 -05:00 committed by GitHub
commit 548bd50985
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 1 deletions

View file

@ -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))

View file

@ -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!"