Quota.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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($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. /**
  32. * @return int|float quota value
  33. */
  34. public function getQuota(): int|float {
  35. if ($this->quota === null) {
  36. $quotaCallback = $this->quotaCallback;
  37. if ($quotaCallback === null) {
  38. throw new \Exception("No quota or quota callback provider");
  39. }
  40. $this->quota = $quotaCallback();
  41. }
  42. return $this->quota;
  43. }
  44. private function hasQuota(): bool {
  45. return $this->getQuota() !== FileInfo::SPACE_UNLIMITED;
  46. }
  47. /**
  48. * @param string $path
  49. * @param IStorage $storage
  50. * @return int|float
  51. */
  52. protected function getSize($path, $storage = null) {
  53. if ($this->quotaIncludeExternalStorage) {
  54. $rootInfo = Filesystem::getFileInfo('', 'ext');
  55. if ($rootInfo) {
  56. return $rootInfo->getSize(true);
  57. }
  58. return FileInfo::SPACE_NOT_COMPUTED;
  59. } else {
  60. $cache = is_null($storage) ? $this->getCache() : $storage->getCache();
  61. $data = $cache->get($path);
  62. if ($data instanceof ICacheEntry && isset($data['size'])) {
  63. return $data['size'];
  64. } else {
  65. return FileInfo::SPACE_NOT_COMPUTED;
  66. }
  67. }
  68. }
  69. /**
  70. * Get free space as limited by the quota
  71. *
  72. * @param string $path
  73. * @return int|float|bool
  74. */
  75. public function free_space($path) {
  76. if (!$this->hasQuota()) {
  77. return $this->storage->free_space($path);
  78. }
  79. if ($this->getQuota() < 0 || str_starts_with($path, 'cache') || str_starts_with($path, 'uploads')) {
  80. return $this->storage->free_space($path);
  81. } else {
  82. $used = $this->getSize($this->sizeRoot);
  83. if ($used < 0) {
  84. return FileInfo::SPACE_NOT_COMPUTED;
  85. } else {
  86. $free = $this->storage->free_space($path);
  87. $quotaFree = max($this->getQuota() - $used, 0);
  88. // if free space is known
  89. $free = $free >= 0 ? min($free, $quotaFree) : $quotaFree;
  90. return $free;
  91. }
  92. }
  93. }
  94. /**
  95. * see https://www.php.net/manual/en/function.file_put_contents.php
  96. *
  97. * @param string $path
  98. * @param mixed $data
  99. * @return int|float|false
  100. */
  101. public function file_put_contents($path, $data) {
  102. if (!$this->hasQuota()) {
  103. return $this->storage->file_put_contents($path, $data);
  104. }
  105. $free = $this->free_space($path);
  106. if ($free < 0 || strlen($data) < $free) {
  107. return $this->storage->file_put_contents($path, $data);
  108. } else {
  109. return false;
  110. }
  111. }
  112. /**
  113. * see https://www.php.net/manual/en/function.copy.php
  114. *
  115. * @param string $source
  116. * @param string $target
  117. * @return bool
  118. */
  119. public function copy($source, $target) {
  120. if (!$this->hasQuota()) {
  121. return $this->storage->copy($source, $target);
  122. }
  123. $free = $this->free_space($target);
  124. if ($free < 0 || $this->getSize($source) < $free) {
  125. return $this->storage->copy($source, $target);
  126. } else {
  127. return false;
  128. }
  129. }
  130. /**
  131. * see https://www.php.net/manual/en/function.fopen.php
  132. *
  133. * @param string $path
  134. * @param string $mode
  135. * @return resource|bool
  136. */
  137. public function fopen($path, $mode) {
  138. if (!$this->hasQuota()) {
  139. return $this->storage->fopen($path, $mode);
  140. }
  141. $source = $this->storage->fopen($path, $mode);
  142. // don't apply quota for part files
  143. if (!$this->isPartFile($path)) {
  144. $free = $this->free_space($path);
  145. if ($source && (is_int($free) || is_float($free)) && $free >= 0 && $mode !== 'r' && $mode !== 'rb') {
  146. // only apply quota for files, not metadata, trash or others
  147. if ($this->shouldApplyQuota($path)) {
  148. return \OC\Files\Stream\Quota::wrap($source, $free);
  149. }
  150. }
  151. }
  152. return $source;
  153. }
  154. /**
  155. * Checks whether the given path is a part file
  156. *
  157. * @param string $path Path that may identify a .part file
  158. * @return bool
  159. * @note this is needed for reusing keys
  160. */
  161. private function isPartFile($path) {
  162. $extension = pathinfo($path, PATHINFO_EXTENSION);
  163. return ($extension === 'part');
  164. }
  165. /**
  166. * Only apply quota for files, not metadata, trash or others
  167. */
  168. private function shouldApplyQuota(string $path): bool {
  169. return str_starts_with(ltrim($path, '/'), 'files/');
  170. }
  171. /**
  172. * @param IStorage $sourceStorage
  173. * @param string $sourceInternalPath
  174. * @param string $targetInternalPath
  175. * @return bool
  176. */
  177. public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
  178. if (!$this->hasQuota()) {
  179. return $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
  180. }
  181. $free = $this->free_space($targetInternalPath);
  182. if ($free < 0 || $this->getSize($sourceInternalPath, $sourceStorage) < $free) {
  183. return $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
  184. } else {
  185. return false;
  186. }
  187. }
  188. /**
  189. * @param IStorage $sourceStorage
  190. * @param string $sourceInternalPath
  191. * @param string $targetInternalPath
  192. * @return bool
  193. */
  194. public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
  195. if (!$this->hasQuota()) {
  196. return $this->storage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
  197. }
  198. $free = $this->free_space($targetInternalPath);
  199. if ($free < 0 || $this->getSize($sourceInternalPath, $sourceStorage) < $free) {
  200. return $this->storage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
  201. } else {
  202. return false;
  203. }
  204. }
  205. public function mkdir($path) {
  206. if (!$this->hasQuota()) {
  207. return $this->storage->mkdir($path);
  208. }
  209. $free = $this->free_space($path);
  210. if ($this->shouldApplyQuota($path) && $free == 0) {
  211. return false;
  212. }
  213. return parent::mkdir($path);
  214. }
  215. public function touch($path, $mtime = null) {
  216. if (!$this->hasQuota()) {
  217. return $this->storage->touch($path, $mtime);
  218. }
  219. $free = $this->free_space($path);
  220. if ($free == 0) {
  221. return false;
  222. }
  223. return parent::touch($path, $mtime);
  224. }
  225. }