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"
70 # s;\\;\\\\;g; s;\t;\\t;g;
71 # s;\n;\\n;g; s;\r;\\r;g;
72 # s;\+;\\+;g; s; ;+;g;
76 [ $# -gt 0 ] && in="$*" || in="$(cat)"
77 while [ "$in" ]; do case $in in
78 \\*) out="${out}\\\\"; in="${in#\\}" ;;
79 "$BR"*) out="${out}\\n"; in="${in#${BR}}" ;;
80 "$CR"*) out="${out}\\r"; in="${in#${CR}}" ;;
81 " "*) out="${out}\\t"; in="${in# }" ;;
82 +*) out="${out}\\+"; in="${in#+}" ;;
83 " "*) out="${out}+"; in="${in# }" ;;
84 *) out="${out}${in%%[\\${CR}${BR} + ]*}"; in="${in#"${in%%[\\${BR}${CR} + ]*}"}" ;;
91 s;((^|[^\\])(\\\\)*)\\n;\1\n;g;
92 s;((^|[^\\])(\\\\)*)\\t;\1\t;g;
93 s;((^|[^\\])(\\\\)*)\\r;\1\r;g;
94 s;((^|[^\\])(\\\\)*)\+;\1 ;g;
96 s;((^|[^\\])(\\\\)*)\\\+;\1+;g;
101 [ $# -gt 0 ] && in="$*" || in="$(cat)"
102 while [ "$in" ]; do case $in in
103 \\\\*) out="${out}\\"; in="${in#\\\\}" ;;
104 \\n*) out="${out}${BR}"; in="${in#\\n}" ;;
105 \\r*) out="${out}${CR}"; in="${in#\\r}" ;;
106 \\t*) out="${out} "; in="${in#\\t}" ;;
107 \\+*) out="${out}+"; in="${in#\\+}" ;;
108 +*) out="${out} "; in="${in#+}" ;;
109 \\*) in="${in#\\}" ;;
110 *) out="${out}${in%%[\\+]*}"; in="${in#"${in%%[\\+]*}"}" ;;
116 local file="$1" cmd="$2"
123 while read -r k v; do if [ "$k" = "$key" ]; then
125 fi; done <"$file" 2>&-
130 while read -r k v; do if [ "$k" = "$key" ]; then
133 fi; done <"$file" 2>&-
137 key="$(STRING "$1")" value="$(STRING "$2")"
138 LOCK "$file" || return 1
139 { while read -r k v; do
140 [ "$k" = "$key" ] || printf '%s\t%s\n' "$k" "$v"
142 printf '%s\t%s\n' "$key" "$value"
144 mv "${file}.$$.tmp" "${file}"
149 k="$1" key="$(STRING "$1")" value="$(STRING "$2")"
150 LOCK "$file" || return 1
151 if DBM "$file" check "$k"; then
155 printf '%s\t%s\n' "$key" "$value" >>"${file}"
161 k="$1" key="$(STRING "$1")" value="$(STRING "$2")"
162 LOCK "$file" || return 1
163 if ! DBM "$file" check "$k"; then
167 { while read -r k v; do
169 && printf '%s\t%s\n' "$key" "$value" \
170 || printf '%s\t%s\n' "$k" "$v"
173 mv "${file}.$$.tmp" "${file}"
178 key="$(STRING "$1")" value="$(STRING "$2")"
179 LOCK "$file" || return 1
180 if ! DBM "$file" check "$1"; then
184 { while read -r k v; do
186 && printf '%s\t%s\n' "$key" "$v$value" \
187 || printf '%s\t%s\n' "$k" "$v"
190 mv "${file}.$$.tmp" "${file}"
196 LOCK "$file" || return 1
197 { while read -r k v; do
198 [ "$k" = "$key" ] || printf '%s\t%s\n' "$k" "$v"
201 mv "${file}.$$.tmp" "${file}"