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:
parent
03f86565a6
commit
e5e6b87e26
1 changed files with 7 additions and 2 deletions
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue