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

Create temporary file exclusively and clean

This commit is contained in:
Nobuyoshi Nakada 2022-08-06 20:56:23 +09:00
parent 03f86565a6
commit e5e6b87e26
No known key found for this signature in database
GPG key ID: 7CD2805BFA3770C6

View file

@ -66,12 +66,17 @@ end
def atomic_write_open(filename)
filename_new = filename + ".new.#$$"
open(filename_new, "wb") do |f|
clean = false
File.open(filename_new, "wbx") do |f|
clean = true
yield f
end
if File.binread(filename_new) != (File.binread(filename) rescue nil)
File.rename(filename_new, filename)
else
clean = false
end
ensure
if clean
File.unlink(filename_new)
end
end