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
31 if [ \! -w "${lock%/*}" ] || [ -e "$lock" -a \! -d "$lock" ]; then
32 debug "Impossible to get lock: $lock"
36 while ! mkdir "$lock" 2>&-; do
37 block="$(cat "$lock/pid" || printf 1)"
38 if ! { ps -eo pid |grep -qwF "$block"; }; then
39 debug "Overriding stale lock: $lock"
42 if [ $timeout -le 0 ]; then
43 debug "Timeout while trying to get lock: $lock"
46 timeout=$((timeout - 1))
49 printf '%i\n' $$ >"${lock}/pid"
56 if [ "$(cat "$lock/pid")" = "$$" ]; then
58 if ! rmdir "$lock"; then
59 debug "Cannot remove tainted lock: $lock"
60 printf '%i\n' $$ >"${lock}/pid"
65 debug "Refusing to release foreign lock: $lock"
71 # s;\\;\\\\;g; s;\t;\\t;g;
72 # s;\n;\\n;g; s;\r;\\r;g;
73 # s;\+;\\+;g; s; ;+;g;
77 [ $# -gt 0 ] && in="$*" || in="$(cat)"
78 while [ "$in" ]; do case $in in
79 \\*) out="${out}\\\\"; in="${in#\\}" ;;
80 "$BR"*) out="${out}\\n"; in="${in#${BR}}" ;;
81 "$CR"*) out="${out}\\r"; in="${in#${CR}}" ;;
82 " "*) out="${out}\\t"; in="${in# }" ;;
83 +*) out="${out}\\+"; in="${in#+}" ;;
84 " "*) out="${out}+"; in="${in# }" ;;
85 *) out="${out}${in%%[\\${CR}${BR} + ]*}"; in="${in#"${in%%[\\${BR}${CR} + ]*}"}" ;;
92 s;((^|[^\\])(\\\\)*)\\n;\1\n;g;
93 s;((^|[^\\])(\\\\)*)\\t;\1\t;g;
94 s;((^|[^\\])(\\\\)*)\\r;\1\r;g;
95 s;((^|[^\\])(\\\\)*)\+;\1 ;g;
97 s;((^|[^\\])(\\\\)*)\\\+;\1+;g;
102 [ $# -gt 0 ] && in="$*" || in="$(cat)"
103 while [ "$in" ]; do case $in in
104 \\\\*) out="${out}\\"; in="${in#\\\\}" ;;
105 \\n*) out="${out}${BR}"; in="${in#\\n}" ;;
106 \\r*) out="${out}${CR}"; in="${in#\\r}" ;;
107 \\t*) out="${out} "; in="${in#\\t}" ;;
108 \\+*) out="${out}+"; in="${in#\\+}" ;;
109 +*) out="${out} "; in="${in#+}" ;;
110 \\*) in="${in#\\}" ;;
111 *) out="${out}${in%%[\\+]*}"; in="${in#"${in%%[\\+]*}"}" ;;
117 local file="$1" cmd="$2"
124 while read -r k v; do if [ "$k" = "$key" ]; then
126 fi; done <"$file" 2>&-
131 while read -r k v; do if [ "$k" = "$key" ]; then
134 fi; done <"$file" 2>&-
138 key="$(STRING "$1")" value="$(STRING "$2")"
139 LOCK "$file" || return 1
140 { while read -r k v; do
141 [ "$k" = "$key" ] || printf '%s\t%s\n' "$k" "$v"
143 printf '%s\t%s\n' "$key" "$value"
145 mv "${file}.$$.tmp" "${file}"
150 k="$1" key="$(STRING "$1")" value="$(STRING "$2")"
151 LOCK "$file" || return 1
152 if DBM "$file" check "$k"; then
156 printf '%s\t%s\n' "$key" "$value" >>"${file}"
162 k="$1" key="$(STRING "$1")" value="$(STRING "$2")"
163 LOCK "$file" || return 1
164 if ! DBM "$file" check "$k"; then
168 { while read -r k v; do
170 && printf '%s\t%s\n' "$key" "$value" \
171 || printf '%s\t%s\n' "$k" "$v"
174 mv "${file}.$$.tmp" "${file}"
179 key="$(STRING "$1")" value="$(STRING "$2")"
180 LOCK "$file" || return 1
181 if ! DBM "$file" check "$1"; then
185 { while read -r k v; do
187 && printf '%s\t%s\n' "$key" "$v$value" \
188 || printf '%s\t%s\n' "$k" "$v"
191 mv "${file}.$$.tmp" "${file}"
197 LOCK "$file" || return 1
198 { while read -r k v; do
199 [ "$k" = "$key" ] || printf '%s\t%s\n' "$k" "$v"
202 mv "${file}.$$.tmp" "${file}"