5179477e28c8a3c64abd8b16308271f7e7a4b9bf
[lhc/web/wiklou.git] / includes / libs / filebackend / FileBackendStore.php
1 <?php
2 /**
3 * Base class for all backends using particular storage medium.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup FileBackend
22 * @author Aaron Schulz
23 */
24 use Wikimedia\Timestamp\ConvertibleTimestamp;
25
26 /**
27 * @brief Base class for all backends using particular storage medium.
28 *
29 * This class defines the methods as abstract that subclasses must implement.
30 * Outside callers should *not* use functions with "Internal" in the name.
31 *
32 * The FileBackend operations are implemented using basic functions
33 * such as storeInternal(), copyInternal(), deleteInternal() and the like.
34 * This class is also responsible for path resolution and sanitization.
35 *
36 * @ingroup FileBackend
37 * @since 1.19
38 */
39 abstract class FileBackendStore extends FileBackend {
40 /** @var WANObjectCache */
41 protected $memCache;
42 /** @var BagOStuff */
43 protected $srvCache;
44 /** @var ProcessCacheLRU Map of paths to small (RAM/disk) cache items */
45 protected $cheapCache;
46 /** @var ProcessCacheLRU Map of paths to large (RAM/disk) cache items */
47 protected $expensiveCache;
48
49 /** @var array Map of container names to sharding config */
50 protected $shardViaHashLevels = [];
51
52 /** @var callable Method to get the MIME type of files */
53 protected $mimeCallback;
54
55 protected $maxFileSize = 4294967296; // integer bytes (4GiB)
56
57 const CACHE_TTL = 10; // integer; TTL in seconds for process cache entries
58 const CACHE_CHEAP_SIZE = 500; // integer; max entries in "cheap cache"
59 const CACHE_EXPENSIVE_SIZE = 5; // integer; max entries in "expensive cache"
60
61 /**
62 * @see FileBackend::__construct()
63 * Additional $config params include:
64 * - srvCache : BagOStuff cache to APC/XCache or the like.
65 * - wanCache : WANObjectCache object to use for persistent caching.
66 * - mimeCallback : Callback that takes (storage path, content, file system path) and
67 * returns the MIME type of the file or 'unknown/unknown'. The file
68 * system path parameter should be used if the content one is null.
69 *
70 * @param array $config
71 */
72 public function __construct( array $config ) {
73 parent::__construct( $config );
74 $this->mimeCallback = isset( $config['mimeCallback'] )
75 ? $config['mimeCallback']
76 : null;
77 $this->srvCache = new EmptyBagOStuff(); // disabled by default
78 $this->memCache = WANObjectCache::newEmpty(); // disabled by default
79 $this->cheapCache = new ProcessCacheLRU( self::CACHE_CHEAP_SIZE );
80 $this->expensiveCache = new ProcessCacheLRU( self::CACHE_EXPENSIVE_SIZE );
81 }
82
83 /**
84 * Get the maximum allowable file size given backend
85 * medium restrictions and basic performance constraints.
86 * Do not call this function from places outside FileBackend and FileOp.
87 *
88 * @return int Bytes
89 */
90 final public function maxFileSizeInternal() {
91 return $this->maxFileSize;
92 }
93
94 /**
95 * Check if a file can be created or changed at a given storage path.
96 * FS backends should check if the parent directory exists, files can be
97 * written under it, and that any file already there is writable.
98 * Backends using key/value stores should check if the container exists.
99 *
100 * @param string $storagePath
101 * @return bool
102 */
103 abstract public function isPathUsableInternal( $storagePath );
104
105 /**
106 * Create a file in the backend with the given contents.
107 * This will overwrite any file that exists at the destination.
108 * Do not call this function from places outside FileBackend and FileOp.
109 *
110 * $params include:
111 * - content : the raw file contents
112 * - dst : destination storage path
113 * - headers : HTTP header name/value map
114 * - async : StatusValue will be returned immediately if supported.
115 * If the StatusValue is OK, then its value field will be
116 * set to a FileBackendStoreOpHandle object.
117 * - dstExists : Whether a file exists at the destination (optimization).
118 * Callers can use "false" if no existing file is being changed.
119 *
120 * @param array $params
121 * @return StatusValue
122 */
123 final public function createInternal( array $params ) {
124 $ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
125 if ( strlen( $params['content'] ) > $this->maxFileSizeInternal() ) {
126 $status = $this->newStatus( 'backend-fail-maxsize',
127 $params['dst'], $this->maxFileSizeInternal() );
128 } else {
129 $status = $this->doCreateInternal( $params );
130 $this->clearCache( [ $params['dst'] ] );
131 if ( !isset( $params['dstExists'] ) || $params['dstExists'] ) {
132 $this->deleteFileCache( $params['dst'] ); // persistent cache
133 }
134 }
135
136 return $status;
137 }
138
139 /**
140 * @see FileBackendStore::createInternal()
141 * @param array $params
142 * @return StatusValue
143 */
144 abstract protected function doCreateInternal( array $params );
145
146 /**
147 * Store a file into the backend from a file on disk.
148 * This will overwrite any file that exists at the destination.
149 * Do not call this function from places outside FileBackend and FileOp.
150 *
151 * $params include:
152 * - src : source path on disk
153 * - dst : destination storage path
154 * - headers : HTTP header name/value map
155 * - async : StatusValue will be returned immediately if supported.
156 * If the StatusValue is OK, then its value field will be
157 * set to a FileBackendStoreOpHandle object.
158 * - dstExists : Whether a file exists at the destination (optimization).
159 * Callers can use "false" if no existing file is being changed.
160 *
161 * @param array $params
162 * @return StatusValue
163 */
164 final public function storeInternal( array $params ) {
165 $ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
166 if ( filesize( $params['src'] ) > $this->maxFileSizeInternal() ) {
167 $status = $this->newStatus( 'backend-fail-maxsize',
168 $params['dst'], $this->maxFileSizeInternal() );
169 } else {
170 $status = $this->doStoreInternal( $params );
171 $this->clearCache( [ $params['dst'] ] );
172 if ( !isset( $params['dstExists'] ) || $params['dstExists'] ) {
173 $this->deleteFileCache( $params['dst'] ); // persistent cache
174 }
175 }
176
177 return $status;
178 }
179
180 /**
181 * @see FileBackendStore::storeInternal()
182 * @param array $params
183 * @return StatusValue
184 */
185 abstract protected function doStoreInternal( array $params );
186
187 /**
188 * Copy a file from one storage path to another in the backend.
189 * This will overwrite any file that exists at the destination.
190 * Do not call this function from places outside FileBackend and FileOp.
191 *
192 * $params include:
193 * - src : source storage path
194 * - dst : destination storage path
195 * - ignoreMissingSource : do nothing if the source file does not exist
196 * - headers : HTTP header name/value map
197 * - async : StatusValue will be returned immediately if supported.
198 * If the StatusValue is OK, then its value field will be
199 * set to a FileBackendStoreOpHandle object.
200 * - dstExists : Whether a file exists at the destination (optimization).
201 * Callers can use "false" if no existing file is being changed.
202 *
203 * @param array $params
204 * @return StatusValue
205 */
206 final public function copyInternal( array $params ) {
207 $ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
208 $status = $this->doCopyInternal( $params );
209 $this->clearCache( [ $params['dst'] ] );
210 if ( !isset( $params['dstExists'] ) || $params['dstExists'] ) {
211 $this->deleteFileCache( $params['dst'] ); // persistent cache
212 }
213
214 return $status;
215 }
216
217 /**
218 * @see FileBackendStore::copyInternal()
219 * @param array $params
220 * @return StatusValue
221 */
222 abstract protected function doCopyInternal( array $params );
223
224 /**
225 * Delete a file at the storage path.
226 * Do not call this function from places outside FileBackend and FileOp.
227 *
228 * $params include:
229 * - src : source storage path
230 * - ignoreMissingSource : do nothing if the source file does not exist
231 * - async : StatusValue will be returned immediately if supported.
232 * If the StatusValue is OK, then its value field will be
233 * set to a FileBackendStoreOpHandle object.
234 *
235 * @param array $params
236 * @return StatusValue
237 */
238 final public function deleteInternal( array $params ) {
239 $ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
240 $status = $this->doDeleteInternal( $params );
241 $this->clearCache( [ $params['src'] ] );
242 $this->deleteFileCache( $params['src'] ); // persistent cache
243 return $status;
244 }
245
246 /**
247 * @see FileBackendStore::deleteInternal()
248 * @param array $params
249 * @return StatusValue
250 */
251 abstract protected function doDeleteInternal( array $params );
252
253 /**
254 * Move a file from one storage path to another in the backend.
255 * This will overwrite any file that exists at the destination.
256 * Do not call this function from places outside FileBackend and FileOp.
257 *
258 * $params include:
259 * - src : source storage path
260 * - dst : destination storage path
261 * - ignoreMissingSource : do nothing if the source file does not exist
262 * - headers : HTTP header name/value map
263 * - async : StatusValue will be returned immediately if supported.
264 * If the StatusValue is OK, then its value field will be
265 * set to a FileBackendStoreOpHandle object.
266 * - dstExists : Whether a file exists at the destination (optimization).
267 * Callers can use "false" if no existing file is being changed.
268 *
269 * @param array $params
270 * @return StatusValue
271 */
272 final public function moveInternal( array $params ) {
273 $ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
274 $status = $this->doMoveInternal( $params );
275 $this->clearCache( [ $params['src'], $params['dst'] ] );
276 $this->deleteFileCache( $params['src'] ); // persistent cache
277 if ( !isset( $params['dstExists'] ) || $params['dstExists'] ) {
278 $this->deleteFileCache( $params['dst'] ); // persistent cache
279 }
280
281 return $status;
282 }
283
284 /**
285 * @see FileBackendStore::moveInternal()
286 * @param array $params
287 * @return StatusValue
288 */
289 protected function doMoveInternal( array $params ) {
290 unset( $params['async'] ); // two steps, won't work here :)
291 $nsrc = FileBackend::normalizeStoragePath( $params['src'] );
292 $ndst = FileBackend::normalizeStoragePath( $params['dst'] );
293 // Copy source to dest
294 $status = $this->copyInternal( $params );
295 if ( $nsrc !== $ndst && $status->isOK() ) {
296 // Delete source (only fails due to races or network problems)
297 $status->merge( $this->deleteInternal( [ 'src' => $params['src'] ] ) );
298 $status->setResult( true, $status->value ); // ignore delete() errors
299 }
300
301 return $status;
302 }
303
304 /**
305 * Alter metadata for a file at the storage path.
306 * Do not call this function from places outside FileBackend and FileOp.
307 *
308 * $params include:
309 * - src : source storage path
310 * - headers : HTTP header name/value map
311 * - async : StatusValue will be returned immediately if supported.
312 * If the StatusValue is OK, then its value field will be
313 * set to a FileBackendStoreOpHandle object.
314 *
315 * @param array $params
316 * @return StatusValue
317 */
318 final public function describeInternal( array $params ) {
319 $ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
320 if ( count( $params['headers'] ) ) {
321 $status = $this->doDescribeInternal( $params );
322 $this->clearCache( [ $params['src'] ] );
323 $this->deleteFileCache( $params['src'] ); // persistent cache
324 } else {
325 $status = $this->newStatus(); // nothing to do
326 }
327
328 return $status;
329 }
330
331 /**
332 * @see FileBackendStore::describeInternal()
333 * @param array $params
334 * @return StatusValue
335 */
336 protected function doDescribeInternal( array $params ) {
337 return $this->newStatus();
338 }
339
340 /**
341 * No-op file operation that does nothing.
342 * Do not call this function from places outside FileBackend and FileOp.
343 *
344 * @param array $params
345 * @return StatusValue
346 */
347 final public function nullInternal( array $params ) {
348 return $this->newStatus();
349 }
350
351 final public function concatenate( array $params ) {
352 $ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
353 $status = $this->newStatus();
354
355 // Try to lock the source files for the scope of this function
356 $scopeLockS = $this->getScopedFileLocks( $params['srcs'], LockManager::LOCK_UW, $status );
357 if ( $status->isOK() ) {
358 // Actually do the file concatenation...
359 $start_time = microtime( true );
360 $status->merge( $this->doConcatenate( $params ) );
361 $sec = microtime( true ) - $start_time;
362 if ( !$status->isOK() ) {
363 $this->logger->error( get_class( $this ) . "-{$this->name}" .
364 " failed to concatenate " . count( $params['srcs'] ) . " file(s) [$sec sec]" );
365 }
366 }
367
368 return $status;
369 }
370
371 /**
372 * @see FileBackendStore::concatenate()
373 * @param array $params
374 * @return StatusValue
375 */
376 protected function doConcatenate( array $params ) {
377 $status = $this->newStatus();
378 $tmpPath = $params['dst']; // convenience
379 unset( $params['latest'] ); // sanity
380
381 // Check that the specified temp file is valid...
382 MediaWiki\suppressWarnings();
383 $ok = ( is_file( $tmpPath ) && filesize( $tmpPath ) == 0 );
384 MediaWiki\restoreWarnings();
385 if ( !$ok ) { // not present or not empty
386 $status->fatal( 'backend-fail-opentemp', $tmpPath );
387
388 return $status;
389 }
390
391 // Get local FS versions of the chunks needed for the concatenation...
392 $fsFiles = $this->getLocalReferenceMulti( $params );
393 foreach ( $fsFiles as $path => &$fsFile ) {
394 if ( !$fsFile ) { // chunk failed to download?
395 $fsFile = $this->getLocalReference( [ 'src' => $path ] );
396 if ( !$fsFile ) { // retry failed?
397 $status->fatal( 'backend-fail-read', $path );
398
399 return $status;
400 }
401 }
402 }
403 unset( $fsFile ); // unset reference so we can reuse $fsFile
404
405 // Get a handle for the destination temp file
406 $tmpHandle = fopen( $tmpPath, 'ab' );
407 if ( $tmpHandle === false ) {
408 $status->fatal( 'backend-fail-opentemp', $tmpPath );
409
410 return $status;
411 }
412
413 // Build up the temp file using the source chunks (in order)...
414 foreach ( $fsFiles as $virtualSource => $fsFile ) {
415 // Get a handle to the local FS version
416 $sourceHandle = fopen( $fsFile->getPath(), 'rb' );
417 if ( $sourceHandle === false ) {
418 fclose( $tmpHandle );
419 $status->fatal( 'backend-fail-read', $virtualSource );
420
421 return $status;
422 }
423 // Append chunk to file (pass chunk size to avoid magic quotes)
424 if ( !stream_copy_to_stream( $sourceHandle, $tmpHandle ) ) {
425 fclose( $sourceHandle );
426 fclose( $tmpHandle );
427 $status->fatal( 'backend-fail-writetemp', $tmpPath );
428
429 return $status;
430 }
431 fclose( $sourceHandle );
432 }
433 if ( !fclose( $tmpHandle ) ) {
434 $status->fatal( 'backend-fail-closetemp', $tmpPath );
435
436 return $status;
437 }
438
439 clearstatcache(); // temp file changed
440
441 return $status;
442 }
443
444 final protected function doPrepare( array $params ) {
445 $ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
446 $status = $this->newStatus();
447
448 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
449 if ( $dir === null ) {
450 $status->fatal( 'backend-fail-invalidpath', $params['dir'] );
451
452 return $status; // invalid storage path
453 }
454
455 if ( $shard !== null ) { // confined to a single container/shard
456 $status->merge( $this->doPrepareInternal( $fullCont, $dir, $params ) );
457 } else { // directory is on several shards
458 $this->logger->debug( __METHOD__ . ": iterating over all container shards.\n" );
459 list( , $shortCont, ) = self::splitStoragePath( $params['dir'] );
460 foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
461 $status->merge( $this->doPrepareInternal( "{$fullCont}{$suffix}", $dir, $params ) );
462 }
463 }
464
465 return $status;
466 }
467
468 /**
469 * @see FileBackendStore::doPrepare()
470 * @param string $container
471 * @param string $dir
472 * @param array $params
473 * @return StatusValue
474 */
475 protected function doPrepareInternal( $container, $dir, array $params ) {
476 return $this->newStatus();
477 }
478
479 final protected function doSecure( array $params ) {
480 $ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
481 $status = $this->newStatus();
482
483 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
484 if ( $dir === null ) {
485 $status->fatal( 'backend-fail-invalidpath', $params['dir'] );
486
487 return $status; // invalid storage path
488 }
489
490 if ( $shard !== null ) { // confined to a single container/shard
491 $status->merge( $this->doSecureInternal( $fullCont, $dir, $params ) );
492 } else { // directory is on several shards
493 $this->logger->debug( __METHOD__ . ": iterating over all container shards.\n" );
494 list( , $shortCont, ) = self::splitStoragePath( $params['dir'] );
495 foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
496 $status->merge( $this->doSecureInternal( "{$fullCont}{$suffix}", $dir, $params ) );
497 }
498 }
499
500 return $status;
501 }
502
503 /**
504 * @see FileBackendStore::doSecure()
505 * @param string $container
506 * @param string $dir
507 * @param array $params
508 * @return StatusValue
509 */
510 protected function doSecureInternal( $container, $dir, array $params ) {
511 return $this->newStatus();
512 }
513
514 final protected function doPublish( array $params ) {
515 $ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
516 $status = $this->newStatus();
517
518 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
519 if ( $dir === null ) {
520 $status->fatal( 'backend-fail-invalidpath', $params['dir'] );
521
522 return $status; // invalid storage path
523 }
524
525 if ( $shard !== null ) { // confined to a single container/shard
526 $status->merge( $this->doPublishInternal( $fullCont, $dir, $params ) );
527 } else { // directory is on several shards
528 $this->logger->debug( __METHOD__ . ": iterating over all container shards.\n" );
529 list( , $shortCont, ) = self::splitStoragePath( $params['dir'] );
530 foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
531 $status->merge( $this->doPublishInternal( "{$fullCont}{$suffix}", $dir, $params ) );
532 }
533 }
534
535 return $status;
536 }
537
538 /**
539 * @see FileBackendStore::doPublish()
540 * @param string $container
541 * @param string $dir
542 * @param array $params
543 * @return StatusValue
544 */
545 protected function doPublishInternal( $container, $dir, array $params ) {
546 return $this->newStatus();
547 }
548
549 final protected function doClean( array $params ) {
550 $ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
551 $status = $this->newStatus();
552
553 // Recursive: first delete all empty subdirs recursively
554 if ( !empty( $params['recursive'] ) && !$this->directoriesAreVirtual() ) {
555 $subDirsRel = $this->getTopDirectoryList( [ 'dir' => $params['dir'] ] );
556 if ( $subDirsRel !== null ) { // no errors
557 foreach ( $subDirsRel as $subDirRel ) {
558 $subDir = $params['dir'] . "/{$subDirRel}"; // full path
559 $status->merge( $this->doClean( [ 'dir' => $subDir ] + $params ) );
560 }
561 unset( $subDirsRel ); // free directory for rmdir() on Windows (for FS backends)
562 }
563 }
564
565 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
566 if ( $dir === null ) {
567 $status->fatal( 'backend-fail-invalidpath', $params['dir'] );
568
569 return $status; // invalid storage path
570 }
571
572 // Attempt to lock this directory...
573 $filesLockEx = [ $params['dir'] ];
574 $scopedLockE = $this->getScopedFileLocks( $filesLockEx, LockManager::LOCK_EX, $status );
575 if ( !$status->isOK() ) {
576 return $status; // abort
577 }
578
579 if ( $shard !== null ) { // confined to a single container/shard
580 $status->merge( $this->doCleanInternal( $fullCont, $dir, $params ) );
581 $this->deleteContainerCache( $fullCont ); // purge cache
582 } else { // directory is on several shards
583 $this->logger->debug( __METHOD__ . ": iterating over all container shards.\n" );
584 list( , $shortCont, ) = self::splitStoragePath( $params['dir'] );
585 foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
586 $status->merge( $this->doCleanInternal( "{$fullCont}{$suffix}", $dir, $params ) );
587 $this->deleteContainerCache( "{$fullCont}{$suffix}" ); // purge cache
588 }
589 }
590
591 return $status;
592 }
593
594 /**
595 * @see FileBackendStore::doClean()
596 * @param string $container
597 * @param string $dir
598 * @param array $params
599 * @return StatusValue
600 */
601 protected function doCleanInternal( $container, $dir, array $params ) {
602 return $this->newStatus();
603 }
604
605 final public function fileExists( array $params ) {
606 $ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
607 $stat = $this->getFileStat( $params );
608
609 return ( $stat === null ) ? null : (bool)$stat; // null => failure
610 }
611
612 final public function getFileTimestamp( array $params ) {
613 $ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
614 $stat = $this->getFileStat( $params );
615
616 return $stat ? $stat['mtime'] : false;
617 }
618
619 final public function getFileSize( array $params ) {
620 $ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
621 $stat = $this->getFileStat( $params );
622
623 return $stat ? $stat['size'] : false;
624 }
625
626 final public function getFileStat( array $params ) {
627 $path = self::normalizeStoragePath( $params['src'] );
628 if ( $path === null ) {
629 return false; // invalid storage path
630 }
631 $ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
632 $latest = !empty( $params['latest'] ); // use latest data?
633 if ( !$latest && !$this->cheapCache->has( $path, 'stat', self::CACHE_TTL ) ) {
634 $this->primeFileCache( [ $path ] ); // check persistent cache
635 }
636 if ( $this->cheapCache->has( $path, 'stat', self::CACHE_TTL ) ) {
637 $stat = $this->cheapCache->get( $path, 'stat' );
638 // If we want the latest data, check that this cached
639 // value was in fact fetched with the latest available data.
640 if ( is_array( $stat ) ) {
641 if ( !$latest || $stat['latest'] ) {
642 return $stat;
643 }
644 } elseif ( in_array( $stat, [ 'NOT_EXIST', 'NOT_EXIST_LATEST' ] ) ) {
645 if ( !$latest || $stat === 'NOT_EXIST_LATEST' ) {
646 return false;
647 }
648 }
649 }
650 $stat = $this->doGetFileStat( $params );
651 if ( is_array( $stat ) ) { // file exists
652 // Strongly consistent backends can automatically set "latest"
653 $stat['latest'] = isset( $stat['latest'] ) ? $stat['latest'] : $latest;
654 $this->cheapCache->set( $path, 'stat', $stat );
655 $this->setFileCache( $path, $stat ); // update persistent cache
656 if ( isset( $stat['sha1'] ) ) { // some backends store SHA-1 as metadata
657 $this->cheapCache->set( $path, 'sha1',
658 [ 'hash' => $stat['sha1'], 'latest' => $latest ] );
659 }
660 if ( isset( $stat['xattr'] ) ) { // some backends store headers/metadata
661 $stat['xattr'] = self::normalizeXAttributes( $stat['xattr'] );
662 $this->cheapCache->set( $path, 'xattr',
663 [ 'map' => $stat['xattr'], 'latest' => $latest ] );
664 }
665 } elseif ( $stat === false ) { // file does not exist
666 $this->cheapCache->set( $path, 'stat', $latest ? 'NOT_EXIST_LATEST' : 'NOT_EXIST' );
667 $this->cheapCache->set( $path, 'xattr', [ 'map' => false, 'latest' => $latest ] );
668 $this->cheapCache->set( $path, 'sha1', [ 'hash' => false, 'latest' => $latest ] );
669 $this->logger->debug( __METHOD__ . ": File $path does not exist.\n" );
670 } else { // an error occurred
671 $this->logger->warning( __METHOD__ . ": Could not stat file $path.\n" );
672 }
673
674 return $stat;
675 }
676
677 /**
678 * @see FileBackendStore::getFileStat()
679 * @param array $params
680 */
681 abstract protected function doGetFileStat( array $params );
682
683 public function getFileContentsMulti( array $params ) {
684 $ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
685
686 $params = $this->setConcurrencyFlags( $params );
687 $contents = $this->doGetFileContentsMulti( $params );
688
689 return $contents;
690 }
691
692 /**
693 * @see FileBackendStore::getFileContentsMulti()
694 * @param array $params
695 * @return array
696 */
697 protected function doGetFileContentsMulti( array $params ) {
698 $contents = [];
699 foreach ( $this->doGetLocalReferenceMulti( $params ) as $path => $fsFile ) {
700 MediaWiki\suppressWarnings();
701 $contents[$path] = $fsFile ? file_get_contents( $fsFile->getPath() ) : false;
702 MediaWiki\restoreWarnings();
703 }
704
705 return $contents;
706 }
707
708 final public function getFileXAttributes( array $params ) {
709 $path = self::normalizeStoragePath( $params['src'] );
710 if ( $path === null ) {
711 return false; // invalid storage path
712 }
713 $ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
714 $latest = !empty( $params['latest'] ); // use latest data?
715 if ( $this->cheapCache->has( $path, 'xattr', self::CACHE_TTL ) ) {
716 $stat = $this->cheapCache->get( $path, 'xattr' );
717 // If we want the latest data, check that this cached
718 // value was in fact fetched with the latest available data.
719 if ( !$latest || $stat['latest'] ) {
720 return $stat['map'];
721 }
722 }
723 $fields = $this->doGetFileXAttributes( $params );
724 $fields = is_array( $fields ) ? self::normalizeXAttributes( $fields ) : false;
725 $this->cheapCache->set( $path, 'xattr', [ 'map' => $fields, 'latest' => $latest ] );
726
727 return $fields;
728 }
729
730 /**
731 * @see FileBackendStore::getFileXAttributes()
732 * @param array $params
733 * @return array[][]
734 */
735 protected function doGetFileXAttributes( array $params ) {
736 return [ 'headers' => [], 'metadata' => [] ]; // not supported
737 }
738
739 final public function getFileSha1Base36( array $params ) {
740 $path = self::normalizeStoragePath( $params['src'] );
741 if ( $path === null ) {
742 return false; // invalid storage path
743 }
744 $ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
745 $latest = !empty( $params['latest'] ); // use latest data?
746 if ( $this->cheapCache->has( $path, 'sha1', self::CACHE_TTL ) ) {
747 $stat = $this->cheapCache->get( $path, 'sha1' );
748 // If we want the latest data, check that this cached
749 // value was in fact fetched with the latest available data.
750 if ( !$latest || $stat['latest'] ) {
751 return $stat['hash'];
752 }
753 }
754 $hash = $this->doGetFileSha1Base36( $params );
755 $this->cheapCache->set( $path, 'sha1', [ 'hash' => $hash, 'latest' => $latest ] );
756
757 return $hash;
758 }
759
760 /**
761 * @see FileBackendStore::getFileSha1Base36()
762 * @param array $params
763 * @return bool|string
764 */
765 protected function doGetFileSha1Base36( array $params ) {
766 $fsFile = $this->getLocalReference( $params );
767 if ( !$fsFile ) {
768 return false;
769 } else {
770 return $fsFile->getSha1Base36();
771 }
772 }
773
774 final public function getFileProps( array $params ) {
775 $ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
776 $fsFile = $this->getLocalReference( $params );
777 $props = $fsFile ? $fsFile->getProps() : FSFile::placeholderProps();
778
779 return $props;
780 }
781
782 final public function getLocalReferenceMulti( array $params ) {
783 $ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
784
785 $params = $this->setConcurrencyFlags( $params );
786
787 $fsFiles = []; // (path => FSFile)
788 $latest = !empty( $params['latest'] ); // use latest data?
789 // Reuse any files already in process cache...
790 foreach ( $params['srcs'] as $src ) {
791 $path = self::normalizeStoragePath( $src );
792 if ( $path === null ) {
793 $fsFiles[$src] = null; // invalid storage path
794 } elseif ( $this->expensiveCache->has( $path, 'localRef' ) ) {
795 $val = $this->expensiveCache->get( $path, 'localRef' );
796 // If we want the latest data, check that this cached
797 // value was in fact fetched with the latest available data.
798 if ( !$latest || $val['latest'] ) {
799 $fsFiles[$src] = $val['object'];
800 }
801 }
802 }
803 // Fetch local references of any remaning files...
804 $params['srcs'] = array_diff( $params['srcs'], array_keys( $fsFiles ) );
805 foreach ( $this->doGetLocalReferenceMulti( $params ) as $path => $fsFile ) {
806 $fsFiles[$path] = $fsFile;
807 if ( $fsFile ) { // update the process cache...
808 $this->expensiveCache->set( $path, 'localRef',
809 [ 'object' => $fsFile, 'latest' => $latest ] );
810 }
811 }
812
813 return $fsFiles;
814 }
815
816 /**
817 * @see FileBackendStore::getLocalReferenceMulti()
818 * @param array $params
819 * @return array
820 */
821 protected function doGetLocalReferenceMulti( array $params ) {
822 return $this->doGetLocalCopyMulti( $params );
823 }
824
825 final public function getLocalCopyMulti( array $params ) {
826 $ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
827
828 $params = $this->setConcurrencyFlags( $params );
829 $tmpFiles = $this->doGetLocalCopyMulti( $params );
830
831 return $tmpFiles;
832 }
833
834 /**
835 * @see FileBackendStore::getLocalCopyMulti()
836 * @param array $params
837 * @return array
838 */
839 abstract protected function doGetLocalCopyMulti( array $params );
840
841 /**
842 * @see FileBackend::getFileHttpUrl()
843 * @param array $params
844 * @return string|null
845 */
846 public function getFileHttpUrl( array $params ) {
847 return null; // not supported
848 }
849
850 final public function streamFile( array $params ) {
851 $ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
852 $status = $this->newStatus();
853
854 // Always set some fields for subclass convenience
855 $params['options'] = isset( $params['options'] ) ? $params['options'] : [];
856 $params['headers'] = isset( $params['headers'] ) ? $params['headers'] : [];
857
858 // Don't stream it out as text/html if there was a PHP error
859 if ( ( empty( $params['headless'] ) || $params['headers'] ) && headers_sent() ) {
860 print "Headers already sent, terminating.\n";
861 $status->fatal( 'backend-fail-stream', $params['src'] );
862 return $status;
863 }
864
865 $status->merge( $this->doStreamFile( $params ) );
866
867 return $status;
868 }
869
870 /**
871 * @see FileBackendStore::streamFile()
872 * @param array $params
873 * @return StatusValue
874 */
875 protected function doStreamFile( array $params ) {
876 $status = $this->newStatus();
877
878 $flags = 0;
879 $flags |= !empty( $params['headless'] ) ? HTTPFileStreamer::STREAM_HEADLESS : 0;
880 $flags |= !empty( $params['allowOB'] ) ? HTTPFileStreamer::STREAM_ALLOW_OB : 0;
881
882 $fsFile = $this->getLocalReference( $params );
883 if ( $fsFile ) {
884 $streamer = new HTTPFileStreamer(
885 $fsFile->getPath(),
886 [
887 'obResetFunc' => $this->obResetFunc,
888 'streamMimeFunc' => $this->streamMimeFunc
889 ]
890 );
891 $res = $streamer->stream( $params['headers'], true, $params['options'], $flags );
892 } else {
893 $res = false;
894 HTTPFileStreamer::send404Message( $params['src'], $flags );
895 }
896
897 if ( !$res ) {
898 $status->fatal( 'backend-fail-stream', $params['src'] );
899 }
900
901 return $status;
902 }
903
904 final public function directoryExists( array $params ) {
905 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
906 if ( $dir === null ) {
907 return false; // invalid storage path
908 }
909 if ( $shard !== null ) { // confined to a single container/shard
910 return $this->doDirectoryExists( $fullCont, $dir, $params );
911 } else { // directory is on several shards
912 $this->logger->debug( __METHOD__ . ": iterating over all container shards.\n" );
913 list( , $shortCont, ) = self::splitStoragePath( $params['dir'] );
914 $res = false; // response
915 foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
916 $exists = $this->doDirectoryExists( "{$fullCont}{$suffix}", $dir, $params );
917 if ( $exists ) {
918 $res = true;
919 break; // found one!
920 } elseif ( $exists === null ) { // error?
921 $res = null; // if we don't find anything, it is indeterminate
922 }
923 }
924
925 return $res;
926 }
927 }
928
929 /**
930 * @see FileBackendStore::directoryExists()
931 *
932 * @param string $container Resolved container name
933 * @param string $dir Resolved path relative to container
934 * @param array $params
935 * @return bool|null
936 */
937 abstract protected function doDirectoryExists( $container, $dir, array $params );
938
939 final public function getDirectoryList( array $params ) {
940 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
941 if ( $dir === null ) { // invalid storage path
942 return null;
943 }
944 if ( $shard !== null ) {
945 // File listing is confined to a single container/shard
946 return $this->getDirectoryListInternal( $fullCont, $dir, $params );
947 } else {
948 $this->logger->debug( __METHOD__ . ": iterating over all container shards.\n" );
949 // File listing spans multiple containers/shards
950 list( , $shortCont, ) = self::splitStoragePath( $params['dir'] );
951
952 return new FileBackendStoreShardDirIterator( $this,
953 $fullCont, $dir, $this->getContainerSuffixes( $shortCont ), $params );
954 }
955 }
956
957 /**
958 * Do not call this function from places outside FileBackend
959 *
960 * @see FileBackendStore::getDirectoryList()
961 *
962 * @param string $container Resolved container name
963 * @param string $dir Resolved path relative to container
964 * @param array $params
965 * @return Traversable|array|null Returns null on failure
966 */
967 abstract public function getDirectoryListInternal( $container, $dir, array $params );
968
969 final public function getFileList( array $params ) {
970 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
971 if ( $dir === null ) { // invalid storage path
972 return null;
973 }
974 if ( $shard !== null ) {
975 // File listing is confined to a single container/shard
976 return $this->getFileListInternal( $fullCont, $dir, $params );
977 } else {
978 $this->logger->debug( __METHOD__ . ": iterating over all container shards.\n" );
979 // File listing spans multiple containers/shards
980 list( , $shortCont, ) = self::splitStoragePath( $params['dir'] );
981
982 return new FileBackendStoreShardFileIterator( $this,
983 $fullCont, $dir, $this->getContainerSuffixes( $shortCont ), $params );
984 }
985 }
986
987 /**
988 * Do not call this function from places outside FileBackend
989 *
990 * @see FileBackendStore::getFileList()
991 *
992 * @param string $container Resolved container name
993 * @param string $dir Resolved path relative to container
994 * @param array $params
995 * @return Traversable|array|null Returns null on failure
996 */
997 abstract public function getFileListInternal( $container, $dir, array $params );
998
999 /**
1000 * Return a list of FileOp objects from a list of operations.
1001 * Do not call this function from places outside FileBackend.
1002 *
1003 * The result must have the same number of items as the input.
1004 * An exception is thrown if an unsupported operation is requested.
1005 *
1006 * @param array $ops Same format as doOperations()
1007 * @return FileOp[] List of FileOp objects
1008 * @throws FileBackendError
1009 */
1010 final public function getOperationsInternal( array $ops ) {
1011 $supportedOps = [
1012 'store' => 'StoreFileOp',
1013 'copy' => 'CopyFileOp',
1014 'move' => 'MoveFileOp',
1015 'delete' => 'DeleteFileOp',
1016 'create' => 'CreateFileOp',
1017 'describe' => 'DescribeFileOp',
1018 'null' => 'NullFileOp'
1019 ];
1020
1021 $performOps = []; // array of FileOp objects
1022 // Build up ordered array of FileOps...
1023 foreach ( $ops as $operation ) {
1024 $opName = $operation['op'];
1025 if ( isset( $supportedOps[$opName] ) ) {
1026 $class = $supportedOps[$opName];
1027 // Get params for this operation
1028 $params = $operation;
1029 // Append the FileOp class
1030 $performOps[] = new $class( $this, $params, $this->logger );
1031 } else {
1032 throw new FileBackendError( "Operation '$opName' is not supported." );
1033 }
1034 }
1035
1036 return $performOps;
1037 }
1038
1039 /**
1040 * Get a list of storage paths to lock for a list of operations
1041 * Returns an array with LockManager::LOCK_UW (shared locks) and
1042 * LockManager::LOCK_EX (exclusive locks) keys, each corresponding
1043 * to a list of storage paths to be locked. All returned paths are
1044 * normalized.
1045 *
1046 * @param array $performOps List of FileOp objects
1047 * @return array (LockManager::LOCK_UW => path list, LockManager::LOCK_EX => path list)
1048 */
1049 final public function getPathsToLockForOpsInternal( array $performOps ) {
1050 // Build up a list of files to lock...
1051 $paths = [ 'sh' => [], 'ex' => [] ];
1052 foreach ( $performOps as $fileOp ) {
1053 $paths['sh'] = array_merge( $paths['sh'], $fileOp->storagePathsRead() );
1054 $paths['ex'] = array_merge( $paths['ex'], $fileOp->storagePathsChanged() );
1055 }
1056 // Optimization: if doing an EX lock anyway, don't also set an SH one
1057 $paths['sh'] = array_diff( $paths['sh'], $paths['ex'] );
1058 // Get a shared lock on the parent directory of each path changed
1059 $paths['sh'] = array_merge( $paths['sh'], array_map( 'dirname', $paths['ex'] ) );
1060
1061 return [
1062 LockManager::LOCK_UW => $paths['sh'],
1063 LockManager::LOCK_EX => $paths['ex']
1064 ];
1065 }
1066
1067 public function getScopedLocksForOps( array $ops, StatusValue $status ) {
1068 $paths = $this->getPathsToLockForOpsInternal( $this->getOperationsInternal( $ops ) );
1069
1070 return $this->getScopedFileLocks( $paths, 'mixed', $status );
1071 }
1072
1073 final protected function doOperationsInternal( array $ops, array $opts ) {
1074 $ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
1075 $status = $this->newStatus();
1076
1077 // Fix up custom header name/value pairs...
1078 $ops = array_map( [ $this, 'sanitizeOpHeaders' ], $ops );
1079
1080 // Build up a list of FileOps...
1081 $performOps = $this->getOperationsInternal( $ops );
1082
1083 // Acquire any locks as needed...
1084 if ( empty( $opts['nonLocking'] ) ) {
1085 // Build up a list of files to lock...
1086 $paths = $this->getPathsToLockForOpsInternal( $performOps );
1087 // Try to lock those files for the scope of this function...
1088
1089 $scopeLock = $this->getScopedFileLocks( $paths, 'mixed', $status );
1090 if ( !$status->isOK() ) {
1091 return $status; // abort
1092 }
1093 }
1094
1095 // Clear any file cache entries (after locks acquired)
1096 if ( empty( $opts['preserveCache'] ) ) {
1097 $this->clearCache();
1098 }
1099
1100 // Build the list of paths involved
1101 $paths = [];
1102 foreach ( $performOps as $performOp ) {
1103 $paths = array_merge( $paths, $performOp->storagePathsRead() );
1104 $paths = array_merge( $paths, $performOp->storagePathsChanged() );
1105 }
1106
1107 // Enlarge the cache to fit the stat entries of these files
1108 $this->cheapCache->resize( max( 2 * count( $paths ), self::CACHE_CHEAP_SIZE ) );
1109
1110 // Load from the persistent container caches
1111 $this->primeContainerCache( $paths );
1112 // Get the latest stat info for all the files (having locked them)
1113 $ok = $this->preloadFileStat( [ 'srcs' => $paths, 'latest' => true ] );
1114
1115 if ( $ok ) {
1116 // Actually attempt the operation batch...
1117 $opts = $this->setConcurrencyFlags( $opts );
1118 $subStatus = FileOpBatch::attempt( $performOps, $opts, $this->fileJournal );
1119 } else {
1120 // If we could not even stat some files, then bail out...
1121 $subStatus = $this->newStatus( 'backend-fail-internal', $this->name );
1122 foreach ( $ops as $i => $op ) { // mark each op as failed
1123 $subStatus->success[$i] = false;
1124 ++$subStatus->failCount;
1125 }
1126 $this->logger->error( get_class( $this ) . "-{$this->name} " .
1127 " stat failure; aborted operations: " . FormatJson::encode( $ops ) );
1128 }
1129
1130 // Merge errors into StatusValue fields
1131 $status->merge( $subStatus );
1132 $status->success = $subStatus->success; // not done in merge()
1133
1134 // Shrink the stat cache back to normal size
1135 $this->cheapCache->resize( self::CACHE_CHEAP_SIZE );
1136
1137 return $status;
1138 }
1139
1140 final protected function doQuickOperationsInternal( array $ops ) {
1141 $ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
1142 $status = $this->newStatus();
1143
1144 // Fix up custom header name/value pairs...
1145 $ops = array_map( [ $this, 'sanitizeOpHeaders' ], $ops );
1146
1147 // Clear any file cache entries
1148 $this->clearCache();
1149
1150 $supportedOps = [ 'create', 'store', 'copy', 'move', 'delete', 'describe', 'null' ];
1151 // Parallel ops may be disabled in config due to dependencies (e.g. needing popen())
1152 $async = ( $this->parallelize === 'implicit' && count( $ops ) > 1 );
1153 $maxConcurrency = $this->concurrency; // throttle
1154 /** @var StatusValue[] $statuses */
1155 $statuses = []; // array of (index => StatusValue)
1156 $fileOpHandles = []; // list of (index => handle) arrays
1157 $curFileOpHandles = []; // current handle batch
1158 // Perform the sync-only ops and build up op handles for the async ops...
1159 foreach ( $ops as $index => $params ) {
1160 if ( !in_array( $params['op'], $supportedOps ) ) {
1161 throw new FileBackendError( "Operation '{$params['op']}' is not supported." );
1162 }
1163 $method = $params['op'] . 'Internal'; // e.g. "storeInternal"
1164 $subStatus = $this->$method( [ 'async' => $async ] + $params );
1165 if ( $subStatus->value instanceof FileBackendStoreOpHandle ) { // async
1166 if ( count( $curFileOpHandles ) >= $maxConcurrency ) {
1167 $fileOpHandles[] = $curFileOpHandles; // push this batch
1168 $curFileOpHandles = [];
1169 }
1170 $curFileOpHandles[$index] = $subStatus->value; // keep index
1171 } else { // error or completed
1172 $statuses[$index] = $subStatus; // keep index
1173 }
1174 }
1175 if ( count( $curFileOpHandles ) ) {
1176 $fileOpHandles[] = $curFileOpHandles; // last batch
1177 }
1178 // Do all the async ops that can be done concurrently...
1179 foreach ( $fileOpHandles as $fileHandleBatch ) {
1180 $statuses = $statuses + $this->executeOpHandlesInternal( $fileHandleBatch );
1181 }
1182 // Marshall and merge all the responses...
1183 foreach ( $statuses as $index => $subStatus ) {
1184 $status->merge( $subStatus );
1185 if ( $subStatus->isOK() ) {
1186 $status->success[$index] = true;
1187 ++$status->successCount;
1188 } else {
1189 $status->success[$index] = false;
1190 ++$status->failCount;
1191 }
1192 }
1193
1194 return $status;
1195 }
1196
1197 /**
1198 * Execute a list of FileBackendStoreOpHandle handles in parallel.
1199 * The resulting StatusValue object fields will correspond
1200 * to the order in which the handles where given.
1201 *
1202 * @param FileBackendStoreOpHandle[] $fileOpHandles
1203 *
1204 * @throws FileBackendError
1205 * @return StatusValue[] Map of StatusValue objects
1206 */
1207 final public function executeOpHandlesInternal( array $fileOpHandles ) {
1208 $ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
1209
1210 foreach ( $fileOpHandles as $fileOpHandle ) {
1211 if ( !( $fileOpHandle instanceof FileBackendStoreOpHandle ) ) {
1212 throw new InvalidArgumentException( "Got a non-FileBackendStoreOpHandle object." );
1213 } elseif ( $fileOpHandle->backend->getName() !== $this->getName() ) {
1214 throw new InvalidArgumentException(
1215 "Got a FileBackendStoreOpHandle for the wrong backend." );
1216 }
1217 }
1218 $res = $this->doExecuteOpHandlesInternal( $fileOpHandles );
1219 foreach ( $fileOpHandles as $fileOpHandle ) {
1220 $fileOpHandle->closeResources();
1221 }
1222
1223 return $res;
1224 }
1225
1226 /**
1227 * @see FileBackendStore::executeOpHandlesInternal()
1228 *
1229 * @param FileBackendStoreOpHandle[] $fileOpHandles
1230 *
1231 * @throws FileBackendError
1232 * @return StatusValue[] List of corresponding StatusValue objects
1233 */
1234 protected function doExecuteOpHandlesInternal( array $fileOpHandles ) {
1235 if ( count( $fileOpHandles ) ) {
1236 throw new LogicException( "Backend does not support asynchronous operations." );
1237 }
1238
1239 return [];
1240 }
1241
1242 /**
1243 * Normalize and filter HTTP headers from a file operation
1244 *
1245 * This normalizes and strips long HTTP headers from a file operation.
1246 * Most headers are just numbers, but some are allowed to be long.
1247 * This function is useful for cleaning up headers and avoiding backend
1248 * specific errors, especially in the middle of batch file operations.
1249 *
1250 * @param array $op Same format as doOperation()
1251 * @return array
1252 */
1253 protected function sanitizeOpHeaders( array $op ) {
1254 static $longs = [ 'content-disposition' ];
1255
1256 if ( isset( $op['headers'] ) ) { // op sets HTTP headers
1257 $newHeaders = [];
1258 foreach ( $op['headers'] as $name => $value ) {
1259 $name = strtolower( $name );
1260 $maxHVLen = in_array( $name, $longs ) ? INF : 255;
1261 if ( strlen( $name ) > 255 || strlen( $value ) > $maxHVLen ) {
1262 trigger_error( "Header '$name: $value' is too long." );
1263 } else {
1264 $newHeaders[$name] = strlen( $value ) ? $value : ''; // null/false => ""
1265 }
1266 }
1267 $op['headers'] = $newHeaders;
1268 }
1269
1270 return $op;
1271 }
1272
1273 final public function preloadCache( array $paths ) {
1274 $fullConts = []; // full container names
1275 foreach ( $paths as $path ) {
1276 list( $fullCont, , ) = $this->resolveStoragePath( $path );
1277 $fullConts[] = $fullCont;
1278 }
1279 // Load from the persistent file and container caches
1280 $this->primeContainerCache( $fullConts );
1281 $this->primeFileCache( $paths );
1282 }
1283
1284 final public function clearCache( array $paths = null ) {
1285 if ( is_array( $paths ) ) {
1286 $paths = array_map( 'FileBackend::normalizeStoragePath', $paths );
1287 $paths = array_filter( $paths, 'strlen' ); // remove nulls
1288 }
1289 if ( $paths === null ) {
1290 $this->cheapCache->clear();
1291 $this->expensiveCache->clear();
1292 } else {
1293 foreach ( $paths as $path ) {
1294 $this->cheapCache->clear( $path );
1295 $this->expensiveCache->clear( $path );
1296 }
1297 }
1298 $this->doClearCache( $paths );
1299 }
1300
1301 /**
1302 * Clears any additional stat caches for storage paths
1303 *
1304 * @see FileBackend::clearCache()
1305 *
1306 * @param array $paths Storage paths (optional)
1307 */
1308 protected function doClearCache( array $paths = null ) {
1309 }
1310
1311 final public function preloadFileStat( array $params ) {
1312 $ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
1313 $success = true; // no network errors
1314
1315 $params['concurrency'] = ( $this->parallelize !== 'off' ) ? $this->concurrency : 1;
1316 $stats = $this->doGetFileStatMulti( $params );
1317 if ( $stats === null ) {
1318 return true; // not supported
1319 }
1320
1321 $latest = !empty( $params['latest'] ); // use latest data?
1322 foreach ( $stats as $path => $stat ) {
1323 $path = FileBackend::normalizeStoragePath( $path );
1324 if ( $path === null ) {
1325 continue; // this shouldn't happen
1326 }
1327 if ( is_array( $stat ) ) { // file exists
1328 // Strongly consistent backends can automatically set "latest"
1329 $stat['latest'] = isset( $stat['latest'] ) ? $stat['latest'] : $latest;
1330 $this->cheapCache->set( $path, 'stat', $stat );
1331 $this->setFileCache( $path, $stat ); // update persistent cache
1332 if ( isset( $stat['sha1'] ) ) { // some backends store SHA-1 as metadata
1333 $this->cheapCache->set( $path, 'sha1',
1334 [ 'hash' => $stat['sha1'], 'latest' => $latest ] );
1335 }
1336 if ( isset( $stat['xattr'] ) ) { // some backends store headers/metadata
1337 $stat['xattr'] = self::normalizeXAttributes( $stat['xattr'] );
1338 $this->cheapCache->set( $path, 'xattr',
1339 [ 'map' => $stat['xattr'], 'latest' => $latest ] );
1340 }
1341 } elseif ( $stat === false ) { // file does not exist
1342 $this->cheapCache->set( $path, 'stat',
1343 $latest ? 'NOT_EXIST_LATEST' : 'NOT_EXIST' );
1344 $this->cheapCache->set( $path, 'xattr',
1345 [ 'map' => false, 'latest' => $latest ] );
1346 $this->cheapCache->set( $path, 'sha1',
1347 [ 'hash' => false, 'latest' => $latest ] );
1348 $this->logger->debug( __METHOD__ . ": File $path does not exist.\n" );
1349 } else { // an error occurred
1350 $success = false;
1351 $this->logger->warning( __METHOD__ . ": Could not stat file $path.\n" );
1352 }
1353 }
1354
1355 return $success;
1356 }
1357
1358 /**
1359 * Get file stat information (concurrently if possible) for several files
1360 *
1361 * @see FileBackend::getFileStat()
1362 *
1363 * @param array $params Parameters include:
1364 * - srcs : list of source storage paths
1365 * - latest : use the latest available data
1366 * @return array|null Map of storage paths to array|bool|null (returns null if not supported)
1367 * @since 1.23
1368 */
1369 protected function doGetFileStatMulti( array $params ) {
1370 return null; // not supported
1371 }
1372
1373 /**
1374 * Is this a key/value store where directories are just virtual?
1375 * Virtual directories exists in so much as files exists that are
1376 * prefixed with the directory path followed by a forward slash.
1377 *
1378 * @return bool
1379 */
1380 abstract protected function directoriesAreVirtual();
1381
1382 /**
1383 * Check if a short container name is valid
1384 *
1385 * This checks for length and illegal characters.
1386 * This may disallow certain characters that can appear
1387 * in the prefix used to make the full container name.
1388 *
1389 * @param string $container
1390 * @return bool
1391 */
1392 final protected static function isValidShortContainerName( $container ) {
1393 // Suffixes like '.xxx' (hex shard chars) or '.seg' (file segments)
1394 // might be used by subclasses. Reserve the dot character for sanity.
1395 // The only way dots end up in containers (e.g. resolveStoragePath)
1396 // is due to the wikiId container prefix or the above suffixes.
1397 return self::isValidContainerName( $container ) && !preg_match( '/[.]/', $container );
1398 }
1399
1400 /**
1401 * Check if a full container name is valid
1402 *
1403 * This checks for length and illegal characters.
1404 * Limiting the characters makes migrations to other stores easier.
1405 *
1406 * @param string $container
1407 * @return bool
1408 */
1409 final protected static function isValidContainerName( $container ) {
1410 // This accounts for NTFS, Swift, and Ceph restrictions
1411 // and disallows directory separators or traversal characters.
1412 // Note that matching strings URL encode to the same string;
1413 // in Swift/Ceph, the length restriction is *after* URL encoding.
1414 return (bool)preg_match( '/^[a-z0-9][a-z0-9-_.]{0,199}$/i', $container );
1415 }
1416
1417 /**
1418 * Splits a storage path into an internal container name,
1419 * an internal relative file name, and a container shard suffix.
1420 * Any shard suffix is already appended to the internal container name.
1421 * This also checks that the storage path is valid and within this backend.
1422 *
1423 * If the container is sharded but a suffix could not be determined,
1424 * this means that the path can only refer to a directory and can only
1425 * be scanned by looking in all the container shards.
1426 *
1427 * @param string $storagePath
1428 * @return array (container, path, container suffix) or (null, null, null) if invalid
1429 */
1430 final protected function resolveStoragePath( $storagePath ) {
1431 list( $backend, $shortCont, $relPath ) = self::splitStoragePath( $storagePath );
1432 if ( $backend === $this->name ) { // must be for this backend
1433 $relPath = self::normalizeContainerPath( $relPath );
1434 if ( $relPath !== null && self::isValidShortContainerName( $shortCont ) ) {
1435 // Get shard for the normalized path if this container is sharded
1436 $cShard = $this->getContainerShard( $shortCont, $relPath );
1437 // Validate and sanitize the relative path (backend-specific)
1438 $relPath = $this->resolveContainerPath( $shortCont, $relPath );
1439 if ( $relPath !== null ) {
1440 // Prepend any wiki ID prefix to the container name
1441 $container = $this->fullContainerName( $shortCont );
1442 if ( self::isValidContainerName( $container ) ) {
1443 // Validate and sanitize the container name (backend-specific)
1444 $container = $this->resolveContainerName( "{$container}{$cShard}" );
1445 if ( $container !== null ) {
1446 return [ $container, $relPath, $cShard ];
1447 }
1448 }
1449 }
1450 }
1451 }
1452
1453 return [ null, null, null ];
1454 }
1455
1456 /**
1457 * Like resolveStoragePath() except null values are returned if
1458 * the container is sharded and the shard could not be determined
1459 * or if the path ends with '/'. The latter case is illegal for FS
1460 * backends and can confuse listings for object store backends.
1461 *
1462 * This function is used when resolving paths that must be valid
1463 * locations for files. Directory and listing functions should
1464 * generally just use resolveStoragePath() instead.
1465 *
1466 * @see FileBackendStore::resolveStoragePath()
1467 *
1468 * @param string $storagePath
1469 * @return array (container, path) or (null, null) if invalid
1470 */
1471 final protected function resolveStoragePathReal( $storagePath ) {
1472 list( $container, $relPath, $cShard ) = $this->resolveStoragePath( $storagePath );
1473 if ( $cShard !== null && substr( $relPath, -1 ) !== '/' ) {
1474 return [ $container, $relPath ];
1475 }
1476
1477 return [ null, null ];
1478 }
1479
1480 /**
1481 * Get the container name shard suffix for a given path.
1482 * Any empty suffix means the container is not sharded.
1483 *
1484 * @param string $container Container name
1485 * @param string $relPath Storage path relative to the container
1486 * @return string|null Returns null if shard could not be determined
1487 */
1488 final protected function getContainerShard( $container, $relPath ) {
1489 list( $levels, $base, $repeat ) = $this->getContainerHashLevels( $container );
1490 if ( $levels == 1 || $levels == 2 ) {
1491 // Hash characters are either base 16 or 36
1492 $char = ( $base == 36 ) ? '[0-9a-z]' : '[0-9a-f]';
1493 // Get a regex that represents the shard portion of paths.
1494 // The concatenation of the captures gives us the shard.
1495 if ( $levels === 1 ) { // 16 or 36 shards per container
1496 $hashDirRegex = '(' . $char . ')';
1497 } else { // 256 or 1296 shards per container
1498 if ( $repeat ) { // verbose hash dir format (e.g. "a/ab/abc")
1499 $hashDirRegex = $char . '/(' . $char . '{2})';
1500 } else { // short hash dir format (e.g. "a/b/c")
1501 $hashDirRegex = '(' . $char . ')/(' . $char . ')';
1502 }
1503 }
1504 // Allow certain directories to be above the hash dirs so as
1505 // to work with FileRepo (e.g. "archive/a/ab" or "temp/a/ab").
1506 // They must be 2+ chars to avoid any hash directory ambiguity.
1507 $m = [];
1508 if ( preg_match( "!^(?:[^/]{2,}/)*$hashDirRegex(?:/|$)!", $relPath, $m ) ) {
1509 return '.' . implode( '', array_slice( $m, 1 ) );
1510 }
1511
1512 return null; // failed to match
1513 }
1514
1515 return ''; // no sharding
1516 }
1517
1518 /**
1519 * Check if a storage path maps to a single shard.
1520 * Container dirs like "a", where the container shards on "x/xy",
1521 * can reside on several shards. Such paths are tricky to handle.
1522 *
1523 * @param string $storagePath Storage path
1524 * @return bool
1525 */
1526 final public function isSingleShardPathInternal( $storagePath ) {
1527 list( , , $shard ) = $this->resolveStoragePath( $storagePath );
1528
1529 return ( $shard !== null );
1530 }
1531
1532 /**
1533 * Get the sharding config for a container.
1534 * If greater than 0, then all file storage paths within
1535 * the container are required to be hashed accordingly.
1536 *
1537 * @param string $container
1538 * @return array (integer levels, integer base, repeat flag) or (0, 0, false)
1539 */
1540 final protected function getContainerHashLevels( $container ) {
1541 if ( isset( $this->shardViaHashLevels[$container] ) ) {
1542 $config = $this->shardViaHashLevels[$container];
1543 $hashLevels = (int)$config['levels'];
1544 if ( $hashLevels == 1 || $hashLevels == 2 ) {
1545 $hashBase = (int)$config['base'];
1546 if ( $hashBase == 16 || $hashBase == 36 ) {
1547 return [ $hashLevels, $hashBase, $config['repeat'] ];
1548 }
1549 }
1550 }
1551
1552 return [ 0, 0, false ]; // no sharding
1553 }
1554
1555 /**
1556 * Get a list of full container shard suffixes for a container
1557 *
1558 * @param string $container
1559 * @return array
1560 */
1561 final protected function getContainerSuffixes( $container ) {
1562 $shards = [];
1563 list( $digits, $base ) = $this->getContainerHashLevels( $container );
1564 if ( $digits > 0 ) {
1565 $numShards = pow( $base, $digits );
1566 for ( $index = 0; $index < $numShards; $index++ ) {
1567 $shards[] = '.' . Wikimedia\base_convert( $index, 10, $base, $digits );
1568 }
1569 }
1570
1571 return $shards;
1572 }
1573
1574 /**
1575 * Get the full container name, including the wiki ID prefix
1576 *
1577 * @param string $container
1578 * @return string
1579 */
1580 final protected function fullContainerName( $container ) {
1581 if ( $this->domainId != '' ) {
1582 return "{$this->domainId}-$container";
1583 } else {
1584 return $container;
1585 }
1586 }
1587
1588 /**
1589 * Resolve a container name, checking if it's allowed by the backend.
1590 * This is intended for internal use, such as encoding illegal chars.
1591 * Subclasses can override this to be more restrictive.
1592 *
1593 * @param string $container
1594 * @return string|null
1595 */
1596 protected function resolveContainerName( $container ) {
1597 return $container;
1598 }
1599
1600 /**
1601 * Resolve a relative storage path, checking if it's allowed by the backend.
1602 * This is intended for internal use, such as encoding illegal chars or perhaps
1603 * getting absolute paths (e.g. FS based backends). Note that the relative path
1604 * may be the empty string (e.g. the path is simply to the container).
1605 *
1606 * @param string $container Container name
1607 * @param string $relStoragePath Storage path relative to the container
1608 * @return string|null Path or null if not valid
1609 */
1610 protected function resolveContainerPath( $container, $relStoragePath ) {
1611 return $relStoragePath;
1612 }
1613
1614 /**
1615 * Get the cache key for a container
1616 *
1617 * @param string $container Resolved container name
1618 * @return string
1619 */
1620 private function containerCacheKey( $container ) {
1621 return "filebackend:{$this->name}:{$this->domainId}:container:{$container}";
1622 }
1623
1624 /**
1625 * Set the cached info for a container
1626 *
1627 * @param string $container Resolved container name
1628 * @param array $val Information to cache
1629 */
1630 final protected function setContainerCache( $container, array $val ) {
1631 $this->memCache->set( $this->containerCacheKey( $container ), $val, 14 * 86400 );
1632 }
1633
1634 /**
1635 * Delete the cached info for a container.
1636 * The cache key is salted for a while to prevent race conditions.
1637 *
1638 * @param string $container Resolved container name
1639 */
1640 final protected function deleteContainerCache( $container ) {
1641 if ( !$this->memCache->delete( $this->containerCacheKey( $container ), 300 ) ) {
1642 trigger_error( "Unable to delete stat cache for container $container." );
1643 }
1644 }
1645
1646 /**
1647 * Do a batch lookup from cache for container stats for all containers
1648 * used in a list of container names or storage paths objects.
1649 * This loads the persistent cache values into the process cache.
1650 *
1651 * @param array $items
1652 */
1653 final protected function primeContainerCache( array $items ) {
1654 $ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
1655
1656 $paths = []; // list of storage paths
1657 $contNames = []; // (cache key => resolved container name)
1658 // Get all the paths/containers from the items...
1659 foreach ( $items as $item ) {
1660 if ( self::isStoragePath( $item ) ) {
1661 $paths[] = $item;
1662 } elseif ( is_string( $item ) ) { // full container name
1663 $contNames[$this->containerCacheKey( $item )] = $item;
1664 }
1665 }
1666 // Get all the corresponding cache keys for paths...
1667 foreach ( $paths as $path ) {
1668 list( $fullCont, , ) = $this->resolveStoragePath( $path );
1669 if ( $fullCont !== null ) { // valid path for this backend
1670 $contNames[$this->containerCacheKey( $fullCont )] = $fullCont;
1671 }
1672 }
1673
1674 $contInfo = []; // (resolved container name => cache value)
1675 // Get all cache entries for these container cache keys...
1676 $values = $this->memCache->getMulti( array_keys( $contNames ) );
1677 foreach ( $values as $cacheKey => $val ) {
1678 $contInfo[$contNames[$cacheKey]] = $val;
1679 }
1680
1681 // Populate the container process cache for the backend...
1682 $this->doPrimeContainerCache( array_filter( $contInfo, 'is_array' ) );
1683 }
1684
1685 /**
1686 * Fill the backend-specific process cache given an array of
1687 * resolved container names and their corresponding cached info.
1688 * Only containers that actually exist should appear in the map.
1689 *
1690 * @param array $containerInfo Map of resolved container names to cached info
1691 */
1692 protected function doPrimeContainerCache( array $containerInfo ) {
1693 }
1694
1695 /**
1696 * Get the cache key for a file path
1697 *
1698 * @param string $path Normalized storage path
1699 * @return string
1700 */
1701 private function fileCacheKey( $path ) {
1702 return "filebackend:{$this->name}:{$this->domainId}:file:" . sha1( $path );
1703 }
1704
1705 /**
1706 * Set the cached stat info for a file path.
1707 * Negatives (404s) are not cached. By not caching negatives, we can skip cache
1708 * salting for the case when a file is created at a path were there was none before.
1709 *
1710 * @param string $path Storage path
1711 * @param array $val Stat information to cache
1712 */
1713 final protected function setFileCache( $path, array $val ) {
1714 $path = FileBackend::normalizeStoragePath( $path );
1715 if ( $path === null ) {
1716 return; // invalid storage path
1717 }
1718 $mtime = ConvertibleTimestamp::convert( TS_UNIX, $val['mtime'] );
1719 $ttl = $this->memCache->adaptiveTTL( $mtime, 7 * 86400, 300, .1 );
1720 $key = $this->fileCacheKey( $path );
1721 // Set the cache unless it is currently salted.
1722 $this->memCache->set( $key, $val, $ttl );
1723 }
1724
1725 /**
1726 * Delete the cached stat info for a file path.
1727 * The cache key is salted for a while to prevent race conditions.
1728 * Since negatives (404s) are not cached, this does not need to be called when
1729 * a file is created at a path were there was none before.
1730 *
1731 * @param string $path Storage path
1732 */
1733 final protected function deleteFileCache( $path ) {
1734 $path = FileBackend::normalizeStoragePath( $path );
1735 if ( $path === null ) {
1736 return; // invalid storage path
1737 }
1738 if ( !$this->memCache->delete( $this->fileCacheKey( $path ), 300 ) ) {
1739 trigger_error( "Unable to delete stat cache for file $path." );
1740 }
1741 }
1742
1743 /**
1744 * Do a batch lookup from cache for file stats for all paths
1745 * used in a list of storage paths or FileOp objects.
1746 * This loads the persistent cache values into the process cache.
1747 *
1748 * @param array $items List of storage paths
1749 */
1750 final protected function primeFileCache( array $items ) {
1751 $ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
1752
1753 $paths = []; // list of storage paths
1754 $pathNames = []; // (cache key => storage path)
1755 // Get all the paths/containers from the items...
1756 foreach ( $items as $item ) {
1757 if ( self::isStoragePath( $item ) ) {
1758 $paths[] = FileBackend::normalizeStoragePath( $item );
1759 }
1760 }
1761 // Get rid of any paths that failed normalization...
1762 $paths = array_filter( $paths, 'strlen' ); // remove nulls
1763 // Get all the corresponding cache keys for paths...
1764 foreach ( $paths as $path ) {
1765 list( , $rel, ) = $this->resolveStoragePath( $path );
1766 if ( $rel !== null ) { // valid path for this backend
1767 $pathNames[$this->fileCacheKey( $path )] = $path;
1768 }
1769 }
1770 // Get all cache entries for these file cache keys...
1771 $values = $this->memCache->getMulti( array_keys( $pathNames ) );
1772 foreach ( $values as $cacheKey => $val ) {
1773 $path = $pathNames[$cacheKey];
1774 if ( is_array( $val ) ) {
1775 $val['latest'] = false; // never completely trust cache
1776 $this->cheapCache->set( $path, 'stat', $val );
1777 if ( isset( $val['sha1'] ) ) { // some backends store SHA-1 as metadata
1778 $this->cheapCache->set( $path, 'sha1',
1779 [ 'hash' => $val['sha1'], 'latest' => false ] );
1780 }
1781 if ( isset( $val['xattr'] ) ) { // some backends store headers/metadata
1782 $val['xattr'] = self::normalizeXAttributes( $val['xattr'] );
1783 $this->cheapCache->set( $path, 'xattr',
1784 [ 'map' => $val['xattr'], 'latest' => false ] );
1785 }
1786 }
1787 }
1788 }
1789
1790 /**
1791 * Normalize file headers/metadata to the FileBackend::getFileXAttributes() format
1792 *
1793 * @param array $xattr
1794 * @return array
1795 * @since 1.22
1796 */
1797 final protected static function normalizeXAttributes( array $xattr ) {
1798 $newXAttr = [ 'headers' => [], 'metadata' => [] ];
1799
1800 foreach ( $xattr['headers'] as $name => $value ) {
1801 $newXAttr['headers'][strtolower( $name )] = $value;
1802 }
1803
1804 foreach ( $xattr['metadata'] as $name => $value ) {
1805 $newXAttr['metadata'][strtolower( $name )] = $value;
1806 }
1807
1808 return $newXAttr;
1809 }
1810
1811 /**
1812 * Set the 'concurrency' option from a list of operation options
1813 *
1814 * @param array $opts Map of operation options
1815 * @return array
1816 */
1817 final protected function setConcurrencyFlags( array $opts ) {
1818 $opts['concurrency'] = 1; // off
1819 if ( $this->parallelize === 'implicit' ) {
1820 if ( !isset( $opts['parallelize'] ) || $opts['parallelize'] ) {
1821 $opts['concurrency'] = $this->concurrency;
1822 }
1823 } elseif ( $this->parallelize === 'explicit' ) {
1824 if ( !empty( $opts['parallelize'] ) ) {
1825 $opts['concurrency'] = $this->concurrency;
1826 }
1827 }
1828
1829 return $opts;
1830 }
1831
1832 /**
1833 * Get the content type to use in HEAD/GET requests for a file
1834 *
1835 * @param string $storagePath
1836 * @param string|null $content File data
1837 * @param string|null $fsPath File system path
1838 * @return string MIME type
1839 */
1840 protected function getContentType( $storagePath, $content, $fsPath ) {
1841 if ( $this->mimeCallback ) {
1842 return call_user_func_array( $this->mimeCallback, func_get_args() );
1843 }
1844
1845 $mime = null;
1846 if ( $fsPath !== null && function_exists( 'finfo_file' ) ) {
1847 $finfo = finfo_open( FILEINFO_MIME_TYPE );
1848 $mime = finfo_file( $finfo, $fsPath );
1849 finfo_close( $finfo );
1850 }
1851
1852 return is_string( $mime ) ? $mime : 'unknown/unknown';
1853 }
1854 }
1855
1856 /**
1857 * FileBackendStore helper class for performing asynchronous file operations.
1858 *
1859 * For example, calling FileBackendStore::createInternal() with the "async"
1860 * param flag may result in a StatusValue that contains this object as a value.
1861 * This class is largely backend-specific and is mostly just "magic" to be
1862 * passed to FileBackendStore::executeOpHandlesInternal().
1863 */
1864 abstract class FileBackendStoreOpHandle {
1865 /** @var array */
1866 public $params = []; // params to caller functions
1867 /** @var FileBackendStore */
1868 public $backend;
1869 /** @var array */
1870 public $resourcesToClose = [];
1871
1872 public $call; // string; name that identifies the function called
1873
1874 /**
1875 * Close all open file handles
1876 */
1877 public function closeResources() {
1878 array_map( 'fclose', $this->resourcesToClose );
1879 }
1880 }
1881
1882 /**
1883 * FileBackendStore helper function to handle listings that span container shards.
1884 * Do not use this class from places outside of FileBackendStore.
1885 *
1886 * @ingroup FileBackend
1887 */
1888 abstract class FileBackendStoreShardListIterator extends FilterIterator {
1889 /** @var FileBackendStore */
1890 protected $backend;
1891
1892 /** @var array */
1893 protected $params;
1894
1895 /** @var string Full container name */
1896 protected $container;
1897
1898 /** @var string Resolved relative path */
1899 protected $directory;
1900
1901 /** @var array */
1902 protected $multiShardPaths = []; // (rel path => 1)
1903
1904 /**
1905 * @param FileBackendStore $backend
1906 * @param string $container Full storage container name
1907 * @param string $dir Storage directory relative to container
1908 * @param array $suffixes List of container shard suffixes
1909 * @param array $params
1910 */
1911 public function __construct(
1912 FileBackendStore $backend, $container, $dir, array $suffixes, array $params
1913 ) {
1914 $this->backend = $backend;
1915 $this->container = $container;
1916 $this->directory = $dir;
1917 $this->params = $params;
1918
1919 $iter = new AppendIterator();
1920 foreach ( $suffixes as $suffix ) {
1921 $iter->append( $this->listFromShard( $this->container . $suffix ) );
1922 }
1923
1924 parent::__construct( $iter );
1925 }
1926
1927 public function accept() {
1928 $rel = $this->getInnerIterator()->current(); // path relative to given directory
1929 $path = $this->params['dir'] . "/{$rel}"; // full storage path
1930 if ( $this->backend->isSingleShardPathInternal( $path ) ) {
1931 return true; // path is only on one shard; no issue with duplicates
1932 } elseif ( isset( $this->multiShardPaths[$rel] ) ) {
1933 // Don't keep listing paths that are on multiple shards
1934 return false;
1935 } else {
1936 $this->multiShardPaths[$rel] = 1;
1937
1938 return true;
1939 }
1940 }
1941
1942 public function rewind() {
1943 parent::rewind();
1944 $this->multiShardPaths = [];
1945 }
1946
1947 /**
1948 * Get the list for a given container shard
1949 *
1950 * @param string $container Resolved container name
1951 * @return Iterator
1952 */
1953 abstract protected function listFromShard( $container );
1954 }
1955
1956 /**
1957 * Iterator for listing directories
1958 */
1959 class FileBackendStoreShardDirIterator extends FileBackendStoreShardListIterator {
1960 protected function listFromShard( $container ) {
1961 $list = $this->backend->getDirectoryListInternal(
1962 $container, $this->directory, $this->params );
1963 if ( $list === null ) {
1964 return new ArrayIterator( [] );
1965 } else {
1966 return is_array( $list ) ? new ArrayIterator( $list ) : $list;
1967 }
1968 }
1969 }
1970
1971 /**
1972 * Iterator for listing regular files
1973 */
1974 class FileBackendStoreShardFileIterator extends FileBackendStoreShardListIterator {
1975 protected function listFromShard( $container ) {
1976 $list = $this->backend->getFileListInternal(
1977 $container, $this->directory, $this->params );
1978 if ( $list === null ) {
1979 return new ArrayIterator( [] );
1980 } else {
1981 return is_array( $list ) ? new ArrayIterator( $list ) : $list;
1982 }
1983 }
1984 }