8000 [Filesystem] Add $suffix argument to tempnam() by jdufresne · Pull Request #33003 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
8000

[Filesystem] Add $suffix argument to tempnam() #33003

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
Feb 4, 2020
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
1 change: 1 addition & 0 deletions src/Symfony/Component/Filesystem/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ CHANGELOG
-----

* support for passing a `null` value to `Filesystem::isAbsolutePath()` is deprecated and will be removed in 5.0
* `tempnam()` now accepts a third argument `$suffix`.

4.3.0
-----
Expand Down
8 changes: 5 additions & 3 deletions src/Symfony/Component/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -584,15 +584,17 @@ public function isAbsolutePath(string $file)
*
* @param string $prefix The prefix of the generated temporary filename
* Note: Windows uses only the first three characters of prefix
* @param string $suffix The suffix of the generated temporary filename
*
* @return string The new temporary filename (with path), or throw an exception on failure
*/
public function tempnam(string $dir, string $prefix)
public function tempnam(string $dir, string $prefix/*, string $suffix = ''*/)
{
$suffix = \func_num_args() > 2 ? func_get_arg(2) : '';
list($scheme, $hierarchy) = $this->getSchemeAndHierarchy($dir);

// If no scheme or scheme is "file" or "gs" (Google Cloud) create temp file in local filesystem
if (null === $scheme || 'file' === $scheme || 'gs' === $scheme) {
if ((null === $scheme || 'file' === $scheme || 'gs' === $scheme) && '' === $suffix) {
$tmpFile = @tempnam($hierarchy, $prefix);

// If tempnam failed or no scheme return the filename otherwise prepend the scheme
Expand All @@ -610,7 +612,7 @@ public function tempnam(string $dir, string $prefix)
// Loop until we create a valid temp file or have reached 10 attempts
for ($i = 0; $i < 10; ++$i) {
// Create a unique filename
$tmpFile = $dir.'/'.$prefix.uniqid(mt_rand(), true);
$tmpFile = $dir.'/'.$prefix.uniqid(mt_rand(), true).$suffix;

// Use fopen instead of file_exists as some streams do not support stat
// Use mode 'x+' to atomically check existence and create to avoid a TOCTOU vulnerability
Expand Down
16 changes: 16 additions & 0 deletions src/Symfony/Component/Filesystem/Tests/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1509,6 +1509,22 @@ public function testTempnamOnUnwritableFallsBackToSysTmp()
@unlink($filename);
}

public function testTempnamWithSuffix()
{
$dirname = $this->workspace;
$filename = $this->filesystem->tempnam($dirname, 'foo', '.bar');
$this->assertStringEndsWith('.bar', $filename);
$this->assertFileExists($filename);
}

public function testTempnamWithSuffix0()
{
$dirname = $this->workspace;
$filename = $this->filesystem->tempnam($dirname, 'foo', '0');
$this->assertStringEndsWith('0', $filename);
$this->assertFileExists($filename);
}

public function testDumpFile()
{
$filename = $this->workspace.\DIRECTORY_SEPARATOR.'foo'.\DIRECTORY_SEPARATOR.'baz.txt';
Expand Down
0