Skip to content

Commit d0a7f83

Browse files
authored
Merge pull request #22116 from nextcloud/bugfix/noid/transfer-ownership-share-root
Fix share transfer of single files and on the transfered node
2 parents e0d767d + ac2999a commit d0a7f83

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

apps/files/lib/Service/OwnershipTransferService.php

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
use OC\Files\View;
3838
use OCA\Files\Exception\TransferOwnershipException;
3939
use OCP\Encryption\IManager as IEncryptionManager;
40+
use OCP\Files\Config\IUserMountCache;
4041
use OCP\Files\FileInfo;
4142
use OCP\Files\IHomeStorage;
4243
use OCP\Files\InvalidPathException;
@@ -65,12 +66,17 @@ class OwnershipTransferService {
6566
/** @var IMountManager */
6667
private $mountManager;
6768

69+
/** @var IUserMountCache */
70+
private $userMountCache;
71+
6872
public function __construct(IEncryptionManager $manager,
6973
IShareManager $shareManager,
70-
IMountManager $mountManager) {
74+
IMountManager $mountManager,
75+
IUserMountCache $userMountCache) {
7176
$this->encryptionManager = $manager;
7277
$this->shareManager = $shareManager;
7378
$this->mountManager = $mountManager;
79+
$this->userMountCache = $userMountCache;
7480
}
7581

7682
/**
@@ -151,7 +157,9 @@ public function transfer(IUser $sourceUser,
151157
// collect all the shares
152158
$shares = $this->collectUsersShares(
153159
$sourceUid,
154-
$output
160+
$output,
161+
$view,
162+
$sourcePath
155163
);
156164

157165
// transfer the files
@@ -236,7 +244,9 @@ function (FileInfo $fileInfo) use ($progress) {
236244
}
237245

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

242252
$shares = [];
@@ -249,6 +259,23 @@ private function collectUsersShares(string $sourceUid,
249259
if (empty($sharePage)) {
250260
break;
251261
}
262+
if ($path !== null) {
263+
$sharePage = array_filter($sharePage, function (IShare $share) use ($view, $path) {
264+
try {
265+
$relativePath = $view->getPath($share->getNodeId());
266+
$singleFileTranfer = $view->is_file($path);
267+
if ($singleFileTranfer) {
268+
return Filesystem::normalizePath($relativePath) === Filesystem::normalizePath($path);
269+
}
270+
271+
return mb_strpos(
272+
Filesystem::normalizePath($relativePath . '/', false),
273+
Filesystem::normalizePath($path . '/', false)) === 0;
274+
} catch (\Exception $e) {
275+
return false;
276+
}
277+
});
278+
}
252279
$shares = array_merge($shares, $sharePage);
253280
$offset += 50;
254281
}
@@ -309,6 +336,12 @@ private function restoreShares(string $sourceUid,
309336
$share->setSharedBy($destinationUid);
310337
}
311338

339+
340+
// trigger refetching of the node so that the new owner and mountpoint are taken into account
341+
// otherwise the checks on the share update will fail due to the original node not being available in the new user scope
342+
$this->userMountCache->clear();
343+
$share->setNodeId($share->getNode()->getId());
344+
312345
$this->shareManager->updateShare($share);
313346
}
314347
} catch (\OCP\Files\NotFoundException $e) {

lib/private/Files/Config/UserMountCache.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,4 +409,9 @@ public function getUsedSpaceForUsers(array $users) {
409409
$result->closeCursor();
410410
return $results;
411411
}
412+
413+
public function clear(): void {
414+
$this->cacheInfoCache = new CappedMemoryCache();
415+
$this->mountsForUsers = new CappedMemoryCache();
416+
}
412417
}

lib/public/Files/Config/IUserMountCache.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,11 @@ public function remoteStorageMounts($storageId);
117117
* @since 13.0.0
118118
*/
119119
public function getUsedSpaceForUsers(array $users);
120+
121+
/**
122+
* Clear all entries from the in-memory cache
123+
*
124+
* @since 20.0.0
125+
*/
126+
public function clear(): void;
120127
}

0 commit comments

Comments
 (0)