Moved locking/unlocking into a function. Added PROMPT_COMMAND that went missing.

This commit is contained in:
Darren 'Tadgy' Austin 2019-07-01 21:14:42 +01:00
commit 70abe8c58f
3 changed files with 45 additions and 88 deletions

38
.bashrc
View file

@ -66,6 +66,32 @@ _find_agent_sock() {
return 1
}
_lock_agents_file() {
local I
# Lock the ~/.ssh/agents file.
if [[ "$_PLATFORM" == "Linux" ]]; then
# Linux has 'flock', thankfully.
if exec 9>~/.ssh/agents.lock && flock -E 10 -w 0.5 9; then
return 0
else
echo "$(tput setaf 1 || tput AF 1)Failed to obtain lockfile!$(tput op)"
fi
elif [[ "$_PLATFORM" == "Darwin" ]]; then
# Do locking the sucky way on OSX.
for ((I=0; I < 5; I++)); do
if shlock -f "/tmp/agents.lock.$$" -p "$$"; then
return 0
else
sleep 0.1
fi
done
[[ "${_GOT_LOCK:-0}" == "0" ]] && echo "$(tput setaf 1 || tput AF 1)Failed to obtain lockfile!$(tput op)"
else
echo "$(tput setaf 1 || tput AF 1)File locking unsupported - skipping update of agents file!$(tput op)"
fi
return 1
}
_pop_agent_sock() {
local I IFS=$'\n' REPLY SOCKS=()
# Read the current list of auth sockets.
@ -107,6 +133,15 @@ _push_agent_sock() {
return $?
}
_unlock_agents_file() {
# Unlock the ~/.ssh/agents file.
if [[ "$_PLATFORM" == "Linux" ]]; then
exec 9>&-
elif [[ "$_PLATFORM" == "Darwin" ]]; then
rm -f "/tmp/agents.lock.$$"
fi
}
# Make bash a little more pleasent - these are valid for all versions.
shopt -s cdspell checkhash checkwinsize cmdhist histappend no_empty_cmd_completion
@ -160,6 +195,9 @@ unset _COLOUR
# PS4="+(\\\$? = \$?) \${BASH_SOURCE##*/}\${FUNCNAME:+(\$FUNCNAME)}:\$LINENO: "
PS4="+(\[\e[33m\]\\\$? = \$?\[$(tput sgr0)\]) \[$(tput bold)$(tput setaf 4)\]\${BASH_SOURCE##*/}\[$(tput sgr0)\]\${FUNCNAME:+(\[$(tput bold)$(tput setaf 2)\]\$FUNCNAME\[$(tput sgr0)\])}:\[$(tput bold)$(tput setaf 1)\]\$LINENO\[$(tput sgr0)\]: "
# The commands to execute before the prompt is displayed.
PROMPT_COMMAND="_agent_prompt_command"
# Platform specific set up.
_PLATFORM="$(uname -s)"
if [[ "$_PLATFORM" = "Linux" ]]; then