]> git.plutz.net Git - confetti/blob - session_lock.sh
vcard exporter
[confetti] / session_lock.sh
1 #!/bin/sh
2
3 [ "$include_session_lock" ] && return 0
4 include_session_lock="$0"
5
6 SLOCK(){
7   local file="$1";
8   local timeout="${2-900}"
9   local lockdir="$_DATA/lock/${file#$_DATA}"; lockdir="${lockdir%/}"
10   local ovlock="${lockdir%/*}/delete.${lockdir##*/}"
11   local tempfile="$lockdir/${SESSION_ID}"
12   local lockexpire=$(( $(date +%s) - timeout ))
13
14   mkdir -p "$_DATA/lock/${file%/*}"
15
16   if [ -e "$lockdir" ] \
17      && [ "$(stat -c %Y "$lockdir")" -lt "$lockexpire" ] \
18      && mkdir "$ovlock"; then
19     [ "$(stat -c %Y "$lockdir")" -lt "$lockexpire" ] \
20     && rm -r "$lockdir"
21     rmdir "$ovlock"
22   fi
23
24   printf '%s\n' "$tempfile"
25   if mkdir "$lockdir" 2>&-; then
26     cp "$file" "$tempfile"
27     return 0
28   else
29     return 1
30   fi
31 }
32
33 CHECK_SLOCK(){
34   local file="$1";
35   local lockdir="$_DATA/lock/${file#$_DATA}"; lockdir="${lockdir%/}"
36   local tempfile="$lockdir/${SESSION_ID}"
37
38   printf '%s\n' "$tempfile"
39   if [ -f "$tempfile" ]; then
40     touch "$lockdir"
41     return 0
42   else
43     return 1
44   fi
45 }
46
47 RELEASE_SLOCK(){
48   local file="$1";
49   local lockdir="$_DATA/lock/${file#$_DATA}"; lockdir="${lockdir%/}"
50   local ovlock="${lockdir%/*}/delete.${lockdir##*/}"
51   local tempfile="$lockdir/${SESSION_ID}"
52
53   if [ -f "$tempfile" ] && mkdir "$ovlock"; then
54     [ -f "$tempfile" ] && rm -r "$lockdir"
55     rmdir "$ovlock"
56     return 0
57   else
58     return 1
59   fi
60 }