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\AtEase\AtEase
;
44 use Wikimedia\Timestamp\ConvertibleTimestamp
;
47 * @brief Class for a file system (FS) based file backend.
49 * All "containers" each map to a directory under the backend's base directory.
50 * For backwards-compatibility, some container paths can be set to custom paths.
51 * The domain ID will not be used in any custom paths, so this should be avoided.
53 * Having directories with thousands of files will diminish performance.
54 * Sharding can be accomplished by using FileRepo-style hash paths.
56 * StatusValue messages should avoid mentioning the internal FS paths.
57 * PHP warnings are assumed to be logged rather than output.
59 * @ingroup FileBackend
62 class FSFileBackend
extends FileBackendStore
{
63 /** @var string Directory holding the container directories */
66 /** @var array Map of container names to root paths for custom container paths */
67 protected $containerPaths;
69 /** @var int Directory permission mode */
71 /** @var int File permission mode */
73 /** @var string Required OS username to own files */
76 /** @var bool Whether the OS is Windows (otherwise assumed Unix-like)*/
78 /** @var string OS username running this script */
79 protected $currentUser;
81 /** @var bool[] Map of (stack index => whether a warning happened) */
82 private $warningTrapStack = [];
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 $this->containerPaths
= [];
106 foreach ( ( $config['containerPaths'] ??
[] ) as $container => $path ) {
107 $this->containerPaths
[$container] = rtrim( $path, '/' ); // remove trailing slash
110 $this->fileMode
= $config['fileMode'] ??
0644;
111 $this->dirMode
= $config['directoryMode'] ??
0777;
112 if ( isset( $config['fileOwner'] ) && function_exists( 'posix_getuid' ) ) {
113 $this->fileOwner
= $config['fileOwner'];
114 // cache this, assuming it doesn't change
115 $this->currentUser
= posix_getpwuid( posix_getuid() )['name'];
119 public function getFeatures() {
120 if ( $this->isWindows
&& version_compare( PHP_VERSION
, '7.1', 'lt' ) ) {
121 // PHP before 7.1 used 8-bit code page for filesystem paths on Windows;
122 // See https://www.php.net/manual/en/migration71.windows-support.php
125 return FileBackend
::ATTR_UNICODE_PATHS
;
129 protected function resolveContainerPath( $container, $relStoragePath ) {
130 // Check that container has a root directory
131 if ( isset( $this->containerPaths
[$container] ) ||
isset( $this->basePath
) ) {
132 // Check for sane relative paths (assume the base paths are OK)
133 if ( $this->isLegalRelPath( $relStoragePath ) ) {
134 return $relStoragePath;
138 return null; // invalid
142 * Sanity check a relative file system path for validity
144 * @param string $path Normalized relative path
147 protected function isLegalRelPath( $path ) {
148 // Check for file names longer than 255 chars
149 if ( preg_match( '![^/]{256}!', $path ) ) { // ext3/NTFS
152 if ( $this->isWindows
) { // NTFS
153 return !preg_match( '![:*?"<>|]!', $path );
160 * Given the short (unresolved) and full (resolved) name of
161 * a container, return the file system path of the container.
163 * @param string $shortCont
164 * @param string $fullCont
165 * @return string|null
167 protected function containerFSRoot( $shortCont, $fullCont ) {
168 if ( isset( $this->containerPaths
[$shortCont] ) ) {
169 return $this->containerPaths
[$shortCont];
170 } elseif ( isset( $this->basePath
) ) {
171 return "{$this->basePath}/{$fullCont}";
174 return null; // no container base path defined
178 * Get the absolute file system path for a storage path
180 * @param string $storagePath Storage path
181 * @return string|null
183 protected function resolveToFSPath( $storagePath ) {
184 list( $fullCont, $relPath ) = $this->resolveStoragePathReal( $storagePath );
185 if ( $relPath === null ) {
186 return null; // invalid
188 list( , $shortCont, ) = FileBackend
::splitStoragePath( $storagePath );
189 $fsPath = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
190 if ( $relPath != '' ) {
191 $fsPath .= "/{$relPath}";
197 public function isPathUsableInternal( $storagePath ) {
198 $fsPath = $this->resolveToFSPath( $storagePath );
199 if ( $fsPath === null ) {
200 return false; // invalid
202 $parentDir = dirname( $fsPath );
204 if ( file_exists( $fsPath ) ) {
205 $ok = is_file( $fsPath ) && is_writable( $fsPath );
207 $ok = is_dir( $parentDir ) && is_writable( $parentDir );
210 if ( $this->fileOwner
!== null && $this->currentUser
!== $this->fileOwner
) {
212 trigger_error( __METHOD__
. ": PHP process owner is not '{$this->fileOwner}'." );
218 protected function doCreateInternal( array $params ) {
219 $status = $this->newStatus();
221 $dest = $this->resolveToFSPath( $params['dst'] );
222 if ( $dest === null ) {
223 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
228 if ( !empty( $params['async'] ) ) { // deferred
229 $tempFile = $this->stageContentAsTempFile( $params );
231 $status->fatal( 'backend-fail-create', $params['dst'] );
235 $cmd = implode( ' ', [
236 $this->isWindows ?
'COPY /B /Y' : 'cp', // (binary, overwrite)
237 escapeshellarg( $this->cleanPathSlashes( $tempFile->getPath() ) ),
238 escapeshellarg( $this->cleanPathSlashes( $dest ) )
240 $handler = function ( $errors, StatusValue
$status, array $params, $cmd ) {
241 if ( $errors !== '' && !( $this->isWindows
&& $errors[0] === " " ) ) {
242 $status->fatal( 'backend-fail-create', $params['dst'] );
243 trigger_error( "$cmd\n$errors", E_USER_WARNING
); // command output
246 $status->value
= new FSFileOpHandle( $this, $params, $handler, $cmd, $dest );
247 $tempFile->bind( $status->value
);
248 } else { // immediate write
249 $this->trapWarnings();
250 $bytes = file_put_contents( $dest, $params['content'] );
251 $this->untrapWarnings();
252 if ( $bytes === false ) {
253 $status->fatal( 'backend-fail-create', $params['dst'] );
257 $this->chmod( $dest );
263 protected function doStoreInternal( array $params ) {
264 $status = $this->newStatus();
266 $dest = $this->resolveToFSPath( $params['dst'] );
267 if ( $dest === null ) {
268 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
273 if ( !empty( $params['async'] ) ) { // deferred
274 $cmd = implode( ' ', [
275 $this->isWindows ?
'COPY /B /Y' : 'cp', // (binary, overwrite)
276 escapeshellarg( $this->cleanPathSlashes( $params['src'] ) ),
277 escapeshellarg( $this->cleanPathSlashes( $dest ) )
279 $handler = function ( $errors, StatusValue
$status, array $params, $cmd ) {
280 if ( $errors !== '' && !( $this->isWindows
&& $errors[0] === " " ) ) {
281 $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] );
282 trigger_error( "$cmd\n$errors", E_USER_WARNING
); // command output
285 $status->value
= new FSFileOpHandle( $this, $params, $handler, $cmd, $dest );
286 } else { // immediate write
287 $this->trapWarnings();
288 $ok = copy( $params['src'], $dest );
289 $this->untrapWarnings();
290 // In some cases (at least over NFS), copy() returns true when it fails
291 if ( !$ok ||
( filesize( $params['src'] ) !== filesize( $dest ) ) ) {
292 if ( $ok ) { // PHP bug
293 unlink( $dest ); // remove broken file
294 trigger_error( __METHOD__
. ": copy() failed but returned true." );
296 $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] );
300 $this->chmod( $dest );
306 protected function doCopyInternal( array $params ) {
307 $status = $this->newStatus();
309 $source = $this->resolveToFSPath( $params['src'] );
310 if ( $source === null ) {
311 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
316 $dest = $this->resolveToFSPath( $params['dst'] );
317 if ( $dest === null ) {
318 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
323 if ( !is_file( $source ) ) {
324 if ( empty( $params['ignoreMissingSource'] ) ) {
325 $status->fatal( 'backend-fail-copy', $params['src'] );
328 return $status; // do nothing; either OK or bad status
331 if ( !empty( $params['async'] ) ) { // deferred
332 $cmd = implode( ' ', [
333 $this->isWindows ?
'COPY /B /Y' : 'cp', // (binary, overwrite)
334 escapeshellarg( $this->cleanPathSlashes( $source ) ),
335 escapeshellarg( $this->cleanPathSlashes( $dest ) )
337 $handler = function ( $errors, StatusValue
$status, array $params, $cmd ) {
338 if ( $errors !== '' && !( $this->isWindows
&& $errors[0] === " " ) ) {
339 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
340 trigger_error( "$cmd\n$errors", E_USER_WARNING
); // command output
343 $status->value
= new FSFileOpHandle( $this, $params, $handler, $cmd, $dest );
344 } else { // immediate write
345 $this->trapWarnings();
346 $ok = ( $source === $dest ) ?
true : copy( $source, $dest );
347 $this->untrapWarnings();
348 // In some cases (at least over NFS), copy() returns true when it fails
349 if ( !$ok ||
( filesize( $source ) !== filesize( $dest ) ) ) {
350 if ( $ok ) { // PHP bug
351 $this->trapWarnings();
352 unlink( $dest ); // remove broken file
353 $this->untrapWarnings();
354 trigger_error( __METHOD__
. ": copy() failed but returned true." );
356 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
360 $this->chmod( $dest );
366 protected function doMoveInternal( array $params ) {
367 $status = $this->newStatus();
369 $source = $this->resolveToFSPath( $params['src'] );
370 if ( $source === null ) {
371 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
376 $dest = $this->resolveToFSPath( $params['dst'] );
377 if ( $dest === null ) {
378 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
383 if ( !is_file( $source ) ) {
384 if ( empty( $params['ignoreMissingSource'] ) ) {
385 $status->fatal( 'backend-fail-move', $params['src'] );
388 return $status; // do nothing; either OK or bad status
391 if ( !empty( $params['async'] ) ) { // deferred
392 $cmd = implode( ' ', [
393 $this->isWindows ?
'MOVE /Y' : 'mv', // (overwrite)
394 escapeshellarg( $this->cleanPathSlashes( $source ) ),
395 escapeshellarg( $this->cleanPathSlashes( $dest ) )
397 $handler = function ( $errors, StatusValue
$status, array $params, $cmd ) {
398 if ( $errors !== '' && !( $this->isWindows
&& $errors[0] === " " ) ) {
399 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
400 trigger_error( "$cmd\n$errors", E_USER_WARNING
); // command output
403 $status->value
= new FSFileOpHandle( $this, $params, $handler, $cmd );
404 } else { // immediate write
405 $this->trapWarnings();
406 $ok = ( $source === $dest ) ?
true : rename( $source, $dest );
407 $this->untrapWarnings();
408 clearstatcache(); // file no longer at source
410 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
419 protected function doDeleteInternal( array $params ) {
420 $status = $this->newStatus();
422 $fsSrcPath = $this->resolveToFSPath( $params['src'] );
423 if ( $fsSrcPath === null ) {
424 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
429 $ignoreMissing = !empty( $params['ignoreMissingSource'] );
431 if ( !empty( $params['async'] ) ) { // deferred
432 // https://manpages.debian.org/buster/coreutils/rm.1.en.html
433 // https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/del
434 $encSrc = escapeshellarg( $this->cleanPathSlashes( $fsSrcPath ) );
435 if ( $this->isWindows
) {
436 $writeCmd = "DEL /Q $encSrc";
437 $cmd = $ignoreMissing ?
"IF EXIST $encSrc $writeCmd" : $writeCmd;
439 $cmd = $ignoreMissing ?
"rm -f $encSrc" : "rm $encSrc";
441 $handler = function ( $errors, StatusValue
$status, array $params, $cmd ) {
442 if ( $errors !== '' && !( $this->isWindows
&& $errors[0] === " " ) ) {
443 $status->fatal( 'backend-fail-delete', $params['src'] );
444 trigger_error( "$cmd\n$errors", E_USER_WARNING
); // command output
447 $status->value
= new FSFileOpHandle( $this, $params, $handler, $cmd );
448 } else { // immediate write
449 $this->trapWarnings( '/: No such file or directory$/' );
450 $deleted = unlink( $fsSrcPath );
451 $hadError = $this->untrapWarnings();
452 if ( $hadError ||
( !$deleted && !$ignoreMissing ) ) {
453 $status->fatal( 'backend-fail-delete', $params['src'] );
463 * @param string $fullCont
464 * @param string $dirRel
465 * @param array $params
466 * @return StatusValue
468 protected function doPrepareInternal( $fullCont, $dirRel, array $params ) {
469 $status = $this->newStatus();
470 list( , $shortCont, ) = FileBackend
::splitStoragePath( $params['dir'] );
471 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
472 $dir = ( $dirRel != '' ) ?
"{$contRoot}/{$dirRel}" : $contRoot;
473 $existed = is_dir( $dir ); // already there?
474 // Create the directory and its parents as needed...
475 AtEase
::suppressWarnings();
476 if ( !$existed && !mkdir( $dir, $this->dirMode
, true ) && !is_dir( $dir ) ) {
477 $this->logger
->error( __METHOD__
. ": cannot create directory $dir" );
478 $status->fatal( 'directorycreateerror', $params['dir'] ); // fails on races
479 } elseif ( !is_writable( $dir ) ) {
480 $this->logger
->error( __METHOD__
. ": directory $dir is read-only" );
481 $status->fatal( 'directoryreadonlyerror', $params['dir'] );
482 } elseif ( !is_readable( $dir ) ) {
483 $this->logger
->error( __METHOD__
. ": directory $dir is not readable" );
484 $status->fatal( 'directorynotreadableerror', $params['dir'] );
486 AtEase
::restoreWarnings();
487 // Respect any 'noAccess' or 'noListing' flags...
488 if ( is_dir( $dir ) && !$existed ) {
489 $status->merge( $this->doSecureInternal( $fullCont, $dirRel, $params ) );
495 protected function doSecureInternal( $fullCont, $dirRel, array $params ) {
496 $status = $this->newStatus();
497 list( , $shortCont, ) = FileBackend
::splitStoragePath( $params['dir'] );
498 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
499 $dir = ( $dirRel != '' ) ?
"{$contRoot}/{$dirRel}" : $contRoot;
500 // Seed new directories with a blank index.html, to prevent crawling...
501 if ( !empty( $params['noListing'] ) && !file_exists( "{$dir}/index.html" ) ) {
502 $this->trapWarnings();
503 $bytes = file_put_contents( "{$dir}/index.html", $this->indexHtmlPrivate() );
504 $this->untrapWarnings();
505 if ( $bytes === false ) {
506 $status->fatal( 'backend-fail-create', $params['dir'] . '/index.html' );
509 // Add a .htaccess file to the root of the container...
510 if ( !empty( $params['noAccess'] ) && !file_exists( "{$contRoot}/.htaccess" ) ) {
511 AtEase
::suppressWarnings();
512 $bytes = file_put_contents( "{$contRoot}/.htaccess", $this->htaccessPrivate() );
513 AtEase
::restoreWarnings();
514 if ( $bytes === false ) {
515 $storeDir = "mwstore://{$this->name}/{$shortCont}";
516 $status->fatal( 'backend-fail-create', "{$storeDir}/.htaccess" );
523 protected function doPublishInternal( $fullCont, $dirRel, array $params ) {
524 $status = $this->newStatus();
525 list( , $shortCont, ) = FileBackend
::splitStoragePath( $params['dir'] );
526 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
527 $dir = ( $dirRel != '' ) ?
"{$contRoot}/{$dirRel}" : $contRoot;
528 // Unseed new directories with a blank index.html, to allow crawling...
529 if ( !empty( $params['listing'] ) && is_file( "{$dir}/index.html" ) ) {
530 $exists = ( file_get_contents( "{$dir}/index.html" ) === $this->indexHtmlPrivate() );
531 if ( $exists && !$this->unlink( "{$dir}/index.html" ) ) { // reverse secure()
532 $status->fatal( 'backend-fail-delete', $params['dir'] . '/index.html' );
535 // Remove the .htaccess file from the root of the container...
536 if ( !empty( $params['access'] ) && is_file( "{$contRoot}/.htaccess" ) ) {
537 $exists = ( file_get_contents( "{$contRoot}/.htaccess" ) === $this->htaccessPrivate() );
538 if ( $exists && !$this->unlink( "{$contRoot}/.htaccess" ) ) { // reverse secure()
539 $storeDir = "mwstore://{$this->name}/{$shortCont}";
540 $status->fatal( 'backend-fail-delete', "{$storeDir}/.htaccess" );
547 protected function doCleanInternal( $fullCont, $dirRel, array $params ) {
548 $status = $this->newStatus();
549 list( , $shortCont, ) = FileBackend
::splitStoragePath( $params['dir'] );
550 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
551 $dir = ( $dirRel != '' ) ?
"{$contRoot}/{$dirRel}" : $contRoot;
552 AtEase
::suppressWarnings();
553 if ( is_dir( $dir ) ) {
554 rmdir( $dir ); // remove directory if empty
556 AtEase
::restoreWarnings();
561 protected function doGetFileStat( array $params ) {
562 $source = $this->resolveToFSPath( $params['src'] );
563 if ( $source === null ) {
564 return self
::$RES_ERROR; // invalid storage path
567 $this->trapWarnings(); // don't trust 'false' if there were errors
568 $stat = is_file( $source ) ?
stat( $source ) : false; // regular files only
569 $hadError = $this->untrapWarnings();
571 if ( is_array( $stat ) ) {
572 $ct = new ConvertibleTimestamp( $stat['mtime'] );
575 'mtime' => $ct->getTimestamp( TS_MW
),
576 'size' => $stat['size']
580 return $hadError ? self
::$RES_ERROR : self
::$RES_ABSENT;
583 protected function doClearCache( array $paths = null ) {
584 clearstatcache(); // clear the PHP file stat cache
587 protected function doDirectoryExists( $fullCont, $dirRel, array $params ) {
588 list( , $shortCont, ) = FileBackend
::splitStoragePath( $params['dir'] );
589 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
590 $dir = ( $dirRel != '' ) ?
"{$contRoot}/{$dirRel}" : $contRoot;
592 $this->trapWarnings(); // don't trust 'false' if there were errors
593 $exists = is_dir( $dir );
594 $hadError = $this->untrapWarnings();
596 return $hadError ? self
::$RES_ERROR : $exists;
600 * @see FileBackendStore::getDirectoryListInternal()
601 * @param string $fullCont
602 * @param string $dirRel
603 * @param array $params
604 * @return array|FSFileBackendDirList|null
606 public function getDirectoryListInternal( $fullCont, $dirRel, array $params ) {
607 list( , $shortCont, ) = FileBackend
::splitStoragePath( $params['dir'] );
608 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
609 $dir = ( $dirRel != '' ) ?
"{$contRoot}/{$dirRel}" : $contRoot;
611 $this->trapWarnings(); // don't trust 'false' if there were errors
612 $exists = is_dir( $dir );
613 $isReadable = $exists ?
is_readable( $dir ) : false;
614 $hadError = $this->untrapWarnings();
617 return new FSFileBackendDirList( $dir, $params );
618 } elseif ( $exists ) {
619 $this->logger
->warning( __METHOD__
. ": given directory is unreadable: '$dir'" );
621 return self
::$RES_ERROR; // bad permissions?
622 } elseif ( $hadError ) {
623 $this->logger
->warning( __METHOD__
. ": given directory was unreachable: '$dir'" );
625 return self
::$RES_ERROR;
627 $this->logger
->info( __METHOD__
. ": given directory does not exist: '$dir'" );
629 return []; // nothing under this dir
634 * @see FileBackendStore::getFileListInternal()
635 * @param string $fullCont
636 * @param string $dirRel
637 * @param array $params
638 * @return array|FSFileBackendFileList|null
640 public function getFileListInternal( $fullCont, $dirRel, array $params ) {
641 list( , $shortCont, ) = FileBackend
::splitStoragePath( $params['dir'] );
642 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
643 $dir = ( $dirRel != '' ) ?
"{$contRoot}/{$dirRel}" : $contRoot;
645 $this->trapWarnings(); // don't trust 'false' if there were errors
646 $exists = is_dir( $dir );
647 $isReadable = $exists ?
is_readable( $dir ) : false;
648 $hadError = $this->untrapWarnings();
650 if ( $exists && $isReadable ) {
651 return new FSFileBackendFileList( $dir, $params );
652 } elseif ( $exists ) {
653 $this->logger
->warning( __METHOD__
. ": given directory is unreadable: '$dir'\n" );
655 return self
::$RES_ERROR; // bad permissions?
656 } elseif ( $hadError ) {
657 $this->logger
->warning( __METHOD__
. ": given directory was unreachable: '$dir'\n" );
659 return self
::$RES_ERROR;
661 $this->logger
->info( __METHOD__
. ": given directory does not exist: '$dir'\n" );
663 return []; // nothing under this dir
667 protected function doGetLocalReferenceMulti( array $params ) {
668 $fsFiles = []; // (path => FSFile)
670 foreach ( $params['srcs'] as $src ) {
671 $source = $this->resolveToFSPath( $src );
672 if ( $source === null ) {
673 $fsFiles[$src] = self
::$RES_ERROR; // invalid path
677 $this->trapWarnings(); // don't trust 'false' if there were errors
678 $isFile = is_file( $source ); // regular files only
679 $hadError = $this->untrapWarnings();
682 $fsFiles[$src] = new FSFile( $source );
683 } elseif ( $hadError ) {
684 $fsFiles[$src] = self
::$RES_ERROR;
686 $fsFiles[$src] = self
::$RES_ABSENT;
693 protected function doGetLocalCopyMulti( array $params ) {
694 $tmpFiles = []; // (path => TempFSFile)
696 foreach ( $params['srcs'] as $src ) {
697 $source = $this->resolveToFSPath( $src );
698 if ( $source === null ) {
699 $tmpFiles[$src] = self
::$RES_ERROR; // invalid path
702 // Create a new temporary file with the same extension...
703 $ext = FileBackend
::extensionFromPath( $src );
704 $tmpFile = $this->tmpFileFactory
->newTempFSFile( 'localcopy_', $ext );
706 $tmpFiles[$src] = self
::$RES_ERROR;
710 $tmpPath = $tmpFile->getPath();
711 // Copy the source file over the temp file
712 $this->trapWarnings(); // don't trust 'false' if there were errors
713 $isFile = is_file( $source ); // regular files only
714 $copySuccess = $isFile ?
copy( $source, $tmpPath ) : false;
715 $hadError = $this->untrapWarnings();
717 if ( $copySuccess ) {
718 $this->chmod( $tmpPath );
719 $tmpFiles[$src] = $tmpFile;
720 } elseif ( $hadError ) {
721 $tmpFiles[$src] = self
::$RES_ERROR; // copy failed
723 $tmpFiles[$src] = self
::$RES_ABSENT;
730 protected function directoriesAreVirtual() {
735 * @param FSFileOpHandle[] $fileOpHandles
737 * @return StatusValue[]
739 protected function doExecuteOpHandlesInternal( array $fileOpHandles ) {
743 $octalPermissions = '0' . decoct( $this->fileMode
);
744 foreach ( $fileOpHandles as $index => $fileOpHandle ) {
745 $cmd = "{$fileOpHandle->cmd} 2>&1";
746 // Add a post-operation chmod command for permissions cleanup if applicable
749 $fileOpHandle->chmodPath
!== null &&
750 strlen( $octalPermissions ) == 4
752 $encPath = escapeshellarg( $fileOpHandle->chmodPath
);
753 $cmd .= " && chmod $octalPermissions $encPath 2>/dev/null";
755 $pipes[$index] = popen( $cmd, 'r' );
759 foreach ( $pipes as $index => $pipe ) {
760 // Result will be empty on success in *NIX. On Windows,
761 // it may be something like " 1 file(s) [copied|moved].".
762 $errs[$index] = stream_get_contents( $pipe );
766 foreach ( $fileOpHandles as $index => $fileOpHandle ) {
767 $status = $this->newStatus();
768 $function = $fileOpHandle->call
;
769 $function( $errs[$index], $status, $fileOpHandle->params
, $fileOpHandle->cmd
);
770 $statuses[$index] = $status;
773 clearstatcache(); // files changed
779 * Chmod a file, suppressing the warnings
781 * @param string $path Absolute file system path
782 * @return bool Success
784 protected function chmod( $path ) {
785 if ( $this->isWindows
) {
789 AtEase
::suppressWarnings();
790 $ok = chmod( $path, $this->fileMode
);
791 AtEase
::restoreWarnings();
797 * Unlink a file, suppressing the warnings
799 * @param string $path Absolute file system path
800 * @return bool Success
802 protected function unlink( $path ) {
803 AtEase
::suppressWarnings();
804 $ok = unlink( $path );
805 AtEase
::restoreWarnings();
811 * @param array $params Operation parameters with 'content' and 'headers' fields
812 * @return TempFSFile|null
814 protected function stageContentAsTempFile( array $params ) {
815 $content = $params['content'];
816 $tempFile = $this->tmpFileFactory
->newTempFSFile( 'create_', 'tmp' );
821 AtEase
::suppressWarnings();
822 $tmpPath = $tempFile->getPath();
823 if ( file_put_contents( $tmpPath, $content ) === false ) {
826 AtEase
::restoreWarnings();
832 * Return the text of an index.html file to hide directory listings
836 protected function indexHtmlPrivate() {
841 * Return the text of a .htaccess file to make a directory private
845 protected function htaccessPrivate() {
846 return "Deny from all\n";
850 * Clean up directory separators for the given OS
852 * @param string $path FS path
855 protected function cleanPathSlashes( $path ) {
856 return $this->isWindows ?
strtr( $path, '/', '\\' ) : $path;
860 * Listen for E_WARNING errors and track whether any that happen
862 * @param string|null $regexIgnore Optional regex of errors to ignore
864 protected function trapWarnings( $regexIgnore = null ) {
865 $this->warningTrapStack
[] = false;
866 set_error_handler( function ( $errno, $errstr ) use ( $regexIgnore ) {
867 if ( $regexIgnore === null ||
!preg_match( $regexIgnore, $errstr ) ) {
868 $this->logger
->error( $errstr );
869 $this->warningTrapStack
[count( $this->warningTrapStack
) - 1] = true;
871 return true; // suppress from PHP handler
876 * Stop listening for E_WARNING errors and get whether any happened
878 * @return bool Whether any warnings happened
880 protected function untrapWarnings() {
881 restore_error_handler();
883 return array_pop( $this->warningTrapStack
);