Wrapper.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author J0WI <J0WI@users.noreply.github.com>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. * @author Robin McCorkell <robin@mccorkell.me.uk>
  12. * @author Thomas Müller <thomas.mueller@tmit.eu>
  13. * @author Tigran Mkrtchyan <tigran.mkrtchyan@desy.de>
  14. * @author Vincent Petry <vincent@nextcloud.com>
  15. * @author Vinicius Cubas Brand <vinicius@eita.org.br>
  16. *
  17. * @license AGPL-3.0
  18. *
  19. * This code is free software: you can redistribute it and/or modify
  20. * it under the terms of the GNU Affero General Public License, version 3,
  21. * as published by the Free Software Foundation.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU Affero General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU Affero General Public License, version 3,
  29. * along with this program. If not, see <http://www.gnu.org/licenses/>
  30. *
  31. */
  32. namespace OC\Files\Storage\Wrapper;
  33. use OC\Files\Storage\FailedStorage;
  34. use OCP\Files\InvalidPathException;
  35. use OCP\Files\Storage\ILockingStorage;
  36. use OCP\Files\Storage\IStorage;
  37. use OCP\Files\Storage\IWriteStreamStorage;
  38. use OCP\Lock\ILockingProvider;
  39. use OCP\Server;
  40. use Psr\Log\LoggerInterface;
  41. class Wrapper implements \OC\Files\Storage\Storage, ILockingStorage, IWriteStreamStorage {
  42. /**
  43. * @var \OC\Files\Storage\Storage $storage
  44. */
  45. protected $storage;
  46. public $cache;
  47. public $scanner;
  48. public $watcher;
  49. public $propagator;
  50. public $updater;
  51. /**
  52. * @param array $parameters
  53. */
  54. public function __construct($parameters) {
  55. $this->storage = $parameters['storage'];
  56. }
  57. /**
  58. * @return \OC\Files\Storage\Storage
  59. */
  60. public function getWrapperStorage() {
  61. if (!$this->storage) {
  62. $message = "storage wrapper " . get_class($this) . " doesn't have a wrapped storage set";
  63. $logger = Server::get(LoggerInterface::class);
  64. $logger->error($message);
  65. $this->storage = new FailedStorage(['exception' => new \Exception($message)]);
  66. }
  67. return $this->storage;
  68. }
  69. /**
  70. * Get the identifier for the storage,
  71. * the returned id should be the same for every storage object that is created with the same parameters
  72. * and two storage objects with the same id should refer to two storages that display the same files.
  73. *
  74. * @return string
  75. */
  76. public function getId() {
  77. return $this->getWrapperStorage()->getId();
  78. }
  79. /**
  80. * see https://www.php.net/manual/en/function.mkdir.php
  81. *
  82. * @param string $path
  83. * @return bool
  84. */
  85. public function mkdir($path) {
  86. return $this->getWrapperStorage()->mkdir($path);
  87. }
  88. /**
  89. * see https://www.php.net/manual/en/function.rmdir.php
  90. *
  91. * @param string $path
  92. * @return bool
  93. */
  94. public function rmdir($path) {
  95. return $this->getWrapperStorage()->rmdir($path);
  96. }
  97. /**
  98. * see https://www.php.net/manual/en/function.opendir.php
  99. *
  100. * @param string $path
  101. * @return resource|false
  102. */
  103. public function opendir($path) {
  104. return $this->getWrapperStorage()->opendir($path);
  105. }
  106. /**
  107. * see https://www.php.net/manual/en/function.is_dir.php
  108. *
  109. * @param string $path
  110. * @return bool
  111. */
  112. public function is_dir($path) {
  113. return $this->getWrapperStorage()->is_dir($path);
  114. }
  115. /**
  116. * see https://www.php.net/manual/en/function.is_file.php
  117. *
  118. * @param string $path
  119. * @return bool
  120. */
  121. public function is_file($path) {
  122. return $this->getWrapperStorage()->is_file($path);
  123. }
  124. /**
  125. * see https://www.php.net/manual/en/function.stat.php
  126. * only the following keys are required in the result: size and mtime
  127. *
  128. * @param string $path
  129. * @return array|bool
  130. */
  131. public function stat($path) {
  132. return $this->getWrapperStorage()->stat($path);
  133. }
  134. /**
  135. * see https://www.php.net/manual/en/function.filetype.php
  136. *
  137. * @param string $path
  138. * @return string|bool
  139. */
  140. public function filetype($path) {
  141. return $this->getWrapperStorage()->filetype($path);
  142. }
  143. /**
  144. * see https://www.php.net/manual/en/function.filesize.php
  145. * The result for filesize when called on a folder is required to be 0
  146. */
  147. public function filesize($path): false|int|float {
  148. return $this->getWrapperStorage()->filesize($path);
  149. }
  150. /**
  151. * check if a file can be created in $path
  152. *
  153. * @param string $path
  154. * @return bool
  155. */
  156. public function isCreatable($path) {
  157. return $this->getWrapperStorage()->isCreatable($path);
  158. }
  159. /**
  160. * check if a file can be read
  161. *
  162. * @param string $path
  163. * @return bool
  164. */
  165. public function isReadable($path) {
  166. return $this->getWrapperStorage()->isReadable($path);
  167. }
  168. /**
  169. * check if a file can be written to
  170. *
  171. * @param string $path
  172. * @return bool
  173. */
  174. public function isUpdatable($path) {
  175. return $this->getWrapperStorage()->isUpdatable($path);
  176. }
  177. /**
  178. * check if a file can be deleted
  179. *
  180. * @param string $path
  181. * @return bool
  182. */
  183. public function isDeletable($path) {
  184. return $this->getWrapperStorage()->isDeletable($path);
  185. }
  186. /**
  187. * check if a file can be shared
  188. *
  189. * @param string $path
  190. * @return bool
  191. */
  192. public function isSharable($path) {
  193. return $this->getWrapperStorage()->isSharable($path);
  194. }
  195. /**
  196. * get the full permissions of a path.
  197. * Should return a combination of the PERMISSION_ constants defined in lib/public/constants.php
  198. *
  199. * @param string $path
  200. * @return int
  201. */
  202. public function getPermissions($path) {
  203. return $this->getWrapperStorage()->getPermissions($path);
  204. }
  205. /**
  206. * see https://www.php.net/manual/en/function.file_exists.php
  207. *
  208. * @param string $path
  209. * @return bool
  210. */
  211. public function file_exists($path) {
  212. return $this->getWrapperStorage()->file_exists($path);
  213. }
  214. /**
  215. * see https://www.php.net/manual/en/function.filemtime.php
  216. *
  217. * @param string $path
  218. * @return int|bool
  219. */
  220. public function filemtime($path) {
  221. return $this->getWrapperStorage()->filemtime($path);
  222. }
  223. /**
  224. * see https://www.php.net/manual/en/function.file_get_contents.php
  225. *
  226. * @param string $path
  227. * @return string|false
  228. */
  229. public function file_get_contents($path) {
  230. return $this->getWrapperStorage()->file_get_contents($path);
  231. }
  232. /**
  233. * see https://www.php.net/manual/en/function.file_put_contents.php
  234. *
  235. * @param string $path
  236. * @param mixed $data
  237. * @return int|float|false
  238. */
  239. public function file_put_contents($path, $data) {
  240. return $this->getWrapperStorage()->file_put_contents($path, $data);
  241. }
  242. /**
  243. * see https://www.php.net/manual/en/function.unlink.php
  244. *
  245. * @param string $path
  246. * @return bool
  247. */
  248. public function unlink($path) {
  249. return $this->getWrapperStorage()->unlink($path);
  250. }
  251. /**
  252. * see https://www.php.net/manual/en/function.rename.php
  253. *
  254. * @param string $source
  255. * @param string $target
  256. * @return bool
  257. */
  258. public function rename($source, $target) {
  259. return $this->getWrapperStorage()->rename($source, $target);
  260. }
  261. /**
  262. * see https://www.php.net/manual/en/function.copy.php
  263. *
  264. * @param string $source
  265. * @param string $target
  266. * @return bool
  267. */
  268. public function copy($source, $target) {
  269. return $this->getWrapperStorage()->copy($source, $target);
  270. }
  271. /**
  272. * see https://www.php.net/manual/en/function.fopen.php
  273. *
  274. * @param string $path
  275. * @param string $mode
  276. * @return resource|bool
  277. */
  278. public function fopen($path, $mode) {
  279. return $this->getWrapperStorage()->fopen($path, $mode);
  280. }
  281. /**
  282. * get the mimetype for a file or folder
  283. * The mimetype for a folder is required to be "httpd/unix-directory"
  284. *
  285. * @param string $path
  286. * @return string|bool
  287. */
  288. public function getMimeType($path) {
  289. return $this->getWrapperStorage()->getMimeType($path);
  290. }
  291. /**
  292. * see https://www.php.net/manual/en/function.hash.php
  293. *
  294. * @param string $type
  295. * @param string $path
  296. * @param bool $raw
  297. * @return string|bool
  298. */
  299. public function hash($type, $path, $raw = false) {
  300. return $this->getWrapperStorage()->hash($type, $path, $raw);
  301. }
  302. /**
  303. * see https://www.php.net/manual/en/function.free_space.php
  304. *
  305. * @param string $path
  306. * @return int|float|bool
  307. */
  308. public function free_space($path) {
  309. return $this->getWrapperStorage()->free_space($path);
  310. }
  311. /**
  312. * search for occurrences of $query in file names
  313. *
  314. * @param string $query
  315. * @return array|bool
  316. */
  317. public function search($query) {
  318. return $this->getWrapperStorage()->search($query);
  319. }
  320. /**
  321. * see https://www.php.net/manual/en/function.touch.php
  322. * If the backend does not support the operation, false should be returned
  323. *
  324. * @param string $path
  325. * @param int $mtime
  326. * @return bool
  327. */
  328. public function touch($path, $mtime = null) {
  329. return $this->getWrapperStorage()->touch($path, $mtime);
  330. }
  331. /**
  332. * get the path to a local version of the file.
  333. * The local version of the file can be temporary and doesn't have to be persistent across requests
  334. *
  335. * @param string $path
  336. * @return string|false
  337. */
  338. public function getLocalFile($path) {
  339. return $this->getWrapperStorage()->getLocalFile($path);
  340. }
  341. /**
  342. * check if a file or folder has been updated since $time
  343. *
  344. * @param string $path
  345. * @param int $time
  346. * @return bool
  347. *
  348. * hasUpdated for folders should return at least true if a file inside the folder is add, removed or renamed.
  349. * returning true for other changes in the folder is optional
  350. */
  351. public function hasUpdated($path, $time) {
  352. return $this->getWrapperStorage()->hasUpdated($path, $time);
  353. }
  354. /**
  355. * get a cache instance for the storage
  356. *
  357. * @param string $path
  358. * @param \OC\Files\Storage\Storage|null (optional) the storage to pass to the cache
  359. * @return \OC\Files\Cache\Cache
  360. */
  361. public function getCache($path = '', $storage = null) {
  362. if (!$storage) {
  363. $storage = $this;
  364. }
  365. return $this->getWrapperStorage()->getCache($path, $storage);
  366. }
  367. /**
  368. * get a scanner instance for the storage
  369. *
  370. * @param string $path
  371. * @param \OC\Files\Storage\Storage (optional) the storage to pass to the scanner
  372. * @return \OC\Files\Cache\Scanner
  373. */
  374. public function getScanner($path = '', $storage = null) {
  375. if (!$storage) {
  376. $storage = $this;
  377. }
  378. return $this->getWrapperStorage()->getScanner($path, $storage);
  379. }
  380. /**
  381. * get the user id of the owner of a file or folder
  382. *
  383. * @param string $path
  384. * @return string
  385. */
  386. public function getOwner($path) {
  387. return $this->getWrapperStorage()->getOwner($path);
  388. }
  389. /**
  390. * get a watcher instance for the cache
  391. *
  392. * @param string $path
  393. * @param \OC\Files\Storage\Storage (optional) the storage to pass to the watcher
  394. * @return \OC\Files\Cache\Watcher
  395. */
  396. public function getWatcher($path = '', $storage = null) {
  397. if (!$storage) {
  398. $storage = $this;
  399. }
  400. return $this->getWrapperStorage()->getWatcher($path, $storage);
  401. }
  402. public function getPropagator($storage = null) {
  403. if (!$storage) {
  404. $storage = $this;
  405. }
  406. return $this->getWrapperStorage()->getPropagator($storage);
  407. }
  408. public function getUpdater($storage = null) {
  409. if (!$storage) {
  410. $storage = $this;
  411. }
  412. return $this->getWrapperStorage()->getUpdater($storage);
  413. }
  414. /**
  415. * @return \OC\Files\Cache\Storage
  416. */
  417. public function getStorageCache() {
  418. return $this->getWrapperStorage()->getStorageCache();
  419. }
  420. /**
  421. * get the ETag for a file or folder
  422. *
  423. * @param string $path
  424. * @return string|false
  425. */
  426. public function getETag($path) {
  427. return $this->getWrapperStorage()->getETag($path);
  428. }
  429. /**
  430. * Returns true
  431. *
  432. * @return true
  433. */
  434. public function test() {
  435. return $this->getWrapperStorage()->test();
  436. }
  437. /**
  438. * Returns the wrapped storage's value for isLocal()
  439. *
  440. * @return bool wrapped storage's isLocal() value
  441. */
  442. public function isLocal() {
  443. return $this->getWrapperStorage()->isLocal();
  444. }
  445. /**
  446. * Check if the storage is an instance of $class or is a wrapper for a storage that is an instance of $class
  447. *
  448. * @param class-string<IStorage> $class
  449. * @return bool
  450. */
  451. public function instanceOfStorage($class) {
  452. if (ltrim($class, '\\') === 'OC\Files\Storage\Shared') {
  453. // FIXME Temporary fix to keep existing checks working
  454. $class = '\OCA\Files_Sharing\SharedStorage';
  455. }
  456. return is_a($this, $class) or $this->getWrapperStorage()->instanceOfStorage($class);
  457. }
  458. /**
  459. * @psalm-template T of IStorage
  460. * @psalm-param class-string<T> $class
  461. * @psalm-return T|null
  462. */
  463. public function getInstanceOfStorage(string $class) {
  464. $storage = $this;
  465. while ($storage instanceof Wrapper) {
  466. if ($storage instanceof $class) {
  467. break;
  468. }
  469. $storage = $storage->getWrapperStorage();
  470. }
  471. if (!($storage instanceof $class)) {
  472. return null;
  473. }
  474. return $storage;
  475. }
  476. /**
  477. * Pass any methods custom to specific storage implementations to the wrapped storage
  478. *
  479. * @param string $method
  480. * @param array $args
  481. * @return mixed
  482. */
  483. public function __call($method, $args) {
  484. return call_user_func_array([$this->getWrapperStorage(), $method], $args);
  485. }
  486. /**
  487. * A custom storage implementation can return an url for direct download of a give file.
  488. *
  489. * For now the returned array can hold the parameter url - in future more attributes might follow.
  490. *
  491. * @param string $path
  492. * @return array|bool
  493. */
  494. public function getDirectDownload($path) {
  495. return $this->getWrapperStorage()->getDirectDownload($path);
  496. }
  497. /**
  498. * Get availability of the storage
  499. *
  500. * @return array [ available, last_checked ]
  501. */
  502. public function getAvailability() {
  503. return $this->getWrapperStorage()->getAvailability();
  504. }
  505. /**
  506. * Set availability of the storage
  507. *
  508. * @param bool $isAvailable
  509. */
  510. public function setAvailability($isAvailable) {
  511. $this->getWrapperStorage()->setAvailability($isAvailable);
  512. }
  513. /**
  514. * @param string $path the path of the target folder
  515. * @param string $fileName the name of the file itself
  516. * @return void
  517. * @throws InvalidPathException
  518. */
  519. public function verifyPath($path, $fileName) {
  520. $this->getWrapperStorage()->verifyPath($path, $fileName);
  521. }
  522. /**
  523. * @param IStorage $sourceStorage
  524. * @param string $sourceInternalPath
  525. * @param string $targetInternalPath
  526. * @return bool
  527. */
  528. public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
  529. if ($sourceStorage === $this) {
  530. return $this->copy($sourceInternalPath, $targetInternalPath);
  531. }
  532. return $this->getWrapperStorage()->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
  533. }
  534. /**
  535. * @param IStorage $sourceStorage
  536. * @param string $sourceInternalPath
  537. * @param string $targetInternalPath
  538. * @return bool
  539. */
  540. public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
  541. if ($sourceStorage === $this) {
  542. return $this->rename($sourceInternalPath, $targetInternalPath);
  543. }
  544. return $this->getWrapperStorage()->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
  545. }
  546. public function getMetaData($path) {
  547. return $this->getWrapperStorage()->getMetaData($path);
  548. }
  549. /**
  550. * @param string $path
  551. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  552. * @param \OCP\Lock\ILockingProvider $provider
  553. * @throws \OCP\Lock\LockedException
  554. */
  555. public function acquireLock($path, $type, ILockingProvider $provider) {
  556. if ($this->getWrapperStorage()->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) {
  557. $this->getWrapperStorage()->acquireLock($path, $type, $provider);
  558. }
  559. }
  560. /**
  561. * @param string $path
  562. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  563. * @param \OCP\Lock\ILockingProvider $provider
  564. */
  565. public function releaseLock($path, $type, ILockingProvider $provider) {
  566. if ($this->getWrapperStorage()->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) {
  567. $this->getWrapperStorage()->releaseLock($path, $type, $provider);
  568. }
  569. }
  570. /**
  571. * @param string $path
  572. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  573. * @param \OCP\Lock\ILockingProvider $provider
  574. */
  575. public function changeLock($path, $type, ILockingProvider $provider) {
  576. if ($this->getWrapperStorage()->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) {
  577. $this->getWrapperStorage()->changeLock($path, $type, $provider);
  578. }
  579. }
  580. /**
  581. * @return bool
  582. */
  583. public function needsPartFile() {
  584. return $this->getWrapperStorage()->needsPartFile();
  585. }
  586. public function writeStream(string $path, $stream, ?int $size = null): int {
  587. $storage = $this->getWrapperStorage();
  588. if ($storage->instanceOfStorage(IWriteStreamStorage::class)) {
  589. /** @var IWriteStreamStorage $storage */
  590. return $storage->writeStream($path, $stream, $size);
  591. } else {
  592. $target = $this->fopen($path, 'w');
  593. [$count, $result] = \OC_Helper::streamCopy($stream, $target);
  594. fclose($stream);
  595. fclose($target);
  596. return $count;
  597. }
  598. }
  599. public function getDirectoryContent($directory): \Traversable {
  600. return $this->getWrapperStorage()->getDirectoryContent($directory);
  601. }
  602. public function isWrapperOf(IStorage $storage) {
  603. $wrapped = $this->getWrapperStorage();
  604. if ($wrapped === $storage) {
  605. return true;
  606. }
  607. if ($wrapped instanceof Wrapper) {
  608. return $wrapped->isWrapperOf($storage);
  609. }
  610. return false;
  611. }
  612. }