Skip to content

Commit

Permalink
Refactor the code which manages temp files.
Browse files Browse the repository at this point in the history
Combine the creation of the temporary filename with the writing to it.
  • Loading branch information
hainesr committed Sep 5, 2016
1 parent b005c48 commit 63ed0d9
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions lib/zip/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -402,23 +402,19 @@ def check_file(path)
end

def on_success_replace
tmp_filename = create_tmpname
if yield tmp_filename
::File.rename(tmp_filename, name)
::File.chmod(@file_permissions, name) if @create.nil?
end
ensure
::File.unlink(tmp_filename) if ::File.exist?(tmp_filename)
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
::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?
end
ensure
::File.unlink(tmp_filename) if ::File.exist?(tmp_filename)
end
end
end

end
end

Expand Down

0 comments on commit 63ed0d9

Please sign in to comment.