/adei/ui

To get this branch, use:
bzr branch http://darksoft.org/webbzr/adei/ui

« back to all changes in this revision

Viewing changes to classes/lock.php

  • Committer: Suren A. Chilingaryan
  • Date: 2008-06-17 23:19:26 UTC
  • Revision ID: csa@dside.dyndns.org-20080617231926-w9mpfxw6lv0r0450
Administrative interface and better handling of missing group channels

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 
3
3
class LOCK {
4
4
 var $lockf;
 
5
 var $rlock;
5
6
 
6
7
 const BLOCK = 1;
 
8
 const ALL = 2;
7
9
 
8
10
 function __construct($name) {
9
11
    global $TMP_PATH;
12
14
    $dir = $TMP_PATH . "/locks";
13
15
 
14
16
    if (!is_dir($dir)) {
15
 
        if (!@mkdir($dir, 0755, true)) 
 
17
        if (!@mkdir($dir, 0777 /*0755*/, true)) 
16
18
            throw new ADEIException(translate("It is not possible to create lock directory \"$dir\""));
17
19
    }
18
20
    
20
22
    else $fname = $dir . "/ADEI__${name}.lock";
21
23
 
22
24
    $this->lockf = @fopen($fname, "a+");
23
 
    if (!$this->lockf)
24
 
        throw new ADEIException(translate("It is not possible to create lock file \"$fname\""));
 
25
    if (!$this->lockf) {
 
26
        throw new ADEIException(translate("It is not possible to create lock file \"$fname\""));
 
27
    }
 
28
 
 
29
    $fname = $dir . "/${name}.lock";
 
30
    $this->rlock = @fopen($fname, "a+");
 
31
    if (!$this->rlock) {
 
32
        fclose($this->lockf);
 
33
        throw new ADEIException(translate("It is not possible to create lock file \"$fname\""));
 
34
    }
25
35
 }
26
36
 
27
37
 function __destruct() {
28
38
    fclose($this->lockf);
 
39
    fclose($this->rlock);
29
40
 }
30
 
 
 
41
 
31
42
 function Lock($flag = 0, $errmsg = false) {
32
43
    if ($flag&LOCK::BLOCK) {
 
44
        
 
45
        if ($flag&LOCK::ALL)
 
46
            $res = flock($this->rlock, LOCK_EX);
 
47
        else
 
48
            $res = flock($this->rlock, LOCK_SH);
 
49
 
 
50
        if (!$res) {
 
51
            if ($errmsg) throw new ADEIException($errmsg);
 
52
            else throw new ADEIException(translate("Locking is failed"));
 
53
        }
 
54
        
33
55
        $res = flock($this->lockf, LOCK_EX);
34
56
        if (!$res) {
 
57
            flock($this->rlock,  LOCK_UN);
 
58
            
35
59
            if ($errmsg) throw new ADEIException($errmsg);
36
60
            else throw new ADEIException(translate("Locking is failed"));
37
61
        }
38
62
    } else {
 
63
        if ($flag&LOCK::ALL)
 
64
            $res = flock($this->rlock, LOCK_EX|LOCK_NB);
 
65
        else
 
66
            $res = flock($this->rlock, LOCK_SH|LOCK_NB);
 
67
 
 
68
        if ((!$res)&&($errmsg)) throw new ADEIException($errmsg);
 
69
 
39
70
        $res = flock($this->lockf, LOCK_EX|LOCK_NB);
40
 
        if ((!$res)&&($errmsg)) throw new ADEIException($errmsg);
 
71
        if (!$res) {
 
72
            flock($this->rlock, LOCK_UN);
 
73
            if ($errmsg) throw new ADEIException($errmsg);
 
74
        }
41
75
    }
42
76
    
43
77
    return $res;
45
79
 
46
80
 function UnLock() {
47
81
    flock($this->lockf, LOCK_UN);
 
82
    flock($this->rlock, LOCK_UN);
48
83
 }
49
84
}
50
85