3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
19 * @ingroup FileBackend
23 * File system based backend.
25 * This program is free software; you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation; either version 2 of the License, or
28 * (at your option) any later version.
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
35 * You should have received a copy of the GNU General Public License along
36 * with this program; if not, write to the Free Software Foundation, Inc.,
37 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
38 * http://www.gnu.org/copyleft/gpl.html
41 * @ingroup FileBackend
43 use Wikimedia\Timestamp\ConvertibleTimestamp
;
46 * @brief Class for a file system (FS) based file backend.
48 * All "containers" each map to a directory under the backend's base directory.
49 * For backwards-compatibility, some container paths can be set to custom paths.
50 * The domain ID will not be used in any custom paths, so this should be avoided.
52 * Having directories with thousands of files will diminish performance.
53 * Sharding can be accomplished by using FileRepo-style hash paths.
55 * StatusValue messages should avoid mentioning the internal FS paths.
56 * PHP warnings are assumed to be logged rather than output.
58 * @ingroup FileBackend
61 class FSFileBackend
extends FileBackendStore
{
62 /** @var string Directory holding the container directories */
65 /** @var array Map of container names to root paths for custom container paths */
66 protected $containerPaths = [];
68 /** @var int File permission mode */
70 /** @var int File permission mode */
73 /** @var string Required OS username to own files */
78 /** @var string OS username running this script */
79 protected $currentUser;
82 protected $hadWarningErrors = [];
85 * @see FileBackendStore::__construct()
86 * Additional $config params include:
87 * - basePath : File system directory that holds containers.
88 * - containerPaths : Map of container names to custom file system directories.
89 * This should only be used for backwards-compatibility.
90 * - fileMode : Octal UNIX file permissions to use on files stored.
91 * - directoryMode : Octal UNIX file permissions to use on directories created.
92 * @param array $config
94 public function __construct( array $config ) {
95 parent
::__construct( $config );
97 $this->isWindows
= ( strtoupper( substr( PHP_OS
, 0, 3 ) ) === 'WIN' );
98 // Remove any possible trailing slash from directories
99 if ( isset( $config['basePath'] ) ) {
100 $this->basePath
= rtrim( $config['basePath'], '/' ); // remove trailing slash
102 $this->basePath
= null; // none; containers must have explicit paths
105 if ( isset( $config['containerPaths'] ) ) {
106 $this->containerPaths
= (array)$config['containerPaths'];
107 foreach ( $this->containerPaths
as &$path ) {
108 $path = rtrim( $path, '/' ); // remove trailing slash
112 $this->fileMode
= $config['fileMode'] ??
0644;
113 $this->dirMode
= $config['directoryMode'] ??
0777;
114 if ( isset( $config['fileOwner'] ) && function_exists( 'posix_getuid' ) ) {
115 $this->fileOwner
= $config['fileOwner'];
116 // cache this, assuming it doesn't change
117 $this->currentUser
= posix_getpwuid( posix_getuid() )['name'];
121 public function getFeatures() {
122 if ( $this->isWindows
&& version_compare( PHP_VERSION
, '7.1', 'lt' ) ) {
123 // PHP before 7.1 used 8-bit code page for filesystem paths on Windows;
124 // See https://www.php.net/manual/en/migration71.windows-support.php
127 return FileBackend
::ATTR_UNICODE_PATHS
;
131 protected function resolveContainerPath( $container, $relStoragePath ) {
132 // Check that container has a root directory
133 if ( isset( $this->containerPaths
[$container] ) ||
isset( $this->basePath
) ) {
134 // Check for sane relative paths (assume the base paths are OK)
135 if ( $this->isLegalRelPath( $relStoragePath ) ) {
136 return $relStoragePath;
140 return null; // invalid
144 * Sanity check a relative file system path for validity
146 * @param string $path Normalized relative path
149 protected function isLegalRelPath( $path ) {
150 // Check for file names longer than 255 chars
151 if ( preg_match( '![^/]{256}!', $path ) ) { // ext3/NTFS
154 if ( $this->isWindows
) { // NTFS
155 return !preg_match( '![:*?"<>|]!', $path );
162 * Given the short (unresolved) and full (resolved) name of
163 * a container, return the file system path of the container.
165 * @param string $shortCont
166 * @param string $fullCont
167 * @return string|null
169 protected function containerFSRoot( $shortCont, $fullCont ) {
170 if ( isset( $this->containerPaths
[$shortCont] ) ) {
171 return $this->containerPaths
[$shortCont];
172 } elseif ( isset( $this->basePath
) ) {
173 return "{$this->basePath}/{$fullCont}";
176 return null; // no container base path defined
180 * Get the absolute file system path for a storage path
182 * @param string $storagePath Storage path
183 * @return string|null
185 protected function resolveToFSPath( $storagePath ) {
186 list( $fullCont, $relPath ) = $this->resolveStoragePathReal( $storagePath );
187 if ( $relPath === null ) {
188 return null; // invalid
190 list( , $shortCont, ) = FileBackend
::splitStoragePath( $storagePath );
191 $fsPath = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
192 if ( $relPath != '' ) {
193 $fsPath .= "/{$relPath}";
199 public function isPathUsableInternal( $storagePath ) {
200 $fsPath = $this->resolveToFSPath( $storagePath );
201 if ( $fsPath === null ) {
202 return false; // invalid
204 $parentDir = dirname( $fsPath );
206 if ( file_exists( $fsPath ) ) {
207 $ok = is_file( $fsPath ) && is_writable( $fsPath );
209 $ok = is_dir( $parentDir ) && is_writable( $parentDir );
212 if ( $this->fileOwner
!== null && $this->currentUser
!== $this->fileOwner
) {
214 trigger_error( __METHOD__
. ": PHP process owner is not '{$this->fileOwner}'." );
220 protected function doCreateInternal( array $params ) {
221 $status = $this->newStatus();
223 $dest = $this->resolveToFSPath( $params['dst'] );
224 if ( $dest === null ) {
225 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
230 if ( !empty( $params['async'] ) ) { // deferred
231 $tempFile = $this->tmpFileFactory
->newTempFSFile( 'create_', 'tmp' );
233 $status->fatal( 'backend-fail-create', $params['dst'] );
237 $this->trapWarnings();
238 $bytes = file_put_contents( $tempFile->getPath(), $params['content'] );
239 $this->untrapWarnings();
240 if ( $bytes === false ) {
241 $status->fatal( 'backend-fail-create', $params['dst'] );
245 $cmd = implode( ' ', [
246 $this->isWindows ?
'COPY /B /Y' : 'cp', // (binary, overwrite)
247 escapeshellarg( $this->cleanPathSlashes( $tempFile->getPath() ) ),
248 escapeshellarg( $this->cleanPathSlashes( $dest ) )
250 $handler = function ( $errors, StatusValue
$status, array $params, $cmd ) {
251 if ( $errors !== '' && !( $this->isWindows
&& $errors[0] === " " ) ) {
252 $status->fatal( 'backend-fail-create', $params['dst'] );
253 trigger_error( "$cmd\n$errors", E_USER_WARNING
); // command output
256 $status->value
= new FSFileOpHandle( $this, $params, $handler, $cmd, $dest );
257 $tempFile->bind( $status->value
);
258 } else { // immediate write
259 $this->trapWarnings();
260 $bytes = file_put_contents( $dest, $params['content'] );
261 $this->untrapWarnings();
262 if ( $bytes === false ) {
263 $status->fatal( 'backend-fail-create', $params['dst'] );
267 $this->chmod( $dest );
273 protected function doStoreInternal( array $params ) {
274 $status = $this->newStatus();
276 $dest = $this->resolveToFSPath( $params['dst'] );
277 if ( $dest === null ) {
278 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
283 if ( !empty( $params['async'] ) ) { // deferred
284 $cmd = implode( ' ', [
285 $this->isWindows ?
'COPY /B /Y' : 'cp', // (binary, overwrite)
286 escapeshellarg( $this->cleanPathSlashes( $params['src'] ) ),
287 escapeshellarg( $this->cleanPathSlashes( $dest ) )
289 $handler = function ( $errors, StatusValue
$status, array $params, $cmd ) {
290 if ( $errors !== '' && !( $this->isWindows
&& $errors[0] === " " ) ) {
291 $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] );
292 trigger_error( "$cmd\n$errors", E_USER_WARNING
); // command output
295 $status->value
= new FSFileOpHandle( $this, $params, $handler, $cmd, $dest );
296 } else { // immediate write
297 $this->trapWarnings();
298 $ok = copy( $params['src'], $dest );
299 $this->untrapWarnings();
300 // In some cases (at least over NFS), copy() returns true when it fails
301 if ( !$ok ||
( filesize( $params['src'] ) !== filesize( $dest ) ) ) {
302 if ( $ok ) { // PHP bug
303 unlink( $dest ); // remove broken file
304 trigger_error( __METHOD__
. ": copy() failed but returned true." );
306 $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] );
310 $this->chmod( $dest );
316 protected function doCopyInternal( array $params ) {
317 $status = $this->newStatus();
319 $source = $this->resolveToFSPath( $params['src'] );
320 if ( $source === null ) {
321 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
326 $dest = $this->resolveToFSPath( $params['dst'] );
327 if ( $dest === null ) {
328 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
333 if ( !is_file( $source ) ) {
334 if ( empty( $params['ignoreMissingSource'] ) ) {
335 $status->fatal( 'backend-fail-copy', $params['src'] );
338 return $status; // do nothing; either OK or bad status
341 if ( !empty( $params['async'] ) ) { // deferred
342 $cmd = implode( ' ', [
343 $this->isWindows ?
'COPY /B /Y' : 'cp', // (binary, overwrite)
344 escapeshellarg( $this->cleanPathSlashes( $source ) ),
345 escapeshellarg( $this->cleanPathSlashes( $dest ) )
347 $handler = function ( $errors, StatusValue
$status, array $params, $cmd ) {
348 if ( $errors !== '' && !( $this->isWindows
&& $errors[0] === " " ) ) {
349 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
350 trigger_error( "$cmd\n$errors", E_USER_WARNING
); // command output
353 $status->value
= new FSFileOpHandle( $this, $params, $handler, $cmd, $dest );
354 } else { // immediate write
355 $this->trapWarnings();
356 $ok = ( $source === $dest ) ?
true : copy( $source, $dest );
357 $this->untrapWarnings();
358 // In some cases (at least over NFS), copy() returns true when it fails
359 if ( !$ok ||
( filesize( $source ) !== filesize( $dest ) ) ) {
360 if ( $ok ) { // PHP bug
361 $this->trapWarnings();
362 unlink( $dest ); // remove broken file
363 $this->untrapWarnings();
364 trigger_error( __METHOD__
. ": copy() failed but returned true." );
366 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
370 $this->chmod( $dest );
376 protected function doMoveInternal( array $params ) {
377 $status = $this->newStatus();
379 $source = $this->resolveToFSPath( $params['src'] );
380 if ( $source === null ) {
381 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
386 $dest = $this->resolveToFSPath( $params['dst'] );
387 if ( $dest === null ) {
388 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
393 if ( !is_file( $source ) ) {
394 if ( empty( $params['ignoreMissingSource'] ) ) {
395 $status->fatal( 'backend-fail-move', $params['src'] );
398 return $status; // do nothing; either OK or bad status
401 if ( !empty( $params['async'] ) ) { // deferred
402 $cmd = implode( ' ', [
403 $this->isWindows ?
'MOVE /Y' : 'mv', // (overwrite)
404 escapeshellarg( $this->cleanPathSlashes( $source ) ),
405 escapeshellarg( $this->cleanPathSlashes( $dest ) )
407 $handler = function ( $errors, StatusValue
$status, array $params, $cmd ) {
408 if ( $errors !== '' && !( $this->isWindows
&& $errors[0] === " " ) ) {
409 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
410 trigger_error( "$cmd\n$errors", E_USER_WARNING
); // command output
413 $status->value
= new FSFileOpHandle( $this, $params, $handler, $cmd );
414 } else { // immediate write
415 $this->trapWarnings();
416 $ok = ( $source === $dest ) ?
true : rename( $source, $dest );
417 $this->untrapWarnings();
418 clearstatcache(); // file no longer at source
420 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
429 protected function doDeleteInternal( array $params ) {
430 $status = $this->newStatus();
432 $source = $this->resolveToFSPath( $params['src'] );
433 if ( $source === null ) {
434 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
439 if ( !is_file( $source ) ) {
440 if ( empty( $params['ignoreMissingSource'] ) ) {
441 $status->fatal( 'backend-fail-delete', $params['src'] );
444 return $status; // do nothing; either OK or bad status
447 if ( !empty( $params['async'] ) ) { // deferred
448 $cmd = implode( ' ', [
449 $this->isWindows ?
'DEL' : 'unlink',
450 escapeshellarg( $this->cleanPathSlashes( $source ) )
452 $handler = function ( $errors, StatusValue
$status, array $params, $cmd ) {
453 if ( $errors !== '' && !( $this->isWindows
&& $errors[0] === " " ) ) {
454 $status->fatal( 'backend-fail-delete', $params['src'] );
455 trigger_error( "$cmd\n$errors", E_USER_WARNING
); // command output
458 $status->value
= new FSFileOpHandle( $this, $params, $handler, $cmd );
459 } else { // immediate write
460 $this->trapWarnings();
461 $ok = unlink( $source );
462 $this->untrapWarnings();
464 $status->fatal( 'backend-fail-delete', $params['src'] );
474 * @param string $fullCont
475 * @param string $dirRel
476 * @param array $params
477 * @return StatusValue
479 protected function doPrepareInternal( $fullCont, $dirRel, array $params ) {
480 $status = $this->newStatus();
481 list( , $shortCont, ) = FileBackend
::splitStoragePath( $params['dir'] );
482 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
483 $dir = ( $dirRel != '' ) ?
"{$contRoot}/{$dirRel}" : $contRoot;
484 $existed = is_dir( $dir ); // already there?
485 // Create the directory and its parents as needed...
486 $this->trapWarnings();
487 if ( !$existed && !mkdir( $dir, $this->dirMode
, true ) && !is_dir( $dir ) ) {
488 $this->logger
->error( __METHOD__
. ": cannot create directory $dir" );
489 $status->fatal( 'directorycreateerror', $params['dir'] ); // fails on races
490 } elseif ( !is_writable( $dir ) ) {
491 $this->logger
->error( __METHOD__
. ": directory $dir is read-only" );
492 $status->fatal( 'directoryreadonlyerror', $params['dir'] );
493 } elseif ( !is_readable( $dir ) ) {
494 $this->logger
->error( __METHOD__
. ": directory $dir is not readable" );
495 $status->fatal( 'directorynotreadableerror', $params['dir'] );
497 $this->untrapWarnings();
498 // Respect any 'noAccess' or 'noListing' flags...
499 if ( is_dir( $dir ) && !$existed ) {
500 $status->merge( $this->doSecureInternal( $fullCont, $dirRel, $params ) );
506 protected function doSecureInternal( $fullCont, $dirRel, array $params ) {
507 $status = $this->newStatus();
508 list( , $shortCont, ) = FileBackend
::splitStoragePath( $params['dir'] );
509 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
510 $dir = ( $dirRel != '' ) ?
"{$contRoot}/{$dirRel}" : $contRoot;
511 // Seed new directories with a blank index.html, to prevent crawling...
512 if ( !empty( $params['noListing'] ) && !file_exists( "{$dir}/index.html" ) ) {
513 $this->trapWarnings();
514 $bytes = file_put_contents( "{$dir}/index.html", $this->indexHtmlPrivate() );
515 $this->untrapWarnings();
516 if ( $bytes === false ) {
517 $status->fatal( 'backend-fail-create', $params['dir'] . '/index.html' );
520 // Add a .htaccess file to the root of the container...
521 if ( !empty( $params['noAccess'] ) && !file_exists( "{$contRoot}/.htaccess" ) ) {
522 $this->trapWarnings();
523 $bytes = file_put_contents( "{$contRoot}/.htaccess", $this->htaccessPrivate() );
524 $this->untrapWarnings();
525 if ( $bytes === false ) {
526 $storeDir = "mwstore://{$this->name}/{$shortCont}";
527 $status->fatal( 'backend-fail-create', "{$storeDir}/.htaccess" );
534 protected function doPublishInternal( $fullCont, $dirRel, array $params ) {
535 $status = $this->newStatus();
536 list( , $shortCont, ) = FileBackend
::splitStoragePath( $params['dir'] );
537 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
538 $dir = ( $dirRel != '' ) ?
"{$contRoot}/{$dirRel}" : $contRoot;
539 // Unseed new directories with a blank index.html, to allow crawling...
540 if ( !empty( $params['listing'] ) && is_file( "{$dir}/index.html" ) ) {
541 $exists = ( file_get_contents( "{$dir}/index.html" ) === $this->indexHtmlPrivate() );
542 $this->trapWarnings();
543 if ( $exists && !unlink( "{$dir}/index.html" ) ) { // reverse secure()
544 $status->fatal( 'backend-fail-delete', $params['dir'] . '/index.html' );
546 $this->untrapWarnings();
548 // Remove the .htaccess file from the root of the container...
549 if ( !empty( $params['access'] ) && is_file( "{$contRoot}/.htaccess" ) ) {
550 $exists = ( file_get_contents( "{$contRoot}/.htaccess" ) === $this->htaccessPrivate() );
551 $this->trapWarnings();
552 if ( $exists && !unlink( "{$contRoot}/.htaccess" ) ) { // reverse secure()
553 $storeDir = "mwstore://{$this->name}/{$shortCont}";
554 $status->fatal( 'backend-fail-delete', "{$storeDir}/.htaccess" );
556 $this->untrapWarnings();
562 protected function doCleanInternal( $fullCont, $dirRel, array $params ) {
563 $status = $this->newStatus();
564 list( , $shortCont, ) = FileBackend
::splitStoragePath( $params['dir'] );
565 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
566 $dir = ( $dirRel != '' ) ?
"{$contRoot}/{$dirRel}" : $contRoot;
567 $this->trapWarnings();
568 if ( is_dir( $dir ) ) {
569 rmdir( $dir ); // remove directory if empty
571 $this->untrapWarnings();
576 protected function doGetFileStat( array $params ) {
577 $source = $this->resolveToFSPath( $params['src'] );
578 if ( $source === null ) {
579 return self
::$RES_ERROR; // invalid storage path
582 $this->trapWarnings(); // don't trust 'false' if there were errors
583 $stat = is_file( $source ) ?
stat( $source ) : false; // regular files only
584 $hadError = $this->untrapWarnings();
586 if ( is_array( $stat ) ) {
587 $ct = new ConvertibleTimestamp( $stat['mtime'] );
590 'mtime' => $ct->getTimestamp( TS_MW
),
591 'size' => $stat['size']
595 return $hadError ? self
::$RES_ERROR : self
::$RES_ABSENT;
598 protected function doClearCache( array $paths = null ) {
599 clearstatcache(); // clear the PHP file stat cache
602 protected function doDirectoryExists( $fullCont, $dirRel, array $params ) {
603 list( , $shortCont, ) = FileBackend
::splitStoragePath( $params['dir'] );
604 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
605 $dir = ( $dirRel != '' ) ?
"{$contRoot}/{$dirRel}" : $contRoot;
607 $this->trapWarnings(); // don't trust 'false' if there were errors
608 $exists = is_dir( $dir );
609 $hadError = $this->untrapWarnings();
611 return $hadError ? self
::$RES_ERROR : $exists;
615 * @see FileBackendStore::getDirectoryListInternal()
616 * @param string $fullCont
617 * @param string $dirRel
618 * @param array $params
619 * @return array|FSFileBackendDirList|null
621 public function getDirectoryListInternal( $fullCont, $dirRel, array $params ) {
622 list( , $shortCont, ) = FileBackend
::splitStoragePath( $params['dir'] );
623 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
624 $dir = ( $dirRel != '' ) ?
"{$contRoot}/{$dirRel}" : $contRoot;
626 $this->trapWarnings(); // don't trust 'false' if there were errors
627 $exists = is_dir( $dir );
628 $isReadable = $exists ?
is_readable( $dir ) : false;
629 $hadError = $this->untrapWarnings();
632 return new FSFileBackendDirList( $dir, $params );
633 } elseif ( $exists ) {
634 $this->logger
->warning( __METHOD__
. ": given directory is unreadable: '$dir'" );
636 return self
::$RES_ERROR; // bad permissions?
637 } elseif ( $hadError ) {
638 $this->logger
->warning( __METHOD__
. ": given directory was unreachable: '$dir'" );
640 return self
::$RES_ERROR;
642 $this->logger
->info( __METHOD__
. ": given directory does not exist: '$dir'" );
644 return []; // nothing under this dir
649 * @see FileBackendStore::getFileListInternal()
650 * @param string $fullCont
651 * @param string $dirRel
652 * @param array $params
653 * @return array|FSFileBackendFileList|null
655 public function getFileListInternal( $fullCont, $dirRel, array $params ) {
656 list( , $shortCont, ) = FileBackend
::splitStoragePath( $params['dir'] );
657 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
658 $dir = ( $dirRel != '' ) ?
"{$contRoot}/{$dirRel}" : $contRoot;
660 $this->trapWarnings(); // don't trust 'false' if there were errors
661 $exists = is_dir( $dir );
662 $isReadable = $exists ?
is_readable( $dir ) : false;
663 $hadError = $this->untrapWarnings();
665 if ( $exists && $isReadable ) {
666 return new FSFileBackendFileList( $dir, $params );
667 } elseif ( $exists ) {
668 $this->logger
->warning( __METHOD__
. ": given directory is unreadable: '$dir'\n" );
670 return self
::$RES_ERROR; // bad permissions?
671 } elseif ( $hadError ) {
672 $this->logger
->warning( __METHOD__
. ": given directory was unreachable: '$dir'\n" );
674 return self
::$RES_ERROR;
676 $this->logger
->info( __METHOD__
. ": given directory does not exist: '$dir'\n" );
678 return []; // nothing under this dir
682 protected function doGetLocalReferenceMulti( array $params ) {
683 $fsFiles = []; // (path => FSFile)
685 foreach ( $params['srcs'] as $src ) {
686 $source = $this->resolveToFSPath( $src );
687 if ( $source === null ) {
688 $fsFiles[$src] = self
::$RES_ERROR; // invalid path
692 $this->trapWarnings(); // don't trust 'false' if there were errors
693 $isFile = is_file( $source ); // regular files only
694 $hadError = $this->untrapWarnings();
697 $fsFiles[$src] = new FSFile( $source );
698 } elseif ( $hadError ) {
699 $fsFiles[$src] = self
::$RES_ERROR;
701 $fsFiles[$src] = self
::$RES_ABSENT;
708 protected function doGetLocalCopyMulti( array $params ) {
709 $tmpFiles = []; // (path => TempFSFile)
711 foreach ( $params['srcs'] as $src ) {
712 $source = $this->resolveToFSPath( $src );
713 if ( $source === null ) {
714 $tmpFiles[$src] = self
::$RES_ERROR; // invalid path
717 // Create a new temporary file with the same extension...
718 $ext = FileBackend
::extensionFromPath( $src );
719 $tmpFile = $this->tmpFileFactory
->newTempFSFile( 'localcopy_', $ext );
721 $tmpFiles[$src] = self
::$RES_ERROR;
725 $tmpPath = $tmpFile->getPath();
726 // Copy the source file over the temp file
727 $this->trapWarnings();
728 $isFile = is_file( $source ); // regular files only
729 $copySuccess = $isFile ?
copy( $source, $tmpPath ) : false;
730 $hadError = $this->untrapWarnings();
732 if ( $copySuccess ) {
733 $this->chmod( $tmpPath );
734 $tmpFiles[$src] = $tmpFile;
735 } elseif ( $hadError ) {
736 $tmpFiles[$src] = self
::$RES_ERROR; // copy failed
738 $tmpFiles[$src] = self
::$RES_ABSENT;
745 protected function directoriesAreVirtual() {
750 * @param FSFileOpHandle[] $fileOpHandles
752 * @return StatusValue[]
754 protected function doExecuteOpHandlesInternal( array $fileOpHandles ) {
758 foreach ( $fileOpHandles as $index => $fileOpHandle ) {
759 $pipes[$index] = popen( "{$fileOpHandle->cmd} 2>&1", 'r' );
763 foreach ( $pipes as $index => $pipe ) {
764 // Result will be empty on success in *NIX. On Windows,
765 // it may be something like " 1 file(s) [copied|moved].".
766 $errs[$index] = stream_get_contents( $pipe );
770 foreach ( $fileOpHandles as $index => $fileOpHandle ) {
771 $status = $this->newStatus();
772 $function = $fileOpHandle->call
;
773 $function( $errs[$index], $status, $fileOpHandle->params
, $fileOpHandle->cmd
);
774 $statuses[$index] = $status;
775 if ( $status->isOK() && $fileOpHandle->chmodPath
) {
776 $this->chmod( $fileOpHandle->chmodPath
);
780 clearstatcache(); // files changed
785 * Chmod a file, suppressing the warnings
787 * @param string $path Absolute file system path
788 * @return bool Success
790 protected function chmod( $path ) {
791 $this->trapWarnings();
792 $ok = chmod( $path, $this->fileMode
);
793 $this->untrapWarnings();
799 * Return the text of an index.html file to hide directory listings
803 protected function indexHtmlPrivate() {
808 * Return the text of a .htaccess file to make a directory private
812 protected function htaccessPrivate() {
813 return "Deny from all\n";
817 * Clean up directory separators for the given OS
819 * @param string $path FS path
822 protected function cleanPathSlashes( $path ) {
823 return $this->isWindows ?
strtr( $path, '/', '\\' ) : $path;
827 * Listen for E_WARNING errors and track whether any happen
829 protected function trapWarnings() {
831 $this->hadWarningErrors
[] = false;
832 set_error_handler( function ( $errno, $errstr ) {
833 // more detailed error logging
834 $this->logger
->error( $errstr );
835 $this->hadWarningErrors
[count( $this->hadWarningErrors
) - 1] = true;
837 // suppress from PHP handler
843 * Stop listening for E_WARNING errors and return true if any happened
847 protected function untrapWarnings() {
848 // restore previous handler
849 restore_error_handler();
851 return array_pop( $this->hadWarningErrors
);