diff --git a/src/main/java/org/mastodon/mamut/io/ProjectSaver.java b/src/main/java/org/mastodon/mamut/io/ProjectSaver.java index f72644c81..8bd93c1ac 100644 --- a/src/main/java/org/mastodon/mamut/io/ProjectSaver.java +++ b/src/main/java/org/mastodon/mamut/io/ProjectSaver.java @@ -73,6 +73,7 @@ import org.mastodon.ui.util.FileChooser; import org.mastodon.ui.util.FileChooser.SelectionMode; import org.mastodon.util.BDVImagePlusExporter; +import org.mastodon.util.FileUtils; import org.mastodon.views.bdv.SharedBigDataViewerData; import org.scijava.Context; @@ -256,8 +257,14 @@ public static synchronized void saveProject( final File saveTo, final ProjectMod final MamutProject project = appModel.getProject(); final File tmpDatasetXml = ProjectLoader.originalOrBackupDatasetXml( project ); + // Save to a temporary file or directory first. + String suffix = "_pending"; + File saveToPending = FileUtils.createFileOrDirectoryWithSuffix( saveTo, suffix ); + if ( saveToPending == null ) + saveToPending = saveTo; + // Possibly update project root. - project.setProjectRoot( saveTo ); + project.setProjectRoot( saveToPending ); try (final MamutProject.ProjectWriter writer = project.openForWriting()) { MamutProjectIO.save( project, writer ); @@ -272,6 +279,11 @@ public static synchronized void saveProject( final File saveTo, final ProjectMod // Set save point. model.setSavePoint(); } + project.setProjectRoot( saveTo ); + Files.delete( saveTo.toPath() ); + boolean success = saveToPending.renameTo( saveTo ); + if ( !success ) + throw new IOException( "Could not rename " + saveToPending + " to " + saveTo ); // Save BDV settings. // Imperfect because a full saving requires have a view opened,