instiki/vendor/rails/activesupport/lib/active_support/core_ext/file.rb
Jacques Distler 5292899c9a Rails 2.1 RC1
Updated Instiki to Rails 2.1 RC1 (aka 2.0.991).
2008-05-17 23:22:34 -05:00

21 lines
688 B
Ruby

require 'tempfile'
# Write to a file atomically. Useful for situations where you don't
# want other processes or threads to see half-written files.
#
# File.atomic_write("important.file") do |file|
# file.write("hello")
# end
#
# If your temp directory is not on the same filesystem as the file you're
# trying to write, you can provide a different temporary directory.
#
# File.atomic_write("/data/something.important", "/data/tmp") do |f|
# file.write("hello")
# end
def File.atomic_write(file_name, temp_dir = Dir.tmpdir)
temp_file = Tempfile.new(File.basename(file_name), temp_dir)
yield temp_file
temp_file.close
File.rename(temp_file.path, file_name)
end