From 7582458c3156f60e60f23687b71a67b97c00e702 Mon Sep 17 00:00:00 2001 From: Jaime Perez Crespo <jaime.perez@uninett.no> Date: Wed, 15 Apr 2015 12:27:14 +0200 Subject: [PATCH] Refactor SimpleSAML_Utilities::writeFile() to SimpleSAML_Utils_System::writeFile(). Schedule SimpleSAML_Utilities::writeFile() for removal. --- lib/SimpleSAML/Bindings/Shib13/Artifact.php | 2 +- lib/SimpleSAML/Utilities.php | 2 + lib/SimpleSAML/Utils/System.php | 54 ++++++++++++++++++++- modules/metarefresh/lib/MetaLoader.php | 4 +- 4 files changed, 57 insertions(+), 5 deletions(-) diff --git a/lib/SimpleSAML/Bindings/Shib13/Artifact.php b/lib/SimpleSAML/Bindings/Shib13/Artifact.php index d06d71592..3e8f7d392 100644 --- a/lib/SimpleSAML/Bindings/Shib13/Artifact.php +++ b/lib/SimpleSAML/Bindings/Shib13/Artifact.php @@ -139,7 +139,7 @@ class SimpleSAML_Bindings_Shib13_Artifact { $file = SimpleSAML_Utils_System::getTempDir() . DIRECTORY_SEPARATOR . sha1($certData) . '.crt'; if (!file_exists($file)) { - SimpleSAML_Utilities::writeFile($file, $certData); + SimpleSAML_Utils_System::writeFile($file, $certData); } $spKeyCertFile = SimpleSAML_Utilities::resolveCert($spMetadata->getString('privatekey')); diff --git a/lib/SimpleSAML/Utilities.php b/lib/SimpleSAML/Utilities.php index 79d93e0cd..d209a64b6 100644 --- a/lib/SimpleSAML/Utilities.php +++ b/lib/SimpleSAML/Utilities.php @@ -1927,6 +1927,8 @@ class SimpleSAML_Utilities { * * @param string $filename The name of the file. * @param string $data The data we should write to the file. + * + * @deprecated This method will be removed in SSP 2.0. Please use SimpleSAML_Utils_System::writeFile() instead. */ public static function writeFile($filename, $data, $mode=0600) { assert('is_string($filename)'); diff --git a/lib/SimpleSAML/Utils/System.php b/lib/SimpleSAML/Utils/System.php index 3445af038..15f345e46 100644 --- a/lib/SimpleSAML/Utils/System.php +++ b/lib/SimpleSAML/Utils/System.php @@ -18,7 +18,6 @@ class SimpleSAML_Utils_System const IRIX = 7; const SUNOS = 8; - /** * This function returns the Operating System we are running on. * @@ -61,6 +60,10 @@ class SimpleSAML_Utils_System * @return string Path to a temporary directory, without a trailing directory separator. * @throws SimpleSAML_Error_Exception If the temporary directory cannot be created or it exists and does not belong * to the current user. + * + * @author Andreas Solberg, UNINETT AS <andreas.solberg@uninett.no> + * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> + * @author Jaime Perez, UNINETT AS <jaime.perez@uninett.no> */ public static function getTempDir() { @@ -85,4 +88,51 @@ class SimpleSAML_Utils_System return $tempDir; } -} \ No newline at end of file + + /** + * Atomically write a file. + * + * This is a helper function for writing data atomically to a file. It does this by writing the file data to a + * temporary file, then renaming it to the required file name. + * + * @param string $filename The path to the file we want to write to. + * @param string $data The data we should write to the file. + * @param int $mode The permissions to apply to the file. Defaults to 0600. + * + * @throws SimpleSAML_Error_Exception If the file cannot be saved, permissions cannot be changed or it is not + * possible to write to the target file. + * + * @author Andreas Solberg, UNINETT AS <andreas.solberg@uninett.no> + * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> + * @author Andjelko Horvat + * @author Jaime Perez, UNINETT AS <jaime.perez@uninett.no> + */ + public static function writeFile($filename, $data, $mode = 0600) + { + assert('is_string($filename)'); + assert('is_string($data)'); + assert('is_numeric($mode)'); + + $tmpFile = self::getTempDir().DIRECTORY_SEPARATOR.rand(); + + $res = @file_put_contents($tmpFile, $data); + if ($res === false) { + throw new SimpleSAML_Error_Exception('Error saving file "'.$tmpFile. + '": '.SimpleSAML_Utilities::getLastError()); + } + + if (self::getOS() !== self::WINDOWS) { + if (!chmod($tmpFile, $mode)) { + unlink($tmpFile); + throw new SimpleSAML_Error_Exception('Error changing file mode of "'.$tmpFile. + '": '.SimpleSAML_Utilities::getLastError()); + } + } + + if (!rename($tmpFile, $filename)) { + unlink($tmpFile); + throw new SimpleSAML_Error_Exception('Error moving "'.$tmpFile.'" to "'. + $filename.'": '.SimpleSAML_Utilities::getLastError()); + } + } +} diff --git a/modules/metarefresh/lib/MetaLoader.php b/modules/metarefresh/lib/MetaLoader.php index 24790fdb3..f9393ebc6 100644 --- a/modules/metarefresh/lib/MetaLoader.php +++ b/modules/metarefresh/lib/MetaLoader.php @@ -238,7 +238,7 @@ class sspmod_metarefresh_MetaLoader { public function writeState() { if($this->changed) { SimpleSAML_Logger::debug('Writing: ' . $this->stateFile); - SimpleSAML_Utilities::writeFile( + SimpleSAML_Utils_System::writeFile( $this->stateFile, "<?php\n/* This file was generated by the metarefresh module at ".$this->getTime() . ".\n". " Do not update it manually as it will get overwritten. */\n". @@ -393,7 +393,7 @@ class sspmod_metarefresh_MetaLoader { $content .= "\n" . '?>'; - SimpleSAML_Utilities::writeFile($filename, $content, 0644); + SimpleSAML_Utils_System::writeFile($filename, $content, 0644); } elseif(is_file($filename)) { if(unlink($filename)) { SimpleSAML_Logger::debug('Deleting stale metadata file: ' . $filename); -- GitLab