Skip to content

Commit

Permalink
Ensure that the actual project file is only only overwritten, when th…
Browse files Browse the repository at this point in the history
…e saving process was successful and not interrupted

* Strategy to achieve this:
 * When saving the mastodon project file, it is first saved to projectname.mastodon_pending
 * When saving to this file was successful, projectname.mastodon gets overwritten with projectname.mastodon_pending
 * In the past, it has been observed that *.mastodon files were corrupted and could not be read anymore
 * Typical saving times for a project range from ~0.2s to ~2s (depending on project size, attributes, etc.). Not very long, but long enough to kill mastodon during the saving process.
  • Loading branch information
stefanhahmann committed Jun 7, 2024
1 parent 2b7c4aa commit 40ce2fc
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/main/java/org/mastodon/mamut/io/ProjectSaver.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 );
Expand All @@ -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,
Expand Down

0 comments on commit 40ce2fc

Please sign in to comment.