Completed
Pull Request — master (#70)
by Raffael
13:44
created

NativeShare::append()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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\AbstractShare;
11
use Icewind\SMB\Exception\DependencyException;
12
use Icewind\SMB\Exception\InvalidPathException;
13
use Icewind\SMB\Exception\InvalidResourceException;
14
use Icewind\SMB\INotifyHandler;
15
use Icewind\SMB\IServer;
16
use Icewind\SMB\Wrapped\Server;
17
use Icewind\SMB\Wrapped\Share;
18
19
class NativeShare extends AbstractShare {
20
	/**
21
	 * @var IServer $server
22
	 */
23
	private $server;
24
25
	/**
26
	 * @var string $name
27
	 */
28
	private $name;
29
30
	/**
31
	 * @var NativeState $state
32
	 */
33
	private $state;
34
35
	/**
36
	 * @param IServer $server
37
	 * @param string $name
38
	 */
39 510
	public function __construct($server, $name) {
40 510
		parent::__construct();
41 510
		$this->server = $server;
42 510
		$this->name = $name;
43 510
	}
44
45
	/**
46
	 * @throws \Icewind\SMB\Exception\ConnectionException
47
	 * @throws \Icewind\SMB\Exception\AuthenticationException
48
	 * @throws \Icewind\SMB\Exception\InvalidHostException
49
	 */
50 510
	protected function getState() {
51 510
		if ($this->state and $this->state instanceof NativeState) {
52 510
			return $this->state;
53
		}
54
55 510
		$this->state = new NativeState();
56 510
		$this->state->init($this->server->getAuth(), $this->server->getOptions());
57 510
		return $this->state;
58
	}
59
60
	/**
61
	 * Get the name of the share
62
	 *
63
	 * @return string
64
	 */
65 2
	public function getName() {
66 2
		return $this->name;
67
	}
68
69 510
	private function buildUrl($path) {
70 510
		$this->verifyPath($path);
71 510
		$url = sprintf('smb://%s/%s', $this->server->getHost(), $this->name);
72 510
		if ($path) {
73 510
			$path = trim($path, '/');
74 510
			$url .= '/';
75 510
			$url .= implode('/', array_map('rawurlencode', explode('/', $path)));
76 255
		}
77 510
		return $url;
78
	}
79
80
	/**
81
	 * List the content of a remote folder
82
	 *
83
	 * @param string $path
84
	 * @return \Icewind\SMB\IFileInfo[]
85
	 *
86
	 * @throws \Icewind\SMB\Exception\NotFoundException
87
	 * @throws \Icewind\SMB\Exception\InvalidTypeException
88
	 */
89 510
	public function dir($path) {
90 510
		$files = [];
91
92 510
		$dh = $this->getState()->opendir($this->buildUrl($path));
93 510
		while ($file = $this->getState()->readdir($dh)) {
94 510
			$name = $file['name'];
95 510
			if ($name !== '.' and $name !== '..') {
96 204
				$fullPath = $path . '/' . $name;
97 204
				$files [] = new NativeFileInfo($this, $fullPath, $name, function () use ($fullPath) {
98 204
					return $this->getStat($fullPath);
99 204
				});
100 102
			}
101 255
		}
102
103 510
		$this->getState()->closedir($dh);
104 510
		return $files;
105
	}
106
107
	/**
108
	 * @param string $path
109
	 * @return \Icewind\SMB\IFileInfo
110
	 */
111 66
	public function stat($path) {
112 66
		return new NativeFileInfo($this, $path, basename($path), $this->getStat($path));
113
	}
114
115 228
	/**
116 228
	 * Get fstat
117
	 *
118
	 * @param string $path
119
	 * @return array
120
	 */
121
	private function getStat($path) {
122
		return $this->getState()->stat($this->buildUrl($path));
123
	}
124
125
	/**
126
	 * Create a folder on the share
127
	 *
128 510
	 * @param string $path
129 510
	 * @return bool
130
	 *
131
	 * @throws \Icewind\SMB\Exception\NotFoundException
132
	 * @throws \Icewind\SMB\Exception\AlreadyExistsException
133
	 */
134
	public function mkdir($path) {
135
		return $this->getState()->mkdir($this->buildUrl($path));
136
	}
137
138
	/**
139
	 * Remove a folder on the share
140
	 *
141 510
	 * @param string $path
142 510
	 * @return bool
143
	 *
144
	 * @throws \Icewind\SMB\Exception\NotFoundException
145
	 * @throws \Icewind\SMB\Exception\InvalidTypeException
146
	 */
147
	public function rmdir($path) {
148
		return $this->getState()->rmdir($this->buildUrl($path));
149
	}
150
151
	/**
152
	 * Delete a file on the share
153
	 *
154 266
	 * @param string $path
155 266
	 * @return bool
156
	 *
157
	 * @throws \Icewind\SMB\Exception\NotFoundException
158
	 * @throws \Icewind\SMB\Exception\InvalidTypeException
159
	 */
160
	public function del($path) {
161
		return $this->getState()->unlink($this->buildUrl($path));
162
	}
163
164
	/**
165
	 * Rename a remote file
166
	 *
167
	 * @param string $from
168 56
	 * @param string $to
169 56
	 * @return bool
170
	 *
171
	 * @throws \Icewind\SMB\Exception\NotFoundException
172
	 * @throws \Icewind\SMB\Exception\AlreadyExistsException
173
	 */
174
	public function rename($from, $to) {
175
		return $this->getState()->rename($this->buildUrl($from), $this->buildUrl($to));
176
	}
177
178
	/**
179
	 * Upload a local file
180
	 *
181
	 * @param string $source local file
182 226
	 * @param string $target remove file
183 226
	 * @return bool
184 226
	 *
185
	 * @throws \Icewind\SMB\Exception\NotFoundException
186 204
	 * @throws \Icewind\SMB\Exception\InvalidTypeException
187 204
	 */
188 102
	public function put($source, $target) {
189 204
		$sourceHandle = fopen($source, 'rb');
190 204
		$targetHandle = $this->getState()->create($this->buildUrl($target));
191
192
		while ($data = fread($sourceHandle, NativeReadStream::CHUNK_SIZE)) {
193
			$this->getState()->write($targetHandle, $data);
194
		}
195
		$this->getState()->close($targetHandle);
196
		return true;
197
	}
198
199
	/**
200
	 * Download a remote file
201
	 *
202
	 * @param string $source remove file
203
	 * @param string $target local file
204
	 * @return bool
205 88
	 *
206 88
	 * @throws \Icewind\SMB\Exception\NotFoundException
207 18
	 * @throws \Icewind\SMB\Exception\InvalidTypeException
208
	 * @throws \Icewind\SMB\Exception\InvalidPathException
209 70
	 * @throws \Icewind\SMB\Exception\InvalidResourceException
210 70
	 */
211 2
	public function get($source, $target) {
212 2
		if (!$target) {
213 2
			throw new InvalidPathException('Invalid target path: Filename cannot be empty');
214 1
		}
215
		$targetHandle = @fopen($target, 'wb');
216
		if (!$targetHandle) {
217 2
			$error = error_get_last();
218
			if (is_array($error)) {
219
				$reason = $error['message'];
220 68
			} else {
221 64
				$reason = 'Unknown error';
222
			}
223
			throw new InvalidResourceException('Failed opening local file "' . $target . '" for writing: ' . $reason);
224
		}
225
226 64
		$sourceHandle = $this->getState()->open($this->buildUrl($source), 'r');
227 64
		if (!$sourceHandle) {
228 32
			fclose($targetHandle);
229 64
			throw new InvalidResourceException('Failed opening remote file "' . $source . '" for reading');
230 64
		}
231
232
		while ($data = $this->getState()->read($sourceHandle, NativeReadStream::CHUNK_SIZE)) {
233
			fwrite($targetHandle, $data);
234
		}
235
		$this->getState()->close($sourceHandle);
236
		return true;
237
	}
238
239
	/**
240
	 * Open a readable stream to a remote file
241
	 *
242 58
	 * @param string $source
243 58
	 * @return resource a read only stream with the contents of the remote file
244 40
	 *
245 40
	 * @throws \Icewind\SMB\Exception\NotFoundException
246
	 * @throws \Icewind\SMB\Exception\InvalidTypeException
247
	 */
248
	public function read($source) {
249
		$url = $this->buildUrl($source);
250
		$handle = $this->getState()->open($url, 'r');
251
		return NativeReadStream::wrap($this->getState(), $handle, 'r', $url);
0 ignored issues
show
Documentation introduced by
$this->getState() is of type object<Icewind\SMB\Native\NativeState>, but the function expects a object<Icewind\SMB\NativeState>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
252
	}
253
254
	/**
255
	 * Open a writeable stream to a remote file
256
	 * Note: This method will truncate the file to 0bytes first
257 58
	 *
258 58
	 * @param string $source
259 40
	 * @return resource a writeable stream
260 40
	 *
261
	 * @throws \Icewind\SMB\Exception\NotFoundException
262
	 * @throws \Icewind\SMB\Exception\InvalidTypeException
263
	 */
264
	public function write($source) {
265
		$url = $this->buildUrl($source);
266
		$handle = $this->getState()->create($url);
267
		return NativeWriteStream::wrap($this->getState(), $handle, 'w', $url);
0 ignored issues
show
Documentation introduced by
$this->getState() is of type object<Icewind\SMB\Native\NativeState>, but the function expects a object<Icewind\SMB\NativeState>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
268
	}
269
270 28
	/**
271 28
	 * Open a writeable stream and set the cursor to the end of the stream
272
 	 *
273
 	 * @param string $source
274
	 * @return resource a writeable stream
275
	 *
276
	 * @throws \Icewind\SMB\Exception\NotFoundException
277
 	 * @throws \Icewind\SMB\Exception\InvalidTypeException
278
 	 */
279
	public function append($source) {
280
 		$url = $this->buildUrl($source);
281
 		$handle = $this->getState()->open($url, "a");
282 16
 		return NativeWriteStream::wrap($this->getState(), $handle, "a", $url);
0 ignored issues
show
Documentation introduced by
$this->getState() is of type object<Icewind\SMB\Native\NativeState>, but the function expects a object<Icewind\SMB\NativeState>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
283 16
	}
284 16
285 8
	/**
286
	 * Get extended attributes for the path
287 16
	 *
288
	 * @param string $path
289
	 * @param string $attribute attribute to get the info
290
	 * @return string the attribute value
291
	 */
292
	public function getAttribute($path, $attribute) {
293
		return $this->getState()->getxattr($this->buildUrl($path), $attribute);
294
	}
295 16
296 16
	/**
297
	 * Set extended attributes for the given path
298
	 *
299
	 * @param string $path
300
	 * @param string $attribute attribute to get the info
301
	 * @param string|int $value
302
	 * @return mixed the attribute value
303
	 */
304
	public function setAttribute($path, $attribute, $value) {
305
		if ($attribute === 'system.dos_attr.mode' and is_int($value)) {
306
			$value = '0x' . dechex($value);
307
		}
308
309
		return $this->getState()->setxattr($this->buildUrl($path), $attribute, $value);
310
	}
311
312
	/**
313 510
	 * Set DOS comaptible node mode
314 510
	 *
315 510
	 * @param string $path
316
	 * @param int $mode a combination of FileInfo::MODE_READONLY, FileInfo::MODE_ARCHIVE, FileInfo::MODE_SYSTEM and FileInfo::MODE_HIDDEN, FileInfo::NORMAL
317
	 * @return mixed
318
	 */
319
	public function setMode($path, $mode) {
320
		return $this->setAttribute($path, 'system.dos_attr.mode', $mode);
321
	}
322
323
	/**
324
	 * Start smb notify listener
325
	 * Note: This is a blocking call
326
	 *
327
	 * @param string $path
328
	 * @return INotifyHandler
329
	 */
330
	public function notify($path) {
331
		// php-smbclient does not support notify (https://github.com/eduardok/libsmbclient-php/issues/29)
332
		// so we use the smbclient based backend for this
333
		if (!Server::available($this->server->getSystem())) {
334
			throw new DependencyException('smbclient not found in path for notify command');
335
		}
336
		$share = new Share($this->server, $this->getName(), $this->server->getSystem());
337
		return $share->notify($path);
338
	}
339
340
	public function __destruct() {
341
		unset($this->state);
342
	}
343
}
344