Skip to content

Commit

Permalink
SQLiteStorage: filesystem permissions like ordinary file (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
hranicka authored and dg committed Aug 9, 2016
1 parent 9512ca5 commit a710238
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Caching/Storages/SQLiteStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ class SQLiteStorage extends Nette\Object implements Nette\Caching\IStorage

public function __construct($path = ':memory:')
{
if ($path !== ':memory:' && !is_file($path)) {
touch($path); // ensures ordinary file permissions
}

$this->pdo = new \PDO('sqlite:' . $path);
$this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$this->pdo->exec('
Expand Down
40 changes: 40 additions & 0 deletions tests/Storages/SQLiteStorage.permissions.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/**
* Test: Nette\Caching\Storages\SQLiteStorage database file permissions.
*/

use Nette\Caching\Storages\SQLiteStorage;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


if (!extension_loaded('pdo_sqlite')) {
Tester\Environment::skip('Requires PHP extension pdo_sqlite.');
} elseif (defined('PHP_WINDOWS_VERSION_BUILD')) {
Tester\Environment::skip('UNIX test only.');
}


test(function () {
$file = TEMP_DIR . '/sqlitestorage.permissions.1.sqlite';
Assert::false(file_exists($file));

umask(0);
(new SQLiteStorage($file))->write('foo', 'bar', []);

Assert::same(0666, fileperms($file) & 0777);
});


test(function () {
$file = TEMP_DIR . '/sqlitestorage.permissions.2.sqlite';
Assert::false(file_exists($file));

umask(0077);
(new SQLiteStorage($file))->write('foo', 'bar', []);

Assert::same(0600, fileperms($file) & 0777);
});

0 comments on commit a710238

Please sign in to comment.