From a15095dd00e4ddf851f06d9dc903a5cf35ba6f89 Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Sun, 3 Jul 2016 20:46:22 +0900 Subject: [PATCH] Use umask consistently --- common/framework/storage.php | 36 +++++++++++++++++++++++++--- tests/unit/framework/StorageTest.php | 2 +- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/common/framework/storage.php b/common/framework/storage.php index 07a804a36..613384b33 100644 --- a/common/framework/storage.php +++ b/common/framework/storage.php @@ -12,6 +12,11 @@ class Storage */ public static $safe_overwrite = true; + /** + * Cache the umask here. + */ + protected static $_umask; + /** * Check if a path really exists. * @@ -271,7 +276,7 @@ class Storage $filename = $original_filename; } - @chmod($filename, ($perms === null ? (0666 & ~umask()) : $perms)); + @chmod($filename, ($perms === null ? (0666 & ~self::getUmask()) : $perms)); if (function_exists('opcache_invalidate') && substr($filename, -4) === '.php') { @opcache_invalidate($filename, true); @@ -364,7 +369,7 @@ class Storage { if (is_uploaded_file($source)) { - @chmod($destination, 0666 ^ intval(config('file.umask'), 8)); + @chmod($destination, 0666 & ~self::getUmask()); } else { @@ -449,7 +454,7 @@ class Storage $dirname = rtrim($dirname, '/\\'); if ($mode === null) { - $mode = 0777 & ~umask(); + $mode = 0777 & ~self::getUmask(); } return @mkdir($dirname, $mode, true); } @@ -613,4 +618,29 @@ class Storage return true; } } + + /** + * Get the current umask. + * + * @return int + */ + public static function getUmask() + { + if (self::$_umask === null) + { + self::$_umask = intval(config('file.umask'), 8) ?: 0; + } + return self::$_umask; + } + + /** + * Set the current umask. + * + * @param int $umask + * @return void + */ + public static function setUmask($umask) + { + self::$_umask = intval($umask); + } } diff --git a/tests/unit/framework/StorageTest.php b/tests/unit/framework/StorageTest.php index 83bbe8429..28cb7aaf1 100644 --- a/tests/unit/framework/StorageTest.php +++ b/tests/unit/framework/StorageTest.php @@ -138,7 +138,7 @@ class StorageTest extends \Codeception\TestCase\Test $this->assertTrue(Rhymix\Framework\Storage::write($testfile, 'foobarbazzjazz')); $this->assertTrue(file_exists($testfile)); $this->assertEquals('foobarbazzjazz', file_get_contents($testfile)); - $this->assertEquals(0666 & ~umask(), fileperms($testfile) & 0777); + $this->assertEquals(0666 & ~Rhymix\Framework\Storage::getUmask(), fileperms($testfile) & 0777); // Append test $this->assertTrue(Rhymix\Framework\Storage::write($testfile, 'rhymix', 'a', 0666));