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
2 changes: 1 addition & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Folders can be configured from *Group folders* in the admin settings.

After a folder is created, the admin can give access to the folder to one or more groups, control their write/sharing permissions and assign a quota for the folder.
]]></description>
<version>18.1.4</version>
<version>18.1.5</version>
<licence>agpl</licence>
<author>Robin Appelman</author>
<namespace>GroupFolders</namespace>
Expand Down
13 changes: 10 additions & 3 deletions lib/Folder/FolderManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use OCA\GroupFolders\ACL\UserMapping\IUserMapping;
use OCA\GroupFolders\ACL\UserMapping\IUserMappingManager;
use OCA\GroupFolders\ACL\UserMapping\UserMapping;
use OCA\GroupFolders\Mount\FolderStorageManager;
use OCA\GroupFolders\Mount\GroupMountPoint;
use OCA\GroupFolders\ResponseDefinitions;
use OCP\AutoloadNotAllowedException;
Expand Down Expand Up @@ -79,6 +80,7 @@ public function __construct(
private IEventDispatcher $eventDispatcher,
private IConfig $config,
private IUserMappingManager $userMappingManager,
private readonly FolderStorageManager $folderStorageManager,
) {
}

Expand Down Expand Up @@ -128,9 +130,7 @@ private function getGroupFolderRootId(int $rootStorageId): int {

private function joinQueryWithFileCache(IQueryBuilder $query, int $rootStorageId): void {
$conditions = [
// concat with empty string to work around missing cast to string
$query->expr()->eq('c.name', $query->func()->concat('f.folder_id', $query->expr()->literal(''))),
$query->expr()->eq('c.parent', $query->createNamedParameter($this->getGroupFolderRootId($rootStorageId))),
$query->expr()->eq('c.fileid', 'f.root_id'),
];
if ($this->connection->getShardDefinition('filecache')) {
$conditions[] = $query->expr()->eq('c.storage', $query->createNamedParameter($rootStorageId));
Expand Down Expand Up @@ -725,6 +725,13 @@ public function createFolder(string $mountPoint): int {
$query->executeStatement();
$id = $query->getLastInsertId();

['storage_id' => $storageId, 'root_id' => $rootId] = $this->folderStorageManager->getRootAndStorageIdForFolder($id);
$query->update('group_folders')
->set('root_id', $query->createNamedParameter($rootId))
->set('storage_id', $query->createNamedParameter($storageId))
->where($query->expr()->eq('folder_id', $query->createNamedParameter($id)));
$query->executeStatement();

$this->eventDispatcher->dispatchTyped(new CriticalActionPerformedEvent('A new groupfolder "%s" was created with id %d', [$mountPoint, $id]));

return $id;
Expand Down
7 changes: 7 additions & 0 deletions lib/Migration/Version102020Date20180806161449.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace OCA\GroupFolders\Migration;

use OCP\DB\ISchemaWrapper;
use OCP\DB\Types;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;

Expand Down Expand Up @@ -40,6 +41,12 @@ public function changeSchema(IOutput $output, \Closure $schemaClosure, array $op
// Removed in migration Version19000Date20240903062631
//'default' => -3,
]);

// from Version20000Date20250612140256.php
$table->addColumn('root_id', Types::BIGINT, ['notnull' => false]);
$table->addColumn('storage_id', Types::BIGINT, ['notnull' => false]);
$table->addColumn('options', Types::TEXT, ['notnull' => false]);

$table->setPrimaryKey(['folder_id']);
}

Expand Down
138 changes: 138 additions & 0 deletions lib/Migration/Version20000Date20250612140256.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\GroupFolders\Migration;

use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\DB\Types;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;
use Override;

/**
* Adds root_id and options to the group folders table
*/
class Version20000Date20250612140256 extends SimpleMigrationStep {
public function __construct(
private readonly IDBConnection $connection,
) {
}

#[Override]
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();

if ($schema->hasTable('group_folders')) {
$table = $schema->getTable('group_folders');
if (!$table->hasColumn('root_id')) {
$table->addColumn('root_id', Types::BIGINT, ['notnull' => false]);
}
if (!$table->hasColumn('storage_id')) {
$table->addColumn('storage_id', Types::BIGINT, ['notnull' => false]);
}
if (!$table->hasColumn('options')) {
$table->addColumn('options', Types::TEXT, ['notnull' => false]);
}
return $schema;
}
return null;
}

#[Override]
public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
$storageId = $this->getJailedGroupFolderStorageId();
if ($storageId === null) {
return;
}
$rootIds = $this->getJailedRootIds($storageId);
if (count($rootIds) === 0) {
return;
}

try {
$this->connection->beginTransaction();

$query = $this->connection->getQueryBuilder();
$query->update('group_folders')
->set('root_id', $query->createParameter('root_id'))
->set('storage_id', $query->createNamedParameter($storageId))
->where($query->expr()->eq('folder_id', $query->createParameter('folder_id')))
->andWhere($query->expr()->isNull('storage_id'));

foreach ($rootIds as $folderId => $rootId) {
$query->setParameter('root_id', $rootId);
$query->setParameter('folder_id', $folderId);
$query->executeStatement();
}

$this->connection->commit();
} catch (\Exception $e) {
$this->connection->rollBack();
throw $e;
}
}

/**
* @return array<int, int>
*/
private function getJailedRootIds(int $storageId): array {
$parentFolderId = $this->getJailedGroupFolderRootId($storageId);
if ($parentFolderId === null) {
return [];
}

$query = $this->connection->getQueryBuilder();
$query->select('name', 'fileid')
->from('filecache')
->where($query->expr()->eq('parent', $query->createNamedParameter($parentFolderId)))
->andWhere($query->expr()->eq('storage', $query->createNamedParameter($storageId)));
$result = $query->executeQuery();

$rootIds = [];
while ($row = $result->fetch()) {
if (is_numeric($row['name'])) {
$rootIds[(int)$row['name']] = $row['fileid'];
}
}
return $rootIds;
}

private function getJailedGroupFolderRootId(int $storageId): ?int {
$query = $this->connection->getQueryBuilder();
$query->select('fileid')
->from('filecache')
->where($query->expr()->eq('path_hash', $query->createNamedParameter(md5('__groupfolders'))))
->andWhere($query->expr()->eq('storage', $query->createNamedParameter($storageId)));

$id = $query->executeQuery()->fetchOne();
if ($id === false) {
return null;
} else {
return (int)$id;
}
}

private function getJailedGroupFolderStorageId(): ?int {
$query = $this->connection->getQueryBuilder();
$query->select('storage')
->from('filecache')
->runAcrossAllShards()
->andWhere($query->expr()->eq('path_hash', $query->createNamedParameter(md5('__groupfolders'))));

$id = $query->executeQuery()->fetchOne();
if ($id === false) {
return null;
} else {
return (int)$id;
}
}
}
77 changes: 77 additions & 0 deletions lib/Mount/FolderStorageManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Robin Appelman <[email protected]>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\GroupFolders\Mount;

use OC\Files\Storage\Wrapper\Jail;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\Files\Storage\IStorage;
use OCP\IAppConfig;

class FolderStorageManager {
private readonly bool $enableEncryption;

public function __construct(
private readonly IRootFolder $rootFolder,
private readonly IAppConfig $appConfig,
) {
$this->enableEncryption = $this->appConfig->getValueBool('groupfolders', 'enable_encryption');
}

/**
* @return array{storage_id: int, root_id: int}
*/
public function getRootAndStorageIdForFolder(int $folderId): array {
$storage = $this->getBaseStorageForFolder($folderId);
$cache = $storage->getCache();
$id = $cache->getId('');
if ($id === -1) {
$storage->getScanner()->scan('');
$id = $cache->getId('');
if ($id === -1) {
throw new \Exception('Group folder root is not in cache even after scanning for folder ' . $folderId);
}
}
return [
'storage_id' => $cache->getNumericStorageId(),
'root_id' => $id,
];
}

public function getBaseStorageForFolder(int $folderId): IStorage {
try {
/** @var Folder $parentFolder */
$parentFolder = $this->rootFolder->get('__groupfolders');
} catch (NotFoundException) {
$parentFolder = $this->rootFolder->newFolder('__groupfolders');
}

try {
/** @var Folder $folder */
$folder = $parentFolder->get((string)$folderId);
} catch (NotFoundException) {
$folder = $parentFolder->newFolder((string)$folderId);
}
$rootStorage = $folder->getStorage();
$rootPath = $folder->getInternalPath();

if ($this->enableEncryption) {
return new GroupFolderEncryptionJail([
'storage' => $rootStorage,
'root' => $rootPath,
]);
} else {
return new Jail([
'storage' => $rootStorage,
'root' => $rootPath,
]);
}
}
}
Loading
Loading