25use Psr\Log\LoggerInterface;
27use Wikimedia\RequestTimeout\TimeoutException;
64 private const STATE_NEW = 1;
66 private const STATE_CHECKED = 2;
68 private const STATE_ATTEMPTED = 3;
84 foreach ( $required as $name ) {
85 if ( isset(
$params[$name] ) ) {
86 $this->params[$name] =
$params[$name];
88 throw new InvalidArgumentException(
"File operation missing parameter '$name'." );
91 foreach ( $optional as $name ) {
92 if ( isset(
$params[$name] ) ) {
93 $this->params[$name] =
$params[$name];
96 foreach ( $paths as $name ) {
97 if ( isset( $this->params[$name] ) ) {
111 if ( FileBackend::isStoragePath(
$path ) ) {
112 $res = FileBackend::normalizeStoragePath(
$path );
114 return $res ??
$path;
127 return $this->params[$name] ??
null;
145 return [
'read' => [],
'write' => [] ];
169 if ( isset( $deps[
'read'][
$path] ) || isset( $deps[
'write'][
$path] ) ) {
174 if ( isset( $deps[
'write'][
$path] ) ) {
191 if ( $this->state !== self::STATE_NEW ) {
192 return StatusValue::newFatal(
'fileop-fail-state', self::STATE_NEW, $this->state );
194 $this->state = self::STATE_CHECKED;
196 $status = StatusValue::newGood();
198 if ( !$this->backend->isPathUsableInternal(
$path ) ) {
199 $status->fatal(
'backend-fail-usable',
$path );
202 if ( !$status->isOK() ) {
207 $status = $this->
doPrecheck( $opPredicates, $predicates );
208 if ( !$status->isOK() ) {
228 return StatusValue::newGood();
237 if ( $this->state !== self::STATE_CHECKED ) {
238 return StatusValue::newFatal(
'fileop-fail-state', self::STATE_CHECKED, $this->state );
239 } elseif ( $this->
failed ) {
240 return StatusValue::newFatal(
'fileop-fail-attempt-precheck' );
242 $this->state = self::STATE_ATTEMPTED;
244 $status = StatusValue::newGood();
247 if ( !$status->isOK() ) {
260 return StatusValue::newGood();
271 $this->async =
false;
282 $this->state = self::STATE_CHECKED;
293 $this->state = self::STATE_CHECKED;
304 return [ [], [], [] ];
341 return array_values( array_unique(
362 $status = StatusValue::newGood();
366 $this->overwriteSameCase =
false;
367 if ( $this->destExists ) {
368 if ( $this->
getParam(
'overwrite' ) ) {
370 } elseif ( $this->
getParam(
'overwriteSame' ) ) {
372 $sourceSize = ( $sourceSize instanceof Closure ) ? $sourceSize() : $sourceSize;
373 $sourceSha1 = ( $sourceSha1 instanceof Closure ) ? $sourceSha1() : $sourceSha1;
375 $dstSize = $this->
resolveFileSize( $this->params[
'dst'], $opPredicates );
377 if ( !strlen( $sourceSha1 ) || !strlen( $dstSha1 ) ) {
378 $status->fatal(
'backend-fail-hashes' );
379 } elseif ( !is_int( $sourceSize ) || !is_int( $dstSize ) ) {
380 $status->fatal(
'backend-fail-sizes' );
381 } elseif ( $sourceSha1 !== $dstSha1 || $sourceSize !== $dstSize ) {
383 $status->fatal(
'backend-fail-notsame', $this->params[
'dst'] );
385 $this->overwriteSameCase =
true;
388 $status->fatal(
'backend-fail-alreadyexists', $this->params[
'dst'] );
390 } elseif ( $this->destExists === FileBackend::EXISTENCE_ERROR ) {
391 $status->fatal(
'backend-fail-stat', $this->params[
'dst'] );
411 return $this->backend->fileExists( [
'src' =>
$path,
'latest' =>
true ] );
431 return $this->backend->getFileSize( [
'src' =>
$path,
'latest' =>
true ] );
447 return $this->backend->getFileSha1Base36( [
'src' =>
$path,
'latest' =>
true ] );
468 $params[
'failedAction'] = $action;
470 $this->logger->error( static::class .
471 " failed: " . FormatJson::encode(
$params ) );
472 }
catch ( TimeoutException $e ) {
474 }
catch ( Exception $e ) {
Base class for all backends using particular storage medium.
FileBackend helper class for representing operations.
bool null $overwriteSameCase
static normalizeIfValidStoragePath( $path)
Normalize a string if it is a valid storage path.
static newDependencies()
Get a new empty dependency tracking array for paths read/written to.
resolveFileExistence( $source, FileStatePredicates $opPredicates)
Check if a file will exist in storage when this operation is attempted.
precheckDestExistence(FileStatePredicates $opPredicates, $sourceSize, $sourceSha1)
Check for errors with regards to the destination file already existing.
allowedParams()
Get the file operation parameters.
attemptAsync()
Attempt the operation in the background.
getParam( $name)
Get the value of the parameter with the given name.
storagePathsReadOrChanged()
Get a list of storage paths read from or written to for this operation.
storagePathsChanged()
Get a list of storage paths written to for this operation.
int $state
Stage in the operation life-cycle.
applyDependencies(array $deps)
Update a dependency tracking array to account for this operation.
attempt()
Attempt the operation.
setFlags(array $params)
Adjust params to FileBackendStore internal file calls.
bool $async
Whether the operation is part of a concurrent sub-batch of operation.
doPrecheck(FileStatePredicates $opPredicates, FileStatePredicates $batchPredicates)
Do a dry-run precondition check of the operation in the context of op batch.
logFailure( $action)
Log a file operation failure and preserve any temp files.
storagePathsRead()
Get a list of storage paths read from for this operation.
attemptQuick()
Attempt the operation without regards to prechecks.
resolveFileSha1Base36( $source, FileStatePredicates $opPredicates)
Get the SHA-1 of a file in storage when this operation is attempted.
FileBackendStore $backend
dependsOn(array $deps)
Check if this operation changes files listed in $paths.
getBackend()
Get the backend this operation is for.
attemptAsyncQuick()
Attempt the operation in the background without regards to prechecks.
failed()
Check if this operation failed precheck() or attempt()
resolveFileSize( $source, FileStatePredicates $opPredicates)
Get the size a file in storage will have when this operation is attempted.
__construct(FileBackendStore $backend, array $params, LoggerInterface $logger)
Build a new batch file operation transaction.
bool $noOp
Whether the operation pre-check stage marked the attempt stage as a no-op.
bool $failed
Whether the operation pre-check or attempt stage failed.
precheck(FileStatePredicates $predicates)
Do a dry-run precondition check of the operation in the context of op batch.
Helper class for tracking counterfactual file states when pre-checking file operation batches.
resolveFileSha1Base36(string $path, $curSha1Func)
Get the hypothetical SHA-1 hash of a file given predicated and current state of files.
resolveFileSize(string $path, $curSizeFunc)
Get the hypothetical size of a file given predicated and current state of files.
resolveFileExistence(string $path, $curExistenceFunc)
Get the hypothetical existance a file given predicated and current state of files.