12use InvalidArgumentException;
13use Shellbox\Command\BoxedCommand;
33use Wikimedia\Timestamp\ConvertibleTimestamp;
34use Wikimedia\Timestamp\TimestampFormat as TS;
79 protected const RES_ABSENT =
false;
81 protected const RES_ERROR =
null;
84 protected const ABSENT_NORMAL =
'FNE-N';
86 protected const ABSENT_LATEST =
'FNE-L';
102 parent::__construct( $config );
103 $this->mimeCallback = $config[
'mimeCallback'] ??
null;
105 $this->wanCache = $config[
'wanCache'] ?? WANObjectCache::newEmpty();
106 $this->wanStatCache = WANObjectCache::newEmpty();
108 $this->cheapCache =
new MapCacheLRU( self::CACHE_CHEAP_SIZE );
109 $this->expensiveCache =
new MapCacheLRU( self::CACHE_EXPENSIVE_SIZE );
120 return min( $this->maxFileSize, PHP_INT_MAX );
156 $status = $this->
newStatus(
'backend-fail-maxsize',
161 if ( $params[
'dstExists'] ??
true ) {
196 $status = $this->
newStatus(
'backend-fail-maxsize',
201 if ( $params[
'dstExists'] ??
true ) {
238 if ( $params[
'dstExists'] ??
true ) {
301 $this->
clearCache( [ $params[
'src'], $params[
'dst'] ] );
303 if ( $params[
'dstExists'] ??
true ) {
332 if ( count( $params[
'headers'] ) ) {
369 $scopeLockS = $this->
getScopedFileLocks( $params[
'srcs'], LockManager::LOCK_UW, $status );
370 if ( $status->isOK() ) {
372 $hrStart = hrtime(
true );
374 $sec = ( hrtime(
true ) - $hrStart ) / 1e9;
375 if ( !$status->isOK() ) {
376 $this->logger->error( static::class .
"-{$this->name}" .
377 " failed to concatenate " . count( $params[
'srcs'] ) .
" file(s) [$sec sec]" );
392 $tmpPath = $params[
'dst'];
393 unset( $params[
'latest'] );
397 $ok = ( @is_file( $tmpPath ) && @filesize( $tmpPath ) == 0 );
399 $status->fatal(
'backend-fail-opentemp', $tmpPath );
406 foreach ( $fsFiles as
$path => &$fsFile ) {
411 $fsFile === self::RES_ERROR ?
'backend-fail-read' :
'backend-fail-notexists',
422 $tmpHandle = fopen( $tmpPath,
'ab' );
423 if ( $tmpHandle ===
false ) {
424 $status->fatal(
'backend-fail-opentemp', $tmpPath );
430 foreach ( $fsFiles as $virtualSource => $fsFile ) {
432 $sourceHandle = fopen( $fsFile->getPath(),
'rb' );
433 if ( $sourceHandle ===
false ) {
434 fclose( $tmpHandle );
435 $status->fatal(
'backend-fail-read', $virtualSource );
440 if ( !stream_copy_to_stream( $sourceHandle, $tmpHandle ) ) {
441 fclose( $sourceHandle );
442 fclose( $tmpHandle );
443 $status->fatal(
'backend-fail-writetemp', $tmpPath );
447 fclose( $sourceHandle );
449 if ( !fclose( $tmpHandle ) ) {
450 $status->fatal(
'backend-fail-closetemp', $tmpPath );
467 if ( $dir ===
null ) {
468 $status->fatal(
'backend-fail-invalidpath', $params[
'dir'] );
473 if ( $shard !==
null ) {
476 $this->logger->debug( __METHOD__ .
": iterating over all container shards." );
479 $status->merge( $this->
doPrepareInternal(
"{$fullCont}{$suffix}", $dir, $params ) );
499 final protected function doSecure( array $params ) {
503 if ( $dir ===
null ) {
504 $status->fatal(
'backend-fail-invalidpath', $params[
'dir'] );
509 if ( $shard !==
null ) {
512 $this->logger->debug( __METHOD__ .
": iterating over all container shards." );
515 $status->merge( $this->
doSecureInternal(
"{$fullCont}{$suffix}", $dir, $params ) );
539 if ( $dir ===
null ) {
540 $status->fatal(
'backend-fail-invalidpath', $params[
'dir'] );
545 if ( $shard !==
null ) {
548 $this->logger->debug( __METHOD__ .
": iterating over all container shards." );
551 $status->merge( $this->
doPublishInternal(
"{$fullCont}{$suffix}", $dir, $params ) );
571 final protected function doClean( array $params ) {
577 if ( $subDirsRel !==
null ) {
578 foreach ( $subDirsRel as $subDirRel ) {
579 $subDir = $params[
'dir'] .
"/{$subDirRel}";
580 $status->merge( $this->
doClean( [
'dir' => $subDir ] + $params ) );
582 unset( $subDirsRel );
587 if ( $dir ===
null ) {
588 $status->fatal(
'backend-fail-invalidpath', $params[
'dir'] );
594 $filesLockEx = [ $params[
'dir'] ];
596 $scopedLockE = $this->
getScopedFileLocks( $filesLockEx, LockManager::LOCK_EX, $status );
597 if ( !$status->isOK() ) {
601 if ( $shard !==
null ) {
605 $this->logger->debug( __METHOD__ .
": iterating over all container shards." );
608 $status->merge( $this->
doCleanInternal(
"{$fullCont}{$suffix}", $dir, $params ) );
631 if ( is_array( $stat ) ) {
635 return $stat === self::RES_ABSENT ? false : self::EXISTENCE_ERROR;
641 if ( is_array( $stat ) ) {
642 return $stat[
'mtime'];
645 return self::TIMESTAMP_FAIL;
651 if ( is_array( $stat ) ) {
652 return $stat[
'size'];
655 return self::SIZE_FAIL;
661 if (
$path ===
null ) {
662 return self::STAT_ERROR;
667 $latest = !empty( $params[
'latest'] );
669 $requireSHA1 = !empty( $params[
'requireSHA1'] );
671 $stat = $this->cheapCache->getField(
$path,
'stat', self::CACHE_TTL );
680 ( $requireSHA1 && is_array( $stat ) && !isset( $stat[
'sha1'] ) )
684 $stat = $this->cheapCache->getField(
$path,
'stat', self::CACHE_TTL );
688 if ( is_array( $stat ) ) {
690 ( !$latest || !empty( $stat[
'latest'] ) ) &&
691 ( !$requireSHA1 || isset( $stat[
'sha1'] ) )
695 } elseif ( $stat === self::ABSENT_LATEST ) {
696 return self::STAT_ABSENT;
697 } elseif ( $stat === self::ABSENT_NORMAL ) {
699 return self::STAT_ABSENT;
707 if ( is_array( $stat ) ) {
711 return $stat === self::RES_ERROR ? self::STAT_ERROR : self::STAT_ABSENT;
724 foreach ( $stats as
$path => $stat ) {
725 if ( is_array( $stat ) ) {
727 $stat[
'latest'] ??= $latest;
729 $this->cheapCache->setField(
$path,
'stat', $stat );
730 if ( isset( $stat[
'sha1'] ) ) {
732 $this->cheapCache->setField(
735 [
'hash' => $stat[
'sha1'],
'latest' => $latest ]
738 if ( isset( $stat[
'xattr'] ) ) {
741 $this->cheapCache->setField(
744 [
'map' => $stat[
'xattr'],
'latest' => $latest ]
749 } elseif ( $stat === self::RES_ABSENT ) {
750 $this->cheapCache->setField(
753 $latest ? self::ABSENT_LATEST : self::ABSENT_NORMAL
755 $this->cheapCache->setField(
758 [
'map' => self::XATTRS_FAIL,
'latest' => $latest ]
760 $this->cheapCache->setField(
763 [
'hash' => self::SHA1_FAIL,
'latest' => $latest ]
765 $this->logger->debug(
766 __METHOD__ .
': File {path} does not exist',
771 $this->logger->error(
772 __METHOD__ .
': Could not stat file {path}',
792 foreach ( $contents as
$path => $content ) {
793 if ( !is_string( $content ) ) {
794 $contents[
$path] = self::CONTENT_FAIL;
810 if ( $fsFile instanceof
FSFile ) {
812 $content = @file_get_contents( $fsFile->getPath() );
813 $contents[
$path] = is_string( $content ) ? $content : self::RES_ERROR;
816 $contents[
$path] = $fsFile;
826 if (
$path ===
null ) {
827 return self::XATTRS_FAIL;
829 $latest = !empty( $params[
'latest'] );
830 if ( $this->cheapCache->hasField(
$path,
'xattr', self::CACHE_TTL ) ) {
831 $stat = $this->cheapCache->getField(
$path,
'xattr' );
834 if ( !$latest || $stat[
'latest'] ) {
839 if ( is_array( $fields ) ) {
841 $this->cheapCache->setField(
844 [
'map' => $fields,
'latest' => $latest ]
846 } elseif ( $fields === self::RES_ABSENT ) {
847 $this->cheapCache->setField(
850 [
'map' => self::XATTRS_FAIL,
'latest' => $latest ]
853 $fields = self::XATTRS_FAIL;
866 return [
'headers' => [],
'metadata' => [] ];
872 if (
$path ===
null ) {
873 return self::SHA1_FAIL;
875 $latest = !empty( $params[
'latest'] );
876 if ( $this->cheapCache->hasField(
$path,
'sha1', self::CACHE_TTL ) ) {
877 $stat = $this->cheapCache->getField(
$path,
'sha1' );
880 if ( !$latest || $stat[
'latest'] ) {
881 return $stat[
'hash'];
885 if ( is_string( $sha1 ) ) {
886 $this->cheapCache->setField(
889 [
'hash' => $sha1,
'latest' => $latest ]
891 } elseif ( $sha1 === self::RES_ABSENT ) {
892 $this->cheapCache->setField(
895 [
'hash' => self::SHA1_FAIL,
'latest' => $latest ]
898 $sha1 = self::SHA1_FAIL;
912 if ( $fsFile instanceof
FSFile ) {
913 $sha1 = $fsFile->getSha1Base36();
915 return is_string( $sha1 ) ? $sha1 : self::RES_ERROR;
918 return $fsFile === self::RES_ERROR ? self::RES_ERROR : self::RES_ABSENT;
925 return $fsFile ? $fsFile->getProps() : FSFile::placeholderProps();
933 $latest = !empty( $params[
'latest'] );
935 foreach ( $params[
'srcs'] as $src ) {
937 if (
$path ===
null ) {
938 $fsFiles[$src] = self::RES_ERROR;
939 } elseif ( $this->expensiveCache->hasField(
$path,
'localRef' ) ) {
940 $val = $this->expensiveCache->getField(
$path,
'localRef' );
943 if ( !$latest || $val[
'latest'] ) {
944 $fsFiles[$src] = $val[
'object'];
949 $params[
'srcs'] = array_diff( $params[
'srcs'], array_keys( $fsFiles ) );
951 $fsFiles[
$path] = $fsFile;
952 if ( $fsFile instanceof
FSFile ) {
953 $this->expensiveCache->setField(
956 [
'object' => $fsFile,
'latest' => $latest ]
995 return self::TEMPURL_ERROR;
1003 if ( $ref ===
false ) {
1004 return $this->
newStatus(
'backend-fail-notexists', $params[
'src'] );
1005 } elseif ( $ref ===
null ) {
1006 return $this->
newStatus(
'backend-fail-read', $params[
'src'] );
1008 $file = $command->newInputFileFromFile( $ref->getPath() )
1009 ->userData( __CLASS__, $ref );
1010 $command->inputFile( $boxedName, $file );
1020 $params[
'options'] ??= [];
1021 $params[
'headers'] ??= [];
1024 if ( ( empty( $params[
'headless'] ) || $params[
'headers'] ) && headers_sent() ) {
1025 print
"Headers already sent, terminating.\n";
1026 $status->fatal(
'backend-fail-stream', $params[
'src'] );
1052 $this->getStreamerOptions()
1054 $res = $streamer->stream( $params[
'headers'],
true, $params[
'options'], $flags );
1061 $status->fatal(
'backend-fail-stream', $params[
'src'] );
1070 if ( $dir ===
null ) {
1071 return self::EXISTENCE_ERROR;
1073 if ( $shard !==
null ) {
1076 $this->logger->debug( __METHOD__ .
": iterating over all container shards." );
1081 if ( $exists ===
true ) {
1084 } elseif ( $exists === self::RES_ERROR ) {
1085 $res = self::EXISTENCE_ERROR;
1106 if ( $dir ===
null ) {
1107 return self::EXISTENCE_ERROR;
1109 if ( $shard !==
null ) {
1113 $this->logger->debug( __METHOD__ .
": iterating over all container shards." );
1137 if ( $dir ===
null ) {
1138 return self::LIST_ERROR;
1140 if ( $shard !==
null ) {
1144 $this->logger->debug( __METHOD__ .
": iterating over all container shards." );
1178 'store' => StoreFileOp::class,
1179 'copy' => CopyFileOp::class,
1180 'move' => MoveFileOp::class,
1181 'delete' => DeleteFileOp::class,
1182 'create' => CreateFileOp::class,
1183 'describe' => DescribeFileOp::class,
1184 'null' => NullFileOp::class
1189 foreach ( $ops as $operation ) {
1190 $opName = $operation[
'op'];
1191 if ( isset( $supportedOps[$opName] ) ) {
1192 $class = $supportedOps[$opName];
1194 $params = $operation;
1196 $performOps[] =
new $class( $this, $params, $this->logger );
1217 $paths = [
'sh' => [],
'ex' => [] ];
1218 foreach ( $performOps as $fileOp ) {
1219 $paths[
'sh'] = array_merge( $paths[
'sh'], $fileOp->storagePathsRead() );
1220 $paths[
'ex'] = array_merge( $paths[
'ex'], $fileOp->storagePathsChanged() );
1223 $paths[
'sh'] = array_diff( $paths[
'sh'], $paths[
'ex'] );
1225 $paths[
'sh'] = array_merge( $paths[
'sh'], array_map(
'dirname', $paths[
'ex'] ) );
1228 LockManager::LOCK_UW => $paths[
'sh'],
1229 LockManager::LOCK_EX => $paths[
'ex']
1249 foreach ( $fileOps as $fileOp ) {
1250 $pathsUsed = array_merge( $pathsUsed, $fileOp->storagePathsReadOrChanged() );
1254 if ( empty( $opts[
'nonLocking'] ) ) {
1258 if ( !$status->isOK() ) {
1264 if ( empty( $opts[
'preserveCache'] ) ) {
1269 $this->cheapCache->setMaxSize( max( 2 * count( $pathsUsed ), self::CACHE_CHEAP_SIZE ) );
1274 $ok = $this->
preloadFileStat( [
'srcs' => $pathsUsed,
'latest' =>
true ] );
1282 $subStatus = $this->
newStatus(
'backend-fail-internal', $this->name );
1283 foreach ( $ops as $i => $op ) {
1284 $subStatus->success[$i] =
false;
1285 ++$subStatus->failCount;
1287 $this->logger->error( static::class .
"-{$this->name} stat failure",
1288 [
'aborted_operations' => $ops ]
1293 $status->merge( $subStatus );
1294 $status->success = $subStatus->success;
1297 $this->cheapCache->setMaxSize( self::CACHE_CHEAP_SIZE );
1311 foreach ( $fileOps as $fileOp ) {
1312 $pathsUsed = array_merge( $pathsUsed, $fileOp->storagePathsReadOrChanged() );
1319 $async = ( $this->parallelize ===
'implicit' && count( $ops ) > 1 );
1325 foreach ( $fileOps as $index => $fileOp ) {
1327 ? $fileOp->attemptAsyncQuick()
1328 : $fileOp->attemptQuick();
1330 if ( count( $batch ) >= $maxConcurrency ) {
1336 $batch[$index] = $subStatus->value;
1338 $statuses[$index] = $subStatus;
1341 if ( count( $batch ) ) {
1345 foreach ( $statuses as $index => $subStatus ) {
1346 $status->merge( $subStatus );
1347 if ( $subStatus->isOK() ) {
1348 $status->success[$index] =
true;
1349 ++$status->successCount;
1351 $status->success[$index] =
false;
1352 ++$status->failCount;
1371 foreach ( $fileOpHandles as $fileOpHandle ) {
1373 throw new InvalidArgumentException(
"Expected FileBackendStoreOpHandle object." );
1374 } elseif ( $fileOpHandle->backend->getName() !== $this->getName() ) {
1375 throw new InvalidArgumentException(
"Expected handle for this file backend." );
1380 foreach ( $fileOpHandles as $fileOpHandle ) {
1381 $fileOpHandle->closeResources();
1397 if ( count( $fileOpHandles ) ) {
1398 throw new FileBackendError(
"Backend does not support asynchronous operations." );
1416 static $longs = [
'content-disposition' ];
1418 if ( isset( $op[
'headers'] ) ) {
1420 foreach ( $op[
'headers'] as
$name => $value ) {
1422 $maxHVLen = in_array(
$name, $longs ) ? INF : 255;
1423 if ( strlen(
$name ) > 255 || strlen( $value ) > $maxHVLen ) {
1424 $this->logger->error(
"Header '{header}' is too long.", [
1425 'filebackend' => $this->name,
1426 'header' =>
"$name: $value",
1429 $newHeaders[
$name] = strlen( $value ) ? $value :
'';
1432 $op[
'headers'] = $newHeaders;
1440 foreach ( $paths as
$path ) {
1442 $fullConts[] = $fullCont;
1450 if ( is_array( $paths ) ) {
1452 $paths = array_filter( $paths,
'strlen' );
1454 if ( $paths ===
null ) {
1455 $this->cheapCache->clear();
1456 $this->expensiveCache->clear();
1458 foreach ( $paths as
$path ) {
1459 $this->cheapCache->clear(
$path );
1460 $this->expensiveCache->clear(
$path );
1479 $params[
'concurrency'] = ( $this->parallelize !==
'off' ) ? $this->concurrency : 1;
1481 if ( $stats ===
null ) {
1486 $latest = !empty( $params[
'latest'] );
1550 return (
bool)preg_match(
'/^[a-z0-9][a-z0-9-_.]{0,199}$/i', $container );
1568 if ( $backend === $this->name && $relPath !==
null ) {
1570 if ( $relPath !==
null && self::isValidShortContainerName( $shortCont ) ) {
1575 if ( $relPath !==
null ) {
1578 if ( self::isValidContainerName( $container ) ) {
1581 if ( $container !==
null ) {
1582 return [ $container, $relPath, $cShard ];
1589 return [
null,
null, null ];
1609 if ( $cShard !==
null && !str_ends_with( $relPath,
'/' ) ) {
1610 return [ $container, $relPath ];
1613 return [
null, null ];
1626 if ( $levels == 1 || $levels == 2 ) {
1628 $char = ( $base == 36 ) ?
'[0-9a-z]' :
'[0-9a-f]';
1631 if ( $levels === 1 ) {
1632 $hashDirRegex =
'(' . $char .
')';
1635 $hashDirRegex = $char .
'/(' . $char .
'{2})';
1637 $hashDirRegex =
'(' . $char .
')/(' . $char .
')';
1644 if ( preg_match(
"!^(?:[^/]{2,}/)*$hashDirRegex(?:/|$)!", $relPath, $m ) ) {
1645 return '.' . implode(
'', array_slice( $m, 1 ) );
1665 return ( $shard !==
null );
1677 if ( isset( $this->shardViaHashLevels[$container] ) ) {
1678 $config = $this->shardViaHashLevels[$container];
1679 $hashLevels = (int)$config[
'levels'];
1680 if ( $hashLevels == 1 || $hashLevels == 2 ) {
1681 $hashBase = (int)$config[
'base'];
1682 if ( $hashBase == 16 || $hashBase == 36 ) {
1683 return [ $hashLevels, $hashBase, $config[
'repeat'] ];
1688 return [ 0, 0, false ];
1700 if ( $digits > 0 ) {
1701 $numShards = $base ** $digits;
1702 for ( $index = 0; $index < $numShards; $index++ ) {
1703 $shards[] =
'.' . \Wikimedia\base_convert( (
string)$index, 10, $base, $digits );
1717 if ( $this->domainId !=
'' ) {
1718 return "{$this->domainId}-$container";
1749 return $relStoragePath;
1758 private function containerCacheKey( $container ) {
1759 return "filebackend:{$this->name}:{$this->domainId}:container:{$container}";
1769 if ( !$this->wanStatCache->set(
1770 $this->containerCacheKey( $container ),
1774 $this->logger->warning(
"Unable to set stat cache for container {container}.",
1775 [
'filebackend' => $this->name,
'container' => $container ]
1787 if ( !$this->wanStatCache->delete( $this->containerCacheKey( $container ), 300 ) ) {
1788 $this->logger->warning(
"Unable to delete stat cache for container {container}.",
1789 [
'filebackend' => $this->name,
'container' => $container ]
1803 foreach ( $items as $item ) {
1804 if ( self::isStoragePath( $item ) ) {
1806 } elseif ( is_string( $item ) ) {
1807 $contNames[$this->containerCacheKey( $item )] = $item;
1811 foreach ( $paths as
$path ) {
1813 if ( $fullCont !==
null ) {
1814 $contNames[$this->containerCacheKey( $fullCont )] = $fullCont;
1820 $values = $this->wanStatCache->getMulti( array_keys( $contNames ) );
1821 foreach ( $values as $cacheKey => $val ) {
1822 $contInfo[$contNames[$cacheKey]] = $val;
1846 private function fileCacheKey(
$path ) {
1847 return "filebackend:{$this->name}:{$this->domainId}:file:" . sha1(
$path );
1860 if (
$path ===
null ) {
1863 $mtime = (int)ConvertibleTimestamp::convert( TS::UNIX, $val[
'mtime'] );
1864 $ttl = $this->wanStatCache->adaptiveTTL( $mtime, 7 * 86400, 300, 0.1 );
1866 if ( !$this->wanStatCache->set( $this->fileCacheKey(
$path ), $val, $ttl ) ) {
1867 $this->logger->warning(
"Unable to set stat cache for file {path}.",
1868 [
'filebackend' => $this->name,
'path' =>
$path ]
1883 if (
$path ===
null ) {
1886 if ( !$this->wanStatCache->delete( $this->fileCacheKey(
$path ), 300 ) ) {
1887 $this->logger->warning(
"Unable to delete stat cache for file {path}.",
1888 [
'filebackend' => $this->name,
'path' =>
$path ]
1904 foreach ( $items as $item ) {
1905 if ( self::isStoragePath( $item ) ) {
1907 if (
$path !==
null ) {
1913 foreach ( $paths as
$path ) {
1915 if ( $rel !==
null ) {
1916 $pathNames[$this->fileCacheKey(
$path )] =
$path;
1921 $values = $this->wanStatCache->getMulti( array_keys( $pathNames ) );
1923 foreach ( array_filter( $values,
'is_array' ) as $cacheKey => $stat ) {
1924 $path = $pathNames[$cacheKey];
1927 unset( $stat[
'latest'] );
1929 $this->cheapCache->setField(
$path,
'stat', $stat );
1930 if ( isset( $stat[
'sha1'] ) && strlen( $stat[
'sha1'] ) == 31 ) {
1932 $this->cheapCache->setField(
1935 [
'hash' => $stat[
'sha1'],
'latest' =>
false ]
1938 if ( isset( $stat[
'xattr'] ) && is_array( $stat[
'xattr'] ) ) {
1941 $this->cheapCache->setField(
1944 [
'map' => $stat[
'xattr'],
'latest' =>
false ]
1958 $newXAttr = [
'headers' => [],
'metadata' => [] ];
1960 foreach ( $xattr[
'headers'] as
$name => $value ) {
1961 $newXAttr[
'headers'][strtolower(
$name )] = $value;
1964 foreach ( $xattr[
'metadata'] as
$name => $value ) {
1965 $newXAttr[
'metadata'][strtolower(
$name )] = $value;
1978 $opts[
'concurrency'] = 1;
1979 if ( $this->parallelize ===
'implicit' ) {
1980 if ( $opts[
'parallelize'] ??
true ) {
1983 } elseif ( $this->parallelize ===
'explicit' ) {
1984 if ( !empty( $opts[
'parallelize'] ) ) {
2002 if ( $this->mimeCallback ) {
2003 return ( $this->mimeCallback )( $storagePath, $content, $fsPath );
2006 $mime = ( $fsPath !== null ) ? mime_content_type( $fsPath ) :
false;
2007 return $mime ?:
'unknown/unknown';
2012class_alias( FileBackendStore::class,
'FileBackendStore' );
Generic operation result class Has warning/error list, boolean status and arbitrary value.