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 self
::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 $fsSrcPath = $this->resolveToFSPath( $params['src'] );
370 if ( $fsSrcPath === null ) {
371 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
376 $fsDstPath = $this->resolveToFSPath( $params['dst'] );
377 if ( $fsDstPath === null ) {
378 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
383 if ( $fsSrcPath === $fsDstPath ) {
384 return $status; // no-op
387 $ignoreMissing = !empty( $params['ignoreMissingSource'] );
389 if ( !empty( $params['async'] ) ) { // deferred
390 // https://manpages.debian.org/buster/coreutils/mv.1.en.html
391 // https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/move
392 $encSrc = escapeshellarg( $this->cleanPathSlashes( $fsSrcPath ) );
393 $encDst = escapeshellarg( $this->cleanPathSlashes( $fsDstPath ) );
394 if ( $this->isWindows
) {
395 $writeCmd = "MOVE /Y $encSrc $encDst";
396 $cmd = $ignoreMissing ?
"IF EXIST $encSrc $writeCmd" : $writeCmd;
398 $writeCmd = "mv -f $encSrc $encDst";
399 $cmd = $ignoreMissing ?
"test -f $encSrc && $writeCmd" : $writeCmd;
401 $handler = function ( $errors, StatusValue
$status, array $params, $cmd ) {
402 if ( $errors !== '' && !( $this->isWindows
&& $errors[0] === " " ) ) {
403 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
404 trigger_error( "$cmd\n$errors", E_USER_WARNING
); // command output
407 $status->value
= new FSFileOpHandle( $this, $params, $handler, $cmd );
408 } else { // immediate write
409 // Use rename() here since (a) this clears xattrs, (b) any threads still reading the
410 // old inode are unaffected since it writes to a new inode, and (c) this is fast and
411 // atomic within a file system volume (as is normally the case)
412 $this->trapWarnings( '/: No such file or directory$/' );
413 $moved = rename( $fsSrcPath, $fsDstPath );
414 $hadError = $this->untrapWarnings();
415 if ( $hadError ||
( !$moved && !$ignoreMissing ) ) {
416 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
425 protected function doDeleteInternal( array $params ) {
426 $status = $this->newStatus();
428 $fsSrcPath = $this->resolveToFSPath( $params['src'] );
429 if ( $fsSrcPath === null ) {
430 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
435 $ignoreMissing = !empty( $params['ignoreMissingSource'] );
437 if ( !empty( $params['async'] ) ) { // deferred
438 // https://manpages.debian.org/buster/coreutils/rm.1.en.html
439 // https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/del
440 $encSrc = escapeshellarg( $this->cleanPathSlashes( $fsSrcPath ) );
441 if ( $this->isWindows
) {
442 $writeCmd = "DEL /Q $encSrc";
443 $cmd = $ignoreMissing ?
"IF EXIST $encSrc $writeCmd" : $writeCmd;
445 $cmd = $ignoreMissing ?
"rm -f $encSrc" : "rm $encSrc";
447 $handler = function ( $errors, StatusValue
$status, array $params, $cmd ) {
448 if ( $errors !== '' && !( $this->isWindows
&& $errors[0] === " " ) ) {
449 $status->fatal( 'backend-fail-delete', $params['src'] );
450 trigger_error( "$cmd\n$errors", E_USER_WARNING
); // command output
453 $status->value
= new FSFileOpHandle( $this, $params, $handler, $cmd );
454 } else { // immediate write
455 $this->trapWarnings( '/: No such file or directory$/' );
456 $deleted = unlink( $fsSrcPath );
457 $hadError = $this->untrapWarnings();
458 if ( $hadError ||
( !$deleted && !$ignoreMissing ) ) {
459 $status->fatal( 'backend-fail-delete', $params['src'] );
469 * @param string $fullCont
470 * @param string $dirRel
471 * @param array $params
472 * @return StatusValue
474 protected function doPrepareInternal( $fullCont, $dirRel, array $params ) {
475 $status = $this->newStatus();
476 list( , $shortCont, ) = FileBackend
::splitStoragePath( $params['dir'] );
477 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
478 $dir = ( $dirRel != '' ) ?
"{$contRoot}/{$dirRel}" : $contRoot;
479 $existed = is_dir( $dir ); // already there?
480 // Create the directory and its parents as needed...
481 AtEase
::suppressWarnings();
482 if ( !$existed && !mkdir( $dir, $this->dirMode
, true ) && !is_dir( $dir ) ) {
483 $this->logger
->error( __METHOD__
. ": cannot create directory $dir" );
484 $status->fatal( 'directorycreateerror', $params['dir'] ); // fails on races
485 } elseif ( !is_writable( $dir ) ) {
486 $this->logger
->error( __METHOD__
. ": directory $dir is read-only" );
487 $status->fatal( 'directoryreadonlyerror', $params['dir'] );
488 } elseif ( !is_readable( $dir ) ) {
489 $this->logger
->error( __METHOD__
. ": directory $dir is not readable" );
490 $status->fatal( 'directorynotreadableerror', $params['dir'] );
492 AtEase
::restoreWarnings();
493 // Respect any 'noAccess' or 'noListing' flags...
494 if ( is_dir( $dir ) && !$existed ) {
495 $status->merge( $this->doSecureInternal( $fullCont, $dirRel, $params ) );
501 protected function doSecureInternal( $fullCont, $dirRel, array $params ) {
502 $status = $this->newStatus();
503 list( , $shortCont, ) = FileBackend
::splitStoragePath( $params['dir'] );
504 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
505 $dir = ( $dirRel != '' ) ?
"{$contRoot}/{$dirRel}" : $contRoot;
506 // Seed new directories with a blank index.html, to prevent crawling...
507 if ( !empty( $params['noListing'] ) && !file_exists( "{$dir}/index.html" ) ) {
508 $this->trapWarnings();
509 $bytes = file_put_contents( "{$dir}/index.html", $this->indexHtmlPrivate() );
510 $this->untrapWarnings();
511 if ( $bytes === false ) {
512 $status->fatal( 'backend-fail-create', $params['dir'] . '/index.html' );
515 // Add a .htaccess file to the root of the container...
516 if ( !empty( $params['noAccess'] ) && !file_exists( "{$contRoot}/.htaccess" ) ) {
517 AtEase
::suppressWarnings();
518 $bytes = file_put_contents( "{$contRoot}/.htaccess", $this->htaccessPrivate() );
519 AtEase
::restoreWarnings();
520 if ( $bytes === false ) {
521 $storeDir = "mwstore://{$this->name}/{$shortCont}";
522 $status->fatal( 'backend-fail-create', "{$storeDir}/.htaccess" );
529 protected function doPublishInternal( $fullCont, $dirRel, array $params ) {
530 $status = $this->newStatus();
531 list( , $shortCont, ) = FileBackend
::splitStoragePath( $params['dir'] );
532 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
533 $dir = ( $dirRel != '' ) ?
"{$contRoot}/{$dirRel}" : $contRoot;
534 // Unseed new directories with a blank index.html, to allow crawling...
535 if ( !empty( $params['listing'] ) && is_file( "{$dir}/index.html" ) ) {
536 $exists = ( file_get_contents( "{$dir}/index.html" ) === $this->indexHtmlPrivate() );
537 if ( $exists && !$this->unlink( "{$dir}/index.html" ) ) { // reverse secure()
538 $status->fatal( 'backend-fail-delete', $params['dir'] . '/index.html' );
541 // Remove the .htaccess file from the root of the container...
542 if ( !empty( $params['access'] ) && is_file( "{$contRoot}/.htaccess" ) ) {
543 $exists = ( file_get_contents( "{$contRoot}/.htaccess" ) === $this->htaccessPrivate() );
544 if ( $exists && !$this->unlink( "{$contRoot}/.htaccess" ) ) { // reverse secure()
545 $storeDir = "mwstore://{$this->name}/{$shortCont}";
546 $status->fatal( 'backend-fail-delete', "{$storeDir}/.htaccess" );
553 protected function doCleanInternal( $fullCont, $dirRel, array $params ) {
554 $status = $this->newStatus();
555 list( , $shortCont, ) = FileBackend
::splitStoragePath( $params['dir'] );
556 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
557 $dir = ( $dirRel != '' ) ?
"{$contRoot}/{$dirRel}" : $contRoot;
558 AtEase
::suppressWarnings();
559 if ( is_dir( $dir ) ) {
560 rmdir( $dir ); // remove directory if empty
562 AtEase
::restoreWarnings();
567 protected function doGetFileStat( array $params ) {
568 $source = $this->resolveToFSPath( $params['src'] );
569 if ( $source === null ) {
570 return self
::$RES_ERROR; // invalid storage path
573 $this->trapWarnings(); // don't trust 'false' if there were errors
574 $stat = is_file( $source ) ?
stat( $source ) : false; // regular files only
575 $hadError = $this->untrapWarnings();
577 if ( is_array( $stat ) ) {
578 $ct = new ConvertibleTimestamp( $stat['mtime'] );
581 'mtime' => $ct->getTimestamp( TS_MW
),
582 'size' => $stat['size']
586 return $hadError ? self
::$RES_ERROR : self
::$RES_ABSENT;
589 protected function doClearCache( array $paths = null ) {
590 clearstatcache(); // clear the PHP file stat cache
593 protected function doDirectoryExists( $fullCont, $dirRel, array $params ) {
594 list( , $shortCont, ) = FileBackend
::splitStoragePath( $params['dir'] );
595 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
596 $dir = ( $dirRel != '' ) ?
"{$contRoot}/{$dirRel}" : $contRoot;
598 $this->trapWarnings(); // don't trust 'false' if there were errors
599 $exists = is_dir( $dir );
600 $hadError = $this->untrapWarnings();
602 return $hadError ? self
::$RES_ERROR : $exists;
606 * @see FileBackendStore::getDirectoryListInternal()
607 * @param string $fullCont
608 * @param string $dirRel
609 * @param array $params
610 * @return array|FSFileBackendDirList|null
612 public function getDirectoryListInternal( $fullCont, $dirRel, array $params ) {
613 list( , $shortCont, ) = FileBackend
::splitStoragePath( $params['dir'] );
614 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
615 $dir = ( $dirRel != '' ) ?
"{$contRoot}/{$dirRel}" : $contRoot;
617 $this->trapWarnings(); // don't trust 'false' if there were errors
618 $exists = is_dir( $dir );
619 $isReadable = $exists ?
is_readable( $dir ) : false;
620 $hadError = $this->untrapWarnings();
623 return new FSFileBackendDirList( $dir, $params );
624 } elseif ( $exists ) {
625 $this->logger
->warning( __METHOD__
. ": given directory is unreadable: '$dir'" );
627 return self
::$RES_ERROR; // bad permissions?
628 } elseif ( $hadError ) {
629 $this->logger
->warning( __METHOD__
. ": given directory was unreachable: '$dir'" );
631 return self
::$RES_ERROR;
633 $this->logger
->info( __METHOD__
. ": given directory does not exist: '$dir'" );
635 return []; // nothing under this dir
640 * @see FileBackendStore::getFileListInternal()
641 * @param string $fullCont
642 * @param string $dirRel
643 * @param array $params
644 * @return array|FSFileBackendFileList|null
646 public function getFileListInternal( $fullCont, $dirRel, array $params ) {
647 list( , $shortCont, ) = FileBackend
::splitStoragePath( $params['dir'] );
648 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
649 $dir = ( $dirRel != '' ) ?
"{$contRoot}/{$dirRel}" : $contRoot;
651 $this->trapWarnings(); // don't trust 'false' if there were errors
652 $exists = is_dir( $dir );
653 $isReadable = $exists ?
is_readable( $dir ) : false;
654 $hadError = $this->untrapWarnings();
656 if ( $exists && $isReadable ) {
657 return new FSFileBackendFileList( $dir, $params );
658 } elseif ( $exists ) {
659 $this->logger
->warning( __METHOD__
. ": given directory is unreadable: '$dir'\n" );
661 return self
::$RES_ERROR; // bad permissions?
662 } elseif ( $hadError ) {
663 $this->logger
->warning( __METHOD__
. ": given directory was unreachable: '$dir'\n" );
665 return self
::$RES_ERROR;
667 $this->logger
->info( __METHOD__
. ": given directory does not exist: '$dir'\n" );
669 return []; // nothing under this dir
673 protected function doGetLocalReferenceMulti( array $params ) {
674 $fsFiles = []; // (path => FSFile)
676 foreach ( $params['srcs'] as $src ) {
677 $source = $this->resolveToFSPath( $src );
678 if ( $source === null ) {
679 $fsFiles[$src] = self
::$RES_ERROR; // invalid path
683 $this->trapWarnings(); // don't trust 'false' if there were errors
684 $isFile = is_file( $source ); // regular files only
685 $hadError = $this->untrapWarnings();
688 $fsFiles[$src] = new FSFile( $source );
689 } elseif ( $hadError ) {
690 $fsFiles[$src] = self
::$RES_ERROR;
692 $fsFiles[$src] = self
::$RES_ABSENT;
699 protected function doGetLocalCopyMulti( array $params ) {
700 $tmpFiles = []; // (path => TempFSFile)
702 foreach ( $params['srcs'] as $src ) {
703 $source = $this->resolveToFSPath( $src );
704 if ( $source === null ) {
705 $tmpFiles[$src] = self
::$RES_ERROR; // invalid path
708 // Create a new temporary file with the same extension...
709 $ext = FileBackend
::extensionFromPath( $src );
710 $tmpFile = $this->tmpFileFactory
->newTempFSFile( 'localcopy_', $ext );
712 $tmpFiles[$src] = self
::$RES_ERROR;
716 $tmpPath = $tmpFile->getPath();
717 // Copy the source file over the temp file
718 $this->trapWarnings(); // don't trust 'false' if there were errors
719 $isFile = is_file( $source ); // regular files only
720 $copySuccess = $isFile ?
copy( $source, $tmpPath ) : false;
721 $hadError = $this->untrapWarnings();
723 if ( $copySuccess ) {
724 $this->chmod( $tmpPath );
725 $tmpFiles[$src] = $tmpFile;
726 } elseif ( $hadError ) {
727 $tmpFiles[$src] = self
::$RES_ERROR; // copy failed
729 $tmpFiles[$src] = self
::$RES_ABSENT;
736 protected function directoriesAreVirtual() {
741 * @param FSFileOpHandle[] $fileOpHandles
743 * @return StatusValue[]
745 protected function doExecuteOpHandlesInternal( array $fileOpHandles ) {
749 $octalPermissions = '0' . decoct( $this->fileMode
);
750 foreach ( $fileOpHandles as $index => $fileOpHandle ) {
751 $cmd = "{$fileOpHandle->cmd} 2>&1";
752 // Add a post-operation chmod command for permissions cleanup if applicable
755 $fileOpHandle->chmodPath
!== null &&
756 strlen( $octalPermissions ) == 4
758 $encPath = escapeshellarg( $fileOpHandle->chmodPath
);
759 $cmd .= " && chmod $octalPermissions $encPath 2>/dev/null";
761 $pipes[$index] = popen( $cmd, 'r' );
765 foreach ( $pipes as $index => $pipe ) {
766 // Result will be empty on success in *NIX. On Windows,
767 // it may be something like " 1 file(s) [copied|moved].".
768 $errs[$index] = stream_get_contents( $pipe );
772 foreach ( $fileOpHandles as $index => $fileOpHandle ) {
773 $status = $this->newStatus();
774 $function = $fileOpHandle->call
;
775 $function( $errs[$index], $status, $fileOpHandle->params
, $fileOpHandle->cmd
);
776 $statuses[$index] = $status;
779 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 if ( $this->isWindows
) {
795 AtEase
::suppressWarnings();
796 $ok = chmod( $path, $this->fileMode
);
797 AtEase
::restoreWarnings();
803 * Unlink a file, suppressing the warnings
805 * @param string $path Absolute file system path
806 * @return bool Success
808 protected function unlink( $path ) {
809 AtEase
::suppressWarnings();
810 $ok = unlink( $path );
811 AtEase
::restoreWarnings();
817 * @param array $params Operation parameters with 'content' and 'headers' fields
818 * @return TempFSFile|null
820 protected function stageContentAsTempFile( array $params ) {
821 $content = $params['content'];
822 $tempFile = $this->tmpFileFactory
->newTempFSFile( 'create_', 'tmp' );
827 AtEase
::suppressWarnings();
828 $tmpPath = $tempFile->getPath();
829 if ( file_put_contents( $tmpPath, $content ) === false ) {
832 AtEase
::restoreWarnings();
838 * Return the text of an index.html file to hide directory listings
842 protected function indexHtmlPrivate() {
847 * Return the text of a .htaccess file to make a directory private
851 protected function htaccessPrivate() {
852 return "Deny from all\n";
856 * Clean up directory separators for the given OS
858 * @param string $path FS path
861 protected function cleanPathSlashes( $path ) {
862 return $this->isWindows ?
strtr( $path, '/', '\\' ) : $path;
866 * Listen for E_WARNING errors and track whether any that happen
868 * @param string|null $regexIgnore Optional regex of errors to ignore
870 protected function trapWarnings( $regexIgnore = null ) {
871 $this->warningTrapStack
[] = false;
872 set_error_handler( function ( $errno, $errstr ) use ( $regexIgnore ) {
873 if ( $regexIgnore === null ||
!preg_match( $regexIgnore, $errstr ) ) {
874 $this->logger
->error( $errstr );
875 $this->warningTrapStack
[count( $this->warningTrapStack
) - 1] = true;
877 return true; // suppress from PHP handler
882 * Stop listening for E_WARNING errors and get whether any happened
884 * @return bool Whether any warnings happened
886 protected function untrapWarnings() {
887 restore_error_handler();
889 return array_pop( $this->warningTrapStack
);