You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Quota.php 6.5KB

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