diff options
author | Robin Appelman <icewind@owncloud.com> | 2014-10-22 17:33:36 +0200 |
---|---|---|
committer | Robin Appelman <icewind@owncloud.com> | 2014-10-24 12:18:46 +0200 |
commit | 83c74b80ad826af60d894ba91bb1e56fd2005d32 (patch) | |
tree | a9729bb74e9a69ef5e05cca79cf2ae798a4c51de /lib/private | |
parent | 0525341a1243bcf1ef992407af524e391efd7624 (diff) | |
download | nextcloud-server-83c74b80ad826af60d894ba91bb1e56fd2005d32.tar.gz nextcloud-server-83c74b80ad826af60d894ba91bb1e56fd2005d32.zip |
Add \OC\TempManager to handle creating and cleaning temporary files
Diffstat (limited to 'lib/private')
-rw-r--r-- | lib/private/server.php | 13 | ||||
-rw-r--r-- | lib/private/tempmanager.php | 146 |
2 files changed, 159 insertions, 0 deletions
diff --git a/lib/private/server.php b/lib/private/server.php index b0d63af1554..34fd8ab48fd 100644 --- a/lib/private/server.php +++ b/lib/private/server.php @@ -256,6 +256,10 @@ class Server extends SimpleContainer implements IServerContainer { return new NullQueryLogger(); } }); + $this->registerService('TempManager', function ($c) { + /** @var Server $c */ + return new TempManager(get_temp_dir(), $c->getLogger()); + }); } /** @@ -617,4 +621,13 @@ class Server extends SimpleContainer implements IServerContainer { function getQueryLogger() { return $this->query('QueryLogger'); } + + /** + * 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..a3bb07f9d63 --- /dev/null +++ b/lib/private/tempmanager.php @@ -0,0 +1,146 @@ +<?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\ILogger; +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 \OCP\ILogger + */ + protected $log; + + /** + * @param string $baseDir + * @param \OCP\ILogger $logger + */ + public function __construct($baseDir, ILogger $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; + } +} |