diff --git a/src/com/olexyn/ensync/Execute.java b/src/com/olexyn/ensync/Execute.java index dc4a036..e92f362 100644 --- a/src/com/olexyn/ensync/Execute.java +++ b/src/com/olexyn/ensync/Execute.java @@ -27,15 +27,24 @@ public class Execute { return twobr; } - public void executeBatch(List batch){ - for (String[] strings : batch) { - execute(strings); + public TwoBr execute(List cmd) { + + String[] cmdArr = new String[cmd.size()]; + for (int i = 0; i < cmd.size(); i++) { + cmdArr[i] = cmd.get(i); } + return execute(cmdArr); } + public void executeBatch(List batch) { + + for (String[] strings : batch) { + execute(strings); + } + } public class TwoBr { diff --git a/src/com/olexyn/ensync/Flow.java b/src/com/olexyn/ensync/Flow.java index 56659cd..ec7f83e 100644 --- a/src/com/olexyn/ensync/Flow.java +++ b/src/com/olexyn/ensync/Flow.java @@ -32,7 +32,7 @@ public class Flow implements Runnable { SyncDirectory SD = SDEntry.getValue(); state = "READ"; - SD.findState(); + SD.readFreshState(); SD.makeListCreated(); SD.makeListDeleted(); @@ -51,8 +51,9 @@ public class Flow implements Runnable { try { - System.out.println("Pausing..."); - Thread.sleep(2000); + long pause = 2000; + System.out.println("Pausing... for "+pause+ "ms."); + Thread.sleep(pause); } catch (InterruptedException ignored) { } diff --git a/src/com/olexyn/ensync/artifacts/SyncDirectory.java b/src/com/olexyn/ensync/artifacts/SyncDirectory.java index d60fc2a..2230867 100644 --- a/src/com/olexyn/ensync/artifacts/SyncDirectory.java +++ b/src/com/olexyn/ensync/artifacts/SyncDirectory.java @@ -15,6 +15,7 @@ import java.util.Map; public class SyncDirectory { private String flowState; + private SyncDirectory thisSD = this; private SyncMap syncMap; @@ -44,7 +45,7 @@ public class SyncDirectory { /** * Get the current state by using the `find` command. */ - public Map findState() { + public Map readFreshState() { //NOTE that the SFile().lastModifiedOld is not set here, so it is 0 by default. Map filemap = new HashMap<>(); @@ -81,7 +82,7 @@ public class SyncDirectory { String sFilePath = line.replace(modTimeString + " ", ""); SyncFile sfile = new SyncFile(this, sFilePath); - sfile.setStateFileTime(modTime); + sfile.setTimeModifiedFromStateFile(modTime); filemap.put(sFilePath, sfile); } @@ -98,7 +99,7 @@ public class SyncDirectory { * @return */ public void makeListCreated() { - Map fromA = findState(); + Map fromA = readFreshState(); Map substractB = readStateFile(); listCreated = tools.mapMinus(fromA, substractB); @@ -111,9 +112,12 @@ public class SyncDirectory { */ public void makeListDeleted() { Map fromA = readStateFile(); - Map substractB = findState(); + Map substractB = readFreshState(); listDeleted = tools.mapMinus(fromA, substractB); + + + } @@ -124,22 +128,18 @@ public class SyncDirectory { public List makeListModified() { listModified = new ArrayList<>(); - Map oldMap = readStateFile(); + Map stateFileMap = readStateFile(); - for (var newFileEntry : findState().entrySet()) { + for (var freshFileEntry : readFreshState().entrySet()) { - String newFileKey = newFileEntry.getKey(); + String freshFileKey = freshFileEntry.getKey(); + SyncFile freshFile = freshFileEntry.getValue(); // If KEY exists in OLD , thus FILE was NOT created. - if (oldMap.containsKey(newFileKey)) { + if (stateFileMap.containsKey(freshFileKey)) { - SyncFile newFile = newFileEntry.getValue(); - - long lastModifiedNew = newFile.lastModified(); - long lastModifiedOld = oldMap.get(newFileKey).stateFileTime(); - - if (lastModifiedNew > lastModifiedOld) { - listModified.add(newFile); + if (freshFile.getTimeModified() > freshFile.getTimeModifiedFromStateFile()) { + listModified.add(freshFile); } } } @@ -172,15 +172,10 @@ public class SyncDirectory { private class Info { - private String relativePath; private String thisFilePath; private String otherFilePath; - private File otherFile; private String otherParentPath; private File otherParentFile; - private long thisStateFileTime; - private long thisFileTimeModified; - private long otherFileTimeModified; private Info(SyncDirectory thisSD, SyncFile sFile, SyncDirectory otherSD) { @@ -189,38 +184,21 @@ public class SyncDirectory { // otherSyncDirectory /bar // createdFile /foo/hello/created-file.gif // relativePath /hello/created-file.gif - this.relativePath = sFile.getPath().replace(thisSD.path, ""); + String relativePath = sFile.getPath().replace(thisSD.path, ""); this.thisFilePath = sFile.getPath(); this.otherFilePath = otherSD.path + relativePath; - this.otherFile = new File(otherFilePath); + File otherFile = new File(otherFilePath); this.otherParentPath = otherFile.getParent(); this.otherParentFile = new File(otherParentPath); - if (thisSD.readStateFile().get(thisFilePath) != null) { - this.thisStateFileTime = thisSD.readStateFile().get(thisFilePath).stateFileTime(); - } else { - // thisFile does not exist in StateFile, a value of 0 can be seen as equal to "never existed". - this.thisStateFileTime = 0; - } - this.thisFileTimeModified = sFile.lastModified(); - if (otherFile.exists()) { - this.otherFileTimeModified = otherFile.lastModified(); - } else { - if (otherSD.readStateFile().get(otherFilePath) != null) { - this.otherFileTimeModified = otherSD.readStateFile().get(otherFilePath).stateFileTime(); - } else { - this.otherFileTimeModified = 0; - } - } - - } + } } @@ -228,17 +206,14 @@ public class SyncDirectory { for (SyncFile createdFile : listCreated) { - if (createdFile.isFile()) { - - for (var otherEntry : syncMap.syncDirectories.entrySet()) { - SyncDirectory otherSD = otherEntry.getValue(); + for (var otherEntry : syncMap.syncDirectories.entrySet()) { + SyncDirectory otherSD = otherEntry.getValue(); - if (this.equals(otherSD)) { continue; } + if (this.equals(otherSD)) { continue; } - Info info = new Info(this, createdFile, otherSD); + Info info = new Info(this, createdFile, otherSD); - writeFile(info, this, createdFile, otherSD); - } + writeFile(info, this, createdFile, otherSD); } } } @@ -256,68 +231,70 @@ public class SyncDirectory { if (this.equals(otherSD)) { continue; } - Info info = new Info(this, deletedFile, otherSD); - if (info.otherFile.isFile()) { + Info info = new Info(thisSD, deletedFile, otherSD); + deleteFile(info, thisSD, deletedFile, otherSD); + - // if the otherFile was created with ensync it will have the == TimeModified. - if (info.thisStateFileTime >= info.otherFileTimeModified) { - String[] cmd = new String[]{"rm", - "-r", - info.otherFilePath}; - x.execute(cmd); - } - } } } } + private void deleteFile(Info info, SyncDirectory thisSD, SyncFile thisFile, SyncDirectory otherSD){ - public void doModify() { + SyncFile otherFile = new SyncFile(otherSD, otherSD.path + thisFile.relativePath); - for (SyncFile modifiedFile : this.listModified) { + // if the otherFile was created with ensync it will have the == TimeModified. + if (thisFile.getTimeModified() >= otherFile.getTimeModified()) { + List cmd = List.of("rm", "-r", info.otherFilePath); + x.execute(cmd); + } + } - if (modifiedFile.isFile()) { - for (var otherEntry : syncMap.syncDirectories.entrySet()) { - SyncDirectory otherSD = otherEntry.getValue(); + public void doModify() { + + for (SyncFile modifiedFile : this.listModified) { - if (this.equals(otherSD)) { continue; } + for (var otherEntry : syncMap.syncDirectories.entrySet()) { + SyncDirectory otherSD = otherEntry.getValue(); - Info info = new Info(this, modifiedFile, otherSD); + if (this.equals(otherSD)) { continue; } - writeFile(info, this, modifiedFile, otherSD); + Info info = new Info(this, modifiedFile, otherSD); - } + writeFile(info, this, modifiedFile, otherSD); } - } } private void writeFile(Info info, SyncDirectory thisSD, SyncFile thisFile, SyncDirectory otherSD) { - File otherFile = new File(otherSD.path + thisFile.relativePath); + SyncFile otherFile = new SyncFile(otherSD, otherSD.path + thisFile.relativePath); + + + if (otherFile.exists() && thisFile.getTimeModified() < otherFile.getTimeModified()) { return;} + + + if (thisFile.isDirectory() && !otherFile.exists()) { + List cmd = List.of("mkdir", "-p", info.otherFilePath); + x.execute(cmd); + return; + } + + if (thisFile.isFile()) { - if (!otherFile.exists() || info.thisFileTimeModified > info.otherFileTimeModified) { if (!info.otherParentFile.exists()) { - String[] cmd = new String[]{"mkdir", - "-p", - info.otherParentPath}; + List cmd = List.of("mkdir", "-p", info.otherParentPath); x.execute(cmd); } - String[] cmd = new String[]{"cp", - "-p", - info.thisFilePath, - info.otherFilePath}; + + List cmd = List.of("cp", "-p", info.thisFilePath, info.otherFilePath); x.execute(cmd); } } - public String flowState() { - return flowState == null ? "NONE" : flowState; - } - } diff --git a/src/com/olexyn/ensync/artifacts/SyncFile.java b/src/com/olexyn/ensync/artifacts/SyncFile.java index 8402aa2..075c759 100644 --- a/src/com/olexyn/ensync/artifacts/SyncFile.java +++ b/src/com/olexyn/ensync/artifacts/SyncFile.java @@ -6,7 +6,7 @@ public class SyncFile extends File { // Very IMPORTANT field. Allows to store lastModified as it is stored in the StateFile. - private long stateFileTime = 0; + private long timeModifiedFromStateFile = 0; public String relativePath; private SyncDirectory sd; @@ -22,22 +22,33 @@ public class SyncFile extends File { - public long stateFileTime(){ - return stateFileTime; + + + public void setTimeModifiedFromStateFile(long value){ + timeModifiedFromStateFile = value; } - public void setStateFileTime(long value){ - stateFileTime = value; + + public long getTimeModifiedFromStateFile(){ + return sd.readStateFile().get(this.getPath()).timeModifiedFromStateFile; } - public long getFileTimeModified(SyncDirectory otherSD){ + /** + * If File exists on Disk get the TimeModified from there. + * Else try to read it from StateFile. + * Else return 0 ( = oldest possible time - a value of 0 can be seen as equal to "never existed"). + * EXAMPLES: + * If a File was deleted, then the time will be taken from statefile. + * If a File never existed, it will have time = 0, and thus will always be overwritten. + */ + public long getTimeModified(){ if (this.exists()){ return lastModified(); } if (sd.readStateFile().get(this.getPath())!=null){ - + return getTimeModifiedFromStateFile(); }