Skip to content
Snippets Groups Projects
Commit 7582458c authored by Jaime Perez Crespo's avatar Jaime Perez Crespo
Browse files

Refactor SimpleSAML_Utilities::writeFile() to SimpleSAML_Utils_System::writeFile().

Schedule SimpleSAML_Utilities::writeFile() for removal.
parent 85ad231e
No related branches found
No related tags found
No related merge requests found
...@@ -139,7 +139,7 @@ class SimpleSAML_Bindings_Shib13_Artifact { ...@@ -139,7 +139,7 @@ class SimpleSAML_Bindings_Shib13_Artifact {
$file = SimpleSAML_Utils_System::getTempDir() . DIRECTORY_SEPARATOR . sha1($certData) . '.crt'; $file = SimpleSAML_Utils_System::getTempDir() . DIRECTORY_SEPARATOR . sha1($certData) . '.crt';
if (!file_exists($file)) { if (!file_exists($file)) {
SimpleSAML_Utilities::writeFile($file, $certData); SimpleSAML_Utils_System::writeFile($file, $certData);
} }
$spKeyCertFile = SimpleSAML_Utilities::resolveCert($spMetadata->getString('privatekey')); $spKeyCertFile = SimpleSAML_Utilities::resolveCert($spMetadata->getString('privatekey'));
......
...@@ -1927,6 +1927,8 @@ class SimpleSAML_Utilities { ...@@ -1927,6 +1927,8 @@ class SimpleSAML_Utilities {
* *
* @param string $filename The name of the file. * @param string $filename The name of the file.
* @param string $data The data we should write to 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) { public static function writeFile($filename, $data, $mode=0600) {
assert('is_string($filename)'); assert('is_string($filename)');
......
...@@ -18,7 +18,6 @@ class SimpleSAML_Utils_System ...@@ -18,7 +18,6 @@ class SimpleSAML_Utils_System
const IRIX = 7; const IRIX = 7;
const SUNOS = 8; const SUNOS = 8;
/** /**
* This function returns the Operating System we are running on. * This function returns the Operating System we are running on.
* *
...@@ -61,6 +60,10 @@ class SimpleSAML_Utils_System ...@@ -61,6 +60,10 @@ class SimpleSAML_Utils_System
* @return string Path to a temporary directory, without a trailing directory separator. * @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 * @throws SimpleSAML_Error_Exception If the temporary directory cannot be created or it exists and does not belong
* to the current user. * 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() public static function getTempDir()
{ {
...@@ -85,4 +88,51 @@ class SimpleSAML_Utils_System ...@@ -85,4 +88,51 @@ class SimpleSAML_Utils_System
return $tempDir; 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());
}
}
}
...@@ -238,7 +238,7 @@ class sspmod_metarefresh_MetaLoader { ...@@ -238,7 +238,7 @@ class sspmod_metarefresh_MetaLoader {
public function writeState() { public function writeState() {
if($this->changed) { if($this->changed) {
SimpleSAML_Logger::debug('Writing: ' . $this->stateFile); SimpleSAML_Logger::debug('Writing: ' . $this->stateFile);
SimpleSAML_Utilities::writeFile( SimpleSAML_Utils_System::writeFile(
$this->stateFile, $this->stateFile,
"<?php\n/* This file was generated by the metarefresh module at ".$this->getTime() . ".\n". "<?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". " Do not update it manually as it will get overwritten. */\n".
...@@ -393,7 +393,7 @@ class sspmod_metarefresh_MetaLoader { ...@@ -393,7 +393,7 @@ class sspmod_metarefresh_MetaLoader {
$content .= "\n" . '?>'; $content .= "\n" . '?>';
SimpleSAML_Utilities::writeFile($filename, $content, 0644); SimpleSAML_Utils_System::writeFile($filename, $content, 0644);
} elseif(is_file($filename)) { } elseif(is_file($filename)) {
if(unlink($filename)) { if(unlink($filename)) {
SimpleSAML_Logger::debug('Deleting stale metadata file: ' . $filename); SimpleSAML_Logger::debug('Deleting stale metadata file: ' . $filename);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment