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 write only usage |
14
|
|
|
*/ |
15
|
|
|
class NativeWriteStream extends NativeStream { |
16
|
|
|
const CHUNK_SIZE = 1048576; // 1MB chunks |
17
|
|
|
|
18
|
|
|
/** @var StringBuffer */ |
19
|
|
|
private $writeBuffer; |
20
|
|
|
|
21
|
|
|
/** @var int */ |
22
|
|
|
private $pos = 0; |
23
|
|
|
|
24
|
42 |
|
public function __construct() { |
25
|
42 |
|
$this->writeBuffer = new StringBuffer(); |
26
|
|
|
} |
27
|
42 |
|
|
28
|
|
|
public function stream_open($path, $mode, $options, &$opened_path): bool { |
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
|
42 |
|
* @return resource |
40
|
42 |
|
*/ |
41
|
42 |
View Code Duplication |
public static function wrap($state, $smbStream, $mode, $url) { |
42
|
|
|
stream_wrapper_register('nativesmb', NativeWriteStream::class); |
43
|
42 |
|
$context = stream_context_create([ |
44
|
42 |
|
'nativesmb' => [ |
45
|
42 |
|
'state' => $state, |
46
|
|
|
'handle' => $smbStream, |
47
|
|
|
'url' => $url |
48
|
42 |
|
] |
49
|
42 |
|
]); |
50
|
42 |
|
$fh = fopen('nativesmb://', $mode, false, $context); |
51
|
|
|
stream_wrapper_unregister('nativesmb'); |
52
|
|
|
return $fh; |
53
|
2 |
|
} |
54
|
2 |
|
|
55
|
2 |
View Code Duplication |
public function stream_seek($offset, $whence = SEEK_SET) { |
|
|
|
|
56
|
2 |
|
$this->flushWrite(); |
57
|
2 |
|
$result = parent::stream_seek($offset, $whence); |
58
|
|
|
if ($result) { |
59
|
2 |
|
$pos = parent::stream_tell(); |
|
|
|
|
60
|
|
|
if ($pos === false) { |
61
|
|
|
return false; |
62
|
42 |
|
} |
63
|
42 |
|
$this->pos = $pos; |
64
|
42 |
|
} |
65
|
42 |
|
return $result; |
66
|
42 |
|
} |
67
|
42 |
|
|
68
|
|
|
private function flushWrite(): void { |
69
|
38 |
|
$this->state->write($this->handle, $this->writeBuffer->flush(), $this->url); |
70
|
38 |
|
} |
71
|
38 |
|
|
72
|
38 |
|
public function stream_write($data) { |
73
|
|
|
$written = $this->writeBuffer->push($data); |
74
|
38 |
|
$this->pos += $written; |
75
|
|
|
|
76
|
|
|
if ($this->writeBuffer->remaining() >= self::CHUNK_SIZE) { |
77
|
|
|
$this->flushWrite(); |
78
|
38 |
|
} |
79
|
|
|
|
80
|
|
|
return $written; |
81
|
42 |
|
} |
82
|
|
|
|
83
|
42 |
|
public function stream_close() { |
84
|
42 |
|
try { |
85
|
|
|
$this->flushWrite(); |
86
|
|
|
$flushResult = true; |
87
|
|
|
} catch (\Exception $e) { |
88
|
42 |
|
$flushResult = false; |
89
|
|
|
} |
90
|
|
|
return parent::stream_close() && $flushResult; |
91
|
2 |
|
} |
92
|
2 |
|
|
93
|
|
|
public function stream_tell() { |
94
|
|
|
return $this->pos; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function stream_read($count) { |
98
|
|
|
return false; |
99
|
2 |
|
} |
100
|
2 |
|
|
101
|
2 |
|
public function stream_truncate($size) { |
102
|
2 |
|
$this->flushWrite(); |
103
|
|
|
$this->pos = $size; |
104
|
|
|
return parent::stream_truncate($size); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.