]> git.plutz.net Git - confetti/blob - cgilite/storage.sh
Merge commit 'fcfbf01dba9c932dbb66abffa8f65bf1c7a15081'
[confetti] / 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; s;\t;\\t;g;
72 #   s;\n;\\n;g;  s;\r;\\r;g;
73 #   s;\+;\\+;g;  s; ;+;g;
74 # '
75 STRING(){
76   local in out=''
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}   + ]*}"}" ;;
86   esac; done
87   printf '%s' "$out"
88 }
89
90 UNSTRING='
91   :UNSTRING_X
92   s;((^|[^\\])(\\\\)*)\\n;\1\n;g;
93   s;((^|[^\\])(\\\\)*)\\t;\1\t;g;
94   s;((^|[^\\])(\\\\)*)\\r;\1\r;g;
95   s;((^|[^\\])(\\\\)*)\+;\1 ;g;
96   tUNSTRING_X;
97   s;((^|[^\\])(\\\\)*)\\\+;\1+;g;
98   s;\\\\;\\;g;
99 '
100 UNSTRING(){
101   local in out=''
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%%[\\+]*}"}" ;;
112   esac; done
113   printf '%s' "$out"
114 }
115
116 DBM() {
117   local file="$1" cmd="$2"
118   local k v key value
119   shift 2;
120
121   case "$cmd" in
122     check|contains)
123       key="$(STRING "$1")"
124       while read -r k v; do if [ "$k" = "$key" ]; then
125         return 0
126       fi; done <"$file" 2>&-
127       return 1
128       ;;
129     get)
130       key="$(STRING "$1")"
131       while read -r k v; do if [ "$k" = "$key" ]; then
132         UNSTRING "$v"
133         return 0
134       fi; done <"$file" 2>&-
135       return 1
136       ;;
137     set|store)
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"
142         done <"$file" 2>&-
143         printf '%s\t%s\n' "$key" "$value"
144       } >"${file}.$$.tmp"
145       mv "${file}.$$.tmp" "${file}"
146       RELEASE "$file"
147       return 0
148       ;;
149     add|insert)
150       k="$1" key="$(STRING "$1")" value="$(STRING "$2")"
151       LOCK "$file" || return 1
152       if DBM "$file" check "$k"; then
153         RELEASE "$file"
154         return 1
155       else
156         printf '%s\t%s\n' "$key" "$value" >>"${file}"
157         RELEASE "$file"
158         return 0
159       fi
160       ;;
161     update|replace)
162       k="$1" key="$(STRING "$1")" value="$(STRING "$2")"
163       LOCK "$file" || return 1
164       if ! DBM check "$k"; then
165         RELEASE "$file"
166         return 1
167       fi
168       { while read -r k v; do
169           [ "$k" = "$key" ] \
170           && printf '%s\t%s\n' "$key" "$value" \
171           || printf '%s\t%s\n' "$k" "$v"
172         done <"$file" 2>&-
173       } >"${file}.$$.tmp"
174       mv "${file}.$$.tmp" "${file}"
175       RELEASE "$file"
176       return 0
177       ;;
178     append)
179       key="$(STRING "$1")" value="$(STRING "$2")"
180       LOCK "$file" || return 1
181       if ! DBM check "$1"; then
182         RELEASE "$file"
183         return 1
184       fi
185       { while read -r k v; do
186           [ "$k" = "$key" ] \
187           && printf '%s\t%s\n' "$key" "$v$value" \
188           || printf '%s\t%s\n' "$k" "$v"
189         done <"$file" 2>&-
190       } >"${file}.$$.tmp"
191       mv "${file}.$$.tmp" "${file}"
192       RELEASE "$file"
193       return 0
194       ;;
195     delete|remove)
196       key="$(STRING "$1")"
197       LOCK "$file" || return 1
198       { while read -r k v; do
199           [ "$k" = "$key" ] || printf '%s\t%s\n' "$k" "$v"
200         done <"$file" 2>&-
201       } >"${file}.$$.tmp"
202       mv "${file}.$$.tmp" "${file}"
203       RELEASE "$file"
204       return 0
205       ;;
206   esac
207 }