commit bb0da8a4ed076ebe21547f0e789ae27f1f83047d
parent f0de085223ebb72ffe6c3d182725dcf69549f054
Author: hhvn <hayden@haydenvh.com>
Date: Tue, 2 Feb 2021 16:46:22 +0000
include/lock.sh include/userinfo.sh include/log.sh bin/interface.sh: implement db locking
Diffstat:
4 files changed, 25 insertions(+), 0 deletions(-)
diff --git a/bin/interface.sh b/bin/interface.sh
@@ -116,10 +116,12 @@ menu_list(){
anykey
break
}
+ lock $datadir/boards/$board/list
awk -v "data=1 0 $(date +%d-%b-%Y) $user $subject $(basename $file)" -F" " \
'BEGIN {print data}; $4 != "root" {print $1 + 1 "\t" $2 "\t" $3 "\t" $4 "\t" $5 "\t" $6 "\t" $7}' \
< $datadir/boards/$board/list > $datadir/boards/$board/list.new
mv $datadir/boards/$board/list.new $datadir/boards/$board/list
+ unlock $datadir/boards/$board/list
ok "message sent"
anykey
break
@@ -196,12 +198,14 @@ menu_read(){
break
}
lnum=$(echo "$(awk -v "file=$(echo "$ofile" | sed 's/ .*//')" -F" " '$6 == file' < $datadir/boards/$board/list | wc -l) - 1" | bc)
+ lock $datadir/boards/$board/list
awk -v "n=1" -v "file=$(basename "$file")" -v "lnum=$lnum" -v "user=$user" -v "date=$(date +%d-%b-%Y)" -v "ofile=$(echo "$ofile" | sed 's/ .*//')" -F" " '
$6 == ofile && $2 != lnum {print; n = $1}
$6 == ofile && $2 == lnum {print; print n "\t" lnum + 1 "\t" date "\t" user "\t_\t" ofile "\t" file}
$6 != ofile
' < $datadir/boards/$board/list > $datadir/boards/$board/list.new
mv $datadir/boards/$board/list.new $datadir/boards/$board/list
+ unlock $datadir/boards/$board/list
set_mode list
ok "message sent"
anykey
diff --git a/include/lock.sh b/include/lock.sh
@@ -0,0 +1,13 @@
+#!/bin/sh
+
+lock(){
+ while [ -f $1.lock ]
+ do
+ sleep 0.5
+ done
+ touch $1.lock
+}
+
+unlock(){
+ rm $1.lock
+}
diff --git a/include/log.sh b/include/log.sh
@@ -1,11 +1,15 @@
#!/bin/sh
+. $cwd/include/lock.sh
+
add_log(){
+ lock $datadir/log/log
awk -v "date=$(date +%Y-%d-%m)" -v "time=$(date +%H:%M)" -v "key=$1" -v "value=$2" -F" " '
$1 == date # only keep logs from today
END {printf("%s\t%s\t%s\t%s", date, time, key, value)}
' < $datadir/log/log > $datadir/log/log.new
mv $datadir/log/log.new $datadir/log/log
+ unlock $datadir/log/log
}
get_log(){
diff --git a/include/userinfo.sh b/include/userinfo.sh
@@ -1,7 +1,10 @@
#!/bin/sh
+. $cwd/include/lock.sh
+
userset(){
touch $datadir/users/$1
+ lock $datadir/users/$1
awk -v "var=$2" -v "val=$3" -F" " '
$1 == var {
;
@@ -15,6 +18,7 @@ userset(){
print var "\t" val
}' < $datadir/users/$1 > $datadir/users/$1.new 2>/dev/null
mv $datadir/users/$1.new $datadir/users/$1 2>/dev/null
+ unlock $datadir/users/$1
}
userget(){