Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix share transfer of single files and on the transfered node #22116

Merged
merged 1 commit into from
Sep 1, 2020
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
39 changes: 36 additions & 3 deletions apps/files/lib/Service/OwnershipTransferService.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
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;
Expand Down Expand Up @@ -65,12 +66,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 @@ -151,7 +157,9 @@ public function transfer(IUser $sourceUser,
// collect all the shares
$shares = $this->collectUsersShares(
$sourceUid,
$output
$output,
$view,
$sourcePath
);

// transfer the files
Expand Down Expand Up @@ -236,7 +244,9 @@ function (FileInfo $fileInfo) use ($progress) {
}

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

$shares = [];
Expand All @@ -249,6 +259,23 @@ private function collectUsersShares(string $sourceUid,
if (empty($sharePage)) {
break;
}
if ($path !== null) {
$sharePage = array_filter($sharePage, function (IShare $share) use ($view, $path) {
try {
$relativePath = $view->getPath($share->getNodeId());
$singleFileTranfer = $view->is_file($path);
if ($singleFileTranfer) {
return Filesystem::normalizePath($relativePath) === Filesystem::normalizePath($path);
}

return mb_strpos(
Filesystem::normalizePath($relativePath . '/', false),
Filesystem::normalizePath($path . '/', false)) === 0;
} catch (\Exception $e) {
return false;
}
});
}
$shares = array_merge($shares, $sharePage);
$offset += 50;
}
Expand Down Expand Up @@ -309,6 +336,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(): void {
$this->cacheInfoCache = new CappedMemoryCache();
$this->mountsForUsers = new CappedMemoryCache();
}
}
7 changes: 7 additions & 0 deletions lib/public/Files/Config/IUserMountCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,11 @@ public function remoteStorageMounts($storageId);
* @since 13.0.0
*/
public function getUsedSpaceForUsers(array $users);

/**
* Clear all entries from the in-memory cache
*
* @since 20.0.0
*/
public function clear(): void;
}