pull/1/head
Ivan Olexyn 5 years ago
parent 227d861ad0
commit 79979bb41d

@ -29,8 +29,8 @@ public class Execute {
public void executeBatch(List<String[]> batch){
for (int i =0; i<batch.size();i++){
execute(batch.get(i));
for (String[] strings : batch) {
execute(strings);
}
}

@ -9,7 +9,7 @@ import static com.olexyn.ensync.Main.MAP_OF_SYNCMAPS;
public class Flow implements Runnable {
Tools tools = new Tools();
private String state;
@ -27,22 +27,22 @@ public class Flow implements Runnable {
for (var syncMapEntry : MAP_OF_SYNCMAPS.entrySet()) {
for (var syncDirectoryEntry : syncMapEntry.getValue().syncDirectories.entrySet()) {
for (var SDEntry : syncMapEntry.getValue().syncDirectories.entrySet()) {
SyncDirectory syncDirectory = syncDirectoryEntry.getValue();
SyncDirectory SD = SDEntry.getValue();
state = "READ";
syncDirectory.findState();
SD.findState();
syncDirectory.makeListCreated();
syncDirectory.makeListDeleted();
syncDirectory.makeListModified();
SD.makeListCreated();
SD.makeListDeleted();
SD.listModified = SD.makeListModified();
syncDirectory.doCreate();
syncDirectory.doDelete();
syncDirectory.doModify();
SD.doCreate();
SD.doDelete();
SD.doModify();
syncDirectory.writeStateFile(syncDirectory.path);
SD.writeStateFile(SD.path);
}
@ -76,14 +76,14 @@ public class Flow implements Runnable {
state = syncMap.toString();
for (var stringSyncDirectoryEntry : syncMap.syncDirectories.entrySet()) {
SyncDirectory syncDirectory = stringSyncDirectoryEntry.getValue();
String path = syncDirectory.path;
String stateFilePath = syncDirectory.stateFilePath(path);
SyncDirectory SD = stringSyncDirectoryEntry.getValue();
String path = SD.path;
String stateFilePath = tools.stateFilePath(path);
if (new File(stateFilePath).exists()) {
state = "READ-STATE-FILE-" + syncDirectory.readStateFile();
state = "READ-STATE-FILE-" + SD.readStateFile();
} else {
syncDirectory.writeStateFile(path);
SD.writeStateFile(path);
}
}

@ -148,4 +148,7 @@ public class Tools {
}
}
public String stateFilePath(String path) {
return "/tmp/ensync/state" + path.replace("/", "-");
}
}

@ -4,7 +4,10 @@ import com.olexyn.ensync.Execute;
import com.olexyn.ensync.Tools;
import java.io.File;
import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* A SyncDirectory is an occurrence of a particular directory somewhere across the filesystems.
@ -30,8 +33,7 @@ public class SyncDirectory {
*
* @see SyncMap
*/
public SyncDirectory(String path,
SyncMap syncMap) {
public SyncDirectory(String path, SyncMap syncMap) {
this.path = path;
this.syncMap = syncMap;
@ -39,8 +41,6 @@ public class SyncDirectory {
}
/**
* Get the current state by using the `find` command.
*/
@ -54,7 +54,7 @@ public class SyncDirectory {
List<String> 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<String, SyncFile> readStateFile() {
Map<String, SyncFile> filemap = new HashMap<>();
List<String> lines = tools.fileToLines(new File(stateFilePath(path)));
List<String> 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<String, SyncFile> fromA = findState();
Map<String, SyncFile> 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<String, SyncFile> fromA = readStateFile();
Map<String, SyncFile> 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<SyncFile> makeListModified() {
listModified = new ArrayList<>();
Map<String, SyncFile> oldMap = readStateFile();
for (Map.Entry<String, SyncFile> 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<String, SyncDirectory> 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,25 +251,22 @@ public class SyncDirectory {
for (SyncFile deletedFile : listDeleted) {
for (var otherEntry : syncMap.syncDirectories.entrySet()) {
SyncDirectory otherSD = otherEntry.getValue();
for (Map.Entry<String, SyncDirectory> otherEntry : syncMap.syncDirectories.entrySet()) {
SyncDirectory otherSyncDirectory = otherEntry.getValue();
if (this.equals(otherSD)) { continue; }
if (!this.equals(otherSyncDirectory)) {
Info info = new Info(this, deletedFile, otherSyncDirectory);
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) {
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<String, SyncDirectory> 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;
}
}

@ -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;
}
}

@ -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<String, SyncDirectory> syncDirectories = new HashMap<>();
Tools tools = new Tools();

Loading…
Cancel
Save