summaryrefslogtreecommitdiffstats
path: root/apps/files_trashbin/lib
diff options
context:
space:
mode:
authorBjörn Schießle <schiessle@owncloud.com>2013-02-25 12:14:51 +0100
committerBjörn Schießle <schiessle@owncloud.com>2013-02-25 12:14:51 +0100
commit4dea6a6228390713c0296dd6a447b9f60a211767 (patch)
treee5f7840770ab4ea67f0a58d6642b6b75cfa05fda /apps/files_trashbin/lib
parent6aed7abfbe4a58cec822bdd16d8c71d4bac14c28 (diff)
parent9ee5069f2a1d76c899eeef6cec0f06387764fabd (diff)
downloadnextcloud-server-4dea6a6228390713c0296dd6a447b9f60a211767.tar.gz
nextcloud-server-4dea6a6228390713c0296dd6a447b9f60a211767.zip
Merge branch 'master' into trashbin_encryption
Conflicts: apps/files_trashbin/index.php apps/files_trashbin/lib/trash.php
Diffstat (limited to 'apps/files_trashbin/lib')
-rw-r--r--apps/files_trashbin/lib/hooks.php40
-rw-r--r--apps/files_trashbin/lib/trash.php259
2 files changed, 153 insertions, 146 deletions
diff --git a/apps/files_trashbin/lib/hooks.php b/apps/files_trashbin/lib/hooks.php
index d6a62d447b8..9081706a2c5 100644
--- a/apps/files_trashbin/lib/hooks.php
+++ b/apps/files_trashbin/lib/hooks.php
@@ -1,23 +1,23 @@
<?php
-/**
- * ownCloud - trash bin
- *
- * @author Bjoern Schiessle
- * @copyright 2013 Bjoern Schiessle schiessle@owncloud.com
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or any later version.
- *
- * This library 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 along with this library. If not, see <http://www.gnu.org/licenses/>.
- *
+/**
+ * ownCloud - trash bin
+ *
+ * @author Bjoern Schiessle
+ * @copyright 2013 Bjoern Schiessle schiessle@owncloud.com
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or any later version.
+ *
+ * This library 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 along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
*/
/**
@@ -36,7 +36,7 @@ class Hooks {
* to copy the file to the trash bin
*/
public static function remove_hook($params) {
-
+
if ( \OCP\App::isEnabled('files_trashbin') ) {
$path = $params['path'];
Trashbin::move2trash($path);
diff --git a/apps/files_trashbin/lib/trash.php b/apps/files_trashbin/lib/trash.php
index f11e5c4cbfd..83fc424834c 100644
--- a/apps/files_trashbin/lib/trash.php
+++ b/apps/files_trashbin/lib/trash.php
@@ -1,35 +1,37 @@
<?php
-/**
- * ownCloud - trash bin
- *
- * @author Bjoern Schiessle
- * @copyright 2013 Bjoern Schiessle schiessle@owncloud.com
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or any later version.
- *
- * This library 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 along with this library. If not, see <http://www.gnu.org/licenses/>.
- *
- */
-
-namespace OCA\Files_Trashbin;
-
+/**
+ * ownCloud - trash bin
+ *
+ * @author Bjoern Schiessle
+ * @copyright 2013 Bjoern Schiessle schiessle@owncloud.com
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or any later version.
+ *
+ * This library 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 along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OCA\Files_Trashbin;
+
class Trashbin {
-
- const DEFAULT_RETENTION_OBLIGATION=180; // how long do we keep files in the trash bin if no other value is defined in the config file (unit: days)
- const DEFAULTMAXSIZE=50; // unit: percentage; 50% of available disk space/quota
-
+ // 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=180;
+
+ // unit: percentage; 50% of available disk space/quota
+ const DEFAULTMAXSIZE=50;
+
/**
* move file to the trash bin
- *
+ *
* @param $file_path path to the deleted file/directory relative to the files root directory
*/
public static function move2trash($file_path) {
@@ -56,12 +58,12 @@ class Trashbin {
}
if ( ($trashbinSize = \OCP\Config::getAppValue('files_trashbin', 'size')) === null ) {
- $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);
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 (?,?,?,?,?,?)");
+ $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);
@@ -79,7 +81,7 @@ class Trashbin {
$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
@@ -97,30 +99,30 @@ class Trashbin {
} 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'));
}
-
- // 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;
+ if ( $quota == null ) {
+ $quota = \OC\Files\Filesystem::free_space('/');
}
-
+
+ // 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);
+ \OCP\Config::setAppValue('files_trashbin', 'size', $trashbinSize);
}
-
-
+
+
/**
* restore files from trash bin
* @param $file path to the deleted file
@@ -132,22 +134,23 @@ class Trashbin {
$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::calculateSize(new \OC_FilesystemView('/'. $user.'/files_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);
return false;
}
-
- // if location no longer exists, restore file in the root directory
- $location = $result[0]['location'];
- if ( $result[0]['location'] != '/' &&
- (!$view->is_dir('files'.$result[0]['location']) ||
- !$view->isUpdatable('files'.$result[0]['location'])) ) {
- $location = '';
+
+ // if location no longer exists, restore file in the root directory
+ $location = $result[0]['location'];
+ if ( $result[0]['location'] != '/' &&
+ (!$view->is_dir('files'.$result[0]['location']) ||
+ !$view->isUpdatable('files'.$result[0]['location'])) ) {
+ $location = '';
}
} else {
$path_parts = pathinfo($file);
@@ -160,7 +163,7 @@ class Trashbin {
$source = \OC_Filesystem::normalizePath('files_trashbin/files/'.$file);
$target = \OC_Filesystem::normalizePath('files/'.$location.'/'.$filename);
-
+
// we need a extension in case a file/dir with the same name already exists
$ext = self::getUniqueExtension($location, $filename, $view);
$mtime = $view->filemtime($source);
@@ -177,7 +180,7 @@ class Trashbin {
$versionedFile = $filename;
} else {
$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));
@@ -191,7 +194,7 @@ class Trashbin {
$view->rename('files_trashbin/versions/'.$versionedFile.'.v'.$v, 'files_versions/'.$location.'/'.$filename.$ext.'.v'.$v);
}
}
- }
+ }
}
// Take care of encryption keys TODO! Get '.key' in file between file name and delete date (also for permanent delete!)
@@ -215,7 +218,7 @@ class Trashbin {
}
if ( $timestamp ) {
- $query = \OC_DB::prepare('DELETE FROM *PREFIX*files_trash WHERE user=? AND id=? AND timestamp=?');
+ $query = \OC_DB::prepare('DELETE FROM *PREFIX*files_trash WHERE user=? AND id=? AND timestamp=?');
$query->execute(array($user,$filename,$timestamp));
}
@@ -227,30 +230,30 @@ class Trashbin {
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'));
+ $trashbinSize = self::calculateSize(new \OC_FilesystemView('/'. $user.'/files_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;
}
-
+
if ( \OCP\App::isEnabled('files_versions') ) {
if ($view->is_dir('files_trashbin/versions/'.$file)) {
$size += self::calculateSize(new \OC_Filesystemview('/'.$user.'/files_trashbin/versions/'.$file));
@@ -295,8 +298,8 @@ class Trashbin {
$view->unlink('/files_trashbin/files/'.$file);
$trashbinSize -= $size;
\OCP\Config::setAppValue('files_trashbin', 'size', $trashbinSize);
-
- return $size;
+
+ return $size;
}
/**
@@ -319,21 +322,22 @@ class Trashbin {
return $view->file_exists($target);
}
- /**
+ /**
* clean up the trash bin
- * @param max. available disk space for trashbin
- */
+ * @param max. available disk space for trashbin
+ */
private static function expire($availableSpace) {
-
+
$user = \OCP\User::getUser();
$view = new \OC_FilesystemView('/'.$user);
$size = 0;
-
- $query = \OC_DB::prepare('SELECT location,type,id,timestamp FROM *PREFIX*files_trash WHERE user=?');
+
+ $query = \OC_DB::prepare('SELECT location,type,id,timestamp FROM *PREFIX*files_trash WHERE user=?');
$result = $query->execute(array($user))->fetchAll();
-
- $retention_obligation = \OC_Config::getValue('trashbin_retention_obligation', self::DEFAULT_RETENTION_OBLIGATION);
-
+
+ $retention_obligation = \OC_Config::getValue('trashbin_retention_obligation',
+ self::DEFAULT_RETENTION_OBLIGATION);
+
$limit = time() - ($retention_obligation * 86400);
foreach ( $result as $r ) {
@@ -343,11 +347,11 @@ class Trashbin {
$size += self::delete($filename, $timestamp);
}
}
-
$availableSpace = $availableSpace + $size;
// if size limit for trash bin reached, delete oldest files in trash bin
if ($availableSpace < 0) {
- $query = \OC_DB::prepare('SELECT location,type,id,timestamp FROM *PREFIX*files_trash WHERE user=? ORDER BY timestamp ASC');
+ $query = \OC_DB::prepare('SELECT location,type,id,timestamp FROM *PREFIX*files_trash'
+ .' WHERE user=? ORDER BY timestamp ASC');
$result = $query->execute(array($user))->fetchAll();
$length = count($result);
$i = 0;
@@ -356,15 +360,17 @@ class Trashbin {
$availableSpace += $tmp;
$size += $tmp;
$i++;
- }
+ }
+
+
}
-
- return $size;
+
+ return $size;
}
-
+
/**
* recursive copy to copy a whole directory
- *
+ *
* @param $source source path, relative to the users files directory
* @param $destination destination path relative to the users root directoy
* @param $view file view for the users root directory
@@ -391,7 +397,7 @@ class Trashbin {
}
return $size;
}
-
+
/**
* find all versions which belong to the file we want to restore
* @param $filename name of the file which should be restored
@@ -399,7 +405,7 @@ class Trashbin {
*/
private static function getVersionsFromTrash($filename, $timestamp) {
$view = new \OC_FilesystemView('/'.\OCP\User::getUser().'/files_trashbin/versions');
- $versionsName = \OCP\Config::getSystemValue('datadirectory').$view->getAbsolutePath($filename);
+ $versionsName = \OCP\Config::getSystemValue('datadirectory').$view->getAbsolutePath($filename);
$versions = array();
if ($timestamp ) {
// fetch for old versions
@@ -407,20 +413,20 @@ class Trashbin {
$offset = -strlen($timestamp)-2;
} else {
$matches = glob( $versionsName.'.v*' );
- }
-
+ }
+
foreach( $matches as $ma ) {
if ( $timestamp ) {
- $parts = explode( '.v', substr($ma, 0, $offset) );
+ $parts = explode( '.v', substr($ma, 0, $offset) );
$versions[] = ( end( $parts ) );
} else {
- $parts = explode( '.v', $ma );
+ $parts = explode( '.v', $ma );
$versions[] = ( end( $parts ) );
}
}
return $versions;
}
-
+
/**
* find unique extension for restored file if a file with the same name already exists
* @param $location where the file should be restored
@@ -429,39 +435,40 @@ class Trashbin {
* @return string with unique extension
*/
private static function getUniqueExtension($location, $filename, $view) {
- $ext = '';
- if ( $view->file_exists('files'.$location.'/'.$filename) ) {
- $tmpext = '.restored';
- $ext = $tmpext;
- $i = 1;
- while ( $view->file_exists('files'.$location.'/'.$filename.$ext) ) {
- $ext = $tmpext.$i;
- $i++;
- }
+ $ext = '';
+ if ( $view->file_exists('files'.$location.'/'.$filename) ) {
+ $tmpext = '.restored';
+ $ext = $tmpext;
+ $i = 1;
+ while ( $view->file_exists('files'.$location.'/'.$filename.$ext) ) {
+ $ext = $tmpext.$i;
+ $i++;
+ }
}
return $ext;
}
- /**
+ /**
* @brief get the size from a given root folder
- * @param $view file view on the root folder
- * @return size of the folder
- */
- private static function calculateSize($view) {
- $root = \OCP\Config::getSystemValue('datadirectory').$view->getAbsolutePath('');
+ * @param $view file view on the root folder
+ * @return size of the folder
+ */
+ private static function calculateSize($view) {
+ $root = \OCP\Config::getSystemValue('datadirectory').$view->getAbsolutePath('');
if (!file_exists($root)) {
return 0;
}
- $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($root), \RecursiveIteratorIterator::CHILD_FIRST);
- $size = 0;
-
+ $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($root),
+ \RecursiveIteratorIterator::CHILD_FIRST);
+ $size = 0;
+
foreach ($iterator as $path) {
$relpath = substr($path, strlen($root)-1);
if ( !$view->is_dir($relpath) ) {
$size += $view->filesize($relpath);
- }
- }
- return $size;
+ }
+ }
+ return $size;
}
-
-}
+
+}