From cfa62d01797a0ce4c2eb98ced4e017811d772d0b Mon Sep 17 00:00:00 2001 From: Ivan Olexyn Date: Sun, 17 May 2020 21:27:06 +0200 Subject: [PATCH] + change modif-date of parent folder on write. --- src/com/olexyn/ensync/Flow.java | 4 +- .../ensync/artifacts/SyncDirectory.java | 81 +++++++++++++++---- src/com/olexyn/ensync/artifacts/SyncFile.java | 8 +- 3 files changed, 72 insertions(+), 21 deletions(-) diff --git a/src/com/olexyn/ensync/Flow.java b/src/com/olexyn/ensync/Flow.java index ec7f83e..1c06c65 100644 --- a/src/com/olexyn/ensync/Flow.java +++ b/src/com/olexyn/ensync/Flow.java @@ -34,8 +34,8 @@ public class Flow implements Runnable { state = "READ"; SD.readFreshState(); - SD.makeListCreated(); - SD.makeListDeleted(); + SD.listCreated = SD.makeListCreated(); + SD.listDeleted = SD.makeListDeleted(); SD.listModified = SD.makeListModified(); SD.doCreate(); diff --git a/src/com/olexyn/ensync/artifacts/SyncDirectory.java b/src/com/olexyn/ensync/artifacts/SyncDirectory.java index a7b7c75..acb38c8 100644 --- a/src/com/olexyn/ensync/artifacts/SyncDirectory.java +++ b/src/com/olexyn/ensync/artifacts/SyncDirectory.java @@ -95,14 +95,13 @@ public class SyncDirectory { /** * Compare the OLD and NEW pools. * List is cleared and created each time. - * - * @return */ - public void makeListCreated() { + public Map makeListCreated() { + Map fromA = readFreshState(); Map substractB = readStateFile(); - listCreated = tools.mapMinus(fromA, substractB); + return tools.mapMinus(fromA, substractB); } @@ -110,11 +109,12 @@ public class SyncDirectory { * Compare the OLD and NEW pools. * List is cleared and created each time. */ - public void makeListDeleted() { + public Map makeListDeleted() { + Map fromA = readStateFile(); Map substractB = readFreshState(); - listDeleted = tools.mapMinus(fromA, substractB); + Map listDeleted = tools.mapMinus(fromA, substractB); Map swap = new HashMap<>(); @@ -124,15 +124,12 @@ public class SyncDirectory { String key = entry.getKey(); String parentKey = entry.getValue().getParent(); - if (listDeleted.containsKey(parentKey) || swap.containsKey(parentKey)) { swap.put(key, listDeleted.get(key)); } } - listDeleted = tools.mapMinus(listDeleted, swap); - - + return tools.mapMinus(listDeleted, swap); } @@ -142,7 +139,8 @@ public class SyncDirectory { */ public Map makeListModified() { - listModified.clear(); + Map listModified = new HashMap<>(); + Map stateFileMap = readStateFile(); for (var freshFileEntry : readFreshState().entrySet()) { @@ -153,11 +151,11 @@ public class SyncDirectory { if (freshFile.isDirectory()) { continue;} // no need to modify Directories, the Filesystem will do that, if a File changed. // If KEY exists in OLD , thus FILE was NOT created. - if (stateFileMap.containsKey(freshFileKey)) { + boolean oldFileExists = stateFileMap.containsKey(freshFileKey); + boolean fileIsFresher = freshFile.getTimeModified() > freshFile.getTimeModifiedFromStateFile(); - if (freshFile.getTimeModified() > freshFile.getTimeModifiedFromStateFile()) { - listModified.put(freshFileKey, freshFile); - } + if (oldFileExists && fileIsFresher) { + listModified.put(freshFileKey, freshFile); } } return listModified; @@ -257,7 +255,12 @@ public class SyncDirectory { SyncFile otherFile = new SyncFile(otherSD, otherSD.path + thisFile.relativePath); + if (!otherFile.exists()) { return;} + // if the otherFile was created with ensync it will have the == TimeModified. + long thisFileTimeModified = thisFile.getTimeModified(); + long otherFileTimeModified = otherFile.getTimeModified(); + if (thisFile.getTimeModified() >= otherFile.getTimeModified()) { List cmd = List.of("rm", "-r", info.otherFilePath); x.execute(cmd); @@ -299,17 +302,61 @@ public class SyncDirectory { if (thisFile.isFile()) { + if (!info.otherParentFile.exists()) { - List cmd = List.of("mkdir", "-p", info.otherParentPath); - x.execute(cmd); + makeParentChain(otherFile, thisFile); + // List cmd = List.of("mkdir", "-p", info.otherParentPath); + //x.execute(cmd); } List cmd = List.of("cp", "-p", info.thisFilePath, info.otherFilePath); x.execute(cmd); + copyModifDate(thisFile.getParentFile(), otherFile.getParentFile()); } } + private void makeParentChain(File otherFile, File thisFile) { + try { + File otherParent = new File(otherFile.getParent()); + File thisParent = new File(thisFile.getParent()); + + if (!otherParent.exists()) { + makeParentChain(otherParent, thisParent); + makeParentChain(otherFile, thisFile); + + } else if (thisFile.isDirectory()) { + + List cmd = List.of("mkdir", otherFile.getPath()); + x.execute(cmd); + + + cmd = List.of("stat", "--format", "%y", thisFile.getPath()); + + + String mDate = x.execute(cmd).output.readLine(); + + + cmd = List.of("touch", "-m", "--date=" + mDate, otherFile.getPath()); + String error = x.execute(cmd).error.readLine(); + int br = 0; + + + } + } catch (Exception ignored) {} + } + + + private void copyModifDate(File fromFile, File toFile) { + try { + List cmd = List.of("stat", "--format", "%y", fromFile.getPath()); + String mDate = x.execute(cmd).output.readLine(); + + cmd = List.of("touch", "-m", "--date=" + mDate, toFile.getPath()); + x.execute(cmd); + } catch (Exception ignored) {} + } + } diff --git a/src/com/olexyn/ensync/artifacts/SyncFile.java b/src/com/olexyn/ensync/artifacts/SyncFile.java index 075c759..224fc7c 100644 --- a/src/com/olexyn/ensync/artifacts/SyncFile.java +++ b/src/com/olexyn/ensync/artifacts/SyncFile.java @@ -1,12 +1,13 @@ package com.olexyn.ensync.artifacts; import java.io.File; +import java.util.Map; public class SyncFile extends File { // Very IMPORTANT field. Allows to store lastModified as it is stored in the StateFile. - private long timeModifiedFromStateFile = 0; + public long timeModifiedFromStateFile = 0; public String relativePath; private SyncDirectory sd; @@ -30,7 +31,10 @@ public class SyncFile extends File { public long getTimeModifiedFromStateFile(){ - return sd.readStateFile().get(this.getPath()).timeModifiedFromStateFile; + SyncFile record = sd.readStateFile().get(this.getPath()); + + + return record == null ? 0 : record.timeModifiedFromStateFile; }