]> git.plutz.net Git - shellwiki/blob - session_lock.sh
Display attachment action button in tab container. Form field for attachment move...
[shellwiki] / session_lock.sh
1 #!/bin/sh
2
3 [ "$include_sessionlock" ] && return 0
4 include_sessionlock="$0"
5
6 . "$_EXEC/cgilite/storage.sh"
7 . "$_EXEC/cgilite/session.sh"
8
9 LOCK_TIMEOUT="${LOCK_TIMEOUT:-1200}"
10
11 S_LOCK(){
12   local file="$1" timeout="${2:-$LOCK_TIMEOUT}"
13   local date sid
14
15   printf "%i %s\n" "$_DATE" "$SESSION_ID" >>"${file}.lock"
16
17   if ! read date sid <"${file}.lock"; then
18     debug "Unable to access lock: ${file}.lock"
19
20   elif [ $((date + timeout)) -lt $_DATE ]; then
21     # Override stale lock
22     if LOCK "${file}.lock" 1; then
23       debug "Overriding stale lock: ${file}.lock"
24       printf "%i %s\n" "$_DATE" "$SESSION_ID" >"${file}.lock"
25       RELEASE "${file}.lock"
26       return 0
27     else
28       return 1
29     fi
30
31   elif [ "$sid" = "$SESSION_ID" -a "$date" -ne "$_DATE" ]; then
32     # Refresh aged lock
33     printf "%i %s\n" "$_DATE" "$SESSION_ID" >"${file}.lock"
34     return 0
35
36   elif [ "$sid" = "$SESSION_ID" ]; then
37     # Simple success
38     return 0
39
40   else
41     return 1
42   fi
43 }
44
45 S_RELEASE(){
46   local file="$1" timeout="${2:-$LOCK_TIMEOUT}"
47   local date sid
48
49   if ! read date sid <"${file}.lock"; then
50     # File was not locked
51     return 0
52
53   elif [ "$sid" = "$SESSION_ID" -a  $((date + timeout)) -lt $_DATE ]; then
54     # if lock is stale, protect against stale override before release
55     if LOCK "${file}.lock" 1; then
56       rm -- "${file}.lock"
57       RELEASE "${file}.lock"
58       return 0
59     else
60       return 1
61     fi
62
63   elif [ "$sid" = "$SESSION_ID" ]; then
64     # Simple success
65     rm -- "${file}.lock"
66     return 0
67
68   else
69     return 1
70   fi
71 }