]> git.plutz.net Git - cgilite/blob - storage.sh
a8484b0514891fe3e7d6f3cfdbb55fba655cb61a
[cgilite] / storage.sh
1 #!/bin/sh
2
3 # Copyright 2018, 2019 Paul Hänsch
4 #
5 # This is a file format helper, part of CGIlite.
6
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.
11
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.
16
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/>. 
19
20 [ -n "$include_storage" ] && return 0
21 include_storage="$0"
22
23 CR="\r"
24 BR='
25 '
26
27 LOCK(){
28   local lock timeout block
29   lock="${1}.lock"
30   timeout="${2-20}"
31   if [ \! -w "${lock%/*}" ] || [ -e "$lock" -a \! -d "$lock" ]; then
32     debug "Impossible to get lock: $lock"
33     return 1
34   fi
35
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"
40       break
41     fi
42     if [ $timeout -le 0 ]; then
43       debug "Timeout while trying to get lock: $lock"
44       return 1
45     fi
46     timeout=$((timeout - 1))
47     sleep 1
48   done
49   printf '%i\n' $$ >"${lock}/pid"
50   return 0
51 }
52
53 RELEASE(){
54   local lock
55   lock="${1}.lock"
56   if [ "$(cat "$lock/pid")" = "$$" ]; then
57     rm "$lock/pid"
58     if ! rmdir "$lock"; then
59       debug "Cannot remove tainted lock: $lock"
60       printf '%i\n' $$ >"${lock}/pid"
61       return 1
62     fi
63     return 0
64   else
65     debug "Refusing to release foreign lock: $lock"
66     return 1
67   fi
68 }
69
70 STRING='
71   s;\\;\\\\;g;
72   s;\n;\\n;g;
73   s;\t;\\t;g;
74   s;\r;\\r;g;
75   s;\+;\\+;g;
76   s; ;+;g;
77 '
78
79 STRING_OLD(){
80   { [ $# -eq 0 ] && cat || printf %s "$*"; } \
81   | sed -E ':X; $!{N;bX;}'"$STRING"
82 }
83
84 STRING(){
85   local in out=''
86   [ $# -gt 0 ] && in="$*" || in="$(cat)"
87   while [ "$in" ]; do case $in in
88     \\*) out="${out}\\\\"; in="${in#\\}" ;;
89     "$BR"*) out="${out}\\n"; in="${in#${BR}}" ;;
90     "$CR"*) out="${out}\\r"; in="${in#${CR}}" ;;
91     "   "*) out="${out}\\t"; in="${in#  }" ;;
92     +*) out="${out}\\+"; in="${in#+}" ;;
93     " "*) out="${out}+"; in="${in# }" ;;
94     *) out="${out}${in%%[\\${CR}${BR}   + ]*}"; in="${in#"${in%%[\\${BR}${CR}   + ]*}"}" ;;
95   esac; done
96   printf '%s' "$out"
97 }
98
99
100 UNSTRING='
101   :UNSTRING_X
102   s;((^|[^\\])(\\\\)*)\\n;\1\n;g;
103   s;((^|[^\\])(\\\\)*)\\t;\1\t;g;
104   s;((^|[^\\])(\\\\)*)\\r;\1\r;g;
105   s;((^|[^\\])(\\\\)*)\+;\1 ;g;
106   tUNSTRING_X;
107   s;((^|[^\\])(\\\\)*)\\\+;\1+;g;
108   s;\\\\;\\;g;
109 '
110 UNSTRING_OLD(){
111   { [ $# -eq 0 ] && cat || printf %s "$*"; } \
112   | sed -E "$UNSTRING"
113 }
114 UNSTRING(){
115   local in out=''
116   [ $# -gt 0 ] && in="$*" || in="$(cat)"
117   while [ "$in" ]; do case $in in
118     \\\\*) out="${out}\\"; in="${in#\\\\}" ;;
119     \\n*) out="${out}${BR}"; in="${in#\\n}" ;;
120     \\r*) out="${out}${CR}"; in="${in#\\r}" ;;
121     \\t*) out="${out}   "; in="${in#\\t}" ;;
122     \\+) out="${out}+"; in="${in#\\+}" ;;
123     +*) out="${out} "; in="${in#+}" ;;
124     \\*) in="${in#\\}" ;;
125     *) out="${out}${in%%[\\+]*}"; in="${in#"${in%%[\\+]*}"}" ;;
126   esac; done
127   printf '%s' "$out"
128 }
129
130 DBM() {
131   local file="$1" cmd="$2"
132   local k v key value
133   shift 2;
134
135   case "$cmd" in
136     check|contains)
137       key="$(STRING "$1")"
138       while read -r k v; do if [ "$k" = "$key" ]; then
139         return 0
140       fi; done <"$file" 2>&-
141       return 1
142       ;;
143     get)
144       key="$(STRING "$1")"
145       while read -r k v; do if [ "$k" = "$key" ]; then
146         UNSTRING "$v"
147         return 0
148       fi; done <"$file" 2>&-
149       return 1
150       ;;
151     set|store)
152       key="$(STRING "$1")" value="$(STRING "$2")"
153       LOCK "$file" || return 1
154       { while read -r k v; do
155           [ "$k" = "$key" ] || printf '%s\t%s\n' "$k" "$v"
156         done <"$file" 2>&-
157         printf '%s\t%s\n' "$key" "$value"
158       } >"${file}.$$.tmp"
159       mv "${file}.$$.tmp" "${file}"
160       RELEASE "$file"
161       return 0
162       ;;
163     add|insert)
164       k="$1" key="$(STRING "$1")" value="$(STRING "$2")"
165       LOCK "$file" || return 1
166       if DBM "$file" check "$k"; then
167         RELEASE "$file"
168         return 1
169       else
170         printf '%s\t%s\n' "$key" "$value" >>"${file}"
171         RELEASE "$file"
172         return 0
173       fi
174       ;;
175     update|replace)
176       k="$1" key="$(STRING "$1")" value="$(STRING "$2")"
177       LOCK "$file" || return 1
178       if ! DBM check "$k"; then
179         RELEASE "$file"
180         return 1
181       fi
182       { while read -r k v; do
183           [ "$k" = "$key" ] \
184           && printf '%s\t%s\n' "$key" "$value" \
185           || printf '%s\t%s\n' "$k" "$v"
186         done <"$file" 2>&-
187       } >"${file}.$$.tmp"
188       mv "${file}.$$.tmp" "${file}"
189       RELEASE "$file"
190       return 0
191       ;;
192     append)
193       key="$(STRING "$1")" value="$(STRING "$2")"
194       LOCK "$file" || return 1
195       if ! DBM check "$1"; then
196         RELEASE "$file"
197         return 1
198       fi
199       { while read -r k v; do
200           [ "$k" = "$key" ] \
201           && printf '%s\t%s\n' "$key" "$v$value" \
202           || printf '%s\t%s\n' "$k" "$v"
203         done <"$file" 2>&-
204       } >"${file}.$$.tmp"
205       mv "${file}.$$.tmp" "${file}"
206       RELEASE "$file"
207       return 0
208       ;;
209     delete|remove)
210       key="$(STRING "$1")"
211       LOCK "$file" || return 1
212       { while read -r k v; do
213           [ "$k" = "$key" ] || printf '%s\t%s\n' "$k" "$v"
214         done <"$file" 2>&-
215       } >"${file}.$$.tmp"
216       mv "${file}.$$.tmp" "${file}"
217       RELEASE "$file"
218       return 0
219       ;;
220   esac
221 }