* Fixed 'success' value of doOperations() Status to match documentation.
[lhc/web/wiklou.git] / includes / filerepo / backend / FSFileBackend.php
1 <?php
2 /**
3 * @file
4 * @ingroup FileBackend
5 * @author Aaron Schulz
6 */
7
8 /**
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.
13 *
14 * Status messages should avoid mentioning the internal FS paths.
15 * Likewise, error suppression should be used to avoid path disclosure.
16 *
17 * @ingroup FileBackend
18 */
19 class FSFileBackend extends FileBackend {
20 /** @var Array Map of container names to paths */
21 protected $containerPaths = array();
22 protected $fileMode; // file permission mode
23
24 /**
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
29 */
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
36 }
37 }
38 $this->fileMode = isset( $config['fileMode'] )
39 ? $config['fileMode']
40 : 0644;
41 }
42
43 /**
44 * @see FileBackend::resolveContainerPath()
45 */
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}";
50 }
51 return null;
52 }
53
54 /**
55 * @see FileBackend::doStoreInternal()
56 */
57 protected function doStoreInternal( array $params ) {
58 $status = Status::newGood();
59
60 list( $c, $dest ) = $this->resolveStoragePathReal( $params['dst'] );
61 if ( $dest === null ) {
62 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
63 return $status;
64 }
65 if ( file_exists( $dest ) ) {
66 if ( !empty( $params['overwriteDest'] ) ) {
67 wfSuppressWarnings();
68 $ok = unlink( $dest );
69 wfRestoreWarnings();
70 if ( !$ok ) {
71 $status->fatal( 'backend-fail-delete', $params['dst'] );
72 return $status;
73 }
74 } else {
75 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
76 return $status;
77 }
78 } else {
79 if ( !wfMkdirParents( dirname( $dest ) ) ) {
80 $status->fatal( 'directorycreateerror', $params['dst'] );
81 return $status;
82 }
83 }
84
85 wfSuppressWarnings();
86 $ok = copy( $params['src'], $dest );
87 wfRestoreWarnings();
88 if ( !$ok ) {
89 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
90 return $status;
91 }
92
93 $this->chmod( $dest );
94
95 return $status;
96 }
97
98 /**
99 * @see FileBackend::doCopyInternal()
100 */
101 protected function doCopyInternal( array $params ) {
102 $status = Status::newGood();
103
104 list( $c, $source ) = $this->resolveStoragePathReal( $params['src'] );
105 if ( $source === null ) {
106 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
107 return $status;
108 }
109
110 list( $c, $dest ) = $this->resolveStoragePathReal( $params['dst'] );
111 if ( $dest === null ) {
112 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
113 return $status;
114 }
115
116 if ( file_exists( $dest ) ) {
117 if ( !empty( $params['overwriteDest'] ) ) {
118 wfSuppressWarnings();
119 $ok = unlink( $dest );
120 wfRestoreWarnings();
121 if ( !$ok ) {
122 $status->fatal( 'backend-fail-delete', $params['dst'] );
123 return $status;
124 }
125 } else {
126 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
127 return $status;
128 }
129 } else {
130 if ( !wfMkdirParents( dirname( $dest ) ) ) {
131 $status->fatal( 'directorycreateerror', $params['dst'] );
132 return $status;
133 }
134 }
135
136 wfSuppressWarnings();
137 $ok = copy( $source, $dest );
138 wfRestoreWarnings();
139 if ( !$ok ) {
140 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
141 return $status;
142 }
143
144 $this->chmod( $dest );
145
146 return $status;
147 }
148
149 /**
150 * @see FileBackend::doMoveInternal()
151 */
152 protected function doMoveInternal( array $params ) {
153 $status = Status::newGood();
154
155 list( $c, $source ) = $this->resolveStoragePathReal( $params['src'] );
156 if ( $source === null ) {
157 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
158 return $status;
159 }
160 list( $c, $dest ) = $this->resolveStoragePathReal( $params['dst'] );
161 if ( $dest === null ) {
162 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
163 return $status;
164 }
165
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 );
172 wfRestoreWarnings();
173 if ( !$ok ) {
174 $status->fatal( 'backend-fail-delete', $params['dst'] );
175 return $status;
176 }
177 }
178 } else {
179 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
180 return $status;
181 }
182 } else {
183 if ( !wfMkdirParents( dirname( $dest ) ) ) {
184 $status->fatal( 'directorycreateerror', $params['dst'] );
185 return $status;
186 }
187 }
188
189 wfSuppressWarnings();
190 $ok = rename( $source, $dest );
191 clearstatcache(); // file no longer at source
192 wfRestoreWarnings();
193 if ( !$ok ) {
194 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
195 return $status;
196 }
197
198 return $status;
199 }
200
201 /**
202 * @see FileBackend::doDeleteInternal()
203 */
204 protected function doDeleteInternal( array $params ) {
205 $status = Status::newGood();
206
207 list( $c, $source ) = $this->resolveStoragePathReal( $params['src'] );
208 if ( $source === null ) {
209 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
210 return $status;
211 }
212
213 if ( !is_file( $source ) ) {
214 if ( empty( $params['ignoreMissingSource'] ) ) {
215 $status->fatal( 'backend-fail-delete', $params['src'] );
216 }
217 return $status; // do nothing; either OK or bad status
218 }
219
220 wfSuppressWarnings();
221 $ok = unlink( $source );
222 wfRestoreWarnings();
223 if ( !$ok ) {
224 $status->fatal( 'backend-fail-delete', $params['src'] );
225 return $status;
226 }
227
228 return $status;
229 }
230
231 /**
232 * @see FileBackend::doCreateInternal()
233 */
234 protected function doCreateInternal( array $params ) {
235 $status = Status::newGood();
236
237 list( $c, $dest ) = $this->resolveStoragePathReal( $params['dst'] );
238 if ( $dest === null ) {
239 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
240 return $status;
241 }
242
243 if ( file_exists( $dest ) ) {
244 if ( !empty( $params['overwriteDest'] ) ) {
245 wfSuppressWarnings();
246 $ok = unlink( $dest );
247 wfRestoreWarnings();
248 if ( !$ok ) {
249 $status->fatal( 'backend-fail-delete', $params['dst'] );
250 return $status;
251 }
252 } else {
253 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
254 return $status;
255 }
256 } else {
257 if ( !wfMkdirParents( dirname( $dest ) ) ) {
258 $status->fatal( 'directorycreateerror', $params['dst'] );
259 return $status;
260 }
261 }
262
263 wfSuppressWarnings();
264 $ok = file_put_contents( $dest, $params['content'] );
265 wfRestoreWarnings();
266 if ( !$ok ) {
267 $status->fatal( 'backend-fail-create', $params['dst'] );
268 return $status;
269 }
270
271 $this->chmod( $dest );
272
273 return $status;
274 }
275
276 /**
277 * @see FileBackend::doPrepare()
278 */
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'] );
287 }
288 return $status;
289 }
290
291 /**
292 * @see FileBackend::doSecure()
293 */
294 protected function doSecure( $container, $dir, array $params ) {
295 $status = Status::newGood();
296 if ( !wfMkdirParents( $dir ) ) {
297 $status->fatal( 'directorycreateerror', $params['dir'] );
298 return $status;
299 }
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", '' );
304 wfRestoreWarnings();
305 if ( !$ok ) {
306 $status->fatal( 'backend-fail-create', $params['dir'] . '/index.html' );
307 return $status;
308 }
309 }
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" );
316 wfRestoreWarnings();
317 if ( !$ok ) {
318 $storeDir = "mwstore://{$this->name}/{$container}";
319 $status->fatal( 'backend-fail-create', "$storeDir/.htaccess" );
320 return $status;
321 }
322 }
323 return $status;
324 }
325
326 /**
327 * @see FileBackend::doClean()
328 */
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
334 }
335 wfRestoreWarnings();
336 return $status;
337 }
338
339 /**
340 * @see FileBackend::doFileExists()
341 */
342 protected function doGetFileStat( array $params ) {
343 list( $c, $source ) = $this->resolveStoragePathReal( $params['src'] );
344 if ( $source === null ) {
345 return false; // invalid storage path
346 }
347
348 wfSuppressWarnings();
349 if ( is_file( $source ) ) { // regular file?
350 $stat = stat( $source );
351 } else {
352 $stat = false;
353 }
354 wfRestoreWarnings();
355
356 if ( $stat ) {
357 return array(
358 'mtime' => wfTimestamp( TS_MW, $stat['mtime'] ),
359 'size' => $stat['size']
360 );
361 } else {
362 return false;
363 }
364 }
365
366 /**
367 * @see FileBackend::getFileListInternal()
368 */
369 public function getFileListInternal( $container, $dir, array $params ) {
370 wfSuppressWarnings();
371 $exists = is_dir( $dir );
372 wfRestoreWarnings();
373 if ( !$exists ) {
374 return array(); // nothing under this dir
375 }
376 wfSuppressWarnings();
377 $readable = is_readable( $dir );
378 wfRestoreWarnings();
379 if ( !$readable ) {
380 return null; // bad permissions?
381 }
382 return new FSFileIterator( $dir );
383 }
384
385 /**
386 * @see FileBackend::getLocalReference()
387 */
388 public function getLocalReference( array $params ) {
389 list( $c, $source ) = $this->resolveStoragePathReal( $params['src'] );
390 if ( $source === null ) {
391 return null;
392 }
393 return new FSFile( $source );
394 }
395
396 /**
397 * @see FileBackend::getLocalCopy()
398 */
399 public function getLocalCopy( array $params ) {
400 list( $c, $source ) = $this->resolveStoragePathReal( $params['src'] );
401 if ( $source === null ) {
402 return null;
403 }
404
405 // Create a new temporary file with the same extension...
406 $ext = FileBackend::extensionFromPath( $params['src'] );
407 $tmpFile = TempFSFile::factory( wfBaseName( $source ) . '_', $ext );
408 if ( !$tmpFile ) {
409 return null;
410 }
411 $tmpPath = $tmpFile->getPath();
412
413 // Copy the source file over the temp file
414 wfSuppressWarnings();
415 $ok = copy( $source, $tmpPath );
416 wfRestoreWarnings();
417 if ( !$ok ) {
418 return null;
419 }
420
421 $this->chmod( $tmpPath );
422
423 return $tmpFile;
424 }
425
426 /**
427 * Chmod a file, suppressing the warnings
428 *
429 * @param $path string Absolute file system path
430 * @return bool Success
431 */
432 protected function chmod( $path ) {
433 wfSuppressWarnings();
434 $ok = chmod( $path, $this->fileMode );
435 wfRestoreWarnings();
436
437 return $ok;
438 }
439 }
440
441 /**
442 * Wrapper around RecursiveDirectoryIterator that catches
443 * exception or does any custom behavoir that we may want.
444 *
445 * @ingroup FileBackend
446 */
447 class FSFileIterator implements Iterator {
448 /** @var RecursiveIteratorIterator */
449 protected $iter;
450 protected $suffixStart; // integer
451
452 /**
453 * Get an FSFileIterator from a file system directory
454 *
455 * @param $dir string
456 */
457 public function __construct( $dir ) {
458 $this->suffixStart = strlen( realpath( $dir ) ) + 1; // size of "path/to/dir/"
459 try {
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?
465 }
466 }
467
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 ) );
471 }
472
473 public function key() {
474 return $this->iter->key();
475 }
476
477 public function next() {
478 try {
479 $this->iter->next();
480 } catch ( UnexpectedValueException $e ) {
481 $this->iter = null;
482 }
483 }
484
485 public function rewind() {
486 try {
487 $this->iter->rewind();
488 } catch ( UnexpectedValueException $e ) {
489 $this->iter = null;
490 }
491 }
492
493 public function valid() {
494 return $this->iter && $this->iter->valid();
495 }
496 }