X-Git-Url: http://git.plutz.net/?p=cgilite;a=blobdiff_plain;f=storage.sh;h=7f70e6480d6c89c8a32d3a9ee3fb68a5bfcc35b6;hp=bf8b7b9f44343d3c210c57800b5e606208f6e6b2;hb=f0383eeb634ce1ae71bfb409a9bc25d84b6d79e9;hpb=438b528a68c8638b7681e7dca95ee225d07df48d diff --git a/storage.sh b/storage.sh index bf8b7b9..7f70e64 100755 --- a/storage.sh +++ b/storage.sh @@ -1,6 +1,6 @@ #!/bin/sh -# Copyright 2018 Paul Hänsch +# Copyright 2018, 2019 Paul Hänsch # # This is a file format helper, part of CGIlite. # @@ -17,57 +17,113 @@ # You should have received a copy of the GNU Affero General Public License # along with CGIlite. If not, see . -# ksh and zsh workaround -case "${0##*/}" in - zsh) setopt -o OCTAL_ZEROES 2>&- ;; # zsh, mostly ignored by other shells - ksh) set -o posix ;; # ksh, will crash most other shells -esac +[ -n "$include_storage" ] && return 0 +include_storage="$0" -STORE(){ - # Store fields from a HTTP GET or POST string in a file - # usage: STORE "${QUERY_STRING}" - # or: STORE "$(head -n $HTTP_CONTENT_LENGTH)" - # backslashes and newline characters will be escaped in field values, - # the escape symbol is a backslash - # hexadecimal character descriptions (%00 - %FF) will be expanded - # the + character will be converted to [space] - # one line in the output corresponds to exactly one field, so you can - # use grep to filter which fields of a query should be stored - printf "$( - printf '%s' "$@" \ - | sed -r ':X; $bY; N; bX; :Y; - s;\+; ;g; s;(\\|5[Cc]);\\\\\\\\;g; s;(\n|%0[Aa]);\\\\n;g; s;(^|&)([^=]+)=;\1\2:;g; s;&;\n;g; - s;^\n+;;; s;$;\\n;; s;\n+;\n;g; - # Hexadecimal { %00 - %FF } will be transformed to octal { \000 - \377 } for posix printf - s;%[0123].;&\\0;g; s;%[4567].;&\\1;g; s;%[89AB].;&\\2;g; s;%[CDEF].;&\\3;g; - s;%[048C][0-7]\\.;&0;g; s;%[048C][89A-F]\\.;&1;g; s;%[159D][0-7]\\.;&2;g; s;%[159D][89A-F]\\.;&3;g; - s;%[26AE][0-7]\\.;&4;g; s;%[26AE][89A-F]\\.;&5;g; s;%[37BF][0-7]\\.;&6;g; s;%[37BF][89A-F]\\.;&7;g; - s;%.[08](\\..);\10;g; s;%.[19](\\..);\11;g; s;%.[2A](\\..);\12;g; s;%.[3B](\\..);\13;g; - s;%.[4C](\\..);\14;g; s;%.[5D](\\..);\15;g; s;%.[6E](\\..);\16;g; s;%.[7F](\\..);\17;g; - ' - )" +CR=" " +BR=' +' + +LOCK(){ + local lock timeout block + lock="${1}.lock" + timeout="${2-20}" + if [ \! -w "${lock%/*}" ] || [ -e "$lock" -a \! -d "$lock" ]; then + printf 'Impossible to get lock: %s\n' "$lock" >&2 + return 1 + fi + + while ! mkdir "$lock" 2>&-; do + block="$(cat "$lock/pid" || printf 1)" + if ! { ps -eo pid |grep -qwF "$block"; }; then + printf 'Overriding stale lock: %s\n' "$lock" >&2 + break + fi + if [ $timeout -le 0 ]; then + printf 'Timeout while trying to get lock: %s\n' "$lock" >&2 + return 1 + fi + timeout=$((timeout - 1)) + sleep 1 + done + printf '%i\n' $$ >"${lock}/pid" + return 0 +} + +RELEASE(){ + local lock + lock="${1}.lock" + if [ "$(cat "$lock/pid")" = "$$" ]; then + rm "$lock/pid" + if ! rmdir "$lock"; then + printf 'Cannot remove tainted lock: %s\n' "$lock" >&2 + printf '%i\n' $$ >"${lock}/pid" + return 1 + fi + return 0 + else + printf 'Refusing to release foreign lock: %s\n' "$lock" >&2 + return 1 + fi } -LOAD(){ - # read a file written by STORE and assign all fields to variables - # usage: eval "$(LOAD