utils/lock.sh: repair style

This commit is contained in:
hyuraku 2021-04-21 20:51:50 +09:00
parent daa8f7c858
commit 05260f4565

View File

@ -1,17 +1,19 @@
# create a lock using `flock(2)`. A name is required as first argument. # create a lock using `flock(2)`. A name is required as first argument.
# the lock will be automatically unlocked when the shell process quits. # the lock will be automatically unlocked when the shell process quits.
# Noted due to the fixed FD, a shell process can only create one lock. # Noted due to the fixed FD, a shell process can only create one lock.
# HOMEBREW_PREFIX is set by extend/ENV/super.rb
# shellcheck disable=SC2154
lock() { lock() {
local name="$1" local name="$1"
local lock_dir="$HOMEBREW_PREFIX/var/homebrew/locks" local lock_dir="${HOMEBREW_PREFIX}/var/homebrew/locks"
local lock_file="$lock_dir/$name" local lock_file="${lock_dir}/${name}"
[[ -d "$lock_dir" ]] || mkdir -p "$lock_dir" [[ -d "${lock_dir}" ]] || mkdir -p "${lock_dir}"
if ! [[ -w "$lock_dir" ]] if ! [[ -w "${lock_dir}" ]]
then then
odie <<EOS odie <<EOS
Can't create $name lock in $lock_dir! Can't create ${name} lock in ${lock_dir}!
Fix permissions by running: Fix permissions by running:
sudo chown -R \$(whoami) $HOMEBREW_PREFIX/var/homebrew sudo chown -R \$(whoami) ${HOMEBREW_PREFIX}/var/homebrew
EOS EOS
fi fi
# 200 is the file descriptor used in the lock. # 200 is the file descriptor used in the lock.
@ -24,11 +26,11 @@ EOS
# close FD first, this is required if parent process holds a different lock. # close FD first, this is required if parent process holds a different lock.
exec 200>&- exec 200>&-
# open the lock file to FD, so the shell process can hold the lock. # open the lock file to FD, so the shell process can hold the lock.
exec 200>"$lock_file" exec 200>"${lock_file}"
if ! _create_lock 200 "$name" if ! _create_lock 200 "${name}"
then then
odie <<EOS odie <<EOS
Another active Homebrew $name process is already in progress. Another active Homebrew ${name} process is already in progress.
Please wait for it to finish or terminate it to continue. Please wait for it to finish or terminate it to continue.
EOS EOS
fi fi
@ -39,19 +41,19 @@ _create_lock() {
local name="$2" local name="$2"
local ruby="/usr/bin/ruby" local ruby="/usr/bin/ruby"
local python="/usr/bin/python" local python="/usr/bin/python"
[[ -x "$ruby" ]] || ruby="$(type -P ruby)" [[ -x "${ruby}" ]] || ruby="$(type -P ruby)"
[[ -x "$python" ]] || python="$(type -P python)" [[ -x "${python}" ]] || python="$(type -P python)"
if [[ -x "$ruby" ]] && "$ruby" -e "exit(RUBY_VERSION >= '1.8.7')" if [[ -x "${ruby}" ]] && "${ruby}" -e "exit(RUBY_VERSION >= '1.8.7')"
then then
"$ruby" -e "File.new($lock_fd).flock(File::LOCK_EX | File::LOCK_NB) || exit(1)" "${ruby}" -e "File.new(${lock_fd}).flock(File::LOCK_EX | File::LOCK_NB) || exit(1)"
elif [[ -x "$python" ]] elif [[ -x "${python}" ]]
then then
"$python" -c "import fcntl; fcntl.flock($lock_fd, fcntl.LOCK_EX | fcntl.LOCK_NB)" "${python}" -c "import fcntl; fcntl.flock(${lock_fd}, fcntl.LOCK_EX | fcntl.LOCK_NB)"
elif [[ -x "$(type -P flock)" ]] elif [[ -x "$(type -P flock)" ]]
then then
flock -n "$lock_fd" flock -n "${lock_fd}"
else else
onoe "Cannot create $name lock, please avoid running Homebrew in parallel." onoe "Cannot create ${name} lock, please avoid running Homebrew in parallel."
fi fi
} }