diff --git a/Library/Homebrew/cleanup.rb b/Library/Homebrew/cleanup.rb index 0cda61fb8bb4d..99d2fa3311f1f 100644 --- a/Library/Homebrew/cleanup.rb +++ b/Library/Homebrew/cleanup.rb @@ -555,7 +555,7 @@ def cleanup_cache_db(rack = nil) end def rm_ds_store(dirs = nil) - dirs ||= Keg::MUST_EXIST_DIRECTORIES + [ + dirs ||= Keg.must_exist_directories + [ HOMEBREW_PREFIX/"Caskroom", ] dirs.select(&:directory?) @@ -623,7 +623,7 @@ def prune_prefix_symlinks_and_directories dirs = [] children_count = {} - Keg::MUST_EXIST_SUBDIRECTORIES.each do |dir| + Keg.must_exist_subdirectories.each do |dir| next unless dir.directory? dir.find do |path| @@ -639,7 +639,7 @@ def prune_prefix_symlinks_and_directories path.unlink end end - elsif path.directory? && Keg::MUST_EXIST_SUBDIRECTORIES.exclude?(path) + elsif path.directory? && Keg.must_exist_subdirectories.exclude?(path) dirs << path children_count[path] = path.children.length if dry_run? end diff --git a/Library/Homebrew/diagnostic.rb b/Library/Homebrew/diagnostic.rb index 6b63ac93aee05..c88b0b941227a 100644 --- a/Library/Homebrew/diagnostic.rb +++ b/Library/Homebrew/diagnostic.rb @@ -315,7 +315,7 @@ def check_for_stray_headers def check_for_broken_symlinks broken_symlinks = [] - Keg::MUST_EXIST_SUBDIRECTORIES.each do |d| + Keg.must_exist_subdirectories.each do |d| next unless d.directory? d.find do |path| @@ -344,7 +344,7 @@ def check_tmpdir_sticky_bit def check_exist_directories return if HOMEBREW_PREFIX.writable? - not_exist_dirs = Keg::MUST_EXIST_DIRECTORIES.reject(&:exist?) + not_exist_dirs = Keg.must_exist_directories.reject(&:exist?) return if not_exist_dirs.empty? <<~EOS @@ -359,8 +359,8 @@ def check_exist_directories def check_access_directories not_writable_dirs = - Keg::MUST_BE_WRITABLE_DIRECTORIES.select(&:exist?) - .reject(&:writable?) + Keg.must_be_writable_directories.select(&:exist?) + .reject(&:writable?) return if not_writable_dirs.empty? <<~EOS diff --git a/Library/Homebrew/extend/os/mac/keg.rb b/Library/Homebrew/extend/os/mac/keg.rb index b71aaee900528..aae4eb6ad3552 100644 --- a/Library/Homebrew/extend/os/mac/keg.rb +++ b/Library/Homebrew/extend/os/mac/keg.rb @@ -3,32 +3,38 @@ require "system_command" -class Keg - include SystemCommand::Mixin - - # TODO: re-implement these as functions, so that we aren't modifying constants: - GENERIC_KEG_LINK_DIRECTORIES = (remove_const :KEG_LINK_DIRECTORIES).freeze - KEG_LINK_DIRECTORIES = (GENERIC_KEG_LINK_DIRECTORIES + ["Frameworks"]).freeze - GENERIC_MUST_EXIST_SUBDIRECTORIES = (remove_const :MUST_EXIST_SUBDIRECTORIES).freeze - MUST_EXIST_SUBDIRECTORIES = ( - GENERIC_MUST_EXIST_SUBDIRECTORIES + - [HOMEBREW_PREFIX/"Frameworks"] - ).sort.uniq.freeze - GENERIC_MUST_EXIST_DIRECTORIES = (remove_const :MUST_EXIST_DIRECTORIES).freeze - MUST_EXIST_DIRECTORIES = ( - GENERIC_MUST_EXIST_DIRECTORIES + - [HOMEBREW_PREFIX/"Frameworks"] - ).sort.uniq.freeze - GENERIC_MUST_BE_WRITABLE_DIRECTORIES = (remove_const :MUST_BE_WRITABLE_DIRECTORIES).freeze - MUST_BE_WRITABLE_DIRECTORIES = ( - GENERIC_MUST_BE_WRITABLE_DIRECTORIES + - [HOMEBREW_PREFIX/"Frameworks"] - ).sort.uniq.freeze -end - module OS module Mac module Keg + include SystemCommand::Mixin + + module ClassMethods + def keg_link_directories + @keg_link_directories ||= (super + ["Frameworks"]).freeze + end + + def must_exist_subdirectories + @must_exist_subdirectories ||= ( + super + + [HOMEBREW_PREFIX/"Frameworks"] + ).sort.uniq.freeze + end + + def must_exist_directories + @must_exist_directories = ( + super + + [HOMEBREW_PREFIX/"Frameworks"] + ).sort.uniq.freeze + end + + def must_be_writable_directories + @must_be_writable_directories ||= ( + super + + [HOMEBREW_PREFIX/"Frameworks"] + ).sort.uniq.freeze + end + end + def binary_executable_or_library_files = mach_o_files def codesign_patched_binary(file) @@ -121,4 +127,5 @@ def consistent_reproducible_symlink_permissions! end end +Keg.singleton_class.prepend(OS::Mac::Keg::ClassMethods) Keg.prepend(OS::Mac::Keg) diff --git a/Library/Homebrew/formula_installer.rb b/Library/Homebrew/formula_installer.rb index 47bdd56e61e06..15a4df4bf49c9 100644 --- a/Library/Homebrew/formula_installer.rb +++ b/Library/Homebrew/formula_installer.rb @@ -1229,7 +1229,7 @@ def post_install sandbox.deny_write_homebrew_repository sandbox.allow_write_cellar(formula) sandbox.deny_all_network unless formula.network_access_allowed?(:postinstall) - Keg::KEG_LINK_DIRECTORIES.each do |dir| + Keg.keg_link_directories.each do |dir| sandbox.allow_write_path "#{HOMEBREW_PREFIX}/#{dir}" end sandbox.run(*args) diff --git a/Library/Homebrew/install.rb b/Library/Homebrew/install.rb index 795192b3a0a03..34849222f1d8b 100644 --- a/Library/Homebrew/install.rb +++ b/Library/Homebrew/install.rb @@ -325,7 +325,7 @@ def check_cc_argv(cc) end def attempt_directory_creation - Keg::MUST_EXIST_DIRECTORIES.each do |dir| + Keg.must_exist_directories.each do |dir| FileUtils.mkdir_p(dir) unless dir.exist? rescue nil diff --git a/Library/Homebrew/keg.rb b/Library/Homebrew/keg.rb index 8cbe45ec603ce..0b0f67d2564cd 100644 --- a/Library/Homebrew/keg.rb +++ b/Library/Homebrew/keg.rb @@ -78,38 +78,6 @@ def to_s # Locale-specific directories have the form `language[_territory][.codeset][@modifier]` LOCALEDIR_RX = %r{(locale|man)/([a-z]{2}|C|POSIX)(_[A-Z]{2})?(\.[a-zA-Z\-0-9]+(@.+)?)?} INFOFILE_RX = %r{info/([^.].*?\.info(\.gz)?|dir)$} - KEG_LINK_DIRECTORIES = %w[ - bin etc include lib sbin share var - ].freeze - MUST_EXIST_SUBDIRECTORIES = ( - KEG_LINK_DIRECTORIES - %w[var] + %w[ - opt - var/homebrew/linked - ] - ).map { |dir| HOMEBREW_PREFIX/dir }.sort.uniq.freeze - - # Keep relatively in sync with - # {https://github.com/Homebrew/install/blob/HEAD/install.sh} - MUST_EXIST_DIRECTORIES = (MUST_EXIST_SUBDIRECTORIES + [ - HOMEBREW_CELLAR, - ].sort.uniq).freeze - MUST_BE_WRITABLE_DIRECTORIES = ( - %w[ - etc/bash_completion.d lib/pkgconfig - share/aclocal share/doc share/info share/locale share/man - share/man/man1 share/man/man2 share/man/man3 share/man/man4 - share/man/man5 share/man/man6 share/man/man7 share/man/man8 - share/zsh share/zsh/site-functions - var/log - ].map { |dir| HOMEBREW_PREFIX/dir } + MUST_EXIST_SUBDIRECTORIES + [ - HOMEBREW_CACHE, - HOMEBREW_CELLAR, - HOMEBREW_LOCKS, - HOMEBREW_LOGS, - HOMEBREW_REPOSITORY, - Language::Python.homebrew_site_packages, - ] - ).sort.uniq.freeze # These paths relative to the keg's share directory should always be real # directories in the prefix, never symlinks. @@ -146,6 +114,51 @@ def self.all Formula.racks.flat_map(&:subdirs).map { |d| new(d) } end + def self.keg_link_directories + @keg_link_directories ||= %w[ + bin etc include lib sbin share var + ].freeze + end + + def self.must_exist_subdirectories + @must_exist_subdirectories ||= ( + keg_link_directories - %w[var] + %w[ + opt + var/homebrew/linked + ] + ).map { |dir| HOMEBREW_PREFIX/dir }.sort.uniq.freeze + end + + # Keep relatively in sync with + # {https://github.com/Homebrew/install/blob/HEAD/install.sh} + def self.must_exist_directories + @must_exist_directories ||= (must_exist_subdirectories + [ + HOMEBREW_CELLAR, + ].sort.uniq).freeze + end + + # Keep relatively in sync with + # {https://github.com/Homebrew/install/blob/HEAD/install.sh} + def self.must_be_writable_directories + @must_be_writable_directories ||= ( + %w[ + etc/bash_completion.d lib/pkgconfig + share/aclocal share/doc share/info share/locale share/man + share/man/man1 share/man/man2 share/man/man3 share/man/man4 + share/man/man5 share/man/man6 share/man/man7 share/man/man8 + share/zsh share/zsh/site-functions + var/log + ].map { |dir| HOMEBREW_PREFIX/dir } + must_exist_subdirectories + [ + HOMEBREW_CACHE, + HOMEBREW_CELLAR, + HOMEBREW_LOCKS, + HOMEBREW_LOGS, + HOMEBREW_REPOSITORY, + Language::Python.homebrew_site_packages, + ] + ).sort.uniq.freeze + end + attr_reader :path, :name, :linked_keg_record, :opt_record protected :path @@ -288,8 +301,8 @@ def unlink(verbose: false, dry_run: false) dirs = [] - keg_directories = KEG_LINK_DIRECTORIES.map { |d| path/d } - .select(&:exist?) + keg_directories = self.class.keg_link_directories.map { |d| path/d } + .select(&:exist?) keg_directories.each do |dir| dir.find do |src| dst = HOMEBREW_PREFIX + src.relative_path_from(path) @@ -316,7 +329,7 @@ def unlink(verbose: false, dry_run: false) unless dry_run remove_old_aliases remove_linked_keg_record if linked? - (dirs - MUST_EXIST_SUBDIRECTORIES).reverse_each(&:rmdir_if_possible) + (dirs - self.class.must_exist_subdirectories).reverse_each(&:rmdir_if_possible) end ObserverPathnameExtension.n diff --git a/Library/Homebrew/test/spec_helper.rb b/Library/Homebrew/test/spec_helper.rb index f86636bf2e041..783b522b9664e 100644 --- a/Library/Homebrew/test/spec_helper.rb +++ b/Library/Homebrew/test/spec_helper.rb @@ -272,7 +272,7 @@ FileUtils.rm_rf [ *TEST_DIRECTORIES, - *Keg::MUST_EXIST_SUBDIRECTORIES, + *Keg.must_exist_subdirectories, HOMEBREW_LINKED_KEGS, HOMEBREW_PINNED_KEGS, HOMEBREW_PREFIX/"var",