From f5596618ff3e6819c547bf833388d25b9bb36293 Mon Sep 17 00:00:00 2001 From: Aleksey Veresov Date: Sun, 6 Dec 2020 16:57:48 +0300 Subject: . --- vsvs | 67 ++++++++++++++++++++++++++++++++++++------------------------- vsvs-get | 2 -- vsvs-parser | 15 ++++++++++++++ 3 files changed, 55 insertions(+), 29 deletions(-) delete mode 100755 vsvs-get create mode 100644 vsvs-parser diff --git a/vsvs b/vsvs index 7977ec3..f486ba0 100755 --- a/vsvs +++ b/vsvs @@ -1,31 +1,44 @@ #!/bin/bash -res="" -buf="" -key="" -while IFS="" read str -do - if [ -z "${str##* *}" ] - then - newkey="${str%% *}" - val="${str#* }" - else - newkey="${str}" - val="" - fi - if [ -z "${newkey}" ] + +# This is a parser of the Very Simple Values Storage. +# It has two arguments: an action and an object. +# Action is a string to be evaluated with key and its value provided. +action=$1 +# Object is like a path in filesystem (which pretty much it is), +# this is a string describing piece of data in storage. +object=$2 + +# Utility functions: +after () { test -z "${2##*$1*}" && echo -n "${2#*$1}" } + +# File processor: +pfile () { + # First of all, we need to lock the file. + lockfile="$base/proc/$proc/lock/$object" + mkdir -p -- "${lockfile%/*}" + echo $proc > $lockfile + lockpath="$path.lock" + if ln $lockfile $lockpath then - buf="${buf}"$'\n'"${val}" - else - if [ -n "${buf}" ] - then - eval $1 - fi - key="${newkey}" - buf="${val}" + # PARSER + rm $lockpath fi + rm $lockfile +} + +# First of all, we need to locate object in filesystem. +# To do so, we will get parts of the path from the beginning of object, +# until we will run out of object or meet the file in filesystem. +test "${object::1}" == "/" && path=$base || path="." +lastpath=$object +until test -n "$lastpath" +do + # Tear another part of $lastpath until slash into $path: + path="$path/${lastpath%%/*}" + lastpath=$(after / "$lastpath") + # If $path does not exist, the object does not exist too, + test -e "$path" || exit 1 + # otherwise it can be file, then we need to parse it and exit, + # or it can be a directory, then we need to repeat the process. + test -f "$path" && pfile "$action" "$object" "$path" "$lastpath"; exit 0 done -if [ -n "${buf}" ] -then - eval $1 -fi -echo -n "${res}" diff --git a/vsvs-get b/vsvs-get deleted file mode 100755 index b3ab53f..0000000 --- a/vsvs-get +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/bash -vsvs "if [ \"\${key}\" == \"$1\" ]; then res=\"\${buf}\"; fi" diff --git a/vsvs-parser b/vsvs-parser new file mode 100644 index 0000000..ba9c098 --- /dev/null +++ b/vsvs-parser @@ -0,0 +1,15 @@ +#!/bin/bash +after () { test -z "${2##*$1*}" && echo -n "${2#*$1}" } +key="" +value="" +while IFS="" read line +do + if test -z "${line%% *}" + then value="${value}"$'\n'"$(after " " "$line")" + else + eval $1 + key="${line%% *}" + value="$(after " " "$line")" + fi +done +eval $1 -- cgit v1.2.3