Skip to content

Commit

Permalink
SQLiteJournal: filesystem permissions like ordinary file [Closes #29]
Browse files Browse the repository at this point in the history
  • Loading branch information
milo authored and dg committed Jul 31, 2016
1 parent 485c09a commit 9512ca5
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/SQLiteJournal.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ private function open()
throw new Nette\NotSupportedException('SQLiteJournal requires PHP extension pdo_sqlite which is not loaded.');
}

if ($this->path !== ':memory:' && !is_file($this->path)) {
touch($this->path); // ensures ordinary file permissions
}

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

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

use Nette\Caching\Storages\SQLiteJournal;
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 . '/sqlitejournal.permissions.1.sqlite';
Assert::false(file_exists($file));

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

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


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

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

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

0 comments on commit 9512ca5

Please sign in to comment.