_ fix StateFile path

_ clear lists instead of returning them
pull/1/head
io42630 3 years ago
parent 661d7d0c35
commit 0da4772388

@ -45,10 +45,10 @@ public class Flow implements Runnable {
*/ */
private void doSyncDirectory(SyncDirectory sd) { private void doSyncDirectory(SyncDirectory sd) {
LOGGER.info("READ"); LOGGER.info("READ");
sd.readStateFromFS(); sd.readFileSystem();
sd.makeListOfLocallyCreatedFiles(); sd.fillListOfLocallyCreatedFiles();
sd.makeListOfLocallyDeletedFiles(); sd.makeListOfLocallyDeletedFiles();
sd.makeListOfLocallyModifiedFiles(); sd.makeListOfLocallyModifiedFiles();

@ -5,7 +5,7 @@ import java.io.File
class StateFile(val targetPath: String) { class StateFile(val targetPath: String) {
fun getPath(): String { fun getPath(): String {
return targetPath + Constants.STATE_FILE_NAME return targetPath + "/" + Constants.STATE_FILE_NAME
} }
private fun getFile(): File { private fun getFile(): File {
@ -13,7 +13,7 @@ class StateFile(val targetPath: String) {
} }
fun exists(): Boolean { fun exists(): Boolean {
return getFile().exists(); return getFile().exists()
} }
} }

@ -50,16 +50,16 @@ public class SyncDirectory {
/** /**
* Get the current state by using the `find` command. * Read the current state of the file system.
*/ */
public Map<String, SyncFile> readStateFromFS() { public Map<String, SyncFile> readFileSystem() {
//NOTE that the SyncFile().lastModifiedOld is not set here, so it is 0 by default. //NOTE that the SyncFile().lastModifiedOld is not set here, so it is 0 by default.
Map<String, SyncFile> filemap = new HashMap<>(); return getFiles()
getFiles()
.map(file -> new SyncFile(this, file.getAbsolutePath())) .map(file -> new SyncFile(this, file.getAbsolutePath()))
.forEach(file -> filemap.put(file.getAbsolutePath(), file) .collect(Collectors.toMap(
); SyncFile::getRelativePath,
return filemap; syncFile -> syncFile
));
} }
@ -91,12 +91,11 @@ 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 Map<String, SyncFile> makeListOfLocallyCreatedFiles() { public void fillListOfLocallyCreatedFiles() {
listCreated.clear();
Map<String, SyncFile> fromA = readStateFromFS(); var fromA = readFileSystem();
Map<String, SyncFile> substractB = readStateFile(); var substractB = readStateFile();
listCreated.putAll(tools.mapMinus(fromA, substractB));
return tools.mapMinus(fromA, substractB);
} }
@ -104,27 +103,20 @@ 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 Map<String, SyncFile> makeListOfLocallyDeletedFiles() { public void makeListOfLocallyDeletedFiles() {
listDeleted.clear();
var fromA = readStateFile(); var fromA = readStateFile();
var substractB = readStateFromFS(); var substractB = readFileSystem();
var listDeleted = tools.mapMinus(fromA, substractB); var listDeleted = tools.mapMinus(fromA, substractB);
Map<String, SyncFile> swap = new HashMap<>(); Map<String, SyncFile> swap = new HashMap<>();
for (var entry : listDeleted.entrySet()) { for (var entry : listDeleted.entrySet()) {
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.putAll(tools.mapMinus(listDeleted, swap));
return tools.mapMinus(listDeleted, swap);
} }
@ -133,13 +125,11 @@ public class SyncDirectory {
* List is cleared and created each time. * List is cleared and created each time.
*/ */
public void makeListOfLocallyModifiedFiles() { public void makeListOfLocallyModifiedFiles() {
listModified.clear();
Map<String, SyncFile> listModified = new HashMap<>();
Map<String, SyncFile> stateFileMap = readStateFile(); Map<String, SyncFile> stateFileMap = readStateFile();
for (var freshFileEntry : readStateFromFS().entrySet()) { for (var freshFileEntry : readFileSystem().entrySet()) {
String freshFileKey = freshFileEntry.getKey(); String freshFileKey = freshFileEntry.getKey();
SyncFile freshFile = freshFileEntry.getValue(); SyncFile freshFile = freshFileEntry.getValue();
@ -156,7 +146,6 @@ public class SyncDirectory {
} }
} }
/** /**
* QUERY state of the filesystem at realPath. * QUERY state of the filesystem at realPath.
* WRITE the state of the filesystem to file. * WRITE the state of the filesystem to file.
@ -241,6 +230,7 @@ public class SyncDirectory {
Path.of(thisFile.getPath()), Path.of(thisFile.getPath()),
Path.of(otherFile.getPath()) Path.of(otherFile.getPath())
); );
otherFile.setLastModified(thisFile.lastModified());
} catch (IOException e) { } catch (IOException e) {
LOGGER.severe("Could not copy file."); LOGGER.severe("Could not copy file.");
} }

@ -1,32 +1,42 @@
package com.olexyn.ensync.artifacts; package com.olexyn.ensync.artifacts;
import com.olexyn.ensync.LogUtil;
import java.io.File; import java.io.File;
import java.util.logging.Logger;
public class SyncFile extends File { public class SyncFile extends File {
private static final Logger LOGGER = LogUtil.get(SyncFile.class);
// 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.
public long timeModifiedFromStateFile = 0; public long timeModifiedFromStateFile = 0;
public String relativePath; private String relativePath;
private final SyncDirectory sd; private final SyncDirectory sd;
public SyncFile(SyncDirectory sd, String pathname) { public SyncFile(SyncDirectory sd, String absolutePath) {
super(pathname); super(absolutePath);
this.sd = sd; this.sd = sd;
relativePath = this.getPath().replace(sd.directoryPath, ""); relativePath = this.getPath().replace(sd.directoryPath, "");
} }
public String getRelativePath() {
return relativePath;
}
public void setTimeModifiedFromStateFile(long value) { public void setTimeModifiedFromStateFile(long value) {
timeModifiedFromStateFile = value; timeModifiedFromStateFile = value;
} }
public long getTimeModifiedFromStateFile() { public long getTimeModifiedFromStateFile() {
SyncFile record = sd.readStateFile().get(this.getPath()); SyncFile record = sd.readStateFile().get(getRelativePath());
if (record == null) {
LOGGER.severe("Did not find record for file in StateFile. Setting modifiedDate to MIN, thus 0");
return record == null ? 0 : record.timeModifiedFromStateFile; return 0;
}
return record.timeModifiedFromStateFile;
} }
/** /**

Loading…
Cancel
Save