From c34e6d85aa693a444477eea2ab4e036e1d22356e Mon Sep 17 00:00:00 2001 From: io42630 Date: Tue, 5 Apr 2022 07:11:27 +0200 Subject: [PATCH] _ remove surplus logs _ use SHA256 --- src/main/java/com/olexyn/ensync/Flow.java | 2 +- .../ensync/artifacts/SyncDirectory.java | 26 +++++++++---------- .../com/olexyn/ensync/artifacts/SyncFile.java | 1 - 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/olexyn/ensync/Flow.java b/src/main/java/com/olexyn/ensync/Flow.java index c3b7fcd..414e828 100644 --- a/src/main/java/com/olexyn/ensync/Flow.java +++ b/src/main/java/com/olexyn/ensync/Flow.java @@ -39,7 +39,7 @@ public class Flow implements Runnable { ); } try { - LOGGER.info("Pausing... for " + POLLING_PAUSE + "ms."); + LOGGER.info("Sleeping... for " + POLLING_PAUSE + "ms."); Thread.sleep(POLLING_PAUSE); } catch (InterruptedException ignored) { LOGGER.info("Thread interrupted."); diff --git a/src/main/java/com/olexyn/ensync/artifacts/SyncDirectory.java b/src/main/java/com/olexyn/ensync/artifacts/SyncDirectory.java index b2f6843..1ac3e00 100644 --- a/src/main/java/com/olexyn/ensync/artifacts/SyncDirectory.java +++ b/src/main/java/com/olexyn/ensync/artifacts/SyncDirectory.java @@ -124,9 +124,9 @@ public class SyncDirectory { ).collect(Collectors.toMap(Entry::getKey, Entry::getValue)); } - private String getMd5(Path path) { + private String getHash(Path path) { try (var fos = new FileInputStream(path.toFile())) { - var m = MessageDigest.getInstance("MD5"); + var m = MessageDigest.getInstance("SHA256"); byte[] data = fos.readAllBytes(); m.update(data, 0, data.length); var i = new BigInteger(1, m.digest()); @@ -155,7 +155,7 @@ public class SyncDirectory { outputList.add(line); }); - LOGGER.info("Writing " + outputList.size() + " files to " + record.getPath()); + LOGGER.info("Writing " + outputList.size() + " files to Record: " + record.getPath()); tools.writeStringListToFile(record.getPath().toString(), outputList); } @@ -233,14 +233,14 @@ public class SyncDirectory { * Overwrite other file if this file is newer. */ private void writeFileIfNewer(SyncFile thisFile, SyncFile otherFile) { - LOGGER.info("Write from: " + thisFile.toPath()); - LOGGER.info(" to: " + otherFile.toPath()); + LOGGER.info("Try write from: " + thisFile.toPath()); + LOGGER.info(" to: " + otherFile.toPath()); if (!thisFile.isFile()) { return; } if (otherFile.exists()) { - var thisMd5 = getMd5(thisFile.toPath()); - var otherMd5 = getMd5(otherFile.toPath()); - if (thisMd5 == null || otherMd5 == null) { return; } - if (thisMd5.equals(otherMd5)) { + var thisHash = getHash(thisFile.toPath()); + var otherHash = getHash(otherFile.toPath()); + if (thisHash == null || otherHash == null) { return; } + if (thisHash.equals(otherHash)) { dropAge(thisFile, otherFile); return; } else if (thisFile.isOlder(otherFile)) { @@ -254,10 +254,10 @@ public class SyncDirectory { private void dropAge(SyncFile thisFile, SyncFile otherFile) { if (thisFile.isOlder(otherFile)) { otherFile.setLastModified(thisFile.lastModified()); - LOGGER.info("Dropped age of OTHER: " + otherFile.toPath() + " to: " + otherFile.lastModified()); + LOGGER.info("Dropped age of: " + otherFile.toPath() + " -> " + otherFile.lastModified()); } else { thisFile.setLastModified(otherFile.lastModified()); - LOGGER.info("Dropped age of THIS: " + thisFile.toPath() + " to: " + thisFile.lastModified()); + LOGGER.info("Dropped age of: " + thisFile.toPath() + " -> " + thisFile.lastModified()); } } @@ -270,10 +270,10 @@ public class SyncDirectory { StandardCopyOption.COPY_ATTRIBUTES ); LOGGER.info("Copied from: " + thisFile.toPath()); - LOGGER.info(" to: " + otherFile.toPath()); + LOGGER.info(" to: " + otherFile.toPath()); } catch (IOException e) { LOGGER.severe("Could not copy file from: " + thisFile.toPath()); - LOGGER.severe(" to: " + otherFile.toPath()); + LOGGER.severe(" to: " + otherFile.toPath()); e.printStackTrace(); } } diff --git a/src/main/java/com/olexyn/ensync/artifacts/SyncFile.java b/src/main/java/com/olexyn/ensync/artifacts/SyncFile.java index d66708e..cd02bd8 100644 --- a/src/main/java/com/olexyn/ensync/artifacts/SyncFile.java +++ b/src/main/java/com/olexyn/ensync/artifacts/SyncFile.java @@ -42,7 +42,6 @@ public class SyncFile extends File { } public boolean isNewer(SyncFile otherFile) { - LOGGER.info("" + this.lastModified() + " >= " + otherFile.lastModified()); return this.lastModified() >= otherFile.lastModified(); }