From: Björn Schießle Date: Mon, 25 Feb 2013 10:14:06 +0000 (+0100) Subject: Merge branch 'master' into update-expire-function X-Git-Tag: v5.0.0beta2~8^2~2 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=632ca9f06d2247061b7f03ce9bfcd0402bd92441;p=nextcloud-server.git Merge branch 'master' into update-expire-function Conflicts: apps/files_trashbin/lib/trash.php --- 632ca9f06d2247061b7f03ce9bfcd0402bd92441 diff --cc apps/files_trashbin/lib/trash.php index 898f8972737,8355c7252b0..3c9797e671f --- a/apps/files_trashbin/lib/trash.php +++ b/apps/files_trashbin/lib/trash.php @@@ -34,29 -36,28 +36,29 @@@ class Trashbin */ public static function move2trash($file_path) { $user = \OCP\User::getUser(); - $view = new \OC_FilesystemView('/'. $user); - if (!$view->is_dir('files_trashbin')) { - $view->mkdir('files_trashbin'); - $view->mkdir("versions_trashbin"); - } - - $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 = new \OC_FilesystemView('/'. $user); + if (!$view->is_dir('files_trashbin')) { + $view->mkdir('files_trashbin'); + $view->mkdir("versions_trashbin"); + } + + $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'; } - - if ( ($trashbinSize = \OCP\Config::getAppValue('files_trashbin', 'size')) === null ) { + + $trashbinSize = self::getTrashbinSize($user); - if ( $trashbinSize === false || $trashbinSize < 0 ) { ++ if ( $trashbinSize === false || $trashbinSize < 0 ) { $trashbinSize = self::calculateSize(new \OC_FilesystemView('/'. $user.'/files_trashbin')); - $trashbinSize += self::calculateSize(new \OC_FilesystemView('/'. $user.'/versions_trashbin')); + $trashbinSize += self::calculateSize(new \OC_FilesystemView('/'. $user.'/versions_trashbin')); } $trashbinSize += self::copy_recursive($file_path, 'files_trashbin/'.$deleted.'.d'.$timestamp, $view); @@@ -83,31 -88,30 +89,32 @@@ } 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('/'); + + // 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('/') / count(\OCP\User::getUsers()); ++ $quota = \OC\Files\Filesystem::free_space('/') / count(\OCP\User::getUsers()); } - - // calculate available space for trash bin - $rootInfo = $view->getFileInfo('/files'); - $free = $quota-$rootInfo['size']; // remaining free space for user - if ( $free > 0 ) { - $availableSpace = ($free * self::DEFAULTMAXSIZE / 100) - $trashbinSize; // how much space can be used for versions - } else { - $availableSpace = $free-$trashbinSize; + + // calculate available space for trash bin + $rootInfo = $view->getFileInfo('/files'); + $free = $quota-$rootInfo['size']; // remaining free space for user + if ( $free > 0 ) { + $availableSpace = ($free * self::DEFAULTMAXSIZE / 100) - $trashbinSize; // how much space can be used for versions + } else { + $availableSpace = $free-$trashbinSize; } - + $trashbinSize -= self::expire($availableSpace); - \OCP\Config::setAppValue('files_trashbin', 'size', $trashbinSize); + + self::setTrashbinSize($user, $trashbinSize); ++ } - - + + /** * restore files from trash bin * @param $file path to the deleted file @@@ -117,14 -121,14 +124,15 @@@ public static function restore($file, $filename, $timestamp) { $user = \OCP\User::getUser(); $view = new \OC_FilesystemView('/'.$user); - - if ( ($trashbinSize = \OCP\Config::getAppValue('files_trashbin', 'size')) === null ) { - $trashbinSize = self::calculateSize(new \OC_FilesystemView('/'. $user.'/files_trashbin')); + + $trashbinSize = self::getTrashbinSize($user); + if ( $trashbinSize === false || $trashbinSize < 0 ) { + $trashbinSize = self::calculateSize(new \OC_FilesystemView('/'. $user.'/files_trashbin')); - $trashbinSize += self::calculateSize(new \OC_FilesystemView('/'. $user.'/versions_trashbin')); + $trashbinSize += self::calculateSize(new \OC_FilesystemView('/'. $user.'/versions_trashbin')); } if ( $timestamp ) { - $query = \OC_DB::prepare('SELECT location,type FROM *PREFIX*files_trash WHERE user=? AND id=? AND timestamp=?'); + $query = \OC_DB::prepare('SELECT location,type FROM *PREFIX*files_trash' + .' WHERE user=? AND id=? AND timestamp=?'); $result = $query->execute(array($user,$filename,$timestamp))->fetchAll(); if ( count($result) != 1 ) { \OC_Log::write('files_trashbin', 'trash bin database inconsistent!', \OC_Log::ERROR); @@@ -197,27 -205,26 +210,27 @@@ return false; } - - /** - * delete file from trash bin permanently + + /** + * delete file from trash bin permanently * @param $filename path to the file - * @param $timestamp of deletion time - * @return size of deleted files - */ - public static function delete($filename, $timestamp=null) { - $user = \OCP\User::getUser(); + * @param $timestamp of deletion time + * @return size of deleted files + */ + public static function delete($filename, $timestamp=null) { + $user = \OCP\User::getUser(); $view = new \OC_FilesystemView('/'.$user); - $size = 0; - - if ( ($trashbinSize = \OCP\Config::getAppValue('files_trashbin', 'size')) === null ) { - $trashbinSize = self::calculateSize(new \OC_FilesystemView('/'. $user.'/files_trashbin')); + $size = 0; + + $trashbinSize = self::getTrashbinSize($user); + if ( $trashbinSize === false || $trashbinSize < 0 ) { + $trashbinSize = self::calculateSize(new \OC_FilesystemView('/'. $user.'/files_trashbin')); - $trashbinSize += self::calculateSize(new \OC_FilesystemView('/'. $user.'/versions_trashbin')); + $trashbinSize += self::calculateSize(new \OC_FilesystemView('/'. $user.'/versions_trashbin')); } - - if ( $timestamp ) { - $query = \OC_DB::prepare('DELETE FROM *PREFIX*files_trash WHERE user=? AND id=? AND timestamp=?'); - $query->execute(array($user,$filename,$timestamp)); + + if ( $timestamp ) { + $query = \OC_DB::prepare('DELETE FROM *PREFIX*files_trash WHERE user=? AND id=? AND timestamp=?'); + $query->execute(array($user,$filename,$timestamp)); $file = $filename.'.d'.$timestamp; } else { $file = $filename; @@@ -247,9 -254,9 +260,9 @@@ } $view->unlink('/files_trashbin/'.$file); $trashbinSize -= $size; - \OCP\Config::setAppValue('files_trashbin', 'size', $trashbinSize); - + self::setTrashbinSize($user, $trashbinSize); + - return $size; + return $size; } /** @@@ -430,40 -444,9 +450,40 @@@ $relpath = substr($path, strlen($root)-1); if ( !$view->is_dir($relpath) ) { $size += $view->filesize($relpath); - } - } - 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)); + } + - } ++} diff --cc apps/files_versions/lib/versions.php index 415830a9a00,adbf2c1df7e..62574236343 --- a/apps/files_versions/lib/versions.php +++ b/apps/files_versions/lib/versions.php @@@ -44,38 -45,7 +45,38 @@@ 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. */ @@@ -128,21 -96,20 +129,21 @@@ public static function delete($filename) { list($uid, $filename) = self::getUidAndFilename($filename); $versions_fileview = new \OC\Files\View('/'.$uid .'/files_versions'); - + $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); } } - + /** * rename versions of a file */ @@@ -348,13 -321,12 +355,13 @@@ $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); } }