9 * Base class for all file backend classes (including multi-write backends).
10 * This class defines the methods as abstract that subclasses must implement.
11 * Outside callers can assume that all backends will have these functions.
13 * All "storage paths" are of the format "mwstore://backend/container/path".
14 * The paths use UNIX file system (FS) notation, though any particular backend may
15 * not actually be using a local filesystem. Therefore, the paths are only virtual.
17 * Backend contents are stored under wiki-specific container names by default.
18 * For legacy reasons, this has no effect for the FS backend class, and per-wiki
19 * segregation must be done by setting the container paths appropriately.
21 * FS-based backends are somewhat more restrictive due to the existence of real
22 * directory files; a regular file cannot have the same name as a directory. Other
23 * backends with virtual directories may not have this limitation. Callers should
24 * store files in such a way that no files and directories are under the same path.
26 * Methods should avoid throwing exceptions at all costs.
27 * As a corollary, external dependencies should be kept to a minimum.
29 * @ingroup FileBackend
32 abstract class FileBackendBase
{
33 protected $name; // unique backend name
34 protected $wikiId; // unique wiki name
35 protected $readOnly; // string
36 /** @var LockManager */
37 protected $lockManager;
40 * Create a new backend instance from configuration.
41 * This should only be called from within FileBackendGroup.
44 * 'name' : The unique name of this backend.
45 * 'wikiId' : Prefix to container names that is unique to this wiki.
46 * This should consist of alphanumberic, '-', and '_' chars.
47 * 'lockManager' : Registered name of a file lock manager to use.
48 * 'readOnly' : Write operations are disallowed if this is a non-empty string.
49 * It should be an explanation for the backend being read-only.
51 * @param $config Array
53 public function __construct( array $config ) {
54 $defaultConfig = array(
55 'lockManager' => null,
57 $config = array_merge( $defaultConfig, $config );
58 $this->name
= $config['name'];
59 $this->wikiId
= isset( $config['wikiId'] )
61 : wfWikiID(); // e.g. "my_wiki-en_"
62 $this->wikiId
= $this->resolveWikiId( $this->wikiId
);
63 $this->lockManager
= LockManagerGroup
::singleton()->get( $config['lockManager'] );
64 $this->readOnly
= isset( $config['readOnly'] )
65 ?
(string)$config['readOnly']
70 * Normalize a wiki ID by replacing characters that are
71 * not supported by the backend as part of container names.
73 * @param $wikiId string
76 protected function resolveWikiId( $wikiId ) {
81 * Get the unique backend name.
82 * We may have multiple different backends of the same type.
83 * For example, we can have two Swift backends using different proxies.
87 final public function getName() {
92 * This is the main entry point into the backend for write operations.
93 * Callers supply an ordered list of operations to perform as a transaction.
94 * If any serious errors occur, all attempted operations will be rolled back.
96 * $ops is an array of arrays. The outer array holds a list of operations.
97 * Each inner array is a set of key value pairs that specify an operation.
99 * Supported operations and their parameters:
100 * a) Create a new file in storage with the contents of a string
103 * 'dst' => <storage path>,
104 * 'content' => <string of new file contents>,
105 * 'overwrite' => <boolean>,
106 * 'overwriteSame' => <boolean>
108 * b) Copy a file system file into storage
111 * 'src' => <file system path>,
112 * 'dst' => <storage path>,
113 * 'overwrite' => <boolean>,
114 * 'overwriteSame' => <boolean>
116 * c) Copy a file within storage
119 * 'src' => <storage path>,
120 * 'dst' => <storage path>,
121 * 'overwrite' => <boolean>,
122 * 'overwriteSame' => <boolean>
124 * d) Move a file within storage
127 * 'src' => <storage path>,
128 * 'dst' => <storage path>,
129 * 'overwrite' => <boolean>,
130 * 'overwriteSame' => <boolean>
132 * e) Delete a file within storage
135 * 'src' => <storage path>,
136 * 'ignoreMissingSource' => <boolean>
138 * f) Do nothing (no-op)
143 * Boolean flags for operations (operation-specific):
144 * 'ignoreMissingSource' : The operation will simply succeed and do
145 * nothing if the source file does not exist.
146 * 'overwrite' : Any destination file will be overwritten.
147 * 'overwriteSame' : An error will not be given if a file already
148 * exists at the destination that has the same
149 * contents as the new contents to be written there.
151 * $opts is an associative of boolean flags, including:
152 * 'force' : Errors that would normally cause a rollback do not.
153 * The remaining operations are still attempted if any fail.
154 * 'nonLocking' : No locks are acquired for the operations.
155 * This can increase performance for non-critical writes.
156 * This has no effect unless the 'force' flag is set.
157 * 'allowStale' : Don't require the latest available data.
158 * This can increase performance for non-critical writes.
159 * This has no effect unless the 'force' flag is set.
162 * File system paths given to operations should refer to files that are
163 * either locked or otherwise safe from modification from other processes.
164 * Normally these files will be new temp files, which should be adequate.
167 * This returns a Status, which contains all warnings and fatals that occured
168 * during the operation. The 'failCount', 'successCount', and 'success' members
169 * will reflect each operation attempted. The status will be "OK" unless any
170 * of the operations failed and the 'force' parameter was not set.
172 * @param $ops Array List of operations to execute in order
173 * @param $opts Array Batch operation options
176 final public function doOperations( array $ops, array $opts = array() ) {
177 if ( $this->readOnly
!= '' ) {
178 return Status
::newFatal( 'backend-fail-readonly', $this->name
, $this->readOnly
);
180 if ( empty( $opts['force'] ) ) { // sanity
181 unset( $opts['nonLocking'] );
182 unset( $opts['allowStale'] );
184 return $this->doOperationsInternal( $ops, $opts );
188 * @see FileBackendBase::doOperations()
190 abstract protected function doOperationsInternal( array $ops, array $opts );
193 * Same as doOperations() except it takes a single operation.
194 * If you are doing a batch of operations that should either
195 * all succeed or all fail, then use that function instead.
197 * @see FileBackendBase::doOperations()
199 * @param $op Array Operation
200 * @param $opts Array Operation options
203 final public function doOperation( array $op, array $opts = array() ) {
204 return $this->doOperations( array( $op ), $opts );
208 * Performs a single create operation.
209 * This sets $params['op'] to 'create' and passes it to doOperation().
211 * @see FileBackendBase::doOperation()
213 * @param $params Array Operation parameters
214 * @param $opts Array Operation options
217 final public function create( array $params, array $opts = array() ) {
218 $params['op'] = 'create';
219 return $this->doOperation( $params, $opts );
223 * Performs a single store operation.
224 * This sets $params['op'] to 'store' and passes it to doOperation().
226 * @see FileBackendBase::doOperation()
228 * @param $params Array Operation parameters
229 * @param $opts Array Operation options
232 final public function store( array $params, array $opts = array() ) {
233 $params['op'] = 'store';
234 return $this->doOperation( $params, $opts );
238 * Performs a single copy operation.
239 * This sets $params['op'] to 'copy' and passes it to doOperation().
241 * @see FileBackendBase::doOperation()
243 * @param $params Array Operation parameters
244 * @param $opts Array Operation options
247 final public function copy( array $params, array $opts = array() ) {
248 $params['op'] = 'copy';
249 return $this->doOperation( $params, $opts );
253 * Performs a single move operation.
254 * This sets $params['op'] to 'move' and passes it to doOperation().
256 * @see FileBackendBase::doOperation()
258 * @param $params Array Operation parameters
259 * @param $opts Array Operation options
262 final public function move( array $params, array $opts = array() ) {
263 $params['op'] = 'move';
264 return $this->doOperation( $params, $opts );
268 * Performs a single delete operation.
269 * This sets $params['op'] to 'delete' and passes it to doOperation().
271 * @see FileBackendBase::doOperation()
273 * @param $params Array Operation parameters
274 * @param $opts Array Operation options
277 final public function delete( array $params, array $opts = array() ) {
278 $params['op'] = 'delete';
279 return $this->doOperation( $params, $opts );
283 * Concatenate a list of storage files into a single file on the file system
285 * srcs : ordered source storage paths (e.g. chunk1, chunk2, ...)
286 * dst : file system path to 0-byte temp file
288 * @param $params Array Operation parameters
291 abstract public function concatenate( array $params );
294 * Prepare a storage directory for usage.
295 * This will create any required containers and parent directories.
296 * Backends using key/value stores only need to create the container.
299 * dir : storage directory
301 * @param $params Array
304 final public function prepare( array $params ) {
305 if ( $this->readOnly
!= '' ) {
306 return Status
::newFatal( 'backend-fail-readonly', $this->name
, $this->readOnly
);
308 return $this->doPrepare( $params );
312 * @see FileBackendBase::prepare()
314 abstract protected function doPrepare( array $params );
317 * Take measures to block web access to a storage directory and
318 * the container it belongs to. FS backends might add .htaccess
319 * files whereas key/value store backends might restrict container
320 * access to the auth user that represents end-users in web request.
321 * This is not guaranteed to actually do anything.
324 * dir : storage directory
325 * noAccess : try to deny file access
326 * noListing : try to deny file listing
328 * @param $params Array
331 final public function secure( array $params ) {
332 if ( $this->readOnly
!= '' ) {
333 return Status
::newFatal( 'backend-fail-readonly', $this->name
, $this->readOnly
);
335 $status = $this->doPrepare( $params ); // dir must exist to restrict it
336 if ( $status->isOK() ) {
337 $status->merge( $this->doSecure( $params ) );
343 * @see FileBackendBase::secure()
345 abstract protected function doSecure( array $params );
348 * Delete a storage directory if it is empty.
349 * Backends using key/value stores may do nothing unless the directory
350 * is that of an empty container, in which case it should be deleted.
353 * dir : storage directory
355 * @param $params Array
358 final public function clean( array $params ) {
359 if ( $this->readOnly
!= '' ) {
360 return Status
::newFatal( 'backend-fail-readonly', $this->name
, $this->readOnly
);
362 return $this->doClean( $params );
366 * @see FileBackendBase::clean()
368 abstract protected function doClean( array $params );
371 * Check if a file exists at a storage path in the backend.
372 * This returns false if only a directory exists at the path.
375 * src : source storage path
376 * latest : use the latest available data
378 * @param $params Array
379 * @return bool|null Returns null on failure
381 abstract public function fileExists( array $params );
384 * Get the last-modified timestamp of the file at a storage path.
387 * src : source storage path
388 * latest : use the latest available data
390 * @param $params Array
391 * @return string|false TS_MW timestamp or false on failure
393 abstract public function getFileTimestamp( array $params );
396 * Get the contents of a file at a storage path in the backend.
397 * This should be avoided for potentially large files.
400 * src : source storage path
401 * latest : use the latest available data
403 * @param $params Array
404 * @return string|false Returns false on failure
406 abstract public function getFileContents( array $params );
409 * Get the size (bytes) of a file at a storage path in the backend.
412 * src : source storage path
413 * latest : use the latest available data
415 * @param $params Array
416 * @return integer|false Returns false on failure
418 abstract public function getFileSize( array $params );
421 * Get quick information about a file at a storage path in the backend.
422 * If the file does not exist, then this returns false.
423 * Otherwise, the result is an associative array that includes:
424 * mtime : the last-modified timestamp (TS_MW)
425 * size : the file size (bytes)
426 * Additional values may be included for internal use only.
429 * src : source storage path
430 * latest : use the latest available data
432 * @param $params Array
433 * @return Array|false|null Returns null on failure
435 abstract public function getFileStat( array $params );
438 * Get a SHA-1 hash of the file at a storage path in the backend.
441 * src : source storage path
442 * latest : use the latest available data
444 * @param $params Array
445 * @return string|false Hash string or false on failure
447 abstract public function getFileSha1Base36( array $params );
450 * Get the properties of the file at a storage path in the backend.
451 * Returns FSFile::placeholderProps() on failure.
454 * src : source storage path
455 * latest : use the latest available data
457 * @param $params Array
460 abstract public function getFileProps( array $params );
463 * Stream the file at a storage path in the backend.
464 * If the file does not exists, a 404 error will be given.
465 * Appropriate HTTP headers (Status, Content-Type, Content-Length)
466 * must be sent if streaming began, while none should be sent otherwise.
467 * Implementations should flush the output buffer before sending data.
470 * src : source storage path
471 * headers : additional HTTP headers to send on success
472 * latest : use the latest available data
474 * @param $params Array
477 abstract public function streamFile( array $params );
480 * Returns a file system file, identical to the file at a storage path.
481 * The file returned is either:
482 * a) A local copy of the file at a storage path in the backend.
483 * The temporary copy will have the same extension as the source.
484 * b) An original of the file at a storage path in the backend.
485 * Temporary files may be purged when the file object falls out of scope.
487 * Write operations should *never* be done on this file as some backends
488 * may do internal tracking or may be instances of FileBackendMultiWrite.
489 * In that later case, there are copies of the file that must stay in sync.
492 * src : source storage path
493 * latest : use the latest available data
495 * @param $params Array
496 * @return FSFile|null Returns null on failure
498 abstract public function getLocalReference( array $params );
501 * Get a local copy on disk of the file at a storage path in the backend.
502 * The temporary copy will have the same file extension as the source.
503 * Temporary files may be purged when the file object falls out of scope.
506 * src : source storage path
507 * latest : use the latest available data
509 * @param $params Array
510 * @return TempFSFile|null Returns null on failure
512 abstract public function getLocalCopy( array $params );
515 * Get an iterator to list out all stored files under a storage directory.
516 * If the directory is of the form "mwstore://container", then all items in
517 * the container should be listed. If of the form "mwstore://container/dir",
518 * then all items under that container directory should be listed.
519 * Results should be storage paths relative to the given directory.
522 * dir : storage path directory
524 * @return Traversable|Array|null Returns null on failure
526 abstract public function getFileList( array $params );
529 * Invalidate any in-process file existence and property cache.
530 * If $paths is given, then only the cache for those files will be cleared.
532 * @param $paths Array Storage paths (optional)
535 abstract public function clearCache( array $paths = null );
538 * Lock the files at the given storage paths in the backend.
539 * This will either lock all the files or none (on failure).
541 * Callers should consider using getScopedFileLocks() instead.
543 * @param $paths Array Storage paths
544 * @param $type integer LockManager::LOCK_* constant
547 final public function lockFiles( array $paths, $type ) {
548 return $this->lockManager
->lock( $paths, $type );
552 * Unlock the files at the given storage paths in the backend.
554 * @param $paths Array Storage paths
555 * @param $type integer LockManager::LOCK_* constant
558 final public function unlockFiles( array $paths, $type ) {
559 return $this->lockManager
->unlock( $paths, $type );
563 * Lock the files at the given storage paths in the backend.
564 * This will either lock all the files or none (on failure).
565 * On failure, the status object will be updated with errors.
567 * Once the return value goes out scope, the locks will be released and
568 * the status updated. Unlock fatals will not change the status "OK" value.
570 * @param $paths Array Storage paths
571 * @param $type integer LockManager::LOCK_* constant
572 * @param $status Status Status to update on lock/unlock
573 * @return ScopedLock|null Returns null on failure
575 final public function getScopedFileLocks( array $paths, $type, Status
$status ) {
576 return ScopedLock
::factory( $this->lockManager
, $paths, $type, $status );
581 * Base class for all single-write backends.
582 * This class defines the methods as abstract that subclasses must implement.
583 * Callers outside of FileBackend and its helper classes, such as FileOp,
584 * should only call functions that are present in FileBackendBase.
586 * The FileBackendBase operations are implemented using primitive functions
587 * such as storeInternal(), copyInternal(), deleteInternal() and the like.
588 * This class is also responsible for path resolution and sanitization.
590 * @ingroup FileBackend
593 abstract class FileBackend
extends FileBackendBase
{
595 protected $cache = array(); // (storage path => key => value)
596 protected $maxCacheSize = 75; // integer; max paths with entries
598 protected $shardViaHashLevels = array(); // (container name => integer)
600 protected $maxFileSize = 1000000000; // integer bytes (1GB)
603 * Get the maximum allowable file size given backend
604 * medium restrictions and basic performance constraints.
605 * Do not call this function from places outside FileBackend and FileOp.
607 * @return integer Bytes
609 final public function maxFileSizeInternal() {
610 return $this->maxFileSize
;
614 * Check if a file can be created at a given storage path.
615 * FS backends should check if the parent directory exists and the file is writable.
616 * Backends using key/value stores should check if the container exists.
618 * @param $storagePath string
621 abstract public function isPathUsableInternal( $storagePath );
624 * Create a file in the backend with the given contents.
625 * Do not call this function from places outside FileBackend and FileOp.
628 * content : the raw file contents
629 * dst : destination storage path
630 * overwrite : overwrite any file that exists at the destination
632 * @param $params Array
635 final public function createInternal( array $params ) {
636 wfProfileIn( __METHOD__
);
637 if ( strlen( $params['content'] ) > $this->maxFileSizeInternal() ) {
638 $status = Status
::newFatal( 'backend-fail-create', $params['dst'] );
640 $status = $this->doCreateInternal( $params );
641 $this->clearCache( array( $params['dst'] ) );
643 wfProfileOut( __METHOD__
);
648 * @see FileBackend::createInternal()
650 abstract protected function doCreateInternal( array $params );
653 * Store a file into the backend from a file on disk.
654 * Do not call this function from places outside FileBackend and FileOp.
657 * src : source path on disk
658 * dst : destination storage path
659 * overwrite : overwrite any file that exists at the destination
661 * @param $params Array
664 final public function storeInternal( array $params ) {
665 wfProfileIn( __METHOD__
);
666 if ( filesize( $params['src'] ) > $this->maxFileSizeInternal() ) {
667 $status = Status
::newFatal( 'backend-fail-store', $params['dst'] );
669 $status = $this->doStoreInternal( $params );
670 $this->clearCache( array( $params['dst'] ) );
672 wfProfileOut( __METHOD__
);
677 * @see FileBackend::storeInternal()
679 abstract protected function doStoreInternal( array $params );
682 * Copy a file from one storage path to another in the backend.
683 * Do not call this function from places outside FileBackend and FileOp.
686 * src : source storage path
687 * dst : destination storage path
688 * overwrite : overwrite any file that exists at the destination
690 * @param $params Array
693 final public function copyInternal( array $params ) {
694 wfProfileIn( __METHOD__
);
695 $status = $this->doCopyInternal( $params );
696 $this->clearCache( array( $params['dst'] ) );
697 wfProfileOut( __METHOD__
);
702 * @see FileBackend::copyInternal()
704 abstract protected function doCopyInternal( array $params );
707 * Delete a file at the storage path.
708 * Do not call this function from places outside FileBackend and FileOp.
711 * src : source storage path
712 * ignoreMissingSource : do nothing if the source file does not exist
714 * @param $params Array
717 final public function deleteInternal( array $params ) {
718 wfProfileIn( __METHOD__
);
719 $status = $this->doDeleteInternal( $params );
720 $this->clearCache( array( $params['src'] ) );
721 wfProfileOut( __METHOD__
);
726 * @see FileBackend::deleteInternal()
728 abstract protected function doDeleteInternal( array $params );
731 * Move a file from one storage path to another in the backend.
732 * Do not call this function from places outside FileBackend and FileOp.
735 * src : source storage path
736 * dst : destination storage path
737 * overwrite : overwrite any file that exists at the destination
739 * @param $params Array
742 final public function moveInternal( array $params ) {
743 wfProfileIn( __METHOD__
);
744 $status = $this->doMoveInternal( $params );
745 $this->clearCache( array( $params['src'], $params['dst'] ) );
746 wfProfileOut( __METHOD__
);
751 * @see FileBackend::moveInternal()
753 protected function doMoveInternal( array $params ) {
754 // Copy source to dest
755 $status = $this->copyInternal( $params );
756 if ( $status->isOK() ) {
757 // Delete source (only fails due to races or medium going down)
758 $status->merge( $this->deleteInternal( array( 'src' => $params['src'] ) ) );
759 $status->setResult( true, $status->value
); // ignore delete() errors
765 * @see FileBackendBase::concatenate()
767 final public function concatenate( array $params ) {
768 wfProfileIn( __METHOD__
);
769 $status = Status
::newGood();
771 // Try to lock the source files for the scope of this function
772 $scopeLockS = $this->getScopedFileLocks( $params['srcs'], LockManager
::LOCK_UW
, $status );
773 if ( $status->isOK() ) {
774 // Actually do the concatenation
775 $status->merge( $this->doConcatenate( $params ) );
778 wfProfileOut( __METHOD__
);
783 * @see FileBackend::concatenate()
785 protected function doConcatenate( array $params ) {
786 $status = Status
::newGood();
787 $tmpPath = $params['dst']; // convenience
789 // Check that the specified temp file is valid...
790 wfSuppressWarnings();
791 $ok = ( is_file( $tmpPath ) && !filesize( $tmpPath ) );
793 if ( !$ok ) { // not present or not empty
794 $status->fatal( 'backend-fail-opentemp', $tmpPath );
798 // Build up the temp file using the source chunks (in order)...
799 $tmpHandle = fopen( $tmpPath, 'a' );
800 if ( $tmpHandle === false ) {
801 $status->fatal( 'backend-fail-opentemp', $tmpPath );
804 foreach ( $params['srcs'] as $virtualSource ) {
805 // Get a local FS version of the chunk
806 $tmpFile = $this->getLocalReference( array( 'src' => $virtualSource ) );
808 $status->fatal( 'backend-fail-read', $virtualSource );
811 // Get a handle to the local FS version
812 $sourceHandle = fopen( $tmpFile->getPath(), 'r' );
813 if ( $sourceHandle === false ) {
814 fclose( $tmpHandle );
815 $status->fatal( 'backend-fail-read', $virtualSource );
818 // Append chunk to file (pass chunk size to avoid magic quotes)
819 if ( !stream_copy_to_stream( $sourceHandle, $tmpHandle ) ) {
820 fclose( $sourceHandle );
821 fclose( $tmpHandle );
822 $status->fatal( 'backend-fail-writetemp', $tmpPath );
825 fclose( $sourceHandle );
827 if ( !fclose( $tmpHandle ) ) {
828 $status->fatal( 'backend-fail-closetemp', $tmpPath );
832 clearstatcache(); // temp file changed
838 * @see FileBackendBase::doPrepare()
840 final protected function doPrepare( array $params ) {
841 wfProfileIn( __METHOD__
);
843 $status = Status
::newGood();
844 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
845 if ( $dir === null ) {
846 $status->fatal( 'backend-fail-invalidpath', $params['dir'] );
847 wfProfileOut( __METHOD__
);
848 return $status; // invalid storage path
851 if ( $shard !== null ) { // confined to a single container/shard
852 $status->merge( $this->doPrepareInternal( $fullCont, $dir, $params ) );
853 } else { // directory is on several shards
854 wfDebug( __METHOD__
. ": iterating over all container shards.\n" );
855 list( $b, $shortCont, $r ) = self
::splitStoragePath( $params['dir'] );
856 foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
857 $status->merge( $this->doPrepareInternal( "{$fullCont}{$suffix}", $dir, $params ) );
861 wfProfileOut( __METHOD__
);
866 * @see FileBackend::doPrepare()
868 protected function doPrepareInternal( $container, $dir, array $params ) {
869 return Status
::newGood();
873 * @see FileBackendBase::doSecure()
875 final protected function doSecure( array $params ) {
876 wfProfileIn( __METHOD__
);
877 $status = Status
::newGood();
879 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
880 if ( $dir === null ) {
881 $status->fatal( 'backend-fail-invalidpath', $params['dir'] );
882 wfProfileOut( __METHOD__
);
883 return $status; // invalid storage path
886 if ( $shard !== null ) { // confined to a single container/shard
887 $status->merge( $this->doSecureInternal( $fullCont, $dir, $params ) );
888 } else { // directory is on several shards
889 wfDebug( __METHOD__
. ": iterating over all container shards.\n" );
890 list( $b, $shortCont, $r ) = self
::splitStoragePath( $params['dir'] );
891 foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
892 $status->merge( $this->doSecureInternal( "{$fullCont}{$suffix}", $dir, $params ) );
896 wfProfileOut( __METHOD__
);
901 * @see FileBackend::doSecure()
903 protected function doSecureInternal( $container, $dir, array $params ) {
904 return Status
::newGood();
908 * @see FileBackendBase::doClean()
910 final protected function doClean( array $params ) {
911 wfProfileIn( __METHOD__
);
912 $status = Status
::newGood();
914 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
915 if ( $dir === null ) {
916 $status->fatal( 'backend-fail-invalidpath', $params['dir'] );
917 wfProfileOut( __METHOD__
);
918 return $status; // invalid storage path
921 // Attempt to lock this directory...
922 $filesLockEx = array( $params['dir'] );
923 $scopedLockE = $this->getScopedFileLocks( $filesLockEx, LockManager
::LOCK_EX
, $status );
924 if ( !$status->isOK() ) {
925 wfProfileOut( __METHOD__
);
926 return $status; // abort
929 if ( $shard !== null ) { // confined to a single container/shard
930 $status->merge( $this->doCleanInternal( $fullCont, $dir, $params ) );
931 } else { // directory is on several shards
932 wfDebug( __METHOD__
. ": iterating over all container shards.\n" );
933 list( $b, $shortCont, $r ) = self
::splitStoragePath( $params['dir'] );
934 foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
935 $status->merge( $this->doCleanInternal( "{$fullCont}{$suffix}", $dir, $params ) );
939 wfProfileOut( __METHOD__
);
944 * @see FileBackend::doClean()
946 protected function doCleanInternal( $container, $dir, array $params ) {
947 return Status
::newGood();
951 * @see FileBackendBase::fileExists()
953 final public function fileExists( array $params ) {
954 wfProfileIn( __METHOD__
);
955 $stat = $this->getFileStat( $params );
956 wfProfileOut( __METHOD__
);
957 return ( $stat === null ) ?
null : (bool)$stat; // null => failure
961 * @see FileBackendBase::getFileTimestamp()
963 final public function getFileTimestamp( array $params ) {
964 wfProfileIn( __METHOD__
);
965 $stat = $this->getFileStat( $params );
966 wfProfileOut( __METHOD__
);
967 return $stat ?
$stat['mtime'] : false;
971 * @see FileBackendBase::getFileSize()
973 final public function getFileSize( array $params ) {
974 wfProfileIn( __METHOD__
);
975 $stat = $this->getFileStat( $params );
976 wfProfileOut( __METHOD__
);
977 return $stat ?
$stat['size'] : false;
981 * @see FileBackendBase::getFileStat()
983 final public function getFileStat( array $params ) {
984 wfProfileIn( __METHOD__
);
985 $path = $params['src'];
986 $latest = !empty( $params['latest'] );
987 if ( isset( $this->cache
[$path]['stat'] ) ) {
988 // If we want the latest data, check that this cached
989 // value was in fact fetched with the latest available data.
990 if ( !$latest ||
$this->cache
[$path]['stat']['latest'] ) {
991 wfProfileOut( __METHOD__
);
992 return $this->cache
[$path]['stat'];
995 $stat = $this->doGetFileStat( $params );
996 if ( is_array( $stat ) ) { // don't cache negatives
997 $this->trimCache(); // limit memory
998 $this->cache
[$path]['stat'] = $stat;
999 $this->cache
[$path]['stat']['latest'] = $latest;
1001 wfProfileOut( __METHOD__
);
1006 * @see FileBackend::getFileStat()
1008 abstract protected function doGetFileStat( array $params );
1011 * @see FileBackendBase::getFileContents()
1013 public function getFileContents( array $params ) {
1014 wfProfileIn( __METHOD__
);
1015 $tmpFile = $this->getLocalReference( $params );
1017 wfProfileOut( __METHOD__
);
1020 wfSuppressWarnings();
1021 $data = file_get_contents( $tmpFile->getPath() );
1022 wfRestoreWarnings();
1023 wfProfileOut( __METHOD__
);
1028 * @see FileBackendBase::getFileSha1Base36()
1030 final public function getFileSha1Base36( array $params ) {
1031 wfProfileIn( __METHOD__
);
1032 $path = $params['src'];
1033 if ( isset( $this->cache
[$path]['sha1'] ) ) {
1034 wfProfileOut( __METHOD__
);
1035 return $this->cache
[$path]['sha1'];
1037 $hash = $this->doGetFileSha1Base36( $params );
1038 if ( $hash ) { // don't cache negatives
1039 $this->trimCache(); // limit memory
1040 $this->cache
[$path]['sha1'] = $hash;
1042 wfProfileOut( __METHOD__
);
1047 * @see FileBackend::getFileSha1Base36()
1049 protected function doGetFileSha1Base36( array $params ) {
1050 $fsFile = $this->getLocalReference( $params );
1054 return $fsFile->getSha1Base36();
1059 * @see FileBackendBase::getFileProps()
1061 final public function getFileProps( array $params ) {
1062 wfProfileIn( __METHOD__
);
1063 $fsFile = $this->getLocalReference( $params );
1064 $props = $fsFile ?
$fsFile->getProps() : FSFile
::placeholderProps();
1065 wfProfileOut( __METHOD__
);
1070 * @see FileBackendBase::getLocalReference()
1072 public function getLocalReference( array $params ) {
1073 wfProfileIn( __METHOD__
);
1074 $path = $params['src'];
1075 if ( isset( $this->cache
[$path]['localRef'] ) ) {
1076 wfProfileOut( __METHOD__
);
1077 return $this->cache
[$path]['localRef'];
1079 $tmpFile = $this->getLocalCopy( $params );
1080 if ( $tmpFile ) { // don't cache negatives
1081 $this->trimCache(); // limit memory
1082 $this->cache
[$path]['localRef'] = $tmpFile;
1084 wfProfileOut( __METHOD__
);
1089 * @see FileBackendBase::streamFile()
1091 final public function streamFile( array $params ) {
1092 wfProfileIn( __METHOD__
);
1093 $status = Status
::newGood();
1095 $info = $this->getFileStat( $params );
1096 if ( !$info ) { // let StreamFile handle the 404
1097 $status->fatal( 'backend-fail-notexists', $params['src'] );
1100 // Set output buffer and HTTP headers for stream
1101 $extraHeaders = $params['headers'] ?
$params['headers'] : array();
1102 $res = StreamFile
::prepareForStream( $params['src'], $info, $extraHeaders );
1103 if ( $res == StreamFile
::NOT_MODIFIED
) {
1104 // do nothing; client cache is up to date
1105 } elseif ( $res == StreamFile
::READY_STREAM
) {
1106 $status = $this->doStreamFile( $params );
1108 $status->fatal( 'backend-fail-stream', $params['src'] );
1111 wfProfileOut( __METHOD__
);
1116 * @see FileBackend::streamFile()
1118 protected function doStreamFile( array $params ) {
1119 $status = Status
::newGood();
1121 $fsFile = $this->getLocalReference( $params );
1123 $status->fatal( 'backend-fail-stream', $params['src'] );
1124 } elseif ( !readfile( $fsFile->getPath() ) ) {
1125 $status->fatal( 'backend-fail-stream', $params['src'] );
1132 * @see FileBackendBase::getFileList()
1134 final public function getFileList( array $params ) {
1135 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
1136 if ( $dir === null ) { // invalid storage path
1139 if ( $shard !== null ) {
1140 // File listing is confined to a single container/shard
1141 return $this->getFileListInternal( $fullCont, $dir, $params );
1143 wfDebug( __METHOD__
. ": iterating over all container shards.\n" );
1144 // File listing spans multiple containers/shards
1145 list( $b, $shortCont, $r ) = self
::splitStoragePath( $params['dir'] );
1146 return new FileBackendShardListIterator( $this,
1147 $fullCont, $this->getContainerSuffixes( $shortCont ), $params );
1152 * Do not call this function from places outside FileBackend and ContainerFileListIterator
1154 * @param $container string Resolved container name
1155 * @param $dir string Resolved path relative to container
1156 * @param $params Array
1157 * @see FileBackend::getFileList()
1159 abstract public function getFileListInternal( $container, $dir, array $params );
1162 * Get the list of supported operations and their corresponding FileOp classes.
1166 protected function supportedOperations() {
1168 'store' => 'StoreFileOp',
1169 'copy' => 'CopyFileOp',
1170 'move' => 'MoveFileOp',
1171 'delete' => 'DeleteFileOp',
1172 'create' => 'CreateFileOp',
1173 'null' => 'NullFileOp'
1178 * Return a list of FileOp objects from a list of operations.
1179 * Do not call this function from places outside FileBackend.
1181 * The result must have the same number of items as the input.
1182 * An exception is thrown if an unsupported operation is requested.
1184 * @param $ops Array Same format as doOperations()
1185 * @return Array List of FileOp objects
1186 * @throws MWException
1188 final public function getOperations( array $ops ) {
1189 $supportedOps = $this->supportedOperations();
1191 $performOps = array(); // array of FileOp objects
1192 // Build up ordered array of FileOps...
1193 foreach ( $ops as $operation ) {
1194 $opName = $operation['op'];
1195 if ( isset( $supportedOps[$opName] ) ) {
1196 $class = $supportedOps[$opName];
1197 // Get params for this operation
1198 $params = $operation;
1199 // Append the FileOp class
1200 $performOps[] = new $class( $this, $params );
1202 throw new MWException( "Operation `$opName` is not supported." );
1210 * @see FileBackendBase::doOperationsInternal()
1212 protected function doOperationsInternal( array $ops, array $opts ) {
1213 wfProfileIn( __METHOD__
);
1214 $status = Status
::newGood();
1216 // Build up a list of FileOps...
1217 $performOps = $this->getOperations( $ops );
1219 // Acquire any locks as needed...
1220 if ( empty( $opts['nonLocking'] ) ) {
1221 // Build up a list of files to lock...
1222 $filesLockEx = $filesLockSh = array();
1223 foreach ( $performOps as $fileOp ) {
1224 $filesLockSh = array_merge( $filesLockSh, $fileOp->storagePathsRead() );
1225 $filesLockEx = array_merge( $filesLockEx, $fileOp->storagePathsChanged() );
1227 // Optimization: if doing an EX lock anyway, don't also set an SH one
1228 $filesLockSh = array_diff( $filesLockSh, $filesLockEx );
1229 // Get a shared lock on the parent directory of each path changed
1230 $filesLockSh = array_merge( $filesLockSh, array_map( 'dirname', $filesLockEx ) );
1231 // Try to lock those files for the scope of this function...
1232 $scopeLockS = $this->getScopedFileLocks( $filesLockSh, LockManager
::LOCK_UW
, $status );
1233 $scopeLockE = $this->getScopedFileLocks( $filesLockEx, LockManager
::LOCK_EX
, $status );
1234 if ( !$status->isOK() ) {
1235 wfProfileOut( __METHOD__
);
1236 return $status; // abort
1240 // Clear any cache entries (after locks acquired)
1241 $this->clearCache();
1243 // Actually attempt the operation batch...
1244 $subStatus = FileOp
::attemptBatch( $performOps, $opts );
1246 // Merge errors into status fields
1247 $status->merge( $subStatus );
1248 $status->success
= $subStatus->success
; // not done in merge()
1250 wfProfileOut( __METHOD__
);
1255 * @see FileBackendBase::clearCache()
1257 final public function clearCache( array $paths = null ) {
1258 if ( $paths === null ) {
1259 $this->cache
= array();
1261 foreach ( $paths as $path ) {
1262 unset( $this->cache
[$path] );
1265 $this->doClearCache( $paths );
1269 * Clears any additional stat caches for storage paths
1271 * @see FileBackendBase::clearCache()
1273 * @param $paths Array Storage paths (optional)
1276 protected function doClearCache( array $paths = null ) {}
1279 * Prune the cache if it is too big to add an item
1283 protected function trimCache() {
1284 if ( count( $this->cache
) >= $this->maxCacheSize
) {
1285 reset( $this->cache
);
1286 $key = key( $this->cache
);
1287 unset( $this->cache
[$key] );
1292 * Get the parent storage directory of a storage path.
1293 * This returns a path like "mwstore://backend/container",
1294 * "mwstore://backend/container/...", or null if there is no parent.
1296 * @param $storagePath string
1297 * @return string|null
1299 final public static function parentStoragePath( $storagePath ) {
1300 $storagePath = dirname( $storagePath );
1301 list( $b, $cont, $rel ) = self
::splitStoragePath( $storagePath );
1302 return ( $rel === null ) ?
null : $storagePath;
1306 * Check if a given path is a mwstore:// path.
1307 * This does not do any actual validation or existence checks.
1309 * @param $path string
1312 final public static function isStoragePath( $path ) {
1313 return ( strpos( $path, 'mwstore://' ) === 0 );
1317 * Split a storage path (e.g. "mwstore://backend/container/path/to/object")
1318 * into a backend name, a container name, and a relative object path.
1320 * @param $storagePath string
1321 * @return Array (backend, container, rel object) or (null, null, null)
1323 final public static function splitStoragePath( $storagePath ) {
1324 if ( self
::isStoragePath( $storagePath ) ) {
1325 // Note: strlen( 'mwstore://' ) = 10
1326 $parts = explode( '/', substr( $storagePath, 10 ), 3 );
1327 if ( count( $parts ) == 3 ) {
1328 return $parts; // e.g. "backend/container/path"
1329 } elseif ( count( $parts ) == 2 ) {
1330 return array( $parts[0], $parts[1], '' ); // e.g. "backend/container"
1333 return array( null, null, null );
1337 * Check if a container name is valid.
1338 * This checks for for length and illegal characters.
1340 * @param $container string
1343 final protected static function isValidContainerName( $container ) {
1344 // This accounts for Swift and S3 restrictions while leaving room
1345 // for things like '.xxx' (hex shard chars) or '.seg' (segments).
1346 // Note that matching strings URL encode to the same string;
1347 // in Swift, the length restriction is *after* URL encoding.
1348 return preg_match( '/^[a-z0-9][a-z0-9-_]{0,199}$/i', $container );
1352 * Validate and normalize a relative storage path.
1353 * Null is returned if the path involves directory traversal.
1354 * Traversal is insecure for FS backends and broken for others.
1356 * @param $path string Storage path relative to a container
1357 * @return string|null
1359 final protected static function normalizeContainerPath( $path ) {
1360 // Normalize directory separators
1361 $path = strtr( $path, '\\', '/' );
1362 // Collapse consecutive directory separators
1363 $path = preg_replace( '![/]{2,}!', '/', $path );
1364 // Use the same traversal protection as Title::secureAndSplit()
1365 if ( strpos( $path, '.' ) !== false ) {
1369 strpos( $path, './' ) === 0 ||
1370 strpos( $path, '../' ) === 0 ||
1371 strpos( $path, '/./' ) !== false ||
1372 strpos( $path, '/../' ) !== false
1381 * Splits a storage path into an internal container name,
1382 * an internal relative file name, and a container shard suffix.
1383 * Any shard suffix is already appended to the internal container name.
1384 * This also checks that the storage path is valid and within this backend.
1386 * If the container is sharded but a suffix could not be determined,
1387 * this means that the path can only refer to a directory and can only
1388 * be scanned by looking in all the container shards.
1390 * @param $storagePath string
1391 * @return Array (container, path, container suffix) or (null, null, null) if invalid
1393 final protected function resolveStoragePath( $storagePath ) {
1394 list( $backend, $container, $relPath ) = self
::splitStoragePath( $storagePath );
1395 if ( $backend === $this->name
) { // must be for this backend
1396 $relPath = self
::normalizeContainerPath( $relPath );
1397 if ( $relPath !== null ) {
1398 // Get shard for the normalized path if this container is sharded
1399 $cShard = $this->getContainerShard( $container, $relPath );
1400 // Validate and sanitize the relative path (backend-specific)
1401 $relPath = $this->resolveContainerPath( $container, $relPath );
1402 if ( $relPath !== null ) {
1403 // Prepend any wiki ID prefix to the container name
1404 $container = $this->fullContainerName( $container );
1405 if ( self
::isValidContainerName( $container ) ) {
1406 // Validate and sanitize the container name (backend-specific)
1407 $container = $this->resolveContainerName( "{$container}{$cShard}" );
1408 if ( $container !== null ) {
1409 return array( $container, $relPath, $cShard );
1415 return array( null, null, null );
1419 * Like resolveStoragePath() except null values are returned if
1420 * the container is sharded and the shard could not be determined.
1422 * @see FileBackend::resolveStoragePath()
1424 * @param $storagePath string
1425 * @return Array (container, path) or (null, null) if invalid
1427 final protected function resolveStoragePathReal( $storagePath ) {
1428 list( $container, $relPath, $cShard ) = $this->resolveStoragePath( $storagePath );
1429 if ( $cShard !== null ) {
1430 return array( $container, $relPath );
1432 return array( null, null );
1436 * Get the container name shard suffix for a given path.
1437 * Any empty suffix means the container is not sharded.
1439 * @param $container string Container name
1440 * @param $relStoragePath string Storage path relative to the container
1441 * @return string|null Returns null if shard could not be determined
1443 final protected function getContainerShard( $container, $relPath ) {
1444 $hashLevels = $this->getContainerHashLevels( $container );
1445 if ( $hashLevels === 1 ) { // 16 shards per container
1446 $hashDirRegex = '(?P<shard>[0-9a-f])';
1447 } elseif ( $hashLevels === 2 ) { // 256 shards per container
1448 $hashDirRegex = '[0-9a-f]/(?P<shard>[0-9a-f]{2})';
1450 return ''; // no sharding
1452 // Allow certain directories to be above the hash dirs so as
1453 // to work with FileRepo (e.g. "archive/a/ab" or "temp/a/ab").
1454 // They must be 2+ chars to avoid any hash directory ambiguity.
1455 if ( preg_match( "!^(?:[^/]{2,}/)*$hashDirRegex(?:/|$)!", $relPath, $m ) ) {
1456 return '.' . str_pad( $m['shard'], $hashLevels, '0', STR_PAD_LEFT
);
1458 return null; // failed to match
1462 * Get the number of hash levels for a container.
1463 * If greater than 0, then all file storage paths within
1464 * the container are required to be hashed accordingly.
1466 * @param $container string
1469 final protected function getContainerHashLevels( $container ) {
1470 if ( isset( $this->shardViaHashLevels
[$container] ) ) {
1471 $hashLevels = (int)$this->shardViaHashLevels
[$container];
1472 if ( $hashLevels >= 0 && $hashLevels <= 2 ) {
1476 return 0; // no sharding
1480 * Get a list of full container shard suffixes for a container
1482 * @param $container string
1485 final protected function getContainerSuffixes( $container ) {
1487 $digits = $this->getContainerHashLevels( $container );
1488 if ( $digits > 0 ) {
1489 $numShards = 1 << ( $digits * 4 );
1490 for ( $index = 0; $index < $numShards; $index++
) {
1491 $shards[] = '.' . str_pad( dechex( $index ), $digits, '0', STR_PAD_LEFT
);
1498 * Get the full container name, including the wiki ID prefix
1500 * @param $container string
1503 final protected function fullContainerName( $container ) {
1504 if ( $this->wikiId
!= '' ) {
1505 return "{$this->wikiId}-$container";
1512 * Resolve a container name, checking if it's allowed by the backend.
1513 * This is intended for internal use, such as encoding illegal chars.
1514 * Subclasses can override this to be more restrictive.
1516 * @param $container string
1517 * @return string|null
1519 protected function resolveContainerName( $container ) {
1524 * Resolve a relative storage path, checking if it's allowed by the backend.
1525 * This is intended for internal use, such as encoding illegal chars or perhaps
1526 * getting absolute paths (e.g. FS based backends). Note that the relative path
1527 * may be the empty string (e.g. the path is simply to the container).
1529 * @param $container string Container name
1530 * @param $relStoragePath string Storage path relative to the container
1531 * @return string|null Path or null if not valid
1533 protected function resolveContainerPath( $container, $relStoragePath ) {
1534 return $relStoragePath;
1538 * Get the final extension from a storage or FS path
1540 * @param $path string
1543 final public static function extensionFromPath( $path ) {
1544 $i = strrpos( $path, '.' );
1545 return strtolower( $i ?
substr( $path, $i +
1 ) : '' );
1550 * FileBackend helper function to handle file listings that span container shards.
1551 * Do not use this class from places outside of FileBackend.
1553 * @ingroup FileBackend
1555 class FileBackendShardListIterator
implements Iterator
{
1556 /* @var FileBackend */
1561 protected $shardSuffixes;
1562 protected $container; // string
1563 protected $directory; // string
1565 /* @var Traversable */
1567 protected $curShard = 0; // integer
1568 protected $pos = 0; // integer
1571 * @param $backend FileBackend
1572 * @param $container string Full storage container name
1573 * @param $dir string Storage directory relative to container
1574 * @param $suffixes Array List of container shard suffixes
1575 * @param $params Array
1577 public function __construct(
1578 FileBackend
$backend, $container, $dir, array $suffixes, array $params
1580 $this->backend
= $backend;
1581 $this->container
= $container;
1582 $this->directory
= $dir;
1583 $this->shardSuffixes
= $suffixes;
1584 $this->params
= $params;
1587 public function current() {
1588 if ( is_array( $this->iter
) ) {
1589 return current( $this->iter
);
1591 return $this->iter
->current();
1595 public function key() {
1599 public function next() {
1601 if ( is_array( $this->iter
) ) {
1602 next( $this->iter
);
1604 $this->iter
->next();
1606 // Find the next non-empty shard if no elements are left
1607 $this->nextShardIteratorIfNotValid();
1611 * If the iterator for this container shard is out of items,
1612 * then move on to the next container that has items.
1613 * If there are none, then it advances to the last container.
1615 protected function nextShardIteratorIfNotValid() {
1616 while ( !$this->valid() ) {
1617 if ( ++
$this->curShard
>= count( $this->shardSuffixes
) ) {
1618 break; // no more container shards
1620 $this->setIteratorFromCurrentShard();
1624 protected function setIteratorFromCurrentShard() {
1625 $suffix = $this->shardSuffixes
[$this->curShard
];
1626 $this->iter
= $this->backend
->getFileListInternal(
1627 "{$this->container}{$suffix}", $this->directory
, $this->params
);
1630 public function rewind() {
1632 $this->curShard
= 0;
1633 $this->setIteratorFromCurrentShard();
1634 // Find the next non-empty shard if this one has no elements
1635 $this->nextShardIteratorIfNotValid();
1638 public function valid() {
1639 if ( $this->iter
== null ) {
1640 return false; // some failure?
1641 } elseif ( is_array( $this->iter
) ) {
1642 return ( current( $this->iter
) !== false ); // no paths can have this value
1644 return $this->iter
->valid();