2011-12-20 03:52:06 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
2012-05-07 07:11:33 +00:00
|
|
|
* Helper class for representing operations with transaction support.
|
|
|
|
|
*
|
|
|
|
|
* 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
|
|
|
|
|
*
|
2011-12-20 03:52:06 +00:00
|
|
|
* @file
|
|
|
|
|
* @ingroup FileBackend
|
|
|
|
|
* @author Aaron Schulz
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Helper class for representing operations with transaction support.
|
2012-01-13 23:30:46 +00:00
|
|
|
* Do not use this class from places outside FileBackend.
|
2012-01-24 05:54:47 +00:00
|
|
|
*
|
|
|
|
|
* Methods called from attemptBatch() should avoid throwing exceptions at all costs.
|
|
|
|
|
* FileOp objects should be lightweight in order to support large arrays in memory.
|
2012-04-03 22:45:12 +00:00
|
|
|
*
|
2011-12-20 03:52:06 +00:00
|
|
|
* @ingroup FileBackend
|
|
|
|
|
* @since 1.19
|
|
|
|
|
*/
|
|
|
|
|
abstract class FileOp {
|
2012-01-15 22:45:14 +00:00
|
|
|
/** @var Array */
|
2011-12-20 03:52:06 +00:00
|
|
|
protected $params = array();
|
2012-01-29 22:22:28 +00:00
|
|
|
/** @var FileBackendStore */
|
2011-12-20 03:52:06 +00:00
|
|
|
protected $backend;
|
|
|
|
|
|
|
|
|
|
protected $state = self::STATE_NEW; // integer
|
|
|
|
|
protected $failed = false; // boolean
|
2012-01-05 06:18:36 +00:00
|
|
|
protected $useLatest = true; // boolean
|
2012-03-13 01:46:33 +00:00
|
|
|
protected $batchId; // string
|
2012-01-15 22:45:14 +00:00
|
|
|
|
|
|
|
|
protected $sourceSha1; // string
|
|
|
|
|
protected $destSameAsSource; // boolean
|
2011-12-20 03:52:06 +00:00
|
|
|
|
|
|
|
|
/* Object life-cycle */
|
|
|
|
|
const STATE_NEW = 1;
|
|
|
|
|
const STATE_CHECKED = 2;
|
|
|
|
|
const STATE_ATTEMPTED = 3;
|
|
|
|
|
|
2012-01-19 02:07:48 +00:00
|
|
|
/* Timeout related parameters */
|
|
|
|
|
const MAX_BATCH_SIZE = 1000;
|
|
|
|
|
const TIME_LIMIT_SEC = 300; // 5 minutes
|
|
|
|
|
|
2011-12-20 03:52:06 +00:00
|
|
|
/**
|
|
|
|
|
* Build a new file operation transaction
|
|
|
|
|
*
|
2012-02-19 23:40:02 +00:00
|
|
|
* @param $backend FileBackendStore
|
|
|
|
|
* @param $params Array
|
2012-01-24 05:54:47 +00:00
|
|
|
* @throws MWException
|
2011-12-20 03:52:06 +00:00
|
|
|
*/
|
2012-01-29 22:22:28 +00:00
|
|
|
final public function __construct( FileBackendStore $backend, array $params ) {
|
2011-12-20 03:52:06 +00:00
|
|
|
$this->backend = $backend;
|
2012-01-24 18:49:53 +00:00
|
|
|
list( $required, $optional ) = $this->allowedParams();
|
|
|
|
|
foreach ( $required as $name ) {
|
2012-01-24 05:54:47 +00:00
|
|
|
if ( isset( $params[$name] ) ) {
|
|
|
|
|
$this->params[$name] = $params[$name];
|
|
|
|
|
} else {
|
|
|
|
|
throw new MWException( "File operation missing parameter '$name'." );
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-01-24 18:49:53 +00:00
|
|
|
foreach ( $optional as $name ) {
|
2011-12-20 03:52:06 +00:00
|
|
|
if ( isset( $params[$name] ) ) {
|
|
|
|
|
$this->params[$name] = $params[$name];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$this->params = $params;
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-13 01:46:33 +00:00
|
|
|
/**
|
|
|
|
|
* Set the batch UUID this operation belongs to
|
|
|
|
|
*
|
|
|
|
|
* @param $batchId string
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
final protected function setBatchId( $batchId ) {
|
|
|
|
|
$this->batchId = $batchId;
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-05 06:18:36 +00:00
|
|
|
/**
|
2012-03-09 19:36:19 +00:00
|
|
|
* Whether to allow stale data for file reads and stat checks
|
2012-01-12 19:41:18 +00:00
|
|
|
*
|
2012-03-09 19:36:19 +00:00
|
|
|
* @param $allowStale bool
|
2012-01-12 19:41:18 +00:00
|
|
|
* @return void
|
2012-01-05 06:18:36 +00:00
|
|
|
*/
|
2012-03-09 19:36:19 +00:00
|
|
|
final protected function allowStaleReads( $allowStale ) {
|
|
|
|
|
$this->useLatest = !$allowStale;
|
2012-01-05 06:18:36 +00:00
|
|
|
}
|
|
|
|
|
|
2011-12-20 03:52:06 +00:00
|
|
|
/**
|
2012-03-13 01:46:33 +00:00
|
|
|
* Attempt to perform a series of file operations.
|
2011-12-20 03:52:06 +00:00
|
|
|
* Callers are responsible for handling file locking.
|
2012-04-03 22:45:12 +00:00
|
|
|
*
|
2012-01-09 21:04:48 +00:00
|
|
|
* $opts is an array of options, including:
|
2012-03-13 01:46:33 +00:00
|
|
|
* 'force' : Errors that would normally cause a rollback do not.
|
|
|
|
|
* The remaining operations are still attempted if any fail.
|
|
|
|
|
* 'allowStale' : Don't require the latest available data.
|
|
|
|
|
* This can increase performance for non-critical writes.
|
|
|
|
|
* This has no effect unless the 'force' flag is set.
|
|
|
|
|
* 'nonJournaled' : Don't log this operation batch in the file journal.
|
2012-04-03 22:45:12 +00:00
|
|
|
*
|
2012-03-08 22:31:04 +00:00
|
|
|
* The resulting Status will be "OK" unless:
|
|
|
|
|
* a) unexpected operation errors occurred (network partitions, disk full...)
|
|
|
|
|
* b) significant operation errors occured and 'force' was not set
|
2012-04-03 22:45:12 +00:00
|
|
|
*
|
2011-12-20 03:52:06 +00:00
|
|
|
* @param $performOps Array List of FileOp operations
|
|
|
|
|
* @param $opts Array Batch operation options
|
2012-03-13 01:46:33 +00:00
|
|
|
* @param $journal FileJournal Journal to log operations to
|
2012-04-03 22:45:12 +00:00
|
|
|
* @return Status
|
2011-12-20 03:52:06 +00:00
|
|
|
*/
|
2012-03-13 01:46:33 +00:00
|
|
|
final public static function attemptBatch(
|
|
|
|
|
array $performOps, array $opts, FileJournal $journal
|
|
|
|
|
) {
|
2011-12-20 03:52:06 +00:00
|
|
|
$status = Status::newGood();
|
|
|
|
|
|
2012-01-19 02:07:48 +00:00
|
|
|
$n = count( $performOps );
|
|
|
|
|
if ( $n > self::MAX_BATCH_SIZE ) {
|
|
|
|
|
$status->fatal( 'backend-fail-batchsize', $n, self::MAX_BATCH_SIZE );
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-13 01:46:33 +00:00
|
|
|
$batchId = $journal->getTimestampedUUID();
|
|
|
|
|
$allowStale = !empty( $opts['allowStale'] );
|
|
|
|
|
$ignoreErrors = !empty( $opts['force'] );
|
|
|
|
|
$journaled = empty( $opts['nonJournaled'] );
|
|
|
|
|
|
|
|
|
|
$entries = array(); // file journal entries
|
2011-12-20 03:52:06 +00:00
|
|
|
$predicates = FileOp::newPredicates(); // account for previous op in prechecks
|
|
|
|
|
// Do pre-checks for each operation; abort on failure...
|
|
|
|
|
foreach ( $performOps as $index => $fileOp ) {
|
2012-03-13 01:46:33 +00:00
|
|
|
$fileOp->setBatchId( $batchId );
|
2012-03-09 19:36:19 +00:00
|
|
|
$fileOp->allowStaleReads( $allowStale );
|
2012-03-13 01:46:33 +00:00
|
|
|
$oldPredicates = $predicates;
|
|
|
|
|
$subStatus = $fileOp->precheck( $predicates ); // updates $predicates
|
2012-01-15 22:45:14 +00:00
|
|
|
$status->merge( $subStatus );
|
2012-03-13 01:46:33 +00:00
|
|
|
if ( $subStatus->isOK() ) {
|
|
|
|
|
if ( $journaled ) { // journal log entry
|
|
|
|
|
$entries = array_merge( $entries,
|
|
|
|
|
self::getJournalEntries( $fileOp, $oldPredicates, $predicates ) );
|
|
|
|
|
}
|
|
|
|
|
} else { // operation failed?
|
2012-01-15 22:45:14 +00:00
|
|
|
$status->success[$index] = false;
|
|
|
|
|
++$status->failCount;
|
|
|
|
|
if ( !$ignoreErrors ) {
|
|
|
|
|
return $status; // abort
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-13 01:46:33 +00:00
|
|
|
// Log the operations in file journal...
|
|
|
|
|
if ( count( $entries ) ) {
|
|
|
|
|
$subStatus = $journal->logChangeBatch( $entries, $batchId );
|
|
|
|
|
if ( !$subStatus->isOK() ) {
|
|
|
|
|
return $subStatus; // abort
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $ignoreErrors ) { // treat precheck() fatals as mere warnings
|
2012-03-08 22:31:04 +00:00
|
|
|
$status->setResult( true, $status->value );
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-15 22:45:14 +00:00
|
|
|
// Attempt each operation...
|
2011-12-20 03:52:06 +00:00
|
|
|
foreach ( $performOps as $index => $fileOp ) {
|
|
|
|
|
if ( $fileOp->failed() ) {
|
|
|
|
|
continue; // nothing to do
|
|
|
|
|
}
|
2012-01-15 22:45:14 +00:00
|
|
|
$subStatus = $fileOp->attempt();
|
|
|
|
|
$status->merge( $subStatus );
|
2011-12-20 03:52:06 +00:00
|
|
|
if ( $subStatus->isOK() ) {
|
|
|
|
|
$status->success[$index] = true;
|
2012-01-15 22:45:14 +00:00
|
|
|
++$status->successCount;
|
2011-12-20 03:52:06 +00:00
|
|
|
} else {
|
|
|
|
|
$status->success[$index] = false;
|
2012-01-15 22:45:14 +00:00
|
|
|
++$status->failCount;
|
2012-03-06 22:59:59 +00:00
|
|
|
// We can't continue (even with $ignoreErrors) as $predicates is wrong.
|
|
|
|
|
// Log the remaining ops as failed for recovery...
|
|
|
|
|
for ( $i = ($index + 1); $i < count( $performOps ); $i++ ) {
|
|
|
|
|
$performOps[$i]->logFailure( 'attempt_aborted' );
|
2012-01-15 22:45:14 +00:00
|
|
|
}
|
2012-03-06 22:59:59 +00:00
|
|
|
return $status; // bail out
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-13 01:46:33 +00:00
|
|
|
/**
|
|
|
|
|
* Get the file journal entries for a single file operation
|
2012-04-03 22:45:12 +00:00
|
|
|
*
|
2012-03-13 01:46:33 +00:00
|
|
|
* @param $fileOp FileOp
|
|
|
|
|
* @param $oPredicates Array Pre-op information about files
|
|
|
|
|
* @param $nPredicates Array Post-op information about files
|
|
|
|
|
* @return Array
|
|
|
|
|
*/
|
|
|
|
|
final protected static function getJournalEntries(
|
|
|
|
|
FileOp $fileOp, array $oPredicates, array $nPredicates
|
|
|
|
|
) {
|
|
|
|
|
$nullEntries = array();
|
|
|
|
|
$updateEntries = array();
|
|
|
|
|
$deleteEntries = array();
|
|
|
|
|
$pathsUsed = array_merge( $fileOp->storagePathsRead(), $fileOp->storagePathsChanged() );
|
|
|
|
|
foreach ( $pathsUsed as $path ) {
|
|
|
|
|
$nullEntries[] = array( // assertion for recovery
|
|
|
|
|
'op' => 'null',
|
|
|
|
|
'path' => $path,
|
|
|
|
|
'newSha1' => $fileOp->fileSha1( $path, $oPredicates )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
foreach ( $fileOp->storagePathsChanged() as $path ) {
|
|
|
|
|
if ( $nPredicates['sha1'][$path] === false ) { // deleted
|
|
|
|
|
$deleteEntries[] = array(
|
|
|
|
|
'op' => 'delete',
|
|
|
|
|
'path' => $path,
|
|
|
|
|
'newSha1' => ''
|
|
|
|
|
);
|
|
|
|
|
} else { // created/updated
|
|
|
|
|
$updateEntries[] = array(
|
|
|
|
|
'op' => $fileOp->fileExists( $path, $oPredicates ) ? 'update' : 'create',
|
|
|
|
|
'path' => $path,
|
|
|
|
|
'newSha1' => $nPredicates['sha1'][$path]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return array_merge( $nullEntries, $updateEntries, $deleteEntries );
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-20 03:52:06 +00:00
|
|
|
/**
|
2012-01-15 22:45:14 +00:00
|
|
|
* Get the value of the parameter with the given name
|
2012-04-03 22:45:12 +00:00
|
|
|
*
|
2011-12-20 03:52:06 +00:00
|
|
|
* @param $name string
|
2012-01-15 22:45:14 +00:00
|
|
|
* @return mixed Returns null if the parameter is not set
|
2011-12-20 03:52:06 +00:00
|
|
|
*/
|
|
|
|
|
final public function getParam( $name ) {
|
|
|
|
|
return isset( $this->params[$name] ) ? $this->params[$name] : null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if this operation failed precheck() or attempt()
|
2012-04-03 22:45:12 +00:00
|
|
|
*
|
|
|
|
|
* @return bool
|
2011-12-20 03:52:06 +00:00
|
|
|
*/
|
|
|
|
|
final public function failed() {
|
|
|
|
|
return $this->failed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a new empty predicates array for precheck()
|
|
|
|
|
*
|
2012-04-03 22:45:12 +00:00
|
|
|
* @return Array
|
2011-12-20 03:52:06 +00:00
|
|
|
*/
|
|
|
|
|
final public static function newPredicates() {
|
2012-01-15 22:45:14 +00:00
|
|
|
return array( 'exists' => array(), 'sha1' => array() );
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check preconditions of the operation without writing anything
|
|
|
|
|
*
|
|
|
|
|
* @param $predicates Array
|
|
|
|
|
* @return Status
|
|
|
|
|
*/
|
|
|
|
|
final public function precheck( array &$predicates ) {
|
|
|
|
|
if ( $this->state !== self::STATE_NEW ) {
|
|
|
|
|
return Status::newFatal( 'fileop-fail-state', self::STATE_NEW, $this->state );
|
|
|
|
|
}
|
|
|
|
|
$this->state = self::STATE_CHECKED;
|
|
|
|
|
$status = $this->doPrecheck( $predicates );
|
|
|
|
|
if ( !$status->isOK() ) {
|
|
|
|
|
$this->failed = true;
|
|
|
|
|
}
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Attempt the operation, backing up files as needed; this must be reversible
|
|
|
|
|
*
|
|
|
|
|
* @return Status
|
|
|
|
|
*/
|
|
|
|
|
final public function attempt() {
|
|
|
|
|
if ( $this->state !== self::STATE_CHECKED ) {
|
|
|
|
|
return Status::newFatal( 'fileop-fail-state', self::STATE_CHECKED, $this->state );
|
|
|
|
|
} elseif ( $this->failed ) { // failed precheck
|
|
|
|
|
return Status::newFatal( 'fileop-fail-attempt-precheck' );
|
|
|
|
|
}
|
|
|
|
|
$this->state = self::STATE_ATTEMPTED;
|
|
|
|
|
$status = $this->doAttempt();
|
|
|
|
|
if ( !$status->isOK() ) {
|
|
|
|
|
$this->failed = true;
|
|
|
|
|
$this->logFailure( 'attempt' );
|
|
|
|
|
}
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-24 18:49:53 +00:00
|
|
|
/**
|
2012-01-25 01:57:28 +00:00
|
|
|
* Get the file operation parameters
|
2012-04-03 22:45:12 +00:00
|
|
|
*
|
2012-01-24 18:49:53 +00:00
|
|
|
* @return Array (required params list, optional params list)
|
|
|
|
|
*/
|
|
|
|
|
protected function allowedParams() {
|
|
|
|
|
return array( array(), array() );
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-20 03:52:06 +00:00
|
|
|
/**
|
|
|
|
|
* Get a list of storage paths read from for this operation
|
|
|
|
|
*
|
|
|
|
|
* @return Array
|
|
|
|
|
*/
|
|
|
|
|
public function storagePathsRead() {
|
|
|
|
|
return array();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a list of storage paths written to for this operation
|
|
|
|
|
*
|
|
|
|
|
* @return Array
|
|
|
|
|
*/
|
|
|
|
|
public function storagePathsChanged() {
|
|
|
|
|
return array();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return Status
|
|
|
|
|
*/
|
|
|
|
|
protected function doPrecheck( array &$predicates ) {
|
|
|
|
|
return Status::newGood();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return Status
|
|
|
|
|
*/
|
2012-01-29 19:23:26 +00:00
|
|
|
protected function doAttempt() {
|
|
|
|
|
return Status::newGood();
|
|
|
|
|
}
|
2011-12-20 03:52:06 +00:00
|
|
|
|
|
|
|
|
/**
|
2012-01-15 22:45:14 +00:00
|
|
|
* Check for errors with regards to the destination file already existing.
|
|
|
|
|
* This also updates the destSameAsSource and sourceSha1 member variables.
|
|
|
|
|
* A bad status will be returned if there is no chance it can be overwritten.
|
2012-04-03 22:45:12 +00:00
|
|
|
*
|
2011-12-20 03:52:06 +00:00
|
|
|
* @param $predicates Array
|
|
|
|
|
* @return Status
|
|
|
|
|
*/
|
|
|
|
|
protected function precheckDestExistence( array $predicates ) {
|
|
|
|
|
$status = Status::newGood();
|
2012-01-15 22:45:14 +00:00
|
|
|
// Get hash of source file/string and the destination file
|
|
|
|
|
$this->sourceSha1 = $this->getSourceSha1Base36(); // FS file or data string
|
|
|
|
|
if ( $this->sourceSha1 === null ) { // file in storage?
|
|
|
|
|
$this->sourceSha1 = $this->fileSha1( $this->params['src'], $predicates );
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
|
|
|
|
$this->destSameAsSource = false;
|
2012-01-15 22:45:14 +00:00
|
|
|
if ( $this->fileExists( $this->params['dst'], $predicates ) ) {
|
2012-01-19 02:24:49 +00:00
|
|
|
if ( $this->getParam( 'overwrite' ) ) {
|
2012-01-15 22:45:14 +00:00
|
|
|
return $status; // OK
|
|
|
|
|
} elseif ( $this->getParam( 'overwriteSame' ) ) {
|
|
|
|
|
$dhash = $this->fileSha1( $this->params['dst'], $predicates );
|
|
|
|
|
// Check if hashes are valid and match each other...
|
|
|
|
|
if ( !strlen( $this->sourceSha1 ) || !strlen( $dhash ) ) {
|
2011-12-20 03:52:06 +00:00
|
|
|
$status->fatal( 'backend-fail-hashes' );
|
2012-01-15 22:45:14 +00:00
|
|
|
} elseif ( $this->sourceSha1 !== $dhash ) {
|
2011-12-20 03:52:06 +00:00
|
|
|
// Give an error if the files are not identical
|
|
|
|
|
$status->fatal( 'backend-fail-notsame', $this->params['dst'] );
|
|
|
|
|
} else {
|
2012-01-15 22:45:14 +00:00
|
|
|
$this->destSameAsSource = true; // OK
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
|
|
|
|
return $status; // do nothing; either OK or bad status
|
2012-01-15 22:45:14 +00:00
|
|
|
} else {
|
|
|
|
|
$status->fatal( 'backend-fail-alreadyexists', $this->params['dst'] );
|
|
|
|
|
return $status;
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-01-15 22:45:14 +00:00
|
|
|
* precheckDestExistence() helper function to get the source file SHA-1.
|
|
|
|
|
* Subclasses should overwride this iff the source is not in storage.
|
2011-12-20 03:52:06 +00:00
|
|
|
*
|
2012-02-09 18:01:54 +00:00
|
|
|
* @return string|bool Returns false on failure
|
2011-12-20 03:52:06 +00:00
|
|
|
*/
|
|
|
|
|
protected function getSourceSha1Base36() {
|
|
|
|
|
return null; // N/A
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if a file will exist in storage when this operation is attempted
|
2012-04-03 22:45:12 +00:00
|
|
|
*
|
2011-12-20 03:52:06 +00:00
|
|
|
* @param $source string Storage path
|
|
|
|
|
* @param $predicates Array
|
2012-04-03 22:45:12 +00:00
|
|
|
* @return bool
|
2011-12-20 03:52:06 +00:00
|
|
|
*/
|
|
|
|
|
final protected function fileExists( $source, array $predicates ) {
|
|
|
|
|
if ( isset( $predicates['exists'][$source] ) ) {
|
|
|
|
|
return $predicates['exists'][$source]; // previous op assures this
|
|
|
|
|
} else {
|
2012-01-05 06:18:36 +00:00
|
|
|
$params = array( 'src' => $source, 'latest' => $this->useLatest );
|
|
|
|
|
return $this->backend->fileExists( $params );
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-15 22:45:14 +00:00
|
|
|
/**
|
|
|
|
|
* Get the SHA-1 of a file in storage when this operation is attempted
|
2012-04-03 22:45:12 +00:00
|
|
|
*
|
2012-01-15 22:45:14 +00:00
|
|
|
* @param $source string Storage path
|
|
|
|
|
* @param $predicates Array
|
2012-02-10 15:37:33 +00:00
|
|
|
* @return string|bool False on failure
|
2012-01-15 22:45:14 +00:00
|
|
|
*/
|
|
|
|
|
final protected function fileSha1( $source, array $predicates ) {
|
|
|
|
|
if ( isset( $predicates['sha1'][$source] ) ) {
|
|
|
|
|
return $predicates['sha1'][$source]; // previous op assures this
|
|
|
|
|
} else {
|
|
|
|
|
$params = array( 'src' => $source, 'latest' => $this->useLatest );
|
|
|
|
|
return $this->backend->getFileSha1Base36( $params );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-20 03:52:06 +00:00
|
|
|
/**
|
|
|
|
|
* Log a file operation failure and preserve any temp files
|
2012-04-03 22:45:12 +00:00
|
|
|
*
|
2012-01-15 22:45:14 +00:00
|
|
|
* @param $action string
|
2012-01-12 19:41:18 +00:00
|
|
|
* @return void
|
2011-12-20 03:52:06 +00:00
|
|
|
*/
|
|
|
|
|
final protected function logFailure( $action ) {
|
|
|
|
|
$params = $this->params;
|
|
|
|
|
$params['failedAction'] = $action;
|
|
|
|
|
try {
|
2012-03-13 01:46:33 +00:00
|
|
|
wfDebugLog( 'FileOperation', get_class( $this ) .
|
|
|
|
|
" failed (batch #{$this->batchId}): " . FormatJson::encode( $params ) );
|
2011-12-20 03:52:06 +00:00
|
|
|
} catch ( Exception $e ) {
|
|
|
|
|
// bad config? debug log error?
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Store a file into the backend from a file on the file system.
|
2012-01-29 22:22:28 +00:00
|
|
|
* Parameters similar to FileBackendStore::storeInternal(), which include:
|
2011-12-20 03:52:06 +00:00
|
|
|
* src : source path on file system
|
|
|
|
|
* dst : destination storage path
|
2012-01-19 02:24:49 +00:00
|
|
|
* overwrite : do nothing and pass if an identical file exists at destination
|
2011-12-20 03:52:06 +00:00
|
|
|
* overwriteSame : override any existing file at destination
|
|
|
|
|
*/
|
|
|
|
|
class StoreFileOp extends FileOp {
|
2012-01-24 18:49:53 +00:00
|
|
|
protected function allowedParams() {
|
|
|
|
|
return array( array( 'src', 'dst' ), array( 'overwrite', 'overwriteSame' ) );
|
|
|
|
|
}
|
2011-12-20 03:52:06 +00:00
|
|
|
|
|
|
|
|
protected function doPrecheck( array &$predicates ) {
|
|
|
|
|
$status = Status::newGood();
|
|
|
|
|
// Check if the source file exists on the file system
|
|
|
|
|
if ( !is_file( $this->params['src'] ) ) {
|
|
|
|
|
$status->fatal( 'backend-fail-notexists', $this->params['src'] );
|
|
|
|
|
return $status;
|
2012-01-19 02:07:48 +00:00
|
|
|
// Check if the source file is too big
|
2012-01-19 23:18:03 +00:00
|
|
|
} elseif ( filesize( $this->params['src'] ) > $this->backend->maxFileSizeInternal() ) {
|
|
|
|
|
$status->fatal( 'backend-fail-store', $this->params['src'], $this->params['dst'] );
|
|
|
|
|
return $status;
|
|
|
|
|
// Check if a file can be placed at the destination
|
|
|
|
|
} elseif ( !$this->backend->isPathUsableInternal( $this->params['dst'] ) ) {
|
|
|
|
|
$status->fatal( 'backend-fail-store', $this->params['src'], $this->params['dst'] );
|
2012-01-19 02:07:48 +00:00
|
|
|
return $status;
|
|
|
|
|
}
|
2012-01-15 22:45:14 +00:00
|
|
|
// Check if destination file exists
|
|
|
|
|
$status->merge( $this->precheckDestExistence( $predicates ) );
|
2012-01-19 23:18:03 +00:00
|
|
|
if ( $status->isOK() ) {
|
|
|
|
|
// Update file existence predicates
|
|
|
|
|
$predicates['exists'][$this->params['dst']] = true;
|
|
|
|
|
$predicates['sha1'][$this->params['dst']] = $this->sourceSha1;
|
2012-01-15 22:45:14 +00:00
|
|
|
}
|
|
|
|
|
return $status; // safe to call attempt()
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function doAttempt() {
|
|
|
|
|
$status = Status::newGood();
|
|
|
|
|
// Store the file at the destination
|
|
|
|
|
if ( !$this->destSameAsSource ) {
|
2011-12-21 09:16:28 +00:00
|
|
|
$status->merge( $this->backend->storeInternal( $this->params ) );
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function getSourceSha1Base36() {
|
2012-01-15 22:45:14 +00:00
|
|
|
wfSuppressWarnings();
|
|
|
|
|
$hash = sha1_file( $this->params['src'] );
|
|
|
|
|
wfRestoreWarnings();
|
|
|
|
|
if ( $hash !== false ) {
|
|
|
|
|
$hash = wfBaseConvert( $hash, 16, 36, 31 );
|
|
|
|
|
}
|
|
|
|
|
return $hash;
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function storagePathsChanged() {
|
|
|
|
|
return array( $this->params['dst'] );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a file in the backend with the given content.
|
2012-01-29 22:22:28 +00:00
|
|
|
* Parameters similar to FileBackendStore::createInternal(), which include:
|
2012-01-24 05:54:47 +00:00
|
|
|
* content : the raw file contents
|
2011-12-20 03:52:06 +00:00
|
|
|
* dst : destination storage path
|
2012-01-19 02:24:49 +00:00
|
|
|
* overwrite : do nothing and pass if an identical file exists at destination
|
2011-12-20 03:52:06 +00:00
|
|
|
* overwriteSame : override any existing file at destination
|
|
|
|
|
*/
|
|
|
|
|
class CreateFileOp extends FileOp {
|
2012-01-24 18:49:53 +00:00
|
|
|
protected function allowedParams() {
|
|
|
|
|
return array( array( 'content', 'dst' ), array( 'overwrite', 'overwriteSame' ) );
|
|
|
|
|
}
|
2011-12-20 03:52:06 +00:00
|
|
|
|
|
|
|
|
protected function doPrecheck( array &$predicates ) {
|
|
|
|
|
$status = Status::newGood();
|
2012-01-19 02:07:48 +00:00
|
|
|
// Check if the source data is too big
|
2012-01-23 14:48:49 +00:00
|
|
|
if ( strlen( $this->getParam( 'content' ) ) > $this->backend->maxFileSizeInternal() ) {
|
2012-01-19 02:07:48 +00:00
|
|
|
$status->fatal( 'backend-fail-create', $this->params['dst'] );
|
|
|
|
|
return $status;
|
2012-01-19 23:18:03 +00:00
|
|
|
// Check if a file can be placed at the destination
|
|
|
|
|
} elseif ( !$this->backend->isPathUsableInternal( $this->params['dst'] ) ) {
|
|
|
|
|
$status->fatal( 'backend-fail-create', $this->params['dst'] );
|
|
|
|
|
return $status;
|
2012-01-19 02:07:48 +00:00
|
|
|
}
|
2011-12-20 03:52:06 +00:00
|
|
|
// Check if destination file exists
|
|
|
|
|
$status->merge( $this->precheckDestExistence( $predicates ) );
|
2012-01-19 23:18:03 +00:00
|
|
|
if ( $status->isOK() ) {
|
|
|
|
|
// Update file existence predicates
|
|
|
|
|
$predicates['exists'][$this->params['dst']] = true;
|
|
|
|
|
$predicates['sha1'][$this->params['dst']] = $this->sourceSha1;
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
2012-01-15 22:45:14 +00:00
|
|
|
return $status; // safe to call attempt()
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function doAttempt() {
|
|
|
|
|
$status = Status::newGood();
|
|
|
|
|
// Create the file at the destination
|
|
|
|
|
if ( !$this->destSameAsSource ) {
|
2011-12-21 09:16:28 +00:00
|
|
|
$status->merge( $this->backend->createInternal( $this->params ) );
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function getSourceSha1Base36() {
|
|
|
|
|
return wfBaseConvert( sha1( $this->params['content'] ), 16, 36, 31 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function storagePathsChanged() {
|
|
|
|
|
return array( $this->params['dst'] );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Copy a file from one storage path to another in the backend.
|
2012-01-29 22:22:28 +00:00
|
|
|
* Parameters similar to FileBackendStore::copyInternal(), which include:
|
2011-12-20 03:52:06 +00:00
|
|
|
* src : source storage path
|
|
|
|
|
* dst : destination storage path
|
2012-01-19 02:24:49 +00:00
|
|
|
* overwrite : do nothing and pass if an identical file exists at destination
|
2011-12-20 03:52:06 +00:00
|
|
|
* overwriteSame : override any existing file at destination
|
|
|
|
|
*/
|
|
|
|
|
class CopyFileOp extends FileOp {
|
2012-01-24 18:49:53 +00:00
|
|
|
protected function allowedParams() {
|
|
|
|
|
return array( array( 'src', 'dst' ), array( 'overwrite', 'overwriteSame' ) );
|
|
|
|
|
}
|
2011-12-20 03:52:06 +00:00
|
|
|
|
|
|
|
|
protected function doPrecheck( array &$predicates ) {
|
|
|
|
|
$status = Status::newGood();
|
|
|
|
|
// Check if the source file exists
|
|
|
|
|
if ( !$this->fileExists( $this->params['src'], $predicates ) ) {
|
|
|
|
|
$status->fatal( 'backend-fail-notexists', $this->params['src'] );
|
|
|
|
|
return $status;
|
2012-01-19 23:18:03 +00:00
|
|
|
// Check if a file can be placed at the destination
|
|
|
|
|
} elseif ( !$this->backend->isPathUsableInternal( $this->params['dst'] ) ) {
|
|
|
|
|
$status->fatal( 'backend-fail-copy', $this->params['src'], $this->params['dst'] );
|
|
|
|
|
return $status;
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
2012-01-15 22:45:14 +00:00
|
|
|
// Check if destination file exists
|
|
|
|
|
$status->merge( $this->precheckDestExistence( $predicates ) );
|
2012-01-19 23:18:03 +00:00
|
|
|
if ( $status->isOK() ) {
|
|
|
|
|
// Update file existence predicates
|
|
|
|
|
$predicates['exists'][$this->params['dst']] = true;
|
|
|
|
|
$predicates['sha1'][$this->params['dst']] = $this->sourceSha1;
|
2012-01-15 22:45:14 +00:00
|
|
|
}
|
|
|
|
|
return $status; // safe to call attempt()
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function doAttempt() {
|
|
|
|
|
$status = Status::newGood();
|
2012-01-15 22:45:14 +00:00
|
|
|
// Do nothing if the src/dst paths are the same
|
|
|
|
|
if ( $this->params['src'] !== $this->params['dst'] ) {
|
|
|
|
|
// Copy the file into the destination
|
|
|
|
|
if ( !$this->destSameAsSource ) {
|
|
|
|
|
$status->merge( $this->backend->copyInternal( $this->params ) );
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function storagePathsRead() {
|
|
|
|
|
return array( $this->params['src'] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function storagePathsChanged() {
|
|
|
|
|
return array( $this->params['dst'] );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Move a file from one storage path to another in the backend.
|
2012-01-29 22:22:28 +00:00
|
|
|
* Parameters similar to FileBackendStore::moveInternal(), which include:
|
2011-12-20 03:52:06 +00:00
|
|
|
* src : source storage path
|
|
|
|
|
* dst : destination storage path
|
2012-01-19 02:24:49 +00:00
|
|
|
* overwrite : do nothing and pass if an identical file exists at destination
|
2011-12-20 03:52:06 +00:00
|
|
|
* overwriteSame : override any existing file at destination
|
|
|
|
|
*/
|
|
|
|
|
class MoveFileOp extends FileOp {
|
2012-01-24 18:49:53 +00:00
|
|
|
protected function allowedParams() {
|
|
|
|
|
return array( array( 'src', 'dst' ), array( 'overwrite', 'overwriteSame' ) );
|
|
|
|
|
}
|
2011-12-20 03:52:06 +00:00
|
|
|
|
|
|
|
|
protected function doPrecheck( array &$predicates ) {
|
|
|
|
|
$status = Status::newGood();
|
|
|
|
|
// Check if the source file exists
|
|
|
|
|
if ( !$this->fileExists( $this->params['src'], $predicates ) ) {
|
|
|
|
|
$status->fatal( 'backend-fail-notexists', $this->params['src'] );
|
|
|
|
|
return $status;
|
2012-01-19 23:18:03 +00:00
|
|
|
// Check if a file can be placed at the destination
|
|
|
|
|
} elseif ( !$this->backend->isPathUsableInternal( $this->params['dst'] ) ) {
|
|
|
|
|
$status->fatal( 'backend-fail-move', $this->params['src'], $this->params['dst'] );
|
|
|
|
|
return $status;
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
2012-01-15 22:45:14 +00:00
|
|
|
// Check if destination file exists
|
|
|
|
|
$status->merge( $this->precheckDestExistence( $predicates ) );
|
2012-01-19 23:18:03 +00:00
|
|
|
if ( $status->isOK() ) {
|
|
|
|
|
// Update file existence predicates
|
|
|
|
|
$predicates['exists'][$this->params['src']] = false;
|
|
|
|
|
$predicates['sha1'][$this->params['src']] = false;
|
|
|
|
|
$predicates['exists'][$this->params['dst']] = true;
|
|
|
|
|
$predicates['sha1'][$this->params['dst']] = $this->sourceSha1;
|
2012-01-15 22:45:14 +00:00
|
|
|
}
|
|
|
|
|
return $status; // safe to call attempt()
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function doAttempt() {
|
|
|
|
|
$status = Status::newGood();
|
2012-01-15 22:45:14 +00:00
|
|
|
// Do nothing if the src/dst paths are the same
|
|
|
|
|
if ( $this->params['src'] !== $this->params['dst'] ) {
|
|
|
|
|
if ( !$this->destSameAsSource ) {
|
|
|
|
|
// Move the file into the destination
|
|
|
|
|
$status->merge( $this->backend->moveInternal( $this->params ) );
|
|
|
|
|
} else {
|
|
|
|
|
// Just delete source as the destination needs no changes
|
|
|
|
|
$params = array( 'src' => $this->params['src'] );
|
|
|
|
|
$status->merge( $this->backend->deleteInternal( $params ) );
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function storagePathsRead() {
|
|
|
|
|
return array( $this->params['src'] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function storagePathsChanged() {
|
2012-04-03 22:45:12 +00:00
|
|
|
return array( $this->params['src'], $this->params['dst'] );
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-01-29 19:23:26 +00:00
|
|
|
* Delete a file at the given storage path from the backend.
|
2012-01-29 22:22:28 +00:00
|
|
|
* Parameters similar to FileBackendStore::deleteInternal(), which include:
|
2011-12-20 03:52:06 +00:00
|
|
|
* src : source storage path
|
|
|
|
|
* ignoreMissingSource : don't return an error if the file does not exist
|
|
|
|
|
*/
|
|
|
|
|
class DeleteFileOp extends FileOp {
|
2012-01-24 18:49:53 +00:00
|
|
|
protected function allowedParams() {
|
|
|
|
|
return array( array( 'src' ), array( 'ignoreMissingSource' ) );
|
|
|
|
|
}
|
2011-12-20 03:52:06 +00:00
|
|
|
|
2012-01-24 05:54:47 +00:00
|
|
|
protected $needsDelete = true;
|
2011-12-20 03:52:06 +00:00
|
|
|
|
|
|
|
|
protected function doPrecheck( array &$predicates ) {
|
|
|
|
|
$status = Status::newGood();
|
|
|
|
|
// Check if the source file exists
|
|
|
|
|
if ( !$this->fileExists( $this->params['src'], $predicates ) ) {
|
|
|
|
|
if ( !$this->getParam( 'ignoreMissingSource' ) ) {
|
|
|
|
|
$status->fatal( 'backend-fail-notexists', $this->params['src'] );
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
$this->needsDelete = false;
|
|
|
|
|
}
|
|
|
|
|
// Update file existence predicates
|
|
|
|
|
$predicates['exists'][$this->params['src']] = false;
|
2012-01-15 22:45:14 +00:00
|
|
|
$predicates['sha1'][$this->params['src']] = false;
|
|
|
|
|
return $status; // safe to call attempt()
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function doAttempt() {
|
|
|
|
|
$status = Status::newGood();
|
|
|
|
|
if ( $this->needsDelete ) {
|
|
|
|
|
// Delete the source file
|
2011-12-21 09:16:28 +00:00
|
|
|
$status->merge( $this->backend->deleteInternal( $this->params ) );
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function storagePathsChanged() {
|
|
|
|
|
return array( $this->params['src'] );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Placeholder operation that has no params and does nothing
|
|
|
|
|
*/
|
2012-01-29 19:23:26 +00:00
|
|
|
class NullFileOp extends FileOp {}
|