3 * Base class for all backends using particular storage medium.
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.
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.
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
21 * @ingroup FileBackend
22 * @author Aaron Schulz
26 * @brief Base class for all backends using particular storage medium.
28 * This class defines the methods as abstract that subclasses must implement.
29 * Outside callers should *not* use functions with "Internal" in the name.
31 * The FileBackend operations are implemented using basic functions
32 * such as storeInternal(), copyInternal(), deleteInternal() and the like.
33 * This class is also responsible for path resolution and sanitization.
35 * @ingroup FileBackend
38 abstract class FileBackendStore
extends FileBackend
{
41 /** @var ProcessCacheLRU Map of paths to small (RAM/disk) cache items */
42 protected $cheapCache;
43 /** @var ProcessCacheLRU Map of paths to large (RAM/disk) cache items */
44 protected $expensiveCache;
46 /** @var Array Map of container names to sharding config */
47 protected $shardViaHashLevels = array();
49 /** @var callback Method to get the MIME type of files */
50 protected $mimeCallback;
52 protected $maxFileSize = 4294967296; // integer bytes (4GiB)
54 const CACHE_TTL
= 10; // integer; TTL in seconds for process cache entries
55 const CACHE_CHEAP_SIZE
= 500; // integer; max entries in "cheap cache"
56 const CACHE_EXPENSIVE_SIZE
= 5; // integer; max entries in "expensive cache"
59 * @see FileBackend::__construct()
60 * Additional $config params include:
61 * - mimeCallback : Callback that takes (storage path, content, file system path) and
62 * returns the MIME type of the file or 'unknown/unknown'. The file
63 * system path parameter should be used if the content one is null.
65 * @param array $config
67 public function __construct( array $config ) {
68 parent
::__construct( $config );
69 $this->mimeCallback
= isset( $config['mimeCallback'] )
70 ?
$config['mimeCallback']
71 : function( $storagePath, $content, $fsPath ) {
72 // @TODO: handle the case of extension-less files using the contents
73 return StreamFile
::contentTypeFromPath( $storagePath ) ?
: 'unknown/unknown';
75 $this->memCache
= new EmptyBagOStuff(); // disabled by default
76 $this->cheapCache
= new ProcessCacheLRU( self
::CACHE_CHEAP_SIZE
);
77 $this->expensiveCache
= new ProcessCacheLRU( self
::CACHE_EXPENSIVE_SIZE
);
81 * Get the maximum allowable file size given backend
82 * medium restrictions and basic performance constraints.
83 * Do not call this function from places outside FileBackend and FileOp.
85 * @return integer Bytes
87 final public function maxFileSizeInternal() {
88 return $this->maxFileSize
;
92 * Check if a file can be created or changed at a given storage path.
93 * FS backends should check if the parent directory exists, files can be
94 * written under it, and that any file already there is writable.
95 * Backends using key/value stores should check if the container exists.
97 * @param string $storagePath
100 abstract public function isPathUsableInternal( $storagePath );
103 * Create a file in the backend with the given contents.
104 * This will overwrite any file that exists at the destination.
105 * Do not call this function from places outside FileBackend and FileOp.
108 * - content : the raw file contents
109 * - dst : destination storage path
110 * - headers : HTTP header name/value map
111 * - async : Status will be returned immediately if supported.
112 * If the status is OK, then its value field will be
113 * set to a FileBackendStoreOpHandle object.
114 * - dstExists : Whether a file exists at the destination (optimization).
115 * Callers can use "false" if no existing file is being changed.
117 * @param array $params
120 final public function createInternal( array $params ) {
121 $section = new ProfileSection( __METHOD__
. "-{$this->name}" );
122 if ( strlen( $params['content'] ) > $this->maxFileSizeInternal() ) {
123 $status = Status
::newFatal( 'backend-fail-maxsize',
124 $params['dst'], $this->maxFileSizeInternal() );
126 $status = $this->doCreateInternal( $params );
127 $this->clearCache( array( $params['dst'] ) );
128 if ( !isset( $params['dstExists'] ) ||
$params['dstExists'] ) {
129 $this->deleteFileCache( $params['dst'] ); // persistent cache
136 * @see FileBackendStore::createInternal()
139 abstract protected function doCreateInternal( array $params );
142 * Store a file into the backend from a file on disk.
143 * This will overwrite any file that exists at the destination.
144 * Do not call this function from places outside FileBackend and FileOp.
147 * - src : source path on disk
148 * - dst : destination storage path
149 * - headers : HTTP header name/value map
150 * - async : Status will be returned immediately if supported.
151 * If the status is OK, then its value field will be
152 * set to a FileBackendStoreOpHandle object.
153 * - dstExists : Whether a file exists at the destination (optimization).
154 * Callers can use "false" if no existing file is being changed.
156 * @param array $params
159 final public function storeInternal( array $params ) {
160 $section = new ProfileSection( __METHOD__
. "-{$this->name}" );
161 if ( filesize( $params['src'] ) > $this->maxFileSizeInternal() ) {
162 $status = Status
::newFatal( 'backend-fail-maxsize',
163 $params['dst'], $this->maxFileSizeInternal() );
165 $status = $this->doStoreInternal( $params );
166 $this->clearCache( array( $params['dst'] ) );
167 if ( !isset( $params['dstExists'] ) ||
$params['dstExists'] ) {
168 $this->deleteFileCache( $params['dst'] ); // persistent cache
175 * @see FileBackendStore::storeInternal()
178 abstract protected function doStoreInternal( array $params );
181 * Copy a file from one storage path to another in the backend.
182 * This will overwrite any file that exists at the destination.
183 * Do not call this function from places outside FileBackend and FileOp.
186 * - src : source storage path
187 * - dst : destination storage path
188 * - ignoreMissingSource : do nothing if the source file does not exist
189 * - headers : HTTP header name/value map
190 * - async : Status will be returned immediately if supported.
191 * If the status is OK, then its value field will be
192 * set to a FileBackendStoreOpHandle object.
193 * - dstExists : Whether a file exists at the destination (optimization).
194 * Callers can use "false" if no existing file is being changed.
196 * @param array $params
199 final public function copyInternal( array $params ) {
200 $section = new ProfileSection( __METHOD__
. "-{$this->name}" );
201 $status = $this->doCopyInternal( $params );
202 $this->clearCache( array( $params['dst'] ) );
203 if ( !isset( $params['dstExists'] ) ||
$params['dstExists'] ) {
204 $this->deleteFileCache( $params['dst'] ); // persistent cache
210 * @see FileBackendStore::copyInternal()
213 abstract protected function doCopyInternal( array $params );
216 * Delete a file at the storage path.
217 * Do not call this function from places outside FileBackend and FileOp.
220 * - src : source storage path
221 * - ignoreMissingSource : do nothing if the source file does not exist
222 * - async : Status will be returned immediately if supported.
223 * If the status is OK, then its value field will be
224 * set to a FileBackendStoreOpHandle object.
226 * @param array $params
229 final public function deleteInternal( array $params ) {
230 $section = new ProfileSection( __METHOD__
. "-{$this->name}" );
231 $status = $this->doDeleteInternal( $params );
232 $this->clearCache( array( $params['src'] ) );
233 $this->deleteFileCache( $params['src'] ); // persistent cache
238 * @see FileBackendStore::deleteInternal()
241 abstract protected function doDeleteInternal( array $params );
244 * Move a file from one storage path to another in the backend.
245 * This will overwrite any file that exists at the destination.
246 * Do not call this function from places outside FileBackend and FileOp.
249 * - src : source storage path
250 * - dst : destination storage path
251 * - ignoreMissingSource : do nothing if the source file does not exist
252 * - headers : HTTP header name/value map
253 * - async : Status will be returned immediately if supported.
254 * If the status is OK, then its value field will be
255 * set to a FileBackendStoreOpHandle object.
256 * - dstExists : Whether a file exists at the destination (optimization).
257 * Callers can use "false" if no existing file is being changed.
259 * @param array $params
262 final public function moveInternal( array $params ) {
263 $section = new ProfileSection( __METHOD__
. "-{$this->name}" );
264 $status = $this->doMoveInternal( $params );
265 $this->clearCache( array( $params['src'], $params['dst'] ) );
266 $this->deleteFileCache( $params['src'] ); // persistent cache
267 if ( !isset( $params['dstExists'] ) ||
$params['dstExists'] ) {
268 $this->deleteFileCache( $params['dst'] ); // persistent cache
274 * @see FileBackendStore::moveInternal()
277 protected function doMoveInternal( array $params ) {
278 unset( $params['async'] ); // two steps, won't work here :)
279 $nsrc = FileBackend
::normalizeStoragePath( $params['src'] );
280 $ndst = FileBackend
::normalizeStoragePath( $params['dst'] );
281 // Copy source to dest
282 $status = $this->copyInternal( $params );
283 if ( $nsrc !== $ndst && $status->isOK() ) {
284 // Delete source (only fails due to races or network problems)
285 $status->merge( $this->deleteInternal( array( 'src' => $params['src'] ) ) );
286 $status->setResult( true, $status->value
); // ignore delete() errors
292 * Alter metadata for a file at the storage path.
293 * Do not call this function from places outside FileBackend and FileOp.
296 * - src : source storage path
297 * - headers : HTTP header name/value map
298 * - async : Status will be returned immediately if supported.
299 * If the status is OK, then its value field will be
300 * set to a FileBackendStoreOpHandle object.
302 * @param array $params
305 final public function describeInternal( array $params ) {
306 $section = new ProfileSection( __METHOD__
. "-{$this->name}" );
307 if ( count( $params['headers'] ) ) {
308 $status = $this->doDescribeInternal( $params );
309 $this->clearCache( array( $params['src'] ) );
310 $this->deleteFileCache( $params['src'] ); // persistent cache
312 $status = Status
::newGood(); // nothing to do
318 * @see FileBackendStore::describeInternal()
321 protected function doDescribeInternal( array $params ) {
322 return Status
::newGood();
326 * No-op file operation that does nothing.
327 * Do not call this function from places outside FileBackend and FileOp.
329 * @param array $params
332 final public function nullInternal( array $params ) {
333 return Status
::newGood();
336 final public function concatenate( array $params ) {
337 $section = new ProfileSection( __METHOD__
. "-{$this->name}" );
338 $status = Status
::newGood();
340 // Try to lock the source files for the scope of this function
341 $scopeLockS = $this->getScopedFileLocks( $params['srcs'], LockManager
::LOCK_UW
, $status );
342 if ( $status->isOK() ) {
343 // Actually do the file concatenation...
344 $start_time = microtime( true );
345 $status->merge( $this->doConcatenate( $params ) );
346 $sec = microtime( true ) - $start_time;
347 if ( !$status->isOK() ) {
348 wfDebugLog( 'FileOperation', get_class( $this ) . " failed to concatenate " .
349 count( $params['srcs'] ) . " file(s) [$sec sec]" );
357 * @see FileBackendStore::concatenate()
360 protected function doConcatenate( array $params ) {
361 $status = Status
::newGood();
362 $tmpPath = $params['dst']; // convenience
363 unset( $params['latest'] ); // sanity
365 // Check that the specified temp file is valid...
366 wfSuppressWarnings();
367 $ok = ( is_file( $tmpPath ) && filesize( $tmpPath ) == 0 );
369 if ( !$ok ) { // not present or not empty
370 $status->fatal( 'backend-fail-opentemp', $tmpPath );
374 // Get local FS versions of the chunks needed for the concatenation...
375 $fsFiles = $this->getLocalReferenceMulti( $params );
376 foreach ( $fsFiles as $path => &$fsFile ) {
377 if ( !$fsFile ) { // chunk failed to download?
378 $fsFile = $this->getLocalReference( array( 'src' => $path ) );
379 if ( !$fsFile ) { // retry failed?
380 $status->fatal( 'backend-fail-read', $path );
385 unset( $fsFile ); // unset reference so we can reuse $fsFile
387 // Get a handle for the destination temp file
388 $tmpHandle = fopen( $tmpPath, 'ab' );
389 if ( $tmpHandle === false ) {
390 $status->fatal( 'backend-fail-opentemp', $tmpPath );
394 // Build up the temp file using the source chunks (in order)...
395 foreach ( $fsFiles as $virtualSource => $fsFile ) {
396 // Get a handle to the local FS version
397 $sourceHandle = fopen( $fsFile->getPath(), 'rb' );
398 if ( $sourceHandle === false ) {
399 fclose( $tmpHandle );
400 $status->fatal( 'backend-fail-read', $virtualSource );
403 // Append chunk to file (pass chunk size to avoid magic quotes)
404 if ( !stream_copy_to_stream( $sourceHandle, $tmpHandle ) ) {
405 fclose( $sourceHandle );
406 fclose( $tmpHandle );
407 $status->fatal( 'backend-fail-writetemp', $tmpPath );
410 fclose( $sourceHandle );
412 if ( !fclose( $tmpHandle ) ) {
413 $status->fatal( 'backend-fail-closetemp', $tmpPath );
417 clearstatcache(); // temp file changed
422 final protected function doPrepare( array $params ) {
423 $section = new ProfileSection( __METHOD__
. "-{$this->name}" );
424 $status = Status
::newGood();
426 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
427 if ( $dir === null ) {
428 $status->fatal( 'backend-fail-invalidpath', $params['dir'] );
429 return $status; // invalid storage path
432 if ( $shard !== null ) { // confined to a single container/shard
433 $status->merge( $this->doPrepareInternal( $fullCont, $dir, $params ) );
434 } else { // directory is on several shards
435 wfDebug( __METHOD__
. ": iterating over all container shards.\n" );
436 list( , $shortCont, ) = self
::splitStoragePath( $params['dir'] );
437 foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
438 $status->merge( $this->doPrepareInternal( "{$fullCont}{$suffix}", $dir, $params ) );
446 * @see FileBackendStore::doPrepare()
449 protected function doPrepareInternal( $container, $dir, array $params ) {
450 return Status
::newGood();
453 final protected function doSecure( array $params ) {
454 $section = new ProfileSection( __METHOD__
. "-{$this->name}" );
455 $status = Status
::newGood();
457 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
458 if ( $dir === null ) {
459 $status->fatal( 'backend-fail-invalidpath', $params['dir'] );
460 return $status; // invalid storage path
463 if ( $shard !== null ) { // confined to a single container/shard
464 $status->merge( $this->doSecureInternal( $fullCont, $dir, $params ) );
465 } else { // directory is on several shards
466 wfDebug( __METHOD__
. ": iterating over all container shards.\n" );
467 list( , $shortCont, ) = self
::splitStoragePath( $params['dir'] );
468 foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
469 $status->merge( $this->doSecureInternal( "{$fullCont}{$suffix}", $dir, $params ) );
477 * @see FileBackendStore::doSecure()
480 protected function doSecureInternal( $container, $dir, array $params ) {
481 return Status
::newGood();
484 final protected function doPublish( array $params ) {
485 $section = new ProfileSection( __METHOD__
. "-{$this->name}" );
486 $status = Status
::newGood();
488 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
489 if ( $dir === null ) {
490 $status->fatal( 'backend-fail-invalidpath', $params['dir'] );
491 return $status; // invalid storage path
494 if ( $shard !== null ) { // confined to a single container/shard
495 $status->merge( $this->doPublishInternal( $fullCont, $dir, $params ) );
496 } else { // directory is on several shards
497 wfDebug( __METHOD__
. ": iterating over all container shards.\n" );
498 list( , $shortCont, ) = self
::splitStoragePath( $params['dir'] );
499 foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
500 $status->merge( $this->doPublishInternal( "{$fullCont}{$suffix}", $dir, $params ) );
508 * @see FileBackendStore::doPublish()
511 protected function doPublishInternal( $container, $dir, array $params ) {
512 return Status
::newGood();
515 final protected function doClean( array $params ) {
516 $section = new ProfileSection( __METHOD__
. "-{$this->name}" );
517 $status = Status
::newGood();
519 // Recursive: first delete all empty subdirs recursively
520 if ( !empty( $params['recursive'] ) && !$this->directoriesAreVirtual() ) {
521 $subDirsRel = $this->getTopDirectoryList( array( 'dir' => $params['dir'] ) );
522 if ( $subDirsRel !== null ) { // no errors
523 foreach ( $subDirsRel as $subDirRel ) {
524 $subDir = $params['dir'] . "/{$subDirRel}"; // full path
525 $status->merge( $this->doClean( array( 'dir' => $subDir ) +
$params ) );
527 unset( $subDirsRel ); // free directory for rmdir() on Windows (for FS backends)
531 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
532 if ( $dir === null ) {
533 $status->fatal( 'backend-fail-invalidpath', $params['dir'] );
534 return $status; // invalid storage path
537 // Attempt to lock this directory...
538 $filesLockEx = array( $params['dir'] );
539 $scopedLockE = $this->getScopedFileLocks( $filesLockEx, LockManager
::LOCK_EX
, $status );
540 if ( !$status->isOK() ) {
541 return $status; // abort
544 if ( $shard !== null ) { // confined to a single container/shard
545 $status->merge( $this->doCleanInternal( $fullCont, $dir, $params ) );
546 $this->deleteContainerCache( $fullCont ); // purge cache
547 } else { // directory is on several shards
548 wfDebug( __METHOD__
. ": iterating over all container shards.\n" );
549 list( , $shortCont, ) = self
::splitStoragePath( $params['dir'] );
550 foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
551 $status->merge( $this->doCleanInternal( "{$fullCont}{$suffix}", $dir, $params ) );
552 $this->deleteContainerCache( "{$fullCont}{$suffix}" ); // purge cache
560 * @see FileBackendStore::doClean()
563 protected function doCleanInternal( $container, $dir, array $params ) {
564 return Status
::newGood();
567 final public function fileExists( array $params ) {
568 $section = new ProfileSection( __METHOD__
. "-{$this->name}" );
569 $stat = $this->getFileStat( $params );
570 return ( $stat === null ) ?
null : (bool)$stat; // null => failure
573 final public function getFileTimestamp( array $params ) {
574 $section = new ProfileSection( __METHOD__
. "-{$this->name}" );
575 $stat = $this->getFileStat( $params );
576 return $stat ?
$stat['mtime'] : false;
579 final public function getFileSize( array $params ) {
580 $section = new ProfileSection( __METHOD__
. "-{$this->name}" );
581 $stat = $this->getFileStat( $params );
582 return $stat ?
$stat['size'] : false;
585 final public function getFileStat( array $params ) {
586 $path = self
::normalizeStoragePath( $params['src'] );
587 if ( $path === null ) {
588 return false; // invalid storage path
590 $section = new ProfileSection( __METHOD__
. "-{$this->name}" );
591 $latest = !empty( $params['latest'] ); // use latest data?
592 if ( !$this->cheapCache
->has( $path, 'stat', self
::CACHE_TTL
) ) {
593 $this->primeFileCache( array( $path ) ); // check persistent cache
595 if ( $this->cheapCache
->has( $path, 'stat', self
::CACHE_TTL
) ) {
596 $stat = $this->cheapCache
->get( $path, 'stat' );
597 // If we want the latest data, check that this cached
598 // value was in fact fetched with the latest available data.
599 if ( is_array( $stat ) ) {
600 if ( !$latest ||
$stat['latest'] ) {
603 } elseif ( in_array( $stat, array( 'NOT_EXIST', 'NOT_EXIST_LATEST' ) ) ) {
604 if ( !$latest ||
$stat === 'NOT_EXIST_LATEST' ) {
609 wfProfileIn( __METHOD__
. '-miss' );
610 wfProfileIn( __METHOD__
. '-miss-' . $this->name
);
611 $stat = $this->doGetFileStat( $params );
612 wfProfileOut( __METHOD__
. '-miss-' . $this->name
);
613 wfProfileOut( __METHOD__
. '-miss' );
614 if ( is_array( $stat ) ) { // file exists
615 $stat['latest'] = $latest;
616 $this->cheapCache
->set( $path, 'stat', $stat );
617 $this->setFileCache( $path, $stat ); // update persistent cache
618 if ( isset( $stat['sha1'] ) ) { // some backends store SHA-1 as metadata
619 $this->cheapCache
->set( $path, 'sha1',
620 array( 'hash' => $stat['sha1'], 'latest' => $latest ) );
622 } elseif ( $stat === false ) { // file does not exist
623 $this->cheapCache
->set( $path, 'stat', $latest ?
'NOT_EXIST_LATEST' : 'NOT_EXIST' );
624 $this->cheapCache
->set( $path, 'sha1', // the SHA-1 must be false too
625 array( 'hash' => false, 'latest' => $latest ) );
626 wfDebug( __METHOD__
. ": File $path does not exist.\n" );
627 } else { // an error occurred
628 wfDebug( __METHOD__
. ": Could not stat file $path.\n" );
634 * @see FileBackendStore::getFileStat()
636 abstract protected function doGetFileStat( array $params );
638 public function getFileContentsMulti( array $params ) {
639 $section = new ProfileSection( __METHOD__
. "-{$this->name}" );
641 $params = $this->setConcurrencyFlags( $params );
642 $contents = $this->doGetFileContentsMulti( $params );
648 * @see FileBackendStore::getFileContentsMulti()
651 protected function doGetFileContentsMulti( array $params ) {
653 foreach ( $this->doGetLocalReferenceMulti( $params ) as $path => $fsFile ) {
654 wfSuppressWarnings();
655 $contents[$path] = $fsFile ?
file_get_contents( $fsFile->getPath() ) : false;
661 final public function getFileSha1Base36( array $params ) {
662 $path = self
::normalizeStoragePath( $params['src'] );
663 if ( $path === null ) {
664 return false; // invalid storage path
666 $section = new ProfileSection( __METHOD__
. "-{$this->name}" );
667 $latest = !empty( $params['latest'] ); // use latest data?
668 if ( $this->cheapCache
->has( $path, 'sha1', self
::CACHE_TTL
) ) {
669 $stat = $this->cheapCache
->get( $path, 'sha1' );
670 // If we want the latest data, check that this cached
671 // value was in fact fetched with the latest available data.
672 if ( !$latest ||
$stat['latest'] ) {
673 return $stat['hash'];
676 wfProfileIn( __METHOD__
. '-miss' );
677 wfProfileIn( __METHOD__
. '-miss-' . $this->name
);
678 $hash = $this->doGetFileSha1Base36( $params );
679 wfProfileOut( __METHOD__
. '-miss-' . $this->name
);
680 wfProfileOut( __METHOD__
. '-miss' );
681 $this->cheapCache
->set( $path, 'sha1', array( 'hash' => $hash, 'latest' => $latest ) );
686 * @see FileBackendStore::getFileSha1Base36()
687 * @return bool|string
689 protected function doGetFileSha1Base36( array $params ) {
690 $fsFile = $this->getLocalReference( $params );
694 return $fsFile->getSha1Base36();
698 final public function getFileProps( array $params ) {
699 $section = new ProfileSection( __METHOD__
. "-{$this->name}" );
700 $fsFile = $this->getLocalReference( $params );
701 $props = $fsFile ?
$fsFile->getProps() : FSFile
::placeholderProps();
705 final public function getLocalReferenceMulti( array $params ) {
706 $section = new ProfileSection( __METHOD__
. "-{$this->name}" );
708 $params = $this->setConcurrencyFlags( $params );
710 $fsFiles = array(); // (path => FSFile)
711 $latest = !empty( $params['latest'] ); // use latest data?
712 // Reuse any files already in process cache...
713 foreach ( $params['srcs'] as $src ) {
714 $path = self
::normalizeStoragePath( $src );
715 if ( $path === null ) {
716 $fsFiles[$src] = null; // invalid storage path
717 } elseif ( $this->expensiveCache
->has( $path, 'localRef' ) ) {
718 $val = $this->expensiveCache
->get( $path, 'localRef' );
719 // If we want the latest data, check that this cached
720 // value was in fact fetched with the latest available data.
721 if ( !$latest ||
$val['latest'] ) {
722 $fsFiles[$src] = $val['object'];
726 // Fetch local references of any remaning files...
727 $params['srcs'] = array_diff( $params['srcs'], array_keys( $fsFiles ) );
728 foreach ( $this->doGetLocalReferenceMulti( $params ) as $path => $fsFile ) {
729 $fsFiles[$path] = $fsFile;
730 if ( $fsFile ) { // update the process cache...
731 $this->expensiveCache
->set( $path, 'localRef',
732 array( 'object' => $fsFile, 'latest' => $latest ) );
740 * @see FileBackendStore::getLocalReferenceMulti()
743 protected function doGetLocalReferenceMulti( array $params ) {
744 return $this->doGetLocalCopyMulti( $params );
747 final public function getLocalCopyMulti( array $params ) {
748 $section = new ProfileSection( __METHOD__
. "-{$this->name}" );
750 $params = $this->setConcurrencyFlags( $params );
751 $tmpFiles = $this->doGetLocalCopyMulti( $params );
757 * @see FileBackendStore::getLocalCopyMulti()
760 abstract protected function doGetLocalCopyMulti( array $params );
763 * @see FileBackend::getFileHttpUrl()
764 * @return string|null
766 public function getFileHttpUrl( array $params ) {
767 return null; // not supported
770 final public function streamFile( array $params ) {
771 $section = new ProfileSection( __METHOD__
. "-{$this->name}" );
772 $status = Status
::newGood();
774 $info = $this->getFileStat( $params );
775 if ( !$info ) { // let StreamFile handle the 404
776 $status->fatal( 'backend-fail-notexists', $params['src'] );
779 // Set output buffer and HTTP headers for stream
780 $extraHeaders = isset( $params['headers'] ) ?
$params['headers'] : array();
781 $res = StreamFile
::prepareForStream( $params['src'], $info, $extraHeaders );
782 if ( $res == StreamFile
::NOT_MODIFIED
) {
783 // do nothing; client cache is up to date
784 } elseif ( $res == StreamFile
::READY_STREAM
) {
785 wfProfileIn( __METHOD__
. '-send' );
786 wfProfileIn( __METHOD__
. '-send-' . $this->name
);
787 $status = $this->doStreamFile( $params );
788 wfProfileOut( __METHOD__
. '-send-' . $this->name
);
789 wfProfileOut( __METHOD__
. '-send' );
790 if ( !$status->isOK() ) {
791 // Per bug 41113, nasty things can happen if bad cache entries get
792 // stuck in cache. It's also possible that this error can come up
793 // with simple race conditions. Clear out the stat cache to be safe.
794 $this->clearCache( array( $params['src'] ) );
795 $this->deleteFileCache( $params['src'] );
796 trigger_error( "Bad stat cache or race condition for file {$params['src']}." );
799 $status->fatal( 'backend-fail-stream', $params['src'] );
806 * @see FileBackendStore::streamFile()
809 protected function doStreamFile( array $params ) {
810 $status = Status
::newGood();
812 $fsFile = $this->getLocalReference( $params );
814 $status->fatal( 'backend-fail-stream', $params['src'] );
815 } elseif ( !readfile( $fsFile->getPath() ) ) {
816 $status->fatal( 'backend-fail-stream', $params['src'] );
822 final public function directoryExists( array $params ) {
823 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
824 if ( $dir === null ) {
825 return false; // invalid storage path
827 if ( $shard !== null ) { // confined to a single container/shard
828 return $this->doDirectoryExists( $fullCont, $dir, $params );
829 } else { // directory is on several shards
830 wfDebug( __METHOD__
. ": iterating over all container shards.\n" );
831 list( , $shortCont, ) = self
::splitStoragePath( $params['dir'] );
832 $res = false; // response
833 foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
834 $exists = $this->doDirectoryExists( "{$fullCont}{$suffix}", $dir, $params );
838 } elseif ( $exists === null ) { // error?
839 $res = null; // if we don't find anything, it is indeterminate
847 * @see FileBackendStore::directoryExists()
849 * @param string $container Resolved container name
850 * @param string $dir Resolved path relative to container
851 * @param array $params
854 abstract protected function doDirectoryExists( $container, $dir, array $params );
856 final public function getDirectoryList( array $params ) {
857 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
858 if ( $dir === null ) { // invalid storage path
861 if ( $shard !== null ) {
862 // File listing is confined to a single container/shard
863 return $this->getDirectoryListInternal( $fullCont, $dir, $params );
865 wfDebug( __METHOD__
. ": iterating over all container shards.\n" );
866 // File listing spans multiple containers/shards
867 list( , $shortCont, ) = self
::splitStoragePath( $params['dir'] );
868 return new FileBackendStoreShardDirIterator( $this,
869 $fullCont, $dir, $this->getContainerSuffixes( $shortCont ), $params );
874 * Do not call this function from places outside FileBackend
876 * @see FileBackendStore::getDirectoryList()
878 * @param string $container Resolved container name
879 * @param string $dir Resolved path relative to container
880 * @param array $params
881 * @return Traversable|Array|null Returns null on failure
883 abstract public function getDirectoryListInternal( $container, $dir, array $params );
885 final public function getFileList( array $params ) {
886 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
887 if ( $dir === null ) { // invalid storage path
890 if ( $shard !== null ) {
891 // File listing is confined to a single container/shard
892 return $this->getFileListInternal( $fullCont, $dir, $params );
894 wfDebug( __METHOD__
. ": iterating over all container shards.\n" );
895 // File listing spans multiple containers/shards
896 list( , $shortCont, ) = self
::splitStoragePath( $params['dir'] );
897 return new FileBackendStoreShardFileIterator( $this,
898 $fullCont, $dir, $this->getContainerSuffixes( $shortCont ), $params );
903 * Do not call this function from places outside FileBackend
905 * @see FileBackendStore::getFileList()
907 * @param string $container Resolved container name
908 * @param string $dir Resolved path relative to container
909 * @param array $params
910 * @return Traversable|Array|null Returns null on failure
912 abstract public function getFileListInternal( $container, $dir, array $params );
915 * Return a list of FileOp objects from a list of operations.
916 * Do not call this function from places outside FileBackend.
918 * The result must have the same number of items as the input.
919 * An exception is thrown if an unsupported operation is requested.
921 * @param array $ops Same format as doOperations()
922 * @return Array List of FileOp objects
923 * @throws MWException
925 final public function getOperationsInternal( array $ops ) {
926 $supportedOps = array(
927 'store' => 'StoreFileOp',
928 'copy' => 'CopyFileOp',
929 'move' => 'MoveFileOp',
930 'delete' => 'DeleteFileOp',
931 'create' => 'CreateFileOp',
932 'describe' => 'DescribeFileOp',
933 'null' => 'NullFileOp'
936 $performOps = array(); // array of FileOp objects
937 // Build up ordered array of FileOps...
938 foreach ( $ops as $operation ) {
939 $opName = $operation['op'];
940 if ( isset( $supportedOps[$opName] ) ) {
941 $class = $supportedOps[$opName];
942 // Get params for this operation
943 $params = $operation;
944 // Append the FileOp class
945 $performOps[] = new $class( $this, $params );
947 throw new MWException( "Operation '$opName' is not supported." );
955 * Get a list of storage paths to lock for a list of operations
956 * Returns an array with LockManager::LOCK_UW (shared locks) and
957 * LockManager::LOCK_EX (exclusive locks) keys, each corresponding
958 * to a list of storage paths to be locked. All returned paths are
961 * @param array $performOps List of FileOp objects
962 * @return Array (LockManager::LOCK_UW => path list, LockManager::LOCK_EX => path list)
964 final public function getPathsToLockForOpsInternal( array $performOps ) {
965 // Build up a list of files to lock...
966 $paths = array( 'sh' => array(), 'ex' => array() );
967 foreach ( $performOps as $fileOp ) {
968 $paths['sh'] = array_merge( $paths['sh'], $fileOp->storagePathsRead() );
969 $paths['ex'] = array_merge( $paths['ex'], $fileOp->storagePathsChanged() );
971 // Optimization: if doing an EX lock anyway, don't also set an SH one
972 $paths['sh'] = array_diff( $paths['sh'], $paths['ex'] );
973 // Get a shared lock on the parent directory of each path changed
974 $paths['sh'] = array_merge( $paths['sh'], array_map( 'dirname', $paths['ex'] ) );
977 LockManager
::LOCK_UW
=> $paths['sh'],
978 LockManager
::LOCK_EX
=> $paths['ex']
982 public function getScopedLocksForOps( array $ops, Status
$status ) {
983 $paths = $this->getPathsToLockForOpsInternal( $this->getOperationsInternal( $ops ) );
984 return array( $this->getScopedFileLocks( $paths, 'mixed', $status ) );
987 final protected function doOperationsInternal( array $ops, array $opts ) {
988 $section = new ProfileSection( __METHOD__
. "-{$this->name}" );
989 $status = Status
::newGood();
991 // Fix up custom header name/value pairs...
992 $ops = array_map( array( $this, 'stripInvalidHeadersFromOp' ), $ops );
994 // Build up a list of FileOps...
995 $performOps = $this->getOperationsInternal( $ops );
997 // Acquire any locks as needed...
998 if ( empty( $opts['nonLocking'] ) ) {
999 // Build up a list of files to lock...
1000 $paths = $this->getPathsToLockForOpsInternal( $performOps );
1001 // Try to lock those files for the scope of this function...
1002 $scopeLock = $this->getScopedFileLocks( $paths, 'mixed', $status );
1003 if ( !$status->isOK() ) {
1004 return $status; // abort
1008 // Clear any file cache entries (after locks acquired)
1009 if ( empty( $opts['preserveCache'] ) ) {
1010 $this->clearCache();
1013 // Load from the persistent file and container caches
1014 $this->primeFileCache( $performOps );
1015 $this->primeContainerCache( $performOps );
1017 // Actually attempt the operation batch...
1018 $opts = $this->setConcurrencyFlags( $opts );
1019 $subStatus = FileOpBatch
::attempt( $performOps, $opts, $this->fileJournal
);
1021 // Merge errors into status fields
1022 $status->merge( $subStatus );
1023 $status->success
= $subStatus->success
; // not done in merge()
1028 final protected function doQuickOperationsInternal( array $ops ) {
1029 $section = new ProfileSection( __METHOD__
. "-{$this->name}" );
1030 $status = Status
::newGood();
1032 // Fix up custom header name/value pairs...
1033 $ops = array_map( array( $this, 'stripInvalidHeadersFromOp' ), $ops );
1035 // Clear any file cache entries
1036 $this->clearCache();
1038 $supportedOps = array( 'create', 'store', 'copy', 'move', 'delete', 'describe', 'null' );
1039 $async = ( $this->parallelize
=== 'implicit' && count( $ops ) > 1 );
1040 $maxConcurrency = $this->concurrency
; // throttle
1042 $statuses = array(); // array of (index => Status)
1043 $fileOpHandles = array(); // list of (index => handle) arrays
1044 $curFileOpHandles = array(); // current handle batch
1045 // Perform the sync-only ops and build up op handles for the async ops...
1046 foreach ( $ops as $index => $params ) {
1047 if ( !in_array( $params['op'], $supportedOps ) ) {
1048 throw new MWException( "Operation '{$params['op']}' is not supported." );
1050 $method = $params['op'] . 'Internal'; // e.g. "storeInternal"
1051 $subStatus = $this->$method( array( 'async' => $async ) +
$params );
1052 if ( $subStatus->value
instanceof FileBackendStoreOpHandle
) { // async
1053 if ( count( $curFileOpHandles ) >= $maxConcurrency ) {
1054 $fileOpHandles[] = $curFileOpHandles; // push this batch
1055 $curFileOpHandles = array();
1057 $curFileOpHandles[$index] = $subStatus->value
; // keep index
1058 } else { // error or completed
1059 $statuses[$index] = $subStatus; // keep index
1062 if ( count( $curFileOpHandles ) ) {
1063 $fileOpHandles[] = $curFileOpHandles; // last batch
1065 // Do all the async ops that can be done concurrently...
1066 foreach ( $fileOpHandles as $fileHandleBatch ) {
1067 $statuses = $statuses +
$this->executeOpHandlesInternal( $fileHandleBatch );
1069 // Marshall and merge all the responses...
1070 foreach ( $statuses as $index => $subStatus ) {
1071 $status->merge( $subStatus );
1072 if ( $subStatus->isOK() ) {
1073 $status->success
[$index] = true;
1074 ++
$status->successCount
;
1076 $status->success
[$index] = false;
1077 ++
$status->failCount
;
1085 * Execute a list of FileBackendStoreOpHandle handles in parallel.
1086 * The resulting Status object fields will correspond
1087 * to the order in which the handles where given.
1089 * @param array $handles List of FileBackendStoreOpHandle objects
1090 * @return Array Map of Status objects
1091 * @throws MWException
1093 final public function executeOpHandlesInternal( array $fileOpHandles ) {
1094 $section = new ProfileSection( __METHOD__
. "-{$this->name}" );
1095 foreach ( $fileOpHandles as $fileOpHandle ) {
1096 if ( !( $fileOpHandle instanceof FileBackendStoreOpHandle
) ) {
1097 throw new MWException( "Given a non-FileBackendStoreOpHandle object." );
1098 } elseif ( $fileOpHandle->backend
->getName() !== $this->getName() ) {
1099 throw new MWException( "Given a FileBackendStoreOpHandle for the wrong backend." );
1102 $res = $this->doExecuteOpHandlesInternal( $fileOpHandles );
1103 foreach ( $fileOpHandles as $fileOpHandle ) {
1104 $fileOpHandle->closeResources();
1110 * @see FileBackendStore::executeOpHandlesInternal()
1111 * @param array $fileOpHandles
1112 * @throws MWException
1113 * @return Array List of corresponding Status objects
1115 protected function doExecuteOpHandlesInternal( array $fileOpHandles ) {
1116 foreach ( $fileOpHandles as $fileOpHandle ) { // OK if empty
1117 throw new MWException( "This backend supports no asynchronous operations." );
1123 * Strip long HTTP headers from a file operation.
1124 * Most headers are just numbers, but some are allowed to be long.
1125 * This function is useful for cleaning up headers and avoiding backend
1126 * specific errors, especially in the middle of batch file operations.
1128 * @param array $op Same format as doOperation()
1131 protected function stripInvalidHeadersFromOp( array $op ) {
1132 static $longs = array( 'Content-Disposition' );
1133 if ( isset( $op['headers'] ) ) { // op sets HTTP headers
1134 foreach ( $op['headers'] as $name => $value ) {
1135 $maxHVLen = in_array( $name, $longs ) ? INF
: 255;
1136 if ( strlen( $name ) > 255 ||
strlen( $value ) > $maxHVLen ) {
1137 trigger_error( "Header '$name: $value' is too long." );
1138 unset( $op['headers'][$name] );
1139 } elseif ( !strlen( $value ) ) {
1140 $op['headers'][$name] = ''; // null/false => ""
1147 final public function preloadCache( array $paths ) {
1148 $fullConts = array(); // full container names
1149 foreach ( $paths as $path ) {
1150 list( $fullCont, , ) = $this->resolveStoragePath( $path );
1151 $fullConts[] = $fullCont;
1153 // Load from the persistent file and container caches
1154 $this->primeContainerCache( $fullConts );
1155 $this->primeFileCache( $paths );
1158 final public function clearCache( array $paths = null ) {
1159 if ( is_array( $paths ) ) {
1160 $paths = array_map( 'FileBackend::normalizeStoragePath', $paths );
1161 $paths = array_filter( $paths, 'strlen' ); // remove nulls
1163 if ( $paths === null ) {
1164 $this->cheapCache
->clear();
1165 $this->expensiveCache
->clear();
1167 foreach ( $paths as $path ) {
1168 $this->cheapCache
->clear( $path );
1169 $this->expensiveCache
->clear( $path );
1172 $this->doClearCache( $paths );
1176 * Clears any additional stat caches for storage paths
1178 * @see FileBackend::clearCache()
1180 * @param array $paths Storage paths (optional)
1183 protected function doClearCache( array $paths = null ) {}
1186 * Is this a key/value store where directories are just virtual?
1187 * Virtual directories exists in so much as files exists that are
1188 * prefixed with the directory path followed by a forward slash.
1192 abstract protected function directoriesAreVirtual();
1195 * Check if a container name is valid.
1196 * This checks for for length and illegal characters.
1198 * @param string $container
1201 final protected static function isValidContainerName( $container ) {
1202 // This accounts for Swift and S3 restrictions while leaving room
1203 // for things like '.xxx' (hex shard chars) or '.seg' (segments).
1204 // This disallows directory separators or traversal characters.
1205 // Note that matching strings URL encode to the same string;
1206 // in Swift, the length restriction is *after* URL encoding.
1207 return preg_match( '/^[a-z0-9][a-z0-9-_]{0,199}$/i', $container );
1211 * Splits a storage path into an internal container name,
1212 * an internal relative file name, and a container shard suffix.
1213 * Any shard suffix is already appended to the internal container name.
1214 * This also checks that the storage path is valid and within this backend.
1216 * If the container is sharded but a suffix could not be determined,
1217 * this means that the path can only refer to a directory and can only
1218 * be scanned by looking in all the container shards.
1220 * @param string $storagePath
1221 * @return Array (container, path, container suffix) or (null, null, null) if invalid
1223 final protected function resolveStoragePath( $storagePath ) {
1224 list( $backend, $container, $relPath ) = self
::splitStoragePath( $storagePath );
1225 if ( $backend === $this->name
) { // must be for this backend
1226 $relPath = self
::normalizeContainerPath( $relPath );
1227 if ( $relPath !== null ) {
1228 // Get shard for the normalized path if this container is sharded
1229 $cShard = $this->getContainerShard( $container, $relPath );
1230 // Validate and sanitize the relative path (backend-specific)
1231 $relPath = $this->resolveContainerPath( $container, $relPath );
1232 if ( $relPath !== null ) {
1233 // Prepend any wiki ID prefix to the container name
1234 $container = $this->fullContainerName( $container );
1235 if ( self
::isValidContainerName( $container ) ) {
1236 // Validate and sanitize the container name (backend-specific)
1237 $container = $this->resolveContainerName( "{$container}{$cShard}" );
1238 if ( $container !== null ) {
1239 return array( $container, $relPath, $cShard );
1245 return array( null, null, null );
1249 * Like resolveStoragePath() except null values are returned if
1250 * the container is sharded and the shard could not be determined
1251 * or if the path ends with '/'. The later case is illegal for FS
1252 * backends and can confuse listings for object store backends.
1254 * This function is used when resolving paths that must be valid
1255 * locations for files. Directory and listing functions should
1256 * generally just use resolveStoragePath() instead.
1258 * @see FileBackendStore::resolveStoragePath()
1260 * @param string $storagePath
1261 * @return Array (container, path) or (null, null) if invalid
1263 final protected function resolveStoragePathReal( $storagePath ) {
1264 list( $container, $relPath, $cShard ) = $this->resolveStoragePath( $storagePath );
1265 if ( $cShard !== null && substr( $relPath, -1 ) !== '/' ) {
1266 return array( $container, $relPath );
1268 return array( null, null );
1272 * Get the container name shard suffix for a given path.
1273 * Any empty suffix means the container is not sharded.
1275 * @param string $container Container name
1276 * @param string $relPath Storage path relative to the container
1277 * @return string|null Returns null if shard could not be determined
1279 final protected function getContainerShard( $container, $relPath ) {
1280 list( $levels, $base, $repeat ) = $this->getContainerHashLevels( $container );
1281 if ( $levels == 1 ||
$levels == 2 ) {
1282 // Hash characters are either base 16 or 36
1283 $char = ( $base == 36 ) ?
'[0-9a-z]' : '[0-9a-f]';
1284 // Get a regex that represents the shard portion of paths.
1285 // The concatenation of the captures gives us the shard.
1286 if ( $levels === 1 ) { // 16 or 36 shards per container
1287 $hashDirRegex = '(' . $char . ')';
1288 } else { // 256 or 1296 shards per container
1289 if ( $repeat ) { // verbose hash dir format (e.g. "a/ab/abc")
1290 $hashDirRegex = $char . '/(' . $char . '{2})';
1291 } else { // short hash dir format (e.g. "a/b/c")
1292 $hashDirRegex = '(' . $char . ')/(' . $char . ')';
1295 // Allow certain directories to be above the hash dirs so as
1296 // to work with FileRepo (e.g. "archive/a/ab" or "temp/a/ab").
1297 // They must be 2+ chars to avoid any hash directory ambiguity.
1299 if ( preg_match( "!^(?:[^/]{2,}/)*$hashDirRegex(?:/|$)!", $relPath, $m ) ) {
1300 return '.' . implode( '', array_slice( $m, 1 ) );
1302 return null; // failed to match
1304 return ''; // no sharding
1308 * Check if a storage path maps to a single shard.
1309 * Container dirs like "a", where the container shards on "x/xy",
1310 * can reside on several shards. Such paths are tricky to handle.
1312 * @param string $storagePath Storage path
1315 final public function isSingleShardPathInternal( $storagePath ) {
1316 list( , , $shard ) = $this->resolveStoragePath( $storagePath );
1317 return ( $shard !== null );
1321 * Get the sharding config for a container.
1322 * If greater than 0, then all file storage paths within
1323 * the container are required to be hashed accordingly.
1325 * @param string $container
1326 * @return Array (integer levels, integer base, repeat flag) or (0, 0, false)
1328 final protected function getContainerHashLevels( $container ) {
1329 if ( isset( $this->shardViaHashLevels
[$container] ) ) {
1330 $config = $this->shardViaHashLevels
[$container];
1331 $hashLevels = (int)$config['levels'];
1332 if ( $hashLevels == 1 ||
$hashLevels == 2 ) {
1333 $hashBase = (int)$config['base'];
1334 if ( $hashBase == 16 ||
$hashBase == 36 ) {
1335 return array( $hashLevels, $hashBase, $config['repeat'] );
1339 return array( 0, 0, false ); // no sharding
1343 * Get a list of full container shard suffixes for a container
1345 * @param string $container
1348 final protected function getContainerSuffixes( $container ) {
1350 list( $digits, $base ) = $this->getContainerHashLevels( $container );
1351 if ( $digits > 0 ) {
1352 $numShards = pow( $base, $digits );
1353 for ( $index = 0; $index < $numShards; $index++
) {
1354 $shards[] = '.' . wfBaseConvert( $index, 10, $base, $digits );
1361 * Get the full container name, including the wiki ID prefix
1363 * @param string $container
1366 final protected function fullContainerName( $container ) {
1367 if ( $this->wikiId
!= '' ) {
1368 return "{$this->wikiId}-$container";
1375 * Resolve a container name, checking if it's allowed by the backend.
1376 * This is intended for internal use, such as encoding illegal chars.
1377 * Subclasses can override this to be more restrictive.
1379 * @param string $container
1380 * @return string|null
1382 protected function resolveContainerName( $container ) {
1387 * Resolve a relative storage path, checking if it's allowed by the backend.
1388 * This is intended for internal use, such as encoding illegal chars or perhaps
1389 * getting absolute paths (e.g. FS based backends). Note that the relative path
1390 * may be the empty string (e.g. the path is simply to the container).
1392 * @param string $container Container name
1393 * @param string $relStoragePath Storage path relative to the container
1394 * @return string|null Path or null if not valid
1396 protected function resolveContainerPath( $container, $relStoragePath ) {
1397 return $relStoragePath;
1401 * Get the cache key for a container
1403 * @param string $container Resolved container name
1406 private function containerCacheKey( $container ) {
1407 return wfMemcKey( 'backend', $this->getName(), 'container', $container );
1411 * Set the cached info for a container
1413 * @param string $container Resolved container name
1414 * @param array $val Information to cache
1417 final protected function setContainerCache( $container, array $val ) {
1418 $this->memCache
->add( $this->containerCacheKey( $container ), $val, 14 * 86400 );
1422 * Delete the cached info for a container.
1423 * The cache key is salted for a while to prevent race conditions.
1425 * @param string $container Resolved container name
1428 final protected function deleteContainerCache( $container ) {
1429 if ( !$this->memCache
->set( $this->containerCacheKey( $container ), 'PURGED', 300 ) ) {
1430 trigger_error( "Unable to delete stat cache for container $container." );
1435 * Do a batch lookup from cache for container stats for all containers
1436 * used in a list of container names, storage paths, or FileOp objects.
1437 * This loads the persistent cache values into the process cache.
1439 * @param Array $items
1442 final protected function primeContainerCache( array $items ) {
1443 $section = new ProfileSection( __METHOD__
. "-{$this->name}" );
1445 $paths = array(); // list of storage paths
1446 $contNames = array(); // (cache key => resolved container name)
1447 // Get all the paths/containers from the items...
1448 foreach ( $items as $item ) {
1449 if ( $item instanceof FileOp
) {
1450 $paths = array_merge( $paths, $item->storagePathsRead() );
1451 $paths = array_merge( $paths, $item->storagePathsChanged() );
1452 } elseif ( self
::isStoragePath( $item ) ) {
1454 } elseif ( is_string( $item ) ) { // full container name
1455 $contNames[$this->containerCacheKey( $item )] = $item;
1458 // Get all the corresponding cache keys for paths...
1459 foreach ( $paths as $path ) {
1460 list( $fullCont, , ) = $this->resolveStoragePath( $path );
1461 if ( $fullCont !== null ) { // valid path for this backend
1462 $contNames[$this->containerCacheKey( $fullCont )] = $fullCont;
1466 $contInfo = array(); // (resolved container name => cache value)
1467 // Get all cache entries for these container cache keys...
1468 $values = $this->memCache
->getMulti( array_keys( $contNames ) );
1469 foreach ( $values as $cacheKey => $val ) {
1470 $contInfo[$contNames[$cacheKey]] = $val;
1473 // Populate the container process cache for the backend...
1474 $this->doPrimeContainerCache( array_filter( $contInfo, 'is_array' ) );
1478 * Fill the backend-specific process cache given an array of
1479 * resolved container names and their corresponding cached info.
1480 * Only containers that actually exist should appear in the map.
1482 * @param array $containerInfo Map of resolved container names to cached info
1485 protected function doPrimeContainerCache( array $containerInfo ) {}
1488 * Get the cache key for a file path
1490 * @param string $path Normalized storage path
1493 private function fileCacheKey( $path ) {
1494 return wfMemcKey( 'backend', $this->getName(), 'file', sha1( $path ) );
1498 * Set the cached stat info for a file path.
1499 * Negatives (404s) are not cached. By not caching negatives, we can skip cache
1500 * salting for the case when a file is created at a path were there was none before.
1502 * @param string $path Storage path
1503 * @param array $val Stat information to cache
1506 final protected function setFileCache( $path, array $val ) {
1507 $path = FileBackend
::normalizeStoragePath( $path );
1508 if ( $path === null ) {
1509 return; // invalid storage path
1511 $age = time() - wfTimestamp( TS_UNIX
, $val['mtime'] );
1512 $ttl = min( 7 * 86400, max( 300, floor( .1 * $age ) ) );
1513 $this->memCache
->add( $this->fileCacheKey( $path ), $val, $ttl );
1517 * Delete the cached stat info for a file path.
1518 * The cache key is salted for a while to prevent race conditions.
1519 * Since negatives (404s) are not cached, this does not need to be called when
1520 * a file is created at a path were there was none before.
1522 * @param string $path Storage path
1525 final protected function deleteFileCache( $path ) {
1526 $path = FileBackend
::normalizeStoragePath( $path );
1527 if ( $path === null ) {
1528 return; // invalid storage path
1530 if ( !$this->memCache
->set( $this->fileCacheKey( $path ), 'PURGED', 300 ) ) {
1531 trigger_error( "Unable to delete stat cache for file $path." );
1536 * Do a batch lookup from cache for file stats for all paths
1537 * used in a list of storage paths or FileOp objects.
1538 * This loads the persistent cache values into the process cache.
1540 * @param array $items List of storage paths or FileOps
1543 final protected function primeFileCache( array $items ) {
1544 $section = new ProfileSection( __METHOD__
. "-{$this->name}" );
1546 $paths = array(); // list of storage paths
1547 $pathNames = array(); // (cache key => storage path)
1548 // Get all the paths/containers from the items...
1549 foreach ( $items as $item ) {
1550 if ( $item instanceof FileOp
) {
1551 $paths = array_merge( $paths, $item->storagePathsRead() );
1552 $paths = array_merge( $paths, $item->storagePathsChanged() );
1553 } elseif ( self
::isStoragePath( $item ) ) {
1554 $paths[] = FileBackend
::normalizeStoragePath( $item );
1557 // Get rid of any paths that failed normalization...
1558 $paths = array_filter( $paths, 'strlen' ); // remove nulls
1559 // Get all the corresponding cache keys for paths...
1560 foreach ( $paths as $path ) {
1561 list( , $rel, ) = $this->resolveStoragePath( $path );
1562 if ( $rel !== null ) { // valid path for this backend
1563 $pathNames[$this->fileCacheKey( $path )] = $path;
1566 // Get all cache entries for these container cache keys...
1567 $values = $this->memCache
->getMulti( array_keys( $pathNames ) );
1568 foreach ( $values as $cacheKey => $val ) {
1569 if ( is_array( $val ) ) {
1570 $path = $pathNames[$cacheKey];
1571 $this->cheapCache
->set( $path, 'stat', $val );
1572 if ( isset( $val['sha1'] ) ) { // some backends store SHA-1 as metadata
1573 $this->cheapCache
->set( $path, 'sha1',
1574 array( 'hash' => $val['sha1'], 'latest' => $val['latest'] ) );
1581 * Set the 'concurrency' option from a list of operation options
1583 * @param array $opts Map of operation options
1586 final protected function setConcurrencyFlags( array $opts ) {
1587 $opts['concurrency'] = 1; // off
1588 if ( $this->parallelize
=== 'implicit' ) {
1589 if ( !isset( $opts['parallelize'] ) ||
$opts['parallelize'] ) {
1590 $opts['concurrency'] = $this->concurrency
;
1592 } elseif ( $this->parallelize
=== 'explicit' ) {
1593 if ( !empty( $opts['parallelize'] ) ) {
1594 $opts['concurrency'] = $this->concurrency
;
1601 * Get the content type to use in HEAD/GET requests for a file
1603 * @param string $storagePath
1604 * @param string|null $content File data
1605 * @param string|null $fsPath File system path
1608 protected function getContentType( $storagePath, $content, $fsPath ) {
1609 return call_user_func_array( $this->mimeCallback
, func_get_args() );
1614 * FileBackendStore helper class for performing asynchronous file operations.
1616 * For example, calling FileBackendStore::createInternal() with the "async"
1617 * param flag may result in a Status that contains this object as a value.
1618 * This class is largely backend-specific and is mostly just "magic" to be
1619 * passed to FileBackendStore::executeOpHandlesInternal().
1621 abstract class FileBackendStoreOpHandle
{
1623 public $params = array(); // params to caller functions
1624 /** @var FileBackendStore */
1627 public $resourcesToClose = array();
1629 public $call; // string; name that identifies the function called
1632 * Close all open file handles
1636 public function closeResources() {
1637 array_map( 'fclose', $this->resourcesToClose
);
1642 * FileBackendStore helper function to handle listings that span container shards.
1643 * Do not use this class from places outside of FileBackendStore.
1645 * @ingroup FileBackend
1647 abstract class FileBackendStoreShardListIterator
extends FilterIterator
{
1648 /** @var FileBackendStore */
1653 protected $container; // string; full container name
1654 protected $directory; // string; resolved relative path
1657 protected $multiShardPaths = array(); // (rel path => 1)
1660 * @param FileBackendStore $backend
1661 * @param string $container Full storage container name
1662 * @param string $dir Storage directory relative to container
1663 * @param array $suffixes List of container shard suffixes
1664 * @param array $params
1666 public function __construct(
1667 FileBackendStore
$backend, $container, $dir, array $suffixes, array $params
1669 $this->backend
= $backend;
1670 $this->container
= $container;
1671 $this->directory
= $dir;
1672 $this->params
= $params;
1674 $iter = new AppendIterator();
1675 foreach ( $suffixes as $suffix ) {
1676 $iter->append( $this->listFromShard( $this->container
. $suffix ) );
1679 parent
::__construct( $iter );
1682 public function accept() {
1683 $rel = $this->getInnerIterator()->current(); // path relative to given directory
1684 $path = $this->params
['dir'] . "/{$rel}"; // full storage path
1685 if ( $this->backend
->isSingleShardPathInternal( $path ) ) {
1686 return true; // path is only on one shard; no issue with duplicates
1687 } elseif ( isset( $this->multiShardPaths
[$rel] ) ) {
1688 // Don't keep listing paths that are on multiple shards
1691 $this->multiShardPaths
[$rel] = 1;
1696 public function rewind() {
1698 $this->multiShardPaths
= array();
1702 * Get the list for a given container shard
1704 * @param string $container Resolved container name
1707 abstract protected function listFromShard( $container );
1711 * Iterator for listing directories
1713 class FileBackendStoreShardDirIterator
extends FileBackendStoreShardListIterator
{
1714 protected function listFromShard( $container ) {
1715 $list = $this->backend
->getDirectoryListInternal(
1716 $container, $this->directory
, $this->params
);
1717 if ( $list === null ) {
1718 return new ArrayIterator( array() );
1720 return is_array( $list ) ?
new ArrayIterator( $list ) : $list;
1726 * Iterator for listing regular files
1728 class FileBackendStoreShardFileIterator
extends FileBackendStoreShardListIterator
{
1729 protected function listFromShard( $container ) {
1730 $list = $this->backend
->getFileListInternal(
1731 $container, $this->directory
, $this->params
);
1732 if ( $list === null ) {
1733 return new ArrayIterator( array() );
1735 return is_array( $list ) ?
new ArrayIterator( $list ) : $list;