mirror of
https://github.com/unraid/webgui.git
synced 2026-05-05 01:09:25 -05:00
Merge pull request #1732 from Squidly271/patch-39
Use atomic writes for updates of config files
This commit is contained in:
@@ -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
@@ -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'])) {
|
||||
|
||||
Reference in New Issue
Block a user