Wrapper.php 16 KB

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