This repository was archived by the owner on Aug 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathLockFile.php
More file actions
110 lines (97 loc) · 2.14 KB
/
LockFile.php
File metadata and controls
110 lines (97 loc) · 2.14 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
/**
* A class for file's locking tasks. DONE.
*
*/
class lockFile
{
/**
* Lock file identifier
*
* @var string
*/
private $id;
/**
* Initialise the lock file
*
* @param string $id The lock identifier
*/
function __construct($id)
{
$am = AccountManager::getInstance();
$appConf = $am->appConf;
// For security, we don't want to have IDs that can traverse directories.
$id = basename($id);
$this->id = $id;
$this->path = $appConf['GLOBAL_CONFIGURATION']['data.path'] . '.' . $this->id;
}
/**
* Tells if the lock file exists
*
* @return boolean Returns TRUE if the file exists, FALSE otherwise
*/
function isLocked()
{
return file_exists($this->path);
}
/**
* Sets the lock file
*
* @return boolean Returns TRUE if the lock was successfully set, FALSE otherwise
*/
function lock()
{
if ($this->isLocked()) {
return false;
}
return touch($this->path);
}
/**
* Release the lock
*
* @return boolean Returns TRUE if the lock was released, FALSE otherwise
*/
function release()
{
if ($this->isLocked()) {
return unlink($this->path);
}
return true;
}
/**
* Write into the lock
*
* @return boolean Returns TRUE if the write is OK, or FALSE otherwise
*/
function writeIntoLock($text)
{
if( $this->isLocked() )
{
$handle = fopen($this->path, 'w');
if (fwrite($handle, $text) === FALSE) {
return false;
} else {
return true;
}
} else {
return false;
}
}
/**
* Read the lock
*
* @return The content of the lock or FALSE if the lock didn't exist
*/
function readLock()
{
if( $this->isLocked() )
{
$handle = fopen($this->path, 'r');
return fread($handle, filesize($this->path));
} else
{
return false;
}
}
}
?>