2012-03-03 18:29:38 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
2012-05-07 07:11:33 +00:00
|
|
|
* Base class for all backends using particular storage medium.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
2012-03-03 18:29:38 +00:00
|
|
|
* @file
|
|
|
|
|
* @ingroup FileBackend
|
|
|
|
|
*/
|
2019-07-17 11:55:19 +00:00
|
|
|
|
2019-08-20 14:09:16 +00:00
|
|
|
use Wikimedia\AtEase\AtEase;
|
2016-10-02 04:51:51 +00:00
|
|
|
use Wikimedia\Timestamp\ConvertibleTimestamp;
|
2012-03-03 18:29:38 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Base class for all backends using particular storage medium.
|
|
|
|
|
*
|
|
|
|
|
* This class defines the methods as abstract that subclasses must implement.
|
|
|
|
|
* Outside callers should *not* use functions with "Internal" in the name.
|
2012-04-06 20:13:06 +00:00
|
|
|
*
|
2012-03-03 18:29:38 +00:00
|
|
|
* The FileBackend operations are implemented using basic functions
|
|
|
|
|
* such as storeInternal(), copyInternal(), deleteInternal() and the like.
|
|
|
|
|
* This class is also responsible for path resolution and sanitization.
|
2012-04-06 20:13:06 +00:00
|
|
|
*
|
2020-07-13 09:00:30 +00:00
|
|
|
* @stable to extend
|
2012-03-03 18:29:38 +00:00
|
|
|
* @ingroup FileBackend
|
|
|
|
|
* @since 1.19
|
|
|
|
|
*/
|
|
|
|
|
abstract class FileBackendStore extends FileBackend {
|
2015-04-27 21:13:02 +00:00
|
|
|
/** @var WANObjectCache */
|
2012-04-23 20:27:58 +00:00
|
|
|
protected $memCache;
|
2016-09-21 23:12:27 +00:00
|
|
|
/** @var BagOStuff */
|
|
|
|
|
protected $srvCache;
|
2018-07-11 12:53:13 +00:00
|
|
|
/** @var MapCacheLRU Map of paths to small (RAM/disk) cache items */
|
2013-09-23 19:58:26 +00:00
|
|
|
protected $cheapCache;
|
2018-07-11 12:53:13 +00:00
|
|
|
/** @var MapCacheLRU Map of paths to large (RAM/disk) cache items */
|
2013-09-23 19:58:26 +00:00
|
|
|
protected $expensiveCache;
|
2012-03-03 18:29:38 +00:00
|
|
|
|
2013-11-23 18:23:32 +00:00
|
|
|
/** @var array Map of container names to sharding config */
|
2016-02-17 09:09:32 +00:00
|
|
|
protected $shardViaHashLevels = [];
|
2013-09-23 19:58:26 +00:00
|
|
|
|
2014-04-19 06:43:31 +00:00
|
|
|
/** @var callable Method to get the MIME type of files */
|
2013-09-23 19:58:26 +00:00
|
|
|
protected $mimeCallback;
|
2012-03-03 18:29:38 +00:00
|
|
|
|
2012-03-07 02:00:55 +00:00
|
|
|
protected $maxFileSize = 4294967296; // integer bytes (4GiB)
|
2012-03-03 18:29:38 +00:00
|
|
|
|
2020-05-16 01:32:53 +00:00
|
|
|
protected const CACHE_TTL = 10; // integer; TTL in seconds for process cache entries
|
|
|
|
|
protected const CACHE_CHEAP_SIZE = 500; // integer; max entries in "cheap cache"
|
|
|
|
|
protected const CACHE_EXPENSIVE_SIZE = 5; // integer; max entries in "expensive cache"
|
2012-11-16 20:02:42 +00:00
|
|
|
|
2019-08-30 07:01:29 +00:00
|
|
|
/** @var false Idiom for "no result due to missing file" (since 1.34) */
|
|
|
|
|
protected static $RES_ABSENT = false;
|
|
|
|
|
/** @var null Idiom for "no result due to I/O errors" (since 1.34) */
|
|
|
|
|
protected static $RES_ERROR = null;
|
|
|
|
|
|
|
|
|
|
/** @var string File does not exist according to a normal stat query */
|
|
|
|
|
protected static $ABSENT_NORMAL = 'FNE-N';
|
|
|
|
|
/** @var string File does not exist according to a "latest"-mode stat query */
|
|
|
|
|
protected static $ABSENT_LATEST = 'FNE-L';
|
|
|
|
|
|
2012-04-23 20:27:58 +00:00
|
|
|
/**
|
|
|
|
|
* @see FileBackend::__construct()
|
2013-09-23 19:58:26 +00:00
|
|
|
* Additional $config params include:
|
2018-02-06 02:11:27 +00:00
|
|
|
* - srvCache : BagOStuff cache to APC or the like.
|
2014-11-14 06:47:06 +00:00
|
|
|
* - wanCache : WANObjectCache object to use for persistent caching.
|
2013-09-23 19:58:26 +00:00
|
|
|
* - mimeCallback : Callback that takes (storage path, content, file system path) and
|
|
|
|
|
* returns the MIME type of the file or 'unknown/unknown'. The file
|
|
|
|
|
* system path parameter should be used if the content one is null.
|
2012-04-23 20:27:58 +00:00
|
|
|
*
|
2020-07-13 08:53:06 +00:00
|
|
|
* @stable to call
|
2020-07-03 12:07:47 +00:00
|
|
|
*
|
2013-06-13 18:18:52 +00:00
|
|
|
* @param array $config
|
2012-04-23 20:27:58 +00:00
|
|
|
*/
|
|
|
|
|
public function __construct( array $config ) {
|
|
|
|
|
parent::__construct( $config );
|
2017-10-06 22:17:58 +00:00
|
|
|
$this->mimeCallback = $config['mimeCallback'] ?? null;
|
2016-09-21 23:12:27 +00:00
|
|
|
$this->srvCache = new EmptyBagOStuff(); // disabled by default
|
2015-04-27 21:13:02 +00:00
|
|
|
$this->memCache = WANObjectCache::newEmpty(); // disabled by default
|
2018-07-11 12:53:13 +00:00
|
|
|
$this->cheapCache = new MapCacheLRU( self::CACHE_CHEAP_SIZE );
|
|
|
|
|
$this->expensiveCache = new MapCacheLRU( self::CACHE_EXPENSIVE_SIZE );
|
2012-04-23 20:27:58 +00:00
|
|
|
}
|
|
|
|
|
|
2012-03-03 18:29:38 +00:00
|
|
|
/**
|
|
|
|
|
* Get the maximum allowable file size given backend
|
|
|
|
|
* medium restrictions and basic performance constraints.
|
|
|
|
|
* Do not call this function from places outside FileBackend and FileOp.
|
2012-04-06 20:13:06 +00:00
|
|
|
*
|
2013-11-23 18:23:32 +00:00
|
|
|
* @return int Bytes
|
2012-03-03 18:29:38 +00:00
|
|
|
*/
|
|
|
|
|
final public function maxFileSizeInternal() {
|
|
|
|
|
return $this->maxFileSize;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-08-30 07:01:29 +00:00
|
|
|
* Check if a file can be created or changed at a given storage path in the backend
|
|
|
|
|
*
|
|
|
|
|
* FS backends should check that the parent directory exists, files can be written
|
|
|
|
|
* under it, and that any file already there is both readable and writable.
|
2012-03-03 18:29:38 +00:00
|
|
|
* Backends using key/value stores should check if the container exists.
|
|
|
|
|
*
|
2013-06-13 18:18:52 +00:00
|
|
|
* @param string $storagePath
|
2012-03-03 18:29:38 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
abstract public function isPathUsableInternal( $storagePath );
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a file in the backend with the given contents.
|
2012-10-29 23:56:43 +00:00
|
|
|
* This will overwrite any file that exists at the destination.
|
2012-03-03 18:29:38 +00:00
|
|
|
* Do not call this function from places outside FileBackend and FileOp.
|
2012-04-06 20:13:06 +00:00
|
|
|
*
|
2012-03-03 18:29:38 +00:00
|
|
|
* $params include:
|
2012-10-24 08:18:29 +00:00
|
|
|
* - content : the raw file contents
|
|
|
|
|
* - dst : destination storage path
|
|
|
|
|
* - headers : HTTP header name/value map
|
2016-09-16 22:55:40 +00:00
|
|
|
* - async : StatusValue will be returned immediately if supported.
|
|
|
|
|
* If the StatusValue is OK, then its value field will be
|
2012-10-24 08:18:29 +00:00
|
|
|
* set to a FileBackendStoreOpHandle object.
|
2012-11-16 23:25:50 +00:00
|
|
|
* - dstExists : Whether a file exists at the destination (optimization).
|
|
|
|
|
* Callers can use "false" if no existing file is being changed.
|
2012-04-06 20:13:06 +00:00
|
|
|
*
|
2013-03-11 18:00:35 +00:00
|
|
|
* @param array $params
|
2016-09-16 22:55:40 +00:00
|
|
|
* @return StatusValue
|
2012-03-03 18:29:38 +00:00
|
|
|
*/
|
|
|
|
|
final public function createInternal( array $params ) {
|
2019-07-17 11:55:19 +00:00
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */
|
2016-09-18 21:40:38 +00:00
|
|
|
$ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
|
2019-08-30 07:01:29 +00:00
|
|
|
|
2012-03-03 18:29:38 +00:00
|
|
|
if ( strlen( $params['content'] ) > $this->maxFileSizeInternal() ) {
|
2016-09-16 22:55:40 +00:00
|
|
|
$status = $this->newStatus( 'backend-fail-maxsize',
|
2012-03-09 16:01:11 +00:00
|
|
|
$params['dst'], $this->maxFileSizeInternal() );
|
2012-03-03 18:29:38 +00:00
|
|
|
} else {
|
|
|
|
|
$status = $this->doCreateInternal( $params );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->clearCache( [ $params['dst'] ] );
|
2020-03-05 14:59:37 +00:00
|
|
|
if ( $params['dstExists'] ?? true ) {
|
2012-11-16 23:25:50 +00:00
|
|
|
$this->deleteFileCache( $params['dst'] ); // persistent cache
|
|
|
|
|
}
|
2012-03-03 18:29:38 +00:00
|
|
|
}
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2012-03-03 18:29:38 +00:00
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see FileBackendStore::createInternal()
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param array $params
|
2016-09-16 22:55:40 +00:00
|
|
|
* @return StatusValue
|
2012-03-03 18:29:38 +00:00
|
|
|
*/
|
|
|
|
|
abstract protected function doCreateInternal( array $params );
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Store a file into the backend from a file on disk.
|
2012-10-29 23:56:43 +00:00
|
|
|
* This will overwrite any file that exists at the destination.
|
2012-03-03 18:29:38 +00:00
|
|
|
* Do not call this function from places outside FileBackend and FileOp.
|
2012-04-06 20:13:06 +00:00
|
|
|
*
|
2012-03-03 18:29:38 +00:00
|
|
|
* $params include:
|
2012-10-24 08:18:29 +00:00
|
|
|
* - src : source path on disk
|
|
|
|
|
* - dst : destination storage path
|
|
|
|
|
* - headers : HTTP header name/value map
|
2016-09-16 22:55:40 +00:00
|
|
|
* - async : StatusValue will be returned immediately if supported.
|
|
|
|
|
* If the StatusValue is OK, then its value field will be
|
2012-10-24 08:18:29 +00:00
|
|
|
* set to a FileBackendStoreOpHandle object.
|
2012-11-16 23:25:50 +00:00
|
|
|
* - dstExists : Whether a file exists at the destination (optimization).
|
|
|
|
|
* Callers can use "false" if no existing file is being changed.
|
2012-04-06 20:13:06 +00:00
|
|
|
*
|
2013-03-11 18:00:35 +00:00
|
|
|
* @param array $params
|
2016-09-16 22:55:40 +00:00
|
|
|
* @return StatusValue
|
2012-03-03 18:29:38 +00:00
|
|
|
*/
|
|
|
|
|
final public function storeInternal( array $params ) {
|
2019-07-17 11:55:19 +00:00
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */
|
2016-09-18 21:40:38 +00:00
|
|
|
$ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
|
2019-08-30 07:01:29 +00:00
|
|
|
|
2012-03-03 18:29:38 +00:00
|
|
|
if ( filesize( $params['src'] ) > $this->maxFileSizeInternal() ) {
|
2016-09-16 22:55:40 +00:00
|
|
|
$status = $this->newStatus( 'backend-fail-maxsize',
|
2012-05-17 18:28:05 +00:00
|
|
|
$params['dst'], $this->maxFileSizeInternal() );
|
2012-03-03 18:29:38 +00:00
|
|
|
} else {
|
|
|
|
|
$status = $this->doStoreInternal( $params );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->clearCache( [ $params['dst'] ] );
|
2020-03-05 14:59:37 +00:00
|
|
|
if ( $params['dstExists'] ?? true ) {
|
2012-11-16 23:25:50 +00:00
|
|
|
$this->deleteFileCache( $params['dst'] ); // persistent cache
|
|
|
|
|
}
|
2012-03-03 18:29:38 +00:00
|
|
|
}
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2012-03-03 18:29:38 +00:00
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see FileBackendStore::storeInternal()
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param array $params
|
2016-09-16 22:55:40 +00:00
|
|
|
* @return StatusValue
|
2012-03-03 18:29:38 +00:00
|
|
|
*/
|
|
|
|
|
abstract protected function doStoreInternal( array $params );
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Copy a file from one storage path to another in the backend.
|
2012-10-29 23:56:43 +00:00
|
|
|
* This will overwrite any file that exists at the destination.
|
2012-03-03 18:29:38 +00:00
|
|
|
* Do not call this function from places outside FileBackend and FileOp.
|
2012-04-06 20:13:06 +00:00
|
|
|
*
|
2012-03-03 18:29:38 +00:00
|
|
|
* $params include:
|
2012-10-29 19:57:04 +00:00
|
|
|
* - src : source storage path
|
|
|
|
|
* - dst : destination storage path
|
|
|
|
|
* - ignoreMissingSource : do nothing if the source file does not exist
|
2013-03-09 23:09:25 +00:00
|
|
|
* - headers : HTTP header name/value map
|
2016-09-16 22:55:40 +00:00
|
|
|
* - async : StatusValue will be returned immediately if supported.
|
|
|
|
|
* If the StatusValue is OK, then its value field will be
|
2012-10-29 19:57:04 +00:00
|
|
|
* set to a FileBackendStoreOpHandle object.
|
2012-11-16 23:25:50 +00:00
|
|
|
* - dstExists : Whether a file exists at the destination (optimization).
|
|
|
|
|
* Callers can use "false" if no existing file is being changed.
|
2012-04-06 20:13:06 +00:00
|
|
|
*
|
2013-03-11 18:00:35 +00:00
|
|
|
* @param array $params
|
2016-09-16 22:55:40 +00:00
|
|
|
* @return StatusValue
|
2012-03-03 18:29:38 +00:00
|
|
|
*/
|
|
|
|
|
final public function copyInternal( array $params ) {
|
2019-07-17 11:55:19 +00:00
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */
|
2016-09-18 21:40:38 +00:00
|
|
|
$ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
|
2019-08-30 07:01:29 +00:00
|
|
|
|
2012-03-03 18:29:38 +00:00
|
|
|
$status = $this->doCopyInternal( $params );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->clearCache( [ $params['dst'] ] );
|
2020-03-05 14:59:37 +00:00
|
|
|
if ( $params['dstExists'] ?? true ) {
|
2012-11-16 23:25:50 +00:00
|
|
|
$this->deleteFileCache( $params['dst'] ); // persistent cache
|
|
|
|
|
}
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2012-03-03 18:29:38 +00:00
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see FileBackendStore::copyInternal()
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param array $params
|
2016-09-16 22:55:40 +00:00
|
|
|
* @return StatusValue
|
2012-03-03 18:29:38 +00:00
|
|
|
*/
|
|
|
|
|
abstract protected function doCopyInternal( array $params );
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Delete a file at the storage path.
|
|
|
|
|
* Do not call this function from places outside FileBackend and FileOp.
|
2012-04-06 20:13:06 +00:00
|
|
|
*
|
2012-03-03 18:29:38 +00:00
|
|
|
* $params include:
|
2012-07-18 19:08:30 +00:00
|
|
|
* - src : source storage path
|
|
|
|
|
* - ignoreMissingSource : do nothing if the source file does not exist
|
2016-09-16 22:55:40 +00:00
|
|
|
* - async : StatusValue will be returned immediately if supported.
|
|
|
|
|
* If the StatusValue is OK, then its value field will be
|
2012-04-11 17:51:02 +00:00
|
|
|
* set to a FileBackendStoreOpHandle object.
|
2012-04-06 20:13:06 +00:00
|
|
|
*
|
2013-03-11 18:00:35 +00:00
|
|
|
* @param array $params
|
2016-09-16 22:55:40 +00:00
|
|
|
* @return StatusValue
|
2012-03-03 18:29:38 +00:00
|
|
|
*/
|
|
|
|
|
final public function deleteInternal( array $params ) {
|
2019-07-17 11:55:19 +00:00
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */
|
2016-09-18 21:40:38 +00:00
|
|
|
$ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
|
2019-08-30 07:01:29 +00:00
|
|
|
|
2012-03-03 18:29:38 +00:00
|
|
|
$status = $this->doDeleteInternal( $params );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->clearCache( [ $params['src'] ] );
|
2012-04-27 00:46:28 +00:00
|
|
|
$this->deleteFileCache( $params['src'] ); // persistent cache
|
2012-03-03 18:29:38 +00:00
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see FileBackendStore::deleteInternal()
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param array $params
|
2016-09-16 22:55:40 +00:00
|
|
|
* @return StatusValue
|
2012-03-03 18:29:38 +00:00
|
|
|
*/
|
|
|
|
|
abstract protected function doDeleteInternal( array $params );
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Move a file from one storage path to another in the backend.
|
2012-10-29 23:56:43 +00:00
|
|
|
* This will overwrite any file that exists at the destination.
|
2012-03-03 18:29:38 +00:00
|
|
|
* Do not call this function from places outside FileBackend and FileOp.
|
2012-04-06 20:13:06 +00:00
|
|
|
*
|
2012-03-03 18:29:38 +00:00
|
|
|
* $params include:
|
2012-10-29 19:57:04 +00:00
|
|
|
* - src : source storage path
|
|
|
|
|
* - dst : destination storage path
|
|
|
|
|
* - ignoreMissingSource : do nothing if the source file does not exist
|
2013-03-09 23:09:25 +00:00
|
|
|
* - headers : HTTP header name/value map
|
2016-09-16 22:55:40 +00:00
|
|
|
* - async : StatusValue will be returned immediately if supported.
|
|
|
|
|
* If the StatusValue is OK, then its value field will be
|
2012-10-29 19:57:04 +00:00
|
|
|
* set to a FileBackendStoreOpHandle object.
|
2012-11-16 23:25:50 +00:00
|
|
|
* - dstExists : Whether a file exists at the destination (optimization).
|
|
|
|
|
* Callers can use "false" if no existing file is being changed.
|
2012-04-06 20:13:06 +00:00
|
|
|
*
|
2013-03-11 18:00:35 +00:00
|
|
|
* @param array $params
|
2016-09-16 22:55:40 +00:00
|
|
|
* @return StatusValue
|
2012-03-03 18:29:38 +00:00
|
|
|
*/
|
|
|
|
|
final public function moveInternal( array $params ) {
|
2019-07-17 11:55:19 +00:00
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */
|
2016-09-18 21:40:38 +00:00
|
|
|
$ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
|
2019-08-30 07:01:29 +00:00
|
|
|
|
2012-03-03 18:29:38 +00:00
|
|
|
$status = $this->doMoveInternal( $params );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->clearCache( [ $params['src'], $params['dst'] ] );
|
2012-04-27 00:46:28 +00:00
|
|
|
$this->deleteFileCache( $params['src'] ); // persistent cache
|
2020-03-05 14:59:37 +00:00
|
|
|
if ( $params['dstExists'] ?? true ) {
|
2012-11-16 23:25:50 +00:00
|
|
|
$this->deleteFileCache( $params['dst'] ); // persistent cache
|
|
|
|
|
}
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2012-03-03 18:29:38 +00:00
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see FileBackendStore::moveInternal()
|
2020-07-13 08:57:12 +00:00
|
|
|
* @stable to override
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param array $params
|
2016-09-16 22:55:40 +00:00
|
|
|
* @return StatusValue
|
2012-03-03 18:29:38 +00:00
|
|
|
*/
|
|
|
|
|
protected function doMoveInternal( array $params ) {
|
2012-04-11 17:51:02 +00:00
|
|
|
unset( $params['async'] ); // two steps, won't work here :)
|
2013-03-20 23:25:05 +00:00
|
|
|
$nsrc = FileBackend::normalizeStoragePath( $params['src'] );
|
2013-04-02 20:58:01 +00:00
|
|
|
$ndst = FileBackend::normalizeStoragePath( $params['dst'] );
|
2012-03-03 18:29:38 +00:00
|
|
|
// Copy source to dest
|
|
|
|
|
$status = $this->copyInternal( $params );
|
2013-03-20 23:25:05 +00:00
|
|
|
if ( $nsrc !== $ndst && $status->isOK() ) {
|
|
|
|
|
// Delete source (only fails due to races or network problems)
|
2016-02-17 09:09:32 +00:00
|
|
|
$status->merge( $this->deleteInternal( [ 'src' => $params['src'] ] ) );
|
2012-03-03 18:29:38 +00:00
|
|
|
$status->setResult( true, $status->value ); // ignore delete() errors
|
|
|
|
|
}
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2012-03-03 18:29:38 +00:00
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-24 08:18:29 +00:00
|
|
|
/**
|
|
|
|
|
* Alter metadata for a file at the storage path.
|
|
|
|
|
* Do not call this function from places outside FileBackend and FileOp.
|
|
|
|
|
*
|
|
|
|
|
* $params include:
|
|
|
|
|
* - src : source storage path
|
|
|
|
|
* - headers : HTTP header name/value map
|
2016-09-16 22:55:40 +00:00
|
|
|
* - async : StatusValue will be returned immediately if supported.
|
|
|
|
|
* If the StatusValue is OK, then its value field will be
|
2012-10-24 08:18:29 +00:00
|
|
|
* set to a FileBackendStoreOpHandle object.
|
|
|
|
|
*
|
2013-03-11 18:00:35 +00:00
|
|
|
* @param array $params
|
2016-09-16 22:55:40 +00:00
|
|
|
* @return StatusValue
|
2012-10-24 08:18:29 +00:00
|
|
|
*/
|
|
|
|
|
final public function describeInternal( array $params ) {
|
2019-07-17 11:55:19 +00:00
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */
|
2016-09-18 21:40:38 +00:00
|
|
|
$ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
|
2019-08-30 07:01:29 +00:00
|
|
|
|
2013-03-20 23:25:05 +00:00
|
|
|
if ( count( $params['headers'] ) ) {
|
|
|
|
|
$status = $this->doDescribeInternal( $params );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->clearCache( [ $params['src'] ] );
|
2013-03-20 23:25:05 +00:00
|
|
|
$this->deleteFileCache( $params['src'] ); // persistent cache
|
|
|
|
|
} else {
|
2016-09-16 22:55:40 +00:00
|
|
|
$status = $this->newStatus(); // nothing to do
|
2013-03-20 23:25:05 +00:00
|
|
|
}
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2012-10-24 08:18:29 +00:00
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see FileBackendStore::describeInternal()
|
2020-07-13 08:57:12 +00:00
|
|
|
* @stable to override
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param array $params
|
2016-09-16 22:55:40 +00:00
|
|
|
* @return StatusValue
|
2012-10-24 08:18:29 +00:00
|
|
|
*/
|
|
|
|
|
protected function doDescribeInternal( array $params ) {
|
2016-09-16 22:55:40 +00:00
|
|
|
return $this->newStatus();
|
2012-10-24 08:18:29 +00:00
|
|
|
}
|
|
|
|
|
|
2012-05-16 00:42:20 +00:00
|
|
|
/**
|
|
|
|
|
* No-op file operation that does nothing.
|
|
|
|
|
* Do not call this function from places outside FileBackend and FileOp.
|
|
|
|
|
*
|
2013-03-11 18:00:35 +00:00
|
|
|
* @param array $params
|
2016-09-16 22:55:40 +00:00
|
|
|
* @return StatusValue
|
2012-05-16 00:42:20 +00:00
|
|
|
*/
|
|
|
|
|
final public function nullInternal( array $params ) {
|
2016-09-16 22:55:40 +00:00
|
|
|
return $this->newStatus();
|
2012-05-16 00:42:20 +00:00
|
|
|
}
|
|
|
|
|
|
2012-03-03 18:29:38 +00:00
|
|
|
final public function concatenate( array $params ) {
|
2019-07-17 11:55:19 +00:00
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */
|
2016-09-18 21:40:38 +00:00
|
|
|
$ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
|
2016-09-16 22:55:40 +00:00
|
|
|
$status = $this->newStatus();
|
2012-03-03 18:29:38 +00:00
|
|
|
|
|
|
|
|
// Try to lock the source files for the scope of this function
|
2019-07-17 11:55:19 +00:00
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */
|
2012-03-03 18:29:38 +00:00
|
|
|
$scopeLockS = $this->getScopedFileLocks( $params['srcs'], LockManager::LOCK_UW, $status );
|
|
|
|
|
if ( $status->isOK() ) {
|
2012-09-10 21:01:29 +00:00
|
|
|
// Actually do the file concatenation...
|
|
|
|
|
$start_time = microtime( true );
|
2012-03-03 18:29:38 +00:00
|
|
|
$status->merge( $this->doConcatenate( $params ) );
|
2012-09-10 21:01:29 +00:00
|
|
|
$sec = microtime( true ) - $start_time;
|
|
|
|
|
if ( !$status->isOK() ) {
|
2017-03-07 02:14:14 +00:00
|
|
|
$this->logger->error( static::class . "-{$this->name}" .
|
2014-05-23 00:37:33 +00:00
|
|
|
" failed to concatenate " . count( $params['srcs'] ) . " file(s) [$sec sec]" );
|
2012-09-10 21:01:29 +00:00
|
|
|
}
|
2012-03-03 18:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see FileBackendStore::concatenate()
|
2020-07-13 08:57:12 +00:00
|
|
|
* @stable to override
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param array $params
|
2016-09-16 22:55:40 +00:00
|
|
|
* @return StatusValue
|
2012-03-03 18:29:38 +00:00
|
|
|
*/
|
|
|
|
|
protected function doConcatenate( array $params ) {
|
2016-09-16 22:55:40 +00:00
|
|
|
$status = $this->newStatus();
|
2021-11-19 23:19:42 +00:00
|
|
|
$tmpPath = $params['dst'];
|
|
|
|
|
unset( $params['latest'] );
|
2012-03-03 18:29:38 +00:00
|
|
|
|
|
|
|
|
// Check that the specified temp file is valid...
|
2019-08-20 14:09:16 +00:00
|
|
|
AtEase::suppressWarnings();
|
2012-09-22 18:46:48 +00:00
|
|
|
$ok = ( is_file( $tmpPath ) && filesize( $tmpPath ) == 0 );
|
2019-08-20 14:09:16 +00:00
|
|
|
AtEase::restoreWarnings();
|
2012-03-03 18:29:38 +00:00
|
|
|
if ( !$ok ) { // not present or not empty
|
|
|
|
|
$status->fatal( 'backend-fail-opentemp', $tmpPath );
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2012-03-03 18:29:38 +00:00
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-22 18:46:48 +00:00
|
|
|
// Get local FS versions of the chunks needed for the concatenation...
|
|
|
|
|
$fsFiles = $this->getLocalReferenceMulti( $params );
|
|
|
|
|
foreach ( $fsFiles as $path => &$fsFile ) {
|
|
|
|
|
if ( !$fsFile ) { // chunk failed to download?
|
2016-02-17 09:09:32 +00:00
|
|
|
$fsFile = $this->getLocalReference( [ 'src' => $path ] );
|
2012-09-22 18:46:48 +00:00
|
|
|
if ( !$fsFile ) { // retry failed?
|
|
|
|
|
$status->fatal( 'backend-fail-read', $path );
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2012-09-22 18:46:48 +00:00
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
unset( $fsFile ); // unset reference so we can reuse $fsFile
|
|
|
|
|
|
|
|
|
|
// Get a handle for the destination temp file
|
2012-03-03 18:29:38 +00:00
|
|
|
$tmpHandle = fopen( $tmpPath, 'ab' );
|
|
|
|
|
if ( $tmpHandle === false ) {
|
|
|
|
|
$status->fatal( 'backend-fail-opentemp', $tmpPath );
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2012-03-03 18:29:38 +00:00
|
|
|
return $status;
|
|
|
|
|
}
|
2012-09-22 18:46:48 +00:00
|
|
|
|
|
|
|
|
// Build up the temp file using the source chunks (in order)...
|
|
|
|
|
foreach ( $fsFiles as $virtualSource => $fsFile ) {
|
2012-03-03 18:29:38 +00:00
|
|
|
// Get a handle to the local FS version
|
2012-09-22 18:46:48 +00:00
|
|
|
$sourceHandle = fopen( $fsFile->getPath(), 'rb' );
|
2012-03-03 18:29:38 +00:00
|
|
|
if ( $sourceHandle === false ) {
|
|
|
|
|
fclose( $tmpHandle );
|
|
|
|
|
$status->fatal( 'backend-fail-read', $virtualSource );
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2012-03-03 18:29:38 +00:00
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
// Append chunk to file (pass chunk size to avoid magic quotes)
|
|
|
|
|
if ( !stream_copy_to_stream( $sourceHandle, $tmpHandle ) ) {
|
|
|
|
|
fclose( $sourceHandle );
|
|
|
|
|
fclose( $tmpHandle );
|
|
|
|
|
$status->fatal( 'backend-fail-writetemp', $tmpPath );
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2012-03-03 18:29:38 +00:00
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
fclose( $sourceHandle );
|
|
|
|
|
}
|
|
|
|
|
if ( !fclose( $tmpHandle ) ) {
|
|
|
|
|
$status->fatal( 'backend-fail-closetemp', $tmpPath );
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2012-03-03 18:29:38 +00:00
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clearstatcache(); // temp file changed
|
|
|
|
|
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
Use DeletePage in FileDeleteForm and fix output of ApiDelete
- Use DeletePage in FileDeleteForm instead of
WikiPage::doDeleteArticleReal
- Properly handle scheduled deletions in FileDeleteForm: previously, a
null status value could indicate a missing page OR a scheduled
deletion, but the code always assumed the first, and it would generate
a duplicated log entry. The API response would also not contain the
"delete-scheduled" message. This has been broken since the introduction
of scheduled deletion.
- In ApiDelete, for file deletions, check whether the status is OK not
good. The two might be equivalent, but this way it's more consistent.
- Add some documentation for the Status objects returned by file-related
methods. This is still incomplete, as there are many methods using
Status and none of them says what the status could be. In particular,
this means that for now we keep checking whether the status is OK
instead of good, even though it's unclear what could produce a
non-fatal error.
- In LocalFileDeleteBatch, avoid using a class property for the returned
status, as that's hard to follow. Instead, use a local variable and
pass it around when needed.
Bug: T288758
Change-Id: I22d60c05bdd4a3ea531e63dbb9e49efc36935137
2021-10-12 12:42:16 +00:00
|
|
|
/**
|
|
|
|
|
* @inheritDoc
|
|
|
|
|
*/
|
2012-03-03 18:29:38 +00:00
|
|
|
final protected function doPrepare( array $params ) {
|
2019-07-17 11:55:19 +00:00
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */
|
2016-09-18 21:40:38 +00:00
|
|
|
$ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
|
2016-09-16 22:55:40 +00:00
|
|
|
$status = $this->newStatus();
|
2013-05-01 03:55:52 +00:00
|
|
|
|
2012-03-03 18:29:38 +00:00
|
|
|
list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
|
|
|
|
|
if ( $dir === null ) {
|
|
|
|
|
$status->fatal( 'backend-fail-invalidpath', $params['dir'] );
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2012-03-03 18:29:38 +00:00
|
|
|
return $status; // invalid storage path
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $shard !== null ) { // confined to a single container/shard
|
|
|
|
|
$status->merge( $this->doPrepareInternal( $fullCont, $dir, $params ) );
|
|
|
|
|
} else { // directory is on several shards
|
2020-06-01 05:00:39 +00:00
|
|
|
$this->logger->debug( __METHOD__ . ": iterating over all container shards." );
|
2012-12-09 03:27:02 +00:00
|
|
|
list( , $shortCont, ) = self::splitStoragePath( $params['dir'] );
|
2012-03-03 18:29:38 +00:00
|
|
|
foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
|
|
|
|
|
$status->merge( $this->doPrepareInternal( "{$fullCont}{$suffix}", $dir, $params ) );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see FileBackendStore::doPrepare()
|
2020-07-13 08:57:12 +00:00
|
|
|
* @stable to override
|
2014-04-19 15:19:17 +00:00
|
|
|
* @param string $container
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param string $dir
|
|
|
|
|
* @param array $params
|
Use DeletePage in FileDeleteForm and fix output of ApiDelete
- Use DeletePage in FileDeleteForm instead of
WikiPage::doDeleteArticleReal
- Properly handle scheduled deletions in FileDeleteForm: previously, a
null status value could indicate a missing page OR a scheduled
deletion, but the code always assumed the first, and it would generate
a duplicated log entry. The API response would also not contain the
"delete-scheduled" message. This has been broken since the introduction
of scheduled deletion.
- In ApiDelete, for file deletions, check whether the status is OK not
good. The two might be equivalent, but this way it's more consistent.
- Add some documentation for the Status objects returned by file-related
methods. This is still incomplete, as there are many methods using
Status and none of them says what the status could be. In particular,
this means that for now we keep checking whether the status is OK
instead of good, even though it's unclear what could produce a
non-fatal error.
- In LocalFileDeleteBatch, avoid using a class property for the returned
status, as that's hard to follow. Instead, use a local variable and
pass it around when needed.
Bug: T288758
Change-Id: I22d60c05bdd4a3ea531e63dbb9e49efc36935137
2021-10-12 12:42:16 +00:00
|
|
|
* @return StatusValue Good status without value for success, fatal otherwise.
|
2012-03-03 18:29:38 +00:00
|
|
|
*/
|
|
|
|
|
protected function doPrepareInternal( $container, $dir, array $params ) {
|
2016-09-16 22:55:40 +00:00
|
|
|
return $this->newStatus();
|
2012-03-03 18:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final protected function doSecure( array $params ) {
|
2019-07-17 11:55:19 +00:00
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */
|
2016-09-18 21:40:38 +00:00
|
|
|
$ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
|
2016-09-16 22:55:40 +00:00
|
|
|
$status = $this->newStatus();
|
2012-03-03 18:29:38 +00:00
|
|
|
|
|
|
|
|
list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
|
|
|
|
|
if ( $dir === null ) {
|
|
|
|
|
$status->fatal( 'backend-fail-invalidpath', $params['dir'] );
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2012-03-03 18:29:38 +00:00
|
|
|
return $status; // invalid storage path
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $shard !== null ) { // confined to a single container/shard
|
|
|
|
|
$status->merge( $this->doSecureInternal( $fullCont, $dir, $params ) );
|
|
|
|
|
} else { // directory is on several shards
|
2020-06-01 05:00:39 +00:00
|
|
|
$this->logger->debug( __METHOD__ . ": iterating over all container shards." );
|
2012-12-09 03:27:02 +00:00
|
|
|
list( , $shortCont, ) = self::splitStoragePath( $params['dir'] );
|
2012-03-03 18:29:38 +00:00
|
|
|
foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
|
|
|
|
|
$status->merge( $this->doSecureInternal( "{$fullCont}{$suffix}", $dir, $params ) );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see FileBackendStore::doSecure()
|
2020-07-13 08:57:12 +00:00
|
|
|
* @stable to override
|
2014-04-19 15:19:17 +00:00
|
|
|
* @param string $container
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param string $dir
|
|
|
|
|
* @param array $params
|
Use DeletePage in FileDeleteForm and fix output of ApiDelete
- Use DeletePage in FileDeleteForm instead of
WikiPage::doDeleteArticleReal
- Properly handle scheduled deletions in FileDeleteForm: previously, a
null status value could indicate a missing page OR a scheduled
deletion, but the code always assumed the first, and it would generate
a duplicated log entry. The API response would also not contain the
"delete-scheduled" message. This has been broken since the introduction
of scheduled deletion.
- In ApiDelete, for file deletions, check whether the status is OK not
good. The two might be equivalent, but this way it's more consistent.
- Add some documentation for the Status objects returned by file-related
methods. This is still incomplete, as there are many methods using
Status and none of them says what the status could be. In particular,
this means that for now we keep checking whether the status is OK
instead of good, even though it's unclear what could produce a
non-fatal error.
- In LocalFileDeleteBatch, avoid using a class property for the returned
status, as that's hard to follow. Instead, use a local variable and
pass it around when needed.
Bug: T288758
Change-Id: I22d60c05bdd4a3ea531e63dbb9e49efc36935137
2021-10-12 12:42:16 +00:00
|
|
|
* @return StatusValue Good status without value for success, fatal otherwise.
|
2012-03-03 18:29:38 +00:00
|
|
|
*/
|
|
|
|
|
protected function doSecureInternal( $container, $dir, array $params ) {
|
2016-09-16 22:55:40 +00:00
|
|
|
return $this->newStatus();
|
2012-03-03 18:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
2012-05-21 22:19:06 +00:00
|
|
|
final protected function doPublish( array $params ) {
|
2019-07-17 11:55:19 +00:00
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */
|
2016-09-18 21:40:38 +00:00
|
|
|
$ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
|
2016-09-16 22:55:40 +00:00
|
|
|
$status = $this->newStatus();
|
2012-05-21 22:19:06 +00:00
|
|
|
|
|
|
|
|
list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
|
|
|
|
|
if ( $dir === null ) {
|
|
|
|
|
$status->fatal( 'backend-fail-invalidpath', $params['dir'] );
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2012-05-21 22:19:06 +00:00
|
|
|
return $status; // invalid storage path
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $shard !== null ) { // confined to a single container/shard
|
|
|
|
|
$status->merge( $this->doPublishInternal( $fullCont, $dir, $params ) );
|
|
|
|
|
} else { // directory is on several shards
|
2020-06-01 05:00:39 +00:00
|
|
|
$this->logger->debug( __METHOD__ . ": iterating over all container shards." );
|
2012-12-09 03:27:02 +00:00
|
|
|
list( , $shortCont, ) = self::splitStoragePath( $params['dir'] );
|
2012-05-21 22:19:06 +00:00
|
|
|
foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
|
|
|
|
|
$status->merge( $this->doPublishInternal( "{$fullCont}{$suffix}", $dir, $params ) );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see FileBackendStore::doPublish()
|
2020-07-13 08:57:12 +00:00
|
|
|
* @stable to override
|
2014-04-19 15:19:17 +00:00
|
|
|
* @param string $container
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param string $dir
|
|
|
|
|
* @param array $params
|
2016-09-16 22:55:40 +00:00
|
|
|
* @return StatusValue
|
2012-05-21 22:19:06 +00:00
|
|
|
*/
|
|
|
|
|
protected function doPublishInternal( $container, $dir, array $params ) {
|
2016-09-16 22:55:40 +00:00
|
|
|
return $this->newStatus();
|
2012-05-21 22:19:06 +00:00
|
|
|
}
|
|
|
|
|
|
2012-03-03 18:29:38 +00:00
|
|
|
final protected function doClean( array $params ) {
|
2019-07-17 11:55:19 +00:00
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */
|
2016-09-18 21:40:38 +00:00
|
|
|
$ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
|
2016-09-16 22:55:40 +00:00
|
|
|
$status = $this->newStatus();
|
2012-03-03 18:29:38 +00:00
|
|
|
|
2012-04-26 18:40:47 +00:00
|
|
|
// Recursive: first delete all empty subdirs recursively
|
|
|
|
|
if ( !empty( $params['recursive'] ) && !$this->directoriesAreVirtual() ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$subDirsRel = $this->getTopDirectoryList( [ 'dir' => $params['dir'] ] );
|
2012-04-26 18:40:47 +00:00
|
|
|
if ( $subDirsRel !== null ) { // no errors
|
|
|
|
|
foreach ( $subDirsRel as $subDirRel ) {
|
|
|
|
|
$subDir = $params['dir'] . "/{$subDirRel}"; // full path
|
2016-02-17 09:09:32 +00:00
|
|
|
$status->merge( $this->doClean( [ 'dir' => $subDir ] + $params ) );
|
2012-04-26 18:40:47 +00:00
|
|
|
}
|
2012-11-16 20:47:01 +00:00
|
|
|
unset( $subDirsRel ); // free directory for rmdir() on Windows (for FS backends)
|
2012-04-26 18:40:47 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-03 18:29:38 +00:00
|
|
|
list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
|
|
|
|
|
if ( $dir === null ) {
|
|
|
|
|
$status->fatal( 'backend-fail-invalidpath', $params['dir'] );
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2012-03-03 18:29:38 +00:00
|
|
|
return $status; // invalid storage path
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Attempt to lock this directory...
|
2016-02-17 09:09:32 +00:00
|
|
|
$filesLockEx = [ $params['dir'] ];
|
2019-07-17 11:55:19 +00:00
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */
|
2012-03-03 18:29:38 +00:00
|
|
|
$scopedLockE = $this->getScopedFileLocks( $filesLockEx, LockManager::LOCK_EX, $status );
|
|
|
|
|
if ( !$status->isOK() ) {
|
|
|
|
|
return $status; // abort
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $shard !== null ) { // confined to a single container/shard
|
|
|
|
|
$status->merge( $this->doCleanInternal( $fullCont, $dir, $params ) );
|
2012-04-23 20:27:58 +00:00
|
|
|
$this->deleteContainerCache( $fullCont ); // purge cache
|
2012-03-03 18:29:38 +00:00
|
|
|
} else { // directory is on several shards
|
2020-06-01 05:00:39 +00:00
|
|
|
$this->logger->debug( __METHOD__ . ": iterating over all container shards." );
|
2012-12-09 03:27:02 +00:00
|
|
|
list( , $shortCont, ) = self::splitStoragePath( $params['dir'] );
|
2012-03-03 18:29:38 +00:00
|
|
|
foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
|
|
|
|
|
$status->merge( $this->doCleanInternal( "{$fullCont}{$suffix}", $dir, $params ) );
|
2012-04-23 20:27:58 +00:00
|
|
|
$this->deleteContainerCache( "{$fullCont}{$suffix}" ); // purge cache
|
2012-03-03 18:29:38 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see FileBackendStore::doClean()
|
2020-07-13 08:57:12 +00:00
|
|
|
* @stable to override
|
2014-04-19 15:19:17 +00:00
|
|
|
* @param string $container
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param string $dir
|
|
|
|
|
* @param array $params
|
2016-09-16 22:55:40 +00:00
|
|
|
* @return StatusValue
|
2012-03-03 18:29:38 +00:00
|
|
|
*/
|
|
|
|
|
protected function doCleanInternal( $container, $dir, array $params ) {
|
2016-09-16 22:55:40 +00:00
|
|
|
return $this->newStatus();
|
2012-03-03 18:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final public function fileExists( array $params ) {
|
2019-07-17 11:55:19 +00:00
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */
|
2016-09-18 21:40:38 +00:00
|
|
|
$ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
|
2019-08-30 07:01:29 +00:00
|
|
|
|
2012-03-03 18:29:38 +00:00
|
|
|
$stat = $this->getFileStat( $params );
|
2019-08-30 07:01:29 +00:00
|
|
|
if ( is_array( $stat ) ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2019-08-30 07:01:29 +00:00
|
|
|
return ( $stat === self::$RES_ABSENT ) ? false : self::EXISTENCE_ERROR;
|
2012-03-03 18:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final public function getFileTimestamp( array $params ) {
|
2019-07-17 11:55:19 +00:00
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */
|
2016-09-18 21:40:38 +00:00
|
|
|
$ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
|
2019-08-30 07:01:29 +00:00
|
|
|
|
2012-03-03 18:29:38 +00:00
|
|
|
$stat = $this->getFileStat( $params );
|
2019-08-30 07:01:29 +00:00
|
|
|
if ( is_array( $stat ) ) {
|
|
|
|
|
return $stat['mtime'];
|
|
|
|
|
}
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2019-08-30 07:01:29 +00:00
|
|
|
return self::TIMESTAMP_FAIL; // all failure cases
|
2012-03-03 18:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final public function getFileSize( array $params ) {
|
2019-07-17 11:55:19 +00:00
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */
|
2016-09-18 21:40:38 +00:00
|
|
|
$ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
|
2019-08-30 07:01:29 +00:00
|
|
|
|
2012-03-03 18:29:38 +00:00
|
|
|
$stat = $this->getFileStat( $params );
|
2019-08-30 07:01:29 +00:00
|
|
|
if ( is_array( $stat ) ) {
|
|
|
|
|
return $stat['size'];
|
|
|
|
|
}
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2019-08-30 07:01:29 +00:00
|
|
|
return self::SIZE_FAIL; // all failure cases
|
2012-03-03 18:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final public function getFileStat( array $params ) {
|
2019-08-30 07:01:29 +00:00
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */
|
|
|
|
|
$ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
|
|
|
|
|
|
2012-03-03 18:29:38 +00:00
|
|
|
$path = self::normalizeStoragePath( $params['src'] );
|
|
|
|
|
if ( $path === null ) {
|
2019-08-30 07:01:29 +00:00
|
|
|
return self::STAT_ERROR; // invalid storage path
|
2012-03-03 18:29:38 +00:00
|
|
|
}
|
2018-11-09 01:13:51 +00:00
|
|
|
|
2019-08-30 07:01:29 +00:00
|
|
|
// Whether to bypass cache except for process cache entries loaded directly from
|
|
|
|
|
// high consistency backend queries (caller handles any cache flushing and locking)
|
|
|
|
|
$latest = !empty( $params['latest'] );
|
|
|
|
|
// Whether to ignore cache entries missing the SHA-1 field for existing files
|
|
|
|
|
$requireSHA1 = !empty( $params['requireSHA1'] );
|
2018-11-09 01:13:51 +00:00
|
|
|
|
2019-08-30 07:01:29 +00:00
|
|
|
$stat = $this->cheapCache->getField( $path, 'stat', self::CACHE_TTL );
|
|
|
|
|
// Load the persistent stat cache into process cache if needed
|
2018-11-09 01:13:51 +00:00
|
|
|
if ( !$latest ) {
|
|
|
|
|
if (
|
2019-08-30 07:01:29 +00:00
|
|
|
// File stat is not in process cache
|
|
|
|
|
$stat === null ||
|
|
|
|
|
// Key/value store backends might opportunistically set file stat process
|
|
|
|
|
// cache entries from object listings that do not include the SHA-1. In that
|
|
|
|
|
// case, loading the persistent stat cache will likely yield the SHA-1.
|
2018-11-09 01:13:51 +00:00
|
|
|
( $requireSHA1 && is_array( $stat ) && !isset( $stat['sha1'] ) )
|
|
|
|
|
) {
|
2019-08-30 07:01:29 +00:00
|
|
|
$this->primeFileCache( [ $path ] );
|
|
|
|
|
// Get any newly process-cached entry
|
|
|
|
|
$stat = $this->cheapCache->getField( $path, 'stat', self::CACHE_TTL );
|
2018-11-09 01:13:51 +00:00
|
|
|
}
|
2012-04-27 00:46:28 +00:00
|
|
|
}
|
2018-11-09 01:13:51 +00:00
|
|
|
|
2018-12-11 19:58:35 +00:00
|
|
|
if ( is_array( $stat ) ) {
|
|
|
|
|
if (
|
|
|
|
|
( !$latest || $stat['latest'] ) &&
|
|
|
|
|
( !$requireSHA1 || isset( $stat['sha1'] ) )
|
|
|
|
|
) {
|
|
|
|
|
return $stat;
|
|
|
|
|
}
|
2019-08-30 07:01:29 +00:00
|
|
|
} elseif ( $stat === self::$ABSENT_LATEST ) {
|
|
|
|
|
return self::STAT_ABSENT;
|
|
|
|
|
} elseif ( $stat === self::$ABSENT_NORMAL ) {
|
|
|
|
|
if ( !$latest ) {
|
|
|
|
|
return self::STAT_ABSENT;
|
2012-03-03 18:29:38 +00:00
|
|
|
}
|
|
|
|
|
}
|
2018-11-09 01:13:51 +00:00
|
|
|
|
2019-08-30 07:01:29 +00:00
|
|
|
// Load the file stat from the backend and update caches
|
2012-03-03 18:29:38 +00:00
|
|
|
$stat = $this->doGetFileStat( $params );
|
2019-08-30 07:01:29 +00:00
|
|
|
$this->ingestFreshFileStats( [ $path => $stat ], $latest );
|
2018-11-09 01:13:51 +00:00
|
|
|
|
2019-08-30 07:01:29 +00:00
|
|
|
if ( is_array( $stat ) ) {
|
|
|
|
|
return $stat;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ( $stat === self::$RES_ERROR ) ? self::STAT_ERROR : self::STAT_ABSENT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Ingest file stat entries that just came from querying the backend (not cache)
|
|
|
|
|
*
|
|
|
|
|
* @param array[]|bool[]|null[] $stats Map of (path => doGetFileStat() stype result)
|
|
|
|
|
* @param bool $latest Whether doGetFileStat()/doGetFileStatMulti() had the 'latest' flag
|
|
|
|
|
* @return bool Whether all files have non-error stat replies
|
|
|
|
|
*/
|
|
|
|
|
final protected function ingestFreshFileStats( array $stats, $latest ) {
|
|
|
|
|
$success = true;
|
|
|
|
|
|
|
|
|
|
foreach ( $stats as $path => $stat ) {
|
|
|
|
|
if ( is_array( $stat ) ) {
|
|
|
|
|
// Strongly consistent backends might automatically set this flag
|
|
|
|
|
$stat['latest'] = $stat['latest'] ?? $latest;
|
|
|
|
|
|
|
|
|
|
$this->cheapCache->setField( $path, 'stat', $stat );
|
|
|
|
|
if ( isset( $stat['sha1'] ) ) {
|
|
|
|
|
// Some backends store the SHA-1 hash as metadata
|
|
|
|
|
$this->cheapCache->setField(
|
|
|
|
|
$path,
|
|
|
|
|
'sha1',
|
|
|
|
|
[ 'hash' => $stat['sha1'], 'latest' => $latest ]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if ( isset( $stat['xattr'] ) ) {
|
|
|
|
|
// Some backends store custom headers/metadata
|
|
|
|
|
$stat['xattr'] = self::normalizeXAttributes( $stat['xattr'] );
|
|
|
|
|
$this->cheapCache->setField(
|
|
|
|
|
$path,
|
|
|
|
|
'xattr',
|
|
|
|
|
[ 'map' => $stat['xattr'], 'latest' => $latest ]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
// Update persistent cache (@TODO: set all entries in one batch)
|
|
|
|
|
$this->setFileCache( $path, $stat );
|
|
|
|
|
} elseif ( $stat === self::$RES_ABSENT ) {
|
|
|
|
|
$this->cheapCache->setField(
|
|
|
|
|
$path,
|
|
|
|
|
'stat',
|
|
|
|
|
$latest ? self::$ABSENT_LATEST : self::$ABSENT_NORMAL
|
|
|
|
|
);
|
|
|
|
|
$this->cheapCache->setField(
|
|
|
|
|
$path,
|
|
|
|
|
'xattr',
|
|
|
|
|
[ 'map' => self::XATTRS_FAIL, 'latest' => $latest ]
|
|
|
|
|
);
|
|
|
|
|
$this->cheapCache->setField(
|
|
|
|
|
$path,
|
|
|
|
|
'sha1',
|
|
|
|
|
[ 'hash' => self::SHA1_FAIL, 'latest' => $latest ]
|
|
|
|
|
);
|
|
|
|
|
$this->logger->debug(
|
|
|
|
|
__METHOD__ . ': File {path} does not exist',
|
|
|
|
|
[ 'path' => $path ]
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
$success = false;
|
|
|
|
|
$this->logger->error(
|
|
|
|
|
__METHOD__ . ': Could not stat file {path}',
|
|
|
|
|
[ 'path' => $path ]
|
|
|
|
|
);
|
2013-09-30 07:12:10 +00:00
|
|
|
}
|
2012-03-03 18:29:38 +00:00
|
|
|
}
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2019-08-30 07:01:29 +00:00
|
|
|
return $success;
|
2012-03-03 18:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see FileBackendStore::getFileStat()
|
2016-09-24 10:55:38 +00:00
|
|
|
* @param array $params
|
2012-03-03 18:29:38 +00:00
|
|
|
*/
|
|
|
|
|
abstract protected function doGetFileStat( array $params );
|
|
|
|
|
|
2012-09-22 18:01:58 +00:00
|
|
|
public function getFileContentsMulti( array $params ) {
|
2019-07-17 11:55:19 +00:00
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */
|
2016-09-18 21:40:38 +00:00
|
|
|
$ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
|
2012-09-22 18:01:58 +00:00
|
|
|
|
|
|
|
|
$params = $this->setConcurrencyFlags( $params );
|
|
|
|
|
$contents = $this->doGetFileContentsMulti( $params );
|
2019-08-30 07:01:29 +00:00
|
|
|
foreach ( $contents as $path => $content ) {
|
|
|
|
|
if ( !is_string( $content ) ) {
|
|
|
|
|
$contents[$path] = self::CONTENT_FAIL; // used for all failure cases
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-09-22 18:01:58 +00:00
|
|
|
|
|
|
|
|
return $contents;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see FileBackendStore::getFileContentsMulti()
|
2020-07-13 08:57:12 +00:00
|
|
|
* @stable to override
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param array $params
|
2019-08-30 07:01:29 +00:00
|
|
|
* @return string[]|bool[]|null[] Map of (path => string, false (missing), or null (error))
|
2012-09-22 18:01:58 +00:00
|
|
|
*/
|
|
|
|
|
protected function doGetFileContentsMulti( array $params ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$contents = [];
|
2012-09-22 18:01:58 +00:00
|
|
|
foreach ( $this->doGetLocalReferenceMulti( $params ) as $path => $fsFile ) {
|
2019-08-30 07:01:29 +00:00
|
|
|
if ( $fsFile instanceof FSFile ) {
|
|
|
|
|
AtEase::suppressWarnings();
|
|
|
|
|
$content = file_get_contents( $fsFile->getPath() );
|
|
|
|
|
AtEase::restoreWarnings();
|
|
|
|
|
$contents[$path] = is_string( $content ) ? $content : self::$RES_ERROR;
|
|
|
|
|
} elseif ( $fsFile === self::$RES_ABSENT ) {
|
|
|
|
|
$contents[$path] = self::$RES_ABSENT;
|
|
|
|
|
} else {
|
|
|
|
|
$contents[$path] = self::$RES_ERROR;
|
|
|
|
|
}
|
2012-09-22 18:01:58 +00:00
|
|
|
}
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2012-09-22 18:01:58 +00:00
|
|
|
return $contents;
|
2012-03-03 18:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
2013-09-30 07:12:10 +00:00
|
|
|
final public function getFileXAttributes( array $params ) {
|
2019-08-30 07:01:29 +00:00
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */
|
|
|
|
|
$ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
|
|
|
|
|
|
2013-09-30 07:12:10 +00:00
|
|
|
$path = self::normalizeStoragePath( $params['src'] );
|
|
|
|
|
if ( $path === null ) {
|
2019-08-30 07:01:29 +00:00
|
|
|
return self::XATTRS_FAIL; // invalid storage path
|
2013-09-30 07:12:10 +00:00
|
|
|
}
|
|
|
|
|
$latest = !empty( $params['latest'] ); // use latest data?
|
2018-07-11 12:53:13 +00:00
|
|
|
if ( $this->cheapCache->hasField( $path, 'xattr', self::CACHE_TTL ) ) {
|
|
|
|
|
$stat = $this->cheapCache->getField( $path, 'xattr' );
|
2013-09-30 07:12:10 +00:00
|
|
|
// If we want the latest data, check that this cached
|
|
|
|
|
// value was in fact fetched with the latest available data.
|
|
|
|
|
if ( !$latest || $stat['latest'] ) {
|
|
|
|
|
return $stat['map'];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$fields = $this->doGetFileXAttributes( $params );
|
2019-08-30 07:01:29 +00:00
|
|
|
if ( is_array( $fields ) ) {
|
|
|
|
|
$fields = self::normalizeXAttributes( $fields );
|
|
|
|
|
$this->cheapCache->setField(
|
|
|
|
|
$path,
|
|
|
|
|
'xattr',
|
|
|
|
|
[ 'map' => $fields, 'latest' => $latest ]
|
|
|
|
|
);
|
|
|
|
|
} elseif ( $fields === self::$RES_ABSENT ) {
|
|
|
|
|
$this->cheapCache->setField(
|
|
|
|
|
$path,
|
|
|
|
|
'xattr',
|
|
|
|
|
[ 'map' => self::XATTRS_FAIL, 'latest' => $latest ]
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
$fields = self::XATTRS_FAIL; // used for all failure cases
|
|
|
|
|
}
|
2014-02-05 11:02:29 +00:00
|
|
|
|
2013-09-30 07:12:10 +00:00
|
|
|
return $fields;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see FileBackendStore::getFileXAttributes()
|
2020-07-13 08:57:12 +00:00
|
|
|
* @stable to override
|
2016-09-24 10:55:38 +00:00
|
|
|
* @param array $params
|
2019-08-30 07:01:29 +00:00
|
|
|
* @return array[][]|false|null Attributes, false (missing file), or null (error)
|
2013-09-30 07:12:10 +00:00
|
|
|
*/
|
|
|
|
|
protected function doGetFileXAttributes( array $params ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [ 'headers' => [], 'metadata' => [] ]; // not supported
|
2013-09-30 07:12:10 +00:00
|
|
|
}
|
|
|
|
|
|
2012-03-03 18:29:38 +00:00
|
|
|
final public function getFileSha1Base36( array $params ) {
|
2019-08-30 07:01:29 +00:00
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */
|
|
|
|
|
$ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
|
|
|
|
|
|
2012-05-26 05:42:33 +00:00
|
|
|
$path = self::normalizeStoragePath( $params['src'] );
|
|
|
|
|
if ( $path === null ) {
|
2019-08-30 07:01:29 +00:00
|
|
|
return self::SHA1_FAIL; // invalid storage path
|
2012-05-26 05:42:33 +00:00
|
|
|
}
|
|
|
|
|
$latest = !empty( $params['latest'] ); // use latest data?
|
2018-07-11 12:53:13 +00:00
|
|
|
if ( $this->cheapCache->hasField( $path, 'sha1', self::CACHE_TTL ) ) {
|
|
|
|
|
$stat = $this->cheapCache->getField( $path, 'sha1' );
|
2012-05-26 05:42:33 +00:00
|
|
|
// If we want the latest data, check that this cached
|
|
|
|
|
// value was in fact fetched with the latest available data.
|
2012-06-30 03:08:06 +00:00
|
|
|
if ( !$latest || $stat['latest'] ) {
|
|
|
|
|
return $stat['hash'];
|
2012-05-26 05:42:33 +00:00
|
|
|
}
|
2012-03-03 18:29:38 +00:00
|
|
|
}
|
2019-08-30 07:01:29 +00:00
|
|
|
$sha1 = $this->doGetFileSha1Base36( $params );
|
|
|
|
|
if ( is_string( $sha1 ) ) {
|
|
|
|
|
$this->cheapCache->setField(
|
|
|
|
|
$path,
|
|
|
|
|
'sha1',
|
|
|
|
|
[ 'hash' => $sha1, 'latest' => $latest ]
|
|
|
|
|
);
|
|
|
|
|
} elseif ( $sha1 === self::$RES_ABSENT ) {
|
|
|
|
|
$this->cheapCache->setField(
|
|
|
|
|
$path,
|
|
|
|
|
'sha1',
|
|
|
|
|
[ 'hash' => self::SHA1_FAIL, 'latest' => $latest ]
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
$sha1 = self::SHA1_FAIL; // used for all failure cases
|
|
|
|
|
}
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2019-08-30 07:01:29 +00:00
|
|
|
return $sha1;
|
2012-03-03 18:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see FileBackendStore::getFileSha1Base36()
|
2020-07-13 08:57:12 +00:00
|
|
|
* @stable to override
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param array $params
|
2019-08-30 07:01:29 +00:00
|
|
|
* @return bool|string|null SHA1, false (missing file), or null (error)
|
2012-03-03 18:29:38 +00:00
|
|
|
*/
|
|
|
|
|
protected function doGetFileSha1Base36( array $params ) {
|
|
|
|
|
$fsFile = $this->getLocalReference( $params );
|
2019-08-30 07:01:29 +00:00
|
|
|
if ( $fsFile instanceof FSFile ) {
|
|
|
|
|
$sha1 = $fsFile->getSha1Base36();
|
|
|
|
|
|
|
|
|
|
return is_string( $sha1 ) ? $sha1 : self::$RES_ERROR;
|
2012-03-03 18:29:38 +00:00
|
|
|
}
|
2019-08-30 07:01:29 +00:00
|
|
|
|
|
|
|
|
return ( $fsFile === self::$RES_ERROR ) ? self::$RES_ERROR : self::$RES_ABSENT;
|
2012-03-03 18:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final public function getFileProps( array $params ) {
|
2019-07-17 11:55:19 +00:00
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */
|
2016-09-18 21:40:38 +00:00
|
|
|
$ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
|
2019-08-30 07:01:29 +00:00
|
|
|
|
2012-03-03 18:29:38 +00:00
|
|
|
$fsFile = $this->getLocalReference( $params );
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2019-08-30 07:01:29 +00:00
|
|
|
return $fsFile ? $fsFile->getProps() : FSFile::placeholderProps();
|
2012-03-03 18:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
2012-09-18 18:21:30 +00:00
|
|
|
final public function getLocalReferenceMulti( array $params ) {
|
2019-07-17 11:55:19 +00:00
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */
|
2016-09-18 21:40:38 +00:00
|
|
|
$ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
|
2012-09-18 18:21:30 +00:00
|
|
|
|
|
|
|
|
$params = $this->setConcurrencyFlags( $params );
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$fsFiles = []; // (path => FSFile)
|
2012-05-26 05:42:33 +00:00
|
|
|
$latest = !empty( $params['latest'] ); // use latest data?
|
2012-09-18 18:21:30 +00:00
|
|
|
// Reuse any files already in process cache...
|
|
|
|
|
foreach ( $params['srcs'] as $src ) {
|
|
|
|
|
$path = self::normalizeStoragePath( $src );
|
|
|
|
|
if ( $path === null ) {
|
|
|
|
|
$fsFiles[$src] = null; // invalid storage path
|
2018-07-11 12:53:13 +00:00
|
|
|
} elseif ( $this->expensiveCache->hasField( $path, 'localRef' ) ) {
|
|
|
|
|
$val = $this->expensiveCache->getField( $path, 'localRef' );
|
2012-09-18 18:21:30 +00:00
|
|
|
// If we want the latest data, check that this cached
|
|
|
|
|
// value was in fact fetched with the latest available data.
|
|
|
|
|
if ( !$latest || $val['latest'] ) {
|
|
|
|
|
$fsFiles[$src] = $val['object'];
|
|
|
|
|
}
|
2012-05-26 05:42:33 +00:00
|
|
|
}
|
2012-03-03 18:29:38 +00:00
|
|
|
}
|
2021-12-30 12:44:13 +00:00
|
|
|
// Fetch local references of any remaining files...
|
2012-09-18 18:21:30 +00:00
|
|
|
$params['srcs'] = array_diff( $params['srcs'], array_keys( $fsFiles ) );
|
|
|
|
|
foreach ( $this->doGetLocalReferenceMulti( $params ) as $path => $fsFile ) {
|
2019-08-30 07:01:29 +00:00
|
|
|
if ( $fsFile instanceof FSFile ) {
|
|
|
|
|
$fsFiles[$path] = $fsFile;
|
|
|
|
|
$this->expensiveCache->setField(
|
|
|
|
|
$path,
|
|
|
|
|
'localRef',
|
|
|
|
|
[ 'object' => $fsFile, 'latest' => $latest ]
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
$fsFiles[$path] = null; // used for all failure cases
|
2012-09-18 18:21:30 +00:00
|
|
|
}
|
2012-03-03 18:29:38 +00:00
|
|
|
}
|
2012-09-18 18:21:30 +00:00
|
|
|
|
|
|
|
|
return $fsFiles;
|
2012-03-03 18:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
2012-09-18 18:21:30 +00:00
|
|
|
/**
|
|
|
|
|
* @see FileBackendStore::getLocalReferenceMulti()
|
2020-07-13 08:57:12 +00:00
|
|
|
* @stable to override
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param array $params
|
2019-08-30 07:01:29 +00:00
|
|
|
* @return string[]|bool[]|null[] Map of (path => FSFile, false (missing), or null (error))
|
2012-09-18 18:21:30 +00:00
|
|
|
*/
|
|
|
|
|
protected function doGetLocalReferenceMulti( array $params ) {
|
|
|
|
|
return $this->doGetLocalCopyMulti( $params );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final public function getLocalCopyMulti( array $params ) {
|
2019-07-17 11:55:19 +00:00
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */
|
2016-09-18 21:40:38 +00:00
|
|
|
$ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
|
2012-09-18 18:21:30 +00:00
|
|
|
|
|
|
|
|
$params = $this->setConcurrencyFlags( $params );
|
|
|
|
|
$tmpFiles = $this->doGetLocalCopyMulti( $params );
|
2019-08-30 07:01:29 +00:00
|
|
|
foreach ( $tmpFiles as $path => $tmpFile ) {
|
|
|
|
|
if ( !$tmpFile ) {
|
|
|
|
|
$tmpFiles[$path] = null; // used for all failure cases
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-09-18 18:21:30 +00:00
|
|
|
|
|
|
|
|
return $tmpFiles;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see FileBackendStore::getLocalCopyMulti()
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param array $params
|
2019-08-30 07:01:29 +00:00
|
|
|
* @return string[]|bool[]|null[] Map of (path => TempFSFile, false (missing), or null (error))
|
2012-09-18 18:21:30 +00:00
|
|
|
*/
|
|
|
|
|
abstract protected function doGetLocalCopyMulti( array $params );
|
|
|
|
|
|
2012-11-06 20:49:40 +00:00
|
|
|
/**
|
|
|
|
|
* @see FileBackend::getFileHttpUrl()
|
2020-07-13 08:57:12 +00:00
|
|
|
* @stable to override
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param array $params
|
2012-11-06 20:49:40 +00:00
|
|
|
* @return string|null
|
|
|
|
|
*/
|
|
|
|
|
public function getFileHttpUrl( array $params ) {
|
2019-08-30 07:01:29 +00:00
|
|
|
return self::TEMPURL_ERROR; // not supported
|
2012-11-06 20:49:40 +00:00
|
|
|
}
|
|
|
|
|
|
2012-03-03 18:29:38 +00:00
|
|
|
final public function streamFile( array $params ) {
|
2019-07-17 11:55:19 +00:00
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */
|
2016-09-18 21:40:38 +00:00
|
|
|
$ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
|
2016-09-16 22:55:40 +00:00
|
|
|
$status = $this->newStatus();
|
2012-03-03 18:29:38 +00:00
|
|
|
|
2014-04-20 08:40:06 +00:00
|
|
|
// Always set some fields for subclass convenience
|
2017-10-06 22:17:58 +00:00
|
|
|
$params['options'] = $params['options'] ?? [];
|
|
|
|
|
$params['headers'] = $params['headers'] ?? [];
|
2014-04-20 08:40:06 +00:00
|
|
|
|
|
|
|
|
// Don't stream it out as text/html if there was a PHP error
|
|
|
|
|
if ( ( empty( $params['headless'] ) || $params['headers'] ) && headers_sent() ) {
|
|
|
|
|
print "Headers already sent, terminating.\n";
|
2012-03-03 18:29:38 +00:00
|
|
|
$status->fatal( 'backend-fail-stream', $params['src'] );
|
2014-04-20 08:40:06 +00:00
|
|
|
return $status;
|
2012-03-03 18:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
2014-04-20 08:40:06 +00:00
|
|
|
$status->merge( $this->doStreamFile( $params ) );
|
|
|
|
|
|
2012-03-03 18:29:38 +00:00
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see FileBackendStore::streamFile()
|
2020-07-13 08:57:12 +00:00
|
|
|
* @stable to override
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param array $params
|
2016-09-16 22:55:40 +00:00
|
|
|
* @return StatusValue
|
2012-03-03 18:29:38 +00:00
|
|
|
*/
|
|
|
|
|
protected function doStreamFile( array $params ) {
|
2016-09-16 22:55:40 +00:00
|
|
|
$status = $this->newStatus();
|
2012-03-03 18:29:38 +00:00
|
|
|
|
2014-04-20 08:40:06 +00:00
|
|
|
$flags = 0;
|
2016-09-18 22:49:34 +00:00
|
|
|
$flags |= !empty( $params['headless'] ) ? HTTPFileStreamer::STREAM_HEADLESS : 0;
|
|
|
|
|
$flags |= !empty( $params['allowOB'] ) ? HTTPFileStreamer::STREAM_ALLOW_OB : 0;
|
2014-04-20 08:40:06 +00:00
|
|
|
|
2012-03-03 18:29:38 +00:00
|
|
|
$fsFile = $this->getLocalReference( $params );
|
2014-04-20 08:40:06 +00:00
|
|
|
if ( $fsFile ) {
|
2016-09-18 22:49:34 +00:00
|
|
|
$streamer = new HTTPFileStreamer(
|
|
|
|
|
$fsFile->getPath(),
|
|
|
|
|
[
|
|
|
|
|
'obResetFunc' => $this->obResetFunc,
|
|
|
|
|
'streamMimeFunc' => $this->streamMimeFunc
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
$res = $streamer->stream( $params['headers'], true, $params['options'], $flags );
|
2014-04-20 08:40:06 +00:00
|
|
|
} else {
|
|
|
|
|
$res = false;
|
2016-09-18 22:49:34 +00:00
|
|
|
HTTPFileStreamer::send404Message( $params['src'], $flags );
|
2014-04-20 08:40:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( !$res ) {
|
2012-03-03 18:29:38 +00:00
|
|
|
$status->fatal( 'backend-fail-stream', $params['src'] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-05 05:56:08 +00:00
|
|
|
final public function directoryExists( array $params ) {
|
|
|
|
|
list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
|
|
|
|
|
if ( $dir === null ) {
|
2019-08-30 07:01:29 +00:00
|
|
|
return self::EXISTENCE_ERROR; // invalid storage path
|
2012-04-05 05:56:08 +00:00
|
|
|
}
|
|
|
|
|
if ( $shard !== null ) { // confined to a single container/shard
|
|
|
|
|
return $this->doDirectoryExists( $fullCont, $dir, $params );
|
|
|
|
|
} else { // directory is on several shards
|
2020-06-01 05:00:39 +00:00
|
|
|
$this->logger->debug( __METHOD__ . ": iterating over all container shards." );
|
2012-12-09 03:27:02 +00:00
|
|
|
list( , $shortCont, ) = self::splitStoragePath( $params['dir'] );
|
2012-04-05 05:56:08 +00:00
|
|
|
$res = false; // response
|
|
|
|
|
foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
|
|
|
|
|
$exists = $this->doDirectoryExists( "{$fullCont}{$suffix}", $dir, $params );
|
2019-08-30 07:01:29 +00:00
|
|
|
if ( $exists === true ) {
|
2012-04-05 05:56:08 +00:00
|
|
|
$res = true;
|
|
|
|
|
break; // found one!
|
2019-08-30 07:01:29 +00:00
|
|
|
} elseif ( $exists === self::$RES_ERROR ) {
|
|
|
|
|
$res = self::EXISTENCE_ERROR;
|
2012-04-05 05:56:08 +00:00
|
|
|
}
|
|
|
|
|
}
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2012-04-05 05:56:08 +00:00
|
|
|
return $res;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see FileBackendStore::directoryExists()
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $container Resolved container name
|
|
|
|
|
* @param string $dir Resolved path relative to container
|
2013-03-11 18:00:35 +00:00
|
|
|
* @param array $params
|
2012-04-05 05:56:08 +00:00
|
|
|
* @return bool|null
|
|
|
|
|
*/
|
|
|
|
|
abstract protected function doDirectoryExists( $container, $dir, array $params );
|
|
|
|
|
|
|
|
|
|
final public function getDirectoryList( array $params ) {
|
|
|
|
|
list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
|
2019-08-30 07:01:29 +00:00
|
|
|
if ( $dir === null ) {
|
|
|
|
|
return self::EXISTENCE_ERROR; // invalid storage path
|
2012-04-05 05:56:08 +00:00
|
|
|
}
|
|
|
|
|
if ( $shard !== null ) {
|
|
|
|
|
// File listing is confined to a single container/shard
|
|
|
|
|
return $this->getDirectoryListInternal( $fullCont, $dir, $params );
|
|
|
|
|
} else {
|
2020-06-01 05:00:39 +00:00
|
|
|
$this->logger->debug( __METHOD__ . ": iterating over all container shards." );
|
2012-04-05 05:56:08 +00:00
|
|
|
// File listing spans multiple containers/shards
|
2012-12-09 03:27:02 +00:00
|
|
|
list( , $shortCont, ) = self::splitStoragePath( $params['dir'] );
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2012-04-05 05:56:08 +00:00
|
|
|
return new FileBackendStoreShardDirIterator( $this,
|
|
|
|
|
$fullCont, $dir, $this->getContainerSuffixes( $shortCont ), $params );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Do not call this function from places outside FileBackend
|
|
|
|
|
*
|
|
|
|
|
* @see FileBackendStore::getDirectoryList()
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $container Resolved container name
|
|
|
|
|
* @param string $dir Resolved path relative to container
|
2013-03-11 18:00:35 +00:00
|
|
|
* @param array $params
|
2019-08-30 07:01:29 +00:00
|
|
|
* @return Traversable|array|null Iterable list or null (error)
|
2012-04-05 05:56:08 +00:00
|
|
|
*/
|
|
|
|
|
abstract public function getDirectoryListInternal( $container, $dir, array $params );
|
|
|
|
|
|
2012-03-03 18:29:38 +00:00
|
|
|
final public function getFileList( array $params ) {
|
|
|
|
|
list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
|
2019-08-30 07:01:29 +00:00
|
|
|
if ( $dir === null ) {
|
|
|
|
|
return self::LIST_ERROR; // invalid storage path
|
2012-03-03 18:29:38 +00:00
|
|
|
}
|
|
|
|
|
if ( $shard !== null ) {
|
|
|
|
|
// File listing is confined to a single container/shard
|
|
|
|
|
return $this->getFileListInternal( $fullCont, $dir, $params );
|
|
|
|
|
} else {
|
2020-06-01 05:00:39 +00:00
|
|
|
$this->logger->debug( __METHOD__ . ": iterating over all container shards." );
|
2012-03-03 18:29:38 +00:00
|
|
|
// File listing spans multiple containers/shards
|
2012-12-09 03:27:02 +00:00
|
|
|
list( , $shortCont, ) = self::splitStoragePath( $params['dir'] );
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2012-04-05 05:56:08 +00:00
|
|
|
return new FileBackendStoreShardFileIterator( $this,
|
2012-03-03 18:29:38 +00:00
|
|
|
$fullCont, $dir, $this->getContainerSuffixes( $shortCont ), $params );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Do not call this function from places outside FileBackend
|
|
|
|
|
*
|
|
|
|
|
* @see FileBackendStore::getFileList()
|
2012-04-06 20:13:06 +00:00
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $container Resolved container name
|
|
|
|
|
* @param string $dir Resolved path relative to container
|
2013-03-11 18:00:35 +00:00
|
|
|
* @param array $params
|
2019-08-30 07:01:29 +00:00
|
|
|
* @return Traversable|string[]|null Iterable list or null (error)
|
2012-03-03 18:29:38 +00:00
|
|
|
*/
|
|
|
|
|
abstract public function getFileListInternal( $container, $dir, array $params );
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return a list of FileOp objects from a list of operations.
|
|
|
|
|
* Do not call this function from places outside FileBackend.
|
|
|
|
|
*
|
|
|
|
|
* The result must have the same number of items as the input.
|
|
|
|
|
* An exception is thrown if an unsupported operation is requested.
|
2012-04-06 20:13:06 +00:00
|
|
|
*
|
2019-10-12 12:49:17 +00:00
|
|
|
* @param array[] $ops Same format as doOperations()
|
2021-06-17 14:32:05 +00:00
|
|
|
* @return FileOp[]
|
2013-11-23 20:28:34 +00:00
|
|
|
* @throws FileBackendError
|
2012-03-03 18:29:38 +00:00
|
|
|
*/
|
2012-04-24 10:55:11 +00:00
|
|
|
final public function getOperationsInternal( array $ops ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$supportedOps = [
|
2018-01-13 00:02:09 +00:00
|
|
|
'store' => StoreFileOp::class,
|
|
|
|
|
'copy' => CopyFileOp::class,
|
|
|
|
|
'move' => MoveFileOp::class,
|
|
|
|
|
'delete' => DeleteFileOp::class,
|
|
|
|
|
'create' => CreateFileOp::class,
|
|
|
|
|
'describe' => DescribeFileOp::class,
|
|
|
|
|
'null' => NullFileOp::class
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2012-03-03 18:29:38 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$performOps = []; // array of FileOp objects
|
2012-03-03 18:29:38 +00:00
|
|
|
// Build up ordered array of FileOps...
|
|
|
|
|
foreach ( $ops as $operation ) {
|
|
|
|
|
$opName = $operation['op'];
|
|
|
|
|
if ( isset( $supportedOps[$opName] ) ) {
|
|
|
|
|
$class = $supportedOps[$opName];
|
|
|
|
|
// Get params for this operation
|
|
|
|
|
$params = $operation;
|
|
|
|
|
// Append the FileOp class
|
2016-09-18 23:07:34 +00:00
|
|
|
$performOps[] = new $class( $this, $params, $this->logger );
|
2012-03-03 18:29:38 +00:00
|
|
|
} else {
|
2013-11-23 20:28:34 +00:00
|
|
|
throw new FileBackendError( "Operation '$opName' is not supported." );
|
2012-03-03 18:29:38 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $performOps;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-24 18:03:10 +00:00
|
|
|
/**
|
|
|
|
|
* Get a list of storage paths to lock for a list of operations
|
2013-06-12 19:31:15 +00:00
|
|
|
* Returns an array with LockManager::LOCK_UW (shared locks) and
|
|
|
|
|
* LockManager::LOCK_EX (exclusive locks) keys, each corresponding
|
|
|
|
|
* to a list of storage paths to be locked. All returned paths are
|
|
|
|
|
* normalized.
|
2012-04-24 18:03:10 +00:00
|
|
|
*
|
2019-09-10 05:02:19 +00:00
|
|
|
* @param FileOp[] $performOps List of FileOp objects
|
|
|
|
|
* @return string[][] (LockManager::LOCK_UW => path list, LockManager::LOCK_EX => path list)
|
2012-04-24 18:03:10 +00:00
|
|
|
*/
|
|
|
|
|
final public function getPathsToLockForOpsInternal( array $performOps ) {
|
|
|
|
|
// Build up a list of files to lock...
|
2016-02-17 09:09:32 +00:00
|
|
|
$paths = [ 'sh' => [], 'ex' => [] ];
|
2012-04-24 18:03:10 +00:00
|
|
|
foreach ( $performOps as $fileOp ) {
|
|
|
|
|
$paths['sh'] = array_merge( $paths['sh'], $fileOp->storagePathsRead() );
|
|
|
|
|
$paths['ex'] = array_merge( $paths['ex'], $fileOp->storagePathsChanged() );
|
|
|
|
|
}
|
|
|
|
|
// Optimization: if doing an EX lock anyway, don't also set an SH one
|
|
|
|
|
$paths['sh'] = array_diff( $paths['sh'], $paths['ex'] );
|
|
|
|
|
// Get a shared lock on the parent directory of each path changed
|
|
|
|
|
$paths['sh'] = array_merge( $paths['sh'], array_map( 'dirname', $paths['ex'] ) );
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
2013-06-12 19:31:15 +00:00
|
|
|
LockManager::LOCK_UW => $paths['sh'],
|
|
|
|
|
LockManager::LOCK_EX => $paths['ex']
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2012-04-24 18:03:10 +00:00
|
|
|
}
|
|
|
|
|
|
2016-09-16 22:55:40 +00:00
|
|
|
public function getScopedLocksForOps( array $ops, StatusValue $status ) {
|
2012-04-28 19:49:46 +00:00
|
|
|
$paths = $this->getPathsToLockForOpsInternal( $this->getOperationsInternal( $ops ) );
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2015-09-18 17:45:15 +00:00
|
|
|
return $this->getScopedFileLocks( $paths, 'mixed', $status );
|
2012-04-28 19:49:46 +00:00
|
|
|
}
|
|
|
|
|
|
2012-05-17 23:34:52 +00:00
|
|
|
final protected function doOperationsInternal( array $ops, array $opts ) {
|
2019-07-17 11:55:19 +00:00
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */
|
2016-09-18 21:40:38 +00:00
|
|
|
$ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
|
2016-09-16 22:55:40 +00:00
|
|
|
$status = $this->newStatus();
|
2012-03-03 18:29:38 +00:00
|
|
|
|
2019-09-10 05:02:19 +00:00
|
|
|
// Fix up custom header name/value pairs
|
2016-02-17 09:09:32 +00:00
|
|
|
$ops = array_map( [ $this, 'sanitizeOpHeaders' ], $ops );
|
2019-09-10 05:02:19 +00:00
|
|
|
// Build up a list of FileOps and involved paths
|
|
|
|
|
$fileOps = $this->getOperationsInternal( $ops );
|
|
|
|
|
$pathsUsed = [];
|
|
|
|
|
foreach ( $fileOps as $fileOp ) {
|
|
|
|
|
$pathsUsed = array_merge( $pathsUsed, $fileOp->storagePathsReadOrChanged() );
|
|
|
|
|
}
|
2012-11-20 01:38:17 +00:00
|
|
|
|
2019-09-10 05:02:19 +00:00
|
|
|
// Acquire any locks as needed for the scope of this function
|
2012-03-03 18:29:38 +00:00
|
|
|
if ( empty( $opts['nonLocking'] ) ) {
|
2019-09-10 05:02:19 +00:00
|
|
|
$pathsByLockType = $this->getPathsToLockForOpsInternal( $fileOps );
|
2019-07-17 11:55:19 +00:00
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */
|
2019-09-10 05:02:19 +00:00
|
|
|
$scopeLock = $this->getScopedFileLocks( $pathsByLockType, 'mixed', $status );
|
2012-03-03 18:29:38 +00:00
|
|
|
if ( !$status->isOK() ) {
|
|
|
|
|
return $status; // abort
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-23 20:27:58 +00:00
|
|
|
// Clear any file cache entries (after locks acquired)
|
2012-08-26 09:11:31 +00:00
|
|
|
if ( empty( $opts['preserveCache'] ) ) {
|
2019-09-10 05:02:19 +00:00
|
|
|
$this->clearCache( $pathsUsed );
|
2014-01-16 00:46:01 +00:00
|
|
|
}
|
2014-04-19 00:02:19 +00:00
|
|
|
|
|
|
|
|
// Enlarge the cache to fit the stat entries of these files
|
2019-09-10 05:02:19 +00:00
|
|
|
$this->cheapCache->setMaxSize( max( 2 * count( $pathsUsed ), self::CACHE_CHEAP_SIZE ) );
|
2014-04-19 00:02:19 +00:00
|
|
|
|
2014-01-16 00:46:01 +00:00
|
|
|
// Load from the persistent container caches
|
2019-09-10 05:02:19 +00:00
|
|
|
$this->primeContainerCache( $pathsUsed );
|
2014-01-16 00:46:01 +00:00
|
|
|
// Get the latest stat info for all the files (having locked them)
|
2019-09-10 05:02:19 +00:00
|
|
|
$ok = $this->preloadFileStat( [ 'srcs' => $pathsUsed, 'latest' => true ] );
|
2012-04-23 20:27:58 +00:00
|
|
|
|
2014-04-18 18:34:39 +00:00
|
|
|
if ( $ok ) {
|
|
|
|
|
// Actually attempt the operation batch...
|
|
|
|
|
$opts = $this->setConcurrencyFlags( $opts );
|
2021-08-12 01:33:53 +00:00
|
|
|
$subStatus = FileOpBatch::attempt( $fileOps, $opts );
|
2014-04-18 18:34:39 +00:00
|
|
|
} else {
|
2019-09-10 05:02:19 +00:00
|
|
|
// If we could not even stat some files, then bail out
|
2016-09-16 22:55:40 +00:00
|
|
|
$subStatus = $this->newStatus( 'backend-fail-internal', $this->name );
|
2014-04-18 18:34:39 +00:00
|
|
|
foreach ( $ops as $i => $op ) { // mark each op as failed
|
|
|
|
|
$subStatus->success[$i] = false;
|
|
|
|
|
++$subStatus->failCount;
|
|
|
|
|
}
|
2017-03-07 02:14:14 +00:00
|
|
|
$this->logger->error( static::class . "-{$this->name} " .
|
2014-05-23 00:37:33 +00:00
|
|
|
" stat failure; aborted operations: " . FormatJson::encode( $ops ) );
|
2014-04-18 18:34:39 +00:00
|
|
|
}
|
2012-03-03 18:29:38 +00:00
|
|
|
|
2016-09-16 22:55:40 +00:00
|
|
|
// Merge errors into StatusValue fields
|
2012-03-03 18:29:38 +00:00
|
|
|
$status->merge( $subStatus );
|
|
|
|
|
$status->success = $subStatus->success; // not done in merge()
|
|
|
|
|
|
2014-04-19 00:02:19 +00:00
|
|
|
// Shrink the stat cache back to normal size
|
2018-07-11 12:53:13 +00:00
|
|
|
$this->cheapCache->setMaxSize( self::CACHE_CHEAP_SIZE );
|
2014-04-19 00:02:19 +00:00
|
|
|
|
2012-03-03 18:29:38 +00:00
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-28 17:41:39 +00:00
|
|
|
final protected function doQuickOperationsInternal( array $ops, array $opts ) {
|
2019-07-17 11:55:19 +00:00
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */
|
2016-09-18 21:40:38 +00:00
|
|
|
$ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
|
2016-09-16 22:55:40 +00:00
|
|
|
$status = $this->newStatus();
|
2012-05-16 00:42:20 +00:00
|
|
|
|
2019-09-10 05:02:19 +00:00
|
|
|
// Fix up custom header name/value pairs
|
2016-02-17 09:09:32 +00:00
|
|
|
$ops = array_map( [ $this, 'sanitizeOpHeaders' ], $ops );
|
2019-09-10 05:02:19 +00:00
|
|
|
// Build up a list of FileOps and involved paths
|
|
|
|
|
$fileOps = $this->getOperationsInternal( $ops );
|
|
|
|
|
$pathsUsed = [];
|
|
|
|
|
foreach ( $fileOps as $fileOp ) {
|
|
|
|
|
$pathsUsed = array_merge( $pathsUsed, $fileOp->storagePathsReadOrChanged() );
|
|
|
|
|
}
|
2012-11-20 01:38:17 +00:00
|
|
|
|
2019-09-10 05:02:19 +00:00
|
|
|
// Clear any file cache entries for involved paths
|
|
|
|
|
$this->clearCache( $pathsUsed );
|
2012-11-17 18:47:49 +00:00
|
|
|
|
2014-06-18 17:18:49 +00:00
|
|
|
// Parallel ops may be disabled in config due to dependencies (e.g. needing popen())
|
2013-05-15 17:34:31 +00:00
|
|
|
$async = ( $this->parallelize === 'implicit' && count( $ops ) > 1 );
|
2012-05-16 00:42:20 +00:00
|
|
|
$maxConcurrency = $this->concurrency; // throttle
|
2016-09-16 22:55:40 +00:00
|
|
|
/** @var StatusValue[] $statuses */
|
|
|
|
|
$statuses = []; // array of (index => StatusValue)
|
2016-02-17 09:09:32 +00:00
|
|
|
$fileOpHandles = []; // list of (index => handle) arrays
|
|
|
|
|
$curFileOpHandles = []; // current handle batch
|
2012-05-16 00:42:20 +00:00
|
|
|
// Perform the sync-only ops and build up op handles for the async ops...
|
2019-09-10 05:02:19 +00:00
|
|
|
foreach ( $fileOps as $index => $fileOp ) {
|
|
|
|
|
$subStatus = $async
|
|
|
|
|
? $fileOp->attemptAsyncQuick()
|
|
|
|
|
: $fileOp->attemptQuick();
|
2012-05-16 00:42:20 +00:00
|
|
|
if ( $subStatus->value instanceof FileBackendStoreOpHandle ) { // async
|
|
|
|
|
if ( count( $curFileOpHandles ) >= $maxConcurrency ) {
|
|
|
|
|
$fileOpHandles[] = $curFileOpHandles; // push this batch
|
2016-02-17 09:09:32 +00:00
|
|
|
$curFileOpHandles = [];
|
2012-05-16 00:42:20 +00:00
|
|
|
}
|
|
|
|
|
$curFileOpHandles[$index] = $subStatus->value; // keep index
|
|
|
|
|
} else { // error or completed
|
|
|
|
|
$statuses[$index] = $subStatus; // keep index
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( count( $curFileOpHandles ) ) {
|
|
|
|
|
$fileOpHandles[] = $curFileOpHandles; // last batch
|
|
|
|
|
}
|
|
|
|
|
// Do all the async ops that can be done concurrently...
|
|
|
|
|
foreach ( $fileOpHandles as $fileHandleBatch ) {
|
2020-07-31 19:41:28 +00:00
|
|
|
$statuses += $this->executeOpHandlesInternal( $fileHandleBatch );
|
2012-05-16 00:42:20 +00:00
|
|
|
}
|
|
|
|
|
// Marshall and merge all the responses...
|
|
|
|
|
foreach ( $statuses as $index => $subStatus ) {
|
|
|
|
|
$status->merge( $subStatus );
|
|
|
|
|
if ( $subStatus->isOK() ) {
|
|
|
|
|
$status->success[$index] = true;
|
|
|
|
|
++$status->successCount;
|
|
|
|
|
} else {
|
|
|
|
|
$status->success[$index] = false;
|
|
|
|
|
++$status->failCount;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-10 05:02:19 +00:00
|
|
|
$this->clearCache( $pathsUsed );
|
|
|
|
|
|
2012-05-16 00:42:20 +00:00
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-11 17:51:02 +00:00
|
|
|
/**
|
|
|
|
|
* Execute a list of FileBackendStoreOpHandle handles in parallel.
|
2016-09-16 22:55:40 +00:00
|
|
|
* The resulting StatusValue object fields will correspond
|
2012-04-11 17:51:02 +00:00
|
|
|
* to the order in which the handles where given.
|
|
|
|
|
*
|
2015-03-18 16:22:52 +00:00
|
|
|
* @param FileBackendStoreOpHandle[] $fileOpHandles
|
2016-09-16 22:55:40 +00:00
|
|
|
* @return StatusValue[] Map of StatusValue objects
|
2017-03-13 21:26:56 +00:00
|
|
|
* @throws FileBackendError
|
2012-04-11 17:51:02 +00:00
|
|
|
*/
|
|
|
|
|
final public function executeOpHandlesInternal( array $fileOpHandles ) {
|
2019-07-17 11:55:19 +00:00
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */
|
2016-09-18 21:40:38 +00:00
|
|
|
$ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
|
2013-11-23 20:28:34 +00:00
|
|
|
|
2012-04-11 17:51:02 +00:00
|
|
|
foreach ( $fileOpHandles as $fileOpHandle ) {
|
|
|
|
|
if ( !( $fileOpHandle instanceof FileBackendStoreOpHandle ) ) {
|
2017-03-13 21:26:56 +00:00
|
|
|
throw new InvalidArgumentException( "Expected FileBackendStoreOpHandle object." );
|
2012-04-11 17:51:02 +00:00
|
|
|
} elseif ( $fileOpHandle->backend->getName() !== $this->getName() ) {
|
2017-03-13 21:26:56 +00:00
|
|
|
throw new InvalidArgumentException( "Expected handle for this file backend." );
|
2012-04-11 17:51:02 +00:00
|
|
|
}
|
|
|
|
|
}
|
2017-03-13 21:26:56 +00:00
|
|
|
|
2019-09-03 08:04:58 +00:00
|
|
|
$statuses = $this->doExecuteOpHandlesInternal( $fileOpHandles );
|
2012-05-18 00:21:39 +00:00
|
|
|
foreach ( $fileOpHandles as $fileOpHandle ) {
|
|
|
|
|
$fileOpHandle->closeResources();
|
|
|
|
|
}
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2019-09-03 08:04:58 +00:00
|
|
|
return $statuses;
|
2012-04-11 17:51:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see FileBackendStore::executeOpHandlesInternal()
|
2020-07-13 08:57:12 +00:00
|
|
|
* @stable to override
|
2015-03-18 16:22:52 +00:00
|
|
|
*
|
|
|
|
|
* @param FileBackendStoreOpHandle[] $fileOpHandles
|
|
|
|
|
*
|
2013-11-23 20:28:34 +00:00
|
|
|
* @throws FileBackendError
|
2016-09-16 22:55:40 +00:00
|
|
|
* @return StatusValue[] List of corresponding StatusValue objects
|
2012-04-11 17:51:02 +00:00
|
|
|
*/
|
|
|
|
|
protected function doExecuteOpHandlesInternal( array $fileOpHandles ) {
|
2013-11-23 18:36:50 +00:00
|
|
|
if ( count( $fileOpHandles ) ) {
|
2019-07-17 11:55:19 +00:00
|
|
|
throw new FileBackendError( "Backend does not support asynchronous operations." );
|
2012-04-11 17:51:02 +00:00
|
|
|
}
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
return [];
|
2012-04-11 17:51:02 +00:00
|
|
|
}
|
|
|
|
|
|
2012-11-20 01:38:17 +00:00
|
|
|
/**
|
2015-10-27 19:21:30 +00:00
|
|
|
* Normalize and filter HTTP headers from a file operation
|
|
|
|
|
*
|
|
|
|
|
* This normalizes and strips long HTTP headers from a file operation.
|
2013-03-09 23:09:25 +00:00
|
|
|
* Most headers are just numbers, but some are allowed to be long.
|
|
|
|
|
* This function is useful for cleaning up headers and avoiding backend
|
|
|
|
|
* specific errors, especially in the middle of batch file operations.
|
2012-11-20 01:38:17 +00:00
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $op Same format as doOperation()
|
2013-11-23 18:23:32 +00:00
|
|
|
* @return array
|
2012-11-20 01:38:17 +00:00
|
|
|
*/
|
2015-10-27 19:21:30 +00:00
|
|
|
protected function sanitizeOpHeaders( array $op ) {
|
2017-06-08 20:30:07 +00:00
|
|
|
static $longs = [ 'content-disposition' ];
|
2015-10-27 19:21:30 +00:00
|
|
|
|
2013-03-09 23:09:25 +00:00
|
|
|
if ( isset( $op['headers'] ) ) { // op sets HTTP headers
|
2016-02-17 09:09:32 +00:00
|
|
|
$newHeaders = [];
|
2012-11-20 01:38:17 +00:00
|
|
|
foreach ( $op['headers'] as $name => $value ) {
|
2015-10-27 19:21:30 +00:00
|
|
|
$name = strtolower( $name );
|
2013-03-09 23:09:25 +00:00
|
|
|
$maxHVLen = in_array( $name, $longs ) ? INF : 255;
|
|
|
|
|
if ( strlen( $name ) > 255 || strlen( $value ) > $maxHVLen ) {
|
2019-09-26 00:57:01 +00:00
|
|
|
$this->logger->error( "Header '{header}' is too long.", [
|
|
|
|
|
'filebackend' => $this->name,
|
|
|
|
|
'header' => "$name: $value",
|
|
|
|
|
] );
|
2015-10-27 19:21:30 +00:00
|
|
|
} else {
|
|
|
|
|
$newHeaders[$name] = strlen( $value ) ? $value : ''; // null/false => ""
|
2012-11-20 01:38:17 +00:00
|
|
|
}
|
|
|
|
|
}
|
2015-10-27 19:21:30 +00:00
|
|
|
$op['headers'] = $newHeaders;
|
2012-11-20 01:38:17 +00:00
|
|
|
}
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2012-11-20 01:38:17 +00:00
|
|
|
return $op;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-24 06:17:16 +00:00
|
|
|
final public function preloadCache( array $paths ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$fullConts = []; // full container names
|
2012-08-24 06:17:16 +00:00
|
|
|
foreach ( $paths as $path ) {
|
2012-12-09 03:27:02 +00:00
|
|
|
list( $fullCont, , ) = $this->resolveStoragePath( $path );
|
2012-08-24 06:17:16 +00:00
|
|
|
$fullConts[] = $fullCont;
|
|
|
|
|
}
|
|
|
|
|
// Load from the persistent file and container caches
|
|
|
|
|
$this->primeContainerCache( $fullConts );
|
|
|
|
|
$this->primeFileCache( $paths );
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-03 18:29:38 +00:00
|
|
|
final public function clearCache( array $paths = null ) {
|
|
|
|
|
if ( is_array( $paths ) ) {
|
2020-12-31 11:37:36 +00:00
|
|
|
$paths = array_map( [ FileBackend::class, 'normalizeStoragePath' ], $paths );
|
2012-03-03 18:29:38 +00:00
|
|
|
$paths = array_filter( $paths, 'strlen' ); // remove nulls
|
|
|
|
|
}
|
|
|
|
|
if ( $paths === null ) {
|
2012-06-30 03:08:06 +00:00
|
|
|
$this->cheapCache->clear();
|
|
|
|
|
$this->expensiveCache->clear();
|
2012-03-03 18:29:38 +00:00
|
|
|
} else {
|
|
|
|
|
foreach ( $paths as $path ) {
|
2012-06-30 03:08:06 +00:00
|
|
|
$this->cheapCache->clear( $path );
|
|
|
|
|
$this->expensiveCache->clear( $path );
|
2012-03-03 18:29:38 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$this->doClearCache( $paths );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Clears any additional stat caches for storage paths
|
2020-07-13 08:57:12 +00:00
|
|
|
* @stable to override
|
2012-04-06 20:13:06 +00:00
|
|
|
*
|
2012-03-03 18:29:38 +00:00
|
|
|
* @see FileBackend::clearCache()
|
2012-04-06 20:13:06 +00:00
|
|
|
*
|
2018-06-26 21:14:43 +00:00
|
|
|
* @param array|null $paths Storage paths (optional)
|
2012-03-03 18:29:38 +00:00
|
|
|
*/
|
2013-11-22 21:17:15 +00:00
|
|
|
protected function doClearCache( array $paths = null ) {
|
|
|
|
|
}
|
2012-03-03 18:29:38 +00:00
|
|
|
|
2014-01-16 00:46:01 +00:00
|
|
|
final public function preloadFileStat( array $params ) {
|
2019-07-17 11:55:19 +00:00
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */
|
2016-09-18 21:40:38 +00:00
|
|
|
$ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
|
2014-01-16 00:46:01 +00:00
|
|
|
|
|
|
|
|
$params['concurrency'] = ( $this->parallelize !== 'off' ) ? $this->concurrency : 1;
|
|
|
|
|
$stats = $this->doGetFileStatMulti( $params );
|
|
|
|
|
if ( $stats === null ) {
|
2014-04-18 18:34:39 +00:00
|
|
|
return true; // not supported
|
2014-01-16 00:46:01 +00:00
|
|
|
}
|
|
|
|
|
|
2019-08-30 07:01:29 +00:00
|
|
|
// Whether this queried the backend in high consistency mode
|
|
|
|
|
$latest = !empty( $params['latest'] );
|
2014-04-18 18:34:39 +00:00
|
|
|
|
2019-08-30 07:01:29 +00:00
|
|
|
return $this->ingestFreshFileStats( $stats, $latest );
|
2014-01-16 00:46:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get file stat information (concurrently if possible) for several files
|
2020-07-13 08:57:12 +00:00
|
|
|
* @stable to override
|
2014-01-16 00:46:01 +00:00
|
|
|
*
|
|
|
|
|
* @see FileBackend::getFileStat()
|
|
|
|
|
*
|
|
|
|
|
* @param array $params Parameters include:
|
|
|
|
|
* - srcs : list of source storage paths
|
|
|
|
|
* - latest : use the latest available data
|
|
|
|
|
* @return array|null Map of storage paths to array|bool|null (returns null if not supported)
|
|
|
|
|
* @since 1.23
|
|
|
|
|
*/
|
|
|
|
|
protected function doGetFileStatMulti( array $params ) {
|
|
|
|
|
return null; // not supported
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-06 20:13:06 +00:00
|
|
|
/**
|
2012-04-26 18:40:47 +00:00
|
|
|
* Is this a key/value store where directories are just virtual?
|
|
|
|
|
* Virtual directories exists in so much as files exists that are
|
|
|
|
|
* prefixed with the directory path followed by a forward slash.
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
abstract protected function directoriesAreVirtual();
|
|
|
|
|
|
2012-03-03 18:29:38 +00:00
|
|
|
/**
|
2015-06-20 00:10:49 +00:00
|
|
|
* Check if a short container name is valid
|
|
|
|
|
*
|
|
|
|
|
* This checks for length and illegal characters.
|
|
|
|
|
* This may disallow certain characters that can appear
|
|
|
|
|
* in the prefix used to make the full container name.
|
|
|
|
|
*
|
|
|
|
|
* @param string $container
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
final protected static function isValidShortContainerName( $container ) {
|
|
|
|
|
// Suffixes like '.xxx' (hex shard chars) or '.seg' (file segments)
|
2021-11-19 23:19:42 +00:00
|
|
|
// might be used by subclasses. Reserve the dot character.
|
2015-06-20 00:10:49 +00:00
|
|
|
// The only way dots end up in containers (e.g. resolveStoragePath)
|
|
|
|
|
// is due to the wikiId container prefix or the above suffixes.
|
|
|
|
|
return self::isValidContainerName( $container ) && !preg_match( '/[.]/', $container );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if a full container name is valid
|
|
|
|
|
*
|
2014-12-16 00:41:45 +00:00
|
|
|
* This checks for length and illegal characters.
|
2015-06-20 00:10:49 +00:00
|
|
|
* Limiting the characters makes migrations to other stores easier.
|
2012-04-06 20:13:06 +00:00
|
|
|
*
|
2013-06-13 18:18:52 +00:00
|
|
|
* @param string $container
|
2012-03-03 18:29:38 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
final protected static function isValidContainerName( $container ) {
|
2015-06-20 00:10:49 +00:00
|
|
|
// This accounts for NTFS, Swift, and Ceph restrictions
|
|
|
|
|
// and disallows directory separators or traversal characters.
|
2012-03-03 18:29:38 +00:00
|
|
|
// Note that matching strings URL encode to the same string;
|
2015-06-20 00:10:49 +00:00
|
|
|
// in Swift/Ceph, the length restriction is *after* URL encoding.
|
|
|
|
|
return (bool)preg_match( '/^[a-z0-9][a-z0-9-_.]{0,199}$/i', $container );
|
2012-03-03 18:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Splits a storage path into an internal container name,
|
|
|
|
|
* an internal relative file name, and a container shard suffix.
|
|
|
|
|
* Any shard suffix is already appended to the internal container name.
|
|
|
|
|
* This also checks that the storage path is valid and within this backend.
|
|
|
|
|
*
|
|
|
|
|
* If the container is sharded but a suffix could not be determined,
|
|
|
|
|
* this means that the path can only refer to a directory and can only
|
|
|
|
|
* be scanned by looking in all the container shards.
|
|
|
|
|
*
|
2013-06-13 18:18:52 +00:00
|
|
|
* @param string $storagePath
|
2013-11-23 18:23:32 +00:00
|
|
|
* @return array (container, path, container suffix) or (null, null, null) if invalid
|
2012-03-03 18:29:38 +00:00
|
|
|
*/
|
|
|
|
|
final protected function resolveStoragePath( $storagePath ) {
|
2015-06-20 00:10:49 +00:00
|
|
|
list( $backend, $shortCont, $relPath ) = self::splitStoragePath( $storagePath );
|
2012-03-03 18:29:38 +00:00
|
|
|
if ( $backend === $this->name ) { // must be for this backend
|
|
|
|
|
$relPath = self::normalizeContainerPath( $relPath );
|
2015-06-20 00:10:49 +00:00
|
|
|
if ( $relPath !== null && self::isValidShortContainerName( $shortCont ) ) {
|
2012-03-03 18:29:38 +00:00
|
|
|
// Get shard for the normalized path if this container is sharded
|
2015-06-20 00:10:49 +00:00
|
|
|
$cShard = $this->getContainerShard( $shortCont, $relPath );
|
2012-03-03 18:29:38 +00:00
|
|
|
// Validate and sanitize the relative path (backend-specific)
|
2015-06-20 00:10:49 +00:00
|
|
|
$relPath = $this->resolveContainerPath( $shortCont, $relPath );
|
2012-03-03 18:29:38 +00:00
|
|
|
if ( $relPath !== null ) {
|
2019-09-09 11:44:36 +00:00
|
|
|
// Prepend any domain ID prefix to the container name
|
2015-06-20 00:10:49 +00:00
|
|
|
$container = $this->fullContainerName( $shortCont );
|
2012-03-03 18:29:38 +00:00
|
|
|
if ( self::isValidContainerName( $container ) ) {
|
|
|
|
|
// Validate and sanitize the container name (backend-specific)
|
|
|
|
|
$container = $this->resolveContainerName( "{$container}{$cShard}" );
|
|
|
|
|
if ( $container !== null ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [ $container, $relPath, $cShard ];
|
2012-03-03 18:29:38 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
return [ null, null, null ];
|
2012-03-03 18:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Like resolveStoragePath() except null values are returned if
|
2013-04-04 23:50:51 +00:00
|
|
|
* the container is sharded and the shard could not be determined
|
2016-09-07 20:12:38 +00:00
|
|
|
* or if the path ends with '/'. The latter case is illegal for FS
|
2013-04-04 23:50:51 +00:00
|
|
|
* backends and can confuse listings for object store backends.
|
|
|
|
|
*
|
|
|
|
|
* This function is used when resolving paths that must be valid
|
|
|
|
|
* locations for files. Directory and listing functions should
|
|
|
|
|
* generally just use resolveStoragePath() instead.
|
2012-03-03 18:29:38 +00:00
|
|
|
*
|
|
|
|
|
* @see FileBackendStore::resolveStoragePath()
|
|
|
|
|
*
|
2013-06-13 18:18:52 +00:00
|
|
|
* @param string $storagePath
|
2013-11-23 18:23:32 +00:00
|
|
|
* @return array (container, path) or (null, null) if invalid
|
2012-03-03 18:29:38 +00:00
|
|
|
*/
|
|
|
|
|
final protected function resolveStoragePathReal( $storagePath ) {
|
|
|
|
|
list( $container, $relPath, $cShard ) = $this->resolveStoragePath( $storagePath );
|
2013-04-04 23:50:51 +00:00
|
|
|
if ( $cShard !== null && substr( $relPath, -1 ) !== '/' ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [ $container, $relPath ];
|
2012-03-03 18:29:38 +00:00
|
|
|
}
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
return [ null, null ];
|
2012-03-03 18:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the container name shard suffix for a given path.
|
|
|
|
|
* Any empty suffix means the container is not sharded.
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $container Container name
|
|
|
|
|
* @param string $relPath Storage path relative to the container
|
2012-03-03 18:29:38 +00:00
|
|
|
* @return string|null Returns null if shard could not be determined
|
|
|
|
|
*/
|
|
|
|
|
final protected function getContainerShard( $container, $relPath ) {
|
|
|
|
|
list( $levels, $base, $repeat ) = $this->getContainerHashLevels( $container );
|
|
|
|
|
if ( $levels == 1 || $levels == 2 ) {
|
|
|
|
|
// Hash characters are either base 16 or 36
|
|
|
|
|
$char = ( $base == 36 ) ? '[0-9a-z]' : '[0-9a-f]';
|
|
|
|
|
// Get a regex that represents the shard portion of paths.
|
|
|
|
|
// The concatenation of the captures gives us the shard.
|
|
|
|
|
if ( $levels === 1 ) { // 16 or 36 shards per container
|
|
|
|
|
$hashDirRegex = '(' . $char . ')';
|
|
|
|
|
} else { // 256 or 1296 shards per container
|
|
|
|
|
if ( $repeat ) { // verbose hash dir format (e.g. "a/ab/abc")
|
|
|
|
|
$hashDirRegex = $char . '/(' . $char . '{2})';
|
|
|
|
|
} else { // short hash dir format (e.g. "a/b/c")
|
|
|
|
|
$hashDirRegex = '(' . $char . ')/(' . $char . ')';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Allow certain directories to be above the hash dirs so as
|
|
|
|
|
// to work with FileRepo (e.g. "archive/a/ab" or "temp/a/ab").
|
|
|
|
|
// They must be 2+ chars to avoid any hash directory ambiguity.
|
2016-02-17 09:09:32 +00:00
|
|
|
$m = [];
|
2012-03-03 18:29:38 +00:00
|
|
|
if ( preg_match( "!^(?:[^/]{2,}/)*$hashDirRegex(?:/|$)!", $relPath, $m ) ) {
|
|
|
|
|
return '.' . implode( '', array_slice( $m, 1 ) );
|
|
|
|
|
}
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2012-03-03 18:29:38 +00:00
|
|
|
return null; // failed to match
|
|
|
|
|
}
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2012-03-03 18:29:38 +00:00
|
|
|
return ''; // no sharding
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-05 05:56:08 +00:00
|
|
|
/**
|
|
|
|
|
* Check if a storage path maps to a single shard.
|
|
|
|
|
* Container dirs like "a", where the container shards on "x/xy",
|
|
|
|
|
* can reside on several shards. Such paths are tricky to handle.
|
|
|
|
|
*
|
2021-11-26 15:21:17 +00:00
|
|
|
* @param string $storagePath
|
2012-04-05 05:56:08 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
final public function isSingleShardPathInternal( $storagePath ) {
|
2012-12-09 03:27:02 +00:00
|
|
|
list( , , $shard ) = $this->resolveStoragePath( $storagePath );
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2012-04-05 05:56:08 +00:00
|
|
|
return ( $shard !== null );
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-03 18:29:38 +00:00
|
|
|
/**
|
|
|
|
|
* Get the sharding config for a container.
|
|
|
|
|
* If greater than 0, then all file storage paths within
|
|
|
|
|
* the container are required to be hashed accordingly.
|
|
|
|
|
*
|
2013-06-13 18:18:52 +00:00
|
|
|
* @param string $container
|
2013-11-23 18:23:32 +00:00
|
|
|
* @return array (integer levels, integer base, repeat flag) or (0, 0, false)
|
2012-03-03 18:29:38 +00:00
|
|
|
*/
|
|
|
|
|
final protected function getContainerHashLevels( $container ) {
|
|
|
|
|
if ( isset( $this->shardViaHashLevels[$container] ) ) {
|
|
|
|
|
$config = $this->shardViaHashLevels[$container];
|
|
|
|
|
$hashLevels = (int)$config['levels'];
|
|
|
|
|
if ( $hashLevels == 1 || $hashLevels == 2 ) {
|
|
|
|
|
$hashBase = (int)$config['base'];
|
|
|
|
|
if ( $hashBase == 16 || $hashBase == 36 ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [ $hashLevels, $hashBase, $config['repeat'] ];
|
2012-03-03 18:29:38 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
return [ 0, 0, false ]; // no sharding
|
2012-03-03 18:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a list of full container shard suffixes for a container
|
2012-04-06 20:13:06 +00:00
|
|
|
*
|
2013-06-13 18:18:52 +00:00
|
|
|
* @param string $container
|
2013-11-23 18:23:32 +00:00
|
|
|
* @return array
|
2012-03-03 18:29:38 +00:00
|
|
|
*/
|
|
|
|
|
final protected function getContainerSuffixes( $container ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$shards = [];
|
2012-03-03 18:29:38 +00:00
|
|
|
list( $digits, $base ) = $this->getContainerHashLevels( $container );
|
|
|
|
|
if ( $digits > 0 ) {
|
2017-10-06 19:17:52 +00:00
|
|
|
$numShards = $base ** $digits;
|
2012-03-03 18:29:38 +00:00
|
|
|
for ( $index = 0; $index < $numShards; $index++ ) {
|
2022-02-26 16:28:48 +00:00
|
|
|
$shards[] = '.' . Wikimedia\base_convert( (string)$index, 10, $base, $digits );
|
2012-03-03 18:29:38 +00:00
|
|
|
}
|
|
|
|
|
}
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2012-03-03 18:29:38 +00:00
|
|
|
return $shards;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-09-09 11:44:36 +00:00
|
|
|
* Get the full container name, including the domain ID prefix
|
2012-04-06 20:13:06 +00:00
|
|
|
*
|
2013-06-13 18:18:52 +00:00
|
|
|
* @param string $container
|
2012-04-06 20:13:06 +00:00
|
|
|
* @return string
|
2012-03-03 18:29:38 +00:00
|
|
|
*/
|
|
|
|
|
final protected function fullContainerName( $container ) {
|
2016-09-18 21:40:38 +00:00
|
|
|
if ( $this->domainId != '' ) {
|
|
|
|
|
return "{$this->domainId}-$container";
|
2012-03-03 18:29:38 +00:00
|
|
|
} else {
|
|
|
|
|
return $container;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Resolve a container name, checking if it's allowed by the backend.
|
|
|
|
|
* This is intended for internal use, such as encoding illegal chars.
|
|
|
|
|
* Subclasses can override this to be more restrictive.
|
2020-07-13 08:57:12 +00:00
|
|
|
* @stable to override
|
2012-04-06 20:13:06 +00:00
|
|
|
*
|
2013-06-13 18:18:52 +00:00
|
|
|
* @param string $container
|
2012-04-06 20:13:06 +00:00
|
|
|
* @return string|null
|
2012-03-03 18:29:38 +00:00
|
|
|
*/
|
|
|
|
|
protected function resolveContainerName( $container ) {
|
|
|
|
|
return $container;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Resolve a relative storage path, checking if it's allowed by the backend.
|
|
|
|
|
* This is intended for internal use, such as encoding illegal chars or perhaps
|
|
|
|
|
* getting absolute paths (e.g. FS based backends). Note that the relative path
|
|
|
|
|
* may be the empty string (e.g. the path is simply to the container).
|
2020-07-13 08:57:12 +00:00
|
|
|
* @stable to override
|
2012-03-03 18:29:38 +00:00
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $container Container name
|
|
|
|
|
* @param string $relStoragePath Storage path relative to the container
|
2012-03-03 18:29:38 +00:00
|
|
|
* @return string|null Path or null if not valid
|
|
|
|
|
*/
|
|
|
|
|
protected function resolveContainerPath( $container, $relStoragePath ) {
|
|
|
|
|
return $relStoragePath;
|
|
|
|
|
}
|
2012-04-23 20:27:58 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the cache key for a container
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $container Resolved container name
|
2012-04-23 20:27:58 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
private function containerCacheKey( $container ) {
|
2016-09-18 21:40:38 +00:00
|
|
|
return "filebackend:{$this->name}:{$this->domainId}:container:{$container}";
|
2012-04-23 20:27:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the cached info for a container
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $container Resolved container name
|
2013-04-14 20:32:34 +00:00
|
|
|
* @param array $val Information to cache
|
2012-04-23 20:27:58 +00:00
|
|
|
*/
|
2013-04-14 20:32:34 +00:00
|
|
|
final protected function setContainerCache( $container, array $val ) {
|
2015-04-27 21:13:02 +00:00
|
|
|
$this->memCache->set( $this->containerCacheKey( $container ), $val, 14 * 86400 );
|
2012-04-23 20:27:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-06-07 19:50:43 +00:00
|
|
|
* Delete the cached info for a container.
|
|
|
|
|
* The cache key is salted for a while to prevent race conditions.
|
2012-04-23 20:27:58 +00:00
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $container Resolved container name
|
2012-04-23 20:27:58 +00:00
|
|
|
*/
|
|
|
|
|
final protected function deleteContainerCache( $container ) {
|
2015-04-27 21:13:02 +00:00
|
|
|
if ( !$this->memCache->delete( $this->containerCacheKey( $container ), 300 ) ) {
|
2019-09-26 00:57:01 +00:00
|
|
|
$this->logger->warning( "Unable to delete stat cache for container {container}.",
|
|
|
|
|
[ 'filebackend' => $this->name, 'container' => $container ]
|
|
|
|
|
);
|
2012-04-27 00:46:28 +00:00
|
|
|
}
|
2012-04-23 20:27:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Do a batch lookup from cache for container stats for all containers
|
2014-01-16 00:46:01 +00:00
|
|
|
* used in a list of container names or storage paths objects.
|
2012-10-18 18:45:12 +00:00
|
|
|
* This loads the persistent cache values into the process cache.
|
2012-04-23 20:27:58 +00:00
|
|
|
*
|
2013-11-23 18:23:32 +00:00
|
|
|
* @param array $items
|
2012-04-23 20:27:58 +00:00
|
|
|
*/
|
|
|
|
|
final protected function primeContainerCache( array $items ) {
|
2019-07-17 11:55:19 +00:00
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */
|
2016-09-18 21:40:38 +00:00
|
|
|
$ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
|
2012-04-11 17:51:02 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$paths = []; // list of storage paths
|
|
|
|
|
$contNames = []; // (cache key => resolved container name)
|
2012-04-23 20:27:58 +00:00
|
|
|
// Get all the paths/containers from the items...
|
|
|
|
|
foreach ( $items as $item ) {
|
2014-01-16 00:46:01 +00:00
|
|
|
if ( self::isStoragePath( $item ) ) {
|
2012-04-23 20:27:58 +00:00
|
|
|
$paths[] = $item;
|
|
|
|
|
} elseif ( is_string( $item ) ) { // full container name
|
|
|
|
|
$contNames[$this->containerCacheKey( $item )] = $item;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Get all the corresponding cache keys for paths...
|
|
|
|
|
foreach ( $paths as $path ) {
|
2012-12-09 03:27:02 +00:00
|
|
|
list( $fullCont, , ) = $this->resolveStoragePath( $path );
|
2012-04-23 20:27:58 +00:00
|
|
|
if ( $fullCont !== null ) { // valid path for this backend
|
|
|
|
|
$contNames[$this->containerCacheKey( $fullCont )] = $fullCont;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$contInfo = []; // (resolved container name => cache value)
|
2012-04-23 20:27:58 +00:00
|
|
|
// Get all cache entries for these container cache keys...
|
2012-05-15 08:48:22 +00:00
|
|
|
$values = $this->memCache->getMulti( array_keys( $contNames ) );
|
2012-04-23 20:27:58 +00:00
|
|
|
foreach ( $values as $cacheKey => $val ) {
|
|
|
|
|
$contInfo[$contNames[$cacheKey]] = $val;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Populate the container process cache for the backend...
|
|
|
|
|
$this->doPrimeContainerCache( array_filter( $contInfo, 'is_array' ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fill the backend-specific process cache given an array of
|
|
|
|
|
* resolved container names and their corresponding cached info.
|
|
|
|
|
* Only containers that actually exist should appear in the map.
|
2020-07-13 08:57:12 +00:00
|
|
|
* @stable to override
|
2012-04-23 20:27:58 +00:00
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $containerInfo Map of resolved container names to cached info
|
2012-04-23 20:27:58 +00:00
|
|
|
*/
|
2013-11-22 21:17:15 +00:00
|
|
|
protected function doPrimeContainerCache( array $containerInfo ) {
|
|
|
|
|
}
|
2012-04-27 00:46:28 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the cache key for a file path
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $path Normalized storage path
|
2012-04-27 00:46:28 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
private function fileCacheKey( $path ) {
|
2016-09-18 21:40:38 +00:00
|
|
|
return "filebackend:{$this->name}:{$this->domainId}:file:" . sha1( $path );
|
2012-04-27 00:46:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-08-30 19:47:19 +00:00
|
|
|
* Set the cached stat info for a file path.
|
|
|
|
|
* Negatives (404s) are not cached. By not caching negatives, we can skip cache
|
|
|
|
|
* salting for the case when a file is created at a path were there was none before.
|
2012-04-27 00:46:28 +00:00
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $path Storage path
|
2013-04-14 20:32:34 +00:00
|
|
|
* @param array $val Stat information to cache
|
2012-04-27 00:46:28 +00:00
|
|
|
*/
|
2013-04-14 20:32:34 +00:00
|
|
|
final protected function setFileCache( $path, array $val ) {
|
2012-10-18 18:45:12 +00:00
|
|
|
$path = FileBackend::normalizeStoragePath( $path );
|
|
|
|
|
if ( $path === null ) {
|
|
|
|
|
return; // invalid storage path
|
|
|
|
|
}
|
2021-10-16 21:47:01 +00:00
|
|
|
$mtime = (int)ConvertibleTimestamp::convert( TS_UNIX, $val['mtime'] );
|
2017-09-10 19:11:37 +00:00
|
|
|
$ttl = $this->memCache->adaptiveTTL( $mtime, 7 * 86400, 300, 0.1 );
|
2014-03-15 09:08:04 +00:00
|
|
|
$key = $this->fileCacheKey( $path );
|
2015-04-27 21:13:02 +00:00
|
|
|
// Set the cache unless it is currently salted.
|
|
|
|
|
$this->memCache->set( $key, $val, $ttl );
|
2012-04-27 00:46:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-06-07 19:50:43 +00:00
|
|
|
* Delete the cached stat info for a file path.
|
|
|
|
|
* The cache key is salted for a while to prevent race conditions.
|
2012-11-16 23:25:50 +00:00
|
|
|
* Since negatives (404s) are not cached, this does not need to be called when
|
|
|
|
|
* a file is created at a path were there was none before.
|
2012-04-27 00:46:28 +00:00
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $path Storage path
|
2012-04-27 00:46:28 +00:00
|
|
|
*/
|
|
|
|
|
final protected function deleteFileCache( $path ) {
|
2012-10-18 18:45:12 +00:00
|
|
|
$path = FileBackend::normalizeStoragePath( $path );
|
|
|
|
|
if ( $path === null ) {
|
|
|
|
|
return; // invalid storage path
|
|
|
|
|
}
|
2015-04-27 21:13:02 +00:00
|
|
|
if ( !$this->memCache->delete( $this->fileCacheKey( $path ), 300 ) ) {
|
2019-09-26 00:57:01 +00:00
|
|
|
$this->logger->warning( "Unable to delete stat cache for file {path}.",
|
|
|
|
|
[ 'filebackend' => $this->name, 'path' => $path ]
|
|
|
|
|
);
|
2012-04-27 00:46:28 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Do a batch lookup from cache for file stats for all paths
|
|
|
|
|
* used in a list of storage paths or FileOp objects.
|
2012-10-18 18:45:12 +00:00
|
|
|
* This loads the persistent cache values into the process cache.
|
2012-04-27 00:46:28 +00:00
|
|
|
*
|
2014-01-16 00:46:01 +00:00
|
|
|
* @param array $items List of storage paths
|
2012-04-27 00:46:28 +00:00
|
|
|
*/
|
|
|
|
|
final protected function primeFileCache( array $items ) {
|
2019-07-17 11:55:19 +00:00
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */
|
2016-09-18 21:40:38 +00:00
|
|
|
$ps = $this->scopedProfileSection( __METHOD__ . "-{$this->name}" );
|
2012-04-11 17:51:02 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$paths = []; // list of storage paths
|
|
|
|
|
$pathNames = []; // (cache key => storage path)
|
2012-04-27 00:46:28 +00:00
|
|
|
// Get all the paths/containers from the items...
|
|
|
|
|
foreach ( $items as $item ) {
|
2014-01-16 00:46:01 +00:00
|
|
|
if ( self::isStoragePath( $item ) ) {
|
2022-03-05 18:55:11 +00:00
|
|
|
$path = FileBackend::normalizeStoragePath( $item );
|
|
|
|
|
if ( $path !== null ) {
|
|
|
|
|
$paths[] = $path;
|
|
|
|
|
}
|
2012-04-27 00:46:28 +00:00
|
|
|
}
|
|
|
|
|
}
|
2019-08-30 07:01:29 +00:00
|
|
|
// Get rid of any paths that failed normalization
|
2012-10-18 18:45:12 +00:00
|
|
|
$paths = array_filter( $paths, 'strlen' ); // remove nulls
|
2012-04-27 00:46:28 +00:00
|
|
|
// Get all the corresponding cache keys for paths...
|
|
|
|
|
foreach ( $paths as $path ) {
|
2012-12-09 03:27:02 +00:00
|
|
|
list( , $rel, ) = $this->resolveStoragePath( $path );
|
2012-04-27 00:46:28 +00:00
|
|
|
if ( $rel !== null ) { // valid path for this backend
|
|
|
|
|
$pathNames[$this->fileCacheKey( $path )] = $path;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-30 07:01:29 +00:00
|
|
|
// Get all cache entries for these file cache keys.
|
|
|
|
|
// Note that negatives are not cached by getFileStat()/preloadFileStat().
|
2012-05-15 08:48:22 +00:00
|
|
|
$values = $this->memCache->getMulti( array_keys( $pathNames ) );
|
2019-08-30 07:01:29 +00:00
|
|
|
// Load all of the results into process cache...
|
|
|
|
|
foreach ( array_filter( $values, 'is_array' ) as $cacheKey => $stat ) {
|
2015-01-25 09:19:24 +00:00
|
|
|
$path = $pathNames[$cacheKey];
|
2021-11-19 23:19:42 +00:00
|
|
|
// This flag only applies to stat info loaded directly
|
2019-08-30 07:01:29 +00:00
|
|
|
// from a high consistency backend query to the process cache
|
|
|
|
|
unset( $stat['latest'] );
|
|
|
|
|
|
|
|
|
|
$this->cheapCache->setField( $path, 'stat', $stat );
|
|
|
|
|
if ( isset( $stat['sha1'] ) && strlen( $stat['sha1'] ) == 31 ) {
|
|
|
|
|
// Some backends store SHA-1 as metadata
|
|
|
|
|
$this->cheapCache->setField(
|
|
|
|
|
$path,
|
|
|
|
|
'sha1',
|
|
|
|
|
[ 'hash' => $stat['sha1'], 'latest' => false ]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if ( isset( $stat['xattr'] ) && is_array( $stat['xattr'] ) ) {
|
|
|
|
|
// Some backends store custom headers/metadata
|
|
|
|
|
$stat['xattr'] = self::normalizeXAttributes( $stat['xattr'] );
|
|
|
|
|
$this->cheapCache->setField(
|
|
|
|
|
$path,
|
|
|
|
|
'xattr',
|
|
|
|
|
[ 'map' => $stat['xattr'], 'latest' => false ]
|
|
|
|
|
);
|
2012-04-27 00:46:28 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-09-18 23:04:13 +00:00
|
|
|
|
2013-09-30 07:12:10 +00:00
|
|
|
/**
|
|
|
|
|
* Normalize file headers/metadata to the FileBackend::getFileXAttributes() format
|
|
|
|
|
*
|
|
|
|
|
* @param array $xattr
|
|
|
|
|
* @return array
|
|
|
|
|
* @since 1.22
|
|
|
|
|
*/
|
|
|
|
|
final protected static function normalizeXAttributes( array $xattr ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$newXAttr = [ 'headers' => [], 'metadata' => [] ];
|
2014-02-05 11:02:29 +00:00
|
|
|
|
2013-09-30 07:12:10 +00:00
|
|
|
foreach ( $xattr['headers'] as $name => $value ) {
|
|
|
|
|
$newXAttr['headers'][strtolower( $name )] = $value;
|
|
|
|
|
}
|
2014-02-05 11:02:29 +00:00
|
|
|
|
2013-09-30 07:12:10 +00:00
|
|
|
foreach ( $xattr['metadata'] as $name => $value ) {
|
|
|
|
|
$newXAttr['metadata'][strtolower( $name )] = $value;
|
|
|
|
|
}
|
2014-02-05 11:02:29 +00:00
|
|
|
|
2013-09-30 07:12:10 +00:00
|
|
|
return $newXAttr;
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-18 23:04:13 +00:00
|
|
|
/**
|
|
|
|
|
* Set the 'concurrency' option from a list of operation options
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $opts Map of operation options
|
2013-11-23 18:23:32 +00:00
|
|
|
* @return array
|
2012-09-18 23:04:13 +00:00
|
|
|
*/
|
|
|
|
|
final protected function setConcurrencyFlags( array $opts ) {
|
|
|
|
|
$opts['concurrency'] = 1; // off
|
|
|
|
|
if ( $this->parallelize === 'implicit' ) {
|
2020-03-05 14:59:37 +00:00
|
|
|
if ( $opts['parallelize'] ?? true ) {
|
2012-09-18 23:04:13 +00:00
|
|
|
$opts['concurrency'] = $this->concurrency;
|
|
|
|
|
}
|
|
|
|
|
} elseif ( $this->parallelize === 'explicit' ) {
|
|
|
|
|
if ( !empty( $opts['parallelize'] ) ) {
|
|
|
|
|
$opts['concurrency'] = $this->concurrency;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-22 21:17:15 +00:00
|
|
|
|
2012-09-18 23:04:13 +00:00
|
|
|
return $opts;
|
|
|
|
|
}
|
2013-09-23 19:58:26 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the content type to use in HEAD/GET requests for a file
|
2020-07-13 08:57:12 +00:00
|
|
|
* @stable to override
|
2013-09-23 19:58:26 +00:00
|
|
|
*
|
|
|
|
|
* @param string $storagePath
|
|
|
|
|
* @param string|null $content File data
|
|
|
|
|
* @param string|null $fsPath File system path
|
2014-07-24 17:43:03 +00:00
|
|
|
* @return string MIME type
|
2013-09-23 19:58:26 +00:00
|
|
|
*/
|
|
|
|
|
protected function getContentType( $storagePath, $content, $fsPath ) {
|
2014-11-14 06:47:06 +00:00
|
|
|
if ( $this->mimeCallback ) {
|
|
|
|
|
return call_user_func_array( $this->mimeCallback, func_get_args() );
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-25 05:11:17 +00:00
|
|
|
$mime = ( $fsPath !== null ) ? mime_content_type( $fsPath ) : false;
|
|
|
|
|
return $mime ?: 'unknown/unknown';
|
2013-09-23 19:58:26 +00:00
|
|
|
}
|
2012-03-03 18:29:38 +00:00
|
|
|
}
|