Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 8 additions & 6 deletions apps/dav/lib/Connector/Sabre/Directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,20 +315,22 @@ public function delete() {
}
}

private function getLogger(): LoggerInterface {
return \OC::$server->get(LoggerInterface::class);
}

/**
* Returns available diskspace information
*
* @return array
*/
public function getQuotaInfo() {
/** @var LoggerInterface $logger */
$logger = \OC::$server->get(LoggerInterface::class);
if ($this->quotaInfo) {
return $this->quotaInfo;
}
$relativePath = $this->fileView->getRelativePath($this->info->getPath());
if ($relativePath === null) {
$logger->warning("error while getting quota as the relative path cannot be found");
$this->getLogger()->warning("error while getting quota as the relative path cannot be found");
return [0, 0];
}

Expand All @@ -345,13 +347,13 @@ public function getQuotaInfo() {
];
return $this->quotaInfo;
} catch (\OCP\Files\NotFoundException $e) {
$logger->warning("error while getting quota into", ['exception' => $e]);
$this->getLogger()->warning("error while getting quota into", ['exception' => $e]);
return [0, 0];
} catch (\OCP\Files\StorageNotAvailableException $e) {
$logger->warning("error while getting quota into", ['exception' => $e]);
$this->getLogger()->warning("error while getting quota into", ['exception' => $e]);
return [0, 0];
} catch (NotPermittedException $e) {
$logger->warning("error while getting quota into", ['exception' => $e]);
$this->getLogger()->warning("error while getting quota into", ['exception' => $e]);
return [0, 0];
}
}
Expand Down
63 changes: 30 additions & 33 deletions apps/files_trashbin/lib/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,43 +40,32 @@
use OCP\Files\Storage\IStorage;
use OCP\ILogger;
use OCP\IUserManager;
use Psr\Log\LoggerInterface;

class Storage extends Wrapper {
/** @var IMountPoint */
private $mountPoint;

/** @var IUserManager */
private $userManager;

/** @var ILogger */
private $logger;

/** @var IEventDispatcher */
private $eventDispatcher;

/** @var IRootFolder */
private $rootFolder;

/** @var ITrashManager */
private $trashManager;

private $trashEnabled = true;
private string $mountPoint;
private IUserManager$userManager;
private LoggerInterface $logger;
private IEventDispatcher $eventDispatcher;
private IRootFolder $rootFolder;
private ITrashManager $trashManager;
private bool $trashEnabled = true;

/**
* Storage constructor.
*
* @param array $parameters
* @param ITrashManager $trashManager
* @param ITrashManager|null $trashManager
* @param IUserManager|null $userManager
* @param ILogger|null $logger
* @param LoggerInterface|null $logger
* @param IEventDispatcher|null $eventDispatcher
* @param IRootFolder|null $rootFolder
*/
public function __construct(
$parameters,
ITrashManager $trashManager = null,
IUserManager $userManager = null,
ILogger $logger = null,
LoggerInterface $logger = null,
IEventDispatcher $eventDispatcher = null,
IRootFolder $rootFolder = null
) {
Expand Down Expand Up @@ -209,19 +198,27 @@ private function doDelete($path, $method) {
}

/**
* Setup the storate wrapper callback
* Setup the storage wrapper callback
*/
public static function setupStorage() {
\OC\Files\Filesystem::addStorageWrapper('oc_trashbin', function ($mountPoint, $storage) {
return new \OCA\Files_Trashbin\Storage(
['storage' => $storage, 'mountPoint' => $mountPoint],
\OC::$server->query(ITrashManager::class),
\OC::$server->getUserManager(),
\OC::$server->getLogger(),
\OC::$server->get(IEventDispatcher::class),
\OC::$server->getLazyRootFolder()
);
}, 1);
$trashManager = \OC::$server->get(ITrashManager::class);
$userManager = \OC::$server->get(IUserManager::class);
$logger = \OC::$server->get(LoggerInterface::class);
$eventDispatcher = \OC::$server->get(IEventDispatcher::class);
$rootFolder = \OC::$server->get(IRootFolder::class);
Filesystem::addStorageWrapper(
'oc_trashbin',
function (string $mountPoint, IStorage $storage) use ($trashManager, $userManager, $logger, $eventDispatcher, $rootFolder) {
return new Storage(
['storage' => $storage, 'mountPoint' => $mountPoint],
$trashManager,
$userManager,
$logger,
$eventDispatcher,
$rootFolder,
);
},
1);
}

public function getMountPoint() {
Expand Down
3 changes: 2 additions & 1 deletion apps/files_trashbin/tests/StorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
use OCP\IUserManager;
use OCP\Lock\ILockingProvider;
use OCP\Share\IShare;
use Psr\Log\LoggerInterface;
use Test\Traits\MountProviderTrait;

class TemporaryNoCross extends Temporary {
Expand Down Expand Up @@ -606,7 +607,7 @@ public function testShouldMoveToTrash($mountPoint, $path, $userExists, $appDisab
->disableOriginalConstructor()->getMock();
$userManager->expects($this->any())
->method('userExists')->willReturn($userExists);
$logger = $this->getMockBuilder(ILogger::class)->getMock();
$logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
$eventDispatcher = $this->createMock(IEventDispatcher::class);
$rootFolder = $this->createMock(IRootFolder::class);
$userFolder = $this->createMock(Folder::class);
Expand Down
14 changes: 11 additions & 3 deletions lib/private/Files/Cache/Wrapper/CacheWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@
use OC\Files\Cache\QuerySearchHelper;
use OCP\Files\Cache\ICache;
use OCP\Files\Cache\ICacheEntry;
use OCP\Files\IMimeTypeLoader;
use OCP\Files\Search\ISearchOperator;
use OCP\Files\Search\ISearchQuery;
use OCP\IDBConnection;

class CacheWrapper extends Cache {
/**
Expand All @@ -47,9 +49,15 @@ class CacheWrapper extends Cache {
*/
public function __construct($cache) {
$this->cache = $cache;
$this->mimetypeLoader = \OC::$server->getMimeTypeLoader();
$this->connection = \OC::$server->getDatabaseConnection();
$this->querySearchHelper = \OC::$server->get(QuerySearchHelper::class);
if ($cache instanceof Cache) {
$this->mimetypeLoader = $cache->mimetypeLoader;
$this->connection = $cache->connection;
$this->querySearchHelper = $cache->querySearchHelper;
} else {
$this->mimetypeLoader = \OC::$server->get(IMimeTypeLoader::class);
$this->connection = \OC::$server->get(IDBConnection::class);
$this->querySearchHelper = \OC::$server->get(QuerySearchHelper::class);
}
}

protected function getCache() {
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 @@ -164,7 +164,8 @@ private function setupBuiltinWrappers() {
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 @@ -176,7 +177,7 @@ private function setupBuiltinWrappers() {
$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
19 changes: 12 additions & 7 deletions lib/private/legacy/OC_Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@
*/
class OC_Helper {
private static $templateManager;
private static ?ICacheFactory $cacheFactory = null;
private static ?bool $quotaIncludeExternalStorage = null;

/**
* Make a human file size
Expand Down Expand Up @@ -475,12 +477,15 @@ public static function findBinaryPath(string $program): ?string {
* @throws \OCP\Files\NotFoundException
*/
public static function getStorageInfo($path, $rootInfo = null, $includeMountPoints = true, $useCache = true) {
/** @var ICacheFactory $cacheFactory */
$cacheFactory = \OC::$server->get(ICacheFactory::class);
$memcache = $cacheFactory->createLocal('storage_info');
if (!self::$cacheFactory) {
self::$cacheFactory = \OC::$server->get(ICacheFactory::class);
}
$memcache = self::$cacheFactory->createLocal('storage_info');

// return storage info without adding mount points
$includeExtStorage = \OC::$server->getSystemConfig()->getValue('quota_include_external_storage', false);
if (self::$quotaIncludeExternalStorage === null) {
self::$quotaIncludeExternalStorage = \OC::$server->getSystemConfig()->getValue('quota_include_external_storage', false);
}

$view = Filesystem::getView();
if (!$view) {
Expand All @@ -497,7 +502,7 @@ public static function getStorageInfo($path, $rootInfo = null, $includeMountPoin
}

if (!$rootInfo) {
$rootInfo = \OC\Files\Filesystem::getFileInfo($path, $includeExtStorage ? 'ext' : false);
$rootInfo = \OC\Files\Filesystem::getFileInfo($path, self::$quotaIncludeExternalStorage ? 'ext' : false);
}
if (!$rootInfo instanceof \OCP\Files\FileInfo) {
throw new \OCP\Files\NotFoundException('The root directory of the user\'s files is missing');
Expand All @@ -512,9 +517,9 @@ public static function getStorageInfo($path, $rootInfo = null, $includeMountPoin
$storage = $mount->getStorage();
$sourceStorage = $storage;
if ($storage->instanceOfStorage('\OCA\Files_Sharing\SharedStorage')) {
$includeExtStorage = false;
self::$quotaIncludeExternalStorage = false;
}
if ($includeExtStorage) {
if (self::$quotaIncludeExternalStorage) {
if ($storage->instanceOfStorage('\OC\Files\Storage\Home')
|| $storage->instanceOfStorage('\OC\Files\ObjectStore\HomeObjectStoreStorage')
) {
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 @@ -22,6 +22,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 @@ -1590,7 +1591,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