Quota.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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\Filesystem;
  9. use OC\SystemConfig;
  10. use OCP\Files\Cache\ICacheEntry;
  11. use OCP\Files\FileInfo;
  12. use OCP\Files\Storage\IStorage;
  13. class Quota extends Wrapper {
  14. /** @var callable|null */
  15. protected $quotaCallback;
  16. /** @var int|float|null int on 64bits, float on 32bits for bigint */
  17. protected int|float|null $quota;
  18. protected string $sizeRoot;
  19. private SystemConfig $config;
  20. private bool $quotaIncludeExternalStorage;
  21. /**
  22. * @param array $parameters
  23. */
  24. public function __construct(array $parameters) {
  25. parent::__construct($parameters);
  26. $this->quota = $parameters['quota'] ?? null;
  27. $this->quotaCallback = $parameters['quotaCallback'] ?? null;
  28. $this->sizeRoot = $parameters['root'] ?? '';
  29. $this->quotaIncludeExternalStorage = $parameters['include_external_storage'] ?? false;
  30. }
  31. public function getQuota(): int|float {
  32. if ($this->quota === null) {
  33. $quotaCallback = $this->quotaCallback;
  34. if ($quotaCallback === null) {
  35. throw new \Exception('No quota or quota callback provider');
  36. }
  37. $this->quota = $quotaCallback();
  38. }
  39. return $this->quota;
  40. }
  41. private function hasQuota(): bool {
  42. return $this->getQuota() !== FileInfo::SPACE_UNLIMITED;
  43. }
  44. protected function getSize(string $path, ?IStorage $storage = null): int|float {
  45. if ($this->quotaIncludeExternalStorage) {
  46. $rootInfo = Filesystem::getFileInfo('', 'ext');
  47. if ($rootInfo) {
  48. return $rootInfo->getSize(true);
  49. }
  50. return FileInfo::SPACE_NOT_COMPUTED;
  51. } else {
  52. $cache = is_null($storage) ? $this->getCache() : $storage->getCache();
  53. $data = $cache->get($path);
  54. if ($data instanceof ICacheEntry && isset($data['size'])) {
  55. return $data['size'];
  56. } else {
  57. return FileInfo::SPACE_NOT_COMPUTED;
  58. }
  59. }
  60. }
  61. public function free_space(string $path): int|float|false {
  62. if (!$this->hasQuota()) {
  63. return $this->storage->free_space($path);
  64. }
  65. if ($this->getQuota() < 0 || str_starts_with($path, 'cache') || str_starts_with($path, 'uploads')) {
  66. return $this->storage->free_space($path);
  67. } else {
  68. $used = $this->getSize($this->sizeRoot);
  69. if ($used < 0) {
  70. return FileInfo::SPACE_NOT_COMPUTED;
  71. } else {
  72. $free = $this->storage->free_space($path);
  73. $quotaFree = max($this->getQuota() - $used, 0);
  74. // if free space is known
  75. $free = $free >= 0 ? min($free, $quotaFree) : $quotaFree;
  76. return $free;
  77. }
  78. }
  79. }
  80. public function file_put_contents(string $path, mixed $data): int|float|false {
  81. if (!$this->hasQuota()) {
  82. return $this->storage->file_put_contents($path, $data);
  83. }
  84. $free = $this->free_space($path);
  85. if ($free < 0 || strlen($data) < $free) {
  86. return $this->storage->file_put_contents($path, $data);
  87. } else {
  88. return false;
  89. }
  90. }
  91. public function copy(string $source, string $target): bool {
  92. if (!$this->hasQuota()) {
  93. return $this->storage->copy($source, $target);
  94. }
  95. $free = $this->free_space($target);
  96. if ($free < 0 || $this->getSize($source) < $free) {
  97. return $this->storage->copy($source, $target);
  98. } else {
  99. return false;
  100. }
  101. }
  102. public function fopen(string $path, string $mode) {
  103. if (!$this->hasQuota()) {
  104. return $this->storage->fopen($path, $mode);
  105. }
  106. $source = $this->storage->fopen($path, $mode);
  107. // don't apply quota for part files
  108. if (!$this->isPartFile($path)) {
  109. $free = $this->free_space($path);
  110. if ($source && (is_int($free) || is_float($free)) && $free >= 0 && $mode !== 'r' && $mode !== 'rb') {
  111. // only apply quota for files, not metadata, trash or others
  112. if ($this->shouldApplyQuota($path)) {
  113. return \OC\Files\Stream\Quota::wrap($source, $free);
  114. }
  115. }
  116. }
  117. return $source;
  118. }
  119. /**
  120. * Checks whether the given path is a part file
  121. *
  122. * @param string $path Path that may identify a .part file
  123. * @note this is needed for reusing keys
  124. */
  125. private function isPartFile(string $path): bool {
  126. $extension = pathinfo($path, PATHINFO_EXTENSION);
  127. return ($extension === 'part');
  128. }
  129. /**
  130. * Only apply quota for files, not metadata, trash or others
  131. */
  132. protected function shouldApplyQuota(string $path): bool {
  133. return str_starts_with(ltrim($path, '/'), 'files/');
  134. }
  135. public function copyFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath): bool {
  136. if (!$this->hasQuota()) {
  137. return $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
  138. }
  139. $free = $this->free_space($targetInternalPath);
  140. if ($free < 0 || $this->getSize($sourceInternalPath, $sourceStorage) < $free) {
  141. return $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
  142. } else {
  143. return false;
  144. }
  145. }
  146. public function moveFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath): bool {
  147. if (!$this->hasQuota()) {
  148. return $this->storage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
  149. }
  150. $free = $this->free_space($targetInternalPath);
  151. if ($free < 0 || $this->getSize($sourceInternalPath, $sourceStorage) < $free) {
  152. return $this->storage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
  153. } else {
  154. return false;
  155. }
  156. }
  157. public function mkdir(string $path): bool {
  158. if (!$this->hasQuota()) {
  159. return $this->storage->mkdir($path);
  160. }
  161. $free = $this->free_space($path);
  162. if ($this->shouldApplyQuota($path) && $free == 0) {
  163. return false;
  164. }
  165. return parent::mkdir($path);
  166. }
  167. public function touch(string $path, ?int $mtime = null): bool {
  168. if (!$this->hasQuota()) {
  169. return $this->storage->touch($path, $mtime);
  170. }
  171. $free = $this->free_space($path);
  172. if ($free == 0) {
  173. return false;
  174. }
  175. return parent::touch($path, $mtime);
  176. }
  177. }