Skip to content
Open
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
5 changes: 3 additions & 2 deletions apps/dav/appinfo/v1/publicwebdav.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\IRootFolder;
use OCP\Files\Mount\IMountManager;
use OCP\Files\Storage\IStorage;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IPreview;
Expand Down Expand Up @@ -109,14 +110,14 @@ function (\Sabre\DAV\Server $server) use (

// FIXME: should not add storage wrappers outside of preSetup, need to find a better way
$previousLog = Filesystem::logWarningWhenAddingStorageWrapper(false);
Filesystem::addStorageWrapper('sharePermissions', function ($mountPoint, $storage) use ($share) {
Filesystem::addStorageWrapper('sharePermissions', function (string $mountPoint, IStorage $storage) use ($share) {
return new DirPermissionsMask([
'storage' => $storage,
'mask' => $share->getPermissions() | Constants::PERMISSION_SHARE,
'path' => 'files'
]);
});
Filesystem::addStorageWrapper('shareOwner', function ($mountPoint, $storage) use ($share) {
Filesystem::addStorageWrapper('shareOwner', function (string $mountPoint, IStorage $storage) use ($share) {
return new PublicOwnerWrapper(['storage' => $storage, 'owner' => $share->getShareOwner()]);
});
Filesystem::logWarningWhenAddingStorageWrapper($previousLog);
Expand Down
7 changes: 4 additions & 3 deletions apps/dav/appinfo/v2/publicremote.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use OCP\Files\IHomeStorage;
use OCP\Files\IRootFolder;
use OCP\Files\Mount\IMountManager;
use OCP\Files\Storage\IStorage;
use OCP\ICacheFactory;
use OCP\IConfig;
use OCP\IDBConnection;
Expand Down Expand Up @@ -120,7 +121,7 @@
$previousLog = Filesystem::logWarningWhenAddingStorageWrapper(false);

/** @psalm-suppress MissingClosureParamType */
Filesystem::addStorageWrapper('sharePermissions', function ($mountPoint, $storage) use ($requestUri, $baseuri, $share) {
Filesystem::addStorageWrapper('sharePermissions', function (string $mountPoint, IStorage $storage) use ($requestUri, $baseuri, $share) {
$mask = $share->getPermissions() | Constants::PERMISSION_SHARE;

// For chunked uploads it is necessary to have read and delete permission,
Expand All @@ -141,13 +142,13 @@
});

/** @psalm-suppress MissingClosureParamType */
Filesystem::addStorageWrapper('shareOwner', function ($mountPoint, $storage) use ($share) {
Filesystem::addStorageWrapper('shareOwner', function (string $mountPoint, IStorage $storage) use ($share) {
return new PublicOwnerWrapper(['storage' => $storage, 'owner' => $share->getShareOwner()]);
});

// Ensure that also private shares have the `getShare` method
/** @psalm-suppress MissingClosureParamType */
Filesystem::addStorageWrapper('getShare', function ($mountPoint, $storage) use ($share) {
Filesystem::addStorageWrapper('getShare', function (string $mountPoint, IStorage $storage) use ($share) {
return new PublicShareWrapper(['storage' => $storage, 'share' => $share]);
}, 0);

Expand Down
3 changes: 2 additions & 1 deletion apps/files_sharing/tests/DeleteOrphanedSharesJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use OC\Files\Filesystem;
use OC\SystemConfig;
use OCA\Files_Sharing\DeleteOrphanedSharesJob;
use OCA\Files_Trashbin\Storage;
use OCP\App\IAppManager;
use OCP\Constants;
use OCP\IDBConnection;
Expand Down Expand Up @@ -60,7 +61,7 @@ public static function setUpBeforeClass(): void {
$appManager->disableApp('files_trashbin');

// just in case...
Filesystem::getLoader()->removeStorageWrapper('oc_trashbin');
Filesystem::getLoader()->removeStorageWrapper(Storage::class);
}

public static function tearDownAfterClass(): void {
Expand Down
3 changes: 2 additions & 1 deletion apps/files_sharing/tests/UpdaterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use OC\Files\View;
use OCA\Files_Sharing\Helper;
use OCA\Files_Trashbin\AppInfo\Application;
use OCA\Files_Trashbin\Storage;
use OCP\App\IAppManager;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\Constants;
Expand Down Expand Up @@ -121,7 +122,7 @@ public function testDeleteParentFolder(): void {
$appManager->disableApp('files_trashbin');
}

Filesystem::getLoader()->removeStorageWrapper('oc_trashbin');
Filesystem::getLoader()->removeStorageWrapper(Storage::class);
}

public static function shareFolderProvider() {
Expand Down
30 changes: 26 additions & 4 deletions apps/files_trashbin/lib/Listener/EventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,33 @@
namespace OCA\Files_Trashbin\Listener;

use OCA\Files_Trashbin\Storage;
use OCA\Files_Trashbin\Trash\ITrashManager;
use OCA\Files_Trashbin\Trashbin;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\EventDispatcher\IEventListener;
use OCP\Files\Events\BeforeFileSystemSetupEvent;
use OCP\Files\Events\Node\NodeWrittenEvent;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\Files\Storage\IStorage;
use OCP\IRequest;
use OCP\IUserManager;
use OCP\User\Events\BeforeUserDeletedEvent;
use Psr\Log\LoggerInterface;

/** @template-implements IEventListener<NodeWrittenEvent|BeforeUserDeletedEvent|BeforeFileSystemSetupEvent> */
class EventListener implements IEventListener {
public function __construct(
private IUserManager $userManager,
private IRootFolder $rootFolder,
private ?string $userId = null,
private readonly IUserManager $userManager,
private readonly IRootFolder $rootFolder,
private readonly IRequest $request,
private readonly IEventDispatcher $eventDispatcher,
private readonly LoggerInterface $logger,
private readonly ITrashManager $trashManager,
private readonly ?string $userId = null,
) {
}

Expand Down Expand Up @@ -57,7 +66,20 @@ public function handle(Event $event): void {
}

if ($event instanceof BeforeFileSystemSetupEvent) {
Storage::setupStorage();
$event->addStorageWrapper(
Storage::class,
function (string $mountPoint, IStorage $storage): Storage {
return new Storage(
['storage' => $storage, 'mountPoint' => $mountPoint],
$this->trashManager,
$this->userManager,
$this->logger,
$this->eventDispatcher,
$this->rootFolder,
$this->request,
);
},
1);
}
}
}
2 changes: 1 addition & 1 deletion apps/files_trashbin/lib/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public static function setupStorage(): void {
$rootFolder = Server::get(IRootFolder::class);
$request = Server::get(IRequest::class);
Filesystem::addStorageWrapper(
'oc_trashbin',
Storage::class,
function (string $mountPoint, IStorage $storage) use ($trashManager, $userManager, $logger, $eventDispatcher, $rootFolder, $request) {
return new Storage(
['storage' => $storage, 'mountPoint' => $mountPoint],
Expand Down
4 changes: 2 additions & 2 deletions apps/files_trashbin/tests/StorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ protected function setUp(): void {
}

protected function tearDown(): void {
Filesystem::getLoader()->removeStorageWrapper('oc_trashbin');
Filesystem::getLoader()->removeStorageWrapper(Storage::class);
$this->logout();
$user = Server::get(IUserManager::class)->get($this->user);
if ($user !== null) {
Expand Down Expand Up @@ -502,7 +502,7 @@ public function testKeepFileAndVersionsWhenMovingFileBetweenStorages(): void {
$storage2 = new Temporary([]);
Filesystem::mount($storage2, [], $this->user . '/files/substorage');

// trigger a version (multiple would not work because of the expire logic)
// trigger a version (multiple would not work because of the expiration logic)
$this->userView->file_put_contents('test.txt', 'v1');

$results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files');
Expand Down
3 changes: 2 additions & 1 deletion apps/files_trashbin/tests/TrashbinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use OCA\Files_Trashbin\AppInfo\Application as TrashbinApplication;
use OCA\Files_Trashbin\Expiration;
use OCA\Files_Trashbin\Helper;
use OCA\Files_Trashbin\Storage;
use OCA\Files_Trashbin\Trashbin;
use OCP\App\IAppManager;
use OCP\AppFramework\Utility\ITimeFactory;
Expand Down Expand Up @@ -96,7 +97,7 @@ public static function tearDownAfterClass(): void {

\OC_Hook::clear();

Filesystem::getLoader()->removeStorageWrapper('oc_trashbin');
Filesystem::getLoader()->removeStorageWrapper(Storage::class);

if (self::$trashBinStatus) {
Server::get(IAppManager::class)->enableApp('files_trashbin');
Expand Down
4 changes: 2 additions & 2 deletions lib/private/Encryption/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,11 @@ public function getDefaultEncryptionModuleId() {
/**
* Add storage wrapper
*/
public function setupStorage() {
public function setupStorage(): void {
// If encryption is disabled and there are no loaded modules it makes no sense to load the wrapper
if (!empty($this->encryptionModules) || $this->isEnabled()) {
$encryptionWrapper = new EncryptionWrapper($this->arrayCache, $this, $this->logger);
Filesystem::addStorageWrapper('oc_encryption', [$encryptionWrapper, 'wrapStorage'], 2);
Filesystem::addStorageWrapper('oc_encryption', $encryptionWrapper->wrapStorage(...), 2);
}
}

Expand Down
3 changes: 0 additions & 3 deletions lib/private/Files/Mount/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,6 @@ public function findByStorageId(string $id): array {
return $result;
}

/**
* @return IMountPoint[]
*/
#[\Override]
public function getAll(): array {
return $this->mounts;
Expand Down
56 changes: 32 additions & 24 deletions lib/private/Files/SetupManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
use OCP\Files\Mount\IMountPoint;
use OCP\Files\NotFoundException;
use OCP\Files\Storage\IStorage;
use OCP\Files\Storage\IStorageFactory;
use OCP\Group\Events\UserAddedEvent;
use OCP\Group\Events\UserRemovedEvent;
use OCP\HintException;
Expand Down Expand Up @@ -115,6 +116,7 @@ public function __construct(
private IAppManager $appManager,
private FileAccess $fileAccess,
private IAppConfig $appConfig,
private IStorageFactory $storageFactory,
) {
$this->cache = $cacheFactory->createDistributed('setupmanager::');
$this->listeningForProviders = false;
Expand Down Expand Up @@ -154,28 +156,30 @@ private function isPathSetup(string $path): bool {
return false;
}

private function setupBuiltinWrappers(): void {
/**
* @param IMountPoint[] $existingMounts
*/
private function setupBuiltinWrappers(array $existingMounts): void {
if ($this->setupBuiltinWrappersDone) {
return;
}
$this->setupBuiltinWrappersDone = true;

// load all filesystem apps before, so no setup-hook gets lost
$this->appManager->loadApps(['filesystem']);
$prevLogging = Filesystem::logWarningWhenAddingStorageWrapper(false);

Filesystem::addStorageWrapper('mount_options', function ($mountPoint, IStorage $storage, IMountPoint $mount) {
$this->storageFactory->addStorageWrapper('mount_options', function (string $mountPoint, IStorage $storage, IMountPoint $mount): IStorage {
if ($storage->instanceOfStorage(Common::class)) {
$options = array_merge($mount->getOptions(), ['mount_point' => $mountPoint]);
$storage->setMountOptions($options);
}
return $storage;
});
}, 50, $existingMounts);

$reSharingEnabled = $this->appConfig->getValueBool('core', 'shareapi_allow_resharing', true);
$user = $this->userSession->getUser();
$sharingEnabledForUser = $user ? !$this->shareDisableChecker->sharingDisabledForUser($user->getUID()) : true;
Filesystem::addStorageWrapper(
$this->storageFactory->addStorageWrapper(
'sharing_mask',
function ($mountPoint, IStorage $storage, IMountPoint $mount) use ($reSharingEnabled, $sharingEnabledForUser) {
$sharingEnabledForMount = $mount->getOption('enable_sharing', true);
Expand All @@ -187,27 +191,26 @@ function ($mountPoint, IStorage $storage, IMountPoint $mount) use ($reSharingEna
]);
}
return $storage;
}
);
}, 50, $existingMounts);

// install storage availability wrapper, before most other wrappers
Filesystem::addStorageWrapper('oc_availability', function ($mountPoint, IStorage $storage, IMountPoint $mount) {
$this->storageFactory->addStorageWrapper(Availability::class, function (string $mountPoint, IStorage $storage, IMountPoint $mount) {
$externalMount = $mount instanceof ExternalMountPoint || $mount instanceof Mount;
if ($externalMount && !$storage->isLocal()) {
return new Availability(['storage' => $storage]);
}
return $storage;
});
}, 50, $existingMounts);

Filesystem::addStorageWrapper('oc_encoding', function ($mountPoint, IStorage $storage, IMountPoint $mount) {
$this->storageFactory->addStorageWrapper(Encoding::class, function (string $mountPoint, IStorage $storage, IMountPoint $mount) {
if ($mount->getOption('encoding_compatibility', false) && !$mount instanceof SharedMount) {
return new Encoding(['storage' => $storage]);
}
return $storage;
});
}, 50, $existingMounts);

$quotaIncludeExternal = $this->config->getSystemValue('quota_include_external_storage', false);
Filesystem::addStorageWrapper('oc_quota', function ($mountPoint, $storage, IMountPoint $mount) use ($quotaIncludeExternal) {
$this->storageFactory->addStorageWrapper(Quota::class, function ($mountPoint, $storage, IMountPoint $mount) use ($quotaIncludeExternal) {
// set up quota for home storages, even for other users
// which can happen when using sharing
if ($mount instanceof HomeMountPoint) {
Expand All @@ -218,9 +221,9 @@ function ($mountPoint, IStorage $storage, IMountPoint $mount) use ($reSharingEna
}

return $storage;
});
}, 50, $existingMounts);

Filesystem::addStorageWrapper('readonly', function ($mountPoint, IStorage $storage, IMountPoint $mount) {
$this->storageFactory->addStorageWrapper('readonly', function (string $mountPoint, IStorage $storage, IMountPoint $mount) {
/*
* Do not allow any operations that modify the storage
*/
Expand All @@ -235,9 +238,7 @@ function ($mountPoint, IStorage $storage, IMountPoint $mount) use ($reSharingEna
]);
}
return $storage;
});

Filesystem::logWarningWhenAddingStorageWrapper($prevLogging);
}, 50, $existingMounts);
}

/**
Expand Down Expand Up @@ -299,7 +300,7 @@ public function setupForUser(IUser $user): void {
/**
* Part of the user setup that is run only once per user.
*/
private function oneTimeUserSetup(IUser $user) {
private function oneTimeUserSetup(IUser $user): void {
if ($this->isSetupStarted($user)) {
return;
}
Expand All @@ -309,18 +310,24 @@ private function oneTimeUserSetup(IUser $user) {

$this->eventLogger->start('fs:setup:user:onetime', 'Onetime filesystem for user');

$this->setupBuiltinWrappers();

$prevLogging = Filesystem::logWarningWhenAddingStorageWrapper(false);
$mounts = $this->mountManager->getAll();
$this->setupBuiltinWrappers($mounts);

// TODO remove hook
$prevLogging = Filesystem::logWarningWhenAddingStorageWrapper(false);
OC_Hook::emit('OC_Filesystem', 'preSetup', ['user' => $user->getUID()]);

$event = new BeforeFileSystemSetupEvent($user);
$this->eventDispatcher->dispatchTyped($event);

Filesystem::logWarningWhenAddingStorageWrapper($prevLogging);

$storageWrappers = $event->getStorageWrappers();
if ($storageWrappers !== []) {
foreach ($storageWrappers as $wrapperName => $wrapper) {
$this->storageFactory->addStorageWrapper($wrapperName, $wrapper['callable'], $wrapper['priority'], $mounts);
}
}

$userDir = '/' . $user->getUID() . '/files';

Filesystem::initInternal($userDir);
Expand Down Expand Up @@ -426,7 +433,8 @@ public function setupRoot(): void {
return;
}

$this->setupBuiltinWrappers();
$mounts = $this->mountManager->getAll();
$this->setupBuiltinWrappers($mounts);

$this->rootSetup = true;

Expand Down Expand Up @@ -772,7 +780,7 @@ private function listenForNewMountProviders() {
}
}

private function setupListeners() {
private function setupListeners(): void {
// note that this event handling is intentionally pessimistic
// clearing the cache to often is better than not enough

Expand Down
Loading
Loading