From 8429a48dac648dd77cbb5883995c0175e63dd02d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=96=D0=B0=D0=BA=D0=BE=D0=B2=20=D0=92=D0=B8=D1=82=D0=B0?= =?UTF-8?q?=D0=BB=D0=B8=D0=B9?= Date: Wed, 15 Jan 2020 17:09:44 +0500 Subject: [PATCH 1/7] Change only modified files --- src/Composer/Autoload/AutoloadGenerator.php | 72 ++++++++++++++++----- src/Composer/Installer.php | 7 -- src/Composer/Json/JsonFile.php | 15 ++++- 3 files changed, 70 insertions(+), 24 deletions(-) diff --git a/src/Composer/Autoload/AutoloadGenerator.php b/src/Composer/Autoload/AutoloadGenerator.php index 5e068ccff..bb0c94c49 100644 --- a/src/Composer/Autoload/AutoloadGenerator.php +++ b/src/Composer/Autoload/AutoloadGenerator.php @@ -285,24 +285,24 @@ EOF; } } - file_put_contents($targetDir.'/autoload_namespaces.php', $namespacesFile); - file_put_contents($targetDir.'/autoload_psr4.php', $psr4File); - file_put_contents($targetDir.'/autoload_classmap.php', $classmapFile); + $this->filePutContentsIfModified($targetDir.'/autoload_namespaces.php', $namespacesFile); + $this->filePutContentsIfModified($targetDir.'/autoload_psr4.php', $psr4File); + $this->filePutContentsIfModified($targetDir.'/autoload_classmap.php', $classmapFile); $includePathFilePath = $targetDir.'/include_paths.php'; if ($includePathFileContents = $this->getIncludePathsFile($packageMap, $filesystem, $basePath, $vendorPath, $vendorPathCode52, $appBaseDirCode)) { - file_put_contents($includePathFilePath, $includePathFileContents); + $this->filePutContentsIfModified($includePathFilePath, $includePathFileContents); } elseif (file_exists($includePathFilePath)) { unlink($includePathFilePath); } $includeFilesFilePath = $targetDir.'/autoload_files.php'; if ($includeFilesFileContents = $this->getIncludeFilesFile($autoloads['files'], $filesystem, $basePath, $vendorPath, $vendorPathCode52, $appBaseDirCode)) { - file_put_contents($includeFilesFilePath, $includeFilesFileContents); + $this->filePutContentsIfModified($includeFilesFilePath, $includeFilesFileContents); } elseif (file_exists($includeFilesFilePath)) { unlink($includeFilesFilePath); } - file_put_contents($targetDir.'/autoload_static.php', $this->getStaticFile($suffix, $targetDir, $vendorPath, $basePath, $staticPhpVersion)); - file_put_contents($vendorPath.'/autoload.php', $this->getAutoloadFile($vendorPathToTargetDirCode, $suffix)); - file_put_contents($targetDir.'/autoload_real.php', $this->getAutoloadRealFile(true, (bool) $includePathFileContents, $targetDirLoader, (bool) $includeFilesFileContents, $vendorPathCode, $appBaseDirCode, $suffix, $useGlobalIncludePath, $prependAutoloader, $staticPhpVersion)); + $this->filePutContentsIfModified($targetDir.'/autoload_static.php', $this->getStaticFile($suffix, $targetDir, $vendorPath, $basePath, $staticPhpVersion)); + $this->filePutContentsIfModified($vendorPath.'/autoload.php', $this->getAutoloadFile($vendorPathToTargetDirCode, $suffix)); + $this->filePutContentsIfModified($targetDir.'/autoload_real.php', $this->getAutoloadRealFile(true, (bool) $includePathFileContents, $targetDirLoader, (bool) $includeFilesFileContents, $vendorPathCode, $appBaseDirCode, $suffix, $useGlobalIncludePath, $prependAutoloader, $staticPhpVersion)); $this->safeCopy(__DIR__.'/ClassLoader.php', $targetDir.'/ClassLoader.php'); $this->safeCopy(__DIR__.'/../../../LICENSE', $targetDir.'/LICENSE'); @@ -316,6 +316,16 @@ EOF; return count($classMap); } + private function filePutContentsIfModified($path, $content) + { + $currentContent = file_get_contents($path); + if (!$currentContent || ($currentContent != $content)) { + return file_put_contents($path, $content); + } + + return 0; + } + private function addClassMapCode($filesystem, $basePath, $vendorPath, $dir, $blacklist = null, $namespaceFilter = null, $autoloadType = null, array $classMap = array()) { foreach ($this->generateClassMap($dir, $blacklist, $namespaceFilter, $autoloadType) as $class => $path) { @@ -383,8 +393,8 @@ EOF; /** * Compiles an ordered list of namespace => path mappings * - * @param array $packageMap array of array(package, installDir-relative-to-composer.json) - * @param PackageInterface $mainPackage root package instance + * @param array $packageMap array of array(package, installDir-relative-to-composer.json) + * @param PackageInterface $mainPackage root package instance * @param bool $filterOutRequireDevPackages whether to filter out require-dev packages * @return array array('psr-0' => array('Ns\\Foo' => array('installDir'))) */ @@ -986,7 +996,6 @@ INITIALIZER; $sortedPackages = PackageSorter::sortPackages($packages); - $sortedPackageMap = array(); foreach ($sortedPackages as $package) { @@ -1005,11 +1014,42 @@ INITIALIZER; */ protected function safeCopy($source, $target) { - $source = fopen($source, 'r'); - $target = fopen($target, 'w+'); + if (!$this->filesAreEqual($source, $target)) { + $source = fopen($source, 'r'); + $target = fopen($target, 'w+'); + + stream_copy_to_stream($source, $target); + fclose($source); + fclose($target); + } + } + + /** + * compare 2 files + * https://stackoverflow.com/questions/3060125/can-i-use-file-get-contents-to-compare-two-files + */ + private function filesAreEqual($a, $b) + { + // Check if filesize is different + if (filesize($a) !== filesize($b)) { + return false; + } + + // Check if content is different + $ah = fopen($a, 'rb'); + $bh = fopen($b, 'rb'); + + $result = true; + while (!feof($ah)) { + if (fread($ah, 8192) != fread($bh, 8192)) { + $result = false; + break; + } + } + + fclose($ah); + fclose($bh); - stream_copy_to_stream($source, $target); - fclose($source); - fclose($target); + return $result; } } diff --git a/src/Composer/Installer.php b/src/Composer/Installer.php index c5ec5cc5d..bf0523356 100644 --- a/src/Composer/Installer.php +++ b/src/Composer/Installer.php @@ -311,13 +311,6 @@ class Installer foreach ($localRepo->getPackages() as $package) { $this->installationManager->ensureBinariesPresence($package); } - - $vendorDir = $this->config->get('vendor-dir'); - if (is_dir($vendorDir)) { - // suppress errors as this fails sometimes on OSX for no apparent reason - // see https://github.com/composer/composer/issues/4070#issuecomment-129792748 - @touch($vendorDir); - } } if ($this->runScripts) { diff --git a/src/Composer/Json/JsonFile.php b/src/Composer/Json/JsonFile.php index f2d950004..3b9b74082 100644 --- a/src/Composer/Json/JsonFile.php +++ b/src/Composer/Json/JsonFile.php @@ -129,7 +129,7 @@ class JsonFile $retries = 3; while ($retries--) { try { - file_put_contents($this->path, static::encode($hash, $options). ($options & self::JSON_PRETTY_PRINT ? "\n" : '')); + $this->filePutContentsIfModified($this->path, static::encode($hash, $options). ($options & self::JSON_PRETTY_PRINT ? "\n" : '')); break; } catch (\Exception $e) { if ($retries) { @@ -142,6 +142,19 @@ class JsonFile } } + /** + * modify file properties only if content modified + */ + private function filePutContentsIfModified($path, $content) + { + $currentContent = file_get_contents($path); + if (!$currentContent || ($currentContent != $content)) { + return file_put_contents($path, $content); + } + + return 0; + } + /** * Validates the schema of the current json file according to composer-schema.json rules * From a4dc076dc8fd997c054325ed21bab30e30f68fbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=96=D0=B0=D0=BA=D0=BE=D0=B2=20=D0=92=D0=B8=D1=82=D0=B0?= =?UTF-8?q?=D0=BB=D0=B8=D0=B9?= Date: Wed, 15 Jan 2020 18:09:42 +0500 Subject: [PATCH 2/7] fix file load errors --- src/Composer/Autoload/AutoloadGenerator.php | 4 ++-- src/Composer/Json/JsonFile.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Composer/Autoload/AutoloadGenerator.php b/src/Composer/Autoload/AutoloadGenerator.php index bb0c94c49..6ca4b3329 100644 --- a/src/Composer/Autoload/AutoloadGenerator.php +++ b/src/Composer/Autoload/AutoloadGenerator.php @@ -318,7 +318,7 @@ EOF; private function filePutContentsIfModified($path, $content) { - $currentContent = file_get_contents($path); + $currentContent = @file_get_contents($path); if (!$currentContent || ($currentContent != $content)) { return file_put_contents($path, $content); } @@ -1014,7 +1014,7 @@ INITIALIZER; */ protected function safeCopy($source, $target) { - if (!$this->filesAreEqual($source, $target)) { + if (!file_exists($target) OR !file_exists($source) OR !$this->filesAreEqual($source, $target)) { $source = fopen($source, 'r'); $target = fopen($target, 'w+'); diff --git a/src/Composer/Json/JsonFile.php b/src/Composer/Json/JsonFile.php index 3b9b74082..89524df39 100644 --- a/src/Composer/Json/JsonFile.php +++ b/src/Composer/Json/JsonFile.php @@ -147,7 +147,7 @@ class JsonFile */ private function filePutContentsIfModified($path, $content) { - $currentContent = file_get_contents($path); + $currentContent = @file_get_contents($path); if (!$currentContent || ($currentContent != $content)) { return file_put_contents($path, $content); } From f02989ceba7448546873f327feaf344387c04478 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=96=D0=B0=D0=BA=D0=BE=D0=B2=20=D0=92=D0=B8=D1=82=D0=B0?= =?UTF-8?q?=D0=BB=D0=B8=D0=B9?= Date: Wed, 15 Jan 2020 18:27:12 +0500 Subject: [PATCH 3/7] revert formatting --- src/Composer/Autoload/AutoloadGenerator.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Composer/Autoload/AutoloadGenerator.php b/src/Composer/Autoload/AutoloadGenerator.php index 6ca4b3329..6432c8065 100644 --- a/src/Composer/Autoload/AutoloadGenerator.php +++ b/src/Composer/Autoload/AutoloadGenerator.php @@ -393,8 +393,8 @@ EOF; /** * Compiles an ordered list of namespace => path mappings * - * @param array $packageMap array of array(package, installDir-relative-to-composer.json) - * @param PackageInterface $mainPackage root package instance + * @param array $packageMap array of array(package, installDir-relative-to-composer.json) + * @param PackageInterface $mainPackage root package instance * @param bool $filterOutRequireDevPackages whether to filter out require-dev packages * @return array array('psr-0' => array('Ns\\Foo' => array('installDir'))) */ From 33db6ec71bd2efc86b73c987878ddbd309d5c627 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=96=D0=B0=D0=BA=D0=BE=D0=B2=20=D0=92=D0=B8=D1=82=D0=B0?= =?UTF-8?q?=D0=BB=D0=B8=D0=B9?= Date: Fri, 17 Jan 2020 10:58:52 +0500 Subject: [PATCH 4/7] touch only if update --- src/Composer/Installer.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Composer/Installer.php b/src/Composer/Installer.php index bf0523356..270c75544 100644 --- a/src/Composer/Installer.php +++ b/src/Composer/Installer.php @@ -313,6 +313,15 @@ class Installer } } + if ($this->update) { + $vendorDir = $this->config->get('vendor-dir'); + if (is_dir($vendorDir)) { + // suppress errors as this fails sometimes on OSX for no apparent reason + // see https://github.com/composer/composer/issues/4070#issuecomment-129792748 + @touch($vendorDir); + } + } + if ($this->runScripts) { // dispatch post event $eventName = $this->update ? ScriptEvents::POST_UPDATE_CMD : ScriptEvents::POST_INSTALL_CMD; From 1e927806000e46b73fed225446151af8825719e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=96=D0=B0=D0=BA=D0=BE=D0=B2=20=D0=92=D0=B8=D1=82=D0=B0?= =?UTF-8?q?=D0=BB=D0=B8=D0=B9?= Date: Fri, 17 Jan 2020 19:37:39 +0500 Subject: [PATCH 5/7] operations case touch --- src/Composer/Installer.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Composer/Installer.php b/src/Composer/Installer.php index 270c75544..a0e365ded 100644 --- a/src/Composer/Installer.php +++ b/src/Composer/Installer.php @@ -313,15 +313,6 @@ class Installer } } - if ($this->update) { - $vendorDir = $this->config->get('vendor-dir'); - if (is_dir($vendorDir)) { - // suppress errors as this fails sometimes on OSX for no apparent reason - // see https://github.com/composer/composer/issues/4070#issuecomment-129792748 - @touch($vendorDir); - } - } - if ($this->runScripts) { // dispatch post event $eventName = $this->update ? ScriptEvents::POST_UPDATE_CMD : ScriptEvents::POST_INSTALL_CMD; @@ -633,6 +624,15 @@ class Installer $localRepo->write(); } + if ($operations) { + $vendorDir = $this->config->get('vendor-dir'); + if (is_dir($vendorDir)) { + // suppress errors as this fails sometimes on OSX for no apparent reason + // see https://github.com/composer/composer/issues/4070#issuecomment-129792748 + @touch($vendorDir); + } + } + return array(0, $devPackages); } From 48c7442b63fd74bea6416d80f5095a1723374b05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=96=D0=B0=D0=BA=D0=BE=D0=B2=20=D0=92=D0=B8=D1=82=D0=B0?= =?UTF-8?q?=D0=BB=D0=B8=D0=B9?= Date: Mon, 20 Jan 2020 16:25:38 +0500 Subject: [PATCH 6/7] should be || not OR --- src/Composer/Autoload/AutoloadGenerator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Composer/Autoload/AutoloadGenerator.php b/src/Composer/Autoload/AutoloadGenerator.php index 6432c8065..940ab3f18 100644 --- a/src/Composer/Autoload/AutoloadGenerator.php +++ b/src/Composer/Autoload/AutoloadGenerator.php @@ -1014,7 +1014,7 @@ INITIALIZER; */ protected function safeCopy($source, $target) { - if (!file_exists($target) OR !file_exists($source) OR !$this->filesAreEqual($source, $target)) { + if (!file_exists($target) || !file_exists($source) || !$this->filesAreEqual($source, $target)) { $source = fopen($source, 'r'); $target = fopen($target, 'w+'); From ccd8be382ba7c95d79178b67af61eec48d0f8481 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=96=D0=B0=D0=BA=D0=BE=D0=B2=20=D0=92=D0=B8=D1=82=D0=B0?= =?UTF-8?q?=D0=BB=D0=B8=D0=B9?= Date: Tue, 21 Jan 2020 11:10:18 +0500 Subject: [PATCH 7/7] comments for #2764 --- src/Composer/Installer.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Composer/Installer.php b/src/Composer/Installer.php index a0e365ded..71fa13d9b 100644 --- a/src/Composer/Installer.php +++ b/src/Composer/Installer.php @@ -624,6 +624,7 @@ class Installer $localRepo->write(); } + // see https://github.com/composer/composer/issues/2764 if ($operations) { $vendorDir = $this->config->get('vendor-dir'); if (is_dir($vendorDir)) {