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

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