Wrapper.php 16 KB

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