From: Aaron Schulz Date: Wed, 21 Sep 2016 22:36:16 +0000 (-0700) Subject: Move FSFilebackend and MemoryFileBackend to /libs X-Git-Tag: 1.31.0-rc.0~5335^2 X-Git-Url: http://git.cyclocoop.org/%22%24script/%7B%7B%20url_for%28?a=commitdiff_plain;h=1bf0390fbd88ab47e2b4f53b572569806d749380;p=lhc%2Fweb%2Fwiklou.git Move FSFilebackend and MemoryFileBackend to /libs Change-Id: Id38a2ef9e0e22537d0267b536f8a4dfd60c5b41d --- diff --git a/autoload.php b/autoload.php index f2347380dd..2df7cbb8d6 100644 --- a/autoload.php +++ b/autoload.php @@ -433,11 +433,11 @@ $wgAutoloadLocalClasses = [ 'ExternalStoreMedium' => __DIR__ . '/includes/externalstore/ExternalStoreMedium.php', 'ExternalStoreMwstore' => __DIR__ . '/includes/externalstore/ExternalStoreMwstore.php', 'FSFile' => __DIR__ . '/includes/libs/filebackend/FSFile.php', - 'FSFileBackend' => __DIR__ . '/includes/filebackend/FSFileBackend.php', - 'FSFileBackendDirList' => __DIR__ . '/includes/filebackend/FSFileBackend.php', - 'FSFileBackendFileList' => __DIR__ . '/includes/filebackend/FSFileBackend.php', - 'FSFileBackendList' => __DIR__ . '/includes/filebackend/FSFileBackend.php', - 'FSFileOpHandle' => __DIR__ . '/includes/filebackend/FSFileBackend.php', + 'FSFileBackend' => __DIR__ . '/includes/libs/filebackend/FSFileBackend.php', + 'FSFileBackendDirList' => __DIR__ . '/includes/libs/filebackend/FSFileBackend.php', + 'FSFileBackendFileList' => __DIR__ . '/includes/libs/filebackend/FSFileBackend.php', + 'FSFileBackendList' => __DIR__ . '/includes/libs/filebackend/FSFileBackend.php', + 'FSFileOpHandle' => __DIR__ . '/includes/libs/filebackend/FSFileBackend.php', 'FSLockManager' => __DIR__ . '/includes/libs/lockmanager/FSLockManager.php', 'FSRepo' => __DIR__ . '/includes/filerepo/FSRepo.php', 'FakeAuthTemplate' => __DIR__ . '/includes/specialpage/LoginSignupSpecialPage.php', @@ -924,7 +924,7 @@ $wgAutoloadLocalClasses = [ 'MemcachedPeclBagOStuff' => __DIR__ . '/includes/libs/objectcache/MemcachedPeclBagOStuff.php', 'MemcachedPhpBagOStuff' => __DIR__ . '/includes/libs/objectcache/MemcachedPhpBagOStuff.php', 'MemoizedCallable' => __DIR__ . '/includes/libs/MemoizedCallable.php', - 'MemoryFileBackend' => __DIR__ . '/includes/filebackend/MemoryFileBackend.php', + 'MemoryFileBackend' => __DIR__ . '/includes/libs/filebackend/MemoryFileBackend.php', 'MergeHistory' => __DIR__ . '/includes/MergeHistory.php', 'MergeHistoryPager' => __DIR__ . '/includes/specials/pagers/MergeHistoryPager.php', 'MergeLogFormatter' => __DIR__ . '/includes/logging/MergeLogFormatter.php', diff --git a/includes/filebackend/FSFileBackend.php b/includes/filebackend/FSFileBackend.php deleted file mode 100644 index ed1b4c00d3..0000000000 --- a/includes/filebackend/FSFileBackend.php +++ /dev/null @@ -1,984 +0,0 @@ -isWindows = ( strtoupper( substr( PHP_OS, 0, 3 ) ) === 'WIN' ); - // Remove any possible trailing slash from directories - if ( isset( $config['basePath'] ) ) { - $this->basePath = rtrim( $config['basePath'], '/' ); // remove trailing slash - } else { - $this->basePath = null; // none; containers must have explicit paths - } - - if ( isset( $config['containerPaths'] ) ) { - $this->containerPaths = (array)$config['containerPaths']; - foreach ( $this->containerPaths as &$path ) { - $path = rtrim( $path, '/' ); // remove trailing slash - } - } - - $this->fileMode = isset( $config['fileMode'] ) ? $config['fileMode'] : 0644; - $this->dirMode = isset( $config['directoryMode'] ) ? $config['directoryMode'] : 0777; - if ( isset( $config['fileOwner'] ) && function_exists( 'posix_getuid' ) ) { - $this->fileOwner = $config['fileOwner']; - // cache this, assuming it doesn't change - $this->currentUser = posix_getpwuid( posix_getuid() )['name']; - } - } - - public function getFeatures() { - return !$this->isWindows ? FileBackend::ATTR_UNICODE_PATHS : 0; - } - - protected function resolveContainerPath( $container, $relStoragePath ) { - // Check that container has a root directory - if ( isset( $this->containerPaths[$container] ) || isset( $this->basePath ) ) { - // Check for sane relative paths (assume the base paths are OK) - if ( $this->isLegalRelPath( $relStoragePath ) ) { - return $relStoragePath; - } - } - - return null; - } - - /** - * Sanity check a relative file system path for validity - * - * @param string $path Normalized relative path - * @return bool - */ - protected function isLegalRelPath( $path ) { - // Check for file names longer than 255 chars - if ( preg_match( '![^/]{256}!', $path ) ) { // ext3/NTFS - return false; - } - if ( $this->isWindows ) { // NTFS - return !preg_match( '![:*?"<>|]!', $path ); - } else { - return true; - } - } - - /** - * Given the short (unresolved) and full (resolved) name of - * a container, return the file system path of the container. - * - * @param string $shortCont - * @param string $fullCont - * @return string|null - */ - protected function containerFSRoot( $shortCont, $fullCont ) { - if ( isset( $this->containerPaths[$shortCont] ) ) { - return $this->containerPaths[$shortCont]; - } elseif ( isset( $this->basePath ) ) { - return "{$this->basePath}/{$fullCont}"; - } - - return null; // no container base path defined - } - - /** - * Get the absolute file system path for a storage path - * - * @param string $storagePath Storage path - * @return string|null - */ - protected function resolveToFSPath( $storagePath ) { - list( $fullCont, $relPath ) = $this->resolveStoragePathReal( $storagePath ); - if ( $relPath === null ) { - return null; // invalid - } - list( , $shortCont, ) = FileBackend::splitStoragePath( $storagePath ); - $fsPath = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid - if ( $relPath != '' ) { - $fsPath .= "/{$relPath}"; - } - - return $fsPath; - } - - public function isPathUsableInternal( $storagePath ) { - $fsPath = $this->resolveToFSPath( $storagePath ); - if ( $fsPath === null ) { - return false; // invalid - } - $parentDir = dirname( $fsPath ); - - if ( file_exists( $fsPath ) ) { - $ok = is_file( $fsPath ) && is_writable( $fsPath ); - } else { - $ok = is_dir( $parentDir ) && is_writable( $parentDir ); - } - - if ( $this->fileOwner !== null && $this->currentUser !== $this->fileOwner ) { - $ok = false; - trigger_error( __METHOD__ . ": PHP process owner is not '{$this->fileOwner}'." ); - } - - return $ok; - } - - protected function doCreateInternal( array $params ) { - $status = $this->newStatus(); - - $dest = $this->resolveToFSPath( $params['dst'] ); - if ( $dest === null ) { - $status->fatal( 'backend-fail-invalidpath', $params['dst'] ); - - return $status; - } - - if ( !empty( $params['async'] ) ) { // deferred - $tempFile = TempFSFile::factory( 'create_', 'tmp', $this->tmpDirectory ); - if ( !$tempFile ) { - $status->fatal( 'backend-fail-create', $params['dst'] ); - - return $status; - } - $this->trapWarnings(); - $bytes = file_put_contents( $tempFile->getPath(), $params['content'] ); - $this->untrapWarnings(); - if ( $bytes === false ) { - $status->fatal( 'backend-fail-create', $params['dst'] ); - - return $status; - } - $cmd = implode( ' ', [ - $this->isWindows ? 'COPY /B /Y' : 'cp', // (binary, overwrite) - escapeshellarg( $this->cleanPathSlashes( $tempFile->getPath() ) ), - escapeshellarg( $this->cleanPathSlashes( $dest ) ) - ] ); - $handler = function ( $errors, StatusValue $status, array $params, $cmd ) { - if ( $errors !== '' && !( $this->isWindows && $errors[0] === " " ) ) { - $status->fatal( 'backend-fail-create', $params['dst'] ); - trigger_error( "$cmd\n$errors", E_USER_WARNING ); // command output - } - }; - $status->value = new FSFileOpHandle( $this, $params, $handler, $cmd, $dest ); - $tempFile->bind( $status->value ); - } else { // immediate write - $this->trapWarnings(); - $bytes = file_put_contents( $dest, $params['content'] ); - $this->untrapWarnings(); - if ( $bytes === false ) { - $status->fatal( 'backend-fail-create', $params['dst'] ); - - return $status; - } - $this->chmod( $dest ); - } - - return $status; - } - - protected function doStoreInternal( array $params ) { - $status = $this->newStatus(); - - $dest = $this->resolveToFSPath( $params['dst'] ); - if ( $dest === null ) { - $status->fatal( 'backend-fail-invalidpath', $params['dst'] ); - - return $status; - } - - if ( !empty( $params['async'] ) ) { // deferred - $cmd = implode( ' ', [ - $this->isWindows ? 'COPY /B /Y' : 'cp', // (binary, overwrite) - escapeshellarg( $this->cleanPathSlashes( $params['src'] ) ), - escapeshellarg( $this->cleanPathSlashes( $dest ) ) - ] ); - $handler = function ( $errors, StatusValue $status, array $params, $cmd ) { - if ( $errors !== '' && !( $this->isWindows && $errors[0] === " " ) ) { - $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] ); - trigger_error( "$cmd\n$errors", E_USER_WARNING ); // command output - } - }; - $status->value = new FSFileOpHandle( $this, $params, $handler, $cmd, $dest ); - } else { // immediate write - $this->trapWarnings(); - $ok = copy( $params['src'], $dest ); - $this->untrapWarnings(); - // In some cases (at least over NFS), copy() returns true when it fails - if ( !$ok || ( filesize( $params['src'] ) !== filesize( $dest ) ) ) { - if ( $ok ) { // PHP bug - unlink( $dest ); // remove broken file - trigger_error( __METHOD__ . ": copy() failed but returned true." ); - } - $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] ); - - return $status; - } - $this->chmod( $dest ); - } - - return $status; - } - - protected function doCopyInternal( array $params ) { - $status = $this->newStatus(); - - $source = $this->resolveToFSPath( $params['src'] ); - if ( $source === null ) { - $status->fatal( 'backend-fail-invalidpath', $params['src'] ); - - return $status; - } - - $dest = $this->resolveToFSPath( $params['dst'] ); - if ( $dest === null ) { - $status->fatal( 'backend-fail-invalidpath', $params['dst'] ); - - return $status; - } - - if ( !is_file( $source ) ) { - if ( empty( $params['ignoreMissingSource'] ) ) { - $status->fatal( 'backend-fail-copy', $params['src'] ); - } - - return $status; // do nothing; either OK or bad status - } - - if ( !empty( $params['async'] ) ) { // deferred - $cmd = implode( ' ', [ - $this->isWindows ? 'COPY /B /Y' : 'cp', // (binary, overwrite) - escapeshellarg( $this->cleanPathSlashes( $source ) ), - escapeshellarg( $this->cleanPathSlashes( $dest ) ) - ] ); - $handler = function ( $errors, StatusValue $status, array $params, $cmd ) { - if ( $errors !== '' && !( $this->isWindows && $errors[0] === " " ) ) { - $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] ); - trigger_error( "$cmd\n$errors", E_USER_WARNING ); // command output - } - }; - $status->value = new FSFileOpHandle( $this, $params, $handler, $cmd, $dest ); - } else { // immediate write - $this->trapWarnings(); - $ok = ( $source === $dest ) ? true : copy( $source, $dest ); - $this->untrapWarnings(); - // In some cases (at least over NFS), copy() returns true when it fails - if ( !$ok || ( filesize( $source ) !== filesize( $dest ) ) ) { - if ( $ok ) { // PHP bug - $this->trapWarnings(); - unlink( $dest ); // remove broken file - $this->untrapWarnings(); - trigger_error( __METHOD__ . ": copy() failed but returned true." ); - } - $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] ); - - return $status; - } - $this->chmod( $dest ); - } - - return $status; - } - - protected function doMoveInternal( array $params ) { - $status = $this->newStatus(); - - $source = $this->resolveToFSPath( $params['src'] ); - if ( $source === null ) { - $status->fatal( 'backend-fail-invalidpath', $params['src'] ); - - return $status; - } - - $dest = $this->resolveToFSPath( $params['dst'] ); - if ( $dest === null ) { - $status->fatal( 'backend-fail-invalidpath', $params['dst'] ); - - return $status; - } - - if ( !is_file( $source ) ) { - if ( empty( $params['ignoreMissingSource'] ) ) { - $status->fatal( 'backend-fail-move', $params['src'] ); - } - - return $status; // do nothing; either OK or bad status - } - - if ( !empty( $params['async'] ) ) { // deferred - $cmd = implode( ' ', [ - $this->isWindows ? 'MOVE /Y' : 'mv', // (overwrite) - escapeshellarg( $this->cleanPathSlashes( $source ) ), - escapeshellarg( $this->cleanPathSlashes( $dest ) ) - ] ); - $handler = function ( $errors, StatusValue $status, array $params, $cmd ) { - if ( $errors !== '' && !( $this->isWindows && $errors[0] === " " ) ) { - $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] ); - trigger_error( "$cmd\n$errors", E_USER_WARNING ); // command output - } - }; - $status->value = new FSFileOpHandle( $this, $params, $handler, $cmd ); - } else { // immediate write - $this->trapWarnings(); - $ok = ( $source === $dest ) ? true : rename( $source, $dest ); - $this->untrapWarnings(); - clearstatcache(); // file no longer at source - if ( !$ok ) { - $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] ); - - return $status; - } - } - - return $status; - } - - protected function doDeleteInternal( array $params ) { - $status = $this->newStatus(); - - $source = $this->resolveToFSPath( $params['src'] ); - if ( $source === null ) { - $status->fatal( 'backend-fail-invalidpath', $params['src'] ); - - return $status; - } - - if ( !is_file( $source ) ) { - if ( empty( $params['ignoreMissingSource'] ) ) { - $status->fatal( 'backend-fail-delete', $params['src'] ); - } - - return $status; // do nothing; either OK or bad status - } - - if ( !empty( $params['async'] ) ) { // deferred - $cmd = implode( ' ', [ - $this->isWindows ? 'DEL' : 'unlink', - escapeshellarg( $this->cleanPathSlashes( $source ) ) - ] ); - $handler = function ( $errors, StatusValue $status, array $params, $cmd ) { - if ( $errors !== '' && !( $this->isWindows && $errors[0] === " " ) ) { - $status->fatal( 'backend-fail-delete', $params['src'] ); - trigger_error( "$cmd\n$errors", E_USER_WARNING ); // command output - } - }; - $status->value = new FSFileOpHandle( $this, $params, $handler, $cmd ); - } else { // immediate write - $this->trapWarnings(); - $ok = unlink( $source ); - $this->untrapWarnings(); - if ( !$ok ) { - $status->fatal( 'backend-fail-delete', $params['src'] ); - - return $status; - } - } - - return $status; - } - - /** - * @param string $fullCont - * @param string $dirRel - * @param array $params - * @return StatusValue - */ - protected function doPrepareInternal( $fullCont, $dirRel, array $params ) { - $status = $this->newStatus(); - list( , $shortCont, ) = FileBackend::splitStoragePath( $params['dir'] ); - $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid - $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot; - $existed = is_dir( $dir ); // already there? - // Create the directory and its parents as needed... - $this->trapWarnings(); - if ( !$existed && !mkdir( $dir, $this->dirMode, true ) && !is_dir( $dir ) ) { - $this->logger->error( __METHOD__ . ": cannot create directory $dir" ); - $status->fatal( 'directorycreateerror', $params['dir'] ); // fails on races - } elseif ( !is_writable( $dir ) ) { - $this->logger->error( __METHOD__ . ": directory $dir is read-only" ); - $status->fatal( 'directoryreadonlyerror', $params['dir'] ); - } elseif ( !is_readable( $dir ) ) { - $this->logger->error( __METHOD__ . ": directory $dir is not readable" ); - $status->fatal( 'directorynotreadableerror', $params['dir'] ); - } - $this->untrapWarnings(); - // Respect any 'noAccess' or 'noListing' flags... - if ( is_dir( $dir ) && !$existed ) { - $status->merge( $this->doSecureInternal( $fullCont, $dirRel, $params ) ); - } - - return $status; - } - - protected function doSecureInternal( $fullCont, $dirRel, array $params ) { - $status = $this->newStatus(); - list( , $shortCont, ) = FileBackend::splitStoragePath( $params['dir'] ); - $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid - $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot; - // Seed new directories with a blank index.html, to prevent crawling... - if ( !empty( $params['noListing'] ) && !file_exists( "{$dir}/index.html" ) ) { - $this->trapWarnings(); - $bytes = file_put_contents( "{$dir}/index.html", $this->indexHtmlPrivate() ); - $this->untrapWarnings(); - if ( $bytes === false ) { - $status->fatal( 'backend-fail-create', $params['dir'] . '/index.html' ); - } - } - // Add a .htaccess file to the root of the container... - if ( !empty( $params['noAccess'] ) && !file_exists( "{$contRoot}/.htaccess" ) ) { - $this->trapWarnings(); - $bytes = file_put_contents( "{$contRoot}/.htaccess", $this->htaccessPrivate() ); - $this->untrapWarnings(); - if ( $bytes === false ) { - $storeDir = "mwstore://{$this->name}/{$shortCont}"; - $status->fatal( 'backend-fail-create', "{$storeDir}/.htaccess" ); - } - } - - return $status; - } - - protected function doPublishInternal( $fullCont, $dirRel, array $params ) { - $status = $this->newStatus(); - list( , $shortCont, ) = FileBackend::splitStoragePath( $params['dir'] ); - $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid - $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot; - // Unseed new directories with a blank index.html, to allow crawling... - if ( !empty( $params['listing'] ) && is_file( "{$dir}/index.html" ) ) { - $exists = ( file_get_contents( "{$dir}/index.html" ) === $this->indexHtmlPrivate() ); - $this->trapWarnings(); - if ( $exists && !unlink( "{$dir}/index.html" ) ) { // reverse secure() - $status->fatal( 'backend-fail-delete', $params['dir'] . '/index.html' ); - } - $this->untrapWarnings(); - } - // Remove the .htaccess file from the root of the container... - if ( !empty( $params['access'] ) && is_file( "{$contRoot}/.htaccess" ) ) { - $exists = ( file_get_contents( "{$contRoot}/.htaccess" ) === $this->htaccessPrivate() ); - $this->trapWarnings(); - if ( $exists && !unlink( "{$contRoot}/.htaccess" ) ) { // reverse secure() - $storeDir = "mwstore://{$this->name}/{$shortCont}"; - $status->fatal( 'backend-fail-delete', "{$storeDir}/.htaccess" ); - } - $this->untrapWarnings(); - } - - return $status; - } - - protected function doCleanInternal( $fullCont, $dirRel, array $params ) { - $status = $this->newStatus(); - list( , $shortCont, ) = FileBackend::splitStoragePath( $params['dir'] ); - $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid - $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot; - $this->trapWarnings(); - if ( is_dir( $dir ) ) { - rmdir( $dir ); // remove directory if empty - } - $this->untrapWarnings(); - - return $status; - } - - protected function doGetFileStat( array $params ) { - $source = $this->resolveToFSPath( $params['src'] ); - if ( $source === null ) { - return false; // invalid storage path - } - - $this->trapWarnings(); // don't trust 'false' if there were errors - $stat = is_file( $source ) ? stat( $source ) : false; // regular files only - $hadError = $this->untrapWarnings(); - - if ( $stat ) { - $ct = new ConvertibleTimestamp( $stat['mtime'] ); - - return [ - 'mtime' => $ct->getTimestamp( TS_MW ), - 'size' => $stat['size'] - ]; - } elseif ( !$hadError ) { - return false; // file does not exist - } else { - return null; // failure - } - } - - protected function doClearCache( array $paths = null ) { - clearstatcache(); // clear the PHP file stat cache - } - - protected function doDirectoryExists( $fullCont, $dirRel, array $params ) { - list( , $shortCont, ) = FileBackend::splitStoragePath( $params['dir'] ); - $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid - $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot; - - $this->trapWarnings(); // don't trust 'false' if there were errors - $exists = is_dir( $dir ); - $hadError = $this->untrapWarnings(); - - return $hadError ? null : $exists; - } - - /** - * @see FileBackendStore::getDirectoryListInternal() - * @param string $fullCont - * @param string $dirRel - * @param array $params - * @return array|null - */ - public function getDirectoryListInternal( $fullCont, $dirRel, array $params ) { - list( , $shortCont, ) = FileBackend::splitStoragePath( $params['dir'] ); - $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid - $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot; - $exists = is_dir( $dir ); - if ( !$exists ) { - $this->logger->warning( __METHOD__ . "() given directory does not exist: '$dir'\n" ); - - return []; // nothing under this dir - } elseif ( !is_readable( $dir ) ) { - $this->logger->warning( __METHOD__ . "() given directory is unreadable: '$dir'\n" ); - - return null; // bad permissions? - } - - return new FSFileBackendDirList( $dir, $params ); - } - - /** - * @see FileBackendStore::getFileListInternal() - * @param string $fullCont - * @param string $dirRel - * @param array $params - * @return array|FSFileBackendFileList|null - */ - public function getFileListInternal( $fullCont, $dirRel, array $params ) { - list( , $shortCont, ) = FileBackend::splitStoragePath( $params['dir'] ); - $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid - $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot; - $exists = is_dir( $dir ); - if ( !$exists ) { - $this->logger->warning( __METHOD__ . "() given directory does not exist: '$dir'\n" ); - - return []; // nothing under this dir - } elseif ( !is_readable( $dir ) ) { - $this->logger->warning( __METHOD__ . "() given directory is unreadable: '$dir'\n" ); - - return null; // bad permissions? - } - - return new FSFileBackendFileList( $dir, $params ); - } - - protected function doGetLocalReferenceMulti( array $params ) { - $fsFiles = []; // (path => FSFile) - - foreach ( $params['srcs'] as $src ) { - $source = $this->resolveToFSPath( $src ); - if ( $source === null || !is_file( $source ) ) { - $fsFiles[$src] = null; // invalid path or file does not exist - } else { - $fsFiles[$src] = new FSFile( $source ); - } - } - - return $fsFiles; - } - - protected function doGetLocalCopyMulti( array $params ) { - $tmpFiles = []; // (path => TempFSFile) - - foreach ( $params['srcs'] as $src ) { - $source = $this->resolveToFSPath( $src ); - if ( $source === null ) { - $tmpFiles[$src] = null; // invalid path - } else { - // Create a new temporary file with the same extension... - $ext = FileBackend::extensionFromPath( $src ); - $tmpFile = TempFSFile::factory( 'localcopy_', $ext, $this->tmpDirectory ); - if ( !$tmpFile ) { - $tmpFiles[$src] = null; - } else { - $tmpPath = $tmpFile->getPath(); - // Copy the source file over the temp file - $this->trapWarnings(); - $ok = copy( $source, $tmpPath ); - $this->untrapWarnings(); - if ( !$ok ) { - $tmpFiles[$src] = null; - } else { - $this->chmod( $tmpPath ); - $tmpFiles[$src] = $tmpFile; - } - } - } - } - - return $tmpFiles; - } - - protected function directoriesAreVirtual() { - return false; - } - - /** - * @param FSFileOpHandle[] $fileOpHandles - * - * @return StatusValue[] - */ - protected function doExecuteOpHandlesInternal( array $fileOpHandles ) { - $statuses = []; - - $pipes = []; - foreach ( $fileOpHandles as $index => $fileOpHandle ) { - $pipes[$index] = popen( "{$fileOpHandle->cmd} 2>&1", 'r' ); - } - - $errs = []; - foreach ( $pipes as $index => $pipe ) { - // Result will be empty on success in *NIX. On Windows, - // it may be something like " 1 file(s) [copied|moved].". - $errs[$index] = stream_get_contents( $pipe ); - fclose( $pipe ); - } - - foreach ( $fileOpHandles as $index => $fileOpHandle ) { - $status = $this->newStatus(); - $function = $fileOpHandle->call; - $function( $errs[$index], $status, $fileOpHandle->params, $fileOpHandle->cmd ); - $statuses[$index] = $status; - if ( $status->isOK() && $fileOpHandle->chmodPath ) { - $this->chmod( $fileOpHandle->chmodPath ); - } - } - - clearstatcache(); // files changed - return $statuses; - } - - /** - * Chmod a file, suppressing the warnings - * - * @param string $path Absolute file system path - * @return bool Success - */ - protected function chmod( $path ) { - $this->trapWarnings(); - $ok = chmod( $path, $this->fileMode ); - $this->untrapWarnings(); - - return $ok; - } - - /** - * Return the text of an index.html file to hide directory listings - * - * @return string - */ - protected function indexHtmlPrivate() { - return ''; - } - - /** - * Return the text of a .htaccess file to make a directory private - * - * @return string - */ - protected function htaccessPrivate() { - return "Deny from all\n"; - } - - /** - * Clean up directory separators for the given OS - * - * @param string $path FS path - * @return string - */ - protected function cleanPathSlashes( $path ) { - return $this->isWindows ? strtr( $path, '/', '\\' ) : $path; - } - - /** - * Listen for E_WARNING errors and track whether any happen - */ - protected function trapWarnings() { - $this->hadWarningErrors[] = false; // push to stack - set_error_handler( [ $this, 'handleWarning' ], E_WARNING ); - } - - /** - * Stop listening for E_WARNING errors and return true if any happened - * - * @return bool - */ - protected function untrapWarnings() { - restore_error_handler(); // restore previous handler - return array_pop( $this->hadWarningErrors ); // pop from stack - } - - /** - * @param int $errno - * @param string $errstr - * @return bool - * @access private - */ - public function handleWarning( $errno, $errstr ) { - $this->logger->error( $errstr ); // more detailed error logging - $this->hadWarningErrors[count( $this->hadWarningErrors ) - 1] = true; - - return true; // suppress from PHP handler - } -} - -/** - * @see FileBackendStoreOpHandle - */ -class FSFileOpHandle extends FileBackendStoreOpHandle { - public $cmd; // string; shell command - public $chmodPath; // string; file to chmod - - /** - * @param FSFileBackend $backend - * @param array $params - * @param callable $call - * @param string $cmd - * @param int|null $chmodPath - */ - public function __construct( - FSFileBackend $backend, array $params, $call, $cmd, $chmodPath = null - ) { - $this->backend = $backend; - $this->params = $params; - $this->call = $call; - $this->cmd = $cmd; - $this->chmodPath = $chmodPath; - } -} - -/** - * Wrapper around RecursiveDirectoryIterator/DirectoryIterator that - * catches exception or does any custom behavoir that we may want. - * Do not use this class from places outside FSFileBackend. - * - * @ingroup FileBackend - */ -abstract class FSFileBackendList implements Iterator { - /** @var Iterator */ - protected $iter; - - /** @var int */ - protected $suffixStart; - - /** @var int */ - protected $pos = 0; - - /** @var array */ - protected $params = []; - - /** - * @param string $dir File system directory - * @param array $params - */ - public function __construct( $dir, array $params ) { - $path = realpath( $dir ); // normalize - if ( $path === false ) { - $path = $dir; - } - $this->suffixStart = strlen( $path ) + 1; // size of "path/to/dir/" - $this->params = $params; - - try { - $this->iter = $this->initIterator( $path ); - } catch ( UnexpectedValueException $e ) { - $this->iter = null; // bad permissions? deleted? - } - } - - /** - * Return an appropriate iterator object to wrap - * - * @param string $dir File system directory - * @return Iterator - */ - protected function initIterator( $dir ) { - if ( !empty( $this->params['topOnly'] ) ) { // non-recursive - # Get an iterator that will get direct sub-nodes - return new DirectoryIterator( $dir ); - } else { // recursive - # Get an iterator that will return leaf nodes (non-directories) - # RecursiveDirectoryIterator extends FilesystemIterator. - # FilesystemIterator::SKIP_DOTS default is inconsistent in PHP 5.3.x. - $flags = FilesystemIterator::CURRENT_AS_SELF | FilesystemIterator::SKIP_DOTS; - - return new RecursiveIteratorIterator( - new RecursiveDirectoryIterator( $dir, $flags ), - RecursiveIteratorIterator::CHILD_FIRST // include dirs - ); - } - } - - /** - * @see Iterator::key() - * @return int - */ - public function key() { - return $this->pos; - } - - /** - * @see Iterator::current() - * @return string|bool String or false - */ - public function current() { - return $this->getRelPath( $this->iter->current()->getPathname() ); - } - - /** - * @see Iterator::next() - * @throws FileBackendError - */ - public function next() { - try { - $this->iter->next(); - $this->filterViaNext(); - } catch ( UnexpectedValueException $e ) { // bad permissions? deleted? - throw new FileBackendError( "File iterator gave UnexpectedValueException." ); - } - ++$this->pos; - } - - /** - * @see Iterator::rewind() - * @throws FileBackendError - */ - public function rewind() { - $this->pos = 0; - try { - $this->iter->rewind(); - $this->filterViaNext(); - } catch ( UnexpectedValueException $e ) { // bad permissions? deleted? - throw new FileBackendError( "File iterator gave UnexpectedValueException." ); - } - } - - /** - * @see Iterator::valid() - * @return bool - */ - public function valid() { - return $this->iter && $this->iter->valid(); - } - - /** - * Filter out items by advancing to the next ones - */ - protected function filterViaNext() { - } - - /** - * Return only the relative path and normalize slashes to FileBackend-style. - * Uses the "real path" since the suffix is based upon that. - * - * @param string $dir - * @return string - */ - protected function getRelPath( $dir ) { - $path = realpath( $dir ); - if ( $path === false ) { - $path = $dir; - } - - return strtr( substr( $path, $this->suffixStart ), '\\', '/' ); - } -} - -class FSFileBackendDirList extends FSFileBackendList { - protected function filterViaNext() { - while ( $this->iter->valid() ) { - if ( $this->iter->current()->isDot() || !$this->iter->current()->isDir() ) { - $this->iter->next(); // skip non-directories and dot files - } else { - break; - } - } - } -} - -class FSFileBackendFileList extends FSFileBackendList { - protected function filterViaNext() { - while ( $this->iter->valid() ) { - if ( !$this->iter->current()->isFile() ) { - $this->iter->next(); // skip non-files and dot files - } else { - break; - } - } - } -} diff --git a/includes/filebackend/MemoryFileBackend.php b/includes/filebackend/MemoryFileBackend.php deleted file mode 100644 index 44fe2cbac6..0000000000 --- a/includes/filebackend/MemoryFileBackend.php +++ /dev/null @@ -1,263 +0,0 @@ - (data,mtime) */ - protected $files = []; - - public function getFeatures() { - return self::ATTR_UNICODE_PATHS; - } - - public function isPathUsableInternal( $storagePath ) { - return true; - } - - protected function doCreateInternal( array $params ) { - $status = $this->newStatus(); - - $dst = $this->resolveHashKey( $params['dst'] ); - if ( $dst === null ) { - $status->fatal( 'backend-fail-invalidpath', $params['dst'] ); - - return $status; - } - - $this->files[$dst] = [ - 'data' => $params['content'], - 'mtime' => wfTimestamp( TS_MW, time() ) - ]; - - return $status; - } - - protected function doStoreInternal( array $params ) { - $status = $this->newStatus(); - - $dst = $this->resolveHashKey( $params['dst'] ); - if ( $dst === null ) { - $status->fatal( 'backend-fail-invalidpath', $params['dst'] ); - - return $status; - } - - MediaWiki\suppressWarnings(); - $data = file_get_contents( $params['src'] ); - MediaWiki\restoreWarnings(); - if ( $data === false ) { // source doesn't exist? - $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] ); - - return $status; - } - - $this->files[$dst] = [ - 'data' => $data, - 'mtime' => wfTimestamp( TS_MW, time() ) - ]; - - return $status; - } - - protected function doCopyInternal( array $params ) { - $status = $this->newStatus(); - - $src = $this->resolveHashKey( $params['src'] ); - if ( $src === null ) { - $status->fatal( 'backend-fail-invalidpath', $params['src'] ); - - return $status; - } - - $dst = $this->resolveHashKey( $params['dst'] ); - if ( $dst === null ) { - $status->fatal( 'backend-fail-invalidpath', $params['dst'] ); - - return $status; - } - - if ( !isset( $this->files[$src] ) ) { - if ( empty( $params['ignoreMissingSource'] ) ) { - $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] ); - } - - return $status; - } - - $this->files[$dst] = [ - 'data' => $this->files[$src]['data'], - 'mtime' => wfTimestamp( TS_MW, time() ) - ]; - - return $status; - } - - protected function doDeleteInternal( array $params ) { - $status = $this->newStatus(); - - $src = $this->resolveHashKey( $params['src'] ); - if ( $src === null ) { - $status->fatal( 'backend-fail-invalidpath', $params['src'] ); - - return $status; - } - - if ( !isset( $this->files[$src] ) ) { - if ( empty( $params['ignoreMissingSource'] ) ) { - $status->fatal( 'backend-fail-delete', $params['src'] ); - } - - return $status; - } - - unset( $this->files[$src] ); - - return $status; - } - - protected function doGetFileStat( array $params ) { - $src = $this->resolveHashKey( $params['src'] ); - if ( $src === null ) { - return null; - } - - if ( isset( $this->files[$src] ) ) { - return [ - 'mtime' => $this->files[$src]['mtime'], - 'size' => strlen( $this->files[$src]['data'] ), - ]; - } - - return false; - } - - protected function doGetLocalCopyMulti( array $params ) { - $tmpFiles = []; // (path => TempFSFile) - foreach ( $params['srcs'] as $srcPath ) { - $src = $this->resolveHashKey( $srcPath ); - if ( $src === null || !isset( $this->files[$src] ) ) { - $fsFile = null; - } else { - // Create a new temporary file with the same extension... - $ext = FileBackend::extensionFromPath( $src ); - $fsFile = TempFSFile::factory( 'localcopy_', $ext, $this->tmpDirectory ); - if ( $fsFile ) { - $bytes = file_put_contents( $fsFile->getPath(), $this->files[$src]['data'] ); - if ( $bytes !== strlen( $this->files[$src]['data'] ) ) { - $fsFile = null; - } - } - } - $tmpFiles[$srcPath] = $fsFile; - } - - return $tmpFiles; - } - - protected function doDirectoryExists( $container, $dir, array $params ) { - $prefix = rtrim( "$container/$dir", '/' ) . '/'; - foreach ( $this->files as $path => $data ) { - if ( strpos( $path, $prefix ) === 0 ) { - return true; - } - } - - return false; - } - - public function getDirectoryListInternal( $container, $dir, array $params ) { - $dirs = []; - $prefix = rtrim( "$container/$dir", '/' ) . '/'; - $prefixLen = strlen( $prefix ); - foreach ( $this->files as $path => $data ) { - if ( strpos( $path, $prefix ) === 0 ) { - $relPath = substr( $path, $prefixLen ); - if ( $relPath === false ) { - continue; - } elseif ( strpos( $relPath, '/' ) === false ) { - continue; // just a file - } - $parts = array_slice( explode( '/', $relPath ), 0, -1 ); // last part is file name - if ( !empty( $params['topOnly'] ) ) { - $dirs[$parts[0]] = 1; // top directory - } else { - $current = ''; - foreach ( $parts as $part ) { // all directories - $dir = ( $current === '' ) ? $part : "$current/$part"; - $dirs[$dir] = 1; - $current = $dir; - } - } - } - } - - return array_keys( $dirs ); - } - - public function getFileListInternal( $container, $dir, array $params ) { - $files = []; - $prefix = rtrim( "$container/$dir", '/' ) . '/'; - $prefixLen = strlen( $prefix ); - foreach ( $this->files as $path => $data ) { - if ( strpos( $path, $prefix ) === 0 ) { - $relPath = substr( $path, $prefixLen ); - if ( $relPath === false ) { - continue; - } elseif ( !empty( $params['topOnly'] ) && strpos( $relPath, '/' ) !== false ) { - continue; - } - $files[] = $relPath; - } - } - - return $files; - } - - protected function directoriesAreVirtual() { - return true; - } - - /** - * Get the absolute file system path for a storage path - * - * @param string $storagePath Storage path - * @return string|null - */ - protected function resolveHashKey( $storagePath ) { - list( $fullCont, $relPath ) = $this->resolveStoragePathReal( $storagePath ); - if ( $relPath === null ) { - return null; // invalid - } - - return ( $relPath !== '' ) ? "$fullCont/$relPath" : $fullCont; - } -} diff --git a/includes/libs/filebackend/FSFileBackend.php b/includes/libs/filebackend/FSFileBackend.php new file mode 100644 index 0000000000..99184e1513 --- /dev/null +++ b/includes/libs/filebackend/FSFileBackend.php @@ -0,0 +1,984 @@ +isWindows = ( strtoupper( substr( PHP_OS, 0, 3 ) ) === 'WIN' ); + // Remove any possible trailing slash from directories + if ( isset( $config['basePath'] ) ) { + $this->basePath = rtrim( $config['basePath'], '/' ); // remove trailing slash + } else { + $this->basePath = null; // none; containers must have explicit paths + } + + if ( isset( $config['containerPaths'] ) ) { + $this->containerPaths = (array)$config['containerPaths']; + foreach ( $this->containerPaths as &$path ) { + $path = rtrim( $path, '/' ); // remove trailing slash + } + } + + $this->fileMode = isset( $config['fileMode'] ) ? $config['fileMode'] : 0644; + $this->dirMode = isset( $config['directoryMode'] ) ? $config['directoryMode'] : 0777; + if ( isset( $config['fileOwner'] ) && function_exists( 'posix_getuid' ) ) { + $this->fileOwner = $config['fileOwner']; + // cache this, assuming it doesn't change + $this->currentUser = posix_getpwuid( posix_getuid() )['name']; + } + } + + public function getFeatures() { + return !$this->isWindows ? FileBackend::ATTR_UNICODE_PATHS : 0; + } + + protected function resolveContainerPath( $container, $relStoragePath ) { + // Check that container has a root directory + if ( isset( $this->containerPaths[$container] ) || isset( $this->basePath ) ) { + // Check for sane relative paths (assume the base paths are OK) + if ( $this->isLegalRelPath( $relStoragePath ) ) { + return $relStoragePath; + } + } + + return null; + } + + /** + * Sanity check a relative file system path for validity + * + * @param string $path Normalized relative path + * @return bool + */ + protected function isLegalRelPath( $path ) { + // Check for file names longer than 255 chars + if ( preg_match( '![^/]{256}!', $path ) ) { // ext3/NTFS + return false; + } + if ( $this->isWindows ) { // NTFS + return !preg_match( '![:*?"<>|]!', $path ); + } else { + return true; + } + } + + /** + * Given the short (unresolved) and full (resolved) name of + * a container, return the file system path of the container. + * + * @param string $shortCont + * @param string $fullCont + * @return string|null + */ + protected function containerFSRoot( $shortCont, $fullCont ) { + if ( isset( $this->containerPaths[$shortCont] ) ) { + return $this->containerPaths[$shortCont]; + } elseif ( isset( $this->basePath ) ) { + return "{$this->basePath}/{$fullCont}"; + } + + return null; // no container base path defined + } + + /** + * Get the absolute file system path for a storage path + * + * @param string $storagePath Storage path + * @return string|null + */ + protected function resolveToFSPath( $storagePath ) { + list( $fullCont, $relPath ) = $this->resolveStoragePathReal( $storagePath ); + if ( $relPath === null ) { + return null; // invalid + } + list( , $shortCont, ) = FileBackend::splitStoragePath( $storagePath ); + $fsPath = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid + if ( $relPath != '' ) { + $fsPath .= "/{$relPath}"; + } + + return $fsPath; + } + + public function isPathUsableInternal( $storagePath ) { + $fsPath = $this->resolveToFSPath( $storagePath ); + if ( $fsPath === null ) { + return false; // invalid + } + $parentDir = dirname( $fsPath ); + + if ( file_exists( $fsPath ) ) { + $ok = is_file( $fsPath ) && is_writable( $fsPath ); + } else { + $ok = is_dir( $parentDir ) && is_writable( $parentDir ); + } + + if ( $this->fileOwner !== null && $this->currentUser !== $this->fileOwner ) { + $ok = false; + trigger_error( __METHOD__ . ": PHP process owner is not '{$this->fileOwner}'." ); + } + + return $ok; + } + + protected function doCreateInternal( array $params ) { + $status = $this->newStatus(); + + $dest = $this->resolveToFSPath( $params['dst'] ); + if ( $dest === null ) { + $status->fatal( 'backend-fail-invalidpath', $params['dst'] ); + + return $status; + } + + if ( !empty( $params['async'] ) ) { // deferred + $tempFile = TempFSFile::factory( 'create_', 'tmp', $this->tmpDirectory ); + if ( !$tempFile ) { + $status->fatal( 'backend-fail-create', $params['dst'] ); + + return $status; + } + $this->trapWarnings(); + $bytes = file_put_contents( $tempFile->getPath(), $params['content'] ); + $this->untrapWarnings(); + if ( $bytes === false ) { + $status->fatal( 'backend-fail-create', $params['dst'] ); + + return $status; + } + $cmd = implode( ' ', [ + $this->isWindows ? 'COPY /B /Y' : 'cp', // (binary, overwrite) + escapeshellarg( $this->cleanPathSlashes( $tempFile->getPath() ) ), + escapeshellarg( $this->cleanPathSlashes( $dest ) ) + ] ); + $handler = function ( $errors, StatusValue $status, array $params, $cmd ) { + if ( $errors !== '' && !( $this->isWindows && $errors[0] === " " ) ) { + $status->fatal( 'backend-fail-create', $params['dst'] ); + trigger_error( "$cmd\n$errors", E_USER_WARNING ); // command output + } + }; + $status->value = new FSFileOpHandle( $this, $params, $handler, $cmd, $dest ); + $tempFile->bind( $status->value ); + } else { // immediate write + $this->trapWarnings(); + $bytes = file_put_contents( $dest, $params['content'] ); + $this->untrapWarnings(); + if ( $bytes === false ) { + $status->fatal( 'backend-fail-create', $params['dst'] ); + + return $status; + } + $this->chmod( $dest ); + } + + return $status; + } + + protected function doStoreInternal( array $params ) { + $status = $this->newStatus(); + + $dest = $this->resolveToFSPath( $params['dst'] ); + if ( $dest === null ) { + $status->fatal( 'backend-fail-invalidpath', $params['dst'] ); + + return $status; + } + + if ( !empty( $params['async'] ) ) { // deferred + $cmd = implode( ' ', [ + $this->isWindows ? 'COPY /B /Y' : 'cp', // (binary, overwrite) + escapeshellarg( $this->cleanPathSlashes( $params['src'] ) ), + escapeshellarg( $this->cleanPathSlashes( $dest ) ) + ] ); + $handler = function ( $errors, StatusValue $status, array $params, $cmd ) { + if ( $errors !== '' && !( $this->isWindows && $errors[0] === " " ) ) { + $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] ); + trigger_error( "$cmd\n$errors", E_USER_WARNING ); // command output + } + }; + $status->value = new FSFileOpHandle( $this, $params, $handler, $cmd, $dest ); + } else { // immediate write + $this->trapWarnings(); + $ok = copy( $params['src'], $dest ); + $this->untrapWarnings(); + // In some cases (at least over NFS), copy() returns true when it fails + if ( !$ok || ( filesize( $params['src'] ) !== filesize( $dest ) ) ) { + if ( $ok ) { // PHP bug + unlink( $dest ); // remove broken file + trigger_error( __METHOD__ . ": copy() failed but returned true." ); + } + $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] ); + + return $status; + } + $this->chmod( $dest ); + } + + return $status; + } + + protected function doCopyInternal( array $params ) { + $status = $this->newStatus(); + + $source = $this->resolveToFSPath( $params['src'] ); + if ( $source === null ) { + $status->fatal( 'backend-fail-invalidpath', $params['src'] ); + + return $status; + } + + $dest = $this->resolveToFSPath( $params['dst'] ); + if ( $dest === null ) { + $status->fatal( 'backend-fail-invalidpath', $params['dst'] ); + + return $status; + } + + if ( !is_file( $source ) ) { + if ( empty( $params['ignoreMissingSource'] ) ) { + $status->fatal( 'backend-fail-copy', $params['src'] ); + } + + return $status; // do nothing; either OK or bad status + } + + if ( !empty( $params['async'] ) ) { // deferred + $cmd = implode( ' ', [ + $this->isWindows ? 'COPY /B /Y' : 'cp', // (binary, overwrite) + escapeshellarg( $this->cleanPathSlashes( $source ) ), + escapeshellarg( $this->cleanPathSlashes( $dest ) ) + ] ); + $handler = function ( $errors, StatusValue $status, array $params, $cmd ) { + if ( $errors !== '' && !( $this->isWindows && $errors[0] === " " ) ) { + $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] ); + trigger_error( "$cmd\n$errors", E_USER_WARNING ); // command output + } + }; + $status->value = new FSFileOpHandle( $this, $params, $handler, $cmd, $dest ); + } else { // immediate write + $this->trapWarnings(); + $ok = ( $source === $dest ) ? true : copy( $source, $dest ); + $this->untrapWarnings(); + // In some cases (at least over NFS), copy() returns true when it fails + if ( !$ok || ( filesize( $source ) !== filesize( $dest ) ) ) { + if ( $ok ) { // PHP bug + $this->trapWarnings(); + unlink( $dest ); // remove broken file + $this->untrapWarnings(); + trigger_error( __METHOD__ . ": copy() failed but returned true." ); + } + $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] ); + + return $status; + } + $this->chmod( $dest ); + } + + return $status; + } + + protected function doMoveInternal( array $params ) { + $status = $this->newStatus(); + + $source = $this->resolveToFSPath( $params['src'] ); + if ( $source === null ) { + $status->fatal( 'backend-fail-invalidpath', $params['src'] ); + + return $status; + } + + $dest = $this->resolveToFSPath( $params['dst'] ); + if ( $dest === null ) { + $status->fatal( 'backend-fail-invalidpath', $params['dst'] ); + + return $status; + } + + if ( !is_file( $source ) ) { + if ( empty( $params['ignoreMissingSource'] ) ) { + $status->fatal( 'backend-fail-move', $params['src'] ); + } + + return $status; // do nothing; either OK or bad status + } + + if ( !empty( $params['async'] ) ) { // deferred + $cmd = implode( ' ', [ + $this->isWindows ? 'MOVE /Y' : 'mv', // (overwrite) + escapeshellarg( $this->cleanPathSlashes( $source ) ), + escapeshellarg( $this->cleanPathSlashes( $dest ) ) + ] ); + $handler = function ( $errors, StatusValue $status, array $params, $cmd ) { + if ( $errors !== '' && !( $this->isWindows && $errors[0] === " " ) ) { + $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] ); + trigger_error( "$cmd\n$errors", E_USER_WARNING ); // command output + } + }; + $status->value = new FSFileOpHandle( $this, $params, $handler, $cmd ); + } else { // immediate write + $this->trapWarnings(); + $ok = ( $source === $dest ) ? true : rename( $source, $dest ); + $this->untrapWarnings(); + clearstatcache(); // file no longer at source + if ( !$ok ) { + $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] ); + + return $status; + } + } + + return $status; + } + + protected function doDeleteInternal( array $params ) { + $status = $this->newStatus(); + + $source = $this->resolveToFSPath( $params['src'] ); + if ( $source === null ) { + $status->fatal( 'backend-fail-invalidpath', $params['src'] ); + + return $status; + } + + if ( !is_file( $source ) ) { + if ( empty( $params['ignoreMissingSource'] ) ) { + $status->fatal( 'backend-fail-delete', $params['src'] ); + } + + return $status; // do nothing; either OK or bad status + } + + if ( !empty( $params['async'] ) ) { // deferred + $cmd = implode( ' ', [ + $this->isWindows ? 'DEL' : 'unlink', + escapeshellarg( $this->cleanPathSlashes( $source ) ) + ] ); + $handler = function ( $errors, StatusValue $status, array $params, $cmd ) { + if ( $errors !== '' && !( $this->isWindows && $errors[0] === " " ) ) { + $status->fatal( 'backend-fail-delete', $params['src'] ); + trigger_error( "$cmd\n$errors", E_USER_WARNING ); // command output + } + }; + $status->value = new FSFileOpHandle( $this, $params, $handler, $cmd ); + } else { // immediate write + $this->trapWarnings(); + $ok = unlink( $source ); + $this->untrapWarnings(); + if ( !$ok ) { + $status->fatal( 'backend-fail-delete', $params['src'] ); + + return $status; + } + } + + return $status; + } + + /** + * @param string $fullCont + * @param string $dirRel + * @param array $params + * @return StatusValue + */ + protected function doPrepareInternal( $fullCont, $dirRel, array $params ) { + $status = $this->newStatus(); + list( , $shortCont, ) = FileBackend::splitStoragePath( $params['dir'] ); + $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid + $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot; + $existed = is_dir( $dir ); // already there? + // Create the directory and its parents as needed... + $this->trapWarnings(); + if ( !$existed && !mkdir( $dir, $this->dirMode, true ) && !is_dir( $dir ) ) { + $this->logger->error( __METHOD__ . ": cannot create directory $dir" ); + $status->fatal( 'directorycreateerror', $params['dir'] ); // fails on races + } elseif ( !is_writable( $dir ) ) { + $this->logger->error( __METHOD__ . ": directory $dir is read-only" ); + $status->fatal( 'directoryreadonlyerror', $params['dir'] ); + } elseif ( !is_readable( $dir ) ) { + $this->logger->error( __METHOD__ . ": directory $dir is not readable" ); + $status->fatal( 'directorynotreadableerror', $params['dir'] ); + } + $this->untrapWarnings(); + // Respect any 'noAccess' or 'noListing' flags... + if ( is_dir( $dir ) && !$existed ) { + $status->merge( $this->doSecureInternal( $fullCont, $dirRel, $params ) ); + } + + return $status; + } + + protected function doSecureInternal( $fullCont, $dirRel, array $params ) { + $status = $this->newStatus(); + list( , $shortCont, ) = FileBackend::splitStoragePath( $params['dir'] ); + $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid + $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot; + // Seed new directories with a blank index.html, to prevent crawling... + if ( !empty( $params['noListing'] ) && !file_exists( "{$dir}/index.html" ) ) { + $this->trapWarnings(); + $bytes = file_put_contents( "{$dir}/index.html", $this->indexHtmlPrivate() ); + $this->untrapWarnings(); + if ( $bytes === false ) { + $status->fatal( 'backend-fail-create', $params['dir'] . '/index.html' ); + } + } + // Add a .htaccess file to the root of the container... + if ( !empty( $params['noAccess'] ) && !file_exists( "{$contRoot}/.htaccess" ) ) { + $this->trapWarnings(); + $bytes = file_put_contents( "{$contRoot}/.htaccess", $this->htaccessPrivate() ); + $this->untrapWarnings(); + if ( $bytes === false ) { + $storeDir = "mwstore://{$this->name}/{$shortCont}"; + $status->fatal( 'backend-fail-create', "{$storeDir}/.htaccess" ); + } + } + + return $status; + } + + protected function doPublishInternal( $fullCont, $dirRel, array $params ) { + $status = $this->newStatus(); + list( , $shortCont, ) = FileBackend::splitStoragePath( $params['dir'] ); + $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid + $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot; + // Unseed new directories with a blank index.html, to allow crawling... + if ( !empty( $params['listing'] ) && is_file( "{$dir}/index.html" ) ) { + $exists = ( file_get_contents( "{$dir}/index.html" ) === $this->indexHtmlPrivate() ); + $this->trapWarnings(); + if ( $exists && !unlink( "{$dir}/index.html" ) ) { // reverse secure() + $status->fatal( 'backend-fail-delete', $params['dir'] . '/index.html' ); + } + $this->untrapWarnings(); + } + // Remove the .htaccess file from the root of the container... + if ( !empty( $params['access'] ) && is_file( "{$contRoot}/.htaccess" ) ) { + $exists = ( file_get_contents( "{$contRoot}/.htaccess" ) === $this->htaccessPrivate() ); + $this->trapWarnings(); + if ( $exists && !unlink( "{$contRoot}/.htaccess" ) ) { // reverse secure() + $storeDir = "mwstore://{$this->name}/{$shortCont}"; + $status->fatal( 'backend-fail-delete', "{$storeDir}/.htaccess" ); + } + $this->untrapWarnings(); + } + + return $status; + } + + protected function doCleanInternal( $fullCont, $dirRel, array $params ) { + $status = $this->newStatus(); + list( , $shortCont, ) = FileBackend::splitStoragePath( $params['dir'] ); + $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid + $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot; + $this->trapWarnings(); + if ( is_dir( $dir ) ) { + rmdir( $dir ); // remove directory if empty + } + $this->untrapWarnings(); + + return $status; + } + + protected function doGetFileStat( array $params ) { + $source = $this->resolveToFSPath( $params['src'] ); + if ( $source === null ) { + return false; // invalid storage path + } + + $this->trapWarnings(); // don't trust 'false' if there were errors + $stat = is_file( $source ) ? stat( $source ) : false; // regular files only + $hadError = $this->untrapWarnings(); + + if ( $stat ) { + $ct = new ConvertibleTimestamp( $stat['mtime'] ); + + return [ + 'mtime' => $ct->getTimestamp( TS_MW ), + 'size' => $stat['size'] + ]; + } elseif ( !$hadError ) { + return false; // file does not exist + } else { + return null; // failure + } + } + + protected function doClearCache( array $paths = null ) { + clearstatcache(); // clear the PHP file stat cache + } + + protected function doDirectoryExists( $fullCont, $dirRel, array $params ) { + list( , $shortCont, ) = FileBackend::splitStoragePath( $params['dir'] ); + $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid + $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot; + + $this->trapWarnings(); // don't trust 'false' if there were errors + $exists = is_dir( $dir ); + $hadError = $this->untrapWarnings(); + + return $hadError ? null : $exists; + } + + /** + * @see FileBackendStore::getDirectoryListInternal() + * @param string $fullCont + * @param string $dirRel + * @param array $params + * @return array|null + */ + public function getDirectoryListInternal( $fullCont, $dirRel, array $params ) { + list( , $shortCont, ) = FileBackend::splitStoragePath( $params['dir'] ); + $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid + $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot; + $exists = is_dir( $dir ); + if ( !$exists ) { + $this->logger->warning( __METHOD__ . "() given directory does not exist: '$dir'\n" ); + + return []; // nothing under this dir + } elseif ( !is_readable( $dir ) ) { + $this->logger->warning( __METHOD__ . "() given directory is unreadable: '$dir'\n" ); + + return null; // bad permissions? + } + + return new FSFileBackendDirList( $dir, $params ); + } + + /** + * @see FileBackendStore::getFileListInternal() + * @param string $fullCont + * @param string $dirRel + * @param array $params + * @return array|FSFileBackendFileList|null + */ + public function getFileListInternal( $fullCont, $dirRel, array $params ) { + list( , $shortCont, ) = FileBackend::splitStoragePath( $params['dir'] ); + $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid + $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot; + $exists = is_dir( $dir ); + if ( !$exists ) { + $this->logger->warning( __METHOD__ . "() given directory does not exist: '$dir'\n" ); + + return []; // nothing under this dir + } elseif ( !is_readable( $dir ) ) { + $this->logger->warning( __METHOD__ . "() given directory is unreadable: '$dir'\n" ); + + return null; // bad permissions? + } + + return new FSFileBackendFileList( $dir, $params ); + } + + protected function doGetLocalReferenceMulti( array $params ) { + $fsFiles = []; // (path => FSFile) + + foreach ( $params['srcs'] as $src ) { + $source = $this->resolveToFSPath( $src ); + if ( $source === null || !is_file( $source ) ) { + $fsFiles[$src] = null; // invalid path or file does not exist + } else { + $fsFiles[$src] = new FSFile( $source ); + } + } + + return $fsFiles; + } + + protected function doGetLocalCopyMulti( array $params ) { + $tmpFiles = []; // (path => TempFSFile) + + foreach ( $params['srcs'] as $src ) { + $source = $this->resolveToFSPath( $src ); + if ( $source === null ) { + $tmpFiles[$src] = null; // invalid path + } else { + // Create a new temporary file with the same extension... + $ext = FileBackend::extensionFromPath( $src ); + $tmpFile = TempFSFile::factory( 'localcopy_', $ext, $this->tmpDirectory ); + if ( !$tmpFile ) { + $tmpFiles[$src] = null; + } else { + $tmpPath = $tmpFile->getPath(); + // Copy the source file over the temp file + $this->trapWarnings(); + $ok = copy( $source, $tmpPath ); + $this->untrapWarnings(); + if ( !$ok ) { + $tmpFiles[$src] = null; + } else { + $this->chmod( $tmpPath ); + $tmpFiles[$src] = $tmpFile; + } + } + } + } + + return $tmpFiles; + } + + protected function directoriesAreVirtual() { + return false; + } + + /** + * @param FSFileOpHandle[] $fileOpHandles + * + * @return StatusValue[] + */ + protected function doExecuteOpHandlesInternal( array $fileOpHandles ) { + $statuses = []; + + $pipes = []; + foreach ( $fileOpHandles as $index => $fileOpHandle ) { + $pipes[$index] = popen( "{$fileOpHandle->cmd} 2>&1", 'r' ); + } + + $errs = []; + foreach ( $pipes as $index => $pipe ) { + // Result will be empty on success in *NIX. On Windows, + // it may be something like " 1 file(s) [copied|moved].". + $errs[$index] = stream_get_contents( $pipe ); + fclose( $pipe ); + } + + foreach ( $fileOpHandles as $index => $fileOpHandle ) { + $status = $this->newStatus(); + $function = $fileOpHandle->call; + $function( $errs[$index], $status, $fileOpHandle->params, $fileOpHandle->cmd ); + $statuses[$index] = $status; + if ( $status->isOK() && $fileOpHandle->chmodPath ) { + $this->chmod( $fileOpHandle->chmodPath ); + } + } + + clearstatcache(); // files changed + return $statuses; + } + + /** + * Chmod a file, suppressing the warnings + * + * @param string $path Absolute file system path + * @return bool Success + */ + protected function chmod( $path ) { + $this->trapWarnings(); + $ok = chmod( $path, $this->fileMode ); + $this->untrapWarnings(); + + return $ok; + } + + /** + * Return the text of an index.html file to hide directory listings + * + * @return string + */ + protected function indexHtmlPrivate() { + return ''; + } + + /** + * Return the text of a .htaccess file to make a directory private + * + * @return string + */ + protected function htaccessPrivate() { + return "Deny from all\n"; + } + + /** + * Clean up directory separators for the given OS + * + * @param string $path FS path + * @return string + */ + protected function cleanPathSlashes( $path ) { + return $this->isWindows ? strtr( $path, '/', '\\' ) : $path; + } + + /** + * Listen for E_WARNING errors and track whether any happen + */ + protected function trapWarnings() { + $this->hadWarningErrors[] = false; // push to stack + set_error_handler( [ $this, 'handleWarning' ], E_WARNING ); + } + + /** + * Stop listening for E_WARNING errors and return true if any happened + * + * @return bool + */ + protected function untrapWarnings() { + restore_error_handler(); // restore previous handler + return array_pop( $this->hadWarningErrors ); // pop from stack + } + + /** + * @param int $errno + * @param string $errstr + * @return bool + * @access private + */ + public function handleWarning( $errno, $errstr ) { + $this->logger->error( $errstr ); // more detailed error logging + $this->hadWarningErrors[count( $this->hadWarningErrors ) - 1] = true; + + return true; // suppress from PHP handler + } +} + +/** + * @see FileBackendStoreOpHandle + */ +class FSFileOpHandle extends FileBackendStoreOpHandle { + public $cmd; // string; shell command + public $chmodPath; // string; file to chmod + + /** + * @param FSFileBackend $backend + * @param array $params + * @param callable $call + * @param string $cmd + * @param int|null $chmodPath + */ + public function __construct( + FSFileBackend $backend, array $params, $call, $cmd, $chmodPath = null + ) { + $this->backend = $backend; + $this->params = $params; + $this->call = $call; + $this->cmd = $cmd; + $this->chmodPath = $chmodPath; + } +} + +/** + * Wrapper around RecursiveDirectoryIterator/DirectoryIterator that + * catches exception or does any custom behavoir that we may want. + * Do not use this class from places outside FSFileBackend. + * + * @ingroup FileBackend + */ +abstract class FSFileBackendList implements Iterator { + /** @var Iterator */ + protected $iter; + + /** @var int */ + protected $suffixStart; + + /** @var int */ + protected $pos = 0; + + /** @var array */ + protected $params = []; + + /** + * @param string $dir File system directory + * @param array $params + */ + public function __construct( $dir, array $params ) { + $path = realpath( $dir ); // normalize + if ( $path === false ) { + $path = $dir; + } + $this->suffixStart = strlen( $path ) + 1; // size of "path/to/dir/" + $this->params = $params; + + try { + $this->iter = $this->initIterator( $path ); + } catch ( UnexpectedValueException $e ) { + $this->iter = null; // bad permissions? deleted? + } + } + + /** + * Return an appropriate iterator object to wrap + * + * @param string $dir File system directory + * @return Iterator + */ + protected function initIterator( $dir ) { + if ( !empty( $this->params['topOnly'] ) ) { // non-recursive + # Get an iterator that will get direct sub-nodes + return new DirectoryIterator( $dir ); + } else { // recursive + # Get an iterator that will return leaf nodes (non-directories) + # RecursiveDirectoryIterator extends FilesystemIterator. + # FilesystemIterator::SKIP_DOTS default is inconsistent in PHP 5.3.x. + $flags = FilesystemIterator::CURRENT_AS_SELF | FilesystemIterator::SKIP_DOTS; + + return new RecursiveIteratorIterator( + new RecursiveDirectoryIterator( $dir, $flags ), + RecursiveIteratorIterator::CHILD_FIRST // include dirs + ); + } + } + + /** + * @see Iterator::key() + * @return int + */ + public function key() { + return $this->pos; + } + + /** + * @see Iterator::current() + * @return string|bool String or false + */ + public function current() { + return $this->getRelPath( $this->iter->current()->getPathname() ); + } + + /** + * @see Iterator::next() + * @throws FileBackendError + */ + public function next() { + try { + $this->iter->next(); + $this->filterViaNext(); + } catch ( UnexpectedValueException $e ) { // bad permissions? deleted? + throw new FileBackendError( "File iterator gave UnexpectedValueException." ); + } + ++$this->pos; + } + + /** + * @see Iterator::rewind() + * @throws FileBackendError + */ + public function rewind() { + $this->pos = 0; + try { + $this->iter->rewind(); + $this->filterViaNext(); + } catch ( UnexpectedValueException $e ) { // bad permissions? deleted? + throw new FileBackendError( "File iterator gave UnexpectedValueException." ); + } + } + + /** + * @see Iterator::valid() + * @return bool + */ + public function valid() { + return $this->iter && $this->iter->valid(); + } + + /** + * Filter out items by advancing to the next ones + */ + protected function filterViaNext() { + } + + /** + * Return only the relative path and normalize slashes to FileBackend-style. + * Uses the "real path" since the suffix is based upon that. + * + * @param string $dir + * @return string + */ + protected function getRelPath( $dir ) { + $path = realpath( $dir ); + if ( $path === false ) { + $path = $dir; + } + + return strtr( substr( $path, $this->suffixStart ), '\\', '/' ); + } +} + +class FSFileBackendDirList extends FSFileBackendList { + protected function filterViaNext() { + while ( $this->iter->valid() ) { + if ( $this->iter->current()->isDot() || !$this->iter->current()->isDir() ) { + $this->iter->next(); // skip non-directories and dot files + } else { + break; + } + } + } +} + +class FSFileBackendFileList extends FSFileBackendList { + protected function filterViaNext() { + while ( $this->iter->valid() ) { + if ( !$this->iter->current()->isFile() ) { + $this->iter->next(); // skip non-files and dot files + } else { + break; + } + } + } +} diff --git a/includes/libs/filebackend/MemoryFileBackend.php b/includes/libs/filebackend/MemoryFileBackend.php new file mode 100644 index 0000000000..44fe2cbac6 --- /dev/null +++ b/includes/libs/filebackend/MemoryFileBackend.php @@ -0,0 +1,263 @@ + (data,mtime) */ + protected $files = []; + + public function getFeatures() { + return self::ATTR_UNICODE_PATHS; + } + + public function isPathUsableInternal( $storagePath ) { + return true; + } + + protected function doCreateInternal( array $params ) { + $status = $this->newStatus(); + + $dst = $this->resolveHashKey( $params['dst'] ); + if ( $dst === null ) { + $status->fatal( 'backend-fail-invalidpath', $params['dst'] ); + + return $status; + } + + $this->files[$dst] = [ + 'data' => $params['content'], + 'mtime' => wfTimestamp( TS_MW, time() ) + ]; + + return $status; + } + + protected function doStoreInternal( array $params ) { + $status = $this->newStatus(); + + $dst = $this->resolveHashKey( $params['dst'] ); + if ( $dst === null ) { + $status->fatal( 'backend-fail-invalidpath', $params['dst'] ); + + return $status; + } + + MediaWiki\suppressWarnings(); + $data = file_get_contents( $params['src'] ); + MediaWiki\restoreWarnings(); + if ( $data === false ) { // source doesn't exist? + $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] ); + + return $status; + } + + $this->files[$dst] = [ + 'data' => $data, + 'mtime' => wfTimestamp( TS_MW, time() ) + ]; + + return $status; + } + + protected function doCopyInternal( array $params ) { + $status = $this->newStatus(); + + $src = $this->resolveHashKey( $params['src'] ); + if ( $src === null ) { + $status->fatal( 'backend-fail-invalidpath', $params['src'] ); + + return $status; + } + + $dst = $this->resolveHashKey( $params['dst'] ); + if ( $dst === null ) { + $status->fatal( 'backend-fail-invalidpath', $params['dst'] ); + + return $status; + } + + if ( !isset( $this->files[$src] ) ) { + if ( empty( $params['ignoreMissingSource'] ) ) { + $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] ); + } + + return $status; + } + + $this->files[$dst] = [ + 'data' => $this->files[$src]['data'], + 'mtime' => wfTimestamp( TS_MW, time() ) + ]; + + return $status; + } + + protected function doDeleteInternal( array $params ) { + $status = $this->newStatus(); + + $src = $this->resolveHashKey( $params['src'] ); + if ( $src === null ) { + $status->fatal( 'backend-fail-invalidpath', $params['src'] ); + + return $status; + } + + if ( !isset( $this->files[$src] ) ) { + if ( empty( $params['ignoreMissingSource'] ) ) { + $status->fatal( 'backend-fail-delete', $params['src'] ); + } + + return $status; + } + + unset( $this->files[$src] ); + + return $status; + } + + protected function doGetFileStat( array $params ) { + $src = $this->resolveHashKey( $params['src'] ); + if ( $src === null ) { + return null; + } + + if ( isset( $this->files[$src] ) ) { + return [ + 'mtime' => $this->files[$src]['mtime'], + 'size' => strlen( $this->files[$src]['data'] ), + ]; + } + + return false; + } + + protected function doGetLocalCopyMulti( array $params ) { + $tmpFiles = []; // (path => TempFSFile) + foreach ( $params['srcs'] as $srcPath ) { + $src = $this->resolveHashKey( $srcPath ); + if ( $src === null || !isset( $this->files[$src] ) ) { + $fsFile = null; + } else { + // Create a new temporary file with the same extension... + $ext = FileBackend::extensionFromPath( $src ); + $fsFile = TempFSFile::factory( 'localcopy_', $ext, $this->tmpDirectory ); + if ( $fsFile ) { + $bytes = file_put_contents( $fsFile->getPath(), $this->files[$src]['data'] ); + if ( $bytes !== strlen( $this->files[$src]['data'] ) ) { + $fsFile = null; + } + } + } + $tmpFiles[$srcPath] = $fsFile; + } + + return $tmpFiles; + } + + protected function doDirectoryExists( $container, $dir, array $params ) { + $prefix = rtrim( "$container/$dir", '/' ) . '/'; + foreach ( $this->files as $path => $data ) { + if ( strpos( $path, $prefix ) === 0 ) { + return true; + } + } + + return false; + } + + public function getDirectoryListInternal( $container, $dir, array $params ) { + $dirs = []; + $prefix = rtrim( "$container/$dir", '/' ) . '/'; + $prefixLen = strlen( $prefix ); + foreach ( $this->files as $path => $data ) { + if ( strpos( $path, $prefix ) === 0 ) { + $relPath = substr( $path, $prefixLen ); + if ( $relPath === false ) { + continue; + } elseif ( strpos( $relPath, '/' ) === false ) { + continue; // just a file + } + $parts = array_slice( explode( '/', $relPath ), 0, -1 ); // last part is file name + if ( !empty( $params['topOnly'] ) ) { + $dirs[$parts[0]] = 1; // top directory + } else { + $current = ''; + foreach ( $parts as $part ) { // all directories + $dir = ( $current === '' ) ? $part : "$current/$part"; + $dirs[$dir] = 1; + $current = $dir; + } + } + } + } + + return array_keys( $dirs ); + } + + public function getFileListInternal( $container, $dir, array $params ) { + $files = []; + $prefix = rtrim( "$container/$dir", '/' ) . '/'; + $prefixLen = strlen( $prefix ); + foreach ( $this->files as $path => $data ) { + if ( strpos( $path, $prefix ) === 0 ) { + $relPath = substr( $path, $prefixLen ); + if ( $relPath === false ) { + continue; + } elseif ( !empty( $params['topOnly'] ) && strpos( $relPath, '/' ) !== false ) { + continue; + } + $files[] = $relPath; + } + } + + return $files; + } + + protected function directoriesAreVirtual() { + return true; + } + + /** + * Get the absolute file system path for a storage path + * + * @param string $storagePath Storage path + * @return string|null + */ + protected function resolveHashKey( $storagePath ) { + list( $fullCont, $relPath ) = $this->resolveStoragePathReal( $storagePath ); + if ( $relPath === null ) { + return null; // invalid + } + + return ( $relPath !== '' ) ? "$fullCont/$relPath" : $fullCont; + } +}