Skip to content

Commit f4727b7

Browse files
committed
addresses root cache dir permissions issue
1 parent 7324200 commit f4727b7

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

src/Google/Cache/File.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,20 +153,36 @@ private function getCacheDir($file, $forWrite)
153153
// and thus give some basic amount of scalability
154154
$fileHash = substr(md5($file), 0, 2);
155155
$userHash = md5(get_current_user());
156-
$dirHash = $fileHash . DIRECTORY_SEPARATOR . $userHash;
156+
$dirHash = $userHash . DIRECTORY_SEPARATOR . $fileHash;
157157

158158
// trim the directory separator from the path to prevent double separators
159-
$storageDir = rtrim($this->path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $dirHash;
160-
if ($forWrite && ! is_dir($storageDir)) {
159+
$rootCacheDir = rtrim($this->path, DIRECTORY_SEPARATOR);
160+
$storageDir = $rootCacheDir . DIRECTORY_SEPARATOR . $dirHash;
161+
162+
if ($forWrite && !is_dir($storageDir)) {
163+
// create root dir
164+
if (!is_dir($rootCacheDir)) {
165+
if (!mkdir($rootCacheDir, 0777, true)) {
166+
$this->log(
167+
'error',
168+
'File cache creation failed',
169+
array('dir' => $rootCacheDir)
170+
);
171+
throw new Google_Cache_Exception("Could not create cache directory: $rootCacheDir");
172+
}
173+
}
174+
175+
// create dir for file
161176
if (!mkdir($storageDir, 0700, true)) {
162177
$this->log(
163178
'error',
164179
'File cache creation failed',
165180
array('dir' => $storageDir)
166181
);
167-
throw new Google_Cache_Exception("Could not create storage directory: $storageDir");
182+
throw new Google_Cache_Exception("Could not create cache directory: $storageDir");
168183
}
169184
}
185+
170186
return $storageDir;
171187
}
172188

tests/Google/CacheTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function testFileWithTrailingSlash()
4040
$this->getSetDelete($cache);
4141
}
4242

43-
public function testCacheDirectoryPermissions()
43+
public function testBaseCacheDirectoryPermissions()
4444
{
4545
$dir = sys_get_temp_dir() . '/google-api-php-client/tests/' . rand();
4646
$cache = new Google_Cache_File($dir);
@@ -51,6 +51,20 @@ public function testCacheDirectoryPermissions()
5151
$filename = $method->invoke($cache, 'foo');
5252
$stat = stat($dir);
5353

54+
$this->assertEquals(0777 & ~umask(), $stat['mode'] & 0777);
55+
}
56+
57+
public function testCacheDirectoryPermissions()
58+
{
59+
$dir = sys_get_temp_dir() . '/google-api-php-client/tests/' . rand();
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(dirname($filename));
67+
5468
$this->assertEquals(0700, $stat['mode'] & 0777);
5569
}
5670

0 commit comments

Comments
 (0)