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()
81 * @param $container string
82 * @param $relStoragePath string
85 protected function resolveContainerPath( $container, $relStoragePath ) {
86 // Check that container has a root directory
87 if ( isset( $this->containerPaths
[$container] ) ||
isset( $this->basePath
) ) {
88 // Check for sane relative paths (assume the base paths are OK)
89 if ( $this->isLegalRelPath( $relStoragePath ) ) {
90 return $relStoragePath;
97 * Sanity check a relative file system path for validity
99 * @param $path string Normalized relative path
102 protected function isLegalRelPath( $path ) {
103 // Check for file names longer than 255 chars
104 if ( preg_match( '![^/]{256}!', $path ) ) { // ext3/NTFS
107 if ( wfIsWindows() ) { // NTFS
108 return !preg_match( '![:*?"<>|]!', $path );
115 * Given the short (unresolved) and full (resolved) name of
116 * a container, return the file system path of the container.
118 * @param $shortCont string
119 * @param $fullCont string
120 * @return string|null
122 protected function containerFSRoot( $shortCont, $fullCont ) {
123 if ( isset( $this->containerPaths
[$shortCont] ) ) {
124 return $this->containerPaths
[$shortCont];
125 } elseif ( isset( $this->basePath
) ) {
126 return "{$this->basePath}/{$fullCont}";
128 return null; // no container base path defined
132 * Get the absolute file system path for a storage path
134 * @param $storagePath string Storage path
135 * @return string|null
137 protected function resolveToFSPath( $storagePath ) {
138 list( $fullCont, $relPath ) = $this->resolveStoragePathReal( $storagePath );
139 if ( $relPath === null ) {
140 return null; // invalid
142 list( $b, $shortCont, $r ) = FileBackend
::splitStoragePath( $storagePath );
143 $fsPath = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
144 if ( $relPath != '' ) {
145 $fsPath .= "/{$relPath}";
151 * @see FileBackendStore::isPathUsableInternal()
154 public function isPathUsableInternal( $storagePath ) {
155 $fsPath = $this->resolveToFSPath( $storagePath );
156 if ( $fsPath === null ) {
157 return false; // invalid
159 $parentDir = dirname( $fsPath );
161 if ( file_exists( $fsPath ) ) {
162 $ok = is_file( $fsPath ) && is_writable( $fsPath );
164 $ok = is_dir( $parentDir ) && is_writable( $parentDir );
171 * @see FileBackendStore::doStoreInternal()
174 protected function doStoreInternal( array $params ) {
175 $status = Status
::newGood();
177 $dest = $this->resolveToFSPath( $params['dst'] );
178 if ( $dest === null ) {
179 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
183 if ( file_exists( $dest ) ) {
184 if ( !empty( $params['overwrite'] ) ) {
185 $ok = unlink( $dest );
187 $status->fatal( 'backend-fail-delete', $params['dst'] );
191 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
196 if ( !empty( $params['async'] ) ) { // deferred
197 $cmd = implode( ' ', array( wfIsWindows() ?
'COPY' : 'cp',
198 wfEscapeShellArg( $this->cleanPathSlashes( $params['src'] ) ),
199 wfEscapeShellArg( $this->cleanPathSlashes( $dest ) )
201 $status->value
= new FSFileOpHandle( $this, $params, 'Store', $cmd, $dest );
202 } else { // immediate write
203 $ok = copy( $params['src'], $dest );
204 // In some cases (at least over NFS), copy() returns true when it fails
205 if ( !$ok ||
( filesize( $params['src'] ) !== filesize( $dest ) ) ) {
206 if ( $ok ) { // PHP bug
207 unlink( $dest ); // remove broken file
208 trigger_error( __METHOD__
. ": copy() failed but returned true." );
210 $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] );
213 $this->chmod( $dest );
220 * @see FSFileBackend::doExecuteOpHandlesInternal()
222 protected function _getResponseStore( $errors, Status
$status, array $params, $cmd ) {
223 if ( $errors !== '' && !( wfIsWindows() && $errors[0] === " " ) ) {
224 $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] );
225 trigger_error( "$cmd\n$errors", E_USER_WARNING
); // command output
230 * @see FileBackendStore::doCopyInternal()
233 protected function doCopyInternal( array $params ) {
234 $status = Status
::newGood();
236 $source = $this->resolveToFSPath( $params['src'] );
237 if ( $source === null ) {
238 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
242 $dest = $this->resolveToFSPath( $params['dst'] );
243 if ( $dest === null ) {
244 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
248 if ( file_exists( $dest ) ) {
249 if ( !empty( $params['overwrite'] ) ) {
250 $ok = unlink( $dest );
252 $status->fatal( 'backend-fail-delete', $params['dst'] );
256 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
261 if ( !empty( $params['async'] ) ) { // deferred
262 $cmd = implode( ' ', array( wfIsWindows() ?
'COPY' : 'cp',
263 wfEscapeShellArg( $this->cleanPathSlashes( $source ) ),
264 wfEscapeShellArg( $this->cleanPathSlashes( $dest ) )
266 $status->value
= new FSFileOpHandle( $this, $params, 'Copy', $cmd, $dest );
267 } else { // immediate write
268 $ok = copy( $source, $dest );
269 // In some cases (at least over NFS), copy() returns true when it fails
270 if ( !$ok ||
( filesize( $source ) !== filesize( $dest ) ) ) {
271 if ( $ok ) { // PHP bug
272 unlink( $dest ); // remove broken file
273 trigger_error( __METHOD__
. ": copy() failed but returned true." );
275 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
278 $this->chmod( $dest );
285 * @see FSFileBackend::doExecuteOpHandlesInternal()
287 protected function _getResponseCopy( $errors, Status
$status, array $params, $cmd ) {
288 if ( $errors !== '' && !( wfIsWindows() && $errors[0] === " " ) ) {
289 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
290 trigger_error( "$cmd\n$errors", E_USER_WARNING
); // command output
295 * @see FileBackendStore::doMoveInternal()
298 protected function doMoveInternal( array $params ) {
299 $status = Status
::newGood();
301 $source = $this->resolveToFSPath( $params['src'] );
302 if ( $source === null ) {
303 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
307 $dest = $this->resolveToFSPath( $params['dst'] );
308 if ( $dest === null ) {
309 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
313 if ( file_exists( $dest ) ) {
314 if ( !empty( $params['overwrite'] ) ) {
315 // Windows does not support moving over existing files
316 if ( wfIsWindows() ) {
317 $ok = unlink( $dest );
319 $status->fatal( 'backend-fail-delete', $params['dst'] );
324 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
329 if ( !empty( $params['async'] ) ) { // deferred
330 $cmd = implode( ' ', array( wfIsWindows() ?
'MOVE' : 'mv',
331 wfEscapeShellArg( $this->cleanPathSlashes( $source ) ),
332 wfEscapeShellArg( $this->cleanPathSlashes( $dest ) )
334 $status->value
= new FSFileOpHandle( $this, $params, 'Move', $cmd );
335 } else { // immediate write
336 $ok = rename( $source, $dest );
337 clearstatcache(); // file no longer at source
339 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
348 * @see FSFileBackend::doExecuteOpHandlesInternal()
350 protected function _getResponseMove( $errors, Status
$status, array $params, $cmd ) {
351 if ( $errors !== '' && !( wfIsWindows() && $errors[0] === " " ) ) {
352 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
353 trigger_error( "$cmd\n$errors", E_USER_WARNING
); // command output
358 * @see FileBackendStore::doDeleteInternal()
361 protected function doDeleteInternal( array $params ) {
362 $status = Status
::newGood();
364 $source = $this->resolveToFSPath( $params['src'] );
365 if ( $source === null ) {
366 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
370 if ( !is_file( $source ) ) {
371 if ( empty( $params['ignoreMissingSource'] ) ) {
372 $status->fatal( 'backend-fail-delete', $params['src'] );
374 return $status; // do nothing; either OK or bad status
377 if ( !empty( $params['async'] ) ) { // deferred
378 $cmd = implode( ' ', array( wfIsWindows() ?
'DEL' : 'unlink',
379 wfEscapeShellArg( $this->cleanPathSlashes( $source ) )
381 $status->value
= new FSFileOpHandle( $this, $params, 'Copy', $cmd );
382 } else { // immediate write
383 $ok = unlink( $source );
385 $status->fatal( 'backend-fail-delete', $params['src'] );
394 * @see FSFileBackend::doExecuteOpHandlesInternal()
396 protected function _getResponseDelete( $errors, Status
$status, array $params, $cmd ) {
397 if ( $errors !== '' && !( wfIsWindows() && $errors[0] === " " ) ) {
398 $status->fatal( 'backend-fail-delete', $params['src'] );
399 trigger_error( "$cmd\n$errors", E_USER_WARNING
); // command output
404 * @see FileBackendStore::doCreateInternal()
407 protected function doCreateInternal( array $params ) {
408 $status = Status
::newGood();
410 $dest = $this->resolveToFSPath( $params['dst'] );
411 if ( $dest === null ) {
412 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
416 if ( file_exists( $dest ) ) {
417 if ( !empty( $params['overwrite'] ) ) {
418 $ok = unlink( $dest );
420 $status->fatal( 'backend-fail-delete', $params['dst'] );
424 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
429 if ( !empty( $params['async'] ) ) { // deferred
430 $tempFile = TempFSFile
::factory( 'create_', 'tmp' );
432 $status->fatal( 'backend-fail-create', $params['dst'] );
435 $bytes = file_put_contents( $tempFile->getPath(), $params['content'] );
436 if ( $bytes === false ) {
437 $status->fatal( 'backend-fail-create', $params['dst'] );
440 $cmd = implode( ' ', array( wfIsWindows() ?
'COPY' : 'cp',
441 wfEscapeShellArg( $this->cleanPathSlashes( $tempFile->getPath() ) ),
442 wfEscapeShellArg( $this->cleanPathSlashes( $dest ) )
444 $status->value
= new FSFileOpHandle( $this, $params, 'Create', $cmd, $dest );
445 $tempFile->bind( $status->value
);
446 } else { // immediate write
447 $bytes = file_put_contents( $dest, $params['content'] );
448 if ( $bytes === false ) {
449 $status->fatal( 'backend-fail-create', $params['dst'] );
452 $this->chmod( $dest );
459 * @see FSFileBackend::doExecuteOpHandlesInternal()
461 protected function _getResponseCreate( $errors, Status
$status, array $params, $cmd ) {
462 if ( $errors !== '' && !( wfIsWindows() && $errors[0] === " " ) ) {
463 $status->fatal( 'backend-fail-create', $params['dst'] );
464 trigger_error( "$cmd\n$errors", E_USER_WARNING
); // command output
469 * @see FileBackendStore::doPrepareInternal()
472 protected function doPrepareInternal( $fullCont, $dirRel, array $params ) {
473 $status = Status
::newGood();
474 list( $b, $shortCont, $r ) = FileBackend
::splitStoragePath( $params['dir'] );
475 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
476 $dir = ( $dirRel != '' ) ?
"{$contRoot}/{$dirRel}" : $contRoot;
477 $existed = is_dir( $dir ); // already there?
478 if ( !wfMkdirParents( $dir ) ) { // make directory and its parents
479 $status->fatal( 'directorycreateerror', $params['dir'] ); // fails on races
480 } elseif ( !is_writable( $dir ) ) {
481 $status->fatal( 'directoryreadonlyerror', $params['dir'] );
482 } elseif ( !is_readable( $dir ) ) {
483 $status->fatal( 'directorynotreadableerror', $params['dir'] );
485 if ( is_dir( $dir ) && !$existed ) {
486 // Respect any 'noAccess' or 'noListing' flags...
487 $status->merge( $this->doSecureInternal( $fullCont, $dirRel, $params ) );
493 * @see FileBackendStore::doSecureInternal()
496 protected function doSecureInternal( $fullCont, $dirRel, array $params ) {
497 $status = Status
::newGood();
498 list( $b, $shortCont, $r ) = FileBackend
::splitStoragePath( $params['dir'] );
499 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
500 $dir = ( $dirRel != '' ) ?
"{$contRoot}/{$dirRel}" : $contRoot;
501 // Seed new directories with a blank index.html, to prevent crawling...
502 if ( !empty( $params['noListing'] ) && !file_exists( "{$dir}/index.html" ) ) {
503 $bytes = file_put_contents( "{$dir}/index.html", $this->indexHtmlPrivate() );
504 if ( $bytes === false ) {
505 $status->fatal( 'backend-fail-create', $params['dir'] . '/index.html' );
509 // Add a .htaccess file to the root of the container...
510 if ( !empty( $params['noAccess'] ) && !file_exists( "{$contRoot}/.htaccess" ) ) {
511 $bytes = file_put_contents( "{$contRoot}/.htaccess", $this->htaccessPrivate() );
512 if ( $bytes === false ) {
513 $storeDir = "mwstore://{$this->name}/{$shortCont}";
514 $status->fatal( 'backend-fail-create', "{$storeDir}/.htaccess" );
522 * @see FileBackendStore::doPublishInternal()
525 protected function doPublishInternal( $fullCont, $dirRel, array $params ) {
526 $status = Status
::newGood();
527 list( $b, $shortCont, $r ) = FileBackend
::splitStoragePath( $params['dir'] );
528 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
529 $dir = ( $dirRel != '' ) ?
"{$contRoot}/{$dirRel}" : $contRoot;
530 // Unseed new directories with a blank index.html, to allow crawling...
531 if ( !empty( $params['listing'] ) && is_file( "{$dir}/index.html" ) ) {
532 $exists = ( file_get_contents( "{$dir}/index.html" ) === $this->indexHtmlPrivate() );
533 if ( $exists && !unlink( "{$dir}/index.html" ) ) { // reverse secure()
534 $status->fatal( 'backend-fail-delete', $params['dir'] . '/index.html' );
538 // Remove the .htaccess file from the root of the container...
539 if ( !empty( $params['access'] ) && is_file( "{$contRoot}/.htaccess" ) ) {
540 $exists = ( file_get_contents( "{$contRoot}/.htaccess" ) === $this->htaccessPrivate() );
541 if ( $exists && !unlink( "{$contRoot}/.htaccess" ) ) { // reverse secure()
542 $storeDir = "mwstore://{$this->name}/{$shortCont}";
543 $status->fatal( 'backend-fail-delete', "{$storeDir}/.htaccess" );
551 * @see FileBackendStore::doCleanInternal()
554 protected function doCleanInternal( $fullCont, $dirRel, array $params ) {
555 $status = Status
::newGood();
556 list( $b, $shortCont, $r ) = FileBackend
::splitStoragePath( $params['dir'] );
557 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
558 $dir = ( $dirRel != '' ) ?
"{$contRoot}/{$dirRel}" : $contRoot;
559 wfSuppressWarnings();
560 if ( is_dir( $dir ) ) {
561 rmdir( $dir ); // remove directory if empty
568 * @see FileBackendStore::doFileExists()
569 * @return array|bool|null
571 protected function doGetFileStat( array $params ) {
572 $source = $this->resolveToFSPath( $params['src'] );
573 if ( $source === null ) {
574 return false; // invalid storage path
577 $this->trapWarnings(); // don't trust 'false' if there were errors
578 $stat = is_file( $source ) ?
stat( $source ) : false; // regular files only
579 $hadError = $this->untrapWarnings();
583 'mtime' => wfTimestamp( TS_MW
, $stat['mtime'] ),
584 'size' => $stat['size']
586 } elseif ( !$hadError ) {
587 return false; // file does not exist
589 return null; // failure
594 * @see FileBackendStore::doClearCache()
596 protected function doClearCache( array $paths = null ) {
597 clearstatcache(); // clear the PHP file stat cache
601 * @see FileBackendStore::doDirectoryExists()
604 protected function doDirectoryExists( $fullCont, $dirRel, array $params ) {
605 list( $b, $shortCont, $r ) = FileBackend
::splitStoragePath( $params['dir'] );
606 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
607 $dir = ( $dirRel != '' ) ?
"{$contRoot}/{$dirRel}" : $contRoot;
609 $this->trapWarnings(); // don't trust 'false' if there were errors
610 $exists = is_dir( $dir );
611 $hadError = $this->untrapWarnings();
613 return $hadError ?
null : $exists;
617 * @see FileBackendStore::getDirectoryListInternal()
620 public function getDirectoryListInternal( $fullCont, $dirRel, array $params ) {
621 list( $b, $shortCont, $r ) = FileBackend
::splitStoragePath( $params['dir'] );
622 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
623 $dir = ( $dirRel != '' ) ?
"{$contRoot}/{$dirRel}" : $contRoot;
624 $exists = is_dir( $dir );
626 wfDebug( __METHOD__
. "() given directory does not exist: '$dir'\n" );
627 return array(); // nothing under this dir
628 } elseif ( !is_readable( $dir ) ) {
629 wfDebug( __METHOD__
. "() given directory is unreadable: '$dir'\n" );
630 return null; // bad permissions?
632 return new FSFileBackendDirList( $dir, $params );
636 * @see FileBackendStore::getFileListInternal()
637 * @return array|FSFileBackendFileList|null
639 public function getFileListInternal( $fullCont, $dirRel, array $params ) {
640 list( $b, $shortCont, $r ) = FileBackend
::splitStoragePath( $params['dir'] );
641 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
642 $dir = ( $dirRel != '' ) ?
"{$contRoot}/{$dirRel}" : $contRoot;
643 $exists = is_dir( $dir );
645 wfDebug( __METHOD__
. "() given directory does not exist: '$dir'\n" );
646 return array(); // nothing under this dir
647 } elseif ( !is_readable( $dir ) ) {
648 wfDebug( __METHOD__
. "() given directory is unreadable: '$dir'\n" );
649 return null; // bad permissions?
651 return new FSFileBackendFileList( $dir, $params );
655 * @see FileBackendStore::getLocalReference()
656 * @return FSFile|null
658 public function getLocalReference( array $params ) {
659 $source = $this->resolveToFSPath( $params['src'] );
660 if ( $source === null ) {
663 return new FSFile( $source );
667 * @see FileBackendStore::getLocalCopy()
668 * @return null|TempFSFile
670 public function getLocalCopy( array $params ) {
671 $source = $this->resolveToFSPath( $params['src'] );
672 if ( $source === null ) {
676 // Create a new temporary file with the same extension...
677 $ext = FileBackend
::extensionFromPath( $params['src'] );
678 $tmpFile = TempFSFile
::factory( wfBaseName( $source ) . '_', $ext );
682 $tmpPath = $tmpFile->getPath();
684 // Copy the source file over the temp file
685 $ok = copy( $source, $tmpPath );
690 $this->chmod( $tmpPath );
696 * @see FileBackendStore::directoriesAreVirtual()
699 protected function directoriesAreVirtual() {
704 * @see FileBackendStore::doExecuteOpHandlesInternal()
705 * @return Array List of corresponding Status objects
707 protected function doExecuteOpHandlesInternal( array $fileOpHandles ) {
711 foreach ( $fileOpHandles as $index => $fileOpHandle ) {
712 $pipes[$index] = popen( "{$fileOpHandle->cmd} 2>&1", 'r' );
716 foreach ( $pipes as $index => $pipe ) {
717 // Result will be empty on success in *NIX. On Windows,
718 // it may be something like " 1 file(s) [copied|moved].".
719 $errs[$index] = stream_get_contents( $pipe );
723 foreach ( $fileOpHandles as $index => $fileOpHandle ) {
724 $status = Status
::newGood();
725 $function = '_getResponse' . $fileOpHandle->call
;
726 $this->$function( $errs[$index], $status, $fileOpHandle->params
, $fileOpHandle->cmd
);
727 $statuses[$index] = $status;
728 if ( $status->isOK() && $fileOpHandle->chmodPath
) {
729 $this->chmod( $fileOpHandle->chmodPath
);
733 clearstatcache(); // files changed
738 * Chmod a file, suppressing the warnings
740 * @param $path string Absolute file system path
741 * @return bool Success
743 protected function chmod( $path ) {
744 wfSuppressWarnings();
745 $ok = chmod( $path, $this->fileMode
);
752 * Return the text of an index.html file to hide directory listings
756 protected function indexHtmlPrivate() {
761 * Return the text of a .htaccess file to make a directory private
765 protected function htaccessPrivate() {
766 return "Deny from all\n";
770 * Clean up directory separators for the given OS
772 * @param $path string FS path
775 protected function cleanPathSlashes( $path ) {
776 return wfIsWindows() ?
strtr( $path, '/', '\\' ) : $path;
780 * Listen for E_WARNING errors and track whether any happen
784 protected function trapWarnings() {
785 $this->hadWarningErrors
[] = false; // push to stack
786 set_error_handler( array( $this, 'handleWarning' ), E_WARNING
);
787 return false; // invoke normal PHP error handler
791 * Stop listening for E_WARNING errors and return true if any happened
795 protected function untrapWarnings() {
796 restore_error_handler(); // restore previous handler
797 return array_pop( $this->hadWarningErrors
); // pop from stack
803 private function handleWarning() {
804 $this->hadWarningErrors
[count( $this->hadWarningErrors
) - 1] = true;
805 return true; // suppress from PHP handler
810 * @see FileBackendStoreOpHandle
812 class FSFileOpHandle
extends FileBackendStoreOpHandle
{
813 public $cmd; // string; shell command
814 public $chmodPath; // string; file to chmod
818 * @param $params array
821 * @param $chmodPath null
823 public function __construct( $backend, array $params, $call, $cmd, $chmodPath = null ) {
824 $this->backend
= $backend;
825 $this->params
= $params;
828 $this->chmodPath
= $chmodPath;
833 * Wrapper around RecursiveDirectoryIterator/DirectoryIterator that
834 * catches exception or does any custom behavoir that we may want.
835 * Do not use this class from places outside FSFileBackend.
837 * @ingroup FileBackend
839 abstract class FSFileBackendList
implements Iterator
{
842 protected $suffixStart; // integer
843 protected $pos = 0; // integer
845 protected $params = array();
848 * @param $dir string file system directory
849 * @param $params array
851 public function __construct( $dir, array $params ) {
852 $dir = realpath( $dir ); // normalize
853 $this->suffixStart
= strlen( $dir ) +
1; // size of "path/to/dir/"
854 $this->params
= $params;
857 $this->iter
= $this->initIterator( $dir );
858 } catch ( UnexpectedValueException
$e ) {
859 $this->iter
= null; // bad permissions? deleted?
864 * Return an appropriate iterator object to wrap
866 * @param $dir string file system directory
869 protected function initIterator( $dir ) {
870 if ( !empty( $this->params
['topOnly'] ) ) { // non-recursive
871 # Get an iterator that will get direct sub-nodes
872 return new DirectoryIterator( $dir );
873 } else { // recursive
874 # Get an iterator that will return leaf nodes (non-directories)
875 # RecursiveDirectoryIterator extends FilesystemIterator.
876 # FilesystemIterator::SKIP_DOTS default is inconsistent in PHP 5.3.x.
877 $flags = FilesystemIterator
::CURRENT_AS_SELF | FilesystemIterator
::SKIP_DOTS
;
878 return new RecursiveIteratorIterator(
879 new RecursiveDirectoryIterator( $dir, $flags ),
880 RecursiveIteratorIterator
::CHILD_FIRST
// include dirs
886 * @see Iterator::key()
889 public function key() {
894 * @see Iterator::current()
895 * @return string|bool String or false
897 public function current() {
898 return $this->getRelPath( $this->iter
->current()->getPathname() );
902 * @see Iterator::next()
905 public function next() {
908 $this->filterViaNext();
909 } catch ( UnexpectedValueException
$e ) {
916 * @see Iterator::rewind()
919 public function rewind() {
922 $this->iter
->rewind();
923 $this->filterViaNext();
924 } catch ( UnexpectedValueException
$e ) {
930 * @see Iterator::valid()
933 public function valid() {
934 return $this->iter
&& $this->iter
->valid();
938 * Filter out items by advancing to the next ones
940 protected function filterViaNext() {}
943 * Return only the relative path and normalize slashes to FileBackend-style.
944 * Uses the "real path" since the suffix is based upon that.
946 * @param $path string
949 protected function getRelPath( $path ) {
950 return strtr( substr( realpath( $path ), $this->suffixStart
), '\\', '/' );
954 class FSFileBackendDirList
extends FSFileBackendList
{
955 protected function filterViaNext() {
956 while ( $this->iter
->valid() ) {
957 if ( $this->iter
->current()->isDot() ||
!$this->iter
->current()->isDir() ) {
958 $this->iter
->next(); // skip non-directories and dot files
966 class FSFileBackendFileList
extends FSFileBackendList
{
967 protected function filterViaNext() {
968 while ( $this->iter
->valid() ) {
969 if ( !$this->iter
->current()->isFile() ) {
970 $this->iter
->next(); // skip non-files and dot files