-
-
Notifications
You must be signed in to change notification settings - Fork 5k
Expand file tree
/
Copy pathCleanupShareTarget.php
More file actions
212 lines (189 loc) · 7.11 KB
/
Copy pathCleanupShareTarget.php
File metadata and controls
212 lines (189 loc) · 7.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Files_Sharing\Repair;
use OC\Files\Cache\CacheEntry;
use OC\Files\SetupManager;
use OCA\Files_Sharing\ShareTargetValidator;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\Files\Config\ICachedMountInfo;
use OCP\Files\Config\IUserMountCache;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\ICacheFactory;
use OCP\IDBConnection;
use OCP\IUser;
use OCP\IUserManager;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;
use OCP\Share\IShare;
use Psr\Log\LoggerInterface;
/**
* @psalm-type ShareInfo = array{id: string|int, share_type: string, share_with: string, file_source: string, file_target: string}
*/
class CleanupShareTarget implements IRepairStep {
/** we only care about shares with a user target,
* since the underling group/deck/talk share doesn't get moved
*/
private const USER_SHARE_TYPES = [
IShare::TYPE_USER,
IShare::TYPE_USERGROUP,
IShare::TYPE_DECK_USER,
11 // TYPE_USERROOM
];
public function __construct(
private readonly IDBConnection $connection,
private readonly ShareTargetValidator $shareTargetValidator,
private readonly IUserManager $userManager,
private readonly SetupManager $setupManager,
private readonly IUserMountCache $userMountCache,
private readonly IRootFolder $rootFolder,
private readonly LoggerInterface $logger,
private readonly ICacheFactory $cacheFactory,
) {
}
#[\Override]
public function getName() {
return 'Cleanup share names with false conflicts';
}
#[\Override]
public function run(IOutput $output) {
$count = $this->countProblemShares();
if ($count === 0) {
return;
}
$output->startProgress($count);
$lastUser = '';
$userMounts = [];
$userFolder = null;
foreach ($this->getProblemShares() as $shareInfo) {
if ($shareInfo['share_type'] === IShare::TYPE_CIRCLE) {
$recipient = $this->getCircleShareOwner($shareInfo['share_with']);
if ($recipient === null) {
continue;
}
} else {
$recipient = $this->userManager->getExistingUser($shareInfo['share_with']);
}
if (!$recipient->isEnabled()) {
continue;
}
// since we ordered the share by user, we can reuse the last data until we get to the next user
if ($lastUser !== $recipient->getUID()) {
$lastUser = $recipient->getUID();
$this->setupManager->tearDown();
$this->setupManager->setupForUser($recipient);
$mounts = $this->userMountCache->getMountsForUser($recipient);
$mountPoints = array_map(fn (ICachedMountInfo $mount) => $mount->getMountPoint(), $mounts);
$userMounts = array_combine($mountPoints, $mounts);
$userFolder = $this->rootFolder->getUserFolder($recipient->getUID());
}
$oldTarget = $shareInfo['file_target'];
$newTarget = $this->cleanTarget($oldTarget);
$absoluteNewTarget = $userFolder->getFullPath($newTarget);
try {
$targetParentNode = $this->rootFolder->get(dirname($absoluteNewTarget));
} catch (NotFoundException) {
$absoluteNewTarget = $userFolder->getFullPath(basename($newTarget));
$targetParentNode = $userFolder;
}
try {
$absoluteNewTarget = $this->shareTargetValidator->generateUniqueTarget(
(int)$shareInfo['file_source'],
$absoluteNewTarget,
$targetParentNode->getMountPoint(),
fn ($path) => $userMounts[$path] ?? null,
);
$newTarget = $userFolder->getRelativePath($absoluteNewTarget);
if ($newTarget !== $oldTarget) {
$this->moveShare((string)$shareInfo['id'], $newTarget);
$oldMountPoint = "/{$recipient->getUID()}/files$oldTarget/";
$newMountPoint = "/{$recipient->getUID()}/files$newTarget/";
/** @var ICachedMountInfo $mount */
$mount = $userMounts[$oldMountPoint];
$userMounts[$newMountPoint] = $mount;
unset($userMounts[$oldMountPoint]);
$this->userMountCache->removeMount($oldMountPoint);
$this->userMountCache->addMount($recipient, $newMountPoint, new CacheEntry([
'fileid' => $mount->getRootId(),
'storage' => $mount->getStorageId(),
]), $mount->getMountProvider(), $mount->getMountId());
}
} catch (\Exception $e) {
$msg = 'error cleaning up share target: ' . $e->getMessage();
$this->logger->error($msg, ['exception' => $e, 'app' => 'files_sharing']);
$output->warning($msg);
}
$output->advance();
}
$this->cacheFactory->createDistributed('circles/')->clear();
$output->finishProgress();
$output->info("Fixed $count shares");
}
private function countProblemShares(): int {
$query = $this->connection->getQueryBuilder();
$query->select($query->func()->count('id'))
->from('share')
->where($query->expr()->like('file_target', $query->createNamedParameter('% (_) (_)%')))
->andWhere($query->expr()->in('share_type', $query->createNamedParameter(self::USER_SHARE_TYPES, IQueryBuilder::PARAM_INT_ARRAY), IQueryBuilder::PARAM_INT_ARRAY));
return (int)$query->executeQuery()->fetchOne();
}
private function moveShare(string $id, string $target): void {
// since we only process user-specific shares, we can just move them
// without having to check if we need to create a user-specific override
$query = $this->connection->getQueryBuilder();
$query->update('share')
->set('file_target', $query->createNamedParameter($target))
->where($query->expr()->eq('id', $query->createNamedParameter($id)))
->executeStatement();
}
/**
* @return \Traversable<ShareInfo>
*/
private function getProblemShares(): \Traversable {
$query = $this->connection->getQueryBuilder();
$query->select('id', 'share_type', 'share_with', 'file_source', 'file_target')
->from('share')
->where($query->expr()->like('file_target', $query->createNamedParameter('% (_) (_)%')))
->andWhere($query->expr()->orX(
$query->expr()->in('share_type', $query->createNamedParameter(self::USER_SHARE_TYPES, IQueryBuilder::PARAM_INT_ARRAY), IQueryBuilder::PARAM_INT_ARRAY),
$query->expr()->andX(
$query->expr()->eq('share_type', $query->createNamedParameter(
IShare::TYPE_CIRCLE, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT),
$query->expr()->isNotNull('parent'),
),
))
->orderBy('share_with')
->addOrderBy('id');
$result = $query->executeQuery();
/** @var \Traversable<ShareInfo> $rows */
$rows = $result->iterateAssociative();
return $rows;
}
private function cleanTarget(string $target): string {
return preg_replace('/( \([2-9]\)){2,}/', '', $target);
}
/**
* Get the user of a federated user from circles, if the circle id belong to a user
*/
private function getCircleShareOwner(string $circleId): ?IUser {
$query = $this->connection->getQueryBuilder();
$query->select('name')
->from('circles_circle')
->where($query->expr()->eq('unique_id', $query->createNamedParameter($circleId)))
->andWhere($query->expr()->eq('source', $query->createNamedParameter(1, IQueryBuilder::PARAM_INT)));
$name = $query->executeQuery()->fetchOne();
if ($name === false) {
return null;
}
[$type, $userId,] = explode(':', $name, 3);
if ($type === 'user') {
return $this->userManager->getExistingUser($userId);
} else {
return null;
}
}
}