[FileBackend] Moved filerepo/backend/ up to filebackend
[lhc/web/wiklou.git] / includes / filebackend / FSFileBackend.php
1 <?php
2 /**
3 * File system based backend.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup FileBackend
22 * @author Aaron Schulz
23 */
24
25 /**
26 * @brief Class for a file system (FS) based file backend.
27 *
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.
31 *
32 * Having directories with thousands of files will diminish performance.
33 * Sharding can be accomplished by using FileRepo-style hash paths.
34 *
35 * Status messages should avoid mentioning the internal FS paths.
36 * PHP warnings are assumed to be logged rather than output.
37 *
38 * @ingroup FileBackend
39 * @since 1.19
40 */
41 class FSFileBackend extends FileBackendStore {
42 protected $basePath; // string; directory holding the container directories
43 /** @var Array Map of container names to root paths */
44 protected $containerPaths = array(); // for custom container paths
45 protected $fileMode; // integer; file permission mode
46
47 protected $hadWarningErrors = array();
48
49 /**
50 * @see FileBackendStore::__construct()
51 * Additional $config params include:
52 * - basePath : File system directory that holds containers.
53 * - containerPaths : Map of container names to custom file system directories.
54 * This should only be used for backwards-compatibility.
55 * - fileMode : Octal UNIX file permissions to use on files stored.
56 */
57 public function __construct( array $config ) {
58 parent::__construct( $config );
59
60 // Remove any possible trailing slash from directories
61 if ( isset( $config['basePath'] ) ) {
62 $this->basePath = rtrim( $config['basePath'], '/' ); // remove trailing slash
63 } else {
64 $this->basePath = null; // none; containers must have explicit paths
65 }
66
67 if ( isset( $config['containerPaths'] ) ) {
68 $this->containerPaths = (array)$config['containerPaths'];
69 foreach ( $this->containerPaths as &$path ) {
70 $path = rtrim( $path, '/' ); // remove trailing slash
71 }
72 }
73
74 $this->fileMode = isset( $config['fileMode'] )
75 ? $config['fileMode']
76 : 0644;
77 }
78
79 /**
80 * @see FileBackendStore::resolveContainerPath()
81 * @param $container string
82 * @param $relStoragePath string
83 * @return null|string
84 */
85 protected function resolveContainerPath( $container, $relStoragePath ) {
86 // Check that container has a root directory
87 if ( isset( $this->containerPaths[$container] ) || isset( $this->basePath ) ) {
88 // Check for sane relative paths (assume the base paths are OK)
89 if ( $this->isLegalRelPath( $relStoragePath ) ) {
90 return $relStoragePath;
91 }
92 }
93 return null;
94 }
95
96 /**
97 * Sanity check a relative file system path for validity
98 *
99 * @param $path string Normalized relative path
100 * @return bool
101 */
102 protected function isLegalRelPath( $path ) {
103 // Check for file names longer than 255 chars
104 if ( preg_match( '![^/]{256}!', $path ) ) { // ext3/NTFS
105 return false;
106 }
107 if ( wfIsWindows() ) { // NTFS
108 return !preg_match( '![:*?"<>|]!', $path );
109 } else {
110 return true;
111 }
112 }
113
114 /**
115 * Given the short (unresolved) and full (resolved) name of
116 * a container, return the file system path of the container.
117 *
118 * @param $shortCont string
119 * @param $fullCont string
120 * @return string|null
121 */
122 protected function containerFSRoot( $shortCont, $fullCont ) {
123 if ( isset( $this->containerPaths[$shortCont] ) ) {
124 return $this->containerPaths[$shortCont];
125 } elseif ( isset( $this->basePath ) ) {
126 return "{$this->basePath}/{$fullCont}";
127 }
128 return null; // no container base path defined
129 }
130
131 /**
132 * Get the absolute file system path for a storage path
133 *
134 * @param $storagePath string Storage path
135 * @return string|null
136 */
137 protected function resolveToFSPath( $storagePath ) {
138 list( $fullCont, $relPath ) = $this->resolveStoragePathReal( $storagePath );
139 if ( $relPath === null ) {
140 return null; // invalid
141 }
142 list( $b, $shortCont, $r ) = FileBackend::splitStoragePath( $storagePath );
143 $fsPath = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
144 if ( $relPath != '' ) {
145 $fsPath .= "/{$relPath}";
146 }
147 return $fsPath;
148 }
149
150 /**
151 * @see FileBackendStore::isPathUsableInternal()
152 * @return bool
153 */
154 public function isPathUsableInternal( $storagePath ) {
155 $fsPath = $this->resolveToFSPath( $storagePath );
156 if ( $fsPath === null ) {
157 return false; // invalid
158 }
159 $parentDir = dirname( $fsPath );
160
161 if ( file_exists( $fsPath ) ) {
162 $ok = is_file( $fsPath ) && is_writable( $fsPath );
163 } else {
164 $ok = is_dir( $parentDir ) && is_writable( $parentDir );
165 }
166
167 return $ok;
168 }
169
170 /**
171 * @see FileBackendStore::doStoreInternal()
172 * @return Status
173 */
174 protected function doStoreInternal( array $params ) {
175 $status = Status::newGood();
176
177 $dest = $this->resolveToFSPath( $params['dst'] );
178 if ( $dest === null ) {
179 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
180 return $status;
181 }
182
183 if ( file_exists( $dest ) ) {
184 if ( !empty( $params['overwrite'] ) ) {
185 $ok = unlink( $dest );
186 if ( !$ok ) {
187 $status->fatal( 'backend-fail-delete', $params['dst'] );
188 return $status;
189 }
190 } else {
191 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
192 return $status;
193 }
194 }
195
196 if ( !empty( $params['async'] ) ) { // deferred
197 $cmd = implode( ' ', array( wfIsWindows() ? 'COPY' : 'cp',
198 wfEscapeShellArg( $this->cleanPathSlashes( $params['src'] ) ),
199 wfEscapeShellArg( $this->cleanPathSlashes( $dest ) )
200 ) );
201 $status->value = new FSFileOpHandle( $this, $params, 'Store', $cmd, $dest );
202 } else { // immediate write
203 $ok = copy( $params['src'], $dest );
204 // In some cases (at least over NFS), copy() returns true when it fails
205 if ( !$ok || ( filesize( $params['src'] ) !== filesize( $dest ) ) ) {
206 if ( $ok ) { // PHP bug
207 unlink( $dest ); // remove broken file
208 trigger_error( __METHOD__ . ": copy() failed but returned true." );
209 }
210 $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] );
211 return $status;
212 }
213 $this->chmod( $dest );
214 }
215
216 return $status;
217 }
218
219 /**
220 * @see FSFileBackend::doExecuteOpHandlesInternal()
221 */
222 protected function _getResponseStore( $errors, Status $status, array $params, $cmd ) {
223 if ( $errors !== '' && !( wfIsWindows() && $errors[0] === " " ) ) {
224 $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] );
225 trigger_error( "$cmd\n$errors", E_USER_WARNING ); // command output
226 }
227 }
228
229 /**
230 * @see FileBackendStore::doCopyInternal()
231 * @return Status
232 */
233 protected function doCopyInternal( array $params ) {
234 $status = Status::newGood();
235
236 $source = $this->resolveToFSPath( $params['src'] );
237 if ( $source === null ) {
238 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
239 return $status;
240 }
241
242 $dest = $this->resolveToFSPath( $params['dst'] );
243 if ( $dest === null ) {
244 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
245 return $status;
246 }
247
248 if ( file_exists( $dest ) ) {
249 if ( !empty( $params['overwrite'] ) ) {
250 $ok = unlink( $dest );
251 if ( !$ok ) {
252 $status->fatal( 'backend-fail-delete', $params['dst'] );
253 return $status;
254 }
255 } else {
256 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
257 return $status;
258 }
259 }
260
261 if ( !empty( $params['async'] ) ) { // deferred
262 $cmd = implode( ' ', array( wfIsWindows() ? 'COPY' : 'cp',
263 wfEscapeShellArg( $this->cleanPathSlashes( $source ) ),
264 wfEscapeShellArg( $this->cleanPathSlashes( $dest ) )
265 ) );
266 $status->value = new FSFileOpHandle( $this, $params, 'Copy', $cmd, $dest );
267 } else { // immediate write
268 $ok = copy( $source, $dest );
269 // In some cases (at least over NFS), copy() returns true when it fails
270 if ( !$ok || ( filesize( $source ) !== filesize( $dest ) ) ) {
271 if ( $ok ) { // PHP bug
272 unlink( $dest ); // remove broken file
273 trigger_error( __METHOD__ . ": copy() failed but returned true." );
274 }
275 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
276 return $status;
277 }
278 $this->chmod( $dest );
279 }
280
281 return $status;
282 }
283
284 /**
285 * @see FSFileBackend::doExecuteOpHandlesInternal()
286 */
287 protected function _getResponseCopy( $errors, Status $status, array $params, $cmd ) {
288 if ( $errors !== '' && !( wfIsWindows() && $errors[0] === " " ) ) {
289 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
290 trigger_error( "$cmd\n$errors", E_USER_WARNING ); // command output
291 }
292 }
293
294 /**
295 * @see FileBackendStore::doMoveInternal()
296 * @return Status
297 */
298 protected function doMoveInternal( array $params ) {
299 $status = Status::newGood();
300
301 $source = $this->resolveToFSPath( $params['src'] );
302 if ( $source === null ) {
303 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
304 return $status;
305 }
306
307 $dest = $this->resolveToFSPath( $params['dst'] );
308 if ( $dest === null ) {
309 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
310 return $status;
311 }
312
313 if ( file_exists( $dest ) ) {
314 if ( !empty( $params['overwrite'] ) ) {
315 // Windows does not support moving over existing files
316 if ( wfIsWindows() ) {
317 $ok = unlink( $dest );
318 if ( !$ok ) {
319 $status->fatal( 'backend-fail-delete', $params['dst'] );
320 return $status;
321 }
322 }
323 } else {
324 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
325 return $status;
326 }
327 }
328
329 if ( !empty( $params['async'] ) ) { // deferred
330 $cmd = implode( ' ', array( wfIsWindows() ? 'MOVE' : 'mv',
331 wfEscapeShellArg( $this->cleanPathSlashes( $source ) ),
332 wfEscapeShellArg( $this->cleanPathSlashes( $dest ) )
333 ) );
334 $status->value = new FSFileOpHandle( $this, $params, 'Move', $cmd );
335 } else { // immediate write
336 $ok = rename( $source, $dest );
337 clearstatcache(); // file no longer at source
338 if ( !$ok ) {
339 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
340 return $status;
341 }
342 }
343
344 return $status;
345 }
346
347 /**
348 * @see FSFileBackend::doExecuteOpHandlesInternal()
349 */
350 protected function _getResponseMove( $errors, Status $status, array $params, $cmd ) {
351 if ( $errors !== '' && !( wfIsWindows() && $errors[0] === " " ) ) {
352 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
353 trigger_error( "$cmd\n$errors", E_USER_WARNING ); // command output
354 }
355 }
356
357 /**
358 * @see FileBackendStore::doDeleteInternal()
359 * @return Status
360 */
361 protected function doDeleteInternal( array $params ) {
362 $status = Status::newGood();
363
364 $source = $this->resolveToFSPath( $params['src'] );
365 if ( $source === null ) {
366 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
367 return $status;
368 }
369
370 if ( !is_file( $source ) ) {
371 if ( empty( $params['ignoreMissingSource'] ) ) {
372 $status->fatal( 'backend-fail-delete', $params['src'] );
373 }
374 return $status; // do nothing; either OK or bad status
375 }
376
377 if ( !empty( $params['async'] ) ) { // deferred
378 $cmd = implode( ' ', array( wfIsWindows() ? 'DEL' : 'unlink',
379 wfEscapeShellArg( $this->cleanPathSlashes( $source ) )
380 ) );
381 $status->value = new FSFileOpHandle( $this, $params, 'Copy', $cmd );
382 } else { // immediate write
383 $ok = unlink( $source );
384 if ( !$ok ) {
385 $status->fatal( 'backend-fail-delete', $params['src'] );
386 return $status;
387 }
388 }
389
390 return $status;
391 }
392
393 /**
394 * @see FSFileBackend::doExecuteOpHandlesInternal()
395 */
396 protected function _getResponseDelete( $errors, Status $status, array $params, $cmd ) {
397 if ( $errors !== '' && !( wfIsWindows() && $errors[0] === " " ) ) {
398 $status->fatal( 'backend-fail-delete', $params['src'] );
399 trigger_error( "$cmd\n$errors", E_USER_WARNING ); // command output
400 }
401 }
402
403 /**
404 * @see FileBackendStore::doCreateInternal()
405 * @return Status
406 */
407 protected function doCreateInternal( array $params ) {
408 $status = Status::newGood();
409
410 $dest = $this->resolveToFSPath( $params['dst'] );
411 if ( $dest === null ) {
412 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
413 return $status;
414 }
415
416 if ( file_exists( $dest ) ) {
417 if ( !empty( $params['overwrite'] ) ) {
418 $ok = unlink( $dest );
419 if ( !$ok ) {
420 $status->fatal( 'backend-fail-delete', $params['dst'] );
421 return $status;
422 }
423 } else {
424 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
425 return $status;
426 }
427 }
428
429 if ( !empty( $params['async'] ) ) { // deferred
430 $tempFile = TempFSFile::factory( 'create_', 'tmp' );
431 if ( !$tempFile ) {
432 $status->fatal( 'backend-fail-create', $params['dst'] );
433 return $status;
434 }
435 $bytes = file_put_contents( $tempFile->getPath(), $params['content'] );
436 if ( $bytes === false ) {
437 $status->fatal( 'backend-fail-create', $params['dst'] );
438 return $status;
439 }
440 $cmd = implode( ' ', array( wfIsWindows() ? 'COPY' : 'cp',
441 wfEscapeShellArg( $this->cleanPathSlashes( $tempFile->getPath() ) ),
442 wfEscapeShellArg( $this->cleanPathSlashes( $dest ) )
443 ) );
444 $status->value = new FSFileOpHandle( $this, $params, 'Create', $cmd, $dest );
445 $tempFile->bind( $status->value );
446 } else { // immediate write
447 $bytes = file_put_contents( $dest, $params['content'] );
448 if ( $bytes === false ) {
449 $status->fatal( 'backend-fail-create', $params['dst'] );
450 return $status;
451 }
452 $this->chmod( $dest );
453 }
454
455 return $status;
456 }
457
458 /**
459 * @see FSFileBackend::doExecuteOpHandlesInternal()
460 */
461 protected function _getResponseCreate( $errors, Status $status, array $params, $cmd ) {
462 if ( $errors !== '' && !( wfIsWindows() && $errors[0] === " " ) ) {
463 $status->fatal( 'backend-fail-create', $params['dst'] );
464 trigger_error( "$cmd\n$errors", E_USER_WARNING ); // command output
465 }
466 }
467
468 /**
469 * @see FileBackendStore::doPrepareInternal()
470 * @return Status
471 */
472 protected function doPrepareInternal( $fullCont, $dirRel, array $params ) {
473 $status = Status::newGood();
474 list( $b, $shortCont, $r ) = FileBackend::splitStoragePath( $params['dir'] );
475 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
476 $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
477 $existed = is_dir( $dir ); // already there?
478 if ( !wfMkdirParents( $dir ) ) { // make directory and its parents
479 $status->fatal( 'directorycreateerror', $params['dir'] ); // fails on races
480 } elseif ( !is_writable( $dir ) ) {
481 $status->fatal( 'directoryreadonlyerror', $params['dir'] );
482 } elseif ( !is_readable( $dir ) ) {
483 $status->fatal( 'directorynotreadableerror', $params['dir'] );
484 }
485 if ( is_dir( $dir ) && !$existed ) {
486 // Respect any 'noAccess' or 'noListing' flags...
487 $status->merge( $this->doSecureInternal( $fullCont, $dirRel, $params ) );
488 }
489 return $status;
490 }
491
492 /**
493 * @see FileBackendStore::doSecureInternal()
494 * @return Status
495 */
496 protected function doSecureInternal( $fullCont, $dirRel, array $params ) {
497 $status = Status::newGood();
498 list( $b, $shortCont, $r ) = FileBackend::splitStoragePath( $params['dir'] );
499 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
500 $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
501 // Seed new directories with a blank index.html, to prevent crawling...
502 if ( !empty( $params['noListing'] ) && !file_exists( "{$dir}/index.html" ) ) {
503 $bytes = file_put_contents( "{$dir}/index.html", $this->indexHtmlPrivate() );
504 if ( $bytes === false ) {
505 $status->fatal( 'backend-fail-create', $params['dir'] . '/index.html' );
506 return $status;
507 }
508 }
509 // Add a .htaccess file to the root of the container...
510 if ( !empty( $params['noAccess'] ) && !file_exists( "{$contRoot}/.htaccess" ) ) {
511 $bytes = file_put_contents( "{$contRoot}/.htaccess", $this->htaccessPrivate() );
512 if ( $bytes === false ) {
513 $storeDir = "mwstore://{$this->name}/{$shortCont}";
514 $status->fatal( 'backend-fail-create', "{$storeDir}/.htaccess" );
515 return $status;
516 }
517 }
518 return $status;
519 }
520
521 /**
522 * @see FileBackendStore::doPublishInternal()
523 * @return Status
524 */
525 protected function doPublishInternal( $fullCont, $dirRel, array $params ) {
526 $status = Status::newGood();
527 list( $b, $shortCont, $r ) = FileBackend::splitStoragePath( $params['dir'] );
528 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
529 $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
530 // Unseed new directories with a blank index.html, to allow crawling...
531 if ( !empty( $params['listing'] ) && is_file( "{$dir}/index.html" ) ) {
532 $exists = ( file_get_contents( "{$dir}/index.html" ) === $this->indexHtmlPrivate() );
533 if ( $exists && !unlink( "{$dir}/index.html" ) ) { // reverse secure()
534 $status->fatal( 'backend-fail-delete', $params['dir'] . '/index.html' );
535 return $status;
536 }
537 }
538 // Remove the .htaccess file from the root of the container...
539 if ( !empty( $params['access'] ) && is_file( "{$contRoot}/.htaccess" ) ) {
540 $exists = ( file_get_contents( "{$contRoot}/.htaccess" ) === $this->htaccessPrivate() );
541 if ( $exists && !unlink( "{$contRoot}/.htaccess" ) ) { // reverse secure()
542 $storeDir = "mwstore://{$this->name}/{$shortCont}";
543 $status->fatal( 'backend-fail-delete', "{$storeDir}/.htaccess" );
544 return $status;
545 }
546 }
547 return $status;
548 }
549
550 /**
551 * @see FileBackendStore::doCleanInternal()
552 * @return Status
553 */
554 protected function doCleanInternal( $fullCont, $dirRel, array $params ) {
555 $status = Status::newGood();
556 list( $b, $shortCont, $r ) = FileBackend::splitStoragePath( $params['dir'] );
557 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
558 $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
559 wfSuppressWarnings();
560 if ( is_dir( $dir ) ) {
561 rmdir( $dir ); // remove directory if empty
562 }
563 wfRestoreWarnings();
564 return $status;
565 }
566
567 /**
568 * @see FileBackendStore::doFileExists()
569 * @return array|bool|null
570 */
571 protected function doGetFileStat( array $params ) {
572 $source = $this->resolveToFSPath( $params['src'] );
573 if ( $source === null ) {
574 return false; // invalid storage path
575 }
576
577 $this->trapWarnings(); // don't trust 'false' if there were errors
578 $stat = is_file( $source ) ? stat( $source ) : false; // regular files only
579 $hadError = $this->untrapWarnings();
580
581 if ( $stat ) {
582 return array(
583 'mtime' => wfTimestamp( TS_MW, $stat['mtime'] ),
584 'size' => $stat['size']
585 );
586 } elseif ( !$hadError ) {
587 return false; // file does not exist
588 } else {
589 return null; // failure
590 }
591 }
592
593 /**
594 * @see FileBackendStore::doClearCache()
595 */
596 protected function doClearCache( array $paths = null ) {
597 clearstatcache(); // clear the PHP file stat cache
598 }
599
600 /**
601 * @see FileBackendStore::doDirectoryExists()
602 * @return bool|null
603 */
604 protected function doDirectoryExists( $fullCont, $dirRel, array $params ) {
605 list( $b, $shortCont, $r ) = FileBackend::splitStoragePath( $params['dir'] );
606 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
607 $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
608
609 $this->trapWarnings(); // don't trust 'false' if there were errors
610 $exists = is_dir( $dir );
611 $hadError = $this->untrapWarnings();
612
613 return $hadError ? null : $exists;
614 }
615
616 /**
617 * @see FileBackendStore::getDirectoryListInternal()
618 * @return Array|null
619 */
620 public function getDirectoryListInternal( $fullCont, $dirRel, array $params ) {
621 list( $b, $shortCont, $r ) = FileBackend::splitStoragePath( $params['dir'] );
622 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
623 $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
624 $exists = is_dir( $dir );
625 if ( !$exists ) {
626 wfDebug( __METHOD__ . "() given directory does not exist: '$dir'\n" );
627 return array(); // nothing under this dir
628 } elseif ( !is_readable( $dir ) ) {
629 wfDebug( __METHOD__ . "() given directory is unreadable: '$dir'\n" );
630 return null; // bad permissions?
631 }
632 return new FSFileBackendDirList( $dir, $params );
633 }
634
635 /**
636 * @see FileBackendStore::getFileListInternal()
637 * @return array|FSFileBackendFileList|null
638 */
639 public function getFileListInternal( $fullCont, $dirRel, array $params ) {
640 list( $b, $shortCont, $r ) = FileBackend::splitStoragePath( $params['dir'] );
641 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
642 $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
643 $exists = is_dir( $dir );
644 if ( !$exists ) {
645 wfDebug( __METHOD__ . "() given directory does not exist: '$dir'\n" );
646 return array(); // nothing under this dir
647 } elseif ( !is_readable( $dir ) ) {
648 wfDebug( __METHOD__ . "() given directory is unreadable: '$dir'\n" );
649 return null; // bad permissions?
650 }
651 return new FSFileBackendFileList( $dir, $params );
652 }
653
654 /**
655 * @see FileBackendStore::getLocalReference()
656 * @return FSFile|null
657 */
658 public function getLocalReference( array $params ) {
659 $source = $this->resolveToFSPath( $params['src'] );
660 if ( $source === null ) {
661 return null;
662 }
663 return new FSFile( $source );
664 }
665
666 /**
667 * @see FileBackendStore::getLocalCopy()
668 * @return null|TempFSFile
669 */
670 public function getLocalCopy( array $params ) {
671 $source = $this->resolveToFSPath( $params['src'] );
672 if ( $source === null ) {
673 return null;
674 }
675
676 // Create a new temporary file with the same extension...
677 $ext = FileBackend::extensionFromPath( $params['src'] );
678 $tmpFile = TempFSFile::factory( wfBaseName( $source ) . '_', $ext );
679 if ( !$tmpFile ) {
680 return null;
681 }
682 $tmpPath = $tmpFile->getPath();
683
684 // Copy the source file over the temp file
685 $ok = copy( $source, $tmpPath );
686 if ( !$ok ) {
687 return null;
688 }
689
690 $this->chmod( $tmpPath );
691
692 return $tmpFile;
693 }
694
695 /**
696 * @see FileBackendStore::directoriesAreVirtual()
697 * @return bool
698 */
699 protected function directoriesAreVirtual() {
700 return false;
701 }
702
703 /**
704 * @see FileBackendStore::doExecuteOpHandlesInternal()
705 * @return Array List of corresponding Status objects
706 */
707 protected function doExecuteOpHandlesInternal( array $fileOpHandles ) {
708 $statuses = array();
709
710 $pipes = array();
711 foreach ( $fileOpHandles as $index => $fileOpHandle ) {
712 $pipes[$index] = popen( "{$fileOpHandle->cmd} 2>&1", 'r' );
713 }
714
715 $errs = array();
716 foreach ( $pipes as $index => $pipe ) {
717 // Result will be empty on success in *NIX. On Windows,
718 // it may be something like " 1 file(s) [copied|moved].".
719 $errs[$index] = stream_get_contents( $pipe );
720 fclose( $pipe );
721 }
722
723 foreach ( $fileOpHandles as $index => $fileOpHandle ) {
724 $status = Status::newGood();
725 $function = '_getResponse' . $fileOpHandle->call;
726 $this->$function( $errs[$index], $status, $fileOpHandle->params, $fileOpHandle->cmd );
727 $statuses[$index] = $status;
728 if ( $status->isOK() && $fileOpHandle->chmodPath ) {
729 $this->chmod( $fileOpHandle->chmodPath );
730 }
731 }
732
733 clearstatcache(); // files changed
734 return $statuses;
735 }
736
737 /**
738 * Chmod a file, suppressing the warnings
739 *
740 * @param $path string Absolute file system path
741 * @return bool Success
742 */
743 protected function chmod( $path ) {
744 wfSuppressWarnings();
745 $ok = chmod( $path, $this->fileMode );
746 wfRestoreWarnings();
747
748 return $ok;
749 }
750
751 /**
752 * Return the text of an index.html file to hide directory listings
753 *
754 * @return string
755 */
756 protected function indexHtmlPrivate() {
757 return '';
758 }
759
760 /**
761 * Return the text of a .htaccess file to make a directory private
762 *
763 * @return string
764 */
765 protected function htaccessPrivate() {
766 return "Deny from all\n";
767 }
768
769 /**
770 * Clean up directory separators for the given OS
771 *
772 * @param $path string FS path
773 * @return string
774 */
775 protected function cleanPathSlashes( $path ) {
776 return wfIsWindows() ? strtr( $path, '/', '\\' ) : $path;
777 }
778
779 /**
780 * Listen for E_WARNING errors and track whether any happen
781 *
782 * @return bool
783 */
784 protected function trapWarnings() {
785 $this->hadWarningErrors[] = false; // push to stack
786 set_error_handler( array( $this, 'handleWarning' ), E_WARNING );
787 return false; // invoke normal PHP error handler
788 }
789
790 /**
791 * Stop listening for E_WARNING errors and return true if any happened
792 *
793 * @return bool
794 */
795 protected function untrapWarnings() {
796 restore_error_handler(); // restore previous handler
797 return array_pop( $this->hadWarningErrors ); // pop from stack
798 }
799
800 /**
801 * @return bool
802 */
803 private function handleWarning() {
804 $this->hadWarningErrors[count( $this->hadWarningErrors ) - 1] = true;
805 return true; // suppress from PHP handler
806 }
807 }
808
809 /**
810 * @see FileBackendStoreOpHandle
811 */
812 class FSFileOpHandle extends FileBackendStoreOpHandle {
813 public $cmd; // string; shell command
814 public $chmodPath; // string; file to chmod
815
816 /**
817 * @param $backend
818 * @param $params array
819 * @param $call
820 * @param $cmd
821 * @param $chmodPath null
822 */
823 public function __construct( $backend, array $params, $call, $cmd, $chmodPath = null ) {
824 $this->backend = $backend;
825 $this->params = $params;
826 $this->call = $call;
827 $this->cmd = $cmd;
828 $this->chmodPath = $chmodPath;
829 }
830 }
831
832 /**
833 * Wrapper around RecursiveDirectoryIterator/DirectoryIterator that
834 * catches exception or does any custom behavoir that we may want.
835 * Do not use this class from places outside FSFileBackend.
836 *
837 * @ingroup FileBackend
838 */
839 abstract class FSFileBackendList implements Iterator {
840 /** @var Iterator */
841 protected $iter;
842 protected $suffixStart; // integer
843 protected $pos = 0; // integer
844 /** @var Array */
845 protected $params = array();
846
847 /**
848 * @param $dir string file system directory
849 * @param $params array
850 */
851 public function __construct( $dir, array $params ) {
852 $dir = realpath( $dir ); // normalize
853 $this->suffixStart = strlen( $dir ) + 1; // size of "path/to/dir/"
854 $this->params = $params;
855
856 try {
857 $this->iter = $this->initIterator( $dir );
858 } catch ( UnexpectedValueException $e ) {
859 $this->iter = null; // bad permissions? deleted?
860 }
861 }
862
863 /**
864 * Return an appropriate iterator object to wrap
865 *
866 * @param $dir string file system directory
867 * @return Iterator
868 */
869 protected function initIterator( $dir ) {
870 if ( !empty( $this->params['topOnly'] ) ) { // non-recursive
871 # Get an iterator that will get direct sub-nodes
872 return new DirectoryIterator( $dir );
873 } else { // recursive
874 # Get an iterator that will return leaf nodes (non-directories)
875 # RecursiveDirectoryIterator extends FilesystemIterator.
876 # FilesystemIterator::SKIP_DOTS default is inconsistent in PHP 5.3.x.
877 $flags = FilesystemIterator::CURRENT_AS_SELF | FilesystemIterator::SKIP_DOTS;
878 return new RecursiveIteratorIterator(
879 new RecursiveDirectoryIterator( $dir, $flags ),
880 RecursiveIteratorIterator::CHILD_FIRST // include dirs
881 );
882 }
883 }
884
885 /**
886 * @see Iterator::key()
887 * @return integer
888 */
889 public function key() {
890 return $this->pos;
891 }
892
893 /**
894 * @see Iterator::current()
895 * @return string|bool String or false
896 */
897 public function current() {
898 return $this->getRelPath( $this->iter->current()->getPathname() );
899 }
900
901 /**
902 * @see Iterator::next()
903 * @return void
904 */
905 public function next() {
906 try {
907 $this->iter->next();
908 $this->filterViaNext();
909 } catch ( UnexpectedValueException $e ) {
910 $this->iter = null;
911 }
912 ++$this->pos;
913 }
914
915 /**
916 * @see Iterator::rewind()
917 * @return void
918 */
919 public function rewind() {
920 $this->pos = 0;
921 try {
922 $this->iter->rewind();
923 $this->filterViaNext();
924 } catch ( UnexpectedValueException $e ) {
925 $this->iter = null;
926 }
927 }
928
929 /**
930 * @see Iterator::valid()
931 * @return bool
932 */
933 public function valid() {
934 return $this->iter && $this->iter->valid();
935 }
936
937 /**
938 * Filter out items by advancing to the next ones
939 */
940 protected function filterViaNext() {}
941
942 /**
943 * Return only the relative path and normalize slashes to FileBackend-style.
944 * Uses the "real path" since the suffix is based upon that.
945 *
946 * @param $path string
947 * @return string
948 */
949 protected function getRelPath( $path ) {
950 return strtr( substr( realpath( $path ), $this->suffixStart ), '\\', '/' );
951 }
952 }
953
954 class FSFileBackendDirList extends FSFileBackendList {
955 protected function filterViaNext() {
956 while ( $this->iter->valid() ) {
957 if ( $this->iter->current()->isDot() || !$this->iter->current()->isDir() ) {
958 $this->iter->next(); // skip non-directories and dot files
959 } else {
960 break;
961 }
962 }
963 }
964 }
965
966 class FSFileBackendFileList extends FSFileBackendList {
967 protected function filterViaNext() {
968 while ( $this->iter->valid() ) {
969 if ( !$this->iter->current()->isFile() ) {
970 $this->iter->next(); // skip non-files and dot files
971 } else {
972 break;
973 }
974 }
975 }
976 }