3 * File system based backend.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup FileBackend
22 * @author Aaron Schulz
26 * @brief Class for a file system (FS) based file backend.
28 * All "containers" each map to a directory under the backend's base directory.
29 * For backwards-compatibility, some container paths can be set to custom paths.
30 * The wiki ID will not be used in any custom paths, so this should be avoided.
32 * Having directories with thousands of files will diminish performance.
33 * Sharding can be accomplished by using FileRepo-style hash paths.
35 * Status messages should avoid mentioning the internal FS paths.
36 * PHP warnings are assumed to be logged rather than output.
38 * @ingroup FileBackend
41 class FSFileBackend
extends FileBackendStore
{
42 protected $basePath; // string; directory holding the container directories
43 /** @var Array Map of container names to root paths */
44 protected $containerPaths = array(); // for custom container paths
45 protected $fileMode; // integer; file permission mode
47 protected $hadWarningErrors = array();
50 * @see FileBackendStore::__construct()
51 * Additional $config params include:
52 * basePath : File system directory that holds containers.
53 * containerPaths : Map of container names to custom file system directories.
54 * This should only be used for backwards-compatibility.
55 * fileMode : Octal UNIX file permissions to use on files stored.
57 public function __construct( array $config ) {
58 parent
::__construct( $config );
60 // Remove any possible trailing slash from directories
61 if ( isset( $config['basePath'] ) ) {
62 $this->basePath
= rtrim( $config['basePath'], '/' ); // remove trailing slash
64 $this->basePath
= null; // none; containers must have explicit paths
67 if ( isset( $config['containerPaths'] ) ) {
68 $this->containerPaths
= (array)$config['containerPaths'];
69 foreach ( $this->containerPaths
as &$path ) {
70 $path = rtrim( $path, '/' ); // remove trailing slash
74 $this->fileMode
= isset( $config['fileMode'] )
80 * @see FileBackendStore::resolveContainerPath()
83 protected function resolveContainerPath( $container, $relStoragePath ) {
84 // Check that container has a root directory
85 if ( isset( $this->containerPaths
[$container] ) ||
isset( $this->basePath
) ) {
86 // Check for sane relative paths (assume the base paths are OK)
87 if ( $this->isLegalRelPath( $relStoragePath ) ) {
88 return $relStoragePath;
95 * Sanity check a relative file system path for validity
97 * @param $path string Normalized relative path
100 protected function isLegalRelPath( $path ) {
101 // Check for file names longer than 255 chars
102 if ( preg_match( '![^/]{256}!', $path ) ) { // ext3/NTFS
105 if ( wfIsWindows() ) { // NTFS
106 return !preg_match( '![:*?"<>|]!', $path );
113 * Given the short (unresolved) and full (resolved) name of
114 * a container, return the file system path of the container.
116 * @param $shortCont string
117 * @param $fullCont string
118 * @return string|null
120 protected function containerFSRoot( $shortCont, $fullCont ) {
121 if ( isset( $this->containerPaths
[$shortCont] ) ) {
122 return $this->containerPaths
[$shortCont];
123 } elseif ( isset( $this->basePath
) ) {
124 return "{$this->basePath}/{$fullCont}";
126 return null; // no container base path defined
130 * Get the absolute file system path for a storage path
132 * @param $storagePath string Storage path
133 * @return string|null
135 protected function resolveToFSPath( $storagePath ) {
136 list( $fullCont, $relPath ) = $this->resolveStoragePathReal( $storagePath );
137 if ( $relPath === null ) {
138 return null; // invalid
140 list( $b, $shortCont, $r ) = FileBackend
::splitStoragePath( $storagePath );
141 $fsPath = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
142 if ( $relPath != '' ) {
143 $fsPath .= "/{$relPath}";
149 * @see FileBackendStore::isPathUsableInternal()
152 public function isPathUsableInternal( $storagePath ) {
153 $fsPath = $this->resolveToFSPath( $storagePath );
154 if ( $fsPath === null ) {
155 return false; // invalid
157 $parentDir = dirname( $fsPath );
159 if ( file_exists( $fsPath ) ) {
160 $ok = is_file( $fsPath ) && is_writable( $fsPath );
162 $ok = is_dir( $parentDir ) && is_writable( $parentDir );
169 * @see FileBackendStore::doStoreInternal()
172 protected function doStoreInternal( array $params ) {
173 $status = Status
::newGood();
175 $dest = $this->resolveToFSPath( $params['dst'] );
176 if ( $dest === null ) {
177 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
181 if ( file_exists( $dest ) ) {
182 if ( !empty( $params['overwrite'] ) ) {
183 $ok = unlink( $dest );
185 $status->fatal( 'backend-fail-delete', $params['dst'] );
189 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
194 if ( !empty( $params['async'] ) ) { // deferred
195 $cmd = implode( ' ', array( wfIsWindows() ?
'COPY' : 'cp',
196 wfEscapeShellArg( $this->cleanPathSlashes( $params['src'] ) ),
197 wfEscapeShellArg( $this->cleanPathSlashes( $dest ) )
199 $status->value
= new FSFileOpHandle( $this, $params, 'Store', $cmd, $dest );
200 } else { // immediate write
201 $ok = copy( $params['src'], $dest );
202 // In some cases (at least over NFS), copy() returns true when it fails
203 if ( !$ok ||
( filesize( $params['src'] ) !== filesize( $dest ) ) ) {
204 if ( $ok ) { // PHP bug
205 unlink( $dest ); // remove broken file
206 trigger_error( __METHOD__
. ": copy() failed but returned true." );
208 $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] );
211 $this->chmod( $dest );
218 * @see FSFileBackend::doExecuteOpHandlesInternal()
220 protected function _getResponseStore( $errors, Status
$status, array $params, $cmd ) {
221 if ( $errors !== '' && !( wfIsWindows() && $errors[0] === " " ) ) {
222 $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] );
223 trigger_error( "$cmd\n$errors", E_USER_WARNING
); // command output
228 * @see FileBackendStore::doCopyInternal()
231 protected function doCopyInternal( array $params ) {
232 $status = Status
::newGood();
234 $source = $this->resolveToFSPath( $params['src'] );
235 if ( $source === null ) {
236 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
240 $dest = $this->resolveToFSPath( $params['dst'] );
241 if ( $dest === null ) {
242 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
246 if ( file_exists( $dest ) ) {
247 if ( !empty( $params['overwrite'] ) ) {
248 $ok = unlink( $dest );
250 $status->fatal( 'backend-fail-delete', $params['dst'] );
254 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
259 if ( !empty( $params['async'] ) ) { // deferred
260 $cmd = implode( ' ', array( wfIsWindows() ?
'COPY' : 'cp',
261 wfEscapeShellArg( $this->cleanPathSlashes( $source ) ),
262 wfEscapeShellArg( $this->cleanPathSlashes( $dest ) )
264 $status->value
= new FSFileOpHandle( $this, $params, 'Copy', $cmd, $dest );
265 } else { // immediate write
266 $ok = copy( $source, $dest );
267 // In some cases (at least over NFS), copy() returns true when it fails
268 if ( !$ok ||
( filesize( $source ) !== filesize( $dest ) ) ) {
269 if ( $ok ) { // PHP bug
270 unlink( $dest ); // remove broken file
271 trigger_error( __METHOD__
. ": copy() failed but returned true." );
273 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
276 $this->chmod( $dest );
283 * @see FSFileBackend::doExecuteOpHandlesInternal()
285 protected function _getResponseCopy( $errors, Status
$status, array $params, $cmd ) {
286 if ( $errors !== '' && !( wfIsWindows() && $errors[0] === " " ) ) {
287 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
288 trigger_error( "$cmd\n$errors", E_USER_WARNING
); // command output
293 * @see FileBackendStore::doMoveInternal()
296 protected function doMoveInternal( array $params ) {
297 $status = Status
::newGood();
299 $source = $this->resolveToFSPath( $params['src'] );
300 if ( $source === null ) {
301 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
305 $dest = $this->resolveToFSPath( $params['dst'] );
306 if ( $dest === null ) {
307 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
311 if ( file_exists( $dest ) ) {
312 if ( !empty( $params['overwrite'] ) ) {
313 // Windows does not support moving over existing files
314 if ( wfIsWindows() ) {
315 $ok = unlink( $dest );
317 $status->fatal( 'backend-fail-delete', $params['dst'] );
322 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
327 if ( !empty( $params['async'] ) ) { // deferred
328 $cmd = implode( ' ', array( wfIsWindows() ?
'MOVE' : 'mv',
329 wfEscapeShellArg( $this->cleanPathSlashes( $source ) ),
330 wfEscapeShellArg( $this->cleanPathSlashes( $dest ) )
332 $status->value
= new FSFileOpHandle( $this, $params, 'Move', $cmd );
333 } else { // immediate write
334 $ok = rename( $source, $dest );
335 clearstatcache(); // file no longer at source
337 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
346 * @see FSFileBackend::doExecuteOpHandlesInternal()
348 protected function _getResponseMove( $errors, Status
$status, array $params, $cmd ) {
349 if ( $errors !== '' && !( wfIsWindows() && $errors[0] === " " ) ) {
350 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
351 trigger_error( "$cmd\n$errors", E_USER_WARNING
); // command output
356 * @see FileBackendStore::doDeleteInternal()
359 protected function doDeleteInternal( array $params ) {
360 $status = Status
::newGood();
362 $source = $this->resolveToFSPath( $params['src'] );
363 if ( $source === null ) {
364 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
368 if ( !is_file( $source ) ) {
369 if ( empty( $params['ignoreMissingSource'] ) ) {
370 $status->fatal( 'backend-fail-delete', $params['src'] );
372 return $status; // do nothing; either OK or bad status
375 if ( !empty( $params['async'] ) ) { // deferred
376 $cmd = implode( ' ', array( wfIsWindows() ?
'DEL' : 'unlink',
377 wfEscapeShellArg( $this->cleanPathSlashes( $source ) )
379 $status->value
= new FSFileOpHandle( $this, $params, 'Copy', $cmd );
380 } else { // immediate write
381 $ok = unlink( $source );
383 $status->fatal( 'backend-fail-delete', $params['src'] );
392 * @see FSFileBackend::doExecuteOpHandlesInternal()
394 protected function _getResponseDelete( $errors, Status
$status, array $params, $cmd ) {
395 if ( $errors !== '' && !( wfIsWindows() && $errors[0] === " " ) ) {
396 $status->fatal( 'backend-fail-delete', $params['src'] );
397 trigger_error( "$cmd\n$errors", E_USER_WARNING
); // command output
402 * @see FileBackendStore::doCreateInternal()
405 protected function doCreateInternal( array $params ) {
406 $status = Status
::newGood();
408 $dest = $this->resolveToFSPath( $params['dst'] );
409 if ( $dest === null ) {
410 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
414 if ( file_exists( $dest ) ) {
415 if ( !empty( $params['overwrite'] ) ) {
416 $ok = unlink( $dest );
418 $status->fatal( 'backend-fail-delete', $params['dst'] );
422 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
427 if ( !empty( $params['async'] ) ) { // deferred
428 $tempFile = TempFSFile
::factory( 'create_', 'tmp' );
430 $status->fatal( 'backend-fail-create', $params['dst'] );
433 $bytes = file_put_contents( $tempFile->getPath(), $params['content'] );
434 if ( $bytes === false ) {
435 $status->fatal( 'backend-fail-create', $params['dst'] );
438 $cmd = implode( ' ', array( wfIsWindows() ?
'COPY' : 'cp',
439 wfEscapeShellArg( $this->cleanPathSlashes( $tempFile->getPath() ) ),
440 wfEscapeShellArg( $this->cleanPathSlashes( $dest ) )
442 $status->value
= new FSFileOpHandle( $this, $params, 'Create', $cmd, $dest );
443 $tempFile->bind( $status->value
);
444 } else { // immediate write
445 $bytes = file_put_contents( $dest, $params['content'] );
446 if ( $bytes === false ) {
447 $status->fatal( 'backend-fail-create', $params['dst'] );
450 $this->chmod( $dest );
457 * @see FSFileBackend::doExecuteOpHandlesInternal()
459 protected function _getResponseCreate( $errors, Status
$status, array $params, $cmd ) {
460 if ( $errors !== '' && !( wfIsWindows() && $errors[0] === " " ) ) {
461 $status->fatal( 'backend-fail-create', $params['dst'] );
462 trigger_error( "$cmd\n$errors", E_USER_WARNING
); // command output
467 * @see FileBackendStore::doPrepareInternal()
470 protected function doPrepareInternal( $fullCont, $dirRel, array $params ) {
471 $status = Status
::newGood();
472 list( $b, $shortCont, $r ) = FileBackend
::splitStoragePath( $params['dir'] );
473 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
474 $dir = ( $dirRel != '' ) ?
"{$contRoot}/{$dirRel}" : $contRoot;
475 if ( !wfMkdirParents( $dir ) ) { // make directory and its parents
476 $status->fatal( 'directorycreateerror', $params['dir'] );
477 } elseif ( !is_writable( $dir ) ) {
478 $status->fatal( 'directoryreadonlyerror', $params['dir'] );
479 } elseif ( !is_readable( $dir ) ) {
480 $status->fatal( 'directorynotreadableerror', $params['dir'] );
486 * @see FileBackendStore::doSecureInternal()
489 protected function doSecureInternal( $fullCont, $dirRel, array $params ) {
490 $status = Status
::newGood();
491 list( $b, $shortCont, $r ) = FileBackend
::splitStoragePath( $params['dir'] );
492 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
493 $dir = ( $dirRel != '' ) ?
"{$contRoot}/{$dirRel}" : $contRoot;
494 // Seed new directories with a blank index.html, to prevent crawling...
495 if ( !empty( $params['noListing'] ) && !file_exists( "{$dir}/index.html" ) ) {
496 $bytes = file_put_contents( "{$dir}/index.html", '' );
498 $status->fatal( 'backend-fail-create', $params['dir'] . '/index.html' );
502 // Add a .htaccess file to the root of the container...
503 if ( !empty( $params['noAccess'] ) ) {
504 if ( !file_exists( "{$contRoot}/.htaccess" ) ) {
505 $bytes = file_put_contents( "{$contRoot}/.htaccess", "Deny from all\n" );
507 $storeDir = "mwstore://{$this->name}/{$shortCont}";
508 $status->fatal( 'backend-fail-create', "{$storeDir}/.htaccess" );
517 * @see FileBackendStore::doCleanInternal()
520 protected function doCleanInternal( $fullCont, $dirRel, array $params ) {
521 $status = Status
::newGood();
522 list( $b, $shortCont, $r ) = FileBackend
::splitStoragePath( $params['dir'] );
523 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
524 $dir = ( $dirRel != '' ) ?
"{$contRoot}/{$dirRel}" : $contRoot;
525 wfSuppressWarnings();
526 if ( is_dir( $dir ) ) {
527 rmdir( $dir ); // remove directory if empty
534 * @see FileBackendStore::doFileExists()
535 * @return array|bool|null
537 protected function doGetFileStat( array $params ) {
538 $source = $this->resolveToFSPath( $params['src'] );
539 if ( $source === null ) {
540 return false; // invalid storage path
543 $this->trapWarnings(); // don't trust 'false' if there were errors
544 $stat = is_file( $source ) ?
stat( $source ) : false; // regular files only
545 $hadError = $this->untrapWarnings();
549 'mtime' => wfTimestamp( TS_MW
, $stat['mtime'] ),
550 'size' => $stat['size']
552 } elseif ( !$hadError ) {
553 return false; // file does not exist
555 return null; // failure
560 * @see FileBackendStore::doClearCache()
562 protected function doClearCache( array $paths = null ) {
563 clearstatcache(); // clear the PHP file stat cache
567 * @see FileBackendStore::doDirectoryExists()
570 protected function doDirectoryExists( $fullCont, $dirRel, array $params ) {
571 list( $b, $shortCont, $r ) = FileBackend
::splitStoragePath( $params['dir'] );
572 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
573 $dir = ( $dirRel != '' ) ?
"{$contRoot}/{$dirRel}" : $contRoot;
575 $this->trapWarnings(); // don't trust 'false' if there were errors
576 $exists = is_dir( $dir );
577 $hadError = $this->untrapWarnings();
579 return $hadError ?
null : $exists;
583 * @see FileBackendStore::getDirectoryListInternal()
586 public function getDirectoryListInternal( $fullCont, $dirRel, array $params ) {
587 list( $b, $shortCont, $r ) = FileBackend
::splitStoragePath( $params['dir'] );
588 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
589 $dir = ( $dirRel != '' ) ?
"{$contRoot}/{$dirRel}" : $contRoot;
590 $exists = is_dir( $dir );
592 wfDebug( __METHOD__
. "() given directory does not exist: '$dir'\n" );
593 return array(); // nothing under this dir
594 } elseif ( !is_readable( $dir ) ) {
595 wfDebug( __METHOD__
. "() given directory is unreadable: '$dir'\n" );
596 return null; // bad permissions?
598 return new FSFileBackendDirList( $dir, $params );
602 * @see FileBackendStore::getFileListInternal()
603 * @return array|FSFileBackendFileList|null
605 public function getFileListInternal( $fullCont, $dirRel, array $params ) {
606 list( $b, $shortCont, $r ) = FileBackend
::splitStoragePath( $params['dir'] );
607 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
608 $dir = ( $dirRel != '' ) ?
"{$contRoot}/{$dirRel}" : $contRoot;
609 $exists = is_dir( $dir );
611 wfDebug( __METHOD__
. "() given directory does not exist: '$dir'\n" );
612 return array(); // nothing under this dir
613 } elseif ( !is_readable( $dir ) ) {
614 wfDebug( __METHOD__
. "() given directory is unreadable: '$dir'\n" );
615 return null; // bad permissions?
617 return new FSFileBackendFileList( $dir, $params );
621 * @see FileBackendStore::getLocalReference()
622 * @return FSFile|null
624 public function getLocalReference( array $params ) {
625 $source = $this->resolveToFSPath( $params['src'] );
626 if ( $source === null ) {
629 return new FSFile( $source );
633 * @see FileBackendStore::getLocalCopy()
634 * @return null|TempFSFile
636 public function getLocalCopy( array $params ) {
637 $source = $this->resolveToFSPath( $params['src'] );
638 if ( $source === null ) {
642 // Create a new temporary file with the same extension...
643 $ext = FileBackend
::extensionFromPath( $params['src'] );
644 $tmpFile = TempFSFile
::factory( wfBaseName( $source ) . '_', $ext );
648 $tmpPath = $tmpFile->getPath();
650 // Copy the source file over the temp file
651 $ok = copy( $source, $tmpPath );
656 $this->chmod( $tmpPath );
662 * @see FileBackendStore::directoriesAreVirtual()
665 protected function directoriesAreVirtual() {
670 * @see FileBackendStore::doExecuteOpHandlesInternal()
671 * @return Array List of corresponding Status objects
673 protected function doExecuteOpHandlesInternal( array $fileOpHandles ) {
677 foreach ( $fileOpHandles as $index => $fileOpHandle ) {
678 $pipes[$index] = popen( "{$fileOpHandle->cmd} 2>&1", 'r' );
682 foreach ( $pipes as $index => $pipe ) {
683 // Result will be empty on success in *NIX. On Windows,
684 // it may be something like " 1 file(s) [copied|moved].".
685 $errs[$index] = stream_get_contents( $pipe );
689 foreach ( $fileOpHandles as $index => $fileOpHandle ) {
690 $status = Status
::newGood();
691 $function = '_getResponse' . $fileOpHandle->call
;
692 $this->$function( $errs[$index], $status, $fileOpHandle->params
, $fileOpHandle->cmd
);
693 $statuses[$index] = $status;
694 if ( $status->isOK() && $fileOpHandle->chmodPath
) {
695 $this->chmod( $fileOpHandle->chmodPath
);
699 clearstatcache(); // files changed
704 * Chmod a file, suppressing the warnings
706 * @param $path string Absolute file system path
707 * @return bool Success
709 protected function chmod( $path ) {
710 wfSuppressWarnings();
711 $ok = chmod( $path, $this->fileMode
);
718 * Clean up directory separators for the given OS
720 * @param $path string FS path
723 protected function cleanPathSlashes( $path ) {
724 return wfIsWindows() ?
strtr( $path, '/', '\\' ) : $path;
728 * Listen for E_WARNING errors and track whether any happen
732 protected function trapWarnings() {
733 $this->hadWarningErrors
[] = false; // push to stack
734 set_error_handler( array( $this, 'handleWarning' ), E_WARNING
);
735 return false; // invoke normal PHP error handler
739 * Stop listening for E_WARNING errors and return true if any happened
743 protected function untrapWarnings() {
744 restore_error_handler(); // restore previous handler
745 return array_pop( $this->hadWarningErrors
); // pop from stack
748 private function handleWarning() {
749 $this->hadWarningErrors
[count( $this->hadWarningErrors
) - 1] = true;
750 return true; // suppress from PHP handler
755 * @see FileBackendStoreOpHandle
757 class FSFileOpHandle
extends FileBackendStoreOpHandle
{
758 public $cmd; // string; shell command
759 public $chmodPath; // string; file to chmod
761 public function __construct( $backend, array $params, $call, $cmd, $chmodPath = null ) {
762 $this->backend
= $backend;
763 $this->params
= $params;
766 $this->chmodPath
= $chmodPath;
771 * Wrapper around RecursiveDirectoryIterator/DirectoryIterator that
772 * catches exception or does any custom behavoir that we may want.
773 * Do not use this class from places outside FSFileBackend.
775 * @ingroup FileBackend
777 abstract class FSFileBackendList
implements Iterator
{
780 protected $suffixStart; // integer
781 protected $pos = 0; // integer
783 protected $params = array();
786 * @param $dir string file system directory
788 public function __construct( $dir, array $params ) {
789 $dir = realpath( $dir ); // normalize
790 $this->suffixStart
= strlen( $dir ) +
1; // size of "path/to/dir/"
791 $this->params
= $params;
794 $this->iter
= $this->initIterator( $dir );
795 } catch ( UnexpectedValueException
$e ) {
796 $this->iter
= null; // bad permissions? deleted?
801 * Return an appropriate iterator object to wrap
803 * @param $dir string file system directory
806 protected function initIterator( $dir ) {
807 if ( !empty( $this->params
['topOnly'] ) ) { // non-recursive
808 # Get an iterator that will get direct sub-nodes
809 return new DirectoryIterator( $dir );
810 } else { // recursive
811 # Get an iterator that will return leaf nodes (non-directories)
812 # RecursiveDirectoryIterator extends FilesystemIterator.
813 # FilesystemIterator::SKIP_DOTS default is inconsistent in PHP 5.3.x.
814 $flags = FilesystemIterator
::CURRENT_AS_SELF | FilesystemIterator
::SKIP_DOTS
;
815 return new RecursiveIteratorIterator(
816 new RecursiveDirectoryIterator( $dir, $flags ),
817 RecursiveIteratorIterator
::CHILD_FIRST
// include dirs
823 * @see Iterator::key()
826 public function key() {
831 * @see Iterator::current()
832 * @return string|bool String or false
834 public function current() {
835 return $this->getRelPath( $this->iter
->current()->getPathname() );
839 * @see Iterator::next()
842 public function next() {
845 $this->filterViaNext();
846 } catch ( UnexpectedValueException
$e ) {
853 * @see Iterator::rewind()
856 public function rewind() {
859 $this->iter
->rewind();
860 $this->filterViaNext();
861 } catch ( UnexpectedValueException
$e ) {
867 * @see Iterator::valid()
870 public function valid() {
871 return $this->iter
&& $this->iter
->valid();
875 * Filter out items by advancing to the next ones
877 protected function filterViaNext() {}
880 * Return only the relative path and normalize slashes to FileBackend-style.
881 * Uses the "real path" since the suffix is based upon that.
883 * @param $path string
886 protected function getRelPath( $path ) {
887 return strtr( substr( realpath( $path ), $this->suffixStart
), '\\', '/' );
891 class FSFileBackendDirList
extends FSFileBackendList
{
892 protected function filterViaNext() {
893 while ( $this->iter
->valid() ) {
894 if ( $this->iter
->current()->isDot() ||
!$this->iter
->current()->isDir() ) {
895 $this->iter
->next(); // skip non-directories and dot files
903 class FSFileBackendFileList
extends FSFileBackendList
{
904 protected function filterViaNext() {
905 while ( $this->iter
->valid() ) {
906 if ( !$this->iter
->current()->isFile() ) {
907 $this->iter
->next(); // skip non-files and dot files