Skip to content
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

[5.7] Fix filesystem locking hangs in PackageManifest::build() #26010

Merged
merged 3 commits into from
Oct 10, 2018
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
31 changes: 31 additions & 0 deletions src/Illuminate/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Filesystem;

use Exception;
use ErrorException;
use FilesystemIterator;
use Symfony\Component\Finder\Finder;
Expand Down Expand Up @@ -122,6 +123,36 @@ public function put($path, $contents, $lock = false)
return file_put_contents($path, $contents, $lock ? LOCK_EX : 0);
}

/**
* Write the contents of a file, replacing it atomically if it already exists.
*
* This will also replace the target file permissions.
*
* @param string $path
* @param string $content
* @throws Exception
*/
public function replace($path, $content)
{
// Just in case path already exists and is a symlink, we want to make sure we get the real path.
clearstatcache(true, $path);
$realPath = realpath($path);
if ($realPath) {
$path = $realPath;
}

$dirName = dirname($path);
if (! is_writable($dirName)) {
throw new Exception("Replacing $path requires it's parent directory to be writable.");
}

// Write out the contents to a temp file, so we then can rename the file atomically.
$tempPath = tempnam($dirName, basename($path));
file_put_contents($tempPath, $content);

rename($tempPath, $path);
}

/**
* Prepend to a file.
*
Expand Down
12 changes: 2 additions & 10 deletions src/Illuminate/Foundation/PackageManifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Illuminate\Foundation;

use Exception;
use Illuminate\Filesystem\Filesystem;

class PackageManifest
Expand Down Expand Up @@ -97,7 +96,7 @@ protected function getManifest()
$this->build();
}

$this->files->get($this->manifestPath, true);
$this->files->get($this->manifestPath);

return $this->manifest = file_exists($this->manifestPath) ?
$this->files->getRequire($this->manifestPath) : [];
Expand Down Expand Up @@ -164,13 +163,6 @@ protected function packagesToIgnore()
*/
protected function write(array $manifest)
{
if (! is_writable(dirname($this->manifestPath))) {
throw new Exception('The '.dirname($this->manifestPath).' directory must be present and writable.');
}

$this->files->put(
$this->manifestPath, '<?php return '.var_export($manifest, true).';',
true
);
$this->files->replace($this->manifestPath, '<?php return '.var_export($manifest, true).';');
}
}
10 changes: 10 additions & 0 deletions tests/Filesystem/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ public function testPutStoresFiles()
$this->assertStringEqualsFile($this->tempDir.'/file.txt', 'Hello World');
}

public function testReplaceStoresFiles()
{
$files = new Filesystem;
$files->replace($this->tempDir.'/file.txt', 'Hello World');
$this->assertStringEqualsFile($this->tempDir.'/file.txt', 'Hello World');

$files->replace($this->tempDir.'/file.txt', 'Something Else');
$this->assertStringEqualsFile($this->tempDir.'/file.txt', 'Something Else');
}

public function testSetChmod()
{
file_put_contents($this->tempDir.'/file.txt', 'Hello World');
Expand Down