Make CurlDownloadStrategy resume aborted downloads

* `CurlDownloadStrategy#_fetch` (and the same methods in its
  subclasses) now fetches the file to a temporary path, and
  `CurlDownloadStrategy#fetch` moves it to the correct location.
* `Homebrew#cleanup` cleans the temporary files `CurlDownloadStrategy`
  creates if they're left in the cache.

Closes Homebrew/homebrew#13953.

Signed-off-by: Mike McQuaid <mike@mikemcquaid.com>
This commit is contained in:
Simon Sigurdhsson 2012-08-04 18:50:17 +02:00 committed by Mike McQuaid
parent c0822f8400
commit af3bfab9db
2 changed files with 15 additions and 6 deletions

View File

@ -77,6 +77,10 @@ module Homebrew extend self
end
end
end
if pn.basename.to_s.split('.').last == 'incomplete'
puts "Removing #{pn}..."
rm pn unless ARGV.dry_run?
end
end
end

View File

@ -44,15 +44,20 @@ class CurlDownloadStrategy < AbstractDownloadStrategy
else
@tarball_path=HOMEBREW_CACHE+File.basename(@url)
end
@temporary_path=Pathname.new(@tarball_path.to_s + ".incomplete")
end
def cached_location
@tarball_path
end
def downloaded_size
@temporary_path.size? or 0
end
# Private method, can be overridden if needed.
def _fetch
curl @url, '-o', @tarball_path
curl @url, '-C', downloaded_size, '-o', @temporary_path
end
def fetch
@ -66,13 +71,13 @@ class CurlDownloadStrategy < AbstractDownloadStrategy
begin
_fetch
rescue Exception => e
ignore_interrupts { @tarball_path.unlink if @tarball_path.exist? }
if e.kind_of? ErrorDuringExecution
raise CurlDownloadStrategyError, "Download failed: #{@url}"
else
raise
end
end
ignore_interrupts { @temporary_path.rename(@tarball_path) }
else
puts "Already downloaded: #{@tarball_path}"
end
@ -154,8 +159,8 @@ class CurlApacheMirrorDownloadStrategy < CurlDownloadStrategy
mirrors = MultiJson.decode(open("#{@url}&asjson=1").read)
url = mirrors.fetch('preferred') + mirrors.fetch('path_info')
ohai "Best Mirror #{url}"
curl url, '-o', @tarball_path
ohai "Best Mirror #{mirror_url}"
curl url, '-C', downloaded_size, '-o', @temporary_path
rescue IndexError
raise "Couldn't determine mirror. Try again later."
end
@ -166,7 +171,7 @@ end
class CurlPostDownloadStrategy < CurlDownloadStrategy
def _fetch
base_url,data = @url.split('?')
curl base_url, '-d', data, '-o', @tarball_path
curl base_url, '-d', data, '-C', downloaded_size, '-o', @temporary_path
end
end
@ -191,7 +196,7 @@ end
# Try not to need this, as we probably won't accept the formula.
class CurlUnsafeDownloadStrategy < CurlDownloadStrategy
def _fetch
curl @url, '--insecure', '-o', @tarball_path
curl @url, '--insecure', '-C', downloaded_size, '-o', @temporary_path
end
end