|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the php-vfs package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Michael Donat <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace VirtualFileSystem\Structure; |
|
12
|
|
|
|
|
13
|
|
|
use SplObjectStorage; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Object representation of File. |
|
17
|
|
|
* |
|
18
|
|
|
* @author Michael Donat <[email protected]> |
|
19
|
|
|
* @package php-vfs |
|
20
|
|
|
*/ |
|
21
|
|
|
class File extends Node |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @see http://man7.org/linux/man-pages/man2/lstat.2.html |
|
25
|
|
|
*/ |
|
26
|
|
|
const S_IFTYPE = 0100000; |
|
27
|
|
|
|
|
28
|
|
|
protected $data; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Resource with exclusive lock on this file |
|
32
|
|
|
* @var resource|null |
|
33
|
|
|
*/ |
|
34
|
|
|
private $exclusiveLock = null; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Resources with a shared lock on this file |
|
38
|
|
|
* @var SplObjectStorage |
|
39
|
|
|
*/ |
|
40
|
|
|
private $sharedLock; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @inherit |
|
44
|
|
|
* @param string $basename |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct($basename) |
|
47
|
|
|
{ |
|
48
|
|
|
parent::__construct($basename); |
|
49
|
|
|
$this->sharedLock = new SplObjectStorage; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Returns contents size. |
|
54
|
|
|
* |
|
55
|
|
|
* @return int |
|
56
|
|
|
*/ |
|
57
|
|
|
public function size() |
|
58
|
|
|
{ |
|
59
|
|
|
return strlen($this->data); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Returns contents. |
|
64
|
|
|
* |
|
65
|
|
|
* @return null|string |
|
66
|
|
|
*/ |
|
67
|
|
|
public function data() |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->data; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Sets contents. |
|
74
|
|
|
* |
|
75
|
|
|
* @param $data |
|
76
|
|
|
* @param null|string $data |
|
77
|
|
|
*/ |
|
78
|
|
|
public function setData($data) |
|
79
|
|
|
{ |
|
80
|
|
|
$this->data = $data; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function lock($resource, $operation) |
|
84
|
|
|
{ |
|
85
|
|
|
if ($this->exclusiveLock === $resource) { |
|
86
|
|
|
$this->exclusiveLock = null; |
|
87
|
|
|
} else { |
|
88
|
|
|
$this->sharedLock->detach($resource); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
if ($operation & LOCK_NB) { |
|
92
|
|
|
$operation -= LOCK_NB; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$unlock = $operation === LOCK_UN; |
|
96
|
|
|
$exclusive = $operation === LOCK_EX; |
|
97
|
|
|
|
|
98
|
|
|
if ($unlock) { |
|
99
|
|
|
return true; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
if ($this->exclusiveLock !== null) { |
|
103
|
|
|
return false; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
if (!$exclusive) { |
|
107
|
|
|
$this->sharedLock->attach($resource); |
|
108
|
|
|
|
|
109
|
|
|
return true; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
if ($this->sharedLock->count()) { |
|
113
|
|
|
return false; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$this->exclusiveLock = $resource; |
|
117
|
|
|
|
|
118
|
|
|
return true; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|