3 * @defgroup FileBackend File backend
6 * This module regroup classes meant for MediaWiki to interacts with
11 * @ingroup FileBackend
12 * @author Aaron Schulz
16 * Base class for all file backend classes (including multi-write backends).
18 * This class defines the methods as abstract that subclasses must implement.
19 * Outside callers can assume that all backends will have these functions.
21 * All "storage paths" are of the format "mwstore://backend/container/path".
22 * The paths use UNIX file system (FS) notation, though any particular backend may
23 * not actually be using a local filesystem. Therefore, the paths are only virtual.
25 * Backend contents are stored under wiki-specific container names by default.
26 * For legacy reasons, this has no effect for the FS backend class, and per-wiki
27 * segregation must be done by setting the container paths appropriately.
29 * FS-based backends are somewhat more restrictive due to the existence of real
30 * directory files; a regular file cannot have the same name as a directory. Other
31 * backends with virtual directories may not have this limitation. Callers should
32 * store files in such a way that no files and directories are under the same path.
34 * Methods should avoid throwing exceptions at all costs.
35 * As a corollary, external dependencies should be kept to a minimum.
37 * @ingroup FileBackend
40 abstract class FileBackend
{
41 protected $name; // string; unique backend name
42 protected $wikiId; // string; unique wiki name
43 protected $readOnly; // string; read-only explanation message
44 /** @var LockManager */
45 protected $lockManager;
48 * Create a new backend instance from configuration.
49 * This should only be called from within FileBackendGroup.
52 * 'name' : The unique name of this backend.
53 * This should consist of alphanumberic, '-', and '_' characters.
54 * 'wikiId' : Prefix to container names that is unique to this wiki.
55 * This should consist of alphanumberic, '-', and '_' characters.
56 * 'lockManager' : Registered name of a file lock manager to use.
57 * 'readOnly' : Write operations are disallowed if this is a non-empty string.
58 * It should be an explanation for the backend being read-only.
60 * @param $config Array
62 public function __construct( array $config ) {
63 $this->name
= $config['name'];
64 $this->wikiId
= isset( $config['wikiId'] )
66 : wfWikiID(); // e.g. "my_wiki-en_"
67 $this->lockManager
= ( $config['lockManager'] instanceof LockManager
)
68 ?
$config['lockManager']
69 : LockManagerGroup
::singleton()->get( $config['lockManager'] );
70 $this->readOnly
= isset( $config['readOnly'] )
71 ?
(string)$config['readOnly']
76 * Get the unique backend name.
77 * We may have multiple different backends of the same type.
78 * For example, we can have two Swift backends using different proxies.
82 final public function getName() {
87 * Check if this backend is read-only
91 final public function isReadOnly() {
92 return ( $this->readOnly
!= '' );
96 * Get an explanatory message if this backend is read-only
98 * @return string|false Returns falls if the backend is not read-only
100 final public function getReadOnlyReason() {
101 return ( $this->readOnly
!= '' ) ?
$this->readOnly
: false;
105 * This is the main entry point into the backend for write operations.
106 * Callers supply an ordered list of operations to perform as a transaction.
107 * Files will be locked, the stat cache cleared, and then the operations attempted.
108 * If any serious errors occur, all attempted operations will be rolled back.
110 * $ops is an array of arrays. The outer array holds a list of operations.
111 * Each inner array is a set of key value pairs that specify an operation.
113 * Supported operations and their parameters:
114 * a) Create a new file in storage with the contents of a string
117 * 'dst' => <storage path>,
118 * 'content' => <string of new file contents>,
119 * 'overwrite' => <boolean>,
120 * 'overwriteSame' => <boolean>
122 * b) Copy a file system file into storage
125 * 'src' => <file system path>,
126 * 'dst' => <storage path>,
127 * 'overwrite' => <boolean>,
128 * 'overwriteSame' => <boolean>
130 * c) Copy a file within storage
133 * 'src' => <storage path>,
134 * 'dst' => <storage path>,
135 * 'overwrite' => <boolean>,
136 * 'overwriteSame' => <boolean>
138 * d) Move a file within storage
141 * 'src' => <storage path>,
142 * 'dst' => <storage path>,
143 * 'overwrite' => <boolean>,
144 * 'overwriteSame' => <boolean>
146 * e) Delete a file within storage
149 * 'src' => <storage path>,
150 * 'ignoreMissingSource' => <boolean>
152 * f) Do nothing (no-op)
157 * Boolean flags for operations (operation-specific):
158 * 'ignoreMissingSource' : The operation will simply succeed and do
159 * nothing if the source file does not exist.
160 * 'overwrite' : Any destination file will be overwritten.
161 * 'overwriteSame' : An error will not be given if a file already
162 * exists at the destination that has the same
163 * contents as the new contents to be written there.
165 * $opts is an associative of boolean flags, including:
166 * 'force' : Errors that would normally cause a rollback do not.
167 * The remaining operations are still attempted if any fail.
168 * 'nonLocking' : No locks are acquired for the operations.
169 * This can increase performance for non-critical writes.
170 * This has no effect unless the 'force' flag is set.
171 * 'allowStale' : Don't require the latest available data.
172 * This can increase performance for non-critical writes.
173 * This has no effect unless the 'force' flag is set.
175 * Remarks on locking:
176 * File system paths given to operations should refer to files that are
177 * already locked or otherwise safe from modification from other processes.
178 * Normally these files will be new temp files, which should be adequate.
181 * This returns a Status, which contains all warnings and fatals that occured
182 * during the operation. The 'failCount', 'successCount', and 'success' members
183 * will reflect each operation attempted. The status will be "OK" unless any
184 * of the operations failed and the 'force' parameter was not set.
186 * @param $ops Array List of operations to execute in order
187 * @param $opts Array Batch operation options
190 final public function doOperations( array $ops, array $opts = array() ) {
191 if ( $this->isReadOnly() ) {
192 return Status
::newFatal( 'backend-fail-readonly', $this->name
, $this->readOnly
);
194 if ( empty( $opts['force'] ) ) { // sanity
195 unset( $opts['nonLocking'] );
196 unset( $opts['allowStale'] );
198 return $this->doOperationsInternal( $ops, $opts );
202 * @see FileBackend::doOperations()
204 abstract protected function doOperationsInternal( array $ops, array $opts );
207 * Same as doOperations() except it takes a single operation.
208 * If you are doing a batch of operations that should either
209 * all succeed or all fail, then use that function instead.
211 * @see FileBackend::doOperations()
213 * @param $op Array Operation
214 * @param $opts Array Operation options
217 final public function doOperation( array $op, array $opts = array() ) {
218 return $this->doOperations( array( $op ), $opts );
222 * Performs a single create operation.
223 * This sets $params['op'] to 'create' and passes it to doOperation().
225 * @see FileBackend::doOperation()
227 * @param $params Array Operation parameters
228 * @param $opts Array Operation options
231 final public function create( array $params, array $opts = array() ) {
232 $params['op'] = 'create';
233 return $this->doOperation( $params, $opts );
237 * Performs a single store operation.
238 * This sets $params['op'] to 'store' and passes it to doOperation().
240 * @see FileBackend::doOperation()
242 * @param $params Array Operation parameters
243 * @param $opts Array Operation options
246 final public function store( array $params, array $opts = array() ) {
247 $params['op'] = 'store';
248 return $this->doOperation( $params, $opts );
252 * Performs a single copy operation.
253 * This sets $params['op'] to 'copy' and passes it to doOperation().
255 * @see FileBackend::doOperation()
257 * @param $params Array Operation parameters
258 * @param $opts Array Operation options
261 final public function copy( array $params, array $opts = array() ) {
262 $params['op'] = 'copy';
263 return $this->doOperation( $params, $opts );
267 * Performs a single move operation.
268 * This sets $params['op'] to 'move' and passes it to doOperation().
270 * @see FileBackend::doOperation()
272 * @param $params Array Operation parameters
273 * @param $opts Array Operation options
276 final public function move( array $params, array $opts = array() ) {
277 $params['op'] = 'move';
278 return $this->doOperation( $params, $opts );
282 * Performs a single delete operation.
283 * This sets $params['op'] to 'delete' and passes it to doOperation().
285 * @see FileBackend::doOperation()
287 * @param $params Array Operation parameters
288 * @param $opts Array Operation options
291 final public function delete( array $params, array $opts = array() ) {
292 $params['op'] = 'delete';
293 return $this->doOperation( $params, $opts );
297 * Concatenate a list of storage files into a single file system file.
298 * The target path should refer to a file that is already locked or
299 * otherwise safe from modification from other processes. Normally,
300 * the file will be a new temp file, which should be adequate.
302 * srcs : ordered source storage paths (e.g. chunk1, chunk2, ...)
303 * dst : file system path to 0-byte temp file
305 * @param $params Array Operation parameters
308 abstract public function concatenate( array $params );
311 * Prepare a storage directory for usage.
312 * This will create any required containers and parent directories.
313 * Backends using key/value stores only need to create the container.
316 * dir : storage directory
318 * @param $params Array
321 final public function prepare( array $params ) {
322 if ( $this->isReadOnly() ) {
323 return Status
::newFatal( 'backend-fail-readonly', $this->name
, $this->readOnly
);
325 return $this->doPrepare( $params );
329 * @see FileBackend::prepare()
331 abstract protected function doPrepare( array $params );
334 * Take measures to block web access to a storage directory and
335 * the container it belongs to. FS backends might add .htaccess
336 * files whereas key/value store backends might restrict container
337 * access to the auth user that represents end-users in web request.
338 * This is not guaranteed to actually do anything.
341 * dir : storage directory
342 * noAccess : try to deny file access
343 * noListing : try to deny file listing
345 * @param $params Array
348 final public function secure( array $params ) {
349 if ( $this->isReadOnly() ) {
350 return Status
::newFatal( 'backend-fail-readonly', $this->name
, $this->readOnly
);
352 $status = $this->doPrepare( $params ); // dir must exist to restrict it
353 if ( $status->isOK() ) {
354 $status->merge( $this->doSecure( $params ) );
360 * @see FileBackend::secure()
362 abstract protected function doSecure( array $params );
365 * Delete a storage directory if it is empty.
366 * Backends using key/value stores may do nothing unless the directory
367 * is that of an empty container, in which case it should be deleted.
370 * dir : storage directory
372 * @param $params Array
375 final public function clean( array $params ) {
376 if ( $this->isReadOnly() ) {
377 return Status
::newFatal( 'backend-fail-readonly', $this->name
, $this->readOnly
);
379 return $this->doClean( $params );
383 * @see FileBackend::clean()
385 abstract protected function doClean( array $params );
388 * Check if a file exists at a storage path in the backend.
389 * This returns false if only a directory exists at the path.
392 * src : source storage path
393 * latest : use the latest available data
395 * @param $params Array
396 * @return bool|null Returns null on failure
398 abstract public function fileExists( array $params );
401 * Get the last-modified timestamp of the file at a storage path.
404 * src : source storage path
405 * latest : use the latest available data
407 * @param $params Array
408 * @return string|false TS_MW timestamp or false on failure
410 abstract public function getFileTimestamp( array $params );
413 * Get the contents of a file at a storage path in the backend.
414 * This should be avoided for potentially large files.
417 * src : source storage path
418 * latest : use the latest available data
420 * @param $params Array
421 * @return string|false Returns false on failure
423 abstract public function getFileContents( array $params );
426 * Get the size (bytes) of a file at a storage path in the backend.
429 * src : source storage path
430 * latest : use the latest available data
432 * @param $params Array
433 * @return integer|false Returns false on failure
435 abstract public function getFileSize( array $params );
438 * Get quick information about a file at a storage path in the backend.
439 * If the file does not exist, then this returns false.
440 * Otherwise, the result is an associative array that includes:
441 * mtime : the last-modified timestamp (TS_MW)
442 * size : the file size (bytes)
443 * Additional values may be included for internal use only.
446 * src : source storage path
447 * latest : use the latest available data
449 * @param $params Array
450 * @return Array|false|null Returns null on failure
452 abstract public function getFileStat( array $params );
455 * Get a SHA-1 hash of the file at a storage path in the backend.
458 * src : source storage path
459 * latest : use the latest available data
461 * @param $params Array
462 * @return string|false Hash string or false on failure
464 abstract public function getFileSha1Base36( array $params );
467 * Get the properties of the file at a storage path in the backend.
468 * Returns FSFile::placeholderProps() on failure.
471 * src : source storage path
472 * latest : use the latest available data
474 * @param $params Array
477 abstract public function getFileProps( array $params );
480 * Stream the file at a storage path in the backend.
481 * If the file does not exists, a 404 error will be given.
482 * Appropriate HTTP headers (Status, Content-Type, Content-Length)
483 * must be sent if streaming began, while none should be sent otherwise.
484 * Implementations should flush the output buffer before sending data.
487 * src : source storage path
488 * headers : additional HTTP headers to send on success
489 * latest : use the latest available data
491 * @param $params Array
494 abstract public function streamFile( array $params );
497 * Returns a file system file, identical to the file at a storage path.
498 * The file returned is either:
499 * a) A local copy of the file at a storage path in the backend.
500 * The temporary copy will have the same extension as the source.
501 * b) An original of the file at a storage path in the backend.
502 * Temporary files may be purged when the file object falls out of scope.
504 * Write operations should *never* be done on this file as some backends
505 * may do internal tracking or may be instances of FileBackendMultiWrite.
506 * In that later case, there are copies of the file that must stay in sync.
507 * Additionally, further calls to this function may return the same file.
510 * src : source storage path
511 * latest : use the latest available data
513 * @param $params Array
514 * @return FSFile|null Returns null on failure
516 abstract public function getLocalReference( array $params );
519 * Get a local copy on disk of the file at a storage path in the backend.
520 * The temporary copy will have the same file extension as the source.
521 * Temporary files may be purged when the file object falls out of scope.
524 * src : source storage path
525 * latest : use the latest available data
527 * @param $params Array
528 * @return TempFSFile|null Returns null on failure
530 abstract public function getLocalCopy( array $params );
533 * Get an iterator to list out all stored files under a storage directory.
534 * If the directory is of the form "mwstore://backend/container",
535 * then all files in the container should be listed.
536 * If the directory is of form "mwstore://backend/container/dir",
537 * then all files under that container directory should be listed.
538 * Results should be storage paths relative to the given directory.
540 * Storage backends with eventual consistency might return stale data.
543 * dir : storage path directory
545 * @return Traversable|Array|null Returns null on failure
547 abstract public function getFileList( array $params );
550 * Invalidate any in-process file existence and property cache.
551 * If $paths is given, then only the cache for those files will be cleared.
553 * @param $paths Array Storage paths (optional)
556 public function clearCache( array $paths = null ) {}
559 * Lock the files at the given storage paths in the backend.
560 * This will either lock all the files or none (on failure).
562 * Callers should consider using getScopedFileLocks() instead.
564 * @param $paths Array Storage paths
565 * @param $type integer LockManager::LOCK_* constant
568 final public function lockFiles( array $paths, $type ) {
569 return $this->lockManager
->lock( $paths, $type );
573 * Unlock the files at the given storage paths in the backend.
575 * @param $paths Array Storage paths
576 * @param $type integer LockManager::LOCK_* constant
579 final public function unlockFiles( array $paths, $type ) {
580 return $this->lockManager
->unlock( $paths, $type );
584 * Lock the files at the given storage paths in the backend.
585 * This will either lock all the files or none (on failure).
586 * On failure, the status object will be updated with errors.
588 * Once the return value goes out scope, the locks will be released and
589 * the status updated. Unlock fatals will not change the status "OK" value.
591 * @param $paths Array Storage paths
592 * @param $type integer LockManager::LOCK_* constant
593 * @param $status Status Status to update on lock/unlock
594 * @return ScopedLock|null Returns null on failure
596 final public function getScopedFileLocks( array $paths, $type, Status
$status ) {
597 return ScopedLock
::factory( $this->lockManager
, $paths, $type, $status );
601 * Check if a given path is a "mwstore://" path.
602 * This does not do any further validation or any existence checks.
604 * @param $path string
607 final public static function isStoragePath( $path ) {
608 return ( strpos( $path, 'mwstore://' ) === 0 );
612 * Split a storage path into a backend name, a container name,
613 * and a relative file path. The relative path may be the empty string.
614 * This does not do any path normalization or traversal checks.
616 * @param $storagePath string
617 * @return Array (backend, container, rel object) or (null, null, null)
619 final public static function splitStoragePath( $storagePath ) {
620 if ( self
::isStoragePath( $storagePath ) ) {
621 // Remove the "mwstore://" prefix and split the path
622 $parts = explode( '/', substr( $storagePath, 10 ), 3 );
623 if ( count( $parts ) >= 2 && $parts[0] != '' && $parts[1] != '' ) {
624 if ( count( $parts ) == 3 ) {
625 return $parts; // e.g. "backend/container/path"
627 return array( $parts[0], $parts[1], '' ); // e.g. "backend/container"
631 return array( null, null, null );
635 * Normalize a storage path by cleaning up directory separators.
636 * Returns null if the path is not of the format of a valid storage path.
638 * @param $storagePath string
639 * @return string|null
641 final public static function normalizeStoragePath( $storagePath ) {
642 list( $backend, $container, $relPath ) = self
::splitStoragePath( $storagePath );
643 if ( $relPath !== null ) { // must be for this backend
644 $relPath = self
::normalizeContainerPath( $relPath );
645 if ( $relPath !== null ) {
646 return ( $relPath != '' )
647 ?
"mwstore://{$backend}/{$container}/{$relPath}"
648 : "mwstore://{$backend}/{$container}";
655 * Validate and normalize a relative storage path.
656 * Null is returned if the path involves directory traversal.
657 * Traversal is insecure for FS backends and broken for others.
659 * @param $path string Storage path relative to a container
660 * @return string|null
662 final protected static function normalizeContainerPath( $path ) {
663 // Normalize directory separators
664 $path = strtr( $path, '\\', '/' );
665 // Collapse any consecutive directory separators
666 $path = preg_replace( '![/]{2,}!', '/', $path );
667 // Remove any leading directory separator
668 $path = ltrim( $path, '/' );
669 // Use the same traversal protection as Title::secureAndSplit()
670 if ( strpos( $path, '.' ) !== false ) {
674 strpos( $path, './' ) === 0 ||
675 strpos( $path, '../' ) === 0 ||
676 strpos( $path, '/./' ) !== false ||
677 strpos( $path, '/../' ) !== false
686 * Get the parent storage directory of a storage path.
687 * This returns a path like "mwstore://backend/container",
688 * "mwstore://backend/container/...", or null if there is no parent.
690 * @param $storagePath string
691 * @return string|null
693 final public static function parentStoragePath( $storagePath ) {
694 $storagePath = dirname( $storagePath );
695 list( $b, $cont, $rel ) = self
::splitStoragePath( $storagePath );
696 return ( $rel === null ) ?
null : $storagePath;
700 * Get the final extension from a storage or FS path
702 * @param $path string
705 final public static function extensionFromPath( $path ) {
706 $i = strrpos( $path, '.' );
707 return strtolower( $i ?
substr( $path, $i +
1 ) : '' );
712 * @brief Base class for all backends associated with a particular storage medium.
714 * This class defines the methods as abstract that subclasses must implement.
715 * Outside callers should *not* use functions with "Internal" in the name.
717 * The FileBackend operations are implemented using basic functions
718 * such as storeInternal(), copyInternal(), deleteInternal() and the like.
719 * This class is also responsible for path resolution and sanitization.
721 * @ingroup FileBackend
724 abstract class FileBackendStore
extends FileBackend
{
725 /** @var Array Map of paths to small (RAM/disk) cache items */
726 protected $cache = array(); // (storage path => key => value)
727 protected $maxCacheSize = 100; // integer; max paths with entries
728 /** @var Array Map of paths to large (RAM/disk) cache items */
729 protected $expensiveCache = array(); // (storage path => key => value)
730 protected $maxExpensiveCacheSize = 10; // integer; max paths with entries
732 /** @var Array Map of container names to sharding settings */
733 protected $shardViaHashLevels = array(); // (container name => config array)
735 protected $maxFileSize = 1000000000; // integer bytes (1GB)
738 * Get the maximum allowable file size given backend
739 * medium restrictions and basic performance constraints.
740 * Do not call this function from places outside FileBackend and FileOp.
742 * @return integer Bytes
744 final public function maxFileSizeInternal() {
745 return $this->maxFileSize
;
749 * Check if a file can be created at a given storage path.
750 * FS backends should check if the parent directory exists and the file is writable.
751 * Backends using key/value stores should check if the container exists.
753 * @param $storagePath string
756 abstract public function isPathUsableInternal( $storagePath );
759 * Create a file in the backend with the given contents.
760 * Do not call this function from places outside FileBackend and FileOp.
763 * content : the raw file contents
764 * dst : destination storage path
765 * overwrite : overwrite any file that exists at the destination
767 * @param $params Array
770 final public function createInternal( array $params ) {
771 wfProfileIn( __METHOD__
);
772 if ( strlen( $params['content'] ) > $this->maxFileSizeInternal() ) {
773 $status = Status
::newFatal( 'backend-fail-create', $params['dst'] );
775 $status = $this->doCreateInternal( $params );
776 $this->clearCache( array( $params['dst'] ) );
778 wfProfileOut( __METHOD__
);
783 * @see FileBackendStore::createInternal()
785 abstract protected function doCreateInternal( array $params );
788 * Store a file into the backend from a file on disk.
789 * Do not call this function from places outside FileBackend and FileOp.
792 * src : source path on disk
793 * dst : destination storage path
794 * overwrite : overwrite any file that exists at the destination
796 * @param $params Array
799 final public function storeInternal( array $params ) {
800 wfProfileIn( __METHOD__
);
801 if ( filesize( $params['src'] ) > $this->maxFileSizeInternal() ) {
802 $status = Status
::newFatal( 'backend-fail-store', $params['dst'] );
804 $status = $this->doStoreInternal( $params );
805 $this->clearCache( array( $params['dst'] ) );
807 wfProfileOut( __METHOD__
);
812 * @see FileBackendStore::storeInternal()
814 abstract protected function doStoreInternal( array $params );
817 * Copy a file from one storage path to another in the backend.
818 * Do not call this function from places outside FileBackend and FileOp.
821 * src : source storage path
822 * dst : destination storage path
823 * overwrite : overwrite any file that exists at the destination
825 * @param $params Array
828 final public function copyInternal( array $params ) {
829 wfProfileIn( __METHOD__
);
830 $status = $this->doCopyInternal( $params );
831 $this->clearCache( array( $params['dst'] ) );
832 wfProfileOut( __METHOD__
);
837 * @see FileBackendStore::copyInternal()
839 abstract protected function doCopyInternal( array $params );
842 * Delete a file at the storage path.
843 * Do not call this function from places outside FileBackend and FileOp.
846 * src : source storage path
847 * ignoreMissingSource : do nothing if the source file does not exist
849 * @param $params Array
852 final public function deleteInternal( array $params ) {
853 wfProfileIn( __METHOD__
);
854 $status = $this->doDeleteInternal( $params );
855 $this->clearCache( array( $params['src'] ) );
856 wfProfileOut( __METHOD__
);
861 * @see FileBackendStore::deleteInternal()
863 abstract protected function doDeleteInternal( array $params );
866 * Move a file from one storage path to another in the backend.
867 * Do not call this function from places outside FileBackend and FileOp.
870 * src : source storage path
871 * dst : destination storage path
872 * overwrite : overwrite any file that exists at the destination
874 * @param $params Array
877 final public function moveInternal( array $params ) {
878 wfProfileIn( __METHOD__
);
879 $status = $this->doMoveInternal( $params );
880 $this->clearCache( array( $params['src'], $params['dst'] ) );
881 wfProfileOut( __METHOD__
);
886 * @see FileBackendStore::moveInternal()
888 protected function doMoveInternal( array $params ) {
889 // Copy source to dest
890 $status = $this->copyInternal( $params );
891 if ( $status->isOK() ) {
892 // Delete source (only fails due to races or medium going down)
893 $status->merge( $this->deleteInternal( array( 'src' => $params['src'] ) ) );
894 $status->setResult( true, $status->value
); // ignore delete() errors
900 * @see FileBackend::concatenate()
902 final public function concatenate( array $params ) {
903 wfProfileIn( __METHOD__
);
904 $status = Status
::newGood();
906 // Try to lock the source files for the scope of this function
907 $scopeLockS = $this->getScopedFileLocks( $params['srcs'], LockManager
::LOCK_UW
, $status );
908 if ( $status->isOK() ) {
909 // Actually do the concatenation
910 $status->merge( $this->doConcatenate( $params ) );
913 wfProfileOut( __METHOD__
);
918 * @see FileBackendStore::concatenate()
920 protected function doConcatenate( array $params ) {
921 $status = Status
::newGood();
922 $tmpPath = $params['dst']; // convenience
924 // Check that the specified temp file is valid...
925 wfSuppressWarnings();
926 $ok = ( is_file( $tmpPath ) && !filesize( $tmpPath ) );
928 if ( !$ok ) { // not present or not empty
929 $status->fatal( 'backend-fail-opentemp', $tmpPath );
933 // Build up the temp file using the source chunks (in order)...
934 $tmpHandle = fopen( $tmpPath, 'ab' );
935 if ( $tmpHandle === false ) {
936 $status->fatal( 'backend-fail-opentemp', $tmpPath );
939 foreach ( $params['srcs'] as $virtualSource ) {
940 // Get a local FS version of the chunk
941 $tmpFile = $this->getLocalReference( array( 'src' => $virtualSource ) );
943 $status->fatal( 'backend-fail-read', $virtualSource );
946 // Get a handle to the local FS version
947 $sourceHandle = fopen( $tmpFile->getPath(), 'r' );
948 if ( $sourceHandle === false ) {
949 fclose( $tmpHandle );
950 $status->fatal( 'backend-fail-read', $virtualSource );
953 // Append chunk to file (pass chunk size to avoid magic quotes)
954 if ( !stream_copy_to_stream( $sourceHandle, $tmpHandle ) ) {
955 fclose( $sourceHandle );
956 fclose( $tmpHandle );
957 $status->fatal( 'backend-fail-writetemp', $tmpPath );
960 fclose( $sourceHandle );
962 if ( !fclose( $tmpHandle ) ) {
963 $status->fatal( 'backend-fail-closetemp', $tmpPath );
967 clearstatcache(); // temp file changed
973 * @see FileBackend::doPrepare()
975 final protected function doPrepare( array $params ) {
976 wfProfileIn( __METHOD__
);
978 $status = Status
::newGood();
979 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
980 if ( $dir === null ) {
981 $status->fatal( 'backend-fail-invalidpath', $params['dir'] );
982 wfProfileOut( __METHOD__
);
983 return $status; // invalid storage path
986 if ( $shard !== null ) { // confined to a single container/shard
987 $status->merge( $this->doPrepareInternal( $fullCont, $dir, $params ) );
988 } else { // directory is on several shards
989 wfDebug( __METHOD__
. ": iterating over all container shards.\n" );
990 list( $b, $shortCont, $r ) = self
::splitStoragePath( $params['dir'] );
991 foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
992 $status->merge( $this->doPrepareInternal( "{$fullCont}{$suffix}", $dir, $params ) );
996 wfProfileOut( __METHOD__
);
1001 * @see FileBackendStore::doPrepare()
1003 protected function doPrepareInternal( $container, $dir, array $params ) {
1004 return Status
::newGood();
1008 * @see FileBackend::doSecure()
1010 final protected function doSecure( array $params ) {
1011 wfProfileIn( __METHOD__
);
1012 $status = Status
::newGood();
1014 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
1015 if ( $dir === null ) {
1016 $status->fatal( 'backend-fail-invalidpath', $params['dir'] );
1017 wfProfileOut( __METHOD__
);
1018 return $status; // invalid storage path
1021 if ( $shard !== null ) { // confined to a single container/shard
1022 $status->merge( $this->doSecureInternal( $fullCont, $dir, $params ) );
1023 } else { // directory is on several shards
1024 wfDebug( __METHOD__
. ": iterating over all container shards.\n" );
1025 list( $b, $shortCont, $r ) = self
::splitStoragePath( $params['dir'] );
1026 foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
1027 $status->merge( $this->doSecureInternal( "{$fullCont}{$suffix}", $dir, $params ) );
1031 wfProfileOut( __METHOD__
);
1036 * @see FileBackendStore::doSecure()
1038 protected function doSecureInternal( $container, $dir, array $params ) {
1039 return Status
::newGood();
1043 * @see FileBackend::doClean()
1045 final protected function doClean( array $params ) {
1046 wfProfileIn( __METHOD__
);
1047 $status = Status
::newGood();
1049 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
1050 if ( $dir === null ) {
1051 $status->fatal( 'backend-fail-invalidpath', $params['dir'] );
1052 wfProfileOut( __METHOD__
);
1053 return $status; // invalid storage path
1056 // Attempt to lock this directory...
1057 $filesLockEx = array( $params['dir'] );
1058 $scopedLockE = $this->getScopedFileLocks( $filesLockEx, LockManager
::LOCK_EX
, $status );
1059 if ( !$status->isOK() ) {
1060 wfProfileOut( __METHOD__
);
1061 return $status; // abort
1064 if ( $shard !== null ) { // confined to a single container/shard
1065 $status->merge( $this->doCleanInternal( $fullCont, $dir, $params ) );
1066 } else { // directory is on several shards
1067 wfDebug( __METHOD__
. ": iterating over all container shards.\n" );
1068 list( $b, $shortCont, $r ) = self
::splitStoragePath( $params['dir'] );
1069 foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
1070 $status->merge( $this->doCleanInternal( "{$fullCont}{$suffix}", $dir, $params ) );
1074 wfProfileOut( __METHOD__
);
1079 * @see FileBackendStore::doClean()
1081 protected function doCleanInternal( $container, $dir, array $params ) {
1082 return Status
::newGood();
1086 * @see FileBackend::fileExists()
1088 final public function fileExists( array $params ) {
1089 wfProfileIn( __METHOD__
);
1090 $stat = $this->getFileStat( $params );
1091 wfProfileOut( __METHOD__
);
1092 return ( $stat === null ) ?
null : (bool)$stat; // null => failure
1096 * @see FileBackend::getFileTimestamp()
1098 final public function getFileTimestamp( array $params ) {
1099 wfProfileIn( __METHOD__
);
1100 $stat = $this->getFileStat( $params );
1101 wfProfileOut( __METHOD__
);
1102 return $stat ?
$stat['mtime'] : false;
1106 * @see FileBackend::getFileSize()
1108 final public function getFileSize( array $params ) {
1109 wfProfileIn( __METHOD__
);
1110 $stat = $this->getFileStat( $params );
1111 wfProfileOut( __METHOD__
);
1112 return $stat ?
$stat['size'] : false;
1116 * @see FileBackend::getFileStat()
1118 final public function getFileStat( array $params ) {
1119 wfProfileIn( __METHOD__
);
1120 $path = self
::normalizeStoragePath( $params['src'] );
1121 if ( $path === null ) {
1122 return false; // invalid storage path
1124 $latest = !empty( $params['latest'] );
1125 if ( isset( $this->cache
[$path]['stat'] ) ) {
1126 // If we want the latest data, check that this cached
1127 // value was in fact fetched with the latest available data.
1128 if ( !$latest ||
$this->cache
[$path]['stat']['latest'] ) {
1129 wfProfileOut( __METHOD__
);
1130 return $this->cache
[$path]['stat'];
1133 $stat = $this->doGetFileStat( $params );
1134 if ( is_array( $stat ) ) { // don't cache negatives
1135 $this->trimCache(); // limit memory
1136 $this->cache
[$path]['stat'] = $stat;
1137 $this->cache
[$path]['stat']['latest'] = $latest;
1139 wfProfileOut( __METHOD__
);
1144 * @see FileBackendStore::getFileStat()
1146 abstract protected function doGetFileStat( array $params );
1149 * @see FileBackend::getFileContents()
1151 public function getFileContents( array $params ) {
1152 wfProfileIn( __METHOD__
);
1153 $tmpFile = $this->getLocalReference( $params );
1155 wfProfileOut( __METHOD__
);
1158 wfSuppressWarnings();
1159 $data = file_get_contents( $tmpFile->getPath() );
1160 wfRestoreWarnings();
1161 wfProfileOut( __METHOD__
);
1166 * @see FileBackend::getFileSha1Base36()
1168 final public function getFileSha1Base36( array $params ) {
1169 wfProfileIn( __METHOD__
);
1170 $path = $params['src'];
1171 if ( isset( $this->cache
[$path]['sha1'] ) ) {
1172 wfProfileOut( __METHOD__
);
1173 return $this->cache
[$path]['sha1'];
1175 $hash = $this->doGetFileSha1Base36( $params );
1176 if ( $hash ) { // don't cache negatives
1177 $this->trimCache(); // limit memory
1178 $this->cache
[$path]['sha1'] = $hash;
1180 wfProfileOut( __METHOD__
);
1185 * @see FileBackendStore::getFileSha1Base36()
1187 protected function doGetFileSha1Base36( array $params ) {
1188 $fsFile = $this->getLocalReference( $params );
1192 return $fsFile->getSha1Base36();
1197 * @see FileBackend::getFileProps()
1199 final public function getFileProps( array $params ) {
1200 wfProfileIn( __METHOD__
);
1201 $fsFile = $this->getLocalReference( $params );
1202 $props = $fsFile ?
$fsFile->getProps() : FSFile
::placeholderProps();
1203 wfProfileOut( __METHOD__
);
1208 * @see FileBackend::getLocalReference()
1210 public function getLocalReference( array $params ) {
1211 wfProfileIn( __METHOD__
);
1212 $path = $params['src'];
1213 if ( isset( $this->expensiveCache
[$path]['localRef'] ) ) {
1214 wfProfileOut( __METHOD__
);
1215 return $this->expensiveCache
[$path]['localRef'];
1217 $tmpFile = $this->getLocalCopy( $params );
1218 if ( $tmpFile ) { // don't cache negatives
1219 $this->trimExpensiveCache(); // limit memory
1220 $this->expensiveCache
[$path]['localRef'] = $tmpFile;
1222 wfProfileOut( __METHOD__
);
1227 * @see FileBackend::streamFile()
1229 final public function streamFile( array $params ) {
1230 wfProfileIn( __METHOD__
);
1231 $status = Status
::newGood();
1233 $info = $this->getFileStat( $params );
1234 if ( !$info ) { // let StreamFile handle the 404
1235 $status->fatal( 'backend-fail-notexists', $params['src'] );
1238 // Set output buffer and HTTP headers for stream
1239 $extraHeaders = $params['headers'] ?
$params['headers'] : array();
1240 $res = StreamFile
::prepareForStream( $params['src'], $info, $extraHeaders );
1241 if ( $res == StreamFile
::NOT_MODIFIED
) {
1242 // do nothing; client cache is up to date
1243 } elseif ( $res == StreamFile
::READY_STREAM
) {
1244 $status = $this->doStreamFile( $params );
1246 $status->fatal( 'backend-fail-stream', $params['src'] );
1249 wfProfileOut( __METHOD__
);
1254 * @see FileBackendStore::streamFile()
1256 protected function doStreamFile( array $params ) {
1257 $status = Status
::newGood();
1259 $fsFile = $this->getLocalReference( $params );
1261 $status->fatal( 'backend-fail-stream', $params['src'] );
1262 } elseif ( !readfile( $fsFile->getPath() ) ) {
1263 $status->fatal( 'backend-fail-stream', $params['src'] );
1270 * @copydoc FileBackend::getFileList()
1272 final public function getFileList( array $params ) {
1273 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
1274 if ( $dir === null ) { // invalid storage path
1277 if ( $shard !== null ) {
1278 // File listing is confined to a single container/shard
1279 return $this->getFileListInternal( $fullCont, $dir, $params );
1281 wfDebug( __METHOD__
. ": iterating over all container shards.\n" );
1282 // File listing spans multiple containers/shards
1283 list( $b, $shortCont, $r ) = self
::splitStoragePath( $params['dir'] );
1284 return new FileBackendStoreShardListIterator( $this,
1285 $fullCont, $dir, $this->getContainerSuffixes( $shortCont ), $params );
1290 * Do not call this function from places outside FileBackend
1292 * @see FileBackendStore::getFileList()
1294 * @param $container string Resolved container name
1295 * @param $dir string Resolved path relative to container
1296 * @param $params Array
1297 * @return Traversable|Array|null
1299 abstract public function getFileListInternal( $container, $dir, array $params );
1302 * Get the list of supported operations and their corresponding FileOp classes.
1306 protected function supportedOperations() {
1308 'store' => 'StoreFileOp',
1309 'copy' => 'CopyFileOp',
1310 'move' => 'MoveFileOp',
1311 'delete' => 'DeleteFileOp',
1312 'create' => 'CreateFileOp',
1313 'null' => 'NullFileOp'
1318 * Return a list of FileOp objects from a list of operations.
1319 * Do not call this function from places outside FileBackend.
1321 * The result must have the same number of items as the input.
1322 * An exception is thrown if an unsupported operation is requested.
1324 * @param $ops Array Same format as doOperations()
1325 * @return Array List of FileOp objects
1326 * @throws MWException
1328 final public function getOperations( array $ops ) {
1329 $supportedOps = $this->supportedOperations();
1331 $performOps = array(); // array of FileOp objects
1332 // Build up ordered array of FileOps...
1333 foreach ( $ops as $operation ) {
1334 $opName = $operation['op'];
1335 if ( isset( $supportedOps[$opName] ) ) {
1336 $class = $supportedOps[$opName];
1337 // Get params for this operation
1338 $params = $operation;
1339 // Append the FileOp class
1340 $performOps[] = new $class( $this, $params );
1342 throw new MWException( "Operation `$opName` is not supported." );
1350 * @see FileBackend::doOperationsInternal()
1352 protected function doOperationsInternal( array $ops, array $opts ) {
1353 wfProfileIn( __METHOD__
);
1354 $status = Status
::newGood();
1356 // Build up a list of FileOps...
1357 $performOps = $this->getOperations( $ops );
1359 // Acquire any locks as needed...
1360 if ( empty( $opts['nonLocking'] ) ) {
1361 // Build up a list of files to lock...
1362 $filesLockEx = $filesLockSh = array();
1363 foreach ( $performOps as $fileOp ) {
1364 $filesLockSh = array_merge( $filesLockSh, $fileOp->storagePathsRead() );
1365 $filesLockEx = array_merge( $filesLockEx, $fileOp->storagePathsChanged() );
1367 // Optimization: if doing an EX lock anyway, don't also set an SH one
1368 $filesLockSh = array_diff( $filesLockSh, $filesLockEx );
1369 // Get a shared lock on the parent directory of each path changed
1370 $filesLockSh = array_merge( $filesLockSh, array_map( 'dirname', $filesLockEx ) );
1371 // Try to lock those files for the scope of this function...
1372 $scopeLockS = $this->getScopedFileLocks( $filesLockSh, LockManager
::LOCK_UW
, $status );
1373 $scopeLockE = $this->getScopedFileLocks( $filesLockEx, LockManager
::LOCK_EX
, $status );
1374 if ( !$status->isOK() ) {
1375 wfProfileOut( __METHOD__
);
1376 return $status; // abort
1380 // Clear any cache entries (after locks acquired)
1381 $this->clearCache();
1383 // Actually attempt the operation batch...
1384 $subStatus = FileOp
::attemptBatch( $performOps, $opts );
1386 // Merge errors into status fields
1387 $status->merge( $subStatus );
1388 $status->success
= $subStatus->success
; // not done in merge()
1390 wfProfileOut( __METHOD__
);
1395 * @see FileBackend::clearCache()
1397 final public function clearCache( array $paths = null ) {
1398 if ( is_array( $paths ) ) {
1399 $paths = array_map( 'FileBackend::normalizeStoragePath', $paths );
1400 $paths = array_filter( $paths, 'strlen' ); // remove nulls
1402 if ( $paths === null ) {
1403 $this->cache
= array();
1404 $this->expensiveCache
= array();
1406 foreach ( $paths as $path ) {
1407 unset( $this->cache
[$path] );
1408 unset( $this->expensiveCache
[$path] );
1411 $this->doClearCache( $paths );
1415 * Clears any additional stat caches for storage paths
1417 * @see FileBackend::clearCache()
1419 * @param $paths Array Storage paths (optional)
1422 protected function doClearCache( array $paths = null ) {}
1425 * Prune the inexpensive cache if it is too big to add an item
1429 protected function trimCache() {
1430 if ( count( $this->cache
) >= $this->maxCacheSize
) {
1431 reset( $this->cache
);
1432 unset( $this->cache
[key( $this->cache
)] );
1437 * Prune the expensive cache if it is too big to add an item
1441 protected function trimExpensiveCache() {
1442 if ( count( $this->expensiveCache
) >= $this->maxExpensiveCacheSize
) {
1443 reset( $this->expensiveCache
);
1444 unset( $this->expensiveCache
[key( $this->expensiveCache
)] );
1449 * Check if a container name is valid.
1450 * This checks for for length and illegal characters.
1452 * @param $container string
1455 final protected static function isValidContainerName( $container ) {
1456 // This accounts for Swift and S3 restrictions while leaving room
1457 // for things like '.xxx' (hex shard chars) or '.seg' (segments).
1458 // This disallows directory separators or traversal characters.
1459 // Note that matching strings URL encode to the same string;
1460 // in Swift, the length restriction is *after* URL encoding.
1461 return preg_match( '/^[a-z0-9][a-z0-9-_]{0,199}$/i', $container );
1465 * Splits a storage path into an internal container name,
1466 * an internal relative file name, and a container shard suffix.
1467 * Any shard suffix is already appended to the internal container name.
1468 * This also checks that the storage path is valid and within this backend.
1470 * If the container is sharded but a suffix could not be determined,
1471 * this means that the path can only refer to a directory and can only
1472 * be scanned by looking in all the container shards.
1474 * @param $storagePath string
1475 * @return Array (container, path, container suffix) or (null, null, null) if invalid
1477 final protected function resolveStoragePath( $storagePath ) {
1478 list( $backend, $container, $relPath ) = self
::splitStoragePath( $storagePath );
1479 if ( $backend === $this->name
) { // must be for this backend
1480 $relPath = self
::normalizeContainerPath( $relPath );
1481 if ( $relPath !== null ) {
1482 // Get shard for the normalized path if this container is sharded
1483 $cShard = $this->getContainerShard( $container, $relPath );
1484 // Validate and sanitize the relative path (backend-specific)
1485 $relPath = $this->resolveContainerPath( $container, $relPath );
1486 if ( $relPath !== null ) {
1487 // Prepend any wiki ID prefix to the container name
1488 $container = $this->fullContainerName( $container );
1489 if ( self
::isValidContainerName( $container ) ) {
1490 // Validate and sanitize the container name (backend-specific)
1491 $container = $this->resolveContainerName( "{$container}{$cShard}" );
1492 if ( $container !== null ) {
1493 return array( $container, $relPath, $cShard );
1499 return array( null, null, null );
1503 * Like resolveStoragePath() except null values are returned if
1504 * the container is sharded and the shard could not be determined.
1506 * @see FileBackendStore::resolveStoragePath()
1508 * @param $storagePath string
1509 * @return Array (container, path) or (null, null) if invalid
1511 final protected function resolveStoragePathReal( $storagePath ) {
1512 list( $container, $relPath, $cShard ) = $this->resolveStoragePath( $storagePath );
1513 if ( $cShard !== null ) {
1514 return array( $container, $relPath );
1516 return array( null, null );
1520 * Get the container name shard suffix for a given path.
1521 * Any empty suffix means the container is not sharded.
1523 * @param $container string Container name
1524 * @param $relStoragePath string Storage path relative to the container
1525 * @return string|null Returns null if shard could not be determined
1527 final protected function getContainerShard( $container, $relPath ) {
1528 list( $levels, $base, $repeat ) = $this->getContainerHashLevels( $container );
1529 if ( $levels == 1 ||
$levels == 2 ) {
1530 // Hash characters are either base 16 or 36
1531 $char = ( $base == 36 ) ?
'[0-9a-z]' : '[0-9a-f]';
1532 // Get a regex that represents the shard portion of paths.
1533 // The concatenation of the captures gives us the shard.
1534 if ( $levels === 1 ) { // 16 or 36 shards per container
1535 $hashDirRegex = '(' . $char . ')';
1536 } else { // 256 or 1296 shards per container
1537 if ( $repeat ) { // verbose hash dir format (e.g. "a/ab/abc")
1538 $hashDirRegex = $char . '/(' . $char . '{2})';
1539 } else { // short hash dir format (e.g. "a/b/c")
1540 $hashDirRegex = '(' . $char . ')/(' . $char . ')';
1543 // Allow certain directories to be above the hash dirs so as
1544 // to work with FileRepo (e.g. "archive/a/ab" or "temp/a/ab").
1545 // They must be 2+ chars to avoid any hash directory ambiguity.
1547 if ( preg_match( "!^(?:[^/]{2,}/)*$hashDirRegex(?:/|$)!", $relPath, $m ) ) {
1548 return '.' . implode( '', array_slice( $m, 1 ) );
1550 return null; // failed to match
1552 return ''; // no sharding
1556 * Get the sharding config for a container.
1557 * If greater than 0, then all file storage paths within
1558 * the container are required to be hashed accordingly.
1560 * @param $container string
1561 * @return Array (integer levels, integer base, repeat flag) or (0, 0, false)
1563 final protected function getContainerHashLevels( $container ) {
1564 if ( isset( $this->shardViaHashLevels
[$container] ) ) {
1565 $config = $this->shardViaHashLevels
[$container];
1566 $hashLevels = (int)$config['levels'];
1567 if ( $hashLevels == 1 ||
$hashLevels == 2 ) {
1568 $hashBase = (int)$config['base'];
1569 if ( $hashBase == 16 ||
$hashBase == 36 ) {
1570 return array( $hashLevels, $hashBase, $config['repeat'] );
1574 return array( 0, 0, false ); // no sharding
1578 * Get a list of full container shard suffixes for a container
1580 * @param $container string
1583 final protected function getContainerSuffixes( $container ) {
1585 list( $digits, $base ) = $this->getContainerHashLevels( $container );
1586 if ( $digits > 0 ) {
1587 $numShards = pow( $base, $digits );
1588 for ( $index = 0; $index < $numShards; $index++
) {
1589 $shards[] = '.' . wfBaseConvert( $index, 10, $base, $digits );
1596 * Get the full container name, including the wiki ID prefix
1598 * @param $container string
1601 final protected function fullContainerName( $container ) {
1602 if ( $this->wikiId
!= '' ) {
1603 return "{$this->wikiId}-$container";
1610 * Resolve a container name, checking if it's allowed by the backend.
1611 * This is intended for internal use, such as encoding illegal chars.
1612 * Subclasses can override this to be more restrictive.
1614 * @param $container string
1615 * @return string|null
1617 protected function resolveContainerName( $container ) {
1622 * Resolve a relative storage path, checking if it's allowed by the backend.
1623 * This is intended for internal use, such as encoding illegal chars or perhaps
1624 * getting absolute paths (e.g. FS based backends). Note that the relative path
1625 * may be the empty string (e.g. the path is simply to the container).
1627 * @param $container string Container name
1628 * @param $relStoragePath string Storage path relative to the container
1629 * @return string|null Path or null if not valid
1631 protected function resolveContainerPath( $container, $relStoragePath ) {
1632 return $relStoragePath;
1637 * FileBackendStore helper function to handle file listings that span container shards.
1638 * Do not use this class from places outside of FileBackendStore.
1640 * @ingroup FileBackend
1642 class FileBackendStoreShardListIterator
implements Iterator
{
1643 /* @var FileBackendStore */
1648 protected $shardSuffixes;
1649 protected $container; // string
1650 protected $directory; // string
1652 /* @var Traversable */
1654 protected $curShard = 0; // integer
1655 protected $pos = 0; // integer
1658 * @param $backend FileBackendStore
1659 * @param $container string Full storage container name
1660 * @param $dir string Storage directory relative to container
1661 * @param $suffixes Array List of container shard suffixes
1662 * @param $params Array
1664 public function __construct(
1665 FileBackendStore
$backend, $container, $dir, array $suffixes, array $params
1667 $this->backend
= $backend;
1668 $this->container
= $container;
1669 $this->directory
= $dir;
1670 $this->shardSuffixes
= $suffixes;
1671 $this->params
= $params;
1674 public function current() {
1675 if ( is_array( $this->iter
) ) {
1676 return current( $this->iter
);
1678 return $this->iter
->current();
1682 public function key() {
1686 public function next() {
1688 if ( is_array( $this->iter
) ) {
1689 next( $this->iter
);
1691 $this->iter
->next();
1693 // Find the next non-empty shard if no elements are left
1694 $this->nextShardIteratorIfNotValid();
1698 * If the iterator for this container shard is out of items,
1699 * then move on to the next container that has items.
1700 * If there are none, then it advances to the last container.
1702 protected function nextShardIteratorIfNotValid() {
1703 while ( !$this->valid() ) {
1704 if ( ++
$this->curShard
>= count( $this->shardSuffixes
) ) {
1705 break; // no more container shards
1707 $this->setIteratorFromCurrentShard();
1711 protected function setIteratorFromCurrentShard() {
1712 $suffix = $this->shardSuffixes
[$this->curShard
];
1713 $this->iter
= $this->backend
->getFileListInternal(
1714 "{$this->container}{$suffix}", $this->directory
, $this->params
);
1717 public function rewind() {
1719 $this->curShard
= 0;
1720 $this->setIteratorFromCurrentShard();
1721 // Find the next non-empty shard if this one has no elements
1722 $this->nextShardIteratorIfNotValid();
1725 public function valid() {
1726 if ( $this->iter
== null ) {
1727 return false; // some failure?
1728 } elseif ( is_array( $this->iter
) ) {
1729 return ( current( $this->iter
) !== false ); // no paths can have this value
1731 return $this->iter
->valid();