|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Storeman\IndexMerger; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Log\LoggerAwareInterface; |
|
6
|
|
|
use Psr\Log\LoggerAwareTrait; |
|
7
|
|
|
use Psr\Log\NullLogger; |
|
8
|
|
|
use Storeman\Config\Configuration; |
|
9
|
|
|
use Storeman\ConflictHandler\ConflictHandlerInterface; |
|
10
|
|
|
use Storeman\Hash\HashProvider; |
|
11
|
|
|
use Storeman\Index\Index; |
|
12
|
|
|
use Storeman\Index\IndexObject; |
|
13
|
|
|
|
|
14
|
|
|
class StandardIndexMerger implements IndexMergerInterface, LoggerAwareInterface |
|
15
|
|
|
{ |
|
16
|
|
|
use LoggerAwareTrait; |
|
17
|
|
|
|
|
18
|
|
|
public const VERIFY_CONTENT = 2; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var Configuration |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $configuration; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var HashProvider |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $hashProvider; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct(Configuration $configuration, HashProvider $hashProvider) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->configuration = $configuration; |
|
33
|
|
|
$this->hashProvider = $hashProvider; |
|
34
|
|
|
$this->logger = new NullLogger(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* {@inheritdoc} |
|
39
|
|
|
*/ |
|
40
|
|
|
public function merge(ConflictHandlerInterface $conflictHandler, Index $remoteIndex, Index $localIndex, ?Index $lastLocalIndex, int $options = 0): Index |
|
41
|
|
|
{ |
|
42
|
|
|
$this->logger->info(sprintf("Merging indices using %s (Options: %s)", static::class, static::getOptionsDebugString($options))); |
|
43
|
|
|
|
|
44
|
|
|
$mergedIndex = new Index(); |
|
45
|
|
|
$lastLocalIndex = $lastLocalIndex ?: new Index(); |
|
46
|
|
|
|
|
47
|
|
|
foreach ($localIndex as $localObject) |
|
48
|
|
|
{ |
|
49
|
|
|
/** @var IndexObject $localObject */ |
|
50
|
|
|
|
|
51
|
|
|
$remoteObject = $remoteIndex->getObjectByPath($localObject->getRelativePath()); |
|
52
|
|
|
$lastLocalObject = $lastLocalIndex->getObjectByPath($localObject->getRelativePath()); |
|
53
|
|
|
|
|
54
|
|
|
if ($mergedObject = $this->mergeObject1($conflictHandler, $remoteObject, $localObject, $lastLocalObject, $options)) |
|
|
|
|
|
|
55
|
|
|
{ |
|
56
|
|
|
$mergedIndex->addObject($mergedObject); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
foreach ($remoteIndex as $remoteObject) |
|
61
|
|
|
{ |
|
62
|
|
|
/** @var IndexObject $remoteObject */ |
|
63
|
|
|
|
|
64
|
|
|
$localObject = $localIndex->getObjectByPath($remoteObject->getRelativePath()); |
|
65
|
|
|
$lastLocalObject = $lastLocalIndex->getObjectByPath($remoteObject->getRelativePath()); |
|
66
|
|
|
|
|
67
|
|
|
if ($localObject !== null) |
|
68
|
|
|
{ |
|
69
|
|
|
// already taken care of in local index iteration |
|
70
|
|
|
continue; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
if ($mergedObject = $this->mergeObject1($conflictHandler, $remoteObject, $localObject, $lastLocalObject, $options)) |
|
|
|
|
|
|
74
|
|
|
{ |
|
75
|
|
|
$mergedIndex->addObject($mergedObject); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
if ($options & static::INJECT_BLOBID) |
|
80
|
|
|
{ |
|
81
|
|
|
$this->injectBlobIds($mergedIndex, $localIndex); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $mergedIndex; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* First stage object merging looking at primarily at pure existence. |
|
89
|
|
|
* |
|
90
|
|
|
* @param ConflictHandlerInterface $conflictHandler |
|
91
|
|
|
* @param IndexObject $remoteObject |
|
92
|
|
|
* @param IndexObject $localObject |
|
93
|
|
|
* @param IndexObject $lastLocalObject |
|
94
|
|
|
* @param int $options |
|
95
|
|
|
* @return IndexObject |
|
96
|
|
|
*/ |
|
97
|
|
|
protected function mergeObject1(ConflictHandlerInterface $conflictHandler, ?IndexObject $remoteObject, ?IndexObject $localObject, ?IndexObject $lastLocalObject, int $options = 0): ?IndexObject |
|
98
|
|
|
{ |
|
99
|
|
|
if ($remoteObject === null && $localObject === null) |
|
100
|
|
|
{ |
|
101
|
|
|
// locally and remotely deleted |
|
102
|
|
|
return null; |
|
103
|
|
|
} |
|
104
|
|
|
elseif ($lastLocalObject === null) |
|
105
|
|
|
{ |
|
106
|
|
|
if ($remoteObject === null) |
|
107
|
|
|
{ |
|
108
|
|
|
// locally created |
|
109
|
|
|
return clone $localObject; |
|
110
|
|
|
} |
|
111
|
|
|
elseif ($localObject === null) |
|
112
|
|
|
{ |
|
113
|
|
|
// remotely created |
|
114
|
|
|
return clone $remoteObject; |
|
115
|
|
|
} |
|
116
|
|
|
elseif ($remoteObject !== null && $localObject !== null) |
|
117
|
|
|
{ |
|
118
|
|
|
// remotely and locally created |
|
119
|
|
|
return $this->resolveConflict($conflictHandler, $remoteObject, $localObject, $lastLocalObject); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
elseif ($remoteObject === null) |
|
123
|
|
|
{ |
|
124
|
|
|
// remotely deleted and locally changed |
|
125
|
|
|
return $this->resolveConflict($conflictHandler, $remoteObject, $localObject, $lastLocalObject); |
|
126
|
|
|
} |
|
127
|
|
|
elseif ($localObject === null) |
|
128
|
|
|
{ |
|
129
|
|
|
if ($remoteObject->equals($lastLocalObject)) |
|
130
|
|
|
{ |
|
131
|
|
|
// locally deleted |
|
132
|
|
|
return null; |
|
133
|
|
|
} |
|
134
|
|
|
else |
|
135
|
|
|
{ |
|
136
|
|
|
// remotely changed and locally deleted |
|
137
|
|
|
return $this->resolveConflict($conflictHandler, $remoteObject, $localObject, $lastLocalObject); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
return $this->mergeObject2($conflictHandler, $remoteObject, $localObject, $lastLocalObject, $options); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Second stage object merging. |
|
146
|
|
|
* |
|
147
|
|
|
* @param ConflictHandlerInterface $conflictHandler |
|
148
|
|
|
* @param IndexObject $remoteObject |
|
149
|
|
|
* @param IndexObject $localObject |
|
150
|
|
|
* @param IndexObject $lastLocalObject |
|
151
|
|
|
* @param int $options |
|
152
|
|
|
* @return IndexObject |
|
153
|
|
|
*/ |
|
154
|
|
|
protected function mergeObject2(ConflictHandlerInterface $conflictHandler, IndexObject $remoteObject, IndexObject $localObject, IndexObject $lastLocalObject, int $options = 0): IndexObject |
|
155
|
|
|
{ |
|
156
|
|
|
if ($remoteObject->getType() !== $localObject->getType()) |
|
157
|
|
|
{ |
|
158
|
|
|
return $this->resolveConflict($conflictHandler, $remoteObject, $localObject, $lastLocalObject); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
$attributes = [ |
|
162
|
|
|
'size' => null, |
|
163
|
|
|
'inode' => null, |
|
164
|
|
|
'blobId' => null, |
|
165
|
|
|
'hashes' => null, |
|
166
|
|
|
]; |
|
167
|
|
|
|
|
168
|
|
|
foreach (['permissions', 'mtime', 'linkTarget'] as $attribute) |
|
169
|
|
|
{ |
|
170
|
|
|
$modifiedRemote = $lastLocalObject[$attribute] !== $remoteObject[$attribute]; |
|
171
|
|
|
$modifiedLocal = $lastLocalObject[$attribute] !== $localObject[$attribute]; |
|
172
|
|
|
|
|
173
|
|
|
if ($modifiedRemote && $modifiedLocal) |
|
174
|
|
|
{ |
|
175
|
|
|
switch ($conflictHandler->handleConflict($remoteObject, $localObject, $lastLocalObject)) |
|
176
|
|
|
{ |
|
177
|
|
|
case ConflictHandlerInterface::USE_LOCAL: $modifiedRemote = false; break; |
|
|
|
|
|
|
178
|
|
|
case ConflictHandlerInterface::USE_REMOTE: $modifiedLocal = false; break; |
|
|
|
|
|
|
179
|
|
|
default: throw new \LogicException(); |
|
|
|
|
|
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
if ($modifiedRemote || !$modifiedLocal) |
|
184
|
|
|
{ |
|
185
|
|
|
$attributes[$attribute] = $remoteObject[$attribute]; |
|
186
|
|
|
} |
|
187
|
|
|
else |
|
188
|
|
|
{ |
|
189
|
|
|
$attributes[$attribute] = $localObject[$attribute]; |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
if ($localObject->isFile()) |
|
194
|
|
|
{ |
|
195
|
|
|
$remoteFileContentModified = $this->isRemoteFileContentModified($remoteObject, $lastLocalObject); |
|
196
|
|
|
$localFileContentModified = $this->isLocalFileContentModified($localObject, $lastLocalObject, $options); |
|
197
|
|
|
|
|
198
|
|
|
if ($remoteFileContentModified && $localFileContentModified) |
|
199
|
|
|
{ |
|
200
|
|
|
switch ($conflictHandler->handleConflict($remoteObject, $localObject, $lastLocalObject)) |
|
201
|
|
|
{ |
|
202
|
|
|
case ConflictHandlerInterface::USE_LOCAL: $remoteFileContentModified = false; break; |
|
|
|
|
|
|
203
|
|
|
case ConflictHandlerInterface::USE_REMOTE: $localFileContentModified = false; break; |
|
|
|
|
|
|
204
|
|
|
default: throw new \LogicException(); |
|
|
|
|
|
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
if ($remoteFileContentModified || !$localFileContentModified) |
|
209
|
|
|
{ |
|
210
|
|
|
$attributes['size'] = $remoteObject->getSize(); |
|
211
|
|
|
$attributes['blobId'] = $remoteObject->getBlobId(); |
|
212
|
|
|
$attributes['hashes'] = clone $remoteObject->getHashes(); |
|
213
|
|
|
} |
|
214
|
|
|
else |
|
215
|
|
|
{ |
|
216
|
|
|
$attributes['size'] = $localObject->getSize(); |
|
217
|
|
|
$attributes['inode'] = $localObject->getInode(); |
|
218
|
|
|
$attributes['hashes'] = clone $localObject->getHashes(); |
|
219
|
|
|
} |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
return new IndexObject( |
|
223
|
|
|
$localObject->getRelativePath(), |
|
224
|
|
|
$localObject->getType(), |
|
225
|
|
|
$attributes['mtime'], |
|
226
|
|
|
null, |
|
227
|
|
|
$attributes['permissions'], |
|
228
|
|
|
$attributes['size'], |
|
229
|
|
|
$attributes['inode'], |
|
230
|
|
|
$attributes['linkTarget'], |
|
231
|
|
|
$attributes['blobId'], |
|
232
|
|
|
$attributes['hashes'] |
|
233
|
|
|
); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
protected function isLocalFileContentModified(IndexObject $localObject, IndexObject $lastLocalObject, int $options): bool |
|
237
|
|
|
{ |
|
238
|
|
|
assert($localObject->isFile()); |
|
239
|
|
|
assert($lastLocalObject->isFile()); |
|
240
|
|
|
|
|
241
|
|
|
$modified = false; |
|
242
|
|
|
$modified |= !$localObject->getHashes()->equals($lastLocalObject->getHashes()); |
|
243
|
|
|
$modified |= $localObject->getSize() !== $lastLocalObject->getSize(); |
|
244
|
|
|
$modified |= $localObject->getInode() !== $lastLocalObject->getInode(); |
|
245
|
|
|
$modified |= $localObject->getMtime() !== $lastLocalObject->getMtime(); |
|
246
|
|
|
|
|
247
|
|
|
$verifyContent = false; |
|
248
|
|
|
$verifyContent |= !$modified && $localObject->getCtime() !== $lastLocalObject->getCtime(); |
|
249
|
|
|
$verifyContent |= !$modified && $options & static::VERIFY_CONTENT; |
|
250
|
|
|
|
|
251
|
|
|
if ($verifyContent) |
|
252
|
|
|
{ |
|
253
|
|
|
$existingHashes = iterator_to_array($lastLocalObject->getHashes()); |
|
254
|
|
|
$configuredAlgorithms = $this->configuration->getFileChecksums(); |
|
255
|
|
|
|
|
256
|
|
|
if (!empty($comparableAlgorithms = array_intersect($configuredAlgorithms, array_keys($existingHashes)))) |
|
257
|
|
|
{ |
|
258
|
|
|
$this->hashProvider->loadHashes($localObject, $comparableAlgorithms); |
|
259
|
|
|
|
|
260
|
|
|
foreach ($comparableAlgorithms as $algorithm) |
|
261
|
|
|
{ |
|
262
|
|
|
if ($this->hashProvider->getHash($localObject, $algorithm) !== $existingHashes[$algorithm]) |
|
263
|
|
|
{ |
|
264
|
|
|
$modified = true; |
|
265
|
|
|
} |
|
266
|
|
|
} |
|
267
|
|
|
} |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
return $modified; |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
protected function isRemoteFileContentModified(IndexObject $remoteObject, IndexObject $lastLocalObject): bool |
|
274
|
|
|
{ |
|
275
|
|
|
return $remoteObject->getBlobId() !== $lastLocalObject->getBlobId(); |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
protected function resolveConflict(ConflictHandlerInterface $conflictHandler, IndexObject $remoteObject, ?IndexObject $localObject, ?IndexObject $lastLocalObject): IndexObject |
|
279
|
|
|
{ |
|
280
|
|
|
$this->logger->notice("Resolving conflict at {$remoteObject->getRelativePath()}..."); |
|
281
|
|
|
|
|
282
|
|
|
assert(($localObject === null) || ($localObject->getRelativePath() === $remoteObject->getRelativePath())); |
|
283
|
|
|
assert(($lastLocalObject === null) || ($lastLocalObject->getRelativePath() === $remoteObject->getRelativePath())); |
|
284
|
|
|
|
|
285
|
|
|
$solution = $conflictHandler->handleConflict($remoteObject, $localObject, $lastLocalObject); |
|
286
|
|
|
|
|
287
|
|
|
$return = null; |
|
|
|
|
|
|
288
|
|
|
switch ($solution) |
|
289
|
|
|
{ |
|
290
|
|
|
case ConflictHandlerInterface::USE_LOCAL: |
|
|
|
|
|
|
291
|
|
|
|
|
292
|
|
|
$this->logger->info("Using local version for conflict at {$remoteObject->getRelativePath()}"); |
|
293
|
|
|
|
|
294
|
|
|
$return = clone $localObject; |
|
295
|
|
|
|
|
296
|
|
|
break; |
|
297
|
|
|
|
|
298
|
|
|
case ConflictHandlerInterface::USE_REMOTE: |
|
|
|
|
|
|
299
|
|
|
|
|
300
|
|
|
$this->logger->info("Using remote version for conflict at {$remoteObject->getRelativePath()}"); |
|
301
|
|
|
|
|
302
|
|
|
$return = clone $remoteObject; |
|
303
|
|
|
|
|
304
|
|
|
break; |
|
305
|
|
|
|
|
306
|
|
|
default: |
|
|
|
|
|
|
307
|
|
|
|
|
308
|
|
|
throw new \LogicException(); |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
return $return; |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
protected function injectBlobIds(Index $mergedIndex, Index $localIndex): void |
|
315
|
|
|
{ |
|
316
|
|
|
foreach ($mergedIndex as $object) |
|
317
|
|
|
{ |
|
318
|
|
|
/** @var IndexObject $object*/ |
|
319
|
|
|
|
|
320
|
|
|
if ($object->getBlobId() !== null) |
|
321
|
|
|
{ |
|
322
|
|
|
if ($localObject = $localIndex->getObjectByPath($object->getRelativePath())) |
|
|
|
|
|
|
323
|
|
|
{ |
|
324
|
|
|
$localObject->setBlobId($object->getBlobId()); |
|
325
|
|
|
} |
|
326
|
|
|
} |
|
327
|
|
|
} |
|
328
|
|
|
} |
|
329
|
|
|
|
|
330
|
|
|
public static function getOptionsDebugString(int $options): string |
|
331
|
|
|
{ |
|
332
|
|
|
$strings = []; |
|
333
|
|
|
|
|
334
|
|
|
if ($options & static::VERIFY_CONTENT) |
|
335
|
|
|
{ |
|
336
|
|
|
$strings[] = 'VERIFY_CONTENT'; |
|
337
|
|
|
} |
|
338
|
|
|
|
|
339
|
|
|
return empty($strings) ? '-' : implode(',', $strings); |
|
340
|
|
|
} |
|
341
|
|
|
} |
|
342
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: