Passed
Push — master ( 441132...5330ed )
by Robin
20:27 queued 05:26
created

NativeShare::getStat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
rs 10
c 1
b 0
f 0
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 254
	public function __construct($server, $name) {
40 254
		parent::__construct();
41 254
		$this->server = $server;
42 254
		$this->name = $name;
43 254
	}
44
45
	/**
46
	 * @throws \Icewind\SMB\Exception\ConnectionException
47
	 * @throws \Icewind\SMB\Exception\AuthenticationException
48
	 * @throws \Icewind\SMB\Exception\InvalidHostException
49
	 */
50 254
	protected function getState() {
51 254
		if ($this->state and $this->state instanceof NativeState) {
52 254
			return $this->state;
53
		}
54
55 254
		$this->state = new NativeState();
56 254
		$this->state->init($this->server->getAuth(), $this->server->getOptions());
57 254
		return $this->state;
58
	}
59
60
	/**
61
	 * Get the name of the share
62
	 *
63
	 * @return string
64
	 */
65
	public function getName() {
66
		return $this->name;
67
	}
68
69 254
	private function buildUrl($path) {
70 254
		$this->verifyPath($path);
71 254
		$url = sprintf('smb://%s/%s', $this->server->getHost(), $this->name);
72 254
		if ($path) {
73 254
			$path = trim($path, '/');
74 254
			$url .= '/';
75 254
			$url .= implode('/', array_map('rawurlencode', explode('/', $path)));
76
		}
77 254
		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 254
	public function dir($path) {
90 254
		$files = [];
91
92 254
		$dh = $this->getState()->opendir($this->buildUrl($path));
93 254
		while ($file = $this->getState()->readdir($dh)) {
94 254
			$name = $file['name'];
95 254
			if ($name !== '.' and $name !== '..') {
96 102
				$fullPath = $path . '/' . $name;
97 102
				$files [] = new NativeFileInfo($this, $fullPath, $name);
98
			}
99
		}
100
101 254
		$this->getState()->closedir($dh);
102 254
		return $files;
103
	}
104
105
	/**
106
	 * @param string $path
107
	 * @return \Icewind\SMB\IFileInfo
108
	 */
109 32
	public function stat($path) {
110 32
		$info = new NativeFileInfo($this, $path, self::mb_basename($path));
111
112
		// trigger attribute loading
113 32
		$info->getSize();
114
115 22
		return $info;
116
	}
117
118
	/**
119
	 * Multibyte unicode safe version of basename()
120
	 *
121
	 * @param string $path
122
	 * @link http://php.net/manual/en/function.basename.php#121405
123
	 * @return string
124
	 */
125 32
	protected static function mb_basename($path) {
126 32
		if (preg_match('@^.*[\\\\/]([^\\\\/]+)$@s', $path, $matches)) {
127 22
			return $matches[1];
128 10
		} elseif (preg_match('@^([^\\\\/]+)$@s', $path, $matches)) {
129 9
			return $matches[1];
130
		}
131
132 1
		return '';
133
	}
134
135
	/**
136
	 * Create a folder on the share
137
	 *
138
	 * @param string $path
139
	 * @return bool
140
	 *
141
	 * @throws \Icewind\SMB\Exception\NotFoundException
142
	 * @throws \Icewind\SMB\Exception\AlreadyExistsException
143
	 */
144
	public function mkdir($path) {
145
		return $this->getState()->mkdir($this->buildUrl($path));
146
	}
147
148 254
	/**
149 254
	 * Remove a folder on the share
150
	 *
151
	 * @param string $path
152
	 * @return bool
153
	 *
154
	 * @throws \Icewind\SMB\Exception\NotFoundException
155
	 * @throws \Icewind\SMB\Exception\InvalidTypeException
156
	 */
157
	public function rmdir($path) {
158
		return $this->getState()->rmdir($this->buildUrl($path));
159
	}
160
161 254
	/**
162 254
	 * Delete a file on the share
163
	 *
164
	 * @param string $path
165
	 * @return bool
166
	 *
167
	 * @throws \Icewind\SMB\Exception\NotFoundException
168
	 * @throws \Icewind\SMB\Exception\InvalidTypeException
169
	 */
170
	public function del($path) {
171
		return $this->getState()->unlink($this->buildUrl($path));
172
	}
173
174 133
	/**
175 133
	 * Rename a remote file
176
	 *
177
	 * @param string $from
178
	 * @param string $to
179
	 * @return bool
180
	 *
181
	 * @throws \Icewind\SMB\Exception\NotFoundException
182
	 * @throws \Icewind\SMB\Exception\AlreadyExistsException
183
	 */
184
	public function rename($from, $to) {
185
		return $this->getState()->rename($this->buildUrl($from), $this->buildUrl($to));
186
	}
187
188 28
	/**
189 28
	 * Upload a local file
190
	 *
191
	 * @param string $source local file
192
	 * @param string $target remove file
193
	 * @return bool
194
	 *
195
	 * @throws \Icewind\SMB\Exception\NotFoundException
196
	 * @throws \Icewind\SMB\Exception\InvalidTypeException
197
	 */
198
	public function put($source, $target) {
199
		$sourceHandle = fopen($source, 'rb');
200
		$targetHandle = $this->getState()->create($this->buildUrl($target));
201
202 112
		while ($data = fread($sourceHandle, NativeReadStream::CHUNK_SIZE)) {
0 ignored issues
show
Bug introduced by
It seems like $sourceHandle can also be of type false; however, parameter $handle of fread() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

202
		while ($data = fread(/** @scrutinizer ignore-type */ $sourceHandle, NativeReadStream::CHUNK_SIZE)) {
Loading history...
203 112
			$this->getState()->write($targetHandle, $data);
204 112
		}
205
		$this->getState()->close($targetHandle);
206 101
		return true;
207 101
	}
208
209 101
	/**
210 101
	 * Download a remote file
211
	 *
212
	 * @param string $source remove file
213
	 * @param string $target local file
214
	 * @return bool
215
	 *
216
	 * @throws \Icewind\SMB\Exception\NotFoundException
217
	 * @throws \Icewind\SMB\Exception\InvalidTypeException
218
	 * @throws \Icewind\SMB\Exception\InvalidPathException
219
	 * @throws \Icewind\SMB\Exception\InvalidResourceException
220
	 */
221
	public function get($source, $target) {
222
		if (!$target) {
223
			throw new InvalidPathException('Invalid target path: Filename cannot be empty');
224
		}
225 45
226 45
		$sourceHandle = $this->getState()->open($this->buildUrl($source), 'r');
227 9
		if (!$sourceHandle) {
0 ignored issues
show
introduced by
$sourceHandle is of type resource, thus it always evaluated to false.
Loading history...
228
			throw new InvalidResourceException('Failed opening remote file "' . $source . '" for reading');
229
		}
230 36
231 34
		$targetHandle = @fopen($target, 'wb');
232
		if (!$targetHandle) {
233
			$error = error_get_last();
234
			if (is_array($error)) {
235 34
				$reason = $error['message'];
236 34
			} else {
237 1
				$reason = 'Unknown error';
238 1
			}
239 1
			$this->getState()->close($sourceHandle);
240
			throw new InvalidResourceException('Failed opening local file "' . $target . '" for writing: ' . $reason);
241
		}
242
243 1
		while ($data = $this->getState()->read($sourceHandle, NativeReadStream::CHUNK_SIZE)) {
244 1
			fwrite($targetHandle, $data);
245
		}
246
		$this->getState()->close($sourceHandle);
247 33
		return true;
248 33
	}
249
250 33
	/**
251 33
	 * Open a readable stream to a remote file
252
	 *
253
	 * @param string $source
254
	 * @return resource a read only stream with the contents of the remote file
255
	 *
256
	 * @throws \Icewind\SMB\Exception\NotFoundException
257
	 * @throws \Icewind\SMB\Exception\InvalidTypeException
258
	 */
259
	public function read($source) {
260
		$url = $this->buildUrl($source);
261
		$handle = $this->getState()->open($url, 'r');
262
		return NativeReadStream::wrap($this->getState(), $handle, 'r', $url);
263 29
	}
264 29
265 20
	/**
266 20
	 * Open a writeable stream to a remote file
267
	 * Note: This method will truncate the file to 0bytes first
268
	 *
269
	 * @param string $source
270
	 * @return resource a writeable stream
271
	 *
272
	 * @throws \Icewind\SMB\Exception\NotFoundException
273
	 * @throws \Icewind\SMB\Exception\InvalidTypeException
274
	 */
275
	public function write($source) {
276
		$url = $this->buildUrl($source);
277
		$handle = $this->getState()->create($url);
278
		return NativeWriteStream::wrap($this->getState(), $handle, 'w', $url);
279 29
	}
280 29
281 20
	/**
282 20
	 * Open a writeable stream and set the cursor to the end of the stream
283
	 *
284
	 * @param string $source
285
	 * @return resource a writeable stream
286
	 *
287
	 * @throws \Icewind\SMB\Exception\NotFoundException
288
	 * @throws \Icewind\SMB\Exception\InvalidTypeException
289
	 */
290
	public function append($source) {
291
		$url = $this->buildUrl($source);
292
		$handle = $this->getState()->open($url, "a+");
293
		return NativeWriteStream::wrap($this->getState(), $handle, "a", $url);
294 1
	}
295 1
296 1
	/**
297 1
	 * Get extended attributes for the path
298
	 *
299
	 * @param string $path
300
	 * @param string $attribute attribute to get the info
301
	 * @return string the attribute value
302
	 */
303
	public function getAttribute($path, $attribute) {
304
		return $this->getState()->getxattr($this->buildUrl($path), $attribute);
305
	}
306
307 113
	/**
308 113
	 * Set extended attributes for the given path
309
	 *
310
	 * @param string $path
311
	 * @param string $attribute attribute to get the info
312
	 * @param string|int $value
313
	 * @return mixed the attribute value
314
	 */
315
	public function setAttribute($path, $attribute, $value) {
316
		if ($attribute === 'system.dos_attr.mode' and is_int($value)) {
317
			$value = '0x' . dechex($value);
318
		}
319 8
320 8
		return $this->getState()->setxattr($this->buildUrl($path), $attribute, $value);
321 8
	}
322
323
	/**
324 8
	 * Set DOS comaptible node mode
325
	 *
326
	 * @param string $path
327
	 * @param int $mode a combination of FileInfo::MODE_READONLY, FileInfo::MODE_ARCHIVE, FileInfo::MODE_SYSTEM and FileInfo::MODE_HIDDEN, FileInfo::NORMAL
328
	 * @return mixed
329
	 */
330
	public function setMode($path, $mode) {
331
		return $this->setAttribute($path, 'system.dos_attr.mode', $mode);
332
	}
333
334 8
	/**
335 8
	 * Start smb notify listener
336
	 * Note: This is a blocking call
337
	 *
338
	 * @param string $path
339
	 * @return INotifyHandler
340
	 */
341
	public function notify($path) {
342
		// php-smbclient does not support notify (https://github.com/eduardok/libsmbclient-php/issues/29)
343
		// so we use the smbclient based backend for this
344
		if (!Server::available($this->server->getSystem())) {
345
			throw new DependencyException('smbclient not found in path for notify command');
346
		}
347
		$share = new Share($this->server, $this->getName(), $this->server->getSystem());
348
		return $share->notify($path);
349
	}
350
351
	public function __destruct() {
352
		unset($this->state);
353
	}
354
}
355