Skip to content
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
16 changes: 13 additions & 3 deletions src/Util/PharExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Joli\JoliNotif\Util;

use Joli\JoliNotif\Exception\PharExtractionException;
use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem;

/**
Expand Down Expand Up @@ -53,6 +54,7 @@ public static function extractFile(string $filePath, bool $overwrite = false): s
$extractionDir = self::getExtractionDirectory($pharPath);
$extractedFilePath = $extractionDir . '/' . $relativeFilePath;
$lockPath = $extractionDir . '/.lock';
$filesystem = new Filesystem();
$lock = fopen($lockPath, 'c');

if (false === $lock) {
Expand All @@ -65,12 +67,16 @@ public static function extractFile(string $filePath, bool $overwrite = false): s
}

// Mark the directory as still in use, so it does not get pruned.
touch($lockPath);
try {
$filesystem->touch($lockPath);
} catch (IOException) {
// At worst, the directory will be extracted again after being pruned.
}

// Check after acquiring the lock: another process may have extracted the file.
clearstatcache(true, $extractedFilePath);

if (!file_exists($extractedFilePath) || $overwrite) {
if (!$filesystem->exists($extractedFilePath) || $overwrite) {
$phar = new \Phar($pharPath);
$phar->extractTo($extractionDir, $relativeFilePath, $overwrite);
}
Expand Down Expand Up @@ -174,7 +180,11 @@ private static function pruneStaleDirectories(string $cacheDir): void
private static function createPrivateDirectory(string $directory): void
{
// A concurrent creator is fine; validate the resulting directory in either case.
@mkdir($directory, 0o700, true);
try {
(new Filesystem())->mkdir($directory, 0o700);
} catch (IOException) {
}

clearstatcache(true, $directory);
$stat = @lstat($directory);

Expand Down
6 changes: 2 additions & 4 deletions tests/Driver/AbstractDriverTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,15 @@
use Joli\JoliNotif\Driver\DriverInterface;
use Joli\JoliNotif\Notification;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Filesystem\Filesystem;

abstract class AbstractDriverTestCase extends TestCase
{
public static function getIconDir(): string
{
$iconDir = sys_get_temp_dir() . '/jolinotif-tests-fixtures';

if (!is_dir($iconDir)) {
mkdir($iconDir);
copy(\dirname(__DIR__) . '/fixtures/image.gif', $iconDir . '/image.gif');
}
(new Filesystem())->copy(\dirname(__DIR__) . '/fixtures/image.gif', $iconDir . '/image.gif');

// Resolve symlinks (e.g. /var -> /private/var on macOS) to match Notification::setIcon()
return (string) realpath($iconDir);
Expand Down
5 changes: 3 additions & 2 deletions tests/NotificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public function testItExtractsIconFromPhar(): void
$homeDir = $testDir . '/home';
$pharPath = $testDir . '/notification-extract-icon-' . $key . '.phar';

mkdir($homeDir, 0o700, true);
$filesystem = new Filesystem();
$filesystem->mkdir($homeDir, 0o700);

$bootstrap = <<<'PHAR_BOOTSTRAP'
<?php
Expand Down Expand Up @@ -85,7 +86,7 @@ public function testItExtractsIconFromPhar(): void
\Phar::unlinkArchive($pharPath);
}

(new Filesystem())->remove($testDir);
$filesystem->remove($testDir);
}
}

Expand Down
41 changes: 23 additions & 18 deletions tests/Util/PharExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ class PharExtractorTest extends TestCase
*/
private array $pharPaths = [];

private Filesystem $filesystem;

protected function setUp(): void
{
$this->testDir = sys_get_temp_dir() . '/jolinotif-' . bin2hex(random_bytes(8));
$this->homeDir = $this->testDir . '/home';
$this->tmpDir = $this->testDir . '/tmp';
mkdir($this->homeDir, 0o700, true);
mkdir($this->tmpDir, 0o700);
$this->filesystem = new Filesystem();
$this->filesystem->mkdir([$this->homeDir, $this->tmpDir], 0o700);
}

protected function tearDown(): void
Expand All @@ -42,7 +44,7 @@ protected function tearDown(): void
\Phar::unlinkArchive($pharPath);
}

(new Filesystem())->remove($this->testDir);
$this->filesystem->remove($this->testDir);
}

public function testIsLocatedInsideAPhar(): void
Expand Down Expand Up @@ -74,7 +76,7 @@ public function testExtractFileDoesntOverwriteExistingFileIfNotSpecified(): void
$pharPath = $this->generatePhar('contents');
$this->getProcess($pharPath)->mustRun();
$extractedFilePath = $this->getExtractedFilePath($pharPath);
file_put_contents($extractedFilePath, 'cached contents');
$this->filesystem->dumpFile($extractedFilePath, 'cached contents');

$this->getProcess($pharPath)->mustRun();

Expand All @@ -86,7 +88,7 @@ public function testExtractFileOverwritesExistingFileIfSpecified(): void
$pharPath = $this->generatePhar('contents');
$this->getProcess($pharPath)->mustRun();
$extractedFilePath = $this->getExtractedFilePath($pharPath);
file_put_contents($extractedFilePath, 'cached contents');
$this->filesystem->dumpFile($extractedFilePath, 'cached contents');

$this->getProcess($pharPath, ['--overwrite'])->mustRun();

Expand Down Expand Up @@ -139,8 +141,8 @@ public function testFallsBackToTheTemporaryDirectoryWithUnusableHome(): void
self::markTestSkipped('The temporary directory is always used on Windows.');
}

mkdir($this->getCacheDirectory(), 0o777, true);
chmod($this->getCacheDirectory(), 0o777);
$this->filesystem->mkdir($this->getCacheDirectory());
$this->filesystem->chmod($this->getCacheDirectory(), 0o777);
$pharPath = $this->generatePhar('contents');
$process = $this->getProcess($pharPath);
$process->mustRun();
Expand All @@ -155,8 +157,8 @@ public function testRejectsASymlinkCacheDirectory(): void
}

$targetDir = $this->testDir . '/untrusted';
mkdir($targetDir, 0o700);
symlink($targetDir, $this->getFallbackCacheDirectory());
$this->filesystem->mkdir($targetDir, 0o700);
$this->filesystem->symlink($targetDir, $this->getFallbackCacheDirectory());
$process = $this->getProcess($this->generatePhar('contents'), [], ['HOME' => false]);

$this->assertExtractionFails($process, 'not a real directory');
Expand All @@ -169,8 +171,8 @@ public function testRejectsAWorldWritableCacheDirectory(): void
}

$cacheDir = $this->getFallbackCacheDirectory();
mkdir($cacheDir, 0o700);
chmod($cacheDir, 0o777);
$this->filesystem->mkdir($cacheDir, 0o700);
$this->filesystem->chmod($cacheDir, 0o777);
$process = $this->getProcess($this->generatePhar('contents'), [], ['HOME' => false]);

$this->assertExtractionFails($process, 'permissions 0700');
Expand All @@ -181,13 +183,10 @@ public function testPrunesStaleDirectories(): void
{
$staleDir = $this->getCacheDirectory() . '/stale';
$recentDir = $this->getCacheDirectory() . '/recent';
mkdir($staleDir . '/path', 0o700, true);
mkdir($recentDir, 0o700);
touch($staleDir . '/path/file.txt');
touch($staleDir . '/.lock', time() - 40 * 24 * 3600);
touch($staleDir, time() - 40 * 24 * 3600);
touch($recentDir . '/.lock', time() - 40 * 24 * 3600);
touch($recentDir . '/.lock');
$this->filesystem->mkdir([$staleDir . '/path', $recentDir], 0o700);
$this->filesystem->touch($staleDir . '/path/file.txt');
$this->filesystem->touch([$staleDir . '/.lock', $staleDir], time() - 40 * 24 * 3600);
$this->filesystem->touch($recentDir . '/.lock');

$this->getProcess($this->generatePhar('contents'))->mustRun();

Expand Down Expand Up @@ -250,6 +249,9 @@ private function generatePhar(string $fileContent): string
$bootstrap = <<<'PHAR_BOOTSTRAP'
<?php

require __DIR__.'/vendor/symfony/filesystem/Exception/ExceptionInterface.php';
require __DIR__.'/vendor/symfony/filesystem/Exception/IOExceptionInterface.php';
require __DIR__.'/vendor/symfony/filesystem/Exception/IOException.php';
require __DIR__.'/vendor/symfony/filesystem/Filesystem.php';
require __DIR__.'/src/Exception/ExceptionInterface.php';
require __DIR__.'/src/Exception/PharExtractionException.php';
Expand All @@ -267,6 +269,9 @@ private function generatePhar(string $fileContent): string
$phar = new \Phar($pharPath);

$files = [
'vendor/symfony/filesystem/Exception/ExceptionInterface.php',
'vendor/symfony/filesystem/Exception/IOExceptionInterface.php',
'vendor/symfony/filesystem/Exception/IOException.php',
'vendor/symfony/filesystem/Filesystem.php',
'src/Exception/ExceptionInterface.php',
'src/Exception/PharExtractionException.php',
Expand Down
Loading