Wrapper.php 15 KB

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