From e85e57b217939a47ba8d7f1adfec192b47d96ec0 Mon Sep 17 00:00:00 2001 From: Martin Afanasjew Date: Sat, 15 Aug 2015 15:19:36 +0200 Subject: [PATCH] cleanup: make --force less aggressive Change behavior for `brew cleanup` as follows: - If `--force` is supplied, remove only outdated keg-only packages. - If `--prune=` is supplied, remove both logs and cached downloads older than the specified number of days. Use `--prune=all` to remove all logs and cached downloads irrespective of age. - By default, remove logs after 14 days and cached downloads never. Also centralizes handling of `--prune`, thus removing duplicate logic. This is motivated by commit 17eee232838d4639b25f863aa342b1dda61b81bc that made `--force` much more aggressive and made it override whatever was specified via `--prune`, completely removing all: - outdated keg-only packages - cached downloads irrespective of age - logs irrespective of age This made it impossible to remove outdated keg-only packages without also deleting all cached downloads, which is at least inconvenient for people with limited bandwidth wanting to rebuild packages later. Closes Homebrew/homebrew#42970. Signed-off-by: Mike McQuaid --- Library/Homebrew/cmd/cleanup.rb | 35 +++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/Library/Homebrew/cmd/cleanup.rb b/Library/Homebrew/cmd/cleanup.rb index 8d53546737..955a6e3da0 100644 --- a/Library/Homebrew/cmd/cleanup.rb +++ b/Library/Homebrew/cmd/cleanup.rb @@ -19,14 +19,8 @@ module Homebrew def cleanup_logs return unless HOMEBREW_LOGS.directory? - prune = ARGV.value "prune" - if prune - time = Time.now - 60 * 60 * 24 * prune.to_i - else - time = Time.now - 60 * 60 * 24 * 7 * 2 # two weeks - end HOMEBREW_LOGS.subdirs.each do |dir| - cleanup_path(dir) { dir.rmtree } if ARGV.force? || (dir.mtime < time) + cleanup_path(dir) { dir.rmtree } if prune?(:logs, dir.mtime) end end @@ -61,10 +55,8 @@ module Homebrew def cleanup_cache return unless HOMEBREW_CACHE.directory? - prune = ARGV.value "prune" - time = Time.now - 60 * 60 * 24 * prune.to_i HOMEBREW_CACHE.children.each do |path| - if ARGV.force? || (prune && path.mtime < time) + if prune?(:cache, path.mtime) if path.file? cleanup_path(path) { path.unlink } elsif path.directory? && path.to_s.include?("--") @@ -129,6 +121,29 @@ module Homebrew quiet_system "find", *args end + def prune?(kind, time) + unless @cleanup_prune_limit + # Infer and cache prune limits for cleanup: Use '--prune' or fall back to + # defaults (never prune download cache and prune logs after two weeks). + days = ARGV.value "prune" + offset = Time.now + @cleanup_prune_limit = {} + { :cache => nil, :logs => 14 }.each do |k, days_default| + @cleanup_prune_limit[k] = if days == "all" + "all" + elsif days + offset - 60 * 60 * 24 * days.to_i + elsif days_default + offset - 60 * 60 * 24 * days_default + end + end + end + + if limit = @cleanup_prune_limit[kind] + limit == "all" || time < limit + end + end + def eligible_for_cleanup?(formula) # It used to be the case that keg-only kegs could not be cleaned up, because # older brews were built against the full path to the keg-only keg. Then we