summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorRobin Appelman <icewind@owncloud.com>2014-10-22 17:33:36 +0200
committerRobin Appelman <icewind@owncloud.com>2015-01-08 16:22:44 +0100
commit299ff5a3b121bff8f4c72c5f0b1eb4e0831aed10 (patch)
tree9b298204d0a6003649b1cd1ec0e88ea028af2f27 /lib
parent441cdccc3ecab5255875d35d573d430400cb312d (diff)
downloadnextcloud-server-299ff5a3b121bff8f4c72c5f0b1eb4e0831aed10.tar.gz
nextcloud-server-299ff5a3b121bff8f4c72c5f0b1eb4e0831aed10.zip
Add \OC\TempManager to handle creating and cleaning temporary files
Diffstat (limited to 'lib')
-rw-r--r--lib/private/server.php12
-rw-r--r--lib/private/tempmanager.php145
-rw-r--r--lib/public/iservercontainer.php7
-rw-r--r--lib/public/itempmanager.php38
4 files changed, 202 insertions, 0 deletions
diff --git a/lib/private/server.php b/lib/private/server.php
index 0e8af4b797e..f1c823e0dd0 100644
--- a/lib/private/server.php
+++ b/lib/private/server.php
@@ -153,6 +153,10 @@ class Server extends SimpleContainer implements IServerContainer {
$config = $c->query('AllConfig');
return new HTTPHelper($config);
});
+ $this->registerService('TempManager', function ($c) {
+ /** @var Server $c */
+ return new TempManager(get_temp_dir(), \OC_Log::$object);
+ });
}
/**
@@ -356,4 +360,12 @@ class Server extends SimpleContainer implements IServerContainer {
return $this->query('HTTPHelper');
}
+ /**
+ * Get the manager for temporary files and folders
+ *
+ * @return \OCP\ITempManager
+ */
+ function getTempManager() {
+ return $this->query('TempManager');
+ }
}
diff --git a/lib/private/tempmanager.php b/lib/private/tempmanager.php
new file mode 100644
index 00000000000..4b9ac6b6b1e
--- /dev/null
+++ b/lib/private/tempmanager.php
@@ -0,0 +1,145 @@
+<?php
+
+/**
+ * Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace OC;
+
+use OCP\ITempManager;
+
+class TempManager implements ITempManager {
+ /**
+ * Current temporary files and folders
+ *
+ * @var string[]
+ */
+ protected $current = array();
+
+ /**
+ * i.e. /tmp on linux systems
+ *
+ * @var string
+ */
+ protected $tmpBaseDir;
+
+ /**
+ * @var \OC\Log
+ */
+ protected $log;
+
+ /**
+ * @param string $baseDir
+ * @param \OC\Log $logger
+ */
+ public function __construct($baseDir, Log $logger) {
+ $this->tmpBaseDir = $baseDir;
+ $this->log = $logger;
+ }
+
+ protected function generatePath($postFix) {
+ return $this->tmpBaseDir . '/oc_tmp_' . md5(time() . rand()) . $postFix;
+ }
+
+ /**
+ * Create a temporary file and return the path
+ *
+ * @param string $postFix
+ * @return string
+ */
+ public function getTemporaryFile($postFix = '') {
+ $file = $this->generatePath($postFix);
+ if (is_writable($this->tmpBaseDir)) {
+ touch($file);
+ $this->current[] = $file;
+ return $file;
+ } else {
+ $this->log->warning(
+ 'Can not create a temporary file in directory {dir}. Check it exists and has correct permissions',
+ array(
+ 'dir' => $this->tmpBaseDir
+ )
+ );
+ return false;
+ }
+ }
+
+ /**
+ * Create a temporary folder and return the path
+ *
+ * @param string $postFix
+ * @return string
+ */
+ public function getTemporaryFolder($postFix = '') {
+ $path = $this->generatePath($postFix);
+ if (is_writable($this->tmpBaseDir)) {
+ mkdir($path);
+ $this->current[] = $path;
+ return $path . '/';
+ } else {
+ $this->log->warning(
+ 'Can not create a temporary folder in directory {dir}. Check it exists and has correct permissions',
+ array(
+ 'dir' => $this->tmpBaseDir
+ )
+ );
+ return false;
+ }
+ }
+
+ /**
+ * Remove the temporary files and folders generated during this request
+ */
+ public function clean() {
+ $this->cleanFiles($this->current);
+ }
+
+ protected function cleanFiles($files) {
+ foreach ($files as $file) {
+ if (file_exists($file)) {
+ try {
+ \OC_Helper::rmdirr($file);
+ } catch (\UnexpectedValueException $ex) {
+ $this->log->warning(
+ "Error deleting temporary file/folder: {file} - Reason: {error}",
+ array(
+ 'file' => $file,
+ 'error' => $ex->getMessage()
+ )
+ );
+ }
+ }
+ }
+ }
+
+ /**
+ * Remove old temporary files and folders that were failed to be cleaned
+ */
+ public function cleanOld() {
+ $this->cleanFiles($this->getOldFiles());
+ }
+
+ /**
+ * Get all temporary files and folders generated by oc older than an hour
+ *
+ * @return string[]
+ */
+ protected function getOldFiles() {
+ $cutOfTime = time() - 3600;
+ $files = array();
+ $dh = opendir($this->tmpBaseDir);
+ while (($file = readdir($dh)) !== false) {
+ if (substr($file, 0, 7) === 'oc_tmp_') {
+ $path = $this->tmpBaseDir . '/' . $file;
+ $mtime = filemtime($path);
+ if ($mtime < $cutOfTime) {
+ $files[] = $path;
+ }
+ }
+ }
+ return $files;
+ }
+}
diff --git a/lib/public/iservercontainer.php b/lib/public/iservercontainer.php
index dc59e01bced..f44e5fba26c 100644
--- a/lib/public/iservercontainer.php
+++ b/lib/public/iservercontainer.php
@@ -174,4 +174,11 @@ interface IServerContainer {
* @return \OC\HTTPHelper
*/
function getHTTPHelper();
+
+ /**
+ * Get the manager for temporary files and folders
+ *
+ * @return \OCP\ITempManager
+ */
+ function getTempManager();
}
diff --git a/lib/public/itempmanager.php b/lib/public/itempmanager.php
new file mode 100644
index 00000000000..ebd94978038
--- /dev/null
+++ b/lib/public/itempmanager.php
@@ -0,0 +1,38 @@
+<?php
+
+/**
+ * Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace OCP;
+
+interface ITempManager {
+ /**
+ * Create a temporary file and return the path
+ *
+ * @param string $postFix
+ * @return string
+ */
+ public function getTemporaryFile($postFix = '');
+
+ /**
+ * Create a temporary folder and return the path
+ *
+ * @param string $postFix
+ * @return string
+ */
+ public function getTemporaryFolder($postFix = '');
+
+ /**
+ * Remove the temporary files and folders generated during this request
+ */
+ public function clean();
+
+ /**
+ * Remove old temporary files and folders that were failed to be cleaned
+ */
+ public function cleanOld();
+}