9 * Helper class for representing operations with transaction support.
10 * FileBackend::doOperations() will require these classes for supported operations.
11 * Do not use this class from places outside FileBackend.
13 * Use of large fields should be avoided as we want to support
14 * potentially many FileOp classes in large arrays in memory.
16 * @ingroup FileBackend
19 abstract class FileOp
{
21 protected $params = array();
22 /** @var FileBackendBase */
25 protected $state = self
::STATE_NEW
; // integer
26 protected $failed = false; // boolean
27 protected $useLatest = true; // boolean
29 protected $sourceSha1; // string
30 protected $destSameAsSource; // boolean
32 /* Object life-cycle */
34 const STATE_CHECKED
= 2;
35 const STATE_ATTEMPTED
= 3;
37 /* Timeout related parameters */
38 const MAX_BATCH_SIZE
= 1000;
39 const TIME_LIMIT_SEC
= 300; // 5 minutes
42 * Build a new file operation transaction
44 * @params $backend FileBackend
45 * @params $params Array
47 final public function __construct( FileBackendBase
$backend, array $params ) {
48 $this->backend
= $backend;
49 foreach ( $this->allowedParams() as $name ) {
50 if ( isset( $params[$name] ) ) {
51 $this->params
[$name] = $params[$name];
54 $this->params
= $params;
58 * Allow stale data for file reads and existence checks
62 final protected function allowStaleReads() {
63 $this->useLatest
= false;
67 * Attempt a series of file operations.
68 * Callers are responsible for handling file locking.
70 * $opts is an array of options, including:
71 * 'force' : Errors that would normally cause a rollback do not.
72 * The remaining operations are still attempted if any fail.
73 * 'allowStale' : Don't require the latest available data.
74 * This can increase performance for non-critical writes.
75 * This has no effect unless the 'force' flag is set.
77 * @param $performOps Array List of FileOp operations
78 * @param $opts Array Batch operation options
81 final public static function attemptBatch( array $performOps, array $opts ) {
82 $status = Status
::newGood();
84 $allowStale = !empty( $opts['allowStale'] );
85 $ignoreErrors = !empty( $opts['force'] );
87 $n = count( $performOps );
88 if ( $n > self
::MAX_BATCH_SIZE
) {
89 $status->fatal( 'backend-fail-batchsize', $n, self
::MAX_BATCH_SIZE
);
93 $predicates = FileOp
::newPredicates(); // account for previous op in prechecks
94 // Do pre-checks for each operation; abort on failure...
95 foreach ( $performOps as $index => $fileOp ) {
97 $fileOp->allowStaleReads(); // allow potentially stale reads
99 $subStatus = $fileOp->precheck( $predicates );
100 $status->merge( $subStatus );
101 if ( !$subStatus->isOK() ) { // operation failed?
102 $status->success
[$index] = false;
103 ++
$status->failCount
;
104 if ( !$ignoreErrors ) {
105 return $status; // abort
110 // Restart PHP's execution timer and set the timeout to safe amount.
111 // This handles cases where the operations take a long time or where we are
112 // already running low on time left. The old timeout is restored afterwards.
113 $scopedTimeLimit = new FileOpScopedPHPTimeout( self
::TIME_LIMIT_SEC
);
115 // Attempt each operation...
116 foreach ( $performOps as $index => $fileOp ) {
117 if ( $fileOp->failed() ) {
118 continue; // nothing to do
120 $subStatus = $fileOp->attempt();
121 $status->merge( $subStatus );
122 if ( $subStatus->isOK() ) {
123 $status->success
[$index] = true;
124 ++
$status->successCount
;
126 $status->success
[$index] = false;
127 ++
$status->failCount
;
128 if ( !$ignoreErrors ) {
129 // Log remaining ops as failed for recovery...
130 for ( $i = ($index +
1); $i < count( $performOps ); $i++
) {
131 $performOps[$i]->logFailure( 'attempt_aborted' );
133 return $status; // bail out
142 * Get the value of the parameter with the given name
144 * @param $name string
145 * @return mixed Returns null if the parameter is not set
147 final public function getParam( $name ) {
148 return isset( $this->params
[$name] ) ?
$this->params
[$name] : null;
152 * Check if this operation failed precheck() or attempt()
156 final public function failed() {
157 return $this->failed
;
161 * Get a new empty predicates array for precheck()
165 final public static function newPredicates() {
166 return array( 'exists' => array(), 'sha1' => array() );
170 * Check preconditions of the operation without writing anything
172 * @param $predicates Array
175 final public function precheck( array &$predicates ) {
176 if ( $this->state
!== self
::STATE_NEW
) {
177 return Status
::newFatal( 'fileop-fail-state', self
::STATE_NEW
, $this->state
);
179 $this->state
= self
::STATE_CHECKED
;
180 $status = $this->doPrecheck( $predicates );
181 if ( !$status->isOK() ) {
182 $this->failed
= true;
188 * Attempt the operation, backing up files as needed; this must be reversible
192 final public function attempt() {
193 if ( $this->state
!== self
::STATE_CHECKED
) {
194 return Status
::newFatal( 'fileop-fail-state', self
::STATE_CHECKED
, $this->state
);
195 } elseif ( $this->failed
) { // failed precheck
196 return Status
::newFatal( 'fileop-fail-attempt-precheck' );
198 $this->state
= self
::STATE_ATTEMPTED
;
199 $status = $this->doAttempt();
200 if ( !$status->isOK() ) {
201 $this->failed
= true;
202 $this->logFailure( 'attempt' );
208 * Get a list of storage paths read from for this operation
212 public function storagePathsRead() {
217 * Get a list of storage paths written to for this operation
221 public function storagePathsChanged() {
226 * @return Array List of allowed parameters
228 protected function allowedParams() {
235 protected function doPrecheck( array &$predicates ) {
236 return Status
::newGood();
242 abstract protected function doAttempt();
245 * Check for errors with regards to the destination file already existing.
246 * This also updates the destSameAsSource and sourceSha1 member variables.
247 * A bad status will be returned if there is no chance it can be overwritten.
249 * @param $predicates Array
252 protected function precheckDestExistence( array $predicates ) {
253 $status = Status
::newGood();
254 // Get hash of source file/string and the destination file
255 $this->sourceSha1
= $this->getSourceSha1Base36(); // FS file or data string
256 if ( $this->sourceSha1
=== null ) { // file in storage?
257 $this->sourceSha1
= $this->fileSha1( $this->params
['src'], $predicates );
259 $this->destSameAsSource
= false;
260 if ( $this->fileExists( $this->params
['dst'], $predicates ) ) {
261 if ( $this->getParam( 'overwrite' ) ) {
262 return $status; // OK
263 } elseif ( $this->getParam( 'overwriteSame' ) ) {
264 $dhash = $this->fileSha1( $this->params
['dst'], $predicates );
265 // Check if hashes are valid and match each other...
266 if ( !strlen( $this->sourceSha1
) ||
!strlen( $dhash ) ) {
267 $status->fatal( 'backend-fail-hashes' );
268 } elseif ( $this->sourceSha1
!== $dhash ) {
269 // Give an error if the files are not identical
270 $status->fatal( 'backend-fail-notsame', $this->params
['dst'] );
272 $this->destSameAsSource
= true; // OK
274 return $status; // do nothing; either OK or bad status
276 $status->fatal( 'backend-fail-alreadyexists', $this->params
['dst'] );
284 * precheckDestExistence() helper function to get the source file SHA-1.
285 * Subclasses should overwride this iff the source is not in storage.
287 * @return string|false Returns false on failure
289 protected function getSourceSha1Base36() {
294 * Check if a file will exist in storage when this operation is attempted
296 * @param $source string Storage path
297 * @param $predicates Array
300 final protected function fileExists( $source, array $predicates ) {
301 if ( isset( $predicates['exists'][$source] ) ) {
302 return $predicates['exists'][$source]; // previous op assures this
304 $params = array( 'src' => $source, 'latest' => $this->useLatest
);
305 return $this->backend
->fileExists( $params );
310 * Get the SHA-1 of a file in storage when this operation is attempted
312 * @param $source string Storage path
313 * @param $predicates Array
314 * @return string|false
316 final protected function fileSha1( $source, array $predicates ) {
317 if ( isset( $predicates['sha1'][$source] ) ) {
318 return $predicates['sha1'][$source]; // previous op assures this
320 $params = array( 'src' => $source, 'latest' => $this->useLatest
);
321 return $this->backend
->getFileSha1Base36( $params );
326 * Log a file operation failure and preserve any temp files
328 * @param $action string
331 final protected function logFailure( $action ) {
332 $params = $this->params
;
333 $params['failedAction'] = $action;
335 wfDebugLog( 'FileOperation',
336 get_class( $this ) . ' failed:' . serialize( $params ) );
337 } catch ( Exception
$e ) {
338 // bad config? debug log error?
344 * FileOp helper class to expand PHP execution time for a function.
345 * On construction, set_time_limit() is called and set to $seconds.
346 * When the object goes out of scope, the timer is restarted, with
347 * the original time limit minus the time the object existed.
349 class FileOpScopedPHPTimeout
{
350 protected $startTime; // integer seconds
351 protected $oldTimeout; // integer seconds
354 * @param $seconds integer
356 public function __construct( $seconds ) {
357 if ( ini_get( 'max_execution_time' ) > 0 ) { // CLI uses 0
358 $this->oldTimeout
= ini_set( 'max_execution_time', $seconds );
360 $this->startTime
= time();
364 * Restore the original timeout.
365 * This does not account for the timer value on __construct().
367 public function __destruct() {
368 if ( $this->oldTimeout
) {
369 $elapsed = time() - $this->startTime
;
370 // Note: a limit of 0 is treated as "forever"
371 set_time_limit( max( 1, $this->oldTimeout
- $elapsed ) );
377 * Store a file into the backend from a file on the file system.
378 * Parameters similar to FileBackend::storeInternal(), which include:
379 * src : source path on file system
380 * dst : destination storage path
381 * overwrite : do nothing and pass if an identical file exists at destination
382 * overwriteSame : override any existing file at destination
384 class StoreFileOp
extends FileOp
{
385 protected function allowedParams() {
386 return array( 'src', 'dst', 'overwrite', 'overwriteSame' );
389 protected function doPrecheck( array &$predicates ) {
390 $status = Status
::newGood();
391 // Check if the source file exists on the file system
392 if ( !is_file( $this->params
['src'] ) ) {
393 $status->fatal( 'backend-fail-notexists', $this->params
['src'] );
395 // Check if the source file is too big
396 } elseif ( filesize( $this->params
['src'] ) > $this->backend
->maxFileSizeInternal() ) {
397 $status->fatal( 'backend-fail-store', $this->params
['src'], $this->params
['dst'] );
399 // Check if a file can be placed at the destination
400 } elseif ( !$this->backend
->isPathUsableInternal( $this->params
['dst'] ) ) {
401 $status->fatal( 'backend-fail-store', $this->params
['src'], $this->params
['dst'] );
404 // Check if destination file exists
405 $status->merge( $this->precheckDestExistence( $predicates ) );
406 if ( $status->isOK() ) {
407 // Update file existence predicates
408 $predicates['exists'][$this->params
['dst']] = true;
409 $predicates['sha1'][$this->params
['dst']] = $this->sourceSha1
;
411 return $status; // safe to call attempt()
414 protected function doAttempt() {
415 $status = Status
::newGood();
416 // Store the file at the destination
417 if ( !$this->destSameAsSource
) {
418 $status->merge( $this->backend
->storeInternal( $this->params
) );
423 protected function getSourceSha1Base36() {
424 wfSuppressWarnings();
425 $hash = sha1_file( $this->params
['src'] );
427 if ( $hash !== false ) {
428 $hash = wfBaseConvert( $hash, 16, 36, 31 );
433 public function storagePathsChanged() {
434 return array( $this->params
['dst'] );
439 * Create a file in the backend with the given content.
440 * Parameters similar to FileBackend::createInternal(), which include:
441 * content : a string of raw file content. Let unset to create an empty file.
442 * dst : destination storage path
443 * overwrite : do nothing and pass if an identical file exists at destination
444 * overwriteSame : override any existing file at destination
446 class CreateFileOp
extends FileOp
{
447 protected function allowedParams() {
448 return array( 'content', 'dst', 'overwrite', 'overwriteSame' );
451 protected function doPrecheck( array &$predicates ) {
452 $status = Status
::newGood();
453 // Check if the source data is too big
454 if ( strlen( $this->getParam( 'content' ) ) > $this->backend
->maxFileSizeInternal() ) {
455 $status->fatal( 'backend-fail-create', $this->params
['dst'] );
457 // Check if a file can be placed at the destination
458 } elseif ( !$this->backend
->isPathUsableInternal( $this->params
['dst'] ) ) {
459 $status->fatal( 'backend-fail-create', $this->params
['dst'] );
462 // Check if destination file exists
463 $status->merge( $this->precheckDestExistence( $predicates ) );
464 if ( $status->isOK() ) {
465 // Update file existence predicates
466 $predicates['exists'][$this->params
['dst']] = true;
467 $predicates['sha1'][$this->params
['dst']] = $this->sourceSha1
;
469 return $status; // safe to call attempt()
472 protected function doAttempt() {
473 $status = Status
::newGood();
474 // Create the file at the destination
475 if ( !$this->destSameAsSource
) {
476 $status->merge( $this->backend
->createInternal( $this->params
) );
481 protected function getSourceSha1Base36() {
482 return wfBaseConvert( sha1( $this->params
['content'] ), 16, 36, 31 );
485 public function storagePathsChanged() {
486 return array( $this->params
['dst'] );
491 * Copy a file from one storage path to another in the backend.
492 * Parameters similar to FileBackend::copyInternal(), which include:
493 * src : source storage path
494 * dst : destination storage path
495 * overwrite : do nothing and pass if an identical file exists at destination
496 * overwriteSame : override any existing file at destination
498 class CopyFileOp
extends FileOp
{
499 protected function allowedParams() {
500 return array( 'src', 'dst', 'overwrite', 'overwriteSame' );
503 protected function doPrecheck( array &$predicates ) {
504 $status = Status
::newGood();
505 // Check if the source file exists
506 if ( !$this->fileExists( $this->params
['src'], $predicates ) ) {
507 $status->fatal( 'backend-fail-notexists', $this->params
['src'] );
509 // Check if a file can be placed at the destination
510 } elseif ( !$this->backend
->isPathUsableInternal( $this->params
['dst'] ) ) {
511 $status->fatal( 'backend-fail-copy', $this->params
['src'], $this->params
['dst'] );
514 // Check if destination file exists
515 $status->merge( $this->precheckDestExistence( $predicates ) );
516 if ( $status->isOK() ) {
517 // Update file existence predicates
518 $predicates['exists'][$this->params
['dst']] = true;
519 $predicates['sha1'][$this->params
['dst']] = $this->sourceSha1
;
521 return $status; // safe to call attempt()
524 protected function doAttempt() {
525 $status = Status
::newGood();
526 // Do nothing if the src/dst paths are the same
527 if ( $this->params
['src'] !== $this->params
['dst'] ) {
528 // Copy the file into the destination
529 if ( !$this->destSameAsSource
) {
530 $status->merge( $this->backend
->copyInternal( $this->params
) );
536 public function storagePathsRead() {
537 return array( $this->params
['src'] );
540 public function storagePathsChanged() {
541 return array( $this->params
['dst'] );
546 * Move a file from one storage path to another in the backend.
547 * Parameters similar to FileBackend::moveInternal(), which include:
548 * src : source storage path
549 * dst : destination storage path
550 * overwrite : do nothing and pass if an identical file exists at destination
551 * overwriteSame : override any existing file at destination
553 class MoveFileOp
extends FileOp
{
554 protected function allowedParams() {
555 return array( 'src', 'dst', 'overwrite', 'overwriteSame' );
558 protected function doPrecheck( array &$predicates ) {
559 $status = Status
::newGood();
560 // Check if the source file exists
561 if ( !$this->fileExists( $this->params
['src'], $predicates ) ) {
562 $status->fatal( 'backend-fail-notexists', $this->params
['src'] );
564 // Check if a file can be placed at the destination
565 } elseif ( !$this->backend
->isPathUsableInternal( $this->params
['dst'] ) ) {
566 $status->fatal( 'backend-fail-move', $this->params
['src'], $this->params
['dst'] );
569 // Check if destination file exists
570 $status->merge( $this->precheckDestExistence( $predicates ) );
571 if ( $status->isOK() ) {
572 // Update file existence predicates
573 $predicates['exists'][$this->params
['src']] = false;
574 $predicates['sha1'][$this->params
['src']] = false;
575 $predicates['exists'][$this->params
['dst']] = true;
576 $predicates['sha1'][$this->params
['dst']] = $this->sourceSha1
;
578 return $status; // safe to call attempt()
581 protected function doAttempt() {
582 $status = Status
::newGood();
583 // Do nothing if the src/dst paths are the same
584 if ( $this->params
['src'] !== $this->params
['dst'] ) {
585 if ( !$this->destSameAsSource
) {
586 // Move the file into the destination
587 $status->merge( $this->backend
->moveInternal( $this->params
) );
589 // Just delete source as the destination needs no changes
590 $params = array( 'src' => $this->params
['src'] );
591 $status->merge( $this->backend
->deleteInternal( $params ) );
597 public function storagePathsRead() {
598 return array( $this->params
['src'] );
601 public function storagePathsChanged() {
602 return array( $this->params
['dst'] );
607 * Delete a file at the storage path.
608 * Parameters similar to FileBackend::deleteInternal(), which include:
609 * src : source storage path
610 * ignoreMissingSource : don't return an error if the file does not exist
612 class DeleteFileOp
extends FileOp
{
613 protected $needsDelete = true;
615 protected function allowedParams() {
616 return array( 'src', 'ignoreMissingSource' );
619 protected function doPrecheck( array &$predicates ) {
620 $status = Status
::newGood();
621 // Check if the source file exists
622 if ( !$this->fileExists( $this->params
['src'], $predicates ) ) {
623 if ( !$this->getParam( 'ignoreMissingSource' ) ) {
624 $status->fatal( 'backend-fail-notexists', $this->params
['src'] );
627 $this->needsDelete
= false;
629 // Update file existence predicates
630 $predicates['exists'][$this->params
['src']] = false;
631 $predicates['sha1'][$this->params
['src']] = false;
632 return $status; // safe to call attempt()
635 protected function doAttempt() {
636 $status = Status
::newGood();
637 if ( $this->needsDelete
) {
638 // Delete the source file
639 $status->merge( $this->backend
->deleteInternal( $this->params
) );
644 public function storagePathsChanged() {
645 return array( $this->params
['src'] );
650 * Placeholder operation that has no params and does nothing
652 class NullFileOp
extends FileOp
{
653 protected function doAttempt() {
654 return Status
::newGood();