Quota.php 7.6 KB

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