Files
Michael Herrmann 5096a546df Fix errors in macOS .pkg postinstall script (#36483)
Eg. when installing from Brave-Browser-universal.pkg, the previous
implementation gave the following `postinstall` errors in
`/var/log/install.log`:

chmod: /Applications/Brave Browser universal.app: No such file or directory
chown: /Applications/Brave Browser universal.app: No such file or directory
chgrp: /Applications/Brave Browser universal.app: No such file or directory
2026-05-15 19:43:26 +01:00

57 lines
2.1 KiB
Bash
Executable File

#!/bin/bash
# This postinstall script is run on macOS pkg installers
# Extract a referral / promo code from the installer path of the format /dir/path/Setup-Brave-Anything-xxx001.pkg
# where the promo code would be xxx001
installerPath=$1
installationPath=$2
promoCode=""
# Detect if installer contained a referral promotion code within the filename
standardPromoCodeRegex='[a-zA-Z0-9]{3}[0-9]{3}'
installerPathPromoCodeRegex=".+-((${standardPromoCodeRegex})|([a-zA-Z]{1,}-[a-zA-Z]{1,}))([[:blank:]]?\([0-9]+\))?\.pkg$"
if [[ $installerPath =~ $installerPathPromoCodeRegex ]]; then
n=${#BASH_REMATCH[*]}
rematch=${BASH_REMATCH[1]}
if [[ $n -ge 1 && ! ${rematch} =~ ^Browser-.*$ ]]; then
promoCode=${rematch}
if [[ $promoCode =~ $standardPromoCodeRegex ]]; then
promoCode=$(tr a-z A-Z <<< $promoCode)
fi
echo "Got promo code: $promoCode"
fi
fi
# The bundle name and user-data subdirectory come from GN at build time.
installationAppPath="$installationPath/@APP_BUNDLE@"
appDataDir="$HOME/Library/Application Support/@PRODUCT_DIR_NAME@"
promoCodePath="$appDataDir/promoCode"
# pkg runs this script as root so we need to get current username
userName="$USER"
echo "Installer path is: $installerPath"
echo "Username is: $userName"
echo "Installation app path is: $installationAppPath"
# Fix the permissions on installed .app so that updater has permissions to write new contents.
# By default pkg will install the .app with root:wheel owner and 'drwxr-xr-x' permission.
# We'll allow all admin users of the machine permissions to update the app, as well as the installing-user
# (who may not be an admin).
sudo chmod -R 775 "$installationAppPath"
sudo chown -R $userName "$installationAppPath"
sudo chgrp -R admin "$installationAppPath"
# handle promoCode specific logic (if promoCode was found)
if [ "$promoCode" != "" ]; then
echo "Writing to user data directory at: $promoCodePath"
mkdir -p "$appDataDir"
echo "$promoCode" > "$promoCodePath"
# mkdir -p above may have created the parent directory as root; chown
# it (and everything below) so the installing user can write here.
sudo chown -R "$userName" "$(dirname "$appDataDir")"
fi
exit 0