Completed
Push — master ( 569fcf...fb98c1 )
by Robin
05:42
created

NativeState::opendir()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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