From cf7a9669004ee32d6e15e1b1de8319f6dc408d1f Mon Sep 17 00:00:00 2001 From: Bob Weinand Date: Mon, 3 Oct 2016 12:17:05 +0200 Subject: [PATCH] Fix #11 - use 0666 default permissions and rely on umask() --- lib/BlockingDriver.php | 2 +- lib/EioDriver.php | 8 ++++---- lib/UvDriver.php | 6 +++--- test/DriverTest.php | 18 +++++++++++++----- 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/lib/BlockingDriver.php b/lib/BlockingDriver.php index 192127a..fdbdaa2 100644 --- a/lib/BlockingDriver.php +++ b/lib/BlockingDriver.php @@ -203,7 +203,7 @@ class BlockingDriver implements Driver { /** * {@inheritdoc} */ - public function mkdir($path, $mode = 0644, $recursive = false) { + public function mkdir($path, $mode = 0666, $recursive = false) { return new Success((bool) @\mkdir($path, $mode, $recursive)); } diff --git a/lib/EioDriver.php b/lib/EioDriver.php index ce3213a..1e1f0aa 100644 --- a/lib/EioDriver.php +++ b/lib/EioDriver.php @@ -71,7 +71,7 @@ class EioDriver implements Driver { "Invalid open mode" )); } - $chmod = ($flags & \EIO_O_CREAT) ? 0644 : 0; + $chmod = ($flags & \EIO_O_CREAT) ? 0666 : 0; \call_user_func($this->incrementor, 1); $promisor = new Deferred; $openArr = [$mode, $path, $promisor]; @@ -356,7 +356,7 @@ class EioDriver implements Driver { /** * {@inheritdoc} */ - public function mkdir($path, $mode = 0644, $recursive = false) { + public function mkdir($path, $mode = 0666, $recursive = false) { \call_user_func($this->incrementor, 1); $promisor = new Deferred; $priority = \EIO_PRI_DEFAULT; @@ -538,8 +538,8 @@ class EioDriver implements Driver { * {@inheritdoc} */ public function put($path, $contents) { - $flags = \EIO_O_RDWR | \EIO_O_CREAT | \EIO_O_TRUNC; - $mode = \EIO_S_IRUSR | \EIO_S_IWUSR | \EIO_S_IXUSR; + $flags = \EIO_O_WRONLY | \EIO_O_CREAT | \EIO_O_TRUNC; + $mode = 0666; $priority = \EIO_PRI_DEFAULT; \call_user_func($this->incrementor, 1); diff --git a/lib/UvDriver.php b/lib/UvDriver.php index ab92d73..0b9a826 100644 --- a/lib/UvDriver.php +++ b/lib/UvDriver.php @@ -38,7 +38,7 @@ class UvDriver implements Driver { "Invalid open mode" )); } - $chmod = ($flags & \UV::O_CREAT) ? 0644 : 0; + $chmod = ($flags & \UV::O_CREAT) ? 0666 : 0; $this->reactor->addRef(); $promisor = new Deferred; $openArr = [$mode, $path, $promisor]; @@ -300,7 +300,7 @@ class UvDriver implements Driver { /** * {@inheritdoc} */ - public function mkdir($path, $mode = 0644, $recursive = false) { + public function mkdir($path, $mode = 0666, $recursive = false) { $this->reactor->addRef(); $promisor = new Deferred; @@ -516,7 +516,7 @@ class UvDriver implements Driver { private function doPut($path, $contents): \Generator { $flags = \UV::O_WRONLY | \UV::O_CREAT | \UV::O_TRUNC; - $mode = \UV::S_IRWXU | \UV::S_IRUSR; + $mode = 0666; $this->reactor->addRef(); $promise = $this->doFsOpen($path, $flags, $mode); if (!$fh = (yield $promise)) { diff --git a/test/DriverTest.php b/test/DriverTest.php index 6851af9..ac2b144 100644 --- a/test/DriverTest.php +++ b/test/DriverTest.php @@ -7,6 +7,7 @@ use Amp\File as file; abstract class DriverTest extends \PHPUnit_Framework_TestCase { private static $fixtureId; + private static $umask; private static function getFixturePath() { if (empty(self::$fixtureId)) { @@ -33,6 +34,7 @@ abstract class DriverTest extends \PHPUnit_Framework_TestCase { $fixtureDir = self::getFixturePath(); self::clearFixtureDir(); + self::$umask = umask(0022); if (!\mkdir($fixtureDir, $mode = 0777, $recursive = true)) { throw new \RuntimeException( @@ -53,6 +55,7 @@ abstract class DriverTest extends \PHPUnit_Framework_TestCase { public static function tearDownAfterClass() { self::clearFixtureDir(); + umask(self::$umask); } protected function setUp() { @@ -258,12 +261,15 @@ abstract class DriverTest extends \PHPUnit_Framework_TestCase { }); } - public function testUnlink() { + public function testPermsAndUnlink() { amp\run(function () { $fixtureDir = self::getFixturePath(); $toUnlink = "{$fixtureDir}/unlink"; + $mask = umask(062); yield file\put($toUnlink, "unlink me"); - $this->assertTrue((bool) (yield file\stat($toUnlink))); + umask($mask); + $stat = yield file\stat($toUnlink); + $this->assertTrue(($stat["mode"] & 0777) == 0604); yield file\unlink($toUnlink); $this->assertNull(yield file\stat($toUnlink)); }); @@ -276,14 +282,16 @@ abstract class DriverTest extends \PHPUnit_Framework_TestCase { $dir = "{$fixtureDir}/newdir"; yield file\mkdir($dir); - $this->assertNotNull(yield file\stat($dir)); + $stat = yield file\stat($dir); + $this->assertTrue(($stat["mode"] & 0777) == 0644); yield file\rmdir($dir); $this->assertNull(yield file\stat($dir)); $dir = "{$fixtureDir}/newdir/with/recursive/creation"; - yield file\mkdir($dir, 0744, true); - $this->assertNotNull(yield file\stat($dir)); + yield file\mkdir($dir, 0764, true); // the umask is 022 by default + $stat = yield file\stat($dir); + $this->assertTrue(($stat["mode"] & 0777) == 0744); }); }