From a89a3dfe91faf3f53a611d048ac7dfabb9e041ec Mon Sep 17 00:00:00 2001 From: Mike McQuaid Date: Wed, 22 May 2019 10:18:56 +0100 Subject: [PATCH 1/3] Use `whoami` consistently in `chown` commands --- Library/Homebrew/cask/cmd/doctor.rb | 2 +- docs/Gems,-Eggs-and-Perl-Modules.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/cask/cmd/doctor.rb b/Library/Homebrew/cask/cmd/doctor.rb index ddd033a2e1..0a6a015bba 100644 --- a/Library/Homebrew/cask/cmd/doctor.rb +++ b/Library/Homebrew/cask/cmd/doctor.rb @@ -67,7 +67,7 @@ module Cask if path.exist? && !path.writable? add_error "The staging path #{user_tilde(path.to_s)} is not writable by the current user." - add_error "To fix, run \'sudo chown -R ${USER}:staff #{user_tilde(path.to_s)}'" + add_error "To fix, run \'sudo chown -R $(whoami):staff #{user_tilde(path.to_s)}'" end puts user_tilde(path.to_s) diff --git a/docs/Gems,-Eggs-and-Perl-Modules.md b/docs/Gems,-Eggs-and-Perl-Modules.md index 8fafd9655a..9b6bd42308 100644 --- a/docs/Gems,-Eggs-and-Perl-Modules.md +++ b/docs/Gems,-Eggs-and-Perl-Modules.md @@ -116,7 +116,7 @@ If you ever did a `sudo gem`, etc. before then a lot of files will have been created owned by root. Fix with: ```sh -sudo chown -R $USER /Library/Ruby /Library/Perl /Library/Python +sudo chown -R $(whoami) /Library/Ruby /Library/Perl /Library/Python ``` ## Perl CPAN modules without sudo From 8908dc51d610f2ce3b2c086b25e04cb9322a66d1 Mon Sep 17 00:00:00 2001 From: Mike McQuaid Date: Wed, 22 May 2019 10:19:20 +0100 Subject: [PATCH 2/3] keg: tell people what command will remove keg. This will work regardless of permissions. --- Library/Homebrew/keg.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Library/Homebrew/keg.rb b/Library/Homebrew/keg.rb index 5c509d4600..48d6feff09 100644 --- a/Library/Homebrew/keg.rb +++ b/Library/Homebrew/keg.rb @@ -311,7 +311,10 @@ class Keg remove_old_aliases remove_oldname_opt_record rescue Errno::ENOTEMPTY - ofail "Could not remove #{path}! Check its permissions." + odie <<~EOS + Could not remove #{name} keg! Do so manually: + sudo rm -rf #{path} + EOS end def unlink(mode = OpenStruct.new) From 4759ffb88f4d4715f99cbf8431ebf8b1f1aab630 Mon Sep 17 00:00:00 2001 From: Mike McQuaid Date: Fri, 24 May 2019 16:46:54 +0100 Subject: [PATCH 3/3] reinstall: handle remove/rename permission errors. --- Library/Homebrew/formula_installer.rb | 9 ++++++++- Library/Homebrew/keg.rb | 2 +- Library/Homebrew/reinstall.rb | 18 ++++++++++++++++-- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/Library/Homebrew/formula_installer.rb b/Library/Homebrew/formula_installer.rb index b549b762d6..e861f38c78 100644 --- a/Library/Homebrew/formula_installer.rb +++ b/Library/Homebrew/formula_installer.rb @@ -289,7 +289,14 @@ class FormulaInstaller rescue Exception => e # rubocop:disable Lint/RescueException # any exceptions must leave us with nothing installed ignore_interrupts do - formula.prefix.rmtree if formula.prefix.directory? + begin + formula.prefix.rmtree if formula.prefix.directory? + rescue Errno::EACCES, Errno::ENOTEMPTY + odie <<~EOS + Could not remove #{formula.prefix.basename} keg! Do so manually: + sudo rm -rf #{formula.prefix} + EOS + end formula.rack.rmdir_if_possible end raise if ARGV.homebrew_developer? || diff --git a/Library/Homebrew/keg.rb b/Library/Homebrew/keg.rb index 48d6feff09..bafea64514 100644 --- a/Library/Homebrew/keg.rb +++ b/Library/Homebrew/keg.rb @@ -310,7 +310,7 @@ class Keg remove_opt_record if optlinked? remove_old_aliases remove_oldname_opt_record - rescue Errno::ENOTEMPTY + rescue Errno::EACCES, Errno::ENOTEMPTY odie <<~EOS Could not remove #{name} keg! Do so manually: sudo rm -rf #{path} diff --git a/Library/Homebrew/reinstall.rb b/Library/Homebrew/reinstall.rb index 015fc8a468..51862302a1 100644 --- a/Library/Homebrew/reinstall.rb +++ b/Library/Homebrew/reinstall.rb @@ -45,12 +45,26 @@ module Homebrew ignore_interrupts { restore_backup(keg, keg_was_linked) } raise else - backup_path(keg).rmtree if backup_path(keg).exist? + begin + backup_path(keg).rmtree if backup_path(keg).exist? + rescue Errno::EACCES, Errno::ENOTEMPTY + odie <<~EOS + Could not remove #{backup_path(keg).parent.basename} backup keg! Do so manually: + sudo rm -rf #{backup_path(keg)} + EOS + end end def backup(keg) keg.unlink - keg.rename backup_path(keg) + begin + keg.rename backup_path(keg) + rescue Errno::EACCES, Errno::ENOTEMPTY + odie <<~EOS + Could not rename #{keg.name} keg! Check/fix its permissions: + sudo chown -R $(whoami) #{keg} + EOS + end end def restore_backup(keg, keg_was_linked)