Refactor the code which manages temp files.

Combine the creation of the temporary filename with the writing to it.
This commit is contained in:
Robert Haines 2016-09-05 14:50:22 +01:00
parent b005c487b4
commit 63ed0d970b
1 changed files with 10 additions and 14 deletions

View File

@ -402,7 +402,9 @@ module Zip
end
def on_success_replace
tmp_filename = create_tmpname
dirname, basename = ::File.split(name)
::Dir::Tmpname.create(basename, dirname) do |tmp_filename|
begin
if yield tmp_filename
::File.rename(tmp_filename, name)
::File.chmod(@file_permissions, name) if @create.nil?
@ -410,15 +412,9 @@ module Zip
ensure
::File.unlink(tmp_filename) if ::File.exist?(tmp_filename)
end
end
end
def create_tmpname
dirname, basename = ::File.split(name)
::Dir::Tmpname.create(basename, dirname) do |tmpname|
opts = {mode: ::File::CREAT | ::File::WRONLY | ::File::EXCL}
f = File.open(tmpname, opts)
f.close
end
end
end
end