tempmanager.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * @author Morris Jobke <hey@morrisjobke.de>
  4. * @author Robin Appelman <icewind@owncloud.com>
  5. *
  6. * @copyright Copyright (c) 2015, ownCloud, Inc.
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OC;
  23. use OCP\ILogger;
  24. use OCP\ITempManager;
  25. class TempManager implements ITempManager {
  26. /**
  27. * Current temporary files and folders
  28. *
  29. * @var string[]
  30. */
  31. protected $current = array();
  32. /**
  33. * i.e. /tmp on linux systems
  34. *
  35. * @var string
  36. */
  37. protected $tmpBaseDir;
  38. /**
  39. * @var \OCP\ILogger
  40. */
  41. protected $log;
  42. /**
  43. * @param string $baseDir
  44. * @param \OCP\ILogger $logger
  45. */
  46. public function __construct($baseDir, ILogger $logger) {
  47. $this->tmpBaseDir = $baseDir;
  48. $this->log = $logger;
  49. }
  50. /**
  51. * @param string $postFix
  52. * @return string
  53. */
  54. protected function generatePath($postFix) {
  55. if ($postFix) {
  56. $postFix = '.' . ltrim($postFix, '.');
  57. }
  58. $postFix = str_replace(['\\', '/'], '', $postFix);
  59. return $this->tmpBaseDir . '/oc_tmp_' . md5(time() . rand()) . $postFix;
  60. }
  61. /**
  62. * Create a temporary file and return the path
  63. *
  64. * @param string $postFix
  65. * @return string
  66. */
  67. public function getTemporaryFile($postFix = '') {
  68. $file = $this->generatePath($postFix);
  69. if (is_writable($this->tmpBaseDir)) {
  70. touch($file);
  71. $this->current[] = $file;
  72. return $file;
  73. } else {
  74. $this->log->warning(
  75. 'Can not create a temporary file in directory {dir}. Check it exists and has correct permissions',
  76. array(
  77. 'dir' => $this->tmpBaseDir
  78. )
  79. );
  80. return false;
  81. }
  82. }
  83. /**
  84. * Create a temporary folder and return the path
  85. *
  86. * @param string $postFix
  87. * @return string
  88. */
  89. public function getTemporaryFolder($postFix = '') {
  90. $path = $this->generatePath($postFix);
  91. if (is_writable($this->tmpBaseDir)) {
  92. mkdir($path);
  93. $this->current[] = $path;
  94. return $path . '/';
  95. } else {
  96. $this->log->warning(
  97. 'Can not create a temporary folder in directory {dir}. Check it exists and has correct permissions',
  98. array(
  99. 'dir' => $this->tmpBaseDir
  100. )
  101. );
  102. return false;
  103. }
  104. }
  105. /**
  106. * Remove the temporary files and folders generated during this request
  107. */
  108. public function clean() {
  109. $this->cleanFiles($this->current);
  110. }
  111. protected function cleanFiles($files) {
  112. foreach ($files as $file) {
  113. if (file_exists($file)) {
  114. try {
  115. \OC_Helper::rmdirr($file);
  116. } catch (\UnexpectedValueException $ex) {
  117. $this->log->warning(
  118. "Error deleting temporary file/folder: {file} - Reason: {error}",
  119. array(
  120. 'file' => $file,
  121. 'error' => $ex->getMessage()
  122. )
  123. );
  124. }
  125. }
  126. }
  127. }
  128. /**
  129. * Remove old temporary files and folders that were failed to be cleaned
  130. */
  131. public function cleanOld() {
  132. $this->cleanFiles($this->getOldFiles());
  133. }
  134. /**
  135. * Get all temporary files and folders generated by oc older than an hour
  136. *
  137. * @return string[]
  138. */
  139. protected function getOldFiles() {
  140. $cutOfTime = time() - 3600;
  141. $files = array();
  142. $dh = opendir($this->tmpBaseDir);
  143. if ($dh) {
  144. while (($file = readdir($dh)) !== false) {
  145. if (substr($file, 0, 7) === 'oc_tmp_') {
  146. $path = $this->tmpBaseDir . '/' . $file;
  147. $mtime = filemtime($path);
  148. if ($mtime < $cutOfTime) {
  149. $files[] = $path;
  150. }
  151. }
  152. }
  153. }
  154. return $files;
  155. }
  156. }