/adei/trunk

To get this branch, use:
bzr branch http://darksoft.org/webbzr/adei/trunk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php

class LOCK {
 var $lockf;
 var $rlock;
 
 const BLOCK = 1;               // Blocking wait or report error
 const ALL = 2;                 // Lock for all setups or just for the current
 const EXCLUSIVE = 2;           // Exclusive lock
 
 function __construct($name) {
    global $TMP_PATH;
    global $ADEI_SETUP;
    
    $dir = $TMP_PATH . "/locks";

    if (!is_dir($dir)) {
    	if (!@mkdir($dir, 0777 /*0755*/, true))
	    throw new ADEIException(translate("It is not possible to create lock directory \"$dir\""));

# When creating from apache, the 0777 mode is ignored for unknown reason	
	@chmod($dir, 0777);
    }
    
    if ($ADEI_SETUP) $fname = $dir . "/${ADEI_SETUP}__${name}.lock";
    else $fname = $dir . "/ADEI__${name}.lock";

    $umask = @umask(0);

    $this->lockf = @fopen($fname, "a+");
    if (!$this->lockf) {
	@umask($umask);
	throw new ADEIException(translate("It is not possible to create lock file \"$fname\""));
    }

    $fname = $dir . "/${name}.lock";
    $this->rlock = @fopen($fname, "a+");
    if (!$this->rlock) {
	fclose($this->lockf);
        @umask($umask);
	throw new ADEIException(translate("It is not possible to create lock file \"$fname\""));
    }

    @umask($umask);
 }

 function __destruct() {
    fclose($this->lockf);
    fclose($this->rlock);
 }

 function Lock($flag = 0, $errmsg = false) {
    if ($flag&LOCK::BLOCK) {
	
	if ($flag&LOCK::ALL)
	    $res = flock($this->rlock, LOCK_EX);
	else
	    $res = flock($this->rlock, LOCK_SH);

	if (!$res) {
	    if ($errmsg) throw new ADEIException($errmsg);
	    else throw new ADEIException(translate("Locking is failed"));
	}
	
	$res = flock($this->lockf, LOCK_EX);
        if (!$res) {
	    flock($this->rlock,  LOCK_UN);
	    
	    if ($errmsg) throw new ADEIException($errmsg);
	    else throw new ADEIException(translate("Locking is failed"));
	}
    } else {
	if ($flag&LOCK::ALL)
	    $res = flock($this->rlock, LOCK_EX|LOCK_NB);
	else
	    $res = flock($this->rlock, LOCK_SH|LOCK_NB);

        if ((!$res)&&($errmsg)) throw new ADEIException($errmsg);

	$res = flock($this->lockf, LOCK_EX|LOCK_NB);
        if (!$res) {
	    flock($this->rlock, LOCK_UN);
	    if ($errmsg) throw new ADEIException($errmsg);
	}
    }
    
    return $res;
 }
 
 function UnLock() {
    flock($this->lockf, LOCK_UN);
    flock($this->rlock, LOCK_UN);
 }
}


?>