Completed
Push — master ( 065daa...3c5e45 )
by Robin
04:15
created

NativeState::rmdir()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
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\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\NoRouteToHostException;
20
use Icewind\SMB\Exception\NotEmptyException;
21
use Icewind\SMB\Exception\NotFoundException;
22
use Icewind\SMB\Exception\OutOfSpaceException;
23
use Icewind\SMB\Exception\TimedOutException;
24
use Icewind\SMB\IAuth;
25
use Icewind\SMB\IOptions;
26
27
/**
28
 * Low level wrapper for libsmbclient-php with error handling
29
 */
30
class NativeState {
31
	/**
32
	 * @var resource
33
	 */
34
	protected $state;
35
36
	protected $handlerSet = false;
37
38
	protected $connected = false;
39
40
	// see error.h
41
	const EXCEPTION_MAP = [
42
		1   => ForbiddenException::class,
43
		2   => NotFoundException::class,
44
		13  => ForbiddenException::class,
45
		16  => FileInUseException::class,
46
		17  => AlreadyExistsException::class,
47
		20  => InvalidTypeException::class,
48
		21  => InvalidTypeException::class,
49
		22  => InvalidArgumentException::class,
50
		28  => OutOfSpaceException::class,
51
		39  => NotEmptyException::class,
52
		104 => ConnectionResetException::class,
53
		110 => TimedOutException::class,
54
		111 => ConnectionRefusedException::class,
55
		112 => HostDownException::class,
56
		113 => NoRouteToHostException::class
57
	];
58
59 256
	protected function handleError($path) {
60 256
		$error = smbclient_state_errno($this->state);
61 256
		if ($error === 0) {
62 256
			return;
63
		}
64 19
		throw Exception::fromMap(self::EXCEPTION_MAP, $error, $path);
65
	}
66
67 256
	protected function testResult($result, $uri) {
68 256
		if ($result === false or $result === null) {
69
			// smb://host/share/path
70 256
			if (is_string($uri) && count(explode('/', $uri, 5)) > 4) {
71 19
				list(, , , , $path) = explode('/', $uri, 5);
72 19
				$path = '/' . $path;
73
			} else {
74 256
				$path = null;
75
			}
76 256
			$this->handleError($path);
77
		}
78 256
	}
79
80
	/**
81
	 * @param IAuth $auth
82
	 * @param IOptions $options
83
	 * @return bool
84
	 */
85 256
	public function init(IAuth $auth, IOptions $options) {
86 256
		if ($this->connected) {
87
			return true;
88
		}
89 256
		$this->state = smbclient_state_new();
90 256
		smbclient_option_set($this->state, SMBCLIENT_OPT_AUTO_ANONYMOUS_LOGIN, false);
91 256
		smbclient_option_set($this->state, SMBCLIENT_OPT_TIMEOUT, $options->getTimeout() * 1000);
92 256
		$auth->setExtraSmbClientOptions($this->state);
93 256
		$result = @smbclient_state_init($this->state, $auth->getWorkgroup(), $auth->getUsername(), $auth->getPassword());
94
95 256
		$this->testResult($result, '');
96 256
		$this->connected = true;
97 256
		return $result;
98
	}
99
100
	/**
101
	 * @param string $uri
102
	 * @return resource
103
	 */
104 256
	public function opendir($uri) {
105 256
		$result = @smbclient_opendir($this->state, $uri);
106
107 256
		$this->testResult($result, $uri);
108 256
		return $result;
109
	}
110
111
	/**
112
	 * @param resource $dir
113
	 * @return array
114
	 */
115 256
	public function readdir($dir) {
116 256
		$result = @smbclient_readdir($this->state, $dir);
117
118 256
		$this->testResult($result, $dir);
119 256
		return $result;
120
	}
121
122
	/**
123
	 * @param $dir
124
	 * @return bool
125
	 */
126 256
	public function closedir($dir) {
127 256
		$result = smbclient_closedir($this->state, $dir);
128
129 256
		$this->testResult($result, $dir);
130 256
		return $result;
131
	}
132
133
	/**
134
	 * @param string $old
135
	 * @param string $new
136
	 * @return bool
137
	 */
138 19
	public function rename($old, $new) {
139 19
		$result = @smbclient_rename($this->state, $old, $this->state, $new);
140
141 19
		$this->testResult($result, $new);
142 16
		return $result;
143
	}
144
145
	/**
146
	 * @param string $uri
147
	 * @return bool
148
	 */
149 125
	public function unlink($uri) {
150 125
		$result = @smbclient_unlink($this->state, $uri);
151
152 125
		$this->testResult($result, $uri);
153 123
		return $result;
154
	}
155
156
	/**
157
	 * @param string $uri
158
	 * @param int $mask
159
	 * @return bool
160
	 */
161 256
	public function mkdir($uri, $mask = 0777) {
162 256
		$result = @smbclient_mkdir($this->state, $uri, $mask);
163
164 256
		$this->testResult($result, $uri);
165 256
		return $result;
166
	}
167
168
	/**
169
	 * @param string $uri
170
	 * @return bool
171
	 */
172 256
	public function rmdir($uri) {
173 256
		$result = @smbclient_rmdir($this->state, $uri);
174
175 256
		$this->testResult($result, $uri);
176 256
		return $result;
177
	}
178
179
	/**
180
	 * @param string $uri
181
	 * @return array
182
	 */
183 121
	public function stat($uri) {
184 121
		$result = @smbclient_stat($this->state, $uri);
185
186 121
		$this->testResult($result, $uri);
187 120
		return $result;
188
	}
189
190
	/**
191
	 * @param resource $file
192
	 * @return array
193
	 */
194
	public function fstat($file) {
195
		$result = @smbclient_fstat($this->state, $file);
196
197
		$this->testResult($result, $file);
198
		return $result;
199
	}
200
201
	/**
202
	 * @param string $uri
203
	 * @param string $mode
204
	 * @param int $mask
205
	 * @return resource
206
	 */
207 56
	public function open($uri, $mode, $mask = 0666) {
208 56
		$result = @smbclient_open($this->state, $uri, $mode, $mask);
209
210 56
		$this->testResult($result, $uri);
211 54
		return $result;
212
	}
213
214
	/**
215
	 * @param string $uri
216
	 * @param int $mask
217
	 * @return resource
218
	 */
219 124
	public function create($uri, $mask = 0666) {
220 124
		$result = @smbclient_creat($this->state, $uri, $mask);
221
222 124
		$this->testResult($result, $uri);
223 122
		return $result;
224
	}
225
226
	/**
227
	 * @param resource $file
228
	 * @param int $bytes
229
	 * @return string
230
	 */
231 52
	public function read($file, $bytes) {
232 52
		$result = @smbclient_read($this->state, $file, $bytes);
233
234 52
		$this->testResult($result, $file);
235 52
		return $result;
236
	}
237
238
	/**
239
	 * @param resource $file
240
	 * @param string $data
241
	 * @param int $length
242
	 * @return int
243
	 */
244 123
	public function write($file, $data, $length = null) {
245 123
		$result = @smbclient_write($this->state, $file, $data, $length);
246
247 123
		$this->testResult($result, $file);
248 123
		return $result;
249
	}
250
251
	/**
252
	 * @param resource $file
253
	 * @param int $offset
254
	 * @param int $whence SEEK_SET | SEEK_CUR | SEEK_END
255
	 * @return int|bool new file offset as measured from the start of the file on success, false on failure.
256
	 */
257 2
	public function lseek($file, $offset, $whence = SEEK_SET) {
258 2
		$result = @smbclient_lseek($this->state, $file, $offset, $whence);
259
260 2
		$this->testResult($result, $file);
261 2
		return $result;
262
	}
263
264
	/**
265
	 * @param resource $file
266
	 * @param int $size
267
	 * @return bool
268
	 */
269 1
	public function ftruncate($file, $size) {
270 1
		$result = @smbclient_ftruncate($this->state, $file, $size);
271
272 1
		$this->testResult($result, $file);
273 1
		return $result;
274
	}
275
276 123
	public function close($file) {
277 123
		$result = @smbclient_close($this->state, $file);
278
279 123
		$this->testResult($result, $file);
280 123
		return $result;
281
	}
282
283
	/**
284
	 * @param string $uri
285
	 * @param string $key
286
	 * @return string
287
	 */
288
	public function getxattr($uri, $key) {
289
		$result = @smbclient_getxattr($this->state, $uri, $key);
290
291
		$this->testResult($result, $uri);
292
		return $result;
293
	}
294
295
	/**
296
	 * @param string $uri
297
	 * @param string $key
298
	 * @param string $value
299
	 * @param int $flags
300
	 * @return mixed
301
	 */
302 8
	public function setxattr($uri, $key, $value, $flags = 0) {
303 8
		$result = @smbclient_setxattr($this->state, $uri, $key, $value, $flags);
304
305 8
		$this->testResult($result, $uri);
306 8
		return $result;
307
	}
308
309 257
	public function __destruct() {
310 257
		if ($this->connected) {
311 256
			smbclient_state_free($this->state);
312
		}
313 257
	}
314
}
315