From 40f2f21172486b9135d6353bcb9b345eaefa5990 Mon Sep 17 00:00:00 2001 From: Ivan Olexyn Date: Thu, 23 Jan 2020 06:12:50 +0100 Subject: [PATCH] Changes... --- README.md | 21 +++ src/com/olexyn/ensync/Flow.java | 27 +-- src/com/olexyn/ensync/Tools.java | 12 +- .../ensync/artifacts/SyncDirectory.java | 95 ++++++++--- .../ensync/file-ops-covered-vs-missing.uxf | 160 +++++++++--------- 5 files changed, 202 insertions(+), 113 deletions(-) diff --git a/README.md b/README.md index 961d93f..f5c2d03 100644 --- a/README.md +++ b/README.md @@ -37,3 +37,24 @@ Sync files across directories. - Have some error handling. (i.e. if a web-directory is not available) - Create a UI. - Start the program at system start. +- Track files that were modified during the loop. + - currently `writeStateFile` just takes from `find` + - this means any changes made during the loop will be written to the `StateFile` + - and created files are tracked by comparing `StateFile` (=old state) and `State` (=new state). + - because of this it will appear as if the file created while the loop was running + was already there. + - thus the creation of said file will not be replicated to the other directories. + - to solve this `writeStateFile` should take the old `State` and manually add every operation that was performed by the loop (!= user created file while the loop was running). + - however this will be done later . . maybe. +- If file is deleted in DirA and DirB, then two delete commands will be issued. + - They will both return errors and effectively do nothing. + - However this is a dirty solution. + - Fix this by checking if deleted file of DirA exists in DirB.listDeleted + - To do so .listDeleted would need to be a field of Dir + - And the .lists of every dir would need to be calculated before any deletion took place. + - Check if the reduced reobustness is worth the prettier solution. + - File is created in DirA + - Sync creates the file in DirB + - Sync creates the file in DirB + - this means the file in DirB is overwritten with `cp` for no reason. + - implement a check to prevent this. \ No newline at end of file diff --git a/src/com/olexyn/ensync/Flow.java b/src/com/olexyn/ensync/Flow.java index 5bd4714..847e3c4 100644 --- a/src/com/olexyn/ensync/Flow.java +++ b/src/com/olexyn/ensync/Flow.java @@ -55,22 +55,27 @@ public class Flow { syncDirectory.readState(path); - List listCreated = syncDirectory.makeListCreated(path); - List listDeleted = syncDirectory.makeListDeleted(path); - List listModified = syncDirectory.makeListModified(path); + syncDirectory.makeListCreated(path); + syncDirectory.makeListDeleted(path); + syncDirectory.makeListModified(path); + + syncDirectory.doCreate(); + syncDirectory.doDelete(); + syncDirectory.doModify(); + - syncDirectory.doCreate(listCreated); - syncDirectory.doDelete(listDeleted); - syncDirectory.doModify(listModified); syncDirectory.writeStateFile(path); - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - } - }} + } + + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + + } + } } } diff --git a/src/com/olexyn/ensync/Tools.java b/src/com/olexyn/ensync/Tools.java index e9b7dd4..db4915a 100644 --- a/src/com/olexyn/ensync/Tools.java +++ b/src/com/olexyn/ensync/Tools.java @@ -79,11 +79,13 @@ public class Tools { while (iterator.hasNext()) { Map.Entry entry = (Map.Entry) iterator.next(); String key = (String) entry.getKey(); + //String path + - SyncFile file = fromA.get(key); if (fromA.containsKey(key) && !substractB.containsKey(key)) { + SyncFile file = fromA.get(key); difference.add(file); } @@ -128,6 +130,14 @@ public class Tools { */ public void writeStringListToFile(String path, List list) { + File file = new File(path); + File parent = new File(file.getParent()); + if (!parent.exists()){ + + x.execute(new String[]{"mkdir", "-p", parent.getPath()}); + } + + try { BufferedWriter bw = new BufferedWriter(new FileWriter(new File(path))); StringBuilder sb = stringListToSb(list); diff --git a/src/com/olexyn/ensync/artifacts/SyncDirectory.java b/src/com/olexyn/ensync/artifacts/SyncDirectory.java index fe5f0a2..30abcd7 100644 --- a/src/com/olexyn/ensync/artifacts/SyncDirectory.java +++ b/src/com/olexyn/ensync/artifacts/SyncDirectory.java @@ -13,9 +13,11 @@ public class SyncDirectory { private SyncEntity syncEntity; - public String path= null; - + public String path = null; + public List listCreated = new ArrayList<>(); + public List listDeleted = new ArrayList<>(); + public List listModified = new ArrayList<>(); Tools tools = new Tools(); @@ -24,10 +26,10 @@ public class SyncDirectory { /** * Create a SyncDirectory from realPath. * - * @see SyncEntity */ - public SyncDirectory(String path , SyncEntity syncEntity) { + public SyncDirectory(String path, + SyncEntity syncEntity) { this.path = path; this.syncEntity = syncEntity; @@ -47,7 +49,9 @@ public class SyncDirectory { List pathList = tools.brToListString(find.output); for (String filePath : pathList) { - filemap.put(filePath, new SyncFile(filePath)); + SyncFile file = new SyncFile(filePath); + + filemap.put(filePath, file); } @@ -66,15 +70,15 @@ public class SyncDirectory { for (String line : lines) { // this is a predefined format: "modification-time path" - String modTimeString = line.split("")[0]; + String modTimeString = line.split(" ")[0]; long modTime = Long.parseLong(modTimeString); - String sFilePath = line.replace(modTimeString + "", ""); + String sFilePath = line.replace(modTimeString + " ", ""); SyncFile sfile = new SyncFile(sFilePath); sfile.setLastModifiedOld(modTime); - filemap.put(line, sfile); + filemap.put(sFilePath, sfile); } return filemap; @@ -84,12 +88,16 @@ public class SyncDirectory { /** * Compare the OLD and NEW pools. + * List is cleared and created each time. * * @return */ - public List makeListCreated(String path) { + public void makeListCreated(String path) { + listCreated = new ArrayList<>(); + Map fromA = readState(path); + Map substractB = readStateFile(path); - return tools.mapMinus(readState(path), readStateFile(path)); + listCreated = tools.mapMinus(fromA, substractB); } @@ -100,32 +108,39 @@ public class SyncDirectory { /** * Compare the OLD and NEW pools. + * List is cleared and created each time. * * @return */ - public List makeListDeleted(String path) { + public void makeListDeleted(String path) { + listDeleted = new ArrayList<>(); + Map fromA = readStateFile(path); + Map substractB = readState(path); - return tools.mapMinus(readStateFile(path), readState(path)); + listDeleted = tools.mapMinus(fromA, substractB); } /** * Compare the OLD and NEW pools. + * List is cleared and created each time. * * @return */ - public List makeListModified(String path) { + public void makeListModified(String path) { - List listModified = new ArrayList<>(); + listModified = new ArrayList<>(); Map oldMap = readStateFile(path); for (Map.Entry newFileEntry : readState(path).entrySet()) { // If KEY exists in OLD , thus FILE was NOT created. String newFileKey = newFileEntry.getKey(); - SyncFile newFile = newFileEntry.getValue(); + + if (oldMap.containsKey(newFileKey)) { + SyncFile newFile = newFileEntry.getValue(); long lastModifiedNew = newFile.lastModified(); @@ -136,7 +151,7 @@ public class SyncDirectory { } } } - return listModified; + } @@ -148,7 +163,6 @@ public class SyncDirectory { List outputList = new ArrayList<>(); - Execute.TwoBr find = x.execute(new String[]{"find", path}); @@ -164,7 +178,7 @@ public class SyncDirectory { } - public void doCreate(List listCreated){ + public void doCreate() { for (File createdFile : listCreated) { @@ -198,8 +212,7 @@ public class SyncDirectory { } - - public void doDelete(List listDeleted){ + public void doDelete() { for (File deletedFile : listDeleted) { @@ -220,12 +233,52 @@ public class SyncDirectory { } - public void doModify(List listModified) { + public void doModify() { + + for (SyncFile modifiedFile : this.listModified) { + + if (modifiedFile.isFile()) { + + for (Map.Entry otherEntry : syncEntity.syncDirectories.entrySet()) { + SyncDirectory otherSyncDirectory = otherEntry.getValue(); + if (!this.equals(otherSyncDirectory)) { + String relativePath = modifiedFile.getPath().replace(this.path, ""); + String otherFilePath = otherSyncDirectory.path + relativePath; + SyncFile otherFile = new SyncFile(otherFilePath); + if (otherFile.exists()) { + if (modifiedFile.lastModified() > otherFile.lastModified()) { + // IF both Files exist, and this File NEWER -> UPDATE the other File + String[] cmd = new String[]{"cp", + modifiedFile.getPath(), + otherFilePath}; + x.execute(cmd); + } + } else { + // IF other file does NOT exist -> UPDATE (i.e. create) the other File + String[] cmd = new String[]{"mkdir", + "-p", + otherFilePath}; + x.execute(cmd); + + cmd = new String[]{"cp", + modifiedFile.getPath(), + otherFilePath}; + x.execute(cmd); + } + + + } + + + } + } + + } } diff --git a/src/com/olexyn/ensync/file-ops-covered-vs-missing.uxf b/src/com/olexyn/ensync/file-ops-covered-vs-missing.uxf index 7a42dfe..f0ebaa8 100644 --- a/src/com/olexyn/ensync/file-ops-covered-vs-missing.uxf +++ b/src/com/olexyn/ensync/file-ops-covered-vs-missing.uxf @@ -4,8 +4,8 @@ UMLObject - 760 - 420 + 700 + 720 110 40 @@ -15,8 +15,8 @@ UMLObject - 760 - 380 + 700 + 680 110 40 @@ -26,8 +26,8 @@ UMLObject - 760 - 230 + 700 + 530 110 40 @@ -39,8 +39,8 @@ group=1 UMLObject - 760 - 270 + 700 + 570 110 40 @@ -52,8 +52,8 @@ group=1 UMLObject - 760 - 310 + 700 + 610 110 40 @@ -65,8 +65,8 @@ group=1 UMLState - 910 - 530 + 850 + 830 70 40 @@ -77,8 +77,8 @@ bg=green UMLState - 910 - 420 + 850 + 720 70 40 @@ -88,8 +88,8 @@ bg=green UMLState - 1000 - 420 + 940 + 720 70 40 @@ -100,8 +100,8 @@ bg=red UMLState - 1360 - 530 + 1300 + 830 70 40 @@ -112,8 +112,8 @@ bg=red UMLObject - 760 - 190 + 700 + 490 110 40 @@ -125,8 +125,8 @@ group=1 UMLState - 910 - 380 + 850 + 680 70 40 @@ -136,8 +136,8 @@ group=1 UMLState - 1000 - 380 + 940 + 680 70 40 @@ -147,8 +147,8 @@ group=1 UMLState - 1090 - 420 + 1030 + 720 70 40 @@ -159,8 +159,8 @@ bg=red UMLState - 1090 - 380 + 1030 + 680 70 40 @@ -171,8 +171,8 @@ bg=red UMLState - 1180 - 420 + 1120 + 720 70 40 @@ -183,8 +183,8 @@ bg=green UMLState - 1180 - 380 + 1120 + 680 70 40 @@ -194,8 +194,8 @@ bg=green UMLState - 1270 - 420 + 1210 + 720 70 40 @@ -206,8 +206,8 @@ bg=green UMLState - 1270 - 380 + 1210 + 680 70 40 @@ -218,8 +218,8 @@ bg=green UMLState - 1360 - 420 + 1300 + 720 70 40 @@ -230,8 +230,8 @@ bg=yellow UMLState - 1360 - 380 + 1300 + 680 70 40 @@ -241,8 +241,8 @@ bg=yellow UMLState - 1450 - 420 + 1390 + 720 70 40 @@ -253,8 +253,8 @@ bg=yellow UMLState - 1450 - 380 + 1390 + 680 70 40 @@ -265,8 +265,8 @@ bg=yellow UMLState - 1540 - 380 + 1480 + 680 70 40 @@ -277,8 +277,8 @@ bg=red UMLState - 1540 - 420 + 1480 + 720 70 40 @@ -289,8 +289,8 @@ bg=green UMLState - 1630 - 380 + 1570 + 680 70 40 @@ -301,8 +301,8 @@ bg=red UMLState - 1630 - 420 + 1570 + 720 70 40 @@ -313,8 +313,8 @@ bg=yellow UMLState - 1720 - 380 + 1660 + 680 70 40 @@ -325,8 +325,8 @@ bg=green UMLState - 1720 - 420 + 1660 + 720 70 40 @@ -337,8 +337,8 @@ bg=yellow UMLNote - 890 - 190 + 830 + 490 150 80 @@ -351,8 +351,8 @@ bg=white UMLObject - 760 - 530 + 700 + 830 110 40 @@ -362,20 +362,20 @@ bg=white UMLState - 1090 - 530 + 940 + 830 70 40 - MAYBE -bg=yellow + YES +bg=green UMLState - 1000 - 530 + 1120 + 830 70 40 @@ -386,20 +386,20 @@ bg=green UMLState - 1180 - 530 + 1390 + 830 70 40 - YES -bg=green + NO +bg=red UMLState - 1450 - 530 + 1570 + 830 70 40 @@ -410,8 +410,8 @@ bg=red UMLState - 1630 - 530 + 1660 + 830 70 40 @@ -422,8 +422,8 @@ bg=red UMLState - 1720 - 530 + 1210 + 830 70 40 @@ -434,8 +434,8 @@ bg=red UMLState - 1270 - 530 + 1480 + 830 70 40 @@ -446,8 +446,8 @@ bg=red UMLState - 1540 - 530 + 1030 + 830 70 40