From 0da4772388806434221ebff5ca6e04396ee5e42f Mon Sep 17 00:00:00 2001 From: io42630 Date: Sat, 2 Apr 2022 06:47:40 +0200 Subject: [PATCH] _ fix StateFile path _ clear lists instead of returning them --- src/main/java/com/olexyn/ensync/Flow.java | 4 +- .../com/olexyn/ensync/artifacts/StateFile.kt | 4 +- .../ensync/artifacts/SyncDirectory.java | 48 ++++++++----------- .../com/olexyn/ensync/artifacts/SyncFile.java | 24 +++++++--- 4 files changed, 40 insertions(+), 40 deletions(-) diff --git a/src/main/java/com/olexyn/ensync/Flow.java b/src/main/java/com/olexyn/ensync/Flow.java index 7f184cb..bdee97f 100644 --- a/src/main/java/com/olexyn/ensync/Flow.java +++ b/src/main/java/com/olexyn/ensync/Flow.java @@ -45,10 +45,10 @@ public class Flow implements Runnable { */ private void doSyncDirectory(SyncDirectory sd) { LOGGER.info("READ"); - sd.readStateFromFS(); + sd.readFileSystem(); - sd.makeListOfLocallyCreatedFiles(); + sd.fillListOfLocallyCreatedFiles(); sd.makeListOfLocallyDeletedFiles(); sd.makeListOfLocallyModifiedFiles(); diff --git a/src/main/java/com/olexyn/ensync/artifacts/StateFile.kt b/src/main/java/com/olexyn/ensync/artifacts/StateFile.kt index 59205fc..25d1b41 100644 --- a/src/main/java/com/olexyn/ensync/artifacts/StateFile.kt +++ b/src/main/java/com/olexyn/ensync/artifacts/StateFile.kt @@ -5,7 +5,7 @@ import java.io.File class StateFile(val targetPath: String) { fun getPath(): String { - return targetPath + Constants.STATE_FILE_NAME + return targetPath + "/" + Constants.STATE_FILE_NAME } private fun getFile(): File { @@ -13,7 +13,7 @@ class StateFile(val targetPath: String) { } fun exists(): Boolean { - return getFile().exists(); + return getFile().exists() } } \ No newline at end of file diff --git a/src/main/java/com/olexyn/ensync/artifacts/SyncDirectory.java b/src/main/java/com/olexyn/ensync/artifacts/SyncDirectory.java index da1503c..5fcc63e 100644 --- a/src/main/java/com/olexyn/ensync/artifacts/SyncDirectory.java +++ b/src/main/java/com/olexyn/ensync/artifacts/SyncDirectory.java @@ -50,16 +50,16 @@ public class SyncDirectory { /** - * Get the current state by using the `find` command. + * Read the current state of the file system. */ - public Map readStateFromFS() { + public Map readFileSystem() { //NOTE that the SyncFile().lastModifiedOld is not set here, so it is 0 by default. - Map filemap = new HashMap<>(); - getFiles() + return getFiles() .map(file -> new SyncFile(this, file.getAbsolutePath())) - .forEach(file -> filemap.put(file.getAbsolutePath(), file) - ); - return filemap; + .collect(Collectors.toMap( + SyncFile::getRelativePath, + syncFile -> syncFile + )); } @@ -91,12 +91,11 @@ public class SyncDirectory { * Compare the OLD and NEW pools. * List is cleared and created each time. */ - public Map makeListOfLocallyCreatedFiles() { - - Map fromA = readStateFromFS(); - Map substractB = readStateFile(); - - return tools.mapMinus(fromA, substractB); + public void fillListOfLocallyCreatedFiles() { + listCreated.clear(); + var fromA = readFileSystem(); + var substractB = readStateFile(); + listCreated.putAll(tools.mapMinus(fromA, substractB)); } @@ -104,27 +103,20 @@ public class SyncDirectory { * Compare the OLD and NEW pools. * List is cleared and created each time. */ - public Map makeListOfLocallyDeletedFiles() { - + public void makeListOfLocallyDeletedFiles() { + listDeleted.clear(); var fromA = readStateFile(); - var substractB = readStateFromFS(); - + var substractB = readFileSystem(); var listDeleted = tools.mapMinus(fromA, substractB); - Map swap = new HashMap<>(); - - for (var entry : listDeleted.entrySet()) { - String key = entry.getKey(); String parentKey = entry.getValue().getParent(); - if (listDeleted.containsKey(parentKey) || swap.containsKey(parentKey)) { swap.put(key, listDeleted.get(key)); } } - - return tools.mapMinus(listDeleted, swap); + listDeleted.putAll(tools.mapMinus(listDeleted, swap)); } @@ -133,13 +125,11 @@ public class SyncDirectory { * List is cleared and created each time. */ public void makeListOfLocallyModifiedFiles() { - - - Map listModified = new HashMap<>(); + listModified.clear(); Map stateFileMap = readStateFile(); - for (var freshFileEntry : readStateFromFS().entrySet()) { + for (var freshFileEntry : readFileSystem().entrySet()) { String freshFileKey = freshFileEntry.getKey(); SyncFile freshFile = freshFileEntry.getValue(); @@ -156,7 +146,6 @@ public class SyncDirectory { } } - /** * QUERY state of the filesystem at realPath. * WRITE the state of the filesystem to file. @@ -241,6 +230,7 @@ public class SyncDirectory { Path.of(thisFile.getPath()), Path.of(otherFile.getPath()) ); + otherFile.setLastModified(thisFile.lastModified()); } catch (IOException e) { LOGGER.severe("Could not copy file."); } diff --git a/src/main/java/com/olexyn/ensync/artifacts/SyncFile.java b/src/main/java/com/olexyn/ensync/artifacts/SyncFile.java index be17a5c..6260464 100644 --- a/src/main/java/com/olexyn/ensync/artifacts/SyncFile.java +++ b/src/main/java/com/olexyn/ensync/artifacts/SyncFile.java @@ -1,32 +1,42 @@ package com.olexyn.ensync.artifacts; +import com.olexyn.ensync.LogUtil; + import java.io.File; +import java.util.logging.Logger; public class SyncFile extends File { + private static final Logger LOGGER = LogUtil.get(SyncFile.class); // Very IMPORTANT field. Allows to store lastModified as it is stored in the StateFile. public long timeModifiedFromStateFile = 0; - public String relativePath; + private String relativePath; private final SyncDirectory sd; - public SyncFile(SyncDirectory sd, String pathname) { + public SyncFile(SyncDirectory sd, String absolutePath) { - super(pathname); + super(absolutePath); this.sd = sd; relativePath = this.getPath().replace(sd.directoryPath, ""); } + public String getRelativePath() { + return relativePath; + } + public void setTimeModifiedFromStateFile(long value) { timeModifiedFromStateFile = value; } public long getTimeModifiedFromStateFile() { - SyncFile record = sd.readStateFile().get(this.getPath()); - - - return record == null ? 0 : record.timeModifiedFromStateFile; + SyncFile record = sd.readStateFile().get(getRelativePath()); + if (record == null) { + LOGGER.severe("Did not find record for file in StateFile. Setting modifiedDate to MIN, thus 0"); + return 0; + } + return record.timeModifiedFromStateFile; } /**