From 4ef26157880f5cd5d5bd27abe0a6991d7c8a415a Mon Sep 17 00:00:00 2001 From: Victor Dubiniuk Date: Thu, 30 Jul 2015 22:31:18 +0300 Subject: [PATCH] Enhance trashbin expiration settings --- apps/files_trashbin/appinfo/application.php | 15 +- apps/files_trashbin/lib/expiration.php | 153 ++++++++++++++++++++ apps/files_trashbin/lib/trashbin.php | 31 ++-- apps/files_trashbin/tests/expiration.php | 145 +++++++++++++++++++ apps/files_trashbin/tests/trashbin.php | 7 +- config/config.sample.php | 36 +++-- 6 files changed, 352 insertions(+), 35 deletions(-) create mode 100644 apps/files_trashbin/lib/expiration.php create mode 100644 apps/files_trashbin/tests/expiration.php diff --git a/apps/files_trashbin/appinfo/application.php b/apps/files_trashbin/appinfo/application.php index 8d76d40f63..08ab7cd5c1 100644 --- a/apps/files_trashbin/appinfo/application.php +++ b/apps/files_trashbin/appinfo/application.php @@ -1,6 +1,7 @@ + * @author Victor Dubiniuk * * @copyright Copyright (c) 2015, ownCloud, Inc. * @license AGPL-3.0 @@ -22,16 +23,26 @@ namespace OCA\Files_Trashbin\AppInfo; use OCP\AppFramework\App; +use OCA\Files_Trashbin\Expiration; class Application extends App { - public function __construct(array $urlParams = array()) { + public function __construct (array $urlParams = []) { parent::__construct('files_trashbin', $urlParams); $container = $this->getContainer(); - /* * Register capabilities */ $container->registerCapability('OCA\Files_Trashbin\Capabilities'); + + /* + * Register expiration + */ + $container->registerService('Expiration', function($c) { + return new Expiration( + $c->query('ServerContainer')->getConfig(), + $c->query('OCP\AppFramework\Utility\ITimeFactory') + ); + }); } } diff --git a/apps/files_trashbin/lib/expiration.php b/apps/files_trashbin/lib/expiration.php new file mode 100644 index 0000000000..3b57c067ae --- /dev/null +++ b/apps/files_trashbin/lib/expiration.php @@ -0,0 +1,153 @@ + + * + * @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 + * + */ + +namespace OCA\Files_Trashbin; + +use \OCP\IConfig; +use \OCP\AppFramework\Utility\ITimeFactory; + +class Expiration +{ + + // how long do we keep files in the trash bin if no other value is defined in the config file (unit: days) + const DEFAULT_RETENTION_OBLIGATION = 30; + const NO_OBLIGATION = -1; + + /** @var ITimeFactory */ + private $timeFactory; + + /** @var string */ + private $retentionObligation; + + /** @var int */ + private $minAge; + + /** @var int */ + private $maxAge; + + /** @var bool */ + private $canPurgeToSaveSpace; + + public function __construct(IConfig $config,ITimeFactory $timeFactory){ + $this->timeFactory = $timeFactory; + $this->retentionObligation = $config->getValue('trashbin_retention_obligation', 'auto'); + + if ($this->retentionObligation !== 'disabled') { + $this->parseRetentionObligation(); + } + } + + /** + * Is trashbin expiration enabled + * @return bool + */ + public function isEnabled(){ + return $this->retentionObligation !== 'disabled'; + } + + /** + * Check if given timestamp in expiration range + * @param int $timestamp + * @param bool $quotaExceeded + * @return bool + */ + public function isExpired($timestamp, $quotaExceeded = false){ + // No expiration if disabled + if (!$this->isEnabled()) { + return false; + } + + // Purge to save space (if allowed) + if ($quotaExceeded && $this->canPurgeToSaveSpace) { + return true; + } + + $time = $this->timeFactory->getTime(); + // Never expire dates in future e.g. misconfiguration or negative time + // adjustment + if ($time<$timestamp) { + return false; + } + + // Purge as too old + if ($this->maxAge !== self::NO_OBLIGATION) { + $maxTimestamp = $time - ($this->maxAge * 86400); + $isOlderThanMax = $timestamp < $maxTimestamp; + } else { + $isOlderThanMax = false; + } + + if ($this->minAge !== self::NO_OBLIGATION) { + // older than Min obligation and we are running out of quota? + $minTimestamp = $time - ($this->minAge * 86400); + $isMinReached = ($timestamp < $minTimestamp) && $quotaExceeded; + } else { + $isMinReached = false; + } + + return $isOlderThanMax || $isMinReached; + } + + private function parseRetentionObligation(){ + $splitValues = explode(',', $this->retentionObligation); + if (!isset($splitValues[0])) { + $minValue = self::DEFAULT_RETENTION_OBLIGATION; + } else { + $minValue = trim($splitValues[0]); + } + + if (!isset($splitValues[1]) && $minValue === 'auto') { + $maxValue = 'auto'; + } elseif (!isset($splitValues[1])) { + $maxValue = self::DEFAULT_RETENTION_OBLIGATION; + } else { + $maxValue = trim($splitValues[1]); + } + + if ($minValue === 'auto' && $maxValue === 'auto') { + // Default: Keep for 30 days but delete anytime if space needed + $this->minAge = self::DEFAULT_RETENTION_OBLIGATION; + $this->maxAge = self::NO_OBLIGATION; + $this->canPurgeToSaveSpace = true; + } elseif ($minValue !== 'auto' && $maxValue === 'auto') { + // Keep for X days but delete anytime if space needed + $this->minAge = intval($minValue); + $this->maxAge = self::NO_OBLIGATION; + $this->canPurgeToSaveSpace = true; + } elseif ($minValue === 'auto' && $maxValue !== 'auto') { + // Delete anytime if space needed, Delete all older than max automatically + $this->minAge = self::NO_OBLIGATION; + $this->maxAge = intval($maxValue); + $this->canPurgeToSaveSpace = true; + } elseif ($minValue !== 'auto' && $maxValue !== 'auto') { + // Delete all older than max OR older than min if space needed + + // Max < Min as per https://github.com/owncloud/core/issues/16300 + if ($maxValue < $minValue) { + $maxValue = $minValue; + } + + $this->minAge = intval($minValue); + $this->maxAge = intval($maxValue); + $this->canPurgeToSaveSpace = false; + } + } +} diff --git a/apps/files_trashbin/lib/trashbin.php b/apps/files_trashbin/lib/trashbin.php index 8b666c56c6..2719eece2a 100644 --- a/apps/files_trashbin/lib/trashbin.php +++ b/apps/files_trashbin/lib/trashbin.php @@ -38,12 +38,10 @@ namespace OCA\Files_Trashbin; use OC\Files\Filesystem; use OC\Files\View; +use OCA\Files_Trashbin\AppInfo\Application; use OCA\Files_Trashbin\Command\Expire; class Trashbin { - // how long do we keep files in the trash bin if no other value is defined in the config file (unit: days) - - const DEFAULT_RETENTION_OBLIGATION = 30; // unit: percentage; 50% of available disk space/quota const DEFAULTMAXSIZE = 50; @@ -631,14 +629,10 @@ class Trashbin { $availableSpace = self::calculateFreeSpace($trashBinSize, $user); $size = 0; - $retention_obligation = \OC_Config::getValue('trashbin_retention_obligation', self::DEFAULT_RETENTION_OBLIGATION); - - $limit = time() - ($retention_obligation * 86400); - $dirContent = Helper::getTrashFiles('/', $user, 'mtime'); // delete all files older then $retention_obligation - list($delSize, $count) = self::deleteExpiredFiles($dirContent, $user, $limit, $retention_obligation); + list($delSize, $count) = self::deleteExpiredFiles($dirContent, $user); $size += $delSize; $availableSpace += $size; @@ -652,11 +646,11 @@ class Trashbin { */ private static function scheduleExpire($trashBinSize, $user) { // let the admin disable auto expire - $autoExpire = \OC_Config::getValue('trashbin_auto_expire', true); - if ($autoExpire === false) { - return; + $application = new Application(); + $expiration = $application->getContainer()->query('Expiration'); + if ($expiration->isEnabled()) { + \OC::$server->getCommandBus()->push(new Expire($user, $trashBinSize)); } - \OC::$server->getCommandBus()->push(new Expire($user, $trashBinSize)); } /** @@ -669,11 +663,13 @@ class Trashbin { * @return int size of deleted files */ protected static function deleteFiles($files, $user, $availableSpace) { + $application = new Application(); + $expiration = $application->getContainer()->query('Expiration'); $size = 0; if ($availableSpace < 0) { foreach ($files as $file) { - if ($availableSpace < 0) { + if ($availableSpace < 0 && $expiration->isExpired($file['mtime'], true)) { $tmp = self::delete($file['name'], $user, $file['mtime']); \OCP\Util::writeLog('files_trashbin', 'remove "' . $file['name'] . '" (' . $tmp . 'B) to meet the limit of trash bin size (50% of available quota)', \OCP\Util::INFO); $availableSpace += $tmp; @@ -691,20 +687,19 @@ class Trashbin { * * @param array $files list of files sorted by mtime * @param string $user - * @param int $limit files older then limit should be deleted - * @param int $retention_obligation max age of file in days * @return array size of deleted files and number of deleted files */ - protected static function deleteExpiredFiles($files, $user, $limit, $retention_obligation) { + protected static function deleteExpiredFiles($files, $user) { + $application = new Application(); + $expiration = $application->getContainer()->query('Expiration'); $size = 0; $count = 0; foreach ($files as $file) { $timestamp = $file['mtime']; $filename = $file['name']; - if ($timestamp <= $limit) { + if ($expiration->isExpired($timestamp)) { $count++; $size += self::delete($filename, $user, $timestamp); - \OCP\Util::writeLog('files_trashbin', 'remove "' . $filename . '" from trash bin because it is older than ' . $retention_obligation, \OCP\Util::INFO); } else { break; } diff --git a/apps/files_trashbin/tests/expiration.php b/apps/files_trashbin/tests/expiration.php new file mode 100644 index 0000000000..68fd32c911 --- /dev/null +++ b/apps/files_trashbin/tests/expiration.php @@ -0,0 +1,145 @@ + + * + * @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 + * + */ + +use \OCA\Files_Trashbin\Expiration; + +class Expiration_Test extends \PHPUnit_Framework_TestCase { + const SECONDS_PER_DAY = 86400; //60*60*24 + + public function expirationData(){ + $today = 100*self::SECONDS_PER_DAY; + $back10Days = (100-10)*self::SECONDS_PER_DAY; + $back20Days = (100-20)*self::SECONDS_PER_DAY; + $back30Days = (100-30)*self::SECONDS_PER_DAY; + $back35Days = (100-35)*self::SECONDS_PER_DAY; + + // it should never happen, but who knows :/ + $ahead100Days = (100+100)*self::SECONDS_PER_DAY; + + return [ + // Expiration is disabled - always should return false + [ 'disabled', $today, $back10Days, false, false], + [ 'disabled', $today, $back10Days, true, false], + [ 'disabled', $today, $ahead100Days, true, false], + + // Default: expire in 30 days or earlier when quota requirements are met + [ 'auto', $today, $back10Days, false, false], + [ 'auto', $today, $back35Days, false, false], + [ 'auto', $today, $back10Days, true, true], + [ 'auto', $today, $back35Days, true, true], + [ 'auto', $today, $ahead100Days, true, true], + + // The same with 'auto' + [ 'auto, auto', $today, $back10Days, false, false], + [ 'auto, auto', $today, $back35Days, false, false], + [ 'auto, auto', $today, $back10Days, true, true], + [ 'auto, auto', $today, $back35Days, true, true], + + // Keep for 15 days but expire anytime if space needed + [ '15, auto', $today, $back10Days, false, false], + [ '15, auto', $today, $back20Days, false, false], + [ '15, auto', $today, $back10Days, true, true], + [ '15, auto', $today, $back20Days, true, true], + [ '15, auto', $today, $ahead100Days, true, true], + + // Expire anytime if space needed, Expire all older than max + [ 'auto, 15', $today, $back10Days, false, false], + [ 'auto, 15', $today, $back20Days, false, true], + [ 'auto, 15', $today, $back10Days, true, true], + [ 'auto, 15', $today, $back20Days, true, true], + [ 'auto, 15', $today, $ahead100Days, true, true], + + // Expire all older than max OR older than min if space needed + [ '15, 25', $today, $back10Days, false, false], + [ '15, 25', $today, $back20Days, false, false], + [ '15, 25', $today, $back30Days, false, true], + [ '15, 25', $today, $back10Days, false, false], + [ '15, 25', $today, $back20Days, true, true], + [ '15, 25', $today, $back30Days, true, true], + [ '15, 25', $today, $ahead100Days, true, false], + + // Expire all older than max OR older than min if space needed + // MaxgetMockBuilder('\OCP\IConfig') + ->disableOriginalConstructor() + ->setMethods( + [ + 'getValue', + 'setSystemValues', + 'setSystemValue', + 'getSystemValue', + 'deleteSystemValue', + 'getAppKeys', + 'setAppValue', + 'getAppValue', + 'deleteAppValue', + 'deleteAppValues', + 'setUserValue', + 'getUserValue', + 'getUserValueForUsers', + 'getUserKeys', + 'deleteUserValue', + 'deleteAllUserValues', + 'deleteAppFromAllUsers', + 'getUsersForUserValue' + ] + ) + ->getMock() + ; + $mockedConfig->expects($this->any())->method('getValue')->will( + $this->returnValue($retentionObligation) + ); + + $mockedTimeFactory = $this->getMockBuilder('\OCP\AppFramework\Utility\ITimeFactory') + ->disableOriginalConstructor() + ->setMethods(['getTime']) + ->getMock() + ; + $mockedTimeFactory->expects($this->any())->method('getTime')->will( + $this->returnValue($timeNow) + ); + + $expiration = new Expiration($mockedConfig, $mockedTimeFactory); + $actualResult = $expiration->isExpired($timestamp, $quotaExceeded); + + $this->assertEquals($expectedResult, $actualResult); + } +} diff --git a/apps/files_trashbin/tests/trashbin.php b/apps/files_trashbin/tests/trashbin.php index 299c45b19a..0bdca60f7e 100644 --- a/apps/files_trashbin/tests/trashbin.php +++ b/apps/files_trashbin/tests/trashbin.php @@ -38,7 +38,6 @@ class Test_Trashbin extends \Test\TestCase { private $trashRoot2; private static $rememberRetentionObligation; - private static $rememberAutoExpire; /** * @var bool @@ -71,11 +70,8 @@ class Test_Trashbin extends \Test\TestCase { \OC_App::disable('encryption'); //configure trashbin - self::$rememberRetentionObligation = \OC_Config::getValue('trashbin_retention_obligation', Files_Trashbin\Trashbin::DEFAULT_RETENTION_OBLIGATION); + self::$rememberRetentionObligation = \OC_Config::getValue('trashbin_retention_obligation', Files_Trashbin\Expiration::DEFAULT_RETENTION_OBLIGATION); \OC_Config::setValue('trashbin_retention_obligation', 2); - self::$rememberAutoExpire = \OC_Config::getValue('trashbin_auto_expire', true); - \OC_Config::setValue('trashbin_auto_expire', true); - // register hooks Files_Trashbin\Trashbin::registerHooks(); @@ -92,7 +88,6 @@ class Test_Trashbin extends \Test\TestCase { \OC_User::deleteUser(self::TEST_TRASHBIN_USER1); \OC_Config::setValue('trashbin_retention_obligation', self::$rememberRetentionObligation); - \OC_Config::setValue('trashbin_auto_expire', self::$rememberAutoExpire); \OC_Hook::clear(); diff --git a/config/config.sample.php b/config/config.sample.php index 3b5632087f..7becf3c3dc 100644 --- a/config/config.sample.php +++ b/config/config.sample.php @@ -399,16 +399,34 @@ $CONFIG = array( */ /** - * When the trash bin app is enabled (default), this is the number of days a - * file will be kept in the trash bin. Default is 30 days. + * If the trash bin app is enabled (default), this setting defines the policy + * for when files and folders in the trash bin will be permanently deleted. + * The app allows for two settings, a minimum time for trash bin retention, + * and a maximum time for trash bin retention. + * Minimum time is the number of days a file will be kept, after which it + * may be deleted. Maximum time is the number of days at which it is guaranteed + * to be deleted. + * Both minimum and maximum times can be set together to explicitly define + * file and folder deletion. For migration purposes, this setting is installed + * initially set to "auto", which is equivalent to the default setting in + * ownCloud 8.1 and before. + * + * Available values: + * ``auto`` default setting. keeps files and folders in the trash bin + * for 30 days and automatically deletes anytime after that + * if space is needed (note: files may not be deleted if space + * is not needed). + * ``D, auto`` keeps files and folders in the trash bin for D+ days, + * delete anytime if space needed (note: files may not be deleted + * if space is not needed) + * * ``auto, D`` delete all files in the trash bin that are older than D days + * automatically, delete other files anytime if space needed + * * ``D1, D2`` keep files and folders the in trash bin for at least D1 days + * and delete when exceeds D2 days + * ``disabled`` trash bin auto clean disabled, files and folders will be + * kept forever */ -'trashbin_retention_obligation' => 30, - -/** - * Disable or enable auto-expiration for the trash bin. By default - * auto-expiration is enabled. - */ -'trashbin_auto_expire' => true, +'trashbin_retention_obligation' => 'auto', /**