2013-07-02 12:22:49 -04:00
|
|
|
<?php
|
2024-05-23 03:26:56 -04:00
|
|
|
|
2013-07-02 12:22:49 -04:00
|
|
|
/**
|
2024-05-23 03:26:56 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2013-07-02 12:22:49 -04:00
|
|
|
*/
|
|
|
|
|
namespace OC\Files\Storage\Wrapper;
|
|
|
|
|
|
2020-03-04 12:02:50 -05:00
|
|
|
use OC\Files\Filesystem;
|
2022-08-18 05:43:18 -04:00
|
|
|
use OC\SystemConfig;
|
2015-12-02 09:28:49 -05:00
|
|
|
use OCP\Files\Cache\ICacheEntry;
|
2022-08-18 05:43:18 -04:00
|
|
|
use OCP\Files\FileInfo;
|
2017-07-19 13:44:10 -04:00
|
|
|
use OCP\Files\Storage\IStorage;
|
2015-12-02 09:28:49 -05:00
|
|
|
|
2013-07-02 12:22:49 -04:00
|
|
|
class Quota extends Wrapper {
|
2022-08-18 05:43:18 -04:00
|
|
|
/** @var callable|null */
|
|
|
|
|
protected $quotaCallback;
|
2022-12-19 07:54:23 -05:00
|
|
|
/** @var int|float|null int on 64bits, float on 32bits for bigint */
|
2023-03-30 04:30:12 -04:00
|
|
|
protected int|float|null $quota;
|
2022-08-18 05:43:18 -04:00
|
|
|
protected string $sizeRoot;
|
|
|
|
|
private SystemConfig $config;
|
2023-08-15 12:33:57 -04:00
|
|
|
private bool $quotaIncludeExternalStorage;
|
2020-03-04 12:02:50 -05:00
|
|
|
|
2013-07-02 12:22:49 -04:00
|
|
|
/**
|
|
|
|
|
* @param array $parameters
|
|
|
|
|
*/
|
|
|
|
|
public function __construct($parameters) {
|
2018-05-02 15:46:28 -04:00
|
|
|
parent::__construct($parameters);
|
2022-08-18 05:43:18 -04:00
|
|
|
$this->quota = $parameters['quota'] ?? null;
|
|
|
|
|
$this->quotaCallback = $parameters['quotaCallback'] ?? null;
|
|
|
|
|
$this->sizeRoot = $parameters['root'] ?? '';
|
2023-08-15 12:33:57 -04:00
|
|
|
$this->quotaIncludeExternalStorage = $parameters['include_external_storage'] ?? false;
|
2013-07-02 12:22:49 -04:00
|
|
|
}
|
|
|
|
|
|
2014-03-10 10:19:18 -04:00
|
|
|
/**
|
2022-12-19 07:54:23 -05:00
|
|
|
* @return int|float quota value
|
2014-03-10 10:19:18 -04:00
|
|
|
*/
|
2023-03-30 04:30:12 -04:00
|
|
|
public function getQuota(): int|float {
|
2022-08-18 05:43:18 -04:00
|
|
|
if ($this->quota === null) {
|
|
|
|
|
$quotaCallback = $this->quotaCallback;
|
|
|
|
|
if ($quotaCallback === null) {
|
|
|
|
|
throw new \Exception("No quota or quota callback provider");
|
|
|
|
|
}
|
|
|
|
|
$this->quota = $quotaCallback();
|
|
|
|
|
}
|
2022-09-12 03:35:12 -04:00
|
|
|
|
2014-03-10 10:19:18 -04:00
|
|
|
return $this->quota;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-18 05:43:18 -04:00
|
|
|
private function hasQuota(): bool {
|
|
|
|
|
return $this->getQuota() !== FileInfo::SPACE_UNLIMITED;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-06 10:30:58 -05:00
|
|
|
/**
|
|
|
|
|
* @param string $path
|
2022-12-19 07:54:23 -05:00
|
|
|
* @param IStorage $storage
|
|
|
|
|
* @return int|float
|
2014-02-06 10:30:58 -05:00
|
|
|
*/
|
2015-03-05 05:49:15 -05:00
|
|
|
protected function getSize($path, $storage = null) {
|
2023-08-15 12:33:57 -04:00
|
|
|
if ($this->quotaIncludeExternalStorage) {
|
2020-03-04 12:02:50 -05:00
|
|
|
$rootInfo = Filesystem::getFileInfo('', 'ext');
|
2020-11-07 18:10:06 -05:00
|
|
|
if ($rootInfo) {
|
|
|
|
|
return $rootInfo->getSize(true);
|
|
|
|
|
}
|
2022-09-12 03:35:12 -04:00
|
|
|
return FileInfo::SPACE_NOT_COMPUTED;
|
2013-07-02 12:22:49 -04:00
|
|
|
} else {
|
2022-09-12 03:35:12 -04:00
|
|
|
$cache = is_null($storage) ? $this->getCache() : $storage->getCache();
|
2020-03-04 12:02:50 -05:00
|
|
|
$data = $cache->get($path);
|
2022-09-12 03:35:12 -04:00
|
|
|
if ($data instanceof ICacheEntry && isset($data['size'])) {
|
2020-03-04 12:02:50 -05:00
|
|
|
return $data['size'];
|
|
|
|
|
} else {
|
2022-09-12 03:35:12 -04:00
|
|
|
return FileInfo::SPACE_NOT_COMPUTED;
|
2020-03-04 12:02:50 -05:00
|
|
|
}
|
2013-07-02 12:22:49 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get free space as limited by the quota
|
|
|
|
|
*
|
|
|
|
|
* @param string $path
|
2022-12-19 07:54:23 -05:00
|
|
|
* @return int|float|bool
|
2013-07-02 12:22:49 -04:00
|
|
|
*/
|
|
|
|
|
public function free_space($path) {
|
2022-08-18 05:43:18 -04:00
|
|
|
if (!$this->hasQuota()) {
|
|
|
|
|
return $this->storage->free_space($path);
|
|
|
|
|
}
|
2023-05-15 07:47:19 -04:00
|
|
|
if ($this->getQuota() < 0 || str_starts_with($path, 'cache') || str_starts_with($path, 'uploads')) {
|
2013-07-02 12:22:49 -04:00
|
|
|
return $this->storage->free_space($path);
|
|
|
|
|
} else {
|
2014-01-27 10:26:54 -05:00
|
|
|
$used = $this->getSize($this->sizeRoot);
|
2013-07-02 12:22:49 -04:00
|
|
|
if ($used < 0) {
|
2022-09-12 03:35:12 -04:00
|
|
|
return FileInfo::SPACE_NOT_COMPUTED;
|
2013-07-02 12:22:49 -04:00
|
|
|
} else {
|
|
|
|
|
$free = $this->storage->free_space($path);
|
2022-08-18 05:43:18 -04:00
|
|
|
$quotaFree = max($this->getQuota() - $used, 0);
|
2014-03-19 14:07:11 -04:00
|
|
|
// if free space is known
|
2022-09-12 03:35:12 -04:00
|
|
|
$free = $free >= 0 ? min($free, $quotaFree) : $quotaFree;
|
2014-03-19 14:07:11 -04:00
|
|
|
return $free;
|
2013-07-02 12:22:49 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-09-17 11:23:07 -04:00
|
|
|
* see https://www.php.net/manual/en/function.file_put_contents.php
|
2013-07-02 12:22:49 -04:00
|
|
|
*
|
|
|
|
|
* @param string $path
|
2020-12-21 07:43:51 -05:00
|
|
|
* @param mixed $data
|
2022-12-19 07:54:23 -05:00
|
|
|
* @return int|float|false
|
2013-07-02 12:22:49 -04:00
|
|
|
*/
|
|
|
|
|
public function file_put_contents($path, $data) {
|
2022-08-18 05:43:18 -04:00
|
|
|
if (!$this->hasQuota()) {
|
|
|
|
|
return $this->storage->file_put_contents($path, $data);
|
|
|
|
|
}
|
2018-05-02 15:46:28 -04:00
|
|
|
$free = $this->free_space($path);
|
2022-09-12 03:35:12 -04:00
|
|
|
if ($free < 0 || strlen($data) < $free) {
|
2013-07-02 12:22:49 -04:00
|
|
|
return $this->storage->file_put_contents($path, $data);
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-09-17 11:23:07 -04:00
|
|
|
* see https://www.php.net/manual/en/function.copy.php
|
2013-07-02 12:22:49 -04:00
|
|
|
*
|
|
|
|
|
* @param string $source
|
|
|
|
|
* @param string $target
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function copy($source, $target) {
|
2022-08-18 05:43:18 -04:00
|
|
|
if (!$this->hasQuota()) {
|
|
|
|
|
return $this->storage->copy($source, $target);
|
|
|
|
|
}
|
2018-05-02 15:46:28 -04:00
|
|
|
$free = $this->free_space($target);
|
2022-09-12 03:35:12 -04:00
|
|
|
if ($free < 0 || $this->getSize($source) < $free) {
|
2013-07-02 12:22:49 -04:00
|
|
|
return $this->storage->copy($source, $target);
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-09-17 11:23:07 -04:00
|
|
|
* see https://www.php.net/manual/en/function.fopen.php
|
2013-07-02 12:22:49 -04:00
|
|
|
*
|
|
|
|
|
* @param string $path
|
|
|
|
|
* @param string $mode
|
2020-12-03 05:03:17 -05:00
|
|
|
* @return resource|bool
|
2013-07-02 12:22:49 -04:00
|
|
|
*/
|
|
|
|
|
public function fopen($path, $mode) {
|
2022-08-18 05:43:18 -04:00
|
|
|
if (!$this->hasQuota()) {
|
|
|
|
|
return $this->storage->fopen($path, $mode);
|
|
|
|
|
}
|
2013-07-02 12:22:49 -04:00
|
|
|
$source = $this->storage->fopen($path, $mode);
|
2016-02-16 08:39:04 -05:00
|
|
|
|
|
|
|
|
// don't apply quota for part files
|
|
|
|
|
if (!$this->isPartFile($path)) {
|
2018-05-02 15:46:28 -04:00
|
|
|
$free = $this->free_space($path);
|
2022-12-19 07:54:23 -05:00
|
|
|
if ($source && (is_int($free) || is_float($free)) && $free >= 0 && $mode !== 'r' && $mode !== 'rb') {
|
2016-02-16 08:39:04 -05:00
|
|
|
// only apply quota for files, not metadata, trash or others
|
2020-09-08 01:33:54 -04:00
|
|
|
if ($this->shouldApplyQuota($path)) {
|
2016-02-16 08:39:04 -05:00
|
|
|
return \OC\Files\Stream\Quota::wrap($source, $free);
|
|
|
|
|
}
|
2015-05-28 12:31:20 -04:00
|
|
|
}
|
2013-07-02 12:22:49 -04:00
|
|
|
}
|
2022-09-12 03:35:12 -04:00
|
|
|
|
2015-05-28 12:31:20 -04:00
|
|
|
return $source;
|
2013-07-02 12:22:49 -04:00
|
|
|
}
|
2015-03-05 05:49:15 -05:00
|
|
|
|
|
|
|
|
/**
|
2016-02-16 08:39:04 -05:00
|
|
|
* Checks whether the given path is a part file
|
|
|
|
|
*
|
|
|
|
|
* @param string $path Path that may identify a .part file
|
2022-12-19 07:54:23 -05:00
|
|
|
* @return bool
|
2016-02-16 08:39:04 -05:00
|
|
|
* @note this is needed for reusing keys
|
|
|
|
|
*/
|
|
|
|
|
private function isPartFile($path) {
|
|
|
|
|
$extension = pathinfo($path, PATHINFO_EXTENSION);
|
|
|
|
|
|
|
|
|
|
return ($extension === 'part');
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-08 01:33:54 -04:00
|
|
|
/**
|
|
|
|
|
* Only apply quota for files, not metadata, trash or others
|
|
|
|
|
*/
|
2024-10-09 02:25:30 -04:00
|
|
|
protected function shouldApplyQuota(string $path): bool {
|
2023-05-15 07:47:19 -04:00
|
|
|
return str_starts_with(ltrim($path, '/'), 'files/');
|
2020-09-08 01:33:54 -04:00
|
|
|
}
|
|
|
|
|
|
2016-02-16 08:39:04 -05:00
|
|
|
/**
|
2017-07-19 13:44:10 -04:00
|
|
|
* @param IStorage $sourceStorage
|
2015-03-05 05:49:15 -05:00
|
|
|
* @param string $sourceInternalPath
|
|
|
|
|
* @param string $targetInternalPath
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2017-07-19 13:44:10 -04:00
|
|
|
public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
|
2022-08-18 05:43:18 -04:00
|
|
|
if (!$this->hasQuota()) {
|
|
|
|
|
return $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
|
|
|
|
|
}
|
2018-05-02 15:46:28 -04:00
|
|
|
$free = $this->free_space($targetInternalPath);
|
2022-09-12 03:35:12 -04:00
|
|
|
if ($free < 0 || $this->getSize($sourceInternalPath, $sourceStorage) < $free) {
|
2015-03-05 05:49:15 -05:00
|
|
|
return $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-07-19 13:44:10 -04:00
|
|
|
* @param IStorage $sourceStorage
|
2015-03-05 05:49:15 -05:00
|
|
|
* @param string $sourceInternalPath
|
|
|
|
|
* @param string $targetInternalPath
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2017-07-19 13:44:10 -04:00
|
|
|
public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
|
2022-08-18 05:43:18 -04:00
|
|
|
if (!$this->hasQuota()) {
|
|
|
|
|
return $this->storage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
|
|
|
|
|
}
|
2018-05-02 15:46:28 -04:00
|
|
|
$free = $this->free_space($targetInternalPath);
|
2022-09-12 03:35:12 -04:00
|
|
|
if ($free < 0 || $this->getSize($sourceInternalPath, $sourceStorage) < $free) {
|
2015-03-05 05:49:15 -05:00
|
|
|
return $this->storage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-05-02 16:05:50 -04:00
|
|
|
|
|
|
|
|
public function mkdir($path) {
|
2022-08-18 05:43:18 -04:00
|
|
|
if (!$this->hasQuota()) {
|
|
|
|
|
return $this->storage->mkdir($path);
|
|
|
|
|
}
|
2018-06-07 06:07:37 -04:00
|
|
|
$free = $this->free_space($path);
|
2022-08-18 05:43:18 -04:00
|
|
|
if ($this->shouldApplyQuota($path) && $free == 0) {
|
2018-05-02 16:05:50 -04:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return parent::mkdir($path);
|
|
|
|
|
}
|
2019-05-28 07:05:20 -04:00
|
|
|
|
|
|
|
|
public function touch($path, $mtime = null) {
|
2022-08-18 05:43:18 -04:00
|
|
|
if (!$this->hasQuota()) {
|
|
|
|
|
return $this->storage->touch($path, $mtime);
|
|
|
|
|
}
|
2019-05-28 07:05:20 -04:00
|
|
|
$free = $this->free_space($path);
|
2022-08-18 05:43:18 -04:00
|
|
|
if ($free == 0) {
|
2019-05-28 07:05:20 -04:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return parent::touch($path, $mtime);
|
|
|
|
|
}
|
2013-07-02 12:22:49 -04:00
|
|
|
}
|