From: Björn Schießle Date: Mon, 25 Feb 2013 13:26:59 +0000 (+0100) Subject: Merge branch 'master' into update-expire-function X-Git-Tag: v5.0.0beta2~8^2~1 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=4c980a4851804c4f5379e364e106edacd04c42e4;p=nextcloud-server.git Merge branch 'master' into update-expire-function Conflicts: apps/files_trashbin/appinfo/version apps/files_trashbin/lib/trash.php --- 4c980a4851804c4f5379e364e106edacd04c42e4 diff --cc apps/files_trashbin/lib/trash.php index 3c9797e671f,5963f9a1b7c..032df86a61e --- a/apps/files_trashbin/lib/trash.php +++ b/apps/files_trashbin/lib/trash.php @@@ -36,67 -36,78 +36,78 @@@ 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\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'; } - $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::calculateSize(new \OC_FilesystemView('/'. $user.'/versions_trashbin')); } - $trashbinSize += self::copy_recursive($file_path, 'files_trashbin/'.$deleted.'.d'.$timestamp, $view); + $trashbinSize += self::copy_recursive($file_path, 'files_trashbin/files/'.$deleted.'.d'.$timestamp, $view); - if ( $view->file_exists('files_trashbin/'.$deleted.'.d'.$timestamp) ) { - $query = \OC_DB::prepare("INSERT INTO *PREFIX*files_trash (id,timestamp,location,type,mime,user)" - ." VALUES (?,?,?,?,?,?)"); + 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/'.$deleted.'.d'.$timestamp); + $view->deleteAll('files_trashbin/files/'.$deleted.'.d'.$timestamp); \OC_Log::write('files_trashbin', 'trash bin database couldn\'t be updated', \OC_log::ERROR); return; - } - - 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, 'versions_trashbin/'. $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'], - 'versions_trashbin/'. $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); + 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 { + $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('/'); + + // get available disk space for user + $quota = \OCP\Util::computerFileSize(\OC_Preferences::getValue($user, 'files', 'quota')); - if ( $quota == null ) { ++ if ( $quota === null ) { + $quota = \OCP\Util::computerFileSize(\OC_Appconfig::getValue('files', 'default_quota')); + } - if ( $quota == null ) { ++ if ( $quota === null ) { + $quota = \OC\Files\Filesystem::free_space('/') / count(\OCP\User::getUsers()); } // calculate available space for trash bin @@@ -125,10 -134,9 +136,9 @@@ $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')); - $trashbinSize += self::calculateSize(new \OC_FilesystemView('/'. $user.'/versions_trashbin')); ++ if ( $trashbinSize === false || $trashbinSize < 0 ) { + $trashbinSize = self::calculateSize(new \OC_FilesystemView('/'. $user.'/files_trashbin')); } if ( $timestamp ) { $query = \OC_DB::prepare('SELECT location,type FROM *PREFIX*files_trash' @@@ -222,10 -244,9 +247,9 @@@ $view = new \OC_FilesystemView('/'.$user); $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')); - $trashbinSize += self::calculateSize(new \OC_FilesystemView('/'. $user.'/versions_trashbin')); ++ if ( $trashbinSize === false || $trashbinSize < 0 ) { + $trashbinSize = self::calculateSize(new \OC_FilesystemView('/'. $user.'/files_trashbin')); } if ( $timestamp ) { @@@ -252,16 -273,35 +276,35 @@@ } } } - - if ($view->is_dir('/files_trashbin/'.$file)) { - $size += self::calculateSize(new \OC_Filesystemview('/'.$user.'/files_trashbin/'.$file)); + + // 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); + } + + if ($view->is_dir('/files_trashbin/files/'.$file)) { + $size += self::calculateSize(new \OC_Filesystemview('/'.$user.'/files_trashbin/files/'.$file)); } else { - $size += $view->filesize('/files_trashbin/'.$file); + $size += $view->filesize('/files_trashbin/files/'.$file); } - $view->unlink('/files_trashbin/'.$file); + $view->unlink('/files_trashbin/files/'.$file); $trashbinSize -= $size; - \OCP\Config::setAppValue('files_trashbin', 'size', $trashbinSize); - + self::setTrashbinSize($user, $trashbinSize); + return $size; } @@@ -455,35 -474,4 +477,35 @@@ 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)); + } + - } + }