From a16bc9a318af3dfd0c0a18440cadf699985b2d35 Mon Sep 17 00:00:00 2001 From: Maarten Billemont Date: Tue, 10 Jul 2018 00:54:11 -0400 Subject: [PATCH] Don't save changes made to model while it's being read from file. --- .../masterpassword/model/impl/Changeable.java | 25 +++++++++++++------ .../masterpassword/model/impl/MPFileUser.java | 2 ++ .../model/impl/MPFlatUnmarshaller.java | 5 +++- .../masterpassword/model/impl/MPJSONFile.java | 2 +- 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/Changeable.java b/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/Changeable.java index b09f91e4..5efdf5a9 100644 --- a/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/Changeable.java +++ b/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/Changeable.java @@ -11,21 +11,22 @@ public class Changeable { private static final ExecutorService changeExecutor = Executors.newSingleThreadExecutor(); - private boolean changed; - private boolean batchingChanges; + private boolean changed; + private Grouping grouping = Grouping.APPLY; void setChanged() { synchronized (changeExecutor) { if (changed) return; - changed = true; - if (batchingChanges) + if (grouping != Grouping.IGNORE) + changed = true; + if (grouping != Grouping.APPLY) return; changeExecutor.submit( () -> { synchronized (changeExecutor) { - if (batchingChanges) + if (grouping != Grouping.APPLY) return; changed = false; } @@ -40,13 +41,19 @@ public class Changeable { public void beginChanges() { synchronized (changeExecutor) { - batchingChanges = true; + grouping = Grouping.BATCH; + } + } + + public void ignoreChanges() { + synchronized (changeExecutor) { + grouping = Grouping.IGNORE; } } public boolean endChanges() { synchronized (changeExecutor) { - batchingChanges = false; + grouping = Grouping.APPLY; if (changed) { this.changed = false; @@ -56,4 +63,8 @@ public class Changeable { return false; } } + + private enum Grouping { + APPLY, BATCH, IGNORE + } } diff --git a/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFileUser.java b/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFileUser.java index 3cb7a3dc..6d095005 100755 --- a/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFileUser.java +++ b/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFileUser.java @@ -133,6 +133,8 @@ public class MPFileUser extends MPBasicUser { public void setJSON(final MPJSONFile json) { this.json = json; + + setChanged(); } @Nonnull diff --git a/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFlatUnmarshaller.java b/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFlatUnmarshaller.java index 0c37fea9..e3b00dbd 100644 --- a/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFlatUnmarshaller.java +++ b/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFlatUnmarshaller.java @@ -74,11 +74,13 @@ public class MPFlatUnmarshaller implements MPUnmarshaller { if (!headerStarted) // Starts the header. headerStarted = true; - else + else { // Ends the header. user = new MPFileUser( fullName, keyID, MPAlgorithm.Version.fromInt( mpVersion ).getAlgorithm(), avatar, defaultType, new Instant( 0 ), MPMarshalFormat.Flat, clearContent? MPMarshaller.ContentMode.VISIBLE: MPMarshaller.ContentMode.PROTECTED ); + user.ignoreChanges(); + } // Comment. else if (line.startsWith( "#" )) { @@ -154,6 +156,7 @@ public class MPFlatUnmarshaller implements MPUnmarshaller { if (user == null) throw new MPMarshalException( "No full header found in import file." ); + user.endChanges(); return user; } } diff --git a/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPJSONFile.java b/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPJSONFile.java index 4e3f9435..12521888 100644 --- a/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPJSONFile.java +++ b/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPJSONFile.java @@ -145,7 +145,7 @@ public class MPJSONFile extends MPJSONAnyObject { (user.default_type != null)? user.default_type: algorithm.mpw_default_result_type(), (user.last_used != null)? MPConstants.dateTimeFormatter.parseDateTime( user.last_used ): new Instant(), MPMarshalFormat.JSON, export.redacted? MPMarshaller.ContentMode.PROTECTED: MPMarshaller.ContentMode.VISIBLE ); - model.beginChanges(); + model.ignoreChanges(); model.setJSON( this ); if (masterPassword != null) model.authenticate( masterPassword );