Skip to content

Commit 19d65f7

Browse files
hranickadg
authored andcommitted
SQLiteStorage: filesystem permissions like ordinary file (#44)
1 parent da827a7 commit 19d65f7

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/Caching/Storages/SQLiteStorage.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ class SQLiteStorage implements Nette\Caching\IStorage, Nette\Caching\IBulkReader
2424

2525
public function __construct($path)
2626
{
27+
if ($path !== ':memory:' && !is_file($path)) {
28+
touch($path); // ensures ordinary file permissions
29+
}
30+
2731
$this->pdo = new \PDO('sqlite:' . $path);
2832
$this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
2933
$this->pdo->exec('
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\Caching\Storages\SQLiteStorage database file permissions.
5+
*/
6+
7+
use Nette\Caching\Storages\SQLiteStorage;
8+
use Tester\Assert;
9+
10+
11+
require __DIR__ . '/../bootstrap.php';
12+
13+
14+
if (!extension_loaded('pdo_sqlite')) {
15+
Tester\Environment::skip('Requires PHP extension pdo_sqlite.');
16+
} elseif (defined('PHP_WINDOWS_VERSION_BUILD')) {
17+
Tester\Environment::skip('UNIX test only.');
18+
}
19+
20+
21+
test(function () {
22+
$file = TEMP_DIR . '/sqlitestorage.permissions.1.sqlite';
23+
Assert::false(file_exists($file));
24+
25+
umask(0);
26+
(new SQLiteStorage($file))->write('foo', 'bar', []);
27+
28+
Assert::same(0666, fileperms($file) & 0777);
29+
});
30+
31+
32+
test(function () {
33+
$file = TEMP_DIR . '/sqlitestorage.permissions.2.sqlite';
34+
Assert::false(file_exists($file));
35+
36+
umask(0077);
37+
(new SQLiteStorage($file))->write('foo', 'bar', []);
38+
39+
Assert::same(0600, fileperms($file) & 0777);
40+
});

0 commit comments

Comments
 (0)