# File lib/sass/util.rb, line 888 def atomic_create_and_write_file(filename, perms = 0666) require 'tempfile' tmpfile = Tempfile.new(File.basename(filename), File.dirname(filename)) tmpfile.binmode if tmpfile.respond_to?(:binmode) result = yield tmpfile tmpfile.flush # ensure all writes are flushed to the OS begin tmpfile.fsync # ensure all buffered data in the OS is sync'd to disk. rescue NotImplementedError # Not all OSes support fsync end tmpfile.close # Windows cannot rename an open file. # Make file readable and writeable to all but respect umask (usually 022). File.chmod(perms & ~File.umask, tmpfile.path) ATOMIC_WRITE_MUTEX.synchronize do File.rename tmpfile.path, filename end result ensure # close and remove the tempfile if it still exists, # presumably due to an error during write tmpfile.close if tmpfile tmpfile.unlink if tmpfile end