Wrapper.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 OC\Files\Storage\Storage;
  10. use OCP\Files\Cache\ICache;
  11. use OCP\Files\Cache\IPropagator;
  12. use OCP\Files\Cache\IScanner;
  13. use OCP\Files\Cache\IUpdater;
  14. use OCP\Files\Cache\IWatcher;
  15. use OCP\Files\Storage\ILockingStorage;
  16. use OCP\Files\Storage\IStorage;
  17. use OCP\Files\Storage\IWriteStreamStorage;
  18. use OCP\Lock\ILockingProvider;
  19. use OCP\Server;
  20. use Psr\Log\LoggerInterface;
  21. class Wrapper implements \OC\Files\Storage\Storage, ILockingStorage, IWriteStreamStorage {
  22. /**
  23. * @var \OC\Files\Storage\Storage $storage
  24. */
  25. protected $storage;
  26. public $cache;
  27. public $scanner;
  28. public $watcher;
  29. public $propagator;
  30. public $updater;
  31. /**
  32. * @param array $parameters
  33. */
  34. public function __construct(array $parameters) {
  35. $this->storage = $parameters['storage'];
  36. }
  37. public function getWrapperStorage(): Storage {
  38. if (!$this->storage) {
  39. $message = 'storage wrapper ' . get_class($this) . " doesn't have a wrapped storage set";
  40. $logger = Server::get(LoggerInterface::class);
  41. $logger->error($message);
  42. $this->storage = new FailedStorage(['exception' => new \Exception($message)]);
  43. }
  44. return $this->storage;
  45. }
  46. public function getId(): string {
  47. return $this->getWrapperStorage()->getId();
  48. }
  49. public function mkdir(string $path): bool {
  50. return $this->getWrapperStorage()->mkdir($path);
  51. }
  52. public function rmdir(string $path): bool {
  53. return $this->getWrapperStorage()->rmdir($path);
  54. }
  55. public function opendir(string $path) {
  56. return $this->getWrapperStorage()->opendir($path);
  57. }
  58. public function is_dir(string $path): bool {
  59. return $this->getWrapperStorage()->is_dir($path);
  60. }
  61. public function is_file(string $path): bool {
  62. return $this->getWrapperStorage()->is_file($path);
  63. }
  64. public function stat(string $path): array|false {
  65. return $this->getWrapperStorage()->stat($path);
  66. }
  67. public function filetype(string $path): string|false {
  68. return $this->getWrapperStorage()->filetype($path);
  69. }
  70. public function filesize(string $path): int|float|false {
  71. return $this->getWrapperStorage()->filesize($path);
  72. }
  73. public function isCreatable(string $path): bool {
  74. return $this->getWrapperStorage()->isCreatable($path);
  75. }
  76. public function isReadable(string $path): bool {
  77. return $this->getWrapperStorage()->isReadable($path);
  78. }
  79. public function isUpdatable(string $path): bool {
  80. return $this->getWrapperStorage()->isUpdatable($path);
  81. }
  82. public function isDeletable(string $path): bool {
  83. return $this->getWrapperStorage()->isDeletable($path);
  84. }
  85. public function isSharable(string $path): bool {
  86. return $this->getWrapperStorage()->isSharable($path);
  87. }
  88. public function getPermissions(string $path): int {
  89. return $this->getWrapperStorage()->getPermissions($path);
  90. }
  91. public function file_exists(string $path): bool {
  92. return $this->getWrapperStorage()->file_exists($path);
  93. }
  94. public function filemtime(string $path): int|false {
  95. return $this->getWrapperStorage()->filemtime($path);
  96. }
  97. public function file_get_contents(string $path): string|false {
  98. return $this->getWrapperStorage()->file_get_contents($path);
  99. }
  100. public function file_put_contents(string $path, mixed $data): int|float|false {
  101. return $this->getWrapperStorage()->file_put_contents($path, $data);
  102. }
  103. public function unlink(string $path): bool {
  104. return $this->getWrapperStorage()->unlink($path);
  105. }
  106. public function rename(string $source, string $target): bool {
  107. return $this->getWrapperStorage()->rename($source, $target);
  108. }
  109. public function copy(string $source, string $target): bool {
  110. return $this->getWrapperStorage()->copy($source, $target);
  111. }
  112. public function fopen(string $path, string $mode) {
  113. return $this->getWrapperStorage()->fopen($path, $mode);
  114. }
  115. public function getMimeType(string $path): string|false {
  116. return $this->getWrapperStorage()->getMimeType($path);
  117. }
  118. public function hash(string $type, string $path, bool $raw = false): string|false {
  119. return $this->getWrapperStorage()->hash($type, $path, $raw);
  120. }
  121. public function free_space(string $path): int|float|false {
  122. return $this->getWrapperStorage()->free_space($path);
  123. }
  124. public function touch(string $path, ?int $mtime = null): bool {
  125. return $this->getWrapperStorage()->touch($path, $mtime);
  126. }
  127. public function getLocalFile(string $path): string|false {
  128. return $this->getWrapperStorage()->getLocalFile($path);
  129. }
  130. public function hasUpdated(string $path, int $time): bool {
  131. return $this->getWrapperStorage()->hasUpdated($path, $time);
  132. }
  133. public function getCache(string $path = '', ?IStorage $storage = null): ICache {
  134. if (!$storage) {
  135. $storage = $this;
  136. }
  137. return $this->getWrapperStorage()->getCache($path, $storage);
  138. }
  139. public function getScanner(string $path = '', ?IStorage $storage = null): IScanner {
  140. if (!$storage) {
  141. $storage = $this;
  142. }
  143. return $this->getWrapperStorage()->getScanner($path, $storage);
  144. }
  145. public function getOwner(string $path): string|false {
  146. return $this->getWrapperStorage()->getOwner($path);
  147. }
  148. public function getWatcher(string $path = '', ?IStorage $storage = null): IWatcher {
  149. if (!$storage) {
  150. $storage = $this;
  151. }
  152. return $this->getWrapperStorage()->getWatcher($path, $storage);
  153. }
  154. public function getPropagator(?IStorage $storage = null): IPropagator {
  155. if (!$storage) {
  156. $storage = $this;
  157. }
  158. return $this->getWrapperStorage()->getPropagator($storage);
  159. }
  160. public function getUpdater(?IStorage $storage = null): IUpdater {
  161. if (!$storage) {
  162. $storage = $this;
  163. }
  164. return $this->getWrapperStorage()->getUpdater($storage);
  165. }
  166. public function getStorageCache(): \OC\Files\Cache\Storage {
  167. return $this->getWrapperStorage()->getStorageCache();
  168. }
  169. public function getETag(string $path): string|false {
  170. return $this->getWrapperStorage()->getETag($path);
  171. }
  172. public function test(): bool {
  173. return $this->getWrapperStorage()->test();
  174. }
  175. public function isLocal(): bool {
  176. return $this->getWrapperStorage()->isLocal();
  177. }
  178. public function instanceOfStorage(string $class): bool {
  179. if (ltrim($class, '\\') === 'OC\Files\Storage\Shared') {
  180. // FIXME Temporary fix to keep existing checks working
  181. $class = '\OCA\Files_Sharing\SharedStorage';
  182. }
  183. return is_a($this, $class) or $this->getWrapperStorage()->instanceOfStorage($class);
  184. }
  185. /**
  186. * @psalm-template T of IStorage
  187. * @psalm-param class-string<T> $class
  188. * @psalm-return T|null
  189. */
  190. public function getInstanceOfStorage(string $class): ?IStorage {
  191. $storage = $this;
  192. while ($storage instanceof Wrapper) {
  193. if ($storage instanceof $class) {
  194. break;
  195. }
  196. $storage = $storage->getWrapperStorage();
  197. }
  198. if (!($storage instanceof $class)) {
  199. return null;
  200. }
  201. return $storage;
  202. }
  203. /**
  204. * Pass any methods custom to specific storage implementations to the wrapped storage
  205. *
  206. * @return mixed
  207. */
  208. public function __call(string $method, array $args) {
  209. return call_user_func_array([$this->getWrapperStorage(), $method], $args);
  210. }
  211. public function getDirectDownload(string $path): array|false {
  212. return $this->getWrapperStorage()->getDirectDownload($path);
  213. }
  214. public function getAvailability(): array {
  215. return $this->getWrapperStorage()->getAvailability();
  216. }
  217. public function setAvailability(bool $isAvailable): void {
  218. $this->getWrapperStorage()->setAvailability($isAvailable);
  219. }
  220. public function verifyPath(string $path, string $fileName): void {
  221. $this->getWrapperStorage()->verifyPath($path, $fileName);
  222. }
  223. public function copyFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath): bool {
  224. if ($sourceStorage === $this) {
  225. return $this->copy($sourceInternalPath, $targetInternalPath);
  226. }
  227. return $this->getWrapperStorage()->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
  228. }
  229. public function moveFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath): bool {
  230. if ($sourceStorage === $this) {
  231. return $this->rename($sourceInternalPath, $targetInternalPath);
  232. }
  233. return $this->getWrapperStorage()->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
  234. }
  235. public function getMetaData(string $path): ?array {
  236. return $this->getWrapperStorage()->getMetaData($path);
  237. }
  238. public function acquireLock(string $path, int $type, ILockingProvider $provider): void {
  239. if ($this->getWrapperStorage()->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) {
  240. $this->getWrapperStorage()->acquireLock($path, $type, $provider);
  241. }
  242. }
  243. public function releaseLock(string $path, int $type, ILockingProvider $provider): void {
  244. if ($this->getWrapperStorage()->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) {
  245. $this->getWrapperStorage()->releaseLock($path, $type, $provider);
  246. }
  247. }
  248. public function changeLock(string $path, int $type, ILockingProvider $provider): void {
  249. if ($this->getWrapperStorage()->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) {
  250. $this->getWrapperStorage()->changeLock($path, $type, $provider);
  251. }
  252. }
  253. public function needsPartFile(): bool {
  254. return $this->getWrapperStorage()->needsPartFile();
  255. }
  256. public function writeStream(string $path, $stream, ?int $size = null): int {
  257. $storage = $this->getWrapperStorage();
  258. if ($storage->instanceOfStorage(IWriteStreamStorage::class)) {
  259. /** @var IWriteStreamStorage $storage */
  260. return $storage->writeStream($path, $stream, $size);
  261. } else {
  262. $target = $this->fopen($path, 'w');
  263. [$count, $result] = \OC_Helper::streamCopy($stream, $target);
  264. fclose($stream);
  265. fclose($target);
  266. return $count;
  267. }
  268. }
  269. public function getDirectoryContent(string $directory): \Traversable {
  270. return $this->getWrapperStorage()->getDirectoryContent($directory);
  271. }
  272. public function isWrapperOf(IStorage $storage): bool {
  273. $wrapped = $this->getWrapperStorage();
  274. if ($wrapped === $storage) {
  275. return true;
  276. }
  277. if ($wrapped instanceof Wrapper) {
  278. return $wrapped->isWrapperOf($storage);
  279. }
  280. return false;
  281. }
  282. public function setOwner(?string $user): void {
  283. $this->getWrapperStorage()->setOwner($user);
  284. }
  285. }