From e5d3dd9a03f795995233e2b4c56303f321f1840f Mon Sep 17 00:00:00 2001 From: Gravit Date: Thu, 18 Oct 2018 19:22:29 +0700 Subject: [PATCH] =?UTF-8?q?=D0=A2=D0=B5=D1=85=D0=BD=D0=B8=D1=87=D0=B5?= =?UTF-8?q?=D1=81=D0=BA=D0=B0=D1=8F=20=D0=B2=D0=BE=D0=B7=D0=BC=D0=BE=D0=B6?= =?UTF-8?q?=D0=BD=D0=BE=D1=81=D1=82=D1=8C=20=D1=81=D1=80=D0=B0=D0=B2=D0=BD?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B4=D0=B8=D1=80=D0=B5=D0=BA=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=B8=D0=B9,=20=D0=B0=20=D0=B7=D0=BD=D0=B0=D1=87?= =?UTF-8?q?=D0=B8=D1=82=20=D0=B8=20=D0=BF=D0=BE=D0=B8=D1=81=D0=BA=D0=B0=20?= =?UTF-8?q?=D1=84=D0=B0=D0=B9=D0=BB=D0=BE=D0=B2=20=D0=B2=20=D0=B4=D1=80?= =?UTF-8?q?=D1=83=D0=B3=D0=B8=D1=85=20=D0=BA=D0=BB=D0=B8=D0=B5=D0=BD=D1=82?= =?UTF-8?q?=D0=B0=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ru/gravit/launcher/hasher/HashedDir.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/libLauncher/src/main/java/ru/gravit/launcher/hasher/HashedDir.java b/libLauncher/src/main/java/ru/gravit/launcher/hasher/HashedDir.java index 054edd4f..cb705b23 100644 --- a/libLauncher/src/main/java/ru/gravit/launcher/hasher/HashedDir.java +++ b/libLauncher/src/main/java/ru/gravit/launcher/hasher/HashedDir.java @@ -143,6 +143,12 @@ public Diff diff(HashedDir other, FileNameMatcher matcher) { HashedDir extra = other.sideDiff(this, matcher, new LinkedList<>(), false); return new Diff(mismatch, extra); } + @LauncherAPI + public Diff compare(HashedDir other, FileNameMatcher matcher) { + HashedDir mismatch = sideDiff(other, matcher, new LinkedList<>(), true); + HashedDir extra = other.sideDiff(this, matcher, new LinkedList<>(), false); + return new Diff(mismatch, extra); + } public void remove(String name) { map.remove(name); @@ -261,6 +267,58 @@ private HashedDir sideDiff(HashedDir other, FileNameMatcher matcher, Deque path, boolean mismatchList) { + HashedDir diff = new HashedDir(); + for (Entry mapEntry : map.entrySet()) { + String name = mapEntry.getKey(); + HashedEntry entry = mapEntry.getValue(); + path.add(name); + + // Should update? + boolean shouldUpdate = matcher == null || matcher.shouldUpdate(path); + + // Not found or of different type + Type type = entry.getType(); + HashedEntry otherEntry = other.map.get(name); + if (otherEntry == null || otherEntry.getType() != type) { + if (shouldUpdate || mismatchList && otherEntry == null) { + diff.map.put(name, entry); + + // Should be deleted! + if (!mismatchList) + entry.flag = true; + } + path.removeLast(); + continue; + } + + // Compare entries based on type + switch (type) { + case FILE: + HashedFile file = (HashedFile) entry; + HashedFile otherFile = (HashedFile) otherEntry; + if (mismatchList && shouldUpdate && file.isSame(otherFile)) + diff.map.put(name, entry); + break; + case DIR: + HashedDir dir = (HashedDir) entry; + HashedDir otherDir = (HashedDir) otherEntry; + if (mismatchList || shouldUpdate) { // Maybe isn't need to go deeper? + HashedDir mismatch = dir.sideCompare(otherDir, matcher, path, mismatchList); + if (!mismatch.isEmpty()) + diff.map.put(name, mismatch); + } + break; + default: + throw new AssertionError("Unsupported hashed entry type: " + type.name()); + } + + // Remove this path entry + path.removeLast(); + } + return diff; + } + @Override public long size() { return map.values().stream().mapToLong(HashedEntry::size).sum();