lib/appmap/util.rb in appmap-0.43.0 vs lib/appmap/util.rb in appmap-0.44.0

- old
+ new

@@ -69,8 +69,24 @@ sanitize_paths(event) end event end + + # Atomically writes AppMap data to +filename+. + def write_appmap(filename, appmap) + require 'fileutils' + require 'tmpdir' + + # This is what Ruby Tempfile does; but we don't want the file to be unlinked. + mode = File::RDWR | File::CREAT | File::EXCL + ::Dir::Tmpname.create([ 'appmap_', '.json' ]) do |tmpname| + tempfile = File.open(tmpname, mode) + tempfile.write(appmap) + tempfile.close + # Atomically move the tempfile into place. + FileUtils.mv tempfile.path, filename + end + end end end end