3 # Copyright 2018 - 2021 Paul Hänsch
5 # Permission to use, copy, modify, and/or distribute this software for any
6 # purpose with or without fee is hereby granted, provided that the above
7 # copyright notice and this permission notice appear in all copies.
9 # THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
12 # SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
15 # IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 [ -n "$include_storage" ] && return 0
25 local lock="${1}.lock" timeout="${2-20}" block
27 if [ \! -w "${lock%/*}" ] || [ -e "$lock" -a \! -f "$lock" ]; then
28 debug "Impossible to get lock: $lock"
32 while [ $timeout -gt 0 ]; do
33 printf '%i\n' $$ >>"${lock}"
35 if [ "$block" = $$ ]; then
37 elif ! { ps -eo pid |grep -qwF "$block"; }; then
38 debug "Trying to override stale lock: $lock"
39 if LOCK "$lock" 1; then
44 timeout=$((timeout - 1))
45 [ $timeout -gt 0 ] && sleep 1
49 debug "Timeout while trying to get lock: $lock"
54 local lock="${1}.lock" block
57 if [ "$block" = $$ ]; then
61 debug "Refusing to release foreign lock: $lock"
68 [ $# -gt 0 ] && in="$*" || in="$(cat)"
69 while [ "$in" ]; do case $in in
70 \\*) out="${out}\\\\"; in="${in#\\}" ;;
71 "$BR"*) out="${out}\\n"; in="${in#${BR}}" ;;
72 "$CR"*) out="${out}\\r"; in="${in#${CR}}" ;;
73 " "*) out="${out}\\t"; in="${in# }" ;;
74 +*) out="${out}\\+"; in="${in#+}" ;;
75 " "*) out="${out}+"; in="${in# }" ;;
76 *) out="${out}${in%%[\\${CR}${BR} + ]*}"; in="${in#"${in%%[\\${BR}${CR} + ]*}"}" ;;
78 printf '%s' "${out:-\\}"
83 [ $# -gt 0 ] && in="$*" || in="$(cat)"
84 while [ "$in" ]; do case $in in
85 \\\\*) out="${out}\\"; in="${in#\\\\}" ;;
86 \\n*) out="${out}${BR}"; in="${in#\\n}" ;;
87 \\r*) out="${out}${CR}"; in="${in#\\r}" ;;
88 \\t*) out="${out} "; in="${in#\\t}" ;;
89 \\+*) out="${out}+"; in="${in#\\+}" ;;
90 +*) out="${out} "; in="${in#+}" ;;
92 *) out="${out}${in%%[\\+]*}"; in="${in#"${in%%[\\+]*}"}" ;;
98 local file="$1" cmd="$2"
105 while read -r k v; do if [ "$k" = "$key" ]; then
107 fi; done <"$file" 2>&-
112 while read -r k v; do if [ "$k" = "$key" ]; then
115 fi; done <"$file" 2>&-
119 key="$(STRING "$1")" value="$(STRING "$2")"
120 LOCK "$file" || return 1
121 { while read -r k v; do
122 [ "$k" = "$key" ] || printf '%s\t%s\n' "$k" "$v"
124 printf '%s\t%s\n' "$key" "$value"
126 mv "${file}.$$.tmp" "${file}"
131 k="$1" key="$(STRING "$1")" value="$(STRING "$2")"
132 LOCK "$file" || return 1
133 if DBM "$file" check "$k"; then
137 printf '%s\t%s\n' "$key" "$value" >>"${file}"
143 k="$1" key="$(STRING "$1")" value="$(STRING "$2")"
144 LOCK "$file" || return 1
145 if ! DBM "$file" check "$k"; then
149 { while read -r k v; do
151 && printf '%s\t%s\n' "$key" "$value" \
152 || printf '%s\t%s\n' "$k" "$v"
155 mv "${file}.$$.tmp" "${file}"
160 key="$(STRING "$1")" value="$(STRING "$2")"
161 LOCK "$file" || return 1
162 if ! DBM "$file" check "$1"; then
166 { while read -r k v; do
168 && printf '%s\t%s\n' "$key" "$v$value" \
169 || printf '%s\t%s\n' "$k" "$v"
172 mv "${file}.$$.tmp" "${file}"
178 LOCK "$file" || return 1
179 { while read -r k v; do
180 [ "$k" = "$key" ] || printf '%s\t%s\n' "$k" "$v"
183 mv "${file}.$$.tmp" "${file}"