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