Skip to content

Commit e2fae18

Browse files
committed
Merge pull request #839 from google/file-cache-umask
File cache umask
2 parents bc39060 + 587ff7f commit e2fae18

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

src/Google/Cache/File.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ private function getCacheDir($file, $forWrite)
155155
// trim the directory separator from the path to prevent double separators
156156
$storageDir = rtrim($this->path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $dirHash;
157157
if ($forWrite && ! is_dir($storageDir)) {
158-
if (! mkdir($storageDir, 0700, true)) {
158+
if (!mkdir($storageDir, 0777, true)) {
159159
$this->log(
160160
'error',
161161
'File cache creation failed',
@@ -199,7 +199,7 @@ private function acquireLock($type, $storageFile)
199199
return false;
200200
}
201201
if ($type == LOCK_EX) {
202-
chmod($storageFile, 0600);
202+
chmod($storageFile, 0666 & ~umask());
203203
}
204204
$count = 0;
205205
while (!flock($this->fh, $type | LOCK_NB)) {

tests/Google/CacheTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,48 @@ 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+
57+
public function testFileWithDefaultMask()
58+
{
59+
$dir = sys_get_temp_dir() . '/google-api-php-client/tests/';
60+
$cache = new Google_Cache_File($dir);
61+
$cache->set('foo', 'bar');
62+
63+
$method = new ReflectionMethod($cache, 'getWriteableCacheFile');
64+
$method->setAccessible(true);
65+
$filename = $method->invoke($cache, 'foo');
66+
$stat = stat($filename);
67+
68+
$this->assertEquals(0666 & ~umask(), $stat['mode'] & 0777);
69+
}
70+
71+
public function testFileWithCustomMask()
72+
{
73+
$dir = sys_get_temp_dir() . '/google-api-php-client/tests/';
74+
$cache = new Google_Cache_File($dir, null);
75+
$cache->set('foo', 'bar');
76+
77+
$method = new ReflectionMethod($cache, 'getWriteableCacheFile');
78+
$method->setAccessible(true);
79+
$filename = $method->invoke($cache, 'foo');
80+
$stat = stat($filename);
81+
82+
$this->assertEquals(0666 & ~umask(), $stat['mode'] & 0777);
83+
}
84+
4385
public function testNull()
4486
{
4587
$cache = new Google_Cache_Null();

0 commit comments

Comments
 (0)