From 39bbc33a2784e5eb48b9721dfb304570f917e464 Mon Sep 17 00:00:00 2001 From: Mike McQuaid Date: Fri, 29 Sep 2023 12:32:22 +0100 Subject: [PATCH] Improve use of `whoami` If you're e.g. running Homebrew over `sudo`: shelling out to `whoami` is less effective than just telling people which user you're running as when we run the check. --- Library/Homebrew/cmd/update.sh | 2 +- Library/Homebrew/diagnostic.rb | 10 +++++++--- Library/Homebrew/reinstall.rb | 2 +- Library/Homebrew/utils/lock.sh | 2 +- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/Library/Homebrew/cmd/update.sh b/Library/Homebrew/cmd/update.sh index 100093c037a88..f4fa8d1139cb3 100644 --- a/Library/Homebrew/cmd/update.sh +++ b/Library/Homebrew/cmd/update.sh @@ -411,7 +411,7 @@ EOS ${HOMEBREW_REPOSITORY} is not writable. You should change the ownership and permissions of ${HOMEBREW_REPOSITORY} back to your user account: - sudo chown -R \$(whoami) ${HOMEBREW_REPOSITORY} + sudo chown -R ${USER-\$(whoami)} ${HOMEBREW_REPOSITORY} EOS fi diff --git a/Library/Homebrew/diagnostic.rb b/Library/Homebrew/diagnostic.rb index e0c8904a1ec35..512f475b77bd7 100644 --- a/Library/Homebrew/diagnostic.rb +++ b/Library/Homebrew/diagnostic.rb @@ -352,7 +352,7 @@ def check_exist_directories You should create these directories and change their ownership to your user. sudo mkdir -p #{not_exist_dirs.join(" ")} - sudo chown -R $(whoami) #{not_exist_dirs.join(" ")} + sudo chown -R #{current_user} #{not_exist_dirs.join(" ")} EOS end @@ -367,7 +367,7 @@ def check_access_directories #{not_writable_dirs.join("\n")} You should change the ownership of these directories to your user. - sudo chown -R $(whoami) #{not_writable_dirs.join(" ")} + sudo chown -R #{current_user} #{not_writable_dirs.join(" ")} And make sure that your user has write permission. chmod u+w #{not_writable_dirs.join(" ")} @@ -916,7 +916,7 @@ def check_cask_staging_location <<~EOS The staging path #{user_tilde(path.to_s)} is not writable by the current user. To fix, run: - sudo chown -R $(whoami):staff #{user_tilde(path.to_s)} + sudo chown -R #{current_user} #{user_tilde(path.to_s)} EOS end @@ -1027,6 +1027,10 @@ def all def cask_checks all.grep(/^check_cask_/) end + + def current_user + ENV.fetch("USER", "$(whoami)") + end end end end diff --git a/Library/Homebrew/reinstall.rb b/Library/Homebrew/reinstall.rb index f260d041b7c15..87bfb12004b5c 100644 --- a/Library/Homebrew/reinstall.rb +++ b/Library/Homebrew/reinstall.rb @@ -86,7 +86,7 @@ def backup(keg) rescue Errno::EACCES, Errno::ENOTEMPTY odie <<~EOS Could not rename #{keg.name} keg! Check/fix its permissions: - sudo chown -R $(whoami) #{keg} + sudo chown -R #{ENV.fetch("USER", "$(whoami)")} #{keg} EOS end end diff --git a/Library/Homebrew/utils/lock.sh b/Library/Homebrew/utils/lock.sh index 8cc5f997ce555..20062d6aeb009 100644 --- a/Library/Homebrew/utils/lock.sh +++ b/Library/Homebrew/utils/lock.sh @@ -13,7 +13,7 @@ lock() { odie <