@@ -57,482 +57,482 @@ |
||
57 | 57 | use OCP\ILogger; |
58 | 58 | |
59 | 59 | class SMB extends Common implements INotifyStorage { |
60 | - /** |
|
61 | - * @var \Icewind\SMB\IServer |
|
62 | - */ |
|
63 | - protected $server; |
|
64 | - |
|
65 | - /** |
|
66 | - * @var \Icewind\SMB\IShare |
|
67 | - */ |
|
68 | - protected $share; |
|
69 | - |
|
70 | - /** |
|
71 | - * @var string |
|
72 | - */ |
|
73 | - protected $root; |
|
74 | - |
|
75 | - /** |
|
76 | - * @var \Icewind\SMB\IFileInfo[] |
|
77 | - */ |
|
78 | - protected $statCache; |
|
79 | - |
|
80 | - public function __construct($params) { |
|
81 | - if (isset($params['host']) && isset($params['user']) && isset($params['password']) && isset($params['share'])) { |
|
82 | - $auth = new BasicAuth($params['user'], '', $params['password']); |
|
83 | - $serverFactory = new ServerFactory(); |
|
84 | - $this->server = $serverFactory->createServer($params['host'], $auth); |
|
85 | - $this->share = $this->server->getShare(trim($params['share'], '/')); |
|
86 | - |
|
87 | - $this->root = $params['root'] ?? '/'; |
|
88 | - $this->root = '/' . ltrim($this->root, '/'); |
|
89 | - $this->root = rtrim($this->root, '/') . '/'; |
|
90 | - } else { |
|
91 | - throw new \Exception('Invalid configuration'); |
|
92 | - } |
|
93 | - $this->statCache = new CappedMemoryCache(); |
|
94 | - parent::__construct($params); |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * @return string |
|
99 | - */ |
|
100 | - public function getId() { |
|
101 | - // FIXME: double slash to keep compatible with the old storage ids, |
|
102 | - // failure to do so will lead to creation of a new storage id and |
|
103 | - // loss of shares from the storage |
|
104 | - return 'smb::' . $this->server->getAuth()->getUsername() . '@' . $this->server->getHost() . '//' . $this->share->getName() . '/' . $this->root; |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * @param string $path |
|
109 | - * @return string |
|
110 | - */ |
|
111 | - protected function buildPath($path) { |
|
112 | - return Filesystem::normalizePath($this->root . '/' . $path, true, false, true); |
|
113 | - } |
|
114 | - |
|
115 | - protected function relativePath($fullPath) { |
|
116 | - if ($fullPath === $this->root) { |
|
117 | - return ''; |
|
118 | - } else if (substr($fullPath, 0, strlen($this->root)) === $this->root) { |
|
119 | - return substr($fullPath, strlen($this->root)); |
|
120 | - } else { |
|
121 | - return null; |
|
122 | - } |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * @param string $path |
|
127 | - * @return \Icewind\SMB\IFileInfo |
|
128 | - * @throws StorageNotAvailableException |
|
129 | - */ |
|
130 | - protected function getFileInfo($path) { |
|
131 | - try { |
|
132 | - $path = $this->buildPath($path); |
|
133 | - if (!isset($this->statCache[$path])) { |
|
134 | - $this->statCache[$path] = $this->share->stat($path); |
|
135 | - } |
|
136 | - return $this->statCache[$path]; |
|
137 | - } catch (ConnectException $e) { |
|
138 | - throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
139 | - } |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * @param string $path |
|
144 | - * @return \Icewind\SMB\IFileInfo[] |
|
145 | - * @throws StorageNotAvailableException |
|
146 | - */ |
|
147 | - protected function getFolderContents($path) { |
|
148 | - try { |
|
149 | - $path = $this->buildPath($path); |
|
150 | - $files = $this->share->dir($path); |
|
151 | - foreach ($files as $file) { |
|
152 | - $this->statCache[$path . '/' . $file->getName()] = $file; |
|
153 | - } |
|
154 | - return array_filter($files, function (IFileInfo $file) { |
|
155 | - try { |
|
156 | - return !$file->isHidden(); |
|
157 | - } catch (ForbiddenException $e) { |
|
158 | - return false; |
|
159 | - } catch (NotFoundException $e) { |
|
160 | - return false; |
|
161 | - } |
|
162 | - }); |
|
163 | - } catch (ConnectException $e) { |
|
164 | - throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
165 | - } |
|
166 | - } |
|
167 | - |
|
168 | - /** |
|
169 | - * @param \Icewind\SMB\IFileInfo $info |
|
170 | - * @return array |
|
171 | - */ |
|
172 | - protected function formatInfo($info) { |
|
173 | - $result = [ |
|
174 | - 'size' => $info->getSize(), |
|
175 | - 'mtime' => $info->getMTime(), |
|
176 | - ]; |
|
177 | - if ($info->isDirectory()) { |
|
178 | - $result['type'] = 'dir'; |
|
179 | - } else { |
|
180 | - $result['type'] = 'file'; |
|
181 | - } |
|
182 | - return $result; |
|
183 | - } |
|
184 | - |
|
185 | - /** |
|
186 | - * Rename the files. If the source or the target is the root, the rename won't happen. |
|
187 | - * |
|
188 | - * @param string $source the old name of the path |
|
189 | - * @param string $target the new name of the path |
|
190 | - * @return bool true if the rename is successful, false otherwise |
|
191 | - */ |
|
192 | - public function rename($source, $target) { |
|
193 | - if ($this->isRootDir($source) || $this->isRootDir($target)) { |
|
194 | - return false; |
|
195 | - } |
|
196 | - |
|
197 | - $absoluteSource = $this->buildPath($source); |
|
198 | - $absoluteTarget = $this->buildPath($target); |
|
199 | - try { |
|
200 | - $result = $this->share->rename($absoluteSource, $absoluteTarget); |
|
201 | - } catch (AlreadyExistsException $e) { |
|
202 | - $this->remove($target); |
|
203 | - $result = $this->share->rename($absoluteSource, $absoluteTarget); |
|
204 | - } catch (\Exception $e) { |
|
205 | - \OC::$server->getLogger()->logException($e, ['level' => ILogger::WARN]); |
|
206 | - return false; |
|
207 | - } |
|
208 | - unset($this->statCache[$absoluteSource], $this->statCache[$absoluteTarget]); |
|
209 | - return $result; |
|
210 | - } |
|
211 | - |
|
212 | - public function stat($path) { |
|
213 | - try { |
|
214 | - $result = $this->formatInfo($this->getFileInfo($path)); |
|
215 | - } catch (ForbiddenException $e) { |
|
216 | - return false; |
|
217 | - } catch (NotFoundException $e) { |
|
218 | - return false; |
|
219 | - } |
|
220 | - if ($this->remoteIsShare() && $this->isRootDir($path)) { |
|
221 | - $result['mtime'] = $this->shareMTime(); |
|
222 | - } |
|
223 | - return $result; |
|
224 | - } |
|
225 | - |
|
226 | - /** |
|
227 | - * get the best guess for the modification time of the share |
|
228 | - * |
|
229 | - * @return int |
|
230 | - */ |
|
231 | - private function shareMTime() { |
|
232 | - $highestMTime = 0; |
|
233 | - $files = $this->share->dir($this->root); |
|
234 | - foreach ($files as $fileInfo) { |
|
235 | - try { |
|
236 | - if ($fileInfo->getMTime() > $highestMTime) { |
|
237 | - $highestMTime = $fileInfo->getMTime(); |
|
238 | - } |
|
239 | - } catch (NotFoundException $e) { |
|
240 | - // Ignore this, can happen on unavailable DFS shares |
|
241 | - } |
|
242 | - } |
|
243 | - return $highestMTime; |
|
244 | - } |
|
245 | - |
|
246 | - /** |
|
247 | - * Check if the path is our root dir (not the smb one) |
|
248 | - * |
|
249 | - * @param string $path the path |
|
250 | - * @return bool |
|
251 | - */ |
|
252 | - private function isRootDir($path) { |
|
253 | - return $path === '' || $path === '/' || $path === '.'; |
|
254 | - } |
|
255 | - |
|
256 | - /** |
|
257 | - * Check if our root points to a smb share |
|
258 | - * |
|
259 | - * @return bool true if our root points to a share false otherwise |
|
260 | - */ |
|
261 | - private function remoteIsShare() { |
|
262 | - return $this->share->getName() && (!$this->root || $this->root === '/'); |
|
263 | - } |
|
264 | - |
|
265 | - /** |
|
266 | - * @param string $path |
|
267 | - * @return bool |
|
268 | - */ |
|
269 | - public function unlink($path) { |
|
270 | - if ($this->isRootDir($path)) { |
|
271 | - return false; |
|
272 | - } |
|
273 | - |
|
274 | - try { |
|
275 | - if ($this->is_dir($path)) { |
|
276 | - return $this->rmdir($path); |
|
277 | - } else { |
|
278 | - $path = $this->buildPath($path); |
|
279 | - unset($this->statCache[$path]); |
|
280 | - $this->share->del($path); |
|
281 | - return true; |
|
282 | - } |
|
283 | - } catch (NotFoundException $e) { |
|
284 | - return false; |
|
285 | - } catch (ForbiddenException $e) { |
|
286 | - return false; |
|
287 | - } catch (ConnectException $e) { |
|
288 | - throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
289 | - } |
|
290 | - } |
|
291 | - |
|
292 | - /** |
|
293 | - * check if a file or folder has been updated since $time |
|
294 | - * |
|
295 | - * @param string $path |
|
296 | - * @param int $time |
|
297 | - * @return bool |
|
298 | - */ |
|
299 | - public function hasUpdated($path, $time) { |
|
300 | - if (!$path and $this->root === '/') { |
|
301 | - // mtime doesn't work for shares, but giving the nature of the backend, |
|
302 | - // doing a full update is still just fast enough |
|
303 | - return true; |
|
304 | - } else { |
|
305 | - $actualTime = $this->filemtime($path); |
|
306 | - return $actualTime > $time; |
|
307 | - } |
|
308 | - } |
|
309 | - |
|
310 | - /** |
|
311 | - * @param string $path |
|
312 | - * @param string $mode |
|
313 | - * @return resource|false |
|
314 | - */ |
|
315 | - public function fopen($path, $mode) { |
|
316 | - $fullPath = $this->buildPath($path); |
|
317 | - try { |
|
318 | - switch ($mode) { |
|
319 | - case 'r': |
|
320 | - case 'rb': |
|
321 | - if (!$this->file_exists($path)) { |
|
322 | - return false; |
|
323 | - } |
|
324 | - return $this->share->read($fullPath); |
|
325 | - case 'w': |
|
326 | - case 'wb': |
|
327 | - $source = $this->share->write($fullPath); |
|
328 | - return CallBackWrapper::wrap($source, null, null, function () use ($fullPath) { |
|
329 | - unset($this->statCache[$fullPath]); |
|
330 | - }); |
|
331 | - case 'a': |
|
332 | - case 'ab': |
|
333 | - case 'r+': |
|
334 | - case 'w+': |
|
335 | - case 'wb+': |
|
336 | - case 'a+': |
|
337 | - case 'x': |
|
338 | - case 'x+': |
|
339 | - case 'c': |
|
340 | - case 'c+': |
|
341 | - //emulate these |
|
342 | - if (strrpos($path, '.') !== false) { |
|
343 | - $ext = substr($path, strrpos($path, '.')); |
|
344 | - } else { |
|
345 | - $ext = ''; |
|
346 | - } |
|
347 | - if ($this->file_exists($path)) { |
|
348 | - if (!$this->isUpdatable($path)) { |
|
349 | - return false; |
|
350 | - } |
|
351 | - $tmpFile = $this->getCachedFile($path); |
|
352 | - } else { |
|
353 | - if (!$this->isCreatable(dirname($path))) { |
|
354 | - return false; |
|
355 | - } |
|
356 | - $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($ext); |
|
357 | - } |
|
358 | - $source = fopen($tmpFile, $mode); |
|
359 | - $share = $this->share; |
|
360 | - return CallbackWrapper::wrap($source, null, null, function () use ($tmpFile, $fullPath, $share) { |
|
361 | - unset($this->statCache[$fullPath]); |
|
362 | - $share->put($tmpFile, $fullPath); |
|
363 | - unlink($tmpFile); |
|
364 | - }); |
|
365 | - } |
|
366 | - return false; |
|
367 | - } catch (NotFoundException $e) { |
|
368 | - return false; |
|
369 | - } catch (ForbiddenException $e) { |
|
370 | - return false; |
|
371 | - } catch (ConnectException $e) { |
|
372 | - throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
373 | - } |
|
374 | - } |
|
375 | - |
|
376 | - public function rmdir($path) { |
|
377 | - if ($this->isRootDir($path)) { |
|
378 | - return false; |
|
379 | - } |
|
380 | - |
|
381 | - try { |
|
382 | - $this->statCache = array(); |
|
383 | - $content = $this->share->dir($this->buildPath($path)); |
|
384 | - foreach ($content as $file) { |
|
385 | - if ($file->isDirectory()) { |
|
386 | - $this->rmdir($path . '/' . $file->getName()); |
|
387 | - } else { |
|
388 | - $this->share->del($file->getPath()); |
|
389 | - } |
|
390 | - } |
|
391 | - $this->share->rmdir($this->buildPath($path)); |
|
392 | - return true; |
|
393 | - } catch (NotFoundException $e) { |
|
394 | - return false; |
|
395 | - } catch (ForbiddenException $e) { |
|
396 | - return false; |
|
397 | - } catch (ConnectException $e) { |
|
398 | - throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
399 | - } |
|
400 | - } |
|
401 | - |
|
402 | - public function touch($path, $time = null) { |
|
403 | - try { |
|
404 | - if (!$this->file_exists($path)) { |
|
405 | - $fh = $this->share->write($this->buildPath($path)); |
|
406 | - fclose($fh); |
|
407 | - return true; |
|
408 | - } |
|
409 | - return false; |
|
410 | - } catch (ConnectException $e) { |
|
411 | - throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
412 | - } |
|
413 | - } |
|
414 | - |
|
415 | - public function opendir($path) { |
|
416 | - try { |
|
417 | - $files = $this->getFolderContents($path); |
|
418 | - } catch (NotFoundException $e) { |
|
419 | - return false; |
|
420 | - } catch (ForbiddenException $e) { |
|
421 | - return false; |
|
422 | - } |
|
423 | - $names = array_map(function ($info) { |
|
424 | - /** @var \Icewind\SMB\IFileInfo $info */ |
|
425 | - return $info->getName(); |
|
426 | - }, $files); |
|
427 | - return IteratorDirectory::wrap($names); |
|
428 | - } |
|
429 | - |
|
430 | - public function filetype($path) { |
|
431 | - try { |
|
432 | - return $this->getFileInfo($path)->isDirectory() ? 'dir' : 'file'; |
|
433 | - } catch (NotFoundException $e) { |
|
434 | - return false; |
|
435 | - } catch (ForbiddenException $e) { |
|
436 | - return false; |
|
437 | - } |
|
438 | - } |
|
439 | - |
|
440 | - public function mkdir($path) { |
|
441 | - $path = $this->buildPath($path); |
|
442 | - try { |
|
443 | - $this->share->mkdir($path); |
|
444 | - return true; |
|
445 | - } catch (ConnectException $e) { |
|
446 | - throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
447 | - } catch (Exception $e) { |
|
448 | - return false; |
|
449 | - } |
|
450 | - } |
|
451 | - |
|
452 | - public function file_exists($path) { |
|
453 | - try { |
|
454 | - $this->getFileInfo($path); |
|
455 | - return true; |
|
456 | - } catch (NotFoundException $e) { |
|
457 | - return false; |
|
458 | - } catch (ForbiddenException $e) { |
|
459 | - return false; |
|
460 | - } catch (ConnectException $e) { |
|
461 | - throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
462 | - } |
|
463 | - } |
|
464 | - |
|
465 | - public function isReadable($path) { |
|
466 | - try { |
|
467 | - $info = $this->getFileInfo($path); |
|
468 | - return !$info->isHidden(); |
|
469 | - } catch (NotFoundException $e) { |
|
470 | - return false; |
|
471 | - } catch (ForbiddenException $e) { |
|
472 | - return false; |
|
473 | - } |
|
474 | - } |
|
475 | - |
|
476 | - public function isUpdatable($path) { |
|
477 | - try { |
|
478 | - $info = $this->getFileInfo($path); |
|
479 | - // following windows behaviour for read-only folders: they can be written into |
|
480 | - // (https://support.microsoft.com/en-us/kb/326549 - "cause" section) |
|
481 | - return !$info->isHidden() && (!$info->isReadOnly() || $this->is_dir($path)); |
|
482 | - } catch (NotFoundException $e) { |
|
483 | - return false; |
|
484 | - } catch (ForbiddenException $e) { |
|
485 | - return false; |
|
486 | - } |
|
487 | - } |
|
488 | - |
|
489 | - public function isDeletable($path) { |
|
490 | - try { |
|
491 | - $info = $this->getFileInfo($path); |
|
492 | - return !$info->isHidden() && !$info->isReadOnly(); |
|
493 | - } catch (NotFoundException $e) { |
|
494 | - return false; |
|
495 | - } catch (ForbiddenException $e) { |
|
496 | - return false; |
|
497 | - } |
|
498 | - } |
|
499 | - |
|
500 | - /** |
|
501 | - * check if smbclient is installed |
|
502 | - */ |
|
503 | - public static function checkDependencies() { |
|
504 | - return ( |
|
505 | - (bool)\OC_Helper::findBinaryPath('smbclient') |
|
506 | - || NativeServer::available(new System()) |
|
507 | - ) ? true : ['smbclient']; |
|
508 | - } |
|
509 | - |
|
510 | - /** |
|
511 | - * Test a storage for availability |
|
512 | - * |
|
513 | - * @return bool |
|
514 | - */ |
|
515 | - public function test() { |
|
516 | - try { |
|
517 | - return parent::test(); |
|
518 | - } catch (Exception $e) { |
|
519 | - return false; |
|
520 | - } |
|
521 | - } |
|
522 | - |
|
523 | - public function listen($path, callable $callback) { |
|
524 | - $this->notify($path)->listen(function (IChange $change) use ($callback) { |
|
525 | - if ($change instanceof IRenameChange) { |
|
526 | - return $callback($change->getType(), $change->getPath(), $change->getTargetPath()); |
|
527 | - } else { |
|
528 | - return $callback($change->getType(), $change->getPath()); |
|
529 | - } |
|
530 | - }); |
|
531 | - } |
|
532 | - |
|
533 | - public function notify($path) { |
|
534 | - $path = '/' . ltrim($path, '/'); |
|
535 | - $shareNotifyHandler = $this->share->notify($this->buildPath($path)); |
|
536 | - return new SMBNotifyHandler($shareNotifyHandler, $this->root); |
|
537 | - } |
|
60 | + /** |
|
61 | + * @var \Icewind\SMB\IServer |
|
62 | + */ |
|
63 | + protected $server; |
|
64 | + |
|
65 | + /** |
|
66 | + * @var \Icewind\SMB\IShare |
|
67 | + */ |
|
68 | + protected $share; |
|
69 | + |
|
70 | + /** |
|
71 | + * @var string |
|
72 | + */ |
|
73 | + protected $root; |
|
74 | + |
|
75 | + /** |
|
76 | + * @var \Icewind\SMB\IFileInfo[] |
|
77 | + */ |
|
78 | + protected $statCache; |
|
79 | + |
|
80 | + public function __construct($params) { |
|
81 | + if (isset($params['host']) && isset($params['user']) && isset($params['password']) && isset($params['share'])) { |
|
82 | + $auth = new BasicAuth($params['user'], '', $params['password']); |
|
83 | + $serverFactory = new ServerFactory(); |
|
84 | + $this->server = $serverFactory->createServer($params['host'], $auth); |
|
85 | + $this->share = $this->server->getShare(trim($params['share'], '/')); |
|
86 | + |
|
87 | + $this->root = $params['root'] ?? '/'; |
|
88 | + $this->root = '/' . ltrim($this->root, '/'); |
|
89 | + $this->root = rtrim($this->root, '/') . '/'; |
|
90 | + } else { |
|
91 | + throw new \Exception('Invalid configuration'); |
|
92 | + } |
|
93 | + $this->statCache = new CappedMemoryCache(); |
|
94 | + parent::__construct($params); |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * @return string |
|
99 | + */ |
|
100 | + public function getId() { |
|
101 | + // FIXME: double slash to keep compatible with the old storage ids, |
|
102 | + // failure to do so will lead to creation of a new storage id and |
|
103 | + // loss of shares from the storage |
|
104 | + return 'smb::' . $this->server->getAuth()->getUsername() . '@' . $this->server->getHost() . '//' . $this->share->getName() . '/' . $this->root; |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * @param string $path |
|
109 | + * @return string |
|
110 | + */ |
|
111 | + protected function buildPath($path) { |
|
112 | + return Filesystem::normalizePath($this->root . '/' . $path, true, false, true); |
|
113 | + } |
|
114 | + |
|
115 | + protected function relativePath($fullPath) { |
|
116 | + if ($fullPath === $this->root) { |
|
117 | + return ''; |
|
118 | + } else if (substr($fullPath, 0, strlen($this->root)) === $this->root) { |
|
119 | + return substr($fullPath, strlen($this->root)); |
|
120 | + } else { |
|
121 | + return null; |
|
122 | + } |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * @param string $path |
|
127 | + * @return \Icewind\SMB\IFileInfo |
|
128 | + * @throws StorageNotAvailableException |
|
129 | + */ |
|
130 | + protected function getFileInfo($path) { |
|
131 | + try { |
|
132 | + $path = $this->buildPath($path); |
|
133 | + if (!isset($this->statCache[$path])) { |
|
134 | + $this->statCache[$path] = $this->share->stat($path); |
|
135 | + } |
|
136 | + return $this->statCache[$path]; |
|
137 | + } catch (ConnectException $e) { |
|
138 | + throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
139 | + } |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * @param string $path |
|
144 | + * @return \Icewind\SMB\IFileInfo[] |
|
145 | + * @throws StorageNotAvailableException |
|
146 | + */ |
|
147 | + protected function getFolderContents($path) { |
|
148 | + try { |
|
149 | + $path = $this->buildPath($path); |
|
150 | + $files = $this->share->dir($path); |
|
151 | + foreach ($files as $file) { |
|
152 | + $this->statCache[$path . '/' . $file->getName()] = $file; |
|
153 | + } |
|
154 | + return array_filter($files, function (IFileInfo $file) { |
|
155 | + try { |
|
156 | + return !$file->isHidden(); |
|
157 | + } catch (ForbiddenException $e) { |
|
158 | + return false; |
|
159 | + } catch (NotFoundException $e) { |
|
160 | + return false; |
|
161 | + } |
|
162 | + }); |
|
163 | + } catch (ConnectException $e) { |
|
164 | + throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
165 | + } |
|
166 | + } |
|
167 | + |
|
168 | + /** |
|
169 | + * @param \Icewind\SMB\IFileInfo $info |
|
170 | + * @return array |
|
171 | + */ |
|
172 | + protected function formatInfo($info) { |
|
173 | + $result = [ |
|
174 | + 'size' => $info->getSize(), |
|
175 | + 'mtime' => $info->getMTime(), |
|
176 | + ]; |
|
177 | + if ($info->isDirectory()) { |
|
178 | + $result['type'] = 'dir'; |
|
179 | + } else { |
|
180 | + $result['type'] = 'file'; |
|
181 | + } |
|
182 | + return $result; |
|
183 | + } |
|
184 | + |
|
185 | + /** |
|
186 | + * Rename the files. If the source or the target is the root, the rename won't happen. |
|
187 | + * |
|
188 | + * @param string $source the old name of the path |
|
189 | + * @param string $target the new name of the path |
|
190 | + * @return bool true if the rename is successful, false otherwise |
|
191 | + */ |
|
192 | + public function rename($source, $target) { |
|
193 | + if ($this->isRootDir($source) || $this->isRootDir($target)) { |
|
194 | + return false; |
|
195 | + } |
|
196 | + |
|
197 | + $absoluteSource = $this->buildPath($source); |
|
198 | + $absoluteTarget = $this->buildPath($target); |
|
199 | + try { |
|
200 | + $result = $this->share->rename($absoluteSource, $absoluteTarget); |
|
201 | + } catch (AlreadyExistsException $e) { |
|
202 | + $this->remove($target); |
|
203 | + $result = $this->share->rename($absoluteSource, $absoluteTarget); |
|
204 | + } catch (\Exception $e) { |
|
205 | + \OC::$server->getLogger()->logException($e, ['level' => ILogger::WARN]); |
|
206 | + return false; |
|
207 | + } |
|
208 | + unset($this->statCache[$absoluteSource], $this->statCache[$absoluteTarget]); |
|
209 | + return $result; |
|
210 | + } |
|
211 | + |
|
212 | + public function stat($path) { |
|
213 | + try { |
|
214 | + $result = $this->formatInfo($this->getFileInfo($path)); |
|
215 | + } catch (ForbiddenException $e) { |
|
216 | + return false; |
|
217 | + } catch (NotFoundException $e) { |
|
218 | + return false; |
|
219 | + } |
|
220 | + if ($this->remoteIsShare() && $this->isRootDir($path)) { |
|
221 | + $result['mtime'] = $this->shareMTime(); |
|
222 | + } |
|
223 | + return $result; |
|
224 | + } |
|
225 | + |
|
226 | + /** |
|
227 | + * get the best guess for the modification time of the share |
|
228 | + * |
|
229 | + * @return int |
|
230 | + */ |
|
231 | + private function shareMTime() { |
|
232 | + $highestMTime = 0; |
|
233 | + $files = $this->share->dir($this->root); |
|
234 | + foreach ($files as $fileInfo) { |
|
235 | + try { |
|
236 | + if ($fileInfo->getMTime() > $highestMTime) { |
|
237 | + $highestMTime = $fileInfo->getMTime(); |
|
238 | + } |
|
239 | + } catch (NotFoundException $e) { |
|
240 | + // Ignore this, can happen on unavailable DFS shares |
|
241 | + } |
|
242 | + } |
|
243 | + return $highestMTime; |
|
244 | + } |
|
245 | + |
|
246 | + /** |
|
247 | + * Check if the path is our root dir (not the smb one) |
|
248 | + * |
|
249 | + * @param string $path the path |
|
250 | + * @return bool |
|
251 | + */ |
|
252 | + private function isRootDir($path) { |
|
253 | + return $path === '' || $path === '/' || $path === '.'; |
|
254 | + } |
|
255 | + |
|
256 | + /** |
|
257 | + * Check if our root points to a smb share |
|
258 | + * |
|
259 | + * @return bool true if our root points to a share false otherwise |
|
260 | + */ |
|
261 | + private function remoteIsShare() { |
|
262 | + return $this->share->getName() && (!$this->root || $this->root === '/'); |
|
263 | + } |
|
264 | + |
|
265 | + /** |
|
266 | + * @param string $path |
|
267 | + * @return bool |
|
268 | + */ |
|
269 | + public function unlink($path) { |
|
270 | + if ($this->isRootDir($path)) { |
|
271 | + return false; |
|
272 | + } |
|
273 | + |
|
274 | + try { |
|
275 | + if ($this->is_dir($path)) { |
|
276 | + return $this->rmdir($path); |
|
277 | + } else { |
|
278 | + $path = $this->buildPath($path); |
|
279 | + unset($this->statCache[$path]); |
|
280 | + $this->share->del($path); |
|
281 | + return true; |
|
282 | + } |
|
283 | + } catch (NotFoundException $e) { |
|
284 | + return false; |
|
285 | + } catch (ForbiddenException $e) { |
|
286 | + return false; |
|
287 | + } catch (ConnectException $e) { |
|
288 | + throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
289 | + } |
|
290 | + } |
|
291 | + |
|
292 | + /** |
|
293 | + * check if a file or folder has been updated since $time |
|
294 | + * |
|
295 | + * @param string $path |
|
296 | + * @param int $time |
|
297 | + * @return bool |
|
298 | + */ |
|
299 | + public function hasUpdated($path, $time) { |
|
300 | + if (!$path and $this->root === '/') { |
|
301 | + // mtime doesn't work for shares, but giving the nature of the backend, |
|
302 | + // doing a full update is still just fast enough |
|
303 | + return true; |
|
304 | + } else { |
|
305 | + $actualTime = $this->filemtime($path); |
|
306 | + return $actualTime > $time; |
|
307 | + } |
|
308 | + } |
|
309 | + |
|
310 | + /** |
|
311 | + * @param string $path |
|
312 | + * @param string $mode |
|
313 | + * @return resource|false |
|
314 | + */ |
|
315 | + public function fopen($path, $mode) { |
|
316 | + $fullPath = $this->buildPath($path); |
|
317 | + try { |
|
318 | + switch ($mode) { |
|
319 | + case 'r': |
|
320 | + case 'rb': |
|
321 | + if (!$this->file_exists($path)) { |
|
322 | + return false; |
|
323 | + } |
|
324 | + return $this->share->read($fullPath); |
|
325 | + case 'w': |
|
326 | + case 'wb': |
|
327 | + $source = $this->share->write($fullPath); |
|
328 | + return CallBackWrapper::wrap($source, null, null, function () use ($fullPath) { |
|
329 | + unset($this->statCache[$fullPath]); |
|
330 | + }); |
|
331 | + case 'a': |
|
332 | + case 'ab': |
|
333 | + case 'r+': |
|
334 | + case 'w+': |
|
335 | + case 'wb+': |
|
336 | + case 'a+': |
|
337 | + case 'x': |
|
338 | + case 'x+': |
|
339 | + case 'c': |
|
340 | + case 'c+': |
|
341 | + //emulate these |
|
342 | + if (strrpos($path, '.') !== false) { |
|
343 | + $ext = substr($path, strrpos($path, '.')); |
|
344 | + } else { |
|
345 | + $ext = ''; |
|
346 | + } |
|
347 | + if ($this->file_exists($path)) { |
|
348 | + if (!$this->isUpdatable($path)) { |
|
349 | + return false; |
|
350 | + } |
|
351 | + $tmpFile = $this->getCachedFile($path); |
|
352 | + } else { |
|
353 | + if (!$this->isCreatable(dirname($path))) { |
|
354 | + return false; |
|
355 | + } |
|
356 | + $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($ext); |
|
357 | + } |
|
358 | + $source = fopen($tmpFile, $mode); |
|
359 | + $share = $this->share; |
|
360 | + return CallbackWrapper::wrap($source, null, null, function () use ($tmpFile, $fullPath, $share) { |
|
361 | + unset($this->statCache[$fullPath]); |
|
362 | + $share->put($tmpFile, $fullPath); |
|
363 | + unlink($tmpFile); |
|
364 | + }); |
|
365 | + } |
|
366 | + return false; |
|
367 | + } catch (NotFoundException $e) { |
|
368 | + return false; |
|
369 | + } catch (ForbiddenException $e) { |
|
370 | + return false; |
|
371 | + } catch (ConnectException $e) { |
|
372 | + throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
373 | + } |
|
374 | + } |
|
375 | + |
|
376 | + public function rmdir($path) { |
|
377 | + if ($this->isRootDir($path)) { |
|
378 | + return false; |
|
379 | + } |
|
380 | + |
|
381 | + try { |
|
382 | + $this->statCache = array(); |
|
383 | + $content = $this->share->dir($this->buildPath($path)); |
|
384 | + foreach ($content as $file) { |
|
385 | + if ($file->isDirectory()) { |
|
386 | + $this->rmdir($path . '/' . $file->getName()); |
|
387 | + } else { |
|
388 | + $this->share->del($file->getPath()); |
|
389 | + } |
|
390 | + } |
|
391 | + $this->share->rmdir($this->buildPath($path)); |
|
392 | + return true; |
|
393 | + } catch (NotFoundException $e) { |
|
394 | + return false; |
|
395 | + } catch (ForbiddenException $e) { |
|
396 | + return false; |
|
397 | + } catch (ConnectException $e) { |
|
398 | + throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
399 | + } |
|
400 | + } |
|
401 | + |
|
402 | + public function touch($path, $time = null) { |
|
403 | + try { |
|
404 | + if (!$this->file_exists($path)) { |
|
405 | + $fh = $this->share->write($this->buildPath($path)); |
|
406 | + fclose($fh); |
|
407 | + return true; |
|
408 | + } |
|
409 | + return false; |
|
410 | + } catch (ConnectException $e) { |
|
411 | + throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
412 | + } |
|
413 | + } |
|
414 | + |
|
415 | + public function opendir($path) { |
|
416 | + try { |
|
417 | + $files = $this->getFolderContents($path); |
|
418 | + } catch (NotFoundException $e) { |
|
419 | + return false; |
|
420 | + } catch (ForbiddenException $e) { |
|
421 | + return false; |
|
422 | + } |
|
423 | + $names = array_map(function ($info) { |
|
424 | + /** @var \Icewind\SMB\IFileInfo $info */ |
|
425 | + return $info->getName(); |
|
426 | + }, $files); |
|
427 | + return IteratorDirectory::wrap($names); |
|
428 | + } |
|
429 | + |
|
430 | + public function filetype($path) { |
|
431 | + try { |
|
432 | + return $this->getFileInfo($path)->isDirectory() ? 'dir' : 'file'; |
|
433 | + } catch (NotFoundException $e) { |
|
434 | + return false; |
|
435 | + } catch (ForbiddenException $e) { |
|
436 | + return false; |
|
437 | + } |
|
438 | + } |
|
439 | + |
|
440 | + public function mkdir($path) { |
|
441 | + $path = $this->buildPath($path); |
|
442 | + try { |
|
443 | + $this->share->mkdir($path); |
|
444 | + return true; |
|
445 | + } catch (ConnectException $e) { |
|
446 | + throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
447 | + } catch (Exception $e) { |
|
448 | + return false; |
|
449 | + } |
|
450 | + } |
|
451 | + |
|
452 | + public function file_exists($path) { |
|
453 | + try { |
|
454 | + $this->getFileInfo($path); |
|
455 | + return true; |
|
456 | + } catch (NotFoundException $e) { |
|
457 | + return false; |
|
458 | + } catch (ForbiddenException $e) { |
|
459 | + return false; |
|
460 | + } catch (ConnectException $e) { |
|
461 | + throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
|
462 | + } |
|
463 | + } |
|
464 | + |
|
465 | + public function isReadable($path) { |
|
466 | + try { |
|
467 | + $info = $this->getFileInfo($path); |
|
468 | + return !$info->isHidden(); |
|
469 | + } catch (NotFoundException $e) { |
|
470 | + return false; |
|
471 | + } catch (ForbiddenException $e) { |
|
472 | + return false; |
|
473 | + } |
|
474 | + } |
|
475 | + |
|
476 | + public function isUpdatable($path) { |
|
477 | + try { |
|
478 | + $info = $this->getFileInfo($path); |
|
479 | + // following windows behaviour for read-only folders: they can be written into |
|
480 | + // (https://support.microsoft.com/en-us/kb/326549 - "cause" section) |
|
481 | + return !$info->isHidden() && (!$info->isReadOnly() || $this->is_dir($path)); |
|
482 | + } catch (NotFoundException $e) { |
|
483 | + return false; |
|
484 | + } catch (ForbiddenException $e) { |
|
485 | + return false; |
|
486 | + } |
|
487 | + } |
|
488 | + |
|
489 | + public function isDeletable($path) { |
|
490 | + try { |
|
491 | + $info = $this->getFileInfo($path); |
|
492 | + return !$info->isHidden() && !$info->isReadOnly(); |
|
493 | + } catch (NotFoundException $e) { |
|
494 | + return false; |
|
495 | + } catch (ForbiddenException $e) { |
|
496 | + return false; |
|
497 | + } |
|
498 | + } |
|
499 | + |
|
500 | + /** |
|
501 | + * check if smbclient is installed |
|
502 | + */ |
|
503 | + public static function checkDependencies() { |
|
504 | + return ( |
|
505 | + (bool)\OC_Helper::findBinaryPath('smbclient') |
|
506 | + || NativeServer::available(new System()) |
|
507 | + ) ? true : ['smbclient']; |
|
508 | + } |
|
509 | + |
|
510 | + /** |
|
511 | + * Test a storage for availability |
|
512 | + * |
|
513 | + * @return bool |
|
514 | + */ |
|
515 | + public function test() { |
|
516 | + try { |
|
517 | + return parent::test(); |
|
518 | + } catch (Exception $e) { |
|
519 | + return false; |
|
520 | + } |
|
521 | + } |
|
522 | + |
|
523 | + public function listen($path, callable $callback) { |
|
524 | + $this->notify($path)->listen(function (IChange $change) use ($callback) { |
|
525 | + if ($change instanceof IRenameChange) { |
|
526 | + return $callback($change->getType(), $change->getPath(), $change->getTargetPath()); |
|
527 | + } else { |
|
528 | + return $callback($change->getType(), $change->getPath()); |
|
529 | + } |
|
530 | + }); |
|
531 | + } |
|
532 | + |
|
533 | + public function notify($path) { |
|
534 | + $path = '/' . ltrim($path, '/'); |
|
535 | + $shareNotifyHandler = $this->share->notify($this->buildPath($path)); |
|
536 | + return new SMBNotifyHandler($shareNotifyHandler, $this->root); |
|
537 | + } |
|
538 | 538 | } |
@@ -85,8 +85,8 @@ discard block |
||
85 | 85 | $this->share = $this->server->getShare(trim($params['share'], '/')); |
86 | 86 | |
87 | 87 | $this->root = $params['root'] ?? '/'; |
88 | - $this->root = '/' . ltrim($this->root, '/'); |
|
89 | - $this->root = rtrim($this->root, '/') . '/'; |
|
88 | + $this->root = '/'.ltrim($this->root, '/'); |
|
89 | + $this->root = rtrim($this->root, '/').'/'; |
|
90 | 90 | } else { |
91 | 91 | throw new \Exception('Invalid configuration'); |
92 | 92 | } |
@@ -101,7 +101,7 @@ discard block |
||
101 | 101 | // FIXME: double slash to keep compatible with the old storage ids, |
102 | 102 | // failure to do so will lead to creation of a new storage id and |
103 | 103 | // loss of shares from the storage |
104 | - return 'smb::' . $this->server->getAuth()->getUsername() . '@' . $this->server->getHost() . '//' . $this->share->getName() . '/' . $this->root; |
|
104 | + return 'smb::'.$this->server->getAuth()->getUsername().'@'.$this->server->getHost().'//'.$this->share->getName().'/'.$this->root; |
|
105 | 105 | } |
106 | 106 | |
107 | 107 | /** |
@@ -109,7 +109,7 @@ discard block |
||
109 | 109 | * @return string |
110 | 110 | */ |
111 | 111 | protected function buildPath($path) { |
112 | - return Filesystem::normalizePath($this->root . '/' . $path, true, false, true); |
|
112 | + return Filesystem::normalizePath($this->root.'/'.$path, true, false, true); |
|
113 | 113 | } |
114 | 114 | |
115 | 115 | protected function relativePath($fullPath) { |
@@ -149,9 +149,9 @@ discard block |
||
149 | 149 | $path = $this->buildPath($path); |
150 | 150 | $files = $this->share->dir($path); |
151 | 151 | foreach ($files as $file) { |
152 | - $this->statCache[$path . '/' . $file->getName()] = $file; |
|
152 | + $this->statCache[$path.'/'.$file->getName()] = $file; |
|
153 | 153 | } |
154 | - return array_filter($files, function (IFileInfo $file) { |
|
154 | + return array_filter($files, function(IFileInfo $file) { |
|
155 | 155 | try { |
156 | 156 | return !$file->isHidden(); |
157 | 157 | } catch (ForbiddenException $e) { |
@@ -325,7 +325,7 @@ discard block |
||
325 | 325 | case 'w': |
326 | 326 | case 'wb': |
327 | 327 | $source = $this->share->write($fullPath); |
328 | - return CallBackWrapper::wrap($source, null, null, function () use ($fullPath) { |
|
328 | + return CallBackWrapper::wrap($source, null, null, function() use ($fullPath) { |
|
329 | 329 | unset($this->statCache[$fullPath]); |
330 | 330 | }); |
331 | 331 | case 'a': |
@@ -357,7 +357,7 @@ discard block |
||
357 | 357 | } |
358 | 358 | $source = fopen($tmpFile, $mode); |
359 | 359 | $share = $this->share; |
360 | - return CallbackWrapper::wrap($source, null, null, function () use ($tmpFile, $fullPath, $share) { |
|
360 | + return CallbackWrapper::wrap($source, null, null, function() use ($tmpFile, $fullPath, $share) { |
|
361 | 361 | unset($this->statCache[$fullPath]); |
362 | 362 | $share->put($tmpFile, $fullPath); |
363 | 363 | unlink($tmpFile); |
@@ -383,7 +383,7 @@ discard block |
||
383 | 383 | $content = $this->share->dir($this->buildPath($path)); |
384 | 384 | foreach ($content as $file) { |
385 | 385 | if ($file->isDirectory()) { |
386 | - $this->rmdir($path . '/' . $file->getName()); |
|
386 | + $this->rmdir($path.'/'.$file->getName()); |
|
387 | 387 | } else { |
388 | 388 | $this->share->del($file->getPath()); |
389 | 389 | } |
@@ -420,7 +420,7 @@ discard block |
||
420 | 420 | } catch (ForbiddenException $e) { |
421 | 421 | return false; |
422 | 422 | } |
423 | - $names = array_map(function ($info) { |
|
423 | + $names = array_map(function($info) { |
|
424 | 424 | /** @var \Icewind\SMB\IFileInfo $info */ |
425 | 425 | return $info->getName(); |
426 | 426 | }, $files); |
@@ -502,7 +502,7 @@ discard block |
||
502 | 502 | */ |
503 | 503 | public static function checkDependencies() { |
504 | 504 | return ( |
505 | - (bool)\OC_Helper::findBinaryPath('smbclient') |
|
505 | + (bool) \OC_Helper::findBinaryPath('smbclient') |
|
506 | 506 | || NativeServer::available(new System()) |
507 | 507 | ) ? true : ['smbclient']; |
508 | 508 | } |
@@ -521,7 +521,7 @@ discard block |
||
521 | 521 | } |
522 | 522 | |
523 | 523 | public function listen($path, callable $callback) { |
524 | - $this->notify($path)->listen(function (IChange $change) use ($callback) { |
|
524 | + $this->notify($path)->listen(function(IChange $change) use ($callback) { |
|
525 | 525 | if ($change instanceof IRenameChange) { |
526 | 526 | return $callback($change->getType(), $change->getPath(), $change->getTargetPath()); |
527 | 527 | } else { |
@@ -531,7 +531,7 @@ discard block |
||
531 | 531 | } |
532 | 532 | |
533 | 533 | public function notify($path) { |
534 | - $path = '/' . ltrim($path, '/'); |
|
534 | + $path = '/'.ltrim($path, '/'); |
|
535 | 535 | $shareNotifyHandler = $this->share->notify($this->buildPath($path)); |
536 | 536 | return new SMBNotifyHandler($shareNotifyHandler, $this->root); |
537 | 537 | } |