Skip to content

Commit

Permalink
Transfer shares of the transferred root node
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <jus@bitgrid.net>
  • Loading branch information
juliushaertl committed Aug 5, 2020
1 parent a4d511d commit 1eb8ddc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
29 changes: 28 additions & 1 deletion apps/files/lib/Service/OwnershipTransferService.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@
use OC\Files\View;
use OCA\Files\Exception\TransferOwnershipException;
use OCP\Encryption\IManager as IEncryptionManager;
use OCP\Files\Config\IUserMountCache;
use OCP\Files\FileInfo;
use OCP\Files\IHomeStorage;
use OCP\Files\InvalidPathException;
use OCP\Files\Mount\IMountManager;
use OCP\Files\NotFoundException;
use OCP\IUser;
use OCP\Share\IManager as IShareManager;
use OCP\Share\IShare;
Expand All @@ -63,12 +65,17 @@ class OwnershipTransferService {
/** @var IMountManager */
private $mountManager;

/** @var IUserMountCache */
private $userMountCache;

public function __construct(IEncryptionManager $manager,
IShareManager $shareManager,
IMountManager $mountManager) {
IMountManager $mountManager,
IUserMountCache $userMountCache) {
$this->encryptionManager = $manager;
$this->shareManager = $shareManager;
$this->mountManager = $mountManager;
$this->userMountCache = $userMountCache;
}

/**
Expand Down Expand Up @@ -149,6 +156,7 @@ public function transfer(IUser $sourceUser,
// collect all the shares
$shares = $this->collectUsersShares(
$sourceUid,
$sourcePath,
$output
);

Expand All @@ -161,6 +169,8 @@ public function transfer(IUser $sourceUser,
$output
);

$view = new View();

// restore the shares
$this->restoreShares(
$sourceUid,
Expand Down Expand Up @@ -234,6 +244,7 @@ function (FileInfo $fileInfo) use ($progress) {
}

private function collectUsersShares(string $sourceUid,
string $path = null,
OutputInterface $output): array {
$output->writeln("Collecting all share information for files and folders of $sourceUid ...");

Expand All @@ -247,6 +258,16 @@ private function collectUsersShares(string $sourceUid,
if (empty($sharePage)) {
break;
}
if ($path) {
$sharePage = array_filter($sharePage, function (IShare $share) use ($path) {
try {
$node = $share->getNode();
return \OC\Files\Filesystem::normalizePath($path) === $node->getPath();
} catch (\Exception $e) {
return false;
}
});
}
$shares = array_merge($shares, $sharePage);
$offset += 50;
}
Expand Down Expand Up @@ -307,6 +328,12 @@ private function restoreShares(string $sourceUid,
$share->setSharedBy($destinationUid);
}


// trigger refetching of the node so that the new owner and mountpoint are taken into account
// otherwise the checks on the share update will fail due to the original node not being available in the new user scope
$this->userMountCache->clear();
$share->setNodeId($share->getNode()->getId());

$this->shareManager->updateShare($share);
}
} catch (\OCP\Files\NotFoundException $e) {
Expand Down
5 changes: 5 additions & 0 deletions lib/private/Files/Config/UserMountCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,4 +409,9 @@ public function getUsedSpaceForUsers(array $users) {
$result->closeCursor();
return $results;
}

public function clear() {
$this->cacheInfoCache = new CappedMemoryCache();
$this->mountsForUsers = new CappedMemoryCache();
}
}

0 comments on commit 1eb8ddc

Please sign in to comment.