From d5adf87cf44c18081edb895996242faa6e340634 Mon Sep 17 00:00:00 2001 From: Mike McQuaid Date: Mon, 28 Jan 2019 16:08:23 +0000 Subject: [PATCH] cleanup: better handle edge cases. - don't complain about "skipping" kegs when `brew cleanup` runs automatically - better handle orphaned directories - output a better error message when a keg cannot be removed Fixes #4989 Fixes #5627 Fixes https://github.com/Homebrew/homebrew-core/pull/35958#issuecomment-458031634 --- Library/Homebrew/cleanup.rb | 13 ++++++++----- Library/Homebrew/formula.rb | 10 ++++++---- Library/Homebrew/keg.rb | 2 ++ 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/Library/Homebrew/cleanup.rb b/Library/Homebrew/cleanup.rb index 0b13099781..386d2e5da7 100644 --- a/Library/Homebrew/cleanup.rb +++ b/Library/Homebrew/cleanup.rb @@ -178,13 +178,13 @@ module Homebrew return false unless periodic_clean_due? ohai "`brew cleanup` has not been run in #{CLEANUP_DEFAULT_DAYS} days, running now..." - clean! + clean!(quiet: true) end - def clean! + def clean!(quiet: false) if args.empty? Formula.installed.sort_by(&:name).each do |formula| - cleanup_formula(formula) + cleanup_formula(formula, quiet: quiet) end cleanup_cache cleanup_logs @@ -221,8 +221,9 @@ module Homebrew @unremovable_kegs ||= [] end - def cleanup_formula(formula) - formula.eligible_kegs_for_cleanup.each(&method(:cleanup_keg)) + def cleanup_formula(formula, quiet: false) + formula.eligible_kegs_for_cleanup(quiet: quiet) + .each(&method(:cleanup_keg)) cleanup_cache(Pathname.glob(cache/"#{formula.name}--*")) rm_ds_store([formula.rack]) cleanup_lockfiles(FormulaLock.new(formula.name).path) @@ -275,6 +276,8 @@ module Homebrew # Skip incomplete downloads which are still in progress. next end + elsif download.directory? + FileUtils.rm_rf download else download.unlink end diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index 106154944c..4cf7b26eae 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -1924,7 +1924,7 @@ class Formula end # @private - def eligible_kegs_for_cleanup + def eligible_kegs_for_cleanup(quiet: false) eligible_for_cleanup = [] if installed? eligible_kegs = if head? && (head_prefix = latest_head_prefix) @@ -1945,9 +1945,9 @@ class Formula unless eligible_kegs.empty? eligible_kegs.each do |keg| if keg.linked? - opoo "Skipping (old) #{keg} due to it being linked" + opoo "Skipping (old) #{keg} due to it being linked" unless quiet elsif pinned? && keg == Keg.new(@pin.path.resolved_path) - opoo "Skipping (old) #{keg} due to it being pinned" + opoo "Skipping (old) #{keg} due to it being pinned" unless quiet else eligible_for_cleanup << keg end @@ -1957,7 +1957,9 @@ class Formula # If the cellar only has one version installed, don't complain # that we can't tell which one to keep. Don't complain at all if the # only installed version is a pinned formula. - opoo "Skipping #{full_name}: most recent version #{pkg_version} not installed" + unless quiet + opoo "Skipping #{full_name}: most recent version #{pkg_version} not installed" + end end eligible_for_cleanup end diff --git a/Library/Homebrew/keg.rb b/Library/Homebrew/keg.rb index 63098cc774..aa62e8594f 100644 --- a/Library/Homebrew/keg.rb +++ b/Library/Homebrew/keg.rb @@ -310,6 +310,8 @@ class Keg remove_opt_record if optlinked? remove_old_aliases remove_oldname_opt_record + rescue Errno::ENOTEMPTY + ofail "Could not remove #{path}! Check its permissions." end def unlink(mode = OpenStruct.new)