From 4f1dfba4b8b56f64d977f67785889305f46e606d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandros=20Koz=C3=A1k?= Date: Thu, 13 Dec 2018 14:24:11 -0800 Subject: [PATCH] @mafredri's flock solution https://github.com/rupa/z/pull/199 --- zsh-z.plugin.zsh | 42 ++++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/zsh-z.plugin.zsh b/zsh-z.plugin.zsh index 0b71e03..ccad7d8 100644 --- a/zsh-z.plugin.zsh +++ b/zsh-z.plugin.zsh @@ -79,6 +79,14 @@ With no ARGUMENT, list the directory history in ascending rank. # of ZSH (( $+EPOCHSECONDS )) || zmodload zsh/datetime +# Load zsh/system, if necessary +whence -w zsystem &> /dev/null || zmodload zsh/system &> /dev/null + +# Determine whether zsystem flock is available +if zsystem supports flock &> /dev/null; then + typeset -g ZSHZ_USE_ZSYSTEM_FLOCK=1 +fi + ######################################################## # Maintain the datafile # @@ -328,8 +336,6 @@ zshz() { esac done - # zsystem flock-based solution by @mafredri - # A temporary file that gets copied over the datafile if all goes well local tempfile="$(mktemp "${datafile}.XXXXXXXX")" @@ -344,24 +350,28 @@ zshz() { local ret=$? - # Replace contents of datafile with tempfile - command cat "$tempfile" >| "$datafile" - - if [[ ${ZSHZ_OWNER:-${_Z_OWNER}} ]]; then - chown ${ZSHZ_OWNER:-${_Z_OWNER}}:$(id -ng ${ZSHZ_OWNER:_${_Z_OWNER}}) "$datafile" - fi - - command rm -f "$tempfile" + if (( $ZSHZ_USE_ZSYSTEM_FLOCK )); then - # Avoid clobbering the datafile in a race condition - if (( $? != 0 )) && [[ -f $datafile ]]; then + # Replace contents of datafile with tempfile + command cat "$tempfile" >| "$datafile" + if [[ ${ZSHZ_OWNER:-${_Z_OWNER}} ]]; then + chown ${ZSHZ_OWNER:-${_Z_OWNER}}:$(id -ng ${ZSHZ_OWNER:_${_Z_OWNER}}) "$datafile" + fi command rm -f "$tempfile" + else - if [[ -n ${ZSHZ_OWNER:-${_Z_OWNER}} ]]; then - chown "${ZSHZ_OWNER:-${_Z_OWNER}}":"$(id -ng "${ZSHZ_OWNER:-${_Z_OWNER}}")" "$tempfile" + + # Avoid clobbering the datafile in a race condition + if (( ret != 0 )) && [[ -f $datafile ]]; then + command rm -f "$tempfile" + else + if [[ -n ${ZSHZ_OWNER:-${_Z_OWNER}} ]]; then + chown "${ZSHZ_OWNER:-${_Z_OWNER}}":"$(id -ng "${ZSHZ_OWNER:-${_Z_OWNER}}")" "$tempfile" + fi + command mv -f "$tempfile" "$datafile" 2> /dev/null \ + || command rm -f "$tempfile" fi - command mv -f "$tempfile" "$datafile" 2> /dev/null \ - || command rm -f "$tempfile" + fi elif [[ ${ZSHZ_COMPLETION:-frecent} == 'legacy' ]] && [[ $1 == '--complete' ]] \