Skip to content

Commit 587ff7f

Browse files
committed
uses umask function instead of custom umask
1 parent 444e687 commit 587ff7f

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

src/Google/Cache/File.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,17 @@ class Google_Cache_File implements CacheInterface
3030
{
3131
const MAX_LOCK_RETRIES = 10;
3232
private $path;
33-
private $umask;
3433
private $fh;
3534

3635
/**
3736
* @var use Psr\Log\LoggerInterface logger
3837
*/
3938
private $logger;
4039

41-
public function __construct($path, LoggerInterface $logger = null, $umask = 077)
40+
public function __construct($path, LoggerInterface $logger = null)
4241
{
4342
$this->path = $path;
4443
$this->logger = $logger;
45-
$this->umask = $umask;
4644
}
4745

4846
public function get($key, $expiration = false)
@@ -157,7 +155,7 @@ private function getCacheDir($file, $forWrite)
157155
// trim the directory separator from the path to prevent double separators
158156
$storageDir = rtrim($this->path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $dirHash;
159157
if ($forWrite && ! is_dir($storageDir)) {
160-
if (! mkdir($storageDir, 0770 & ~$this->umask, true)) {
158+
if (!mkdir($storageDir, 0777, true)) {
161159
$this->log(
162160
'error',
163161
'File cache creation failed',
@@ -201,7 +199,7 @@ private function acquireLock($type, $storageFile)
201199
return false;
202200
}
203201
if ($type == LOCK_EX) {
204-
chmod($storageFile, 0660 & ~$this->umask);
202+
chmod($storageFile, 0666 & ~umask());
205203
}
206204
$count = 0;
207205
while (!flock($this->fh, $type | LOCK_NB)) {

tests/Google/CacheTest.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,20 @@ public function testFileWithTrailingSlash()
4040
$this->getSetDelete($cache);
4141
}
4242

43+
public function testFileDirectoryPermissions()
44+
{
45+
$dir = sys_get_temp_dir() . '/google-api-php-client/tests/' . rand();
46+
$cache = new Google_Cache_File($dir);
47+
$cache->set('foo', 'bar');
48+
49+
$method = new ReflectionMethod($cache, 'getWriteableCacheFile');
50+
$method->setAccessible(true);
51+
$filename = $method->invoke($cache, 'foo');
52+
$stat = stat($dir);
53+
54+
$this->assertEquals(0777 & ~umask(), $stat['mode'] & 0777);
55+
}
56+
4357
public function testFileWithDefaultMask()
4458
{
4559
$dir = sys_get_temp_dir() . '/google-api-php-client/tests/';
@@ -51,21 +65,21 @@ public function testFileWithDefaultMask()
5165
$filename = $method->invoke($cache, 'foo');
5266
$stat = stat($filename);
5367

54-
$this->assertEquals($stat['mode'] & 0777, 0600);
68+
$this->assertEquals(0666 & ~umask(), $stat['mode'] & 0777);
5569
}
5670

5771
public function testFileWithCustomMask()
5872
{
5973
$dir = sys_get_temp_dir() . '/google-api-php-client/tests/';
60-
$cache = new Google_Cache_File($dir, null, 07);
74+
$cache = new Google_Cache_File($dir, null);
6175
$cache->set('foo', 'bar');
6276

6377
$method = new ReflectionMethod($cache, 'getWriteableCacheFile');
6478
$method->setAccessible(true);
6579
$filename = $method->invoke($cache, 'foo');
6680
$stat = stat($filename);
6781

68-
$this->assertEquals($stat['mode'] & 0777, 0660);
82+
$this->assertEquals(0666 & ~umask(), $stat['mode'] & 0777);
6983
}
7084

7185
public function testNull()

0 commit comments

Comments
 (0)