Passed
Push — master ( c25309...927e33 )
by Robin
04:02 queued 29s
created

NativeState::open()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
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\Exception\AlreadyExistsException;
11
use Icewind\SMB\Exception\ConnectionRefusedException;
12
use Icewind\SMB\Exception\ConnectionResetException;
13
use Icewind\SMB\Exception\Exception;
14
use Icewind\SMB\Exception\FileInUseException;
15
use Icewind\SMB\Exception\ForbiddenException;
16
use Icewind\SMB\Exception\HostDownException;
17
use Icewind\SMB\Exception\InvalidArgumentException;
18
use Icewind\SMB\Exception\InvalidTypeException;
19
use Icewind\SMB\Exception\ConnectionAbortedException;
20
use Icewind\SMB\Exception\NoRouteToHostException;
21
use Icewind\SMB\Exception\NotEmptyException;
22
use Icewind\SMB\Exception\NotFoundException;
23
use Icewind\SMB\Exception\OutOfSpaceException;
24
use Icewind\SMB\Exception\TimedOutException;
25
use Icewind\SMB\IAuth;
26
use Icewind\SMB\IOptions;
27
28
/**
29
 * Low level wrapper for libsmbclient-php with error handling
30
 */
31
class NativeState {
32
	/**
33
	 * @var resource
34
	 */
35
	protected $state;
36
37
	protected $handlerSet = false;
38
39
	protected $connected = false;
40
41
	// see error.h
42
	const EXCEPTION_MAP = [
43
		1   => ForbiddenException::class,
44
		2   => NotFoundException::class,
45
		13  => ForbiddenException::class,
46
		16  => FileInUseException::class,
47
		17  => AlreadyExistsException::class,
48
		20  => InvalidTypeException::class,
49
		21  => InvalidTypeException::class,
50
		22  => InvalidArgumentException::class,
51
		28  => OutOfSpaceException::class,
52
		39  => NotEmptyException::class,
53
		103 => ConnectionAbortedException::class,
54
		104 => ConnectionResetException::class,
55
		110 => TimedOutException::class,
56
		111 => ConnectionRefusedException::class,
57
		112 => HostDownException::class,
58
		113 => NoRouteToHostException::class
59
	];
60
61 492
	protected function handleError($path) {
62 492
		$error = smbclient_state_errno($this->state);
0 ignored issues
show
Bug introduced by
The function smbclient_state_errno was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

62
		$error = /** @scrutinizer ignore-call */ smbclient_state_errno($this->state);
Loading history...
63 492
		if ($error === 0) {
64 492
			return;
65
		}
66 38
		throw Exception::fromMap(self::EXCEPTION_MAP, $error, $path);
67
	}
68
69 492
	protected function testResult($result, $uri) {
70 492
		if ($result === false or $result === null) {
71
			// smb://host/share/path
72 492
			if (is_string($uri) && count(explode('/', $uri, 5)) > 4) {
73 38
				list(, , , , $path) = explode('/', $uri, 5);
74 38
				$path = '/' . $path;
75
			} else {
76 492
				$path = null;
77
			}
78 492
			$this->handleError($path);
79
		}
80 492
	}
81
82
	/**
83
	 * @param IAuth $auth
84
	 * @param IOptions $options
85
	 * @return bool
86
	 */
87 492
	public function init(IAuth $auth, IOptions $options) {
88 492
		if ($this->connected) {
89
			return true;
90
		}
91 492
		$this->state = smbclient_state_new();
0 ignored issues
show
Bug introduced by
The function smbclient_state_new was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

91
		$this->state = /** @scrutinizer ignore-call */ smbclient_state_new();
Loading history...
92 492
		smbclient_option_set($this->state, SMBCLIENT_OPT_AUTO_ANONYMOUS_LOGIN, false);
0 ignored issues
show
Bug introduced by
The function smbclient_option_set was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

92
		/** @scrutinizer ignore-call */ 
93
  smbclient_option_set($this->state, SMBCLIENT_OPT_AUTO_ANONYMOUS_LOGIN, false);
Loading history...
Bug introduced by
The constant Icewind\SMB\Native\SMBCL...PT_AUTO_ANONYMOUS_LOGIN was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
93 492
		smbclient_option_set($this->state, SMBCLIENT_OPT_TIMEOUT, $options->getTimeout() * 1000);
0 ignored issues
show
Bug introduced by
The constant Icewind\SMB\Native\SMBCLIENT_OPT_TIMEOUT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
94 492
		$auth->setExtraSmbClientOptions($this->state);
95 492
		$result = @smbclient_state_init($this->state, $auth->getWorkgroup(), $auth->getUsername(), $auth->getPassword());
0 ignored issues
show
Bug introduced by
The function smbclient_state_init was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

95
		$result = @/** @scrutinizer ignore-call */ smbclient_state_init($this->state, $auth->getWorkgroup(), $auth->getUsername(), $auth->getPassword());
Loading history...
96
97 492
		$this->testResult($result, '');
98 492
		$this->connected = true;
99 492
		return $result;
100
	}
101
102
	/**
103
	 * @param string $uri
104
	 * @return resource
105
	 */
106 492
	public function opendir($uri) {
107 492
		$result = @smbclient_opendir($this->state, $uri);
0 ignored issues
show
Bug introduced by
The function smbclient_opendir was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

107
		$result = @/** @scrutinizer ignore-call */ smbclient_opendir($this->state, $uri);
Loading history...
108
109 492
		$this->testResult($result, $uri);
110 492
		return $result;
111
	}
112
113
	/**
114
	 * @param resource $dir
115
	 * @return array
116
	 */
117 492
	public function readdir($dir) {
118 492
		$result = @smbclient_readdir($this->state, $dir);
0 ignored issues
show
Bug introduced by
The function smbclient_readdir was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

118
		$result = @/** @scrutinizer ignore-call */ smbclient_readdir($this->state, $dir);
Loading history...
119
120 492
		$this->testResult($result, $dir);
121 492
		return $result;
122
	}
123
124
	/**
125
	 * @param $dir
126
	 * @return bool
127
	 */
128 492
	public function closedir($dir) {
129 492
		$result = smbclient_closedir($this->state, $dir);
0 ignored issues
show
Bug introduced by
The function smbclient_closedir was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

129
		$result = /** @scrutinizer ignore-call */ smbclient_closedir($this->state, $dir);
Loading history...
130
131 492
		$this->testResult($result, $dir);
132 492
		return $result;
133
	}
134
135
	/**
136
	 * @param string $old
137
	 * @param string $new
138
	 * @return bool
139
	 */
140 38
	public function rename($old, $new) {
141 38
		$result = @smbclient_rename($this->state, $old, $this->state, $new);
0 ignored issues
show
Bug introduced by
The function smbclient_rename was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

141
		$result = @/** @scrutinizer ignore-call */ smbclient_rename($this->state, $old, $this->state, $new);
Loading history...
142
143 38
		$this->testResult($result, $new);
144 32
		return $result;
145
	}
146
147
	/**
148
	 * @param string $uri
149
	 * @return bool
150
	 */
151 232
	public function unlink($uri) {
152 232
		$result = @smbclient_unlink($this->state, $uri);
0 ignored issues
show
Bug introduced by
The function smbclient_unlink was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

152
		$result = @/** @scrutinizer ignore-call */ smbclient_unlink($this->state, $uri);
Loading history...
153
154 232
		$this->testResult($result, $uri);
155 228
		return $result;
156
	}
157
158
	/**
159
	 * @param string $uri
160
	 * @param int $mask
161
	 * @return bool
162
	 */
163 492
	public function mkdir($uri, $mask = 0777) {
164 492
		$result = @smbclient_mkdir($this->state, $uri, $mask);
0 ignored issues
show
Bug introduced by
The function smbclient_mkdir was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

164
		$result = @/** @scrutinizer ignore-call */ smbclient_mkdir($this->state, $uri, $mask);
Loading history...
165
166 492
		$this->testResult($result, $uri);
167 492
		return $result;
168
	}
169
170
	/**
171
	 * @param string $uri
172
	 * @return bool
173
	 */
174 492
	public function rmdir($uri) {
175 492
		$result = @smbclient_rmdir($this->state, $uri);
0 ignored issues
show
Bug introduced by
The function smbclient_rmdir was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

175
		$result = @/** @scrutinizer ignore-call */ smbclient_rmdir($this->state, $uri);
Loading history...
176
177 492
		$this->testResult($result, $uri);
178 492
		return $result;
179
	}
180
181
	/**
182
	 * @param string $uri
183
	 * @return array
184
	 */
185 36
	public function stat($uri) {
186 36
		$result = @smbclient_stat($this->state, $uri);
0 ignored issues
show
Bug introduced by
The function smbclient_stat was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

186
		$result = @/** @scrutinizer ignore-call */ smbclient_stat($this->state, $uri);
Loading history...
187
188 36
		$this->testResult($result, $uri);
189 36
		return $result;
190
	}
191
192
	/**
193
	 * @param resource $file
194
	 * @return array
195
	 */
196
	public function fstat($file) {
197
		$result = @smbclient_fstat($this->state, $file);
0 ignored issues
show
Bug introduced by
The function smbclient_fstat was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

197
		$result = @/** @scrutinizer ignore-call */ smbclient_fstat($this->state, $file);
Loading history...
198
199
		$this->testResult($result, $file);
200
		return $result;
201
	}
202
203
	/**
204
	 * @param string $uri
205
	 * @param string $mode
206
	 * @param int $mask
207
	 * @return resource
208
	 */
209 112
	public function open($uri, $mode, $mask = 0666) {
210 112
		$result = @smbclient_open($this->state, $uri, $mode, $mask);
0 ignored issues
show
Bug introduced by
The function smbclient_open was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

210
		$result = @/** @scrutinizer ignore-call */ smbclient_open($this->state, $uri, $mode, $mask);
Loading history...
211
212 112
		$this->testResult($result, $uri);
213 108
		return $result;
214
	}
215
216
	/**
217
	 * @param string $uri
218
	 * @param int $mask
219
	 * @return resource
220
	 */
221 230
	public function create($uri, $mask = 0666) {
222 230
		$result = @smbclient_creat($this->state, $uri, $mask);
0 ignored issues
show
Bug introduced by
The function smbclient_creat was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

222
		$result = @/** @scrutinizer ignore-call */ smbclient_creat($this->state, $uri, $mask);
Loading history...
223
224 230
		$this->testResult($result, $uri);
225 226
		return $result;
226
	}
227
228
	/**
229
	 * @param resource $file
230
	 * @param int $bytes
231
	 * @return string
232
	 */
233 104
	public function read($file, $bytes) {
234 104
		$result = @smbclient_read($this->state, $file, $bytes);
0 ignored issues
show
Bug introduced by
The function smbclient_read was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

234
		$result = @/** @scrutinizer ignore-call */ smbclient_read($this->state, $file, $bytes);
Loading history...
235
236 104
		$this->testResult($result, $file);
237 104
		return $result;
238
	}
239
240
	/**
241
	 * @param resource $file
242
	 * @param string $data
243
	 * @param string $path
244
	 * @param int $length
245
	 * @return int
246
	 */
247 228
	public function write($file, $data, $path, $length = null) {
248 228
		$result = @smbclient_write($this->state, $file, $data, $length);
0 ignored issues
show
Bug introduced by
The function smbclient_write was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

248
		$result = @/** @scrutinizer ignore-call */ smbclient_write($this->state, $file, $data, $length);
Loading history...
249
250 228
		$this->testResult($result, $path);
251 228
		return $result;
252
	}
253
254
	/**
255
	 * @param resource $file
256
	 * @param int $offset
257
	 * @param int $whence SEEK_SET | SEEK_CUR | SEEK_END
258
	 * @return int|bool new file offset as measured from the start of the file on success, false on failure.
259
	 */
260 4
	public function lseek($file, $offset, $whence = SEEK_SET) {
261 4
		$result = @smbclient_lseek($this->state, $file, $offset, $whence);
0 ignored issues
show
Bug introduced by
The function smbclient_lseek was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

261
		$result = @/** @scrutinizer ignore-call */ smbclient_lseek($this->state, $file, $offset, $whence);
Loading history...
262
263 4
		$this->testResult($result, $file);
264 4
		return $result;
265
	}
266
267
	/**
268
	 * @param resource $file
269
	 * @param int $size
270
	 * @return bool
271
	 */
272 2
	public function ftruncate($file, $size) {
273 2
		$result = @smbclient_ftruncate($this->state, $file, $size);
0 ignored issues
show
Bug introduced by
The function smbclient_ftruncate was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

273
		$result = @/** @scrutinizer ignore-call */ smbclient_ftruncate($this->state, $file, $size);
Loading history...
274
275 2
		$this->testResult($result, $file);
276 2
		return $result;
277
	}
278
279 228
	public function close($file, $path) {
280 228
		$result = @smbclient_close($this->state, $file);
0 ignored issues
show
Bug introduced by
The function smbclient_close was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

280
		$result = @/** @scrutinizer ignore-call */ smbclient_close($this->state, $file);
Loading history...
281
282 228
		$this->testResult($result, $path);
283 228
		return $result;
284
	}
285
286
	/**
287
	 * @param string $uri
288
	 * @param string $key
289
	 * @return string
290
	 */
291 192
	public function getxattr($uri, $key) {
292 192
		$result = @smbclient_getxattr($this->state, $uri, $key);
0 ignored issues
show
Bug introduced by
The function smbclient_getxattr was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

292
		$result = @/** @scrutinizer ignore-call */ smbclient_getxattr($this->state, $uri, $key);
Loading history...
293
294 192
		$this->testResult($result, $uri);
295 190
		return $result;
296
	}
297
298
	/**
299
	 * @param string $uri
300
	 * @param string $key
301
	 * @param string $value
302
	 * @param int $flags
303
	 * @return mixed
304
	 */
305
	public function setxattr($uri, $key, $value, $flags = 0) {
306
		$result = @smbclient_setxattr($this->state, $uri, $key, $value, $flags);
0 ignored issues
show
Bug introduced by
The function smbclient_setxattr was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

306
		$result = @/** @scrutinizer ignore-call */ smbclient_setxattr($this->state, $uri, $key, $value, $flags);
Loading history...
307
308
		$this->testResult($result, $uri);
309
		return $result;
310
	}
311
312 494
	public function __destruct() {
313 494
		if ($this->connected) {
314 492
			smbclient_state_free($this->state);
0 ignored issues
show
Bug introduced by
The function smbclient_state_free was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

314
			/** @scrutinizer ignore-call */ 
315
   smbclient_state_free($this->state);
Loading history...
315
		}
316 494
	}
317
}
318