]> git.plutz.net Git - cgilite/blob - storage.sh
much faster hex decode function
[cgilite] / storage.sh
1 #!/bin/sh
2
3 # Copyright 2018, 2019, 2021 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="${1}.lock" timeout="${2-20}" block
29
30   if [ \! -w "${lock%/*}" ] || [ -e "$lock" -a \! -f "$lock" ]; then
31     debug "Impossible to get lock: $lock"
32     return 1
33   fi
34
35   while [ $timeout -gt 0 ]; do
36     printf '%i\n' $$ >>"${lock}"
37     read block <"$lock"
38     if [ "$block" = $$ ]; then
39       return 0
40     elif ! { ps -eo pid |grep -qwF "$block"; }; then
41       debug "Trying to override stale lock: $lock"
42       if LOCK "$lock" 1; then
43         rm -- "$lock"
44         RELEASE "$lock"
45       fi
46     else
47       timeout=$((timeout - 1))
48       [ $timeout -gt 0 ] && sleep 1
49     fi
50   done
51
52   debug "Timeout while trying to get lock: $lock"
53   return 1
54 }
55
56 RELEASE(){
57   local lock="${1}.lock" block
58
59   read block <"$lock"
60   if [ "$block" = $$ ]; then
61     rm -- "$lock"
62     return 0
63   else
64     debug "Refusing to release foreign lock: $lock"
65     return 1
66   fi
67 }
68
69 # STRING='
70 #   s;\\;\\\\;g; s;\t;\\t;g;
71 #   s;\n;\\n;g;  s;\r;\\r;g;
72 #   s;\+;\\+;g;  s; ;+;g;
73 # '
74 STRING(){
75   local in out=''
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}   + ]*}"}" ;;
85   esac; done
86   printf '%s' "$out"
87 }
88
89 UNSTRING='
90   :UNSTRING_X
91   s;((^|[^\\])(\\\\)*)\\n;\1\n;g;
92   s;((^|[^\\])(\\\\)*)\\t;\1\t;g;
93   s;((^|[^\\])(\\\\)*)\\r;\1\r;g;
94   s;((^|[^\\])(\\\\)*)\+;\1 ;g;
95   tUNSTRING_X;
96   s;((^|[^\\])(\\\\)*)\\\+;\1+;g;
97   s;\\\\;\\;g;
98 '
99 UNSTRING(){
100   local in out=''
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%%[\\+]*}"}" ;;
111   esac; done
112   printf '%s' "$out"
113 }
114
115 DBM() {
116   local file="$1" cmd="$2"
117   local k v key value
118   shift 2;
119
120   case "$cmd" in
121     check|contains)
122       key="$(STRING "$1")"
123       while read -r k v; do if [ "$k" = "$key" ]; then
124         return 0
125       fi; done <"$file" 2>&-
126       return 1
127       ;;
128     get)
129       key="$(STRING "$1")"
130       while read -r k v; do if [ "$k" = "$key" ]; then
131         UNSTRING "$v"
132         return 0
133       fi; done <"$file" 2>&-
134       return 1
135       ;;
136     set|store)
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"
141         done <"$file" 2>&-
142         printf '%s\t%s\n' "$key" "$value"
143       } >"${file}.$$.tmp"
144       mv "${file}.$$.tmp" "${file}"
145       RELEASE "$file"
146       return 0
147       ;;
148     add|insert)
149       k="$1" key="$(STRING "$1")" value="$(STRING "$2")"
150       LOCK "$file" || return 1
151       if DBM "$file" check "$k"; then
152         RELEASE "$file"
153         return 1
154       else
155         printf '%s\t%s\n' "$key" "$value" >>"${file}"
156         RELEASE "$file"
157         return 0
158       fi
159       ;;
160     update|replace)
161       k="$1" key="$(STRING "$1")" value="$(STRING "$2")"
162       LOCK "$file" || return 1
163       if ! DBM "$file" check "$k"; then
164         RELEASE "$file"
165         return 1
166       fi
167       { while read -r k v; do
168           [ "$k" = "$key" ] \
169           && printf '%s\t%s\n' "$key" "$value" \
170           || printf '%s\t%s\n' "$k" "$v"
171         done <"$file" 2>&-
172       } >"${file}.$$.tmp"
173       mv "${file}.$$.tmp" "${file}"
174       RELEASE "$file"
175       return 0
176       ;;
177     append)
178       key="$(STRING "$1")" value="$(STRING "$2")"
179       LOCK "$file" || return 1
180       if ! DBM "$file" check "$1"; then
181         RELEASE "$file"
182         return 1
183       fi
184       { while read -r k v; do
185           [ "$k" = "$key" ] \
186           && printf '%s\t%s\n' "$key" "$v$value" \
187           || printf '%s\t%s\n' "$k" "$v"
188         done <"$file" 2>&-
189       } >"${file}.$$.tmp"
190       mv "${file}.$$.tmp" "${file}"
191       RELEASE "$file"
192       return 0
193       ;;
194     delete|remove)
195       key="$(STRING "$1")"
196       LOCK "$file" || return 1
197       { while read -r k v; do
198           [ "$k" = "$key" ] || printf '%s\t%s\n' "$k" "$v"
199         done <"$file" 2>&-
200       } >"${file}.$$.tmp"
201       mv "${file}.$$.tmp" "${file}"
202       RELEASE "$file"
203       return 0
204       ;;
205   esac
206 }