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)
- 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.

@ -55,22 +55,27 @@ public class Flow {
syncDirectory.readState(path);
List<SyncFile> listCreated = syncDirectory.makeListCreated(path);
List<SyncFile> listDeleted = syncDirectory.makeListDeleted(path);
List<SyncFile> 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) {
}
}}
}
}
}

@ -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<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 {
BufferedWriter bw = new BufferedWriter(new FileWriter(new File(path)));
StringBuilder sb = stringListToSb(list);

@ -13,9 +13,11 @@ public class SyncDirectory {
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();
@ -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<String> 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<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.
* List is cleared and created each time.
*
* @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.
* List is cleared and created each time.
*
* @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);
for (Map.Entry<String, SyncFile> 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<String> outputList = new ArrayList<>();
Execute.TwoBr find = x.execute(new String[]{"find",
path});
@ -164,7 +178,7 @@ public class SyncDirectory {
}
public void doCreate(List<SyncFile> listCreated){
public void doCreate() {
for (File createdFile : listCreated) {
@ -198,8 +212,7 @@ public class SyncDirectory {
}
public void doDelete(List<SyncFile> listDeleted){
public void doDelete() {
for (File deletedFile : listDeleted) {
@ -220,13 +233,53 @@ 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>
<id>UMLObject</id>
<coordinates>
<x>760</x>
<y>420</y>
<x>700</x>
<y>720</y>
<w>110</w>
<h>40</h>
</coordinates>
@ -15,8 +15,8 @@
<element>
<id>UMLObject</id>
<coordinates>
<x>760</x>
<y>380</y>
<x>700</x>
<y>680</y>
<w>110</w>
<h>40</h>
</coordinates>
@ -26,8 +26,8 @@
<element>
<id>UMLObject</id>
<coordinates>
<x>760</x>
<y>230</y>
<x>700</x>
<y>530</y>
<w>110</w>
<h>40</h>
</coordinates>
@ -39,8 +39,8 @@ group=1</panel_attributes>
<element>
<id>UMLObject</id>
<coordinates>
<x>760</x>
<y>270</y>
<x>700</x>
<y>570</y>
<w>110</w>
<h>40</h>
</coordinates>
@ -52,8 +52,8 @@ group=1</panel_attributes>
<element>
<id>UMLObject</id>
<coordinates>
<x>760</x>
<y>310</y>
<x>700</x>
<y>610</y>
<w>110</w>
<h>40</h>
</coordinates>
@ -65,8 +65,8 @@ group=1</panel_attributes>
<element>
<id>UMLState</id>
<coordinates>
<x>910</x>
<y>530</y>
<x>850</x>
<y>830</y>
<w>70</w>
<h>40</h>
</coordinates>
@ -77,8 +77,8 @@ bg=green</panel_attributes>
<element>
<id>UMLState</id>
<coordinates>
<x>910</x>
<y>420</y>
<x>850</x>
<y>720</y>
<w>70</w>
<h>40</h>
</coordinates>
@ -88,8 +88,8 @@ bg=green</panel_attributes>
<element>
<id>UMLState</id>
<coordinates>
<x>1000</x>
<y>420</y>
<x>940</x>
<y>720</y>
<w>70</w>
<h>40</h>
</coordinates>
@ -100,8 +100,8 @@ bg=red</panel_attributes>
<element>
<id>UMLState</id>
<coordinates>
<x>1360</x>
<y>530</y>
<x>1300</x>
<y>830</y>
<w>70</w>
<h>40</h>
</coordinates>
@ -112,8 +112,8 @@ bg=red</panel_attributes>
<element>
<id>UMLObject</id>
<coordinates>
<x>760</x>
<y>190</y>
<x>700</x>
<y>490</y>
<w>110</w>
<h>40</h>
</coordinates>
@ -125,8 +125,8 @@ group=1</panel_attributes>
<element>
<id>UMLState</id>
<coordinates>
<x>910</x>
<y>380</y>
<x>850</x>
<y>680</y>
<w>70</w>
<h>40</h>
</coordinates>
@ -136,8 +136,8 @@ group=1</panel_attributes>
<element>
<id>UMLState</id>
<coordinates>
<x>1000</x>
<y>380</y>
<x>940</x>
<y>680</y>
<w>70</w>
<h>40</h>
</coordinates>
@ -147,8 +147,8 @@ group=1</panel_attributes>
<element>
<id>UMLState</id>
<coordinates>
<x>1090</x>
<y>420</y>
<x>1030</x>
<y>720</y>
<w>70</w>
<h>40</h>
</coordinates>
@ -159,8 +159,8 @@ bg=red</panel_attributes>
<element>
<id>UMLState</id>
<coordinates>
<x>1090</x>
<y>380</y>
<x>1030</x>
<y>680</y>
<w>70</w>
<h>40</h>
</coordinates>
@ -171,8 +171,8 @@ bg=red</panel_attributes>
<element>
<id>UMLState</id>
<coordinates>
<x>1180</x>
<y>420</y>
<x>1120</x>
<y>720</y>
<w>70</w>
<h>40</h>
</coordinates>
@ -183,8 +183,8 @@ bg=green</panel_attributes>
<element>
<id>UMLState</id>
<coordinates>
<x>1180</x>
<y>380</y>
<x>1120</x>
<y>680</y>
<w>70</w>
<h>40</h>
</coordinates>
@ -194,8 +194,8 @@ bg=green</panel_attributes>
<element>
<id>UMLState</id>
<coordinates>
<x>1270</x>
<y>420</y>
<x>1210</x>
<y>720</y>
<w>70</w>
<h>40</h>
</coordinates>
@ -206,8 +206,8 @@ bg=green</panel_attributes>
<element>
<id>UMLState</id>
<coordinates>
<x>1270</x>
<y>380</y>
<x>1210</x>
<y>680</y>
<w>70</w>
<h>40</h>
</coordinates>
@ -218,8 +218,8 @@ bg=green</panel_attributes>
<element>
<id>UMLState</id>
<coordinates>
<x>1360</x>
<y>420</y>
<x>1300</x>
<y>720</y>
<w>70</w>
<h>40</h>
</coordinates>
@ -230,8 +230,8 @@ bg=yellow</panel_attributes>
<element>
<id>UMLState</id>
<coordinates>
<x>1360</x>
<y>380</y>
<x>1300</x>
<y>680</y>
<w>70</w>
<h>40</h>
</coordinates>
@ -241,8 +241,8 @@ bg=yellow</panel_attributes>
<element>
<id>UMLState</id>
<coordinates>
<x>1450</x>
<y>420</y>
<x>1390</x>
<y>720</y>
<w>70</w>
<h>40</h>
</coordinates>
@ -253,8 +253,8 @@ bg=yellow</panel_attributes>
<element>
<id>UMLState</id>
<coordinates>
<x>1450</x>
<y>380</y>
<x>1390</x>
<y>680</y>
<w>70</w>
<h>40</h>
</coordinates>
@ -265,8 +265,8 @@ bg=yellow</panel_attributes>
<element>
<id>UMLState</id>
<coordinates>
<x>1540</x>
<y>380</y>
<x>1480</x>
<y>680</y>
<w>70</w>
<h>40</h>
</coordinates>
@ -277,8 +277,8 @@ bg=red</panel_attributes>
<element>
<id>UMLState</id>
<coordinates>
<x>1540</x>
<y>420</y>
<x>1480</x>
<y>720</y>
<w>70</w>
<h>40</h>
</coordinates>
@ -289,8 +289,8 @@ bg=green</panel_attributes>
<element>
<id>UMLState</id>
<coordinates>
<x>1630</x>
<y>380</y>
<x>1570</x>
<y>680</y>
<w>70</w>
<h>40</h>
</coordinates>
@ -301,8 +301,8 @@ bg=red</panel_attributes>
<element>
<id>UMLState</id>
<coordinates>
<x>1630</x>
<y>420</y>
<x>1570</x>
<y>720</y>
<w>70</w>
<h>40</h>
</coordinates>
@ -313,8 +313,8 @@ bg=yellow</panel_attributes>
<element>
<id>UMLState</id>
<coordinates>
<x>1720</x>
<y>380</y>
<x>1660</x>
<y>680</y>
<w>70</w>
<h>40</h>
</coordinates>
@ -325,8 +325,8 @@ bg=green</panel_attributes>
<element>
<id>UMLState</id>
<coordinates>
<x>1720</x>
<y>420</y>
<x>1660</x>
<y>720</y>
<w>70</w>
<h>40</h>
</coordinates>
@ -337,8 +337,8 @@ bg=yellow</panel_attributes>
<element>
<id>UMLNote</id>
<coordinates>
<x>890</x>
<y>190</y>
<x>830</x>
<y>490</y>
<w>150</w>
<h>80</h>
</coordinates>
@ -351,8 +351,8 @@ bg=white</panel_attributes>
<element>
<id>UMLObject</id>
<coordinates>
<x>760</x>
<y>530</y>
<x>700</x>
<y>830</y>
<w>110</w>
<h>40</h>
</coordinates>
@ -362,20 +362,20 @@ bg=white</panel_attributes>
<element>
<id>UMLState</id>
<coordinates>
<x>1090</x>
<y>530</y>
<x>940</x>
<y>830</y>
<w>70</w>
<h>40</h>
</coordinates>
<panel_attributes>MAYBE
bg=yellow</panel_attributes>
<panel_attributes>YES
bg=green</panel_attributes>
<additional_attributes/>
</element>
<element>
<id>UMLState</id>
<coordinates>
<x>1000</x>
<y>530</y>
<x>1120</x>
<y>830</y>
<w>70</w>
<h>40</h>
</coordinates>
@ -386,20 +386,20 @@ bg=green</panel_attributes>
<element>
<id>UMLState</id>
<coordinates>
<x>1180</x>
<y>530</y>
<x>1390</x>
<y>830</y>
<w>70</w>
<h>40</h>
</coordinates>
<panel_attributes>YES
bg=green</panel_attributes>
<panel_attributes>NO
bg=red</panel_attributes>
<additional_attributes/>
</element>
<element>
<id>UMLState</id>
<coordinates>
<x>1450</x>
<y>530</y>
<x>1570</x>
<y>830</y>
<w>70</w>
<h>40</h>
</coordinates>
@ -410,8 +410,8 @@ bg=red</panel_attributes>
<element>
<id>UMLState</id>
<coordinates>
<x>1630</x>
<y>530</y>
<x>1660</x>
<y>830</y>
<w>70</w>
<h>40</h>
</coordinates>
@ -422,8 +422,8 @@ bg=red</panel_attributes>
<element>
<id>UMLState</id>
<coordinates>
<x>1720</x>
<y>530</y>
<x>1210</x>
<y>830</y>
<w>70</w>
<h>40</h>
</coordinates>
@ -434,8 +434,8 @@ bg=red</panel_attributes>
<element>
<id>UMLState</id>
<coordinates>
<x>1270</x>
<y>530</y>
<x>1480</x>
<y>830</y>
<w>70</w>
<h>40</h>
</coordinates>
@ -446,8 +446,8 @@ bg=red</panel_attributes>
<element>
<id>UMLState</id>
<coordinates>
<x>1540</x>
<y>530</y>
<x>1030</x>
<y>830</y>
<w>70</w>
<h>40</h>
</coordinates>

Loading…
Cancel
Save