+ change modif-date of parent folder on write.

pull/1/head
Ivan Olexyn 5 years ago
parent 3c19f1cf97
commit cfa62d0179

@ -34,8 +34,8 @@ public class Flow implements Runnable {
state = "READ"; state = "READ";
SD.readFreshState(); SD.readFreshState();
SD.makeListCreated(); SD.listCreated = SD.makeListCreated();
SD.makeListDeleted(); SD.listDeleted = SD.makeListDeleted();
SD.listModified = SD.makeListModified(); SD.listModified = SD.makeListModified();
SD.doCreate(); SD.doCreate();

@ -95,14 +95,13 @@ public class SyncDirectory {
/** /**
* Compare the OLD and NEW pools. * Compare the OLD and NEW pools.
* List is cleared and created each time. * List is cleared and created each time.
*
* @return
*/ */
public void makeListCreated() { public Map<String, SyncFile> makeListCreated() {
Map<String, SyncFile> fromA = readFreshState(); Map<String, SyncFile> fromA = readFreshState();
Map<String, SyncFile> substractB = readStateFile(); Map<String, SyncFile> substractB = readStateFile();
listCreated = tools.mapMinus(fromA, substractB); return tools.mapMinus(fromA, substractB);
} }
@ -110,11 +109,12 @@ public class SyncDirectory {
* Compare the OLD and NEW pools. * Compare the OLD and NEW pools.
* List is cleared and created each time. * List is cleared and created each time.
*/ */
public void makeListDeleted() { public Map<String, SyncFile> makeListDeleted() {
Map<String, SyncFile> fromA = readStateFile(); Map<String, SyncFile> fromA = readStateFile();
Map<String, SyncFile> substractB = readFreshState(); Map<String, SyncFile> substractB = readFreshState();
listDeleted = tools.mapMinus(fromA, substractB); Map<String, SyncFile> listDeleted = tools.mapMinus(fromA, substractB);
Map<String, SyncFile> swap = new HashMap<>(); Map<String, SyncFile> swap = new HashMap<>();
@ -124,15 +124,12 @@ public class SyncDirectory {
String key = entry.getKey(); String key = entry.getKey();
String parentKey = entry.getValue().getParent(); String parentKey = entry.getValue().getParent();
if (listDeleted.containsKey(parentKey) || swap.containsKey(parentKey)) { if (listDeleted.containsKey(parentKey) || swap.containsKey(parentKey)) {
swap.put(key, listDeleted.get(key)); swap.put(key, listDeleted.get(key));
} }
} }
listDeleted = tools.mapMinus(listDeleted, swap); return tools.mapMinus(listDeleted, swap);
} }
@ -142,7 +139,8 @@ public class SyncDirectory {
*/ */
public Map<String, SyncFile> makeListModified() { public Map<String, SyncFile> makeListModified() {
listModified.clear(); Map<String, SyncFile> listModified = new HashMap<>();
Map<String, SyncFile> stateFileMap = readStateFile(); Map<String, SyncFile> stateFileMap = readStateFile();
for (var freshFileEntry : readFreshState().entrySet()) { for (var freshFileEntry : readFreshState().entrySet()) {
@ -153,13 +151,13 @@ public class SyncDirectory {
if (freshFile.isDirectory()) { continue;} // no need to modify Directories, the Filesystem will do that, if a File changed. if (freshFile.isDirectory()) { continue;} // no need to modify Directories, the Filesystem will do that, if a File changed.
// If KEY exists in OLD , thus FILE was NOT created. // If KEY exists in OLD , thus FILE was NOT created.
if (stateFileMap.containsKey(freshFileKey)) { boolean oldFileExists = stateFileMap.containsKey(freshFileKey);
boolean fileIsFresher = freshFile.getTimeModified() > freshFile.getTimeModifiedFromStateFile();
if (freshFile.getTimeModified() > freshFile.getTimeModifiedFromStateFile()) { if (oldFileExists && fileIsFresher) {
listModified.put(freshFileKey, freshFile); listModified.put(freshFileKey, freshFile);
} }
} }
}
return listModified; return listModified;
} }
@ -257,7 +255,12 @@ public class SyncDirectory {
SyncFile otherFile = new SyncFile(otherSD, otherSD.path + thisFile.relativePath); SyncFile otherFile = new SyncFile(otherSD, otherSD.path + thisFile.relativePath);
if (!otherFile.exists()) { return;}
// if the otherFile was created with ensync it will have the == TimeModified. // if the otherFile was created with ensync it will have the == TimeModified.
long thisFileTimeModified = thisFile.getTimeModified();
long otherFileTimeModified = otherFile.getTimeModified();
if (thisFile.getTimeModified() >= otherFile.getTimeModified()) { if (thisFile.getTimeModified() >= otherFile.getTimeModified()) {
List<String> cmd = List.of("rm", "-r", info.otherFilePath); List<String> cmd = List.of("rm", "-r", info.otherFilePath);
x.execute(cmd); x.execute(cmd);
@ -299,17 +302,61 @@ public class SyncDirectory {
if (thisFile.isFile()) { if (thisFile.isFile()) {
if (!info.otherParentFile.exists()) { if (!info.otherParentFile.exists()) {
List<String> cmd = List.of("mkdir", "-p", info.otherParentPath); makeParentChain(otherFile, thisFile);
x.execute(cmd); // List<String> cmd = List.of("mkdir", "-p", info.otherParentPath);
//x.execute(cmd);
} }
List<String> cmd = List.of("cp", "-p", info.thisFilePath, info.otherFilePath); List<String> cmd = List.of("cp", "-p", info.thisFilePath, info.otherFilePath);
x.execute(cmd); x.execute(cmd);
copyModifDate(thisFile.getParentFile(), otherFile.getParentFile());
}
}
private void makeParentChain(File otherFile, File thisFile) {
try {
File otherParent = new File(otherFile.getParent());
File thisParent = new File(thisFile.getParent());
if (!otherParent.exists()) {
makeParentChain(otherParent, thisParent);
makeParentChain(otherFile, thisFile);
} else if (thisFile.isDirectory()) {
List<String> cmd = List.of("mkdir", otherFile.getPath());
x.execute(cmd);
cmd = List.of("stat", "--format", "%y", thisFile.getPath());
String mDate = x.execute(cmd).output.readLine();
cmd = List.of("touch", "-m", "--date=" + mDate, otherFile.getPath());
String error = x.execute(cmd).error.readLine();
int br = 0;
} }
} catch (Exception ignored) {}
} }
private void copyModifDate(File fromFile, File toFile) {
try {
List<String> cmd = List.of("stat", "--format", "%y", fromFile.getPath());
String mDate = x.execute(cmd).output.readLine();
cmd = List.of("touch", "-m", "--date=" + mDate, toFile.getPath());
x.execute(cmd);
} catch (Exception ignored) {}
}
} }

@ -1,12 +1,13 @@
package com.olexyn.ensync.artifacts; package com.olexyn.ensync.artifacts;
import java.io.File; import java.io.File;
import java.util.Map;
public class SyncFile extends File { public class SyncFile extends File {
// Very IMPORTANT field. Allows to store lastModified as it is stored in the StateFile. // Very IMPORTANT field. Allows to store lastModified as it is stored in the StateFile.
private long timeModifiedFromStateFile = 0; public long timeModifiedFromStateFile = 0;
public String relativePath; public String relativePath;
private SyncDirectory sd; private SyncDirectory sd;
@ -30,7 +31,10 @@ public class SyncFile extends File {
public long getTimeModifiedFromStateFile(){ public long getTimeModifiedFromStateFile(){
return sd.readStateFile().get(this.getPath()).timeModifiedFromStateFile; SyncFile record = sd.readStateFile().get(this.getPath());
return record == null ? 0 : record.timeModifiedFromStateFile;
} }

Loading…
Cancel
Save