Skip to content

Commit dd88a82

Browse files
committed
Merge pull request #840 from google/add-username-to-cache
use hashed username in cache dir
2 parents e2fae18 + f4727b7 commit dd88a82

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

src/Google/Cache/File.php

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,19 +151,38 @@ private function getCacheDir($file, $forWrite)
151151
// use the first 2 characters of the hash as a directory prefix
152152
// this should prevent slowdowns due to huge directory listings
153153
// and thus give some basic amount of scalability
154-
$dirHash = substr(md5($file), 0, 2);
154+
$fileHash = substr(md5($file), 0, 2);
155+
$userHash = md5(get_current_user());
156+
$dirHash = $userHash . DIRECTORY_SEPARATOR . $fileHash;
157+
155158
// trim the directory separator from the path to prevent double separators
156-
$storageDir = rtrim($this->path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $dirHash;
157-
if ($forWrite && ! is_dir($storageDir)) {
158-
if (!mkdir($storageDir, 0777, true)) {
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
176+
if (!mkdir($storageDir, 0700, true)) {
159177
$this->log(
160178
'error',
161179
'File cache creation failed',
162180
array('dir' => $storageDir)
163181
);
164-
throw new Google_Cache_Exception("Could not create storage directory: $storageDir");
182+
throw new Google_Cache_Exception("Could not create cache directory: $storageDir");
165183
}
166184
}
185+
167186
return $storageDir;
168187
}
169188

@@ -199,7 +218,7 @@ private function acquireLock($type, $storageFile)
199218
return false;
200219
}
201220
if ($type == LOCK_EX) {
202-
chmod($storageFile, 0666 & ~umask());
221+
chmod($storageFile, 0600);
203222
}
204223
$count = 0;
205224
while (!flock($this->fh, $type | LOCK_NB)) {

tests/Google/CacheTest.php

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

43-
public function testFileDirectoryPermissions()
43+
public function testBaseCacheDirectoryPermissions()
4444
{
4545
$dir = sys_get_temp_dir() . '/google-api-php-client/tests/' . rand();
4646
$cache = new Google_Cache_File($dir);
@@ -54,32 +54,32 @@ public function testFileDirectoryPermissions()
5454
$this->assertEquals(0777 & ~umask(), $stat['mode'] & 0777);
5555
}
5656

57-
public function testFileWithDefaultMask()
57+
public function testCacheDirectoryPermissions()
5858
{
59-
$dir = sys_get_temp_dir() . '/google-api-php-client/tests/';
59+
$dir = sys_get_temp_dir() . '/google-api-php-client/tests/' . rand();
6060
$cache = new Google_Cache_File($dir);
6161
$cache->set('foo', 'bar');
6262

6363
$method = new ReflectionMethod($cache, 'getWriteableCacheFile');
6464
$method->setAccessible(true);
6565
$filename = $method->invoke($cache, 'foo');
66-
$stat = stat($filename);
66+
$stat = stat(dirname($filename));
6767

68-
$this->assertEquals(0666 & ~umask(), $stat['mode'] & 0777);
68+
$this->assertEquals(0700, $stat['mode'] & 0777);
6969
}
7070

71-
public function testFileWithCustomMask()
71+
public function testCacheFilePermissions()
7272
{
7373
$dir = sys_get_temp_dir() . '/google-api-php-client/tests/';
74-
$cache = new Google_Cache_File($dir, null);
74+
$cache = new Google_Cache_File($dir);
7575
$cache->set('foo', 'bar');
7676

7777
$method = new ReflectionMethod($cache, 'getWriteableCacheFile');
7878
$method->setAccessible(true);
7979
$filename = $method->invoke($cache, 'foo');
8080
$stat = stat($filename);
8181

82-
$this->assertEquals(0666 & ~umask(), $stat['mode'] & 0777);
82+
$this->assertEquals(0600, $stat['mode'] & 0777);
8383
}
8484

8585
public function testNull()

0 commit comments

Comments
 (0)