Merge pull request #1732 from Squidly271/patch-39

Use atomic writes for updates of config files
This commit is contained in:
tom mortensen
2024-05-18 10:42:41 -07:00
committed by GitHub
2 changed files with 21 additions and 2 deletions
@@ -22,6 +22,23 @@ $_proxy_ = '__';
$_arrow_ = '»';
// Wrapper functions
function file_put_contents_atomic($filename,$data, $flags = 0, $context = null) {
while (true) {
$suffix = rand();
if ( ! is_file("$filename$suffix") )
break;
}
$renResult = false;
$writeResult = @file_put_contents("$filename$suffix",$data,$flags,$context) === strlen($data);
if ( $writeResult )
$renResult = @rename("$filename$suffix",$filename);
if ( ! $writeResult || ! $renResult ) {
my_logger("File_put_contents_atomic failed to write / rename $filename");
@unlink("$filename$suffix");
return false;
}
return strlen($data);
}
function parse_plugin_cfg($plugin, $sections=false, $scanner=INI_SCANNER_NORMAL) {
global $docroot;
$ram = "$docroot/plugins/$plugin/default.cfg";
+4 -2
View File
@@ -40,7 +40,9 @@ function write_log($string) {
readfile('update.htm');
flush();
$docroot = $_SERVER['DOCUMENT_ROOT'];
$docroot = $_SERVER['DOCUMENT_ROOT'] ?: "/usr/local/emhttp";
require_once "$docroot/plugins/dynamix/include/Wrappers.php";
if (isset($_POST['#file'])) {
$file = $_POST['#file'];
$raw_file = isset($_POST['#raw_file']) ? ($_POST['#raw_file'] === 'true') : false;
@@ -75,7 +77,7 @@ if (isset($_POST['#file'])) {
foreach ($keys as $key => $value) if (strlen($value) || !$cleanup) $text .= "$key=\"$value\"\n";
}
@mkdir(dirname($file));
file_put_contents($file, $text);
file_put_contents_atomic($file,$text);
}
}
if (isset($_POST['#command'])) {