1
0

tempmanager.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace OC;
  9. use OCP\ILogger;
  10. use OCP\ITempManager;
  11. class TempManager implements ITempManager {
  12. /**
  13. * Current temporary files and folders
  14. *
  15. * @var string[]
  16. */
  17. protected $current = array();
  18. /**
  19. * i.e. /tmp on linux systems
  20. *
  21. * @var string
  22. */
  23. protected $tmpBaseDir;
  24. /**
  25. * @var \OCP\ILogger
  26. */
  27. protected $log;
  28. /**
  29. * @param string $baseDir
  30. * @param \OCP\ILogger $logger
  31. */
  32. public function __construct($baseDir, ILogger $logger) {
  33. $this->tmpBaseDir = $baseDir;
  34. $this->log = $logger;
  35. }
  36. protected function generatePath($postFix) {
  37. return $this->tmpBaseDir . '/oc_tmp_' . md5(time() . rand()) . $postFix;
  38. }
  39. /**
  40. * Create a temporary file and return the path
  41. *
  42. * @param string $postFix
  43. * @return string
  44. */
  45. public function getTemporaryFile($postFix = '') {
  46. $file = $this->generatePath($postFix);
  47. if (is_writable($this->tmpBaseDir)) {
  48. touch($file);
  49. $this->current[] = $file;
  50. return $file;
  51. } else {
  52. $this->log->warning(
  53. 'Can not create a temporary file in directory {dir}. Check it exists and has correct permissions',
  54. array(
  55. 'dir' => $this->tmpBaseDir
  56. )
  57. );
  58. return false;
  59. }
  60. }
  61. /**
  62. * Create a temporary folder and return the path
  63. *
  64. * @param string $postFix
  65. * @return string
  66. */
  67. public function getTemporaryFolder($postFix = '') {
  68. $path = $this->generatePath($postFix);
  69. if (is_writable($this->tmpBaseDir)) {
  70. mkdir($path);
  71. $this->current[] = $path;
  72. return $path . '/';
  73. } else {
  74. $this->log->warning(
  75. 'Can not create a temporary folder 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. * Remove the temporary files and folders generated during this request
  85. */
  86. public function clean() {
  87. $this->cleanFiles($this->current);
  88. }
  89. protected function cleanFiles($files) {
  90. foreach ($files as $file) {
  91. if (file_exists($file)) {
  92. try {
  93. \OC_Helper::rmdirr($file);
  94. } catch (\UnexpectedValueException $ex) {
  95. $this->log->warning(
  96. "Error deleting temporary file/folder: {file} - Reason: {error}",
  97. array(
  98. 'file' => $file,
  99. 'error' => $ex->getMessage()
  100. )
  101. );
  102. }
  103. }
  104. }
  105. }
  106. /**
  107. * Remove old temporary files and folders that were failed to be cleaned
  108. */
  109. public function cleanOld() {
  110. $this->cleanFiles($this->getOldFiles());
  111. }
  112. /**
  113. * Get all temporary files and folders generated by oc older than an hour
  114. *
  115. * @return string[]
  116. */
  117. protected function getOldFiles() {
  118. $cutOfTime = time() - 3600;
  119. $files = array();
  120. $dh = opendir($this->tmpBaseDir);
  121. if ($dh) {
  122. while (($file = readdir($dh)) !== false) {
  123. if (substr($file, 0, 7) === 'oc_tmp_') {
  124. $path = $this->tmpBaseDir . '/' . $file;
  125. $mtime = filemtime($path);
  126. if ($mtime < $cutOfTime) {
  127. $files[] = $path;
  128. }
  129. }
  130. }
  131. }
  132. return $files;
  133. }
  134. }