Completed
Pull Request — master (#70)
by Raffael
10:29
created

NativeShare::append()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
ccs 1
cts 1
cp 1
rs 10
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 255
	public function __construct($server, $name) {
40 255
		parent::__construct();
41 255
		$this->server = $server;
42 255
		$this->name = $name;
43 255
	}
44
45
	/**
46
	 * @throws \Icewind\SMB\Exception\ConnectionException
47
	 * @throws \Icewind\SMB\Exception\AuthenticationException
48
	 * @throws \Icewind\SMB\Exception\InvalidHostException
49
	 */
50 255
	protected function getState() {
51 255
		if ($this->state and $this->state instanceof NativeState) {
52 255
			return $this->state;
53
		}
54
55 255
		$this->state = new NativeState();
56 255
		$this->state->init($this->server->getAuth(), $this->server->getOptions());
57 255
		return $this->state;
58
	}
59
60
	/**
61
	 * Get the name of the share
62
	 *
63
	 * @return string
64
	 */
65 1
	public function getName() {
66 1
		return $this->name;
67
	}
68
69 255
	private function buildUrl($path) {
70 255
		$this->verifyPath($path);
71 255
		$url = sprintf('smb://%s/%s', $this->server->getHost(), $this->name);
72 255
		if ($path) {
73 255
			$path = trim($path, '/');
74 255
			$url .= '/';
75 255
			$url .= implode('/', array_map('rawurlencode', explode('/', $path)));
76 255
		}
77 255
		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 255
	public function dir($path) {
90 255
		$files = array();
91
92 255
		$dh = $this->getState()->opendir($this->buildUrl($path));
93 255
		while ($file = $this->getState()->readdir($dh)) {
94 255
			$name = $file['name'];
95 255
			if ($name !== '.' and $name !== '..') {
96 102
				$files [] = new NativeFileInfo($this, $path . '/' . $name, $name);
97 102
			}
98 255
		}
99
100 255
		$this->getState()->closedir($dh);
101 255
		return $files;
102
	}
103
104
	/**
105
	 * @param string $path
106
	 * @return \Icewind\SMB\IFileInfo
107
	 */
108 33
	public function stat($path) {
109 33
		return new NativeFileInfo($this, $path, basename($path), $this->getStat($path));
110
	}
111
112 114
    /**
113 114
     * Get fstat
114
     *
115
     * @param string $path
116
     * @return array
117
     */
118
	public function getStat($path) {
119
		return $this->getState()->stat($this->buildUrl($path));
120
	}
121
122
	/**
123
	 * Create a folder on the share
124
	 *
125 255
	 * @param string $path
126 255
	 * @return bool
127
	 *
128
	 * @throws \Icewind\SMB\Exception\NotFoundException
129
	 * @throws \Icewind\SMB\Exception\AlreadyExistsException
130
	 */
131
	public function mkdir($path) {
132
		return $this->getState()->mkdir($this->buildUrl($path));
133
	}
134
135
	/**
136
	 * Remove a folder on the share
137
	 *
138 255
	 * @param string $path
139 255
	 * @return bool
140
	 *
141
	 * @throws \Icewind\SMB\Exception\NotFoundException
142
	 * @throws \Icewind\SMB\Exception\InvalidTypeException
143
	 */
144
	public function rmdir($path) {
145
		return $this->getState()->rmdir($this->buildUrl($path));
146
	}
147
148
	/**
149
	 * Delete a file on the share
150
	 *
151 133
	 * @param string $path
152 133
	 * @return bool
153
	 *
154
	 * @throws \Icewind\SMB\Exception\NotFoundException
155
	 * @throws \Icewind\SMB\Exception\InvalidTypeException
156
	 */
157
	public function del($path) {
158
		return $this->getState()->unlink($this->buildUrl($path));
159
	}
160
161
	/**
162
	 * Rename a remote file
163
	 *
164
	 * @param string $from
165 28
	 * @param string $to
166 28
	 * @return bool
167
	 *
168
	 * @throws \Icewind\SMB\Exception\NotFoundException
169
	 * @throws \Icewind\SMB\Exception\AlreadyExistsException
170
	 */
171
	public function rename($from, $to) {
172
		return $this->getState()->rename($this->buildUrl($from), $this->buildUrl($to));
173
	}
174
175
	/**
176
	 * Upload a local file
177
	 *
178
	 * @param string $source local file
179 113
	 * @param string $target remove file
180 113
	 * @return bool
181 113
	 *
182
	 * @throws \Icewind\SMB\Exception\NotFoundException
183 102
	 * @throws \Icewind\SMB\Exception\InvalidTypeException
184 102
	 */
185 102
	public function put($source, $target) {
186 102
		$sourceHandle = fopen($source, 'rb');
187 102
		$targetHandle = $this->getState()->create($this->buildUrl($target));
188
189
		while ($data = fread($sourceHandle, NativeReadStream::CHUNK_SIZE)) {
190
			$this->getState()->write($targetHandle, $data);
191
		}
192
		$this->getState()->close($targetHandle);
193
		return true;
194
	}
195
196
	/**
197
	 * Download a remote file
198
	 *
199
	 * @param string $source remove file
200
	 * @param string $target local file
201
	 * @return bool
202 44
	 *
203 44
	 * @throws \Icewind\SMB\Exception\NotFoundException
204 9
	 * @throws \Icewind\SMB\Exception\InvalidTypeException
205
	 * @throws \Icewind\SMB\Exception\InvalidPathException
206 35
	 * @throws \Icewind\SMB\Exception\InvalidResourceException
207 35
	 */
208 1
	public function get($source, $target) {
209 1
		if (!$target) {
210 1
			throw new InvalidPathException('Invalid target path: Filename cannot be empty');
211 1
		}
212
		$targetHandle = @fopen($target, 'wb');
213
		if (!$targetHandle) {
214 1
			$error = error_get_last();
215
			if (is_array($error)) {
216
				$reason = $error['message'];
217 34
			} else {
218 32
				$reason = 'Unknown error';
219
			}
220
			throw new InvalidResourceException('Failed opening local file "' . $target . '" for writing: ' . $reason);
221
		}
222
223 32
		$sourceHandle = $this->getState()->open($this->buildUrl($source), 'r');
224 32
		if (!$sourceHandle) {
225 32
			fclose($targetHandle);
226 32
			throw new InvalidResourceException('Failed opening remote file "' . $source . '" for reading');
227 32
		}
228
229
		while ($data = $this->getState()->read($sourceHandle, NativeReadStream::CHUNK_SIZE)) {
230
			fwrite($targetHandle, $data);
231
		}
232
		$this->getState()->close($sourceHandle);
233
		return true;
234
	}
235
236
	/**
237
	 * Open a readable stream to a remote file
238
	 *
239 29
	 * @param string $source
240 29
	 * @return resource a read only stream with the contents of the remote file
241 20
	 *
242 20
	 * @throws \Icewind\SMB\Exception\NotFoundException
243
	 * @throws \Icewind\SMB\Exception\InvalidTypeException
244
	 */
245
	public function read($source) {
246
		$url = $this->buildUrl($source);
247
		$handle = $this->getState()->open($url, 'r');
248
		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...
249
	}
250
251
	/**
252
	 * Open a writeable stream to a remote file
253
	 * Note: This method will truncate the file to 0bytes first
254 29
	 *
255 29
	 * @param string $source
256 20
	 * @return resource a writeable stream
257 20
	 *
258
	 * @throws \Icewind\SMB\Exception\NotFoundException
259
	 * @throws \Icewind\SMB\Exception\InvalidTypeException
260
	 */
261
	public function write($source) {
262
		$url = $this->buildUrl($source);
263
		$handle = $this->getState()->create($url);
264
		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...
265
	}
266
267 14
	/**
268 14
	 * Open a writeable stream and set the cursor to the end of the stream
269
 	 *
270
 	 * @param string $source
271
	 * @return resource a writeable stream
272
	 *
273
	 * @throws \Icewind\SMB\Exception\NotFoundException
274
 	 * @throws \Icewind\SMB\Exception\InvalidTypeException
275
 	 */
276
	public function append($source) {
277
 		$url = $this->buildUrl($source);
278
 		$handle = $this->getState()->open($url, "a");
279 8
 		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...
280
	}
281 8
282 8
	/**
283 8
	 * Get extended attributes for the path
284
	 *
285 8
	 * @param string $path
286
	 * @param string $attribute attribute to get the info
287
	 * @return string the attribute value
288
	 */
289
	public function getAttribute($path, $attribute) {
290
		return $this->getState()->getxattr($this->buildUrl($path), $attribute);
291
	}
292
293 8
	/**
294 8
	 * Set extended attributes for the given path
295
	 *
296
	 * @param string $path
297
	 * @param string $attribute attribute to get the info
298
	 * @param mixed $value
299
	 * @return string the attribute value
300
	 */
301
	public function setAttribute($path, $attribute, $value) {
302
303
		if ($attribute === 'system.dos_attr.mode' and is_int($value)) {
304
			$value = '0x' . dechex($value);
305
		}
306
307
		return $this->getState()->setxattr($this->buildUrl($path), $attribute, $value);
308
	}
309
310
	/**
311 255
	 * Set DOS comaptible node mode
312 255
	 *
313 255
	 * @param string $path
314
	 * @param int $mode a combination of FileInfo::MODE_READONLY, FileInfo::MODE_ARCHIVE, FileInfo::MODE_SYSTEM and FileInfo::MODE_HIDDEN, FileInfo::NORMAL
315
	 * @return mixed
316
	 */
317
	public function setMode($path, $mode) {
318
		return $this->setAttribute($path, 'system.dos_attr.mode', $mode);
319
	}
320
321
	/**
322
	 * Start smb notify listener
323
	 * Note: This is a blocking call
324
	 *
325
	 * @param string $path
326
	 * @return INotifyHandler
327
	 */
328
	public function notify($path) {
329
		// php-smbclient does not support notify (https://github.com/eduardok/libsmbclient-php/issues/29)
330
		// so we use the smbclient based backend for this
331
		if (!Server::available($this->server->getSystem())) {
332
			throw new DependencyException('smbclient not found in path for notify command');
333
		}
334
		$share = new Share($this->server, $this->getName(), $this->server->getSystem());
335
		return $share->notify($path);
336
	}
337
338
	public function __destruct() {
339
		unset($this->state);
340
	}
341
}
342