commit c4c9689412bd998698783a3cdb2444c9da16f24b
parent 85dec595e3e730290609791697a46eb049975ad4
Author: Jaromil <jaromil@dyne.org>
Date: Thu, 27 Feb 2014 10:13:01 +0100
small code refactoring over locking and pids
Diffstat:
3 files changed, 20 insertions(+), 8 deletions(-)
diff --git a/src/zlibs/filters b/src/zlibs/filters
@@ -329,7 +329,9 @@ cat <<EOF >> $sieve
# whitelist
if header :contains "From" [
EOF
+
newlock $TMPDIR/whitelist.sieve.$id
+
cat <<EOF | ${SQL} -batch ${addressbook} \
>> $TMPDIR/whitelist.sieve.$id
SELECT email FROM whitelist;
@@ -337,7 +339,9 @@ EOF
typeset -alU whitelist
for i in `cat $TMPDIR/whitelist.sieve.$id`; do
whitelist+=($i); done
+
unlink $TMPDIR/whitelist.sieve.$id
+
c=${#whitelist}
for w in $whitelist; do
print -n "\"$w\"" >> $sieve
@@ -357,8 +361,6 @@ if header :is "X-Spam-Flag" "YES" {
EOF
-unlock $sieve
-
#### PROCMAIL
cat <<EOF >> $PROCMAILDIR/rc
@@ -465,5 +467,7 @@ cat <<EOF >> $sieve
fileinto "unsorted";
EOF
+unlock $sieve
+
return 0
} # end of update()
diff --git a/src/zlibs/helpers b/src/zlibs/helpers
@@ -138,7 +138,7 @@ open_folder() {
preview_file() {
case $OS in
GNU)
- sensible-browser "${PARAM}" &
+ xdg-open "${PARAM}" &
;;
MAC)
open -g "${PARAM}"
diff --git a/src/zlibs/locking b/src/zlibs/locking
@@ -48,11 +48,13 @@ newlock() { # create locked
pidcheck() { # check if lock belongs to us
if [ -r "${1}.pid" ]; then
+
lockpid="`cat ${1}.pid`"
- func "lockpid: $lockpid"
+ func "pidcheck: $lockpid"
if [ "$$" = "$lockpid" ]; then
func "${1} lock belongs to us"
else
+
error "Unlock attempt by multiple processes on `basename $1`"
{ test "$FORCE" = "1" } || { _prun=1
while [ "$_prun" = "1" ]; do
@@ -64,16 +66,19 @@ pidcheck() { # check if lock belongs to us
sleep 1; continue
else
act "Owner (${lockpid}) not running, taking over the lock"
- rm -f ${1}.pid; echo "$$" > ${1}.pid
+ rm -f ${1}.pid; print "$$" > ${1}.pid
_prun=0
fi
done
}
act "left behind by $lockpid - we ($$) will take over"
+
fi
- else
- act "left behind by unknown owner, we'll take it over"
- rm -f ${1}.pid; echo "$$" > ${1}.pid
+
+ else # pid file doesn't exists
+ func "no pid file found for: $1"
+ func "we will take it over"
+ print "$$" > ${1}.pid
fi
return 0
}
@@ -99,6 +104,9 @@ unlink() { # delete a file that we are locking
pidcheck $1
{ test $? = 0 } || { return 1 }
+ { test -r ${1}.unlink } && {
+ error "Ongoing unlink detected on $1"; return 0 }
+
# signal that is unlinking (for parallel operation)
touch ${1}.unlink
(