Merge pull request #13893 from owncloud/create-cachedir-on-user

Only make sure the cache directory exists when we use it
This commit is contained in:
Morris Jobke 2015-03-11 16:49:24 +01:00
commit 82254ad5ef
2 changed files with 28 additions and 26 deletions

View File

@ -9,22 +9,31 @@
namespace OC\Cache; namespace OC\Cache;
use OC\Files\Filesystem;
use OC\Files\View;
class File { class File {
protected $storage; protected $storage;
/** /**
* Returns the cache storage for the logged in user * Returns the cache storage for the logged in user
*
* @return \OC\Files\View cache storage * @return \OC\Files\View cache storage
*/ */
protected function getStorage() { protected function getStorage() {
if (isset($this->storage)) { if (isset($this->storage)) {
return $this->storage; return $this->storage;
} }
if(\OC_User::isLoggedIn()) { if (\OC_User::isLoggedIn()) {
\OC\Files\Filesystem::initMountPoints(\OC_User::getUser()); $rootView = new View();
$this->storage = new \OC\Files\View('/' . \OC_User::getUser() . '/cache'); $user = \OC::$server->getUserSession()->getUser();
Filesystem::initMountPoints($user->getUID());
if (!$rootView->file_exists('/' . $user->getUID() . '/cache')) {
$rootView->mkdir('/' . $user->getUID() . '/cache');
}
$this->storage = new View('/' . $user->getUID() . '/cache');
return $this->storage; return $this->storage;
}else{ } else {
\OC_Log::write('core', 'Can\'t get cache storage, user not logged in', \OC_Log::ERROR); \OC_Log::write('core', 'Can\'t get cache storage, user not logged in', \OC_Log::ERROR);
throw new \OC\ForbiddenException('Can\t get cache storage, user not logged in'); throw new \OC\ForbiddenException('Can\t get cache storage, user not logged in');
} }
@ -66,7 +75,7 @@ class File {
/** /**
* @param string $key * @param string $key
*/ */
public function set($key, $value, $ttl=0) { public function set($key, $value, $ttl = 0) {
$storage = $this->getStorage(); $storage = $this->getStorage();
$result = false; $result = false;
$proxyStatus = \OC_FileProxy::$enabled; $proxyStatus = \OC_FileProxy::$enabled;
@ -94,20 +103,20 @@ class File {
*/ */
public function remove($key) { public function remove($key) {
$storage = $this->getStorage(); $storage = $this->getStorage();
if(!$storage) { if (!$storage) {
return false; return false;
} }
return $storage->unlink($key); return $storage->unlink($key);
} }
public function clear($prefix='') { public function clear($prefix = '') {
$storage = $this->getStorage(); $storage = $this->getStorage();
if($storage and $storage->is_dir('/')) { if ($storage and $storage->is_dir('/')) {
$dh=$storage->opendir('/'); $dh = $storage->opendir('/');
if(is_resource($dh)) { if (is_resource($dh)) {
while (($file = readdir($dh)) !== false) { while (($file = readdir($dh)) !== false) {
if($file!='.' and $file!='..' and ($prefix==='' || strpos($file, $prefix) === 0)) { if ($file != '.' and $file != '..' and ($prefix === '' || strpos($file, $prefix) === 0)) {
$storage->unlink('/'.$file); $storage->unlink('/' . $file);
} }
} }
} }
@ -117,17 +126,17 @@ class File {
public function gc() { public function gc() {
$storage = $this->getStorage(); $storage = $this->getStorage();
if($storage and $storage->is_dir('/')) { if ($storage and $storage->is_dir('/')) {
$now = time(); $now = time();
$dh=$storage->opendir('/'); $dh = $storage->opendir('/');
if(!is_resource($dh)) { if (!is_resource($dh)) {
return null; return null;
} }
while (($file = readdir($dh)) !== false) { while (($file = readdir($dh)) !== false) {
if($file!='.' and $file!='..') { if ($file != '.' and $file != '..') {
$mtime = $storage->filemtime('/'.$file); $mtime = $storage->filemtime('/' . $file);
if ($mtime < $now) { if ($mtime < $now) {
$storage->unlink('/'.$file); $storage->unlink('/' . $file);
} }
} }
} }

View File

@ -395,14 +395,7 @@ class Filesystem {
*/ */
private static function mountCacheDir($user) { private static function mountCacheDir($user) {
$cacheBaseDir = \OC_Config::getValue('cache_path', ''); $cacheBaseDir = \OC_Config::getValue('cache_path', '');
if ($cacheBaseDir === '') { if ($cacheBaseDir !== '') {
// use local cache dir relative to the user's home
$subdir = 'cache';
$view = new \OC\Files\View('/' . $user);
if(!$view->file_exists($subdir)) {
$view->mkdir($subdir);
}
} else {
$cacheDir = rtrim($cacheBaseDir, '/') . '/' . $user; $cacheDir = rtrim($cacheBaseDir, '/') . '/' . $user;
if (!file_exists($cacheDir)) { if (!file_exists($cacheDir)) {
mkdir($cacheDir, 0770, true); mkdir($cacheDir, 0770, true);