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 File permission mode */
71 /** @var int File permission mode */
74 /** @var string Required OS username to own files */
77 /** @var bool Whether the OS is Windows (otherwise assumed Unix-like)*/
79 /** @var string OS username running this script */
80 protected $currentUser;
83 protected $hadWarningErrors = [];
86 * @see FileBackendStore::__construct()
87 * Additional $config params include:
88 * - basePath : File system directory that holds containers.
89 * - containerPaths : Map of container names to custom file system directories.
90 * This should only be used for backwards-compatibility.
91 * - fileMode : Octal UNIX file permissions to use on files stored.
92 * - directoryMode : Octal UNIX file permissions to use on directories created.
93 * @param array $config
95 public function __construct( array $config ) {
96 parent
::__construct( $config );
98 $this->isWindows
= ( strtoupper( substr( PHP_OS
, 0, 3 ) ) === 'WIN' );
99 // Remove any possible trailing slash from directories
100 if ( isset( $config['basePath'] ) ) {
101 $this->basePath
= rtrim( $config['basePath'], '/' ); // remove trailing slash
103 $this->basePath
= null; // none; containers must have explicit paths
106 $this->containerPaths
= [];
107 foreach ( ( $config['containerPaths'] ??
[] ) as $container => $path ) {
108 $this->containerPaths
[$container] = rtrim( $path, '/' ); // remove trailing slash
111 $this->fileMode
= $config['fileMode'] ??
0644;
112 $this->dirMode
= $config['directoryMode'] ??
0777;
113 if ( isset( $config['fileOwner'] ) && function_exists( 'posix_getuid' ) ) {
114 $this->fileOwner
= $config['fileOwner'];
115 // cache this, assuming it doesn't change
116 $this->currentUser
= posix_getpwuid( posix_getuid() )['name'];
120 public function getFeatures() {
121 if ( $this->isWindows
&& version_compare( PHP_VERSION
, '7.1', 'lt' ) ) {
122 // PHP before 7.1 used 8-bit code page for filesystem paths on Windows;
123 // See https://www.php.net/manual/en/migration71.windows-support.php
126 return FileBackend
::ATTR_UNICODE_PATHS
;
130 protected function resolveContainerPath( $container, $relStoragePath ) {
131 // Check that container has a root directory
132 if ( isset( $this->containerPaths
[$container] ) ||
isset( $this->basePath
) ) {
133 // Check for sane relative paths (assume the base paths are OK)
134 if ( $this->isLegalRelPath( $relStoragePath ) ) {
135 return $relStoragePath;
139 return null; // invalid
143 * Sanity check a relative file system path for validity
145 * @param string $path Normalized relative path
148 protected function isLegalRelPath( $path ) {
149 // Check for file names longer than 255 chars
150 if ( preg_match( '![^/]{256}!', $path ) ) { // ext3/NTFS
153 if ( $this->isWindows
) { // NTFS
154 return !preg_match( '![:*?"<>|]!', $path );
161 * Given the short (unresolved) and full (resolved) name of
162 * a container, return the file system path of the container.
164 * @param string $shortCont
165 * @param string $fullCont
166 * @return string|null
168 protected function containerFSRoot( $shortCont, $fullCont ) {
169 if ( isset( $this->containerPaths
[$shortCont] ) ) {
170 return $this->containerPaths
[$shortCont];
171 } elseif ( isset( $this->basePath
) ) {
172 return "{$this->basePath}/{$fullCont}";
175 return null; // no container base path defined
179 * Get the absolute file system path for a storage path
181 * @param string $storagePath Storage path
182 * @return string|null
184 protected function resolveToFSPath( $storagePath ) {
185 list( $fullCont, $relPath ) = $this->resolveStoragePathReal( $storagePath );
186 if ( $relPath === null ) {
187 return null; // invalid
189 list( , $shortCont, ) = FileBackend
::splitStoragePath( $storagePath );
190 $fsPath = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
191 if ( $relPath != '' ) {
192 $fsPath .= "/{$relPath}";
198 public function isPathUsableInternal( $storagePath ) {
199 $fsPath = $this->resolveToFSPath( $storagePath );
200 if ( $fsPath === null ) {
201 return false; // invalid
203 $parentDir = dirname( $fsPath );
205 if ( file_exists( $fsPath ) ) {
206 $ok = is_file( $fsPath ) && is_writable( $fsPath );
208 $ok = is_dir( $parentDir ) && is_writable( $parentDir );
211 if ( $this->fileOwner
!== null && $this->currentUser
!== $this->fileOwner
) {
213 trigger_error( __METHOD__
. ": PHP process owner is not '{$this->fileOwner}'." );
219 protected function doCreateInternal( array $params ) {
220 $status = $this->newStatus();
222 $dest = $this->resolveToFSPath( $params['dst'] );
223 if ( $dest === null ) {
224 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
229 if ( !empty( $params['async'] ) ) { // deferred
230 $tempFile = $this->stageContentAsTempFile( $params );
232 $status->fatal( 'backend-fail-create', $params['dst'] );
236 $cmd = implode( ' ', [
237 $this->isWindows ?
'COPY /B /Y' : 'cp', // (binary, overwrite)
238 escapeshellarg( $this->cleanPathSlashes( $tempFile->getPath() ) ),
239 escapeshellarg( $this->cleanPathSlashes( $dest ) )
241 $handler = function ( $errors, StatusValue
$status, array $params, $cmd ) {
242 if ( $errors !== '' && !( $this->isWindows
&& $errors[0] === " " ) ) {
243 $status->fatal( 'backend-fail-create', $params['dst'] );
244 trigger_error( "$cmd\n$errors", E_USER_WARNING
); // command output
247 $status->value
= new FSFileOpHandle( $this, $params, $handler, $cmd, $dest );
248 $tempFile->bind( $status->value
);
249 } else { // immediate write
250 $this->trapWarnings();
251 $bytes = file_put_contents( $dest, $params['content'] );
252 $this->untrapWarnings();
253 if ( $bytes === false ) {
254 $status->fatal( 'backend-fail-create', $params['dst'] );
258 $this->chmod( $dest );
264 protected function doStoreInternal( array $params ) {
265 $status = $this->newStatus();
267 $dest = $this->resolveToFSPath( $params['dst'] );
268 if ( $dest === null ) {
269 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
274 if ( !empty( $params['async'] ) ) { // deferred
275 $cmd = implode( ' ', [
276 $this->isWindows ?
'COPY /B /Y' : 'cp', // (binary, overwrite)
277 escapeshellarg( $this->cleanPathSlashes( $params['src'] ) ),
278 escapeshellarg( $this->cleanPathSlashes( $dest ) )
280 $handler = function ( $errors, StatusValue
$status, array $params, $cmd ) {
281 if ( $errors !== '' && !( $this->isWindows
&& $errors[0] === " " ) ) {
282 $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] );
283 trigger_error( "$cmd\n$errors", E_USER_WARNING
); // command output
286 $status->value
= new FSFileOpHandle( $this, $params, $handler, $cmd, $dest );
287 } else { // immediate write
288 $this->trapWarnings();
289 $ok = copy( $params['src'], $dest );
290 $this->untrapWarnings();
291 // In some cases (at least over NFS), copy() returns true when it fails
292 if ( !$ok ||
( filesize( $params['src'] ) !== filesize( $dest ) ) ) {
293 if ( $ok ) { // PHP bug
294 unlink( $dest ); // remove broken file
295 trigger_error( __METHOD__
. ": copy() failed but returned true." );
297 $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] );
301 $this->chmod( $dest );
307 protected function doCopyInternal( array $params ) {
308 $status = $this->newStatus();
310 $source = $this->resolveToFSPath( $params['src'] );
311 if ( $source === null ) {
312 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
317 $dest = $this->resolveToFSPath( $params['dst'] );
318 if ( $dest === null ) {
319 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
324 if ( !is_file( $source ) ) {
325 if ( empty( $params['ignoreMissingSource'] ) ) {
326 $status->fatal( 'backend-fail-copy', $params['src'] );
329 return $status; // do nothing; either OK or bad status
332 if ( !empty( $params['async'] ) ) { // deferred
333 $cmd = implode( ' ', [
334 $this->isWindows ?
'COPY /B /Y' : 'cp', // (binary, overwrite)
335 escapeshellarg( $this->cleanPathSlashes( $source ) ),
336 escapeshellarg( $this->cleanPathSlashes( $dest ) )
338 $handler = function ( $errors, StatusValue
$status, array $params, $cmd ) {
339 if ( $errors !== '' && !( $this->isWindows
&& $errors[0] === " " ) ) {
340 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
341 trigger_error( "$cmd\n$errors", E_USER_WARNING
); // command output
344 $status->value
= new FSFileOpHandle( $this, $params, $handler, $cmd, $dest );
345 } else { // immediate write
346 $this->trapWarnings();
347 $ok = ( $source === $dest ) ?
true : copy( $source, $dest );
348 $this->untrapWarnings();
349 // In some cases (at least over NFS), copy() returns true when it fails
350 if ( !$ok ||
( filesize( $source ) !== filesize( $dest ) ) ) {
351 if ( $ok ) { // PHP bug
352 $this->trapWarnings();
353 unlink( $dest ); // remove broken file
354 $this->untrapWarnings();
355 trigger_error( __METHOD__
. ": copy() failed but returned true." );
357 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
361 $this->chmod( $dest );
367 protected function doMoveInternal( array $params ) {
368 $status = $this->newStatus();
370 $source = $this->resolveToFSPath( $params['src'] );
371 if ( $source === null ) {
372 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
377 $dest = $this->resolveToFSPath( $params['dst'] );
378 if ( $dest === null ) {
379 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
384 if ( !is_file( $source ) ) {
385 if ( empty( $params['ignoreMissingSource'] ) ) {
386 $status->fatal( 'backend-fail-move', $params['src'] );
389 return $status; // do nothing; either OK or bad status
392 if ( !empty( $params['async'] ) ) { // deferred
393 $cmd = implode( ' ', [
394 $this->isWindows ?
'MOVE /Y' : 'mv', // (overwrite)
395 escapeshellarg( $this->cleanPathSlashes( $source ) ),
396 escapeshellarg( $this->cleanPathSlashes( $dest ) )
398 $handler = function ( $errors, StatusValue
$status, array $params, $cmd ) {
399 if ( $errors !== '' && !( $this->isWindows
&& $errors[0] === " " ) ) {
400 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
401 trigger_error( "$cmd\n$errors", E_USER_WARNING
); // command output
404 $status->value
= new FSFileOpHandle( $this, $params, $handler, $cmd );
405 } else { // immediate write
406 $this->trapWarnings();
407 $ok = ( $source === $dest ) ?
true : rename( $source, $dest );
408 $this->untrapWarnings();
409 clearstatcache(); // file no longer at source
411 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
420 protected function doDeleteInternal( array $params ) {
421 $status = $this->newStatus();
423 $source = $this->resolveToFSPath( $params['src'] );
424 if ( $source === null ) {
425 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
430 if ( !is_file( $source ) ) {
431 if ( empty( $params['ignoreMissingSource'] ) ) {
432 $status->fatal( 'backend-fail-delete', $params['src'] );
435 return $status; // do nothing; either OK or bad status
438 if ( !empty( $params['async'] ) ) { // deferred
439 $cmd = implode( ' ', [
440 $this->isWindows ?
'DEL' : 'unlink',
441 escapeshellarg( $this->cleanPathSlashes( $source ) )
443 $handler = function ( $errors, StatusValue
$status, array $params, $cmd ) {
444 if ( $errors !== '' && !( $this->isWindows
&& $errors[0] === " " ) ) {
445 $status->fatal( 'backend-fail-delete', $params['src'] );
446 trigger_error( "$cmd\n$errors", E_USER_WARNING
); // command output
449 $status->value
= new FSFileOpHandle( $this, $params, $handler, $cmd );
450 } else { // immediate write
451 $ok = $this->unlink( $source );
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();
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 happen
862 protected function trapWarnings() {
864 $this->hadWarningErrors
[] = false;
865 set_error_handler( function ( $errno, $errstr ) {
866 // more detailed error logging
867 $this->logger
->error( $errstr );
868 $this->hadWarningErrors
[count( $this->hadWarningErrors
) - 1] = true;
870 // suppress from PHP handler
876 * Stop listening for E_WARNING errors and return true if any happened
880 protected function untrapWarnings() {
881 // restore previous handler
882 restore_error_handler();
884 return array_pop( $this->hadWarningErrors
);