From: Paul Hänsch Date: Tue, 16 Nov 2021 00:29:06 +0000 (+0100) Subject: unified table function X-Git-Url: https://git.plutz.net/?a=commitdiff_plain;h=6cd76fcea5aad8585f307b4fc5b75bd2da746993;p=serve0 unified table function --- diff --git a/db_meta.sh b/db_meta.sh new file mode 100755 index 0000000..e78bcc5 --- /dev/null +++ b/db_meta.sh @@ -0,0 +1,156 @@ +#!/bin/sh + +[ "$include_dbmeta" ] && return 0 +include_dbmeta="$0" + +# == FILE FORMAT == +# LENGTH WIDTH HEIGHT TAGS COMMENT NAME + +# == GLOBALS == +UNSET_META='unset \ + META_LENGTH META_WIDTH META_HEIGHT META_TAGS META_COMMENT META_NAME +' + +LOCAL_META='local \ + META_LENGTH META_WIDTH META_HEIGHT META_TAGS META_COMMENT META_NAME +' + +eval "$UNSET_VIDEO" + +read_meta() { + local name="$1" meta_db="$_DATA/.index/meta" + + [ "${name%%/*}" != "$name" ] \ + && meta_db="$_DATA/${name%%/*}/.index/meta" + name="$(STRING "${name##*/}")" + + # Global exports + META_LENGTH='' META_WIDTH='' META_HEIGHT='' META_TAGS='' + META_COMMENT='' META_NAME='' + + if [ $# -eq 0 ]; then + read -r META_LENGTH META_WIDTH META_HEIGHT META_TAGS META_COMMENT META_NAME + elif [ "$name" -a -f "$meta_db" -a -r "$meta_db" ]; then + while read -r META_LENGTH META_WIDTH META_HEIGHT META_TAGS META_COMMENT META_NAME; do + [ "${META_NAME%${CR}}" = "$name" ] && break + done <"${meta_db}" + [ "${META_NAME%${CR}}" = "$name" ] || eval "$UNSET_META" + fi + if [ "$META_NAME" ]; then + META_NAME="$(UNSTRING "${META_NAME%${CR}}")" + META_COMMENT="$(UNSTRING "${META_COMMENT#comment=}")" + META_TAGS="$(UNSTRING "${META_TAGS#tags=}")" + else + eval "$UNSET_META" + return 1 + fi +} + +update_meta(){ + local name="${1:=${META_NAME}}" tags comment length width height + eval LENGTH WIDTH HEIGH TAGS COMMENT NAME + local arg cnt meta_db="$_DATA/.index/meta" + + [ "${name%%/*}" != "$name" ] \ + && meta_db="$_DATA/${name%%/*}/.index/meta" + name="$(STRING "${name##*/}")" + + for arg in "$@"; do case $arg in + comment=*) comment="${arg#*=}";; + tags=*) tags="${arg#*=}";; + lenght=*) lenght="${arg#*=}";; + width=*) width="${arg#*=}";; + height=*) height="${arg#*=}";; + esac; done + + if LOCK "$meta_db"; then + while read -r LENGTH WIDTH HEIGHT TAGS COMMENT NAME; do + if [ "$name" = "${NAME%${CR}}" ]; then + printf '%i %i %i tags=%s comment=%s %s\r\n' \ + "${length:-${length-${LENGTH}}${length+0}}" \ + "${width:-${width-${WIDTH}}${width+0}}" \ + "${height:-${height-${HEIGHT}}${height+0}}" \ + "$(STRING "${tags-$(UNSTRING "${TAGS#tags=}")}")" \ + "$(STRING "${comment-$(UNSTRING "${COMMENT#comment=}")}")" \ + "$(STRING "${name-$(UNSTRING "${NAME%${CR}}")}")" + else + printf '%i %i %i tags=%s comment=%s %s\r\n' \ + "$length" "$width" "$height" "${TAGS#tags=}" \ + "${COMMENT#comment=}" "${NAME%${CR}}" + fi + done <"$meta_db" >"${meta_db}.$$" + mv -- "${meta_db}.$$" "${meta_db}" + RELEASE "$meta_db" + else + return 1 + fi +} + +new_meta(){ + local name="$1" meta_db="$_DATA/.index/meta" + local LENGTH WIDTH HEIGHT TAGS COMMENT NAME + + [ "${name%%/*}" != "$name" ] \ + && meta_db="$_DATA/${name%%/*}/.index/meta" + name="$(STRING "${name##*/}")" + + if LOCK "$meta_db"; then + while read -r LENGTH WIDTH HEIGHT TAGS COMMENT NAME; do + if [ "$name" = "${NAME%${CR}}" ]; then + RELEASE "$vid_db" + return 1 + fi + done <"$meta_db" + printf '0 0 0 tags=\\ comment=\\ %s\r\n' \ + "${name}" >>"$meta_db" + RELEASE "$meta_db" + else + return 1 + fi +} + +delete_meta() { + local name="$1" meta_db="$_DATA/.index/meta" + local LENGTH WIDTH HEIGHT TAGS COMMENT NAME + + [ "${name%%/*}" != "$name" ] \ + && meta_db="$_DATA/${name%%/*}/.index/meta" + name="$(STRING "${name##*/}")" + + if LOCK "$meta_db"; then + while read -r LENGTH WIDTH HEIGHT TAGS COMMENT NAME; do + [ "$name" = "${NAME%${CR}}" ] \ + || printf '%i %i %i tags=%s comment=%s %s\r\n' \ + "$length" "$width" "$height" "${TAGS#tags=}" \ + "${COMMENT#comment=}" "${NAME%${CR}}" + done <"$meta_db" >"${meta_db}.$$" + + mv -- "${meta_db}.$$" "$meta_db" + RELEASE "$meta_db" + else + return 1 + fi +} + +list_meta(){ + local meta pfx + local LENGTH WIDTH HEIGHT TAGS COMMENT NAME + + if [ "$#" -eq 0 ]; then + find "$_DATA" -path '*/.index/meta' + else + printf %s\\n "$@" + fi \ + | while read meta; do + pfx="${meta#$_DATA}" + pfx="${pfx%/.index/meta}" + pfx="$(STRING "${pfx#/}")" + [ "$pfx" = '\' ] && pfx='' || pfx="${pfx}/" + + while read -r LENGTH WIDTH HEIGHT TAGS COMMENT NAME; do + printf '%i %i %i tags=%s comment=%s %s%s\r\n' \ + $LENGTH $WIDTH $HEIGHT "${TAGS#tags=}" \ + "${COMMENT#comment=}" "${pfx}" "${NAME%${CR}}" + done <"$meta" + done +}