_ 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) {
LOGGER.info("READ");
sd.readStateFromFS();
sd.readFileSystem();
sd.makeListOfLocallyCreatedFiles();
sd.fillListOfLocallyCreatedFiles();
sd.makeListOfLocallyDeletedFiles();
sd.makeListOfLocallyModifiedFiles();

@ -5,7 +5,7 @@ import java.io.File
class StateFile(val targetPath: String) {
fun getPath(): String {
return targetPath + Constants.STATE_FILE_NAME
return targetPath + "/" + Constants.STATE_FILE_NAME
}
private fun getFile(): File {
@ -13,7 +13,7 @@ class StateFile(val targetPath: String) {
}
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.
Map<String, SyncFile> filemap = new HashMap<>();
getFiles()
return getFiles()
.map(file -> new SyncFile(this, file.getAbsolutePath()))
.forEach(file -> filemap.put(file.getAbsolutePath(), file)
);
return filemap;
.collect(Collectors.toMap(
SyncFile::getRelativePath,
syncFile -> syncFile
));
}
@ -91,12 +91,11 @@ public class SyncDirectory {
* Compare the OLD and NEW pools.
* List is cleared and created each time.
*/
public Map<String, SyncFile> makeListOfLocallyCreatedFiles() {
Map<String, SyncFile> fromA = readStateFromFS();
Map<String, SyncFile> substractB = readStateFile();
return tools.mapMinus(fromA, substractB);
public void fillListOfLocallyCreatedFiles() {
listCreated.clear();
var fromA = readFileSystem();
var substractB = readStateFile();
listCreated.putAll(tools.mapMinus(fromA, substractB));
}
@ -104,27 +103,20 @@ public class SyncDirectory {
* Compare the OLD and NEW pools.
* List is cleared and created each time.
*/
public Map<String, SyncFile> makeListOfLocallyDeletedFiles() {
public void makeListOfLocallyDeletedFiles() {
listDeleted.clear();
var fromA = readStateFile();
var substractB = readStateFromFS();
var substractB = readFileSystem();
var listDeleted = tools.mapMinus(fromA, substractB);
Map<String, SyncFile> swap = new HashMap<>();
for (var entry : listDeleted.entrySet()) {
String key = entry.getKey();
String parentKey = entry.getValue().getParent();
if (listDeleted.containsKey(parentKey) || swap.containsKey(parentKey)) {
swap.put(key, listDeleted.get(key));
}
}
return tools.mapMinus(listDeleted, swap);
listDeleted.putAll(tools.mapMinus(listDeleted, swap));
}
@ -133,13 +125,11 @@ public class SyncDirectory {
* List is cleared and created each time.
*/
public void makeListOfLocallyModifiedFiles() {
Map<String, SyncFile> listModified = new HashMap<>();
listModified.clear();
Map<String, SyncFile> stateFileMap = readStateFile();
for (var freshFileEntry : readStateFromFS().entrySet()) {
for (var freshFileEntry : readFileSystem().entrySet()) {
String freshFileKey = freshFileEntry.getKey();
SyncFile freshFile = freshFileEntry.getValue();
@ -156,7 +146,6 @@ public class SyncDirectory {
}
}
/**
* QUERY state of the filesystem at realPath.
* WRITE the state of the filesystem to file.
@ -241,6 +230,7 @@ public class SyncDirectory {
Path.of(thisFile.getPath()),
Path.of(otherFile.getPath())
);
otherFile.setLastModified(thisFile.lastModified());
} catch (IOException e) {
LOGGER.severe("Could not copy file.");
}

@ -1,32 +1,42 @@
package com.olexyn.ensync.artifacts;
import com.olexyn.ensync.LogUtil;
import java.io.File;
import java.util.logging.Logger;
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.
public long timeModifiedFromStateFile = 0;
public String relativePath;
private String relativePath;
private final SyncDirectory sd;
public SyncFile(SyncDirectory sd, String pathname) {
public SyncFile(SyncDirectory sd, String absolutePath) {
super(pathname);
super(absolutePath);
this.sd = sd;
relativePath = this.getPath().replace(sd.directoryPath, "");
}
public String getRelativePath() {
return relativePath;
}
public void setTimeModifiedFromStateFile(long value) {
timeModifiedFromStateFile = value;
}
public long getTimeModifiedFromStateFile() {
SyncFile record = sd.readStateFile().get(this.getPath());
return record == null ? 0 : record.timeModifiedFromStateFile;
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 0;
}
return record.timeModifiedFromStateFile;
}
/**

Loading…
Cancel
Save