Skip to content

Commit 3128cfd

Browse files
minor symfony#24317 Use PHP_MAXPATHLEN in Filesystem.php (edoger)
This PR was submitted for the master branch but it was merged into the 2.7 branch instead (closes symfony#24317). Discussion ---------- Use PHP_MAXPATHLEN in Filesystem.php | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | N/A | License | MIT | Doc PR | N/A Relative to the use of fixed values, I think the use of PHP_MAXPATHLEN will be more robust. Commits ------- 7a47559 Use PHP_MAXPATHLEN in Filesystem.
2 parents 4129fd2 + 7a47559 commit 3128cfd

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

src/Symfony/Component/Filesystem/Filesystem.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,11 @@ public function mkdir($dirs, $mode = 0777)
117117
*/
118118
public function exists($files)
119119
{
120+
$maxPathLength = PHP_MAXPATHLEN - 2;
121+
120122
foreach ($this->toIterator($files) as $file) {
121-
if ('\\' === DIRECTORY_SEPARATOR && strlen($file) > 258) {
122-
throw new IOException('Could not check if file exist because path length exceeds 258 characters.', 0, null, $file);
123+
if (strlen($file) > $maxPathLength) {
124+
throw new IOException(sprintf('Could not check if file exist because path length exceeds %d characters.', $maxPathLength), 0, null, $file);
123125
}
124126

125127
if (!file_exists($file)) {
@@ -301,8 +303,10 @@ public function rename($origin, $target, $overwrite = false)
301303
*/
302304
private function isReadable($filename)
303305
{
304-
if ('\\' === DIRECTORY_SEPARATOR && strlen($filename) > 258) {
305-
throw new IOException('Could not check if file is readable because path length exceeds 258 characters.', 0, null, $filename);
306+
$maxPathLength = PHP_MAXPATHLEN - 2;
307+
308+
if (strlen($filename) > $maxPathLength) {
309+
throw new IOException(sprintf('Could not check if file is readable because path length exceeds %d characters.', $maxPathLength), 0, null, $filename);
306310
}
307311

308312
return is_readable($filename);

src/Symfony/Component/Filesystem/Tests/FilesystemTest.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -374,16 +374,13 @@ public function testFilesExists()
374374
*/
375375
public function testFilesExistsFails()
376376
{
377-
if ('\\' !== DIRECTORY_SEPARATOR) {
378-
$this->markTestSkipped('Test covers edge case on Windows only.');
379-
}
380-
381377
$basePath = $this->workspace.'\\directory\\';
378+
$maxPathLength = PHP_MAXPATHLEN - 2;
382379

383380
$oldPath = getcwd();
384381
mkdir($basePath);
385382
chdir($basePath);
386-
$file = str_repeat('T', 259 - strlen($basePath));
383+
$file = str_repeat('T', $maxPathLength - strlen($basePath) + 1);
387384
$path = $basePath.$file;
388385
exec('TYPE NUL >>'.$file); // equivalent of touch, we can not use the php touch() here because it suffers from the same limitation
389386
$this->longPathNamesWindows[] = $path; // save this so we can clean up later

0 commit comments

Comments
 (0)