| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
namespace Wikimedia\FileBackend; |
| 25 |
|
| 26 |
use InvalidArgumentException; |
| 27 |
use LockManager; |
| 28 |
use LogicException; |
| 29 |
use MediaWiki\Deferred\DeferredUpdates; |
| 30 |
use MediaWiki\Json\FormatJson; |
| 31 |
use Shellbox\Command\BoxedCommand; |
| 32 |
use StatusValue; |
| 33 |
use StringUtils; |
| 34 |
use Wikimedia\Timestamp\ConvertibleTimestamp; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
class FileBackendMultiWrite extends FileBackend { |
| 57 |
|
| 58 |
protected $backends = []; |
| 59 |
|
| 60 |
|
| 61 |
protected $masterIndex = -1; |
| 62 |
|
| 63 |
protected $readIndex = -1; |
| 64 |
|
| 65 |
|
| 66 |
protected $syncChecks = 0; |
| 67 |
|
| 68 |
protected $autoResync = false; |
| 69 |
|
| 70 |
|
| 71 |
protected $asyncWrites = false; |
| 72 |
|
| 73 |
|
| 74 |
private const CHECK_SIZE = 1; |
| 75 |
|
| 76 |
private const CHECK_TIME = 2; |
| 77 |
|
| 78 |
private const CHECK_SHA1 = 4; |
| 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 |
public function __construct( array $config ) { |
| 108 |
parent::__construct( $config ); |
| 109 |
$this->syncChecks = $config['syncChecks'] ?? self::CHECK_SIZE; |
| 110 |
$this->autoResync = $config['autoResync'] ?? false; |
| 111 |
$this->asyncWrites = isset( $config['replication'] ) && $config['replication'] === 'async'; |
| 112 |
|
| 113 |
|
| 114 |
$namesUsed = []; |
| 115 |
foreach ( $config['backends'] as $index => $beConfig ) { |
| 116 |
$name = $beConfig['name']; |
| 117 |
if ( isset( $namesUsed[$name] ) ) { |
| 118 |
throw new LogicException( "Two or more backends defined with the name $name." ); |
| 119 |
} |
| 120 |
$namesUsed[$name] = 1; |
| 121 |
|
| 122 |
unset( $beConfig['readOnly'] ); |
| 123 |
unset( $beConfig['lockManager'] ); |
| 124 |
$beConfig['domainId'] = $this->domainId; |
| 125 |
$beConfig['logger'] = $this->logger; |
| 126 |
if ( !empty( $beConfig['isMultiMaster'] ) ) { |
| 127 |
if ( $this->masterIndex >= 0 ) { |
| 128 |
throw new LogicException( 'More than one master backend defined.' ); |
| 129 |
} |
| 130 |
$this->masterIndex = $index; |
| 131 |
} |
| 132 |
if ( !empty( $beConfig['readAffinity'] ) ) { |
| 133 |
$this->readIndex = $index; |
| 134 |
} |
| 135 |
|
| 136 |
if ( !isset( $beConfig['class'] ) ) { |
| 137 |
throw new InvalidArgumentException( 'No class given for a backend config.' ); |
| 138 |
} |
| 139 |
$class = $beConfig['class']; |
| 140 |
$this->backends[$index] = new $class( $beConfig ); |
| 141 |
} |
| 142 |
if ( $this->masterIndex < 0 ) { |
| 143 |
throw new LogicException( 'No master backend defined.' ); |
| 144 |
} |
| 145 |
if ( $this->readIndex < 0 ) { |
| 146 |
$this->readIndex = $this->masterIndex; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
final protected function doOperationsInternal( array $ops, array $opts ) { |
| 151 |
$status = $this->newStatus(); |
| 152 |
|
| 153 |
$fname = __METHOD__; |
| 154 |
$mbe = $this->backends[$this->masterIndex]; |
| 155 |
|
| 156 |
|
| 157 |
$scopeLock = null; |
| 158 |
if ( empty( $opts['nonLocking'] ) ) { |
| 159 |
$scopeLock = $this->getScopedLocksForOps( $ops, $status ); |
| 160 |
if ( !$status->isOK() ) { |
| 161 |
return $status; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
$relevantPaths = $this->fileStoragePathsForOps( $ops ); |
| 166 |
|
| 167 |
$this->clearCache( $relevantPaths ); |
| 168 |
$opts['preserveCache'] = true; |
| 169 |
|
| 170 |
$status->merge( $this->accessibilityCheck( $relevantPaths ) ); |
| 171 |
if ( !$status->isOK() ) { |
| 172 |
return $status; |
| 173 |
} |
| 174 |
|
| 175 |
$syncStatus = $this->consistencyCheck( $relevantPaths ); |
| 176 |
if ( !$syncStatus->isOK() ) { |
| 177 |
$this->logger->error( |
| 178 |
"$fname: failed sync check: " . FormatJson::encode( $relevantPaths ) |
| 179 |
); |
| 180 |
|
| 181 |
if ( |
| 182 |
$this->autoResync === false || |
| 183 |
!$this->resyncFiles( $relevantPaths, $this->autoResync )->isOK() |
| 184 |
) { |
| 185 |
$status->merge( $syncStatus ); |
| 186 |
|
| 187 |
return $status; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
$realOps = $this->substOpBatchPaths( $ops, $mbe ); |
| 192 |
$masterStatus = $mbe->doOperations( $realOps, $opts ); |
| 193 |
$status->merge( $masterStatus ); |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
if ( $masterStatus->isOK() && $masterStatus->successCount > 0 ) { |
| 198 |
foreach ( $this->backends as $index => $backend ) { |
| 199 |
if ( $index === $this->masterIndex ) { |
| 200 |
continue; |
| 201 |
} |
| 202 |
|
| 203 |
$realOps = $this->substOpBatchPaths( $ops, $backend ); |
| 204 |
if ( $this->asyncWrites && !$this->hasVolatileSources( $ops ) ) { |
| 205 |
|
| 206 |
DeferredUpdates::addCallableUpdate( |
| 207 |
function () use ( |
| 208 |
$backend, $realOps, $opts, $scopeLock, $relevantPaths, $fname |
| 209 |
) { |
| 210 |
$this->logger->debug( |
| 211 |
"$fname: '{$backend->getName()}' async replication; paths: " . |
| 212 |
FormatJson::encode( $relevantPaths ) |
| 213 |
); |
| 214 |
$backend->doOperations( $realOps, $opts ); |
| 215 |
} |
| 216 |
); |
| 217 |
} else { |
| 218 |
$this->logger->debug( |
| 219 |
"$fname: '{$backend->getName()}' sync replication; paths: " . |
| 220 |
FormatJson::encode( $relevantPaths ) |
| 221 |
); |
| 222 |
$status->merge( $backend->doOperations( $realOps, $opts ) ); |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
$status->success = $masterStatus->success; |
| 230 |
$status->successCount = $masterStatus->successCount; |
| 231 |
$status->failCount = $masterStatus->failCount; |
| 232 |
|
| 233 |
return $status; |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
public function consistencyCheck( array $paths ) { |
| 246 |
$status = $this->newStatus(); |
| 247 |
if ( $this->syncChecks == 0 || count( $this->backends ) <= 1 ) { |
| 248 |
return $status; |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
foreach ( $this->backends as $backend ) { |
| 253 |
$realPaths = $this->substPaths( $paths, $backend ); |
| 254 |
$backend->preloadFileStat( [ 'srcs' => $realPaths, 'latest' => true ] ); |
| 255 |
} |
| 256 |
|
| 257 |
foreach ( $paths as $path ) { |
| 258 |
$params = [ 'src' => $path, 'latest' => true ]; |
| 259 |
|
| 260 |
$masterBackend = $this->backends[$this->masterIndex]; |
| 261 |
$masterParams = $this->substOpPaths( $params, $masterBackend ); |
| 262 |
$masterStat = $masterBackend->getFileStat( $masterParams ); |
| 263 |
if ( $masterStat === self::STAT_ERROR ) { |
| 264 |
$status->fatal( 'backend-fail-stat', $path ); |
| 265 |
continue; |
| 266 |
} |
| 267 |
if ( $this->syncChecks & self::CHECK_SHA1 ) { |
| 268 |
$masterSha1 = $masterBackend->getFileSha1Base36( $masterParams ); |
| 269 |
if ( ( $masterSha1 !== false ) !== (bool)$masterStat ) { |
| 270 |
$status->fatal( 'backend-fail-hash', $path ); |
| 271 |
continue; |
| 272 |
} |
| 273 |
} else { |
| 274 |
$masterSha1 = null; |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
foreach ( $this->backends as $index => $cloneBackend ) { |
| 279 |
if ( $index === $this->masterIndex ) { |
| 280 |
continue; |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
$cloneParams = $this->substOpPaths( $params, $cloneBackend ); |
| 285 |
$cloneStat = $cloneBackend->getFileStat( $cloneParams ); |
| 286 |
|
| 287 |
if ( $masterStat ) { |
| 288 |
|
| 289 |
if ( !$cloneStat ) { |
| 290 |
|
| 291 |
$status->fatal( 'backend-fail-synced', $path ); |
| 292 |
} elseif ( |
| 293 |
( $this->syncChecks & self::CHECK_SIZE ) && |
| 294 |
$cloneStat['size'] !== $masterStat['size'] |
| 295 |
) { |
| 296 |
|
| 297 |
$status->fatal( 'backend-fail-synced', $path ); |
| 298 |
} elseif ( |
| 299 |
( $this->syncChecks & self::CHECK_TIME ) && |
| 300 |
abs( |
| 301 |
(int)ConvertibleTimestamp::convert( TS_UNIX, $masterStat['mtime'] ) - |
| 302 |
(int)ConvertibleTimestamp::convert( TS_UNIX, $cloneStat['mtime'] ) |
| 303 |
) > 30 |
| 304 |
) { |
| 305 |
|
| 306 |
$status->fatal( 'backend-fail-synced', $path ); |
| 307 |
} elseif ( |
| 308 |
( $this->syncChecks & self::CHECK_SHA1 ) && |
| 309 |
$cloneBackend->getFileSha1Base36( $cloneParams ) !== $masterSha1 |
| 310 |
) { |
| 311 |
|
| 312 |
$status->fatal( 'backend-fail-synced', $path ); |
| 313 |
} |
| 314 |
} else { |
| 315 |
|
| 316 |
if ( $cloneStat ) { |
| 317 |
|
| 318 |
$status->fatal( 'backend-fail-synced', $path ); |
| 319 |
} |
| 320 |
} |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
return $status; |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
public function accessibilityCheck( array $paths ) { |
| 334 |
$status = $this->newStatus(); |
| 335 |
if ( count( $this->backends ) <= 1 ) { |
| 336 |
return $status; |
| 337 |
} |
| 338 |
|
| 339 |
foreach ( $paths as $path ) { |
| 340 |
foreach ( $this->backends as $backend ) { |
| 341 |
$realPath = $this->substPaths( $path, $backend ); |
| 342 |
if ( !$backend->isPathUsableInternal( $realPath ) ) { |
| 343 |
$status->fatal( 'backend-fail-usable', $path ); |
| 344 |
} |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
return $status; |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
public function resyncFiles( array $paths, $resyncMode = true ) { |
| 362 |
$status = $this->newStatus(); |
| 363 |
|
| 364 |
$fname = __METHOD__; |
| 365 |
foreach ( $paths as $path ) { |
| 366 |
$params = [ 'src' => $path, 'latest' => true ]; |
| 367 |
|
| 368 |
$masterBackend = $this->backends[$this->masterIndex]; |
| 369 |
$masterParams = $this->substOpPaths( $params, $masterBackend ); |
| 370 |
$masterPath = $masterParams['src']; |
| 371 |
$masterStat = $masterBackend->getFileStat( $masterParams ); |
| 372 |
if ( $masterStat === self::STAT_ERROR ) { |
| 373 |
$status->fatal( 'backend-fail-stat', $path ); |
| 374 |
$this->logger->error( "$fname: file '$masterPath' is not available" ); |
| 375 |
continue; |
| 376 |
} |
| 377 |
$masterSha1 = $masterBackend->getFileSha1Base36( $masterParams ); |
| 378 |
if ( ( $masterSha1 !== false ) !== (bool)$masterStat ) { |
| 379 |
$status->fatal( 'backend-fail-hash', $path ); |
| 380 |
$this->logger->error( "$fname: file '$masterPath' hash does not match stat" ); |
| 381 |
continue; |
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
foreach ( $this->backends as $index => $cloneBackend ) { |
| 386 |
if ( $index === $this->masterIndex ) { |
| 387 |
continue; |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
$cloneParams = $this->substOpPaths( $params, $cloneBackend ); |
| 392 |
$clonePath = $cloneParams['src']; |
| 393 |
$cloneStat = $cloneBackend->getFileStat( $cloneParams ); |
| 394 |
if ( $cloneStat === self::STAT_ERROR ) { |
| 395 |
$status->fatal( 'backend-fail-stat', $path ); |
| 396 |
$this->logger->error( "$fname: file '$clonePath' is not available" ); |
| 397 |
continue; |
| 398 |
} |
| 399 |
$cloneSha1 = $cloneBackend->getFileSha1Base36( $cloneParams ); |
| 400 |
if ( ( $cloneSha1 !== false ) !== (bool)$cloneStat ) { |
| 401 |
$status->fatal( 'backend-fail-hash', $path ); |
| 402 |
$this->logger->error( "$fname: file '$clonePath' hash does not match stat" ); |
| 403 |
continue; |
| 404 |
} |
| 405 |
|
| 406 |
if ( $masterSha1 === $cloneSha1 ) { |
| 407 |
|
| 408 |
$this->logger->debug( "$fname: file '$clonePath' matches '$masterPath'" ); |
| 409 |
} elseif ( $masterSha1 !== false ) { |
| 410 |
|
| 411 |
if ( |
| 412 |
$resyncMode === 'conservative' && |
| 413 |
$cloneStat && |
| 414 |
|
| 415 |
$cloneStat['mtime'] > $masterStat['mtime'] |
| 416 |
) { |
| 417 |
|
| 418 |
$status->fatal( 'backend-fail-synced', $path ); |
| 419 |
} else { |
| 420 |
|
| 421 |
$fsFile = $masterBackend->getLocalReference( $masterParams ); |
| 422 |
$status->merge( $cloneBackend->quickStore( [ |
| 423 |
'src' => $fsFile, |
| 424 |
'dst' => $clonePath |
| 425 |
] ) ); |
| 426 |
} |
| 427 |
} elseif ( $masterStat === false ) { |
| 428 |
|
| 429 |
if ( $resyncMode === 'conservative' ) { |
| 430 |
|
| 431 |
$status->fatal( 'backend-fail-synced', $path ); |
| 432 |
$this->logger->error( "$fname: not allowed to delete file '$clonePath'" ); |
| 433 |
} else { |
| 434 |
|
| 435 |
$status->merge( $cloneBackend->quickDelete( [ 'src' => $clonePath ] ) ); |
| 436 |
} |
| 437 |
} |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
if ( !$status->isOK() ) { |
| 442 |
$this->logger->error( "$fname: failed to resync: " . FormatJson::encode( $paths ) ); |
| 443 |
} |
| 444 |
|
| 445 |
return $status; |
| 446 |
} |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
protected function fileStoragePathsForOps( array $ops ) { |
| 455 |
$paths = []; |
| 456 |
foreach ( $ops as $op ) { |
| 457 |
if ( isset( $op['src'] ) ) { |
| 458 |
|
| 459 |
|
| 460 |
if ( empty( $op['ignoreMissingSource'] ) |
| 461 |
|| $this->fileExists( [ 'src' => $op['src'] ] ) |
| 462 |
) { |
| 463 |
$paths[] = $op['src']; |
| 464 |
} |
| 465 |
} |
| 466 |
if ( isset( $op['srcs'] ) ) { |
| 467 |
$paths = array_merge( $paths, $op['srcs'] ); |
| 468 |
} |
| 469 |
if ( isset( $op['dst'] ) ) { |
| 470 |
$paths[] = $op['dst']; |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
return array_values( array_unique( array_filter( $paths, [ FileBackend::class, 'isStoragePath' ] ) ) ); |
| 475 |
} |
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
protected function substOpBatchPaths( array $ops, FileBackendStore $backend ) { |
| 486 |
$newOps = []; |
| 487 |
foreach ( $ops as $op ) { |
| 488 |
$newOp = $op; |
| 489 |
foreach ( [ 'src', 'srcs', 'dst', 'dir' ] as $par ) { |
| 490 |
if ( isset( $newOp[$par] ) ) { |
| 491 |
$newOp[$par] = $this->substPaths( $newOp[$par], $backend ); |
| 492 |
} |
| 493 |
} |
| 494 |
$newOps[] = $newOp; |
| 495 |
} |
| 496 |
|
| 497 |
return $newOps; |
| 498 |
} |
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
protected function substOpPaths( array $ops, FileBackendStore $backend ) { |
| 508 |
$newOps = $this->substOpBatchPaths( [ $ops ], $backend ); |
| 509 |
|
| 510 |
return $newOps[0]; |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
protected function substPaths( $paths, FileBackendStore $backend ) { |
| 521 |
return preg_replace( |
| 522 |
'!^mwstore://' . preg_quote( $this->name, '!' ) . '/!', |
| 523 |
StringUtils::escapeRegexReplacement( "mwstore://{$backend->getName()}/" ), |
| 524 |
$paths |
| 525 |
); |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
protected function unsubstPaths( $paths, FileBackendStore $backend ) { |
| 536 |
return preg_replace( |
| 537 |
'!^mwstore://' . preg_quote( $backend->getName(), '!' ) . '/!', |
| 538 |
StringUtils::escapeRegexReplacement( "mwstore://{$this->name}/" ), |
| 539 |
$paths |
| 540 |
); |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
protected function hasVolatileSources( array $ops ) { |
| 548 |
foreach ( $ops as $op ) { |
| 549 |
if ( $op['op'] === 'store' && !isset( $op['srcRef'] ) ) { |
| 550 |
return true; |
| 551 |
} |
| 552 |
} |
| 553 |
|
| 554 |
return false; |
| 555 |
} |
| 556 |
|
| 557 |
protected function doQuickOperationsInternal( array $ops, array $opts ) { |
| 558 |
$status = $this->newStatus(); |
| 559 |
|
| 560 |
$realOps = $this->substOpBatchPaths( $ops, $this->backends[$this->masterIndex] ); |
| 561 |
$masterStatus = $this->backends[$this->masterIndex]->doQuickOperations( $realOps ); |
| 562 |
$status->merge( $masterStatus ); |
| 563 |
|
| 564 |
foreach ( $this->backends as $index => $backend ) { |
| 565 |
if ( $index === $this->masterIndex ) { |
| 566 |
continue; |
| 567 |
} |
| 568 |
|
| 569 |
$realOps = $this->substOpBatchPaths( $ops, $backend ); |
| 570 |
if ( $this->asyncWrites && !$this->hasVolatileSources( $ops ) ) { |
| 571 |
DeferredUpdates::addCallableUpdate( |
| 572 |
static function () use ( $backend, $realOps ) { |
| 573 |
$backend->doQuickOperations( $realOps ); |
| 574 |
} |
| 575 |
); |
| 576 |
} else { |
| 577 |
$status->merge( $backend->doQuickOperations( $realOps ) ); |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
$status->success = $masterStatus->success; |
| 584 |
$status->successCount = $masterStatus->successCount; |
| 585 |
$status->failCount = $masterStatus->failCount; |
| 586 |
|
| 587 |
return $status; |
| 588 |
} |
| 589 |
|
| 590 |
protected function doPrepare( array $params ) { |
| 591 |
return $this->doDirectoryOp( 'prepare', $params ); |
| 592 |
} |
| 593 |
|
| 594 |
protected function doSecure( array $params ) { |
| 595 |
return $this->doDirectoryOp( 'secure', $params ); |
| 596 |
} |
| 597 |
|
| 598 |
protected function doPublish( array $params ) { |
| 599 |
return $this->doDirectoryOp( 'publish', $params ); |
| 600 |
} |
| 601 |
|
| 602 |
protected function doClean( array $params ) { |
| 603 |
return $this->doDirectoryOp( 'clean', $params ); |
| 604 |
} |
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
protected function doDirectoryOp( $method, array $params ) { |
| 612 |
$status = $this->newStatus(); |
| 613 |
|
| 614 |
$realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] ); |
| 615 |
$masterStatus = $this->backends[$this->masterIndex]->$method( $realParams ); |
| 616 |
$status->merge( $masterStatus ); |
| 617 |
|
| 618 |
foreach ( $this->backends as $index => $backend ) { |
| 619 |
if ( $index === $this->masterIndex ) { |
| 620 |
continue; |
| 621 |
} |
| 622 |
|
| 623 |
$realParams = $this->substOpPaths( $params, $backend ); |
| 624 |
if ( $this->asyncWrites ) { |
| 625 |
DeferredUpdates::addCallableUpdate( |
| 626 |
static function () use ( $backend, $method, $realParams ) { |
| 627 |
$backend->$method( $realParams ); |
| 628 |
} |
| 629 |
); |
| 630 |
} else { |
| 631 |
$status->merge( $backend->$method( $realParams ) ); |
| 632 |
} |
| 633 |
} |
| 634 |
|
| 635 |
return $status; |
| 636 |
} |
| 637 |
|
| 638 |
public function concatenate( array $params ) { |
| 639 |
$status = $this->newStatus(); |
| 640 |
|
| 641 |
$index = $this->getReadIndexFromParams( $params ); |
| 642 |
$realParams = $this->substOpPaths( $params, $this->backends[$index] ); |
| 643 |
|
| 644 |
$status->merge( $this->backends[$index]->concatenate( $realParams ) ); |
| 645 |
|
| 646 |
return $status; |
| 647 |
} |
| 648 |
|
| 649 |
public function fileExists( array $params ) { |
| 650 |
$index = $this->getReadIndexFromParams( $params ); |
| 651 |
$realParams = $this->substOpPaths( $params, $this->backends[$index] ); |
| 652 |
|
| 653 |
return $this->backends[$index]->fileExists( $realParams ); |
| 654 |
} |
| 655 |
|
| 656 |
public function getFileTimestamp( array $params ) { |
| 657 |
$index = $this->getReadIndexFromParams( $params ); |
| 658 |
$realParams = $this->substOpPaths( $params, $this->backends[$index] ); |
| 659 |
|
| 660 |
return $this->backends[$index]->getFileTimestamp( $realParams ); |
| 661 |
} |
| 662 |
|
| 663 |
public function getFileSize( array $params ) { |
| 664 |
$index = $this->getReadIndexFromParams( $params ); |
| 665 |
$realParams = $this->substOpPaths( $params, $this->backends[$index] ); |
| 666 |
|
| 667 |
return $this->backends[$index]->getFileSize( $realParams ); |
| 668 |
} |
| 669 |
|
| 670 |
public function getFileStat( array $params ) { |
| 671 |
$index = $this->getReadIndexFromParams( $params ); |
| 672 |
$realParams = $this->substOpPaths( $params, $this->backends[$index] ); |
| 673 |
|
| 674 |
return $this->backends[$index]->getFileStat( $realParams ); |
| 675 |
} |
| 676 |
|
| 677 |
public function getFileXAttributes( array $params ) { |
| 678 |
$index = $this->getReadIndexFromParams( $params ); |
| 679 |
$realParams = $this->substOpPaths( $params, $this->backends[$index] ); |
| 680 |
|
| 681 |
return $this->backends[$index]->getFileXAttributes( $realParams ); |
| 682 |
} |
| 683 |
|
| 684 |
public function getFileContentsMulti( array $params ) { |
| 685 |
$index = $this->getReadIndexFromParams( $params ); |
| 686 |
$realParams = $this->substOpPaths( $params, $this->backends[$index] ); |
| 687 |
|
| 688 |
$contentsM = $this->backends[$index]->getFileContentsMulti( $realParams ); |
| 689 |
|
| 690 |
$contents = []; |
| 691 |
foreach ( $contentsM as $path => $data ) { |
| 692 |
$contents[$this->unsubstPaths( $path, $this->backends[$index] )] = $data; |
| 693 |
} |
| 694 |
|
| 695 |
return $contents; |
| 696 |
} |
| 697 |
|
| 698 |
public function getFileSha1Base36( array $params ) { |
| 699 |
$index = $this->getReadIndexFromParams( $params ); |
| 700 |
$realParams = $this->substOpPaths( $params, $this->backends[$index] ); |
| 701 |
|
| 702 |
return $this->backends[$index]->getFileSha1Base36( $realParams ); |
| 703 |
} |
| 704 |
|
| 705 |
public function getFileProps( array $params ) { |
| 706 |
$index = $this->getReadIndexFromParams( $params ); |
| 707 |
$realParams = $this->substOpPaths( $params, $this->backends[$index] ); |
| 708 |
|
| 709 |
return $this->backends[$index]->getFileProps( $realParams ); |
| 710 |
} |
| 711 |
|
| 712 |
public function streamFile( array $params ) { |
| 713 |
$index = $this->getReadIndexFromParams( $params ); |
| 714 |
$realParams = $this->substOpPaths( $params, $this->backends[$index] ); |
| 715 |
|
| 716 |
return $this->backends[$index]->streamFile( $realParams ); |
| 717 |
} |
| 718 |
|
| 719 |
public function getLocalReferenceMulti( array $params ) { |
| 720 |
$index = $this->getReadIndexFromParams( $params ); |
| 721 |
$realParams = $this->substOpPaths( $params, $this->backends[$index] ); |
| 722 |
|
| 723 |
$fsFilesM = $this->backends[$index]->getLocalReferenceMulti( $realParams ); |
| 724 |
|
| 725 |
$fsFiles = []; |
| 726 |
foreach ( $fsFilesM as $path => $fsFile ) { |
| 727 |
$fsFiles[$this->unsubstPaths( $path, $this->backends[$index] )] = $fsFile; |
| 728 |
} |
| 729 |
|
| 730 |
return $fsFiles; |
| 731 |
} |
| 732 |
|
| 733 |
public function getLocalCopyMulti( array $params ) { |
| 734 |
$index = $this->getReadIndexFromParams( $params ); |
| 735 |
$realParams = $this->substOpPaths( $params, $this->backends[$index] ); |
| 736 |
|
| 737 |
$tempFilesM = $this->backends[$index]->getLocalCopyMulti( $realParams ); |
| 738 |
|
| 739 |
$tempFiles = []; |
| 740 |
foreach ( $tempFilesM as $path => $tempFile ) { |
| 741 |
$tempFiles[$this->unsubstPaths( $path, $this->backends[$index] )] = $tempFile; |
| 742 |
} |
| 743 |
|
| 744 |
return $tempFiles; |
| 745 |
} |
| 746 |
|
| 747 |
public function getFileHttpUrl( array $params ) { |
| 748 |
$index = $this->getReadIndexFromParams( $params ); |
| 749 |
$realParams = $this->substOpPaths( $params, $this->backends[$index] ); |
| 750 |
|
| 751 |
return $this->backends[$index]->getFileHttpUrl( $realParams ); |
| 752 |
} |
| 753 |
|
| 754 |
public function addShellboxInputFile( BoxedCommand $command, string $boxedName, |
| 755 |
array $params |
| 756 |
) { |
| 757 |
$index = $this->getReadIndexFromParams( $params ); |
| 758 |
$realParams = $this->substOpPaths( $params, $this->backends[$index] ); |
| 759 |
return $this->backends[$index]->addShellboxInputFile( $command, $boxedName, $realParams ); |
| 760 |
} |
| 761 |
|
| 762 |
public function directoryExists( array $params ) { |
| 763 |
$realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] ); |
| 764 |
|
| 765 |
return $this->backends[$this->masterIndex]->directoryExists( $realParams ); |
| 766 |
} |
| 767 |
|
| 768 |
public function getDirectoryList( array $params ) { |
| 769 |
$realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] ); |
| 770 |
|
| 771 |
return $this->backends[$this->masterIndex]->getDirectoryList( $realParams ); |
| 772 |
} |
| 773 |
|
| 774 |
public function getFileList( array $params ) { |
| 775 |
if ( isset( $params['forWrite'] ) && $params['forWrite'] ) { |
| 776 |
return $this->getFileListForWrite( $params ); |
| 777 |
} |
| 778 |
|
| 779 |
$realParams = $this->substOpPaths( $params, $this->backends[$this->masterIndex] ); |
| 780 |
return $this->backends[$this->masterIndex]->getFileList( $realParams ); |
| 781 |
} |
| 782 |
|
| 783 |
private function getFileListForWrite( $params ) { |
| 784 |
$files = []; |
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
foreach ( $this->backends as $backend ) { |
| 789 |
$realParams = $this->substOpPaths( $params, $backend ); |
| 790 |
$iterator = $backend->getFileList( $realParams ); |
| 791 |
if ( $iterator !== null ) { |
| 792 |
foreach ( $iterator as $file ) { |
| 793 |
$files[] = $file; |
| 794 |
} |
| 795 |
} |
| 796 |
} |
| 797 |
|
| 798 |
return array_unique( $files ); |
| 799 |
} |
| 800 |
|
| 801 |
public function getFeatures() { |
| 802 |
return $this->backends[$this->masterIndex]->getFeatures(); |
| 803 |
} |
| 804 |
|
| 805 |
public function clearCache( ?array $paths = null ) { |
| 806 |
foreach ( $this->backends as $backend ) { |
| 807 |
$realPaths = is_array( $paths ) ? $this->substPaths( $paths, $backend ) : null; |
| 808 |
$backend->clearCache( $realPaths ); |
| 809 |
} |
| 810 |
} |
| 811 |
|
| 812 |
public function preloadCache( array $paths ) { |
| 813 |
$realPaths = $this->substPaths( $paths, $this->backends[$this->readIndex] ); |
| 814 |
$this->backends[$this->readIndex]->preloadCache( $realPaths ); |
| 815 |
} |
| 816 |
|
| 817 |
public function preloadFileStat( array $params ) { |
| 818 |
$index = $this->getReadIndexFromParams( $params ); |
| 819 |
$realParams = $this->substOpPaths( $params, $this->backends[$index] ); |
| 820 |
|
| 821 |
return $this->backends[$index]->preloadFileStat( $realParams ); |
| 822 |
} |
| 823 |
|
| 824 |
public function getScopedLocksForOps( array $ops, StatusValue $status ) { |
| 825 |
$realOps = $this->substOpBatchPaths( $ops, $this->backends[$this->masterIndex] ); |
| 826 |
$fileOps = $this->backends[$this->masterIndex]->getOperationsInternal( $realOps ); |
| 827 |
|
| 828 |
$paths = $this->backends[$this->masterIndex]->getPathsToLockForOpsInternal( $fileOps ); |
| 829 |
|
| 830 |
$pbPaths = [ |
| 831 |
LockManager::LOCK_UW => $this->unsubstPaths( |
| 832 |
$paths[LockManager::LOCK_UW], |
| 833 |
$this->backends[$this->masterIndex] |
| 834 |
), |
| 835 |
LockManager::LOCK_EX => $this->unsubstPaths( |
| 836 |
$paths[LockManager::LOCK_EX], |
| 837 |
$this->backends[$this->masterIndex] |
| 838 |
) |
| 839 |
]; |
| 840 |
|
| 841 |
|
| 842 |
return $this->getScopedFileLocks( $pbPaths, 'mixed', $status ); |
| 843 |
} |
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
protected function getReadIndexFromParams( array $params ) { |
| 850 |
return !empty( $params['latest'] ) ? $this->masterIndex : $this->readIndex; |
| 851 |
} |
| 852 |
} |
| 853 |
|
| 854 |
|
| 855 |
class_alias( FileBackendMultiWrite::class, 'FileBackendMultiWrite' ); |