3 module ActiveSupport
#:nodoc:
4 module CoreExtensions
#:nodoc:
7 # Write to a file atomically. Useful for situations where you don't
8 # want other processes or threads to see half-written files.
10 # File.atomic_write("important.file") do |file|
14 # If your temp directory is not on the same filesystem as the file you're
15 # trying to write, you can provide a different temporary directory.
17 # File.atomic_write("/data/something.important", "/data/tmp") do |f|
20 def atomic_write(file_name
, temp_dir
= Dir
.tmpdir
)
21 temp_file
= Tempfile
.new(basename(file_name
), temp_dir
)
26 # Get original file permissions
27 old_stat
= stat(file_name
)
29 # No old permissions, write a temp file to determine the defaults
30 check_name
= ".permissions_check.#{Thread.current.object_id}.#{Process.pid}.#{rand(1000000)}"
31 open(check_name
, "w") { }
32 old_stat
= stat(check_name
)
36 # Overwrite original file with temp file
37 rename(temp_file
.path
, file_name
)
39 # Set correct permissions on new file
40 chown(old_stat
.uid
, old_stat
.gid
, file_name
)
41 chmod(old_stat
.mode
, file_name
)