You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Wrapper.php 16KB

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