Fix cleanup of incomplete downloads.

This commit is contained in:
Markus Reiter 2018-10-14 00:13:04 +02:00
parent 9b84b2404a
commit 1f24c6600c
2 changed files with 22 additions and 4 deletions

View File

@ -152,13 +152,13 @@ module Homebrew
def clean! def clean!
if args.empty? if args.empty?
cleanup_lockfiles
Formula.installed.sort_by(&:name).each do |formula| Formula.installed.sort_by(&:name).each do |formula|
cleanup_formula(formula) cleanup_formula(formula)
end end
cleanup_cache cleanup_cache
cleanup_logs cleanup_logs
cleanup_portable_ruby cleanup_portable_ruby
cleanup_lockfiles
return if dry_run? return if dry_run?
cleanup_old_cache_db cleanup_old_cache_db
@ -221,14 +221,27 @@ module Homebrew
return if dry_run? return if dry_run?
return unless (cache/"downloads").directory? return unless (cache/"downloads").directory?
# We can't use `.reject(&:incomplete?) here due to the refinement scope. downloads = (cache/"downloads").children
downloads = (cache/"downloads").children.reject { |path| path.incomplete? } # rubocop:disable Style/SymbolProc
referenced_downloads = [cache, cache/"Cask"].select(&:directory?) referenced_downloads = [cache, cache/"Cask"].select(&:directory?)
.flat_map(&:children) .flat_map(&:children)
.select(&:symlink?) .select(&:symlink?)
.map(&:resolved_path) .map(&:resolved_path)
(downloads - referenced_downloads).each(&:unlink) (downloads - referenced_downloads).each do |download|
if download.incomplete?
begin
LockFile.new(download.basename).with_lock do
download.unlink
end
rescue OperationInProgressError
# Skip incomplete downloads which are still in progress.
next
end
else
download.unlink
end
end
end end
def cleanup_cache(entries = nil) def cleanup_cache(entries = nil)

View File

@ -277,6 +277,9 @@ class CurlDownloadStrategy < AbstractFileDownloadStrategy
end end
def fetch def fetch
download_lock = LockFile.new(temporary_path.basename)
download_lock.lock
urls = [url, *mirrors] urls = [url, *mirrors]
begin begin
@ -308,6 +311,8 @@ class CurlDownloadStrategy < AbstractFileDownloadStrategy
puts "Trying a mirror..." puts "Trying a mirror..."
retry retry
end end
ensure
download_lock.unlock
end end
def clear_cache def clear_cache