9 * Class for a file system based file backend.
10 * Containers are just directories and container sharding is not supported.
11 * Also, for backwards-compatibility, the wiki ID prefix is not used.
12 * Users of this class should set wiki-specific container paths as needed.
14 * Status messages should avoid mentioning the internal FS paths.
15 * Likewise, error suppression should be used to avoid path disclosure.
17 * @ingroup FileBackend
19 class FSFileBackend
extends FileBackend
{
20 /** @var Array Map of container names to paths */
21 protected $containerPaths = array();
22 protected $fileMode; // file permission mode
25 * @see FileBackend::__construct()
26 * Additional $config params include:
27 * containerPaths : Map of container names to absolute file system paths
28 * fileMode : Octal UNIX file permissions to use on files stored
30 public function __construct( array $config ) {
31 parent
::__construct( $config );
32 $this->containerPaths
= (array)$config['containerPaths'];
33 foreach ( $this->containerPaths
as &$path ) {
34 if ( substr( $path, -1 ) === '/' ) {
35 $path = substr( $path, 0, -1 ); // remove trailing slash
38 $this->fileMode
= isset( $config['fileMode'] )
44 * @see FileBackend::resolveContainerPath()
46 protected function resolveContainerPath( $container, $relStoragePath ) {
47 // Get absolute path given the container base dir
48 if ( isset( $this->containerPaths
[$container] ) ) {
49 return $this->containerPaths
[$container] . "/{$relStoragePath}";
55 * @see FileBackend::doStoreInternal()
57 protected function doStoreInternal( array $params ) {
58 $status = Status
::newGood();
60 list( $c, $dest ) = $this->resolveStoragePathReal( $params['dst'] );
61 if ( $dest === null ) {
62 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
65 if ( file_exists( $dest ) ) {
66 if ( !empty( $params['overwriteDest'] ) ) {
68 $ok = unlink( $dest );
71 $status->fatal( 'backend-fail-delete', $params['dst'] );
75 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
79 if ( !wfMkdirParents( dirname( $dest ) ) ) {
80 $status->fatal( 'directorycreateerror', $params['dst'] );
86 $ok = copy( $params['src'], $dest );
89 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
93 $this->chmod( $dest );
99 * @see FileBackend::doCopyInternal()
101 protected function doCopyInternal( array $params ) {
102 $status = Status
::newGood();
104 list( $c, $source ) = $this->resolveStoragePathReal( $params['src'] );
105 if ( $source === null ) {
106 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
110 list( $c, $dest ) = $this->resolveStoragePathReal( $params['dst'] );
111 if ( $dest === null ) {
112 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
116 if ( file_exists( $dest ) ) {
117 if ( !empty( $params['overwriteDest'] ) ) {
118 wfSuppressWarnings();
119 $ok = unlink( $dest );
122 $status->fatal( 'backend-fail-delete', $params['dst'] );
126 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
130 if ( !wfMkdirParents( dirname( $dest ) ) ) {
131 $status->fatal( 'directorycreateerror', $params['dst'] );
136 wfSuppressWarnings();
137 $ok = copy( $source, $dest );
140 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
144 $this->chmod( $dest );
150 * @see FileBackend::doMoveInternal()
152 protected function doMoveInternal( array $params ) {
153 $status = Status
::newGood();
155 list( $c, $source ) = $this->resolveStoragePathReal( $params['src'] );
156 if ( $source === null ) {
157 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
160 list( $c, $dest ) = $this->resolveStoragePathReal( $params['dst'] );
161 if ( $dest === null ) {
162 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
166 if ( file_exists( $dest ) ) {
167 if ( !empty( $params['overwriteDest'] ) ) {
168 // Windows does not support moving over existing files
169 if ( wfIsWindows() ) {
170 wfSuppressWarnings();
171 $ok = unlink( $dest );
174 $status->fatal( 'backend-fail-delete', $params['dst'] );
179 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
183 if ( !wfMkdirParents( dirname( $dest ) ) ) {
184 $status->fatal( 'directorycreateerror', $params['dst'] );
189 wfSuppressWarnings();
190 $ok = rename( $source, $dest );
191 clearstatcache(); // file no longer at source
194 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
202 * @see FileBackend::doDeleteInternal()
204 protected function doDeleteInternal( array $params ) {
205 $status = Status
::newGood();
207 list( $c, $source ) = $this->resolveStoragePathReal( $params['src'] );
208 if ( $source === null ) {
209 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
213 if ( !is_file( $source ) ) {
214 if ( empty( $params['ignoreMissingSource'] ) ) {
215 $status->fatal( 'backend-fail-delete', $params['src'] );
217 return $status; // do nothing; either OK or bad status
220 wfSuppressWarnings();
221 $ok = unlink( $source );
224 $status->fatal( 'backend-fail-delete', $params['src'] );
232 * @see FileBackend::doCreateInternal()
234 protected function doCreateInternal( array $params ) {
235 $status = Status
::newGood();
237 list( $c, $dest ) = $this->resolveStoragePathReal( $params['dst'] );
238 if ( $dest === null ) {
239 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
243 if ( file_exists( $dest ) ) {
244 if ( !empty( $params['overwriteDest'] ) ) {
245 wfSuppressWarnings();
246 $ok = unlink( $dest );
249 $status->fatal( 'backend-fail-delete', $params['dst'] );
253 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
257 if ( !wfMkdirParents( dirname( $dest ) ) ) {
258 $status->fatal( 'directorycreateerror', $params['dst'] );
263 wfSuppressWarnings();
264 $ok = file_put_contents( $dest, $params['content'] );
267 $status->fatal( 'backend-fail-create', $params['dst'] );
271 $this->chmod( $dest );
277 * @see FileBackend::doPrepareInternal()
279 protected function doPrepareInternal( $container, $dir, array $params ) {
280 $status = Status
::newGood();
281 if ( !wfMkdirParents( $dir ) ) {
282 $status->fatal( 'directorycreateerror', $params['dir'] );
283 } elseif ( !is_writable( $dir ) ) {
284 $status->fatal( 'directoryreadonlyerror', $params['dir'] );
285 } elseif ( !is_readable( $dir ) ) {
286 $status->fatal( 'directorynotreadableerror', $params['dir'] );
292 * @see FileBackend::doSecureInternal()
294 protected function doSecureInternal( $container, $dir, array $params ) {
295 $status = Status
::newGood();
296 if ( !wfMkdirParents( $dir ) ) {
297 $status->fatal( 'directorycreateerror', $params['dir'] );
300 // Seed new directories with a blank index.html, to prevent crawling...
301 if ( !empty( $params['noListing'] ) && !file_exists( "{$dir}/index.html" ) ) {
302 wfSuppressWarnings();
303 $ok = file_put_contents( "{$dir}/index.html", '' );
306 $status->fatal( 'backend-fail-create', $params['dir'] . '/index.html' );
310 // Add a .htaccess file to the root of the container...
311 if ( !empty( $params['noAccess'] ) ) {
312 list( $b, $container, $r ) = FileBackend
::splitStoragePath( $params['dir'] );
313 $dirRoot = $this->containerPaths
[$container]; // real path
314 if ( !file_exists( "{$dirRoot}/.htaccess" ) ) {
315 wfSuppressWarnings();
316 $ok = file_put_contents( "{$dirRoot}/.htaccess", "Deny from all\n" );
319 $storeDir = "mwstore://{$this->name}/{$container}";
320 $status->fatal( 'backend-fail-create', "$storeDir/.htaccess" );
329 * @see FileBackend::doCleanInternal()
331 protected function doCleanInternal( $container, $dir, array $params ) {
332 $status = Status
::newGood();
333 wfSuppressWarnings();
334 if ( is_dir( $dir ) ) {
335 rmdir( $dir ); // remove directory if empty
342 * @see FileBackend::doFileExists()
344 protected function doGetFileStat( array $params ) {
345 list( $c, $source ) = $this->resolveStoragePathReal( $params['src'] );
346 if ( $source === null ) {
347 return false; // invalid storage path
350 wfSuppressWarnings();
351 if ( is_file( $source ) ) { // regular file?
352 $stat = stat( $source );
360 'mtime' => wfTimestamp( TS_MW
, $stat['mtime'] ),
361 'size' => $stat['size']
369 * @see FileBackend::getFileListInternal()
371 public function getFileListInternal( $container, $dir, array $params ) {
372 wfSuppressWarnings();
373 $exists = is_dir( $dir );
376 return array(); // nothing under this dir
378 wfSuppressWarnings();
379 $readable = is_readable( $dir );
382 return null; // bad permissions?
384 return new FSFileIterator( $dir );
388 * @see FileBackend::getLocalReference()
390 public function getLocalReference( array $params ) {
391 list( $c, $source ) = $this->resolveStoragePathReal( $params['src'] );
392 if ( $source === null ) {
395 return new FSFile( $source );
399 * @see FileBackend::getLocalCopy()
401 public function getLocalCopy( array $params ) {
402 list( $c, $source ) = $this->resolveStoragePathReal( $params['src'] );
403 if ( $source === null ) {
407 // Create a new temporary file with the same extension...
408 $ext = FileBackend
::extensionFromPath( $params['src'] );
409 $tmpFile = TempFSFile
::factory( wfBaseName( $source ) . '_', $ext );
413 $tmpPath = $tmpFile->getPath();
415 // Copy the source file over the temp file
416 wfSuppressWarnings();
417 $ok = copy( $source, $tmpPath );
423 $this->chmod( $tmpPath );
429 * Chmod a file, suppressing the warnings
431 * @param $path string Absolute file system path
432 * @return bool Success
434 protected function chmod( $path ) {
435 wfSuppressWarnings();
436 $ok = chmod( $path, $this->fileMode
);
444 * Wrapper around RecursiveDirectoryIterator that catches
445 * exception or does any custom behavoir that we may want.
447 * @ingroup FileBackend
449 class FSFileIterator
implements Iterator
{
450 /** @var RecursiveIteratorIterator */
452 protected $suffixStart; // integer
455 * Get an FSFileIterator from a file system directory
459 public function __construct( $dir ) {
460 $this->suffixStart
= strlen( realpath( $dir ) ) +
1; // size of "path/to/dir/"
462 $flags = FilesystemIterator
::CURRENT_AS_FILEINFO | FilesystemIterator
::SKIP_DOTS
;
463 $this->iter
= new RecursiveIteratorIterator(
464 new RecursiveDirectoryIterator( $dir, $flags ) );
465 } catch ( UnexpectedValueException
$e ) {
466 $this->iter
= null; // bad permissions? deleted?
470 public function current() {
471 // Return only the relative path and normalize slashes to FileBackend-style
472 // Make sure to use the realpath since the suffix is based upon that
473 return str_replace( '\\', '/', substr( realpath($this->iter
->current()), $this->suffixStart
) );
476 public function key() {
477 return $this->iter
->key();
480 public function next() {
483 } catch ( UnexpectedValueException
$e ) {
488 public function rewind() {
490 $this->iter
->rewind();
491 } catch ( UnexpectedValueException
$e ) {
496 public function valid() {
497 return $this->iter
&& $this->iter
->valid();