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
|
|
|
/** |
14
|
|
|
* Abstract class to represent filesystem Node. |
15
|
|
|
* |
16
|
|
|
* @author Michael Donat <[email protected]> |
17
|
|
|
* @package php-vfs |
18
|
|
|
*/ |
19
|
|
|
abstract class Node |
20
|
|
|
{ |
21
|
|
|
const S_IFMT = 0160000; |
22
|
|
|
const DEF_MODE = 0755; |
23
|
|
|
|
24
|
|
|
protected $basename; |
25
|
|
|
protected $parent; |
26
|
|
|
protected $userid; |
27
|
|
|
protected $groupid; |
28
|
|
|
|
29
|
|
|
protected $atime; |
30
|
|
|
protected $mtime; |
31
|
|
|
protected $ctime; |
32
|
|
|
|
33
|
|
|
protected $mode; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Class constructor. |
37
|
|
|
* |
38
|
|
|
* @param string $basename |
39
|
|
|
*/ |
40
|
|
|
public function __construct($basename) |
41
|
|
|
{ |
42
|
|
|
$this->basename = $basename; |
43
|
|
|
$this->chmod(self::DEF_MODE); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Changes access to file. |
48
|
|
|
* |
49
|
|
|
* This will apply the DIR/FILE type mask for use by stat to distinguish between file and directory. |
50
|
|
|
* @see http://man7.org/linux/man-pages/man2/lstat.2.html for explanation. |
51
|
|
|
* |
52
|
|
|
* @param int $mode |
53
|
|
|
*/ |
54
|
|
|
public function chmod($mode) |
55
|
|
|
{ |
56
|
|
|
$this->mode = $mode | static::S_IFTYPE; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Returns file mode |
61
|
|
|
* |
62
|
|
|
* @return int |
63
|
|
|
*/ |
64
|
|
|
public function mode() |
65
|
|
|
{ |
66
|
|
|
return $this->mode; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Changes ownership. |
71
|
|
|
* |
72
|
|
|
* @param $userid |
73
|
|
|
*/ |
74
|
|
|
public function chown($userid) |
75
|
|
|
{ |
76
|
|
|
$this->userid = $userid; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Returns ownership. |
81
|
|
|
* |
82
|
|
|
* @return mixed |
83
|
|
|
*/ |
84
|
|
|
public function user() |
85
|
|
|
{ |
86
|
|
|
return $this->userid; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Changes group ownership. |
91
|
|
|
* |
92
|
|
|
* @param $groupid |
93
|
|
|
*/ |
94
|
|
|
public function chgrp($groupid) |
95
|
|
|
{ |
96
|
|
|
$this->groupid = $groupid; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Returns group ownership. |
101
|
|
|
* |
102
|
|
|
* @return mixed |
103
|
|
|
*/ |
104
|
|
|
public function group() |
105
|
|
|
{ |
106
|
|
|
return $this->groupid; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Returns Node size. |
111
|
|
|
* |
112
|
|
|
* @return mixed |
113
|
|
|
*/ |
114
|
|
|
abstract public function size(); |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Sets parent Node. |
118
|
|
|
* |
119
|
|
|
* @param Directory $parent |
120
|
|
|
*/ |
121
|
|
|
protected function setParent(Directory $parent) |
122
|
|
|
{ |
123
|
|
|
$this->parent = $parent; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Returns Node basename. |
128
|
|
|
* |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
public function basename() |
132
|
|
|
{ |
133
|
|
|
return $this->basename; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Sets new basename |
138
|
|
|
* |
139
|
|
|
* @param string $basename |
140
|
|
|
*/ |
141
|
|
|
public function setBasename($basename) |
142
|
|
|
{ |
143
|
|
|
$this->basename = $basename; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Returns node path. |
148
|
|
|
* |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
|
|
public function path() |
152
|
|
|
{ |
153
|
|
|
$dirname = $this->dirname(); |
154
|
|
|
|
155
|
|
|
if ($this->parent instanceof Root) { //at root |
156
|
|
|
|
157
|
|
|
return $dirname.$this->basename(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return sprintf('%s/%s', $dirname, $this->basename()); |
161
|
|
|
|
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Returns node URL. |
166
|
|
|
* |
167
|
|
|
* @return string |
168
|
|
|
*/ |
169
|
|
|
public function url() |
170
|
|
|
{ |
171
|
|
|
$dirname = $this->parent->url(); |
172
|
|
|
|
173
|
|
|
if ($this->parent instanceof Root) { //at root |
174
|
|
|
|
175
|
|
|
return $dirname.$this->basename(); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return sprintf('%s/%s', $dirname, $this->basename()); |
179
|
|
|
|
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Returns node absolute path (without scheme). |
184
|
|
|
* |
185
|
|
|
* @return string |
186
|
|
|
*/ |
187
|
|
|
public function __toString() |
188
|
|
|
{ |
189
|
|
|
return $this->path(); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Returns Node parent absolute path. |
194
|
|
|
* |
195
|
|
|
* @return string|null |
196
|
|
|
*/ |
197
|
|
|
public function dirname() |
198
|
|
|
{ |
199
|
|
|
if ($this->parent) { |
200
|
|
|
return $this->parent->path(); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Sets last access time |
206
|
|
|
* |
207
|
|
|
* @param int $time |
208
|
|
|
*/ |
209
|
|
|
public function setAccessTime($time) |
210
|
|
|
{ |
211
|
|
|
$this->atime = $time; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Sets last modification time |
216
|
|
|
* |
217
|
|
|
* @param int $time |
218
|
|
|
*/ |
219
|
|
|
public function setModificationTime($time) |
220
|
|
|
{ |
221
|
|
|
$this->mtime = $time; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Sets last inode change time |
226
|
|
|
* |
227
|
|
|
* @param int $time |
228
|
|
|
*/ |
229
|
|
|
public function setChangeTime($time) |
230
|
|
|
{ |
231
|
|
|
$this->ctime = $time; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Returns last access time |
236
|
|
|
* |
237
|
|
|
* @return int |
238
|
|
|
*/ |
239
|
|
|
public function atime() |
240
|
|
|
{ |
241
|
|
|
return $this->atime; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Returns last modification time |
246
|
|
|
* |
247
|
|
|
* @return int |
248
|
|
|
*/ |
249
|
|
|
public function mtime() |
250
|
|
|
{ |
251
|
|
|
return $this->mtime; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Returns last inode change time (chown etc.) |
256
|
|
|
* |
257
|
|
|
* @return int |
258
|
|
|
*/ |
259
|
|
|
public function ctime() |
260
|
|
|
{ |
261
|
|
|
return $this->ctime; |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|