3 # Copyright 2018, 2019 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 timeout block
32 if [ \! -w "${lock%/*}" ] || [ -e "$lock" -a \! -f "$lock" ]; then
33 debug "Impossible to get lock: $lock"
37 while [ $timeout -gt 0 ]; do
38 printf '%i\n' $$ >>"${lock}"
40 if [ "$block" = $$ ]; then
42 elif ! { ps -eo pid |grep -qwF "$block"; }; then
43 debug "Overriding stale lock: $lock"
44 if LOCK "$lock" 1; then
49 timeout=$((timeout - 1))
54 debug "Timeout while trying to get lock: $lock"
63 if [ "$block" = $$ ]; then
67 debug "Refusing to release foreign lock: $lock"
73 # s;\\;\\\\;g; s;\t;\\t;g;
74 # s;\n;\\n;g; s;\r;\\r;g;
75 # s;\+;\\+;g; s; ;+;g;
79 [ $# -gt 0 ] && in="$*" || in="$(cat)"
80 while [ "$in" ]; do case $in in
81 \\*) out="${out}\\\\"; in="${in#\\}" ;;
82 "$BR"*) out="${out}\\n"; in="${in#${BR}}" ;;
83 "$CR"*) out="${out}\\r"; in="${in#${CR}}" ;;
84 " "*) out="${out}\\t"; in="${in# }" ;;
85 +*) out="${out}\\+"; in="${in#+}" ;;
86 " "*) out="${out}+"; in="${in# }" ;;
87 *) out="${out}${in%%[\\${CR}${BR} + ]*}"; in="${in#"${in%%[\\${BR}${CR} + ]*}"}" ;;
94 s;((^|[^\\])(\\\\)*)\\n;\1\n;g;
95 s;((^|[^\\])(\\\\)*)\\t;\1\t;g;
96 s;((^|[^\\])(\\\\)*)\\r;\1\r;g;
97 s;((^|[^\\])(\\\\)*)\+;\1 ;g;
99 s;((^|[^\\])(\\\\)*)\\\+;\1+;g;
104 [ $# -gt 0 ] && in="$*" || in="$(cat)"
105 while [ "$in" ]; do case $in in
106 \\\\*) out="${out}\\"; in="${in#\\\\}" ;;
107 \\n*) out="${out}${BR}"; in="${in#\\n}" ;;
108 \\r*) out="${out}${CR}"; in="${in#\\r}" ;;
109 \\t*) out="${out} "; in="${in#\\t}" ;;
110 \\+*) out="${out}+"; in="${in#\\+}" ;;
111 +*) out="${out} "; in="${in#+}" ;;
112 \\*) in="${in#\\}" ;;
113 *) out="${out}${in%%[\\+]*}"; in="${in#"${in%%[\\+]*}"}" ;;
119 local file="$1" cmd="$2"
126 while read -r k v; do if [ "$k" = "$key" ]; then
128 fi; done <"$file" 2>&-
133 while read -r k v; do if [ "$k" = "$key" ]; then
136 fi; done <"$file" 2>&-
140 key="$(STRING "$1")" value="$(STRING "$2")"
141 LOCK "$file" || return 1
142 { while read -r k v; do
143 [ "$k" = "$key" ] || printf '%s\t%s\n' "$k" "$v"
145 printf '%s\t%s\n' "$key" "$value"
147 mv "${file}.$$.tmp" "${file}"
152 k="$1" key="$(STRING "$1")" value="$(STRING "$2")"
153 LOCK "$file" || return 1
154 if DBM "$file" check "$k"; then
158 printf '%s\t%s\n' "$key" "$value" >>"${file}"
164 k="$1" key="$(STRING "$1")" value="$(STRING "$2")"
165 LOCK "$file" || return 1
166 if ! DBM "$file" check "$k"; then
170 { while read -r k v; do
172 && printf '%s\t%s\n' "$key" "$value" \
173 || printf '%s\t%s\n' "$k" "$v"
176 mv "${file}.$$.tmp" "${file}"
181 key="$(STRING "$1")" value="$(STRING "$2")"
182 LOCK "$file" || return 1
183 if ! DBM "$file" check "$1"; then
187 { while read -r k v; do
189 && printf '%s\t%s\n' "$key" "$v$value" \
190 || printf '%s\t%s\n' "$k" "$v"
193 mv "${file}.$$.tmp" "${file}"
199 LOCK "$file" || return 1
200 { while read -r k v; do
201 [ "$k" = "$key" ] || printf '%s\t%s\n' "$k" "$v"
204 mv "${file}.$$.tmp" "${file}"