Completed
Push — master ( cfa673...036dbf )
by Robin
07:03
created

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