Skip to content

Commit

Permalink
Write file to temp dir to ensure it is writable
Browse files Browse the repository at this point in the history
Fixes #230
  • Loading branch information
gordalina committed Jul 28, 2023
1 parent ba4571e commit c1ae971
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/CacheTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ protected function getWritableTempDir($tempDir = null) {
if (is_null($tempDir)) {
$tempDirs = ['/dev/shm', '/var/run', sys_get_temp_dir()];
foreach ($tempDirs as $dir) {
if (is_dir($dir) && is_writable($dir)) {
if ($this->isWritable($dir)) {
$tempDir = $dir;
break;
}
Expand All @@ -248,4 +248,19 @@ protected function getWritableTempDir($tempDir = null) {

return $tempDir;
}

protected function isWritable($tempDir) {
if (!is_dir($tempDir) || !is_writable($tempDir)) {
return false;
}

$tempFile = tempnam($tempDir, 'cachetool-test');

if ($tempFile === false) {
return false;
}

@unlink($tempFile);
return true;
}
}
5 changes: 5 additions & 0 deletions tests/CacheToolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ public function testWithInexistentTempDir() {
rmdir($dir);
}

public function testWithExistentTempDir() {
$cachetool = new CacheTool(null);
$this->assertDirectoryExists($cachetool->getTempDir());
}

public function testWithSetAdapterAndLogger()
{
$logger = $this->getLogger();
Expand Down

0 comments on commit c1ae971

Please sign in to comment.