From 7a9398d0ce1c8abd22fc2d7beb04fd57fba359f8 Mon Sep 17 00:00:00 2001 From: Danil Sokolov Date: Sun, 22 Nov 2026 10:14:09 +0000 Subject: [PATCH] Keep previous layout file if saving fails FilePersistenceHandler.store emptied the layout file before writing it, so a failed save lost the previous layout. Write to a temporary file and replace the layout file only once the layout is complete. Fixes #40 --- .../persist/FilePersistenceHandler.java | 34 ++++++++++---- .../persist/FilePersistenceHandlerTest.java | 44 +++++++++++++++++++ release-notes.txt | 1 + 3 files changed, 71 insertions(+), 8 deletions(-) diff --git a/flexdock-perspective/src/main/java/org/flexdock/perspective/persist/FilePersistenceHandler.java b/flexdock-perspective/src/main/java/org/flexdock/perspective/persist/FilePersistenceHandler.java index 1ad6e084..4c02c1e7 100644 --- a/flexdock-perspective/src/main/java/org/flexdock/perspective/persist/FilePersistenceHandler.java +++ b/flexdock-perspective/src/main/java/org/flexdock/perspective/persist/FilePersistenceHandler.java @@ -20,8 +20,12 @@ import java.io.File; import java.io.FileInputStream; -import java.io.FileOutputStream; import java.io.IOException; +import java.io.OutputStream; +import java.nio.file.AtomicMoveNotSupportedException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; import org.flexdock.docking.state.PersistenceException; import org.flexdock.perspective.persist.xml.XMLPersister; @@ -73,11 +77,29 @@ public boolean store(String persistenceKey, PerspectiveModel perspectiveModel) t // XMLDebugger.println(perspectiveModel); - FileOutputStream fos = new FileOutputStream(file); + // Write to a temporary file and replace the layout file only once it + // is complete, so that a failure keeps the previous layout + Path target = file.toPath(); + Path temp = Files.createTempFile(file.getAbsoluteFile().getParentFile().toPath(), file.getName(), ".tmp"); try { - return this.persister.store(fos, perspectiveModel); + boolean stored; + OutputStream os = Files.newOutputStream(temp); + try { + stored = this.persister.store(os, perspectiveModel); + } finally { + os.close(); + } + + if(stored) { + try { + Files.move(temp, target, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING); + } catch(AtomicMoveNotSupportedException e) { + Files.move(temp, target, StandardCopyOption.REPLACE_EXISTING); + } + } + return stored; } finally { - fos.close(); + Files.deleteIfExists(temp); } } @@ -110,10 +132,6 @@ protected void validatePerspectiveFile(File file) throws IOException { if(!dir.exists()) { dir.mkdirs(); } - - if(!file.exists()) { - file.createNewFile(); - } } diff --git a/flexdock-perspective/src/test/java/org/flexdock/perspective/persist/FilePersistenceHandlerTest.java b/flexdock-perspective/src/test/java/org/flexdock/perspective/persist/FilePersistenceHandlerTest.java index 8b570c8c..fcd92636 100644 --- a/flexdock-perspective/src/test/java/org/flexdock/perspective/persist/FilePersistenceHandlerTest.java +++ b/flexdock-perspective/src/test/java/org/flexdock/perspective/persist/FilePersistenceHandlerTest.java @@ -5,6 +5,10 @@ import org.junit.jupiter.api.io.TempDir; import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.file.Files; import static org.junit.jupiter.api.Assertions.*; @@ -16,6 +20,22 @@ private static PerspectiveModel createModel() { return new PerspectiveModel("one", "one", new Perspective[] {perspective}); } + // Writes part of a layout, then fails, as a serializer error would + private static Persister createFailingPersister() { + return new Persister() { + @Override + public boolean store(OutputStream os, PerspectiveModel model) throws IOException { + os.write(" handler.store(null, createModel())); + + assertArrayEquals(stored, Files.readAllBytes(file.toPath())); + // No temporary file is left behind + assertArrayEquals(new String[] {"default.xml"}, dir.list()); + } + + @Test + void testFailedFirstStoreLeavesNoFile(@TempDir File dir) { + FilePersistenceHandler handler = new FilePersistenceHandler(new File(dir, "default.xml"), + createFailingPersister()); + + assertThrows(IOException.class, () -> handler.store(null, createModel())); + + assertEquals(0, dir.list().length); + } + } diff --git a/release-notes.txt b/release-notes.txt index a3d2bafa..03528075 100644 --- a/release-notes.txt +++ b/release-notes.txt @@ -13,6 +13,7 @@ Updates: Fixes: * DockingPathSerializer: read all split nodes of a stored docking path +* FilePersistenceHandler: keep the previous layout file if saving fails * PerspectiveManager: ignore detached docking ports when finding the main one * DefaultDockingPort: clear() also clears the nested docking ports * DockingStateListener: validate pending layout before capturing docking state