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 /** @var string Directory holding the container directories */
45 /** @var array Map of container names to root paths for custom container paths */
46 protected $containerPaths = array();
48 /** @var int File permission mode */
51 /** @var string Required OS username to own files */
54 /** @var string OS username running this script */
55 protected $currentUser;
58 protected $hadWarningErrors = array();
61 * @see FileBackendStore::__construct()
62 * Additional $config params include:
63 * - basePath : File system directory that holds containers.
64 * - containerPaths : Map of container names to custom file system directories.
65 * This should only be used for backwards-compatibility.
66 * - fileMode : Octal UNIX file permissions to use on files stored.
68 public function __construct( array $config ) {
69 parent
::__construct( $config );
71 // Remove any possible trailing slash from directories
72 if ( isset( $config['basePath'] ) ) {
73 $this->basePath
= rtrim( $config['basePath'], '/' ); // remove trailing slash
75 $this->basePath
= null; // none; containers must have explicit paths
78 if ( isset( $config['containerPaths'] ) ) {
79 $this->containerPaths
= (array)$config['containerPaths'];
80 foreach ( $this->containerPaths
as &$path ) {
81 $path = rtrim( $path, '/' ); // remove trailing slash
85 $this->fileMode
= isset( $config['fileMode'] ) ?
$config['fileMode'] : 0644;
86 if ( isset( $config['fileOwner'] ) && function_exists( 'posix_getuid' ) ) {
87 $this->fileOwner
= $config['fileOwner'];
88 $info = posix_getpwuid( posix_getuid() );
89 $this->currentUser
= $info['name']; // cache this, assuming it doesn't change
93 public function getFeatures() {
94 return !wfIsWindows() ? FileBackend
::ATTR_UNICODE_PATHS
: 0;
97 protected function resolveContainerPath( $container, $relStoragePath ) {
98 // Check that container has a root directory
99 if ( isset( $this->containerPaths
[$container] ) ||
isset( $this->basePath
) ) {
100 // Check for sane relative paths (assume the base paths are OK)
101 if ( $this->isLegalRelPath( $relStoragePath ) ) {
102 return $relStoragePath;
110 * Sanity check a relative file system path for validity
112 * @param string $path Normalized relative path
115 protected function isLegalRelPath( $path ) {
116 // Check for file names longer than 255 chars
117 if ( preg_match( '![^/]{256}!', $path ) ) { // ext3/NTFS
120 if ( wfIsWindows() ) { // NTFS
121 return !preg_match( '![:*?"<>|]!', $path );
128 * Given the short (unresolved) and full (resolved) name of
129 * a container, return the file system path of the container.
131 * @param string $shortCont
132 * @param string $fullCont
133 * @return string|null
135 protected function containerFSRoot( $shortCont, $fullCont ) {
136 if ( isset( $this->containerPaths
[$shortCont] ) ) {
137 return $this->containerPaths
[$shortCont];
138 } elseif ( isset( $this->basePath
) ) {
139 return "{$this->basePath}/{$fullCont}";
142 return null; // no container base path defined
146 * Get the absolute file system path for a storage path
148 * @param string $storagePath Storage path
149 * @return string|null
151 protected function resolveToFSPath( $storagePath ) {
152 list( $fullCont, $relPath ) = $this->resolveStoragePathReal( $storagePath );
153 if ( $relPath === null ) {
154 return null; // invalid
156 list( , $shortCont, ) = FileBackend
::splitStoragePath( $storagePath );
157 $fsPath = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
158 if ( $relPath != '' ) {
159 $fsPath .= "/{$relPath}";
165 public function isPathUsableInternal( $storagePath ) {
166 $fsPath = $this->resolveToFSPath( $storagePath );
167 if ( $fsPath === null ) {
168 return false; // invalid
170 $parentDir = dirname( $fsPath );
172 if ( file_exists( $fsPath ) ) {
173 $ok = is_file( $fsPath ) && is_writable( $fsPath );
175 $ok = is_dir( $parentDir ) && is_writable( $parentDir );
178 if ( $this->fileOwner
!== null && $this->currentUser
!== $this->fileOwner
) {
180 trigger_error( __METHOD__
. ": PHP process owner is not '{$this->fileOwner}'." );
186 protected function doCreateInternal( array $params ) {
187 $status = Status
::newGood();
189 $dest = $this->resolveToFSPath( $params['dst'] );
190 if ( $dest === null ) {
191 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
196 if ( !empty( $params['async'] ) ) { // deferred
197 $tempFile = TempFSFile
::factory( 'create_', 'tmp' );
199 $status->fatal( 'backend-fail-create', $params['dst'] );
203 $this->trapWarnings();
204 $bytes = file_put_contents( $tempFile->getPath(), $params['content'] );
205 $this->untrapWarnings();
206 if ( $bytes === false ) {
207 $status->fatal( 'backend-fail-create', $params['dst'] );
211 $cmd = implode( ' ', array(
212 wfIsWindows() ?
'COPY /B /Y' : 'cp', // (binary, overwrite)
213 wfEscapeShellArg( $this->cleanPathSlashes( $tempFile->getPath() ) ),
214 wfEscapeShellArg( $this->cleanPathSlashes( $dest ) )
216 $status->value
= new FSFileOpHandle( $this, $params, 'Create', $cmd, $dest );
217 $tempFile->bind( $status->value
);
218 } else { // immediate write
219 $this->trapWarnings();
220 $bytes = file_put_contents( $dest, $params['content'] );
221 $this->untrapWarnings();
222 if ( $bytes === false ) {
223 $status->fatal( 'backend-fail-create', $params['dst'] );
227 $this->chmod( $dest );
234 * @see FSFileBackend::doExecuteOpHandlesInternal()
236 protected function getResponseCreate( $errors, Status
$status, array $params, $cmd ) {
237 if ( $errors !== '' && !( wfIsWindows() && $errors[0] === " " ) ) {
238 $status->fatal( 'backend-fail-create', $params['dst'] );
239 trigger_error( "$cmd\n$errors", E_USER_WARNING
); // command output
243 protected function doStoreInternal( array $params ) {
244 $status = Status
::newGood();
246 $dest = $this->resolveToFSPath( $params['dst'] );
247 if ( $dest === null ) {
248 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
253 if ( !empty( $params['async'] ) ) { // deferred
254 $cmd = implode( ' ', array(
255 wfIsWindows() ?
'COPY /B /Y' : 'cp', // (binary, overwrite)
256 wfEscapeShellArg( $this->cleanPathSlashes( $params['src'] ) ),
257 wfEscapeShellArg( $this->cleanPathSlashes( $dest ) )
259 $status->value
= new FSFileOpHandle( $this, $params, 'Store', $cmd, $dest );
260 } else { // immediate write
261 $this->trapWarnings();
262 $ok = copy( $params['src'], $dest );
263 $this->untrapWarnings();
264 // In some cases (at least over NFS), copy() returns true when it fails
265 if ( !$ok ||
( filesize( $params['src'] ) !== filesize( $dest ) ) ) {
266 if ( $ok ) { // PHP bug
267 unlink( $dest ); // remove broken file
268 trigger_error( __METHOD__
. ": copy() failed but returned true." );
270 $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] );
274 $this->chmod( $dest );
281 * @see FSFileBackend::doExecuteOpHandlesInternal()
283 protected function getResponseStore( $errors, Status
$status, array $params, $cmd ) {
284 if ( $errors !== '' && !( wfIsWindows() && $errors[0] === " " ) ) {
285 $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] );
286 trigger_error( "$cmd\n$errors", E_USER_WARNING
); // command output
290 protected function doCopyInternal( array $params ) {
291 $status = Status
::newGood();
293 $source = $this->resolveToFSPath( $params['src'] );
294 if ( $source === null ) {
295 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
300 $dest = $this->resolveToFSPath( $params['dst'] );
301 if ( $dest === null ) {
302 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
307 if ( !is_file( $source ) ) {
308 if ( empty( $params['ignoreMissingSource'] ) ) {
309 $status->fatal( 'backend-fail-copy', $params['src'] );
312 return $status; // do nothing; either OK or bad status
315 if ( !empty( $params['async'] ) ) { // deferred
316 $cmd = implode( ' ', array(
317 wfIsWindows() ?
'COPY /B /Y' : 'cp', // (binary, overwrite)
318 wfEscapeShellArg( $this->cleanPathSlashes( $source ) ),
319 wfEscapeShellArg( $this->cleanPathSlashes( $dest ) )
321 $status->value
= new FSFileOpHandle( $this, $params, 'Copy', $cmd, $dest );
322 } else { // immediate write
323 $this->trapWarnings();
324 $ok = ( $source === $dest ) ?
true : copy( $source, $dest );
325 $this->untrapWarnings();
326 // In some cases (at least over NFS), copy() returns true when it fails
327 if ( !$ok ||
( filesize( $source ) !== filesize( $dest ) ) ) {
328 if ( $ok ) { // PHP bug
329 $this->trapWarnings();
330 unlink( $dest ); // remove broken file
331 $this->untrapWarnings();
332 trigger_error( __METHOD__
. ": copy() failed but returned true." );
334 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
338 $this->chmod( $dest );
345 * @see FSFileBackend::doExecuteOpHandlesInternal()
347 protected function getResponseCopy( $errors, Status
$status, array $params, $cmd ) {
348 if ( $errors !== '' && !( wfIsWindows() && $errors[0] === " " ) ) {
349 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
350 trigger_error( "$cmd\n$errors", E_USER_WARNING
); // command output
354 protected function doMoveInternal( array $params ) {
355 $status = Status
::newGood();
357 $source = $this->resolveToFSPath( $params['src'] );
358 if ( $source === null ) {
359 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
364 $dest = $this->resolveToFSPath( $params['dst'] );
365 if ( $dest === null ) {
366 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
371 if ( !is_file( $source ) ) {
372 if ( empty( $params['ignoreMissingSource'] ) ) {
373 $status->fatal( 'backend-fail-move', $params['src'] );
376 return $status; // do nothing; either OK or bad status
379 if ( !empty( $params['async'] ) ) { // deferred
380 $cmd = implode( ' ', array(
381 wfIsWindows() ?
'MOVE /Y' : 'mv', // (overwrite)
382 wfEscapeShellArg( $this->cleanPathSlashes( $source ) ),
383 wfEscapeShellArg( $this->cleanPathSlashes( $dest ) )
385 $status->value
= new FSFileOpHandle( $this, $params, 'Move', $cmd );
386 } else { // immediate write
387 $this->trapWarnings();
388 $ok = ( $source === $dest ) ?
true : rename( $source, $dest );
389 $this->untrapWarnings();
390 clearstatcache(); // file no longer at source
392 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
402 * @see FSFileBackend::doExecuteOpHandlesInternal()
404 protected function getResponseMove( $errors, Status
$status, array $params, $cmd ) {
405 if ( $errors !== '' && !( wfIsWindows() && $errors[0] === " " ) ) {
406 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
407 trigger_error( "$cmd\n$errors", E_USER_WARNING
); // command output
411 protected function doDeleteInternal( array $params ) {
412 $status = Status
::newGood();
414 $source = $this->resolveToFSPath( $params['src'] );
415 if ( $source === null ) {
416 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
421 if ( !is_file( $source ) ) {
422 if ( empty( $params['ignoreMissingSource'] ) ) {
423 $status->fatal( 'backend-fail-delete', $params['src'] );
426 return $status; // do nothing; either OK or bad status
429 if ( !empty( $params['async'] ) ) { // deferred
430 $cmd = implode( ' ', array(
431 wfIsWindows() ?
'DEL' : 'unlink',
432 wfEscapeShellArg( $this->cleanPathSlashes( $source ) )
434 $status->value
= new FSFileOpHandle( $this, $params, 'Copy', $cmd );
435 } else { // immediate write
436 $this->trapWarnings();
437 $ok = unlink( $source );
438 $this->untrapWarnings();
440 $status->fatal( 'backend-fail-delete', $params['src'] );
450 * @see FSFileBackend::doExecuteOpHandlesInternal()
452 protected function getResponseDelete( $errors, Status
$status, array $params, $cmd ) {
453 if ( $errors !== '' && !( wfIsWindows() && $errors[0] === " " ) ) {
454 $status->fatal( 'backend-fail-delete', $params['src'] );
455 trigger_error( "$cmd\n$errors", E_USER_WARNING
); // command output
460 * @param string $fullCont
461 * @param string $dirRel
462 * @param array $params
465 protected function doPrepareInternal( $fullCont, $dirRel, array $params ) {
466 $status = Status
::newGood();
467 list( , $shortCont, ) = FileBackend
::splitStoragePath( $params['dir'] );
468 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
469 $dir = ( $dirRel != '' ) ?
"{$contRoot}/{$dirRel}" : $contRoot;
470 $existed = is_dir( $dir ); // already there?
471 // Create the directory and its parents as needed...
472 $this->trapWarnings();
473 if ( !wfMkdirParents( $dir ) ) {
474 $status->fatal( 'directorycreateerror', $params['dir'] ); // fails on races
475 } elseif ( !is_writable( $dir ) ) {
476 $status->fatal( 'directoryreadonlyerror', $params['dir'] );
477 } elseif ( !is_readable( $dir ) ) {
478 $status->fatal( 'directorynotreadableerror', $params['dir'] );
480 $this->untrapWarnings();
481 // Respect any 'noAccess' or 'noListing' flags...
482 if ( is_dir( $dir ) && !$existed ) {
483 $status->merge( $this->doSecureInternal( $fullCont, $dirRel, $params ) );
489 protected function doSecureInternal( $fullCont, $dirRel, array $params ) {
490 $status = Status
::newGood();
491 list( , $shortCont, ) = 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 $this->trapWarnings();
497 $bytes = file_put_contents( "{$dir}/index.html", $this->indexHtmlPrivate() );
498 $this->untrapWarnings();
499 if ( $bytes === false ) {
500 $status->fatal( 'backend-fail-create', $params['dir'] . '/index.html' );
503 // Add a .htaccess file to the root of the container...
504 if ( !empty( $params['noAccess'] ) && !file_exists( "{$contRoot}/.htaccess" ) ) {
505 $this->trapWarnings();
506 $bytes = file_put_contents( "{$contRoot}/.htaccess", $this->htaccessPrivate() );
507 $this->untrapWarnings();
508 if ( $bytes === false ) {
509 $storeDir = "mwstore://{$this->name}/{$shortCont}";
510 $status->fatal( 'backend-fail-create', "{$storeDir}/.htaccess" );
517 protected function doPublishInternal( $fullCont, $dirRel, array $params ) {
518 $status = Status
::newGood();
519 list( , $shortCont, ) = FileBackend
::splitStoragePath( $params['dir'] );
520 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
521 $dir = ( $dirRel != '' ) ?
"{$contRoot}/{$dirRel}" : $contRoot;
522 // Unseed new directories with a blank index.html, to allow crawling...
523 if ( !empty( $params['listing'] ) && is_file( "{$dir}/index.html" ) ) {
524 $exists = ( file_get_contents( "{$dir}/index.html" ) === $this->indexHtmlPrivate() );
525 $this->trapWarnings();
526 if ( $exists && !unlink( "{$dir}/index.html" ) ) { // reverse secure()
527 $status->fatal( 'backend-fail-delete', $params['dir'] . '/index.html' );
529 $this->untrapWarnings();
531 // Remove the .htaccess file from the root of the container...
532 if ( !empty( $params['access'] ) && is_file( "{$contRoot}/.htaccess" ) ) {
533 $exists = ( file_get_contents( "{$contRoot}/.htaccess" ) === $this->htaccessPrivate() );
534 $this->trapWarnings();
535 if ( $exists && !unlink( "{$contRoot}/.htaccess" ) ) { // reverse secure()
536 $storeDir = "mwstore://{$this->name}/{$shortCont}";
537 $status->fatal( 'backend-fail-delete', "{$storeDir}/.htaccess" );
539 $this->untrapWarnings();
545 protected function doCleanInternal( $fullCont, $dirRel, array $params ) {
546 $status = Status
::newGood();
547 list( , $shortCont, ) = FileBackend
::splitStoragePath( $params['dir'] );
548 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
549 $dir = ( $dirRel != '' ) ?
"{$contRoot}/{$dirRel}" : $contRoot;
550 $this->trapWarnings();
551 if ( is_dir( $dir ) ) {
552 rmdir( $dir ); // remove directory if empty
554 $this->untrapWarnings();
559 protected function doGetFileStat( array $params ) {
560 $source = $this->resolveToFSPath( $params['src'] );
561 if ( $source === null ) {
562 return false; // invalid storage path
565 $this->trapWarnings(); // don't trust 'false' if there were errors
566 $stat = is_file( $source ) ?
stat( $source ) : false; // regular files only
567 $hadError = $this->untrapWarnings();
571 'mtime' => wfTimestamp( TS_MW
, $stat['mtime'] ),
572 'size' => $stat['size']
574 } elseif ( !$hadError ) {
575 return false; // file does not exist
577 return null; // failure
582 * @see FileBackendStore::doClearCache()
584 protected function doClearCache( array $paths = null ) {
585 clearstatcache(); // clear the PHP file stat cache
588 protected function doDirectoryExists( $fullCont, $dirRel, array $params ) {
589 list( , $shortCont, ) = FileBackend
::splitStoragePath( $params['dir'] );
590 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
591 $dir = ( $dirRel != '' ) ?
"{$contRoot}/{$dirRel}" : $contRoot;
593 $this->trapWarnings(); // don't trust 'false' if there were errors
594 $exists = is_dir( $dir );
595 $hadError = $this->untrapWarnings();
597 return $hadError ?
null : $exists;
601 * @see FileBackendStore::getDirectoryListInternal()
602 * @param string $fullCont
603 * @param string $dirRel
604 * @param array $params
607 public function getDirectoryListInternal( $fullCont, $dirRel, array $params ) {
608 list( , $shortCont, ) = FileBackend
::splitStoragePath( $params['dir'] );
609 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
610 $dir = ( $dirRel != '' ) ?
"{$contRoot}/{$dirRel}" : $contRoot;
611 $exists = is_dir( $dir );
613 wfDebug( __METHOD__
. "() given directory does not exist: '$dir'\n" );
615 return array(); // nothing under this dir
616 } elseif ( !is_readable( $dir ) ) {
617 wfDebug( __METHOD__
. "() given directory is unreadable: '$dir'\n" );
619 return null; // bad permissions?
622 return new FSFileBackendDirList( $dir, $params );
626 * @see FileBackendStore::getFileListInternal()
627 * @param string $fullCont
628 * @param string $dirRel
629 * @param array $params
630 * @return array|FSFileBackendFileList|null
632 public function getFileListInternal( $fullCont, $dirRel, array $params ) {
633 list( , $shortCont, ) = FileBackend
::splitStoragePath( $params['dir'] );
634 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
635 $dir = ( $dirRel != '' ) ?
"{$contRoot}/{$dirRel}" : $contRoot;
636 $exists = is_dir( $dir );
638 wfDebug( __METHOD__
. "() given directory does not exist: '$dir'\n" );
640 return array(); // nothing under this dir
641 } elseif ( !is_readable( $dir ) ) {
642 wfDebug( __METHOD__
. "() given directory is unreadable: '$dir'\n" );
644 return null; // bad permissions?
647 return new FSFileBackendFileList( $dir, $params );
650 protected function doGetLocalReferenceMulti( array $params ) {
651 $fsFiles = array(); // (path => FSFile)
653 foreach ( $params['srcs'] as $src ) {
654 $source = $this->resolveToFSPath( $src );
655 if ( $source === null ||
!is_file( $source ) ) {
656 $fsFiles[$src] = null; // invalid path or file does not exist
658 $fsFiles[$src] = new FSFile( $source );
665 protected function doGetLocalCopyMulti( array $params ) {
666 $tmpFiles = array(); // (path => TempFSFile)
668 foreach ( $params['srcs'] as $src ) {
669 $source = $this->resolveToFSPath( $src );
670 if ( $source === null ) {
671 $tmpFiles[$src] = null; // invalid path
673 // Create a new temporary file with the same extension...
674 $ext = FileBackend
::extensionFromPath( $src );
675 $tmpFile = TempFSFile
::factory( 'localcopy_', $ext );
677 $tmpFiles[$src] = null;
679 $tmpPath = $tmpFile->getPath();
680 // Copy the source file over the temp file
681 $this->trapWarnings();
682 $ok = copy( $source, $tmpPath );
683 $this->untrapWarnings();
685 $tmpFiles[$src] = null;
687 $this->chmod( $tmpPath );
688 $tmpFiles[$src] = $tmpFile;
697 protected function directoriesAreVirtual() {
701 protected function doExecuteOpHandlesInternal( array $fileOpHandles ) {
705 foreach ( $fileOpHandles as $index => $fileOpHandle ) {
706 $pipes[$index] = popen( "{$fileOpHandle->cmd} 2>&1", 'r' );
710 foreach ( $pipes as $index => $pipe ) {
711 // Result will be empty on success in *NIX. On Windows,
712 // it may be something like " 1 file(s) [copied|moved].".
713 $errs[$index] = stream_get_contents( $pipe );
717 foreach ( $fileOpHandles as $index => $fileOpHandle ) {
718 $status = Status
::newGood();
719 $function = 'getResponse' . $fileOpHandle->call
;
720 $this->$function( $errs[$index], $status, $fileOpHandle->params
, $fileOpHandle->cmd
);
721 $statuses[$index] = $status;
722 if ( $status->isOK() && $fileOpHandle->chmodPath
) {
723 $this->chmod( $fileOpHandle->chmodPath
);
727 clearstatcache(); // files changed
732 * Chmod a file, suppressing the warnings
734 * @param string $path Absolute file system path
735 * @return bool Success
737 protected function chmod( $path ) {
738 $this->trapWarnings();
739 $ok = chmod( $path, $this->fileMode
);
740 $this->untrapWarnings();
746 * Return the text of an index.html file to hide directory listings
750 protected function indexHtmlPrivate() {
755 * Return the text of a .htaccess file to make a directory private
759 protected function htaccessPrivate() {
760 return "Deny from all\n";
764 * Clean up directory separators for the given OS
766 * @param string $path FS path
769 protected function cleanPathSlashes( $path ) {
770 return wfIsWindows() ?
strtr( $path, '/', '\\' ) : $path;
774 * Listen for E_WARNING errors and track whether any happen
776 protected function trapWarnings() {
777 $this->hadWarningErrors
[] = false; // push to stack
778 set_error_handler( array( $this, 'handleWarning' ), E_WARNING
);
782 * Stop listening for E_WARNING errors and return true if any happened
786 protected function untrapWarnings() {
787 restore_error_handler(); // restore previous handler
788 return array_pop( $this->hadWarningErrors
); // pop from stack
793 * @param string $errstr
797 public function handleWarning( $errno, $errstr ) {
798 wfDebugLog( 'FSFileBackend', $errstr ); // more detailed error logging
799 $this->hadWarningErrors
[count( $this->hadWarningErrors
) - 1] = true;
801 return true; // suppress from PHP handler
806 * @see FileBackendStoreOpHandle
808 class FSFileOpHandle
extends FileBackendStoreOpHandle
{
809 public $cmd; // string; shell command
810 public $chmodPath; // string; file to chmod
813 * @param FSFileBackend $backend
814 * @param array $params
815 * @param string $call
817 * @param int|null $chmodPath
819 public function __construct(
820 FSFileBackend
$backend, array $params, $call, $cmd, $chmodPath = null
822 $this->backend
= $backend;
823 $this->params
= $params;
826 $this->chmodPath
= $chmodPath;
831 * Wrapper around RecursiveDirectoryIterator/DirectoryIterator that
832 * catches exception or does any custom behavoir that we may want.
833 * Do not use this class from places outside FSFileBackend.
835 * @ingroup FileBackend
837 abstract class FSFileBackendList
implements Iterator
{
842 protected $suffixStart;
848 protected $params = array();
851 * @param string $dir file system directory
852 * @param array $params
854 public function __construct( $dir, array $params ) {
855 $path = realpath( $dir ); // normalize
856 if ( $path === false ) {
859 $this->suffixStart
= strlen( $path ) +
1; // size of "path/to/dir/"
860 $this->params
= $params;
863 $this->iter
= $this->initIterator( $path );
864 } catch ( UnexpectedValueException
$e ) {
865 $this->iter
= null; // bad permissions? deleted?
870 * Return an appropriate iterator object to wrap
872 * @param string $dir file system directory
875 protected function initIterator( $dir ) {
876 if ( !empty( $this->params
['topOnly'] ) ) { // non-recursive
877 # Get an iterator that will get direct sub-nodes
878 return new DirectoryIterator( $dir );
879 } else { // recursive
880 # Get an iterator that will return leaf nodes (non-directories)
881 # RecursiveDirectoryIterator extends FilesystemIterator.
882 # FilesystemIterator::SKIP_DOTS default is inconsistent in PHP 5.3.x.
883 $flags = FilesystemIterator
::CURRENT_AS_SELF | FilesystemIterator
::SKIP_DOTS
;
885 return new RecursiveIteratorIterator(
886 new RecursiveDirectoryIterator( $dir, $flags ),
887 RecursiveIteratorIterator
::CHILD_FIRST
// include dirs
893 * @see Iterator::key()
896 public function key() {
901 * @see Iterator::current()
902 * @return string|bool String or false
904 public function current() {
905 return $this->getRelPath( $this->iter
->current()->getPathname() );
909 * @see Iterator::next()
910 * @throws FileBackendError
912 public function next() {
915 $this->filterViaNext();
916 } catch ( UnexpectedValueException
$e ) { // bad permissions? deleted?
917 throw new FileBackendError( "File iterator gave UnexpectedValueException." );
923 * @see Iterator::rewind()
924 * @throws FileBackendError
926 public function rewind() {
929 $this->iter
->rewind();
930 $this->filterViaNext();
931 } catch ( UnexpectedValueException
$e ) { // bad permissions? deleted?
932 throw new FileBackendError( "File iterator gave UnexpectedValueException." );
937 * @see Iterator::valid()
940 public function valid() {
941 return $this->iter
&& $this->iter
->valid();
945 * Filter out items by advancing to the next ones
947 protected function filterViaNext() {
951 * Return only the relative path and normalize slashes to FileBackend-style.
952 * Uses the "real path" since the suffix is based upon that.
957 protected function getRelPath( $dir ) {
958 $path = realpath( $dir );
959 if ( $path === false ) {
963 return strtr( substr( $path, $this->suffixStart
), '\\', '/' );
967 class FSFileBackendDirList
extends FSFileBackendList
{
968 protected function filterViaNext() {
969 while ( $this->iter
->valid() ) {
970 if ( $this->iter
->current()->isDot() ||
!$this->iter
->current()->isDir() ) {
971 $this->iter
->next(); // skip non-directories and dot files
979 class FSFileBackendFileList
extends FSFileBackendList
{
980 protected function filterViaNext() {
981 while ( $this->iter
->valid() ) {
982 if ( !$this->iter
->current()->isFile() ) {
983 $this->iter
->next(); // skip non-files and dot files