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 rmdir( $dir ); // remove directory if empty
560 AtEase
::restoreWarnings();
565 protected function doGetFileStat( array $params ) {
566 $source = $this->resolveToFSPath( $params['src'] );
567 if ( $source === null ) {
568 return self
::$RES_ERROR; // invalid storage path
571 $this->trapWarnings(); // don't trust 'false' if there were errors
572 $stat = is_file( $source ) ?
stat( $source ) : false; // regular files only
573 $hadError = $this->untrapWarnings();
575 if ( is_array( $stat ) ) {
576 $ct = new ConvertibleTimestamp( $stat['mtime'] );
579 'mtime' => $ct->getTimestamp( TS_MW
),
580 'size' => $stat['size']
584 return $hadError ? self
::$RES_ERROR : self
::$RES_ABSENT;
587 protected function doClearCache( array $paths = null ) {
588 clearstatcache(); // clear the PHP file stat cache
591 protected function doDirectoryExists( $fullCont, $dirRel, array $params ) {
592 list( , $shortCont, ) = FileBackend
::splitStoragePath( $params['dir'] );
593 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
594 $dir = ( $dirRel != '' ) ?
"{$contRoot}/{$dirRel}" : $contRoot;
596 $this->trapWarnings(); // don't trust 'false' if there were errors
597 $exists = is_dir( $dir );
598 $hadError = $this->untrapWarnings();
600 return $hadError ? self
::$RES_ERROR : $exists;
604 * @see FileBackendStore::getDirectoryListInternal()
605 * @param string $fullCont
606 * @param string $dirRel
607 * @param array $params
608 * @return array|FSFileBackendDirList|null
610 public function getDirectoryListInternal( $fullCont, $dirRel, array $params ) {
611 list( , $shortCont, ) = FileBackend
::splitStoragePath( $params['dir'] );
612 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
613 $dir = ( $dirRel != '' ) ?
"{$contRoot}/{$dirRel}" : $contRoot;
615 $this->trapWarnings(); // don't trust 'false' if there were errors
616 $exists = is_dir( $dir );
617 $isReadable = $exists ?
is_readable( $dir ) : false;
618 $hadError = $this->untrapWarnings();
621 return new FSFileBackendDirList( $dir, $params );
622 } elseif ( $exists ) {
623 $this->logger
->warning( __METHOD__
. ": given directory is unreadable: '$dir'" );
625 return self
::$RES_ERROR; // bad permissions?
626 } elseif ( $hadError ) {
627 $this->logger
->warning( __METHOD__
. ": given directory was unreachable: '$dir'" );
629 return self
::$RES_ERROR;
631 $this->logger
->info( __METHOD__
. ": given directory does not exist: '$dir'" );
633 return []; // nothing under this dir
638 * @see FileBackendStore::getFileListInternal()
639 * @param string $fullCont
640 * @param string $dirRel
641 * @param array $params
642 * @return array|FSFileBackendFileList|null
644 public function getFileListInternal( $fullCont, $dirRel, array $params ) {
645 list( , $shortCont, ) = FileBackend
::splitStoragePath( $params['dir'] );
646 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
647 $dir = ( $dirRel != '' ) ?
"{$contRoot}/{$dirRel}" : $contRoot;
649 $this->trapWarnings(); // don't trust 'false' if there were errors
650 $exists = is_dir( $dir );
651 $isReadable = $exists ?
is_readable( $dir ) : false;
652 $hadError = $this->untrapWarnings();
654 if ( $exists && $isReadable ) {
655 return new FSFileBackendFileList( $dir, $params );
656 } elseif ( $exists ) {
657 $this->logger
->warning( __METHOD__
. ": given directory is unreadable: '$dir'\n" );
659 return self
::$RES_ERROR; // bad permissions?
660 } elseif ( $hadError ) {
661 $this->logger
->warning( __METHOD__
. ": given directory was unreachable: '$dir'\n" );
663 return self
::$RES_ERROR;
665 $this->logger
->info( __METHOD__
. ": given directory does not exist: '$dir'\n" );
667 return []; // nothing under this dir
671 protected function doGetLocalReferenceMulti( array $params ) {
672 $fsFiles = []; // (path => FSFile)
674 foreach ( $params['srcs'] as $src ) {
675 $source = $this->resolveToFSPath( $src );
676 if ( $source === null ) {
677 $fsFiles[$src] = self
::$RES_ERROR; // invalid path
681 $this->trapWarnings(); // don't trust 'false' if there were errors
682 $isFile = is_file( $source ); // regular files only
683 $hadError = $this->untrapWarnings();
686 $fsFiles[$src] = new FSFile( $source );
687 } elseif ( $hadError ) {
688 $fsFiles[$src] = self
::$RES_ERROR;
690 $fsFiles[$src] = self
::$RES_ABSENT;
697 protected function doGetLocalCopyMulti( array $params ) {
698 $tmpFiles = []; // (path => TempFSFile)
700 foreach ( $params['srcs'] as $src ) {
701 $source = $this->resolveToFSPath( $src );
702 if ( $source === null ) {
703 $tmpFiles[$src] = self
::$RES_ERROR; // invalid path
706 // Create a new temporary file with the same extension...
707 $ext = FileBackend
::extensionFromPath( $src );
708 $tmpFile = $this->tmpFileFactory
->newTempFSFile( 'localcopy_', $ext );
710 $tmpFiles[$src] = self
::$RES_ERROR;
714 $tmpPath = $tmpFile->getPath();
715 // Copy the source file over the temp file
716 $this->trapWarnings(); // don't trust 'false' if there were errors
717 $isFile = is_file( $source ); // regular files only
718 $copySuccess = $isFile ?
copy( $source, $tmpPath ) : false;
719 $hadError = $this->untrapWarnings();
721 if ( $copySuccess ) {
722 $this->chmod( $tmpPath );
723 $tmpFiles[$src] = $tmpFile;
724 } elseif ( $hadError ) {
725 $tmpFiles[$src] = self
::$RES_ERROR; // copy failed
727 $tmpFiles[$src] = self
::$RES_ABSENT;
734 protected function directoriesAreVirtual() {
739 * @param FSFileOpHandle[] $fileOpHandles
741 * @return StatusValue[]
743 protected function doExecuteOpHandlesInternal( array $fileOpHandles ) {
747 $octalPermissions = '0' . decoct( $this->fileMode
);
748 foreach ( $fileOpHandles as $index => $fileOpHandle ) {
749 $cmd = "{$fileOpHandle->cmd} 2>&1";
750 // Add a post-operation chmod command for permissions cleanup if applicable
753 $fileOpHandle->chmodPath
!== null &&
754 strlen( $octalPermissions ) == 4
756 $encPath = escapeshellarg( $fileOpHandle->chmodPath
);
757 $cmd .= " && chmod $octalPermissions $encPath 2>/dev/null";
759 $pipes[$index] = popen( $cmd, '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;
777 clearstatcache(); // files changed
783 * Chmod a file, suppressing the warnings
785 * @param string $path Absolute file system path
786 * @return bool Success
788 protected function chmod( $path ) {
789 if ( $this->isWindows
) {
793 AtEase
::suppressWarnings();
794 $ok = chmod( $path, $this->fileMode
);
795 AtEase
::restoreWarnings();
801 * Unlink a file, suppressing the warnings
803 * @param string $path Absolute file system path
804 * @return bool Success
806 protected function unlink( $path ) {
807 AtEase
::suppressWarnings();
808 $ok = unlink( $path );
809 AtEase
::restoreWarnings();
815 * @param array $params Operation parameters with 'content' and 'headers' fields
816 * @return TempFSFile|null
818 protected function stageContentAsTempFile( array $params ) {
819 $content = $params['content'];
820 $tempFile = $this->tmpFileFactory
->newTempFSFile( 'create_', 'tmp' );
825 AtEase
::suppressWarnings();
826 $tmpPath = $tempFile->getPath();
827 if ( file_put_contents( $tmpPath, $content ) === false ) {
830 AtEase
::restoreWarnings();
836 * Return the text of an index.html file to hide directory listings
840 protected function indexHtmlPrivate() {
845 * Return the text of a .htaccess file to make a directory private
849 protected function htaccessPrivate() {
850 return "Deny from all\n";
854 * Clean up directory separators for the given OS
856 * @param string $path FS path
859 protected function cleanPathSlashes( $path ) {
860 return $this->isWindows ?
strtr( $path, '/', '\\' ) : $path;
864 * Listen for E_WARNING errors and track whether any that happen
866 * @param string|null $regexIgnore Optional regex of errors to ignore
868 protected function trapWarnings( $regexIgnore = null ) {
869 $this->warningTrapStack
[] = false;
870 set_error_handler( function ( $errno, $errstr ) use ( $regexIgnore ) {
871 if ( $regexIgnore === null ||
!preg_match( $regexIgnore, $errstr ) ) {
872 $this->logger
->error( $errstr );
873 $this->warningTrapStack
[count( $this->warningTrapStack
) - 1] = true;
875 return true; // suppress from PHP handler
880 * Stop listening for E_WARNING errors and get whether any happened
882 * @return bool Whether any warnings happened
884 protected function untrapWarnings() {
885 restore_error_handler();
887 return array_pop( $this->warningTrapStack
);