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::doPrepare()
279 protected function doPrepare( $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::doSecure()
294 protected function doSecure( $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 list( $b, $container, $r ) = FileBackend
::splitStoragePath( $params['dir'] );
312 $dirRoot = $this->containerPaths
[$container]; // real path
313 if ( !empty( $params['noAccess'] ) && !file_exists( "{$dirRoot}/.htaccess" ) ) {
314 wfSuppressWarnings();
315 $ok = file_put_contents( "{$dirRoot}/.htaccess", "Deny from all\n" );
318 $storeDir = "mwstore://{$this->name}/{$container}";
319 $status->fatal( 'backend-fail-create', "$storeDir/.htaccess" );
327 * @see FileBackend::doClean()
329 protected function doClean( $container, $dir, array $params ) {
330 $status = Status
::newGood();
331 wfSuppressWarnings();
332 if ( is_dir( $dir ) ) {
333 rmdir( $dir ); // remove directory if empty
340 * @see FileBackend::doFileExists()
342 protected function doGetFileStat( array $params ) {
343 list( $c, $source ) = $this->resolveStoragePathReal( $params['src'] );
344 if ( $source === null ) {
345 return false; // invalid storage path
348 wfSuppressWarnings();
349 if ( is_file( $source ) ) { // regular file?
350 $stat = stat( $source );
358 'mtime' => wfTimestamp( TS_MW
, $stat['mtime'] ),
359 'size' => $stat['size']
367 * @see FileBackend::getFileListInternal()
369 public function getFileListInternal( $container, $dir, array $params ) {
370 wfSuppressWarnings();
371 $exists = is_dir( $dir );
374 return array(); // nothing under this dir
376 wfSuppressWarnings();
377 $readable = is_readable( $dir );
380 return null; // bad permissions?
382 return new FSFileIterator( $dir );
386 * @see FileBackend::getLocalReference()
388 public function getLocalReference( array $params ) {
389 list( $c, $source ) = $this->resolveStoragePathReal( $params['src'] );
390 if ( $source === null ) {
393 return new FSFile( $source );
397 * @see FileBackend::getLocalCopy()
399 public function getLocalCopy( array $params ) {
400 list( $c, $source ) = $this->resolveStoragePathReal( $params['src'] );
401 if ( $source === null ) {
405 // Create a new temporary file with the same extension...
406 $ext = FileBackend
::extensionFromPath( $params['src'] );
407 $tmpFile = TempFSFile
::factory( wfBaseName( $source ) . '_', $ext );
411 $tmpPath = $tmpFile->getPath();
413 // Copy the source file over the temp file
414 wfSuppressWarnings();
415 $ok = copy( $source, $tmpPath );
421 $this->chmod( $tmpPath );
427 * Chmod a file, suppressing the warnings
429 * @param $path string Absolute file system path
430 * @return bool Success
432 protected function chmod( $path ) {
433 wfSuppressWarnings();
434 $ok = chmod( $path, $this->fileMode
);
442 * Wrapper around RecursiveDirectoryIterator that catches
443 * exception or does any custom behavoir that we may want.
445 * @ingroup FileBackend
447 class FSFileIterator
implements Iterator
{
448 /** @var RecursiveIteratorIterator */
450 protected $suffixStart; // integer
453 * Get an FSFileIterator from a file system directory
457 public function __construct( $dir ) {
458 $this->suffixStart
= strlen( realpath( $dir ) ) +
1; // size of "path/to/dir/"
460 $flags = FilesystemIterator
::CURRENT_AS_FILEINFO | FilesystemIterator
::SKIP_DOTS
;
461 $this->iter
= new RecursiveIteratorIterator(
462 new RecursiveDirectoryIterator( $dir, $flags ) );
463 } catch ( UnexpectedValueException
$e ) {
464 $this->iter
= null; // bad permissions? deleted?
468 public function current() {
469 // Return only the relative path and normalize slashes to FileBackend-style
470 return str_replace( '\\', '/', substr( $this->iter
->current(), $this->suffixStart
) );
473 public function key() {
474 return $this->iter
->key();
477 public function next() {
480 } catch ( UnexpectedValueException
$e ) {
485 public function rewind() {
487 $this->iter
->rewind();
488 } catch ( UnexpectedValueException
$e ) {
493 public function valid() {
494 return $this->iter
&& $this->iter
->valid();