Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

SMB.php 20KB

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