*IMPORTANT* fix for ranges a-z dependig on locale!

This commit is contained in:
Kay Marquardt (Gnadelwartz) 2021-01-04 12:38:20 +01:00
parent 83fc57e46c
commit 5ad7be59ba
1 changed files with 24 additions and 12 deletions

View File

@ -5,7 +5,7 @@
# This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
#
#### $$VERSION$$ v1.21-0-gc85af77
#### $$VERSION$$ v1.21-1-g83fc57e
#
# source from commands.sh to use jsonDB functions
#
@ -27,6 +27,18 @@ eval "$(basename "${BASH_SOURCE[0]}")(){ :; }"
# lockfile filename.flock is persistent and will be testet with flock for active lock (file open)
export JSSH_LOCKNAME=".flock"
# in UTF-8 äöü etc. are part of [:alnum:] and rages (e.g. a-z), but we want ASCII a-z ranges!
# there are two solutions: set "LC_COLLATE=C" or set bash option "globasciiranges"
# to be independent of the settings mentioned above, we define our own "ranges"
azazaz='abcdefghijklmnopqrstuvwxyz' # a-z :lower:
AZAZAZ='ABCDEFGHIJKLMNOPQRSTUVWXYZ' # A-Z :upper:
R090909='0123456789' # 0-9 :digit:
azAZaz="${azazaz}${AZAZAZ}" # a-zA-Z :alpha:
azAZ09="${azAZaz}${R090909}" # a-zA-z0-9 :alnum:
# characters allowed for key in key/value pairs
JSSH_KEYOK="[-${azAZ09},._]"
# use flock if command exist
if [ "$(LC_ALL=C type -t "flock")" = "file" ]; then
@ -76,7 +88,7 @@ if [ "$(LC_ALL=C type -t "flock")" = "file" ]; then
alias jssh_insertDB=jssh_insertKeyDB # backward compatibility
# renamed to be more consistent
jssh_insertKeyDB() {
[[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3
[[ "$1" =~ ^${JSSH_KEYOK}+$ ]] || return 3
local DB; DB="$(jssh_checkDB "$3")"
[ -z "${DB}" ] && return 1
[ ! -f "${DB}" ] && return 2
@ -94,7 +106,7 @@ if [ "$(LC_ALL=C type -t "flock")" = "file" ]; then
# medium complex slow, wrapper async
jssh_deleteKeyDB() {
[ -z "${2}" ] && return 1
[[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3
[[ "$1" =~ ^${JSSH_KEYOK}+$ ]] || return 3
local DB="${2}.jssh"
# start atomic delete here, exclusive max wait 10s
{ flock -e -w 10 200; jssh_deleteKeyDB_async "$@"; } 200>"${DB}${JSSH_LOCKNAME}"
@ -105,7 +117,7 @@ if [ "$(LC_ALL=C type -t "flock")" = "file" ]; then
# $2 filename (must exist!), must be relative to BASHBOT_ETC, and not contain '..'
alias jssh_getDB=jssh_getKeyDB
jssh_getKeyDB() {
[[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3
[[ "$1" =~ ^${JSSH_KEYOK}+$ ]] || return 3
local DB; DB="$(jssh_checkDB "$2")"
[ -z "${DB}" ] && return 1
# start atomic delete here, exclusive max wait 1s
@ -123,7 +135,7 @@ if [ "$(LC_ALL=C type -t "flock")" = "file" ]; then
# complex, wrapper to async
jssh_countKeyDB() {
[ -z "${2}" ] && return 1
[[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3
[[ "$1" =~ ^${JSSH_KEYOK}+$ ]] || return 3
local DB="${2}.jssh"
# start atomic delete here, exclusive max wait 5
{ flock -e -w 5 200; jssh_countKeyDB_async "$@"; } 200>"${DB}${JSSH_LOCKNAME}"
@ -135,7 +147,7 @@ if [ "$(LC_ALL=C type -t "flock")" = "file" ]; then
# $3 filename (must exist!), must be relative to BASHBOT_ETC, and not contain '..'
#no own locking, so async is the same as updatekeyDB
jssh_updateKeyDB() {
[[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3
[[ "$1" =~ ^${JSSH_KEYOK}+$ ]] || return 3
[ -z "${3}" ] && return 1
declare -A updARR
# shellcheck disable=SC2034
@ -253,7 +265,7 @@ jssh_updateDB_async() {
jssh_insertDB_async() { jssh_insertKeyDB "$@"; }
jssh_insertKeyDB_async() {
[[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3
[[ "$1" =~ ^${JSSH_KEYOK}+$ ]] || return 3
local DB; DB="$(jssh_checkDB "$3")"
[ -z "${DB}" ] && return 1
[ ! -f "${DB}" ] && return 2
@ -263,7 +275,7 @@ jssh_insertKeyDB_async() {
}
jssh_deleteKeyDB_async() {
[[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3
[[ "$1" =~ ^${JSSH_KEYOK}+$ ]] || return 3
local DB; DB="$(jssh_checkDB "$2")"
[ -z "${DB}" ] && return 1
declare -A oldARR
@ -273,14 +285,14 @@ jssh_deleteKeyDB_async() {
}
jssh_getKeyDB_async() {
[[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3
[[ "$1" =~ ^${JSSH_KEYOK}+$ ]] || return 3
local DB; DB="$(jssh_checkDB "$2")"
[ -z "${DB}" ] && return 1
[ -r "${DB}" ] && sed -n 's/\["'"$1"'"\]\t*"\(.*\)"/\1/p' <"${DB}" | tail -n 1
}
jssh_countKeyDB_async() {
[[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3
[[ "$1" =~ ^${JSSH_KEYOK}+$ ]] || return 3
local VAL DB; DB="$(jssh_checkDB "$2")"
[ -z "${DB}" ] && return 1
# start atomic delete here, exclusive max wait 5
@ -302,7 +314,7 @@ jssh_countKeyDB_async() {
# $3 filename (must exist!), must be relative to BASHBOT_ETC, and not contain '..'
#no own locking, so async is the same as updatekeyDB
jssh_updateKeyDB_async() {
[[ "$1" =~ ^[-a-zA-Z0-9,._]+$ ]] || return 3
[[ "$1" =~ ^${JSSH_KEYOK}+$ ]] || return 3
[ -z "${3}" ] && return 1
declare -A updARR
# shellcheck disable=SC2034
@ -344,9 +356,9 @@ Array2Json() {
declare -n ARRAY="$1"
for key in "${!ARRAY[@]}"
do
#[[ "${key}" =~ ^${JSSH_KEYOK}$ ]] || continue
# in case val contains newline convert to \n
val="${ARRAY[${key}]//$'\n'/\\n}"
printf '["%s"]\t"%s"\n' "${key//,/\",\"}" "${val//\"/\\\"}"
done
}