2013-04-20 21:27:46 +00:00
|
|
|
<?php
|
2015-03-26 10:44:34 +00:00
|
|
|
/**
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Robin Appelman <icewind@owncloud.com>
|
|
|
|
* @author Robin McCorkell <rmccorkell@karoshi.org.uk>
|
|
|
|
* @author Thomas Tanghus <thomas@tanghus.net>
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
|
|
|
*/
|
2013-09-17 15:46:33 +00:00
|
|
|
namespace OC\Cache;
|
2013-04-20 21:27:46 +00:00
|
|
|
|
2015-02-25 13:13:58 +00:00
|
|
|
use OC\BackgroundJob\Job;
|
|
|
|
use OCP\IConfig;
|
|
|
|
|
|
|
|
class FileGlobalGC extends Job {
|
2015-03-18 16:19:04 +00:00
|
|
|
// only do cleanup every 5 minutes
|
|
|
|
const CLEANUP_TTL_SEC = 300;
|
|
|
|
|
2015-02-25 13:13:58 +00:00
|
|
|
public function run($argument) {
|
|
|
|
$this->gc(\OC::$server->getConfig(), $this->getCacheDir());
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getCacheDir() {
|
|
|
|
return get_temp_dir() . '/owncloud-' . \OC_Util::getInstanceId() . '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $cacheDir
|
|
|
|
* @param int $now
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getExpiredPaths($cacheDir, $now) {
|
|
|
|
$files = scandir($cacheDir);
|
|
|
|
$files = array_filter($files, function ($file) {
|
|
|
|
return $file != '.' and $file != '..';
|
|
|
|
});
|
|
|
|
$paths = array_map(function ($file) use ($cacheDir) {
|
|
|
|
return $cacheDir . $file;
|
|
|
|
}, $files);
|
|
|
|
return array_values(array_filter($paths, function ($path) use ($now) {
|
|
|
|
return is_file($path) and (filemtime($path) < $now);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \OCP\IConfig $config
|
|
|
|
* @param string $cacheDir
|
|
|
|
*/
|
|
|
|
public function gc(IConfig $config, $cacheDir) {
|
|
|
|
$lastRun = $config->getAppValue('core', 'global_cache_gc_lastrun', 0);
|
|
|
|
$now = time();
|
2015-03-18 16:19:04 +00:00
|
|
|
if (($now - $lastRun) < self::CLEANUP_TTL_SEC) {
|
2015-02-25 13:13:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
$config->setAppValue('core', 'global_cache_gc_lastrun', $now);
|
|
|
|
if (!is_dir($cacheDir)) {
|
|
|
|
return;
|
|
|
|
}
|
2015-02-26 15:42:41 +00:00
|
|
|
$paths = $this->getExpiredPaths($cacheDir, $now);
|
2015-03-18 00:07:18 +00:00
|
|
|
array_walk($paths, function($file) {
|
2015-04-01 09:28:53 +00:00
|
|
|
if (file_exists($file)) {
|
|
|
|
unlink($file);
|
|
|
|
}
|
2015-03-18 00:07:18 +00:00
|
|
|
});
|
2013-04-20 21:27:46 +00:00
|
|
|
}
|
|
|
|
}
|