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
4 changes: 3 additions & 1 deletion system/Cache/FactoriesCache/FileVarExportHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function save(string $key, mixed $val): void

// Two processes may try to create the directory at the same time.
// is_dir() confirms it exists, so suppressing the warning is safe.
if (! is_dir($this->path) && ! @mkdir($this->path, 0777, true) && ! is_dir($this->path)) {
if (! is_dir($this->path) && ! @mkdir($this->path, 0755, true) && ! is_dir($this->path)) {
Comment thread
paulbalandan marked this conversation as resolved.
log_message('error', 'FactoriesCache: cannot create cache directory: ' . $this->path);

return;
Expand All @@ -37,6 +37,8 @@ public function save(string $key, mixed $val): void
return;
}

@chmod($tmp, 0644);

// Another process may have wiped the directory. Clean up on failure.
if (! @rename($tmp, $this->path . "/{$key}")) {
log_message('warning', 'FactoriesCache: failed to commit cache file for key: ' . $key);
Expand Down
37 changes: 37 additions & 0 deletions tests/system/Cache/FactoriesCacheFileVarExportHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,46 @@
#[Group('Others')]
final class FactoriesCacheFileVarExportHandlerTest extends AbstractFactoriesCacheHandlerTestCase
{
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();

helper('filesystem');
}

protected function createFactoriesCache(): void
{
$this->handler = new FileVarExportHandler();
$this->cache = new FactoriesCache($this->handler);
}

public function testSaveCreatesDirectoryAndFileWithCorrectPermissions(): void
{
$dir = WRITEPATH . 'cache_test_dir_' . uniqid('', true);
$oldUmask = umask(0000);

try {
$handler = new FileVarExportHandler();
$this->setPrivateProperty($handler, 'path', $dir);

$handler->save('test_key', ['data']);

$this->assertDirectoryExists($dir);

if (! is_windows()) {
$dirPerms = fileperms($dir) & 0777;
$this->assertSame(0755, $dirPerms);

$filePerms = fileperms($dir . '/test_key') & 0777;
$this->assertSame(0644, $filePerms);
}
} finally {
umask($oldUmask);

if (is_dir($dir)) {
delete_files($dir);
rmdir($dir);
}
}
}
Comment thread
michalsn marked this conversation as resolved.
}
Loading