# Create a lock using `flock(2)`. A command name with arguments is required as # first argument. The lock will be automatically unlocked when the shell process # quits. Note due to the fixed FD, a shell process can only create one lock. # HOMEBREW_LIBRARY is by brew.sh # HOMEBREW_PREFIX is set by extend/ENV/super.rb # shellcheck disable=SC2154 lock() { local command_name_and_args="$1" # use bash to replace spaces with dashes local lock_filename="${command_name_and_args// /-}" local lock_dir="${HOMEBREW_PREFIX}/var/homebrew/locks" local lock_file="${lock_dir}/${lock_filename}" [[ -d "${lock_dir}" ]] || mkdir -p "${lock_dir}" if [[ ! -w "${lock_dir}" ]] then odie <&- # open the lock file to FD, so the shell process can hold the lock. exec 200>"${lock_file}" if ! _create_lock 200 "${command_name_and_args}" then local lock_context if [[ -n "${HOMEBREW_LOCK_CONTEXT}" ]] then lock_context="\n${HOMEBREW_LOCK_CONTEXT}" fi odie <