@@ -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
0 commit comments