From 0eed9eccea7dcedff96953e7745c3f76f2b247e0 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Fri, 29 Aug 2014 20:08:29 -0700 Subject: [PATCH] CRM_Core_CodeGen_Util_File - Better tempdir creation --- CRM/Core/CodeGen/Util/File.php | 20 +++++++------------- CRM/Core/CodeGen/Util/Template.php | 2 +- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/CRM/Core/CodeGen/Util/File.php b/CRM/Core/CodeGen/Util/File.php index 47f99ea623..5784c7ccdf 100644 --- a/CRM/Core/CodeGen/Util/File.php +++ b/CRM/Core/CodeGen/Util/File.php @@ -17,11 +17,16 @@ class CRM_Core_CodeGen_Util_File { /** * @param $dir */ - static function removeDir($dir) { + static function cleanTempDir($dir) { foreach (glob("$dir/*") as $tempFile) { unlink($tempFile); } rmdir($dir); + if (preg_match(':^(.*)\.d$:', $dir, $matches)) { + if (file_exists($matches[1])) { + unlink($matches[1]); + } + } } /** @@ -30,18 +35,7 @@ class CRM_Core_CodeGen_Util_File { * @return string */ static function createTempDir($prefix) { - if (isset($_SERVER['TMPDIR'])) { - $tempDir = $_SERVER['TMPDIR']; - } - else { - $tempDir = '/tmp'; - } - - $newTempDir = $tempDir . '/' . $prefix . rand(1, 10000); - if (function_exists('posix_geteuid')) { - $newTempDir .= '_' . posix_geteuid(); - } - + $newTempDir = tempnam(sys_get_temp_dir(), $prefix) . '.d'; if (file_exists($newTempDir)) { self::removeDir($newTempDir); } diff --git a/CRM/Core/CodeGen/Util/Template.php b/CRM/Core/CodeGen/Util/Template.php index f9a509f584..3d14b8896f 100644 --- a/CRM/Core/CodeGen/Util/Template.php +++ b/CRM/Core/CodeGen/Util/Template.php @@ -50,7 +50,7 @@ class CRM_Core_CodeGen_Util_Template { } function __destruct() { - CRM_Core_CodeGen_Util_File::removeDir($this->compileDir); + CRM_Core_CodeGen_Util_File::cleanTempDir($this->compileDir); } /** -- 2.25.1