1
|
|
|
<?php |
2
|
|
|
namespace PHPDaemon\FS; |
3
|
|
|
|
4
|
|
|
use PHPDaemon\Core\CallbackWrapper; |
5
|
|
|
use PHPDaemon\Network\IOStream; |
6
|
|
|
use PHPDaemon\Structures\StackCallbacks; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* File |
10
|
|
|
* @package PHPDaemon\FS |
11
|
|
|
* @author Vasily Zorin <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class File |
14
|
|
|
{ |
15
|
|
|
use \PHPDaemon\Traits\ClassWatchdog; |
16
|
|
|
use \PHPDaemon\Traits\StaticObjectWatchdog; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var integer Priority |
20
|
|
|
*/ |
21
|
|
|
public $priority = 10; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var integer Chunk size |
25
|
|
|
*/ |
26
|
|
|
public $chunkSize = 4096; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string $stat hash Stat |
30
|
|
|
*/ |
31
|
|
|
protected $stat; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var array Cache of statvfs() |
35
|
|
|
*/ |
36
|
|
|
protected $statvfs; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var integer Current offset |
40
|
|
|
*/ |
41
|
|
|
public $offset = 0; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string Cache key |
45
|
|
|
*/ |
46
|
|
|
public $fdCacheKey; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var boolean Append? |
50
|
|
|
*/ |
51
|
|
|
public $append; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var string Path |
55
|
|
|
*/ |
56
|
|
|
public $path; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var boolean Writing? |
60
|
|
|
*/ |
61
|
|
|
public $writing = false; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var boolean Closed? |
65
|
|
|
*/ |
66
|
|
|
public $closed = false; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var object File descriptor |
70
|
|
|
*/ |
71
|
|
|
protected $fd; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @var PHPDaemon\Structures\StackCallbacks Stack of callbacks called when writing is done |
75
|
|
|
*/ |
76
|
|
|
protected $onWriteOnce; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* File constructor |
80
|
|
|
* @param resource $fd Descriptor |
81
|
|
|
* @param string $path Path |
82
|
|
|
*/ |
83
|
|
|
public function __construct($fd, $path) |
84
|
|
|
{ |
85
|
|
|
$this->fd = $fd; |
|
|
|
|
86
|
|
|
$this->path = $path; |
87
|
|
|
$this->onWriteOnce = new StackCallbacks; |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get file descriptor |
92
|
|
|
* @return resource File descriptor |
|
|
|
|
93
|
|
|
*/ |
94
|
|
|
public function getFd() |
95
|
|
|
{ |
96
|
|
|
return $this->fd; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Converts string of flags to integer or standard text representation |
101
|
|
|
* @param string $mode Mode |
102
|
|
|
* @param boolean $text Text? |
103
|
|
|
* @return mixed |
104
|
|
|
*/ |
105
|
|
|
public static function convertFlags($mode, $text = false) |
106
|
|
|
{ |
107
|
|
|
$plus = mb_orig_strpos($mode, '+') !== false; |
108
|
|
|
$sync = mb_orig_strpos($mode, 's') !== false; |
109
|
|
|
$type = strtr($mode, ['b' => '', '+' => '', 's' => '', '!' => '']); |
110
|
|
|
if ($text) { |
111
|
|
|
return $type; |
112
|
|
|
} |
113
|
|
|
$types = [ |
114
|
|
|
'r' => $plus ? EIO_O_RDWR : EIO_O_RDONLY, |
115
|
|
|
'w' => ($plus ? EIO_O_RDWR : EIO_O_WRONLY) | EIO_O_CREAT | EIO_O_TRUNC, |
116
|
|
|
'a' => ($plus ? EIO_O_RDWR : EIO_O_WRONLY) | EIO_O_CREAT | EIO_O_APPEND, |
117
|
|
|
'x' => ($plus ? EIO_O_RDWR : EIO_O_WRONLY) | EIO_O_EXCL | EIO_O_CREAT, |
118
|
|
|
'c' => ($plus ? EIO_O_RDWR : EIO_O_WRONLY) | EIO_O_CREAT, |
119
|
|
|
]; |
120
|
|
|
$m = $types[$type]; |
121
|
|
|
if ($sync) { |
122
|
|
|
$m |= EIO_O_FSYNC; |
123
|
|
|
} |
124
|
|
|
return $m; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Truncates this file |
129
|
|
|
* @param integer $offset Offset. Default is 0 |
130
|
|
|
* @param callable $cb Callback |
|
|
|
|
131
|
|
|
* @param integer $pri Priority |
132
|
|
|
* @return resource|boolean |
133
|
|
|
*/ |
134
|
|
View Code Duplication |
public function truncate($offset = 0, $cb = null, $pri = EIO_PRI_DEFAULT) |
|
|
|
|
135
|
|
|
{ |
136
|
|
|
$cb = CallbackWrapper::forceWrap($cb); |
|
|
|
|
137
|
|
|
if (!$this->fd || $this->fd === -1) { |
138
|
|
|
if ($cb) { |
139
|
|
|
$cb($this, false); |
140
|
|
|
} |
141
|
|
|
return false; |
142
|
|
|
} |
143
|
|
|
if (!FileSystem::$supported) { |
144
|
|
|
$fp = fopen($this->path, 'r+'); |
145
|
|
|
$r = $fp && ftruncate($fp, $offset); |
146
|
|
|
if ($cb) { |
147
|
|
|
$cb($this, $r); |
148
|
|
|
} |
149
|
|
|
return $r; |
150
|
|
|
} |
151
|
|
|
return eio_ftruncate($this->fd, $offset, $pri, $cb, $this); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Stat() |
156
|
|
|
* @param callable $cb Callback |
157
|
|
|
* @param integer $pri Priority |
158
|
|
|
* @return resource|boolean |
159
|
|
|
*/ |
160
|
|
|
public function stat($cb, $pri = EIO_PRI_DEFAULT) |
161
|
|
|
{ |
162
|
|
|
$cb = CallbackWrapper::forceWrap($cb); |
163
|
|
|
if (!$this->fd || $this->fd === -1) { |
164
|
|
|
if ($cb) { |
165
|
|
|
$cb($this, false); |
166
|
|
|
} |
167
|
|
|
return false; |
168
|
|
|
} |
169
|
|
View Code Duplication |
if (!FileSystem::$supported) { |
|
|
|
|
170
|
|
|
$cb($this, FileSystem::statPrepare(fstat($this->fd))); |
171
|
|
|
return false; |
172
|
|
|
} |
173
|
|
|
if ($this->stat) { |
174
|
|
|
$cb($this, $this->stat); |
175
|
|
|
return true; |
176
|
|
|
} |
177
|
|
|
return eio_fstat($this->fd, $pri, function ($file, $stat) use ($cb) { |
178
|
|
|
$stat = FileSystem::statPrepare($stat); |
179
|
|
|
$file->stat = $stat; |
180
|
|
|
$cb($file, $stat); |
181
|
|
|
}, $this); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Stat() non-cached |
186
|
|
|
* @param callable $cb Callback |
187
|
|
|
* @param integer $pri Priority |
188
|
|
|
* @return resource|boolean |
189
|
|
|
*/ |
190
|
|
|
public function statRefresh($cb, $pri = EIO_PRI_DEFAULT) |
191
|
|
|
{ |
192
|
|
|
$cb = CallbackWrapper::forceWrap($cb); |
193
|
|
|
if (!$this->fd || $this->fd === -1) { |
194
|
|
|
if ($cb) { |
195
|
|
|
$cb($this, false); |
196
|
|
|
} |
197
|
|
|
return false; |
198
|
|
|
} |
199
|
|
View Code Duplication |
if (!FileSystem::$supported) { |
|
|
|
|
200
|
|
|
$cb($this, FileSystem::statPrepare(fstat($this->fd))); |
201
|
|
|
return true; |
202
|
|
|
} |
203
|
|
|
return eio_fstat($this->fd, $pri, function ($file, $stat) use ($cb) { |
204
|
|
|
$stat = FileSystem::statPrepare($stat); |
205
|
|
|
$file->stat = $stat; |
206
|
|
|
$cb($file, $stat); |
207
|
|
|
}, $this); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Statvfs() |
212
|
|
|
* @param callable $cb Callback |
213
|
|
|
* @param integer $pri Priority |
214
|
|
|
* @return resource|boolean |
215
|
|
|
*/ |
216
|
|
|
public function statvfs($cb, $pri = EIO_PRI_DEFAULT) |
217
|
|
|
{ |
218
|
|
|
$cb = CallbackWrapper::forceWrap($cb); |
219
|
|
|
if (!$this->fd) { |
220
|
|
|
if ($cb) { |
221
|
|
|
$cb($this, false); |
222
|
|
|
} |
223
|
|
|
return false; |
224
|
|
|
} |
225
|
|
|
if (!FileSystem::$supported) { |
226
|
|
|
if ($cb) { |
227
|
|
|
$cb($this, false); |
228
|
|
|
} |
229
|
|
|
return false; |
230
|
|
|
} |
231
|
|
|
if ($this->statvfs) { |
|
|
|
|
232
|
|
|
$cb($this, $this->statvfs); |
233
|
|
|
return true; |
234
|
|
|
} |
235
|
|
|
return eio_fstatvfs($this->fd, $pri, function ($file, $stat) use ($cb) { |
236
|
|
|
$file->statvfs = $stat; |
237
|
|
|
$cb($file, $stat); |
238
|
|
|
}, $this); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Sync() |
243
|
|
|
* @param callable $cb Callback |
244
|
|
|
* @param integer $pri Priority |
245
|
|
|
* @return resource|false |
246
|
|
|
*/ |
247
|
|
View Code Duplication |
public function sync($cb, $pri = EIO_PRI_DEFAULT) |
|
|
|
|
248
|
|
|
{ |
249
|
|
|
$cb = CallbackWrapper::forceWrap($cb); |
250
|
|
|
if (!$this->fd) { |
251
|
|
|
if ($cb) { |
252
|
|
|
$cb($this, false); |
253
|
|
|
} |
254
|
|
|
return false; |
255
|
|
|
} |
256
|
|
|
if (!FileSystem::$supported) { |
257
|
|
|
$cb($this, true); |
258
|
|
|
return false; |
259
|
|
|
} |
260
|
|
|
return eio_fsync($this->fd, $pri, $cb, $this); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Datasync() |
265
|
|
|
* @param callable $cb Callback |
266
|
|
|
* @param integer $pri Priority |
267
|
|
|
* @return resource|false |
268
|
|
|
*/ |
269
|
|
View Code Duplication |
public function datasync($cb, $pri = EIO_PRI_DEFAULT) |
|
|
|
|
270
|
|
|
{ |
271
|
|
|
$cb = CallbackWrapper::forceWrap($cb); |
272
|
|
|
if (!$this->fd) { |
273
|
|
|
if ($cb) { |
274
|
|
|
$cb($this, false); |
275
|
|
|
} |
276
|
|
|
return false; |
277
|
|
|
} |
278
|
|
|
if (!FileSystem::$supported) { |
279
|
|
|
$cb($this, true); |
280
|
|
|
return false; |
281
|
|
|
} |
282
|
|
|
return eio_fdatasync($this->fd, $pri, $cb, $this); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Writes data to file |
287
|
|
|
* @param string $data Data |
288
|
|
|
* @param callable $cb Callback |
|
|
|
|
289
|
|
|
* @param integer $offset Offset |
|
|
|
|
290
|
|
|
* @param integer $pri Priority |
291
|
|
|
* @return resource|false |
292
|
|
|
*/ |
293
|
|
|
public function write($data, $cb = null, $offset = null, $pri = EIO_PRI_DEFAULT) |
294
|
|
|
{ |
295
|
|
|
$cb = CallbackWrapper::forceWrap($cb); |
|
|
|
|
296
|
|
|
if (!$this->fd) { |
297
|
|
|
if ($cb) { |
298
|
|
|
$cb($this, false); |
299
|
|
|
} |
300
|
|
|
return false; |
301
|
|
|
} |
302
|
|
|
if ($data === '') { |
303
|
|
|
if ($cb) { |
304
|
|
|
$cb($this, 0); |
305
|
|
|
} |
306
|
|
|
return false; |
307
|
|
|
} |
308
|
|
View Code Duplication |
if (!FileSystem::$supported) { |
|
|
|
|
309
|
|
|
if ($offset !== null) { |
310
|
|
|
fseek($data, $offset); |
311
|
|
|
} |
312
|
|
|
$r = fwrite($this->fd, $data); |
313
|
|
|
if ($cb) { |
314
|
|
|
$cb($this, $r); |
315
|
|
|
} |
316
|
|
|
return false; |
317
|
|
|
} |
318
|
|
|
if ($cb !== null) { |
319
|
|
|
$this->onWriteOnce->push($cb); |
320
|
|
|
} |
321
|
|
|
$l = mb_orig_strlen($data); |
322
|
|
|
if ($offset === null) { |
323
|
|
|
$offset = $this->offset; |
324
|
|
|
$this->offset += $l; |
325
|
|
|
} |
326
|
|
|
$this->writing = true; |
327
|
|
|
$res = eio_write( |
328
|
|
|
$this->fd, |
329
|
|
|
$data, |
330
|
|
|
$l, |
331
|
|
|
$offset, |
332
|
|
|
$pri, |
333
|
|
|
function ($file, $result) { |
334
|
|
|
$this->writing = false; |
335
|
|
|
$this->onWriteOnce->executeAll($file, $result); |
336
|
|
|
}, |
337
|
|
|
$this |
338
|
|
|
); |
339
|
|
|
return $res; |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* Changes ownership of this file |
344
|
|
|
* @param integer $uid User ID |
345
|
|
|
* @param integer $gid Group ID |
346
|
|
|
* @param callable $cb = null Callback |
|
|
|
|
347
|
|
|
* @param integer $pri = EIO_PRI_DEFAULT Priority |
348
|
|
|
* @return resource|false |
349
|
|
|
*/ |
350
|
|
View Code Duplication |
public function chown($uid, $gid = -1, $cb = null, $pri = EIO_PRI_DEFAULT) |
|
|
|
|
351
|
|
|
{ |
352
|
|
|
$cb = CallbackWrapper::forceWrap($cb); |
|
|
|
|
353
|
|
|
if (!$this->fd) { |
354
|
|
|
if ($cb) { |
355
|
|
|
$cb($this, false); |
356
|
|
|
} |
357
|
|
|
return false; |
358
|
|
|
} |
359
|
|
|
if (!FileSystem::$supported) { |
360
|
|
|
$r = chown($this->path, $uid); |
361
|
|
|
if ($gid !== -1) { |
362
|
|
|
$r = $r && chgrp($this->path, $gid); |
363
|
|
|
} |
364
|
|
|
if ($cb) { |
365
|
|
|
$cb($this, $r); |
366
|
|
|
} |
367
|
|
|
return false; |
368
|
|
|
} |
369
|
|
|
return eio_fchown($this->fd, $uid, $gid, $pri, $cb, $this); |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* touch() |
374
|
|
|
* @param integer $mtime Last modification time |
375
|
|
|
* @param integer $atime Last access time |
|
|
|
|
376
|
|
|
* @param callable $cb Callback |
|
|
|
|
377
|
|
|
* @param integer $pri Priority |
378
|
|
|
* @return resource|false |
379
|
|
|
*/ |
380
|
|
|
public function touch($mtime, $atime = null, $cb = null, $pri = EIO_PRI_DEFAULT) |
381
|
|
|
{ |
382
|
|
|
$cb = CallbackWrapper::forceWrap($cb); |
|
|
|
|
383
|
|
|
if (!$this->fd) { |
384
|
|
|
if ($cb) { |
385
|
|
|
$cb($this, false); |
386
|
|
|
} |
387
|
|
|
return false; |
388
|
|
|
} |
389
|
|
|
if (!FileSystem::$supported) { |
390
|
|
|
$r = touch($this->path, $mtime, $atime); |
391
|
|
|
if ($cb) { |
392
|
|
|
$cb($this, $r); |
393
|
|
|
} |
394
|
|
|
return false; |
395
|
|
|
} |
396
|
|
|
return eio_futime($this->fd, $atime, $mtime, $pri, $cb, $this); |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
/** |
400
|
|
|
* Clears cache of stat() and statvfs() |
401
|
|
|
* @return void |
402
|
|
|
*/ |
403
|
|
|
public function clearStatCache() |
404
|
|
|
{ |
405
|
|
|
$this->stat = null; |
406
|
|
|
$this->statvfs = null; |
|
|
|
|
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
/** |
410
|
|
|
* Reads data from file |
411
|
|
|
* @param integer $length Length |
412
|
|
|
* @param integer $offset Offset |
|
|
|
|
413
|
|
|
* @param callable $cb Callback |
|
|
|
|
414
|
|
|
* @param integer $pri Priority |
415
|
|
|
* @return boolean |
|
|
|
|
416
|
|
|
*/ |
417
|
|
|
public function read($length, $offset = null, $cb = null, $pri = EIO_PRI_DEFAULT) |
418
|
|
|
{ |
419
|
|
|
$cb = CallbackWrapper::forceWrap($cb); |
|
|
|
|
420
|
|
|
if (!$this->fd) { |
421
|
|
|
if ($cb) { |
422
|
|
|
$cb($this, false); |
423
|
|
|
} |
424
|
|
|
return false; |
425
|
|
|
} |
426
|
|
View Code Duplication |
if (!FileSystem::$supported) { |
|
|
|
|
427
|
|
|
if ($offset !== null) { |
428
|
|
|
fseek($this->fd, $length); |
429
|
|
|
} |
430
|
|
|
$data = fread($this->fd, $length); |
431
|
|
|
$cb === null || $cb($this, $data); |
432
|
|
|
return $data; |
433
|
|
|
} |
434
|
|
|
$this->offset += $length; |
435
|
|
|
eio_read( |
436
|
|
|
$this->fd, |
437
|
|
|
$length, |
438
|
|
|
$offset !== null ? $offset : $this->offset, |
439
|
|
|
$pri, |
440
|
|
|
$cb, |
441
|
|
|
$this |
442
|
|
|
); |
443
|
|
|
return true; |
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
/** |
447
|
|
|
* sendfile() |
448
|
|
|
* @param mixed $outfd File descriptor |
449
|
|
|
* @param callable $cb Callback |
450
|
|
|
* @param callable $startCb Start callback |
|
|
|
|
451
|
|
|
* @param integer $offset Offset |
452
|
|
|
* @param integer $length Length |
|
|
|
|
453
|
|
|
* @param integer $pri Priority |
454
|
|
|
* @return boolean Success |
455
|
|
|
*/ |
456
|
|
|
public function sendfile($outfd, $cb, $startCb = null, $offset = 0, $length = null, $pri = EIO_PRI_DEFAULT) |
457
|
|
|
{ |
458
|
|
|
$cb = CallbackWrapper::forceWrap($cb); |
459
|
|
|
if (!$this->fd) { |
460
|
|
|
if ($cb) { |
461
|
|
|
$cb($this, false); |
462
|
|
|
} |
463
|
|
|
return false; |
464
|
|
|
} |
465
|
|
|
if (!FileSystem::$supported) { |
466
|
|
|
if ($cb) { |
467
|
|
|
$cb($this, false); |
468
|
|
|
} |
469
|
|
|
return false; |
470
|
|
|
} |
471
|
|
|
static $chunkSize = 1024; |
472
|
|
|
$ret = true; |
473
|
|
|
|
474
|
|
|
$handler = function ( |
475
|
|
|
$file, |
476
|
|
|
$sent = -1 |
477
|
|
|
) use ( |
478
|
|
|
&$ret, |
479
|
|
|
$outfd, |
480
|
|
|
$cb, |
481
|
|
|
&$handler, |
482
|
|
|
&$offset, |
483
|
|
|
&$length, |
484
|
|
|
$pri, |
485
|
|
|
$chunkSize |
486
|
|
|
) { |
487
|
|
|
if ($outfd instanceof IOStream) { |
488
|
|
|
if ($outfd->isFreed()) { |
489
|
|
|
$cb($file, false); |
490
|
|
|
return; |
491
|
|
|
} |
492
|
|
|
$ofd = $outfd->getFd(); |
493
|
|
|
} else { |
494
|
|
|
$ofd = $outfd; |
495
|
|
|
} |
496
|
|
|
if (!$ret) { |
497
|
|
|
$cb($file, false); |
498
|
|
|
return; |
499
|
|
|
} |
500
|
|
|
if ($sent === -1) { |
501
|
|
|
$sent = 0; |
502
|
|
|
} |
503
|
|
|
$offset += $sent; |
504
|
|
|
$length -= $sent; |
505
|
|
|
if ($length <= 0) { |
506
|
|
|
$cb($file, true); |
507
|
|
|
return; |
508
|
|
|
} |
509
|
|
|
if (!$ofd) { |
510
|
|
|
$cb($file, false); |
511
|
|
|
return; |
512
|
|
|
} |
513
|
|
|
$c = min($chunkSize, $length); |
514
|
|
|
$ret = eio_sendfile($ofd, $file->fd, $offset, $c, $pri, $handler, $file); |
515
|
|
|
}; |
516
|
|
|
if ($length !== null) { |
517
|
|
|
if ($startCb !== null) { |
518
|
|
|
if (!$startCb($this, $length, $handler)) { |
519
|
|
|
$handler($this); |
520
|
|
|
} |
521
|
|
|
} else { |
522
|
|
|
$handler($this); |
523
|
|
|
} |
524
|
|
|
return true; |
525
|
|
|
} |
526
|
|
|
$this->statRefresh(function ($file, $stat) use ($startCb, $handler, &$length) { |
527
|
|
|
$length = $stat['size']; |
528
|
|
|
if ($startCb !== null) { |
529
|
|
|
if (!$startCb($file, $length, $handler)) { |
530
|
|
|
$handler($file); |
531
|
|
|
} |
532
|
|
|
} else { |
533
|
|
|
$handler($file); |
534
|
|
|
} |
535
|
|
|
}, $pri); |
536
|
|
|
return true; |
537
|
|
|
} |
538
|
|
|
|
539
|
|
|
/** |
540
|
|
|
* readahead() |
541
|
|
|
* @param integer $length Length |
542
|
|
|
* @param integer $offset Offset |
|
|
|
|
543
|
|
|
* @param callable $cb Callback |
|
|
|
|
544
|
|
|
* @param integer $pri Priority |
545
|
|
|
* @return resource|false |
546
|
|
|
*/ |
547
|
|
|
public function readahead($length, $offset = null, $cb = null, $pri = EIO_PRI_DEFAULT) |
548
|
|
|
{ |
549
|
|
|
$cb = CallbackWrapper::forceWrap($cb); |
|
|
|
|
550
|
|
|
if (!$this->fd) { |
551
|
|
|
if ($cb) { |
552
|
|
|
$cb($this, false); |
553
|
|
|
} |
554
|
|
|
return false; |
555
|
|
|
} |
556
|
|
|
if (!FileSystem::$supported) { |
557
|
|
|
if ($cb) { |
558
|
|
|
$cb($this, false); |
559
|
|
|
} |
560
|
|
|
return false; |
561
|
|
|
} |
562
|
|
|
$this->offset += $length; |
563
|
|
|
return eio_readahead( |
564
|
|
|
$this->fd, |
565
|
|
|
$length, |
566
|
|
|
$offset !== null ? $offset : $this->offset, |
567
|
|
|
$pri, |
568
|
|
|
$cb, |
569
|
|
|
$this |
570
|
|
|
); |
571
|
|
|
} |
572
|
|
|
|
573
|
|
|
/** |
574
|
|
|
* Generates closure-callback for readAll |
575
|
|
|
* @param callable $cb |
576
|
|
|
* @param integer $size |
577
|
|
|
* @param integer &$offset |
578
|
|
|
* @param integer &$pri |
579
|
|
|
* @param string &$buf |
580
|
|
|
* @return callable |
581
|
|
|
*/ |
582
|
|
View Code Duplication |
protected function readAllGenHandler($cb, $size, &$offset, &$pri, &$buf) |
|
|
|
|
583
|
|
|
{ |
584
|
|
|
return function ($file, $data) use ($cb, $size, &$offset, &$pri, &$buf) { |
585
|
|
|
$buf .= $data; |
586
|
|
|
$offset += mb_orig_strlen($data); |
587
|
|
|
$len = min($file->chunkSize, $size - $offset); |
588
|
|
|
if ($offset >= $size) { |
589
|
|
|
if ($cb) { |
590
|
|
|
$cb($file, $buf); |
591
|
|
|
} |
592
|
|
|
return; |
593
|
|
|
} |
594
|
|
|
eio_read($file->fd, $len, $offset, $pri, $this->readAllGenHandler($cb, $size, $offset, $pri, $buf), $this); |
595
|
|
|
}; |
596
|
|
|
} |
597
|
|
|
|
598
|
|
|
/** |
599
|
|
|
* Reads whole file |
600
|
|
|
* @param callable $cb Callback |
601
|
|
|
* @param integer $pri Priority |
602
|
|
|
* @return boolean Success |
603
|
|
|
*/ |
604
|
|
|
public function readAll($cb, $pri = EIO_PRI_DEFAULT) |
605
|
|
|
{ |
606
|
|
|
$cb = CallbackWrapper::forceWrap($cb); |
607
|
|
|
if (!$this->fd) { |
608
|
|
|
if ($cb) { |
609
|
|
|
$cb($this, false); |
610
|
|
|
} |
611
|
|
|
return false; |
612
|
|
|
} |
613
|
|
|
$this->statRefresh( |
614
|
|
View Code Duplication |
function ($file, $stat) use ($cb, $pri) { |
|
|
|
|
615
|
|
|
if (!$stat) { |
616
|
|
|
if ($cb) { |
617
|
|
|
$cb($file, false); |
618
|
|
|
} |
619
|
|
|
return; |
620
|
|
|
} |
621
|
|
|
|
622
|
|
|
$offset = 0; |
623
|
|
|
$buf = ''; |
624
|
|
|
$size = $stat['size']; |
625
|
|
|
|
626
|
|
|
eio_read( |
627
|
|
|
$file->fd, |
628
|
|
|
min($file->chunkSize, $size), |
629
|
|
|
0, |
630
|
|
|
$pri, |
631
|
|
|
$this->readAllGenHandler($cb, $size, $offset, $pri, $buf), |
|
|
|
|
632
|
|
|
$file |
633
|
|
|
); |
634
|
|
|
}, |
635
|
|
|
$pri |
636
|
|
|
); |
637
|
|
|
|
638
|
|
|
return true; |
639
|
|
|
} |
640
|
|
|
|
641
|
|
|
/** |
642
|
|
|
* Generates closure-callback for readAllChunked |
643
|
|
|
* @param callable $cb |
644
|
|
|
* @param callable $chunkcb |
645
|
|
|
* @param integer $size |
646
|
|
|
* @param integer $offset |
647
|
|
|
* @param integer $pri |
648
|
|
|
* @return callable |
649
|
|
|
*/ |
650
|
|
View Code Duplication |
protected function readAllChunkedGenHandler($cb, $chunkcb, $size, &$offset, $pri) |
|
|
|
|
651
|
|
|
{ |
652
|
|
|
return function ($file, $data) use ($cb, $chunkcb, $size, &$offset, $pri) { |
653
|
|
|
$chunkcb($file, $data); |
654
|
|
|
$offset += mb_orig_strlen($data); |
655
|
|
|
$len = min($file->chunkSize, $size - $offset); |
656
|
|
|
|
657
|
|
|
if ($offset >= $size) { |
658
|
|
|
$cb($file, true); |
659
|
|
|
return; |
660
|
|
|
} |
661
|
|
|
|
662
|
|
|
eio_read( |
663
|
|
|
$file->fd, |
664
|
|
|
$len, |
665
|
|
|
$offset, |
666
|
|
|
$pri, |
667
|
|
|
$this->readAllChunkedGenHandler($cb, $chunkcb, $size, $offset, $pri), |
668
|
|
|
$file |
669
|
|
|
); |
670
|
|
|
}; |
671
|
|
|
} |
672
|
|
|
|
673
|
|
|
/** |
674
|
|
|
* Reads file chunk-by-chunk |
675
|
|
|
* @param callable $cb Callback |
|
|
|
|
676
|
|
|
* @param callable $chunkcb Callback of chunk |
|
|
|
|
677
|
|
|
* @param integer $pri Priority |
678
|
|
|
* @return resource|false |
|
|
|
|
679
|
|
|
*/ |
680
|
|
|
public function readAllChunked($cb = null, $chunkcb = null, $pri = EIO_PRI_DEFAULT) |
681
|
|
|
{ |
682
|
|
|
$cb = CallbackWrapper::forceWrap($cb); |
|
|
|
|
683
|
|
|
if (!$this->fd) { |
684
|
|
|
if ($cb) { |
685
|
|
|
$cb($this, false); |
686
|
|
|
} |
687
|
|
|
return false; |
688
|
|
|
} |
689
|
|
|
return $this->statRefresh( |
690
|
|
View Code Duplication |
function ($file, $stat) use ($cb, $chunkcb, $pri) { |
|
|
|
|
691
|
|
|
if (!$stat) { |
692
|
|
|
$cb($file, false); |
693
|
|
|
return; |
694
|
|
|
} |
695
|
|
|
|
696
|
|
|
$offset = 0; |
697
|
|
|
$size = $stat['size']; |
698
|
|
|
|
699
|
|
|
eio_read( |
700
|
|
|
$file->fd, |
701
|
|
|
min($file->chunkSize, $size), |
702
|
|
|
$offset, |
703
|
|
|
$pri, |
704
|
|
|
$this->readAllChunkedGenHandler($cb, $chunkcb, $size, $offset, $pri), |
|
|
|
|
705
|
|
|
$file |
706
|
|
|
); |
707
|
|
|
}, |
708
|
|
|
$pri |
709
|
|
|
); |
710
|
|
|
} |
711
|
|
|
|
712
|
|
|
/** |
713
|
|
|
* toString handler |
714
|
|
|
* @return string |
715
|
|
|
*/ |
716
|
|
|
public function __toString() |
717
|
|
|
{ |
718
|
|
|
return $this->path; |
719
|
|
|
} |
720
|
|
|
|
721
|
|
|
/** |
722
|
|
|
* Set chunk size |
723
|
|
|
* @param integer $n Chunk size |
724
|
|
|
* @return void |
725
|
|
|
*/ |
726
|
|
|
public function setChunkSize($n) |
727
|
|
|
{ |
728
|
|
|
$this->chunkSize = $n; |
729
|
|
|
} |
730
|
|
|
|
731
|
|
|
/** |
732
|
|
|
* Move pointer to arbitrary position |
733
|
|
|
* @param integer $offset Offset |
734
|
|
|
* @param callable $cb Callback |
735
|
|
|
* @param integer $pri Priority |
736
|
|
|
* @return resource|false |
737
|
|
|
*/ |
738
|
|
|
public function seek($offset, $cb, $pri = EIO_PRI_DEFAULT) |
739
|
|
|
{ |
740
|
|
|
$cb = CallbackWrapper::forceWrap($cb); |
741
|
|
|
if (!\EIO::$supported) { |
742
|
|
|
fseek($this->fd, $offset); |
743
|
|
|
return false; |
744
|
|
|
} |
745
|
|
|
return eio_seek($this->fd, $offset, $pri, $cb, $this); |
746
|
|
|
} |
747
|
|
|
|
748
|
|
|
/** |
749
|
|
|
* Get current pointer position |
750
|
|
|
* @return integer |
751
|
|
|
*/ |
752
|
|
|
public function tell() |
753
|
|
|
{ |
754
|
|
|
if (\EIO::$supported) { |
755
|
|
|
return $this->offset; |
756
|
|
|
} |
757
|
|
|
return ftell($this->fd); |
758
|
|
|
} |
759
|
|
|
|
760
|
|
|
/** |
761
|
|
|
* Close the file |
762
|
|
|
* @return resource|false |
763
|
|
|
*/ |
764
|
|
|
public function close() |
765
|
|
|
{ |
766
|
|
|
if ($this->closed) { |
767
|
|
|
return false; |
768
|
|
|
} |
769
|
|
|
$this->closed = true; |
770
|
|
|
if ($this->fdCacheKey !== null) { |
771
|
|
|
FileSystem::$fdCache->invalidate($this->fdCacheKey); |
772
|
|
|
} |
773
|
|
|
if ($this->fd === null) { |
774
|
|
|
return false; |
775
|
|
|
} |
776
|
|
|
|
777
|
|
|
if (!FileSystem::$supported) { |
778
|
|
|
fclose($this->fd); |
779
|
|
|
return false; |
780
|
|
|
} |
781
|
|
|
$r = eio_close($this->fd, EIO_PRI_MAX); |
782
|
|
|
$this->fd = null; |
783
|
|
|
return $r; |
784
|
|
|
} |
785
|
|
|
|
786
|
|
|
/** |
787
|
|
|
* Destructor |
788
|
|
|
*/ |
789
|
|
|
public function __destruct() |
790
|
|
|
{ |
791
|
|
|
$this->close(); |
792
|
|
|
} |
793
|
|
|
} |
794
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..