Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
only determine quota_include_external_storage once for quota wrapper
Signed-off-by: Robin Appelman <[email protected]>
  • Loading branch information
icewind1991 committed Sep 5, 2023
commit c255945294f889cf5f40bfa93a1432274f8e0ffe
3 changes: 3 additions & 0 deletions apps/dav/lib/Connector/Sabre/Directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,9 @@ private function getLogger(): LoggerInterface {
* @return array
*/
public function getQuotaInfo() {
if ($this->quotaInfo) {
return $this->quotaInfo;
}
$relativePath = $this->fileView->getRelativePath($this->info->getPath());
if ($relativePath === null) {
$this->getLogger()->warning("error while getting quota as the relative path cannot be found");
Expand Down
5 changes: 3 additions & 2 deletions lib/private/Files/SetupManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ function ($mountPoint, IStorage $storage, IMountPoint $mount) use ($reSharingEna
return $storage;
});

Filesystem::addStorageWrapper('oc_quota', function ($mountPoint, $storage) {
$quotaIncludeExternal = $this->config->getSystemValue('quota_include_external_storage', false);
Filesystem::addStorageWrapper('oc_quota', function ($mountPoint, $storage) use ($quotaIncludeExternal) {
// set up quota for home storages, even for other users
// which can happen when using sharing

Expand All @@ -168,7 +169,7 @@ function ($mountPoint, IStorage $storage, IMountPoint $mount) use ($reSharingEna
$user = $storage->getUser();
return new Quota(['storage' => $storage, 'quotaCallback' => function () use ($user) {
return OC_Util::getUserQuota($user);
}, 'root' => 'files']);
}, 'root' => 'files', 'include_external_storage' => $quotaIncludeExternal]);
}
}

Expand Down
5 changes: 3 additions & 2 deletions lib/private/Files/Storage/Wrapper/Quota.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Quota extends Wrapper {
protected int|float|null $quota;
protected string $sizeRoot;
private SystemConfig $config;
private bool $quotaIncludeExternalStorage;

/**
* @param array $parameters
Expand All @@ -54,7 +55,7 @@ public function __construct($parameters) {
$this->quota = $parameters['quota'] ?? null;
$this->quotaCallback = $parameters['quotaCallback'] ?? null;
$this->sizeRoot = $parameters['root'] ?? '';
$this->config = \OC::$server->get(SystemConfig::class);
$this->quotaIncludeExternalStorage = $parameters['include_external_storage'] ?? false;
}

/**
Expand Down Expand Up @@ -82,7 +83,7 @@ private function hasQuota(): bool {
* @return int|float
*/
protected function getSize($path, $storage = null) {
if ($this->config->getValue('quota_include_external_storage', false)) {
if ($this->quotaIncludeExternalStorage) {
$rootInfo = Filesystem::getFileInfo('', 'ext');
if ($rootInfo) {
return $rootInfo->getSize(true);
Expand Down
3 changes: 2 additions & 1 deletion tests/lib/Files/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use OCP\Files\GenericFileException;
use OCP\Files\Mount\IMountManager;
use OCP\Files\Storage\IStorage;
use OCP\IDBConnection;
use OCP\Lock\ILockingProvider;
use OCP\Lock\LockedException;
use OCP\Share\IShare;
Expand Down Expand Up @@ -1592,7 +1593,7 @@ private function createTestMovableMountPoints($mountPoints) {
->getMock();
$storage->method('getId')->willReturn('non-null-id');
$storage->method('getStorageCache')->willReturnCallback(function () use ($storage) {
return new \OC\Files\Cache\Storage($storage);
return new \OC\Files\Cache\Storage($storage, true, \OC::$server->get(IDBConnection::class));
});

$mounts[] = $this->getMockBuilder(TestMoveableMountPoint::class)
Expand Down
31 changes: 21 additions & 10 deletions tests/lib/HelperStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use OC\Files\Storage\Temporary;
use OCP\Files\Mount\IMountManager;
use OCP\IConfig;
use Test\Traits\UserTrait;

/**
Expand All @@ -26,12 +27,14 @@ class HelperStorageTest extends \Test\TestCase {
private $storageMock;
/** @var \OC\Files\Storage\Storage */
private $storage;
private bool $savedQuotaIncludeExternalStorage;

protected function setUp(): void {
parent::setUp();

$this->user = $this->getUniqueID('user_');
$this->createUser($this->user, $this->user);
$this->savedQuotaIncludeExternalStorage = $this->getIncludeExternalStorage();

\OC\Files\Filesystem::tearDown();
\OC_User::setUserId($this->user);
Expand All @@ -45,6 +48,7 @@ protected function setUp(): void {
}

protected function tearDown(): void {
$this->setIncludeExternalStorage($this->savedQuotaIncludeExternalStorage);
$this->user = null;

if ($this->storageMock) {
Expand Down Expand Up @@ -91,6 +95,19 @@ public function testGetStorageInfo() {
$this->assertEquals(5, $storageInfo['used']);
$this->assertEquals(17, $storageInfo['total']);
}
private function getIncludeExternalStorage(): bool {
$class = new \ReflectionClass(\OC_Helper::class);
$prop = $class->getProperty('quotaIncludeExternalStorage');
$prop->setAccessible(true);
return $prop->getValue(null) ?? false;
}

private function setIncludeExternalStorage(bool $include) {
$class = new \ReflectionClass(\OC_Helper::class);
$prop = $class->getProperty('quotaIncludeExternalStorage');
$prop->setAccessible(true);
$prop->setValue(null, $include);
}

/**
* Test getting the storage info, ignoring extra mount points
Expand All @@ -104,8 +121,7 @@ public function testGetStorageInfoExcludingExtStorage() {
$extStorage->file_put_contents('extfile.txt', 'abcdefghijklmnopq');
$extStorage->getScanner()->scan(''); // update root size

$config = \OC::$server->getConfig();
$config->setSystemValue('quota_include_external_storage', false);
$this->setIncludeExternalStorage(false);

\OC\Files\Filesystem::mount($extStorage, [], '/' . $this->user . '/files/ext');

Expand All @@ -129,18 +145,16 @@ public function testGetStorageInfoIncludingExtStorage() {

\OC\Files\Filesystem::mount($extStorage, [], '/' . $this->user . '/files/ext');

$config = \OC::$server->getConfig();
$oldConfig = $config->getSystemValue('quota_include_external_storage', false);
$config->setSystemValue('quota_include_external_storage', 'true');
$this->setIncludeExternalStorage(true);

$config = \OC::$server->get(IConfig::class);
$config->setUserValue($this->user, 'files', 'quota', '25');

$storageInfo = \OC_Helper::getStorageInfo('');
$this->assertEquals(3, $storageInfo['free']);
$this->assertEquals(22, $storageInfo['used']);
$this->assertEquals(25, $storageInfo['total']);

$config->setSystemValue('quota_include_external_storage', $oldConfig);
$config->setUserValue($this->user, 'files', 'quota', 'default');
}

Expand All @@ -161,15 +175,12 @@ public function testGetStorageInfoIncludingExtStorageWithNoUserQuota() {
\OC\Files\Filesystem::mount($extStorage, [], '/' . $this->user . '/files/ext');

$config = \OC::$server->getConfig();
$oldConfig = $config->getSystemValue('quota_include_external_storage', false);
$config->setSystemValue('quota_include_external_storage', 'true');
$this->setIncludeExternalStorage(true);

$storageInfo = \OC_Helper::getStorageInfo('');
$this->assertEquals(12, $storageInfo['free'], '12 bytes free in home storage');
$this->assertEquals(22, $storageInfo['used'], '5 bytes of home storage and 17 bytes of the temporary storage are used');
$this->assertEquals(34, $storageInfo['total'], '5 bytes used and 12 bytes free in home storage as well as 17 bytes used in temporary storage');

$config->setSystemValue('quota_include_external_storage', $oldConfig);
}


Expand Down