|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (c) 2014 Robin Appelman <[email protected]> |
|
4
|
|
|
* This file is licensed under the Licensed under the MIT license: |
|
5
|
|
|
* http://opensource.org/licenses/MIT |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Icewind\SMB\Native; |
|
9
|
|
|
|
|
10
|
|
|
use Icewind\SMB\StringBuffer; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Stream optimized for read only usage |
|
14
|
|
|
*/ |
|
15
|
|
|
class NativeReadStream extends NativeStream { |
|
16
|
|
|
const CHUNK_SIZE = 1048576; // 1MB chunks |
|
17
|
|
|
|
|
18
|
|
|
/** @var StringBuffer */ |
|
19
|
|
|
private $readBuffer; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct() { |
|
22
|
|
|
$this->readBuffer = new StringBuffer(); |
|
23
|
|
|
} |
|
24
|
40 |
|
|
|
25
|
40 |
|
/** @var int */ |
|
26
|
|
|
private $pos = 0; |
|
27
|
40 |
|
|
|
28
|
|
|
public function stream_open($path, $mode, $options, &$opened_path) { |
|
29
|
|
|
return parent::stream_open($path, $mode, $options, $opened_path); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Wrap a stream from libsmbclient-php into a regular php stream |
|
34
|
|
|
* |
|
35
|
|
|
* @param NativeState $state |
|
36
|
|
|
* @param resource $smbStream |
|
37
|
|
|
* @param string $mode |
|
38
|
|
|
* @param string $url |
|
39
|
40 |
|
* @return resource |
|
40
|
40 |
|
*/ |
|
41
|
40 |
|
public static function wrap(NativeState $state, $smbStream, string $mode, string $url) { |
|
42
|
|
|
return parent::wrapClass($state, $smbStream, $mode, $url, NativeReadStream::class); |
|
|
|
|
|
|
43
|
40 |
|
} |
|
44
|
40 |
|
|
|
45
|
40 |
|
public function stream_read($count) { |
|
46
|
|
|
// php reads 8192 bytes at once |
|
47
|
|
|
// however due to network latency etc, it's faster to read in larger chunks |
|
48
|
40 |
|
// and buffer the result |
|
49
|
40 |
|
if (!parent::stream_eof() && $this->readBuffer->remaining() < $count) { |
|
|
|
|
|
|
50
|
40 |
|
$chunk = parent::stream_read(self::CHUNK_SIZE); |
|
51
|
|
|
if ($chunk === false) { |
|
52
|
|
|
return false; |
|
53
|
38 |
|
} |
|
54
|
|
|
$this->readBuffer->push($chunk); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
38 |
|
$result = $this->readBuffer->read($count); |
|
58
|
38 |
|
|
|
59
|
38 |
|
$read = strlen($result); |
|
60
|
38 |
|
$this->pos += $read; |
|
61
|
38 |
|
|
|
62
|
38 |
|
return $result; |
|
63
|
38 |
|
} |
|
64
|
|
|
|
|
65
|
|
View Code Duplication |
public function stream_seek($offset, $whence = SEEK_SET) { |
|
|
|
|
|
|
66
|
38 |
|
$result = parent::stream_seek($offset, $whence); |
|
67
|
38 |
|
if ($result) { |
|
68
|
|
|
$this->readBuffer->clear(); |
|
69
|
38 |
|
$pos = parent::stream_tell(); |
|
|
|
|
|
|
70
|
38 |
|
if ($pos === false) { |
|
71
|
|
|
return false; |
|
72
|
38 |
|
} |
|
73
|
|
|
$this->pos = $pos; |
|
74
|
|
|
} |
|
75
|
2 |
|
return $result; |
|
76
|
2 |
|
} |
|
77
|
2 |
|
|
|
78
|
2 |
|
public function stream_eof() { |
|
79
|
2 |
|
return $this->readBuffer->remaining() <= 0 && parent::stream_eof(); |
|
80
|
2 |
|
} |
|
81
|
|
|
|
|
82
|
2 |
|
public function stream_tell() { |
|
83
|
|
|
return $this->pos; |
|
84
|
|
|
} |
|
85
|
38 |
|
|
|
86
|
38 |
|
public function stream_write($data) { |
|
87
|
|
|
return false; |
|
88
|
|
|
} |
|
89
|
2 |
|
|
|
90
|
2 |
|
public function stream_truncate($size) { |
|
91
|
|
|
return false; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.