9 * Class for a file system (FS) based file backend.
11 * All "containers" each map to a directory under the backend's base directory.
12 * For backwards-compatibility, some container paths can be set to custom paths.
13 * The wiki ID will not be used in any custom paths, so this should be avoided.
15 * Having directories with thousands of files will diminish performance.
16 * Sharding can be accomplished by using FileRepo-style hash paths.
18 * Status messages should avoid mentioning the internal FS paths.
19 * Likewise, error suppression should be used to avoid path disclosure.
21 * @ingroup FileBackend
24 class FSFileBackend
extends FileBackendStore
{
25 protected $basePath; // string; directory holding the container directories
26 /** @var Array Map of container names to root paths */
27 protected $containerPaths = array(); // for custom container paths
28 protected $fileMode; // integer; file permission mode
30 protected $hadWarningErrors = array();
33 * @see FileBackendStore::__construct()
34 * Additional $config params include:
35 * basePath : File system directory that holds containers.
36 * containerPaths : Map of container names to custom file system directories.
37 * This should only be used for backwards-compatibility.
38 * fileMode : Octal UNIX file permissions to use on files stored.
40 public function __construct( array $config ) {
41 parent
::__construct( $config );
43 // Remove any possible trailing slash from directories
45 if ( isset( $config['basePath'] ) ) {
46 $this->basePath
= rtrim( $config['basePath'], '/' ); // remove trailing slash
48 $this->basePath
= null; // none; containers must have explicit paths
51 if( isset( $config['containerPaths'] ) ) {
52 $this->containerPaths
= (array)$config['containerPaths'];
53 foreach ( $this->containerPaths
as &$path ) {
54 $path = rtrim( $path, '/' ); // remove trailing slash
58 $this->fileMode
= isset( $config['fileMode'] )
64 * @see FileBackendStore::resolveContainerPath()
66 protected function resolveContainerPath( $container, $relStoragePath ) {
67 // Check that container has a root directory
68 if ( isset( $this->containerPaths
[$container] ) ||
isset( $this->basePath
) ) {
69 // Check for sane relative paths (assume the base paths are OK)
70 if ( $this->isLegalRelPath( $relStoragePath ) ) {
71 return $relStoragePath;
78 * Sanity check a relative file system path for validity
80 * @param $path string Normalized relative path
83 protected function isLegalRelPath( $path ) {
84 // Check for file names longer than 255 chars
85 if ( preg_match( '![^/]{256}!', $path ) ) { // ext3/NTFS
88 if ( wfIsWindows() ) { // NTFS
89 return !preg_match( '![:*?"<>|]!', $path );
96 * Given the short (unresolved) and full (resolved) name of
97 * a container, return the file system path of the container.
99 * @param $shortCont string
100 * @param $fullCont string
101 * @return string|null
103 protected function containerFSRoot( $shortCont, $fullCont ) {
104 if ( isset( $this->containerPaths
[$shortCont] ) ) {
105 return $this->containerPaths
[$shortCont];
106 } elseif ( isset( $this->basePath
) ) {
107 return "{$this->basePath}/{$fullCont}";
109 return null; // no container base path defined
113 * Get the absolute file system path for a storage path
115 * @param $storagePath string Storage path
116 * @return string|null
118 protected function resolveToFSPath( $storagePath ) {
119 list( $fullCont, $relPath ) = $this->resolveStoragePathReal( $storagePath );
120 if ( $relPath === null ) {
121 return null; // invalid
123 list( $b, $shortCont, $r ) = FileBackend
::splitStoragePath( $storagePath );
124 $fsPath = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
125 if ( $relPath != '' ) {
126 $fsPath .= "/{$relPath}";
132 * @see FileBackendStore::isPathUsableInternal()
134 public function isPathUsableInternal( $storagePath ) {
135 $fsPath = $this->resolveToFSPath( $storagePath );
136 if ( $fsPath === null ) {
137 return false; // invalid
139 $parentDir = dirname( $fsPath );
141 wfSuppressWarnings();
142 if ( file_exists( $fsPath ) ) {
143 $ok = is_file( $fsPath ) && is_writable( $fsPath );
145 $ok = is_dir( $parentDir ) && is_writable( $parentDir );
153 * @see FileBackendStore::doStoreInternal()
155 protected function doStoreInternal( array $params ) {
156 $status = Status
::newGood();
158 $dest = $this->resolveToFSPath( $params['dst'] );
159 if ( $dest === null ) {
160 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
164 if ( file_exists( $dest ) ) {
165 if ( !empty( $params['overwrite'] ) ) {
166 wfSuppressWarnings();
167 $ok = unlink( $dest );
170 $status->fatal( 'backend-fail-delete', $params['dst'] );
174 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
179 wfSuppressWarnings();
180 $ok = copy( $params['src'], $dest );
183 $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] );
187 $this->chmod( $dest );
193 * @see FileBackendStore::doCopyInternal()
195 protected function doCopyInternal( array $params ) {
196 $status = Status
::newGood();
198 $source = $this->resolveToFSPath( $params['src'] );
199 if ( $source === null ) {
200 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
204 $dest = $this->resolveToFSPath( $params['dst'] );
205 if ( $dest === null ) {
206 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
210 if ( file_exists( $dest ) ) {
211 if ( !empty( $params['overwrite'] ) ) {
212 wfSuppressWarnings();
213 $ok = unlink( $dest );
216 $status->fatal( 'backend-fail-delete', $params['dst'] );
220 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
225 wfSuppressWarnings();
226 $ok = copy( $source, $dest );
229 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
233 $this->chmod( $dest );
239 * @see FileBackendStore::doMoveInternal()
241 protected function doMoveInternal( array $params ) {
242 $status = Status
::newGood();
244 $source = $this->resolveToFSPath( $params['src'] );
245 if ( $source === null ) {
246 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
250 $dest = $this->resolveToFSPath( $params['dst'] );
251 if ( $dest === null ) {
252 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
256 if ( file_exists( $dest ) ) {
257 if ( !empty( $params['overwrite'] ) ) {
258 // Windows does not support moving over existing files
259 if ( wfIsWindows() ) {
260 wfSuppressWarnings();
261 $ok = unlink( $dest );
264 $status->fatal( 'backend-fail-delete', $params['dst'] );
269 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
274 wfSuppressWarnings();
275 $ok = rename( $source, $dest );
276 clearstatcache(); // file no longer at source
279 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
287 * @see FileBackendStore::doDeleteInternal()
289 protected function doDeleteInternal( array $params ) {
290 $status = Status
::newGood();
292 $source = $this->resolveToFSPath( $params['src'] );
293 if ( $source === null ) {
294 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
298 if ( !is_file( $source ) ) {
299 if ( empty( $params['ignoreMissingSource'] ) ) {
300 $status->fatal( 'backend-fail-delete', $params['src'] );
302 return $status; // do nothing; either OK or bad status
305 wfSuppressWarnings();
306 $ok = unlink( $source );
309 $status->fatal( 'backend-fail-delete', $params['src'] );
317 * @see FileBackendStore::doCreateInternal()
319 protected function doCreateInternal( array $params ) {
320 $status = Status
::newGood();
322 $dest = $this->resolveToFSPath( $params['dst'] );
323 if ( $dest === null ) {
324 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
328 if ( file_exists( $dest ) ) {
329 if ( !empty( $params['overwrite'] ) ) {
330 wfSuppressWarnings();
331 $ok = unlink( $dest );
334 $status->fatal( 'backend-fail-delete', $params['dst'] );
338 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
343 wfSuppressWarnings();
344 $bytes = file_put_contents( $dest, $params['content'] );
346 if ( $bytes === false ) {
347 $status->fatal( 'backend-fail-create', $params['dst'] );
351 $this->chmod( $dest );
357 * @see FileBackendStore::doPrepareInternal()
359 protected function doPrepareInternal( $fullCont, $dirRel, array $params ) {
360 $status = Status
::newGood();
361 list( $b, $shortCont, $r ) = FileBackend
::splitStoragePath( $params['dir'] );
362 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
363 $dir = ( $dirRel != '' ) ?
"{$contRoot}/{$dirRel}" : $contRoot;
364 if ( !wfMkdirParents( $dir ) ) { // make directory and its parents
365 $status->fatal( 'directorycreateerror', $params['dir'] );
366 } elseif ( !is_writable( $dir ) ) {
367 $status->fatal( 'directoryreadonlyerror', $params['dir'] );
368 } elseif ( !is_readable( $dir ) ) {
369 $status->fatal( 'directorynotreadableerror', $params['dir'] );
375 * @see FileBackendStore::doSecureInternal()
377 protected function doSecureInternal( $fullCont, $dirRel, array $params ) {
378 $status = Status
::newGood();
379 list( $b, $shortCont, $r ) = FileBackend
::splitStoragePath( $params['dir'] );
380 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
381 $dir = ( $dirRel != '' ) ?
"{$contRoot}/{$dirRel}" : $contRoot;
382 // Seed new directories with a blank index.html, to prevent crawling...
383 if ( !empty( $params['noListing'] ) && !file_exists( "{$dir}/index.html" ) ) {
384 wfSuppressWarnings();
385 $bytes = file_put_contents( "{$dir}/index.html", '' );
388 $status->fatal( 'backend-fail-create', $params['dir'] . '/index.html' );
392 // Add a .htaccess file to the root of the container...
393 if ( !empty( $params['noAccess'] ) ) {
394 if ( !file_exists( "{$contRoot}/.htaccess" ) ) {
395 wfSuppressWarnings();
396 $bytes = file_put_contents( "{$contRoot}/.htaccess", "Deny from all\n" );
399 $storeDir = "mwstore://{$this->name}/{$shortCont}";
400 $status->fatal( 'backend-fail-create', "{$storeDir}/.htaccess" );
409 * @see FileBackendStore::doCleanInternal()
411 protected function doCleanInternal( $fullCont, $dirRel, array $params ) {
412 $status = Status
::newGood();
413 list( $b, $shortCont, $r ) = FileBackend
::splitStoragePath( $params['dir'] );
414 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
415 $dir = ( $dirRel != '' ) ?
"{$contRoot}/{$dirRel}" : $contRoot;
416 wfSuppressWarnings();
417 if ( is_dir( $dir ) ) {
418 rmdir( $dir ); // remove directory if empty
425 * @see FileBackendStore::doFileExists()
427 protected function doGetFileStat( array $params ) {
428 $source = $this->resolveToFSPath( $params['src'] );
429 if ( $source === null ) {
430 return false; // invalid storage path
433 $this->trapWarnings();
434 $stat = is_file( $source ) ?
stat( $source ) : false; // regular files only
435 $hadError = $this->untrapWarnings();
439 'mtime' => wfTimestamp( TS_MW
, $stat['mtime'] ),
440 'size' => $stat['size']
442 } elseif ( !$hadError ) {
443 return false; // file does not exist
445 return null; // failure
450 * @see FileBackendStore::doClearCache()
452 protected function doClearCache( array $paths = null ) {
453 clearstatcache(); // clear the PHP file stat cache
457 * @see FileBackendStore::getFileListInternal()
459 public function getFileListInternal( $fullCont, $dirRel, array $params ) {
460 list( $b, $shortCont, $r ) = FileBackend
::splitStoragePath( $params['dir'] );
461 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
462 $dir = ( $dirRel != '' ) ?
"{$contRoot}/{$dirRel}" : $contRoot;
463 wfSuppressWarnings();
464 $exists = is_dir( $dir );
467 wfDebug( __METHOD__
. "() given directory does not exist: '$dir'\n" );
468 return array(); // nothing under this dir
470 wfSuppressWarnings();
471 $readable = is_readable( $dir );
474 wfDebug( __METHOD__
. "() given directory is unreadable: '$dir'\n" );
475 return null; // bad permissions?
477 return new FSFileBackendFileList( $dir );
481 * @see FileBackendStore::getLocalReference()
483 public function getLocalReference( array $params ) {
484 $source = $this->resolveToFSPath( $params['src'] );
485 if ( $source === null ) {
488 return new FSFile( $source );
492 * @see FileBackendStore::getLocalCopy()
494 public function getLocalCopy( array $params ) {
495 $source = $this->resolveToFSPath( $params['src'] );
496 if ( $source === null ) {
500 // Create a new temporary file with the same extension...
501 $ext = FileBackend
::extensionFromPath( $params['src'] );
502 $tmpFile = TempFSFile
::factory( wfBaseName( $source ) . '_', $ext );
506 $tmpPath = $tmpFile->getPath();
508 // Copy the source file over the temp file
509 wfSuppressWarnings();
510 $ok = copy( $source, $tmpPath );
516 $this->chmod( $tmpPath );
522 * Chmod a file, suppressing the warnings
524 * @param $path string Absolute file system path
525 * @return bool Success
527 protected function chmod( $path ) {
528 wfSuppressWarnings();
529 $ok = chmod( $path, $this->fileMode
);
536 * Suppress E_WARNING errors and track whether any happen
540 protected function trapWarnings() {
541 $this->hadWarningErrors
[] = false; // push to stack
542 set_error_handler( array( $this, 'handleWarning' ), E_WARNING
);
546 * Unsuppress E_WARNING errors and return true if any happened
550 protected function untrapWarnings() {
551 restore_error_handler(); // restore previous handler
552 return array_pop( $this->hadWarningErrors
); // pop from stack
555 private function handleWarning() {
556 $this->hadWarningErrors
[count( $this->hadWarningErrors
) - 1] = true;
557 return true; // suppress from PHP handler
562 * Wrapper around RecursiveDirectoryIterator that catches
563 * exception or does any custom behavoir that we may want.
564 * Do not use this class from places outside FSFileBackend.
566 * @ingroup FileBackend
568 class FSFileBackendFileList
implements Iterator
{
569 /** @var RecursiveIteratorIterator */
571 protected $suffixStart; // integer
572 protected $pos = 0; // integer
575 * @param $dir string file system directory
577 public function __construct( $dir ) {
578 $dir = realpath( $dir ); // normalize
579 $this->suffixStart
= strlen( $dir ) +
1; // size of "path/to/dir/"
581 # Get an iterator that will return leaf nodes (non-directories)
582 if ( MWInit
::classExists( 'FilesystemIterator' ) ) { // PHP >= 5.3
583 # RecursiveDirectoryIterator extends FilesystemIterator.
584 # FilesystemIterator::SKIP_DOTS default is inconsistent in PHP 5.3.x.
585 $flags = FilesystemIterator
::CURRENT_AS_FILEINFO | FilesystemIterator
::SKIP_DOTS
;
586 $this->iter
= new RecursiveIteratorIterator(
587 new RecursiveDirectoryIterator( $dir, $flags ) );
588 } else { // PHP < 5.3
589 # RecursiveDirectoryIterator extends DirectoryIterator
590 $this->iter
= new RecursiveIteratorIterator(
591 new RecursiveDirectoryIterator( $dir ) );
593 } catch ( UnexpectedValueException
$e ) {
594 $this->iter
= null; // bad permissions? deleted?
598 public function current() {
599 // Return only the relative path and normalize slashes to FileBackend-style
600 // Make sure to use the realpath since the suffix is based upon that
601 return str_replace( '\\', '/',
602 substr( realpath( $this->iter
->current() ), $this->suffixStart
) );
605 public function key() {
609 public function next() {
612 } catch ( UnexpectedValueException
$e ) {
618 public function rewind() {
621 $this->iter
->rewind();
622 } catch ( UnexpectedValueException
$e ) {
627 public function valid() {
628 return $this->iter
&& $this->iter
->valid();