_ fix separator in Record

_ fix Record not using absolute path
_ add ignore mechanism
pull/1/head
io42630 3 years ago
parent 78fa7a5fed
commit 414b9e3727

@ -8,11 +8,14 @@ import org.json.JSONObject;
import java.io.File; import java.io.File;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
public class MainApp { public class MainApp {
final public static Thread FLOW_THREAD = new Thread(new Flow(), "flow"); final public static Thread FLOW_THREAD = new Thread(new Flow(), "flow");
public static List<String> IGNORE = new ArrayList<>();
final private static Tools tools = new Tools(); final private static Tools tools = new Tools();
public static void main(String[] args) throws JSONException { public static void main(String[] args) throws JSONException {
@ -30,6 +33,9 @@ public class MainApp {
DataRoot.get().put(bundleKey, syncBundle); DataRoot.get().put(bundleKey, syncBundle);
} }
String ignorePath = System.getProperty("user.dir") + "/src/main/resources/syncignore";
IGNORE = tools.fileToLines(new File(ignorePath));
FLOW_THREAD.start(); FLOW_THREAD.start();
} }
} }

@ -4,6 +4,8 @@ public interface Constants {
String STATE_FILE_NAME = "record.ensync"; String STATE_FILE_NAME = "record.ensync";
String SPACE = " "; String SPACE = " ";
String SLASH = "/";
String EMPTY = ""; String EMPTY = "";
String RECORD_SEPARATOR = "-_record_-";
} }

@ -1,13 +1,13 @@
package com.olexyn.ensync.artifacts; package com.olexyn.ensync.artifacts;
import com.olexyn.ensync.LogUtil; import com.olexyn.ensync.LogUtil;
import com.olexyn.ensync.MainApp;
import com.olexyn.ensync.Tools; import com.olexyn.ensync.Tools;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.math.BigInteger; import java.math.BigInteger;
import java.nio.file.CopyOption;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
@ -19,12 +19,13 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import static com.olexyn.ensync.artifacts.Constants.EMPTY; import static com.olexyn.ensync.artifacts.Constants.EMPTY;
import static com.olexyn.ensync.artifacts.Constants.SPACE; import static com.olexyn.ensync.artifacts.Constants.RECORD_SEPARATOR;
/** /**
* A SyncDirectory is a singular occurrence of a directory in the filesystems. * A SyncDirectory is a singular occurrence of a directory in the filesystems.
@ -71,15 +72,17 @@ public class SyncDirectory {
List<String> lines = tools.fileToLines(record.getPath().toFile()); List<String> lines = tools.fileToLines(record.getPath().toFile());
for (String line : lines) { for (String line : lines) {
// this is a predefined format: "<modification-time> <relative-path>" // this is a predefined format: "<modification-time>RECORD_SEPARATOR<relative-path>"
var lineArr = line.split(SPACE); var lineArr = line.split(RECORD_SEPARATOR);
long modTime = Long.parseLong(lineArr[0]); long modTime = Long.parseLong(lineArr[0]);
String sFilePath = lineArr[1]; String sFilePath = lineArr[1];
RecordFile sfile = new RecordFile(this, sFilePath); String absolutePath = directoryPath + sFilePath;
RecordFile recordFile = new RecordFile(this, absolutePath);
sfile.setTimeModifiedFromRecord(modTime); recordFile.setTimeModifiedFromRecord(modTime);
if (!shouldIgnore(recordFile.toPath())) {
filemap.put(sFilePath, sfile); filemap.put(sFilePath, recordFile);
}
} }
return filemap; return filemap;
} }
@ -145,21 +148,32 @@ public class SyncDirectory {
String relativePath = file.getAbsolutePath() String relativePath = file.getAbsolutePath()
.replace(record.getTargetPath().toString(), EMPTY); .replace(record.getTargetPath().toString(), EMPTY);
var line = String.join( var line = String.join(
SPACE, RECORD_SEPARATOR,
String.valueOf(file.lastModified()), String.valueOf(file.lastModified()),
relativePath relativePath
); );
outputList.add(line); outputList.add(line);
}); });
LOGGER.info("Writing " + outputList.size() + " files to " + record.getPath()); LOGGER.info("Writing " + outputList.size() + " files to " + record.getPath());
tools.writeStringListToFile(record.getPath().toString(), outputList); tools.writeStringListToFile(record.getPath().toString(), outputList);
} }
private boolean shouldIgnore(Path path) {
for (var entry : MainApp.IGNORE) {
if (path.toString().contains(entry)) {
return true;
}
}
return false;
}
private Stream<File> getFiles() { private Stream<File> getFiles() {
try { try {
return Files.walk(directoryPath) return Files.walk(directoryPath)
.filter(Files::isRegularFile) .filter(Files::isRegularFile)
.map(Path::toFile) .map(Path::toFile)
.filter(file -> !shouldIgnore(file.toPath()))
.filter(file -> !file.getName().equals(Constants.STATE_FILE_NAME)); .filter(file -> !file.getName().equals(Constants.STATE_FILE_NAME));
} catch (IOException e) { } catch (IOException e) {
LOGGER.severe("Could walk the file tree : Record will be empty."); LOGGER.severe("Could walk the file tree : Record will be empty.");

@ -0,0 +1,2 @@
collection\wallpapers
record.ensync
Loading…
Cancel
Save