diff options
author | Aleksey Veresov <aleksey@veresov.pro> | 2020-12-06 16:57:48 +0300 |
---|---|---|
committer | Aleksey Veresov <aleksey@veresov.pro> | 2020-12-06 16:57:48 +0300 |
commit | f5596618ff3e6819c547bf833388d25b9bb36293 (patch) | |
tree | 7a47b6a3ba030e525d20713de428c14d06b6b9e4 | |
parent | f229b632cbc31c5efad109589cd36b07bcd48817 (diff) | |
download | vsvs-f5596618ff3e6819c547bf833388d25b9bb36293.tar vsvs-f5596618ff3e6819c547bf833388d25b9bb36293.tar.xz vsvs-f5596618ff3e6819c547bf833388d25b9bb36293.zip |
.
-rwxr-xr-x | vsvs | 67 | ||||
-rwxr-xr-x | vsvs-get | 2 | ||||
-rw-r--r-- | vsvs-parser | 15 |
3 files changed, 55 insertions, 29 deletions
@@ -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 |