pull/1/head
Ivan Olexyn 5 years ago
parent c8b8329a74
commit 40f2f21172

@ -37,3 +37,24 @@ Sync files across directories.
- Have some error handling. (i.e. if a web-directory is not available) - Have some error handling. (i.e. if a web-directory is not available)
- Create a UI. - Create a UI.
- Start the program at system start. - 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.

@ -55,22 +55,27 @@ public class Flow {
syncDirectory.readState(path); syncDirectory.readState(path);
List<SyncFile> listCreated = syncDirectory.makeListCreated(path); syncDirectory.makeListCreated(path);
List<SyncFile> listDeleted = syncDirectory.makeListDeleted(path); syncDirectory.makeListDeleted(path);
List<SyncFile> listModified = syncDirectory.makeListModified(path); syncDirectory.makeListModified(path);
syncDirectory.doCreate();
syncDirectory.doDelete();
syncDirectory.doModify();
syncDirectory.doCreate(listCreated);
syncDirectory.doDelete(listDeleted);
syncDirectory.doModify(listModified);
syncDirectory.writeStateFile(path); syncDirectory.writeStateFile(path);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
} }
}}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
} }
} }

@ -79,11 +79,13 @@ public class Tools {
while (iterator.hasNext()) { while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next(); Map.Entry entry = (Map.Entry) iterator.next();
String key = (String) entry.getKey(); String key = (String) entry.getKey();
//String path
SyncFile file = fromA.get(key);
if (fromA.containsKey(key) && !substractB.containsKey(key)) { if (fromA.containsKey(key) && !substractB.containsKey(key)) {
SyncFile file = fromA.get(key);
difference.add(file); difference.add(file);
} }
@ -128,6 +130,14 @@ public class Tools {
*/ */
public void writeStringListToFile(String path, public void writeStringListToFile(String path,
List<String> list) { List<String> list) {
File file = new File(path);
File parent = new File(file.getParent());
if (!parent.exists()){
x.execute(new String[]{"mkdir", "-p", parent.getPath()});
}
try { try {
BufferedWriter bw = new BufferedWriter(new FileWriter(new File(path))); BufferedWriter bw = new BufferedWriter(new FileWriter(new File(path)));
StringBuilder sb = stringListToSb(list); StringBuilder sb = stringListToSb(list);

@ -13,9 +13,11 @@ public class SyncDirectory {
private SyncEntity syncEntity; private SyncEntity syncEntity;
public String path= null; public String path = null;
public List<SyncFile> listCreated = new ArrayList<>();
public List<SyncFile> listDeleted = new ArrayList<>();
public List<SyncFile> listModified = new ArrayList<>();
Tools tools = new Tools(); Tools tools = new Tools();
@ -24,10 +26,10 @@ public class SyncDirectory {
/** /**
* Create a SyncDirectory from realPath. * Create a SyncDirectory from realPath.
* *
* @see SyncEntity * @see SyncEntity
*/ */
public SyncDirectory(String path , SyncEntity syncEntity) { public SyncDirectory(String path,
SyncEntity syncEntity) {
this.path = path; this.path = path;
this.syncEntity = syncEntity; this.syncEntity = syncEntity;
@ -47,7 +49,9 @@ public class SyncDirectory {
List<String> pathList = tools.brToListString(find.output); List<String> pathList = tools.brToListString(find.output);
for (String filePath : pathList) { 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) { for (String line : lines) {
// this is a predefined format: "modification-time path" // this is a predefined format: "modification-time path"
String modTimeString = line.split("")[0]; String modTimeString = line.split(" ")[0];
long modTime = Long.parseLong(modTimeString); long modTime = Long.parseLong(modTimeString);
String sFilePath = line.replace(modTimeString + "", ""); String sFilePath = line.replace(modTimeString + " ", "");
SyncFile sfile = new SyncFile(sFilePath); SyncFile sfile = new SyncFile(sFilePath);
sfile.setLastModifiedOld(modTime); sfile.setLastModifiedOld(modTime);
filemap.put(line, sfile); filemap.put(sFilePath, sfile);
} }
return filemap; return filemap;
@ -84,12 +88,16 @@ public class SyncDirectory {
/** /**
* Compare the OLD and NEW pools. * Compare the OLD and NEW pools.
* List is cleared and created each time.
* *
* @return * @return
*/ */
public List<SyncFile> makeListCreated(String path) { public void makeListCreated(String path) {
listCreated = new ArrayList<>();
Map<String, SyncFile> fromA = readState(path);
Map<String, SyncFile> 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. * Compare the OLD and NEW pools.
* List is cleared and created each time.
* *
* @return * @return
*/ */
public List<SyncFile> makeListDeleted(String path) { public void makeListDeleted(String path) {
listDeleted = new ArrayList<>();
Map<String, SyncFile> fromA = readStateFile(path);
Map<String, SyncFile> substractB = readState(path);
return tools.mapMinus(readStateFile(path), readState(path)); listDeleted = tools.mapMinus(fromA, substractB);
} }
/** /**
* Compare the OLD and NEW pools. * Compare the OLD and NEW pools.
* List is cleared and created each time.
* *
* @return * @return
*/ */
public List<SyncFile> makeListModified(String path) { public void makeListModified(String path) {
List<SyncFile> listModified = new ArrayList<>(); listModified = new ArrayList<>();
Map<String, SyncFile> oldMap = readStateFile(path); Map<String, SyncFile> oldMap = readStateFile(path);
for (Map.Entry<String, SyncFile> newFileEntry : readState(path).entrySet()) { for (Map.Entry<String, SyncFile> newFileEntry : readState(path).entrySet()) {
// If KEY exists in OLD , thus FILE was NOT created. // If KEY exists in OLD , thus FILE was NOT created.
String newFileKey = newFileEntry.getKey(); String newFileKey = newFileEntry.getKey();
SyncFile newFile = newFileEntry.getValue();
if (oldMap.containsKey(newFileKey)) { if (oldMap.containsKey(newFileKey)) {
SyncFile newFile = newFileEntry.getValue();
long lastModifiedNew = newFile.lastModified(); long lastModifiedNew = newFile.lastModified();
@ -136,7 +151,7 @@ public class SyncDirectory {
} }
} }
} }
return listModified;
} }
@ -148,7 +163,6 @@ public class SyncDirectory {
List<String> outputList = new ArrayList<>(); List<String> outputList = new ArrayList<>();
Execute.TwoBr find = x.execute(new String[]{"find", Execute.TwoBr find = x.execute(new String[]{"find",
path}); path});
@ -164,7 +178,7 @@ public class SyncDirectory {
} }
public void doCreate(List<SyncFile> listCreated){ public void doCreate() {
for (File createdFile : listCreated) { for (File createdFile : listCreated) {
@ -198,8 +212,7 @@ public class SyncDirectory {
} }
public void doDelete() {
public void doDelete(List<SyncFile> listDeleted){
for (File deletedFile : listDeleted) { for (File deletedFile : listDeleted) {
@ -220,12 +233,52 @@ public class SyncDirectory {
} }
public void doModify(List<SyncFile> listModified) { public void doModify() {
for (SyncFile modifiedFile : this.listModified) {
if (modifiedFile.isFile()) {
for (Map.Entry<String, SyncDirectory> 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);
}
}
}
}
}
} }

@ -4,8 +4,8 @@
<element> <element>
<id>UMLObject</id> <id>UMLObject</id>
<coordinates> <coordinates>
<x>760</x> <x>700</x>
<y>420</y> <y>720</y>
<w>110</w> <w>110</w>
<h>40</h> <h>40</h>
</coordinates> </coordinates>
@ -15,8 +15,8 @@
<element> <element>
<id>UMLObject</id> <id>UMLObject</id>
<coordinates> <coordinates>
<x>760</x> <x>700</x>
<y>380</y> <y>680</y>
<w>110</w> <w>110</w>
<h>40</h> <h>40</h>
</coordinates> </coordinates>
@ -26,8 +26,8 @@
<element> <element>
<id>UMLObject</id> <id>UMLObject</id>
<coordinates> <coordinates>
<x>760</x> <x>700</x>
<y>230</y> <y>530</y>
<w>110</w> <w>110</w>
<h>40</h> <h>40</h>
</coordinates> </coordinates>
@ -39,8 +39,8 @@ group=1</panel_attributes>
<element> <element>
<id>UMLObject</id> <id>UMLObject</id>
<coordinates> <coordinates>
<x>760</x> <x>700</x>
<y>270</y> <y>570</y>
<w>110</w> <w>110</w>
<h>40</h> <h>40</h>
</coordinates> </coordinates>
@ -52,8 +52,8 @@ group=1</panel_attributes>
<element> <element>
<id>UMLObject</id> <id>UMLObject</id>
<coordinates> <coordinates>
<x>760</x> <x>700</x>
<y>310</y> <y>610</y>
<w>110</w> <w>110</w>
<h>40</h> <h>40</h>
</coordinates> </coordinates>
@ -65,8 +65,8 @@ group=1</panel_attributes>
<element> <element>
<id>UMLState</id> <id>UMLState</id>
<coordinates> <coordinates>
<x>910</x> <x>850</x>
<y>530</y> <y>830</y>
<w>70</w> <w>70</w>
<h>40</h> <h>40</h>
</coordinates> </coordinates>
@ -77,8 +77,8 @@ bg=green</panel_attributes>
<element> <element>
<id>UMLState</id> <id>UMLState</id>
<coordinates> <coordinates>
<x>910</x> <x>850</x>
<y>420</y> <y>720</y>
<w>70</w> <w>70</w>
<h>40</h> <h>40</h>
</coordinates> </coordinates>
@ -88,8 +88,8 @@ bg=green</panel_attributes>
<element> <element>
<id>UMLState</id> <id>UMLState</id>
<coordinates> <coordinates>
<x>1000</x> <x>940</x>
<y>420</y> <y>720</y>
<w>70</w> <w>70</w>
<h>40</h> <h>40</h>
</coordinates> </coordinates>
@ -100,8 +100,8 @@ bg=red</panel_attributes>
<element> <element>
<id>UMLState</id> <id>UMLState</id>
<coordinates> <coordinates>
<x>1360</x> <x>1300</x>
<y>530</y> <y>830</y>
<w>70</w> <w>70</w>
<h>40</h> <h>40</h>
</coordinates> </coordinates>
@ -112,8 +112,8 @@ bg=red</panel_attributes>
<element> <element>
<id>UMLObject</id> <id>UMLObject</id>
<coordinates> <coordinates>
<x>760</x> <x>700</x>
<y>190</y> <y>490</y>
<w>110</w> <w>110</w>
<h>40</h> <h>40</h>
</coordinates> </coordinates>
@ -125,8 +125,8 @@ group=1</panel_attributes>
<element> <element>
<id>UMLState</id> <id>UMLState</id>
<coordinates> <coordinates>
<x>910</x> <x>850</x>
<y>380</y> <y>680</y>
<w>70</w> <w>70</w>
<h>40</h> <h>40</h>
</coordinates> </coordinates>
@ -136,8 +136,8 @@ group=1</panel_attributes>
<element> <element>
<id>UMLState</id> <id>UMLState</id>
<coordinates> <coordinates>
<x>1000</x> <x>940</x>
<y>380</y> <y>680</y>
<w>70</w> <w>70</w>
<h>40</h> <h>40</h>
</coordinates> </coordinates>
@ -147,8 +147,8 @@ group=1</panel_attributes>
<element> <element>
<id>UMLState</id> <id>UMLState</id>
<coordinates> <coordinates>
<x>1090</x> <x>1030</x>
<y>420</y> <y>720</y>
<w>70</w> <w>70</w>
<h>40</h> <h>40</h>
</coordinates> </coordinates>
@ -159,8 +159,8 @@ bg=red</panel_attributes>
<element> <element>
<id>UMLState</id> <id>UMLState</id>
<coordinates> <coordinates>
<x>1090</x> <x>1030</x>
<y>380</y> <y>680</y>
<w>70</w> <w>70</w>
<h>40</h> <h>40</h>
</coordinates> </coordinates>
@ -171,8 +171,8 @@ bg=red</panel_attributes>
<element> <element>
<id>UMLState</id> <id>UMLState</id>
<coordinates> <coordinates>
<x>1180</x> <x>1120</x>
<y>420</y> <y>720</y>
<w>70</w> <w>70</w>
<h>40</h> <h>40</h>
</coordinates> </coordinates>
@ -183,8 +183,8 @@ bg=green</panel_attributes>
<element> <element>
<id>UMLState</id> <id>UMLState</id>
<coordinates> <coordinates>
<x>1180</x> <x>1120</x>
<y>380</y> <y>680</y>
<w>70</w> <w>70</w>
<h>40</h> <h>40</h>
</coordinates> </coordinates>
@ -194,8 +194,8 @@ bg=green</panel_attributes>
<element> <element>
<id>UMLState</id> <id>UMLState</id>
<coordinates> <coordinates>
<x>1270</x> <x>1210</x>
<y>420</y> <y>720</y>
<w>70</w> <w>70</w>
<h>40</h> <h>40</h>
</coordinates> </coordinates>
@ -206,8 +206,8 @@ bg=green</panel_attributes>
<element> <element>
<id>UMLState</id> <id>UMLState</id>
<coordinates> <coordinates>
<x>1270</x> <x>1210</x>
<y>380</y> <y>680</y>
<w>70</w> <w>70</w>
<h>40</h> <h>40</h>
</coordinates> </coordinates>
@ -218,8 +218,8 @@ bg=green</panel_attributes>
<element> <element>
<id>UMLState</id> <id>UMLState</id>
<coordinates> <coordinates>
<x>1360</x> <x>1300</x>
<y>420</y> <y>720</y>
<w>70</w> <w>70</w>
<h>40</h> <h>40</h>
</coordinates> </coordinates>
@ -230,8 +230,8 @@ bg=yellow</panel_attributes>
<element> <element>
<id>UMLState</id> <id>UMLState</id>
<coordinates> <coordinates>
<x>1360</x> <x>1300</x>
<y>380</y> <y>680</y>
<w>70</w> <w>70</w>
<h>40</h> <h>40</h>
</coordinates> </coordinates>
@ -241,8 +241,8 @@ bg=yellow</panel_attributes>
<element> <element>
<id>UMLState</id> <id>UMLState</id>
<coordinates> <coordinates>
<x>1450</x> <x>1390</x>
<y>420</y> <y>720</y>
<w>70</w> <w>70</w>
<h>40</h> <h>40</h>
</coordinates> </coordinates>
@ -253,8 +253,8 @@ bg=yellow</panel_attributes>
<element> <element>
<id>UMLState</id> <id>UMLState</id>
<coordinates> <coordinates>
<x>1450</x> <x>1390</x>
<y>380</y> <y>680</y>
<w>70</w> <w>70</w>
<h>40</h> <h>40</h>
</coordinates> </coordinates>
@ -265,8 +265,8 @@ bg=yellow</panel_attributes>
<element> <element>
<id>UMLState</id> <id>UMLState</id>
<coordinates> <coordinates>
<x>1540</x> <x>1480</x>
<y>380</y> <y>680</y>
<w>70</w> <w>70</w>
<h>40</h> <h>40</h>
</coordinates> </coordinates>
@ -277,8 +277,8 @@ bg=red</panel_attributes>
<element> <element>
<id>UMLState</id> <id>UMLState</id>
<coordinates> <coordinates>
<x>1540</x> <x>1480</x>
<y>420</y> <y>720</y>
<w>70</w> <w>70</w>
<h>40</h> <h>40</h>
</coordinates> </coordinates>
@ -289,8 +289,8 @@ bg=green</panel_attributes>
<element> <element>
<id>UMLState</id> <id>UMLState</id>
<coordinates> <coordinates>
<x>1630</x> <x>1570</x>
<y>380</y> <y>680</y>
<w>70</w> <w>70</w>
<h>40</h> <h>40</h>
</coordinates> </coordinates>
@ -301,8 +301,8 @@ bg=red</panel_attributes>
<element> <element>
<id>UMLState</id> <id>UMLState</id>
<coordinates> <coordinates>
<x>1630</x> <x>1570</x>
<y>420</y> <y>720</y>
<w>70</w> <w>70</w>
<h>40</h> <h>40</h>
</coordinates> </coordinates>
@ -313,8 +313,8 @@ bg=yellow</panel_attributes>
<element> <element>
<id>UMLState</id> <id>UMLState</id>
<coordinates> <coordinates>
<x>1720</x> <x>1660</x>
<y>380</y> <y>680</y>
<w>70</w> <w>70</w>
<h>40</h> <h>40</h>
</coordinates> </coordinates>
@ -325,8 +325,8 @@ bg=green</panel_attributes>
<element> <element>
<id>UMLState</id> <id>UMLState</id>
<coordinates> <coordinates>
<x>1720</x> <x>1660</x>
<y>420</y> <y>720</y>
<w>70</w> <w>70</w>
<h>40</h> <h>40</h>
</coordinates> </coordinates>
@ -337,8 +337,8 @@ bg=yellow</panel_attributes>
<element> <element>
<id>UMLNote</id> <id>UMLNote</id>
<coordinates> <coordinates>
<x>890</x> <x>830</x>
<y>190</y> <y>490</y>
<w>150</w> <w>150</w>
<h>80</h> <h>80</h>
</coordinates> </coordinates>
@ -351,8 +351,8 @@ bg=white</panel_attributes>
<element> <element>
<id>UMLObject</id> <id>UMLObject</id>
<coordinates> <coordinates>
<x>760</x> <x>700</x>
<y>530</y> <y>830</y>
<w>110</w> <w>110</w>
<h>40</h> <h>40</h>
</coordinates> </coordinates>
@ -362,20 +362,20 @@ bg=white</panel_attributes>
<element> <element>
<id>UMLState</id> <id>UMLState</id>
<coordinates> <coordinates>
<x>1090</x> <x>940</x>
<y>530</y> <y>830</y>
<w>70</w> <w>70</w>
<h>40</h> <h>40</h>
</coordinates> </coordinates>
<panel_attributes>MAYBE <panel_attributes>YES
bg=yellow</panel_attributes> bg=green</panel_attributes>
<additional_attributes/> <additional_attributes/>
</element> </element>
<element> <element>
<id>UMLState</id> <id>UMLState</id>
<coordinates> <coordinates>
<x>1000</x> <x>1120</x>
<y>530</y> <y>830</y>
<w>70</w> <w>70</w>
<h>40</h> <h>40</h>
</coordinates> </coordinates>
@ -386,20 +386,20 @@ bg=green</panel_attributes>
<element> <element>
<id>UMLState</id> <id>UMLState</id>
<coordinates> <coordinates>
<x>1180</x> <x>1390</x>
<y>530</y> <y>830</y>
<w>70</w> <w>70</w>
<h>40</h> <h>40</h>
</coordinates> </coordinates>
<panel_attributes>YES <panel_attributes>NO
bg=green</panel_attributes> bg=red</panel_attributes>
<additional_attributes/> <additional_attributes/>
</element> </element>
<element> <element>
<id>UMLState</id> <id>UMLState</id>
<coordinates> <coordinates>
<x>1450</x> <x>1570</x>
<y>530</y> <y>830</y>
<w>70</w> <w>70</w>
<h>40</h> <h>40</h>
</coordinates> </coordinates>
@ -410,8 +410,8 @@ bg=red</panel_attributes>
<element> <element>
<id>UMLState</id> <id>UMLState</id>
<coordinates> <coordinates>
<x>1630</x> <x>1660</x>
<y>530</y> <y>830</y>
<w>70</w> <w>70</w>
<h>40</h> <h>40</h>
</coordinates> </coordinates>
@ -422,8 +422,8 @@ bg=red</panel_attributes>
<element> <element>
<id>UMLState</id> <id>UMLState</id>
<coordinates> <coordinates>
<x>1720</x> <x>1210</x>
<y>530</y> <y>830</y>
<w>70</w> <w>70</w>
<h>40</h> <h>40</h>
</coordinates> </coordinates>
@ -434,8 +434,8 @@ bg=red</panel_attributes>
<element> <element>
<id>UMLState</id> <id>UMLState</id>
<coordinates> <coordinates>
<x>1270</x> <x>1480</x>
<y>530</y> <y>830</y>
<w>70</w> <w>70</w>
<h>40</h> <h>40</h>
</coordinates> </coordinates>
@ -446,8 +446,8 @@ bg=red</panel_attributes>
<element> <element>
<id>UMLState</id> <id>UMLState</id>
<coordinates> <coordinates>
<x>1540</x> <x>1030</x>
<y>530</y> <y>830</y>
<w>70</w> <w>70</w>
<h>40</h> <h>40</h>
</coordinates> </coordinates>

Loading…
Cancel
Save