brew.sh: fix HOMEBREW_PREFIX if /home is a symlink

Correct `HOMEBREW_PREFIX`, and its only dependent up to that point, `HOMEBREW_REPOSITORY`, if the defaults contain symlinks. For example:
```console
$ realpath /home/linuxbrew/.linuxbrew
/var/home/linuxbrew/.linuxbrew
$ ls -ld /home
lrwxrwxrwx. 4 root root 8 Jun 29 22:29 /home -> var/home
```
I was hoping to correct `HOMEBREW_PREFIX` from the start (in `bin/brew`), but the default prefixes are in `brew.sh`. See #15656 for a discussion on this middleground solution.

Co-Authored-By: rohanssrao <17805516+rohanssrao@users.noreply.github.com>
Co-Authored-By: Bo Anderson <mail@boanderson.me>
This commit is contained in:
Osama Albahrani 2023-07-09 17:41:30 -04:00
parent 287a359a74
commit 64b6479100
2 changed files with 21 additions and 1 deletions

View File

@ -44,6 +44,26 @@ else
HOMEBREW_DEFAULT_TEMP="/tmp" HOMEBREW_DEFAULT_TEMP="/tmp"
fi fi
realpath() {
(cd "$1" &>/dev/null && pwd -P)
}
# Support systems where HOMEBREW_PREFIX is the default,
# but a parent directory is a symlink.
# Example: Fedora Silverblue symlinks /home -> var/home
if [[ "${HOMEBREW_PREFIX}" != "${HOMEBREW_DEFAULT_PREFIX}" && "$(realpath "${HOMEBREW_DEFAULT_PREFIX}")" == "${HOMEBREW_PREFIX}" ]]
then
HOMEBREW_PREFIX="${HOMEBREW_DEFAULT_PREFIX}"
fi
# Support systems where HOMEBREW_REPOSITORY is the default,
# but a parent directory is a symlink.
# Example: Fedora Silverblue symlinks /home -> var/home
if [[ "${HOMEBREW_REPOSITORY}" != "${HOMEBREW_DEFAULT_REPOSITORY}" && "$(realpath "${HOMEBREW_DEFAULT_REPOSITORY}")" == "${HOMEBREW_REPOSITORY}" ]]
then
HOMEBREW_REPOSITORY="${HOMEBREW_DEFAULT_REPOSITORY}"
fi
# Where we store built products; a Cellar in HOMEBREW_PREFIX (often /usr/local # Where we store built products; a Cellar in HOMEBREW_PREFIX (often /usr/local
# for bottles) unless there's already a Cellar in HOMEBREW_REPOSITORY. # for bottles) unless there's already a Cellar in HOMEBREW_REPOSITORY.
# These variables are set by bin/brew # These variables are set by bin/brew

View File

@ -40,7 +40,7 @@ do
done done
unset cmd unset cmd
BREW_FILE_DIRECTORY="$(quiet_cd "${0%/*}/" && pwd)" BREW_FILE_DIRECTORY="$(quiet_cd "${0%/*}/" && pwd -P)"
HOMEBREW_BREW_FILE="${BREW_FILE_DIRECTORY%/}/${0##*/}" HOMEBREW_BREW_FILE="${BREW_FILE_DIRECTORY%/}/${0##*/}"
HOMEBREW_PREFIX="${HOMEBREW_BREW_FILE%/*/*}" HOMEBREW_PREFIX="${HOMEBREW_BREW_FILE%/*/*}"