Parcourir la source

Make configuration saves crash-safe (#9651)

Fixes #9650

Writes the temporary config file with a checked full write followed by `fflush()` and `fsync()` before the atomic rename, so an unclean shutdown cannot leave `config.php` truncated to zero bytes. The backup is now only replaced when the current config file is non-empty, preserving the last known-good backup if a previous save was interrupted.

Adds regression tests covering a normal save (previous config backed up) and a save when the current config file is empty (existing backup preserved).

* Make configuration saves crash-safe

* Simplify comments

---------

Co-authored-by: Alexandre Alapetite <alexandre@alapetite.fr>
S M Nazmuz Sakib il y a 1 jour
Parent
commit
6048309573
2 fichiers modifiés avec 71 ajouts et 8 suppressions
  1. 10 8
      lib/Minz/Configuration.php
  2. 61 0
      tests/lib/Minz/ConfigurationTest.php

+ 10 - 8
lib/Minz/Configuration.php

@@ -224,18 +224,20 @@ class Minz_Configuration {
 		$tmp_filename = $this->config_filename . '.tmp.php';
 		$tmp_filename = $this->config_filename . '.tmp.php';
 		$back_filename = $this->config_filename . '.bak.php';
 		$back_filename = $this->config_filename . '.bak.php';
 
 
-		if (!file_put_contents($tmp_filename,
-			"<?php\nreturn " . var_export($this->data, true) . ';', LOCK_EX)) {
-			@unlink($tmp_filename);
+		// Make sure an interrupted save cannot leave the configuration truncated
+		$tmp_file = @fopen($tmp_filename, 'wb');
+		if ($tmp_file === false) {
 			return false;
 			return false;
 		}
 		}
+		$content = "<?php\nreturn " . var_export($this->data, true) . ';';
+		$saved = fwrite($tmp_file, $content) === strlen($content) && fflush($tmp_file) && fsync($tmp_file);
+		fclose($tmp_file);
 
 
-		if (!copy($this->config_filename, $back_filename)) {
-			@unlink($tmp_filename);
-			return false;
-		}
+		// An empty configuration file means a previous save was interrupted
+		$saved = $saved && (@filesize($this->config_filename) > 0 ? @copy($this->config_filename, $back_filename) : true)
+			&& @rename($tmp_filename, $this->config_filename);
 
 
-		if (!@rename($tmp_filename, $this->config_filename)) {
+		if (!$saved) {
 			@unlink($tmp_filename);
 			@unlink($tmp_filename);
 			return false;
 			return false;
 		}
 		}

+ 61 - 0
tests/lib/Minz/ConfigurationTest.php

@@ -0,0 +1,61 @@
+<?php
+declare(strict_types=1);
+use PHPUnit\Framework\TestCase;
+
+class ConfigurationTest extends TestCase {
+	/** @var array<string,string> */
+	private array $files = [];
+
+	#[\Override]
+	protected function setUp(): void {
+		$directory = sys_get_temp_dir() . '/freshrss-config-test-' . bin2hex(random_bytes(8));
+		mkdir($directory);
+		$this->files['directory'] = $directory;
+		$this->files['config'] = $directory . '/config.php';
+		$this->files['default'] = $directory . '/default.php';
+		$this->files['backup'] = $this->files['config'] . '.bak.php';
+		file_put_contents($this->files['default'], "<?php\nreturn ['title' => 'Default'];\n");
+	}
+
+	#[\Override]
+	protected function tearDown(): void {
+		foreach ($this->files as $file) {
+			if (is_file($file)) {
+				@unlink($file);
+			}
+		}
+		@rmdir($this->files['directory']);
+	}
+
+	private function configuration(): Minz_Configuration {
+		$namespace = 'test-' . bin2hex(random_bytes(8));
+		Minz_Configuration::register($namespace, $this->files['config'], $this->files['default']);
+		return Minz_Configuration::get($namespace);
+	}
+
+	public function testSaveBacksUpThePreviousConfiguration(): void {
+		$previous = "<?php\nreturn ['title' => 'Previous'];\n";
+		file_put_contents($this->files['config'], $previous);
+
+		$configuration = $this->configuration();
+		$configuration->title = 'Current';
+		self::assertTrue($configuration->save());
+
+		self::assertSame($previous, file_get_contents($this->files['backup']));
+		self::assertSame(['title' => 'Current'], include $this->files['config']);
+		self::assertFileDoesNotExist($this->files['config'] . '.tmp.php');
+	}
+
+	public function testSaveKeepsTheBackupWhenTheConfigurationFileIsEmpty(): void {
+		$goodBackup = "<?php\nreturn ['title' => 'Good backup'];\n";
+		file_put_contents($this->files['backup'], $goodBackup);
+		file_put_contents($this->files['config'], '');
+
+		$configuration = $this->configuration();
+		$configuration->title = 'Recovered';
+		self::assertTrue($configuration->save());
+
+		self::assertSame($goodBackup, file_get_contents($this->files['backup']));
+		self::assertSame(['title' => 'Recovered'], include $this->files['config']);
+	}
+}