minor #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 #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.
This commit is contained in:
Nicolas Grekas 2017-09-26 11:21:42 +02:00
commit 3128cfd723
2 changed files with 10 additions and 9 deletions

View File

@ -117,9 +117,11 @@ class Filesystem
*/ */
public function exists($files) public function exists($files)
{ {
$maxPathLength = PHP_MAXPATHLEN - 2;
foreach ($this->toIterator($files) as $file) { foreach ($this->toIterator($files) as $file) {
if ('\\' === DIRECTORY_SEPARATOR && strlen($file) > 258) { if (strlen($file) > $maxPathLength) {
throw new IOException('Could not check if file exist because path length exceeds 258 characters.', 0, null, $file); throw new IOException(sprintf('Could not check if file exist because path length exceeds %d characters.', $maxPathLength), 0, null, $file);
} }
if (!file_exists($file)) { if (!file_exists($file)) {
@ -301,8 +303,10 @@ class Filesystem
*/ */
private function isReadable($filename) private function isReadable($filename)
{ {
if ('\\' === DIRECTORY_SEPARATOR && strlen($filename) > 258) { $maxPathLength = PHP_MAXPATHLEN - 2;
throw new IOException('Could not check if file is readable because path length exceeds 258 characters.', 0, null, $filename);
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); return is_readable($filename);

View File

@ -374,16 +374,13 @@ class FilesystemTest extends FilesystemTestCase
*/ */
public function testFilesExistsFails() public function testFilesExistsFails()
{ {
if ('\\' !== DIRECTORY_SEPARATOR) {
$this->markTestSkipped('Test covers edge case on Windows only.');
}
$basePath = $this->workspace.'\\directory\\'; $basePath = $this->workspace.'\\directory\\';
$maxPathLength = PHP_MAXPATHLEN - 2;
$oldPath = getcwd(); $oldPath = getcwd();
mkdir($basePath); mkdir($basePath);
chdir($basePath); chdir($basePath);
$file = str_repeat('T', 259 - strlen($basePath)); $file = str_repeat('T', $maxPathLength - strlen($basePath) + 1);
$path = $basePath.$file; $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 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 $this->longPathNamesWindows[] = $path; // save this so we can clean up later