diff --git a/src/Stash/Driver/Sub/SqlitePdo.php b/src/Stash/Driver/Sub/SqlitePdo.php old mode 100644 new mode 100755 index a05cede7..ee2d83f8 --- a/src/Stash/Driver/Sub/SqlitePdo.php +++ b/src/Stash/Driver/Sub/SqlitePdo.php @@ -11,6 +11,8 @@ namespace Stash\Driver\Sub; +use Stash\Exception\RuntimeException; + /** * Class SqlitePDO * @@ -243,8 +245,14 @@ protected function getDriver() $dir = substr($this->path, 0, $pos); } - if (!is_dir($dir)) { - mkdir($dir, $this->dirPermissions, true); + if (!is_dir($dir) && !mkdir($dir, $this->dirPermissions, true) && !is_dir($dir)) { + throw new RuntimeException(sprintf('Directory "%s" was not created', $dir)); + } + if (file_put_contents($this->path, '') === false) { + throw new RuntimeException(sprintf('Cache file "%s" was not created', basename($this->path))); + } + if (!chmod($this->path, $this->filePermissions)) { + throw new RuntimeException(sprintf('Cache file permissions for "%s" could not be set', basename($this->path))); } $runInstall = true; } else { diff --git a/tests/Stash/Test/Driver/SqlitePdoSqlite3Test.php b/tests/Stash/Test/Driver/SqlitePdoSqlite3Test.php old mode 100644 new mode 100755 index 66a87e58..52b3d357 --- a/tests/Stash/Test/Driver/SqlitePdoSqlite3Test.php +++ b/tests/Stash/Test/Driver/SqlitePdoSqlite3Test.php @@ -11,6 +11,8 @@ namespace Stash\Test\Driver; +use Stash\Utilities; + /** * @package Stash * @author Robert Hafner @@ -35,6 +37,25 @@ protected function setUp() : void parent::setUp(); } + public function testFilePermissions() + { + $key = array('apple', 'sauce'); + + $driverClass = '\\' . $this->driverClass; + foreach (array(0666, 0622, 0604) as $perms) { + $driver = new $driverClass(array('filePermissions' => $perms, 'dirPermissions' => 0777)); // constructor first + $filename = rtrim(Utilities::getBaseDirectory($driver), '\\/') . DIRECTORY_SEPARATOR . 'cache.sqlite'; + if (file_exists($filename)) { + unlink($filename); + } + $this->assertTrue($driver->storeData($key, 'test', time() + 30)); + $this->assertFileExists($filename); + $result = fileperms($filename) & 0777; // only care for rwx + $this->assertSame($perms, $result, sprintf('Able to set file permissions to 0%o.', $perms)); + } + @unlink($filename); + } + public function getOptions() { $options = parent::getOptions();