Skip to content

Commit

Permalink
bug #41893 [Filesystem] Workaround cannot dumpFile into "protected" f…
Browse files Browse the repository at this point in the history
…olders on Windows (arnegroskurth)

This PR was merged into the 4.4 branch.

Discussion
----------

[Filesystem] Workaround cannot dumpFile into "protected" folders on Windows

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #39496
| License       | MIT
| Doc PR        |

Commits
-------

4b9b68c [Filesystem] Workaround cannot dumpFile into "protected" folders on Windows
  • Loading branch information
fabpot committed Jun 30, 2021
2 parents 99a8b9f + 4b9b68c commit e2d33fb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
8 changes: 0 additions & 8 deletions src/Symfony/Component/Filesystem/Filesystem.php
Expand Up @@ -678,10 +678,6 @@ public function dumpFile($filename, $content)
$this->mkdir($dir);
}

if (!is_writable($dir)) {
throw new IOException(sprintf('Unable to write to the "%s" directory.', $dir), 0, null, $dir);
}

// Will create a temp file with 0600 access rights
// when the filesystem supports chmod.
$tmpFile = $this->tempnam($dir, basename($filename));
Expand Down Expand Up @@ -721,10 +717,6 @@ public function appendToFile($filename, $content)
$this->mkdir($dir);
}

if (!is_writable($dir)) {
throw new IOException(sprintf('Unable to write to the "%s" directory.', $dir), 0, null, $dir);
}

if (false === @file_put_contents($filename, $content, \FILE_APPEND)) {
throw new IOException(sprintf('Failed to write file "%s".', $filename), 0, null, $filename);
}
Expand Down
21 changes: 21 additions & 0 deletions src/Symfony/Component/Filesystem/Tests/FilesystemTest.php
Expand Up @@ -1756,6 +1756,27 @@ public function testCopyShouldKeepExecutionPermission()
$this->assertFilePermissions(767, $targetFilePath);
}

public function testDumpToProtectedDirectory()
{
if (\DIRECTORY_SEPARATOR !== '\\') {
$this->markTestSkipped('This test is specific to Windows.');
}

if (($userProfilePath = getenv('USERPROFILE')) === false || !is_dir($userProfilePath)) {
throw new \RuntimeException('Failed to retrieve user profile path.');
}

$targetPath = implode(\DIRECTORY_SEPARATOR, [$userProfilePath, 'Downloads', '__test_file.ext']);

try {
$this->assertFileDoesNotExist($targetPath);
$this->filesystem->dumpFile($targetPath, 'foobar');
$this->assertFileExists($targetPath);
} finally {
$this->filesystem->remove($targetPath);
}
}

/**
* Normalize the given path (transform each forward slash into a real directory separator).
*/
Expand Down

0 comments on commit e2d33fb

Please sign in to comment.