SMB.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_External\Lib\Storage;
  8. use Icewind\SMB\ACL;
  9. use Icewind\SMB\BasicAuth;
  10. use Icewind\SMB\Exception\AlreadyExistsException;
  11. use Icewind\SMB\Exception\ConnectException;
  12. use Icewind\SMB\Exception\Exception;
  13. use Icewind\SMB\Exception\ForbiddenException;
  14. use Icewind\SMB\Exception\InvalidArgumentException;
  15. use Icewind\SMB\Exception\InvalidTypeException;
  16. use Icewind\SMB\Exception\NotFoundException;
  17. use Icewind\SMB\Exception\OutOfSpaceException;
  18. use Icewind\SMB\Exception\TimedOutException;
  19. use Icewind\SMB\IFileInfo;
  20. use Icewind\SMB\Native\NativeServer;
  21. use Icewind\SMB\Options;
  22. use Icewind\SMB\ServerFactory;
  23. use Icewind\SMB\System;
  24. use Icewind\Streams\CallbackWrapper;
  25. use Icewind\Streams\IteratorDirectory;
  26. use OC\Files\Filesystem;
  27. use OC\Files\Storage\Common;
  28. use OCA\Files_External\Lib\Notify\SMBNotifyHandler;
  29. use OCP\Cache\CappedMemoryCache;
  30. use OCP\Constants;
  31. use OCP\Files\EntityTooLargeException;
  32. use OCP\Files\Notify\IChange;
  33. use OCP\Files\Notify\IRenameChange;
  34. use OCP\Files\NotPermittedException;
  35. use OCP\Files\Storage\INotifyStorage;
  36. use OCP\Files\StorageAuthException;
  37. use OCP\Files\StorageNotAvailableException;
  38. use Psr\Log\LoggerInterface;
  39. class SMB extends Common implements INotifyStorage {
  40. /**
  41. * @var \Icewind\SMB\IServer
  42. */
  43. protected $server;
  44. /**
  45. * @var \Icewind\SMB\IShare
  46. */
  47. protected $share;
  48. /**
  49. * @var string
  50. */
  51. protected $root;
  52. /** @var CappedMemoryCache<IFileInfo> */
  53. protected CappedMemoryCache $statCache;
  54. /** @var LoggerInterface */
  55. protected $logger;
  56. /** @var bool */
  57. protected $showHidden;
  58. private bool $caseSensitive;
  59. /** @var bool */
  60. protected $checkAcl;
  61. public function __construct($params) {
  62. if (!isset($params['host'])) {
  63. throw new \Exception('Invalid configuration, no host provided');
  64. }
  65. if (isset($params['auth'])) {
  66. $auth = $params['auth'];
  67. } elseif (isset($params['user']) && isset($params['password']) && isset($params['share'])) {
  68. [$workgroup, $user] = $this->splitUser($params['user']);
  69. $auth = new BasicAuth($user, $workgroup, $params['password']);
  70. } else {
  71. throw new \Exception('Invalid configuration, no credentials provided');
  72. }
  73. if (isset($params['logger'])) {
  74. if (!$params['logger'] instanceof LoggerInterface) {
  75. throw new \Exception(
  76. 'Invalid logger. Got '
  77. . get_class($params['logger'])
  78. . ' Expected ' . LoggerInterface::class
  79. );
  80. }
  81. $this->logger = $params['logger'];
  82. } else {
  83. $this->logger = \OC::$server->get(LoggerInterface::class);
  84. }
  85. $options = new Options();
  86. if (isset($params['timeout'])) {
  87. $timeout = (int)$params['timeout'];
  88. if ($timeout > 0) {
  89. $options->setTimeout($timeout);
  90. }
  91. }
  92. $serverFactory = new ServerFactory($options);
  93. $this->server = $serverFactory->createServer($params['host'], $auth);
  94. $this->share = $this->server->getShare(trim($params['share'], '/'));
  95. $this->root = $params['root'] ?? '/';
  96. $this->root = '/' . ltrim($this->root, '/');
  97. $this->root = rtrim($this->root, '/') . '/';
  98. $this->showHidden = isset($params['show_hidden']) && $params['show_hidden'];
  99. $this->caseSensitive = (bool)($params['case_sensitive'] ?? true);
  100. $this->checkAcl = isset($params['check_acl']) && $params['check_acl'];
  101. $this->statCache = new CappedMemoryCache();
  102. parent::__construct($params);
  103. }
  104. private function splitUser($user) {
  105. if (str_contains($user, '/')) {
  106. return explode('/', $user, 2);
  107. } elseif (str_contains($user, '\\')) {
  108. return explode('\\', $user);
  109. }
  110. return [null, $user];
  111. }
  112. /**
  113. * @return string
  114. */
  115. public function getId() {
  116. // FIXME: double slash to keep compatible with the old storage ids,
  117. // failure to do so will lead to creation of a new storage id and
  118. // loss of shares from the storage
  119. return 'smb::' . $this->server->getAuth()->getUsername() . '@' . $this->server->getHost() . '//' . $this->share->getName() . '/' . $this->root;
  120. }
  121. /**
  122. * @param string $path
  123. * @return string
  124. */
  125. protected function buildPath($path) {
  126. return Filesystem::normalizePath($this->root . '/' . $path, true, false, true);
  127. }
  128. protected function relativePath($fullPath) {
  129. if ($fullPath === $this->root) {
  130. return '';
  131. } elseif (substr($fullPath, 0, strlen($this->root)) === $this->root) {
  132. return substr($fullPath, strlen($this->root));
  133. } else {
  134. return null;
  135. }
  136. }
  137. /**
  138. * @param string $path
  139. * @return IFileInfo
  140. * @throws StorageAuthException
  141. */
  142. protected function getFileInfo($path) {
  143. try {
  144. $path = $this->buildPath($path);
  145. $cached = $this->statCache[$path] ?? null;
  146. if ($cached instanceof IFileInfo) {
  147. return $cached;
  148. } else {
  149. $stat = $this->share->stat($path);
  150. $this->statCache[$path] = $stat;
  151. return $stat;
  152. }
  153. } catch (ConnectException $e) {
  154. $this->throwUnavailable($e);
  155. } catch (NotFoundException $e) {
  156. throw new \OCP\Files\NotFoundException($e->getMessage(), 0, $e);
  157. } catch (ForbiddenException $e) {
  158. // with php-smbclient, this exception is thrown when the provided password is invalid.
  159. // Possible is also ForbiddenException with a different error code, so we check it.
  160. if ($e->getCode() === 1) {
  161. $this->throwUnavailable($e);
  162. }
  163. throw new \OCP\Files\ForbiddenException($e->getMessage(), false, $e);
  164. }
  165. }
  166. /**
  167. * @param \Exception $e
  168. * @return never
  169. * @throws StorageAuthException
  170. */
  171. protected function throwUnavailable(\Exception $e) {
  172. $this->logger->error('Error while getting file info', ['exception' => $e]);
  173. throw new StorageAuthException($e->getMessage(), $e);
  174. }
  175. /**
  176. * get the acl from fileinfo that is relevant for the configured user
  177. *
  178. * @param IFileInfo $file
  179. * @return ACL|null
  180. */
  181. private function getACL(IFileInfo $file): ?ACL {
  182. $acls = $file->getAcls();
  183. foreach ($acls as $user => $acl) {
  184. [, $user] = $this->splitUser($user); // strip domain
  185. if ($user === $this->server->getAuth()->getUsername()) {
  186. return $acl;
  187. }
  188. }
  189. return null;
  190. }
  191. /**
  192. * @param string $path
  193. * @return \Generator<IFileInfo>
  194. * @throws StorageNotAvailableException
  195. */
  196. protected function getFolderContents($path): iterable {
  197. try {
  198. $path = ltrim($this->buildPath($path), '/');
  199. try {
  200. $files = $this->share->dir($path);
  201. } catch (ForbiddenException $e) {
  202. $this->logger->critical($e->getMessage(), ['exception' => $e]);
  203. throw new NotPermittedException();
  204. } catch (InvalidTypeException $e) {
  205. return;
  206. }
  207. foreach ($files as $file) {
  208. $this->statCache[$path . '/' . $file->getName()] = $file;
  209. }
  210. foreach ($files as $file) {
  211. try {
  212. // the isHidden check is done before checking the config boolean to ensure that the metadata is always fetch
  213. // so we trigger the below exceptions where applicable
  214. $hide = $file->isHidden() && !$this->showHidden;
  215. if ($this->checkAcl && $acl = $this->getACL($file)) {
  216. // if there is no explicit deny, we assume it's allowed
  217. // this doesn't take inheritance fully into account but if read permissions is denied for a parent we wouldn't be in this folder
  218. // additionally, it's better to have false negatives here then false positives
  219. if ($acl->denies(ACL::MASK_READ) || $acl->denies(ACL::MASK_EXECUTE)) {
  220. $this->logger->debug('Hiding non readable entry ' . $file->getName());
  221. continue;
  222. }
  223. }
  224. if ($hide) {
  225. $this->logger->debug('hiding hidden file ' . $file->getName());
  226. }
  227. if (!$hide) {
  228. yield $file;
  229. }
  230. } catch (ForbiddenException $e) {
  231. $this->logger->debug($e->getMessage(), ['exception' => $e]);
  232. } catch (NotFoundException $e) {
  233. $this->logger->debug('Hiding forbidden entry ' . $file->getName(), ['exception' => $e]);
  234. }
  235. }
  236. } catch (ConnectException $e) {
  237. $this->logger->error('Error while getting folder content', ['exception' => $e]);
  238. throw new StorageNotAvailableException($e->getMessage(), (int)$e->getCode(), $e);
  239. } catch (NotFoundException $e) {
  240. throw new \OCP\Files\NotFoundException($e->getMessage(), 0, $e);
  241. }
  242. }
  243. /**
  244. * @param IFileInfo $info
  245. * @return array
  246. */
  247. protected function formatInfo($info) {
  248. $result = [
  249. 'size' => $info->getSize(),
  250. 'mtime' => $info->getMTime(),
  251. ];
  252. if ($info->isDirectory()) {
  253. $result['type'] = 'dir';
  254. } else {
  255. $result['type'] = 'file';
  256. }
  257. return $result;
  258. }
  259. /**
  260. * Rename the files. If the source or the target is the root, the rename won't happen.
  261. *
  262. * @param string $source the old name of the path
  263. * @param string $target the new name of the path
  264. * @return bool true if the rename is successful, false otherwise
  265. */
  266. public function rename($source, $target, $retry = true): bool {
  267. if ($this->isRootDir($source) || $this->isRootDir($target)) {
  268. return false;
  269. }
  270. if ($this->caseSensitive === false
  271. && mb_strtolower($target) === mb_strtolower($source)
  272. ) {
  273. // Forbid changing case only on case-insensitive file system
  274. return false;
  275. }
  276. $absoluteSource = $this->buildPath($source);
  277. $absoluteTarget = $this->buildPath($target);
  278. try {
  279. $result = $this->share->rename($absoluteSource, $absoluteTarget);
  280. } catch (AlreadyExistsException $e) {
  281. if ($retry) {
  282. $this->remove($target);
  283. $result = $this->share->rename($absoluteSource, $absoluteTarget);
  284. } else {
  285. $this->logger->warning($e->getMessage(), ['exception' => $e]);
  286. return false;
  287. }
  288. } catch (InvalidArgumentException $e) {
  289. if ($retry) {
  290. $this->remove($target);
  291. $result = $this->share->rename($absoluteSource, $absoluteTarget);
  292. } else {
  293. $this->logger->warning($e->getMessage(), ['exception' => $e]);
  294. return false;
  295. }
  296. } catch (\Exception $e) {
  297. $this->logger->warning($e->getMessage(), ['exception' => $e]);
  298. return false;
  299. }
  300. unset($this->statCache[$absoluteSource], $this->statCache[$absoluteTarget]);
  301. return $result;
  302. }
  303. public function stat($path, $retry = true) {
  304. try {
  305. $result = $this->formatInfo($this->getFileInfo($path));
  306. } catch (ForbiddenException $e) {
  307. return false;
  308. } catch (\OCP\Files\NotFoundException $e) {
  309. return false;
  310. } catch (TimedOutException $e) {
  311. if ($retry) {
  312. return $this->stat($path, false);
  313. } else {
  314. throw $e;
  315. }
  316. }
  317. if ($this->remoteIsShare() && $this->isRootDir($path)) {
  318. $result['mtime'] = $this->shareMTime();
  319. }
  320. return $result;
  321. }
  322. /**
  323. * get the best guess for the modification time of the share
  324. *
  325. * @return int
  326. */
  327. private function shareMTime() {
  328. $highestMTime = 0;
  329. $files = $this->share->dir($this->root);
  330. foreach ($files as $fileInfo) {
  331. try {
  332. if ($fileInfo->getMTime() > $highestMTime) {
  333. $highestMTime = $fileInfo->getMTime();
  334. }
  335. } catch (NotFoundException $e) {
  336. // Ignore this, can happen on unavailable DFS shares
  337. } catch (ForbiddenException $e) {
  338. // Ignore this too - it's a symlink
  339. }
  340. }
  341. return $highestMTime;
  342. }
  343. /**
  344. * Check if the path is our root dir (not the smb one)
  345. *
  346. * @param string $path the path
  347. * @return bool
  348. */
  349. private function isRootDir($path) {
  350. return $path === '' || $path === '/' || $path === '.';
  351. }
  352. /**
  353. * Check if our root points to a smb share
  354. *
  355. * @return bool true if our root points to a share false otherwise
  356. */
  357. private function remoteIsShare() {
  358. return $this->share->getName() && (!$this->root || $this->root === '/');
  359. }
  360. /**
  361. * @param string $path
  362. * @return bool
  363. */
  364. public function unlink($path) {
  365. if ($this->isRootDir($path)) {
  366. return false;
  367. }
  368. try {
  369. if ($this->is_dir($path)) {
  370. return $this->rmdir($path);
  371. } else {
  372. $path = $this->buildPath($path);
  373. unset($this->statCache[$path]);
  374. $this->share->del($path);
  375. return true;
  376. }
  377. } catch (NotFoundException $e) {
  378. return false;
  379. } catch (ForbiddenException $e) {
  380. return false;
  381. } catch (ConnectException $e) {
  382. $this->logger->error('Error while deleting file', ['exception' => $e]);
  383. throw new StorageNotAvailableException($e->getMessage(), (int)$e->getCode(), $e);
  384. }
  385. }
  386. /**
  387. * check if a file or folder has been updated since $time
  388. *
  389. * @param string $path
  390. * @param int $time
  391. * @return bool
  392. */
  393. public function hasUpdated($path, $time) {
  394. if (!$path and $this->root === '/') {
  395. // mtime doesn't work for shares, but giving the nature of the backend,
  396. // doing a full update is still just fast enough
  397. return true;
  398. } else {
  399. $actualTime = $this->filemtime($path);
  400. return $actualTime > $time;
  401. }
  402. }
  403. /**
  404. * @param string $path
  405. * @param string $mode
  406. * @return resource|bool
  407. */
  408. public function fopen($path, $mode) {
  409. $fullPath = $this->buildPath($path);
  410. try {
  411. switch ($mode) {
  412. case 'r':
  413. case 'rb':
  414. if (!$this->file_exists($path)) {
  415. return false;
  416. }
  417. return $this->share->read($fullPath);
  418. case 'w':
  419. case 'wb':
  420. $source = $this->share->write($fullPath);
  421. return CallBackWrapper::wrap($source, null, null, function () use ($fullPath) {
  422. unset($this->statCache[$fullPath]);
  423. });
  424. case 'a':
  425. case 'ab':
  426. case 'r+':
  427. case 'w+':
  428. case 'wb+':
  429. case 'a+':
  430. case 'x':
  431. case 'x+':
  432. case 'c':
  433. case 'c+':
  434. //emulate these
  435. if (strrpos($path, '.') !== false) {
  436. $ext = substr($path, strrpos($path, '.'));
  437. } else {
  438. $ext = '';
  439. }
  440. if ($this->file_exists($path)) {
  441. if (!$this->isUpdatable($path)) {
  442. return false;
  443. }
  444. $tmpFile = $this->getCachedFile($path);
  445. } else {
  446. if (!$this->isCreatable(dirname($path))) {
  447. return false;
  448. }
  449. $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($ext);
  450. }
  451. $source = fopen($tmpFile, $mode);
  452. $share = $this->share;
  453. return CallbackWrapper::wrap($source, null, null, function () use ($tmpFile, $fullPath, $share) {
  454. unset($this->statCache[$fullPath]);
  455. $share->put($tmpFile, $fullPath);
  456. unlink($tmpFile);
  457. });
  458. }
  459. return false;
  460. } catch (NotFoundException $e) {
  461. return false;
  462. } catch (ForbiddenException $e) {
  463. return false;
  464. } catch (OutOfSpaceException $e) {
  465. throw new EntityTooLargeException('not enough available space to create file', 0, $e);
  466. } catch (ConnectException $e) {
  467. $this->logger->error('Error while opening file', ['exception' => $e]);
  468. throw new StorageNotAvailableException($e->getMessage(), (int)$e->getCode(), $e);
  469. }
  470. }
  471. public function rmdir($path) {
  472. if ($this->isRootDir($path)) {
  473. return false;
  474. }
  475. try {
  476. $this->statCache = new CappedMemoryCache();
  477. $content = $this->share->dir($this->buildPath($path));
  478. foreach ($content as $file) {
  479. if ($file->isDirectory()) {
  480. $this->rmdir($path . '/' . $file->getName());
  481. } else {
  482. $this->share->del($file->getPath());
  483. }
  484. }
  485. $this->share->rmdir($this->buildPath($path));
  486. return true;
  487. } catch (NotFoundException $e) {
  488. return false;
  489. } catch (ForbiddenException $e) {
  490. return false;
  491. } catch (ConnectException $e) {
  492. $this->logger->error('Error while removing folder', ['exception' => $e]);
  493. throw new StorageNotAvailableException($e->getMessage(), (int)$e->getCode(), $e);
  494. }
  495. }
  496. public function touch($path, $mtime = null) {
  497. try {
  498. if (!$this->file_exists($path)) {
  499. $fh = $this->share->write($this->buildPath($path));
  500. fclose($fh);
  501. return true;
  502. }
  503. return false;
  504. } catch (OutOfSpaceException $e) {
  505. throw new EntityTooLargeException('not enough available space to create file', 0, $e);
  506. } catch (ConnectException $e) {
  507. $this->logger->error('Error while creating file', ['exception' => $e]);
  508. throw new StorageNotAvailableException($e->getMessage(), (int)$e->getCode(), $e);
  509. }
  510. }
  511. public function getMetaData($path) {
  512. try {
  513. $fileInfo = $this->getFileInfo($path);
  514. } catch (\OCP\Files\NotFoundException $e) {
  515. return null;
  516. } catch (ForbiddenException $e) {
  517. return null;
  518. }
  519. if (!$fileInfo) {
  520. return null;
  521. }
  522. return $this->getMetaDataFromFileInfo($fileInfo);
  523. }
  524. private function getMetaDataFromFileInfo(IFileInfo $fileInfo) {
  525. $permissions = Constants::PERMISSION_READ + Constants::PERMISSION_SHARE;
  526. if (
  527. !$fileInfo->isReadOnly() || $fileInfo->isDirectory()
  528. ) {
  529. $permissions += Constants::PERMISSION_DELETE;
  530. $permissions += Constants::PERMISSION_UPDATE;
  531. if ($fileInfo->isDirectory()) {
  532. $permissions += Constants::PERMISSION_CREATE;
  533. }
  534. }
  535. $data = [];
  536. if ($fileInfo->isDirectory()) {
  537. $data['mimetype'] = 'httpd/unix-directory';
  538. } else {
  539. $data['mimetype'] = \OC::$server->getMimeTypeDetector()->detectPath($fileInfo->getPath());
  540. }
  541. $data['mtime'] = $fileInfo->getMTime();
  542. if ($fileInfo->isDirectory()) {
  543. $data['size'] = -1; //unknown
  544. } else {
  545. $data['size'] = $fileInfo->getSize();
  546. }
  547. $data['etag'] = $this->getETag($fileInfo->getPath());
  548. $data['storage_mtime'] = $data['mtime'];
  549. $data['permissions'] = $permissions;
  550. $data['name'] = $fileInfo->getName();
  551. return $data;
  552. }
  553. public function opendir($path) {
  554. try {
  555. $files = $this->getFolderContents($path);
  556. } catch (NotFoundException $e) {
  557. return false;
  558. } catch (NotPermittedException $e) {
  559. return false;
  560. }
  561. $names = array_map(function ($info) {
  562. /** @var IFileInfo $info */
  563. return $info->getName();
  564. }, iterator_to_array($files));
  565. return IteratorDirectory::wrap($names);
  566. }
  567. public function getDirectoryContent($directory): \Traversable {
  568. try {
  569. $files = $this->getFolderContents($directory);
  570. foreach ($files as $file) {
  571. yield $this->getMetaDataFromFileInfo($file);
  572. }
  573. } catch (NotFoundException $e) {
  574. return;
  575. } catch (NotPermittedException $e) {
  576. return;
  577. }
  578. }
  579. public function filetype($path) {
  580. try {
  581. return $this->getFileInfo($path)->isDirectory() ? 'dir' : 'file';
  582. } catch (\OCP\Files\NotFoundException $e) {
  583. return false;
  584. } catch (ForbiddenException $e) {
  585. return false;
  586. }
  587. }
  588. public function mkdir($path) {
  589. $path = $this->buildPath($path);
  590. try {
  591. $this->share->mkdir($path);
  592. return true;
  593. } catch (ConnectException $e) {
  594. $this->logger->error('Error while creating folder', ['exception' => $e]);
  595. throw new StorageNotAvailableException($e->getMessage(), (int)$e->getCode(), $e);
  596. } catch (Exception $e) {
  597. return false;
  598. }
  599. }
  600. public function file_exists($path) {
  601. try {
  602. // Case sensitive filesystem doesn't matter for root directory
  603. if ($this->caseSensitive === false && $path !== '') {
  604. $filename = basename($path);
  605. $siblings = $this->getDirectoryContent(dirname($this->buildPath($path)));
  606. foreach ($siblings as $sibling) {
  607. if ($sibling['name'] === $filename) {
  608. return true;
  609. }
  610. }
  611. return false;
  612. }
  613. $this->getFileInfo($path);
  614. return true;
  615. } catch (\OCP\Files\NotFoundException $e) {
  616. return false;
  617. } catch (ForbiddenException $e) {
  618. return false;
  619. } catch (ConnectException $e) {
  620. throw new StorageNotAvailableException($e->getMessage(), (int)$e->getCode(), $e);
  621. }
  622. }
  623. public function isReadable($path) {
  624. try {
  625. $info = $this->getFileInfo($path);
  626. return $this->showHidden || !$info->isHidden();
  627. } catch (\OCP\Files\NotFoundException $e) {
  628. return false;
  629. } catch (ForbiddenException $e) {
  630. return false;
  631. }
  632. }
  633. public function isUpdatable($path) {
  634. try {
  635. $info = $this->getFileInfo($path);
  636. // following windows behaviour for read-only folders: they can be written into
  637. // (https://support.microsoft.com/en-us/kb/326549 - "cause" section)
  638. return ($this->showHidden || !$info->isHidden()) && (!$info->isReadOnly() || $info->isDirectory());
  639. } catch (\OCP\Files\NotFoundException $e) {
  640. return false;
  641. } catch (ForbiddenException $e) {
  642. return false;
  643. }
  644. }
  645. public function isDeletable($path) {
  646. try {
  647. $info = $this->getFileInfo($path);
  648. return ($this->showHidden || !$info->isHidden()) && !$info->isReadOnly();
  649. } catch (\OCP\Files\NotFoundException $e) {
  650. return false;
  651. } catch (ForbiddenException $e) {
  652. return false;
  653. }
  654. }
  655. /**
  656. * check if smbclient is installed
  657. */
  658. public static function checkDependencies() {
  659. return (
  660. (bool)\OC_Helper::findBinaryPath('smbclient')
  661. || NativeServer::available(new System())
  662. ) ? true : ['smbclient'];
  663. }
  664. /**
  665. * Test a storage for availability
  666. *
  667. * @return bool
  668. */
  669. public function test() {
  670. try {
  671. return parent::test();
  672. } catch (StorageAuthException $e) {
  673. return false;
  674. } catch (ForbiddenException $e) {
  675. return false;
  676. } catch (Exception $e) {
  677. $this->logger->error($e->getMessage(), ['exception' => $e]);
  678. return false;
  679. }
  680. }
  681. public function listen($path, callable $callback) {
  682. $this->notify($path)->listen(function (IChange $change) use ($callback) {
  683. if ($change instanceof IRenameChange) {
  684. return $callback($change->getType(), $change->getPath(), $change->getTargetPath());
  685. } else {
  686. return $callback($change->getType(), $change->getPath());
  687. }
  688. });
  689. }
  690. public function notify($path) {
  691. $path = '/' . ltrim($path, '/');
  692. $shareNotifyHandler = $this->share->notify($this->buildPath($path));
  693. return new SMBNotifyHandler($shareNotifyHandler, $this->root);
  694. }
  695. }