Merge pull request #1823 from owncloud/update-expire-function

Update expire function
This commit is contained in:
Björn Schießle 2013-02-25 05:30:21 -08:00
commit 0f763f4508
6 changed files with 210 additions and 80 deletions

View File

@ -18,7 +18,7 @@
<type>text</type> <type>text</type>
<default></default> <default></default>
<notnull>true</notnull> <notnull>true</notnull>
<length>50</length> <length>250</length>
</field> </field>
<field> <field>
@ -26,7 +26,7 @@
<type>text</type> <type>text</type>
<default></default> <default></default>
<notnull>true</notnull> <notnull>true</notnull>
<length>50</length> <length>64</length>
</field> </field>
<field> <field>
@ -42,7 +42,7 @@
<type>text</type> <type>text</type>
<default></default> <default></default>
<notnull>true</notnull> <notnull>true</notnull>
<length>200</length> <length>512</length>
</field> </field>
<field> <field>
@ -89,4 +89,30 @@
</table> </table>
<table>
<name>*dbprefix*files_trashsize</name>
<declaration>
<field>
<name>user</name>
<type>text</type>
<default></default>
<notnull>true</notnull>
<length>64</length>
</field>
<field>
<name>size</name>
<type>text</type>
<default></default>
<notnull>true</notnull>
<length>50</length>
</field>
</declaration>
</table>
</database> </database>

View File

@ -1 +1 @@
0.2 0.3

View File

@ -57,8 +57,8 @@ class Trashbin {
$type = 'file'; $type = 'file';
} }
$trashbinSize = \OCP\Config::getAppValue('files_trashbin', 'size'); $trashbinSize = self::getTrashbinSize($user);
if ( $trashbinSize === null ) { if ( $trashbinSize === false || $trashbinSize < 0 ) {
$trashbinSize = self::calculateSize(new \OC_FilesystemView('/'. $user.'/files_trashbin')); $trashbinSize = self::calculateSize(new \OC_FilesystemView('/'. $user.'/files_trashbin'));
} }
$trashbinSize += self::copy_recursive($file_path, 'files_trashbin/files/'.$deleted.'.d'.$timestamp, $view); $trashbinSize += self::copy_recursive($file_path, 'files_trashbin/files/'.$deleted.'.d'.$timestamp, $view);
@ -107,7 +107,7 @@ class Trashbin {
$quota = \OCP\Util::computerFileSize(\OC_Appconfig::getValue('files', 'default_quota')); $quota = \OCP\Util::computerFileSize(\OC_Appconfig::getValue('files', 'default_quota'));
} }
if ( $quota === null ) { if ( $quota === null ) {
$quota = \OC\Files\Filesystem::free_space('/'); $quota = \OC\Files\Filesystem::free_space('/') / count(\OCP\User::getUsers());
} }
// calculate available space for trash bin // calculate available space for trash bin
@ -120,7 +120,9 @@ class Trashbin {
} }
$trashbinSize -= self::expire($availableSpace); $trashbinSize -= self::expire($availableSpace);
\OCP\Config::setAppValue('files_trashbin', 'size', $trashbinSize);
self::setTrashbinSize($user, $trashbinSize);
} }
@ -134,8 +136,8 @@ class Trashbin {
$user = \OCP\User::getUser(); $user = \OCP\User::getUser();
$view = new \OC_FilesystemView('/'.$user); $view = new \OC_FilesystemView('/'.$user);
$trashbinSize = \OCP\Config::getAppValue('files_trashbin', 'size'); $trashbinSize = self::getTrashbinSize($user);
if ( $trashbinSize === null ) { if ( $trashbinSize === false || $trashbinSize < 0 ) {
$trashbinSize = self::calculateSize(new \OC_FilesystemView('/'. $user.'/files_trashbin')); $trashbinSize = self::calculateSize(new \OC_FilesystemView('/'. $user.'/files_trashbin'));
} }
if ( $timestamp ) { if ( $timestamp ) {
@ -224,7 +226,8 @@ class Trashbin {
$query->execute(array($user,$filename,$timestamp)); $query->execute(array($user,$filename,$timestamp));
} }
\OCP\Config::setAppValue('files_trashbin', 'size', $trashbinSize); self::setTrashbinSize($user, $trashbinSize);
return true; return true;
} else { } else {
\OC_Log::write('files_trashbin', 'Couldn\'t restore file from trash bin, '.$filename, \OC_log::ERROR); \OC_Log::write('files_trashbin', 'Couldn\'t restore file from trash bin, '.$filename, \OC_log::ERROR);
@ -244,8 +247,8 @@ class Trashbin {
$view = new \OC_FilesystemView('/'.$user); $view = new \OC_FilesystemView('/'.$user);
$size = 0; $size = 0;
$trashbinSize = \OCP\Config::getAppValue('files_trashbin', 'size'); $trashbinSize = self::getTrashbinSize($user);
if ( $trashbinSize === null ) { if ( $trashbinSize === false || $trashbinSize < 0 ) {
$trashbinSize = self::calculateSize(new \OC_FilesystemView('/'. $user.'/files_trashbin')); $trashbinSize = self::calculateSize(new \OC_FilesystemView('/'. $user.'/files_trashbin'));
} }
@ -300,7 +303,7 @@ class Trashbin {
} }
$view->unlink('/files_trashbin/files/'.$file); $view->unlink('/files_trashbin/files/'.$file);
$trashbinSize -= $size; $trashbinSize -= $size;
\OCP\Config::setAppValue('files_trashbin', 'size', $trashbinSize); self::setTrashbinSize($user, $trashbinSize);
return $size; return $size;
} }
@ -474,4 +477,35 @@ class Trashbin {
return $size; return $size;
} }
/**
* get current size of trash bin from a given user
*
* @param $user user who owns the trash bin
* @return mixed trash bin size or false if no trash bin size is stored
*/
private static function getTrashbinSize($user) {
$query = \OC_DB::prepare('SELECT size FROM *PREFIX*files_trashsize WHERE user=?');
$result = $query->execute(array($user))->fetchAll();
if ($result) {
return $result[0]['size'];
}
return false;
}
/**
* write to the database how much space is in use for the trash bin
*
* @param $user owner of the trash bin
* @param $size size of the trash bin
*/
private static function setTrashbinSize($user, $size) {
if ( self::getTrashbinSize($user) === false) {
$query = \OC_DB::prepare('INSERT INTO *PREFIX*files_trashsize (size, user) VALUES (?, ?)');
}else {
$query = \OC_DB::prepare('UPDATE *PREFIX*files_trashsize SET size=? WHERE user=?');
}
$query->execute(array($size, $user));
}
} }

View File

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="ISO-8859-1" ?>
<database>
<name>*dbname*</name>
<create>true</create>
<overwrite>false</overwrite>
<charset>utf8</charset>
<table>
<name>*dbprefix*files_versions</name>
<declaration>
<field>
<name>user</name>
<type>text</type>
<default></default>
<notnull>true</notnull>
<length>64</length>
</field>
<field>
<name>size</name>
<type>text</type>
<default></default>
<notnull>true</notnull>
<length>50</length>
</field>
</declaration>
</table>
</database>

View File

@ -1 +1 @@
1.0.2 1.0.3

View File

@ -46,6 +46,37 @@ class Storage {
return array($uid, $filename); return array($uid, $filename);
} }
/**
* get current size of all versions from a given user
*
* @param $user user who owns the versions
* @return mixed versions size or false if no versions size is stored
*/
private static function getVersionsSize($user) {
$query = \OC_DB::prepare('SELECT size FROM *PREFIX*files_versions WHERE user=?');
$result = $query->execute(array($user))->fetchAll();
if ($result) {
return $result[0]['size'];
}
return false;
}
/**
* write to the database how much space is in use for versions
*
* @param $user owner of the versions
* @param $size size of the versions
*/
private static function setVersionsSize($user, $size) {
if ( self::getVersionsSize($user) === false) {
$query = \OC_DB::prepare('INSERT INTO *PREFIX*files_versions (size, user) VALUES (?, ?)');
}else {
$query = \OC_DB::prepare('UPDATE *PREFIX*files_versions SET size=? WHERE user=?');
}
$query->execute(array($size, $user));
}
/** /**
* store a new version of a file. * store a new version of a file.
*/ */
@ -74,17 +105,19 @@ class Storage {
} }
// store a new version of a file // store a new version of a file
$result = $users_view->copy('files'.$filename, 'files_versions'.$filename.'.v'.$users_view->filemtime('files'.$filename)); $users_view->copy('files'.$filename, 'files_versions'.$filename.'.v'.$users_view->filemtime('files'.$filename));
if ( ($versionsSize = \OCP\Config::getAppValue('files_versions', 'size')) === null ) { $versionsSize = self::getVersionsSize($uid);
if ( $versionsSize === false || $versionSize < 0 ) {
$versionsSize = self::calculateSize($uid); $versionsSize = self::calculateSize($uid);
} }
$versionsSize += $users_view->filesize('files'.$filename); $versionsSize += $users_view->filesize('files'.$filename);
// expire old revisions if necessary // expire old revisions if necessary
$newSize = self::expire($filename, $versionsSize); $newSize = self::expire($filename, $versionsSize);
if ( $newSize != $versionsSize ) { if ( $newSize != $versionsSize ) {
\OCP\Config::setAppValue('files_versions', 'size', $versionsSize); self::setVersionsSize($uid, $newSize);
} }
} }
} }
@ -99,14 +132,15 @@ class Storage {
$abs_path = \OCP\Config::getSystemValue('datadirectory').$versions_fileview->getAbsolutePath('').$filename.'.v'; $abs_path = \OCP\Config::getSystemValue('datadirectory').$versions_fileview->getAbsolutePath('').$filename.'.v';
if( ($versions = self::getVersions($uid, $filename)) ) { if( ($versions = self::getVersions($uid, $filename)) ) {
if ( ($versionsSize = \OCP\Config::getAppValue('files_versions', 'size')) === null ) { $versionsSize = self::getVersionsSize($uid);
if ( $versionsSize === false || $versionsSize < 0 ) {
$versionsSize = self::calculateSize($uid); $versionsSize = self::calculateSize($uid);
} }
foreach ($versions as $v) { foreach ($versions as $v) {
unlink($abs_path . $v['version']); unlink($abs_path . $v['version']);
$versionsSize -= $v['size']; $versionsSize -= $v['size'];
} }
\OCP\Config::setAppValue('files_versions', 'size', $versionsSize); self::setVersionsSize($uid, $versionsSize);
} }
} }
@ -321,12 +355,13 @@ class Storage {
$quota = \OCP\Util::computerFileSize(\OC_Appconfig::getValue('files', 'default_quota')); $quota = \OCP\Util::computerFileSize(\OC_Appconfig::getValue('files', 'default_quota'));
} }
if ( $quota == null ) { if ( $quota == null ) {
$quota = \OC\Files\Filesystem::free_space('/'); $quota = \OC\Files\Filesystem::free_space('/') / count(\OCP\User::getUsers());
} }
// make sure that we have the current size of the version history // make sure that we have the current size of the version history
if ( $versionsSize === null ) { if ( $versionsSize === null ) {
if ( ($versionsSize = \OCP\Config::getAppValue('files_versions', 'size')) === null ) { $versionsSize = self::getVersionsSize($uid);
if ( $versionsSize === false || $versionsSize < 0 ) {
$versionsSize = self::calculateSize($uid); $versionsSize = self::calculateSize($uid);
} }
} }