diff --git a/src/com/olexyn/ensync/Execute.java b/src/com/olexyn/ensync/Execute.java index 2937bf3..dc4a036 100644 --- a/src/com/olexyn/ensync/Execute.java +++ b/src/com/olexyn/ensync/Execute.java @@ -29,8 +29,8 @@ public class Execute { public void executeBatch(List batch){ - for (int i =0; i pathList = tools.brToListString(find.output); for (String filePath : pathList) { - SyncFile file = new SyncFile(filePath); + SyncFile file = new SyncFile(this, filePath); filemap.put(filePath, file); } @@ -71,7 +71,7 @@ public class SyncDirectory { */ public Map readStateFile() { Map filemap = new HashMap<>(); - List lines = tools.fileToLines(new File(stateFilePath(path))); + List lines = tools.fileToLines(new File(tools.stateFilePath(path))); for (String line : lines) { // this is a predefined format: "modification-time path" @@ -79,7 +79,7 @@ public class SyncDirectory { long modTime = Long.parseLong(modTimeString); String sFilePath = line.replace(modTimeString + " ", ""); - SyncFile sfile = new SyncFile(sFilePath); + SyncFile sfile = new SyncFile(this, sFilePath); sfile.setStateFileTime(modTime); @@ -98,7 +98,6 @@ public class SyncDirectory { * @return */ public void makeListCreated() { - listCreated = new ArrayList<>(); Map fromA = findState(); Map substractB = readStateFile(); @@ -106,22 +105,14 @@ public class SyncDirectory { } - public String stateFilePath(String path) { - return "/tmp/ensync/state" + path.replace("/", "-"); - } - - /** * Compare the OLD and NEW pools. * List is cleared and created each time. - * */ public void makeListDeleted() { - listDeleted = new ArrayList<>(); Map fromA = readStateFile(); Map substractB = findState(); - listDeleted = tools.mapMinus(fromA, substractB); } @@ -129,24 +120,22 @@ public class SyncDirectory { /** * Compare the OLD and NEW pools. * List is cleared and created each time. - * */ - public void makeListModified() { + public List makeListModified() { listModified = new ArrayList<>(); Map oldMap = readStateFile(); - for (Map.Entry newFileEntry : findState().entrySet()) { - // If KEY exists in OLD , thus FILE was NOT created. - String newFileKey = newFileEntry.getKey(); + for (var newFileEntry : findState().entrySet()) { + String newFileKey = newFileEntry.getKey(); + // If KEY exists in OLD , thus FILE was NOT created. if (oldMap.containsKey(newFileKey)) { SyncFile newFile = newFileEntry.getValue(); - long lastModifiedNew = newFile.lastModified(); - + long lastModifiedNew = newFile.lastModified(); long lastModifiedOld = oldMap.get(newFileKey).stateFileTime(); if (lastModifiedNew > lastModifiedOld) { @@ -154,7 +143,7 @@ public class SyncDirectory { } } } - + return listModified; } @@ -177,26 +166,24 @@ public class SyncDirectory { outputList.add("" + lastModified + " " + filePath); } - tools.writeStringListToFile(stateFilePath(path), outputList); + tools.writeStringListToFile(tools.stateFilePath(path), outputList); } private class Info { - private String relativePath = null; - private String thisFilePath = null; - private String otherFilePath = null; - private File otherFile = null; - private String otherParentPath = null; - private File otherParentFile = null; - private long thisStateFileTime = 0; - private long thisTimeModified = 0; - private long otherTimeModified = 0; + 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) { + private Info(SyncDirectory thisSD, SyncFile sFile, SyncDirectory otherSD) { // Example: // syncDirectory /foo // otherSyncDirectory /bar @@ -216,18 +203,18 @@ public class SyncDirectory { // thisFile does not exist in StateFile, a value of 0 can be seen as equal to "never existed". this.thisStateFileTime = 0; } - this.thisTimeModified = sFile.lastModified(); + this.thisFileTimeModified = sFile.lastModified(); if (otherFile.exists()) { - this.otherTimeModified = otherFile.lastModified(); + this.otherFileTimeModified = otherFile.lastModified(); } else { if (otherSD.readStateFile().get(otherFilePath) != null) { - this.otherTimeModified = otherSD.readStateFile().get(otherFilePath).stateFileTime(); + this.otherFileTimeModified = otherSD.readStateFile().get(otherFilePath).stateFileTime(); } else { - this.otherTimeModified = 0; + this.otherFileTimeModified = 0; } } @@ -243,15 +230,14 @@ public class SyncDirectory { if (createdFile.isFile()) { - for (Map.Entry otherEntry : syncMap.syncDirectories.entrySet()) { - SyncDirectory otherSyncDirectory = otherEntry.getValue(); + for (var otherEntry : syncMap.syncDirectories.entrySet()) { + SyncDirectory otherSD = otherEntry.getValue(); - if (!this.equals(otherSyncDirectory)) { + if (this.equals(otherSD)) { continue; } - Info info = new Info(this, createdFile, otherSyncDirectory); + Info info = new Info(this, createdFile, otherSD); - createFile(info); - } + writeFile(info, this, createdFile, otherSD); } } } @@ -265,24 +251,21 @@ public class SyncDirectory { for (SyncFile deletedFile : listDeleted) { + for (var otherEntry : syncMap.syncDirectories.entrySet()) { + SyncDirectory otherSD = otherEntry.getValue(); - for (Map.Entry otherEntry : syncMap.syncDirectories.entrySet()) { - SyncDirectory otherSyncDirectory = otherEntry.getValue(); - - if (!this.equals(otherSyncDirectory)) { + if (this.equals(otherSD)) { continue; } - Info info = new Info(this, deletedFile, otherSyncDirectory); - if (info.otherFile.isFile()) { + Info info = new Info(this, deletedFile, otherSD); + if (info.otherFile.isFile()) { - // if the otherFile was created with ensync it will have the == TimeModified. - if (info.thisStateFileTime >= info.otherTimeModified) { - String[] cmd = new String[]{"rm", - "-r", - info.otherFilePath}; - x.execute(cmd); - } + // 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); } - } } } @@ -295,19 +278,14 @@ public class SyncDirectory { if (modifiedFile.isFile()) { - for (Map.Entry otherEntry : syncMap.syncDirectories.entrySet()) { - SyncDirectory otherSyncDirectory = otherEntry.getValue(); - - if (!this.equals(otherSyncDirectory)) { - - Info info = new Info(this, modifiedFile, otherSyncDirectory); + for (var otherEntry : syncMap.syncDirectories.entrySet()) { + SyncDirectory otherSD = otherEntry.getValue(); + if (this.equals(otherSD)) { continue; } - createFile(info); - - - } + Info info = new Info(this, modifiedFile, otherSD); + writeFile(info, this, modifiedFile, otherSD); } } @@ -316,8 +294,11 @@ public class SyncDirectory { } - private void createFile(Info info) { - if (!info.otherFile.exists() || info.thisTimeModified > info.otherTimeModified) { + private void writeFile(Info info, SyncDirectory thisSD, SyncFile thisFile, SyncDirectory otherSD) { + + File otherFile = new File(otherSD.path + thisFile.relativePath); + + if (!otherFile.exists() || info.thisFileTimeModified > info.otherFileTimeModified) { if (!info.otherParentFile.exists()) { String[] cmd = new String[]{"mkdir", "-p", @@ -333,13 +314,11 @@ public class SyncDirectory { } - public String flowState(){ + 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 6c3968e..8402aa2 100644 --- a/src/com/olexyn/ensync/artifacts/SyncFile.java +++ b/src/com/olexyn/ensync/artifacts/SyncFile.java @@ -8,10 +8,16 @@ public class SyncFile extends File { // Very IMPORTANT field. Allows to store lastModified as it is stored in the StateFile. private long stateFileTime = 0; + public String relativePath; + private SyncDirectory sd; - public SyncFile(String pathname) { + + public SyncFile(SyncDirectory sd , String pathname) { + super(pathname); + this.sd = sd; + relativePath = this.getPath().replace(sd.path, ""); } @@ -25,7 +31,18 @@ public class SyncFile extends File { } + public long getFileTimeModified(SyncDirectory otherSD){ + if (this.exists()){ + return lastModified(); + } + + if (sd.readStateFile().get(this.getPath())!=null){ + } + + + return 0; + } } diff --git a/src/com/olexyn/ensync/artifacts/SyncMap.java b/src/com/olexyn/ensync/artifacts/SyncMap.java index 0b63148..1e36c8d 100644 --- a/src/com/olexyn/ensync/artifacts/SyncMap.java +++ b/src/com/olexyn/ensync/artifacts/SyncMap.java @@ -8,14 +8,11 @@ import java.util.HashMap; import java.util.Map; /** - * What is a SyncMap? - * * A SyncMap is the set of such SyncDirectories. The purpose of the SyncMap is to help syncronize the SyncDirectories. */ public class SyncMap { public String name; - public int syncInterval = 3600; public Map syncDirectories = new HashMap<>(); Tools tools = new Tools();