3 * @defgroup FileBackend File backend
6 * File backend is used to interact with file storage systems,
7 * such as the local file system, NFS, or cloud storage systems.
12 * @ingroup FileBackend
13 * @author Aaron Schulz
17 * Base class for all file backend classes (including multi-write backends).
19 * This class defines the methods as abstract that subclasses must implement.
20 * Outside callers can assume that all backends will have these functions.
22 * All "storage paths" are of the format "mwstore://<backend>/<container>/<path>".
23 * The <path> portion is a relative path that uses UNIX file system (FS) notation,
24 * though any particular backend may not actually be using a local filesystem.
25 * Therefore, the relative paths are only virtual.
27 * Backend contents are stored under wiki-specific container names by default.
28 * For legacy reasons, this has no effect for the FS backend class, and per-wiki
29 * segregation must be done by setting the container paths appropriately.
31 * FS-based backends are somewhat more restrictive due to the existence of real
32 * directory files; a regular file cannot have the same name as a directory. Other
33 * backends with virtual directories may not have this limitation. Callers should
34 * store files in such a way that no files and directories are under the same path.
36 * Methods should avoid throwing exceptions at all costs.
37 * As a corollary, external dependencies should be kept to a minimum.
39 * @ingroup FileBackend
42 abstract class FileBackend
{
43 protected $name; // string; unique backend name
44 protected $wikiId; // string; unique wiki name
45 protected $readOnly; // string; read-only explanation message
46 /** @var LockManager */
47 protected $lockManager;
50 * Create a new backend instance from configuration.
51 * This should only be called from within FileBackendGroup.
54 * 'name' : The unique name of this backend.
55 * This should consist of alphanumberic, '-', and '_' characters.
56 * 'wikiId' : Prefix to container names that is unique to this wiki.
57 * This should consist of alphanumberic, '-', and '_' characters.
58 * 'lockManager' : Registered name of a file lock manager to use.
59 * 'readOnly' : Write operations are disallowed if this is a non-empty string.
60 * It should be an explanation for the backend being read-only.
62 * @param $config Array
64 public function __construct( array $config ) {
65 $this->name
= $config['name'];
66 $this->wikiId
= isset( $config['wikiId'] )
68 : wfWikiID(); // e.g. "my_wiki-en_"
69 $this->lockManager
= ( $config['lockManager'] instanceof LockManager
)
70 ?
$config['lockManager']
71 : LockManagerGroup
::singleton()->get( $config['lockManager'] );
72 $this->readOnly
= isset( $config['readOnly'] )
73 ?
(string)$config['readOnly']
78 * Get the unique backend name.
79 * We may have multiple different backends of the same type.
80 * For example, we can have two Swift backends using different proxies.
84 final public function getName() {
89 * Check if this backend is read-only
93 final public function isReadOnly() {
94 return ( $this->readOnly
!= '' );
98 * Get an explanatory message if this backend is read-only
100 * @return string|bool Returns falls if the backend is not read-only
102 final public function getReadOnlyReason() {
103 return ( $this->readOnly
!= '' ) ?
$this->readOnly
: false;
107 * This is the main entry point into the backend for write operations.
108 * Callers supply an ordered list of operations to perform as a transaction.
109 * Files will be locked, the stat cache cleared, and then the operations attempted.
110 * If any serious errors occur, all attempted operations will be rolled back.
112 * $ops is an array of arrays. The outer array holds a list of operations.
113 * Each inner array is a set of key value pairs that specify an operation.
115 * Supported operations and their parameters:
116 * a) Create a new file in storage with the contents of a string
119 * 'dst' => <storage path>,
120 * 'content' => <string of new file contents>,
121 * 'overwrite' => <boolean>,
122 * 'overwriteSame' => <boolean>
124 * b) Copy a file system file into storage
127 * 'src' => <file system path>,
128 * 'dst' => <storage path>,
129 * 'overwrite' => <boolean>,
130 * 'overwriteSame' => <boolean>
132 * c) Copy a file within storage
135 * 'src' => <storage path>,
136 * 'dst' => <storage path>,
137 * 'overwrite' => <boolean>,
138 * 'overwriteSame' => <boolean>
140 * d) Move a file within storage
143 * 'src' => <storage path>,
144 * 'dst' => <storage path>,
145 * 'overwrite' => <boolean>,
146 * 'overwriteSame' => <boolean>
148 * e) Delete a file within storage
151 * 'src' => <storage path>,
152 * 'ignoreMissingSource' => <boolean>
154 * f) Do nothing (no-op)
159 * Boolean flags for operations (operation-specific):
160 * 'ignoreMissingSource' : The operation will simply succeed and do
161 * nothing if the source file does not exist.
162 * 'overwrite' : Any destination file will be overwritten.
163 * 'overwriteSame' : An error will not be given if a file already
164 * exists at the destination that has the same
165 * contents as the new contents to be written there.
167 * $opts is an associative of boolean flags, including:
168 * 'force' : Errors that would normally cause a rollback do not.
169 * The remaining operations are still attempted if any fail.
170 * 'nonLocking' : No locks are acquired for the operations.
171 * This can increase performance for non-critical writes.
172 * This has no effect unless the 'force' flag is set.
173 * 'allowStale' : Don't require the latest available data.
174 * This can increase performance for non-critical writes.
175 * This has no effect unless the 'force' flag is set.
177 * Remarks on locking:
178 * File system paths given to operations should refer to files that are
179 * already locked or otherwise safe from modification from other processes.
180 * Normally these files will be new temp files, which should be adequate.
183 * This returns a Status, which contains all warnings and fatals that occured
184 * during the operation. The 'failCount', 'successCount', and 'success' members
185 * will reflect each operation attempted. The status will be "OK" unless any
186 * of the operations failed and the 'force' parameter was not set.
188 * @param $ops Array List of operations to execute in order
189 * @param $opts Array Batch operation options
192 final public function doOperations( array $ops, array $opts = array() ) {
193 if ( $this->isReadOnly() ) {
194 return Status
::newFatal( 'backend-fail-readonly', $this->name
, $this->readOnly
);
196 if ( empty( $opts['force'] ) ) { // sanity
197 unset( $opts['nonLocking'] );
198 unset( $opts['allowStale'] );
200 return $this->doOperationsInternal( $ops, $opts );
204 * @see FileBackend::doOperations()
206 abstract protected function doOperationsInternal( array $ops, array $opts );
209 * Same as doOperations() except it takes a single operation.
210 * If you are doing a batch of operations that should either
211 * all succeed or all fail, then use that function instead.
213 * @see FileBackend::doOperations()
215 * @param $op Array Operation
216 * @param $opts Array Operation options
219 final public function doOperation( array $op, array $opts = array() ) {
220 return $this->doOperations( array( $op ), $opts );
224 * Performs a single create operation.
225 * This sets $params['op'] to 'create' and passes it to doOperation().
227 * @see FileBackend::doOperation()
229 * @param $params Array Operation parameters
230 * @param $opts Array Operation options
233 final public function create( array $params, array $opts = array() ) {
234 $params['op'] = 'create';
235 return $this->doOperation( $params, $opts );
239 * Performs a single store operation.
240 * This sets $params['op'] to 'store' and passes it to doOperation().
242 * @see FileBackend::doOperation()
244 * @param $params Array Operation parameters
245 * @param $opts Array Operation options
248 final public function store( array $params, array $opts = array() ) {
249 $params['op'] = 'store';
250 return $this->doOperation( $params, $opts );
254 * Performs a single copy operation.
255 * This sets $params['op'] to 'copy' and passes it to doOperation().
257 * @see FileBackend::doOperation()
259 * @param $params Array Operation parameters
260 * @param $opts Array Operation options
263 final public function copy( array $params, array $opts = array() ) {
264 $params['op'] = 'copy';
265 return $this->doOperation( $params, $opts );
269 * Performs a single move operation.
270 * This sets $params['op'] to 'move' and passes it to doOperation().
272 * @see FileBackend::doOperation()
274 * @param $params Array Operation parameters
275 * @param $opts Array Operation options
278 final public function move( array $params, array $opts = array() ) {
279 $params['op'] = 'move';
280 return $this->doOperation( $params, $opts );
284 * Performs a single delete operation.
285 * This sets $params['op'] to 'delete' and passes it to doOperation().
287 * @see FileBackend::doOperation()
289 * @param $params Array Operation parameters
290 * @param $opts Array Operation options
293 final public function delete( array $params, array $opts = array() ) {
294 $params['op'] = 'delete';
295 return $this->doOperation( $params, $opts );
299 * Concatenate a list of storage files into a single file system file.
300 * The target path should refer to a file that is already locked or
301 * otherwise safe from modification from other processes. Normally,
302 * the file will be a new temp file, which should be adequate.
304 * srcs : ordered source storage paths (e.g. chunk1, chunk2, ...)
305 * dst : file system path to 0-byte temp file
307 * @param $params Array Operation parameters
310 abstract public function concatenate( array $params );
313 * Prepare a storage directory for usage.
314 * This will create any required containers and parent directories.
315 * Backends using key/value stores only need to create the container.
318 * dir : storage directory
320 * @param $params Array
323 final public function prepare( array $params ) {
324 if ( $this->isReadOnly() ) {
325 return Status
::newFatal( 'backend-fail-readonly', $this->name
, $this->readOnly
);
327 return $this->doPrepare( $params );
331 * @see FileBackend::prepare()
333 abstract protected function doPrepare( array $params );
336 * Take measures to block web access to a storage directory and
337 * the container it belongs to. FS backends might add .htaccess
338 * files whereas key/value store backends might restrict container
339 * access to the auth user that represents end-users in web request.
340 * This is not guaranteed to actually do anything.
343 * dir : storage directory
344 * noAccess : try to deny file access
345 * noListing : try to deny file listing
347 * @param $params Array
350 final public function secure( array $params ) {
351 if ( $this->isReadOnly() ) {
352 return Status
::newFatal( 'backend-fail-readonly', $this->name
, $this->readOnly
);
354 $status = $this->doPrepare( $params ); // dir must exist to restrict it
355 if ( $status->isOK() ) {
356 $status->merge( $this->doSecure( $params ) );
362 * @see FileBackend::secure()
364 abstract protected function doSecure( array $params );
367 * Delete a storage directory if it is empty.
368 * Backends using key/value stores may do nothing unless the directory
369 * is that of an empty container, in which case it should be deleted.
372 * dir : storage directory
374 * @param $params Array
377 final public function clean( array $params ) {
378 if ( $this->isReadOnly() ) {
379 return Status
::newFatal( 'backend-fail-readonly', $this->name
, $this->readOnly
);
381 return $this->doClean( $params );
385 * @see FileBackend::clean()
387 abstract protected function doClean( array $params );
390 * Check if a file exists at a storage path in the backend.
391 * This returns false if only a directory exists at the path.
394 * src : source storage path
395 * latest : use the latest available data
397 * @param $params Array
398 * @return bool|null Returns null on failure
400 abstract public function fileExists( array $params );
403 * Get the last-modified timestamp of the file at a storage path.
406 * src : source storage path
407 * latest : use the latest available data
409 * @param $params Array
410 * @return string|bool TS_MW timestamp or false on failure
412 abstract public function getFileTimestamp( array $params );
415 * Get the contents of a file at a storage path in the backend.
416 * This should be avoided for potentially large files.
419 * src : source storage path
420 * latest : use the latest available data
422 * @param $params Array
423 * @return string|bool Returns false on failure
425 abstract public function getFileContents( array $params );
428 * Get the size (bytes) of a file at a storage path in the backend.
431 * src : source storage path
432 * latest : use the latest available data
434 * @param $params Array
435 * @return integer|bool Returns false on failure
437 abstract public function getFileSize( array $params );
440 * Get quick information about a file at a storage path in the backend.
441 * If the file does not exist, then this returns false.
442 * Otherwise, the result is an associative array that includes:
443 * mtime : the last-modified timestamp (TS_MW)
444 * size : the file size (bytes)
445 * Additional values may be included for internal use only.
448 * src : source storage path
449 * latest : use the latest available data
451 * @param $params Array
452 * @return Array|bool|null Returns null on failure
454 abstract public function getFileStat( array $params );
457 * Get a SHA-1 hash of the file at a storage path in the backend.
460 * src : source storage path
461 * latest : use the latest available data
463 * @param $params Array
464 * @return string|bool Hash string or false on failure
466 abstract public function getFileSha1Base36( array $params );
469 * Get the properties of the file at a storage path in the backend.
470 * Returns FSFile::placeholderProps() on failure.
473 * src : source storage path
474 * latest : use the latest available data
476 * @param $params Array
479 abstract public function getFileProps( array $params );
482 * Stream the file at a storage path in the backend.
483 * If the file does not exists, a 404 error will be given.
484 * Appropriate HTTP headers (Status, Content-Type, Content-Length)
485 * must be sent if streaming began, while none should be sent otherwise.
486 * Implementations should flush the output buffer before sending data.
489 * src : source storage path
490 * headers : additional HTTP headers to send on success
491 * latest : use the latest available data
493 * @param $params Array
496 abstract public function streamFile( array $params );
499 * Returns a file system file, identical to the file at a storage path.
500 * The file returned is either:
501 * a) A local copy of the file at a storage path in the backend.
502 * The temporary copy will have the same extension as the source.
503 * b) An original of the file at a storage path in the backend.
504 * Temporary files may be purged when the file object falls out of scope.
506 * Write operations should *never* be done on this file as some backends
507 * may do internal tracking or may be instances of FileBackendMultiWrite.
508 * In that later case, there are copies of the file that must stay in sync.
509 * Additionally, further calls to this function may return the same file.
512 * src : source storage path
513 * latest : use the latest available data
515 * @param $params Array
516 * @return FSFile|null Returns null on failure
518 abstract public function getLocalReference( array $params );
521 * Get a local copy on disk of the file at a storage path in the backend.
522 * The temporary copy will have the same file extension as the source.
523 * Temporary files may be purged when the file object falls out of scope.
526 * src : source storage path
527 * latest : use the latest available data
529 * @param $params Array
530 * @return TempFSFile|null Returns null on failure
532 abstract public function getLocalCopy( array $params );
535 * Get an iterator to list out all stored files under a storage directory.
536 * If the directory is of the form "mwstore://backend/container",
537 * then all files in the container should be listed.
538 * If the directory is of form "mwstore://backend/container/dir",
539 * then all files under that container directory should be listed.
540 * Results should be storage paths relative to the given directory.
542 * Storage backends with eventual consistency might return stale data.
545 * dir : storage path directory
547 * @return Traversable|Array|null Returns null on failure
549 abstract public function getFileList( array $params );
552 * Invalidate any in-process file existence and property cache.
553 * If $paths is given, then only the cache for those files will be cleared.
555 * @param $paths Array Storage paths (optional)
558 public function clearCache( array $paths = null ) {}
561 * Lock the files at the given storage paths in the backend.
562 * This will either lock all the files or none (on failure).
564 * Callers should consider using getScopedFileLocks() instead.
566 * @param $paths Array Storage paths
567 * @param $type integer LockManager::LOCK_* constant
570 final public function lockFiles( array $paths, $type ) {
571 return $this->lockManager
->lock( $paths, $type );
575 * Unlock the files at the given storage paths in the backend.
577 * @param $paths Array Storage paths
578 * @param $type integer LockManager::LOCK_* constant
581 final public function unlockFiles( array $paths, $type ) {
582 return $this->lockManager
->unlock( $paths, $type );
586 * Lock the files at the given storage paths in the backend.
587 * This will either lock all the files or none (on failure).
588 * On failure, the status object will be updated with errors.
590 * Once the return value goes out scope, the locks will be released and
591 * the status updated. Unlock fatals will not change the status "OK" value.
593 * @param $paths Array Storage paths
594 * @param $type integer LockManager::LOCK_* constant
595 * @param $status Status Status to update on lock/unlock
596 * @return ScopedLock|null Returns null on failure
598 final public function getScopedFileLocks( array $paths, $type, Status
$status ) {
599 return ScopedLock
::factory( $this->lockManager
, $paths, $type, $status );
603 * Check if a given path is a "mwstore://" path.
604 * This does not do any further validation or any existence checks.
606 * @param $path string
609 final public static function isStoragePath( $path ) {
610 return ( strpos( $path, 'mwstore://' ) === 0 );
614 * Split a storage path into a backend name, a container name,
615 * and a relative file path. The relative path may be the empty string.
616 * This does not do any path normalization or traversal checks.
618 * @param $storagePath string
619 * @return Array (backend, container, rel object) or (null, null, null)
621 final public static function splitStoragePath( $storagePath ) {
622 if ( self
::isStoragePath( $storagePath ) ) {
623 // Remove the "mwstore://" prefix and split the path
624 $parts = explode( '/', substr( $storagePath, 10 ), 3 );
625 if ( count( $parts ) >= 2 && $parts[0] != '' && $parts[1] != '' ) {
626 if ( count( $parts ) == 3 ) {
627 return $parts; // e.g. "backend/container/path"
629 return array( $parts[0], $parts[1], '' ); // e.g. "backend/container"
633 return array( null, null, null );
637 * Normalize a storage path by cleaning up directory separators.
638 * Returns null if the path is not of the format of a valid storage path.
640 * @param $storagePath string
641 * @return string|null
643 final public static function normalizeStoragePath( $storagePath ) {
644 list( $backend, $container, $relPath ) = self
::splitStoragePath( $storagePath );
645 if ( $relPath !== null ) { // must be for this backend
646 $relPath = self
::normalizeContainerPath( $relPath );
647 if ( $relPath !== null ) {
648 return ( $relPath != '' )
649 ?
"mwstore://{$backend}/{$container}/{$relPath}"
650 : "mwstore://{$backend}/{$container}";
657 * Validate and normalize a relative storage path.
658 * Null is returned if the path involves directory traversal.
659 * Traversal is insecure for FS backends and broken for others.
661 * @param $path string Storage path relative to a container
662 * @return string|null
664 final protected static function normalizeContainerPath( $path ) {
665 // Normalize directory separators
666 $path = strtr( $path, '\\', '/' );
667 // Collapse any consecutive directory separators
668 $path = preg_replace( '![/]{2,}!', '/', $path );
669 // Remove any leading directory separator
670 $path = ltrim( $path, '/' );
671 // Use the same traversal protection as Title::secureAndSplit()
672 if ( strpos( $path, '.' ) !== false ) {
676 strpos( $path, './' ) === 0 ||
677 strpos( $path, '../' ) === 0 ||
678 strpos( $path, '/./' ) !== false ||
679 strpos( $path, '/../' ) !== false
688 * Get the parent storage directory of a storage path.
689 * This returns a path like "mwstore://backend/container",
690 * "mwstore://backend/container/...", or null if there is no parent.
692 * @param $storagePath string
693 * @return string|null
695 final public static function parentStoragePath( $storagePath ) {
696 $storagePath = dirname( $storagePath );
697 list( $b, $cont, $rel ) = self
::splitStoragePath( $storagePath );
698 return ( $rel === null ) ?
null : $storagePath;
702 * Get the final extension from a storage or FS path
704 * @param $path string
707 final public static function extensionFromPath( $path ) {
708 $i = strrpos( $path, '.' );
709 return strtolower( $i ?
substr( $path, $i +
1 ) : '' );
714 * @brief Base class for all backends using particular storage medium.
716 * This class defines the methods as abstract that subclasses must implement.
717 * Outside callers should *not* use functions with "Internal" in the name.
719 * The FileBackend operations are implemented using basic functions
720 * such as storeInternal(), copyInternal(), deleteInternal() and the like.
721 * This class is also responsible for path resolution and sanitization.
723 * @ingroup FileBackend
726 abstract class FileBackendStore
extends FileBackend
{
727 /** @var Array Map of paths to small (RAM/disk) cache items */
728 protected $cache = array(); // (storage path => key => value)
729 protected $maxCacheSize = 100; // integer; max paths with entries
730 /** @var Array Map of paths to large (RAM/disk) cache items */
731 protected $expensiveCache = array(); // (storage path => key => value)
732 protected $maxExpensiveCacheSize = 10; // integer; max paths with entries
734 /** @var Array Map of container names to sharding settings */
735 protected $shardViaHashLevels = array(); // (container name => config array)
737 protected $maxFileSize = 1000000000; // integer bytes (1GB)
740 * Get the maximum allowable file size given backend
741 * medium restrictions and basic performance constraints.
742 * Do not call this function from places outside FileBackend and FileOp.
744 * @return integer Bytes
746 final public function maxFileSizeInternal() {
747 return $this->maxFileSize
;
751 * Check if a file can be created at a given storage path.
752 * FS backends should check if the parent directory exists and the file is writable.
753 * Backends using key/value stores should check if the container exists.
755 * @param $storagePath string
758 abstract public function isPathUsableInternal( $storagePath );
761 * Create a file in the backend with the given contents.
762 * Do not call this function from places outside FileBackend and FileOp.
765 * content : the raw file contents
766 * dst : destination storage path
767 * overwrite : overwrite any file that exists at the destination
769 * @param $params Array
772 final public function createInternal( array $params ) {
773 wfProfileIn( __METHOD__
);
774 if ( strlen( $params['content'] ) > $this->maxFileSizeInternal() ) {
775 $status = Status
::newFatal( 'backend-fail-create', $params['dst'] );
777 $status = $this->doCreateInternal( $params );
778 $this->clearCache( array( $params['dst'] ) );
780 wfProfileOut( __METHOD__
);
785 * @see FileBackendStore::createInternal()
787 abstract protected function doCreateInternal( array $params );
790 * Store a file into the backend from a file on disk.
791 * Do not call this function from places outside FileBackend and FileOp.
794 * src : source path on disk
795 * dst : destination storage path
796 * overwrite : overwrite any file that exists at the destination
798 * @param $params Array
801 final public function storeInternal( array $params ) {
802 wfProfileIn( __METHOD__
);
803 if ( filesize( $params['src'] ) > $this->maxFileSizeInternal() ) {
804 $status = Status
::newFatal( 'backend-fail-store', $params['dst'] );
806 $status = $this->doStoreInternal( $params );
807 $this->clearCache( array( $params['dst'] ) );
809 wfProfileOut( __METHOD__
);
814 * @see FileBackendStore::storeInternal()
816 abstract protected function doStoreInternal( array $params );
819 * Copy a file from one storage path to another in the backend.
820 * Do not call this function from places outside FileBackend and FileOp.
823 * src : source storage path
824 * dst : destination storage path
825 * overwrite : overwrite any file that exists at the destination
827 * @param $params Array
830 final public function copyInternal( array $params ) {
831 wfProfileIn( __METHOD__
);
832 $status = $this->doCopyInternal( $params );
833 $this->clearCache( array( $params['dst'] ) );
834 wfProfileOut( __METHOD__
);
839 * @see FileBackendStore::copyInternal()
841 abstract protected function doCopyInternal( array $params );
844 * Delete a file at the storage path.
845 * Do not call this function from places outside FileBackend and FileOp.
848 * src : source storage path
849 * ignoreMissingSource : do nothing if the source file does not exist
851 * @param $params Array
854 final public function deleteInternal( array $params ) {
855 wfProfileIn( __METHOD__
);
856 $status = $this->doDeleteInternal( $params );
857 $this->clearCache( array( $params['src'] ) );
858 wfProfileOut( __METHOD__
);
863 * @see FileBackendStore::deleteInternal()
865 abstract protected function doDeleteInternal( array $params );
868 * Move a file from one storage path to another in the backend.
869 * Do not call this function from places outside FileBackend and FileOp.
872 * src : source storage path
873 * dst : destination storage path
874 * overwrite : overwrite any file that exists at the destination
876 * @param $params Array
879 final public function moveInternal( array $params ) {
880 wfProfileIn( __METHOD__
);
881 $status = $this->doMoveInternal( $params );
882 $this->clearCache( array( $params['src'], $params['dst'] ) );
883 wfProfileOut( __METHOD__
);
888 * @see FileBackendStore::moveInternal()
891 protected function doMoveInternal( array $params ) {
892 // Copy source to dest
893 $status = $this->copyInternal( $params );
894 if ( $status->isOK() ) {
895 // Delete source (only fails due to races or medium going down)
896 $status->merge( $this->deleteInternal( array( 'src' => $params['src'] ) ) );
897 $status->setResult( true, $status->value
); // ignore delete() errors
903 * @see FileBackend::concatenate()
906 final public function concatenate( array $params ) {
907 wfProfileIn( __METHOD__
);
908 $status = Status
::newGood();
910 // Try to lock the source files for the scope of this function
911 $scopeLockS = $this->getScopedFileLocks( $params['srcs'], LockManager
::LOCK_UW
, $status );
912 if ( $status->isOK() ) {
913 // Actually do the concatenation
914 $status->merge( $this->doConcatenate( $params ) );
917 wfProfileOut( __METHOD__
);
922 * @see FileBackendStore::concatenate()
925 protected function doConcatenate( array $params ) {
926 $status = Status
::newGood();
927 $tmpPath = $params['dst']; // convenience
929 // Check that the specified temp file is valid...
930 wfSuppressWarnings();
931 $ok = ( is_file( $tmpPath ) && !filesize( $tmpPath ) );
933 if ( !$ok ) { // not present or not empty
934 $status->fatal( 'backend-fail-opentemp', $tmpPath );
938 // Build up the temp file using the source chunks (in order)...
939 $tmpHandle = fopen( $tmpPath, 'ab' );
940 if ( $tmpHandle === false ) {
941 $status->fatal( 'backend-fail-opentemp', $tmpPath );
944 foreach ( $params['srcs'] as $virtualSource ) {
945 // Get a local FS version of the chunk
946 $tmpFile = $this->getLocalReference( array( 'src' => $virtualSource ) );
948 $status->fatal( 'backend-fail-read', $virtualSource );
951 // Get a handle to the local FS version
952 $sourceHandle = fopen( $tmpFile->getPath(), 'r' );
953 if ( $sourceHandle === false ) {
954 fclose( $tmpHandle );
955 $status->fatal( 'backend-fail-read', $virtualSource );
958 // Append chunk to file (pass chunk size to avoid magic quotes)
959 if ( !stream_copy_to_stream( $sourceHandle, $tmpHandle ) ) {
960 fclose( $sourceHandle );
961 fclose( $tmpHandle );
962 $status->fatal( 'backend-fail-writetemp', $tmpPath );
965 fclose( $sourceHandle );
967 if ( !fclose( $tmpHandle ) ) {
968 $status->fatal( 'backend-fail-closetemp', $tmpPath );
972 clearstatcache(); // temp file changed
978 * @see FileBackend::doPrepare()
981 final protected function doPrepare( array $params ) {
982 wfProfileIn( __METHOD__
);
984 $status = Status
::newGood();
985 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
986 if ( $dir === null ) {
987 $status->fatal( 'backend-fail-invalidpath', $params['dir'] );
988 wfProfileOut( __METHOD__
);
989 return $status; // invalid storage path
992 if ( $shard !== null ) { // confined to a single container/shard
993 $status->merge( $this->doPrepareInternal( $fullCont, $dir, $params ) );
994 } else { // directory is on several shards
995 wfDebug( __METHOD__
. ": iterating over all container shards.\n" );
996 list( $b, $shortCont, $r ) = self
::splitStoragePath( $params['dir'] );
997 foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
998 $status->merge( $this->doPrepareInternal( "{$fullCont}{$suffix}", $dir, $params ) );
1002 wfProfileOut( __METHOD__
);
1007 * @see FileBackendStore::doPrepare()
1010 protected function doPrepareInternal( $container, $dir, array $params ) {
1011 return Status
::newGood();
1015 * @see FileBackend::doSecure()
1018 final protected function doSecure( array $params ) {
1019 wfProfileIn( __METHOD__
);
1020 $status = Status
::newGood();
1022 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
1023 if ( $dir === null ) {
1024 $status->fatal( 'backend-fail-invalidpath', $params['dir'] );
1025 wfProfileOut( __METHOD__
);
1026 return $status; // invalid storage path
1029 if ( $shard !== null ) { // confined to a single container/shard
1030 $status->merge( $this->doSecureInternal( $fullCont, $dir, $params ) );
1031 } else { // directory is on several shards
1032 wfDebug( __METHOD__
. ": iterating over all container shards.\n" );
1033 list( $b, $shortCont, $r ) = self
::splitStoragePath( $params['dir'] );
1034 foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
1035 $status->merge( $this->doSecureInternal( "{$fullCont}{$suffix}", $dir, $params ) );
1039 wfProfileOut( __METHOD__
);
1044 * @see FileBackendStore::doSecure()
1047 protected function doSecureInternal( $container, $dir, array $params ) {
1048 return Status
::newGood();
1052 * @see FileBackend::doClean()
1055 final protected function doClean( array $params ) {
1056 wfProfileIn( __METHOD__
);
1057 $status = Status
::newGood();
1059 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
1060 if ( $dir === null ) {
1061 $status->fatal( 'backend-fail-invalidpath', $params['dir'] );
1062 wfProfileOut( __METHOD__
);
1063 return $status; // invalid storage path
1066 // Attempt to lock this directory...
1067 $filesLockEx = array( $params['dir'] );
1068 $scopedLockE = $this->getScopedFileLocks( $filesLockEx, LockManager
::LOCK_EX
, $status );
1069 if ( !$status->isOK() ) {
1070 wfProfileOut( __METHOD__
);
1071 return $status; // abort
1074 if ( $shard !== null ) { // confined to a single container/shard
1075 $status->merge( $this->doCleanInternal( $fullCont, $dir, $params ) );
1076 } else { // directory is on several shards
1077 wfDebug( __METHOD__
. ": iterating over all container shards.\n" );
1078 list( $b, $shortCont, $r ) = self
::splitStoragePath( $params['dir'] );
1079 foreach ( $this->getContainerSuffixes( $shortCont ) as $suffix ) {
1080 $status->merge( $this->doCleanInternal( "{$fullCont}{$suffix}", $dir, $params ) );
1084 wfProfileOut( __METHOD__
);
1089 * @see FileBackendStore::doClean()
1092 protected function doCleanInternal( $container, $dir, array $params ) {
1093 return Status
::newGood();
1097 * @see FileBackend::fileExists()
1100 final public function fileExists( array $params ) {
1101 wfProfileIn( __METHOD__
);
1102 $stat = $this->getFileStat( $params );
1103 wfProfileOut( __METHOD__
);
1104 return ( $stat === null ) ?
null : (bool)$stat; // null => failure
1108 * @see FileBackend::getFileTimestamp()
1111 final public function getFileTimestamp( array $params ) {
1112 wfProfileIn( __METHOD__
);
1113 $stat = $this->getFileStat( $params );
1114 wfProfileOut( __METHOD__
);
1115 return $stat ?
$stat['mtime'] : false;
1119 * @see FileBackend::getFileSize()
1122 final public function getFileSize( array $params ) {
1123 wfProfileIn( __METHOD__
);
1124 $stat = $this->getFileStat( $params );
1125 wfProfileOut( __METHOD__
);
1126 return $stat ?
$stat['size'] : false;
1130 * @see FileBackend::getFileStat()
1133 final public function getFileStat( array $params ) {
1134 wfProfileIn( __METHOD__
);
1135 $path = self
::normalizeStoragePath( $params['src'] );
1136 if ( $path === null ) {
1137 wfProfileOut( __METHOD__
);
1138 return false; // invalid storage path
1140 $latest = !empty( $params['latest'] );
1141 if ( isset( $this->cache
[$path]['stat'] ) ) {
1142 // If we want the latest data, check that this cached
1143 // value was in fact fetched with the latest available data.
1144 if ( !$latest ||
$this->cache
[$path]['stat']['latest'] ) {
1145 wfProfileOut( __METHOD__
);
1146 return $this->cache
[$path]['stat'];
1149 $stat = $this->doGetFileStat( $params );
1150 if ( is_array( $stat ) ) { // don't cache negatives
1151 $this->trimCache(); // limit memory
1152 $this->cache
[$path]['stat'] = $stat;
1153 $this->cache
[$path]['stat']['latest'] = $latest;
1155 wfProfileOut( __METHOD__
);
1160 * @see FileBackendStore::getFileStat()
1162 abstract protected function doGetFileStat( array $params );
1165 * @see FileBackend::getFileContents()
1166 * @return bool|string
1168 public function getFileContents( array $params ) {
1169 wfProfileIn( __METHOD__
);
1170 $tmpFile = $this->getLocalReference( $params );
1172 wfProfileOut( __METHOD__
);
1175 wfSuppressWarnings();
1176 $data = file_get_contents( $tmpFile->getPath() );
1177 wfRestoreWarnings();
1178 wfProfileOut( __METHOD__
);
1183 * @see FileBackend::getFileSha1Base36()
1184 * @return bool|string
1186 final public function getFileSha1Base36( array $params ) {
1187 wfProfileIn( __METHOD__
);
1188 $path = $params['src'];
1189 if ( isset( $this->cache
[$path]['sha1'] ) ) {
1190 wfProfileOut( __METHOD__
);
1191 return $this->cache
[$path]['sha1'];
1193 $hash = $this->doGetFileSha1Base36( $params );
1194 if ( $hash ) { // don't cache negatives
1195 $this->trimCache(); // limit memory
1196 $this->cache
[$path]['sha1'] = $hash;
1198 wfProfileOut( __METHOD__
);
1203 * @see FileBackendStore::getFileSha1Base36()
1206 protected function doGetFileSha1Base36( array $params ) {
1207 $fsFile = $this->getLocalReference( $params );
1211 return $fsFile->getSha1Base36();
1216 * @see FileBackend::getFileProps()
1219 final public function getFileProps( array $params ) {
1220 wfProfileIn( __METHOD__
);
1221 $fsFile = $this->getLocalReference( $params );
1222 $props = $fsFile ?
$fsFile->getProps() : FSFile
::placeholderProps();
1223 wfProfileOut( __METHOD__
);
1228 * @see FileBackend::getLocalReference()
1229 * @return TempFSFile|null
1231 public function getLocalReference( array $params ) {
1232 wfProfileIn( __METHOD__
);
1233 $path = $params['src'];
1234 if ( isset( $this->expensiveCache
[$path]['localRef'] ) ) {
1235 wfProfileOut( __METHOD__
);
1236 return $this->expensiveCache
[$path]['localRef'];
1238 $tmpFile = $this->getLocalCopy( $params );
1239 if ( $tmpFile ) { // don't cache negatives
1240 $this->trimExpensiveCache(); // limit memory
1241 $this->expensiveCache
[$path]['localRef'] = $tmpFile;
1243 wfProfileOut( __METHOD__
);
1248 * @see FileBackend::streamFile()
1251 final public function streamFile( array $params ) {
1252 wfProfileIn( __METHOD__
);
1253 $status = Status
::newGood();
1255 $info = $this->getFileStat( $params );
1256 if ( !$info ) { // let StreamFile handle the 404
1257 $status->fatal( 'backend-fail-notexists', $params['src'] );
1260 // Set output buffer and HTTP headers for stream
1261 $extraHeaders = $params['headers'] ?
$params['headers'] : array();
1262 $res = StreamFile
::prepareForStream( $params['src'], $info, $extraHeaders );
1263 if ( $res == StreamFile
::NOT_MODIFIED
) {
1264 // do nothing; client cache is up to date
1265 } elseif ( $res == StreamFile
::READY_STREAM
) {
1266 $status = $this->doStreamFile( $params );
1268 $status->fatal( 'backend-fail-stream', $params['src'] );
1271 wfProfileOut( __METHOD__
);
1276 * @see FileBackendStore::streamFile()
1279 protected function doStreamFile( array $params ) {
1280 $status = Status
::newGood();
1282 $fsFile = $this->getLocalReference( $params );
1284 $status->fatal( 'backend-fail-stream', $params['src'] );
1285 } elseif ( !readfile( $fsFile->getPath() ) ) {
1286 $status->fatal( 'backend-fail-stream', $params['src'] );
1293 * @copydoc FileBackend::getFileList()
1294 * @return Array|null|Traversable
1296 final public function getFileList( array $params ) {
1297 list( $fullCont, $dir, $shard ) = $this->resolveStoragePath( $params['dir'] );
1298 if ( $dir === null ) { // invalid storage path
1301 if ( $shard !== null ) {
1302 // File listing is confined to a single container/shard
1303 return $this->getFileListInternal( $fullCont, $dir, $params );
1305 wfDebug( __METHOD__
. ": iterating over all container shards.\n" );
1306 // File listing spans multiple containers/shards
1307 list( $b, $shortCont, $r ) = self
::splitStoragePath( $params['dir'] );
1308 return new FileBackendStoreShardListIterator( $this,
1309 $fullCont, $dir, $this->getContainerSuffixes( $shortCont ), $params );
1314 * Do not call this function from places outside FileBackend
1316 * @see FileBackendStore::getFileList()
1318 * @param $container string Resolved container name
1319 * @param $dir string Resolved path relative to container
1320 * @param $params Array
1321 * @return Traversable|Array|null
1323 abstract public function getFileListInternal( $container, $dir, array $params );
1326 * Get the list of supported operations and their corresponding FileOp classes.
1330 protected function supportedOperations() {
1332 'store' => 'StoreFileOp',
1333 'copy' => 'CopyFileOp',
1334 'move' => 'MoveFileOp',
1335 'delete' => 'DeleteFileOp',
1336 'create' => 'CreateFileOp',
1337 'null' => 'NullFileOp'
1342 * Return a list of FileOp objects from a list of operations.
1343 * Do not call this function from places outside FileBackend.
1345 * The result must have the same number of items as the input.
1346 * An exception is thrown if an unsupported operation is requested.
1348 * @param $ops Array Same format as doOperations()
1349 * @return Array List of FileOp objects
1350 * @throws MWException
1352 final public function getOperations( array $ops ) {
1353 $supportedOps = $this->supportedOperations();
1355 $performOps = array(); // array of FileOp objects
1356 // Build up ordered array of FileOps...
1357 foreach ( $ops as $operation ) {
1358 $opName = $operation['op'];
1359 if ( isset( $supportedOps[$opName] ) ) {
1360 $class = $supportedOps[$opName];
1361 // Get params for this operation
1362 $params = $operation;
1363 // Append the FileOp class
1364 $performOps[] = new $class( $this, $params );
1366 throw new MWException( "Operation `$opName` is not supported." );
1374 * @see FileBackend::doOperationsInternal()
1377 protected function doOperationsInternal( array $ops, array $opts ) {
1378 wfProfileIn( __METHOD__
);
1379 $status = Status
::newGood();
1381 // Build up a list of FileOps...
1382 $performOps = $this->getOperations( $ops );
1384 // Acquire any locks as needed...
1385 if ( empty( $opts['nonLocking'] ) ) {
1386 // Build up a list of files to lock...
1387 $filesLockEx = $filesLockSh = array();
1388 foreach ( $performOps as $fileOp ) {
1389 $filesLockSh = array_merge( $filesLockSh, $fileOp->storagePathsRead() );
1390 $filesLockEx = array_merge( $filesLockEx, $fileOp->storagePathsChanged() );
1392 // Optimization: if doing an EX lock anyway, don't also set an SH one
1393 $filesLockSh = array_diff( $filesLockSh, $filesLockEx );
1394 // Get a shared lock on the parent directory of each path changed
1395 $filesLockSh = array_merge( $filesLockSh, array_map( 'dirname', $filesLockEx ) );
1396 // Try to lock those files for the scope of this function...
1397 $scopeLockS = $this->getScopedFileLocks( $filesLockSh, LockManager
::LOCK_UW
, $status );
1398 $scopeLockE = $this->getScopedFileLocks( $filesLockEx, LockManager
::LOCK_EX
, $status );
1399 if ( !$status->isOK() ) {
1400 wfProfileOut( __METHOD__
);
1401 return $status; // abort
1405 // Clear any cache entries (after locks acquired)
1406 $this->clearCache();
1408 // Actually attempt the operation batch...
1409 $subStatus = FileOp
::attemptBatch( $performOps, $opts );
1411 // Merge errors into status fields
1412 $status->merge( $subStatus );
1413 $status->success
= $subStatus->success
; // not done in merge()
1415 wfProfileOut( __METHOD__
);
1420 * @see FileBackend::clearCache()
1422 final public function clearCache( array $paths = null ) {
1423 if ( is_array( $paths ) ) {
1424 $paths = array_map( 'FileBackend::normalizeStoragePath', $paths );
1425 $paths = array_filter( $paths, 'strlen' ); // remove nulls
1427 if ( $paths === null ) {
1428 $this->cache
= array();
1429 $this->expensiveCache
= array();
1431 foreach ( $paths as $path ) {
1432 unset( $this->cache
[$path] );
1433 unset( $this->expensiveCache
[$path] );
1436 $this->doClearCache( $paths );
1440 * Clears any additional stat caches for storage paths
1442 * @see FileBackend::clearCache()
1444 * @param $paths Array Storage paths (optional)
1447 protected function doClearCache( array $paths = null ) {}
1450 * Prune the inexpensive cache if it is too big to add an item
1454 protected function trimCache() {
1455 if ( count( $this->cache
) >= $this->maxCacheSize
) {
1456 reset( $this->cache
);
1457 unset( $this->cache
[key( $this->cache
)] );
1462 * Prune the expensive cache if it is too big to add an item
1466 protected function trimExpensiveCache() {
1467 if ( count( $this->expensiveCache
) >= $this->maxExpensiveCacheSize
) {
1468 reset( $this->expensiveCache
);
1469 unset( $this->expensiveCache
[key( $this->expensiveCache
)] );
1474 * Check if a container name is valid.
1475 * This checks for for length and illegal characters.
1477 * @param $container string
1480 final protected static function isValidContainerName( $container ) {
1481 // This accounts for Swift and S3 restrictions while leaving room
1482 // for things like '.xxx' (hex shard chars) or '.seg' (segments).
1483 // This disallows directory separators or traversal characters.
1484 // Note that matching strings URL encode to the same string;
1485 // in Swift, the length restriction is *after* URL encoding.
1486 return preg_match( '/^[a-z0-9][a-z0-9-_]{0,199}$/i', $container );
1490 * Splits a storage path into an internal container name,
1491 * an internal relative file name, and a container shard suffix.
1492 * Any shard suffix is already appended to the internal container name.
1493 * This also checks that the storage path is valid and within this backend.
1495 * If the container is sharded but a suffix could not be determined,
1496 * this means that the path can only refer to a directory and can only
1497 * be scanned by looking in all the container shards.
1499 * @param $storagePath string
1500 * @return Array (container, path, container suffix) or (null, null, null) if invalid
1502 final protected function resolveStoragePath( $storagePath ) {
1503 list( $backend, $container, $relPath ) = self
::splitStoragePath( $storagePath );
1504 if ( $backend === $this->name
) { // must be for this backend
1505 $relPath = self
::normalizeContainerPath( $relPath );
1506 if ( $relPath !== null ) {
1507 // Get shard for the normalized path if this container is sharded
1508 $cShard = $this->getContainerShard( $container, $relPath );
1509 // Validate and sanitize the relative path (backend-specific)
1510 $relPath = $this->resolveContainerPath( $container, $relPath );
1511 if ( $relPath !== null ) {
1512 // Prepend any wiki ID prefix to the container name
1513 $container = $this->fullContainerName( $container );
1514 if ( self
::isValidContainerName( $container ) ) {
1515 // Validate and sanitize the container name (backend-specific)
1516 $container = $this->resolveContainerName( "{$container}{$cShard}" );
1517 if ( $container !== null ) {
1518 return array( $container, $relPath, $cShard );
1524 return array( null, null, null );
1528 * Like resolveStoragePath() except null values are returned if
1529 * the container is sharded and the shard could not be determined.
1531 * @see FileBackendStore::resolveStoragePath()
1533 * @param $storagePath string
1534 * @return Array (container, path) or (null, null) if invalid
1536 final protected function resolveStoragePathReal( $storagePath ) {
1537 list( $container, $relPath, $cShard ) = $this->resolveStoragePath( $storagePath );
1538 if ( $cShard !== null ) {
1539 return array( $container, $relPath );
1541 return array( null, null );
1545 * Get the container name shard suffix for a given path.
1546 * Any empty suffix means the container is not sharded.
1548 * @param $container string Container name
1549 * @param $relStoragePath string Storage path relative to the container
1550 * @return string|null Returns null if shard could not be determined
1552 final protected function getContainerShard( $container, $relPath ) {
1553 list( $levels, $base, $repeat ) = $this->getContainerHashLevels( $container );
1554 if ( $levels == 1 ||
$levels == 2 ) {
1555 // Hash characters are either base 16 or 36
1556 $char = ( $base == 36 ) ?
'[0-9a-z]' : '[0-9a-f]';
1557 // Get a regex that represents the shard portion of paths.
1558 // The concatenation of the captures gives us the shard.
1559 if ( $levels === 1 ) { // 16 or 36 shards per container
1560 $hashDirRegex = '(' . $char . ')';
1561 } else { // 256 or 1296 shards per container
1562 if ( $repeat ) { // verbose hash dir format (e.g. "a/ab/abc")
1563 $hashDirRegex = $char . '/(' . $char . '{2})';
1564 } else { // short hash dir format (e.g. "a/b/c")
1565 $hashDirRegex = '(' . $char . ')/(' . $char . ')';
1568 // Allow certain directories to be above the hash dirs so as
1569 // to work with FileRepo (e.g. "archive/a/ab" or "temp/a/ab").
1570 // They must be 2+ chars to avoid any hash directory ambiguity.
1572 if ( preg_match( "!^(?:[^/]{2,}/)*$hashDirRegex(?:/|$)!", $relPath, $m ) ) {
1573 return '.' . implode( '', array_slice( $m, 1 ) );
1575 return null; // failed to match
1577 return ''; // no sharding
1581 * Get the sharding config for a container.
1582 * If greater than 0, then all file storage paths within
1583 * the container are required to be hashed accordingly.
1585 * @param $container string
1586 * @return Array (integer levels, integer base, repeat flag) or (0, 0, false)
1588 final protected function getContainerHashLevels( $container ) {
1589 if ( isset( $this->shardViaHashLevels
[$container] ) ) {
1590 $config = $this->shardViaHashLevels
[$container];
1591 $hashLevels = (int)$config['levels'];
1592 if ( $hashLevels == 1 ||
$hashLevels == 2 ) {
1593 $hashBase = (int)$config['base'];
1594 if ( $hashBase == 16 ||
$hashBase == 36 ) {
1595 return array( $hashLevels, $hashBase, $config['repeat'] );
1599 return array( 0, 0, false ); // no sharding
1603 * Get a list of full container shard suffixes for a container
1605 * @param $container string
1608 final protected function getContainerSuffixes( $container ) {
1610 list( $digits, $base ) = $this->getContainerHashLevels( $container );
1611 if ( $digits > 0 ) {
1612 $numShards = pow( $base, $digits );
1613 for ( $index = 0; $index < $numShards; $index++
) {
1614 $shards[] = '.' . wfBaseConvert( $index, 10, $base, $digits );
1621 * Get the full container name, including the wiki ID prefix
1623 * @param $container string
1626 final protected function fullContainerName( $container ) {
1627 if ( $this->wikiId
!= '' ) {
1628 return "{$this->wikiId}-$container";
1635 * Resolve a container name, checking if it's allowed by the backend.
1636 * This is intended for internal use, such as encoding illegal chars.
1637 * Subclasses can override this to be more restrictive.
1639 * @param $container string
1640 * @return string|null
1642 protected function resolveContainerName( $container ) {
1647 * Resolve a relative storage path, checking if it's allowed by the backend.
1648 * This is intended for internal use, such as encoding illegal chars or perhaps
1649 * getting absolute paths (e.g. FS based backends). Note that the relative path
1650 * may be the empty string (e.g. the path is simply to the container).
1652 * @param $container string Container name
1653 * @param $relStoragePath string Storage path relative to the container
1654 * @return string|null Path or null if not valid
1656 protected function resolveContainerPath( $container, $relStoragePath ) {
1657 return $relStoragePath;
1662 * FileBackendStore helper function to handle file listings that span container shards.
1663 * Do not use this class from places outside of FileBackendStore.
1665 * @ingroup FileBackend
1667 class FileBackendStoreShardListIterator
implements Iterator
{
1668 /* @var FileBackendStore */
1673 protected $shardSuffixes;
1674 protected $container; // string
1675 protected $directory; // string
1677 /* @var Traversable */
1679 protected $curShard = 0; // integer
1680 protected $pos = 0; // integer
1683 * @param $backend FileBackendStore
1684 * @param $container string Full storage container name
1685 * @param $dir string Storage directory relative to container
1686 * @param $suffixes Array List of container shard suffixes
1687 * @param $params Array
1689 public function __construct(
1690 FileBackendStore
$backend, $container, $dir, array $suffixes, array $params
1692 $this->backend
= $backend;
1693 $this->container
= $container;
1694 $this->directory
= $dir;
1695 $this->shardSuffixes
= $suffixes;
1696 $this->params
= $params;
1699 public function current() {
1700 if ( is_array( $this->iter
) ) {
1701 return current( $this->iter
);
1703 return $this->iter
->current();
1707 public function key() {
1711 public function next() {
1713 if ( is_array( $this->iter
) ) {
1714 next( $this->iter
);
1716 $this->iter
->next();
1718 // Find the next non-empty shard if no elements are left
1719 $this->nextShardIteratorIfNotValid();
1723 * If the iterator for this container shard is out of items,
1724 * then move on to the next container that has items.
1725 * If there are none, then it advances to the last container.
1727 protected function nextShardIteratorIfNotValid() {
1728 while ( !$this->valid() ) {
1729 if ( ++
$this->curShard
>= count( $this->shardSuffixes
) ) {
1730 break; // no more container shards
1732 $this->setIteratorFromCurrentShard();
1736 protected function setIteratorFromCurrentShard() {
1737 $suffix = $this->shardSuffixes
[$this->curShard
];
1738 $this->iter
= $this->backend
->getFileListInternal(
1739 "{$this->container}{$suffix}", $this->directory
, $this->params
);
1742 public function rewind() {
1744 $this->curShard
= 0;
1745 $this->setIteratorFromCurrentShard();
1746 // Find the next non-empty shard if this one has no elements
1747 $this->nextShardIteratorIfNotValid();
1750 public function valid() {
1751 if ( $this->iter
== null ) {
1752 return false; // some failure?
1753 } elseif ( is_array( $this->iter
) ) {
1754 return ( current( $this->iter
) !== false ); // no paths can have this value
1756 return $this->iter
->valid();