Use correct stat args and fix FD handling on Darwin.

This commit is contained in:
Darren 'Tadgy' Austin 2023-10-01 23:15:21 +01:00
commit fbf74989f3

14
.bashrc
View file

@ -195,7 +195,8 @@ __read_ssh_agents() {
# Do locking the sucky way on macOS. # Do locking the sucky way on macOS.
for ((I = 0; I <= 5; I++)); do for ((I = 0; I <= 5; I++)); do
if shlock -p "$$" -f "$HOME/.ssh/agents.lock"; then if shlock -p "$$" -f "$HOME/.ssh/agents.lock"; then
exec {FD}<"$HOME/.ssh/agents" exec 9<"$HOME/.ssh/agents"
FD="9"
# Make note of the mtime for use in write_ssh_agents. # Make note of the mtime for use in write_ssh_agents.
SSH_AGENTS_MTIME="$(stat -f %Fm "$HOME/.ssh/agents")" SSH_AGENTS_MTIME="$(stat -f %Fm "$HOME/.ssh/agents")"
ERR=0 ERR=0
@ -221,7 +222,7 @@ __read_ssh_agents() {
ERR=$? ERR=$?
# Close the file descriptor (which on Linux releases the flock too). # Close the file descriptor (which on Linux releases the flock too).
exec {FD}<&- : "$FD"<&-
# On Darwin, release the lock on the file. # On Darwin, release the lock on the file.
rm -f "$HOME/.ssh/agents.lock" rm -f "$HOME/.ssh/agents.lock"
@ -266,7 +267,7 @@ __write_ssh_agents() {
(( ${MTIME/\.} > SSH_AGENTS_MTIME )) && return 2 (( ${MTIME/\.} > SSH_AGENTS_MTIME )) && return 2
# Lock the agents file. # Lock the agents file.
exec {FD}<"$HOME/.ssh/agents" && flock -E 10 -e -w 0.5 "$FD" exec {FD}>"$HOME/.ssh/agents" && flock -E 10 -e -w 0.5 "$FD"
ERR=$? ERR=$?
if (( ERR == 10 )); then if (( ERR == 10 )); then
printf "\\033[1;31m%s\\033[39m\\n" "Failed to obtain lock on ~/.ssh/agents." >&2 printf "\\033[1;31m%s\\033[39m\\n" "Failed to obtain lock on ~/.ssh/agents." >&2
@ -277,13 +278,14 @@ __write_ssh_agents() {
fi fi
elif [[ "$(uname -s)" == "Darwin" ]]; then elif [[ "$(uname -s)" == "Darwin" ]]; then
# Make sure SSH_AUTH_SOCKS has the most up to date data. # Make sure SSH_AUTH_SOCKS has the most up to date data.
MTIME="$(stat --format=%.9Y "$HOME/.ssh/agents")" MTIME="$(stat -f %Fm "$HOME/.ssh/agents")"
(( ${MTIME/\.} > SSH_AGENTS_MTIME )) && return 2 (( ${MTIME/\.} > SSH_AGENTS_MTIME )) && return 2
# Do locking the sucky way on OSX. # Do locking the sucky way on OSX.
for ((I = 0; I <= 5; I++)); do for ((I = 0; I <= 5; I++)); do
if shlock -p "$$" -f "$HOME/.ssh/agents.lock"; then if shlock -p "$$" -f "$HOME/.ssh/agents.lock"; then
exec {FD}<"$HOME/.ssh/agents" exec 9>"$HOME/.ssh/agents"
FD="9"
ERR=0 ERR=0
break break
else else
@ -305,7 +307,7 @@ __write_ssh_agents() {
[[ -n "${SOCKS[*]}" ]] && { printf "%s\\n" "${SOCKS[@]}" >"$HOME/.ssh/agents" 2>/dev/null; ERR=$?; } [[ -n "${SOCKS[*]}" ]] && { printf "%s\\n" "${SOCKS[@]}" >"$HOME/.ssh/agents" 2>/dev/null; ERR=$?; }
# Release locks. # Release locks.
exec {FD}>&- : "$FD"<&-
rm -f "$HOME/.ssh/agents.lock" rm -f "$HOME/.ssh/agents.lock"
# Error out if the data couldn't be written. # Error out if the data couldn't be written.