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