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>
<default></default>
<notnull>true</notnull>
<length>50</length>
<length>250</length>
</field>
<field>
@ -26,7 +26,7 @@
<type>text</type>
<default></default>
<notnull>true</notnull>
<length>50</length>
<length>64</length>
</field>
<field>
@ -42,7 +42,7 @@
<type>text</type>
<default></default>
<notnull>true</notnull>
<length>200</length>
<length>512</length>
</field>
<field>
@ -89,4 +89,30 @@
</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>

View File

@ -1 +1 @@
0.2
0.3

View File

@ -36,78 +36,78 @@ class Trashbin {
*/
public static function move2trash($file_path) {
$user = \OCP\User::getUser();
$view = new \OC\Files\View('/'. $user);
if (!$view->is_dir('files_trashbin')) {
$view->mkdir('files_trashbin');
$view = new \OC\Files\View('/'. $user);
if (!$view->is_dir('files_trashbin')) {
$view->mkdir('files_trashbin');
$view->mkdir("files_trashbin/files");
$view->mkdir("files_trashbin/versions");
$view->mkdir("files_trashbin/keyfiles");
}
$path_parts = pathinfo($file_path);
$deleted = $path_parts['basename'];
$location = $path_parts['dirname'];
$timestamp = time();
$mime = $view->getMimeType('files'.$file_path);
if ( $view->is_dir('files'.$file_path) ) {
$type = 'dir';
} else {
$type = 'file';
$view->mkdir("files_trashbin/keyfiles");
}
$path_parts = pathinfo($file_path);
$deleted = $path_parts['basename'];
$location = $path_parts['dirname'];
$timestamp = time();
$mime = $view->getMimeType('files'.$file_path);
if ( $view->is_dir('files'.$file_path) ) {
$type = 'dir';
} else {
$type = 'file';
}
$trashbinSize = \OCP\Config::getAppValue('files_trashbin', 'size');
if ( $trashbinSize === null ) {
$trashbinSize = self::getTrashbinSize($user);
if ( $trashbinSize === false || $trashbinSize < 0 ) {
$trashbinSize = self::calculateSize(new \OC_FilesystemView('/'. $user.'/files_trashbin'));
}
$trashbinSize += self::copy_recursive($file_path, 'files_trashbin/files/'.$deleted.'.d'.$timestamp, $view);
if ( $view->file_exists('files_trashbin/files/'.$deleted.'.d'.$timestamp) ) {
if ( $view->file_exists('files_trashbin/files/'.$deleted.'.d'.$timestamp) ) {
$query = \OC_DB::prepare("INSERT INTO *PREFIX*files_trash (id,timestamp,location,type,mime,user) VALUES (?,?,?,?,?,?)");
$result = $query->execute(array($deleted, $timestamp, $location, $type, $mime, $user));
if ( !$result ) { // if file couldn't be added to the database than also don't store it in the trash bin.
$view->deleteAll('files_trashbin/files/'.$deleted.'.d'.$timestamp);
\OC_Log::write('files_trashbin', 'trash bin database couldn\'t be updated', \OC_log::ERROR);
return;
}
}
// Take care of file versions
if ( \OCP\App::isEnabled('files_versions') ) {
// Take care of file versions
if ( \OCP\App::isEnabled('files_versions') ) {
if ( $view->is_dir('files_versions'.$file_path) ) {
$trashbinSize += self::calculateSize(new \OC_FilesystemView('/'. $user.'/files_versions/'.$file_path));
$view->rename('files_versions'.$file_path, 'files_trashbin/versions'. $deleted.'.d'.$timestamp);
} else if ( $versions = \OCA\Files_Versions\Storage::getVersions($file_path) ) {
$trashbinSize += self::calculateSize(new \OC_FilesystemView('/'. $user.'/files_versions/'.$file_path));
$view->rename('files_versions'.$file_path, 'files_trashbin/versions'. $deleted.'.d'.$timestamp);
} else if ( $versions = \OCA\Files_Versions\Storage::getVersions($file_path) ) {
foreach ($versions as $v) {
$trashbinSize += $view->filesize('files_versions'.$v['path'].'.v'.$v['version']);
$view->rename('files_versions'.$v['path'].'.v'.$v['version'], 'files_trashbin/versions'. $deleted.'.v'.$v['version'].'.d'.$timestamp);
}
$trashbinSize += $view->filesize('files_versions'.$v['path'].'.v'.$v['version']);
$view->rename('files_versions'.$v['path'].'.v'.$v['version'], 'files_trashbin/versions'. $deleted.'.v'.$v['version'].'.d'.$timestamp);
}
}
}
// Take care of encryption keys
$keyfile = \OC_Filesystem::normalizePath('files_encryption/keyfiles/'.$file_path);
$keyfile = \OC_Filesystem::normalizePath('files_encryption/keyfiles/'.$file_path);
if ( \OCP\App::isEnabled('files_encryption') && $view->file_exists($keyfile.'.key') ) {
if ( $view->is_dir('files'.$file_path) ) {
$trashbinSize += self::calculateSize(new \OC_FilesystemView('/'.$user.'/'.$keyfile));
$view->rename($keyfile, 'files_trashbin/keyfiles/'. $deleted.'.d'.$timestamp);
} else {
} else {
$trashbinSize += $view->filesize($keyfile.'.key');
$view->rename($keyfile.'.key', 'files_trashbin/keyfiles/'. $deleted.'.key.d'.$timestamp);
}
}
}
} else {
\OC_Log::write('files_trashbin', 'Couldn\'t move '.$file_path.' to the trash bin', \OC_log::ERROR);
}
// get available disk space for user
$quota = \OCP\Util::computerFileSize(\OC_Preferences::getValue($user, 'files', 'quota'));
if ( $quota === null ) {
$quota = \OCP\Util::computerFileSize(\OC_Appconfig::getValue('files', 'default_quota'));
}
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
@ -120,7 +120,9 @@ class Trashbin {
}
$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();
$view = new \OC_FilesystemView('/'.$user);
$trashbinSize = \OCP\Config::getAppValue('files_trashbin', 'size');
if ( $trashbinSize === null ) {
$trashbinSize = self::getTrashbinSize($user);
if ( $trashbinSize === false || $trashbinSize < 0 ) {
$trashbinSize = self::calculateSize(new \OC_FilesystemView('/'. $user.'/files_trashbin'));
}
if ( $timestamp ) {
@ -184,9 +186,9 @@ class Trashbin {
$versionedFile = $file;
}
if ( $result[0]['type'] === 'dir' ) {
$trashbinSize -= self::calculateSize(new \OC_FilesystemView('/'.$user.'/'.'files_trashbin/versions/'. $file));
$view->rename(\OC_Filesystem::normalizePath('files_trashbin/versions/'. $file), \OC_Filesystem::normalizePath('files_versions/'.$location.'/'.$filename.$ext));
} else if ( $versions = self::getVersionsFromTrash($versionedFile, $timestamp) ) {
$trashbinSize -= self::calculateSize(new \OC_FilesystemView('/'.$user.'/'.'files_trashbin/versions/'. $file));
$view->rename(\OC_Filesystem::normalizePath('files_trashbin/versions/'. $file), \OC_Filesystem::normalizePath('files_versions/'.$location.'/'.$filename.$ext));
} else if ( $versions = self::getVersionsFromTrash($versionedFile, $timestamp) ) {
foreach ($versions as $v) {
if ($timestamp ) {
$trashbinSize -= $view->filesize('files_trashbin/versions/'.$versionedFile.'.v'.$v.'.d'.$timestamp);
@ -194,8 +196,8 @@ class Trashbin {
} else {
$trashbinSize -= $view->filesize('files_trashbin/versions/'.$versionedFile.'.v'.$v);
$view->rename('files_trashbin/versions/'.$versionedFile.'.v'.$v, 'files_versions/'.$location.'/'.$filename.$ext.'.v'.$v);
}
}
}
}
}
}
@ -208,15 +210,15 @@ class Trashbin {
}
if ($timestamp) {
$keyfile .= '.d'.$timestamp;
}
}
if ( \OCP\App::isEnabled('files_encryption') && $view->file_exists($keyfile) ) {
if ( $result[0]['type'] === 'dir' ) {
if ( $result[0]['type'] === 'dir' ) {
$trashbinSize -= self::calculateSize(new \OC_FilesystemView('/'.$user.'/'.$keyfile));
$view->rename($keyfile, 'files_encryption/keyfiles/'. $location.'/'.$filename);
} else {
$trashbinSize -= $view->filesize($keyfile);
$view->rename($keyfile, 'files_encryption/keyfiles/'. $location.'/'.$filename.'.key');
}
}
}
if ( $timestamp ) {
@ -224,7 +226,8 @@ class Trashbin {
$query->execute(array($user,$filename,$timestamp));
}
\OCP\Config::setAppValue('files_trashbin', 'size', $trashbinSize);
self::setTrashbinSize($user, $trashbinSize);
return true;
} else {
\OC_Log::write('files_trashbin', 'Couldn\'t restore file from trash bin, '.$filename, \OC_log::ERROR);
@ -242,10 +245,10 @@ class Trashbin {
public static function delete($filename, $timestamp=null) {
$user = \OCP\User::getUser();
$view = new \OC_FilesystemView('/'.$user);
$size = 0;
$size = 0;
$trashbinSize = \OCP\Config::getAppValue('files_trashbin', 'size');
if ( $trashbinSize === null ) {
$trashbinSize = self::getTrashbinSize($user);
if ( $trashbinSize === false || $trashbinSize < 0 ) {
$trashbinSize = self::calculateSize(new \OC_FilesystemView('/'. $user.'/files_trashbin'));
}
@ -275,23 +278,23 @@ class Trashbin {
}
// Take care of encryption keys
$parts = pathinfo($file);
if ( $view->is_dir('/files_trashbin/files/'.$file) ) {
$keyfile = \OC_Filesystem::normalizePath('files_trashbin/keyfiles/'.$filename);
} else {
$keyfile = \OC_Filesystem::normalizePath('files_trashbin/keyfiles/'.$filename.'.key');
}
if ($timestamp) {
$keyfile .= '.d'.$timestamp;
}
if ( \OCP\App::isEnabled('files_encryption') && $view->file_exists($keyfile) ) {
if ( $view->is_dir($keyfile) ) {
$size += self::calculateSize(new \OC_FilesystemView('/'.$user.'/'.$keyfile));
} else {
$size += $view->filesize($keyfile);
}
$view->unlink($keyfile);
}
$parts = pathinfo($file);
if ( $view->is_dir('/files_trashbin/files/'.$file) ) {
$keyfile = \OC_Filesystem::normalizePath('files_trashbin/keyfiles/'.$filename);
} else {
$keyfile = \OC_Filesystem::normalizePath('files_trashbin/keyfiles/'.$filename.'.key');
}
if ($timestamp) {
$keyfile .= '.d'.$timestamp;
}
if ( \OCP\App::isEnabled('files_encryption') && $view->file_exists($keyfile) ) {
if ( $view->is_dir($keyfile) ) {
$size += self::calculateSize(new \OC_FilesystemView('/'.$user.'/'.$keyfile));
} else {
$size += $view->filesize($keyfile);
}
$view->unlink($keyfile);
}
if ($view->is_dir('/files_trashbin/files/'.$file)) {
$size += self::calculateSize(new \OC_Filesystemview('/'.$user.'/files_trashbin/files/'.$file));
@ -300,8 +303,8 @@ class Trashbin {
}
$view->unlink('/files_trashbin/files/'.$file);
$trashbinSize -= $size;
\OCP\Config::setAppValue('files_trashbin', 'size', $trashbinSize);
self::setTrashbinSize($user, $trashbinSize);
return $size;
}
@ -474,4 +477,35 @@ class Trashbin {
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);
}
/**
* 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.
*/
@ -74,17 +105,19 @@ class Storage {
}
// store a new version of a file
$result = $users_view->copy('files'.$filename, 'files_versions'.$filename.'.v'.$users_view->filemtime('files'.$filename));
if ( ($versionsSize = \OCP\Config::getAppValue('files_versions', 'size')) === null ) {
$users_view->copy('files'.$filename, 'files_versions'.$filename.'.v'.$users_view->filemtime('files'.$filename));
$versionsSize = self::getVersionsSize($uid);
if ( $versionsSize === false || $versionSize < 0 ) {
$versionsSize = self::calculateSize($uid);
}
$versionsSize += $users_view->filesize('files'.$filename);
// expire old revisions if necessary
$newSize = self::expire($filename, $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';
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);
}
foreach ($versions as $v) {
unlink($abs_path . $v['version']);
$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'));
}
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
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);
}
}