diff --git a/Library/Homebrew/cleanup.rb b/Library/Homebrew/cleanup.rb index 4aa7ff114c07ceb59eb72b6fc4c1023ec94a92cd..987020bc99f055ab9e832849b9592d7eeeeeeca5 100644 --- a/Library/Homebrew/cleanup.rb +++ b/Library/Homebrew/cleanup.rb @@ -43,9 +43,9 @@ module Homebrew end end - def self.cleanup_cache - return unless HOMEBREW_CACHE.directory? - HOMEBREW_CACHE.children.each do |path| + def self.cleanup_cache(cache=HOMEBREW_CACHE) + return unless cache.directory? + cache.children.each do |path| if path.to_s.end_with? ".incomplete" cleanup_path(path) { path.unlink } next diff --git a/Library/Homebrew/cmd/update-report.rb b/Library/Homebrew/cmd/update-report.rb index 9339a63d9ca0f9f0c4bd51d5641d4d7632526d8b..e11cd647b3f4d41151d1db471bd67b94444cb138 100644 --- a/Library/Homebrew/cmd/update-report.rb +++ b/Library/Homebrew/cmd/update-report.rb @@ -2,6 +2,7 @@ require "formula_versions" require "migrator" require "formulary" require "descriptions" +require "cleanup" module Homebrew def update_preinstall_header @@ -69,6 +70,8 @@ module Homebrew updated = true end + migrate_legacy_cache_if_necessary + if !updated if !ARGV.include?("--preinstall") && !ENV["HOMEBREW_UPDATE_FAILED"] puts "Already up-to-date." @@ -101,6 +104,52 @@ module Homebrew ENV["HOMEBREW_UPDATE_BEFORE_HOMEBREW_HOMEBREW_CORE"] = revision ENV["HOMEBREW_UPDATE_AFTER_HOMEBREW_HOMEBREW_CORE"] = revision end + + def migrate_legacy_cache_if_necessary + legacy_cache = Pathname.new "/Library/Caches/Homebrew" + return if HOMEBREW_CACHE.to_s == legacy_cache.to_s + return unless legacy_cache.directory? + return unless legacy_cache.readable_real? + + migration_attempted_file = legacy_cache/".migration_attempted" + return if migration_attempted_file.exist? + + return unless legacy_cache.writable_real? + FileUtils.touch migration_attempted_file + + # Cleanup to avoid copying files unnecessarily + ohai "Cleaning up #{legacy_cache}..." + Cleanup.cleanup_cache legacy_cache + + # This directory could have been compromised if it's world-writable/ + # a symlink/owned by another user so don't copy files in those cases. + return if legacy_cache.world_writable? + return if legacy_cache.symlink? + return if !legacy_cache.owned? && legacy_cache.lstat.uid != 0 + + ohai "Migrating #{legacy_cache} to #{HOMEBREW_CACHE}..." + HOMEBREW_CACHE.mkpath + legacy_cache.cd do + legacy_cache.entries.each do |f| + next if [".", "..", ".migration_attempted"].include? "#{f}" + begin + FileUtils.cp_r f, HOMEBREW_CACHE + rescue + @migration_failed ||= true + end + end + end + + if @migration_failed + opoo <<-EOS.undent + Failed to migrate #{legacy_cache} to + #{HOMEBREW_CACHE}. Please do so manually. + EOS + else + ohai "Deleting #{legacy_cache}..." + FileUtils.rm_rf legacy_cache + end + end end class Reporter diff --git a/Library/Homebrew/config.rb b/Library/Homebrew/config.rb index 4a6ad2f2cde21a193c52d1a98579384942050bd3..a7441059fec8218b2465dd4b3fc45fd4afd37ebd 100644 --- a/Library/Homebrew/config.rb +++ b/Library/Homebrew/config.rb @@ -1,27 +1,4 @@ -def cache - if ENV["HOMEBREW_CACHE"] - Pathname.new(ENV["HOMEBREW_CACHE"]).expand_path - else - # we do this for historic reasons, however the cache *should* be the same - # directory whichever user is used and whatever instance of brew is executed - home_cache = Pathname.new("~/Library/Caches/Homebrew").expand_path - if home_cache.directory? && home_cache.writable_real? - home_cache - else - Pathname.new("/Library/Caches/Homebrew").extend Module.new { - def mkpath - unless exist? - super - chmod 0775 - end - end - } - end - end -end - -HOMEBREW_CACHE = cache -undef cache +HOMEBREW_CACHE = Pathname.new(ENV["HOMEBREW_CACHE"] || "~/Library/Caches/Homebrew").expand_path # Where brews installed via URL are cached HOMEBREW_CACHE_FORMULA = HOMEBREW_CACHE+"Formula" diff --git a/Library/Homebrew/manpages/brew.1.md.erb b/Library/Homebrew/manpages/brew.1.md.erb index cc014df36535048519dfbb95512c619140d1847e..f6f5948437d4679a84180bd6a9fb829292888714 100644 --- a/Library/Homebrew/manpages/brew.1.md.erb +++ b/Library/Homebrew/manpages/brew.1.md.erb @@ -118,8 +118,7 @@ can take several different forms: * `HOMEBREW_CACHE`: If set, instructs Homebrew to use the given directory as the download cache. - *Default:* `~/Library/Caches/Homebrew` if it exists; otherwise, - `/Library/Caches/Homebrew`. + *Default:* `~/Library/Caches/Homebrew`. * `HOMEBREW_CURL_VERBOSE`: If set, Homebrew will pass `--verbose` when invoking `curl`(1). diff --git a/share/doc/homebrew/brew.1.html b/share/doc/homebrew/brew.1.html index 43cddb8d00567c6866678ffa67f2e329e29d5fba..cdec466026cd7dfae07b7aefbe3bd14626d488f7 100644 --- a/share/doc/homebrew/brew.1.html +++ b/share/doc/homebrew/brew.1.html @@ -523,8 +523,7 @@ Homebrew developers. Please do not file issues if you encounter errors when using this environment variable.</p></dd> <dt><code>HOMEBREW_CACHE</code></dt><dd><p>If set, instructs Homebrew to use the given directory as the download cache.</p> -<p><em>Default:</em> <code>~/Library/Caches/Homebrew</code> if it exists; otherwise, -<code>/Library/Caches/Homebrew</code>.</p></dd> +<p><em>Default:</em> <code>~/Library/Caches/Homebrew</code>.</p></dd> <dt><code>HOMEBREW_CURL_VERBOSE</code></dt><dd><p>If set, Homebrew will pass <code>--verbose</code> when invoking <code>curl</code>(1).</p></dd> <dt><code>HOMEBREW_DEBUG</code></dt><dd><p>If set, any commands that can emit debugging information will do so.</p></dd> <dt><code>HOMEBREW_DEBUG_INSTALL</code></dt><dd><p>When <code>brew install -d</code> or <code>brew install -i</code> drops into a shell, diff --git a/share/man/man1/brew.1 b/share/man/man1/brew.1 index 8426050107f580e7ea451fe90296ebe2ee035539..cd70a536993f4b3050ec578319ffa3ab14ce4049 100644 --- a/share/man/man1/brew.1 +++ b/share/man/man1/brew.1 @@ -720,7 +720,7 @@ If set, instructs Homebrew to compile from source even when a formula provides a If set, instructs Homebrew to use the given directory as the download cache\. . .IP -\fIDefault:\fR \fB~/Library/Caches/Homebrew\fR if it exists; otherwise, \fB/Library/Caches/Homebrew\fR\. +\fIDefault:\fR \fB~/Library/Caches/Homebrew\fR\. . .TP \fBHOMEBREW_CURL_VERBOSE\fR