From 79f3ff0a4e5ecf97f6c9fb093e549d04755a09f6 Mon Sep 17 00:00:00 2001 From: Mike McQuaid Date: Mon, 21 Jan 2019 21:35:24 +0000 Subject: [PATCH] cleanup: also consider ctime on prunes. If this is an old tarball then we may end up removing it otherwise if we've respected the `mtime` from the server on download. --- Library/Homebrew/cleanup.rb | 7 +++++-- Library/Homebrew/test/cleanup_spec.rb | 11 ++++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/Library/Homebrew/cleanup.rb b/Library/Homebrew/cleanup.rb index 8851b30855..e11dc893ec 100644 --- a/Library/Homebrew/cleanup.rb +++ b/Library/Homebrew/cleanup.rb @@ -52,7 +52,7 @@ module CleanupRefinement return true if symlink? && !exist? - mtime < days.days.ago + mtime < days.days.ago && ctime < days.days.ago end def stale?(scrub = false) @@ -124,7 +124,10 @@ module CleanupRefinement return true if scrub && !cask.versions.include?(cask.version) - return mtime < CLEANUP_DEFAULT_DAYS.days.ago if cask.version.latest? + if cask.version.latest? + return mtime < CLEANUP_DEFAULT_DAYS.days.ago && + ctime < CLEANUP_DEFAULT_DAYS.days.ago + end false end diff --git a/Library/Homebrew/test/cleanup_spec.rb b/Library/Homebrew/test/cleanup_spec.rb index 36b8eafd4c..9b3df12c8c 100644 --- a/Library/Homebrew/test/cleanup_spec.rb +++ b/Library/Homebrew/test/cleanup_spec.rb @@ -15,12 +15,13 @@ describe CleanupRefinement do path.mkpath end - it "returns true when path_modified_time < days_default" do + it "returns true when ctime and mtime < days_default" do + allow_any_instance_of(Pathname).to receive(:ctime).and_return(2.days.ago) allow_any_instance_of(Pathname).to receive(:mtime).and_return(2.days.ago) expect(path.prune?(1)).to be true end - it "returns false when path_modified_time >= days_default" do + it "returns false when ctime and mtime >= days_default" do expect(path.prune?(2)).to be false end end @@ -181,7 +182,8 @@ describe Homebrew::Cleanup do it "removes the download for the latest version after 30 days" do download = Cask::Cache.path/"#{cask.token}--#{cask.version}" - FileUtils.touch download, mtime: 30.days.ago - 1.hour + allow(download).to receive(:ctime).and_return(30.days.ago - 1.hour) + allow(download).to receive(:mtime).and_return(30.days.ago - 1.hour) subject.cleanup_cask(cask) @@ -203,12 +205,14 @@ describe Homebrew::Cleanup do end it "cleans up logs if older than 30 days" do + allow_any_instance_of(Pathname).to receive(:ctime).and_return(31.days.ago) allow_any_instance_of(Pathname).to receive(:mtime).and_return(31.days.ago) subject.cleanup_logs expect(path).not_to exist end it "does not clean up logs less than 30 days old" do + allow_any_instance_of(Pathname).to receive(:ctime).and_return(15.days.ago) allow_any_instance_of(Pathname).to receive(:mtime).and_return(15.days.ago) subject.cleanup_logs expect(path).to exist @@ -307,6 +311,7 @@ describe Homebrew::Cleanup do it "cleans up VCS checkout directories with modified time < prune time" do foo = (HOMEBREW_CACHE/"--foo") foo.mkpath + allow_any_instance_of(Pathname).to receive(:ctime).and_return(Time.now - 2 * 60 * 60 * 24) allow_any_instance_of(Pathname).to receive(:mtime).and_return(Time.now - 2 * 60 * 60 * 24) described_class.new(days: 1).cleanup_cache expect(foo).not_to exist