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 $this->name
= $config['name'];
55 $this->wikiId
= isset( $config['wikiId'] )
57 : wfWikiID(); // e.g. "my_wiki-en_"
58 $this->wikiId
= $this->resolveWikiId( $this->wikiId
);
59 $this->lockManager
= LockManagerGroup
::singleton()->get( $config['lockManager'] );
60 $this->readOnly
= isset( $config['readOnly'] )
61 ?
(string)$config['readOnly']
66 * Normalize a wiki ID by replacing characters that are
67 * not supported by the backend as part of container names.
69 * @param $wikiId string
72 protected function resolveWikiId( $wikiId ) {
77 * Get the unique backend name.
78 * We may have multiple different backends of the same type.
79 * For example, we can have two Swift backends using different proxies.
83 final public function getName() {
88 * This is the main entry point into the backend for write operations.
89 * Callers supply an ordered list of operations to perform as a transaction.
90 * If any serious errors occur, all attempted operations will be rolled back.
92 * $ops is an array of arrays. The outer array holds a list of operations.
93 * Each inner array is a set of key value pairs that specify an operation.
95 * Supported operations and their parameters:
96 * a) Create a new file in storage with the contents of a string
99 * 'dst' => <storage path>,
100 * 'content' => <string of new file contents>,
101 * 'overwrite' => <boolean>,
102 * 'overwriteSame' => <boolean>
104 * b) Copy a file system file into storage
107 * 'src' => <file system path>,
108 * 'dst' => <storage path>,
109 * 'overwrite' => <boolean>,
110 * 'overwriteSame' => <boolean>
112 * c) Copy a file within storage
115 * 'src' => <storage path>,
116 * 'dst' => <storage path>,
117 * 'overwrite' => <boolean>,
118 * 'overwriteSame' => <boolean>
120 * d) Move a file within storage
123 * 'src' => <storage path>,
124 * 'dst' => <storage path>,
125 * 'overwrite' => <boolean>,
126 * 'overwriteSame' => <boolean>
128 * e) Delete a file within storage
131 * 'src' => <storage path>,
132 * 'ignoreMissingSource' => <boolean>
134 * f) Do nothing (no-op)
139 * Boolean flags for operations (operation-specific):
140 * 'ignoreMissingSource' : The operation will simply succeed and do
141 * nothing if the source file does not exist.
142 * 'overwrite' : Any destination file will be overwritten.
143 * 'overwriteSame' : An error will not be given if a file already
144 * exists at the destination that has the same
145 * contents as the new contents to be written there.
147 * $opts is an associative of boolean flags, including:
148 * 'force' : Errors that would normally cause a rollback do not.
149 * The remaining operations are still attempted if any fail.
150 * 'nonLocking' : No locks are acquired for the operations.
151 * This can increase performance for non-critical writes.
152 * This has no effect unless the 'force' flag is set.
153 * 'allowStale' : Don't require the latest available data.
154 * This can increase performance for non-critical writes.
155 * This has no effect unless the 'force' flag is set.
158 * File system paths given to operations should refer to files that are
159 * either locked or otherwise safe from modification from other processes.
160 * Normally these files will be new temp files, which should be adequate.
163 * This returns a Status, which contains all warnings and fatals that occured
164 * during the operation. The 'failCount', 'successCount', and 'success' members
165 * will reflect each operation attempted. The status will be "OK" unless any
166 * of the operations failed and the 'force' parameter was not set.
168 * @param $ops Array List of operations to execute in order
169 * @param $opts Array Batch operation options
172 final public function doOperations( array $ops, array $opts = array() ) {
173 if ( $this->readOnly
!= '' ) {
174 return Status
::newFatal( 'backend-fail-readonly', $this->name
, $this->readOnly
);
176 if ( empty( $opts['force'] ) ) { // sanity
177 unset( $opts['nonLocking'] );
178 unset( $opts['allowStale'] );
180 return $this->doOperationsInternal( $ops, $opts );
184 * @see FileBackendBase::doOperations()
186 abstract protected function doOperationsInternal( array $ops, array $opts );
189 * Same as doOperations() except it takes a single operation.
190 * If you are doing a batch of operations that should either
191 * all succeed or all fail, then use that function instead.
193 * @see FileBackendBase::doOperations()
195 * @param $op Array Operation
196 * @param $opts Array Operation options
199 final public function doOperation( array $op, array $opts = array() ) {
200 return $this->doOperations( array( $op ), $opts );
204 * Performs a single create operation.
205 * This sets $params['op'] to 'create' and passes it to doOperation().
207 * @see FileBackendBase::doOperation()
209 * @param $params Array Operation parameters
210 * @param $opts Array Operation options
213 final public function create( array $params, array $opts = array() ) {
214 $params['op'] = 'create';
215 return $this->doOperation( $params, $opts );
219 * Performs a single store operation.
220 * This sets $params['op'] to 'store' and passes it to doOperation().
222 * @see FileBackendBase::doOperation()
224 * @param $params Array Operation parameters
225 * @param $opts Array Operation options
228 final public function store( array $params, array $opts = array() ) {
229 $params['op'] = 'store';
230 return $this->doOperation( $params, $opts );
234 * Performs a single copy operation.
235 * This sets $params['op'] to 'copy' and passes it to doOperation().
237 * @see FileBackendBase::doOperation()
239 * @param $params Array Operation parameters
240 * @param $opts Array Operation options
243 final public function copy( array $params, array $opts = array() ) {
244 $params['op'] = 'copy';
245 return $this->doOperation( $params, $opts );
249 * Performs a single move operation.
250 * This sets $params['op'] to 'move' and passes it to doOperation().
252 * @see FileBackendBase::doOperation()
254 * @param $params Array Operation parameters
255 * @param $opts Array Operation options
258 final public function move( array $params, array $opts = array() ) {
259 $params['op'] = 'move';
260 return $this->doOperation( $params, $opts );
264 * Performs a single delete operation.
265 * This sets $params['op'] to 'delete' and passes it to doOperation().
267 * @see FileBackendBase::doOperation()
269 * @param $params Array Operation parameters
270 * @param $opts Array Operation options
273 final public function delete( array $params, array $opts = array() ) {
274 $params['op'] = 'delete';
275 return $this->doOperation( $params, $opts );
279 * Concatenate a list of storage files into a single file on the file system
281 * srcs : ordered source storage paths (e.g. chunk1, chunk2, ...)
282 * dst : file system path to 0-byte temp file
284 * @param $params Array Operation parameters
287 abstract public function concatenate( array $params );
290 * Prepare a storage directory for usage.
291 * This will create any required containers and parent directories.
292 * Backends using key/value stores only need to create the container.
295 * dir : storage directory
297 * @param $params Array
300 final public function prepare( array $params ) {
301 if ( $this->readOnly
!= '' ) {
302 return Status
::newFatal( 'backend-fail-readonly', $this->name
, $this->readOnly
);
304 return $this->doPrepare( $params );
308 * @see FileBackendBase::prepare()
310 abstract protected function doPrepare( array $params );
313 * Take measures to block web access to a storage directory and
314 * the container it belongs to. FS backends might add .htaccess
315 * files whereas key/value store backends might restrict container
316 * access to the auth user that represents end-users in web request.
317 * This is not guaranteed to actually do anything.
320 * dir : storage directory
321 * noAccess : try to deny file access
322 * noListing : try to deny file listing
324 * @param $params Array
327 final public function secure( array $params ) {
328 if ( $this->readOnly
!= '' ) {
329 return Status
::newFatal( 'backend-fail-readonly', $this->name
, $this->readOnly
);
331 $status = $this->doPrepare( $params ); // dir must exist to restrict it
332 if ( $status->isOK() ) {
333 $status->merge( $this->doSecure( $params ) );
339 * @see FileBackendBase::secure()
341 abstract protected function doSecure( array $params );
344 * Delete a storage directory if it is empty.
345 * Backends using key/value stores may do nothing unless the directory
346 * is that of an empty container, in which case it should be deleted.
349 * dir : storage directory
351 * @param $params Array
354 final public function clean( array $params ) {
355 if ( $this->readOnly
!= '' ) {
356 return Status
::newFatal( 'backend-fail-readonly', $this->name
, $this->readOnly
);
358 return $this->doClean( $params );
362 * @see FileBackendBase::clean()
364 abstract protected function doClean( array $params );
367 * Check if a file exists at a storage path in the backend.
368 * This returns false if only a directory exists at the path.
371 * src : source storage path
372 * latest : use the latest available data
374 * @param $params Array
375 * @return bool|null Returns null on failure
377 abstract public function fileExists( array $params );
380 * Get the last-modified timestamp of the file at a storage path.
383 * src : source storage path
384 * latest : use the latest available data
386 * @param $params Array
387 * @return string|false TS_MW timestamp or false on failure
389 abstract public function getFileTimestamp( array $params );
392 * Get the contents of a file at a storage path in the backend.
393 * This should be avoided for potentially large files.
396 * src : source storage path
397 * latest : use the latest available data
399 * @param $params Array
400 * @return string|false Returns false on failure
402 abstract public function getFileContents( array $params );
405 * Get the size (bytes) of a file at a storage path in the backend.
408 * src : source storage path
409 * latest : use the latest available data
411 * @param $params Array
412 * @return integer|false Returns false on failure
414 abstract public function getFileSize( array $params );
417 * Get quick information about a file at a storage path in the backend.
418 * If the file does not exist, then this returns false.
419 * Otherwise, the result is an associative array that includes:
420 * mtime : the last-modified timestamp (TS_MW)
421 * size : the file size (bytes)
422 * Additional values may be included for internal use only.
425 * src : source storage path
426 * latest : use the latest available data
428 * @param $params Array
429 * @return Array|false|null Returns null on failure
431 abstract public function getFileStat( array $params );
434 * Get a SHA-1 hash of the file at a storage path in the backend.
437 * src : source storage path
438 * latest : use the latest available data
440 * @param $params Array
441 * @return string|false Hash string or false on failure
443 abstract public function getFileSha1Base36( array $params );
446 * Get the properties of the file at a storage path in the backend.
447 * Returns FSFile::placeholderProps() on failure.
450 * src : source storage path
451 * latest : use the latest available data
453 * @param $params Array
456 abstract public function getFileProps( array $params );
459 * Stream the file at a storage path in the backend.
460 * If the file does not exists, a 404 error will be given.
461 * Appropriate HTTP headers (Status, Content-Type, Content-Length)
462 * must be sent if streaming began, while none should be sent otherwise.
463 * Implementations should flush the output buffer before sending data.
466 * src : source storage path
467 * headers : additional HTTP headers to send on success
468 * latest : use the latest available data
470 * @param $params Array
473 abstract public function streamFile( array $params );
476 * Returns a file system file, identical to the file at a storage path.
477 * The file returned is either:
478 * a) A local copy of the file at a storage path in the backend.
479 * The temporary copy will have the same extension as the source.
480 * b) An original of the file at a storage path in the backend.
481 * Temporary files may be purged when the file object falls out of scope.
483 * Write operations should *never* be done on this file as some backends
484 * may do internal tracking or may be instances of FileBackendMultiWrite.
485 * In that later case, there are copies of the file that must stay in sync.
488 * src : source storage path
489 * latest : use the latest available data
491 * @param $params Array
492 * @return FSFile|null Returns null on failure
494 abstract public function getLocalReference( array $params );
497 * Get a local copy on disk of the file at a storage path in the backend.
498 * The temporary copy will have the same file extension as the source.
499 * Temporary files may be purged when the file object falls out of scope.
502 * src : source storage path
503 * latest : use the latest available data
505 * @param $params Array
506 * @return TempFSFile|null Returns null on failure
508 abstract public function getLocalCopy( array $params );
511 * Get an iterator to list out all stored files under a storage directory.
512 * If the directory is of the form "mwstore://container", then all items in
513 * the container should be listed. If of the form "mwstore://container/dir",
514 * then all items under that container directory should be listed.
515 * Results should be storage paths relative to the given directory.
518 * dir : storage path directory
520 * @return Traversable|Array|null Returns null on failure
522 abstract public function getFileList( array $params );
525 * Invalidate any in-process file existence and property cache.
526 * If $paths is given, then only the cache for those files will be cleared.
528 * @param $paths Array Storage paths (optional)
531 abstract public function clearCache( array $paths = null );
534 * Lock the files at the given storage paths in the backend.
535 * This will either lock all the files or none (on failure).
537 * Callers should consider using getScopedFileLocks() instead.
539 * @param $paths Array Storage paths
540 * @param $type integer LockManager::LOCK_* constant
543 final public function lockFiles( array $paths, $type ) {
544 return $this->lockManager
->lock( $paths, $type );
548 * Unlock the files at the given storage paths in the backend.
550 * @param $paths Array Storage paths
551 * @param $type integer LockManager::LOCK_* constant
554 final public function unlockFiles( array $paths, $type ) {
555 return $this->lockManager
->unlock( $paths, $type );
559 * Lock the files at the given storage paths in the backend.
560 * This will either lock all the files or none (on failure).
561 * On failure, the status object will be updated with errors.
563 * Once the return value goes out scope, the locks will be released and
564 * the status updated. Unlock fatals will not change the status "OK" value.
566 * @param $paths Array Storage paths
567 * @param $type integer LockManager::LOCK_* constant
568 * @param $status Status Status to update on lock/unlock
569 * @return ScopedLock|null Returns null on failure
571 final public function getScopedFileLocks( array $paths, $type, Status
$status ) {
572 return ScopedLock
::factory( $this->lockManager
, $paths, $type, $status );
577 * Base class for all single-write backends.
578 * This class defines the methods as abstract that subclasses must implement.
579 * Callers outside of FileBackend and its helper classes, such as FileOp,
580 * should only call functions that are present in FileBackendBase.
582 * The FileBackendBase operations are implemented using primitive functions
583 * such as storeInternal(), copyInternal(), deleteInternal() and the like.
584 * This class is also responsible for path resolution and sanitization.
586 * @ingroup FileBackend
589 abstract class FileBackend
extends FileBackendBase
{
591 protected $cache = array(); // (storage path => key => value)
592 protected $maxCacheSize = 75; // integer; max paths with entries
594 protected $shardViaHashLevels = array(); // (container name => integer)
596 protected $maxFileSize = 1000000000; // integer bytes (1GB)
599 * Get the maximum allowable file size given backend
600 * medium restrictions and basic performance constraints.
601 * Do not call this function from places outside FileBackend and FileOp.
603 * @return integer Bytes
605 final public function maxFileSizeInternal() {
606 return $this->maxFileSize
;
610 * Check if a file can be created at a given storage path.
611 * FS backends should check if the parent directory exists and the file is writable.
612 * Backends using key/value stores should check if the container exists.
614 * @param $storagePath string
617 abstract public function isPathUsableInternal( $storagePath );
620 * Create a file in the backend with the given contents.
621 * Do not call this function from places outside FileBackend and FileOp.
624 * content : the raw file contents
625 * dst : destination storage path
626 * overwrite : overwrite any file that exists at the destination
628 * @param $params Array
631 final public function createInternal( array $params ) {
632 if ( strlen( $params['content'] ) > $this->maxFileSizeInternal() ) {
633 $status = Status
::newFatal( 'backend-fail-create', $params['dst'] );
635 $status = $this->doCreateInternal( $params );
636 $this->clearCache( array( $params['dst'] ) );
642 * @see FileBackend::createInternal()
644 abstract protected function doCreateInternal( array $params );
647 * Store a file into the backend from a file on disk.
648 * Do not call this function from places outside FileBackend and FileOp.
651 * src : source path on disk
652 * dst : destination storage path
653 * overwrite : overwrite any file that exists at the destination
655 * @param $params Array
658 final public function storeInternal( array $params ) {
659 if ( filesize( $params['src'] ) > $this->maxFileSizeInternal() ) {
660 $status = Status
::newFatal( 'backend-fail-store', $params['dst'] );
662 $status = $this->doStoreInternal( $params );
663 $this->clearCache( array( $params['dst'] ) );
669 * @see FileBackend::storeInternal()
671 abstract protected function doStoreInternal( array $params );
674 * Copy a file from one storage path to another in the backend.
675 * Do not call this function from places outside FileBackend and FileOp.
678 * src : source storage path
679 * dst : destination storage path
680 * overwrite : overwrite any file that exists at the destination
682 * @param $params Array
685 final public function copyInternal( array $params ) {
686 $status = $this->doCopyInternal( $params );
687 $this->clearCache( array( $params['dst'] ) );
692 * @see FileBackend::copyInternal()
694 abstract protected function doCopyInternal( array $params );
697 * Delete a file at the storage path.
698 * Do not call this function from places outside FileBackend and FileOp.
701 * src : source storage path
702 * ignoreMissingSource : do nothing if the source file does not exist
704 * @param $params Array
707 final public function deleteInternal( array $params ) {
708 $status = $this->doDeleteInternal( $params );
709 $this->clearCache( array( $params['src'] ) );
714 * @see FileBackend::deleteInternal()
716 abstract protected function doDeleteInternal( array $params );
719 * Move a file from one storage path to another in the backend.
720 * Do not call this function from places outside FileBackend and FileOp.
723 * src : source storage path
724 * dst : destination storage path
725 * overwrite : overwrite any file that exists at the destination
727 * @param $params Array
730 final public function moveInternal( array $params ) {
731 $status = $this->doMoveInternal( $params );
732 $this->clearCache( array( $params['src'], $params['dst'] ) );
737 * @see FileBackend::moveInternal()
739 protected function doMoveInternal( array $params ) {
740 // Copy source to dest
741 $status = $this->copyInternal( $params );
742 if ( !$status->isOK() ) {
745 // Delete source (only fails due to races or medium going down)
746 $status->merge( $this->deleteInternal( array( 'src' => $params['src'] ) ) );
747 $status->setResult( true, $status->value
); // ignore delete() errors
752 * @see FileBackendBase::concatenate()
754 final public function concatenate( array $params ) {
755 $status = Status
::newGood();
757 // Try to lock the source files for the scope of this function
758 $scopeLockS = $this->getScopedFileLocks( $params['srcs'], LockManager
::LOCK_UW
, $status );
759 if ( !$status->isOK() ) {
760 return $status; // abort
763 // Actually do the concatenation
764 $status->merge( $this->doConcatenate( $params ) );
770 * @see FileBackend::concatenate()
772 protected function doConcatenate( array $params ) {
773 $status = Status
::newGood();
774 $tmpPath = $params['dst']; // convenience
776 // Check that the specified temp file is valid...
777 wfSuppressWarnings();
778 $ok = ( is_file( $tmpPath ) && !filesize( $tmpPath ) );
780 if ( !$ok ) { // not present or not empty
781 $status->fatal( 'backend-fail-opentemp', $tmpPath );
785 // Build up the temp file using the source chunks (in order)...
786 $tmpHandle = fopen( $tmpPath, 'a' );
787 if ( $tmpHandle === false ) {
788 $status->fatal( 'backend-fail-opentemp', $tmpPath );
791 foreach ( $params['srcs'] as $virtualSource ) {
792 // Get a local FS version of the chunk
793 $tmpFile = $this->getLocalReference( array( 'src' => $virtualSource ) );
795 $status->fatal( 'backend-fail-read', $virtualSource );
798 // Get a handle to the local FS version
799 $sourceHandle = fopen( $tmpFile->getPath(), 'r' );
800 if ( $sourceHandle === false ) {
801 fclose( $tmpHandle );
802 $status->fatal( 'backend-fail-read', $virtualSource );
805 // Append chunk to file (pass chunk size to avoid magic quotes)
806 if ( !stream_copy_to_stream( $sourceHandle, $tmpHandle ) ) {
807 fclose( $sourceHandle );
808 fclose( $tmpHandle );
809 $status->fatal( 'backend-fail-writetemp', $tmpPath );
812 fclose( $sourceHandle );
814 if ( !fclose( $tmpHandle ) ) {
815 $status->fatal( 'backend-fail-closetemp', $tmpPath );
819 clearstatcache(); // temp file changed
825 * @see FileBackendBase::doPrepare()
827 final protected function doPrepare( array $params ) {
828 $status = Status
::newGood();
829 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
830 if ( $dir === null ) {
831 $status->fatal( 'backend-fail-invalidpath', $params['dir'] );
832 return $status; // invalid storage path
834 if ( $shard !== null ) { // confined to a single container/shard
835 $status->merge( $this->doPrepareInternal( $fullCont, $dir, $params ) );
836 } else { // directory is on several shards
837 wfDebug( __METHOD__
. ": iterating over all container shards.\n" );
838 list( $b, $shortCont, $r ) = self
::splitStoragePath( $params['dir'] );
839 foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
840 $status->merge( $this->doPrepareInternal( "{$fullCont}{$suffix}", $dir, $params ) );
847 * @see FileBackend::doPrepare()
849 protected function doPrepareInternal( $container, $dir, array $params ) {
850 return Status
::newGood();
854 * @see FileBackendBase::doSecure()
856 final protected function doSecure( array $params ) {
857 $status = Status
::newGood();
858 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
859 if ( $dir === null ) {
860 $status->fatal( 'backend-fail-invalidpath', $params['dir'] );
861 return $status; // invalid storage path
863 if ( $shard !== null ) { // confined to a single container/shard
864 $status->merge( $this->doSecureInternal( $fullCont, $dir, $params ) );
865 } else { // directory is on several shards
866 wfDebug( __METHOD__
. ": iterating over all container shards.\n" );
867 list( $b, $shortCont, $r ) = self
::splitStoragePath( $params['dir'] );
868 foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
869 $status->merge( $this->doSecureInternal( "{$fullCont}{$suffix}", $dir, $params ) );
876 * @see FileBackend::doSecure()
878 protected function doSecureInternal( $container, $dir, array $params ) {
879 return Status
::newGood();
883 * @see FileBackendBase::doClean()
885 final protected function doClean( array $params ) {
886 $status = Status
::newGood();
887 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
888 if ( $dir === null ) {
889 $status->fatal( 'backend-fail-invalidpath', $params['dir'] );
890 return $status; // invalid storage path
892 // Attempt to lock this directory...
893 $filesLockEx = array( $params['dir'] );
894 $scopedLockE = $this->getScopedFileLocks( $filesLockEx, LockManager
::LOCK_EX
, $status );
895 if ( !$status->isOK() ) {
896 return $status; // abort
898 if ( $shard !== null ) { // confined to a single container/shard
899 $status->merge( $this->doCleanInternal( $fullCont, $dir, $params ) );
900 } else { // directory is on several shards
901 wfDebug( __METHOD__
. ": iterating over all container shards.\n" );
902 list( $b, $shortCont, $r ) = self
::splitStoragePath( $params['dir'] );
903 foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
904 $status->merge( $this->doCleanInternal( "{$fullCont}{$suffix}", $dir, $params ) );
911 * @see FileBackend::doClean()
913 protected function doCleanInternal( $container, $dir, array $params ) {
914 return Status
::newGood();
918 * @see FileBackendBase::fileExists()
920 final public function fileExists( array $params ) {
921 $stat = $this->getFileStat( $params );
922 if ( $stat === null ) {
923 return null; // failure
929 * @see FileBackendBase::getFileTimestamp()
931 final public function getFileTimestamp( array $params ) {
932 $stat = $this->getFileStat( $params );
934 return $stat['mtime'];
941 * @see FileBackendBase::getFileSize()
943 final public function getFileSize( array $params ) {
944 $stat = $this->getFileStat( $params );
946 return $stat['size'];
953 * @see FileBackendBase::getFileStat()
955 final public function getFileStat( array $params ) {
956 $path = $params['src'];
957 $latest = !empty( $params['latest'] );
958 if ( isset( $this->cache
[$path]['stat'] ) ) {
959 // If we want the latest data, check that this cached
960 // value was in fact fetched with the latest available data.
961 if ( !$latest ||
$this->cache
[$path]['stat']['latest'] ) {
962 return $this->cache
[$path]['stat'];
965 $stat = $this->doGetFileStat( $params );
966 if ( is_array( $stat ) ) { // don't cache negatives
967 $this->trimCache(); // limit memory
968 $this->cache
[$path]['stat'] = $stat;
969 $this->cache
[$path]['stat']['latest'] = $latest;
975 * @see FileBackend::getFileStat()
977 abstract protected function doGetFileStat( array $params );
980 * @see FileBackendBase::getFileContents()
982 public function getFileContents( array $params ) {
983 $tmpFile = $this->getLocalReference( $params );
987 wfSuppressWarnings();
988 $data = file_get_contents( $tmpFile->getPath() );
994 * @see FileBackendBase::getFileSha1Base36()
996 final public function getFileSha1Base36( array $params ) {
997 $path = $params['src'];
998 if ( isset( $this->cache
[$path]['sha1'] ) ) {
999 return $this->cache
[$path]['sha1'];
1001 $hash = $this->doGetFileSha1Base36( $params );
1002 if ( $hash ) { // don't cache negatives
1003 $this->trimCache(); // limit memory
1004 $this->cache
[$path]['sha1'] = $hash;
1010 * @see FileBackend::getFileSha1Base36()
1012 protected function doGetFileSha1Base36( array $params ) {
1013 $fsFile = $this->getLocalReference( $params );
1017 return $fsFile->getSha1Base36();
1022 * @see FileBackendBase::getFileProps()
1024 public function getFileProps( array $params ) {
1025 $fsFile = $this->getLocalReference( $params );
1027 return FSFile
::placeholderProps();
1029 return $fsFile->getProps();
1034 * @see FileBackendBase::getLocalReference()
1036 public function getLocalReference( array $params ) {
1037 $path = $params['src'];
1038 if ( isset( $this->cache
[$path]['localRef'] ) ) {
1039 return $this->cache
[$path]['localRef'];
1041 $tmpFile = $this->getLocalCopy( $params );
1042 if ( $tmpFile ) { // don't cache negatives
1043 $this->trimCache(); // limit memory
1044 $this->cache
[$path]['localRef'] = $tmpFile;
1050 * @see FileBackendBase::streamFile()
1052 final public function streamFile( array $params ) {
1053 $status = Status
::newGood();
1055 $info = $this->getFileStat( $params );
1056 if ( !$info ) { // let StreamFile handle the 404
1057 $status->fatal( 'backend-fail-notexists', $params['src'] );
1060 // Set output buffer and HTTP headers for stream
1061 $extraHeaders = $params['headers'] ?
$params['headers'] : array();
1062 $res = StreamFile
::prepareForStream( $params['src'], $info, $extraHeaders );
1063 if ( $res == StreamFile
::NOT_MODIFIED
) {
1064 // do nothing; client cache is up to date
1065 } elseif ( $res == StreamFile
::READY_STREAM
) {
1066 $status = $this->doStreamFile( $params );
1068 $status->fatal( 'backend-fail-stream', $params['src'] );
1075 * @see FileBackend::streamFile()
1077 protected function doStreamFile( array $params ) {
1078 $status = Status
::newGood();
1080 $fsFile = $this->getLocalReference( $params );
1082 $status->fatal( 'backend-fail-stream', $params['src'] );
1083 } elseif ( !readfile( $fsFile->getPath() ) ) {
1084 $status->fatal( 'backend-fail-stream', $params['src'] );
1091 * @see FileBackendBase::getFileList()
1093 final public function getFileList( array $params ) {
1094 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
1095 if ( $dir === null ) { // invalid storage path
1098 if ( $shard !== null ) {
1099 // File listing is confined to a single container/shard
1100 return $this->getFileListInternal( $fullCont, $dir, $params );
1102 wfDebug( __METHOD__
. ": iterating over all container shards.\n" );
1103 // File listing spans multiple containers/shards
1104 list( $b, $shortCont, $r ) = self
::splitStoragePath( $params['dir'] );
1105 return new FileBackendShardListIterator( $this,
1106 $fullCont, $this->getContainerSuffixes( $shortCont ), $params );
1111 * Do not call this function from places outside FileBackend and ContainerFileListIterator
1113 * @param $container string Resolved container name
1114 * @param $dir string Resolved path relative to container
1115 * @param $params Array
1116 * @see FileBackend::getFileList()
1118 abstract public function getFileListInternal( $container, $dir, array $params );
1121 * Get the list of supported operations and their corresponding FileOp classes.
1125 protected function supportedOperations() {
1127 'store' => 'StoreFileOp',
1128 'copy' => 'CopyFileOp',
1129 'move' => 'MoveFileOp',
1130 'delete' => 'DeleteFileOp',
1131 'create' => 'CreateFileOp',
1132 'null' => 'NullFileOp'
1137 * Return a list of FileOp objects from a list of operations.
1138 * Do not call this function from places outside FileBackend.
1140 * The result must have the same number of items as the input.
1141 * An exception is thrown if an unsupported operation is requested.
1143 * @param $ops Array Same format as doOperations()
1144 * @return Array List of FileOp objects
1145 * @throws MWException
1147 final public function getOperations( array $ops ) {
1148 $supportedOps = $this->supportedOperations();
1150 $performOps = array(); // array of FileOp objects
1151 // Build up ordered array of FileOps...
1152 foreach ( $ops as $operation ) {
1153 $opName = $operation['op'];
1154 if ( isset( $supportedOps[$opName] ) ) {
1155 $class = $supportedOps[$opName];
1156 // Get params for this operation
1157 $params = $operation;
1158 // Append the FileOp class
1159 $performOps[] = new $class( $this, $params );
1161 throw new MWException( "Operation `$opName` is not supported." );
1169 * @see FileBackendBase::doOperationsInternal()
1171 protected function doOperationsInternal( array $ops, array $opts ) {
1172 $status = Status
::newGood();
1174 // Build up a list of FileOps...
1175 $performOps = $this->getOperations( $ops );
1177 // Acquire any locks as needed...
1178 if ( empty( $opts['nonLocking'] ) ) {
1179 // Build up a list of files to lock...
1180 $filesLockEx = $filesLockSh = array();
1181 foreach ( $performOps as $fileOp ) {
1182 $filesLockSh = array_merge( $filesLockSh, $fileOp->storagePathsRead() );
1183 $filesLockEx = array_merge( $filesLockEx, $fileOp->storagePathsChanged() );
1185 // Optimization: if doing an EX lock anyway, don't also set an SH one
1186 $filesLockSh = array_diff( $filesLockSh, $filesLockEx );
1187 // Get a shared lock on the parent directory of each path changed
1188 $filesLockSh = array_merge( $filesLockSh, array_map( 'dirname', $filesLockEx ) );
1189 // Try to lock those files for the scope of this function...
1190 $scopeLockS = $this->getScopedFileLocks( $filesLockSh, LockManager
::LOCK_UW
, $status );
1191 $scopeLockE = $this->getScopedFileLocks( $filesLockEx, LockManager
::LOCK_EX
, $status );
1192 if ( !$status->isOK() ) {
1193 return $status; // abort
1197 // Clear any cache entries (after locks acquired)
1198 $this->clearCache();
1200 // Actually attempt the operation batch...
1201 $subStatus = FileOp
::attemptBatch( $performOps, $opts );
1203 // Merge errors into status fields
1204 $status->merge( $subStatus );
1205 $status->success
= $subStatus->success
; // not done in merge()
1211 * @see FileBackendBase::clearCache()
1213 final public function clearCache( array $paths = null ) {
1214 if ( $paths === null ) {
1215 $this->cache
= array();
1217 foreach ( $paths as $path ) {
1218 unset( $this->cache
[$path] );
1221 $this->doClearCache( $paths );
1225 * Clears any additional stat caches for storage paths
1227 * @see FileBackendBase::clearCache()
1229 * @param $paths Array Storage paths (optional)
1232 protected function doClearCache( array $paths = null ) {}
1235 * Prune the cache if it is too big to add an item
1239 protected function trimCache() {
1240 if ( count( $this->cache
) >= $this->maxCacheSize
) {
1241 reset( $this->cache
);
1242 $key = key( $this->cache
);
1243 unset( $this->cache
[$key] );
1248 * Get the parent storage directory of a storage path.
1249 * This returns a path like "mwstore://backend/container",
1250 * "mwstore://backend/container/...", or false if there is no parent.
1252 * @param $storagePath string
1253 * @return string|false
1255 final public static function parentStoragePath( $storagePath ) {
1256 $storagePath = dirname( $storagePath );
1257 list( $b, $cont, $rel ) = self
::splitStoragePath( $storagePath );
1258 return ( $rel === null ) ?
null : $storagePath;
1262 * Check if a given path is a mwstore:// path.
1263 * This does not do any actual validation or existence checks.
1265 * @param $path string
1268 final public static function isStoragePath( $path ) {
1269 return ( strpos( $path, 'mwstore://' ) === 0 );
1273 * Split a storage path (e.g. "mwstore://backend/container/path/to/object")
1274 * into a backend name, a container name, and a relative object path.
1276 * @param $storagePath string
1277 * @return Array (backend, container, rel object) or (null, null, null)
1279 final public static function splitStoragePath( $storagePath ) {
1280 if ( self
::isStoragePath( $storagePath ) ) {
1281 // Note: strlen( 'mwstore://' ) = 10
1282 $parts = explode( '/', substr( $storagePath, 10 ), 3 );
1283 if ( count( $parts ) == 3 ) {
1284 return $parts; // e.g. "backend/container/path"
1285 } elseif ( count( $parts ) == 2 ) {
1286 return array( $parts[0], $parts[1], '' ); // e.g. "backend/container"
1289 return array( null, null, null );
1293 * Check if a container name is valid.
1294 * This checks for for length and illegal characters.
1296 * @param $container string
1299 final protected static function isValidContainerName( $container ) {
1300 // This accounts for Swift and S3 restrictions while leaving room
1301 // for things like '.xxx' (hex shard chars) or '.seg' (segments).
1302 // Note that matching strings URL encode to the same string;
1303 // in Swift, the length resriction is *after* URL encoding.
1304 return preg_match( '/^[a-z0-9][a-z0-9-_]{0,199}$/i', $container );
1308 * Validate and normalize a relative storage path.
1309 * Null is returned if the path involves directory traversal.
1310 * Traversal is insecure for FS backends and broken for others.
1312 * @param $path string Storage path relative to a container
1313 * @return string|null
1315 final protected static function normalizeContainerPath( $path ) {
1316 // Normalize directory separators
1317 $path = strtr( $path, '\\', '/' );
1318 // Collapse consecutive directory separators
1319 $path = preg_replace( '![/]{2,}!', '/', $path );
1320 // Use the same traversal protection as Title::secureAndSplit()
1321 if ( strpos( $path, '.' ) !== false ) {
1325 strpos( $path, './' ) === 0 ||
1326 strpos( $path, '../' ) === 0 ||
1327 strpos( $path, '/./' ) !== false ||
1328 strpos( $path, '/../' ) !== false
1337 * Splits a storage path into an internal container name,
1338 * an internal relative file name, and a container shard suffix.
1339 * Any shard suffix is already appended to the internal container name.
1340 * This also checks that the storage path is valid and within this backend.
1342 * If the container is sharded but a suffix could not be determined,
1343 * this means that the path can only refer to a directory and can only
1344 * be scanned by looking in all the container shards.
1346 * @param $storagePath string
1347 * @return Array (container, path, container suffix) or (null, null, null) if invalid
1349 final protected function resolveStoragePath( $storagePath ) {
1350 list( $backend, $container, $relPath ) = self
::splitStoragePath( $storagePath );
1351 if ( $backend === $this->name
) { // must be for this backend
1352 $relPath = self
::normalizeContainerPath( $relPath );
1353 if ( $relPath !== null ) {
1354 // Get shard for the normalized path if this container is sharded
1355 $cShard = $this->getContainerShard( $container, $relPath );
1356 // Validate and sanitize the relative path (backend-specific)
1357 $relPath = $this->resolveContainerPath( $container, $relPath );
1358 if ( $relPath !== null ) {
1359 // Prepend any wiki ID prefix to the container name
1360 $container = $this->fullContainerName( $container );
1361 if ( self
::isValidContainerName( $container ) ) {
1362 // Validate and sanitize the container name (backend-specific)
1363 $container = $this->resolveContainerName( "{$container}{$cShard}" );
1364 if ( $container !== null ) {
1365 return array( $container, $relPath, $cShard );
1371 return array( null, null, null );
1375 * Like resolveStoragePath() except null values are returned if
1376 * the container is sharded and the shard could not be determined.
1378 * @see FileBackend::resolveStoragePath()
1380 * @param $storagePath string
1381 * @return Array (container, path) or (null, null) if invalid
1383 final protected function resolveStoragePathReal( $storagePath ) {
1384 list( $container, $relPath, $cShard ) = $this->resolveStoragePath( $storagePath );
1385 if ( $cShard !== null ) {
1386 return array( $container, $relPath );
1388 return array( null, null );
1392 * Get the container name shard suffix for a given path.
1393 * Any empty suffix means the container is not sharded.
1395 * @param $container string Container name
1396 * @param $relStoragePath string Storage path relative to the container
1397 * @return string|null Returns null if shard could not be determined
1399 final protected function getContainerShard( $container, $relPath ) {
1400 $hashLevels = $this->getContainerHashLevels( $container );
1401 if ( $hashLevels === 1 ) { // 16 shards per container
1402 $hashDirRegex = '(?P<shard>[0-9a-f])';
1403 } elseif ( $hashLevels === 2 ) { // 256 shards per container
1404 $hashDirRegex = '[0-9a-f]/(?P<shard>[0-9a-f]{2})';
1406 return ''; // no sharding
1408 // Allow certain directories to be above the hash dirs so as
1409 // to work with FileRepo (e.g. "archive/a/ab" or "temp/a/ab").
1410 // They must be 2+ chars to avoid any hash directory ambiguity.
1411 if ( preg_match( "!^(?:[^/]{2,}/)*$hashDirRegex(?:/|$)!", $relPath, $m ) ) {
1412 return '.' . str_pad( $m['shard'], $hashLevels, '0', STR_PAD_LEFT
);
1414 return null; // failed to match
1418 * Get the number of hash levels for a container.
1419 * If greater than 0, then all file storage paths within
1420 * the container are required to be hashed accordingly.
1422 * @param $container string
1425 final protected function getContainerHashLevels( $container ) {
1426 if ( isset( $this->shardViaHashLevels
[$container] ) ) {
1427 $hashLevels = (int)$this->shardViaHashLevels
[$container];
1428 if ( $hashLevels >= 0 && $hashLevels <= 2 ) {
1432 return 0; // no sharding
1436 * Get a list of full container shard suffixes for a container
1438 * @param $container string
1441 final protected function getContainerSuffixes( $container ) {
1443 $digits = $this->getContainerHashLevels( $container );
1444 if ( $digits > 0 ) {
1445 $numShards = 1 << ( $digits * 4 );
1446 for ( $index = 0; $index < $numShards; $index++
) {
1447 $shards[] = '.' . str_pad( dechex( $index ), $digits, '0', STR_PAD_LEFT
);
1454 * Get the full container name, including the wiki ID prefix
1456 * @param $container string
1459 final protected function fullContainerName( $container ) {
1460 if ( $this->wikiId
!= '' ) {
1461 return "{$this->wikiId}-$container";
1468 * Resolve a container name, checking if it's allowed by the backend.
1469 * This is intended for internal use, such as encoding illegal chars.
1470 * Subclasses can override this to be more restrictive.
1472 * @param $container string
1473 * @return string|null
1475 protected function resolveContainerName( $container ) {
1480 * Resolve a relative storage path, checking if it's allowed by the backend.
1481 * This is intended for internal use, such as encoding illegal chars or perhaps
1482 * getting absolute paths (e.g. FS based backends). Note that the relative path
1483 * may be the empty string (e.g. the path is simply to the container).
1485 * @param $container string Container name
1486 * @param $relStoragePath string Storage path relative to the container
1487 * @return string|null Path or null if not valid
1489 protected function resolveContainerPath( $container, $relStoragePath ) {
1490 return $relStoragePath;
1494 * Get the final extension from a storage or FS path
1496 * @param $path string
1499 final public static function extensionFromPath( $path ) {
1500 $i = strrpos( $path, '.' );
1501 return strtolower( $i ?
substr( $path, $i +
1 ) : '' );
1506 * FileBackend helper function to handle file listings that span container shards.
1507 * Do not use this class from places outside of FileBackend.
1509 * @ingroup FileBackend
1511 class FileBackendShardListIterator
implements Iterator
{
1512 /* @var FileBackend */
1517 protected $shardSuffixes;
1518 protected $container; // string
1519 protected $directory; // string
1521 /* @var Traversable */
1523 protected $curShard = 0; // integer
1524 protected $pos = 0; // integer
1527 * @param $backend FileBackend
1528 * @param $container string Full storage container name
1529 * @param $dir string Storage directory relative to container
1530 * @param $suffixes Array List of container shard suffixes
1531 * @param $params Array
1533 public function __construct(
1534 FileBackend
$backend, $container, $dir, array $suffixes, array $params
1536 $this->backend
= $backend;
1537 $this->container
= $container;
1538 $this->directory
= $dir;
1539 $this->shardSuffixes
= $suffixes;
1540 $this->params
= $params;
1543 public function current() {
1544 if ( is_array( $this->iter
) ) {
1545 return current( $this->iter
);
1547 return $this->iter
->current();
1551 public function key() {
1555 public function next() {
1557 if ( is_array( $this->iter
) ) {
1558 next( $this->iter
);
1560 $this->iter
->next();
1562 // Find the next non-empty shard if no elements are left
1563 $this->nextShardIteratorIfNotValid();
1567 * If the iterator for this container shard is out of items,
1568 * then move on to the next container that has items.
1569 * If there are none, then it advances to the last container.
1571 protected function nextShardIteratorIfNotValid() {
1572 while ( !$this->valid() ) {
1573 if ( ++
$this->curShard
>= count( $this->shardSuffixes
) ) {
1574 break; // no more container shards
1576 $this->setIteratorFromCurrentShard();
1580 protected function setIteratorFromCurrentShard() {
1581 $suffix = $this->shardSuffixes
[$this->curShard
];
1582 $this->iter
= $this->backend
->getFileListInternal(
1583 "{$this->container}{$suffix}", $this->directory
, $this->params
);
1586 public function rewind() {
1588 $this->curShard
= 0;
1589 $this->setIteratorFromCurrentShard();
1590 // Find the next non-empty shard if this one has no elements
1591 $this->nextShardIteratorIfNotValid();
1594 public function valid() {
1595 if ( $this->iter
== null ) {
1596 return false; // some failure?
1597 } elseif ( is_array( $this->iter
) ) {
1598 return ( current( $this->iter
) !== false ); // no paths can have this value
1600 return $this->iter
->valid();