SMB.php 21 KB

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