3 # Copyright 2018, 2019, 2021 Paul Hänsch
5 # This is a file format helper, part of CGIlite.
7 # CGIlite is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # CGIlite is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with CGIlite. If not, see <http://www.gnu.org/licenses/>.
20 [ -n "$include_storage" ] && return 0
28 local lock="${1}.lock" timeout="${2-20}" block
30 if [ \! -w "${lock%/*}" ] || [ -e "$lock" -a \! -f "$lock" ]; then
31 debug "Impossible to get lock: $lock"
35 while [ $timeout -gt 0 ]; do
36 printf '%i\n' $$ >>"${lock}"
38 if [ "$block" = $$ ]; then
40 elif ! { ps -eo pid |grep -qwF "$block"; }; then
41 debug "Trying to override stale lock: $lock"
42 if LOCK "$lock" 1; then
47 timeout=$((timeout - 1))
48 [ $timeout -gt 0 ] && sleep 1
52 debug "Timeout while trying to get lock: $lock"
57 local lock="${1}.lock" block
60 if [ "$block" = $$ ]; then
64 debug "Refusing to release foreign lock: $lock"
71 [ $# -gt 0 ] && in="$*" || in="$(cat)"
72 while [ "$in" ]; do case $in in
73 \\*) out="${out}\\\\"; in="${in#\\}" ;;
74 "$BR"*) out="${out}\\n"; in="${in#${BR}}" ;;
75 "$CR"*) out="${out}\\r"; in="${in#${CR}}" ;;
76 " "*) out="${out}\\t"; in="${in# }" ;;
77 +*) out="${out}\\+"; in="${in#+}" ;;
78 " "*) out="${out}+"; in="${in# }" ;;
79 *) out="${out}${in%%[\\${CR}${BR} + ]*}"; in="${in#"${in%%[\\${BR}${CR} + ]*}"}" ;;
81 printf '%s' "${out:-\\}"
86 [ $# -gt 0 ] && in="$*" || in="$(cat)"
87 while [ "$in" ]; do case $in in
88 \\\\*) out="${out}\\"; in="${in#\\\\}" ;;
89 \\n*) out="${out}${BR}"; in="${in#\\n}" ;;
90 \\r*) out="${out}${CR}"; in="${in#\\r}" ;;
91 \\t*) out="${out} "; in="${in#\\t}" ;;
92 \\+*) out="${out}+"; in="${in#\\+}" ;;
93 +*) out="${out} "; in="${in#+}" ;;
95 *) out="${out}${in%%[\\+]*}"; in="${in#"${in%%[\\+]*}"}" ;;
101 local file="$1" cmd="$2"
108 while read -r k v; do if [ "$k" = "$key" ]; then
110 fi; done <"$file" 2>&-
115 while read -r k v; do if [ "$k" = "$key" ]; then
118 fi; done <"$file" 2>&-
122 key="$(STRING "$1")" value="$(STRING "$2")"
123 LOCK "$file" || return 1
124 { while read -r k v; do
125 [ "$k" = "$key" ] || printf '%s\t%s\n' "$k" "$v"
127 printf '%s\t%s\n' "$key" "$value"
129 mv "${file}.$$.tmp" "${file}"
134 k="$1" key="$(STRING "$1")" value="$(STRING "$2")"
135 LOCK "$file" || return 1
136 if DBM "$file" check "$k"; then
140 printf '%s\t%s\n' "$key" "$value" >>"${file}"
146 k="$1" key="$(STRING "$1")" value="$(STRING "$2")"
147 LOCK "$file" || return 1
148 if ! DBM "$file" check "$k"; then
152 { while read -r k v; do
154 && printf '%s\t%s\n' "$key" "$value" \
155 || printf '%s\t%s\n' "$k" "$v"
158 mv "${file}.$$.tmp" "${file}"
163 key="$(STRING "$1")" value="$(STRING "$2")"
164 LOCK "$file" || return 1
165 if ! DBM "$file" check "$1"; then
169 { while read -r k v; do
171 && printf '%s\t%s\n' "$key" "$v$value" \
172 || printf '%s\t%s\n' "$k" "$v"
175 mv "${file}.$$.tmp" "${file}"
181 LOCK "$file" || return 1
182 { while read -r k v; do
183 [ "$k" = "$key" ] || printf '%s\t%s\n' "$k" "$v"
186 mv "${file}.$$.tmp" "${file}"