From 7a475595e2dd64790b3f1caf56f5b57cefcef27f Mon Sep 17 00:00:00 2001 From: Qingshan Luo Date: Tue, 26 Sep 2017 10:03:27 +0800 Subject: [PATCH] Use PHP_MAXPATHLEN in Filesystem. --- src/Symfony/Component/Filesystem/Filesystem.php | 12 ++++++++---- .../Component/Filesystem/Tests/FilesystemTest.php | 7 ++----- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/Symfony/Component/Filesystem/Filesystem.php b/src/Symfony/Component/Filesystem/Filesystem.php index 223e116090..1c10366cee 100644 --- a/src/Symfony/Component/Filesystem/Filesystem.php +++ b/src/Symfony/Component/Filesystem/Filesystem.php @@ -117,9 +117,11 @@ class Filesystem */ public function exists($files) { + $maxPathLength = PHP_MAXPATHLEN - 2; + foreach ($this->toIterator($files) as $file) { - if ('\\' === DIRECTORY_SEPARATOR && strlen($file) > 258) { - throw new IOException('Could not check if file exist because path length exceeds 258 characters.', 0, null, $file); + if (strlen($file) > $maxPathLength) { + throw new IOException(sprintf('Could not check if file exist because path length exceeds %d characters.', $maxPathLength), 0, null, $file); } if (!file_exists($file)) { @@ -301,8 +303,10 @@ class Filesystem */ private function isReadable($filename) { - if ('\\' === DIRECTORY_SEPARATOR && strlen($filename) > 258) { - throw new IOException('Could not check if file is readable because path length exceeds 258 characters.', 0, null, $filename); + $maxPathLength = PHP_MAXPATHLEN - 2; + + if (strlen($filename) > $maxPathLength) { + throw new IOException(sprintf('Could not check if file is readable because path length exceeds %d characters.', $maxPathLength), 0, null, $filename); } return is_readable($filename); diff --git a/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php b/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php index 1228b2d8e2..fb6c135f92 100644 --- a/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php +++ b/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php @@ -374,16 +374,13 @@ class FilesystemTest extends FilesystemTestCase */ public function testFilesExistsFails() { - if ('\\' !== DIRECTORY_SEPARATOR) { - $this->markTestSkipped('Test covers edge case on Windows only.'); - } - $basePath = $this->workspace.'\\directory\\'; + $maxPathLength = PHP_MAXPATHLEN - 2; $oldPath = getcwd(); mkdir($basePath); chdir($basePath); - $file = str_repeat('T', 259 - strlen($basePath)); + $file = str_repeat('T', $maxPathLength - strlen($basePath) + 1); $path = $basePath.$file; exec('TYPE NUL >>'.$file); // equivalent of touch, we can not use the php touch() here because it suffers from the same limitation $this->longPathNamesWindows[] = $path; // save this so we can clean up later