Quota.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author J0WI <J0WI@users.noreply.github.com>
  7. * @author John Molakvoæ <skjnldsv@protonmail.com>
  8. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  9. * @author Julius Härtl <jus@bitgrid.net>
  10. * @author Lukas Reschke <lukas@statuscode.ch>
  11. * @author Morris Jobke <hey@morrisjobke.de>
  12. * @author Robin Appelman <robin@icewind.nl>
  13. * @author Robin McCorkell <robin@mccorkell.me.uk>
  14. * @author Roeland Jago Douma <roeland@famdouma.nl>
  15. * @author Tigran Mkrtchyan <tigran.mkrtchyan@desy.de>
  16. * @author Vincent Petry <vincent@nextcloud.com>
  17. *
  18. * @license AGPL-3.0
  19. *
  20. * This code is free software: you can redistribute it and/or modify
  21. * it under the terms of the GNU Affero General Public License, version 3,
  22. * as published by the Free Software Foundation.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU Affero General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Affero General Public License, version 3,
  30. * along with this program. If not, see <http://www.gnu.org/licenses/>
  31. *
  32. */
  33. namespace OC\Files\Storage\Wrapper;
  34. use OC\Files\Filesystem;
  35. use OC\SystemConfig;
  36. use OCP\Files\Cache\ICacheEntry;
  37. use OCP\Files\FileInfo;
  38. use OCP\Files\Storage\IStorage;
  39. class Quota extends Wrapper {
  40. /** @var callable|null */
  41. protected $quotaCallback;
  42. /** @var int|float|null int on 64bits, float on 32bits for bigint */
  43. protected int|float|null $quota;
  44. protected string $sizeRoot;
  45. private SystemConfig $config;
  46. /**
  47. * @param array $parameters
  48. */
  49. public function __construct($parameters) {
  50. parent::__construct($parameters);
  51. $this->quota = $parameters['quota'] ?? null;
  52. $this->quotaCallback = $parameters['quotaCallback'] ?? null;
  53. $this->sizeRoot = $parameters['root'] ?? '';
  54. $this->config = \OC::$server->get(SystemConfig::class);
  55. }
  56. /**
  57. * @return int|float quota value
  58. */
  59. public function getQuota(): int|float {
  60. if ($this->quota === null) {
  61. $quotaCallback = $this->quotaCallback;
  62. if ($quotaCallback === null) {
  63. throw new \Exception("No quota or quota callback provider");
  64. }
  65. $this->quota = $quotaCallback();
  66. }
  67. return $this->quota;
  68. }
  69. private function hasQuota(): bool {
  70. return $this->getQuota() !== FileInfo::SPACE_UNLIMITED;
  71. }
  72. /**
  73. * @param string $path
  74. * @param IStorage $storage
  75. * @return int|float
  76. */
  77. protected function getSize($path, $storage = null) {
  78. if ($this->config->getValue('quota_include_external_storage', false)) {
  79. $rootInfo = Filesystem::getFileInfo('', 'ext');
  80. if ($rootInfo) {
  81. return $rootInfo->getSize(true);
  82. }
  83. return FileInfo::SPACE_NOT_COMPUTED;
  84. } else {
  85. $cache = is_null($storage) ? $this->getCache() : $storage->getCache();
  86. $data = $cache->get($path);
  87. if ($data instanceof ICacheEntry && isset($data['size'])) {
  88. return $data['size'];
  89. } else {
  90. return FileInfo::SPACE_NOT_COMPUTED;
  91. }
  92. }
  93. }
  94. /**
  95. * Get free space as limited by the quota
  96. *
  97. * @param string $path
  98. * @return int|float|bool
  99. */
  100. public function free_space($path) {
  101. if (!$this->hasQuota()) {
  102. return $this->storage->free_space($path);
  103. }
  104. if ($this->getQuota() < 0 || str_starts_with($path, 'cache') || str_starts_with($path, 'uploads')) {
  105. return $this->storage->free_space($path);
  106. } else {
  107. $used = $this->getSize($this->sizeRoot);
  108. if ($used < 0) {
  109. return FileInfo::SPACE_NOT_COMPUTED;
  110. } else {
  111. $free = $this->storage->free_space($path);
  112. $quotaFree = max($this->getQuota() - $used, 0);
  113. // if free space is known
  114. $free = $free >= 0 ? min($free, $quotaFree) : $quotaFree;
  115. return $free;
  116. }
  117. }
  118. }
  119. /**
  120. * see https://www.php.net/manual/en/function.file_put_contents.php
  121. *
  122. * @param string $path
  123. * @param mixed $data
  124. * @return int|float|false
  125. */
  126. public function file_put_contents($path, $data) {
  127. if (!$this->hasQuota()) {
  128. return $this->storage->file_put_contents($path, $data);
  129. }
  130. $free = $this->free_space($path);
  131. if ($free < 0 || strlen($data) < $free) {
  132. return $this->storage->file_put_contents($path, $data);
  133. } else {
  134. return false;
  135. }
  136. }
  137. /**
  138. * see https://www.php.net/manual/en/function.copy.php
  139. *
  140. * @param string $source
  141. * @param string $target
  142. * @return bool
  143. */
  144. public function copy($source, $target) {
  145. if (!$this->hasQuota()) {
  146. return $this->storage->copy($source, $target);
  147. }
  148. $free = $this->free_space($target);
  149. if ($free < 0 || $this->getSize($source) < $free) {
  150. return $this->storage->copy($source, $target);
  151. } else {
  152. return false;
  153. }
  154. }
  155. /**
  156. * see https://www.php.net/manual/en/function.fopen.php
  157. *
  158. * @param string $path
  159. * @param string $mode
  160. * @return resource|bool
  161. */
  162. public function fopen($path, $mode) {
  163. if (!$this->hasQuota()) {
  164. return $this->storage->fopen($path, $mode);
  165. }
  166. $source = $this->storage->fopen($path, $mode);
  167. // don't apply quota for part files
  168. if (!$this->isPartFile($path)) {
  169. $free = $this->free_space($path);
  170. if ($source && (is_int($free) || is_float($free)) && $free >= 0 && $mode !== 'r' && $mode !== 'rb') {
  171. // only apply quota for files, not metadata, trash or others
  172. if ($this->shouldApplyQuota($path)) {
  173. return \OC\Files\Stream\Quota::wrap($source, $free);
  174. }
  175. }
  176. }
  177. return $source;
  178. }
  179. /**
  180. * Checks whether the given path is a part file
  181. *
  182. * @param string $path Path that may identify a .part file
  183. * @return bool
  184. * @note this is needed for reusing keys
  185. */
  186. private function isPartFile($path) {
  187. $extension = pathinfo($path, PATHINFO_EXTENSION);
  188. return ($extension === 'part');
  189. }
  190. /**
  191. * Only apply quota for files, not metadata, trash or others
  192. */
  193. private function shouldApplyQuota(string $path): bool {
  194. return str_starts_with(ltrim($path, '/'), 'files/');
  195. }
  196. /**
  197. * @param IStorage $sourceStorage
  198. * @param string $sourceInternalPath
  199. * @param string $targetInternalPath
  200. * @return bool
  201. */
  202. public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
  203. if (!$this->hasQuota()) {
  204. return $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
  205. }
  206. $free = $this->free_space($targetInternalPath);
  207. if ($free < 0 || $this->getSize($sourceInternalPath, $sourceStorage) < $free) {
  208. return $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
  209. } else {
  210. return false;
  211. }
  212. }
  213. /**
  214. * @param IStorage $sourceStorage
  215. * @param string $sourceInternalPath
  216. * @param string $targetInternalPath
  217. * @return bool
  218. */
  219. public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
  220. if (!$this->hasQuota()) {
  221. return $this->storage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
  222. }
  223. $free = $this->free_space($targetInternalPath);
  224. if ($free < 0 || $this->getSize($sourceInternalPath, $sourceStorage) < $free) {
  225. return $this->storage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
  226. } else {
  227. return false;
  228. }
  229. }
  230. public function mkdir($path) {
  231. if (!$this->hasQuota()) {
  232. return $this->storage->mkdir($path);
  233. }
  234. $free = $this->free_space($path);
  235. if ($this->shouldApplyQuota($path) && $free == 0) {
  236. return false;
  237. }
  238. return parent::mkdir($path);
  239. }
  240. public function touch($path, $mtime = null) {
  241. if (!$this->hasQuota()) {
  242. return $this->storage->touch($path, $mtime);
  243. }
  244. $free = $this->free_space($path);
  245. if ($free == 0) {
  246. return false;
  247. }
  248. return parent::touch($path, $mtime);
  249. }
  250. }