Skip to content

Commit 2bb3af7

Browse files
author
Vincent Petry
authored
Merge pull request #25829 from owncloud/fix-unmerged-shares-repair-betterregexp
Improve regexp to detect duplicate folders when repairing unmerged shares
2 parents 88ebcc6 + 68d309a commit 2bb3af7

2 files changed

Lines changed: 74 additions & 42 deletions

File tree

lib/private/Repair/RepairUnmergedShares.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ private function getSharesWithUser($shareType, $shareWiths) {
148148
return $groupedShares;
149149
}
150150

151+
private function isPotentialDuplicateName($name) {
152+
return (preg_match('/\(\d+\)(\.[^\.]+)?$/', $name) === 1);
153+
}
154+
151155
/**
152156
* Decide on the best target name based on all group shares and subshares,
153157
* goal is to increase the likeliness that the chosen name matches what
@@ -169,18 +173,12 @@ private function findBestTargetName($groupShares, $subShares) {
169173
$pickedShare = null;
170174
// sort by stime, this also properly sorts the direct user share if any
171175
@usort($subShares, function($a, $b) {
172-
if ($a['stime'] < $b['stime']) {
173-
return -1;
174-
} else if ($a['stime'] > $b['stime']) {
175-
return 1;
176-
}
177-
178-
return 0;
176+
return ((int)$a['stime'] - (int)$b['stime']);
179177
});
180178

181179
foreach ($subShares as $subShare) {
182180
// skip entries that have parenthesis with numbers
183-
if (preg_match('/\([0-9]*\)/', $subShare['file_target']) === 1) {
181+
if ($this->isPotentialDuplicateName($subShare['file_target'])) {
184182
continue;
185183
}
186184
// pick any share found that would match, the last being the most recent

tests/lib/Repair/RepairUnmergedSharesTest.php

Lines changed: 68 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
use OCP\Migration\IRepairStep;
2929
use Test\TestCase;
3030
use OC\Share20\DefaultShareProvider;
31+
use OCP\IUserManager;
32+
use OCP\IGroupManager;
3133

3234
/**
3335
* Tests for repairing invalid shares
@@ -47,6 +49,12 @@ class RepairUnmergedSharesTest extends TestCase {
4749
/** @var int */
4850
private $lastShareTime;
4951

52+
/** @var IUserManager */
53+
private $userManager;
54+
55+
/** @var IGroupManager */
56+
private $groupManager;
57+
5058
protected function setUp() {
5159
parent::setUp();
5260

@@ -61,45 +69,14 @@ protected function setUp() {
6169
$this->connection = \OC::$server->getDatabaseConnection();
6270
$this->deleteAllShares();
6371

64-
$user1 = $this->getMock('\OCP\IUser');
65-
$user1->expects($this->any())
66-
->method('getUID')
67-
->will($this->returnValue('user1'));
68-
69-
$user2 = $this->getMock('\OCP\IUser');
70-
$user2->expects($this->any())
71-
->method('getUID')
72-
->will($this->returnValue('user2'));
73-
74-
$users = [$user1, $user2];
75-
76-
$groupManager = $this->getMock('\OCP\IGroupManager');
77-
$groupManager->expects($this->any())
78-
->method('getUserGroupIds')
79-
->will($this->returnValueMap([
80-
// owner
81-
[$user1, ['samegroup1', 'samegroup2']],
82-
// recipient
83-
[$user2, ['recipientgroup1', 'recipientgroup2']],
84-
]));
85-
86-
$userManager = $this->getMock('\OCP\IUserManager');
87-
$userManager->expects($this->once())
88-
->method('countUsers')
89-
->will($this->returnValue([2]));
90-
$userManager->expects($this->once())
91-
->method('callForAllUsers')
92-
->will($this->returnCallback(function(\Closure $closure) use ($users) {
93-
foreach ($users as $user) {
94-
$closure($user);
95-
}
96-
}));
72+
$this->userManager = $this->getMock('\OCP\IUserManager');
73+
$this->groupManager = $this->getMock('\OCP\IGroupManager');
9774

9875
// used to generate incremental stimes
9976
$this->lastShareTime = time();
10077

10178
/** @var \OCP\IConfig $config */
102-
$this->repair = new RepairUnmergedShares($config, $this->connection, $userManager, $groupManager);
79+
$this->repair = new RepairUnmergedShares($config, $this->connection, $this->userManager, $this->groupManager);
10380
}
10481

10582
protected function tearDown() {
@@ -510,6 +487,38 @@ public function sharesDataProvider() {
510487
* @dataProvider sharesDataProvider
511488
*/
512489
public function testMergeGroupShares($shares, $expectedShares) {
490+
$user1 = $this->getMock('\OCP\IUser');
491+
$user1->expects($this->any())
492+
->method('getUID')
493+
->will($this->returnValue('user1'));
494+
495+
$user2 = $this->getMock('\OCP\IUser');
496+
$user2->expects($this->any())
497+
->method('getUID')
498+
->will($this->returnValue('user2'));
499+
500+
$users = [$user1, $user2];
501+
502+
$this->groupManager->expects($this->any())
503+
->method('getUserGroupIds')
504+
->will($this->returnValueMap([
505+
// owner
506+
[$user1, ['samegroup1', 'samegroup2']],
507+
// recipient
508+
[$user2, ['recipientgroup1', 'recipientgroup2']],
509+
]));
510+
511+
$this->userManager->expects($this->once())
512+
->method('countUsers')
513+
->will($this->returnValue([2]));
514+
$this->userManager->expects($this->once())
515+
->method('callForAllUsers')
516+
->will($this->returnCallback(function(\Closure $closure) use ($users) {
517+
foreach ($users as $user) {
518+
$closure($user);
519+
}
520+
}));
521+
513522
$shareIds = [];
514523

515524
foreach ($shares as $share) {
@@ -536,5 +545,30 @@ public function testMergeGroupShares($shares, $expectedShares) {
536545
$this->assertEquals($expectedShare[1], $share['permissions']);
537546
}
538547
}
548+
549+
public function duplicateNamesProvider() {
550+
return [
551+
// matching
552+
['filename (1).txt', true],
553+
['folder (2)', true],
554+
['filename (1)(2).txt', true],
555+
// non-matching
556+
['filename ().txt', false],
557+
['folder ()', false],
558+
['folder (1x)', false],
559+
['folder (x1)', false],
560+
['filename (a)', false],
561+
['filename (1).', false],
562+
['filename (1).txt.txt', false],
563+
['filename (1)..txt', false],
564+
];
565+
}
566+
567+
/**
568+
* @dataProvider duplicateNamesProvider
569+
*/
570+
public function testIsPotentialDuplicateName($name, $expectedResult) {
571+
$this->assertEquals($expectedResult, $this->invokePrivate($this->repair, 'isPotentialDuplicateName', [$name]));
572+
}
539573
}
540574

0 commit comments

Comments
 (0)