diff --git a/Library/Homebrew/cleanup.rb b/Library/Homebrew/cleanup.rb index 8b4d52856c..d4cc1b9331 100644 --- a/Library/Homebrew/cleanup.rb +++ b/Library/Homebrew/cleanup.rb @@ -5,6 +5,30 @@ require "hbc/cask_loader" module CleanupRefinement LATEST_CASK_DAYS = 7 + refine Enumerator do + def parallel + queue = Queue.new + + each do |element| + queue.enq(element) + end + + workers = (0...Hardware::CPU.cores).map do + Thread.new do + Kernel.loop do + begin + yield queue.deq(true) + rescue ThreadError + break # if queue is empty + end + end + end + end + + workers.each(&:join) + end + end + refine Pathname do def incomplete? extname.end_with?(".incomplete") @@ -149,10 +173,14 @@ module Homebrew def cleanup_formula(formula) formula.eligible_kegs_for_cleanup.each(&method(:cleanup_keg)) cleanup_cache(Pathname.glob(cache/"#{formula.name}--*")) + rm_ds_store([formula.rack]) + cleanup_lockfiles(FormulaLock.new(formula.name).path) end def cleanup_cask(cask) cleanup_cache(Pathname.glob(cache/"Cask/#{cask.token}--*")) + rm_ds_store([cask.caskroom_path]) + cleanup_lockfiles(CaskLock.new(cask.token).path) end def cleanup_keg(keg) @@ -204,32 +232,27 @@ module Homebrew @disk_cleanup_size += disk_usage end - def cleanup_lockfiles + def cleanup_lockfiles(*lockfiles) return unless HOMEBREW_LOCK_DIR.directory? - candidates = HOMEBREW_LOCK_DIR.children - lockfiles = candidates.select(&:file?) + + if lockfiles.empty? + lockfiles = HOMEBREW_LOCK_DIR.children.select(&:file?) + end + lockfiles.each do |file| next unless file.readable? - file.open(File::RDWR).flock(File::LOCK_EX | File::LOCK_NB) && file.unlink + next unless file.open(File::RDWR).flock(File::LOCK_EX | File::LOCK_NB) + cleanup_path(file) { file.unlink } end end - def rm_ds_store - paths = Queue.new - %w[Cellar Frameworks Library bin etc include lib opt sbin share var] - .map { |p| HOMEBREW_PREFIX/p }.each { |p| paths << p if p.exist? } - workers = (0...Hardware::CPU.cores).map do - Thread.new do - Kernel.loop do - begin - quiet_system "find", paths.deq(true), "-name", ".DS_Store", "-delete" - rescue ThreadError - break # if queue is empty - end - end - end + def rm_ds_store(dirs = nil) + dirs ||= %w[Caskroom Cellar Frameworks Library bin etc include lib opt sbin share var] + .map { |path| HOMEBREW_PREFIX/path } + + dirs.select(&:directory?).each.parallel do |dir| + system_command "find", args: [dir, "-name", ".DS_Store", "-delete"], print_stderr: false end - workers.map(&:join) end end end diff --git a/Library/Homebrew/lock_file.rb b/Library/Homebrew/lock_file.rb index ffd29f8172..e18b6e1119 100644 --- a/Library/Homebrew/lock_file.rb +++ b/Library/Homebrew/lock_file.rb @@ -1,6 +1,8 @@ require "fcntl" class LockFile + attr_reader :path + def initialize(name) @name = name.to_s @path = HOMEBREW_LOCK_DIR/"#{@name}.lock"