Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions src/Symfony/Component/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -1751,6 +1751,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